tcmodules.pas 793 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955
  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_ReservedWords;
  312. // anonymous functions
  313. Procedure TestAnonymousProc_Assign_ObjFPC;
  314. Procedure TestAnonymousProc_Assign_Delphi;
  315. Procedure TestAnonymousProc_Arg;
  316. Procedure TestAnonymousProc_Typecast;
  317. Procedure TestAnonymousProc_With;
  318. Procedure TestAnonymousProc_ExceptOn;
  319. Procedure TestAnonymousProc_Nested;
  320. Procedure TestAnonymousProc_NestedAssignResult;
  321. Procedure TestAnonymousProc_Class;
  322. // enums, sets
  323. Procedure TestEnum_Name;
  324. Procedure TestEnum_Number;
  325. Procedure TestEnum_ConstFail;
  326. Procedure TestEnum_Functions;
  327. Procedure TestEnum_AsParams;
  328. Procedure TestEnumRange_Array;
  329. Procedure TestEnum_ForIn;
  330. Procedure TestEnum_ScopedNumber;
  331. Procedure TestEnum_InFunction;
  332. Procedure TestSet_Enum;
  333. Procedure TestSet_Operators;
  334. Procedure TestSet_Operator_In;
  335. Procedure TestSet_Functions;
  336. Procedure TestSet_PassAsArgClone;
  337. Procedure TestSet_AsParams;
  338. Procedure TestSet_Property;
  339. Procedure TestSet_EnumConst;
  340. Procedure TestSet_IntConst;
  341. Procedure TestSet_AnonymousEnumType;
  342. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  343. Procedure TestSet_ConstEnum;
  344. Procedure TestSet_ConstChar;
  345. Procedure TestSet_ConstInt;
  346. Procedure TestSet_InFunction;
  347. Procedure TestSet_ForIn;
  348. // statements
  349. Procedure TestNestBegin;
  350. Procedure TestIncDec;
  351. Procedure TestLoHiFpcMode;
  352. Procedure TestLoHiDelphiMode;
  353. Procedure TestAssignments;
  354. Procedure TestArithmeticOperators1;
  355. Procedure TestLogicalOperators;
  356. Procedure TestBitwiseOperators;
  357. Procedure TestFunctionInt;
  358. Procedure TestFunctionString;
  359. Procedure TestIfThen;
  360. Procedure TestForLoop;
  361. Procedure TestForLoopInsideFunction;
  362. Procedure TestForLoop_ReadVarAfter;
  363. Procedure TestForLoop_Nested;
  364. Procedure TestRepeatUntil;
  365. Procedure TestAsmBlock;
  366. Procedure TestAsmPas_Impl; // ToDo
  367. Procedure TestTryFinally;
  368. Procedure TestTryExcept;
  369. Procedure TestTryExcept_ReservedWords;
  370. Procedure TestIfThenRaiseElse;
  371. Procedure TestCaseOf;
  372. Procedure TestCaseOf_UseSwitch;
  373. Procedure TestCaseOfNoElse;
  374. Procedure TestCaseOfNoElse_UseSwitch;
  375. Procedure TestCaseOfRange;
  376. Procedure TestCaseOfString;
  377. Procedure TestCaseOfChar;
  378. Procedure TestCaseOfExternalClassConst;
  379. Procedure TestDebugger;
  380. // arrays
  381. Procedure TestArray_Dynamic;
  382. Procedure TestArray_Dynamic_Nil;
  383. Procedure TestArray_DynMultiDimensional;
  384. Procedure TestArray_StaticInt;
  385. Procedure TestArray_StaticBool;
  386. Procedure TestArray_StaticChar;
  387. Procedure TestArray_StaticMultiDim;
  388. Procedure TestArray_StaticInFunction;
  389. Procedure TestArrayOfRecord;
  390. Procedure TestArray_StaticRecord;
  391. Procedure TestArrayOfSet;
  392. Procedure TestArray_DynAsParam;
  393. Procedure TestArray_StaticAsParam;
  394. Procedure TestArrayElement_AsParams;
  395. Procedure TestArrayElementFromFuncResult_AsParams;
  396. Procedure TestArrayEnumTypeRange;
  397. Procedure TestArray_SetLengthOutArg;
  398. Procedure TestArray_SetLengthProperty;
  399. Procedure TestArray_SetLengthMultiDim;
  400. Procedure TestArray_OpenArrayOfString;
  401. Procedure TestArray_Concat;
  402. Procedure TestArray_Copy;
  403. Procedure TestArray_InsertDelete;
  404. Procedure TestArray_DynArrayConstObjFPC;
  405. Procedure TestArray_DynArrayConstDelphi;
  406. Procedure TestArray_ArrayLitAsParam;
  407. Procedure TestArray_ArrayLitMultiDimAsParam;
  408. Procedure TestArray_ArrayLitStaticAsParam;
  409. Procedure TestArray_ForInArrOfString;
  410. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  411. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  412. Procedure TestArrayOfConst_TVarRec;
  413. Procedure TestArrayOfConst_PassBaseTypes;
  414. Procedure TestArrayOfConst_PassObj;
  415. // record
  416. Procedure TestRecord_Empty;
  417. Procedure TestRecord_Var;
  418. Procedure TestRecord_VarExternal;
  419. Procedure TestRecord_WithDo;
  420. Procedure TestRecord_Assign;
  421. Procedure TestRecord_AsParams;
  422. Procedure TestRecordElement_AsParams;
  423. Procedure TestRecordElementFromFuncResult_AsParams;
  424. Procedure TestRecordElementFromWith_AsParams;
  425. Procedure TestRecord_Equal;
  426. Procedure TestRecord_TypeCastJSValueToRecord;
  427. Procedure TestRecord_VariantFail;
  428. Procedure TestRecord_FieldArray;
  429. Procedure TestRecord_Const;
  430. Procedure TestRecord_TypecastFail;
  431. Procedure TestRecord_InFunction;
  432. // ToDo: RTTI of local record
  433. // ToDo: pcu local record, name clash and rtti
  434. // advanced record
  435. Procedure TestAdvRecord_Function;
  436. Procedure TestAdvRecord_Property;
  437. Procedure TestAdvRecord_PropertyDefault;
  438. Procedure TestAdvRecord_Property_ClassMethod;
  439. Procedure TestAdvRecord_Const;
  440. Procedure TestAdvRecord_ExternalField;
  441. Procedure TestAdvRecord_SubRecord;
  442. Procedure TestAdvRecord_SubClass;
  443. Procedure TestAdvRecord_SubInterfaceFail;
  444. Procedure TestAdvRecord_Constructor;
  445. Procedure TestAdvRecord_ClassConstructor_Program;
  446. Procedure TestAdvRecord_ClassConstructor_Unit;
  447. // classes
  448. Procedure TestClass_TObjectDefaultConstructor;
  449. Procedure TestClass_TObjectConstructorWithParams;
  450. Procedure TestClass_TObjectConstructorWithDefaultParam;
  451. Procedure TestClass_Var;
  452. Procedure TestClass_Method;
  453. Procedure TestClass_Implementation;
  454. Procedure TestClass_Inheritance;
  455. Procedure TestClass_TypeAlias;
  456. Procedure TestClass_AbstractMethod;
  457. Procedure TestClass_CallInherited_ProcNoParams;
  458. Procedure TestClass_CallInherited_WithParams;
  459. Procedure TestClasS_CallInheritedConstructor;
  460. Procedure TestClass_ClassVar_Assign;
  461. Procedure TestClass_CallClassMethod;
  462. Procedure TestClass_Property;
  463. Procedure TestClass_Property_ClassMethod;
  464. Procedure TestClass_Property_Indexed;
  465. Procedure TestClass_Property_IndexSpec;
  466. Procedure TestClass_PropertyOfTypeArray;
  467. Procedure TestClass_PropertyDefault;
  468. Procedure TestClass_PropertyDefault2;
  469. Procedure TestClass_PropertyOverride;
  470. Procedure TestClass_PropertyIncVisibility;
  471. Procedure TestClass_Assigned;
  472. Procedure TestClass_WithClassDoCreate;
  473. Procedure TestClass_WithClassInstDoProperty;
  474. Procedure TestClass_WithClassInstDoPropertyWithParams;
  475. Procedure TestClass_WithClassInstDoFunc;
  476. Procedure TestClass_TypeCast;
  477. Procedure TestClass_TypeCastUntypedParam;
  478. Procedure TestClass_Overloads;
  479. Procedure TestClass_OverloadsAncestor;
  480. Procedure TestClass_OverloadConstructor;
  481. Procedure TestClass_OverloadDelphiOverride;
  482. Procedure TestClass_ReintroducedVar;
  483. Procedure TestClass_RaiseDescendant;
  484. Procedure TestClass_ExternalMethod;
  485. Procedure TestClass_ExternalVirtualNameMismatchFail;
  486. Procedure TestClass_ExternalOverrideFail;
  487. Procedure TestClass_ExternalVar;
  488. Procedure TestClass_Const;
  489. Procedure TestClass_LocalVarSelfFail;
  490. Procedure TestClass_ArgSelfFail;
  491. Procedure TestClass_NestedProcSelf;
  492. Procedure TestClass_NestedProcSelf2;
  493. Procedure TestClass_NestedProcClassSelf;
  494. Procedure TestClass_NestedProcCallInherited;
  495. Procedure TestClass_TObjectFree;
  496. Procedure TestClass_TObjectFree_VarArg;
  497. Procedure TestClass_TObjectFreeNewInstance;
  498. Procedure TestClass_TObjectFreeLowerCase;
  499. Procedure TestClass_TObjectFreeFunctionFail;
  500. Procedure TestClass_TObjectFreePropertyFail;
  501. Procedure TestClass_ForIn;
  502. Procedure TestClass_Message;
  503. Procedure TestClass_Message_DuplicateIntFail;
  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_Constructor;
  644. Procedure TestTypeHelper_Word;
  645. Procedure TestTypeHelper_Double;
  646. Procedure TestTypeHelper_StringChar;
  647. Procedure TestTypeHelper_Array;
  648. Procedure TestTypeHelper_EnumType;
  649. Procedure TestTypeHelper_SetType;
  650. Procedure TestTypeHelper_InterfaceType;
  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 TestAttributes_Members;
  766. Procedure TestAttributes_Types;
  767. Procedure TestAttributes_HelperConstructor_Fail;
  768. // Assertions, checks
  769. procedure TestAssert;
  770. procedure TestAssert_SysUtils;
  771. procedure TestObjectChecks;
  772. procedure TestOverflowChecks_Int;
  773. procedure TestRangeChecks_AssignInt;
  774. procedure TestRangeChecks_AssignIntRange;
  775. procedure TestRangeChecks_AssignEnum;
  776. procedure TestRangeChecks_AssignEnumRange;
  777. procedure TestRangeChecks_AssignChar;
  778. procedure TestRangeChecks_AssignCharRange;
  779. procedure TestRangeChecks_ArrayIndex;
  780. procedure TestRangeChecks_ArrayOfRecIndex;
  781. procedure TestRangeChecks_StringIndex;
  782. procedure TestRangeChecks_TypecastInt;
  783. procedure TestRangeChecks_TypeHelperInt;
  784. end;
  785. function LinesToStr(Args: array of const): string;
  786. function ExtractFileUnitName(aFilename: string): string;
  787. function JSToStr(El: TJSElement): string;
  788. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  789. implementation
  790. function LinesToStr(Args: array of const): string;
  791. var
  792. s: String;
  793. i: Integer;
  794. begin
  795. s:='';
  796. for i:=Low(Args) to High(Args) do
  797. case Args[i].VType of
  798. vtChar: s += Args[i].VChar+LineEnding;
  799. vtString: s += Args[i].VString^+LineEnding;
  800. vtPChar: s += Args[i].VPChar+LineEnding;
  801. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  802. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  803. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  804. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  805. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  806. end;
  807. Result:=s;
  808. end;
  809. function ExtractFileUnitName(aFilename: string): string;
  810. var
  811. p: Integer;
  812. begin
  813. Result:=ExtractFileName(aFilename);
  814. if Result='' then exit;
  815. for p:=length(Result) downto 1 do
  816. case Result[p] of
  817. '/','\': exit;
  818. '.':
  819. begin
  820. Delete(Result,p,length(Result));
  821. exit;
  822. end;
  823. end;
  824. end;
  825. function JSToStr(El: TJSElement): string;
  826. var
  827. aWriter: TBufferWriter;
  828. aJSWriter: TJSWriter;
  829. begin
  830. aJSWriter:=nil;
  831. aWriter:=TBufferWriter.Create(1000);
  832. try
  833. aJSWriter:=TJSWriter.Create(aWriter);
  834. aJSWriter.IndentSize:=2;
  835. aJSWriter.WriteJS(El);
  836. Result:=aWriter.AsString;
  837. finally
  838. aJSWriter.Free;
  839. aWriter.Free;
  840. end;
  841. end;
  842. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  843. // search diff, ignore changes in spaces
  844. const
  845. SpaceChars = [#9,#10,#13,' '];
  846. var
  847. ExpectedP, ActualP: PChar;
  848. function FindLineEnd(p: PChar): PChar;
  849. begin
  850. Result:=p;
  851. while not (Result^ in [#0,#10,#13]) do inc(Result);
  852. end;
  853. function FindLineStart(p, MinP: PChar): PChar;
  854. begin
  855. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  856. Result:=p;
  857. end;
  858. procedure SkipLineEnd(var p: PChar);
  859. begin
  860. if p^ in [#10,#13] then
  861. begin
  862. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  863. inc(p,2)
  864. else
  865. inc(p);
  866. end;
  867. end;
  868. procedure DiffFound;
  869. var
  870. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  871. ExpLine, ActLine: String;
  872. i, LineNo, DiffLineNo: Integer;
  873. begin
  874. writeln('Diff found "',Msg,'". Lines:');
  875. // write correct lines
  876. p:=PChar(Expected);
  877. LineNo:=0;
  878. DiffLineNo:=0;
  879. repeat
  880. StartPos:=p;
  881. while not (p^ in [#0,#10,#13]) do inc(p);
  882. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  883. SkipLineEnd(p);
  884. inc(LineNo);
  885. if (p<=ExpectedP) and (p^<>#0) then
  886. begin
  887. writeln('= ',ExpLine);
  888. end else begin
  889. // diff line
  890. if DiffLineNo=0 then DiffLineNo:=LineNo;
  891. // write actual line
  892. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  893. ActLineEndP:=FindLineEnd(ActualP);
  894. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  895. writeln('- ',ActLine);
  896. // write expected line
  897. writeln('+ ',ExpLine);
  898. // write empty line with pointer ^
  899. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  900. writeln('^');
  901. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  902. CheckSrcDiff:=false;
  903. // write up to three following actual lines to get some context
  904. for i:=1 to 3 do begin
  905. ActLineStartP:=ActLineEndP;
  906. SkipLineEnd(ActLineStartP);
  907. if ActLineStartP^=#0 then break;
  908. ActLineEndP:=FindLineEnd(ActLineStartP);
  909. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  910. writeln('~ ',ActLine);
  911. end;
  912. exit;
  913. end;
  914. until p^=#0;
  915. writeln('DiffFound Actual:-----------------------');
  916. writeln(Actual);
  917. writeln('DiffFound Expected:---------------------');
  918. writeln(Expected);
  919. writeln('DiffFound ------------------------------');
  920. Msg:='diff found, but lines are the same, internal error';
  921. CheckSrcDiff:=false;
  922. end;
  923. var
  924. IsSpaceNeeded: Boolean;
  925. LastChar, Quote: Char;
  926. begin
  927. Result:=true;
  928. Msg:='';
  929. if Expected='' then Expected:=' ';
  930. if Actual='' then Actual:=' ';
  931. ExpectedP:=PChar(Expected);
  932. ActualP:=PChar(Actual);
  933. repeat
  934. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  935. case ExpectedP^ of
  936. #0:
  937. begin
  938. // check that rest of Actual has only spaces
  939. while ActualP^ in SpaceChars do inc(ActualP);
  940. if ActualP^<>#0 then
  941. begin
  942. DiffFound;
  943. exit;
  944. end;
  945. exit(true);
  946. end;
  947. ' ',#9,#10,#13:
  948. begin
  949. // skip space in Expected
  950. IsSpaceNeeded:=false;
  951. if ExpectedP>PChar(Expected) then
  952. LastChar:=ExpectedP[-1]
  953. else
  954. LastChar:=#0;
  955. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  956. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  957. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  958. IsSpaceNeeded:=true;
  959. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  960. begin
  961. DiffFound;
  962. exit;
  963. end;
  964. while ActualP^ in SpaceChars do inc(ActualP);
  965. end;
  966. '''','"':
  967. begin
  968. while ActualP^ in SpaceChars do inc(ActualP);
  969. if ExpectedP^<>ActualP^ then
  970. begin
  971. DiffFound;
  972. exit;
  973. end;
  974. Quote:=ExpectedP^;
  975. repeat
  976. inc(ExpectedP);
  977. inc(ActualP);
  978. if ExpectedP^<>ActualP^ then
  979. begin
  980. DiffFound;
  981. exit;
  982. end;
  983. if (ExpectedP^ in [#0,#10,#13]) then
  984. break
  985. else if (ExpectedP^=Quote) then
  986. begin
  987. inc(ExpectedP);
  988. inc(ActualP);
  989. break;
  990. end;
  991. until false;
  992. end;
  993. else
  994. while ActualP^ in SpaceChars do inc(ActualP);
  995. if ExpectedP^<>ActualP^ then
  996. begin
  997. DiffFound;
  998. exit;
  999. end;
  1000. inc(ExpectedP);
  1001. inc(ActualP);
  1002. end;
  1003. until false;
  1004. end;
  1005. { TTestEnginePasResolver }
  1006. destructor TTestEnginePasResolver.Destroy;
  1007. begin
  1008. FreeAndNil(FStreamResolver);
  1009. FreeAndNil(FParser);
  1010. FreeAndNil(FScanner);
  1011. FreeAndNil(FStreamResolver);
  1012. if Module<>nil then
  1013. begin
  1014. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1015. FModule:=nil;
  1016. end;
  1017. inherited Destroy;
  1018. end;
  1019. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1020. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1021. begin
  1022. Result:=nil;
  1023. if InFilename<>'' then
  1024. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1025. if Assigned(OnFindUnit) then
  1026. Result:=OnFindUnit(AName);
  1027. if NameExpr=nil then ;
  1028. end;
  1029. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1030. begin
  1031. // do not parse recursively
  1032. // parse via the queue
  1033. if Section=nil then ;
  1034. end;
  1035. { TCustomTestModule }
  1036. function TCustomTestModule.GetMsgCount: integer;
  1037. begin
  1038. Result:=FHintMsgs.Count;
  1039. end;
  1040. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1041. begin
  1042. Result:=TTestHintMessage(FHintMsgs[Index]);
  1043. end;
  1044. function TCustomTestModule.GetResolverCount: integer;
  1045. begin
  1046. Result:=FModules.Count;
  1047. end;
  1048. function TCustomTestModule.GetResolvers(Index: integer
  1049. ): TTestEnginePasResolver;
  1050. begin
  1051. Result:=TTestEnginePasResolver(FModules[Index]);
  1052. end;
  1053. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1054. ): TPasModule;
  1055. var
  1056. DefNamespace: String;
  1057. begin
  1058. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1059. if (Pos('.',aUnitName)<1) then
  1060. begin
  1061. DefNamespace:=GetDefaultNamespace;
  1062. if DefNamespace<>'' then
  1063. begin
  1064. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1065. if Result<>nil then exit;
  1066. end;
  1067. end;
  1068. Result:=LoadUnit(aUnitName);
  1069. if Result<>nil then exit;
  1070. {$IFDEF VerbosePas2JS}
  1071. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1072. {$ENDIF}
  1073. Fail('can''t find unit "'+aUnitName+'"');
  1074. end;
  1075. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1076. var
  1077. aParser: TPasParser;
  1078. Item: TTestHintMessage;
  1079. begin
  1080. aParser:=Sender as TPasParser;
  1081. Item:=TTestHintMessage.Create;
  1082. Item.Id:=aParser.LastMsgNumber;
  1083. Item.MsgType:=aParser.LastMsgType;
  1084. Item.MsgNumber:=aParser.LastMsgNumber;
  1085. Item.Msg:=Msg;
  1086. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1087. {$IFDEF VerbosePas2JS}
  1088. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1089. {$ENDIF}
  1090. FHintMsgs.Add(Item);
  1091. end;
  1092. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1093. );
  1094. var
  1095. aResolver: TTestEnginePasResolver;
  1096. Item: TTestHintMessage;
  1097. begin
  1098. aResolver:=Sender as TTestEnginePasResolver;
  1099. Item:=TTestHintMessage.Create;
  1100. Item.Id:=aResolver.LastMsgId;
  1101. Item.MsgType:=aResolver.LastMsgType;
  1102. Item.MsgNumber:=aResolver.LastMsgNumber;
  1103. Item.Msg:=Msg;
  1104. Item.SourcePos:=aResolver.LastSourcePos;
  1105. {$IFDEF VerbosePas2JS}
  1106. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1107. {$ENDIF}
  1108. FHintMsgs.Add(Item);
  1109. end;
  1110. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1111. var
  1112. Item: TTestHintMessage;
  1113. aScanner: TPas2jsPasScanner;
  1114. begin
  1115. aScanner:=Sender as TPas2jsPasScanner;
  1116. Item:=TTestHintMessage.Create;
  1117. Item.Id:=aScanner.LastMsgNumber;
  1118. Item.MsgType:=aScanner.LastMsgType;
  1119. Item.MsgNumber:=aScanner.LastMsgNumber;
  1120. Item.Msg:=Msg;
  1121. Item.SourcePos:=aScanner.CurSourcePos;
  1122. {$IFDEF VerbosePas2JS}
  1123. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1124. {$ENDIF}
  1125. FHintMsgs.Add(Item);
  1126. end;
  1127. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1128. var
  1129. i: Integer;
  1130. CurEngine: TTestEnginePasResolver;
  1131. CurUnitName: String;
  1132. begin
  1133. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1134. Result:=nil;
  1135. if (Module.ClassType=TPasModule)
  1136. and (CompareText(Module.Name,aUnitName)=0) then
  1137. exit(Module);
  1138. for i:=0 to ResolverCount-1 do
  1139. begin
  1140. CurEngine:=Resolvers[i];
  1141. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1142. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1143. if CompareText(aUnitName,CurUnitName)=0 then
  1144. begin
  1145. Result:=CurEngine.Module;
  1146. if Result<>nil then exit;
  1147. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1148. FileResolver.FindSourceFile(aUnitName);
  1149. CurEngine.StreamResolver:=TStreamResolver.Create;
  1150. CurEngine.StreamResolver.OwnsStreams:=True;
  1151. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1152. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1153. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1154. InitScanner(CurEngine.Scanner);
  1155. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1156. CurEngine.Parser.Options:=po_tcmodules;
  1157. if CompareText(CurUnitName,'System')=0 then
  1158. CurEngine.Parser.ImplicitUses.Clear;
  1159. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1160. try
  1161. CurEngine.Parser.NextToken;
  1162. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1163. except
  1164. on E: Exception do
  1165. HandleException(E);
  1166. end;
  1167. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1168. Result:=CurEngine.Module;
  1169. exit;
  1170. end;
  1171. end;
  1172. end;
  1173. procedure TCustomTestModule.SetUp;
  1174. begin
  1175. {$IFDEF EnablePasTreeGlobalRefCount}
  1176. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1177. {$ENDIF}
  1178. if FModules<>nil then
  1179. begin
  1180. writeln('TCustomTestModule.SetUp FModules<>nil');
  1181. Halt;
  1182. end;
  1183. inherited SetUp;
  1184. FSkipTests:=false;
  1185. FSource:=TStringList.Create;
  1186. FModules:=TObjectList.Create(true);
  1187. FFilename:='test1.pp';
  1188. FFileResolver:=TStreamResolver.Create;
  1189. FFileResolver.OwnsStreams:=True;
  1190. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1191. InitScanner(FScanner);
  1192. FEngine:=AddModule(Filename);
  1193. FEngine.Scanner:=FScanner;
  1194. FScanner.Resolver:=FEngine;
  1195. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1196. FParser.OnLog:=@OnParserLog;
  1197. FEngine.Parser:=FParser;
  1198. Parser.Options:=po_tcmodules;
  1199. FModule:=Nil;
  1200. FConverter:=CreateConverter;
  1201. FExpectedErrorClass:=nil;
  1202. end;
  1203. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1204. begin
  1205. Result:=TPasToJSConverter.Create;
  1206. Result.Options:=co_tcmodules;
  1207. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1208. end;
  1209. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1210. begin
  1211. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1212. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1213. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1214. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1215. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1216. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1217. aScanner.OnLog:=@OnScannerLog;
  1218. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1219. end;
  1220. procedure TCustomTestModule.TearDown;
  1221. {$IFDEF CheckPasTreeRefCount}
  1222. var
  1223. El: TPasElement;
  1224. {$ENDIF}
  1225. var
  1226. i: Integer;
  1227. CurModule: TPasModule;
  1228. begin
  1229. FHintMsgs.Clear;
  1230. FHintMsgsGood.Clear;
  1231. FSkipTests:=false;
  1232. FJSRegModuleCall:=nil;
  1233. FJSModuleCallArgs:=nil;
  1234. FJSImplentationUses:=nil;
  1235. FJSInterfaceUses:=nil;
  1236. FJSModuleSrc:=nil;
  1237. FJSInitBody:=nil;
  1238. FreeAndNil(FJSSource);
  1239. FreeAndNil(FJSModule);
  1240. FreeAndNil(FConverter);
  1241. Engine.Clear;
  1242. FreeAndNil(FSource);
  1243. FreeAndNil(FFileResolver);
  1244. if FModules<>nil then
  1245. begin
  1246. for i:=0 to FModules.Count-1 do
  1247. begin
  1248. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1249. if CurModule=nil then continue;
  1250. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1251. CurModule.ReleaseUsedUnits;
  1252. end;
  1253. if FModule<>nil then
  1254. FModule.ReleaseUsedUnits;
  1255. for i:=0 to FModules.Count-1 do
  1256. begin
  1257. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1258. if CurModule=nil then continue;
  1259. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1260. end;
  1261. FreeAndNil(FModules);
  1262. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1263. FEngine:=nil;
  1264. end;
  1265. inherited TearDown;
  1266. {$IFDEF EnablePasTreeGlobalRefCount}
  1267. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1268. begin
  1269. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1270. {$IFDEF CheckPasTreeRefCount}
  1271. El:=TPasElement.FirstRefEl;
  1272. while El<>nil do
  1273. begin
  1274. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1275. for i:=0 to El.RefIds.Count-1 do
  1276. writeln(' ',El.RefIds[i]);
  1277. El:=El.NextRefEl;
  1278. end;
  1279. {$ENDIF}
  1280. Halt;
  1281. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1282. end;
  1283. {$ENDIF}
  1284. end;
  1285. procedure TCustomTestModule.Add(Line: string);
  1286. begin
  1287. Source.Add(Line);
  1288. end;
  1289. procedure TCustomTestModule.Add(const Lines: array of string);
  1290. var
  1291. i: Integer;
  1292. begin
  1293. for i:=low(Lines) to high(Lines) do
  1294. Add(Lines[i]);
  1295. end;
  1296. procedure TCustomTestModule.StartParsing;
  1297. var
  1298. Src: String;
  1299. begin
  1300. Src:=Source.Text;
  1301. FEngine.Source:=Src;
  1302. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1303. Scanner.OpenFile(FileName);
  1304. Writeln('// Test : ',Self.TestName);
  1305. Writeln(Src);
  1306. end;
  1307. procedure TCustomTestModule.ParseModuleQueue;
  1308. var
  1309. i: Integer;
  1310. CurResolver: TTestEnginePasResolver;
  1311. Found: Boolean;
  1312. Section: TPasSection;
  1313. begin
  1314. // parse til exception or all modules finished
  1315. while not SkipTests do
  1316. begin
  1317. Found:=false;
  1318. for i:=0 to ResolverCount-1 do
  1319. begin
  1320. CurResolver:=Resolvers[i];
  1321. if CurResolver.CurrentParser=nil then continue;
  1322. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1323. continue;
  1324. CurResolver.Parser.ParseContinue;
  1325. Found:=true;
  1326. break;
  1327. end;
  1328. if not Found then break;
  1329. end;
  1330. for i:=0 to ResolverCount-1 do
  1331. begin
  1332. CurResolver:=Resolvers[i];
  1333. if CurResolver.Parser=nil then
  1334. begin
  1335. if CurResolver.CurrentParser<>nil then
  1336. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1337. continue;
  1338. end;
  1339. if CurResolver.Parser.CurModule<>nil then
  1340. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1341. end;
  1342. end;
  1343. procedure TCustomTestModule.ParseModule;
  1344. begin
  1345. if SkipTests then exit;
  1346. FFirstPasStatement:=nil;
  1347. try
  1348. StartParsing;
  1349. Parser.ParseMain(FModule);
  1350. ParseModuleQueue;
  1351. except
  1352. on E: Exception do
  1353. HandleException(E);
  1354. end;
  1355. if SkipTests then exit;
  1356. AssertNotNull('Module resulted in Module',Module);
  1357. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1358. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1359. end;
  1360. procedure TCustomTestModule.ParseProgram;
  1361. begin
  1362. if SkipTests then exit;
  1363. ParseModule;
  1364. if SkipTests then exit;
  1365. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1366. FPasProgram:=TPasProgram(Module);
  1367. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1368. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1369. if (PasProgram.InitializationSection.Elements.Count>0) then
  1370. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1371. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1372. end;
  1373. procedure TCustomTestModule.ParseUnit;
  1374. begin
  1375. if SkipTests then exit;
  1376. ParseModule;
  1377. if SkipTests then exit;
  1378. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1379. AssertNotNull('Has interface section',Module.InterfaceSection);
  1380. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1381. if (Module.InitializationSection<>nil)
  1382. and (Module.InitializationSection.Elements.Count>0)
  1383. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1384. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1385. end;
  1386. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1387. ): TTestEnginePasResolver;
  1388. var
  1389. i: Integer;
  1390. begin
  1391. for i:=0 to ResolverCount-1 do
  1392. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1393. exit(Resolvers[i]);
  1394. Result:=nil;
  1395. end;
  1396. function TCustomTestModule.AddModule(aFilename: string
  1397. ): TTestEnginePasResolver;
  1398. begin
  1399. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1400. if FindModuleWithFilename(aFilename)<>nil then
  1401. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1402. Result:=TTestEnginePasResolver.Create;
  1403. Result.Filename:=aFilename;
  1404. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1405. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1406. Result.OnLog:=@OnPasResolverLog;
  1407. FModules.Add(Result);
  1408. end;
  1409. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1410. ): TTestEnginePasResolver;
  1411. begin
  1412. Result:=AddModule(aFilename);
  1413. Result.Source:=Src;
  1414. end;
  1415. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1416. ImplementationSrc: string): TTestEnginePasResolver;
  1417. var
  1418. Src: String;
  1419. begin
  1420. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1421. Src+=LineEnding;
  1422. Src+='interface'+LineEnding;
  1423. Src+=LineEnding;
  1424. Src+=InterfaceSrc;
  1425. Src+='implementation'+LineEnding;
  1426. Src+=LineEnding;
  1427. Src+=ImplementationSrc;
  1428. Src+='end.'+LineEnding;
  1429. Result:=AddModuleWithSrc(aFilename,Src);
  1430. end;
  1431. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1432. var
  1433. Intf, Impl: TStringList;
  1434. begin
  1435. Intf:=TStringList.Create;
  1436. // interface
  1437. if supTVarRec in Parts then
  1438. Intf.Add('{$modeswitch externalclass}');
  1439. Intf.Add('type');
  1440. Intf.Add(' integer=longint;');
  1441. Intf.Add(' sizeint=nativeint;');
  1442. //'const',
  1443. //' LineEnding = #10;',
  1444. //' DirectorySeparator = ''/'';',
  1445. //' DriveSeparator = '''';',
  1446. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1447. //' AllowDriveSeparators : set of char = [];',
  1448. if supTObject in Parts then
  1449. Intf.AddStrings([
  1450. 'type',
  1451. ' TClass = class of TObject;',
  1452. ' TObject = class',
  1453. ' constructor Create;',
  1454. ' destructor Destroy; virtual;',
  1455. ' class function ClassType: TClass; assembler;',
  1456. ' class function ClassName: String; assembler;',
  1457. ' class function ClassNameIs(const Name: string): boolean;',
  1458. ' class function ClassParent: TClass; assembler;',
  1459. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1460. ' class function UnitName: String; assembler;',
  1461. ' procedure AfterConstruction; virtual;',
  1462. ' procedure BeforeDestruction;virtual;',
  1463. ' function Equals(Obj: TObject): boolean; virtual;',
  1464. ' function ToString: String; virtual;',
  1465. ' end;']);
  1466. if supTVarRec in Parts then
  1467. Intf.AddStrings([
  1468. 'const',
  1469. ' vtInteger = 0;',
  1470. ' vtBoolean = 1;',
  1471. ' vtJSValue = 19;',
  1472. 'type',
  1473. ' PVarRec = ^TVarRec;',
  1474. ' TVarRec = record',
  1475. ' VType : byte;',
  1476. ' VJSValue: JSValue;',
  1477. ' vInteger: longint external name ''VJSValue'';',
  1478. ' vBoolean: boolean external name ''VJSValue'';',
  1479. ' end;',
  1480. ' TVarRecArray = array of TVarRec;',
  1481. 'function VarRecs: TVarRecArray; varargs;',
  1482. '']);
  1483. Intf.Add('var');
  1484. Intf.Add(' ExitCode: Longint = 0;');
  1485. // implementation
  1486. Impl:=TStringList.Create;
  1487. if supTObject in Parts then
  1488. Impl.AddStrings([
  1489. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1490. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1491. 'asm',
  1492. 'end;',
  1493. 'constructor TObject.Create; begin end;',
  1494. 'destructor TObject.Destroy; begin end;',
  1495. 'class function TObject.ClassType: TClass; assembler;',
  1496. 'asm',
  1497. 'end;',
  1498. 'class function TObject.ClassName: String; assembler;',
  1499. 'asm',
  1500. 'end;',
  1501. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1502. 'begin',
  1503. ' Result:=SameText(Name,ClassName);',
  1504. 'end;',
  1505. 'class function TObject.ClassParent: TClass; assembler;',
  1506. 'asm',
  1507. 'end;',
  1508. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1509. 'asm',
  1510. 'end;',
  1511. 'class function TObject.UnitName: String; assembler;',
  1512. 'asm',
  1513. 'end;',
  1514. 'procedure TObject.AfterConstruction; begin end;',
  1515. 'procedure TObject.BeforeDestruction; begin end;',
  1516. 'function TObject.Equals(Obj: TObject): boolean;',
  1517. 'begin',
  1518. ' Result:=Obj=Self;',
  1519. 'end;',
  1520. 'function TObject.ToString: String;',
  1521. 'begin',
  1522. ' Result:=ClassName;',
  1523. 'end;'
  1524. ]);
  1525. if supTVarRec in Parts then
  1526. Impl.AddStrings([
  1527. 'function VarRecs: TVarRecArray; varargs;',
  1528. 'var',
  1529. ' v: PVarRec;',
  1530. 'begin',
  1531. ' v^.VType:=1;',
  1532. ' v^.VJSValue:=2;',
  1533. 'end;',
  1534. '']);
  1535. try
  1536. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1537. finally
  1538. Intf.Free;
  1539. Impl.Free;
  1540. end;
  1541. end;
  1542. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1543. SystemUnitParts: TSystemUnitParts);
  1544. begin
  1545. if NeedSystemUnit then
  1546. AddSystemUnit(SystemUnitParts)
  1547. else
  1548. Parser.ImplicitUses.Clear;
  1549. Add('program '+ExtractFileUnitName(Filename)+';');
  1550. Add('');
  1551. end;
  1552. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1553. SystemUnitParts: TSystemUnitParts);
  1554. begin
  1555. if NeedSystemUnit then
  1556. AddSystemUnit(SystemUnitParts)
  1557. else
  1558. Parser.ImplicitUses.Clear;
  1559. Add('unit Test1;');
  1560. Add('');
  1561. end;
  1562. procedure TCustomTestModule.ConvertModule;
  1563. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1564. out UsesLit: TJSArrayLiteral);
  1565. var
  1566. i: Integer;
  1567. Item: TJSElement;
  1568. Lit: TJSLiteral;
  1569. begin
  1570. UsesLit:=nil;
  1571. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1572. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1573. exit; // null is ok
  1574. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1575. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1576. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1577. begin
  1578. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1579. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1580. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1581. Lit:=TJSLiteral(Item);
  1582. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1583. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1584. end;
  1585. end;
  1586. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1587. out Src: TJSSourceElements);
  1588. var
  1589. FunDecl: TJSFunctionDeclarationStatement;
  1590. FunDef: TJSFuncDef;
  1591. FunBody: TJSFunctionBody;
  1592. begin
  1593. Src:=nil;
  1594. AssertNotNull(ParamName,Arg.Expr);
  1595. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1596. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1597. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1598. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1599. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1600. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1601. AssertNotNull(ParamName+' body',FunDef.Body);
  1602. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1603. FunBody:=FunDef.Body as TJSFunctionBody;
  1604. AssertNotNull(ParamName+' body.A',FunBody.A);
  1605. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1606. Src:=FunBody.A as TJSSourceElements;
  1607. end;
  1608. var
  1609. ModuleNameExpr: TJSLiteral;
  1610. InitFunction: TJSFunctionDeclarationStatement;
  1611. InitAssign: TJSSimpleAssignStatement;
  1612. InitName: String;
  1613. LastNode: TJSElement;
  1614. Arg: TJSArrayLiteralElement;
  1615. begin
  1616. if SkipTests then exit;
  1617. try
  1618. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1619. except
  1620. on E: Exception do
  1621. HandleException(E);
  1622. end;
  1623. if SkipTests then exit;
  1624. if ExpectedErrorClass<>nil then
  1625. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1626. FJSSource:=TStringList.Create;
  1627. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1628. {$IFDEF VerbosePas2JS}
  1629. writeln('TTestModule.ConvertModule JS:');
  1630. write(FJSSource.Text);
  1631. {$ENDIF}
  1632. // rtl.module(...
  1633. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1634. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1635. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1636. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1637. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1638. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1639. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1640. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1641. // parameter 'unitname'
  1642. if JSModuleCallArgs.Elements.Count<1 then
  1643. Fail('rtl.module first param unit missing');
  1644. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1645. AssertNotNull('module name param',Arg.Expr);
  1646. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1647. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1648. if Module is TPasProgram then
  1649. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1650. else
  1651. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1652. // main uses section
  1653. if JSModuleCallArgs.Elements.Count<2 then
  1654. Fail('rtl.module second param main uses missing');
  1655. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1656. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1657. // program/library/interface function()
  1658. if JSModuleCallArgs.Elements.Count<3 then
  1659. Fail('rtl.module third param intf-function missing');
  1660. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1661. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1662. // search for $mod.$init or $mod.$main - the last statement
  1663. if Module is TPasProgram then
  1664. begin
  1665. InitName:='$main';
  1666. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1667. end
  1668. else
  1669. InitName:='$init';
  1670. FJSInitBody:=nil;
  1671. if JSModuleSrc.Statements.Count>0 then
  1672. begin
  1673. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1674. if LastNode is TJSSimpleAssignStatement then
  1675. begin
  1676. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1677. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1678. begin
  1679. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1680. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1681. end
  1682. else if Module is TPasProgram then
  1683. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1684. end;
  1685. end;
  1686. // optional: implementation uses section
  1687. if JSModuleCallArgs.Elements.Count<4 then
  1688. exit;
  1689. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1690. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1691. // optional: implementation function()
  1692. if JSModuleCallArgs.Elements.Count<5 then
  1693. exit;
  1694. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1695. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1696. end;
  1697. procedure TCustomTestModule.ConvertProgram;
  1698. begin
  1699. Add('end.');
  1700. ParseProgram;
  1701. ConvertModule;
  1702. end;
  1703. procedure TCustomTestModule.ConvertUnit;
  1704. begin
  1705. Add('end.');
  1706. ParseUnit;
  1707. ConvertModule;
  1708. end;
  1709. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1710. begin
  1711. Result:=tcmodules.JSToStr(El);
  1712. end;
  1713. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1714. DottedName: string);
  1715. begin
  1716. if DottedName='' then
  1717. begin
  1718. AssertNull(Msg,El);
  1719. end
  1720. else
  1721. begin
  1722. AssertNotNull(Msg,El);
  1723. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1724. end;
  1725. end;
  1726. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1727. begin
  1728. if El=nil then
  1729. Result:=''
  1730. else if El is TJSPrimaryExpressionIdent then
  1731. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1732. else if El is TJSDotMemberExpression then
  1733. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1734. else
  1735. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1736. end;
  1737. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1738. InitStatements: string; ImplStatements: string);
  1739. var
  1740. ActualSrc, ExpectedSrc, InitName: String;
  1741. begin
  1742. ActualSrc:=JSToStr(JSModuleSrc);
  1743. ExpectedSrc:=
  1744. 'var $mod = this;'+LineEnding
  1745. +Statements;
  1746. if coUseStrict in Converter.Options then
  1747. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1748. if Module is TPasProgram then
  1749. InitName:='$main'
  1750. else
  1751. InitName:='$init';
  1752. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1753. ExpectedSrc:=ExpectedSrc+LineEnding
  1754. +'$mod.'+InitName+' = function () {'+LineEnding
  1755. +InitStatements
  1756. +'};'+LineEnding;
  1757. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1758. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1759. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1760. if (JSImplementationSrc<>nil) then
  1761. begin
  1762. ActualSrc:=JSToStr(JSImplementationSrc);
  1763. ExpectedSrc:=
  1764. 'var $mod = this;'+LineEnding
  1765. +'var $impl = $mod.$impl;'+LineEnding
  1766. +ImplStatements;
  1767. end
  1768. else
  1769. begin
  1770. ActualSrc:='';
  1771. ExpectedSrc:=ImplStatements;
  1772. end;
  1773. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1774. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1775. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1776. end;
  1777. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1778. // search diff, ignore changes in spaces
  1779. var
  1780. s: string;
  1781. begin
  1782. if CheckSrcDiff(Expected,Actual,s) then exit;
  1783. Fail(Msg+': '+s);
  1784. end;
  1785. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1786. var
  1787. aResolver: TTestEnginePasResolver;
  1788. aConverter: TPasToJSConverter;
  1789. aJSModule: TJSSourceElements;
  1790. ActualSrc: String;
  1791. begin
  1792. aResolver:=GetResolver(Filename);
  1793. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1794. {$IFDEF VerbosePas2JS}
  1795. writeln('CheckUnit '+Filename+' converting ...');
  1796. {$ENDIF}
  1797. aConverter:=CreateConverter;
  1798. aJSModule:=nil;
  1799. try
  1800. try
  1801. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1802. except
  1803. on E: Exception do
  1804. HandleException(E);
  1805. end;
  1806. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1807. {$IFDEF VerbosePas2JS}
  1808. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1809. write(aResolver.Source);
  1810. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1811. write(ActualSrc);
  1812. {$ENDIF}
  1813. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1814. finally
  1815. aJSModule.Free;
  1816. aConverter.Free;
  1817. end;
  1818. end;
  1819. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1820. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1821. var
  1822. i: Integer;
  1823. Item: TTestHintMessage;
  1824. Expected,Actual: string;
  1825. begin
  1826. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1827. for i:=0 to MsgCount-1 do
  1828. begin
  1829. Item:=Msgs[i];
  1830. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1831. if (Marker<>nil) then
  1832. begin
  1833. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1834. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1835. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1836. end;
  1837. // found
  1838. FHintMsgsGood.Add(Item);
  1839. str(Item.MsgType,Actual);
  1840. str(MsgType,Expected);
  1841. AssertEquals('MsgType',Expected,Actual);
  1842. exit;
  1843. end;
  1844. // needed message missing -> show emitted messages
  1845. WriteSources('',0,0);
  1846. for i:=0 to MsgCount-1 do
  1847. begin
  1848. Item:=Msgs[i];
  1849. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1850. ' ('+IntToStr(Item.MsgNumber),')');
  1851. if Marker<>nil then
  1852. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1853. writeln(' {',Item.Msg,'}');
  1854. end;
  1855. str(MsgType,Expected);
  1856. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1857. if Marker<>nil then
  1858. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1859. Actual:=Actual+' '+Msg;
  1860. Fail(Actual);
  1861. end;
  1862. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1863. );
  1864. var
  1865. i: Integer;
  1866. s, Txt: String;
  1867. Msg: TTestHintMessage;
  1868. begin
  1869. for i:=0 to MsgCount-1 do
  1870. begin
  1871. Msg:=Msgs[i];
  1872. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1873. s:='';
  1874. str(Msg.MsgType,s);
  1875. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1876. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1877. if WithSourcePos then
  1878. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1879. Txt:=Txt+' {'+Msg.Msg+'}';
  1880. Fail(Txt);
  1881. end;
  1882. end;
  1883. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1884. MsgNumber: integer);
  1885. begin
  1886. ExpectedErrorClass:=EScannerError;
  1887. ExpectedErrorMsg:=Msg;
  1888. ExpectedErrorNumber:=MsgNumber;
  1889. end;
  1890. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1891. MsgNumber: integer);
  1892. begin
  1893. ExpectedErrorClass:=EParserError;
  1894. ExpectedErrorMsg:=Msg;
  1895. ExpectedErrorNumber:=MsgNumber;
  1896. end;
  1897. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1898. MsgNumber: integer);
  1899. begin
  1900. ExpectedErrorClass:=EPasResolve;
  1901. ExpectedErrorMsg:=Msg;
  1902. ExpectedErrorNumber:=MsgNumber;
  1903. end;
  1904. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1905. MsgNumber: integer);
  1906. begin
  1907. ExpectedErrorClass:=EPas2JS;
  1908. ExpectedErrorMsg:=Msg;
  1909. ExpectedErrorNumber:=MsgNumber;
  1910. end;
  1911. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1912. var
  1913. MsgNumber: Integer;
  1914. Msg: String;
  1915. begin
  1916. Result:=false;
  1917. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1918. Msg:=E.Message;
  1919. if E is EPas2JS then
  1920. MsgNumber:=EPas2JS(E).MsgNumber
  1921. else if E is EPasResolve then
  1922. MsgNumber:=EPasResolve(E).MsgNumber
  1923. else if E is EParserError then
  1924. MsgNumber:=Parser.LastMsgNumber
  1925. else if E is EScannerError then
  1926. begin
  1927. MsgNumber:=Scanner.LastMsgNumber;
  1928. Msg:=Scanner.LastMsg;
  1929. end
  1930. else
  1931. MsgNumber:=0;
  1932. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1933. if Result then
  1934. SkipTests:=true;
  1935. end;
  1936. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1937. begin
  1938. if IsErrorExpected(E) then exit;
  1939. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1940. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1941. +' '+Scanner.CurFilename
  1942. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1943. FailException(E);
  1944. end;
  1945. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1946. begin
  1947. if IsErrorExpected(E) then exit;
  1948. WriteSources(E.Filename,E.Row,E.Column);
  1949. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1950. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1951. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1952. );
  1953. FailException(E);
  1954. end;
  1955. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1956. var
  1957. P: TPasSourcePos;
  1958. begin
  1959. if IsErrorExpected(E) then exit;
  1960. P:=E.SourcePos;
  1961. WriteSources(P.FileName,P.Row,P.Column);
  1962. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1963. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1964. FailException(E);
  1965. end;
  1966. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1967. var
  1968. Row, Col: integer;
  1969. begin
  1970. if IsErrorExpected(E) then exit;
  1971. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1972. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1973. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1974. +' '+E.PasElement.SourceFilename
  1975. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1976. FailException(E);
  1977. end;
  1978. procedure TCustomTestModule.HandleException(E: Exception);
  1979. begin
  1980. if E is EScannerError then
  1981. HandleScannerError(EScannerError(E))
  1982. else if E is EParserError then
  1983. HandleParserError(EParserError(E))
  1984. else if E is EPasResolve then
  1985. HandlePasResolveError(EPasResolve(E))
  1986. else if E is EPas2JS then
  1987. HandlePas2JSError(EPas2JS(E))
  1988. else
  1989. begin
  1990. if IsErrorExpected(E) then exit;
  1991. if not (E is EAssertionFailedError) then
  1992. begin
  1993. WriteSources('',0,0);
  1994. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  1995. end;
  1996. FailException(E);
  1997. end;
  1998. end;
  1999. procedure TCustomTestModule.FailException(E: Exception);
  2000. var
  2001. MsgNumber: Integer;
  2002. begin
  2003. if ExpectedErrorClass<>nil then
  2004. begin
  2005. if FExpectedErrorClass=E.ClassType then
  2006. begin
  2007. if E is EPas2JS then
  2008. MsgNumber:=EPas2JS(E).MsgNumber
  2009. else if E is EPasResolve then
  2010. MsgNumber:=EPasResolve(E).MsgNumber
  2011. else if E is EParserError then
  2012. MsgNumber:=Parser.LastMsgNumber
  2013. else if E is EScannerError then
  2014. MsgNumber:=Scanner.LastMsgNumber
  2015. else
  2016. MsgNumber:=0;
  2017. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2018. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2019. ExpectedErrorNumber,MsgNumber);
  2020. end else begin
  2021. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2022. end;
  2023. end;
  2024. Fail(E.Message);
  2025. end;
  2026. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2027. aCol: integer);
  2028. var
  2029. IsSrc: Boolean;
  2030. i, j: Integer;
  2031. SrcLines: TStringList;
  2032. Line: string;
  2033. aModule: TTestEnginePasResolver;
  2034. begin
  2035. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2036. for i:=0 to ResolverCount-1 do
  2037. begin
  2038. aModule:=Resolvers[i];
  2039. SrcLines:=TStringList.Create;
  2040. try
  2041. SrcLines.Text:=aModule.Source;
  2042. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2043. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2044. for j:=1 to SrcLines.Count do
  2045. begin
  2046. Line:=SrcLines[j-1];
  2047. if IsSrc and (j=aRow) then
  2048. begin
  2049. write('*');
  2050. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2051. end;
  2052. writeln(Format('%:4d: ',[j]),Line);
  2053. end;
  2054. finally
  2055. SrcLines.Free;
  2056. end;
  2057. end;
  2058. end;
  2059. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2060. var
  2061. i: Integer;
  2062. begin
  2063. for i:=0 to ResolverCount-1 do
  2064. if Filename=Resolvers[i].Filename then exit(i);
  2065. Result:=-1;
  2066. end;
  2067. function TCustomTestModule.GetResolver(const Filename: string
  2068. ): TTestEnginePasResolver;
  2069. var
  2070. i: Integer;
  2071. begin
  2072. i:=IndexOfResolver(Filename);
  2073. if i<0 then exit(nil);
  2074. Result:=Resolvers[i];
  2075. end;
  2076. function TCustomTestModule.GetDefaultNamespace: string;
  2077. var
  2078. C: TClass;
  2079. begin
  2080. Result:='';
  2081. if FModule=nil then exit;
  2082. C:=FModule.ClassType;
  2083. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2084. Result:=Engine.DefaultNameSpace;
  2085. end;
  2086. constructor TCustomTestModule.Create;
  2087. begin
  2088. inherited Create;
  2089. FHintMsgs:=TObjectList.Create(true);
  2090. FHintMsgsGood:=TFPList.Create;
  2091. end;
  2092. destructor TCustomTestModule.Destroy;
  2093. begin
  2094. FreeAndNil(FHintMsgs);
  2095. FreeAndNil(FHintMsgsGood);
  2096. inherited Destroy;
  2097. end;
  2098. { TTestModule }
  2099. procedure TTestModule.TestReservedWords;
  2100. var
  2101. i: integer;
  2102. begin
  2103. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2104. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2105. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2106. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2107. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2108. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2109. end;
  2110. procedure TTestModule.TestEmptyProgram;
  2111. begin
  2112. StartProgram(false);
  2113. Add('begin');
  2114. ConvertProgram;
  2115. CheckSource('TestEmptyProgram','','');
  2116. end;
  2117. procedure TTestModule.TestEmptyProgramUseStrict;
  2118. begin
  2119. Converter.Options:=Converter.Options+[coUseStrict];
  2120. StartProgram(false);
  2121. Add('begin');
  2122. ConvertProgram;
  2123. CheckSource('TestEmptyProgramUseStrict','','');
  2124. end;
  2125. procedure TTestModule.TestEmptyUnit;
  2126. begin
  2127. StartUnit(false);
  2128. Add('interface');
  2129. Add('implementation');
  2130. ConvertUnit;
  2131. CheckSource('TestEmptyUnit',
  2132. LinesToStr([
  2133. ]),
  2134. '');
  2135. end;
  2136. procedure TTestModule.TestEmptyUnitUseStrict;
  2137. begin
  2138. Converter.Options:=Converter.Options+[coUseStrict];
  2139. StartUnit(false);
  2140. Add('interface');
  2141. Add('implementation');
  2142. ConvertUnit;
  2143. CheckSource('TestEmptyUnitUseStrict',
  2144. LinesToStr([
  2145. ''
  2146. ]),
  2147. '');
  2148. end;
  2149. procedure TTestModule.TestDottedUnitNames;
  2150. begin
  2151. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2152. LinesToStr([
  2153. 'var iV: longint;'
  2154. ]),
  2155. '');
  2156. FFilename:='ns1.test1.pp';
  2157. StartProgram(true);
  2158. Add('uses unIt2;');
  2159. Add('implementation');
  2160. Add('var');
  2161. Add(' i: longint;');
  2162. Add('begin');
  2163. Add(' i:=iv;');
  2164. Add(' i:=uNit2.iv;');
  2165. Add(' i:=Ns1.TEst1.i;');
  2166. ConvertProgram;
  2167. CheckSource('TestDottedUnitNames',
  2168. LinesToStr([
  2169. 'this.i = 0;',
  2170. '']),
  2171. LinesToStr([ // this.$init
  2172. '$mod.i = pas["NS1.Unit2"].iV;',
  2173. '$mod.i = pas["NS1.Unit2"].iV;',
  2174. '$mod.i = $mod.i;',
  2175. '']) );
  2176. end;
  2177. procedure TTestModule.TestDottedUnitNameImpl;
  2178. begin
  2179. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2180. LinesToStr([
  2181. 'type',
  2182. ' TObject = class end;',
  2183. ' TTestA = class',
  2184. ' end;'
  2185. ]),
  2186. LinesToStr(['uses TEST.UnitB;'])
  2187. );
  2188. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2189. LinesToStr([
  2190. 'uses TEST.UnitA;',
  2191. 'type TTestB = class(TTestA);'
  2192. ]),
  2193. ''
  2194. );
  2195. StartProgram(true);
  2196. Add('uses TEST.UnitA;');
  2197. Add('begin');
  2198. ConvertProgram;
  2199. CheckSource('TestDottedUnitNameImpl',
  2200. LinesToStr([
  2201. '']),
  2202. LinesToStr([ // this.$init
  2203. '']) );
  2204. CheckUnit('TEST.UnitA.pas',
  2205. LinesToStr([
  2206. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2207. ' var $mod = this;',
  2208. ' rtl.createClass($mod, "TObject", null, function () {',
  2209. ' this.$init = function () {',
  2210. ' };',
  2211. ' this.$final = function () {',
  2212. ' };',
  2213. ' });',
  2214. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2215. ' });',
  2216. '}, ["TEST.UnitB"]);'
  2217. ]));
  2218. CheckUnit('TEST.UnitB.pas',
  2219. LinesToStr([
  2220. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2221. ' var $mod = this;',
  2222. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2223. ' });',
  2224. '});'
  2225. ]));
  2226. end;
  2227. procedure TTestModule.TestDottedUnitExpr;
  2228. begin
  2229. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2230. LinesToStr([
  2231. 'procedure DoIt;'
  2232. ]),
  2233. 'procedure DoIt; begin end;');
  2234. FFilename:='Ns1.SubNs1.Test1.pp';
  2235. StartProgram(true);
  2236. Add('uses Ns2.sUbnS2.unIt2;');
  2237. Add('implementation');
  2238. Add('var');
  2239. Add(' i: longint;');
  2240. Add('begin');
  2241. Add(' ns2.subns2.unit2.doit;');
  2242. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2243. ConvertProgram;
  2244. CheckSource('TestDottedUnitExpr',
  2245. LinesToStr([
  2246. 'this.i = 0;',
  2247. '']),
  2248. LinesToStr([ // this.$init
  2249. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2250. '$mod.i = $mod.i;',
  2251. '']) );
  2252. end;
  2253. procedure TTestModule.Test_ModeFPCFail;
  2254. begin
  2255. StartProgram(false);
  2256. Add('{$mode FPC}');
  2257. Add('begin');
  2258. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2259. ConvertProgram;
  2260. end;
  2261. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2262. begin
  2263. StartProgram(false);
  2264. Add('{$modeswitch cblocks-}');
  2265. Add('begin');
  2266. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2267. ConvertProgram;
  2268. end;
  2269. procedure TTestModule.TestUnit_UseSystem;
  2270. begin
  2271. StartUnit(true);
  2272. Add([
  2273. 'interface',
  2274. 'var i: integer;',
  2275. 'implementation']);
  2276. ConvertUnit;
  2277. CheckSource('TestUnit_UseSystem',
  2278. LinesToStr([
  2279. 'this.i = 0;',
  2280. '']),
  2281. LinesToStr([
  2282. '']) );
  2283. end;
  2284. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2285. begin
  2286. AddModuleWithIntfImplSrc('unit1.pp',
  2287. LinesToStr([
  2288. 'type number = longint;']),
  2289. LinesToStr([
  2290. 'uses test1;',
  2291. 'procedure DoIt;',
  2292. 'begin',
  2293. ' i:=3;',
  2294. 'end;']));
  2295. StartUnit(true);
  2296. Add([
  2297. 'interface',
  2298. 'uses unit1;',
  2299. 'var i: number;',
  2300. 'implementation']);
  2301. ConvertUnit;
  2302. CheckSource('TestUnit_Intf1Impl2Intf1',
  2303. LinesToStr([
  2304. 'this.i = 0;',
  2305. '']),
  2306. LinesToStr([
  2307. '']) );
  2308. end;
  2309. procedure TTestModule.TestIncludeVersion;
  2310. begin
  2311. StartProgram(false);
  2312. Add([
  2313. 'var',
  2314. ' s: string;',
  2315. ' i: word;',
  2316. 'begin',
  2317. ' s:={$I %line%};',
  2318. ' i:={$I %linenum%};',
  2319. ' s:={$I %currentroutine%};',
  2320. ' s:={$I %pas2jsversion%};',
  2321. ' s:={$I %pas2jstarget%};',
  2322. ' s:={$I %pas2jstargetos%};',
  2323. ' s:={$I %pas2jstargetcpu%};',
  2324. ' s:={$I %file%};',
  2325. '']);
  2326. ConvertProgram;
  2327. CheckSource('TestIncludeVersion',
  2328. LinesToStr([
  2329. 'this.s="";',
  2330. 'this.i = 0;']),
  2331. LinesToStr([
  2332. '$mod.s = "7";',
  2333. '$mod.i = 8;',
  2334. '$mod.s = "<anonymous>";',
  2335. '$mod.s = "Comp.Ver.tcmodules";',
  2336. '$mod.s = "Browser";',
  2337. '$mod.s = "Browser";',
  2338. '$mod.s = "ECMAScript5";',
  2339. '$mod.s = "test1.pp";',
  2340. '']));
  2341. end;
  2342. procedure TTestModule.TestVarInt;
  2343. begin
  2344. StartProgram(false);
  2345. Add('var MyI: longint;');
  2346. Add('begin');
  2347. ConvertProgram;
  2348. CheckSource('TestVarInt','this.MyI=0;','');
  2349. end;
  2350. procedure TTestModule.TestVarBaseTypes;
  2351. begin
  2352. StartProgram(false);
  2353. Add('var');
  2354. Add(' i: longint;');
  2355. Add(' s: string;');
  2356. Add(' c: char;');
  2357. Add(' b: boolean;');
  2358. Add(' d: double;');
  2359. Add(' i2: longint = 3;');
  2360. Add(' s2: string = ''foo'';');
  2361. Add(' c2: char = ''4'';');
  2362. Add(' b2: boolean = true;');
  2363. Add(' d2: double = 5.6;');
  2364. Add(' i3: longint = $707;');
  2365. Add(' i4: nativeint = 9007199254740991;');
  2366. Add(' i5: nativeint = -9007199254740991-1;');
  2367. Add(' i6: nativeint = $fffffffffffff;');
  2368. Add(' i7: nativeint = -$fffffffffffff-1;');
  2369. Add(' i8: byte = 00;');
  2370. Add(' u8: nativeuint = $fffffffffffff;');
  2371. Add(' u9: nativeuint = $0000000000000;');
  2372. Add(' u10: nativeuint = $00ff00;');
  2373. Add('begin');
  2374. ConvertProgram;
  2375. CheckSource('TestVarBaseTypes',
  2376. LinesToStr([
  2377. 'this.i = 0;',
  2378. 'this.s = "";',
  2379. 'this.c = "";',
  2380. 'this.b = false;',
  2381. 'this.d = 0.0;',
  2382. 'this.i2 = 3;',
  2383. 'this.s2 = "foo";',
  2384. 'this.c2 = "4";',
  2385. 'this.b2 = true;',
  2386. 'this.d2 = 5.6;',
  2387. 'this.i3 = 0x707;',
  2388. 'this.i4 = 9007199254740991;',
  2389. 'this.i5 = -9007199254740991-1;',
  2390. 'this.i6 = 0xfffffffffffff;',
  2391. 'this.i7 =-0xfffffffffffff-1;',
  2392. 'this.i8 = 0;',
  2393. 'this.u8 = 0xfffffffffffff;',
  2394. 'this.u9 = 0x0;',
  2395. 'this.u10 = 0xff00;'
  2396. ]),
  2397. '');
  2398. end;
  2399. procedure TTestModule.TestBaseTypeSingleFail;
  2400. begin
  2401. StartProgram(false);
  2402. Add('var s: single;');
  2403. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2404. ConvertProgram;
  2405. end;
  2406. procedure TTestModule.TestBaseTypeExtendedFail;
  2407. begin
  2408. StartProgram(false);
  2409. Add('var e: extended;');
  2410. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2411. ConvertProgram;
  2412. end;
  2413. procedure TTestModule.TestConstBaseTypes;
  2414. begin
  2415. StartProgram(false);
  2416. Add('const');
  2417. Add(' i: longint = 3;');
  2418. Add(' s: string = ''foo'';');
  2419. Add(' c: char = ''4'';');
  2420. Add(' b: boolean = true;');
  2421. Add(' d: double = 5.6;');
  2422. Add(' e = low(word);');
  2423. Add(' f = high(word);');
  2424. Add('begin');
  2425. ConvertProgram;
  2426. CheckSource('TestVarBaseTypes',
  2427. LinesToStr([
  2428. 'this.i=3;',
  2429. 'this.s="foo";',
  2430. 'this.c="4";',
  2431. 'this.b=true;',
  2432. 'this.d=5.6;',
  2433. 'this.e = 0;',
  2434. 'this.f = 65535;'
  2435. ]),
  2436. '');
  2437. end;
  2438. procedure TTestModule.TestAliasTypeRef;
  2439. begin
  2440. StartProgram(false);
  2441. Add('type');
  2442. Add(' a=longint;');
  2443. Add(' b=a;');
  2444. Add('var');
  2445. Add(' c: A;');
  2446. Add(' d: B;');
  2447. Add('begin');
  2448. ConvertProgram;
  2449. CheckSource('TestAliasTypeRef',
  2450. LinesToStr([ // statements
  2451. 'this.c = 0;',
  2452. 'this.d = 0;'
  2453. ]),
  2454. LinesToStr([ // this.$main
  2455. ''
  2456. ]));
  2457. end;
  2458. procedure TTestModule.TestTypeCast_BaseTypes;
  2459. begin
  2460. StartProgram(false);
  2461. Add([
  2462. 'var',
  2463. ' i: longint;',
  2464. ' b: boolean;',
  2465. ' d: double;',
  2466. ' s: string;',
  2467. ' c: char;',
  2468. 'begin',
  2469. ' i:=longint(i);',
  2470. ' i:=longint(b);',
  2471. ' b:=boolean(b);',
  2472. ' b:=boolean(i);',
  2473. ' d:=double(d);',
  2474. ' d:=double(i);',
  2475. ' s:=string(s);',
  2476. ' s:=string(c);',
  2477. ' c:=char(c);',
  2478. ' c:=char(i);',
  2479. ' c:=char(65);',
  2480. ' c:=char(#10);',
  2481. ' c:=char(#$E000);',
  2482. '']);
  2483. ConvertProgram;
  2484. CheckSource('TestAliasTypeRef',
  2485. LinesToStr([ // statements
  2486. 'this.i = 0;',
  2487. 'this.b = false;',
  2488. 'this.d = 0.0;',
  2489. 'this.s = "";',
  2490. 'this.c = "";',
  2491. '']),
  2492. LinesToStr([ // this.$main
  2493. '$mod.i = $mod.i;',
  2494. '$mod.i = ($mod.b ? 1 : 0);',
  2495. '$mod.b = $mod.b;',
  2496. '$mod.b = $mod.i != 0;',
  2497. '$mod.d = $mod.d;',
  2498. '$mod.d = $mod.i;',
  2499. '$mod.s = $mod.s;',
  2500. '$mod.s = $mod.c;',
  2501. '$mod.c = $mod.c;',
  2502. '$mod.c = String.fromCharCode($mod.i);',
  2503. '$mod.c = "A";',
  2504. '$mod.c = "\n";',
  2505. '$mod.c = "";',
  2506. '']));
  2507. end;
  2508. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2509. begin
  2510. StartProgram(false);
  2511. Add('type');
  2512. Add(' integer = longint;');
  2513. Add(' TYesNo = boolean;');
  2514. Add(' TFloat = double;');
  2515. Add(' TCaption = string;');
  2516. Add(' TChar = char;');
  2517. Add('var');
  2518. Add(' i: integer;');
  2519. Add(' b: TYesNo;');
  2520. Add(' d: TFloat;');
  2521. Add(' s: TCaption;');
  2522. Add(' c: TChar;');
  2523. Add('begin');
  2524. Add(' i:=integer(i);');
  2525. Add(' i:=integer(b);');
  2526. Add(' b:=TYesNo(b);');
  2527. Add(' b:=TYesNo(i);');
  2528. Add(' d:=TFloat(d);');
  2529. Add(' d:=TFloat(i);');
  2530. Add(' s:=TCaption(s);');
  2531. Add(' s:=TCaption(c);');
  2532. Add(' c:=TChar(c);');
  2533. ConvertProgram;
  2534. CheckSource('TestAliasTypeRef',
  2535. LinesToStr([ // statements
  2536. 'this.i = 0;',
  2537. 'this.b = false;',
  2538. 'this.d = 0.0;',
  2539. 'this.s = "";',
  2540. 'this.c = "";',
  2541. '']),
  2542. LinesToStr([ // this.$main
  2543. '$mod.i = $mod.i;',
  2544. '$mod.i = ($mod.b ? 1 : 0);',
  2545. '$mod.b = $mod.b;',
  2546. '$mod.b = $mod.i != 0;',
  2547. '$mod.d = $mod.d;',
  2548. '$mod.d = $mod.i;',
  2549. '$mod.s = $mod.s;',
  2550. '$mod.s = $mod.c;',
  2551. '$mod.c = $mod.c;',
  2552. '']));
  2553. end;
  2554. procedure TTestModule.TestEmptyProc;
  2555. begin
  2556. StartProgram(false);
  2557. Add('procedure Test;');
  2558. Add('begin');
  2559. Add('end;');
  2560. Add('begin');
  2561. ConvertProgram;
  2562. CheckSource('TestEmptyProc',
  2563. LinesToStr([ // statements
  2564. 'this.Test = function () {',
  2565. '};'
  2566. ]),
  2567. LinesToStr([ // this.$main
  2568. ''
  2569. ]));
  2570. end;
  2571. procedure TTestModule.TestProcOneParam;
  2572. begin
  2573. StartProgram(false);
  2574. Add('procedure ProcA(i: longint);');
  2575. Add('begin');
  2576. Add('end;');
  2577. Add('begin');
  2578. Add(' PROCA(3);');
  2579. ConvertProgram;
  2580. CheckSource('TestProcOneParam',
  2581. LinesToStr([ // statements
  2582. 'this.ProcA = function (i) {',
  2583. '};'
  2584. ]),
  2585. LinesToStr([ // this.$main
  2586. '$mod.ProcA(3);'
  2587. ]));
  2588. end;
  2589. procedure TTestModule.TestFunctionWithoutParams;
  2590. begin
  2591. StartProgram(false);
  2592. Add('function FuncA: longint;');
  2593. Add('begin');
  2594. Add('end;');
  2595. Add('var i: longint;');
  2596. Add('begin');
  2597. Add(' I:=FUNCA();');
  2598. Add(' I:=FUNCA;');
  2599. Add(' FUNCA();');
  2600. Add(' FUNCA;');
  2601. ConvertProgram;
  2602. CheckSource('TestProcWithoutParams',
  2603. LinesToStr([ // statements
  2604. 'this.FuncA = function () {',
  2605. ' var Result = 0;',
  2606. ' return Result;',
  2607. '};',
  2608. 'this.i=0;'
  2609. ]),
  2610. LinesToStr([ // this.$main
  2611. '$mod.i=$mod.FuncA();',
  2612. '$mod.i=$mod.FuncA();',
  2613. '$mod.FuncA();',
  2614. '$mod.FuncA();'
  2615. ]));
  2616. end;
  2617. procedure TTestModule.TestProcedureWithoutParams;
  2618. begin
  2619. StartProgram(false);
  2620. Add('procedure ProcA;');
  2621. Add('begin');
  2622. Add('end;');
  2623. Add('begin');
  2624. Add(' PROCA();');
  2625. Add(' PROCA;');
  2626. ConvertProgram;
  2627. CheckSource('TestProcWithoutParams',
  2628. LinesToStr([ // statements
  2629. 'this.ProcA = function () {',
  2630. '};'
  2631. ]),
  2632. LinesToStr([ // this.$main
  2633. '$mod.ProcA();',
  2634. '$mod.ProcA();'
  2635. ]));
  2636. end;
  2637. procedure TTestModule.TestIncDec;
  2638. begin
  2639. StartProgram(false);
  2640. Add([
  2641. 'procedure DoIt(var i: longint);',
  2642. 'begin',
  2643. ' inc(i);',
  2644. ' inc(i,2);',
  2645. 'end;',
  2646. 'var',
  2647. ' Bar: longint;',
  2648. 'begin',
  2649. ' inc(bar);',
  2650. ' inc(bar,2);',
  2651. ' dec(bar);',
  2652. ' dec(bar,3);',
  2653. '']);
  2654. ConvertProgram;
  2655. CheckSource('TestIncDec',
  2656. LinesToStr([ // statements
  2657. 'this.DoIt = function (i) {',
  2658. ' i.set(i.get()+1);',
  2659. ' i.set(i.get()+2);',
  2660. '};',
  2661. 'this.Bar = 0;'
  2662. ]),
  2663. LinesToStr([ // this.$main
  2664. '$mod.Bar+=1;',
  2665. '$mod.Bar+=2;',
  2666. '$mod.Bar-=1;',
  2667. '$mod.Bar-=3;'
  2668. ]));
  2669. end;
  2670. procedure TTestModule.TestLoHiFpcMode;
  2671. begin
  2672. StartProgram(false);
  2673. Add([
  2674. '{$mode objfpc}',
  2675. 'const',
  2676. ' LoByte1 = Lo(Word($1234));',
  2677. ' HiByte1 = Hi(Word($1234));',
  2678. ' LoByte2 = Lo(SmallInt($1234));',
  2679. ' HiByte2 = Hi(SmallInt($1234));',
  2680. ' LoWord1 = Lo($1234CDEF);',
  2681. ' HiWord1 = Hi($1234CDEF);',
  2682. ' LoWord2 = Lo(-$1234CDEF);',
  2683. ' HiWord2 = Hi(-$1234CDEF);',
  2684. ' lo4:byte=lo(byte($34));',
  2685. ' hi4:byte=hi(byte($34));',
  2686. ' lo5:byte=lo(shortint(-$34));',
  2687. ' hi5:byte=hi(shortint(-$34));',
  2688. ' lo6:longword=lo($123456789ABCD);',
  2689. ' hi6:longword=hi($123456789ABCD);',
  2690. ' lo7:longword=lo(-$123456789ABCD);',
  2691. ' hi7:longword=hi(-$123456789ABCD);',
  2692. 'var',
  2693. ' b: Byte;',
  2694. ' ss: shortint;',
  2695. ' w: Word;',
  2696. ' si: SmallInt;',
  2697. ' lw: LongWord;',
  2698. ' li: LongInt;',
  2699. ' b2: Byte;',
  2700. ' ni: nativeint;',
  2701. 'begin',
  2702. ' w := $1234;',
  2703. ' ss := -$12;',
  2704. ' b := lo(ss);',
  2705. ' b := HI(ss);',
  2706. ' b := lo(w);',
  2707. ' b := HI(w);',
  2708. ' b2 := lo(b);',
  2709. ' b2 := hi(b);',
  2710. ' lw := $1234CDEF;',
  2711. ' w := lo(lw);',
  2712. ' w := hi(lw);',
  2713. ' ni := $123456789ABCD;',
  2714. ' lw := lo(ni);',
  2715. ' lw := hi(ni);',
  2716. '']);
  2717. ConvertProgram;
  2718. CheckSource('TestLoHiFpcMode',
  2719. LinesToStr([ // statements
  2720. 'this.LoByte1 = 0x1234 & 0xFF;',
  2721. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2722. 'this.LoByte2 = 0x1234 & 0xFF;',
  2723. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2724. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2725. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2726. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2727. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2728. 'this.lo4 = 0x34 & 0xF;',
  2729. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2730. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2731. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2732. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2733. 'this.hi6 = 74565 >>> 0;',
  2734. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2735. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2736. 'this.b = 0;',
  2737. 'this.ss = 0;',
  2738. 'this.w = 0;',
  2739. 'this.si = 0;',
  2740. 'this.lw = 0;',
  2741. 'this.li = 0;',
  2742. 'this.b2 = 0;',
  2743. 'this.ni = 0;',
  2744. '']),
  2745. LinesToStr([ // this.$main
  2746. '$mod.w = 0x1234;',
  2747. '$mod.ss = -0x12;',
  2748. '$mod.b = $mod.ss & 0xFF;',
  2749. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2750. '$mod.b = $mod.w & 0xFF;',
  2751. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2752. '$mod.b2 = $mod.b & 0xF;',
  2753. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2754. '$mod.lw = 0x1234CDEF;',
  2755. '$mod.w = $mod.lw & 0xFFFF;',
  2756. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2757. '$mod.ni = 0x123456789ABCD;',
  2758. '$mod.lw = $mod.ni >>> 0;',
  2759. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2760. '']));
  2761. end;
  2762. procedure TTestModule.TestLoHiDelphiMode;
  2763. begin
  2764. StartProgram(false);
  2765. Add([
  2766. '{$mode delphi}',
  2767. 'const',
  2768. ' LoByte1 = Lo(Word($1234));',
  2769. ' HiByte1 = Hi(Word($1234));',
  2770. ' LoByte2 = Lo(SmallInt($1234));',
  2771. ' HiByte2 = Hi(SmallInt($1234));',
  2772. ' LoByte3 = Lo($1234CDEF);',
  2773. ' HiByte3 = Hi($1234CDEF);',
  2774. ' LoByte4 = Lo(-$1234CDEF);',
  2775. ' HiByte4 = Hi(-$1234CDEF);',
  2776. 'var',
  2777. ' b: Byte;',
  2778. ' w: Word;',
  2779. ' si: SmallInt;',
  2780. ' lw: LongWord;',
  2781. ' li: LongInt;',
  2782. 'begin',
  2783. ' w := $1234;',
  2784. ' b := lo(w);',
  2785. ' b := HI(w);',
  2786. ' lw := $1234CDEF;',
  2787. ' b := lo(lw);',
  2788. ' b := hi(lw);',
  2789. '']);
  2790. ConvertProgram;
  2791. CheckSource('TestLoHiDelphiMode',
  2792. LinesToStr([ // statements
  2793. 'this.LoByte1 = 0x1234 & 0xFF;',
  2794. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2795. 'this.LoByte2 = 0x1234 & 0xFF;',
  2796. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2797. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2798. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2799. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2800. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2801. 'this.b = 0;',
  2802. 'this.w = 0;',
  2803. 'this.si = 0;',
  2804. 'this.lw = 0;',
  2805. 'this.li = 0;'
  2806. ]),
  2807. LinesToStr([ // this.$main
  2808. '$mod.w = 0x1234;',
  2809. '$mod.b = $mod.w & 0xFF;',
  2810. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2811. '$mod.lw = 0x1234CDEF;',
  2812. '$mod.b = $mod.lw & 0xFF;',
  2813. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2814. ]));
  2815. end;
  2816. procedure TTestModule.TestAssignments;
  2817. begin
  2818. StartProgram(false);
  2819. Parser.Options:=Parser.Options+[po_cassignments];
  2820. Add('var');
  2821. Add(' Bar:longint;');
  2822. Add('begin');
  2823. Add(' bar:=3;');
  2824. Add(' bar+=4;');
  2825. Add(' bar-=5;');
  2826. Add(' bar*=6;');
  2827. ConvertProgram;
  2828. CheckSource('TestAssignments',
  2829. LinesToStr([ // statements
  2830. 'this.Bar = 0;'
  2831. ]),
  2832. LinesToStr([ // this.$main
  2833. '$mod.Bar=3;',
  2834. '$mod.Bar+=4;',
  2835. '$mod.Bar-=5;',
  2836. '$mod.Bar*=6;'
  2837. ]));
  2838. end;
  2839. procedure TTestModule.TestArithmeticOperators1;
  2840. begin
  2841. StartProgram(false);
  2842. Add('var');
  2843. Add(' vA,vB,vC:longint;');
  2844. Add('begin');
  2845. Add(' va:=1;');
  2846. Add(' vb:=va+va;');
  2847. Add(' vb:=va div vb;');
  2848. Add(' vb:=va mod vb;');
  2849. Add(' vb:=va+va*vb+va div vb;');
  2850. Add(' vc:=-va;');
  2851. Add(' va:=va-vb;');
  2852. Add(' vb:=va;');
  2853. Add(' if va<vb then vc:=va else vc:=vb;');
  2854. ConvertProgram;
  2855. CheckSource('TestArithmeticOperators1',
  2856. LinesToStr([ // statements
  2857. 'this.vA = 0;',
  2858. 'this.vB = 0;',
  2859. 'this.vC = 0;'
  2860. ]),
  2861. LinesToStr([ // this.$main
  2862. '$mod.vA = 1;',
  2863. '$mod.vB = $mod.vA + $mod.vA;',
  2864. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2865. '$mod.vB = $mod.vA % $mod.vB;',
  2866. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2867. '$mod.vC = -$mod.vA;',
  2868. '$mod.vA = $mod.vA - $mod.vB;',
  2869. '$mod.vB = $mod.vA;',
  2870. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2871. ]));
  2872. end;
  2873. procedure TTestModule.TestLogicalOperators;
  2874. begin
  2875. StartProgram(false);
  2876. Add('var');
  2877. Add(' vA,vB,vC:boolean;');
  2878. Add('begin');
  2879. Add(' va:=vb and vc;');
  2880. Add(' va:=vb or vc;');
  2881. Add(' va:=vb xor vc;');
  2882. Add(' va:=true and vc;');
  2883. Add(' va:=(vb and vc) or (va and vb);');
  2884. Add(' va:=not vb;');
  2885. ConvertProgram;
  2886. CheckSource('TestLogicalOperators',
  2887. LinesToStr([ // statements
  2888. 'this.vA = false;',
  2889. 'this.vB = false;',
  2890. 'this.vC = false;'
  2891. ]),
  2892. LinesToStr([ // this.$main
  2893. '$mod.vA = $mod.vB && $mod.vC;',
  2894. '$mod.vA = $mod.vB || $mod.vC;',
  2895. '$mod.vA = $mod.vB ^ $mod.vC;',
  2896. '$mod.vA = true && $mod.vC;',
  2897. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2898. '$mod.vA = !$mod.vB;'
  2899. ]));
  2900. end;
  2901. procedure TTestModule.TestBitwiseOperators;
  2902. begin
  2903. StartProgram(false);
  2904. Add([
  2905. 'var',
  2906. ' vA,vB,vC:longint;',
  2907. ' X,Y,Z: nativeint;',
  2908. 'begin',
  2909. ' va:=vb and vc;',
  2910. ' va:=vb or vc;',
  2911. ' va:=vb xor vc;',
  2912. ' va:=vb shl vc;',
  2913. ' va:=vb shr vc;',
  2914. ' va:=3 and vc;',
  2915. ' va:=(vb and vc) or (va and vb);',
  2916. ' va:=not vb;',
  2917. ' X:=Y and Z;',
  2918. ' X:=Y and va;',
  2919. ' X:=Y or Z;',
  2920. ' X:=Y or va;',
  2921. ' X:=Y xor Z;',
  2922. ' X:=Y xor va;',
  2923. '']);
  2924. ConvertProgram;
  2925. CheckSource('TestBitwiseOperators',
  2926. LinesToStr([ // statements
  2927. 'this.vA = 0;',
  2928. 'this.vB = 0;',
  2929. 'this.vC = 0;',
  2930. 'this.X = 0;',
  2931. 'this.Y = 0;',
  2932. 'this.Z = 0;',
  2933. '']),
  2934. LinesToStr([ // this.$main
  2935. '$mod.vA = $mod.vB & $mod.vC;',
  2936. '$mod.vA = $mod.vB | $mod.vC;',
  2937. '$mod.vA = $mod.vB ^ $mod.vC;',
  2938. '$mod.vA = $mod.vB << $mod.vC;',
  2939. '$mod.vA = $mod.vB >>> $mod.vC;',
  2940. '$mod.vA = 3 & $mod.vC;',
  2941. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2942. '$mod.vA = ~$mod.vB;',
  2943. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  2944. '$mod.X = $mod.Y & $mod.vA;',
  2945. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  2946. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  2947. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  2948. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  2949. '']));
  2950. end;
  2951. procedure TTestModule.TestPrgProcVar;
  2952. begin
  2953. StartProgram(false);
  2954. Add('procedure Proc1;');
  2955. Add('type');
  2956. Add(' t1=longint;');
  2957. Add('var');
  2958. Add(' vA:t1;');
  2959. Add('begin');
  2960. Add('end;');
  2961. Add('begin');
  2962. ConvertProgram;
  2963. CheckSource('TestPrgProcVar',
  2964. LinesToStr([ // statements
  2965. 'this.Proc1 = function () {',
  2966. ' var vA=0;',
  2967. '};'
  2968. ]),
  2969. LinesToStr([ // this.$main
  2970. ''
  2971. ]));
  2972. end;
  2973. procedure TTestModule.TestUnitProcVar;
  2974. begin
  2975. StartUnit(false);
  2976. Add('interface');
  2977. Add('');
  2978. Add('type tA=string; // unit scope');
  2979. Add('procedure Proc1;');
  2980. Add('');
  2981. Add('implementation');
  2982. Add('');
  2983. Add('procedure Proc1;');
  2984. Add('type tA=longint; // local proc scope');
  2985. Add('var v1:tA; // using local tA');
  2986. Add('begin');
  2987. Add('end;');
  2988. Add('var v2:tA; // using interface tA');
  2989. ConvertUnit;
  2990. CheckSource('TestUnitProcVar',
  2991. LinesToStr([ // statements
  2992. 'var $impl = $mod.$impl;',
  2993. 'this.Proc1 = function () {',
  2994. ' var v1 = 0;',
  2995. '};',
  2996. '']),
  2997. // this.$init
  2998. '',
  2999. // implementation
  3000. LinesToStr([
  3001. '$impl.v2 = "";',
  3002. '']));
  3003. end;
  3004. procedure TTestModule.TestImplProc;
  3005. begin
  3006. StartUnit(false);
  3007. Add('interface');
  3008. Add('');
  3009. Add('procedure Proc1;');
  3010. Add('');
  3011. Add('implementation');
  3012. Add('');
  3013. Add('procedure Proc1; begin end;');
  3014. Add('procedure Proc2; begin end;');
  3015. Add('initialization');
  3016. Add(' Proc1;');
  3017. Add(' Proc2;');
  3018. ConvertUnit;
  3019. CheckSource('TestImplProc',
  3020. LinesToStr([ // statements
  3021. 'var $impl = $mod.$impl;',
  3022. 'this.Proc1 = function () {',
  3023. '};',
  3024. '']),
  3025. LinesToStr([ // this.$init
  3026. '$mod.Proc1();',
  3027. '$impl.Proc2();',
  3028. '']),
  3029. LinesToStr([ // implementation
  3030. '$impl.Proc2 = function () {',
  3031. '};',
  3032. ''])
  3033. );
  3034. end;
  3035. procedure TTestModule.TestFunctionResult;
  3036. begin
  3037. StartProgram(false);
  3038. Add('function Func1: longint;');
  3039. Add('begin');
  3040. Add(' Result:=3;');
  3041. Add(' Func1:=4;');
  3042. Add('end;');
  3043. Add('begin');
  3044. ConvertProgram;
  3045. CheckSource('TestFunctionResult',
  3046. LinesToStr([ // statements
  3047. 'this.Func1 = function () {',
  3048. ' var Result = 0;',
  3049. ' Result = 3;',
  3050. ' Result = 4;',
  3051. ' return Result;',
  3052. '};'
  3053. ]),
  3054. '');
  3055. end;
  3056. procedure TTestModule.TestNestedProc;
  3057. begin
  3058. StartProgram(false);
  3059. Add([
  3060. 'var vInUnit: longint;',
  3061. 'function DoIt(pA,pD: longint): longint;',
  3062. 'var',
  3063. ' vB: longint;',
  3064. ' vC: longint;',
  3065. ' function Nesty(pA: longint): longint; ',
  3066. ' var vB: longint;',
  3067. ' begin',
  3068. ' Result:=pa+vb+vc+pd+vInUnit;',
  3069. ' nesty:=3;',
  3070. ' doit:=4;',
  3071. ' exit;',
  3072. ' end;',
  3073. 'begin',
  3074. ' Result:=pa+vb+vc;',
  3075. ' doit:=6;',
  3076. ' exit;',
  3077. 'end;',
  3078. 'begin']);
  3079. ConvertProgram;
  3080. CheckSource('TestNestedProc',
  3081. LinesToStr([ // statements
  3082. 'this.vInUnit = 0;',
  3083. 'this.DoIt = function (pA, pD) {',
  3084. ' var Result = 0;',
  3085. ' var vB = 0;',
  3086. ' var vC = 0;',
  3087. ' function Nesty(pA) {',
  3088. ' var Result$1 = 0;',
  3089. ' var vB = 0;',
  3090. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3091. ' Result$1 = 3;',
  3092. ' Result = 4;',
  3093. ' return Result$1;',
  3094. ' return Result$1;',
  3095. ' };',
  3096. ' Result = pA + vB + vC;',
  3097. ' Result = 6;',
  3098. ' return Result;',
  3099. ' return Result;',
  3100. '};'
  3101. ]),
  3102. '');
  3103. end;
  3104. procedure TTestModule.TestNestedProc_ResultString;
  3105. begin
  3106. StartProgram(false);
  3107. Add([
  3108. 'function DoIt: string;',
  3109. ' function Nesty: string; ',
  3110. ' begin',
  3111. ' nesty:=#65#66;',
  3112. ' nesty[1]:=#67;',
  3113. ' doit:=#68;',
  3114. ' doit[2]:=#69;',
  3115. ' end;',
  3116. 'begin',
  3117. ' doit:=#70;',
  3118. ' doit[3]:=#71;',
  3119. 'end;',
  3120. 'begin']);
  3121. ConvertProgram;
  3122. CheckSource('TestNestedProc_ResultString',
  3123. LinesToStr([ // statements
  3124. 'this.DoIt = function () {',
  3125. ' var Result = "";',
  3126. ' function Nesty() {',
  3127. ' var Result$1 = "";',
  3128. ' Result$1 = "AB";',
  3129. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3130. ' Result = "D";',
  3131. ' Result = rtl.setCharAt(Result, 1, "E");',
  3132. ' return Result$1;',
  3133. ' };',
  3134. ' Result = "F";',
  3135. ' Result = rtl.setCharAt(Result, 2, "G");',
  3136. ' return Result;',
  3137. '};'
  3138. ]),
  3139. '');
  3140. end;
  3141. procedure TTestModule.TestForwardProc;
  3142. begin
  3143. StartProgram(false);
  3144. Add('procedure FuncA(Bar: longint); forward;');
  3145. Add('procedure FuncB(Bar: longint);');
  3146. Add('begin');
  3147. Add(' funca(bar);');
  3148. Add('end;');
  3149. Add('procedure funca(bar: longint);');
  3150. Add('begin');
  3151. Add(' if bar=3 then ;');
  3152. Add('end;');
  3153. Add('begin');
  3154. Add(' funca(4);');
  3155. Add(' funcb(5);');
  3156. ConvertProgram;
  3157. CheckSource('TestForwardProc',
  3158. LinesToStr([ // statements'
  3159. 'this.FuncB = function (Bar) {',
  3160. ' $mod.FuncA(Bar);',
  3161. '};',
  3162. 'this.FuncA = function (Bar) {',
  3163. ' if (Bar === 3);',
  3164. '};'
  3165. ]),
  3166. LinesToStr([
  3167. '$mod.FuncA(4);',
  3168. '$mod.FuncB(5);'
  3169. ])
  3170. );
  3171. end;
  3172. procedure TTestModule.TestNestedForwardProc;
  3173. begin
  3174. StartProgram(false);
  3175. Add('procedure FuncA;');
  3176. Add(' procedure FuncB(i: longint); forward;');
  3177. Add(' procedure FuncC(i: longint);');
  3178. Add(' begin');
  3179. Add(' funcb(i);');
  3180. Add(' end;');
  3181. Add(' procedure FuncB(i: longint);');
  3182. Add(' begin');
  3183. Add(' if i=3 then ;');
  3184. Add(' end;');
  3185. Add('begin');
  3186. Add(' funcc(4)');
  3187. Add('end;');
  3188. Add('begin');
  3189. Add(' funca;');
  3190. ConvertProgram;
  3191. CheckSource('TestNestedForwardProc',
  3192. LinesToStr([ // statements'
  3193. 'this.FuncA = function () {',
  3194. ' function FuncC(i) {',
  3195. ' FuncB(i);',
  3196. ' };',
  3197. ' function FuncB(i) {',
  3198. ' if (i === 3);',
  3199. ' };',
  3200. ' FuncC(4);',
  3201. '};'
  3202. ]),
  3203. LinesToStr([
  3204. '$mod.FuncA();'
  3205. ])
  3206. );
  3207. end;
  3208. procedure TTestModule.TestAssignFunctionResult;
  3209. begin
  3210. StartProgram(false);
  3211. Add('function Func1: longint;');
  3212. Add('begin');
  3213. Add('end;');
  3214. Add('var i: longint;');
  3215. Add('begin');
  3216. Add(' i:=func1();');
  3217. Add(' i:=func1()+func1();');
  3218. ConvertProgram;
  3219. CheckSource('TestAssignFunctionResult',
  3220. LinesToStr([ // statements
  3221. 'this.Func1 = function () {',
  3222. ' var Result = 0;',
  3223. ' return Result;',
  3224. '};',
  3225. 'this.i = 0;'
  3226. ]),
  3227. LinesToStr([
  3228. '$mod.i = $mod.Func1();',
  3229. '$mod.i = $mod.Func1() + $mod.Func1();'
  3230. ]));
  3231. end;
  3232. procedure TTestModule.TestFunctionResultInCondition;
  3233. begin
  3234. StartProgram(false);
  3235. Add('function Func1: longint;');
  3236. Add('begin');
  3237. Add('end;');
  3238. Add('function Func2: boolean;');
  3239. Add('begin');
  3240. Add('end;');
  3241. Add('var i: longint;');
  3242. Add('begin');
  3243. Add(' if func2 then ;');
  3244. Add(' if i=func1() then ;');
  3245. Add(' if i=func1 then ;');
  3246. ConvertProgram;
  3247. CheckSource('TestFunctionResultInCondition',
  3248. LinesToStr([ // statements
  3249. 'this.Func1 = function () {',
  3250. ' var Result = 0;',
  3251. ' return Result;',
  3252. '};',
  3253. 'this.Func2 = function () {',
  3254. ' var Result = false;',
  3255. ' return Result;',
  3256. '};',
  3257. 'this.i = 0;'
  3258. ]),
  3259. LinesToStr([
  3260. 'if ($mod.Func2());',
  3261. 'if ($mod.i === $mod.Func1());',
  3262. 'if ($mod.i === $mod.Func1());'
  3263. ]));
  3264. end;
  3265. procedure TTestModule.TestFunctionResultInForLoop;
  3266. begin
  3267. StartProgram(false);
  3268. Add([
  3269. 'function Func1(a: array of longint): longint;',
  3270. 'begin',
  3271. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3272. ' for Result in a do if a[Result]=0 then exit;',
  3273. 'end;',
  3274. 'begin',
  3275. ' Func1([1,2,3])']);
  3276. ConvertProgram;
  3277. CheckSource('TestFunctionResultInForLoop',
  3278. LinesToStr([ // statements
  3279. 'this.Func1 = function (a) {',
  3280. ' var Result = 0;',
  3281. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3282. ' Result = $l1;',
  3283. ' if (a[Result] === 0) return Result;',
  3284. ' };',
  3285. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3286. ' Result = $in2[$l3];',
  3287. ' if (a[Result] === 0) return Result;',
  3288. ' };',
  3289. ' return Result;',
  3290. '};',
  3291. '']),
  3292. LinesToStr([
  3293. '$mod.Func1([1, 2, 3]);'
  3294. ]));
  3295. end;
  3296. procedure TTestModule.TestFunctionResultInTypeCast;
  3297. begin
  3298. StartProgram(false);
  3299. Add([
  3300. 'function GetInt: longint;',
  3301. 'begin',
  3302. 'end;',
  3303. 'begin',
  3304. ' if Byte(GetInt)=0 then ;',
  3305. '']);
  3306. ConvertProgram;
  3307. CheckSource('TestFunctionResultInTypeCast',
  3308. LinesToStr([ // statements
  3309. 'this.GetInt = function () {',
  3310. ' var Result = 0;',
  3311. ' return Result;',
  3312. '};',
  3313. '']),
  3314. LinesToStr([
  3315. 'if (($mod.GetInt() & 255) === 0) ;'
  3316. ]));
  3317. end;
  3318. procedure TTestModule.TestExit;
  3319. begin
  3320. StartProgram(false);
  3321. Add('procedure ProcA;');
  3322. Add('begin');
  3323. Add(' exit;');
  3324. Add('end;');
  3325. Add('function FuncB: longint;');
  3326. Add('begin');
  3327. Add(' exit;');
  3328. Add(' exit(3);');
  3329. Add('end;');
  3330. Add('function FuncC: string;');
  3331. Add('begin');
  3332. Add(' exit;');
  3333. Add(' exit(''a'');');
  3334. Add(' exit(''abc'');');
  3335. Add('end;');
  3336. Add('begin');
  3337. Add(' exit;');
  3338. Add(' exit(1);');
  3339. ConvertProgram;
  3340. CheckSource('TestExit',
  3341. LinesToStr([ // statements
  3342. 'this.ProcA = function () {',
  3343. ' return;',
  3344. '};',
  3345. 'this.FuncB = function () {',
  3346. ' var Result = 0;',
  3347. ' return Result;',
  3348. ' return 3;',
  3349. ' return Result;',
  3350. '};',
  3351. 'this.FuncC = function () {',
  3352. ' var Result = "";',
  3353. ' return Result;',
  3354. ' return "a";',
  3355. ' return "abc";',
  3356. ' return Result;',
  3357. '};'
  3358. ]),
  3359. LinesToStr([
  3360. 'return;',
  3361. 'return 1;',
  3362. '']));
  3363. end;
  3364. procedure TTestModule.TestBreak;
  3365. begin
  3366. StartProgram(false);
  3367. Add([
  3368. 'var',
  3369. ' i: longint;',
  3370. 'begin',
  3371. ' repeat',
  3372. ' break;',
  3373. ' until true;',
  3374. ' while true do',
  3375. ' break;',
  3376. ' for i:=1 to 2 do',
  3377. ' break;']);
  3378. ConvertProgram;
  3379. CheckSource('TestBreak',
  3380. LinesToStr([ // statements
  3381. 'this.i = 0;'
  3382. ]),
  3383. LinesToStr([
  3384. 'do {',
  3385. ' break;',
  3386. '} while (!true);',
  3387. 'while (true) break;',
  3388. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3389. '']));
  3390. end;
  3391. procedure TTestModule.TestBreakAsVar;
  3392. begin
  3393. StartProgram(false);
  3394. Add([
  3395. 'procedure DoIt(break: boolean);',
  3396. 'begin',
  3397. ' if break then ;',
  3398. 'end;',
  3399. 'var',
  3400. ' break: boolean;',
  3401. 'begin',
  3402. ' if break then ;']);
  3403. ConvertProgram;
  3404. CheckSource('TestBreakAsVar',
  3405. LinesToStr([ // statements
  3406. 'this.DoIt = function (Break) {',
  3407. ' if (Break) ;',
  3408. '};',
  3409. 'this.Break = false;',
  3410. '']),
  3411. LinesToStr([
  3412. 'if($mod.Break) ;',
  3413. '']));
  3414. end;
  3415. procedure TTestModule.TestContinue;
  3416. begin
  3417. StartProgram(false);
  3418. Add('var i: longint;');
  3419. Add('begin');
  3420. Add(' repeat');
  3421. Add(' continue;');
  3422. Add(' until true;');
  3423. Add(' while true do');
  3424. Add(' continue;');
  3425. Add(' for i:=1 to 2 do');
  3426. Add(' continue;');
  3427. ConvertProgram;
  3428. CheckSource('TestContinue',
  3429. LinesToStr([ // statements
  3430. 'this.i = 0;'
  3431. ]),
  3432. LinesToStr([
  3433. 'do {',
  3434. ' continue;',
  3435. '} while (!true);',
  3436. 'while (true) continue;',
  3437. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3438. '']));
  3439. end;
  3440. procedure TTestModule.TestProc_External;
  3441. begin
  3442. StartProgram(false);
  3443. Add('procedure Foo; external name ''console.log'';');
  3444. Add('function Bar: longint; external name ''get.item'';');
  3445. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3446. Add('var');
  3447. Add(' i: longint;');
  3448. Add('begin');
  3449. Add(' Foo;');
  3450. Add(' i:=Bar;');
  3451. Add(' i:=Bla(''abc'');');
  3452. ConvertProgram;
  3453. CheckSource('TestProc_External',
  3454. LinesToStr([ // statements
  3455. 'this.i = 0;'
  3456. ]),
  3457. LinesToStr([
  3458. 'console.log();',
  3459. '$mod.i = get.item();',
  3460. '$mod.i = apply.something("abc");'
  3461. ]));
  3462. end;
  3463. procedure TTestModule.TestProc_ExternalOtherUnit;
  3464. begin
  3465. AddModuleWithIntfImplSrc('unit2.pas',
  3466. LinesToStr([
  3467. 'procedure Now; external name ''Date.now'';',
  3468. 'procedure DoIt;'
  3469. ]),
  3470. 'procedure doit; begin end;');
  3471. StartUnit(true);
  3472. Add('interface');
  3473. Add('uses unit2;');
  3474. Add('implementation');
  3475. Add('begin');
  3476. Add(' now;');
  3477. Add(' now();');
  3478. Add(' uNit2.now;');
  3479. Add(' uNit2.now();');
  3480. Add(' doit;');
  3481. Add(' uNit2.doit;');
  3482. ConvertUnit;
  3483. CheckSource('TestProc_ExternalOtherUnit',
  3484. LinesToStr([
  3485. '']),
  3486. LinesToStr([
  3487. 'Date.now();',
  3488. 'Date.now();',
  3489. 'Date.now();',
  3490. 'Date.now();',
  3491. 'pas.unit2.DoIt();',
  3492. 'pas.unit2.DoIt();',
  3493. '']));
  3494. end;
  3495. procedure TTestModule.TestProc_Asm;
  3496. begin
  3497. StartProgram(false);
  3498. Add([
  3499. '{$mode delphi}',
  3500. 'function DoIt: longint;',
  3501. 'begin;',
  3502. ' asm',
  3503. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3504. ' end;',
  3505. ' asm console.log(); end;',
  3506. ' asm',
  3507. ' s = "'' ";',
  3508. ' s = ''" '';',
  3509. ' s = s + "world" + "''";',
  3510. ' // end',
  3511. ' s = ''end'';',
  3512. ' s = "end";',
  3513. ' end;',
  3514. 'end;',
  3515. 'procedure Fly;',
  3516. 'asm',
  3517. ' return;',
  3518. 'end;',
  3519. 'begin']);
  3520. ConvertProgram;
  3521. CheckSource('TestProc_Asm',
  3522. LinesToStr([ // statements
  3523. 'this.DoIt = function () {',
  3524. ' var Result = 0;',
  3525. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3526. ' console.log();',
  3527. ' s = "'' ";',
  3528. ' s = ''" '';',
  3529. ' s = s + "world" + "''";',
  3530. ' // end',
  3531. ' s = ''end'';',
  3532. ' s = "end";',
  3533. ' return Result;',
  3534. '};',
  3535. 'this.Fly = function () {',
  3536. ' return;',
  3537. '};',
  3538. '']),
  3539. LinesToStr([
  3540. ''
  3541. ]));
  3542. end;
  3543. procedure TTestModule.TestProc_Assembler;
  3544. begin
  3545. StartProgram(false);
  3546. Add('function DoIt: longint; assembler;');
  3547. Add('asm');
  3548. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3549. Add('end;');
  3550. Add('begin');
  3551. ConvertProgram;
  3552. CheckSource('TestProc_Assembler',
  3553. LinesToStr([ // statements
  3554. 'this.DoIt = function () {',
  3555. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3556. '};'
  3557. ]),
  3558. LinesToStr([
  3559. ''
  3560. ]));
  3561. end;
  3562. procedure TTestModule.TestProc_VarParam;
  3563. begin
  3564. StartProgram(false);
  3565. Add('type integer = longint;');
  3566. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3567. Add('var vJ: integer;');
  3568. Add('begin');
  3569. Add(' vg:=vg+1;');
  3570. Add(' vj:=vh+2;');
  3571. Add(' vi:=vi+3;');
  3572. Add(' doit(vg,vg,vg);');
  3573. Add(' doit(vh,vh,vj);');
  3574. Add(' doit(vi,vi,vi);');
  3575. Add(' doit(vj,vj,vj);');
  3576. Add('end;');
  3577. Add('var i: integer;');
  3578. Add('begin');
  3579. Add(' doit(i,i,i);');
  3580. ConvertProgram;
  3581. CheckSource('TestProc_VarParam',
  3582. LinesToStr([ // statements
  3583. 'this.DoIt = function (vG,vH,vI) {',
  3584. ' var vJ = 0;',
  3585. ' vG = vG + 1;',
  3586. ' vJ = vH + 2;',
  3587. ' vI.set(vI.get()+3);',
  3588. ' $mod.DoIt(vG, vG, {',
  3589. ' get: function () {',
  3590. ' return vG;',
  3591. ' },',
  3592. ' set: function (v) {',
  3593. ' vG = v;',
  3594. ' }',
  3595. ' });',
  3596. ' $mod.DoIt(vH, vH, {',
  3597. ' get: function () {',
  3598. ' return vJ;',
  3599. ' },',
  3600. ' set: function (v) {',
  3601. ' vJ = v;',
  3602. ' }',
  3603. ' });',
  3604. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3605. ' $mod.DoIt(vJ, vJ, {',
  3606. ' get: function () {',
  3607. ' return vJ;',
  3608. ' },',
  3609. ' set: function (v) {',
  3610. ' vJ = v;',
  3611. ' }',
  3612. ' });',
  3613. '};',
  3614. 'this.i = 0;'
  3615. ]),
  3616. LinesToStr([
  3617. '$mod.DoIt($mod.i,$mod.i,{',
  3618. ' p: $mod,',
  3619. ' get: function () {',
  3620. ' return this.p.i;',
  3621. ' },',
  3622. ' set: function (v) {',
  3623. ' this.p.i = v;',
  3624. ' }',
  3625. '});'
  3626. ]));
  3627. end;
  3628. procedure TTestModule.TestProc_VarParamString;
  3629. begin
  3630. StartProgram(false);
  3631. Add(['type TCaption = string;',
  3632. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3633. 'var c: char;',
  3634. 'begin',
  3635. ' va[1]:=c;',
  3636. ' vb[2]:=c;',
  3637. ' vc[3]:=c;',
  3638. 'end;',
  3639. 'begin']);
  3640. ConvertProgram;
  3641. CheckSource('TestProc_VarParamString',
  3642. LinesToStr([ // statements
  3643. 'this.DoIt = function (vA,vB,vC) {',
  3644. ' var c = "";',
  3645. ' vA = rtl.setCharAt(vA, 0, c);',
  3646. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3647. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3648. '};',
  3649. '']),
  3650. LinesToStr([
  3651. ]));
  3652. end;
  3653. procedure TTestModule.TestProc_VarParamV;
  3654. begin
  3655. StartProgram(false);
  3656. Add([
  3657. 'procedure Inc2(var i: longint);',
  3658. 'begin',
  3659. ' i:=i+2;',
  3660. 'end;',
  3661. 'procedure DoIt(v: longint);',
  3662. 'var p: array of longint;',
  3663. 'begin',
  3664. ' Inc2(v);',
  3665. ' Inc2(p[v]);',
  3666. 'end;',
  3667. 'begin']);
  3668. ConvertProgram;
  3669. CheckSource('TestProc_VarParamV',
  3670. LinesToStr([ // statements
  3671. 'this.Inc2 = function (i) {',
  3672. ' i.set(i.get()+2);',
  3673. '};',
  3674. 'this.DoIt = function (v) {',
  3675. ' var p = [];',
  3676. ' $mod.Inc2({get: function () {',
  3677. ' return v;',
  3678. ' }, set: function (w) {',
  3679. ' v = w;',
  3680. ' }});',
  3681. ' $mod.Inc2({',
  3682. ' a: v,',
  3683. ' p: p,',
  3684. ' get: function () {',
  3685. ' return this.p[this.a];',
  3686. ' },',
  3687. ' set: function (v) {',
  3688. ' this.p[this.a] = v;',
  3689. ' }',
  3690. ' });',
  3691. '};',
  3692. '']),
  3693. LinesToStr([
  3694. '']));
  3695. end;
  3696. procedure TTestModule.TestProc_Overload;
  3697. begin
  3698. StartProgram(false);
  3699. Add('procedure DoIt(vI: longint); begin end;');
  3700. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3701. Add('procedure DoIt(vD: double); begin end;');
  3702. Add('begin');
  3703. Add(' DoIt(1);');
  3704. Add(' DoIt(2,3);');
  3705. Add(' DoIt(4.5);');
  3706. ConvertProgram;
  3707. CheckSource('TestProcedureOverload',
  3708. LinesToStr([ // statements
  3709. 'this.DoIt = function (vI) {',
  3710. '};',
  3711. 'this.DoIt$1 = function (vI, vJ) {',
  3712. '};',
  3713. 'this.DoIt$2 = function (vD) {',
  3714. '};',
  3715. '']),
  3716. LinesToStr([
  3717. '$mod.DoIt(1);',
  3718. '$mod.DoIt$1(2, 3);',
  3719. '$mod.DoIt$2(4.5);',
  3720. '']));
  3721. end;
  3722. procedure TTestModule.TestProc_OverloadForward;
  3723. begin
  3724. StartProgram(false);
  3725. Add('procedure DoIt(vI: longint); forward;');
  3726. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3727. Add('procedure doit(vi: longint); begin end;');
  3728. Add('begin');
  3729. Add(' doit(1);');
  3730. Add(' doit(2,3);');
  3731. ConvertProgram;
  3732. CheckSource('TestProcedureOverloadForward',
  3733. LinesToStr([ // statements
  3734. 'this.DoIt$1 = function (vI, vJ) {',
  3735. '};',
  3736. 'this.DoIt = function (vI) {',
  3737. '};',
  3738. '']),
  3739. LinesToStr([
  3740. '$mod.DoIt(1);',
  3741. '$mod.DoIt$1(2, 3);',
  3742. '']));
  3743. end;
  3744. procedure TTestModule.TestProc_OverloadIntfImpl;
  3745. begin
  3746. StartUnit(false);
  3747. Add('interface');
  3748. Add('procedure DoIt(vI: longint);');
  3749. Add('procedure DoIt(vI, vJ: longint);');
  3750. Add('implementation');
  3751. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3752. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3753. Add('procedure DoIt(vi: longint); begin end;');
  3754. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3755. Add('procedure DoIt(vi, vj: longint); begin end;');
  3756. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3757. Add('begin');
  3758. Add(' doit(1);');
  3759. Add(' doit(2,3);');
  3760. Add(' doit(4,5,6);');
  3761. Add(' doit(7,8,9,10);');
  3762. Add(' doit(11,12,13,14,15);');
  3763. ConvertUnit;
  3764. CheckSource('TestProcedureOverloadUnit',
  3765. LinesToStr([ // statements
  3766. 'var $impl = $mod.$impl;',
  3767. 'this.DoIt = function (vI) {',
  3768. '};',
  3769. 'this.DoIt$1 = function (vI, vJ) {',
  3770. '};',
  3771. '']),
  3772. LinesToStr([ // this.$init
  3773. '$mod.DoIt(1);',
  3774. '$mod.DoIt$1(2, 3);',
  3775. '$impl.DoIt$3(4,5,6);',
  3776. '$impl.DoIt$4(7,8,9,10);',
  3777. '$impl.DoIt$2(11,12,13,14,15);',
  3778. '']),
  3779. LinesToStr([ // implementation
  3780. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3781. '};',
  3782. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3783. '};',
  3784. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3785. '};',
  3786. '']));
  3787. end;
  3788. procedure TTestModule.TestProc_OverloadNested;
  3789. begin
  3790. StartProgram(false);
  3791. Add([
  3792. 'procedure DoIt(vA: longint); overload; forward;',
  3793. 'procedure DoIt(vB, vC: longint); overload;',
  3794. 'begin // 2 param overload',
  3795. ' doit(1);',
  3796. ' doit(1,2);',
  3797. 'end;',
  3798. 'procedure doit(vA: longint);',
  3799. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3800. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3801. ' begin // 4 param overload',
  3802. ' doit(1);',
  3803. ' doit(1,2);',
  3804. ' doit(1,2,3);',
  3805. ' doit(1,2,3,4);',
  3806. ' end;',
  3807. ' procedure doit(vA, vB, vC: longint);',
  3808. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3809. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3810. ' begin // 6 param overload',
  3811. ' doit(1);',
  3812. ' doit(1,2);',
  3813. ' doit(1,2,3);',
  3814. ' doit(1,2,3,4);',
  3815. ' doit(1,2,3,4,5);',
  3816. ' doit(1,2,3,4,5,6);',
  3817. ' end;',
  3818. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3819. ' begin // 5 param overload',
  3820. ' doit(1);',
  3821. ' doit(1,2);',
  3822. ' doit(1,2,3);',
  3823. ' doit(1,2,3,4);',
  3824. ' doit(1,2,3,4,5);',
  3825. ' doit(1,2,3,4,5,6);',
  3826. ' end;',
  3827. ' begin // 3 param overload',
  3828. ' doit(1);',
  3829. ' doit(1,2);',
  3830. ' doit(1,2,3);',
  3831. ' doit(1,2,3,4);',
  3832. ' doit(1,2,3,4,5);',
  3833. ' doit(1,2,3,4,5,6);',
  3834. ' end;',
  3835. 'begin // 1 param overload',
  3836. ' doit(1);',
  3837. ' doit(1,2);',
  3838. ' doit(1,2,3);',
  3839. ' doit(1,2,3,4);',
  3840. 'end;',
  3841. 'begin // main',
  3842. ' doit(1);',
  3843. ' doit(1,2);']);
  3844. ConvertProgram;
  3845. CheckSource('TestProcedureOverloadNested',
  3846. LinesToStr([ // statements
  3847. 'this.DoIt$1 = function (vB, vC) {',
  3848. ' $mod.DoIt(1);',
  3849. ' $mod.DoIt$1(1, 2);',
  3850. '};',
  3851. 'this.DoIt = function (vA) {',
  3852. ' function DoIt$3(vA, vB, vC, vD) {',
  3853. ' $mod.DoIt(1);',
  3854. ' $mod.DoIt$1(1, 2);',
  3855. ' DoIt$2(1, 2, 3);',
  3856. ' DoIt$3(1, 2, 3, 4);',
  3857. ' };',
  3858. ' function DoIt$2(vA, vB, vC) {',
  3859. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  3860. ' $mod.DoIt(1);',
  3861. ' $mod.DoIt$1(1, 2);',
  3862. ' DoIt$2(1, 2, 3);',
  3863. ' DoIt$3(1, 2, 3, 4);',
  3864. ' DoIt$4(1, 2, 3, 4, 5);',
  3865. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3866. ' };',
  3867. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  3868. ' $mod.DoIt(1);',
  3869. ' $mod.DoIt$1(1, 2);',
  3870. ' DoIt$2(1, 2, 3);',
  3871. ' DoIt$3(1, 2, 3, 4);',
  3872. ' DoIt$4(1, 2, 3, 4, 5);',
  3873. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3874. ' };',
  3875. ' $mod.DoIt(1);',
  3876. ' $mod.DoIt$1(1, 2);',
  3877. ' DoIt$2(1, 2, 3);',
  3878. ' DoIt$3(1, 2, 3, 4);',
  3879. ' DoIt$4(1, 2, 3, 4, 5);',
  3880. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3881. ' };',
  3882. ' $mod.DoIt(1);',
  3883. ' $mod.DoIt$1(1, 2);',
  3884. ' DoIt$2(1, 2, 3);',
  3885. ' DoIt$3(1, 2, 3, 4);',
  3886. '};',
  3887. '']),
  3888. LinesToStr([
  3889. '$mod.DoIt(1);',
  3890. '$mod.DoIt$1(1, 2);',
  3891. '']));
  3892. end;
  3893. procedure TTestModule.TestProc_OverloadUnitCycle;
  3894. begin
  3895. AddModuleWithIntfImplSrc('Unit2.pas',
  3896. LinesToStr([
  3897. 'type',
  3898. ' TObject = class',
  3899. ' procedure DoIt(b: boolean); virtual; abstract;',
  3900. ' procedure DoIt(i: longint); virtual; abstract;',
  3901. ' end;',
  3902. '']),
  3903. 'uses test1;');
  3904. StartUnit(true);
  3905. Add([
  3906. 'interface',
  3907. 'uses unit2;',
  3908. 'type',
  3909. ' TEagle = class(TObject)',
  3910. ' procedure DoIt(b: boolean); override;',
  3911. ' procedure DoIt(i: longint); override;',
  3912. ' end;',
  3913. 'implementation',
  3914. 'procedure TEagle.DoIt(b: boolean); begin end;',
  3915. 'procedure TEagle.DoIt(i: longint); begin end;',
  3916. '']);
  3917. ConvertUnit;
  3918. CheckSource('TestProc_OverloadUnitCycle',
  3919. LinesToStr([ // statements
  3920. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  3921. ' this.DoIt = function (b) {',
  3922. ' };',
  3923. ' this.DoIt$1 = function (i) {',
  3924. ' };',
  3925. '});',
  3926. '']),
  3927. '',
  3928. LinesToStr([
  3929. '']));
  3930. end;
  3931. procedure TTestModule.TestProc_Varargs;
  3932. begin
  3933. StartProgram(false);
  3934. Add([
  3935. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  3936. 'procedure ProcB; varargs; external name ''ProcB'';',
  3937. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  3938. 'function GetIt: longint; begin end;',
  3939. 'begin',
  3940. ' ProcA(1);',
  3941. ' ProcA(1,2);',
  3942. ' ProcA(1,2.0);',
  3943. ' ProcA(1,2,3);',
  3944. ' ProcA(1,''2'');',
  3945. ' ProcA(2,'''');',
  3946. ' ProcA(3,false);',
  3947. ' ProcB;',
  3948. ' ProcB();',
  3949. ' ProcB(4);',
  3950. ' ProcB(''foo'');',
  3951. ' ProcC;',
  3952. ' ProcC();',
  3953. ' ProcC(4);',
  3954. ' ProcC(5,''foo'');',
  3955. ' ProcB(GetIt);',
  3956. ' ProcB(GetIt());',
  3957. ' ProcB(GetIt,GetIt());']);
  3958. ConvertProgram;
  3959. CheckSource('TestProc_Varargs',
  3960. LinesToStr([ // statements
  3961. 'this.GetIt = function () {',
  3962. ' var Result = 0;',
  3963. ' return Result;',
  3964. '};',
  3965. '']),
  3966. LinesToStr([
  3967. 'ProcA(1);',
  3968. 'ProcA(1, 2);',
  3969. 'ProcA(1, 2.0);',
  3970. 'ProcA(1, 2, 3);',
  3971. 'ProcA(1, "2");',
  3972. 'ProcA(2, "");',
  3973. 'ProcA(3, false);',
  3974. 'ProcB();',
  3975. 'ProcB();',
  3976. 'ProcB(4);',
  3977. 'ProcB("foo");',
  3978. 'ProcC(17);',
  3979. 'ProcC(17);',
  3980. 'ProcC(4);',
  3981. 'ProcC(5, "foo");',
  3982. 'ProcB($mod.GetIt());',
  3983. 'ProcB($mod.GetIt());',
  3984. 'ProcB($mod.GetIt(), $mod.GetIt());',
  3985. '']));
  3986. end;
  3987. procedure TTestModule.TestProc_ConstOrder;
  3988. begin
  3989. StartProgram(false);
  3990. Add([
  3991. 'const A = 3;',
  3992. 'const B = A+1;',
  3993. 'procedure DoIt;',
  3994. 'const C = A+1;',
  3995. 'const D = B+1;',
  3996. 'const E = D+C+B+A;',
  3997. 'begin',
  3998. 'end;',
  3999. 'begin'
  4000. ]);
  4001. ConvertProgram;
  4002. CheckSource('TestProc_ConstOrder',
  4003. LinesToStr([ // statements
  4004. 'this.A = 3;',
  4005. 'this.B = 3 + 1;',
  4006. 'var C = 3 + 1;',
  4007. 'var D = 4 + 1;',
  4008. 'var E = 5 + 4 + 4 + 3;',
  4009. 'this.DoIt = function () {',
  4010. '};',
  4011. '']),
  4012. LinesToStr([
  4013. ''
  4014. ]));
  4015. end;
  4016. procedure TTestModule.TestProc_DuplicateConst;
  4017. begin
  4018. StartProgram(false);
  4019. Add([
  4020. 'const A = 1;',
  4021. 'procedure DoIt;',
  4022. 'const A = 2;',
  4023. ' procedure SubIt;',
  4024. ' const A = 21;',
  4025. ' begin',
  4026. ' end;',
  4027. 'begin',
  4028. 'end;',
  4029. 'procedure DoSome;',
  4030. 'const A = 3;',
  4031. 'begin',
  4032. 'end;',
  4033. 'begin'
  4034. ]);
  4035. ConvertProgram;
  4036. CheckSource('TestProc_DuplicateConst',
  4037. LinesToStr([ // statements
  4038. 'this.A = 1;',
  4039. 'var A$1 = 2;',
  4040. 'var A$2 = 21;',
  4041. 'this.DoIt = function () {',
  4042. ' function SubIt() {',
  4043. ' };',
  4044. '};',
  4045. 'var A$3 = 3;',
  4046. 'this.DoSome = function () {',
  4047. '};',
  4048. '']),
  4049. LinesToStr([
  4050. ''
  4051. ]));
  4052. end;
  4053. procedure TTestModule.TestProc_LocalVarAbsolute;
  4054. begin
  4055. StartProgram(false);
  4056. Add([
  4057. 'type',
  4058. ' TObject = class',
  4059. ' Index: longint;',
  4060. ' procedure DoAbs(Item: pointer);',
  4061. ' end;',
  4062. 'procedure TObject.DoAbs(Item: pointer);',
  4063. 'var',
  4064. ' o: TObject absolute Item;',
  4065. 'begin',
  4066. ' if o.Index<o.Index then o.Index:=o.Index;',
  4067. 'end;',
  4068. 'procedure DoIt(i: longint; p: pointer);',
  4069. 'var',
  4070. ' d: double absolute i;',
  4071. ' s: string absolute d;',
  4072. ' oi: TObject absolute i;',
  4073. ' op: TObject absolute p;',
  4074. 'begin',
  4075. ' if d=d then d:=d;',
  4076. ' if s=s then s:=s;',
  4077. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4078. ' if op.Index=op.Index then op.Index:=op.Index;',
  4079. 'end;',
  4080. 'begin']);
  4081. ConvertProgram;
  4082. CheckSource('TestProc_LocalVarAbsolute',
  4083. LinesToStr([ // statements
  4084. 'rtl.createClass($mod, "TObject", null, function () {',
  4085. ' this.$init = function () {',
  4086. ' this.Index = 0;',
  4087. ' };',
  4088. ' this.$final = function () {',
  4089. ' };',
  4090. ' this.DoAbs = function (Item) {',
  4091. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4092. ' };',
  4093. '});',
  4094. 'this.DoIt = function (i, p) {',
  4095. ' if (i === i) i = i;',
  4096. ' if (i === i) i = i;',
  4097. ' if (i.Index < i.Index) i.Index = i.Index;',
  4098. ' if (p.Index === p.Index) p.Index = p.Index;',
  4099. '};'
  4100. ]),
  4101. LinesToStr([
  4102. ]));
  4103. end;
  4104. procedure TTestModule.TestProc_ReservedWords;
  4105. begin
  4106. StartProgram(false);
  4107. Add([
  4108. 'procedure Date(ArrayBuffer: longint);',
  4109. 'const',
  4110. ' NaN: longint = 3;',
  4111. 'var',
  4112. ' &Boolean: longint;',
  4113. ' procedure Error(ArrayBuffer: longint);',
  4114. ' begin',
  4115. ' end;',
  4116. 'begin',
  4117. ' Nan:=&bOolean;',
  4118. 'end;',
  4119. 'begin',
  4120. ' Date(1);']);
  4121. ConvertProgram;
  4122. CheckSource('TestProc_ReservedWords',
  4123. LinesToStr([ // statements
  4124. 'var naN = 3;',
  4125. 'this.Date = function (arrayBuffer) {',
  4126. ' var boolean = 0;',
  4127. ' function error(arrayBuffer) {',
  4128. ' };',
  4129. ' naN = boolean;',
  4130. '};',
  4131. '']),
  4132. LinesToStr([
  4133. ' $mod.Date(1);'
  4134. ]));
  4135. end;
  4136. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4137. begin
  4138. StartProgram(false);
  4139. Add([
  4140. '{$mode objfpc}',
  4141. 'type',
  4142. ' TFunc = reference to function(x: word): word;',
  4143. 'var Func: TFunc;',
  4144. 'procedure DoIt(a: word);',
  4145. 'begin',
  4146. ' Func:=function(b:word): word',
  4147. ' begin',
  4148. ' Result:=a+b;',
  4149. ' exit(b);',
  4150. ' exit(Result);',
  4151. ' end;',// test semicolon
  4152. ' a:=3;',
  4153. 'end;',
  4154. 'begin',
  4155. ' Func:=function(c:word):word begin',
  4156. ' Result:=3+c;',
  4157. ' exit(c);',
  4158. ' exit(Result);',
  4159. ' end;']);
  4160. ConvertProgram;
  4161. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4162. LinesToStr([ // statements
  4163. 'this.Func = null;',
  4164. 'this.DoIt = function (a) {',
  4165. ' $mod.Func = function (b) {',
  4166. ' var Result = 0;',
  4167. ' Result = a + b;',
  4168. ' return b;',
  4169. ' return Result;',
  4170. ' return Result;',
  4171. ' };',
  4172. ' a = 3;',
  4173. '};',
  4174. '']),
  4175. LinesToStr([
  4176. '$mod.Func = function (c) {',
  4177. ' var Result = 0;',
  4178. ' Result = 3 + c;',
  4179. ' return c;',
  4180. ' return Result;',
  4181. ' return Result;',
  4182. '};',
  4183. '']));
  4184. end;
  4185. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4186. begin
  4187. StartProgram(false);
  4188. Add([
  4189. '{$mode delphi}',
  4190. 'type',
  4191. ' TProc = reference to procedure(x: word);',
  4192. 'procedure DoIt(a: word);',
  4193. 'var Proc: TProc;',
  4194. 'begin',
  4195. ' Proc:=procedure(b:word) begin end;',
  4196. 'end;',
  4197. 'var Proc: TProc;',
  4198. 'begin',
  4199. ' Proc:=procedure(c:word) begin end;',
  4200. '']);
  4201. ConvertProgram;
  4202. CheckSource('TestAnonymousProc_Assign_Delphi',
  4203. LinesToStr([ // statements
  4204. 'this.DoIt = function (a) {',
  4205. ' var Proc = null;',
  4206. ' Proc = function (b) {',
  4207. ' };',
  4208. '};',
  4209. 'this.Proc = null;',
  4210. '']),
  4211. LinesToStr([
  4212. '$mod.Proc = function (c) {',
  4213. '};',
  4214. '']));
  4215. end;
  4216. procedure TTestModule.TestAnonymousProc_Arg;
  4217. begin
  4218. StartProgram(false);
  4219. Add([
  4220. 'type',
  4221. ' TProc = reference to procedure;',
  4222. ' TFunc = reference to function(x: word): word;',
  4223. 'procedure DoMore(f,g: TProc);',
  4224. 'begin',
  4225. 'end;',
  4226. 'procedure DoOdd(v: jsvalue);',
  4227. 'begin',
  4228. 'end;',
  4229. 'procedure DoIt(f: TFunc);',
  4230. 'begin',
  4231. ' DoIt(function(b:word): word',
  4232. ' begin',
  4233. ' Result:=1+b;',
  4234. ' end);',
  4235. ' DoMore(procedure begin end, procedure begin end);',
  4236. ' DoOdd(procedure begin end);',
  4237. 'end;',
  4238. 'begin',
  4239. ' DoMore(procedure begin end,',
  4240. ' procedure assembler asm',
  4241. ' console.log("c");',
  4242. ' end);',
  4243. '']);
  4244. ConvertProgram;
  4245. CheckSource('TestAnonymousProc_Arg',
  4246. LinesToStr([ // statements
  4247. 'this.DoMore = function (f, g) {',
  4248. '};',
  4249. 'this.DoOdd = function (v) {',
  4250. '};',
  4251. 'this.DoIt = function (f) {',
  4252. ' $mod.DoIt(function (b) {',
  4253. ' var Result = 0;',
  4254. ' Result = 1 + b;',
  4255. ' return Result;',
  4256. ' });',
  4257. ' $mod.DoMore(function () {',
  4258. ' }, function () {',
  4259. ' });',
  4260. ' $mod.DoOdd(function () {',
  4261. ' });',
  4262. '};',
  4263. '']),
  4264. LinesToStr([
  4265. '$mod.DoMore(function () {',
  4266. '}, function () {',
  4267. ' console.log("c");',
  4268. '});',
  4269. '']));
  4270. end;
  4271. procedure TTestModule.TestAnonymousProc_Typecast;
  4272. begin
  4273. StartProgram(false);
  4274. Add([
  4275. 'type',
  4276. ' TProc = reference to procedure(w: word);',
  4277. ' TArr = array of word;',
  4278. ' TFuncArr = reference to function: TArr;',
  4279. 'procedure DoIt(p: TProc);',
  4280. 'var',
  4281. ' w: word;',
  4282. ' a: TArr;',
  4283. 'begin',
  4284. ' p:=TProc(procedure(b: smallint) begin end);',
  4285. ' a:=TFuncArr(function: TArr begin end)();',
  4286. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4287. 'end;',
  4288. 'begin']);
  4289. ConvertProgram;
  4290. CheckSource('TestAnonymousProc_Typecast',
  4291. LinesToStr([ // statements
  4292. 'this.DoIt = function (p) {',
  4293. ' var w = 0;',
  4294. ' var a = [];',
  4295. ' p = function (b) {',
  4296. ' };',
  4297. ' a = function () {',
  4298. ' var Result = [];',
  4299. ' return Result;',
  4300. ' }();',
  4301. ' w = function () {',
  4302. ' var Result = [];',
  4303. ' return Result;',
  4304. ' }()[3];',
  4305. '};',
  4306. '']),
  4307. LinesToStr([
  4308. '']));
  4309. end;
  4310. procedure TTestModule.TestAnonymousProc_With;
  4311. begin
  4312. StartProgram(false);
  4313. Add([
  4314. 'type',
  4315. ' TProc = reference to procedure(w: word);',
  4316. ' TObject = class',
  4317. ' b: boolean;',
  4318. ' end;',
  4319. 'var',
  4320. ' p: TProc;',
  4321. ' bird: TObject;',
  4322. 'begin',
  4323. ' with bird do',
  4324. ' p:=procedure(w: word)',
  4325. ' begin',
  4326. ' b:=w>2;',
  4327. ' end;',
  4328. '']);
  4329. ConvertProgram;
  4330. CheckSource('TestAnonymousProc_With',
  4331. LinesToStr([ // statements
  4332. 'rtl.createClass($mod, "TObject", null, function () {',
  4333. ' this.$init = function () {',
  4334. ' this.b = false;',
  4335. ' };',
  4336. ' this.$final = function () {',
  4337. ' };',
  4338. '});',
  4339. 'this.p = null;',
  4340. 'this.bird = null;',
  4341. '']),
  4342. LinesToStr([
  4343. 'var $with1 = $mod.bird;',
  4344. '$mod.p = function (w) {',
  4345. ' $with1.b = w > 2;',
  4346. '};',
  4347. '']));
  4348. end;
  4349. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4350. begin
  4351. StartProgram(false);
  4352. Add([
  4353. 'type',
  4354. ' TProc = reference to procedure;',
  4355. ' TObject = class',
  4356. ' b: boolean;',
  4357. ' end;',
  4358. 'procedure DoIt;',
  4359. 'var',
  4360. ' p: TProc;',
  4361. 'begin',
  4362. ' try',
  4363. ' except',
  4364. ' on E: TObject do',
  4365. ' p:=procedure',
  4366. ' begin',
  4367. ' E.b:=true;',
  4368. ' end;',
  4369. ' end;',
  4370. 'end;',
  4371. 'begin']);
  4372. ConvertProgram;
  4373. CheckSource('TestAnonymousProc_ExceptOn',
  4374. LinesToStr([ // statements
  4375. 'rtl.createClass($mod, "TObject", null, function () {',
  4376. ' this.$init = function () {',
  4377. ' this.b = false;',
  4378. ' };',
  4379. ' this.$final = function () {',
  4380. ' };',
  4381. '});',
  4382. 'this.DoIt = function () {',
  4383. ' var p = null;',
  4384. ' try {} catch ($e) {',
  4385. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4386. ' var E = $e;',
  4387. ' p = function () {',
  4388. ' E.b = true;',
  4389. ' };',
  4390. ' } else throw $e',
  4391. ' };',
  4392. '};',
  4393. '']),
  4394. LinesToStr([
  4395. '']));
  4396. end;
  4397. procedure TTestModule.TestAnonymousProc_Nested;
  4398. begin
  4399. StartProgram(false);
  4400. Add([
  4401. 'type',
  4402. ' TProc = reference to procedure;',
  4403. ' TObject = class',
  4404. ' i: byte;',
  4405. ' procedure DoIt;',
  4406. ' end;',
  4407. 'procedure TObject.DoIt;',
  4408. 'var',
  4409. ' p: TProc;',
  4410. ' procedure Sub;',
  4411. ' begin',
  4412. ' p:=procedure',
  4413. ' begin',
  4414. ' i:=3;',
  4415. ' Self.i:=4;',
  4416. ' p:=procedure',
  4417. ' procedure SubSub;',
  4418. ' begin',
  4419. ' i:=13;',
  4420. ' Self.i:=14;',
  4421. ' end;',
  4422. ' begin',
  4423. ' i:=13;',
  4424. ' Self.i:=14;',
  4425. ' end;',
  4426. ' end;',
  4427. ' end;',
  4428. 'begin',
  4429. 'end;',
  4430. 'begin']);
  4431. ConvertProgram;
  4432. CheckSource('TestAnonymousProc_Nested',
  4433. LinesToStr([ // statements
  4434. 'rtl.createClass($mod, "TObject", null, function () {',
  4435. ' this.$init = function () {',
  4436. ' this.i = 0;',
  4437. ' };',
  4438. ' this.$final = function () {',
  4439. ' };',
  4440. ' this.DoIt = function () {',
  4441. ' var $Self = this;',
  4442. ' var p = null;',
  4443. ' function Sub() {',
  4444. ' p = function () {',
  4445. ' $Self.i = 3;',
  4446. ' $Self.i = 4;',
  4447. ' p = function () {',
  4448. ' function SubSub() {',
  4449. ' $Self.i = 13;',
  4450. ' $Self.i = 14;',
  4451. ' };',
  4452. ' $Self.i = 13;',
  4453. ' $Self.i = 14;',
  4454. ' };',
  4455. ' };',
  4456. ' };',
  4457. ' };',
  4458. '});',
  4459. '']),
  4460. LinesToStr([
  4461. '']));
  4462. end;
  4463. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4464. begin
  4465. StartProgram(false);
  4466. Add([
  4467. 'type',
  4468. ' TProc = reference to procedure;',
  4469. 'function DoIt: TProc;',
  4470. ' function Sub: TProc;',
  4471. ' begin',
  4472. ' Result:=procedure',
  4473. ' begin',
  4474. ' Sub:=procedure',
  4475. ' procedure SubSub;',
  4476. ' begin',
  4477. ' Result:=nil;',
  4478. ' Sub:=nil;',
  4479. ' DoIt:=nil;',
  4480. ' end;',
  4481. ' begin',
  4482. ' Result:=nil;',
  4483. ' Sub:=nil;',
  4484. ' DoIt:=nil;',
  4485. ' end;',
  4486. ' end;',
  4487. ' end;',
  4488. 'begin',
  4489. 'end;',
  4490. 'begin']);
  4491. ConvertProgram;
  4492. CheckSource('TestAnonymousProc_NestedAssignResult',
  4493. LinesToStr([ // statements
  4494. 'this.DoIt = function () {',
  4495. ' var Result = null;',
  4496. ' function Sub() {',
  4497. ' var Result$1 = null;',
  4498. ' Result$1 = function () {',
  4499. ' Result$1 = function () {',
  4500. ' function SubSub() {',
  4501. ' Result$1 = null;',
  4502. ' Result$1 = null;',
  4503. ' Result = null;',
  4504. ' };',
  4505. ' Result$1 = null;',
  4506. ' Result$1 = null;',
  4507. ' Result = null;',
  4508. ' };',
  4509. ' };',
  4510. ' return Result$1;',
  4511. ' };',
  4512. ' return Result;',
  4513. '};',
  4514. '']),
  4515. LinesToStr([
  4516. '']));
  4517. end;
  4518. procedure TTestModule.TestAnonymousProc_Class;
  4519. begin
  4520. StartProgram(false);
  4521. Add([
  4522. 'type',
  4523. ' TProc = reference to procedure;',
  4524. ' TObject = class',
  4525. ' Size: word;',
  4526. ' function GetIt: TProc;',
  4527. ' end;',
  4528. 'function TObject.GetIt: TProc;',
  4529. 'begin',
  4530. ' Result:=procedure',
  4531. ' begin',
  4532. ' Size:=Size;',
  4533. ' end;',
  4534. 'end;',
  4535. 'begin']);
  4536. ConvertProgram;
  4537. CheckSource('TestAnonymousProc_Class',
  4538. LinesToStr([ // statements
  4539. 'rtl.createClass($mod, "TObject", null, function () {',
  4540. ' this.$init = function () {',
  4541. ' this.Size = 0;',
  4542. ' };',
  4543. ' this.$final = function () {',
  4544. ' };',
  4545. ' this.GetIt = function () {',
  4546. ' var $Self = this;',
  4547. ' var Result = null;',
  4548. ' Result = function () {',
  4549. ' $Self.Size = $Self.Size;',
  4550. ' };',
  4551. ' return Result;',
  4552. ' };',
  4553. '});',
  4554. '']),
  4555. LinesToStr([
  4556. '']));
  4557. end;
  4558. procedure TTestModule.TestEnum_Name;
  4559. begin
  4560. StartProgram(false);
  4561. Add('type TMyEnum = (Red, Green, Blue);');
  4562. Add('var e: TMyEnum;');
  4563. Add('var f: TMyEnum = Blue;');
  4564. Add('begin');
  4565. Add(' e:=green;');
  4566. Add(' e:=default(TMyEnum);');
  4567. ConvertProgram;
  4568. CheckSource('TestEnumName',
  4569. LinesToStr([ // statements
  4570. 'this.TMyEnum = {',
  4571. ' "0":"Red",',
  4572. ' Red:0,',
  4573. ' "1":"Green",',
  4574. ' Green:1,',
  4575. ' "2":"Blue",',
  4576. ' Blue:2',
  4577. ' };',
  4578. 'this.e = 0;',
  4579. 'this.f = $mod.TMyEnum.Blue;'
  4580. ]),
  4581. LinesToStr([
  4582. '$mod.e=$mod.TMyEnum.Green;',
  4583. '$mod.e=$mod.TMyEnum.Red;'
  4584. ]));
  4585. end;
  4586. procedure TTestModule.TestEnum_Number;
  4587. begin
  4588. Converter.Options:=Converter.Options+[coEnumNumbers];
  4589. StartProgram(false);
  4590. Add('type TMyEnum = (Red, Green);');
  4591. Add('var');
  4592. Add(' e: TMyEnum;');
  4593. Add(' f: TMyEnum = Green;');
  4594. Add(' i: longint;');
  4595. Add('begin');
  4596. Add(' e:=green;');
  4597. Add(' i:=longint(e);');
  4598. ConvertProgram;
  4599. CheckSource('TestEnumNumber',
  4600. LinesToStr([ // statements
  4601. 'this.TMyEnum = {',
  4602. ' "0":"Red",',
  4603. ' Red:0,',
  4604. ' "1":"Green",',
  4605. ' Green:1',
  4606. ' };',
  4607. 'this.e = 0;',
  4608. 'this.f = 1;',
  4609. 'this.i = 0;'
  4610. ]),
  4611. LinesToStr([
  4612. '$mod.e=1;',
  4613. '$mod.i=$mod.e;'
  4614. ]));
  4615. end;
  4616. procedure TTestModule.TestEnum_ConstFail;
  4617. begin
  4618. StartProgram(false);
  4619. Add([
  4620. 'type TMyEnum = (Red = 100, Green = 101);',
  4621. 'var',
  4622. ' e: TMyEnum;',
  4623. ' f: TMyEnum = Green;',
  4624. 'begin',
  4625. ' e:=green;']);
  4626. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4627. ConvertProgram;
  4628. end;
  4629. procedure TTestModule.TestEnum_Functions;
  4630. begin
  4631. StartProgram(false);
  4632. Add([
  4633. 'type TMyEnum = (Red, Green);',
  4634. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4635. 'var',
  4636. ' v: longint;',
  4637. ' s: string;',
  4638. 'begin',
  4639. ' val(s,e,v);',
  4640. ' val(s,e,i);',
  4641. 'end;',
  4642. 'var',
  4643. ' e: TMyEnum;',
  4644. ' i: longint;',
  4645. ' s: string;',
  4646. ' b: boolean;',
  4647. 'begin',
  4648. ' i:=ord(red);',
  4649. ' i:=ord(green);',
  4650. ' i:=ord(e);',
  4651. ' i:=ord(b);',
  4652. ' e:=low(tmyenum);',
  4653. ' e:=low(e);',
  4654. ' b:=low(boolean);',
  4655. ' e:=high(tmyenum);',
  4656. ' e:=high(e);',
  4657. ' b:=high(boolean);',
  4658. ' e:=pred(green);',
  4659. ' e:=pred(e);',
  4660. ' b:=pred(b);',
  4661. ' e:=succ(red);',
  4662. ' e:=succ(e);',
  4663. ' b:=succ(b);',
  4664. ' e:=tmyenum(1);',
  4665. ' e:=tmyenum(i);',
  4666. ' s:=str(e);',
  4667. ' str(e,s);',
  4668. ' str(red,s);',
  4669. ' s:=str(e:3);',
  4670. ' writestr(s,e:3,red);',
  4671. ' val(s,e,i);',
  4672. ' e:=TMyEnum(i);',
  4673. ' i:=longint(e);']);
  4674. ConvertProgram;
  4675. CheckSource('TestEnum_Functions',
  4676. LinesToStr([ // statements
  4677. 'this.TMyEnum = {',
  4678. ' "0":"Red",',
  4679. ' Red:0,',
  4680. ' "1":"Green",',
  4681. ' Green:1',
  4682. ' };',
  4683. 'this.DoIt = function (e, i) {',
  4684. ' var v = 0;',
  4685. ' var s = "";',
  4686. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4687. ' v = w;',
  4688. ' }));',
  4689. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4690. '};',
  4691. 'this.e = 0;',
  4692. 'this.i = 0;',
  4693. 'this.s = "";',
  4694. 'this.b = false;',
  4695. '']),
  4696. LinesToStr([
  4697. '$mod.i=$mod.TMyEnum.Red;',
  4698. '$mod.i=$mod.TMyEnum.Green;',
  4699. '$mod.i=$mod.e;',
  4700. '$mod.i=$mod.b+0;',
  4701. '$mod.e=$mod.TMyEnum.Red;',
  4702. '$mod.e=$mod.TMyEnum.Red;',
  4703. '$mod.b=false;',
  4704. '$mod.e=$mod.TMyEnum.Green;',
  4705. '$mod.e=$mod.TMyEnum.Green;',
  4706. '$mod.b=true;',
  4707. '$mod.e=$mod.TMyEnum.Green-1;',
  4708. '$mod.e=$mod.e-1;',
  4709. '$mod.b=false;',
  4710. '$mod.e=$mod.TMyEnum.Red+1;',
  4711. '$mod.e=$mod.e+1;',
  4712. '$mod.b=true;',
  4713. '$mod.e=1;',
  4714. '$mod.e=$mod.i;',
  4715. '$mod.s = $mod.TMyEnum[$mod.e];',
  4716. '$mod.s = $mod.TMyEnum[$mod.e];',
  4717. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4718. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4719. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4720. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4721. ' $mod.i = v;',
  4722. '});',
  4723. '$mod.e=$mod.i;',
  4724. '$mod.i=$mod.e;',
  4725. '']));
  4726. end;
  4727. procedure TTestModule.TestEnum_AsParams;
  4728. begin
  4729. StartProgram(false);
  4730. Add('type TEnum = (Red,Blue);');
  4731. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4732. Add('var vJ: TEnum;');
  4733. Add('begin');
  4734. Add(' vg:=vg;');
  4735. Add(' vj:=vh;');
  4736. Add(' vi:=vi;');
  4737. Add(' doit(vg,vg,vg);');
  4738. Add(' doit(vh,vh,vj);');
  4739. Add(' doit(vi,vi,vi);');
  4740. Add(' doit(vj,vj,vj);');
  4741. Add('end;');
  4742. Add('var i: TEnum;');
  4743. Add('begin');
  4744. Add(' doit(i,i,i);');
  4745. ConvertProgram;
  4746. CheckSource('TestEnum_AsParams',
  4747. LinesToStr([ // statements
  4748. 'this.TEnum = {',
  4749. ' "0": "Red",',
  4750. ' Red: 0,',
  4751. ' "1": "Blue",',
  4752. ' Blue: 1',
  4753. '};',
  4754. 'this.DoIt = function (vG,vH,vI) {',
  4755. ' var vJ = 0;',
  4756. ' vG = vG;',
  4757. ' vJ = vH;',
  4758. ' vI.set(vI.get());',
  4759. ' $mod.DoIt(vG, vG, {',
  4760. ' get: function () {',
  4761. ' return vG;',
  4762. ' },',
  4763. ' set: function (v) {',
  4764. ' vG = v;',
  4765. ' }',
  4766. ' });',
  4767. ' $mod.DoIt(vH, vH, {',
  4768. ' get: function () {',
  4769. ' return vJ;',
  4770. ' },',
  4771. ' set: function (v) {',
  4772. ' vJ = v;',
  4773. ' }',
  4774. ' });',
  4775. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4776. ' $mod.DoIt(vJ, vJ, {',
  4777. ' get: function () {',
  4778. ' return vJ;',
  4779. ' },',
  4780. ' set: function (v) {',
  4781. ' vJ = v;',
  4782. ' }',
  4783. ' });',
  4784. '};',
  4785. 'this.i = 0;'
  4786. ]),
  4787. LinesToStr([
  4788. '$mod.DoIt($mod.i,$mod.i,{',
  4789. ' p: $mod,',
  4790. ' get: function () {',
  4791. ' return this.p.i;',
  4792. ' },',
  4793. ' set: function (v) {',
  4794. ' this.p.i = v;',
  4795. ' }',
  4796. '});'
  4797. ]));
  4798. end;
  4799. procedure TTestModule.TestEnumRange_Array;
  4800. begin
  4801. StartProgram(false);
  4802. Add([
  4803. 'type',
  4804. ' TEnum = (Red, Green, Blue);',
  4805. ' TEnumRg = green..blue;',
  4806. ' TArr = array[TEnumRg] of byte;',
  4807. ' TArr2 = array[green..blue] of byte;',
  4808. 'var',
  4809. ' a: TArr;',
  4810. ' b: TArr = (3,4);',
  4811. ' c: TArr2 = (5,6);',
  4812. 'begin',
  4813. ' a[green] := b[blue];',
  4814. ' c[green] := c[blue];',
  4815. '']);
  4816. ConvertProgram;
  4817. CheckSource('TestEnumRange_Array',
  4818. LinesToStr([ // statements
  4819. 'this.TEnum = {',
  4820. ' "0": "Red",',
  4821. ' Red: 0,',
  4822. ' "1": "Green",',
  4823. ' Green: 1,',
  4824. ' "2": "Blue",',
  4825. ' Blue: 2',
  4826. '};',
  4827. 'this.a = rtl.arraySetLength(null, 0, 2);',
  4828. 'this.b = [3, 4];',
  4829. 'this.c = [5, 6];',
  4830. '']),
  4831. LinesToStr([
  4832. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  4833. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  4834. '']));
  4835. end;
  4836. procedure TTestModule.TestEnum_ForIn;
  4837. begin
  4838. StartProgram(false);
  4839. Add([
  4840. 'type',
  4841. ' TEnum = (Red, Green, Blue);',
  4842. ' TEnumRg = green..blue;',
  4843. ' TArr = array[TEnum] of byte;',
  4844. ' TArrRg = array[TEnumRg] of byte;',
  4845. 'var',
  4846. ' e: TEnum;',
  4847. ' a1: TArr = (3,4,5);',
  4848. ' a2: TArrRg = (11,12);',
  4849. ' b: byte;',
  4850. 'begin',
  4851. ' for e in TEnum do ;',
  4852. ' for e in TEnumRg do ;',
  4853. ' for e in TArr do ;',
  4854. ' for e in TArrRg do ;',
  4855. ' for b in a1 do ;',
  4856. ' for b in a2 do ;',
  4857. '']);
  4858. ConvertProgram;
  4859. CheckSource('TestEnum_ForIn',
  4860. LinesToStr([ // statements
  4861. 'this.TEnum = {',
  4862. ' "0": "Red",',
  4863. ' Red: 0,',
  4864. ' "1": "Green",',
  4865. ' Green: 1,',
  4866. ' "2": "Blue",',
  4867. ' Blue: 2',
  4868. '};',
  4869. 'this.e = 0;',
  4870. 'this.a1 = [3, 4, 5];',
  4871. 'this.a2 = [11, 12];',
  4872. 'this.b = 0;',
  4873. '']),
  4874. LinesToStr([
  4875. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4876. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4877. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4878. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4879. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  4880. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  4881. '']));
  4882. end;
  4883. procedure TTestModule.TestEnum_ScopedNumber;
  4884. begin
  4885. Converter.Options:=Converter.Options+[coEnumNumbers];
  4886. StartProgram(false);
  4887. Add([
  4888. 'type',
  4889. ' TEnum = (Red, Green);',
  4890. 'var',
  4891. ' e: TEnum;',
  4892. 'begin',
  4893. ' e:=TEnum.Green;',
  4894. '']);
  4895. ConvertProgram;
  4896. CheckSource('TestEnum_ScopedNumber',
  4897. LinesToStr([ // statements
  4898. 'this.TEnum = {',
  4899. ' "0": "Red",',
  4900. ' Red: 0,',
  4901. ' "1": "Green",',
  4902. ' Green: 1',
  4903. '};',
  4904. 'this.e = 0;',
  4905. '']),
  4906. LinesToStr([
  4907. '$mod.e = 1;']));
  4908. end;
  4909. procedure TTestModule.TestEnum_InFunction;
  4910. begin
  4911. StartProgram(false);
  4912. Add([
  4913. 'const TEnum = 3;',
  4914. 'procedure DoIt;',
  4915. 'type',
  4916. ' TEnum = (Red, Green, Blue);',
  4917. ' procedure Sub;',
  4918. ' type',
  4919. ' TEnumSub = (Left, Right);',
  4920. ' var',
  4921. ' es: TEnumSub;',
  4922. ' begin',
  4923. ' es:=Left;',
  4924. ' end;',
  4925. 'var',
  4926. ' e, e2: TEnum;',
  4927. 'begin',
  4928. ' if e in [red,blue] then e2:=e;',
  4929. 'end;',
  4930. 'begin']);
  4931. ConvertProgram;
  4932. CheckSource('TestEnum_InFunction',
  4933. LinesToStr([ // statements
  4934. 'this.TEnum = 3;',
  4935. 'var TEnum$1 = {',
  4936. ' "0":"Red",',
  4937. ' Red:0,',
  4938. ' "1":"Green",',
  4939. ' Green:1,',
  4940. ' "2":"Blue",',
  4941. ' Blue:2',
  4942. ' };',
  4943. 'var TEnumSub = {',
  4944. ' "0": "Left",',
  4945. ' Left: 0,',
  4946. ' "1": "Right",',
  4947. ' Right: 1',
  4948. '};',
  4949. 'this.DoIt = function () {',
  4950. ' function Sub() {',
  4951. ' var es = 0;',
  4952. ' es = TEnumSub.Left;',
  4953. ' };',
  4954. ' var e = 0;',
  4955. ' var e2 = 0;',
  4956. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  4957. '};',
  4958. '']),
  4959. LinesToStr([
  4960. '']));
  4961. end;
  4962. procedure TTestModule.TestSet_Enum;
  4963. begin
  4964. StartProgram(false);
  4965. Add([
  4966. 'type',
  4967. ' TColor = (Red, Green, Blue);',
  4968. ' TColors = set of TColor;',
  4969. 'var',
  4970. ' c: TColor;',
  4971. ' s: TColors;',
  4972. ' t: TColors = [];',
  4973. ' u: TColors = [Red];',
  4974. 'begin',
  4975. ' s:=[];',
  4976. ' s:=[Green];',
  4977. ' s:=[Green,Blue];',
  4978. ' s:=[Red..Blue];',
  4979. ' s:=[Red,Green..Blue];',
  4980. ' s:=[Red,c];',
  4981. ' s:=t;',
  4982. ' s:=default(TColors);',
  4983. '']);
  4984. ConvertProgram;
  4985. CheckSource('TestSet',
  4986. LinesToStr([ // statements
  4987. 'this.TColor = {',
  4988. ' "0":"Red",',
  4989. ' Red:0,',
  4990. ' "1":"Green",',
  4991. ' Green:1,',
  4992. ' "2":"Blue",',
  4993. ' Blue:2',
  4994. ' };',
  4995. 'this.c = 0;',
  4996. 'this.s = {};',
  4997. 'this.t = {};',
  4998. 'this.u = rtl.createSet($mod.TColor.Red);'
  4999. ]),
  5000. LinesToStr([
  5001. '$mod.s={};',
  5002. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5003. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5004. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5005. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5006. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5007. '$mod.s=rtl.refSet($mod.t);',
  5008. '$mod.s={};',
  5009. '']));
  5010. end;
  5011. procedure TTestModule.TestSet_Operators;
  5012. begin
  5013. StartProgram(false);
  5014. Add('type');
  5015. Add(' TColor = (Red, Green, Blue);');
  5016. Add(' TColors = set of tcolor;');
  5017. Add('var');
  5018. Add(' vC: TColor;');
  5019. Add(' vS: TColors;');
  5020. Add(' vT: TColors;');
  5021. Add(' vU: TColors;');
  5022. Add(' B: boolean;');
  5023. Add('begin');
  5024. Add(' include(vs,green);');
  5025. Add(' exclude(vs,vc);');
  5026. Add(' vs:=vt+vu;');
  5027. Add(' vs:=vt+[red];');
  5028. Add(' vs:=[red]+vt;');
  5029. Add(' vs:=[red]+[green];');
  5030. Add(' vs:=vt-vu;');
  5031. Add(' vs:=vt-[red];');
  5032. Add(' vs:=[red]-vt;');
  5033. Add(' vs:=[red]-[green];');
  5034. Add(' vs:=vt*vu;');
  5035. Add(' vs:=vt*[red];');
  5036. Add(' vs:=[red]*vt;');
  5037. Add(' vs:=[red]*[green];');
  5038. Add(' vs:=vt><vu;');
  5039. Add(' vs:=vt><[red];');
  5040. Add(' vs:=[red]><vt;');
  5041. Add(' vs:=[red]><[green];');
  5042. Add(' b:=vt=vu;');
  5043. Add(' b:=vt=[red];');
  5044. Add(' b:=[red]=vt;');
  5045. Add(' b:=[red]=[green];');
  5046. Add(' b:=vt<>vu;');
  5047. Add(' b:=vt<>[red];');
  5048. Add(' b:=[red]<>vt;');
  5049. Add(' b:=[red]<>[green];');
  5050. Add(' b:=vt<=vu;');
  5051. Add(' b:=vt<=[red];');
  5052. Add(' b:=[red]<=vt;');
  5053. Add(' b:=[red]<=[green];');
  5054. Add(' b:=vt>=vu;');
  5055. Add(' b:=vt>=[red];');
  5056. Add(' b:=[red]>=vt;');
  5057. Add(' b:=[red]>=[green];');
  5058. ConvertProgram;
  5059. CheckSource('TestSet_Operators',
  5060. LinesToStr([ // statements
  5061. 'this.TColor = {',
  5062. ' "0":"Red",',
  5063. ' Red:0,',
  5064. ' "1":"Green",',
  5065. ' Green:1,',
  5066. ' "2":"Blue",',
  5067. ' Blue:2',
  5068. ' };',
  5069. 'this.vC = 0;',
  5070. 'this.vS = {};',
  5071. 'this.vT = {};',
  5072. 'this.vU = {};',
  5073. 'this.B = false;'
  5074. ]),
  5075. LinesToStr([
  5076. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5077. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5078. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5079. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5080. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5081. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5082. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5083. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5084. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5085. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5086. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5087. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5088. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5089. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5090. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5091. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5092. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5093. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5094. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5095. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5096. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5097. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5098. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5099. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5100. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5101. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5102. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5103. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5104. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5105. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5106. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5107. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5108. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5109. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5110. '']));
  5111. end;
  5112. procedure TTestModule.TestSet_Operator_In;
  5113. begin
  5114. StartProgram(false);
  5115. Add('type');
  5116. Add(' TColor = (Red, Green, Blue);');
  5117. Add(' TColors = set of tcolor;');
  5118. Add('var');
  5119. Add(' vC: tcolor;');
  5120. Add(' vT: tcolors;');
  5121. Add(' B: boolean;');
  5122. Add('begin');
  5123. Add(' b:=red in vt;');
  5124. Add(' b:=vc in vt;');
  5125. Add(' b:=green in [red..blue];');
  5126. Add(' b:=vc in [red..blue];');
  5127. Add(' ');
  5128. Add(' if red in vt then ;');
  5129. Add(' while vC in vt do ;');
  5130. Add(' repeat');
  5131. Add(' until vC in vt;');
  5132. ConvertProgram;
  5133. CheckSource('TestSet_Operator_In',
  5134. LinesToStr([ // statements
  5135. 'this.TColor = {',
  5136. ' "0":"Red",',
  5137. ' Red:0,',
  5138. ' "1":"Green",',
  5139. ' Green:1,',
  5140. ' "2":"Blue",',
  5141. ' Blue:2',
  5142. ' };',
  5143. 'this.vC = 0;',
  5144. 'this.vT = {};',
  5145. 'this.B = false;'
  5146. ]),
  5147. LinesToStr([
  5148. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5149. '$mod.B = $mod.vC in $mod.vT;',
  5150. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5151. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5152. 'if ($mod.TColor.Red in $mod.vT) ;',
  5153. 'while ($mod.vC in $mod.vT) {',
  5154. '};',
  5155. 'do {',
  5156. '} while (!($mod.vC in $mod.vT));',
  5157. '']));
  5158. end;
  5159. procedure TTestModule.TestSet_Functions;
  5160. begin
  5161. StartProgram(false);
  5162. Add('type');
  5163. Add(' TMyEnum = (Red, Green);');
  5164. Add(' TMyEnums = set of TMyEnum;');
  5165. Add('var');
  5166. Add(' e: TMyEnum;');
  5167. Add(' s: TMyEnums;');
  5168. Add('begin');
  5169. Add(' e:=Low(TMyEnums);');
  5170. Add(' e:=Low(s);');
  5171. Add(' e:=High(TMyEnums);');
  5172. Add(' e:=High(s);');
  5173. ConvertProgram;
  5174. CheckSource('TestSetFunctions',
  5175. LinesToStr([ // statements
  5176. 'this.TMyEnum = {',
  5177. ' "0":"Red",',
  5178. ' Red:0,',
  5179. ' "1":"Green",',
  5180. ' Green:1',
  5181. ' };',
  5182. 'this.e = 0;',
  5183. 'this.s = {};'
  5184. ]),
  5185. LinesToStr([
  5186. '$mod.e=$mod.TMyEnum.Red;',
  5187. '$mod.e=$mod.TMyEnum.Red;',
  5188. '$mod.e=$mod.TMyEnum.Green;',
  5189. '$mod.e=$mod.TMyEnum.Green;',
  5190. '']));
  5191. end;
  5192. procedure TTestModule.TestSet_PassAsArgClone;
  5193. begin
  5194. StartProgram(false);
  5195. Add('type');
  5196. Add(' TMyEnum = (Red, Green);');
  5197. Add(' TMyEnums = set of TMyEnum;');
  5198. Add('procedure DoDefault(s: tmyenums); begin end;');
  5199. Add('procedure DoConst(const s: tmyenums); begin end;');
  5200. Add('var');
  5201. Add(' aSet: tmyenums;');
  5202. Add('begin');
  5203. Add(' dodefault(aset);');
  5204. Add(' doconst(aset);');
  5205. ConvertProgram;
  5206. CheckSource('TestSetFunctions',
  5207. LinesToStr([ // statements
  5208. 'this.TMyEnum = {',
  5209. ' "0":"Red",',
  5210. ' Red:0,',
  5211. ' "1":"Green",',
  5212. ' Green:1',
  5213. ' };',
  5214. 'this.DoDefault = function (s) {',
  5215. '};',
  5216. 'this.DoConst = function (s) {',
  5217. '};',
  5218. 'this.aSet = {};'
  5219. ]),
  5220. LinesToStr([
  5221. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5222. '$mod.DoConst($mod.aSet);',
  5223. '']));
  5224. end;
  5225. procedure TTestModule.TestSet_AsParams;
  5226. begin
  5227. StartProgram(false);
  5228. Add([
  5229. 'type TEnum = (Red,Blue);',
  5230. 'type TEnums = set of TEnum;',
  5231. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5232. 'var vJ: TEnums;',
  5233. 'begin',
  5234. ' Include(vg,red);',
  5235. ' Include(result,blue);',
  5236. ' vg:=vg;',
  5237. ' vj:=vh;',
  5238. ' vi:=vi;',
  5239. ' doit(vg,vg,vg);',
  5240. ' doit(vh,vh,vj);',
  5241. ' doit(vi,vi,vi);',
  5242. ' doit(vj,vj,vj);',
  5243. 'end;',
  5244. 'var i: TEnums;',
  5245. 'begin',
  5246. ' doit(i,i,i);']);
  5247. ConvertProgram;
  5248. CheckSource('TestSet_AsParams',
  5249. LinesToStr([ // statements
  5250. 'this.TEnum = {',
  5251. ' "0": "Red",',
  5252. ' Red: 0,',
  5253. ' "1": "Blue",',
  5254. ' Blue: 1',
  5255. '};',
  5256. 'this.DoIt = function (vG,vH,vI) {',
  5257. ' var Result = {};',
  5258. ' var vJ = {};',
  5259. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5260. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5261. ' vG = rtl.refSet(vG);',
  5262. ' vJ = rtl.refSet(vH);',
  5263. ' vI.set(rtl.refSet(vI.get()));',
  5264. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5265. ' get: function () {',
  5266. ' return vG;',
  5267. ' },',
  5268. ' set: function (v) {',
  5269. ' vG = v;',
  5270. ' }',
  5271. ' });',
  5272. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5273. ' get: function () {',
  5274. ' return vJ;',
  5275. ' },',
  5276. ' set: function (v) {',
  5277. ' vJ = v;',
  5278. ' }',
  5279. ' });',
  5280. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5281. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5282. ' get: function () {',
  5283. ' return vJ;',
  5284. ' },',
  5285. ' set: function (v) {',
  5286. ' vJ = v;',
  5287. ' }',
  5288. ' });',
  5289. ' return Result;',
  5290. '};',
  5291. 'this.i = {};'
  5292. ]),
  5293. LinesToStr([
  5294. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5295. ' p: $mod,',
  5296. ' get: function () {',
  5297. ' return this.p.i;',
  5298. ' },',
  5299. ' set: function (v) {',
  5300. ' this.p.i = v;',
  5301. ' }',
  5302. '});'
  5303. ]));
  5304. end;
  5305. procedure TTestModule.TestSet_Property;
  5306. begin
  5307. StartProgram(false);
  5308. Add('type');
  5309. Add(' TEnum = (Red,Blue);');
  5310. Add(' TEnums = set of TEnum;');
  5311. Add(' TObject = class');
  5312. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5313. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5314. Add(' property Colors: TEnums read GetColors write SetColors;');
  5315. Add(' end;');
  5316. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5317. Add('begin end;');
  5318. Add('var Obj: TObject;');
  5319. Add('begin');
  5320. Add(' Include(Obj.Colors,Red);');
  5321. Add(' Exclude(Obj.Colors,Red);');
  5322. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5323. ConvertProgram;
  5324. CheckSource('TestSet_Property',
  5325. LinesToStr([ // statements
  5326. 'this.TEnum = {',
  5327. ' "0": "Red",',
  5328. ' Red: 0,',
  5329. ' "1": "Blue",',
  5330. ' Blue: 1',
  5331. '};',
  5332. 'rtl.createClass($mod, "TObject", null, function () {',
  5333. ' this.$init = function () {',
  5334. ' };',
  5335. ' this.$final = function () {',
  5336. ' };',
  5337. '});',
  5338. 'this.DoIt = function (i, j, k, l) {',
  5339. '};',
  5340. 'this.Obj = null;',
  5341. '']),
  5342. LinesToStr([
  5343. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5344. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5345. '']));
  5346. end;
  5347. procedure TTestModule.TestSet_EnumConst;
  5348. begin
  5349. StartProgram(false);
  5350. Add([
  5351. 'type',
  5352. ' TEnum = (Red,Blue);',
  5353. ' TEnums = set of TEnum;',
  5354. 'const',
  5355. ' Orange = red;',
  5356. 'var',
  5357. ' Enum: tenum;',
  5358. ' Enums: tenums;',
  5359. 'begin',
  5360. ' Include(enums,orange);',
  5361. ' Exclude(enums,orange);',
  5362. ' if orange in enums then;',
  5363. ' if orange in [orange,red] then;']);
  5364. ConvertProgram;
  5365. CheckSource('TestSet_EnumConst',
  5366. LinesToStr([ // statements
  5367. 'this.TEnum = {',
  5368. ' "0": "Red",',
  5369. ' Red: 0,',
  5370. ' "1": "Blue",',
  5371. ' Blue: 1',
  5372. '};',
  5373. 'this.Orange = $mod.TEnum.Red;',
  5374. 'this.Enum = 0;',
  5375. 'this.Enums = {};',
  5376. '']),
  5377. LinesToStr([
  5378. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5379. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5380. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5381. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5382. '']));
  5383. end;
  5384. procedure TTestModule.TestSet_IntConst;
  5385. begin
  5386. StartProgram(false);
  5387. Add([
  5388. 'type',
  5389. ' TEnums = set of Byte;',
  5390. 'const',
  5391. ' Orange = 0;',
  5392. 'var',
  5393. ' Enum: byte;',
  5394. ' Enums: tenums;',
  5395. 'begin',
  5396. ' Enums:=[];',
  5397. ' Enums:=[0];',
  5398. ' Enums:=[1..2];',
  5399. //' Include(enums,orange);',
  5400. //' Exclude(enums,orange);',
  5401. ' if orange in enums then;',
  5402. ' if orange in [orange,1] then;']);
  5403. ConvertProgram;
  5404. CheckSource('TestSet_IntConst',
  5405. LinesToStr([ // statements
  5406. 'this.Orange = 0;',
  5407. 'this.Enum = 0;',
  5408. 'this.Enums = {};',
  5409. '']),
  5410. LinesToStr([
  5411. '$mod.Enums = {};',
  5412. '$mod.Enums = rtl.createSet(0);',
  5413. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5414. 'if (0 in $mod.Enums) ;',
  5415. 'if (0 in rtl.createSet(0, 1)) ;',
  5416. '']));
  5417. end;
  5418. procedure TTestModule.TestSet_AnonymousEnumType;
  5419. begin
  5420. StartProgram(false);
  5421. Add('type');
  5422. Add(' TFlags = set of (red, green);');
  5423. Add('const');
  5424. Add(' favorite = red;');
  5425. Add('var');
  5426. Add(' f: TFlags;');
  5427. Add(' i: longint;');
  5428. Add('begin');
  5429. Add(' Include(f,red);');
  5430. Add(' Include(f,favorite);');
  5431. Add(' i:=ord(red);');
  5432. Add(' i:=ord(favorite);');
  5433. Add(' i:=ord(low(TFlags));');
  5434. Add(' i:=ord(low(f));');
  5435. Add(' i:=ord(low(favorite));');
  5436. Add(' i:=ord(high(TFlags));');
  5437. Add(' i:=ord(high(f));');
  5438. Add(' i:=ord(high(favorite));');
  5439. Add(' f:=[green,favorite];');
  5440. ConvertProgram;
  5441. CheckSource('TestSet_AnonymousEnumType',
  5442. LinesToStr([ // statements
  5443. 'this.TFlags$a = {',
  5444. ' "0": "red",',
  5445. ' red: 0,',
  5446. ' "1": "green",',
  5447. ' green: 1',
  5448. '};',
  5449. 'this.favorite = $mod.TFlags$a.red;',
  5450. 'this.f = {};',
  5451. 'this.i = 0;',
  5452. '']),
  5453. LinesToStr([
  5454. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5455. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5456. '$mod.i = $mod.TFlags$a.red;',
  5457. '$mod.i = $mod.TFlags$a.red;',
  5458. '$mod.i = $mod.TFlags$a.red;',
  5459. '$mod.i = $mod.TFlags$a.red;',
  5460. '$mod.i = $mod.TFlags$a.red;',
  5461. '$mod.i = $mod.TFlags$a.green;',
  5462. '$mod.i = $mod.TFlags$a.green;',
  5463. '$mod.i = $mod.TFlags$a.green;',
  5464. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5465. '']));
  5466. end;
  5467. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5468. begin
  5469. exit;
  5470. StartProgram(false);
  5471. Add([
  5472. 'type',
  5473. ' TAtoZ = ''A''..''Z'';',
  5474. ' TSetOfAZ = set of TAtoZ;',
  5475. 'var',
  5476. ' c: char;',
  5477. ' a: TAtoZ;',
  5478. ' s: TSetOfAZ = [''P'',''A''];',
  5479. ' i: longint;',
  5480. 'begin',
  5481. ' Include(s,''S'');',
  5482. ' Include(s,c);',
  5483. ' Include(s,a);',
  5484. ' c:=low(TAtoZ);',
  5485. ' i:=ord(low(TAtoZ));',
  5486. ' a:=high(TAtoZ);',
  5487. ' a:=high(TSetOfAtoZ);',
  5488. ' s:=[a,c,''M''];',
  5489. '']);
  5490. ConvertProgram;
  5491. CheckSource('TestSet_AnonymousEnumTypeChar',
  5492. LinesToStr([ // statements
  5493. '']),
  5494. LinesToStr([
  5495. '']));
  5496. end;
  5497. procedure TTestModule.TestSet_ConstEnum;
  5498. begin
  5499. StartProgram(false);
  5500. Add([
  5501. 'type',
  5502. ' TEnum = (red,blue,green);',
  5503. ' TEnums = set of TEnum;',
  5504. 'const',
  5505. ' teAny = [low(TEnum)..high(TEnum)];',
  5506. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5507. 'var',
  5508. ' e: TEnum;',
  5509. ' s: TEnums;',
  5510. 'begin',
  5511. ' if blue in teAny then;',
  5512. ' if blue in teAny+[e] then;',
  5513. ' if blue in teAny+teRedBlue then;',
  5514. ' if e in [red,blue] then;',
  5515. ' s:=teAny;',
  5516. ' s:=teAny+[e];',
  5517. ' s:=[e]+teAny;',
  5518. ' s:=teAny+teRedBlue;',
  5519. ' s:=teAny+teRedBlue+[e];',
  5520. '']);
  5521. ConvertProgram;
  5522. CheckSource('TestSet_ConstEnum',
  5523. LinesToStr([ // statements
  5524. 'this.TEnum = {',
  5525. ' "0": "red",',
  5526. ' red: 0,',
  5527. ' "1": "blue",',
  5528. ' blue: 1,',
  5529. ' "2": "green",',
  5530. ' green: 2',
  5531. '};',
  5532. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5533. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5534. 'this.e = 0;',
  5535. 'this.s = {};',
  5536. '']),
  5537. LinesToStr([
  5538. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5539. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5540. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5541. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5542. '$mod.s = rtl.refSet($mod.teAny);',
  5543. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5544. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5545. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5546. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5547. '']));
  5548. end;
  5549. procedure TTestModule.TestSet_ConstChar;
  5550. begin
  5551. StartProgram(false);
  5552. Add([
  5553. 'const',
  5554. ' LowChars = [''a''..''z''];',
  5555. ' Chars = LowChars+[''A''..''Z''];',
  5556. ' sc = [''А'', ''Я''];',
  5557. 'var',
  5558. ' c: char;',
  5559. ' s: string;',
  5560. 'begin',
  5561. ' if c in lowchars then ;',
  5562. ' if ''a'' in lowchars then ;',
  5563. ' if s[1] in lowchars then ;',
  5564. ' if c in chars then ;',
  5565. ' if c in [''a''..''z'',''_''] then ;',
  5566. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5567. ' if ''Я'' in sc then ;',
  5568. '']);
  5569. ConvertProgram;
  5570. CheckSource('TestSet_ConstChar',
  5571. LinesToStr([ // statements
  5572. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5573. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5574. 'this.sc = rtl.createSet(1040, 1071);',
  5575. 'this.c = "";',
  5576. 'this.s = "";',
  5577. '']),
  5578. LinesToStr([
  5579. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5580. 'if (97 in $mod.LowChars) ;',
  5581. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5582. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5583. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5584. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5585. 'if (1071 in $mod.sc) ;',
  5586. '']));
  5587. end;
  5588. procedure TTestModule.TestSet_ConstInt;
  5589. begin
  5590. StartProgram(false);
  5591. Add([
  5592. 'const',
  5593. ' Months = [1..12];',
  5594. ' Mirror = [-12..-1]+Months;',
  5595. 'var',
  5596. ' i: smallint;',
  5597. 'begin',
  5598. ' if 3 in Months then;',
  5599. ' if i in Months+[i] then;',
  5600. ' if i in Months+Mirror then;',
  5601. ' if i in [4..6,8] then;',
  5602. '']);
  5603. ConvertProgram;
  5604. CheckSource('TestSet_ConstInt',
  5605. LinesToStr([ // statements
  5606. 'this.Months = rtl.createSet(null, 1, 12);',
  5607. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5608. 'this.i = 0;',
  5609. '']),
  5610. LinesToStr([
  5611. 'if (3 in $mod.Months) ;',
  5612. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5613. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5614. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5615. '']));
  5616. end;
  5617. procedure TTestModule.TestSet_InFunction;
  5618. begin
  5619. StartProgram(false);
  5620. Add([
  5621. 'const',
  5622. ' TEnum = 3;',
  5623. ' TSetOfEnum = 4;',
  5624. ' TSetOfAno = 5;',
  5625. 'procedure DoIt;',
  5626. 'type',
  5627. ' TEnum = (red, blue);',
  5628. ' TSetOfEnum = set of TEnum;',
  5629. ' TSetOfAno = set of (up,down);',
  5630. 'var',
  5631. ' e: TEnum;',
  5632. ' se: TSetOfEnum;',
  5633. ' sa: TSetOfAno;',
  5634. 'begin',
  5635. ' se:=[e];',
  5636. ' sa:=[up];',
  5637. 'end;',
  5638. 'begin',
  5639. '']);
  5640. ConvertProgram;
  5641. CheckSource('TestSet_InFunction',
  5642. LinesToStr([ // statements
  5643. 'this.TEnum = 3;',
  5644. 'this.TSetOfEnum = 4;',
  5645. 'this.TSetOfAno = 5;',
  5646. 'var TEnum$1 = {',
  5647. ' "0": "red",',
  5648. ' red: 0,',
  5649. ' "1": "blue",',
  5650. ' blue: 1',
  5651. '};',
  5652. 'var TSetOfAno$a = {',
  5653. ' "0": "up",',
  5654. ' up: 0,',
  5655. ' "1": "down",',
  5656. ' down: 1',
  5657. '};',
  5658. 'this.DoIt = function () {',
  5659. ' var e = 0;',
  5660. ' var se = {};',
  5661. ' var sa = {};',
  5662. ' se = rtl.createSet(e);',
  5663. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5664. '};',
  5665. '']),
  5666. LinesToStr([
  5667. '']));
  5668. end;
  5669. procedure TTestModule.TestSet_ForIn;
  5670. begin
  5671. StartProgram(false);
  5672. Add([
  5673. 'type',
  5674. ' TEnum = (Red, Green, Blue);',
  5675. ' TEnumRg = green..blue;',
  5676. ' TSetOfEnum = set of TEnum;',
  5677. ' TSetOfEnumRg = set of TEnumRg;',
  5678. 'var',
  5679. ' e, e2: TEnum;',
  5680. ' er: TEnum;',
  5681. ' s: TSetOfEnum;',
  5682. 'begin',
  5683. ' for e in TSetOfEnum do ;',
  5684. ' for e in TSetOfEnumRg do ;',
  5685. ' for e in [] do e2:=e;',
  5686. ' for e in [red..green] do e2:=e;',
  5687. ' for e in [green,blue] do e2:=e;',
  5688. ' for e in [red,blue] do e2:=e;',
  5689. ' for e in s do e2:=e;',
  5690. ' for er in TSetOfEnumRg do ;',
  5691. '']);
  5692. ConvertProgram;
  5693. CheckSource('TestSet_ForIn',
  5694. LinesToStr([ // statements
  5695. 'this.TEnum = {',
  5696. ' "0":"Red",',
  5697. ' Red:0,',
  5698. ' "1":"Green",',
  5699. ' Green:1,',
  5700. ' "2":"Blue",',
  5701. ' Blue:2',
  5702. ' };',
  5703. 'this.e = 0;',
  5704. 'this.e2 = 0;',
  5705. 'this.er = 0;',
  5706. 'this.s = {};',
  5707. '']),
  5708. LinesToStr([
  5709. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5710. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5711. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5712. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5713. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5714. 'for (var $l1 in $mod.s){',
  5715. ' $mod.e = +$l1;',
  5716. ' $mod.e2 = $mod.e;',
  5717. '};',
  5718. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5719. '']));
  5720. end;
  5721. procedure TTestModule.TestNestBegin;
  5722. begin
  5723. StartProgram(false);
  5724. Add('begin');
  5725. Add(' begin');
  5726. Add(' begin');
  5727. Add(' end;');
  5728. Add(' begin');
  5729. Add(' if true then ;');
  5730. Add(' end;');
  5731. Add(' end;');
  5732. ConvertProgram;
  5733. CheckSource('TestNestBegin',
  5734. '',
  5735. 'if (true) ;');
  5736. end;
  5737. procedure TTestModule.TestUnitImplVars;
  5738. begin
  5739. StartUnit(false);
  5740. Add('interface');
  5741. Add('implementation');
  5742. Add('var');
  5743. Add(' V1:longint;');
  5744. Add(' V2:longint = 3;');
  5745. Add(' V3:string = ''abc'';');
  5746. ConvertUnit;
  5747. CheckSource('TestUnitImplVars',
  5748. LinesToStr([ // statements
  5749. 'var $impl = $mod.$impl;',
  5750. '']),
  5751. '', // this.$init
  5752. LinesToStr([ // implementation
  5753. '$impl.V1 = 0;',
  5754. '$impl.V2 = 3;',
  5755. '$impl.V3 = "abc";',
  5756. '']) );
  5757. end;
  5758. procedure TTestModule.TestUnitImplConsts;
  5759. begin
  5760. StartUnit(false);
  5761. Add('interface');
  5762. Add('implementation');
  5763. Add('const');
  5764. Add(' v1 = 3;');
  5765. Add(' v2:longint = 4;');
  5766. Add(' v3:string = ''abc'';');
  5767. ConvertUnit;
  5768. CheckSource('TestUnitImplConsts',
  5769. LinesToStr([ // statements
  5770. 'var $impl = $mod.$impl;',
  5771. '']),
  5772. '', // this.$init
  5773. LinesToStr([ // implementation
  5774. '$impl.v1 = 3;',
  5775. '$impl.v2 = 4;',
  5776. '$impl.v3 = "abc";',
  5777. '']) );
  5778. end;
  5779. procedure TTestModule.TestUnitImplRecord;
  5780. begin
  5781. StartUnit(false);
  5782. Add('interface');
  5783. Add('implementation');
  5784. Add('type');
  5785. Add(' TMyRecord = record');
  5786. Add(' i: longint;');
  5787. Add(' end;');
  5788. Add('var aRec: TMyRecord;');
  5789. Add('initialization');
  5790. Add(' arec.i:=3;');
  5791. ConvertUnit;
  5792. CheckSource('TestUnitImplRecord',
  5793. LinesToStr([ // statements
  5794. 'var $impl = $mod.$impl;',
  5795. '']),
  5796. // this.$init
  5797. '$impl.aRec.i = 3;',
  5798. LinesToStr([ // implementation
  5799. 'rtl.recNewT($impl, "TMyRecord", function () {',
  5800. ' this.i = 0;',
  5801. ' this.$eq = function (b) {',
  5802. ' return this.i === b.i;',
  5803. ' };',
  5804. ' this.$assign = function (s) {',
  5805. ' this.i = s.i;',
  5806. ' return this;',
  5807. ' };',
  5808. '});',
  5809. '$impl.aRec = $impl.TMyRecord.$new();',
  5810. '']) );
  5811. end;
  5812. procedure TTestModule.TestRenameJSNameConflict;
  5813. begin
  5814. StartProgram(false);
  5815. Add('var apply: longint;');
  5816. Add('var bind: longint;');
  5817. Add('var call: longint;');
  5818. Add('begin');
  5819. ConvertProgram;
  5820. CheckSource('TestRenameJSNameConflict',
  5821. LinesToStr([ // statements
  5822. 'this.Apply = 0;',
  5823. 'this.Bind = 0;',
  5824. 'this.Call = 0;'
  5825. ]),
  5826. LinesToStr([ // this.$main
  5827. ''
  5828. ]));
  5829. end;
  5830. procedure TTestModule.TestLocalConst;
  5831. begin
  5832. StartProgram(false);
  5833. Add('procedure DoIt;');
  5834. Add('const');
  5835. Add(' cA: longint = 1;');
  5836. Add(' cB = 2;');
  5837. Add(' procedure Sub;');
  5838. Add(' const');
  5839. Add(' csA = 3;');
  5840. Add(' cB: double = 4;');
  5841. Add(' begin');
  5842. Add(' cb:=cb+csa;');
  5843. Add(' ca:=ca+csa+5;');
  5844. Add(' end;');
  5845. Add('begin');
  5846. Add(' ca:=ca+cb+6;');
  5847. Add('end;');
  5848. Add('begin');
  5849. ConvertProgram;
  5850. CheckSource('TestLocalConst',
  5851. LinesToStr([
  5852. 'var cA = 1;',
  5853. 'var cB = 2;',
  5854. 'var csA = 3;',
  5855. 'var cB$1 = 4;',
  5856. 'this.DoIt = function () {',
  5857. ' function Sub() {',
  5858. ' cB$1 = cB$1 + 3;',
  5859. ' cA = cA + 3 + 5;',
  5860. ' };',
  5861. ' cA = cA + 2 + 6;',
  5862. '};'
  5863. ]),
  5864. LinesToStr([
  5865. ]));
  5866. end;
  5867. procedure TTestModule.TestVarExternal;
  5868. begin
  5869. StartProgram(false);
  5870. Add('var');
  5871. Add(' NaN: double; external name ''Global.NaN'';');
  5872. Add(' d: double;');
  5873. Add('begin');
  5874. Add(' d:=NaN;');
  5875. ConvertProgram;
  5876. CheckSource('TestVarExternal',
  5877. LinesToStr([
  5878. 'this.d = 0.0;'
  5879. ]),
  5880. LinesToStr([
  5881. '$mod.d = Global.NaN;'
  5882. ]));
  5883. end;
  5884. procedure TTestModule.TestVarExternalOtherUnit;
  5885. begin
  5886. AddModuleWithIntfImplSrc('unit2.pas',
  5887. LinesToStr([
  5888. 'var NaN: double; external name ''Global.NaN'';',
  5889. 'var iV: longint;'
  5890. ]),
  5891. '');
  5892. StartUnit(true);
  5893. Add('interface');
  5894. Add('uses unit2;');
  5895. Add('implementation');
  5896. Add('var');
  5897. Add(' d: double;');
  5898. Add(' i: longint; external name ''$i'';');
  5899. Add('begin');
  5900. Add(' d:=nan;');
  5901. Add(' d:=uNit2.nan;');
  5902. Add(' d:=test1.d;');
  5903. Add(' i:=iv;');
  5904. Add(' i:=uNit2.iv;');
  5905. Add(' i:=test1.i;');
  5906. ConvertUnit;
  5907. CheckSource('TestVarExternalOtherUnit',
  5908. LinesToStr([
  5909. 'var $impl = $mod.$impl;',
  5910. '']),
  5911. LinesToStr([ // this.$init
  5912. '$impl.d = Global.NaN;',
  5913. '$impl.d = Global.NaN;',
  5914. '$impl.d = $impl.d;',
  5915. '$i = pas.unit2.iV;',
  5916. '$i = pas.unit2.iV;',
  5917. '$i = $i;',
  5918. '']),
  5919. LinesToStr([ // implementation
  5920. '$impl.d = 0.0;',
  5921. '']) );
  5922. end;
  5923. procedure TTestModule.TestVarAbsoluteFail;
  5924. begin
  5925. StartProgram(false);
  5926. Add([
  5927. 'var',
  5928. ' a: longint;',
  5929. ' b: longword absolute a;',
  5930. 'begin']);
  5931. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  5932. ConvertProgram;
  5933. end;
  5934. procedure TTestModule.TestConstExternal;
  5935. begin
  5936. StartProgram(false);
  5937. Add([
  5938. 'const',
  5939. ' PI: double; external name ''Global.PI'';',
  5940. ' Tau = 2*pi;',
  5941. 'var d: double;',
  5942. 'begin',
  5943. ' d:=pi;',
  5944. ' d:=tau+pi;']);
  5945. ConvertProgram;
  5946. CheckSource('TestConstExternal',
  5947. LinesToStr([
  5948. 'this.Tau = 2*Global.PI;',
  5949. 'this.d = 0.0;'
  5950. ]),
  5951. LinesToStr([
  5952. '$mod.d = Global.PI;',
  5953. '$mod.d = $mod.Tau + Global.PI;'
  5954. ]));
  5955. end;
  5956. procedure TTestModule.TestDouble;
  5957. begin
  5958. StartProgram(false);
  5959. Add([
  5960. 'type',
  5961. ' TDateTime = double;',
  5962. 'const',
  5963. ' a = TDateTime(2.7);',
  5964. ' b = a + TDateTime(1.7);',
  5965. ' c = 0.9 + 0.1;',
  5966. ' f0_1 = 0.1;',
  5967. ' f0_3 = 0.3;',
  5968. ' fn0_1 = -0.1;',
  5969. ' fn0_3 = -0.3;',
  5970. ' fn0_003 = -0.003;',
  5971. ' fn0_123456789 = -0.123456789;',
  5972. ' fn300_0 = -300.0;',
  5973. ' fn123456_0 = -123456.0;',
  5974. ' fn1234567_8 = -1234567.8;',
  5975. ' fn12345678_9 = -12345678.9;',
  5976. ' f1_0En12 = 1E-12;',
  5977. ' fn1_0En12 = -1E-12;',
  5978. ' maxdouble = 1.7e+308;',
  5979. ' mindouble = -1.7e+308;',
  5980. ' MinSafeIntDouble = -$1fffffffffffff;',
  5981. ' MinSafeIntDouble2 = -$20000000000000-1;',
  5982. ' MaxSafeIntDouble = $1fffffffffffff;',
  5983. ' DZeroResolution = 1E-12;',
  5984. ' Minus1 = -1E-12;',
  5985. ' EPS = 1E-9;',
  5986. ' DELTA = 0.001;',
  5987. ' Big = 129.789E+100;',
  5988. ' Test0_15 = 0.15;',
  5989. ' Test999 = 2.9999999999999;',
  5990. ' Test111999 = 211199999999999000.0;',
  5991. ' TestMinus111999 = -211199999999999000.0;',
  5992. 'var',
  5993. ' d: double = b;',
  5994. 'begin',
  5995. ' d:=1.0;',
  5996. ' d:=1.0/3.0;',
  5997. ' d:=1/3;',
  5998. ' d:=5.0E-324;',
  5999. ' d:=1.7E308;',
  6000. ' d:=001.00E00;',
  6001. ' d:=002.00E001;',
  6002. ' d:=003.000E000;',
  6003. ' d:=-004.00E-00;',
  6004. ' d:=-005.00E-001;',
  6005. ' d:=10**3;',
  6006. ' d:=10 mod 3;',
  6007. ' d:=10 div 3;',
  6008. ' d:=c;',
  6009. ' d:=f0_1;',
  6010. ' d:=f0_3;',
  6011. ' d:=fn0_1;',
  6012. ' d:=fn0_3;',
  6013. ' d:=fn0_003;',
  6014. ' d:=fn0_123456789;',
  6015. ' d:=fn300_0;',
  6016. ' d:=fn123456_0;',
  6017. ' d:=fn1234567_8;',
  6018. ' d:=fn12345678_9;',
  6019. ' d:=f1_0En12;',
  6020. ' d:=fn1_0En12;',
  6021. ' d:=maxdouble;',
  6022. ' d:=mindouble;',
  6023. ' d:=MinSafeIntDouble;',
  6024. ' d:=double(MinSafeIntDouble);',
  6025. ' d:=MinSafeIntDouble2;',
  6026. ' d:=double(MinSafeIntDouble2);',
  6027. ' d:=MaxSafeIntDouble;',
  6028. ' d:=default(double);',
  6029. '']);
  6030. ConvertProgram;
  6031. CheckSource('TestDouble',
  6032. LinesToStr([
  6033. 'this.a = 2.7;',
  6034. 'this.b = 2.7 + 1.7;',
  6035. 'this.c = 0.9 + 0.1;',
  6036. 'this.f0_1 = 0.1;',
  6037. 'this.f0_3 = 0.3;',
  6038. 'this.fn0_1 = -0.1;',
  6039. 'this.fn0_3 = -0.3;',
  6040. 'this.fn0_003 = -0.003;',
  6041. 'this.fn0_123456789 = -0.123456789;',
  6042. 'this.fn300_0 = -300.0;',
  6043. 'this.fn123456_0 = -123456.0;',
  6044. 'this.fn1234567_8 = -1234567.8;',
  6045. 'this.fn12345678_9 = -12345678.9;',
  6046. 'this.f1_0En12 = 1E-12;',
  6047. 'this.fn1_0En12 = -1E-12;',
  6048. 'this.maxdouble = 1.7e+308;',
  6049. 'this.mindouble = -1.7e+308;',
  6050. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6051. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6052. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6053. 'this.DZeroResolution = 1E-12;',
  6054. 'this.Minus1 = -1E-12;',
  6055. 'this.EPS = 1E-9;',
  6056. 'this.DELTA = 0.001;',
  6057. 'this.Big = 129.789E+100;',
  6058. 'this.Test0_15 = 0.15;',
  6059. 'this.Test999 = 2.9999999999999;',
  6060. 'this.Test111999 = 211199999999999000.0;',
  6061. 'this.TestMinus111999 = -211199999999999000.0;',
  6062. 'this.d = 4.4;'
  6063. ]),
  6064. LinesToStr([
  6065. '$mod.d = 1.0;',
  6066. '$mod.d = 1.0 / 3.0;',
  6067. '$mod.d = 1 / 3;',
  6068. '$mod.d = 5.0E-324;',
  6069. '$mod.d = 1.7E308;',
  6070. '$mod.d = 1.00E0;',
  6071. '$mod.d = 2.00E1;',
  6072. '$mod.d = 3.000E0;',
  6073. '$mod.d = -4.00E-0;',
  6074. '$mod.d = -5.00E-1;',
  6075. '$mod.d = Math.pow(10, 3);',
  6076. '$mod.d = 10 % 3;',
  6077. '$mod.d = Math.floor(10 / 3);',
  6078. '$mod.d = 1;',
  6079. '$mod.d = 0.1;',
  6080. '$mod.d = 0.3;',
  6081. '$mod.d = -0.1;',
  6082. '$mod.d = -0.3;',
  6083. '$mod.d = -0.003;',
  6084. '$mod.d = -0.123456789;',
  6085. '$mod.d = -300;',
  6086. '$mod.d = -123456;',
  6087. '$mod.d = -1234567.8;',
  6088. '$mod.d = -1.23456789E7;',
  6089. '$mod.d = 1E-12;',
  6090. '$mod.d = -1E-12;',
  6091. '$mod.d = 1.7E308;',
  6092. '$mod.d = -1.7E308;',
  6093. '$mod.d = -9007199254740991;',
  6094. '$mod.d = -9007199254740991;',
  6095. '$mod.d = -9.007199254740992E15;',
  6096. '$mod.d = -9.007199254740992E15;',
  6097. '$mod.d = 9007199254740991;',
  6098. '$mod.d = 0.0;',
  6099. '']));
  6100. end;
  6101. procedure TTestModule.TestInteger;
  6102. begin
  6103. StartProgram(false);
  6104. Add([
  6105. 'const',
  6106. ' MinInt = low(NativeInt);',
  6107. ' MaxInt = high(NativeInt);',
  6108. 'type',
  6109. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6110. 'const',
  6111. ' a = low(TMyInt)+High(TMyInt);',
  6112. 'var',
  6113. ' i: TMyInt;',
  6114. 'begin',
  6115. ' i:=-MinInt;',
  6116. ' i:=default(TMyInt);',
  6117. ' i:=low(i)+high(i);',
  6118. '']);
  6119. ConvertProgram;
  6120. CheckSource('TestIntegerRange',
  6121. LinesToStr([
  6122. 'this.MinInt = -9007199254740991;',
  6123. 'this.MaxInt = 9007199254740991;',
  6124. 'this.a = -9007199254740991 + 9007199254740991;',
  6125. 'this.i = 0;',
  6126. '']),
  6127. LinesToStr([
  6128. '$mod.i = - -9007199254740991;',
  6129. '$mod.i = -9007199254740991;',
  6130. '$mod.i = -9007199254740991 + 9007199254740991;',
  6131. '']));
  6132. end;
  6133. procedure TTestModule.TestIntegerRange;
  6134. begin
  6135. StartProgram(false);
  6136. Add([
  6137. 'const',
  6138. ' MinInt = -1;',
  6139. ' MaxInt = +1;',
  6140. 'type',
  6141. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6142. ' TInt2 = 1..3;',
  6143. 'const',
  6144. ' a = low(TMyInt)+High(TMyInt);',
  6145. ' b = low(TInt2)+High(TInt2);',
  6146. ' s1 = [1];',
  6147. ' s2 = [1,2];',
  6148. ' s3 = [1..3];',
  6149. ' s4 = [low(shortint)..high(shortint)];',
  6150. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6151. ' s6 = 1 in s2;',
  6152. 'var',
  6153. ' i: TMyInt;',
  6154. ' i2: TInt2;',
  6155. 'begin',
  6156. ' i:=i2;',
  6157. ' i:=default(TMyInt);',
  6158. ' if i=i2 then ;']);
  6159. ConvertProgram;
  6160. CheckSource('TestIntegerRange',
  6161. LinesToStr([
  6162. 'this.MinInt = -1;',
  6163. 'this.MaxInt = +1;',
  6164. 'this.a = -1 + 1;',
  6165. 'this.b = 1 + 3;',
  6166. 'this.s1 = rtl.createSet(1);',
  6167. 'this.s2 = rtl.createSet(1, 2);',
  6168. 'this.s3 = rtl.createSet(null, 1, 3);',
  6169. 'this.s4 = rtl.createSet(null, -128, 127);',
  6170. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6171. 'this.s6 = 1 in $mod.s2;',
  6172. 'this.i = 0;',
  6173. 'this.i2 = 0;',
  6174. '']),
  6175. LinesToStr([
  6176. '$mod.i = $mod.i2;',
  6177. '$mod.i = -1;',
  6178. 'if ($mod.i === $mod.i2) ;',
  6179. '']));
  6180. end;
  6181. procedure TTestModule.TestIntegerTypecasts;
  6182. begin
  6183. StartProgram(false);
  6184. Add([
  6185. 'var',
  6186. ' i: nativeint;',
  6187. ' b: byte;',
  6188. ' sh: shortint;',
  6189. ' w: word;',
  6190. ' sm: smallint;',
  6191. ' lw: longword;',
  6192. ' li: longint;',
  6193. 'begin',
  6194. ' b:=byte(i);',
  6195. ' sh:=shortint(i);',
  6196. ' w:=word(i);',
  6197. ' sm:=smallint(i);',
  6198. ' lw:=longword(i);',
  6199. ' li:=longint(i);',
  6200. '']);
  6201. ConvertProgram;
  6202. CheckSource('TestIntegerTypecasts',
  6203. LinesToStr([
  6204. 'this.i = 0;',
  6205. 'this.b = 0;',
  6206. 'this.sh = 0;',
  6207. 'this.w = 0;',
  6208. 'this.sm = 0;',
  6209. 'this.lw = 0;',
  6210. 'this.li = 0;',
  6211. '']),
  6212. LinesToStr([
  6213. '$mod.b = $mod.i & 255;',
  6214. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6215. '$mod.w = $mod.i & 65535;',
  6216. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6217. '$mod.lw = $mod.i >>> 0;',
  6218. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6219. '']));
  6220. end;
  6221. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6222. begin
  6223. StartProgram(false);
  6224. Add([
  6225. 'var',
  6226. ' i,j: nativeint;',
  6227. 'begin',
  6228. ' i:=i shr 0;',
  6229. ' i:=i shr 1;',
  6230. ' i:=i shr 3;',
  6231. ' i:=i shr 54;',
  6232. ' i:=j shr i;',
  6233. '']);
  6234. ConvertProgram;
  6235. CheckResolverUnexpectedHints;
  6236. CheckSource('TestInteger_BitwiseShrNativeInt',
  6237. LinesToStr([
  6238. 'this.i = 0;',
  6239. 'this.j = 0;',
  6240. '']),
  6241. LinesToStr([
  6242. '$mod.i = $mod.i;',
  6243. '$mod.i = Math.floor($mod.i / 2);',
  6244. '$mod.i = Math.floor($mod.i / 8);',
  6245. '$mod.i = 0;',
  6246. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6247. '']));
  6248. end;
  6249. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6250. begin
  6251. StartProgram(false);
  6252. Add([
  6253. 'var',
  6254. ' i: nativeint;',
  6255. 'begin',
  6256. ' i:=i shl 0;',
  6257. ' i:=i shl 54;',
  6258. ' i:=123456789012 shl 1;',
  6259. ' i:=i shl 1;',
  6260. '']);
  6261. ConvertProgram;
  6262. CheckResolverUnexpectedHints;
  6263. CheckSource('TestInteger_BitwiseShrNativeInt',
  6264. LinesToStr([
  6265. 'this.i = 0;',
  6266. '']),
  6267. LinesToStr([
  6268. '$mod.i = $mod.i;',
  6269. '$mod.i = 0;',
  6270. '$mod.i = 246913578024;',
  6271. '$mod.i = rtl.shl($mod.i, 1);',
  6272. '']));
  6273. end;
  6274. procedure TTestModule.TestCurrency;
  6275. begin
  6276. StartProgram(false);
  6277. Add([
  6278. 'type',
  6279. ' TCoin = currency;',
  6280. 'const',
  6281. ' a = TCoin(2.7);',
  6282. ' b = a + TCoin(1.7);',
  6283. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6284. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6285. 'var',
  6286. ' c: TCoin = b;',
  6287. ' i: nativeint;',
  6288. ' d: double;',
  6289. ' j: jsvalue;',
  6290. 'function DoIt(c: currency): currency; begin end;',
  6291. 'function GetIt(d: double): double; begin end;',
  6292. 'procedure Write(v: jsvalue); begin end;',
  6293. 'begin',
  6294. ' c:=1.0;',
  6295. ' c:=0.1;',
  6296. ' c:=1.0/3.0;',
  6297. ' c:=1/3;',
  6298. ' c:=a;',
  6299. ' d:=c;',
  6300. ' c:=d;',
  6301. ' c:=currency(c);',
  6302. ' c:=currency(d);',
  6303. ' d:=double(c);',
  6304. ' c:=i;',
  6305. ' c:=currency(i);',
  6306. //' i:=c;', not allowed
  6307. ' i:=nativeint(c);',
  6308. ' c:=c+a;',
  6309. ' c:=-c-a;',
  6310. ' c:=d+c;',
  6311. ' c:=c+d;',
  6312. ' c:=d-c;',
  6313. ' c:=c-d;',
  6314. ' c:=c*a;',
  6315. ' c:=a*c;',
  6316. ' c:=d*c;',
  6317. ' c:=c*d;',
  6318. ' c:=c/a;',
  6319. ' c:=a/c;',
  6320. ' c:=d/c;',
  6321. ' c:=c/d;',
  6322. ' c:=c**a;',
  6323. ' c:=a**c;',
  6324. ' c:=d**c;',
  6325. ' c:=c**d;',
  6326. ' if c=c then ;',
  6327. ' if c=a then ;',
  6328. ' if a=c then ;',
  6329. ' if d=c then ;',
  6330. ' if c=d then ;',
  6331. ' c:=DoIt(c);',
  6332. ' c:=DoIt(i);',
  6333. ' c:=DoIt(d);',
  6334. ' c:=GetIt(c);',
  6335. ' j:=c;',
  6336. ' Write(c);',
  6337. ' c:=default(currency);',
  6338. ' j:=str(c);',
  6339. ' j:=str(c:0:3);',
  6340. '']);
  6341. ConvertProgram;
  6342. CheckSource('TestCurrency',
  6343. LinesToStr([
  6344. 'this.a = 27000;',
  6345. 'this.b = $mod.a + 17000;',
  6346. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6347. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6348. 'this.c = $mod.b;',
  6349. 'this.i = 0;',
  6350. 'this.d = 0.0;',
  6351. 'this.j = undefined;',
  6352. 'this.DoIt = function (c) {',
  6353. ' var Result = 0;',
  6354. ' return Result;',
  6355. '};',
  6356. 'this.GetIt = function (d) {',
  6357. ' var Result = 0.0;',
  6358. ' return Result;',
  6359. '};',
  6360. 'this.Write = function (v) {',
  6361. '};',
  6362. '']),
  6363. LinesToStr([
  6364. '$mod.c = 10000;',
  6365. '$mod.c = 1000;',
  6366. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6367. '$mod.c = Math.floor((1 / 3) * 10000);',
  6368. '$mod.c = $mod.a;',
  6369. '$mod.d = $mod.c / 10000;',
  6370. '$mod.c = Math.floor($mod.d * 10000);',
  6371. '$mod.c = $mod.c;',
  6372. '$mod.c = $mod.d * 10000;',
  6373. '$mod.d = $mod.c / 10000;',
  6374. '$mod.c = $mod.i * 10000;',
  6375. '$mod.c = $mod.i * 10000;',
  6376. '$mod.i = Math.floor($mod.c / 10000);',
  6377. '$mod.c = $mod.c + $mod.a;',
  6378. '$mod.c = -$mod.c - $mod.a;',
  6379. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6380. '$mod.c = $mod.c + ($mod.d * 10000);',
  6381. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6382. '$mod.c = $mod.c - ($mod.d * 10000);',
  6383. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6384. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6385. '$mod.c = $mod.d * $mod.c;',
  6386. '$mod.c = $mod.c * $mod.d;',
  6387. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6388. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6389. '$mod.c = Math.floor($mod.d / $mod.c);',
  6390. '$mod.c = Math.floor($mod.c / $mod.d);',
  6391. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6392. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6393. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6394. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6395. 'if ($mod.c === $mod.c) ;',
  6396. 'if ($mod.c === $mod.a) ;',
  6397. 'if ($mod.a === $mod.c) ;',
  6398. 'if (($mod.d * 10000) === $mod.c) ;',
  6399. 'if ($mod.c === ($mod.d * 10000)) ;',
  6400. '$mod.c = $mod.DoIt($mod.c);',
  6401. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6402. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6403. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6404. '$mod.j = $mod.c / 10000;',
  6405. '$mod.Write($mod.c / 10000);',
  6406. '$mod.c = 0;',
  6407. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6408. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6409. '']));
  6410. end;
  6411. procedure TTestModule.TestForBoolDo;
  6412. begin
  6413. StartProgram(false);
  6414. Add([
  6415. 'var b: boolean;',
  6416. 'begin',
  6417. ' for b:=false to true do ;',
  6418. ' for b:=b downto false do ;',
  6419. ' for b in boolean do ;',
  6420. '']);
  6421. ConvertProgram;
  6422. CheckSource('TestForBoolDo',
  6423. LinesToStr([ // statements
  6424. 'this.b = false;']),
  6425. LinesToStr([ // this.$main
  6426. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6427. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6428. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6429. '']));
  6430. end;
  6431. procedure TTestModule.TestForIntDo;
  6432. begin
  6433. StartProgram(false);
  6434. Add([
  6435. 'var i: longint;',
  6436. 'begin',
  6437. ' for i:=3 to 5 do ;',
  6438. ' for i:=i downto 2 do ;',
  6439. ' for i in byte do ;',
  6440. '']);
  6441. ConvertProgram;
  6442. CheckSource('TestForIntDo',
  6443. LinesToStr([ // statements
  6444. 'this.i = 0;']),
  6445. LinesToStr([ // this.$main
  6446. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6447. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6448. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6449. '']));
  6450. end;
  6451. procedure TTestModule.TestForIntInDo;
  6452. begin
  6453. StartProgram(false);
  6454. Add([
  6455. 'type',
  6456. ' TSetOfInt = set of byte;',
  6457. ' TIntRg = 3..7;',
  6458. ' TSetOfIntRg = set of TIntRg;',
  6459. 'var',
  6460. ' i,i2: longint;',
  6461. ' a1: array of byte;',
  6462. ' a2: array[1..3] of byte;',
  6463. ' soi: TSetOfInt;',
  6464. ' soir: TSetOfIntRg;',
  6465. ' ir: TIntRg;',
  6466. 'begin',
  6467. ' for i in byte do ;',
  6468. ' for i in a1 do ;',
  6469. ' for i in a2 do ;',
  6470. ' for i in [11..13] do ;',
  6471. ' for i in TSetOfInt do ;',
  6472. ' for i in TIntRg do ;',
  6473. ' for i in soi do i2:=i;',
  6474. ' for i in TSetOfIntRg do ;',
  6475. ' for i in soir do ;',
  6476. ' for ir in TIntRg do ;',
  6477. ' for ir in TSetOfIntRg do ;',
  6478. ' for ir in soir do ;',
  6479. '']);
  6480. ConvertProgram;
  6481. CheckSource('TestForIntInDo',
  6482. LinesToStr([ // statements
  6483. 'this.i = 0;',
  6484. 'this.i2 = 0;',
  6485. 'this.a1 = [];',
  6486. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6487. 'this.soi = {};',
  6488. 'this.soir = {};',
  6489. 'this.ir = 0;',
  6490. '']),
  6491. LinesToStr([ // this.$main
  6492. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6493. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6494. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6495. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6496. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6497. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6498. 'for (var $l11 in $mod.soi) {',
  6499. ' $mod.i = +$l11;',
  6500. ' $mod.i2 = $mod.i;',
  6501. '};',
  6502. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6503. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6504. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6505. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6506. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6507. '']));
  6508. end;
  6509. procedure TTestModule.TestCharConst;
  6510. begin
  6511. StartProgram(false);
  6512. Add([
  6513. 'const',
  6514. ' a = #$00F3;',
  6515. ' c: char = ''1'';',
  6516. 'begin',
  6517. ' c:=#0;',
  6518. ' c:=#1;',
  6519. ' c:=#9;',
  6520. ' c:=#10;',
  6521. ' c:=#13;',
  6522. ' c:=#31;',
  6523. ' c:=#32;',
  6524. ' c:=#$A;',
  6525. ' c:=#$0A;',
  6526. ' c:=#$b;',
  6527. ' c:=#$0b;',
  6528. ' c:=^A;',
  6529. ' c:=''"'';',
  6530. ' c:=default(char);',
  6531. ' c:=#$00E4;', // ä
  6532. ' c:=''ä'';',
  6533. ' c:=#$E4;', // ä
  6534. ' c:=#$D800;', // invalid UTF-16
  6535. ' c:=#$DFFF;', // invalid UTF-16
  6536. ' c:=#$FFFF;', // last UCS-2
  6537. ' c:=high(c);', // last UCS-2
  6538. '']);
  6539. ConvertProgram;
  6540. CheckSource('TestCharConst',
  6541. LinesToStr([
  6542. 'this.a="ó";',
  6543. 'this.c="1";'
  6544. ]),
  6545. LinesToStr([
  6546. '$mod.c="\x00";',
  6547. '$mod.c="\x01";',
  6548. '$mod.c="\t";',
  6549. '$mod.c="\n";',
  6550. '$mod.c="\r";',
  6551. '$mod.c="\x1F";',
  6552. '$mod.c=" ";',
  6553. '$mod.c="\n";',
  6554. '$mod.c="\n";',
  6555. '$mod.c="\x0B";',
  6556. '$mod.c="\x0B";',
  6557. '$mod.c="\x01";',
  6558. '$mod.c=''"'';',
  6559. '$mod.c="\x00";',
  6560. '$mod.c = "ä";',
  6561. '$mod.c = "ä";',
  6562. '$mod.c = "ä";',
  6563. '$mod.c="\uD800";',
  6564. '$mod.c="\uDFFF";',
  6565. '$mod.c="\uFFFF";',
  6566. '$mod.c="\uFFFF";',
  6567. '']));
  6568. end;
  6569. procedure TTestModule.TestChar_Compare;
  6570. begin
  6571. StartProgram(false);
  6572. Add('var');
  6573. Add(' c: char;');
  6574. Add(' b: boolean;');
  6575. Add('begin');
  6576. Add(' b:=c=''1'';');
  6577. Add(' b:=''2''=c;');
  6578. Add(' b:=''3''=''4'';');
  6579. Add(' b:=c<>''5'';');
  6580. Add(' b:=''6''<>c;');
  6581. Add(' b:=c>''7'';');
  6582. Add(' b:=''8''>c;');
  6583. Add(' b:=c>=''9'';');
  6584. Add(' b:=''A''>=c;');
  6585. Add(' b:=c<''B'';');
  6586. Add(' b:=''C''<c;');
  6587. Add(' b:=c<=''D'';');
  6588. Add(' b:=''E''<=c;');
  6589. ConvertProgram;
  6590. CheckSource('TestChar_Compare',
  6591. LinesToStr([
  6592. 'this.c="";',
  6593. 'this.b = false;'
  6594. ]),
  6595. LinesToStr([
  6596. '$mod.b = $mod.c === "1";',
  6597. '$mod.b = "2" === $mod.c;',
  6598. '$mod.b = "3" === "4";',
  6599. '$mod.b = $mod.c !== "5";',
  6600. '$mod.b = "6" !== $mod.c;',
  6601. '$mod.b = $mod.c > "7";',
  6602. '$mod.b = "8" > $mod.c;',
  6603. '$mod.b = $mod.c >= "9";',
  6604. '$mod.b = "A" >= $mod.c;',
  6605. '$mod.b = $mod.c < "B";',
  6606. '$mod.b = "C" < $mod.c;',
  6607. '$mod.b = $mod.c <= "D";',
  6608. '$mod.b = "E" <= $mod.c;',
  6609. '']));
  6610. end;
  6611. procedure TTestModule.TestChar_BuiltInProcs;
  6612. begin
  6613. StartProgram(false);
  6614. Add([
  6615. 'var',
  6616. ' c: char;',
  6617. ' i: longint;',
  6618. ' s: string;',
  6619. 'begin',
  6620. ' i:=ord(c);',
  6621. ' i:=ord(s[i]);',
  6622. ' c:=chr(i);',
  6623. ' c:=pred(c);',
  6624. ' c:=succ(c);',
  6625. ' c:=low(c);',
  6626. ' c:=high(c);',
  6627. ' i:=byte(c);',
  6628. ' i:=word(c);',
  6629. ' i:=longint(c);',
  6630. '']);
  6631. ConvertProgram;
  6632. CheckSource('TestChar_BuiltInProcs',
  6633. LinesToStr([
  6634. 'this.c = "";',
  6635. 'this.i = 0;',
  6636. 'this.s = "";'
  6637. ]),
  6638. LinesToStr([
  6639. '$mod.i = $mod.c.charCodeAt();',
  6640. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6641. '$mod.c = String.fromCharCode($mod.i);',
  6642. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6643. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6644. '$mod.c = "\x00";',
  6645. '$mod.c = "\uFFFF";',
  6646. '$mod.i = $mod.c.charCodeAt() & 255;',
  6647. '$mod.i = $mod.c.charCodeAt();',
  6648. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6649. '']));
  6650. end;
  6651. procedure TTestModule.TestStringConst;
  6652. begin
  6653. StartProgram(false);
  6654. Add([
  6655. '{$H+}',
  6656. 'const',
  6657. ' a = #$00F3#$017C;', // first <256, then >=256
  6658. ' b = string(''a'');',
  6659. ' c = string(''ä'');',
  6660. ' d = UnicodeString(''b'');',
  6661. ' e = UnicodeString(''ö'');',
  6662. 'var',
  6663. ' s: string = ''abc'';',
  6664. 'begin',
  6665. ' s:='''';',
  6666. ' s:=#13#10;',
  6667. ' s:=#9''foo'';',
  6668. ' s:=#$A9;',
  6669. ' s:=''foo''#13''bar'';',
  6670. ' s:=''"'';',
  6671. ' s:=''"''''"'';',
  6672. ' s:=#$20AC;', // euro
  6673. ' s:=#$10437;', // outside BMP
  6674. ' s:=default(string);',
  6675. ' s:=concat(s);',
  6676. ' s:=concat(s,''a'',s)',
  6677. '']);
  6678. ConvertProgram;
  6679. CheckSource('TestStringConst',
  6680. LinesToStr([
  6681. 'this.a = "óż";',
  6682. 'this.b = "a";',
  6683. 'this.c = "ä";',
  6684. 'this.d = "b";',
  6685. 'this.e = "ö";',
  6686. 'this.s="abc";',
  6687. '']),
  6688. LinesToStr([
  6689. '$mod.s="";',
  6690. '$mod.s="\r\n";',
  6691. '$mod.s="\tfoo";',
  6692. '$mod.s="©";',
  6693. '$mod.s="foo\rbar";',
  6694. '$mod.s=''"'';',
  6695. '$mod.s=''"\''"'';',
  6696. '$mod.s="€";',
  6697. '$mod.s="'#$F0#$90#$90#$B7'";',
  6698. '$mod.s="";',
  6699. '$mod.s = $mod.s;',
  6700. '$mod.s = $mod.s.concat("a", $mod.s);',
  6701. '']));
  6702. end;
  6703. procedure TTestModule.TestStringConstSurrogate;
  6704. begin
  6705. StartProgram(false);
  6706. Add([
  6707. 'var',
  6708. ' s: string;',
  6709. 'begin',
  6710. ' s:=''😊'';', // 1F60A
  6711. '']);
  6712. ConvertProgram;
  6713. CheckSource('TestStringConstSurrogate',
  6714. LinesToStr([
  6715. 'this.s="";'
  6716. ]),
  6717. LinesToStr([
  6718. '$mod.s="😊";'
  6719. ]));
  6720. end;
  6721. procedure TTestModule.TestString_Length;
  6722. begin
  6723. StartProgram(false);
  6724. Add('const c = ''foo'';');
  6725. Add('var');
  6726. Add(' s: string;');
  6727. Add(' i: longint;');
  6728. Add('begin');
  6729. Add(' i:=length(s);');
  6730. Add(' i:=length(s+s);');
  6731. Add(' i:=length(''abc'');');
  6732. Add(' i:=length(c);');
  6733. ConvertProgram;
  6734. CheckSource('TestString_Length',
  6735. LinesToStr([
  6736. 'this.c = "foo";',
  6737. 'this.s = "";',
  6738. 'this.i = 0;',
  6739. '']),
  6740. LinesToStr([
  6741. '$mod.i = $mod.s.length;',
  6742. '$mod.i = ($mod.s+$mod.s).length;',
  6743. '$mod.i = "abc".length;',
  6744. '$mod.i = $mod.c.length;',
  6745. '']));
  6746. end;
  6747. procedure TTestModule.TestString_Compare;
  6748. begin
  6749. StartProgram(false);
  6750. Add('var');
  6751. Add(' s, t: string;');
  6752. Add(' b: boolean;');
  6753. Add('begin');
  6754. Add(' b:=s=t;');
  6755. Add(' b:=s<>t;');
  6756. Add(' b:=s>t;');
  6757. Add(' b:=s>=t;');
  6758. Add(' b:=s<t;');
  6759. Add(' b:=s<=t;');
  6760. ConvertProgram;
  6761. CheckSource('TestString_Compare',
  6762. LinesToStr([ // statements
  6763. 'this.s = "";',
  6764. 'this.t = "";',
  6765. 'this.b =false;'
  6766. ]),
  6767. LinesToStr([ // this.$main
  6768. '$mod.b = $mod.s === $mod.t;',
  6769. '$mod.b = $mod.s !== $mod.t;',
  6770. '$mod.b = $mod.s > $mod.t;',
  6771. '$mod.b = $mod.s >= $mod.t;',
  6772. '$mod.b = $mod.s < $mod.t;',
  6773. '$mod.b = $mod.s <= $mod.t;',
  6774. '']));
  6775. end;
  6776. procedure TTestModule.TestString_SetLength;
  6777. begin
  6778. StartProgram(false);
  6779. Add([
  6780. 'procedure DoIt(var s: string);',
  6781. 'begin',
  6782. ' SetLength(s,2);',
  6783. 'end;',
  6784. 'var s: string;',
  6785. 'begin',
  6786. ' SetLength(s,3);',
  6787. '']);
  6788. ConvertProgram;
  6789. CheckSource('TestString_SetLength',
  6790. LinesToStr([ // statements
  6791. 'this.DoIt = function (s) {',
  6792. ' s.set(rtl.strSetLength(s.get(), 2));',
  6793. '};',
  6794. 'this.s = "";',
  6795. '']),
  6796. LinesToStr([ // this.$main
  6797. '$mod.s = rtl.strSetLength($mod.s, 3);'
  6798. ]));
  6799. end;
  6800. procedure TTestModule.TestString_CharAt;
  6801. begin
  6802. StartProgram(false);
  6803. Add([
  6804. 'var',
  6805. ' s: string;',
  6806. ' c: char;',
  6807. ' b: boolean;',
  6808. 'begin',
  6809. ' b:= s[1] = c;',
  6810. ' b:= c = s[1];',
  6811. ' b:= c <> s[1];',
  6812. ' b:= c > s[1];',
  6813. ' b:= c >= s[1];',
  6814. ' b:= c < s[2];',
  6815. ' b:= c <= s[1];',
  6816. ' s[1] := c;',
  6817. ' s[2+3] := c;']);
  6818. ConvertProgram;
  6819. CheckSource('TestString_CharAt',
  6820. LinesToStr([ // statements
  6821. 'this.s = "";',
  6822. 'this.c = "";',
  6823. 'this.b = false;'
  6824. ]),
  6825. LinesToStr([ // this.$main
  6826. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  6827. '$mod.b = $mod.c === $mod.s.charAt(0);',
  6828. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  6829. '$mod.b = $mod.c > $mod.s.charAt(0);',
  6830. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  6831. '$mod.b = $mod.c < $mod.s.charAt(1);',
  6832. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  6833. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  6834. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  6835. '']));
  6836. end;
  6837. procedure TTestModule.TestStringHMinusFail;
  6838. begin
  6839. StartProgram(false);
  6840. Add([
  6841. '{$H-}',
  6842. 'var s: string;',
  6843. 'begin']);
  6844. ConvertProgram;
  6845. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  6846. end;
  6847. procedure TTestModule.TestStr;
  6848. begin
  6849. StartProgram(false);
  6850. Add('var');
  6851. Add(' b: boolean;');
  6852. Add(' i: longint;');
  6853. Add(' d: double;');
  6854. Add(' s: string;');
  6855. Add('begin');
  6856. Add(' str(b,s);');
  6857. Add(' str(i,s);');
  6858. Add(' str(d,s);');
  6859. Add(' str(i:3,s);');
  6860. Add(' str(d:3:2,s);');
  6861. Add(' Str(12.456:12:1,s);');
  6862. Add(' Str(12.456:12,s);');
  6863. Add(' s:=str(b);');
  6864. Add(' s:=str(i);');
  6865. Add(' s:=str(d);');
  6866. Add(' s:=str(i,i);');
  6867. Add(' s:=str(i:3);');
  6868. Add(' s:=str(d:3:2);');
  6869. Add(' s:=str(i:4,i);');
  6870. Add(' s:=str(i,i:5);');
  6871. Add(' s:=str(i:4,i:5);');
  6872. Add(' s:=str(s,s);');
  6873. Add(' s:=str(s,''foo'');');
  6874. ConvertProgram;
  6875. CheckSource('TestStr',
  6876. LinesToStr([ // statements
  6877. 'this.b = false;',
  6878. 'this.i = 0;',
  6879. 'this.d = 0.0;',
  6880. 'this.s = "";',
  6881. '']),
  6882. LinesToStr([ // this.$main
  6883. '$mod.s = ""+$mod.b;',
  6884. '$mod.s = ""+$mod.i;',
  6885. '$mod.s = rtl.floatToStr($mod.d);',
  6886. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6887. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6888. '$mod.s = rtl.floatToStr(12.456,12,1);',
  6889. '$mod.s = rtl.floatToStr(12.456,12);',
  6890. '$mod.s = ""+$mod.b;',
  6891. '$mod.s = ""+$mod.i;',
  6892. '$mod.s = rtl.floatToStr($mod.d);',
  6893. '$mod.s = ""+$mod.i+$mod.i;',
  6894. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6895. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6896. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  6897. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  6898. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  6899. '$mod.s = $mod.s + $mod.s;',
  6900. '$mod.s = $mod.s + "foo";',
  6901. '']));
  6902. end;
  6903. procedure TTestModule.TestBaseType_AnsiStringFail;
  6904. begin
  6905. StartProgram(false);
  6906. Add('var s: AnsiString');
  6907. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  6908. ConvertProgram;
  6909. end;
  6910. procedure TTestModule.TestBaseType_WideStringFail;
  6911. begin
  6912. StartProgram(false);
  6913. Add('var s: WideString');
  6914. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  6915. ConvertProgram;
  6916. end;
  6917. procedure TTestModule.TestBaseType_ShortStringFail;
  6918. begin
  6919. StartProgram(false);
  6920. Add('var s: ShortString');
  6921. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  6922. ConvertProgram;
  6923. end;
  6924. procedure TTestModule.TestBaseType_RawByteStringFail;
  6925. begin
  6926. StartProgram(false);
  6927. Add('var s: RawByteString');
  6928. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  6929. ConvertProgram;
  6930. end;
  6931. procedure TTestModule.TestTypeShortstring_Fail;
  6932. begin
  6933. StartProgram(false);
  6934. Add('type t = string[12];');
  6935. Add('var s: t;');
  6936. Add('begin');
  6937. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  6938. ConvertProgram;
  6939. end;
  6940. procedure TTestModule.TestCharSet_Custom;
  6941. begin
  6942. StartProgram(false);
  6943. Add([
  6944. 'type',
  6945. ' TCharRg = ''a''..''z'';',
  6946. ' TSetOfCharRg = set of TCharRg;',
  6947. ' TCharRg2 = ''m''..''p'';',
  6948. 'const',
  6949. ' crg: TCharRg = ''b'';',
  6950. 'var',
  6951. ' c: char;',
  6952. ' crg2: TCharRg2;',
  6953. ' s: TSetOfCharRg;',
  6954. 'begin',
  6955. ' c:=crg;',
  6956. ' crg:=c;',
  6957. ' crg2:=crg;',
  6958. ' if c=crg then ;',
  6959. ' if crg=c then ;',
  6960. ' if crg=crg2 then ;',
  6961. ' if c in s then ;',
  6962. ' if crg2 in s then ;',
  6963. ' c:=default(TCharRg);',
  6964. '']);
  6965. ConvertProgram;
  6966. CheckSource('TestCharSet_Custom',
  6967. LinesToStr([ // statements
  6968. 'this.crg = "b";',
  6969. 'this.c = "";',
  6970. 'this.crg2 = "m";',
  6971. 'this.s = {};',
  6972. '']),
  6973. LinesToStr([ // this.$main
  6974. '$mod.c = $mod.crg;',
  6975. '$mod.crg = $mod.c;',
  6976. '$mod.crg2 = $mod.crg;',
  6977. 'if ($mod.c === $mod.crg) ;',
  6978. 'if ($mod.crg === $mod.c) ;',
  6979. 'if ($mod.crg === $mod.crg2) ;',
  6980. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  6981. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  6982. '$mod.c = "a";',
  6983. '']));
  6984. end;
  6985. procedure TTestModule.TestForCharDo;
  6986. begin
  6987. StartProgram(false);
  6988. Add([
  6989. 'var c: char;',
  6990. 'begin',
  6991. ' for c:=''a'' to ''c'' do ;',
  6992. ' for c:=c downto ''a'' do ;',
  6993. ' for c:=''Б'' to ''Я'' do ;',
  6994. '']);
  6995. ConvertProgram;
  6996. CheckSource('TestForCharDo',
  6997. LinesToStr([ // statements
  6998. 'this.c = "";']),
  6999. LinesToStr([ // this.$main
  7000. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7001. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7002. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7003. '']));
  7004. end;
  7005. procedure TTestModule.TestForCharInDo;
  7006. begin
  7007. StartProgram(false);
  7008. Add([
  7009. 'type',
  7010. ' TSetOfChar = set of char;',
  7011. ' TCharRg = ''a''..''z'';',
  7012. ' TSetOfCharRg = set of TCharRg;',
  7013. 'const Foo = ''foo'';',
  7014. 'var',
  7015. ' c,c2: char;',
  7016. ' s: string;',
  7017. ' a1: array of char;',
  7018. ' a2: array[1..3] of char;',
  7019. ' soc: TSetOfChar;',
  7020. ' socr: TSetOfCharRg;',
  7021. ' cr: TCharRg;',
  7022. 'begin',
  7023. ' for c in foo do ;',
  7024. ' for c in s do ;',
  7025. ' for c in char do ;',
  7026. ' for c in a1 do ;',
  7027. ' for c in a2 do ;',
  7028. ' for c in [''1''..''3''] do ;',
  7029. ' for c in TSetOfChar do ;',
  7030. ' for c in TCharRg do ;',
  7031. ' for c in soc do c2:=c;',
  7032. ' for c in TSetOfCharRg do ;',
  7033. ' for c in socr do ;',
  7034. ' for cr in TCharRg do ;',
  7035. ' for cr in TSetOfCharRg do ;',
  7036. ' for cr in socr do ;',
  7037. '']);
  7038. ConvertProgram;
  7039. CheckSource('TestForCharInDo',
  7040. LinesToStr([ // statements
  7041. 'this.Foo = "foo";',
  7042. 'this.c = "";',
  7043. 'this.c2 = "";',
  7044. 'this.s = "";',
  7045. 'this.a1 = [];',
  7046. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7047. 'this.soc = {};',
  7048. 'this.socr = {};',
  7049. 'this.cr = "a";',
  7050. '']),
  7051. LinesToStr([ // this.$main
  7052. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7053. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7054. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7055. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7056. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7057. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7058. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7059. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7060. 'for (var $l17 in $mod.soc) {',
  7061. ' $mod.c = String.fromCharCode($l17);',
  7062. ' $mod.c2 = $mod.c;',
  7063. '};',
  7064. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7065. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7066. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7067. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7068. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7069. '']));
  7070. end;
  7071. procedure TTestModule.TestProcTwoArgs;
  7072. begin
  7073. StartProgram(false);
  7074. Add('procedure Test(a,b: longint);');
  7075. Add('begin');
  7076. Add('end;');
  7077. Add('begin');
  7078. ConvertProgram;
  7079. CheckSource('TestProcTwoArgs',
  7080. LinesToStr([ // statements
  7081. 'this.Test = function (a,b) {',
  7082. '};'
  7083. ]),
  7084. LinesToStr([ // this.$main
  7085. ''
  7086. ]));
  7087. end;
  7088. procedure TTestModule.TestProc_DefaultValue;
  7089. begin
  7090. StartProgram(false);
  7091. Add('procedure p1(i: longint = 1);');
  7092. Add('begin');
  7093. Add('end;');
  7094. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7095. Add('begin');
  7096. Add('end;');
  7097. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7098. Add('begin');
  7099. Add('end;');
  7100. Add('begin');
  7101. Add(' p1;');
  7102. Add(' p1();');
  7103. Add(' p1(11);');
  7104. Add(' p2;');
  7105. Add(' p2();');
  7106. Add(' p2(12);');
  7107. Add(' p2(13,''b'');');
  7108. Add(' p3();');
  7109. ConvertProgram;
  7110. CheckSource('TestProc_DefaultValue',
  7111. LinesToStr([ // statements
  7112. 'this.p1 = function (i) {',
  7113. '};',
  7114. 'this.p2 = function (i,c) {',
  7115. '};',
  7116. 'this.p3 = function (d,b,s) {',
  7117. '};'
  7118. ]),
  7119. LinesToStr([ // this.$main
  7120. ' $mod.p1(1);',
  7121. ' $mod.p1(1);',
  7122. ' $mod.p1(11);',
  7123. ' $mod.p2(1,"a");',
  7124. ' $mod.p2(1,"a");',
  7125. ' $mod.p2(12,"a");',
  7126. ' $mod.p2(13,"b");',
  7127. ' $mod.p3(1.0,false,"abc");'
  7128. ]));
  7129. end;
  7130. procedure TTestModule.TestFunctionInt;
  7131. begin
  7132. StartProgram(false);
  7133. Add('function MyTest(Bar: longint): longint;');
  7134. Add('begin');
  7135. Add(' Result:=2*bar');
  7136. Add('end;');
  7137. Add('begin');
  7138. ConvertProgram;
  7139. CheckSource('TestFunctionInt',
  7140. LinesToStr([ // statements
  7141. 'this.MyTest = function (Bar) {',
  7142. ' var Result = 0;',
  7143. ' Result = 2*Bar;',
  7144. ' return Result;',
  7145. '};'
  7146. ]),
  7147. LinesToStr([ // this.$main
  7148. ''
  7149. ]));
  7150. end;
  7151. procedure TTestModule.TestFunctionString;
  7152. begin
  7153. StartProgram(false);
  7154. Add('function Test(Bar: string): string;');
  7155. Add('begin');
  7156. Add(' Result:=bar+BAR');
  7157. Add('end;');
  7158. Add('begin');
  7159. ConvertProgram;
  7160. CheckSource('TestFunctionString',
  7161. LinesToStr([ // statements
  7162. 'this.Test = function (Bar) {',
  7163. ' var Result = "";',
  7164. ' Result = Bar+Bar;',
  7165. ' return Result;',
  7166. '};'
  7167. ]),
  7168. LinesToStr([ // this.$main
  7169. ''
  7170. ]));
  7171. end;
  7172. procedure TTestModule.TestIfThen;
  7173. begin
  7174. StartProgram(false);
  7175. Add([
  7176. 'var b: boolean;',
  7177. 'begin',
  7178. ' if b then ;',
  7179. ' if b then else ;']);
  7180. ConvertProgram;
  7181. CheckSource('TestIfThen',
  7182. LinesToStr([ // statements
  7183. 'this.b = false;',
  7184. '']),
  7185. LinesToStr([ // this.$main
  7186. 'if ($mod.b) ;',
  7187. 'if ($mod.b) ;',
  7188. '']));
  7189. end;
  7190. procedure TTestModule.TestForLoop;
  7191. begin
  7192. StartProgram(false);
  7193. Add('var');
  7194. Add(' vI, vJ, vN: longint;');
  7195. Add('begin');
  7196. Add(' VJ:=0;');
  7197. Add(' VN:=3;');
  7198. Add(' for VI:=1 to VN do');
  7199. Add(' begin');
  7200. Add(' VJ:=VJ+VI;');
  7201. Add(' end;');
  7202. ConvertProgram;
  7203. CheckSource('TestForLoop',
  7204. LinesToStr([ // statements
  7205. 'this.vI = 0;',
  7206. 'this.vJ = 0;',
  7207. 'this.vN = 0;'
  7208. ]),
  7209. LinesToStr([ // this.$main
  7210. ' $mod.vJ = 0;',
  7211. ' $mod.vN = 3;',
  7212. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7213. ' $mod.vI = $l1;',
  7214. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7215. ' };',
  7216. '']));
  7217. end;
  7218. procedure TTestModule.TestForLoopInsideFunction;
  7219. begin
  7220. StartProgram(false);
  7221. Add('function SumNumbers(Count: longint): longint;');
  7222. Add('var');
  7223. Add(' vI, vJ: longint;');
  7224. Add('begin');
  7225. Add(' vj:=0;');
  7226. Add(' for vi:=1 to count do');
  7227. Add(' begin');
  7228. Add(' vj:=vj+vi;');
  7229. Add(' end;');
  7230. Add('end;');
  7231. Add('begin');
  7232. Add(' sumnumbers(3);');
  7233. ConvertProgram;
  7234. CheckSource('TestForLoopInsideFunction',
  7235. LinesToStr([ // statements
  7236. 'this.SumNumbers = function (Count) {',
  7237. ' var Result = 0;',
  7238. ' var vI = 0;',
  7239. ' var vJ = 0;',
  7240. ' vJ = 0;',
  7241. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7242. ' vI = $l1;',
  7243. ' vJ = vJ + vI;',
  7244. ' };',
  7245. ' return Result;',
  7246. '};'
  7247. ]),
  7248. LinesToStr([ // $mod.$main
  7249. ' $mod.SumNumbers(3);'
  7250. ]));
  7251. end;
  7252. procedure TTestModule.TestForLoop_ReadVarAfter;
  7253. begin
  7254. StartProgram(false);
  7255. Add('var');
  7256. Add(' vI: longint;');
  7257. Add('begin');
  7258. Add(' for vi:=1 to 2 do ;');
  7259. Add(' if vi=3 then ;');
  7260. ConvertProgram;
  7261. CheckSource('TestForLoop',
  7262. LinesToStr([ // statements
  7263. 'this.vI = 0;'
  7264. ]),
  7265. LinesToStr([ // this.$main
  7266. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7267. ' if ($mod.vI===3) ;'
  7268. ]));
  7269. end;
  7270. procedure TTestModule.TestForLoop_Nested;
  7271. begin
  7272. StartProgram(false);
  7273. Add('function SumNumbers(Count: longint): longint;');
  7274. Add('var');
  7275. Add(' vI, vJ, vK: longint;');
  7276. Add('begin');
  7277. Add(' VK:=0;');
  7278. Add(' for VI:=1 to count do');
  7279. Add(' begin');
  7280. Add(' for vj:=1 to vi do');
  7281. Add(' begin');
  7282. Add(' vk:=VK+VI;');
  7283. Add(' end;');
  7284. Add(' end;');
  7285. Add('end;');
  7286. Add('begin');
  7287. Add(' sumnumbers(3);');
  7288. ConvertProgram;
  7289. CheckSource('TestForLoopInFunction',
  7290. LinesToStr([ // statements
  7291. 'this.SumNumbers = function (Count) {',
  7292. ' var Result = 0;',
  7293. ' var vI = 0;',
  7294. ' var vJ = 0;',
  7295. ' var vK = 0;',
  7296. ' vK = 0;',
  7297. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7298. ' vI = $l1;',
  7299. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7300. ' vJ = $l3;',
  7301. ' vK = vK + vI;',
  7302. ' };',
  7303. ' };',
  7304. ' return Result;',
  7305. '};'
  7306. ]),
  7307. LinesToStr([ // $mod.$main
  7308. ' $mod.SumNumbers(3);'
  7309. ]));
  7310. end;
  7311. procedure TTestModule.TestRepeatUntil;
  7312. begin
  7313. StartProgram(false);
  7314. Add('var');
  7315. Add(' vI, vJ, vN: longint;');
  7316. Add('begin');
  7317. Add(' vn:=3;');
  7318. Add(' vj:=0;');
  7319. Add(' VI:=0;');
  7320. Add(' repeat');
  7321. Add(' VI:=vi+1;');
  7322. Add(' vj:=VJ+vI;');
  7323. Add(' until vi>=vn');
  7324. ConvertProgram;
  7325. CheckSource('TestRepeatUntil',
  7326. LinesToStr([ // statements
  7327. 'this.vI = 0;',
  7328. 'this.vJ = 0;',
  7329. 'this.vN = 0;'
  7330. ]),
  7331. LinesToStr([ // $mod.$main
  7332. ' $mod.vN = 3;',
  7333. ' $mod.vJ = 0;',
  7334. ' $mod.vI = 0;',
  7335. ' do{',
  7336. ' $mod.vI = $mod.vI + 1;',
  7337. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7338. ' }while(!($mod.vI>=$mod.vN));'
  7339. ]));
  7340. end;
  7341. procedure TTestModule.TestAsmBlock;
  7342. begin
  7343. StartProgram(false);
  7344. Add([
  7345. 'var',
  7346. ' vI: longint;',
  7347. 'begin',
  7348. ' vi:=1;',
  7349. ' asm',
  7350. ' if (vI===1) {',
  7351. ' vI=2;',
  7352. //' console.log(''end;'');', ToDo
  7353. ' }',
  7354. ' if (vI===2){ vI=3; }',
  7355. ' end;',
  7356. ' VI:=4;']);
  7357. ConvertProgram;
  7358. CheckSource('TestAsmBlock',
  7359. LinesToStr([ // statements
  7360. 'this.vI = 0;'
  7361. ]),
  7362. LinesToStr([ // $mod.$main
  7363. '$mod.vI = 1;',
  7364. 'if (vI===1) {',
  7365. ' vI=2;',
  7366. '}',
  7367. 'if (vI===2){ vI=3; }',
  7368. ';',
  7369. '$mod.vI = 4;'
  7370. ]));
  7371. end;
  7372. procedure TTestModule.TestAsmPas_Impl;
  7373. begin
  7374. StartUnit(false);
  7375. Add('interface');
  7376. Add('const cIntf: longint = 1;');
  7377. Add('var vIntf: longint;');
  7378. Add('implementation');
  7379. Add('const cImpl: longint = 2;');
  7380. Add('var vImpl: longint;');
  7381. Add('procedure DoIt;');
  7382. Add('const cLoc: longint = 3;');
  7383. Add('var vLoc: longint;');
  7384. Add('begin;');
  7385. Add(' asm');
  7386. //Add(' pas(vIntf)=pas(cIntf);');
  7387. //Add(' pas(vImpl)=pas(cImpl);');
  7388. //Add(' pas(vLoc)=pas(cLoc);');
  7389. Add(' end;');
  7390. Add('end;');
  7391. ConvertUnit;
  7392. CheckSource('TestAsmPas_Impl',
  7393. LinesToStr([
  7394. 'var $impl = $mod.$impl;',
  7395. 'this.cIntf = 1;',
  7396. 'this.vIntf = 0;',
  7397. '']),
  7398. '', // this.$init
  7399. LinesToStr([ // implementation
  7400. '$impl.cImpl = 2;',
  7401. '$impl.vImpl = 0;',
  7402. 'var cLoc = 3;',
  7403. '$impl.DoIt = function () {',
  7404. ' var vLoc = 0;',
  7405. '};',
  7406. '']) );
  7407. end;
  7408. procedure TTestModule.TestTryFinally;
  7409. begin
  7410. StartProgram(false);
  7411. Add('var i: longint;');
  7412. Add('begin');
  7413. Add(' try');
  7414. Add(' i:=0; i:=2 div i;');
  7415. Add(' finally');
  7416. Add(' i:=3');
  7417. Add(' end;');
  7418. ConvertProgram;
  7419. CheckSource('TestTryFinally',
  7420. LinesToStr([ // statements
  7421. 'this.i = 0;'
  7422. ]),
  7423. LinesToStr([ // $mod.$main
  7424. 'try {',
  7425. ' $mod.i = 0;',
  7426. ' $mod.i = Math.floor(2 / $mod.i);',
  7427. '} finally {',
  7428. ' $mod.i = 3;',
  7429. '};'
  7430. ]));
  7431. end;
  7432. procedure TTestModule.TestTryExcept;
  7433. begin
  7434. StartProgram(false);
  7435. Add('type');
  7436. Add(' TObject = class end;');
  7437. Add(' Exception = class Msg: string; end;');
  7438. Add(' EInvalidCast = class(Exception) end;');
  7439. Add('var vI: longint;');
  7440. Add('begin');
  7441. Add(' try');
  7442. Add(' vi:=1;');
  7443. Add(' except');
  7444. Add(' vi:=2');
  7445. Add(' end;');
  7446. Add(' try');
  7447. Add(' vi:=3;');
  7448. Add(' except');
  7449. Add(' raise;');
  7450. Add(' end;');
  7451. Add(' try');
  7452. Add(' VI:=4;');
  7453. Add(' except');
  7454. Add(' on einvalidcast do');
  7455. Add(' raise;');
  7456. Add(' on E: exception do');
  7457. Add(' if e.msg='''' then');
  7458. Add(' raise e;');
  7459. Add(' else');
  7460. Add(' vi:=5');
  7461. Add(' end;');
  7462. Add(' try');
  7463. Add(' VI:=6;');
  7464. Add(' except');
  7465. Add(' on einvalidcast do ;');
  7466. Add(' end;');
  7467. ConvertProgram;
  7468. CheckSource('TestTryExcept',
  7469. LinesToStr([ // statements
  7470. 'rtl.createClass($mod, "TObject", null, function () {',
  7471. ' this.$init = function () {',
  7472. ' };',
  7473. ' this.$final = function () {',
  7474. ' };',
  7475. '});',
  7476. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7477. ' this.$init = function () {',
  7478. ' $mod.TObject.$init.call(this);',
  7479. ' this.Msg = "";',
  7480. ' };',
  7481. '});',
  7482. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7483. '});',
  7484. 'this.vI = 0;'
  7485. ]),
  7486. LinesToStr([ // $mod.$main
  7487. 'try {',
  7488. ' $mod.vI = 1;',
  7489. '} catch ($e) {',
  7490. ' $mod.vI = 2;',
  7491. '};',
  7492. 'try {',
  7493. ' $mod.vI = 3;',
  7494. '} catch ($e) {',
  7495. ' throw $e;',
  7496. '};',
  7497. 'try {',
  7498. ' $mod.vI = 4;',
  7499. '} catch ($e) {',
  7500. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7501. ' throw $e',
  7502. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7503. ' var E = $e;',
  7504. ' if (E.Msg === "") throw E;',
  7505. ' } else {',
  7506. ' $mod.vI = 5;',
  7507. ' }',
  7508. '};',
  7509. 'try {',
  7510. ' $mod.vI = 6;',
  7511. '} catch ($e) {',
  7512. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7513. ' } else throw $e',
  7514. '};',
  7515. '']));
  7516. end;
  7517. procedure TTestModule.TestTryExcept_ReservedWords;
  7518. begin
  7519. StartProgram(false);
  7520. Add([
  7521. 'type',
  7522. ' TObject = class end;',
  7523. ' Exception = class',
  7524. ' Symbol: string;',
  7525. ' end;',
  7526. 'var &try: longint;',
  7527. 'begin',
  7528. ' try',
  7529. ' &try:=4;',
  7530. ' except',
  7531. ' on Error: exception do',
  7532. ' if errOR.symBol='''' then',
  7533. ' raise ERRor;',
  7534. ' end;',
  7535. '']);
  7536. ConvertProgram;
  7537. CheckSource('TestTryExcept_ReservedWords',
  7538. LinesToStr([ // statements
  7539. 'rtl.createClass($mod, "TObject", null, function () {',
  7540. ' this.$init = function () {',
  7541. ' };',
  7542. ' this.$final = function () {',
  7543. ' };',
  7544. '});',
  7545. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7546. ' this.$init = function () {',
  7547. ' $mod.TObject.$init.call(this);',
  7548. ' this.Symbol = "";',
  7549. ' };',
  7550. '});',
  7551. 'this.Try = 0;',
  7552. '']),
  7553. LinesToStr([ // $mod.$main
  7554. 'try {',
  7555. ' $mod.Try = 4;',
  7556. '} catch ($e) {',
  7557. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7558. ' var error = $e;',
  7559. ' if (error.Symbol === "") throw error;',
  7560. ' } else throw $e',
  7561. '};',
  7562. '']));
  7563. end;
  7564. procedure TTestModule.TestIfThenRaiseElse;
  7565. begin
  7566. StartProgram(false);
  7567. Add([
  7568. 'type',
  7569. ' TObject = class',
  7570. ' constructor Create;',
  7571. ' end;',
  7572. 'constructor TObject.Create;',
  7573. 'begin',
  7574. 'end;',
  7575. 'var b: boolean;',
  7576. 'begin',
  7577. ' if b then',
  7578. ' raise TObject.Create',
  7579. ' else',
  7580. ' b:=false;',
  7581. '']);
  7582. ConvertProgram;
  7583. CheckSource('TestIfThenRaiseElse',
  7584. LinesToStr([ // statements
  7585. 'rtl.createClass($mod, "TObject", null, function () {',
  7586. ' this.$init = function () {',
  7587. ' };',
  7588. ' this.$final = function () {',
  7589. ' };',
  7590. ' this.Create = function () {',
  7591. ' return this;',
  7592. ' };',
  7593. '});',
  7594. 'this.b = false;',
  7595. '']),
  7596. LinesToStr([ // $mod.$main
  7597. 'if ($mod.b) {',
  7598. ' throw $mod.TObject.$create("Create")}',
  7599. ' else $mod.b = false;',
  7600. '']));
  7601. end;
  7602. procedure TTestModule.TestCaseOf;
  7603. begin
  7604. StartProgram(false);
  7605. Add([
  7606. 'const e: longint; external name ''$e'';',
  7607. 'var vI: longint;',
  7608. 'begin',
  7609. ' case vi of',
  7610. ' 1: ;',
  7611. ' 2: vi:=3;',
  7612. ' e: ;',
  7613. ' else',
  7614. ' VI:=4',
  7615. ' end;']);
  7616. ConvertProgram;
  7617. CheckSource('TestCaseOf',
  7618. LinesToStr([ // statements
  7619. 'this.vI = 0;'
  7620. ]),
  7621. LinesToStr([ // $mod.$main
  7622. 'var $tmp1 = $mod.vI;',
  7623. 'if ($tmp1 === 1) {}',
  7624. 'else if ($tmp1 === 2) {',
  7625. ' $mod.vI = 3}',
  7626. ' else if ($tmp1 === $e) {}',
  7627. 'else {',
  7628. ' $mod.vI = 4;',
  7629. '};'
  7630. ]));
  7631. end;
  7632. procedure TTestModule.TestCaseOf_UseSwitch;
  7633. begin
  7634. StartProgram(false);
  7635. Converter.UseSwitchStatement:=true;
  7636. Add('var Vi: longint;');
  7637. Add('begin');
  7638. Add(' case vi of');
  7639. Add(' 1: ;');
  7640. Add(' 2: VI:=3;');
  7641. Add(' else');
  7642. Add(' vi:=4');
  7643. Add(' end;');
  7644. ConvertProgram;
  7645. CheckSource('TestCaseOf_UseSwitch',
  7646. LinesToStr([ // statements
  7647. 'this.Vi = 0;'
  7648. ]),
  7649. LinesToStr([ // $mod.$main
  7650. 'switch ($mod.Vi) {',
  7651. 'case 1:',
  7652. ' break;',
  7653. 'case 2:',
  7654. ' $mod.Vi = 3;',
  7655. ' break;',
  7656. 'default:',
  7657. ' $mod.Vi = 4;',
  7658. '};'
  7659. ]));
  7660. end;
  7661. procedure TTestModule.TestCaseOfNoElse;
  7662. begin
  7663. StartProgram(false);
  7664. Add('var Vi: longint;');
  7665. Add('begin');
  7666. Add(' case vi of');
  7667. Add(' 1: begin vi:=2; VI:=3; end;');
  7668. Add(' end;');
  7669. ConvertProgram;
  7670. CheckSource('TestCaseOfNoElse',
  7671. LinesToStr([ // statements
  7672. 'this.Vi = 0;'
  7673. ]),
  7674. LinesToStr([ // $mod.$main
  7675. 'var $tmp1 = $mod.Vi;',
  7676. 'if ($tmp1 === 1) {',
  7677. ' $mod.Vi = 2;',
  7678. ' $mod.Vi = 3;',
  7679. '};'
  7680. ]));
  7681. end;
  7682. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7683. begin
  7684. StartProgram(false);
  7685. Converter.UseSwitchStatement:=true;
  7686. Add('var vI: longint;');
  7687. Add('begin');
  7688. Add(' case vi of');
  7689. Add(' 1: begin VI:=2; vi:=3; end;');
  7690. Add(' end;');
  7691. ConvertProgram;
  7692. CheckSource('TestCaseOfNoElse_UseSwitch',
  7693. LinesToStr([ // statements
  7694. 'this.vI = 0;'
  7695. ]),
  7696. LinesToStr([ // $mod.$main
  7697. 'switch ($mod.vI) {',
  7698. 'case 1:',
  7699. ' $mod.vI = 2;',
  7700. ' $mod.vI = 3;',
  7701. ' break;',
  7702. '};'
  7703. ]));
  7704. end;
  7705. procedure TTestModule.TestCaseOfRange;
  7706. begin
  7707. StartProgram(false);
  7708. Add('var vI: longint;');
  7709. Add('begin');
  7710. Add(' case vi of');
  7711. Add(' 1..3: vi:=14;');
  7712. Add(' 4,5: vi:=16;');
  7713. Add(' 6..7,9..10: ;');
  7714. Add(' else ;');
  7715. Add(' end;');
  7716. ConvertProgram;
  7717. CheckSource('TestCaseOfRange',
  7718. LinesToStr([ // statements
  7719. 'this.vI = 0;'
  7720. ]),
  7721. LinesToStr([ // $mod.$main
  7722. 'var $tmp1 = $mod.vI;',
  7723. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7724. ' $mod.vI = 14',
  7725. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7726. ' $mod.vI = 16',
  7727. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7728. ]));
  7729. end;
  7730. procedure TTestModule.TestCaseOfString;
  7731. begin
  7732. StartProgram(false);
  7733. Add([
  7734. 'var s,h: string;',
  7735. 'begin',
  7736. ' case s of',
  7737. ' ''foo'': s:=h;',
  7738. ' ''a''..''z'': h:=s;',
  7739. ' ''ў'', ''ё'': ;',
  7740. ' ''Б''..''Я'': ;',
  7741. ' end;',
  7742. '']);
  7743. ConvertProgram;
  7744. CheckSource('TestCaseOfString',
  7745. LinesToStr([ // statements
  7746. 'this.s = "";',
  7747. 'this.h = "";',
  7748. '']),
  7749. LinesToStr([ // $mod.$main
  7750. 'var $tmp1 = $mod.s;',
  7751. 'if ($tmp1 === "foo") {',
  7752. ' $mod.s = $mod.h}',
  7753. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7754. ' $mod.h = $mod.s}',
  7755. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7756. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7757. '']));
  7758. end;
  7759. procedure TTestModule.TestCaseOfChar;
  7760. begin
  7761. StartProgram(false);
  7762. Add([
  7763. 'var s,h: char;',
  7764. 'begin',
  7765. ' case s of',
  7766. ' ''a''..''z'': h:=s;',
  7767. ' ''ä'': ;',
  7768. ' ''ў'', ''ё'': ;',
  7769. ' ''Б''..''Я'': ;',
  7770. ' end;',
  7771. '']);
  7772. ConvertProgram;
  7773. CheckSource('TestCaseOfString',
  7774. LinesToStr([ // statements
  7775. 'this.s = "";',
  7776. 'this.h = "";',
  7777. '']),
  7778. LinesToStr([ // $mod.$main
  7779. 'var $tmp1 = $mod.s;',
  7780. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7781. ' $mod.h = $mod.s}',
  7782. ' else if ($tmp1 === "ä") {}',
  7783. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7784. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7785. '']));
  7786. end;
  7787. procedure TTestModule.TestCaseOfExternalClassConst;
  7788. begin
  7789. StartProgram(false);
  7790. Add([
  7791. '{$modeswitch externalclass}',
  7792. 'type',
  7793. ' TBird = class external name ''Bird''',
  7794. ' const e: longint;',
  7795. ' end;',
  7796. 'var vI: longint;',
  7797. 'begin',
  7798. ' case vi of',
  7799. ' 1: vi:=3;',
  7800. ' TBird.e: ;',
  7801. ' end;']);
  7802. ConvertProgram;
  7803. CheckSource('TestCaseOfExternalClassConst',
  7804. LinesToStr([ // statements
  7805. 'this.vI = 0;'
  7806. ]),
  7807. LinesToStr([ // $mod.$main
  7808. 'var $tmp1 = $mod.vI;',
  7809. 'if ($tmp1 === 1) {',
  7810. ' $mod.vI = 3}',
  7811. ' else if ($tmp1 === Bird.e) ;'
  7812. ]));
  7813. end;
  7814. procedure TTestModule.TestDebugger;
  7815. begin
  7816. StartProgram(false);
  7817. Add([
  7818. 'procedure DoIt;',
  7819. 'begin',
  7820. ' deBugger;',
  7821. ' DeBugger();',
  7822. 'end;',
  7823. 'begin',
  7824. ' Debugger;']);
  7825. ConvertProgram;
  7826. CheckSource('TestDebugger',
  7827. LinesToStr([ // statements
  7828. 'this.DoIt = function () {',
  7829. ' debugger;',
  7830. ' debugger;',
  7831. '};',
  7832. '']),
  7833. LinesToStr([ // $mod.$main
  7834. 'debugger;',
  7835. '']));
  7836. end;
  7837. procedure TTestModule.TestArray_Dynamic;
  7838. begin
  7839. StartProgram(false);
  7840. Add(['type',
  7841. ' TArrayInt = array of longint;',
  7842. 'var',
  7843. ' Arr: TArrayInt;',
  7844. ' i: longint;',
  7845. ' b: boolean;',
  7846. 'begin',
  7847. ' SetLength(arr,3);',
  7848. ' arr[0]:=4;',
  7849. ' arr[1]:=length(arr)+arr[0];',
  7850. ' arr[i]:=5;',
  7851. ' arr[arr[i]]:=arr[6];',
  7852. ' i:=low(arr);',
  7853. ' i:=high(arr);',
  7854. ' b:=Assigned(arr);',
  7855. ' Arr:=default(TArrayInt);']);
  7856. ConvertProgram;
  7857. CheckSource('TestArray_Dynamic',
  7858. LinesToStr([ // statements
  7859. 'this.Arr = [];',
  7860. 'this.i = 0;',
  7861. 'this.b = false;'
  7862. ]),
  7863. LinesToStr([ // $mod.$main
  7864. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  7865. '$mod.Arr[0] = 4;',
  7866. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  7867. '$mod.Arr[$mod.i] = 5;',
  7868. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  7869. '$mod.i = 0;',
  7870. '$mod.i = rtl.length($mod.Arr) - 1;',
  7871. '$mod.b = rtl.length($mod.Arr) > 0;',
  7872. '$mod.Arr = [];',
  7873. '']));
  7874. end;
  7875. procedure TTestModule.TestArray_Dynamic_Nil;
  7876. begin
  7877. StartProgram(false);
  7878. Add('type');
  7879. Add(' TArrayInt = array of longint;');
  7880. Add('var');
  7881. Add(' Arr: TArrayInt;');
  7882. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  7883. Add('begin');
  7884. Add(' arr:=nil;');
  7885. Add(' if arr=nil then;');
  7886. Add(' if nil=arr then;');
  7887. Add(' if arr<>nil then;');
  7888. Add(' if nil<>arr then;');
  7889. Add(' DoIt(nil,nil);');
  7890. ConvertProgram;
  7891. CheckSource('TestArray_Dynamic',
  7892. LinesToStr([ // statements
  7893. 'this.Arr = [];',
  7894. 'this.DoIt = function(i,j){',
  7895. '};'
  7896. ]),
  7897. LinesToStr([ // $mod.$main
  7898. '$mod.Arr = [];',
  7899. 'if (rtl.length($mod.Arr) === 0) ;',
  7900. 'if (rtl.length($mod.Arr) === 0) ;',
  7901. 'if (rtl.length($mod.Arr) > 0) ;',
  7902. 'if (rtl.length($mod.Arr) > 0) ;',
  7903. '$mod.DoIt([],[]);',
  7904. '']));
  7905. end;
  7906. procedure TTestModule.TestArray_DynMultiDimensional;
  7907. begin
  7908. StartProgram(false);
  7909. Add('type');
  7910. Add(' TArrayInt = array of longint;');
  7911. Add(' TArrayArrayInt = array of TArrayInt;');
  7912. Add('var');
  7913. Add(' Arr: TArrayInt;');
  7914. Add(' Arr2: TArrayArrayInt;');
  7915. Add(' i: longint;');
  7916. Add('begin');
  7917. Add(' arr2:=nil;');
  7918. Add(' if arr2=nil then;');
  7919. Add(' if nil=arr2 then;');
  7920. Add(' i:=low(arr2);');
  7921. Add(' i:=low(arr2[1]);');
  7922. Add(' i:=high(arr2);');
  7923. Add(' i:=high(arr2[2]);');
  7924. Add(' arr2[3]:=arr;');
  7925. Add(' arr2[4][5]:=i;');
  7926. Add(' i:=arr2[6][7];');
  7927. Add(' arr2[8,9]:=i;');
  7928. Add(' i:=arr2[10,11];');
  7929. Add(' SetLength(arr2,14);');
  7930. Add(' SetLength(arr2[15],16);');
  7931. ConvertProgram;
  7932. CheckSource('TestArray_Dynamic',
  7933. LinesToStr([ // statements
  7934. 'this.Arr = [];',
  7935. 'this.Arr2 = [];',
  7936. 'this.i = 0;'
  7937. ]),
  7938. LinesToStr([ // $mod.$main
  7939. '$mod.Arr2 = [];',
  7940. 'if (rtl.length($mod.Arr2) === 0) ;',
  7941. 'if (rtl.length($mod.Arr2) === 0) ;',
  7942. '$mod.i = 0;',
  7943. '$mod.i = 0;',
  7944. '$mod.i = rtl.length($mod.Arr2) - 1;',
  7945. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  7946. '$mod.Arr2[3] = $mod.Arr;',
  7947. '$mod.Arr2[4][5] = $mod.i;',
  7948. '$mod.i = $mod.Arr2[6][7];',
  7949. '$mod.Arr2[8][9] = $mod.i;',
  7950. '$mod.i = $mod.Arr2[10][11];',
  7951. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  7952. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  7953. '']));
  7954. end;
  7955. procedure TTestModule.TestArray_StaticInt;
  7956. begin
  7957. StartProgram(false);
  7958. Add('type');
  7959. Add(' TArrayInt = array[2..4] of longint;');
  7960. Add('var');
  7961. Add(' Arr: TArrayInt;');
  7962. Add(' Arr2: TArrayInt = (5,6,7);');
  7963. Add(' i: longint;');
  7964. Add(' b: boolean;');
  7965. Add('begin');
  7966. Add(' arr[2]:=4;');
  7967. Add(' arr[3]:=arr[2]+arr[3];');
  7968. Add(' arr[i]:=5;');
  7969. Add(' arr[arr[i]]:=arr[high(arr)];');
  7970. Add(' i:=low(arr);');
  7971. Add(' i:=high(arr);');
  7972. Add(' b:=arr[2]=arr[3];');
  7973. Add(' arr:=default(TArrayInt);');
  7974. ConvertProgram;
  7975. CheckSource('TestArray_StaticInt',
  7976. LinesToStr([ // statements
  7977. 'this.Arr = rtl.arraySetLength(null,0,3);',
  7978. 'this.Arr2 = [5, 6, 7];',
  7979. 'this.i = 0;',
  7980. 'this.b = false;'
  7981. ]),
  7982. LinesToStr([ // $mod.$main
  7983. '$mod.Arr[0] = 4;',
  7984. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  7985. '$mod.Arr[$mod.i-2] = 5;',
  7986. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  7987. '$mod.i = 2;',
  7988. '$mod.i = 4;',
  7989. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7990. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  7991. '']));
  7992. end;
  7993. procedure TTestModule.TestArray_StaticBool;
  7994. begin
  7995. StartProgram(false);
  7996. Add('type');
  7997. Add(' TBools = array[boolean] of boolean;');
  7998. Add(' TBool2 = array[true..true] of boolean;');
  7999. Add('var');
  8000. Add(' Arr: TBools;');
  8001. Add(' Arr2: TBool2;');
  8002. Add(' Arr3: TBools = (true,false);');
  8003. Add(' b: boolean;');
  8004. Add('begin');
  8005. Add(' b:=low(arr);');
  8006. Add(' b:=high(arr);');
  8007. Add(' arr[true]:=false;');
  8008. Add(' arr[false]:=arr[b] or arr[true];');
  8009. Add(' arr[b]:=true;');
  8010. Add(' arr[arr[b]]:=arr[high(arr)];');
  8011. Add(' b:=arr[false]=arr[true];');
  8012. Add(' b:=low(arr2);');
  8013. Add(' b:=high(arr2);');
  8014. Add(' arr2[true]:=true;');
  8015. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8016. Add(' arr2[b]:=false;');
  8017. ConvertProgram;
  8018. CheckSource('TestArray_StaticBool',
  8019. LinesToStr([ // statements
  8020. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8021. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8022. 'this.Arr3 = [true, false];',
  8023. 'this.b = false;'
  8024. ]),
  8025. LinesToStr([ // $mod.$main
  8026. '$mod.b = false;',
  8027. '$mod.b = true;',
  8028. '$mod.Arr[1] = false;',
  8029. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8030. '$mod.Arr[+$mod.b] = true;',
  8031. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8032. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8033. '$mod.b = true;',
  8034. '$mod.b = true;',
  8035. '$mod.Arr2[0] = true;',
  8036. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8037. '$mod.Arr2[1-$mod.b] = false;',
  8038. '']));
  8039. end;
  8040. procedure TTestModule.TestArray_StaticChar;
  8041. begin
  8042. StartProgram(false);
  8043. Add([
  8044. 'type',
  8045. ' TChars = array[char] of char;',
  8046. ' TChars2 = array[''a''..''z''] of char;',
  8047. 'var',
  8048. ' Arr: TChars;',
  8049. ' Arr2: TChars2;',
  8050. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8051. ' Arr4: array[11..13] of char = ''pas'';',
  8052. ' Arr5: array[21..22] of char = ''äö'';',
  8053. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8054. ' c: char;',
  8055. ' b: boolean;',
  8056. 'begin',
  8057. ' c:=low(arr);',
  8058. ' c:=high(arr);',
  8059. ' arr[''B'']:=''a'';',
  8060. ' arr[''D'']:=arr[c];',
  8061. ' arr[c]:=arr[''d''];',
  8062. ' arr[arr[c]]:=arr[high(arr)];',
  8063. ' b:=arr[low(arr)]=arr[''e''];',
  8064. ' c:=low(arr2);',
  8065. ' c:=high(arr2);',
  8066. ' arr2[''b'']:=''f'';',
  8067. ' arr2[''a'']:=arr2[c];',
  8068. ' arr2[c]:=arr2[''g''];']);
  8069. ConvertProgram;
  8070. CheckSource('TestArray_StaticChar',
  8071. LinesToStr([ // statements
  8072. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8073. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8074. 'this.Arr3 = ["p", "a", "s"];',
  8075. 'this.Arr4 = ["p", "a", "s"];',
  8076. 'this.Arr5 = ["ä", "ö"];',
  8077. 'this.Arr6 = ["ä", "ö"];',
  8078. 'this.c = "";',
  8079. 'this.b = false;',
  8080. '']),
  8081. LinesToStr([ // $mod.$main
  8082. '$mod.c = "\x00";',
  8083. '$mod.c = "\uFFFF";',
  8084. '$mod.Arr[66] = "a";',
  8085. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8086. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8087. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8088. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8089. '$mod.c = "a";',
  8090. '$mod.c = "z";',
  8091. '$mod.Arr2[1] = "f";',
  8092. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8093. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8094. '']));
  8095. end;
  8096. procedure TTestModule.TestArray_StaticMultiDim;
  8097. begin
  8098. StartProgram(false);
  8099. Add([
  8100. 'type',
  8101. ' TArrayInt = array[1..3] of longint;',
  8102. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8103. 'var',
  8104. ' Arr: TArrayInt;',
  8105. ' Arr2: TArrayArrayInt;',
  8106. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8107. ' i: longint;',
  8108. 'begin',
  8109. ' i:=low(arr);',
  8110. ' i:=low(arr2);',
  8111. ' i:=low(arr2[5]);',
  8112. ' i:=high(arr);',
  8113. ' i:=high(arr2);',
  8114. ' i:=high(arr2[6]);',
  8115. ' arr2[5]:=arr;',
  8116. ' arr2[6][2]:=i;',
  8117. ' i:=arr2[6][3];',
  8118. ' arr2[6,3]:=i;',
  8119. ' i:=arr2[5,2];',
  8120. ' arr2:=arr2;',// clone multi dim static array
  8121. //' arr3:=arr3;',// clone anonymous multi dim static array
  8122. '']);
  8123. ConvertProgram;
  8124. CheckSource('TestArray_StaticMultiDim',
  8125. LinesToStr([ // statements
  8126. 'this.TArrayArrayInt$clone = function (a) {',
  8127. ' var r = [];',
  8128. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8129. ' return r;',
  8130. '};',
  8131. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8132. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8133. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8134. 'this.i = 0;'
  8135. ]),
  8136. LinesToStr([ // $mod.$main
  8137. '$mod.i = 1;',
  8138. '$mod.i = 5;',
  8139. '$mod.i = 1;',
  8140. '$mod.i = 3;',
  8141. '$mod.i = 6;',
  8142. '$mod.i = 3;',
  8143. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8144. '$mod.Arr2[1][1] = $mod.i;',
  8145. '$mod.i = $mod.Arr2[1][2];',
  8146. '$mod.Arr2[1][2] = $mod.i;',
  8147. '$mod.i = $mod.Arr2[0][1];',
  8148. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8149. '']));
  8150. end;
  8151. procedure TTestModule.TestArray_StaticInFunction;
  8152. begin
  8153. StartProgram(false);
  8154. Add([
  8155. 'const TArrayInt = 3;',
  8156. 'const TArrayArrayInt = 4;',
  8157. 'procedure DoIt;',
  8158. 'type',
  8159. ' TArrayInt = array[1..3] of longint;',
  8160. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8161. 'var',
  8162. ' Arr: TArrayInt;',
  8163. ' Arr2: TArrayArrayInt;',
  8164. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8165. ' i: longint;',
  8166. 'begin',
  8167. ' arr2[5]:=arr;',
  8168. ' arr2:=arr2;',// clone multi dim static array
  8169. 'end;',
  8170. 'begin',
  8171. '']);
  8172. ConvertProgram;
  8173. CheckSource('TestArray_StaticInFunction',
  8174. LinesToStr([ // statements
  8175. 'this.TArrayInt = 3;',
  8176. 'this.TArrayArrayInt = 4;',
  8177. 'var TArrayArrayInt$1$clone = function (a) {',
  8178. ' var r = [];',
  8179. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8180. ' return r;',
  8181. '};',
  8182. 'this.DoIt = function () {',
  8183. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8184. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8185. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8186. ' var i = 0;',
  8187. ' Arr2[0] = Arr.slice(0);',
  8188. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8189. '};',
  8190. '']),
  8191. LinesToStr([ // $mod.$main
  8192. '']));
  8193. end;
  8194. procedure TTestModule.TestArrayOfRecord;
  8195. begin
  8196. StartProgram(false);
  8197. Add([
  8198. 'type',
  8199. ' TRec = record',
  8200. ' Int: longint;',
  8201. ' end;',
  8202. ' TArrayRec = array of TRec;',
  8203. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8204. 'begin',
  8205. 'end;',
  8206. 'var',
  8207. ' Arr: TArrayRec;',
  8208. ' r: TRec;',
  8209. ' i: longint;',
  8210. 'begin',
  8211. ' SetLength(arr,3);',
  8212. ' arr[0].int:=4;',
  8213. ' arr[1].int:=length(arr)+arr[2].int;',
  8214. ' arr[arr[i].int].int:=arr[5].int;',
  8215. ' arr[7]:=r;',
  8216. ' r:=arr[8];',
  8217. ' i:=low(arr);',
  8218. ' i:=high(arr);',
  8219. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8220. ConvertProgram;
  8221. CheckSource('TestArrayOfRecord',
  8222. LinesToStr([ // statements
  8223. 'rtl.recNewT($mod, "TRec", function () {',
  8224. ' this.Int = 0;',
  8225. ' this.$eq = function (b) {',
  8226. ' return this.Int === b.Int;',
  8227. ' };',
  8228. ' this.$assign = function (s) {',
  8229. ' this.Int = s.Int;',
  8230. ' return this;',
  8231. ' };',
  8232. '});',
  8233. 'this.DoIt = function (vd, vc, vv) {',
  8234. '};',
  8235. 'this.Arr = [];',
  8236. 'this.r = $mod.TRec.$new();',
  8237. 'this.i = 0;'
  8238. ]),
  8239. LinesToStr([ // $mod.$main
  8240. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8241. '$mod.Arr[0].Int = 4;',
  8242. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8243. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8244. '$mod.Arr[7].$assign($mod.r);',
  8245. '$mod.r.$assign($mod.Arr[8]);',
  8246. '$mod.i = 0;',
  8247. '$mod.i = rtl.length($mod.Arr)-1;',
  8248. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8249. '']));
  8250. end;
  8251. procedure TTestModule.TestArray_StaticRecord;
  8252. begin
  8253. StartProgram(false);
  8254. Add([
  8255. 'type',
  8256. ' TRec = record',
  8257. ' Int: longint;',
  8258. ' end;',
  8259. ' TArrayRec = array[1..2] of TRec;',
  8260. 'var',
  8261. ' Arr: TArrayRec;',
  8262. 'begin',
  8263. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8264. '']);
  8265. ConvertProgram;
  8266. CheckSource('TestArray_StaticRecord',
  8267. LinesToStr([ // statements
  8268. 'rtl.recNewT($mod, "TRec", function () {',
  8269. ' this.Int = 0;',
  8270. ' this.$eq = function (b) {',
  8271. ' return this.Int === b.Int;',
  8272. ' };',
  8273. ' this.$assign = function (s) {',
  8274. ' this.Int = s.Int;',
  8275. ' return this;',
  8276. ' };',
  8277. '});',
  8278. 'this.TArrayRec$clone = function (a) {',
  8279. ' var r = [];',
  8280. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8281. ' return r;',
  8282. '};',
  8283. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8284. '']),
  8285. LinesToStr([ // $mod.$main
  8286. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8287. end;
  8288. procedure TTestModule.TestArrayOfSet;
  8289. begin
  8290. StartProgram(false);
  8291. Add([
  8292. 'type',
  8293. ' TFlag = (big,small);',
  8294. ' TSetOfFlag = set of tflag;',
  8295. ' TArrayFlag = array of TSetOfFlag;',
  8296. 'procedure DoIt(const a: Tarrayflag);',
  8297. 'begin',
  8298. 'end;',
  8299. 'var',
  8300. ' f: TFlag;',
  8301. ' s: TSetOfFlag;',
  8302. ' Arr: TArrayFlag;',
  8303. ' i: longint;',
  8304. 'begin',
  8305. ' SetLength(arr,3);',
  8306. ' arr[0]:=s;',
  8307. ' arr[1]:=[big];',
  8308. ' arr[2]:=[big]+s;',
  8309. ' arr[3]:=s+[big];',
  8310. ' arr[4]:=arr[5];',
  8311. ' s:=arr[6];',
  8312. ' i:=low(arr);',
  8313. ' i:=high(arr);',
  8314. ' DoIt(arr);',
  8315. ' DoIt([s]);',
  8316. ' DoIt([[],s]);',
  8317. ' DoIt([s,[]]);',
  8318. '']);
  8319. ConvertProgram;
  8320. CheckSource('TestArrayOfSet',
  8321. LinesToStr([ // statements
  8322. 'this.TFlag = {',
  8323. ' "0": "big",',
  8324. ' big: 0,',
  8325. ' "1": "small",',
  8326. ' small: 1',
  8327. '};',
  8328. 'this.DoIt = function (a) {',
  8329. '};',
  8330. 'this.f = 0;',
  8331. 'this.s = {};',
  8332. 'this.Arr = [];',
  8333. 'this.i = 0;',
  8334. '']),
  8335. LinesToStr([ // $mod.$main
  8336. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8337. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8338. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8339. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8340. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8341. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8342. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8343. '$mod.i = 0;',
  8344. '$mod.i = rtl.length($mod.Arr) - 1;',
  8345. '$mod.DoIt($mod.Arr);',
  8346. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8347. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8348. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8349. '']));
  8350. end;
  8351. procedure TTestModule.TestArray_DynAsParam;
  8352. begin
  8353. StartProgram(false);
  8354. Add([
  8355. 'type integer = longint;',
  8356. 'type TArrInt = array of integer;',
  8357. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8358. 'var vJ: TArrInt;',
  8359. 'begin',
  8360. ' vg:=vg;',
  8361. ' vj:=vh;',
  8362. ' vi:=vi;',
  8363. ' doit(vg,vg,vg);',
  8364. ' doit(vh,vh,vj);',
  8365. ' doit(vi,vi,vi);',
  8366. ' doit(vj,vj,vj);',
  8367. 'end;',
  8368. 'var i: TArrInt;',
  8369. 'begin',
  8370. ' doit(i,i,i);']);
  8371. ConvertProgram;
  8372. CheckSource('TestArray_DynAsParams',
  8373. LinesToStr([ // statements
  8374. 'this.DoIt = function (vG,vH,vI) {',
  8375. ' var vJ = [];',
  8376. ' vG = vG;',
  8377. ' vJ = vH;',
  8378. ' vI.set(vI.get());',
  8379. ' $mod.DoIt(vG, vG, {',
  8380. ' get: function () {',
  8381. ' return vG;',
  8382. ' },',
  8383. ' set: function (v) {',
  8384. ' vG = v;',
  8385. ' }',
  8386. ' });',
  8387. ' $mod.DoIt(vH, vH, {',
  8388. ' get: function () {',
  8389. ' return vJ;',
  8390. ' },',
  8391. ' set: function (v) {',
  8392. ' vJ = v;',
  8393. ' }',
  8394. ' });',
  8395. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8396. ' $mod.DoIt(vJ, vJ, {',
  8397. ' get: function () {',
  8398. ' return vJ;',
  8399. ' },',
  8400. ' set: function (v) {',
  8401. ' vJ = v;',
  8402. ' }',
  8403. ' });',
  8404. '};',
  8405. 'this.i = [];'
  8406. ]),
  8407. LinesToStr([
  8408. '$mod.DoIt($mod.i,$mod.i,{',
  8409. ' p: $mod,',
  8410. ' get: function () {',
  8411. ' return this.p.i;',
  8412. ' },',
  8413. ' set: function (v) {',
  8414. ' this.p.i = v;',
  8415. ' }',
  8416. '});'
  8417. ]));
  8418. end;
  8419. procedure TTestModule.TestArray_StaticAsParam;
  8420. begin
  8421. StartProgram(false);
  8422. Add([
  8423. 'type integer = longint;',
  8424. 'type TArrInt = array[1..2] of integer;',
  8425. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8426. 'var vJ: TArrInt;',
  8427. 'begin',
  8428. ' vg:=vg;',
  8429. ' vj:=vh;',
  8430. ' vi:=vi;',
  8431. ' doit(vg,vg,vg);',
  8432. ' doit(vh,vh,vj);',
  8433. ' doit(vi,vi,vi);',
  8434. ' doit(vj,vj,vj);',
  8435. 'end;',
  8436. 'var i: TArrInt;',
  8437. 'begin',
  8438. ' doit(i,i,i);']);
  8439. ConvertProgram;
  8440. CheckSource('TestArray_StaticAsParams',
  8441. LinesToStr([ // statements
  8442. 'this.DoIt = function (vG,vH,vI) {',
  8443. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8444. ' vG = vG.slice(0);',
  8445. ' vJ = vH.slice(0);',
  8446. ' vI.set(vI.get().slice(0));',
  8447. ' $mod.DoIt(vG.slice(0), vG, {',
  8448. ' get: function () {',
  8449. ' return vG;',
  8450. ' },',
  8451. ' set: function (v) {',
  8452. ' vG = v;',
  8453. ' }',
  8454. ' });',
  8455. ' $mod.DoIt(vH.slice(0), vH, {',
  8456. ' get: function () {',
  8457. ' return vJ;',
  8458. ' },',
  8459. ' set: function (v) {',
  8460. ' vJ = v;',
  8461. ' }',
  8462. ' });',
  8463. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8464. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8465. ' get: function () {',
  8466. ' return vJ;',
  8467. ' },',
  8468. ' set: function (v) {',
  8469. ' vJ = v;',
  8470. ' }',
  8471. ' });',
  8472. '};',
  8473. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8474. ]),
  8475. LinesToStr([
  8476. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8477. ' p: $mod,',
  8478. ' get: function () {',
  8479. ' return this.p.i;',
  8480. ' },',
  8481. ' set: function (v) {',
  8482. ' this.p.i = v;',
  8483. ' }',
  8484. '});'
  8485. ]));
  8486. end;
  8487. procedure TTestModule.TestArrayElement_AsParams;
  8488. begin
  8489. StartProgram(false);
  8490. Add('type integer = longint;');
  8491. Add('type TArrayInt = array of integer;');
  8492. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8493. Add('var vJ: tarrayint;');
  8494. Add('begin');
  8495. Add(' vi:=vi;');
  8496. Add(' doit(vi,vi,vi);');
  8497. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8498. Add('end;');
  8499. Add('var a: TArrayInt;');
  8500. Add('begin');
  8501. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8502. ConvertProgram;
  8503. CheckSource('TestArrayElement_AsParams',
  8504. LinesToStr([ // statements
  8505. 'this.DoIt = function (vG,vH,vI) {',
  8506. ' var vJ = [];',
  8507. ' vI.set(vI.get());',
  8508. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8509. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8510. ' a:1+3,',
  8511. ' p:vJ,',
  8512. ' get: function () {',
  8513. ' return this.p[this.a];',
  8514. ' },',
  8515. ' set: function (v) {',
  8516. ' this.p[this.a] = v;',
  8517. ' }',
  8518. ' });',
  8519. '};',
  8520. 'this.a = [];'
  8521. ]),
  8522. LinesToStr([
  8523. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8524. ' a: 1+6,',
  8525. ' p: $mod.a,',
  8526. ' get: function () {',
  8527. ' return this.p[this.a];',
  8528. ' },',
  8529. ' set: function (v) {',
  8530. ' this.p[this.a] = v;',
  8531. ' }',
  8532. '});'
  8533. ]));
  8534. end;
  8535. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8536. begin
  8537. StartProgram(false);
  8538. Add('type Integer = longint;');
  8539. Add('type TArrayInt = array of integer;');
  8540. Add('function GetArr(vB: integer = 0): tarrayint;');
  8541. Add('begin');
  8542. Add('end;');
  8543. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8544. Add('begin');
  8545. Add('end;');
  8546. Add('begin');
  8547. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8548. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8549. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8550. ConvertProgram;
  8551. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8552. LinesToStr([ // statements
  8553. 'this.GetArr = function (vB) {',
  8554. ' var Result = [];',
  8555. ' return Result;',
  8556. '};',
  8557. 'this.DoIt = function (vG,vH,vI) {',
  8558. '};'
  8559. ]),
  8560. LinesToStr([
  8561. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8562. ' a: 1+3,',
  8563. ' p: $mod.GetArr(0),',
  8564. ' get: function () {',
  8565. ' return this.p[this.a];',
  8566. ' },',
  8567. ' set: function (v) {',
  8568. ' this.p[this.a] = v;',
  8569. ' }',
  8570. '});',
  8571. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8572. ' a: 2+3,',
  8573. ' p: $mod.GetArr(0),',
  8574. ' get: function () {',
  8575. ' return this.p[this.a];',
  8576. ' },',
  8577. ' set: function (v) {',
  8578. ' this.p[this.a] = v;',
  8579. ' }',
  8580. '});',
  8581. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8582. ' a: 3+3,',
  8583. ' p: $mod.GetArr(9),',
  8584. ' get: function () {',
  8585. ' return this.p[this.a];',
  8586. ' },',
  8587. ' set: function (v) {',
  8588. ' this.p[this.a] = v;',
  8589. ' }',
  8590. '});',
  8591. '']));
  8592. end;
  8593. procedure TTestModule.TestArrayEnumTypeRange;
  8594. begin
  8595. StartProgram(false);
  8596. Add([
  8597. 'type',
  8598. ' TEnum = (red,blue);',
  8599. ' TEnumArray = array[TEnum] of longint;',
  8600. 'var',
  8601. ' e: TEnum;',
  8602. ' i: longint;',
  8603. ' a: TEnumArray;',
  8604. ' numbers: TEnumArray = (1,2);',
  8605. ' names: array[TEnum] of string = (''red'',''blue'');',
  8606. 'begin',
  8607. ' e:=low(a);',
  8608. ' e:=high(a);',
  8609. ' i:=a[red];',
  8610. ' a[e]:=a[e];']);
  8611. ConvertProgram;
  8612. CheckSource('TestArrayEnumTypeRange',
  8613. LinesToStr([ // statements
  8614. ' this.TEnum = {',
  8615. ' "0": "red",',
  8616. ' red: 0,',
  8617. ' "1": "blue",',
  8618. ' blue: 1',
  8619. '};',
  8620. 'this.e = 0;',
  8621. 'this.i = 0;',
  8622. 'this.a = rtl.arraySetLength(null,0,2);',
  8623. 'this.numbers = [1, 2];',
  8624. 'this.names = ["red", "blue"];',
  8625. '']),
  8626. LinesToStr([ // $mod.$main
  8627. '$mod.e = $mod.TEnum.red;',
  8628. '$mod.e = $mod.TEnum.blue;',
  8629. '$mod.i = $mod.a[$mod.TEnum.red];',
  8630. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8631. '']));
  8632. end;
  8633. procedure TTestModule.TestArray_SetLengthOutArg;
  8634. begin
  8635. StartProgram(false);
  8636. Add([
  8637. 'type TArrInt = array of longint;',
  8638. 'procedure DoIt(out a: TArrInt);',
  8639. 'begin',
  8640. ' SetLength(a,2);',
  8641. 'end;',
  8642. 'begin',
  8643. '']);
  8644. ConvertProgram;
  8645. CheckSource('TestArray_SetLengthOutArg',
  8646. LinesToStr([ // statements
  8647. 'this.DoIt = function (a) {',
  8648. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8649. '};',
  8650. '']),
  8651. LinesToStr([
  8652. '']));
  8653. end;
  8654. procedure TTestModule.TestArray_SetLengthProperty;
  8655. begin
  8656. StartProgram(false);
  8657. Add('type');
  8658. Add(' TArrInt = array of longint;');
  8659. Add(' TObject = class');
  8660. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8661. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8662. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8663. Add(' end;');
  8664. Add('var Obj: TObject;');
  8665. Add('begin');
  8666. Add(' SetLength(Obj.Colors,2);');
  8667. ConvertProgram;
  8668. CheckSource('TestArray_SetLengthProperty',
  8669. LinesToStr([ // statements
  8670. 'rtl.createClass($mod, "TObject", null, function () {',
  8671. ' this.$init = function () {',
  8672. ' };',
  8673. ' this.$final = function () {',
  8674. ' };',
  8675. '});',
  8676. 'this.Obj = null;',
  8677. '']),
  8678. LinesToStr([
  8679. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8680. '']));
  8681. end;
  8682. procedure TTestModule.TestArray_SetLengthMultiDim;
  8683. begin
  8684. StartProgram(false);
  8685. Add([
  8686. 'type',
  8687. ' TArrArrInt = array of array of longint;',
  8688. 'var',
  8689. ' a: TArrArrInt;',
  8690. 'begin',
  8691. ' SetLength(a,2);',
  8692. ' SetLength(a,3,4);',
  8693. '']);
  8694. ConvertProgram;
  8695. CheckSource('TestArray_SetLengthMultiDim',
  8696. LinesToStr([ // statements
  8697. 'this.a = [];']),
  8698. LinesToStr([
  8699. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8700. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8701. '']));
  8702. end;
  8703. procedure TTestModule.TestArray_OpenArrayOfString;
  8704. begin
  8705. StartProgram(false);
  8706. Add('procedure DoIt(const a: array of String);');
  8707. Add('var');
  8708. Add(' i: longint;');
  8709. Add(' s: string;');
  8710. Add('begin');
  8711. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8712. Add('end;');
  8713. Add('var s: string;');
  8714. Add('begin');
  8715. Add(' DoIt([]);');
  8716. Add(' DoIt([s,''foo'','''',s+s]);');
  8717. ConvertProgram;
  8718. CheckSource('TestArray_OpenArrayOfString',
  8719. LinesToStr([ // statements
  8720. 'this.DoIt = function (a) {',
  8721. ' var i = 0;',
  8722. ' var s = "";',
  8723. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8724. ' i = $l1;',
  8725. ' s = a[rtl.length(a) - i - 1];',
  8726. ' };',
  8727. '};',
  8728. 'this.s = "";',
  8729. '']),
  8730. LinesToStr([
  8731. '$mod.DoIt([]);',
  8732. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  8733. '']));
  8734. end;
  8735. procedure TTestModule.TestArray_Concat;
  8736. begin
  8737. StartProgram(false);
  8738. Add([
  8739. 'type',
  8740. ' integer = longint;',
  8741. ' TFlag = (big,small);',
  8742. ' TFlags = set of TFlag;',
  8743. ' TRec = record',
  8744. ' i: integer;',
  8745. ' end;',
  8746. ' TArrInt = array of integer;',
  8747. ' TArrRec = array of TRec;',
  8748. ' TArrFlag = array of TFlag;',
  8749. ' TArrSet = array of TFlags;',
  8750. ' TArrJSValue = array of jsvalue;',
  8751. 'var',
  8752. ' ArrInt: tarrint;',
  8753. ' ArrRec: tarrrec;',
  8754. ' ArrFlag: tarrflag;',
  8755. ' ArrSet: tarrset;',
  8756. ' ArrJSValue: tarrjsvalue;',
  8757. 'begin',
  8758. ' arrint:=concat(arrint);',
  8759. ' arrint:=concat(arrint,arrint);',
  8760. ' arrint:=concat(arrint,arrint,arrint);',
  8761. ' arrrec:=concat(arrrec);',
  8762. ' arrrec:=concat(arrrec,arrrec);',
  8763. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  8764. ' arrset:=concat(arrset);',
  8765. ' arrset:=concat(arrset,arrset);',
  8766. ' arrset:=concat(arrset,arrset,arrset);',
  8767. ' arrjsvalue:=concat(arrjsvalue);',
  8768. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  8769. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  8770. ' arrint:=concat([1],arrint);',
  8771. ' arrflag:=concat([big]);',
  8772. ' arrflag:=concat([big],arrflag);',
  8773. ' arrflag:=concat(arrflag,[small]);',
  8774. '']);
  8775. ConvertProgram;
  8776. CheckSource('TestArray_Concat',
  8777. LinesToStr([ // statements
  8778. 'this.TFlag = {',
  8779. ' "0": "big",',
  8780. ' big: 0,',
  8781. ' "1": "small",',
  8782. ' small: 1',
  8783. '};',
  8784. 'rtl.recNewT($mod, "TRec", function () {',
  8785. ' this.i = 0;',
  8786. ' this.$eq = function (b) {',
  8787. ' return this.i === b.i;',
  8788. ' };',
  8789. ' this.$assign = function (s) {',
  8790. ' this.i = s.i;',
  8791. ' return this;',
  8792. ' };',
  8793. '});',
  8794. 'this.ArrInt = [];',
  8795. 'this.ArrRec = [];',
  8796. 'this.ArrFlag = [];',
  8797. 'this.ArrSet = [];',
  8798. 'this.ArrJSValue = [];',
  8799. '']),
  8800. LinesToStr([ // $mod.$main
  8801. '$mod.ArrInt = $mod.ArrInt;',
  8802. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  8803. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  8804. '$mod.ArrRec = $mod.ArrRec;',
  8805. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  8806. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  8807. '$mod.ArrSet = $mod.ArrSet;',
  8808. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  8809. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  8810. '$mod.ArrJSValue = $mod.ArrJSValue;',
  8811. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  8812. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  8813. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  8814. '$mod.ArrFlag = [$mod.TFlag.big];',
  8815. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  8816. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  8817. '']));
  8818. end;
  8819. procedure TTestModule.TestArray_Copy;
  8820. begin
  8821. StartProgram(false);
  8822. Add([
  8823. 'type',
  8824. ' integer = longint;',
  8825. ' TFlag = (big,small);',
  8826. ' TFlags = set of TFlag;',
  8827. ' TRec = record',
  8828. ' i: integer;',
  8829. ' end;',
  8830. ' TArrInt = array of integer;',
  8831. ' TArrRec = array of TRec;',
  8832. ' TArrSet = array of TFlags;',
  8833. ' TArrJSValue = array of jsvalue;',
  8834. 'var',
  8835. ' ArrInt: tarrint;',
  8836. ' ArrRec: tarrrec;',
  8837. ' ArrSet: tarrset;',
  8838. ' ArrJSValue: tarrjsvalue;',
  8839. 'begin',
  8840. ' arrint:=copy(arrint);',
  8841. ' arrint:=copy(arrint,2);',
  8842. ' arrint:=copy(arrint,3,4);',
  8843. ' arrint:=copy([1,1],1,2);',
  8844. ' arrrec:=copy(arrrec);',
  8845. ' arrrec:=copy(arrrec,5);',
  8846. ' arrrec:=copy(arrrec,6,7);',
  8847. ' arrset:=copy(arrset);',
  8848. ' arrset:=copy(arrset,8);',
  8849. ' arrset:=copy(arrset,9,10);',
  8850. ' arrjsvalue:=copy(arrjsvalue);',
  8851. ' arrjsvalue:=copy(arrjsvalue,11);',
  8852. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  8853. ' ']);
  8854. ConvertProgram;
  8855. CheckSource('TestArray_Copy',
  8856. LinesToStr([ // statements
  8857. 'this.TFlag = {',
  8858. ' "0": "big",',
  8859. ' big: 0,',
  8860. ' "1": "small",',
  8861. ' small: 1',
  8862. '};',
  8863. 'rtl.recNewT($mod, "TRec", function () {',
  8864. ' this.i = 0;',
  8865. ' this.$eq = function (b) {',
  8866. ' return this.i === b.i;',
  8867. ' };',
  8868. ' this.$assign = function (s) {',
  8869. ' this.i = s.i;',
  8870. ' return this;',
  8871. ' };',
  8872. '});',
  8873. 'this.ArrInt = [];',
  8874. 'this.ArrRec = [];',
  8875. 'this.ArrSet = [];',
  8876. 'this.ArrJSValue = [];',
  8877. '']),
  8878. LinesToStr([ // $mod.$main
  8879. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  8880. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  8881. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  8882. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  8883. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  8884. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  8885. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  8886. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  8887. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  8888. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  8889. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  8890. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  8891. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  8892. '']));
  8893. end;
  8894. procedure TTestModule.TestArray_InsertDelete;
  8895. begin
  8896. StartProgram(false);
  8897. Add([
  8898. 'type',
  8899. ' integer = longint;',
  8900. ' TFlag = (big,small);',
  8901. ' TFlags = set of TFlag;',
  8902. ' TRec = record',
  8903. ' i: integer;',
  8904. ' end;',
  8905. ' TArrInt = array of integer;',
  8906. ' TArrRec = array of TRec;',
  8907. ' TArrSet = array of TFlags;',
  8908. ' TArrJSValue = array of jsvalue;',
  8909. ' TArrArrInt = array of TArrInt;',
  8910. 'var',
  8911. ' ArrInt: tarrint;',
  8912. ' ArrRec: tarrrec;',
  8913. ' ArrSet: tarrset;',
  8914. ' ArrJSValue: tarrjsvalue;',
  8915. ' ArrArrInt: TArrArrInt;',
  8916. 'begin',
  8917. ' Insert(1,arrint,2);',
  8918. ' Insert(arrint[3],arrint,4);',
  8919. ' Insert(arrrec[5],arrrec,6);',
  8920. ' Insert(arrset[7],arrset,7);',
  8921. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  8922. ' Insert(10,arrjsvalue,11);',
  8923. ' Insert([23],arrarrint,22);',
  8924. ' Delete(arrint,12,13);',
  8925. ' Delete(arrrec,14,15);',
  8926. ' Delete(arrset,17,18);',
  8927. ' Delete(arrjsvalue,19,10);']);
  8928. ConvertProgram;
  8929. CheckSource('TestArray_InsertDelete',
  8930. LinesToStr([ // statements
  8931. 'this.TFlag = {',
  8932. ' "0": "big",',
  8933. ' big: 0,',
  8934. ' "1": "small",',
  8935. ' small: 1',
  8936. '};',
  8937. 'rtl.recNewT($mod, "TRec", function () {',
  8938. ' this.i = 0;',
  8939. ' this.$eq = function (b) {',
  8940. ' return this.i === b.i;',
  8941. ' };',
  8942. ' this.$assign = function (s) {',
  8943. ' this.i = s.i;',
  8944. ' return this;',
  8945. ' };',
  8946. '});',
  8947. 'this.ArrInt = [];',
  8948. 'this.ArrRec = [];',
  8949. 'this.ArrSet = [];',
  8950. 'this.ArrJSValue = [];',
  8951. 'this.ArrArrInt = [];',
  8952. '']),
  8953. LinesToStr([ // $mod.$main
  8954. '$mod.ArrInt.splice(2, 0, 1);',
  8955. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  8956. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  8957. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  8958. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  8959. '$mod.ArrJSValue.splice(11, 0, 10);',
  8960. '$mod.ArrArrInt.splice(22, 0, [23]);',
  8961. '$mod.ArrInt.splice(12, 13);',
  8962. '$mod.ArrRec.splice(14, 15);',
  8963. '$mod.ArrSet.splice(17, 18);',
  8964. '$mod.ArrJSValue.splice(19, 10);',
  8965. '']));
  8966. end;
  8967. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  8968. begin
  8969. StartProgram(false);
  8970. Add([
  8971. '{$modeswitch arrayoperators}',
  8972. 'type',
  8973. ' integer = longint;',
  8974. ' TArrInt = array of integer;',
  8975. ' TArrStr = array of string;',
  8976. 'const',
  8977. ' Ints: TArrInt = (1,2,3);',
  8978. ' Aliases: TarrStr = (''foo'',''b'');',
  8979. ' OneInt: TArrInt = (7);',
  8980. ' OneStr: array of integer = (7);',
  8981. ' Chars: array of char = ''aoc'';',
  8982. ' Names: array of string = (''a'',''foo'');',
  8983. ' NameCount = low(Names)+high(Names)+length(Names);',
  8984. 'var i: integer;',
  8985. 'begin',
  8986. ' Ints:=[];',
  8987. ' Ints:=[1,1];',
  8988. ' Ints:=[1]+[2];',
  8989. ' Ints:=[2];',
  8990. ' Ints:=[]+ints;',
  8991. ' Ints:=Ints+[];',
  8992. ' Ints:=Ints+OneInt;',
  8993. ' Ints:=Ints+[1,1];',
  8994. ' Ints:=[i,i]+Ints;',
  8995. ' Ints:=[1]+[i]+[3];',
  8996. '']);
  8997. ConvertProgram;
  8998. CheckSource('TestArray_DynArrayConstObjFPC',
  8999. LinesToStr([ // statements
  9000. 'this.Ints = [1, 2, 3];',
  9001. 'this.Aliases = ["foo", "b"];',
  9002. 'this.OneInt = [7];',
  9003. 'this.OneStr = [7];',
  9004. 'this.Chars = ["a", "o", "c"];',
  9005. 'this.Names = ["a", "foo"];',
  9006. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9007. 'this.i = 0;',
  9008. '']),
  9009. LinesToStr([ // $mod.$main
  9010. '$mod.Ints = [];',
  9011. '$mod.Ints = [1, 1];',
  9012. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9013. '$mod.Ints = [2];',
  9014. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9015. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9016. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9017. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9018. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9019. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9020. '']));
  9021. end;
  9022. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9023. begin
  9024. StartProgram(false);
  9025. // Note: const c = [1,1]; defines a set!
  9026. Add([
  9027. '{$mode delphi}',
  9028. 'type',
  9029. ' integer = longint;',
  9030. ' TArrInt = array of integer;',
  9031. ' TArrStr = array of string;',
  9032. 'const',
  9033. ' Ints: TArrInt = [1,1,2];',
  9034. ' Aliases: TarrStr = [''foo'',''b''];',
  9035. ' OneInt: TArrInt = [7];',
  9036. ' OneStr: array of integer = [7]+[8];',
  9037. ' Chars: array of char = ''aoc'';',
  9038. ' Names: array of string = [''a'',''a''];',
  9039. ' NameCount = low(Names)+high(Names)+length(Names);',
  9040. 'begin',
  9041. '']);
  9042. ConvertProgram;
  9043. CheckSource('TestArray_DynArrayConstDelphi',
  9044. LinesToStr([ // statements
  9045. 'this.Ints = [1, 1, 2];',
  9046. 'this.Aliases = ["foo", "b"];',
  9047. 'this.OneInt = [7];',
  9048. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9049. 'this.Chars = ["a", "o", "c"];',
  9050. 'this.Names = ["a", "a"];',
  9051. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9052. '']),
  9053. LinesToStr([ // $mod.$main
  9054. '']));
  9055. end;
  9056. procedure TTestModule.TestArray_ArrayLitAsParam;
  9057. begin
  9058. StartProgram(false);
  9059. Add([
  9060. '{$modeswitch arrayoperators}',
  9061. 'type',
  9062. ' integer = longint;',
  9063. ' TArrInt = array of integer;',
  9064. ' TArrSet = array of (red,green,blue);',
  9065. 'procedure DoOpenInt(a: array of integer); forward;',
  9066. 'procedure DoInt(a: TArrInt);',
  9067. 'begin',
  9068. ' DoInt(a+[1]);',
  9069. ' DoInt([1]+a);',
  9070. ' DoOpenInt(a);',
  9071. ' DoOpenInt(a+[1]);',
  9072. ' DoOpenInt([1]+a);',
  9073. 'end;',
  9074. 'procedure DoOpenInt(a: array of integer);',
  9075. 'begin',
  9076. ' DoOpenInt(a+[1]);',
  9077. ' DoOpenInt([1]+a);',
  9078. ' DoInt(a);',
  9079. ' DoInt(a+[1]);',
  9080. ' DoInt([1]+a);',
  9081. 'end;',
  9082. 'procedure DoSet(a: TArrSet);',
  9083. 'begin',
  9084. ' DoSet(a+[red]);',
  9085. ' DoSet([blue]+a);',
  9086. 'end;',
  9087. 'var',
  9088. ' i: TArrInt;',
  9089. ' s: TArrSet;',
  9090. 'begin',
  9091. ' DoInt([1]);',
  9092. ' DoInt([1]+[2]);',
  9093. ' DoInt(i+[1]);',
  9094. ' DoInt([1]+i);',
  9095. ' DoOpenInt([1]);',
  9096. ' DoOpenInt([1]+[2]);',
  9097. ' DoOpenInt(i+[1]);',
  9098. ' DoOpenInt([1]+i);',
  9099. ' DoSet([red]);',
  9100. ' DoSet([blue]+[green]);',
  9101. ' DoSet(s+[blue]);',
  9102. ' DoSet([red]+s);',
  9103. '']);
  9104. ConvertProgram;
  9105. CheckSource('TestArray_ArrayLitAsParam',
  9106. LinesToStr([ // statements
  9107. 'this.TArrSet$a = {',
  9108. ' "0": "red",',
  9109. ' red: 0,',
  9110. ' "1": "green",',
  9111. ' green: 1,',
  9112. ' "2": "blue",',
  9113. ' blue: 2',
  9114. '};',
  9115. 'this.DoInt = function (a) {',
  9116. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9117. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9118. ' $mod.DoOpenInt(a);',
  9119. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9120. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9121. '};',
  9122. 'this.DoOpenInt = function (a) {',
  9123. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9124. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9125. ' $mod.DoInt(a);',
  9126. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9127. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9128. '};',
  9129. 'this.DoSet = function (a) {',
  9130. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9131. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9132. '};',
  9133. 'this.i = [];',
  9134. 'this.s = [];',
  9135. '']),
  9136. LinesToStr([ // $mod.$main
  9137. '$mod.DoInt([1]);',
  9138. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9139. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9140. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9141. '$mod.DoOpenInt([1]);',
  9142. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9143. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9144. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9145. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9146. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9147. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9148. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9149. '']));
  9150. end;
  9151. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9152. begin
  9153. StartProgram(false);
  9154. Add([
  9155. '{$modeswitch arrayoperators}',
  9156. 'type',
  9157. ' integer = longint;',
  9158. ' TArrInt = array of integer;',
  9159. ' TArrArrInt = array of TArrInt;',
  9160. 'procedure DoInt(a: TArrArrInt);',
  9161. 'begin',
  9162. ' DoInt(a+[[1]]);',
  9163. ' DoInt([[1]]+a);',
  9164. ' DoInt(a);',
  9165. 'end;',
  9166. 'var',
  9167. ' i: TArrInt;',
  9168. ' a: TArrArrInt;',
  9169. 'begin',
  9170. ' a:=[[1]];',
  9171. ' a:=[i];',
  9172. ' a:=a+[i];',
  9173. ' a:=[i]+a;',
  9174. ' a:=[[1]+i];',
  9175. ' a:=[[1]+[2]];',
  9176. ' a:=[i+[2]];',
  9177. ' DoInt([[1]]);',
  9178. ' DoInt([[1]+[2],[3,4],[5]]);',
  9179. ' DoInt([i+[1]]+a);',
  9180. ' DoInt([i]+a);',
  9181. '']);
  9182. ConvertProgram;
  9183. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9184. LinesToStr([ // statements
  9185. 'this.DoInt = function (a) {',
  9186. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9187. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9188. ' $mod.DoInt(a);',
  9189. '};',
  9190. 'this.i = [];',
  9191. 'this.a = [];',
  9192. '']),
  9193. LinesToStr([ // $mod.$main
  9194. '$mod.a = [[1]];',
  9195. '$mod.a = [$mod.i];',
  9196. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9197. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9198. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9199. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9200. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9201. '$mod.DoInt([[1]]);',
  9202. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9203. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9204. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9205. '']));
  9206. end;
  9207. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9208. begin
  9209. StartProgram(false);
  9210. Add([
  9211. '{$modeswitch arrayoperators}',
  9212. 'type',
  9213. ' integer = longint;',
  9214. ' TArrInt = array[1..2] of integer;',
  9215. ' TArrArrInt = array of TArrInt;',
  9216. 'procedure DoInt(a: TArrArrInt);',
  9217. 'begin',
  9218. ' DoInt(a+[[1,2]]);',
  9219. ' DoInt([[1,2]]+a);',
  9220. ' DoInt(a);',
  9221. 'end;',
  9222. 'var',
  9223. ' i: TArrInt;',
  9224. ' a: TArrArrInt;',
  9225. 'begin',
  9226. ' a:=[[1,1]];',
  9227. ' a:=[i];',
  9228. ' a:=a+[i];',
  9229. ' a:=[i]+a;',
  9230. ' DoInt([[1,1]]);',
  9231. ' DoInt([[1,2],[3,4]]);',
  9232. '']);
  9233. ConvertProgram;
  9234. CheckSource('TestArray_ArrayLitStaticAsParam',
  9235. LinesToStr([ // statements
  9236. 'this.DoInt = function (a) {',
  9237. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9238. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9239. ' $mod.DoInt(a);',
  9240. '};',
  9241. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9242. 'this.a = [];',
  9243. '']),
  9244. LinesToStr([ // $mod.$main
  9245. '$mod.a = [[1, 1]];',
  9246. '$mod.a = [$mod.i.slice(0)];',
  9247. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9248. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9249. '$mod.DoInt([[1, 1]]);',
  9250. '$mod.DoInt([[1, 2], [3, 4]]);',
  9251. '']));
  9252. end;
  9253. procedure TTestModule.TestArray_ForInArrOfString;
  9254. begin
  9255. StartProgram(false);
  9256. Add([
  9257. 'type',
  9258. 'type',
  9259. ' TMonthNameArray = array [1..12] of string;',
  9260. ' TMonthNames = TMonthNameArray;',
  9261. ' TObject = class',
  9262. ' private',
  9263. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9264. ' public',
  9265. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9266. ' end;',
  9267. 'var f: TObject;',
  9268. ' Month: string;',
  9269. 'begin',
  9270. ' for Month in f.LongMonthNames do ;',
  9271. '']);
  9272. ConvertProgram;
  9273. CheckSource('TestArray_ForInArrOfString',
  9274. LinesToStr([ // statements
  9275. 'rtl.createClass($mod, "TObject", null, function () {',
  9276. ' this.$init = function () {',
  9277. ' };',
  9278. ' this.$final = function () {',
  9279. ' };',
  9280. '});',
  9281. 'this.f = null;',
  9282. 'this.Month = "";',
  9283. '']),
  9284. LinesToStr([ // $mod.$main
  9285. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9286. '']));
  9287. end;
  9288. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9289. begin
  9290. StartProgram(false);
  9291. Add([
  9292. '{$modeswitch externalclass}',
  9293. 'type',
  9294. ' TJSObject = class external name ''Object''',
  9295. ' end;',
  9296. ' TJSArray = class external name ''Array''',
  9297. ' class function isArray(Value: JSValue) : boolean;',
  9298. ' function concat() : TJSArray; varargs;',
  9299. ' end;',
  9300. 'var',
  9301. ' aObj: TJSArray;',
  9302. ' a: array of longint;',
  9303. ' o: TJSObject;',
  9304. 'begin',
  9305. ' if TJSArray.isArray(65) then ;',
  9306. ' aObj:=TJSArray(a).concat(a);',
  9307. ' o:=TJSObject(a);']);
  9308. ConvertProgram;
  9309. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9310. LinesToStr([ // statements
  9311. 'this.aObj = null;',
  9312. 'this.a = [];',
  9313. 'this.o = null;',
  9314. '']),
  9315. LinesToStr([ // $mod.$main
  9316. 'if (Array.isArray(65)) ;',
  9317. '$mod.aObj = $mod.a.concat($mod.a);',
  9318. '$mod.o = $mod.a;',
  9319. '']));
  9320. end;
  9321. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9322. begin
  9323. StartProgram(false);
  9324. Add([
  9325. '{$modeswitch externalclass}',
  9326. 'type',
  9327. ' TArrStr = array of string;',
  9328. ' TJSArray = class external name ''Array''',
  9329. ' end;',
  9330. ' TJSObject = class external name ''Object''',
  9331. ' end;',
  9332. 'var',
  9333. ' aObj: TJSArray;',
  9334. ' a: TArrStr;',
  9335. ' jo: TJSObject;',
  9336. 'begin',
  9337. ' a:=TArrStr(aObj);',
  9338. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9339. ' a:=TarrStr(jo);',
  9340. '']);
  9341. ConvertProgram;
  9342. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9343. LinesToStr([ // statements
  9344. 'this.aObj = null;',
  9345. 'this.a = [];',
  9346. 'this.jo = null;',
  9347. '']),
  9348. LinesToStr([ // $mod.$main
  9349. '$mod.a = $mod.aObj;',
  9350. '$mod.aObj[1] = $mod.aObj[2];',
  9351. '$mod.a = $mod.jo;',
  9352. '']));
  9353. end;
  9354. procedure TTestModule.TestArrayOfConst_TVarRec;
  9355. begin
  9356. StartProgram(true,[supTVarRec]);
  9357. Add([
  9358. 'procedure Say(args: array of const);',
  9359. 'var',
  9360. ' i: longint;',
  9361. ' v: TVarRec;',
  9362. 'begin',
  9363. ' for i:=low(args) to high(args) do begin',
  9364. ' v:=args[i];',
  9365. ' case v.vtype of',
  9366. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9367. ' end;',
  9368. ' end;',
  9369. ' for v in args do ;',
  9370. ' args:=nil;',
  9371. ' SetLength(args,2);',
  9372. 'end;',
  9373. 'begin']);
  9374. ConvertProgram;
  9375. CheckSource('TestArrayOfConst_TVarRec',
  9376. LinesToStr([ // statements
  9377. 'this.Say = function (args) {',
  9378. ' var i = 0;',
  9379. ' var v = pas.system.TVarRec.$new();',
  9380. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9381. ' i = $l1;',
  9382. ' v.$assign(args[i]);',
  9383. ' var $tmp3 = v.VType;',
  9384. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9385. ' };',
  9386. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9387. ' args = [];',
  9388. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9389. '};',
  9390. '']),
  9391. LinesToStr([ // $mod.$main
  9392. ]));
  9393. end;
  9394. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9395. begin
  9396. StartProgram(true,[supTVarRec]);
  9397. Add([
  9398. 'procedure Say(args: array of const);',
  9399. 'begin',
  9400. ' Say(args);',
  9401. 'end;',
  9402. 'var',
  9403. ' p: Pointer;',
  9404. ' j: jsvalue;',
  9405. ' c: currency;',
  9406. 'begin',
  9407. ' Say([]);',
  9408. ' Say([1]);',
  9409. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9410. '']);
  9411. ConvertProgram;
  9412. CheckSource('TestArrayOfConst_PassBaseTypes',
  9413. LinesToStr([ // statements
  9414. 'this.Say = function (args) {',
  9415. ' $mod.Say(args);',
  9416. '};',
  9417. 'this.p = null;',
  9418. 'this.j = undefined;',
  9419. 'this.c = 0;',
  9420. '']),
  9421. LinesToStr([ // $mod.$main
  9422. '$mod.Say([]);',
  9423. '$mod.Say(pas.system.VarRecs(0, 1));',
  9424. '$mod.Say(pas.system.VarRecs(',
  9425. ' 9,',
  9426. ' "c",',
  9427. ' 18,',
  9428. ' "foo",',
  9429. ' 5,',
  9430. ' null,',
  9431. ' 1,',
  9432. ' true,',
  9433. ' 3,',
  9434. ' 1.3,',
  9435. ' 5,',
  9436. ' $mod.p,',
  9437. ' 20,',
  9438. ' $mod.j,',
  9439. ' 12,',
  9440. ' $mod.c',
  9441. ' ));',
  9442. '']));
  9443. end;
  9444. procedure TTestModule.TestArrayOfConst_PassObj;
  9445. begin
  9446. StartProgram(true,[supTVarRec]);
  9447. Add([
  9448. '{$interfaces corba}',
  9449. 'type',
  9450. ' TObject = class',
  9451. ' end;',
  9452. ' TClass = class of TObject;',
  9453. ' IUnknown = interface',
  9454. ' end;',
  9455. 'procedure Say(args: array of const);',
  9456. 'begin',
  9457. 'end;',
  9458. 'var',
  9459. ' o: TObject;',
  9460. ' c: TClass;',
  9461. ' i: IUnknown;',
  9462. 'begin',
  9463. ' Say([o,c,TObject]);',
  9464. ' Say([nil,i]);',
  9465. '']);
  9466. ConvertProgram;
  9467. CheckSource('TestArrayOfConst_PassObj',
  9468. LinesToStr([ // statements
  9469. 'rtl.createClass($mod, "TObject", null, function () {',
  9470. ' this.$init = function () {',
  9471. ' };',
  9472. ' this.$final = function () {',
  9473. ' };',
  9474. '});',
  9475. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9476. 'this.Say = function (args) {',
  9477. '};',
  9478. 'this.o = null;',
  9479. 'this.c = null;',
  9480. 'this.i = null;',
  9481. '']),
  9482. LinesToStr([ // $mod.$main
  9483. '$mod.Say(pas.system.VarRecs(',
  9484. ' 7,',
  9485. ' $mod.o,',
  9486. ' 8,',
  9487. ' $mod.c,',
  9488. ' 8,',
  9489. ' $mod.TObject',
  9490. '));',
  9491. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9492. '']));
  9493. end;
  9494. procedure TTestModule.TestRecord_Empty;
  9495. begin
  9496. StartProgram(false);
  9497. Add([
  9498. 'type',
  9499. ' TRecA = record',
  9500. ' end;',
  9501. 'var a,b: TRecA;',
  9502. 'begin',
  9503. ' if a=b then ;']);
  9504. ConvertProgram;
  9505. CheckSource('TestRecord_Empty',
  9506. LinesToStr([ // statements
  9507. 'rtl.recNewT($mod, "TRecA", function () {',
  9508. ' this.$eq = function (b) {',
  9509. ' return true;',
  9510. ' };',
  9511. ' this.$assign = function (s) {',
  9512. ' return this;',
  9513. ' };',
  9514. '});',
  9515. 'this.a = $mod.TRecA.$new();',
  9516. 'this.b = $mod.TRecA.$new();',
  9517. '']),
  9518. LinesToStr([ // $mod.$main
  9519. 'if ($mod.a.$eq($mod.b)) ;'
  9520. ]));
  9521. end;
  9522. procedure TTestModule.TestRecord_Var;
  9523. begin
  9524. StartProgram(false);
  9525. Add('type');
  9526. Add(' TRecA = record');
  9527. Add(' Bold: longint;');
  9528. Add(' end;');
  9529. Add('var Rec: TRecA;');
  9530. Add('begin');
  9531. Add(' rec.bold:=123');
  9532. ConvertProgram;
  9533. CheckSource('TestRecord_Var',
  9534. LinesToStr([ // statements
  9535. 'rtl.recNewT($mod, "TRecA", function () {',
  9536. ' this.Bold = 0;',
  9537. ' this.$eq = function (b) {',
  9538. ' return this.Bold === b.Bold;',
  9539. ' };',
  9540. ' this.$assign = function (s) {',
  9541. ' this.Bold = s.Bold;',
  9542. ' return this;',
  9543. ' };',
  9544. '});',
  9545. 'this.Rec = $mod.TRecA.$new();',
  9546. '']),
  9547. LinesToStr([ // $mod.$main
  9548. '$mod.Rec.Bold = 123;'
  9549. ]));
  9550. end;
  9551. procedure TTestModule.TestRecord_VarExternal;
  9552. begin
  9553. StartProgram(false);
  9554. Add([
  9555. '{$modeswitch externalclass}',
  9556. 'type',
  9557. ' TRecA = record',
  9558. ' i: byte;',
  9559. ' length_: longint external name ''length'';',
  9560. ' end;',
  9561. 'var Rec: TRecA;',
  9562. 'begin',
  9563. ' rec.length_ := rec.length_',
  9564. '']);
  9565. ConvertProgram;
  9566. CheckSource('TestRecord_VarExternal',
  9567. LinesToStr([ // statements
  9568. 'rtl.recNewT($mod, "TRecA", function () {',
  9569. ' this.i = 0;',
  9570. ' this.$eq = function (b) {',
  9571. ' return (this.i === b.i) && (this.length === b.length);',
  9572. ' };',
  9573. ' this.$assign = function (s) {',
  9574. ' this.i = s.i;',
  9575. ' this.length = s.length;',
  9576. ' return this;',
  9577. ' };',
  9578. '});',
  9579. 'this.Rec = $mod.TRecA.$new();',
  9580. '']),
  9581. LinesToStr([ // $mod.$main
  9582. '$mod.Rec.length = $mod.Rec.length;'
  9583. ]));
  9584. end;
  9585. procedure TTestModule.TestRecord_WithDo;
  9586. begin
  9587. StartProgram(false);
  9588. Add('type');
  9589. Add(' TRec = record');
  9590. Add(' vI: longint;');
  9591. Add(' end;');
  9592. Add('var');
  9593. Add(' Int: longint;');
  9594. Add(' r: TRec;');
  9595. Add('begin');
  9596. Add(' with r do');
  9597. Add(' int:=vi;');
  9598. Add(' with r do begin');
  9599. Add(' int:=vi;');
  9600. Add(' vi:=int;');
  9601. Add(' end;');
  9602. ConvertProgram;
  9603. CheckSource('TestWithRecordDo',
  9604. LinesToStr([ // statements
  9605. 'rtl.recNewT($mod, "TRec", function () {',
  9606. ' this.vI = 0;',
  9607. ' this.$eq = function (b) {',
  9608. ' return this.vI === b.vI;',
  9609. ' };',
  9610. ' this.$assign = function (s) {',
  9611. ' this.vI = s.vI;',
  9612. ' return this;',
  9613. ' };',
  9614. '});',
  9615. 'this.Int = 0;',
  9616. 'this.r = $mod.TRec.$new();',
  9617. '']),
  9618. LinesToStr([ // $mod.$main
  9619. 'var $with1 = $mod.r;',
  9620. '$mod.Int = $with1.vI;',
  9621. 'var $with2 = $mod.r;',
  9622. '$mod.Int = $with2.vI;',
  9623. '$with2.vI = $mod.Int;'
  9624. ]));
  9625. end;
  9626. procedure TTestModule.TestRecord_Assign;
  9627. begin
  9628. StartProgram(false);
  9629. Add('type');
  9630. Add(' TEnum = (red,green);');
  9631. Add(' TEnums = set of TEnum;');
  9632. Add(' TSmallRec = record');
  9633. Add(' N: longint;');
  9634. Add(' end;');
  9635. Add(' TBigRec = record');
  9636. Add(' Int: longint;');
  9637. Add(' D: double;');
  9638. Add(' Arr: array of longint;');
  9639. Add(' Arr2: array[1..2] of longint;');
  9640. Add(' Small: TSmallRec;');
  9641. Add(' Enums: TEnums;');
  9642. Add(' end;');
  9643. Add('var');
  9644. Add(' r, s: TBigRec;');
  9645. Add('begin');
  9646. Add(' r:=s;');
  9647. Add(' r:=default(TBigRec);');
  9648. Add(' r:=default(s);');
  9649. ConvertProgram;
  9650. CheckSource('TestRecord_Assign',
  9651. LinesToStr([ // statements
  9652. 'this.TEnum = {',
  9653. ' "0": "red",',
  9654. ' red: 0,',
  9655. ' "1": "green",',
  9656. ' green: 1',
  9657. '};',
  9658. 'rtl.recNewT($mod, "TSmallRec", function () {',
  9659. ' this.N = 0;',
  9660. ' this.$eq = function (b) {',
  9661. ' return this.N === b.N;',
  9662. ' };',
  9663. ' this.$assign = function (s) {',
  9664. ' this.N = s.N;',
  9665. ' return this;',
  9666. ' };',
  9667. '});',
  9668. 'rtl.recNewT($mod, "TBigRec", function () {',
  9669. ' this.Int = 0;',
  9670. ' this.D = 0.0;',
  9671. ' this.Arr = [];',
  9672. ' this.$new = function () {',
  9673. ' var r = Object.create(this);',
  9674. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9675. ' r.Small = $mod.TSmallRec.$new();',
  9676. ' r.Enums = {};',
  9677. ' return r;',
  9678. ' };',
  9679. ' this.$eq = function (b) {',
  9680. ' 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);',
  9681. ' };',
  9682. ' this.$assign = function (s) {',
  9683. ' this.Int = s.Int;',
  9684. ' this.D = s.D;',
  9685. ' this.Arr = s.Arr;',
  9686. ' this.Arr2 = s.Arr2.slice(0);',
  9687. ' this.Small.$assign(s.Small);',
  9688. ' this.Enums = rtl.refSet(s.Enums);',
  9689. ' return this;',
  9690. ' };',
  9691. '});',
  9692. 'this.r = $mod.TBigRec.$new();',
  9693. 'this.s = $mod.TBigRec.$new();',
  9694. '']),
  9695. LinesToStr([ // $mod.$main
  9696. '$mod.r.$assign($mod.s);',
  9697. '$mod.r.$assign($mod.TBigRec.$new());',
  9698. '$mod.r.$assign($mod.TBigRec.$new());',
  9699. '']));
  9700. end;
  9701. procedure TTestModule.TestRecord_AsParams;
  9702. begin
  9703. StartProgram(false);
  9704. Add([
  9705. 'type',
  9706. ' integer = longint;',
  9707. ' TRecord = record',
  9708. ' i: integer;',
  9709. ' end;',
  9710. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  9711. 'var vL: TRecord;',
  9712. 'begin',
  9713. ' vd:=vd;',
  9714. ' vd.i:=vd.i;',
  9715. ' vl:=vc;',
  9716. ' vv:=vv;',
  9717. ' vv.i:=vv.i;',
  9718. ' U:=vl;',
  9719. ' U:=vd;',
  9720. ' U:=vc;',
  9721. ' U:=vv;',
  9722. ' doit(vd,vd,vd,vd);',
  9723. ' doit(vc,vc,vl,vl);',
  9724. ' doit(vv,vv,vv,vv);',
  9725. ' doit(vl,vl,vl,vl);',
  9726. //' TRecord(U).i:=3;',
  9727. 'end;',
  9728. 'var i: TRecord;',
  9729. 'begin',
  9730. ' doit(i,i,i,i);']);
  9731. ConvertProgram;
  9732. CheckSource('TestRecord_AsParams',
  9733. LinesToStr([ // statements
  9734. 'rtl.recNewT($mod, "TRecord", function () {',
  9735. ' this.i = 0;',
  9736. ' this.$eq = function (b) {',
  9737. ' return this.i === b.i;',
  9738. ' };',
  9739. ' this.$assign = function (s) {',
  9740. ' this.i = s.i;',
  9741. ' return this;',
  9742. ' };',
  9743. '});',
  9744. 'this.DoIt = function (vD, vC, vV, U) {',
  9745. ' var vL = $mod.TRecord.$new();',
  9746. ' vD.$assign(vD);',
  9747. ' vD.i = vD.i;',
  9748. ' vL.$assign(vC);',
  9749. ' vV.$assign(vV);',
  9750. ' vV.i = vV.i;',
  9751. ' U.set(vL);',
  9752. ' U.set(vD);',
  9753. ' U.set(vC);',
  9754. ' U.set(vV);',
  9755. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  9756. ' get: function () {',
  9757. ' return vD;',
  9758. ' },',
  9759. ' set: function (v) {',
  9760. ' vD.$assign(v);',
  9761. ' }',
  9762. ' });',
  9763. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  9764. ' get: function () {',
  9765. ' return vL;',
  9766. ' },',
  9767. ' set: function (v) {',
  9768. ' vL.$assign(v);',
  9769. ' }',
  9770. ' });',
  9771. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  9772. ' get: function () {',
  9773. ' return vV;',
  9774. ' },',
  9775. ' set: function (v) {',
  9776. ' vV.$assign(v);',
  9777. ' }',
  9778. ' });',
  9779. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  9780. ' get: function () {',
  9781. ' return vL;',
  9782. ' },',
  9783. ' set: function (v) {',
  9784. ' vL.$assign(v);',
  9785. ' }',
  9786. ' });',
  9787. '};',
  9788. 'this.i = $mod.TRecord.$new();'
  9789. ]),
  9790. LinesToStr([
  9791. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  9792. ' p: $mod,',
  9793. ' get: function () {',
  9794. ' return this.p.i;',
  9795. ' },',
  9796. ' set: function (v) {',
  9797. ' this.p.i.$assign(v);',
  9798. ' }',
  9799. '});',
  9800. '']));
  9801. end;
  9802. procedure TTestModule.TestRecordElement_AsParams;
  9803. begin
  9804. StartProgram(false);
  9805. Add('type');
  9806. Add(' integer = longint;');
  9807. Add(' TRecord = record');
  9808. Add(' i: integer;');
  9809. Add(' end;');
  9810. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9811. Add('var vJ: TRecord;');
  9812. Add('begin');
  9813. Add(' doit(vj.i,vj.i,vj.i);');
  9814. Add('end;');
  9815. Add('var r: TRecord;');
  9816. Add('begin');
  9817. Add(' doit(r.i,r.i,r.i);');
  9818. ConvertProgram;
  9819. CheckSource('TestRecordElement_AsParams',
  9820. LinesToStr([ // statements
  9821. 'rtl.recNewT($mod, "TRecord", function () {',
  9822. ' this.i = 0;',
  9823. ' this.$eq = function (b) {',
  9824. ' return this.i === b.i;',
  9825. ' };',
  9826. ' this.$assign = function (s) {',
  9827. ' this.i = s.i;',
  9828. ' return this;',
  9829. ' };',
  9830. '});',
  9831. 'this.DoIt = function (vG,vH,vI) {',
  9832. ' var vJ = $mod.TRecord.$new();',
  9833. ' $mod.DoIt(vJ.i, vJ.i, {',
  9834. ' p: vJ,',
  9835. ' get: function () {',
  9836. ' return this.p.i;',
  9837. ' },',
  9838. ' set: function (v) {',
  9839. ' this.p.i = v;',
  9840. ' }',
  9841. ' });',
  9842. '};',
  9843. 'this.r = $mod.TRecord.$new();'
  9844. ]),
  9845. LinesToStr([
  9846. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  9847. ' p: $mod.r,',
  9848. ' get: function () {',
  9849. ' return this.p.i;',
  9850. ' },',
  9851. ' set: function (v) {',
  9852. ' this.p.i = v;',
  9853. ' }',
  9854. '});'
  9855. ]));
  9856. end;
  9857. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  9858. begin
  9859. StartProgram(false);
  9860. Add('type');
  9861. Add(' integer = longint;');
  9862. Add(' TRecord = record');
  9863. Add(' i: integer;');
  9864. Add(' end;');
  9865. Add('function GetRec(vB: integer = 0): TRecord;');
  9866. Add('begin');
  9867. Add('end;');
  9868. Add('procedure DoIt(vG: integer; const vH: integer);');
  9869. Add('begin');
  9870. Add('end;');
  9871. Add('begin');
  9872. Add(' doit(getrec.i,getrec.i);');
  9873. Add(' doit(getrec().i,getrec().i);');
  9874. Add(' doit(getrec(1).i,getrec(2).i);');
  9875. ConvertProgram;
  9876. CheckSource('TestRecordElementFromFuncResult_AsParams',
  9877. LinesToStr([ // statements
  9878. 'rtl.recNewT($mod, "TRecord", function () {',
  9879. ' this.i = 0;',
  9880. ' this.$eq = function (b) {',
  9881. ' return this.i === b.i;',
  9882. ' };',
  9883. ' this.$assign = function (s) {',
  9884. ' this.i = s.i;',
  9885. ' return this;',
  9886. ' };',
  9887. '});',
  9888. 'this.GetRec = function (vB) {',
  9889. ' var Result = $mod.TRecord.$new();',
  9890. ' return Result;',
  9891. '};',
  9892. 'this.DoIt = function (vG, vH) {',
  9893. '};',
  9894. '']),
  9895. LinesToStr([
  9896. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9897. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9898. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  9899. '']));
  9900. end;
  9901. procedure TTestModule.TestRecordElementFromWith_AsParams;
  9902. begin
  9903. StartProgram(false);
  9904. Add('type');
  9905. Add(' integer = longint;');
  9906. Add(' TRecord = record');
  9907. Add(' i: integer;');
  9908. Add(' end;');
  9909. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9910. Add('begin');
  9911. Add('end;');
  9912. Add('var r: trecord;');
  9913. Add('begin');
  9914. Add(' with r do ');
  9915. Add(' doit(i,i,i);');
  9916. ConvertProgram;
  9917. CheckSource('TestRecordElementFromWith_AsParams',
  9918. LinesToStr([ // statements
  9919. 'rtl.recNewT($mod, "TRecord", function () {',
  9920. ' this.i = 0;',
  9921. ' this.$eq = function (b) {',
  9922. ' return this.i === b.i;',
  9923. ' };',
  9924. ' this.$assign = function (s) {',
  9925. ' this.i = s.i;',
  9926. ' return this;',
  9927. ' };',
  9928. '});',
  9929. 'this.DoIt = function (vG,vH,vI) {',
  9930. '};',
  9931. 'this.r = $mod.TRecord.$new();'
  9932. ]),
  9933. LinesToStr([
  9934. 'var $with1 = $mod.r;',
  9935. '$mod.DoIt($with1.i,$with1.i,{',
  9936. ' p: $with1,',
  9937. ' get: function () {',
  9938. ' return this.p.i;',
  9939. ' },',
  9940. ' set: function (v) {',
  9941. ' this.p.i = v;',
  9942. ' }',
  9943. '});',
  9944. '']));
  9945. end;
  9946. procedure TTestModule.TestRecord_Equal;
  9947. begin
  9948. StartProgram(false);
  9949. Add('type');
  9950. Add(' integer = longint;');
  9951. Add(' TFlag = (red,blue);');
  9952. Add(' TFlags = set of TFlag;');
  9953. Add(' TProc = procedure;');
  9954. Add(' TRecord = record');
  9955. Add(' i: integer;');
  9956. Add(' Event: TProc;');
  9957. Add(' f: TFlags;');
  9958. Add(' end;');
  9959. Add(' TNested = record');
  9960. Add(' r: TRecord;');
  9961. Add(' end;');
  9962. Add('var');
  9963. Add(' b: boolean;');
  9964. Add(' r,s: trecord;');
  9965. Add('begin');
  9966. Add(' b:=r=s;');
  9967. Add(' b:=r<>s;');
  9968. ConvertProgram;
  9969. CheckSource('TestRecord_Equal',
  9970. LinesToStr([ // statements
  9971. 'this.TFlag = {',
  9972. ' "0": "red",',
  9973. ' red: 0,',
  9974. ' "1": "blue",',
  9975. ' blue: 1',
  9976. '};',
  9977. 'rtl.recNewT($mod, "TRecord", function () {',
  9978. ' this.i = 0;',
  9979. ' this.Event = null;',
  9980. ' this.$new = function () {',
  9981. ' var r = Object.create(this);',
  9982. ' r.f = {};',
  9983. ' return r;',
  9984. ' };',
  9985. ' this.$eq = function (b) {',
  9986. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  9987. ' };',
  9988. ' this.$assign = function (s) {',
  9989. ' this.i = s.i;',
  9990. ' this.Event = s.Event;',
  9991. ' this.f = rtl.refSet(s.f);',
  9992. ' return this;',
  9993. ' };',
  9994. '});',
  9995. 'rtl.recNewT($mod, "TNested", function () {',
  9996. ' this.$new = function () {',
  9997. ' var r = Object.create(this);',
  9998. ' r.r = $mod.TRecord.$new();',
  9999. ' return r;',
  10000. ' };',
  10001. ' this.$eq = function (b) {',
  10002. ' return this.r.$eq(b.r);',
  10003. ' };',
  10004. ' this.$assign = function (s) {',
  10005. ' this.r.$assign(s.r);',
  10006. ' return this;',
  10007. ' };',
  10008. '});',
  10009. 'this.b = false;',
  10010. 'this.r = $mod.TRecord.$new();',
  10011. 'this.s = $mod.TRecord.$new();',
  10012. '']),
  10013. LinesToStr([
  10014. '$mod.b = $mod.r.$eq($mod.s);',
  10015. '$mod.b = !$mod.r.$eq($mod.s);',
  10016. '']));
  10017. end;
  10018. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  10019. begin
  10020. StartProgram(false);
  10021. Add('type');
  10022. Add(' TRecord = record');
  10023. Add(' i: longint;');
  10024. Add(' end;');
  10025. Add('var');
  10026. Add(' Jv: jsvalue;');
  10027. Add(' Rec: trecord;');
  10028. Add('begin');
  10029. Add(' rec:=trecord(jv);');
  10030. ConvertProgram;
  10031. CheckSource('TestRecord_TypeCastJSValueToRecord',
  10032. LinesToStr([ // statements
  10033. 'rtl.recNewT($mod, "TRecord", function () {',
  10034. ' this.i = 0;',
  10035. ' this.$eq = function (b) {',
  10036. ' return this.i === b.i;',
  10037. ' };',
  10038. ' this.$assign = function (s) {',
  10039. ' this.i = s.i;',
  10040. ' return this;',
  10041. ' };',
  10042. '});',
  10043. 'this.Jv = undefined;',
  10044. 'this.Rec = $mod.TRecord.$new();',
  10045. '']),
  10046. LinesToStr([
  10047. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10048. '']));
  10049. end;
  10050. procedure TTestModule.TestRecord_VariantFail;
  10051. begin
  10052. StartProgram(false);
  10053. Add([
  10054. 'type',
  10055. ' TRec = record',
  10056. ' case word of',
  10057. ' 0: (b0, b1: Byte);',
  10058. ' 1: (i: word);',
  10059. ' end;',
  10060. 'begin']);
  10061. SetExpectedPasResolverError('variant record is not supported',
  10062. nXIsNotSupported);
  10063. ConvertProgram;
  10064. end;
  10065. procedure TTestModule.TestRecord_FieldArray;
  10066. begin
  10067. StartProgram(false);
  10068. Add([
  10069. 'type',
  10070. ' TArrInt = array[3..4] of longint;',
  10071. ' TArrArrInt = array[3..4] of longint;',
  10072. ' TRec = record',
  10073. ' a: array of longint;',
  10074. ' s: array[1..2] of longint;',
  10075. ' m: array[1..2,3..4] of longint;',
  10076. ' o: TArrArrInt;',
  10077. ' end;',
  10078. 'begin']);
  10079. ConvertProgram;
  10080. CheckSource('TestRecord_FieldArray',
  10081. LinesToStr([ // statements
  10082. 'rtl.recNewT($mod, "TRec", function () {',
  10083. ' this.a = [];',
  10084. ' this.$new = function () {',
  10085. ' var r = Object.create(this);',
  10086. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10087. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10088. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10089. ' return r;',
  10090. ' };',
  10091. ' this.$eq = function (b) {',
  10092. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10093. ' };',
  10094. ' this.$assign = function (s) {',
  10095. ' this.a = s.a;',
  10096. ' this.s = s.s.slice(0);',
  10097. ' this.m = s.m.slice(0);',
  10098. ' this.o = s.o.slice(0);',
  10099. ' return this;',
  10100. ' };',
  10101. '});',
  10102. '']),
  10103. LinesToStr([ // $mod.$main
  10104. '']));
  10105. end;
  10106. procedure TTestModule.TestRecord_Const;
  10107. begin
  10108. StartProgram(false);
  10109. Add([
  10110. 'type',
  10111. ' TArrInt = array[3..4] of longint;',
  10112. ' TPoint = record x,y: longint; end;',
  10113. ' TRec = record',
  10114. ' i: longint;',
  10115. ' a: array of longint;',
  10116. ' s: array[1..2] of longint;',
  10117. ' m: array[1..2,3..4] of longint;',
  10118. ' p: TPoint;',
  10119. ' end;',
  10120. ' TPoints = array of TPoint;',
  10121. 'const',
  10122. ' r: TRec = (',
  10123. ' i:1;',
  10124. ' a:(2,3);',
  10125. ' s:(4,5);',
  10126. ' m:( (11,12), (13,14) );',
  10127. ' p: (x:21; y:22)',
  10128. ' );',
  10129. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10130. 'begin']);
  10131. ConvertProgram;
  10132. CheckSource('TestRecord_Const',
  10133. LinesToStr([ // statements
  10134. 'rtl.recNewT($mod, "TPoint", function () {',
  10135. ' this.x = 0;',
  10136. ' this.y = 0;',
  10137. ' this.$eq = function (b) {',
  10138. ' return (this.x === b.x) && (this.y === b.y);',
  10139. ' };',
  10140. ' this.$assign = function (s) {',
  10141. ' this.x = s.x;',
  10142. ' this.y = s.y;',
  10143. ' return this;',
  10144. ' };',
  10145. '});',
  10146. 'rtl.recNewT($mod, "TRec", function () {',
  10147. ' this.i = 0;',
  10148. ' this.a = [];',
  10149. ' this.$new = function () {',
  10150. ' var r = Object.create(this);',
  10151. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10152. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10153. ' r.p = $mod.TPoint.$new();',
  10154. ' return r;',
  10155. ' };',
  10156. ' this.$eq = function (b) {',
  10157. ' 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);',
  10158. ' };',
  10159. ' this.$assign = function (s) {',
  10160. ' this.i = s.i;',
  10161. ' this.a = s.a;',
  10162. ' this.s = s.s.slice(0);',
  10163. ' this.m = s.m.slice(0);',
  10164. ' this.p.$assign(s.p);',
  10165. ' return this;',
  10166. ' };',
  10167. '});',
  10168. 'this.r = $mod.TRec.$clone({',
  10169. ' i: 1,',
  10170. ' a: [2, 3],',
  10171. ' s: [4, 5],',
  10172. ' m: [[11, 12], [13, 14]],',
  10173. ' p: $mod.TPoint.$clone({',
  10174. ' x: 21,',
  10175. ' y: 22',
  10176. ' })',
  10177. '});',
  10178. 'this.p = [$mod.TPoint.$clone({',
  10179. ' x: 1,',
  10180. ' y: 2',
  10181. '}), $mod.TPoint.$clone({',
  10182. ' x: 3,',
  10183. ' y: 4',
  10184. '})];',
  10185. '']),
  10186. LinesToStr([ // $mod.$main
  10187. '']));
  10188. end;
  10189. procedure TTestModule.TestRecord_TypecastFail;
  10190. begin
  10191. StartProgram(false);
  10192. Add([
  10193. 'type',
  10194. ' TPoint = record x,y: longint; end;',
  10195. ' TRec = record l: longint end;',
  10196. 'var p: TPoint;',
  10197. 'begin',
  10198. ' if TRec(p).l=2 then ;']);
  10199. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10200. nIllegalTypeConversionTo);
  10201. ConvertProgram;
  10202. end;
  10203. procedure TTestModule.TestRecord_InFunction;
  10204. begin
  10205. StartProgram(false);
  10206. Add([
  10207. 'var TPoint: longint = 3;',
  10208. 'procedure DoIt;',
  10209. 'type',
  10210. ' TPoint = record x,y: longint; end;',
  10211. ' TPoints = array of TPoint;',
  10212. 'var',
  10213. ' r: TPoint;',
  10214. ' p: TPoints;',
  10215. 'begin',
  10216. ' SetLength(p,2);',
  10217. 'end;',
  10218. 'begin']);
  10219. ConvertProgram;
  10220. CheckSource('TestRecord_InFunction',
  10221. LinesToStr([ // statements
  10222. 'this.TPoint = 3;',
  10223. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10224. ' this.x = 0;',
  10225. ' this.y = 0;',
  10226. ' this.$eq = function (b) {',
  10227. ' return (this.x === b.x) && (this.y === b.y);',
  10228. ' };',
  10229. ' this.$assign = function (s) {',
  10230. ' this.x = s.x;',
  10231. ' this.y = s.y;',
  10232. ' return this;',
  10233. ' };',
  10234. '});',
  10235. 'this.DoIt = function () {',
  10236. ' var r = TPoint$1.$new();',
  10237. ' var p = [];',
  10238. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10239. '};',
  10240. '']),
  10241. LinesToStr([ // $mod.$main
  10242. '']));
  10243. end;
  10244. procedure TTestModule.TestAdvRecord_Function;
  10245. begin
  10246. StartProgram(false);
  10247. Parser.Options:=Parser.Options+[po_cassignments];
  10248. Add([
  10249. '{$modeswitch AdvancedRecords}',
  10250. 'type',
  10251. ' TPoint = record',
  10252. ' x,y: word;',
  10253. ' function Add(const apt: TPoint): TPoint;',
  10254. ' end;',
  10255. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10256. 'begin',
  10257. ' Result:=Self;',
  10258. ' Result.x+=apt.x;',
  10259. ' Result.y:=Result.y+apt.y;',
  10260. ' Self:=apt;',
  10261. 'end;',
  10262. 'var p,q: TPoint;',
  10263. 'begin',
  10264. ' p.add(q);',
  10265. ' p:=default(TPoint);',
  10266. ' p:=q;',
  10267. '']);
  10268. ConvertProgram;
  10269. CheckSource('TestAdvRecord_Function',
  10270. LinesToStr([ // statements
  10271. 'rtl.recNewT($mod, "TPoint", function () {',
  10272. ' this.x = 0;',
  10273. ' this.y = 0;',
  10274. ' this.$eq = function (b) {',
  10275. ' return (this.x === b.x) && (this.y === b.y);',
  10276. ' };',
  10277. ' this.$assign = function (s) {',
  10278. ' this.x = s.x;',
  10279. ' this.y = s.y;',
  10280. ' return this;',
  10281. ' };',
  10282. ' this.Add = function (apt) {',
  10283. ' var Result = $mod.TPoint.$new();',
  10284. ' Result.$assign(this);',
  10285. ' Result.x += apt.x;',
  10286. ' Result.y = Result.y + apt.y;',
  10287. ' this.$assign(apt);',
  10288. ' return Result;',
  10289. ' };',
  10290. '});',
  10291. 'this.p = $mod.TPoint.$new();',
  10292. 'this.q = $mod.TPoint.$new();',
  10293. '']),
  10294. LinesToStr([ // $mod.$main
  10295. '$mod.p.Add($mod.q);',
  10296. '$mod.p.$assign($mod.TPoint.$new());',
  10297. '$mod.p.$assign($mod.q);',
  10298. '']));
  10299. end;
  10300. procedure TTestModule.TestAdvRecord_Property;
  10301. begin
  10302. StartProgram(false);
  10303. Add([
  10304. '{$modeswitch AdvancedRecords}',
  10305. 'type',
  10306. ' TPoint = record',
  10307. ' x,y: word;',
  10308. ' strict private',
  10309. ' function GetSize: longword;',
  10310. ' procedure SetSize(Value: longword);',
  10311. ' public',
  10312. ' property Size: longword read GetSize write SetSize;',
  10313. ' property Left: word read x write y;',
  10314. ' end;',
  10315. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10316. 'function TPoint.GetSize: longword;',
  10317. 'begin',
  10318. ' x:=y;',
  10319. ' Size:=Size;',
  10320. ' Left:=Left;',
  10321. 'end;',
  10322. 'procedure TPoint.SetSize(Value: longword);',
  10323. 'begin',
  10324. 'end;',
  10325. 'var p,q: TPoint;',
  10326. 'begin',
  10327. ' p.Size:=q.Size;',
  10328. ' p.Left:=q.Left;',
  10329. '']);
  10330. ConvertProgram;
  10331. CheckSource('TestAdvRecord_Property',
  10332. LinesToStr([ // statements
  10333. 'rtl.recNewT($mod, "TPoint", function () {',
  10334. ' this.x = 0;',
  10335. ' this.y = 0;',
  10336. ' this.$eq = function (b) {',
  10337. ' return (this.x === b.x) && (this.y === b.y);',
  10338. ' };',
  10339. ' this.$assign = function (s) {',
  10340. ' this.x = s.x;',
  10341. ' this.y = s.y;',
  10342. ' return this;',
  10343. ' };',
  10344. ' this.GetSize = function () {',
  10345. ' var Result = 0;',
  10346. ' this.x = this.y;',
  10347. ' this.SetSize(this.GetSize());',
  10348. ' this.y = this.x;',
  10349. ' return Result;',
  10350. ' };',
  10351. ' this.SetSize = function (Value) {',
  10352. ' };',
  10353. '});',
  10354. 'this.SetSize = function (Value) {',
  10355. '};',
  10356. 'this.p = $mod.TPoint.$new();',
  10357. 'this.q = $mod.TPoint.$new();',
  10358. '']),
  10359. LinesToStr([ // $mod.$main
  10360. '$mod.p.SetSize($mod.q.GetSize());',
  10361. '$mod.p.y = $mod.q.x;',
  10362. '']));
  10363. end;
  10364. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10365. begin
  10366. StartProgram(false);
  10367. Add([
  10368. '{$modeswitch AdvancedRecords}',
  10369. 'type',
  10370. ' TPoint = record',
  10371. ' strict private',
  10372. ' function GetItems(Index: word): word;',
  10373. ' procedure SetItems(Index: word; Value: word);',
  10374. ' public',
  10375. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10376. ' end;',
  10377. 'function TPoint.GetItems(Index: word): word;',
  10378. 'begin',
  10379. ' Items[index]:=Items[index];',
  10380. ' self.Items[index]:=self.Items[index];',
  10381. 'end;',
  10382. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10383. 'begin',
  10384. 'end;',
  10385. 'var p: TPoint;',
  10386. 'begin',
  10387. ' p[1]:=p[2];',
  10388. ' p.Items[3]:=p.Items[4];',
  10389. '']);
  10390. ConvertProgram;
  10391. CheckSource('TestAdvRecord_PropertyDefault',
  10392. LinesToStr([ // statements
  10393. 'rtl.recNewT($mod, "TPoint", function () {',
  10394. ' this.$eq = function (b) {',
  10395. ' return true;',
  10396. ' };',
  10397. ' this.$assign = function (s) {',
  10398. ' return this;',
  10399. ' };',
  10400. ' this.GetItems = function (Index) {',
  10401. ' var Result = 0;',
  10402. ' this.SetItems(Index, this.GetItems(Index));',
  10403. ' this.SetItems(Index, this.GetItems(Index));',
  10404. ' return Result;',
  10405. ' };',
  10406. ' this.SetItems = function (Index, Value) {',
  10407. ' };',
  10408. '});',
  10409. 'this.p = $mod.TPoint.$new();',
  10410. '']),
  10411. LinesToStr([ // $mod.$main
  10412. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10413. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10414. '']));
  10415. end;
  10416. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10417. begin
  10418. StartProgram(false);
  10419. Add([
  10420. '{$modeswitch AdvancedRecords}',
  10421. 'type',
  10422. ' TRec = record',
  10423. ' class var Fx: longint;',
  10424. ' class var Fy: longint;',
  10425. ' class function GetInt: longint; static;',
  10426. ' class procedure SetInt(Value: longint); static;',
  10427. ' class procedure DoIt; static;',
  10428. ' class property IntA: longint read Fx write Fy;',
  10429. ' class property IntB: longint read GetInt write SetInt;',
  10430. ' end;',
  10431. 'class function trec.getint: longint;',
  10432. 'begin',
  10433. ' result:=fx;',
  10434. 'end;',
  10435. 'class procedure trec.setint(value: longint);',
  10436. 'begin',
  10437. 'end;',
  10438. 'class procedure trec.doit;',
  10439. 'begin',
  10440. ' IntA:=IntA+1;',
  10441. ' IntB:=IntB+1;',
  10442. 'end;',
  10443. 'var r: trec;',
  10444. 'begin',
  10445. ' trec.inta:=trec.inta+1;',
  10446. ' if trec.intb=2 then;',
  10447. ' trec.intb:=trec.intb+2;',
  10448. ' trec.setint(trec.inta);',
  10449. ' r.inta:=r.inta+1;',
  10450. ' if r.intb=2 then;',
  10451. ' r.intb:=r.intb+2;',
  10452. ' r.setint(r.inta);']);
  10453. ConvertProgram;
  10454. CheckSource('TestAdvRecord_Property_ClassMethod',
  10455. LinesToStr([ // statements
  10456. 'rtl.recNewT($mod, "TRec", function () {',
  10457. ' this.Fx = 0;',
  10458. ' this.Fy = 0;',
  10459. ' this.$eq = function (b) {',
  10460. ' return true;',
  10461. ' };',
  10462. ' this.$assign = function (s) {',
  10463. ' return this;',
  10464. ' };',
  10465. ' this.GetInt = function () {',
  10466. ' var Result = 0;',
  10467. ' Result = this.Fx;',
  10468. ' return Result;',
  10469. ' };',
  10470. ' this.SetInt = function (Value) {',
  10471. ' };',
  10472. ' this.DoIt = function () {',
  10473. ' $mod.TRec.Fy = this.Fx + 1;',
  10474. ' this.SetInt(this.GetInt() + 1);',
  10475. ' };',
  10476. '}, true);',
  10477. 'this.r = $mod.TRec.$new();',
  10478. '']),
  10479. LinesToStr([ // $mod.$main
  10480. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10481. 'if ($mod.TRec.GetInt() === 2) ;',
  10482. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10483. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10484. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10485. 'if ($mod.r.GetInt() === 2) ;',
  10486. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10487. '$mod.r.SetInt($mod.r.Fx);',
  10488. '']));
  10489. end;
  10490. procedure TTestModule.TestAdvRecord_Const;
  10491. begin
  10492. StartProgram(false);
  10493. Add([
  10494. '{$modeswitch AdvancedRecords}',
  10495. 'type',
  10496. ' TArrInt = array[3..4] of longint;',
  10497. ' TPoint = record',
  10498. ' x,y: longint;',
  10499. ' class var Count: nativeint;',
  10500. ' end;',
  10501. ' TRec = record',
  10502. ' i: longint;',
  10503. ' a: array of longint;',
  10504. ' s: array[1..2] of longint;',
  10505. ' m: array[1..2,3..4] of longint;',
  10506. ' p: TPoint;',
  10507. ' end;',
  10508. ' TPoints = array of TPoint;',
  10509. 'const',
  10510. ' r: TRec = (',
  10511. ' i:1;',
  10512. ' a:(2,3);',
  10513. ' s:(4,5);',
  10514. ' m:( (11,12), (13,14) );',
  10515. ' p: (x:21)',
  10516. ' );',
  10517. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10518. 'begin']);
  10519. ConvertProgram;
  10520. CheckSource('TestAdvRecord_Const',
  10521. LinesToStr([ // statements
  10522. 'rtl.recNewT($mod, "TPoint", function () {',
  10523. ' this.x = 0;',
  10524. ' this.y = 0;',
  10525. ' this.Count = 0;',
  10526. ' this.$eq = function (b) {',
  10527. ' return (this.x === b.x) && (this.y === b.y);',
  10528. ' };',
  10529. ' this.$assign = function (s) {',
  10530. ' this.x = s.x;',
  10531. ' this.y = s.y;',
  10532. ' return this;',
  10533. ' };',
  10534. '}, true);',
  10535. 'rtl.recNewT($mod, "TRec", function () {',
  10536. ' this.i = 0;',
  10537. ' this.a = [];',
  10538. ' this.$new = function () {',
  10539. ' var r = Object.create(this);',
  10540. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10541. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10542. ' r.p = $mod.TPoint.$new();',
  10543. ' return r;',
  10544. ' };',
  10545. ' this.$eq = function (b) {',
  10546. ' 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);',
  10547. ' };',
  10548. ' this.$assign = function (s) {',
  10549. ' this.i = s.i;',
  10550. ' this.a = s.a;',
  10551. ' this.s = s.s.slice(0);',
  10552. ' this.m = s.m.slice(0);',
  10553. ' this.p.$assign(s.p);',
  10554. ' return this;',
  10555. ' };',
  10556. '});',
  10557. 'this.r = $mod.TRec.$clone({',
  10558. ' i: 1,',
  10559. ' a: [2, 3],',
  10560. ' s: [4, 5],',
  10561. ' m: [[11, 12], [13, 14]],',
  10562. ' p: $mod.TPoint.$clone({',
  10563. ' x: 21,',
  10564. ' y: 0',
  10565. ' })',
  10566. '});',
  10567. 'this.p = [$mod.TPoint.$clone({',
  10568. ' x: 1,',
  10569. ' y: 2',
  10570. '}), $mod.TPoint.$clone({',
  10571. ' x: 3,',
  10572. ' y: 4',
  10573. '})];',
  10574. '']),
  10575. LinesToStr([ // $mod.$main
  10576. '']));
  10577. end;
  10578. procedure TTestModule.TestAdvRecord_ExternalField;
  10579. begin
  10580. StartProgram(false);
  10581. Add([
  10582. '{$modeswitch AdvancedRecords}',
  10583. '{$modeswitch externalclass}',
  10584. 'type',
  10585. ' TCar = record',
  10586. ' public',
  10587. ' Intern: longint external name ''$Intern'';',
  10588. ' Intern2: longint external name ''$Intern2'';',
  10589. ' Bracket: longint external name ''["A B"]'';',
  10590. ' procedure DoIt;',
  10591. ' end;',
  10592. 'implementation',
  10593. 'procedure tcar.doit;',
  10594. 'begin',
  10595. ' Intern:=Intern+1;',
  10596. ' Intern2:=Intern2+2;',
  10597. ' Bracket:=Bracket+3;',
  10598. 'end;',
  10599. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  10600. 'begin',
  10601. ' Rec.intern:=Rec.intern+1;',
  10602. ' Rec.intern2:=Rec.intern2+2;',
  10603. ' Rec.Bracket:=Rec.Bracket+3;',
  10604. ' with Rec do begin',
  10605. ' intern:=intern+1;',
  10606. ' intern2:=intern2+2;',
  10607. ' Bracket:=Bracket+3;',
  10608. ' end;']);
  10609. ConvertProgram;
  10610. CheckSource('TestAdvRecord_ExternalField',
  10611. LinesToStr([ // statements
  10612. 'rtl.recNewT($mod, "TCar", function () {',
  10613. ' this.$eq = function (b) {',
  10614. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  10615. ' };',
  10616. ' this.$assign = function (s) {',
  10617. ' this.$Intern = s.$Intern;',
  10618. ' this.$Intern2 = s.$Intern2;',
  10619. ' this["A B"] = s["A B"];',
  10620. ' return this;',
  10621. ' };',
  10622. ' this.DoIt = function () {',
  10623. ' this.$Intern = this.$Intern + 1;',
  10624. ' this.$Intern2 = this.$Intern2 + 2;',
  10625. ' this["A B"] = this["A B"] + 3;',
  10626. ' };',
  10627. '});',
  10628. 'this.Rec = $mod.TCar.$clone({',
  10629. ' $Intern: 11,',
  10630. ' $Intern2: 12,',
  10631. ' "A B": 13',
  10632. '});',
  10633. '']),
  10634. LinesToStr([ // $mod.$main
  10635. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  10636. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  10637. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  10638. 'var $with1 = $mod.Rec;',
  10639. '$with1.$Intern = $with1.$Intern + 1;',
  10640. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  10641. '$with1["A B"] = $with1["A B"] + 3;',
  10642. '']));
  10643. end;
  10644. procedure TTestModule.TestAdvRecord_SubRecord;
  10645. begin
  10646. StartProgram(false);
  10647. Add([
  10648. '{$modeswitch AdvancedRecords}',
  10649. 'type',
  10650. ' TRec = record',
  10651. ' type',
  10652. ' TPoint = record',
  10653. ' x,y: longint;',
  10654. ' class var Count: nativeint;',
  10655. ' procedure DoIt;',
  10656. ' class procedure DoThat; static;',
  10657. ' end;',
  10658. ' var',
  10659. ' i: longint;',
  10660. ' p: TPoint;',
  10661. ' procedure DoSome;',
  10662. ' end;',
  10663. 'const',
  10664. ' r: TRec = (',
  10665. ' i:1;',
  10666. ' p: (x:21;y:22)',
  10667. ' );',
  10668. 'procedure TRec.DoSome;',
  10669. 'begin',
  10670. ' p.x:=p.y+1;',
  10671. ' p.Count:=p.Count+2;',
  10672. 'end;',
  10673. 'procedure TRec.TPoint.DoIt;',
  10674. 'begin',
  10675. ' Count:=Count+3;',
  10676. 'end;',
  10677. 'class procedure TRec.TPoint.DoThat;',
  10678. 'begin',
  10679. ' Count:=Count+4;',
  10680. 'end;',
  10681. 'begin']);
  10682. ConvertProgram;
  10683. CheckSource('TestAdvRecord_SubRecord',
  10684. LinesToStr([ // statements
  10685. 'rtl.recNewT($mod, "TRec", function () {',
  10686. ' rtl.recNewT(this, "TPoint", function () {',
  10687. ' this.x = 0;',
  10688. ' this.y = 0;',
  10689. ' this.Count = 0;',
  10690. ' this.$eq = function (b) {',
  10691. ' return (this.x === b.x) && (this.y === b.y);',
  10692. ' };',
  10693. ' this.$assign = function (s) {',
  10694. ' this.x = s.x;',
  10695. ' this.y = s.y;',
  10696. ' return this;',
  10697. ' };',
  10698. ' this.DoIt = function () {',
  10699. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  10700. ' };',
  10701. ' this.DoThat = function () {',
  10702. ' $mod.TRec.TPoint.Count = this.Count + 4;',
  10703. ' };',
  10704. ' }, true);',
  10705. ' this.i = 0;',
  10706. ' this.$new = function () {',
  10707. ' var r = Object.create(this);',
  10708. ' r.p = this.TPoint.$new();',
  10709. ' return r;',
  10710. ' };',
  10711. ' this.$eq = function (b) {',
  10712. ' return (this.i === b.i) && this.p.$eq(b.p);',
  10713. ' };',
  10714. ' this.$assign = function (s) {',
  10715. ' this.i = s.i;',
  10716. ' this.p.$assign(s.p);',
  10717. ' return this;',
  10718. ' };',
  10719. ' this.DoSome = function () {',
  10720. ' this.p.x = this.p.y + 1;',
  10721. ' this.TPoint.Count = this.p.Count + 2;',
  10722. ' };',
  10723. '}, true);',
  10724. 'this.r = $mod.TRec.$clone({',
  10725. ' i: 1,',
  10726. ' p: $mod.TRec.TPoint.$clone({',
  10727. ' x: 21,',
  10728. ' y: 22',
  10729. ' })',
  10730. '});',
  10731. '']),
  10732. LinesToStr([ // $mod.$main
  10733. '']));
  10734. end;
  10735. procedure TTestModule.TestAdvRecord_SubClass;
  10736. begin
  10737. StartProgram(false);
  10738. Add([
  10739. '{$modeswitch AdvancedRecords}',
  10740. 'type',
  10741. ' TObject = class end;',
  10742. ' TPoint = record',
  10743. ' type',
  10744. ' TBird = class',
  10745. ' procedure DoIt;',
  10746. ' class procedure Glob;',
  10747. ' end;',
  10748. ' procedure DoIt(b: TBird);',
  10749. ' end;',
  10750. 'procedure TPoint.TBird.DoIt;',
  10751. 'begin',
  10752. ' doit;',
  10753. ' self.doit;',
  10754. ' glob;',
  10755. ' self.glob;',
  10756. 'end;',
  10757. 'class procedure TPoint.TBird.Glob;',
  10758. 'begin',
  10759. ' glob;',
  10760. ' self.glob;',
  10761. 'end;',
  10762. 'procedure TPoint.DoIt(b: TBird);',
  10763. 'begin',
  10764. ' b.doit;',
  10765. ' b.glob;',
  10766. ' TBird.glob;',
  10767. 'end;',
  10768. 'begin',
  10769. '']);
  10770. ConvertProgram;
  10771. CheckSource('TestAdvRecord_SubClass',
  10772. LinesToStr([ // statements
  10773. 'rtl.createClass($mod, "TObject", null, function () {',
  10774. ' this.$init = function () {',
  10775. ' };',
  10776. ' this.$final = function () {',
  10777. ' };',
  10778. '});',
  10779. 'rtl.recNewT($mod, "TPoint", function () {',
  10780. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  10781. ' this.DoIt = function () {',
  10782. ' this.DoIt();',
  10783. ' this.DoIt();',
  10784. ' this.$class.Glob();',
  10785. ' this.$class.Glob();',
  10786. ' };',
  10787. ' this.Glob = function () {',
  10788. ' this.Glob();',
  10789. ' this.Glob();',
  10790. ' };',
  10791. ' });',
  10792. ' this.$eq = function (b) {',
  10793. ' return true;',
  10794. ' };',
  10795. ' this.$assign = function (s) {',
  10796. ' return this;',
  10797. ' };',
  10798. ' this.DoIt = function (b) {',
  10799. ' b.DoIt();',
  10800. ' b.$class.Glob();',
  10801. ' this.TBird.Glob();',
  10802. ' };',
  10803. '}, true);',
  10804. '']),
  10805. LinesToStr([ // $mod.$main
  10806. '']));
  10807. end;
  10808. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  10809. begin
  10810. StartProgram(false);
  10811. Add([
  10812. '{$modeswitch AdvancedRecords}',
  10813. 'type',
  10814. ' IUnknown = interface end;',
  10815. ' TPoint = record',
  10816. ' type IBird = interface end;',
  10817. ' end;',
  10818. 'begin',
  10819. '']);
  10820. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  10821. nNotYetImplemented);
  10822. ParseProgram;
  10823. end;
  10824. procedure TTestModule.TestAdvRecord_Constructor;
  10825. begin
  10826. StartProgram(false);
  10827. Add([
  10828. '{$modeswitch AdvancedRecords}',
  10829. 'type',
  10830. ' TPoint = record',
  10831. ' x,y: longint;',
  10832. ' constructor Create(ax: longint; ay: longint = -1);',
  10833. ' end;',
  10834. 'constructor tpoint.create(ax,ay: longint);',
  10835. 'begin',
  10836. ' x:=ax;',
  10837. ' self.y:=ay;',
  10838. 'end;',
  10839. 'var r: TPoint;',
  10840. 'begin',
  10841. ' r:=TPoint.Create(1,2);',
  10842. ' r.Create(3);',
  10843. ' r:=r.Create(4);',
  10844. '']);
  10845. ConvertProgram;
  10846. CheckSource('TestAdvRecord_Constructor',
  10847. LinesToStr([ // statements
  10848. 'rtl.recNewT($mod, "TPoint", function () {',
  10849. ' this.x = 0;',
  10850. ' this.y = 0;',
  10851. ' this.$eq = function (b) {',
  10852. ' return (this.x === b.x) && (this.y === b.y);',
  10853. ' };',
  10854. ' this.$assign = function (s) {',
  10855. ' this.x = s.x;',
  10856. ' this.y = s.y;',
  10857. ' return this;',
  10858. ' };',
  10859. ' this.Create = function (ax, ay) {',
  10860. ' this.x = ax;',
  10861. ' this.y = ay;',
  10862. ' return this;',
  10863. ' };',
  10864. '}, true);',
  10865. 'this.r = $mod.TPoint.$new();',
  10866. '']),
  10867. LinesToStr([ // $mod.$main
  10868. '$mod.r.$assign($mod.TPoint.$create("Create", [1, 2]));',
  10869. '$mod.r.Create(3, -1);',
  10870. '$mod.r.$assign($mod.r.Create(4, -1));',
  10871. '']));
  10872. end;
  10873. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  10874. begin
  10875. StartProgram(false);
  10876. Add([
  10877. '{$modeswitch AdvancedRecords}',
  10878. 'type',
  10879. ' TPoint = record',
  10880. ' class var x: longint;',
  10881. ' class procedure Fly; static;',
  10882. ' class constructor Init;',
  10883. ' end;',
  10884. 'var count: word;',
  10885. 'class procedure Tpoint.Fly;',
  10886. 'begin',
  10887. 'end;',
  10888. 'class constructor tpoint.init;',
  10889. 'begin',
  10890. ' count:=count+1;',
  10891. ' x:=3;',
  10892. ' tpoint.x:=4;',
  10893. ' fly;',
  10894. ' tpoint.fly;',
  10895. 'end;',
  10896. 'var r: TPoint;',
  10897. 'begin',
  10898. ' r.x:=10;',
  10899. '']);
  10900. ConvertProgram;
  10901. CheckSource('TestAdvRecord_ClassConstructor_Program',
  10902. LinesToStr([ // statements
  10903. 'rtl.recNewT($mod, "TPoint", function () {',
  10904. ' this.x = 0;',
  10905. ' this.$eq = function (b) {',
  10906. ' return true;',
  10907. ' };',
  10908. ' this.$assign = function (s) {',
  10909. ' return this;',
  10910. ' };',
  10911. ' this.Fly = function () {',
  10912. ' };',
  10913. '}, true);',
  10914. 'this.count = 0;',
  10915. 'this.r = $mod.TPoint.$new();',
  10916. '']),
  10917. LinesToStr([ // $mod.$main
  10918. '(function () {',
  10919. ' $mod.count = $mod.count + 1;',
  10920. ' $mod.TPoint.x = 3;',
  10921. ' $mod.TPoint.x = 4;',
  10922. ' $mod.TPoint.Fly();',
  10923. ' $mod.TPoint.Fly();',
  10924. '})();',
  10925. '$mod.TPoint.x = 10;',
  10926. '']));
  10927. end;
  10928. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  10929. begin
  10930. StartUnit(false);
  10931. Add([
  10932. 'interface',
  10933. '{$modeswitch AdvancedRecords}',
  10934. 'type',
  10935. ' TPoint = record',
  10936. ' class var x: longint;',
  10937. ' class procedure Fly; static;',
  10938. ' class constructor Init;',
  10939. ' end;',
  10940. 'implementation',
  10941. 'var count: word;',
  10942. 'class procedure Tpoint.Fly;',
  10943. 'begin',
  10944. 'end;',
  10945. 'class constructor tpoint.init;',
  10946. 'begin',
  10947. ' count:=count+1;',
  10948. ' x:=3;',
  10949. ' tpoint.x:=4;',
  10950. ' fly;',
  10951. ' tpoint.fly;',
  10952. 'end;',
  10953. '']);
  10954. ConvertUnit;
  10955. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  10956. LinesToStr([ // statements
  10957. 'var $impl = $mod.$impl;',
  10958. 'rtl.recNewT($mod, "TPoint", function () {',
  10959. ' this.x = 0;',
  10960. ' this.$eq = function (b) {',
  10961. ' return true;',
  10962. ' };',
  10963. ' this.$assign = function (s) {',
  10964. ' return this;',
  10965. ' };',
  10966. ' this.Fly = function () {',
  10967. ' };',
  10968. '}, true);',
  10969. '']),
  10970. LinesToStr([ // $mod.$init
  10971. '(function () {',
  10972. ' $impl.count = $impl.count + 1;',
  10973. ' $mod.TPoint.x = 3;',
  10974. ' $mod.TPoint.x = 4;',
  10975. ' $mod.TPoint.Fly();',
  10976. ' $mod.TPoint.Fly();',
  10977. '})();',
  10978. '']),
  10979. LinesToStr([ // $mod.$main
  10980. '$impl.count = 0;',
  10981. '']));
  10982. end;
  10983. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  10984. begin
  10985. StartProgram(false);
  10986. Add(['type',
  10987. ' TObject = class',
  10988. ' public',
  10989. ' constructor Create;',
  10990. ' destructor Destroy;',
  10991. ' end;',
  10992. ' TBird = TObject;',
  10993. 'constructor tobject.create;',
  10994. 'begin end;',
  10995. 'destructor tobject.destroy;',
  10996. 'begin end;',
  10997. 'var Obj: tobject;',
  10998. 'begin',
  10999. ' obj:=tobject.create;',
  11000. ' obj:=tobject.create();',
  11001. ' obj:=tbird.create;',
  11002. ' obj:=tbird.create();',
  11003. ' obj:=obj.create();',
  11004. ' obj.destroy;',
  11005. '']);
  11006. ConvertProgram;
  11007. CheckSource('TestClass_TObjectDefaultConstructor',
  11008. LinesToStr([ // statements
  11009. 'rtl.createClass($mod,"TObject",null,function(){',
  11010. ' this.$init = function () {',
  11011. ' };',
  11012. ' this.$final = function () {',
  11013. ' };',
  11014. ' this.Create = function(){',
  11015. ' return this;',
  11016. ' };',
  11017. ' this.Destroy = function(){',
  11018. ' };',
  11019. '});',
  11020. 'this.Obj = null;'
  11021. ]),
  11022. LinesToStr([ // $mod.$main
  11023. '$mod.Obj = $mod.TObject.$create("Create");',
  11024. '$mod.Obj = $mod.TObject.$create("Create");',
  11025. '$mod.Obj = $mod.TObject.$create("Create");',
  11026. '$mod.Obj = $mod.TObject.$create("Create");',
  11027. '$mod.Obj = $mod.Obj.Create();',
  11028. '$mod.Obj.$destroy("Destroy");',
  11029. '']));
  11030. end;
  11031. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11032. begin
  11033. StartProgram(false);
  11034. Add('type');
  11035. Add(' TObject = class');
  11036. Add(' public');
  11037. Add(' constructor Create(Par: longint);');
  11038. Add(' end;');
  11039. Add('constructor tobject.create(par: longint);');
  11040. Add('begin end;');
  11041. Add('var Obj: tobject;');
  11042. Add('begin');
  11043. Add(' obj:=tobject.create(3);');
  11044. ConvertProgram;
  11045. CheckSource('TestClass_TObjectConstructorWithParams',
  11046. LinesToStr([ // statements
  11047. 'rtl.createClass($mod,"TObject",null,function(){',
  11048. ' this.$init = function () {',
  11049. ' };',
  11050. ' this.$final = function () {',
  11051. ' };',
  11052. ' this.Create = function(Par){',
  11053. ' return this;',
  11054. ' };',
  11055. '});',
  11056. 'this.Obj = null;'
  11057. ]),
  11058. LinesToStr([ // $mod.$main
  11059. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11060. ]));
  11061. end;
  11062. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11063. begin
  11064. StartProgram(false);
  11065. Add('type');
  11066. Add(' TObject = class');
  11067. Add(' public');
  11068. Add(' constructor Create;');
  11069. Add(' end;');
  11070. Add(' TTest = class(TObject)');
  11071. Add(' public');
  11072. Add(' constructor Create(const Par: longint = 1);');
  11073. Add(' end;');
  11074. Add('constructor tobject.create;');
  11075. Add('begin end;');
  11076. Add('constructor ttest.create(const par: longint);');
  11077. Add('begin end;');
  11078. Add('var t: ttest;');
  11079. Add('begin');
  11080. Add(' t:=ttest.create;');
  11081. Add(' t:=ttest.create(2);');
  11082. ConvertProgram;
  11083. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11084. LinesToStr([ // statements
  11085. 'rtl.createClass($mod,"TObject",null,function(){',
  11086. ' this.$init = function () {',
  11087. ' };',
  11088. ' this.$final = function () {',
  11089. ' };',
  11090. ' this.Create = function(){',
  11091. ' return this;',
  11092. ' };',
  11093. '});',
  11094. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11095. ' this.Create$1 = function (Par) {',
  11096. ' return this;',
  11097. ' };',
  11098. '});',
  11099. 'this.t = null;'
  11100. ]),
  11101. LinesToStr([ // $mod.$main
  11102. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11103. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11104. ]));
  11105. end;
  11106. procedure TTestModule.TestClass_Var;
  11107. begin
  11108. StartProgram(false);
  11109. Add([
  11110. 'type',
  11111. ' TObject = class',
  11112. ' public',
  11113. ' vI: longint;',
  11114. ' constructor Create(Par: longint);',
  11115. ' end;',
  11116. 'constructor tobject.create(par: longint);',
  11117. 'begin',
  11118. ' vi:=par+3',
  11119. 'end;',
  11120. 'var Obj: tobject;',
  11121. 'begin',
  11122. ' obj:=tobject.create(4);',
  11123. ' obj.vi:=obj.VI+5;']);
  11124. ConvertProgram;
  11125. CheckSource('TestClass_Var',
  11126. LinesToStr([ // statements
  11127. 'rtl.createClass($mod,"TObject",null,function(){',
  11128. ' this.$init = function () {',
  11129. ' this.vI = 0;',
  11130. ' };',
  11131. ' this.$final = function () {',
  11132. ' };',
  11133. ' this.Create = function(Par){',
  11134. ' this.vI = Par+3;',
  11135. ' return this;',
  11136. ' };',
  11137. '});',
  11138. 'this.Obj = null;'
  11139. ]),
  11140. LinesToStr([ // $mod.$main
  11141. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11142. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11143. ]));
  11144. end;
  11145. procedure TTestModule.TestClass_Method;
  11146. begin
  11147. StartProgram(false);
  11148. Add('type');
  11149. Add(' TObject = class');
  11150. Add(' public');
  11151. Add(' vI: longint;');
  11152. Add(' Sub: TObject;');
  11153. Add(' constructor Create;');
  11154. Add(' function GetIt(Par: longint): tobject;');
  11155. Add(' end;');
  11156. Add('constructor tobject.create; begin end;');
  11157. Add('function tobject.getit(par: longint): tobject;');
  11158. Add('begin');
  11159. Add(' Self.vi:=par+3;');
  11160. Add(' Result:=self.sub;');
  11161. Add('end;');
  11162. Add('var Obj: tobject;');
  11163. Add('begin');
  11164. Add(' obj:=tobject.create;');
  11165. Add(' obj.getit(4);');
  11166. Add(' obj.sub.sub:=nil;');
  11167. Add(' obj.sub.getit(5);');
  11168. Add(' obj.sub.getit(6).SUB:=nil;');
  11169. Add(' obj.sub.getit(7).GETIT(8);');
  11170. Add(' obj.sub.getit(9).SuB.getit(10);');
  11171. ConvertProgram;
  11172. CheckSource('TestClass_Method',
  11173. LinesToStr([ // statements
  11174. 'rtl.createClass($mod,"TObject",null,function(){',
  11175. ' this.$init = function () {',
  11176. ' this.vI = 0;',
  11177. ' this.Sub = null;',
  11178. ' };',
  11179. ' this.$final = function () {',
  11180. ' this.Sub = undefined;',
  11181. ' };',
  11182. ' this.Create = function(){',
  11183. ' return this;',
  11184. ' };',
  11185. ' this.GetIt = function(Par){',
  11186. ' var Result = null;',
  11187. ' this.vI = Par + 3;',
  11188. ' Result = this.Sub;',
  11189. ' return Result;',
  11190. ' };',
  11191. '});',
  11192. 'this.Obj = null;'
  11193. ]),
  11194. LinesToStr([ // $mod.$main
  11195. '$mod.Obj = $mod.TObject.$create("Create");',
  11196. '$mod.Obj.GetIt(4);',
  11197. '$mod.Obj.Sub.Sub=null;',
  11198. '$mod.Obj.Sub.GetIt(5);',
  11199. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11200. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11201. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11202. ]));
  11203. end;
  11204. procedure TTestModule.TestClass_Implementation;
  11205. begin
  11206. StartUnit(false);
  11207. Add([
  11208. 'interface',
  11209. 'type',
  11210. ' TObject = class',
  11211. ' constructor Create;',
  11212. ' end;',
  11213. 'implementation',
  11214. 'type',
  11215. ' TIntClass = class',
  11216. ' constructor Create; reintroduce;',
  11217. ' class procedure DoGlob;',
  11218. ' end;',
  11219. 'constructor tintclass.create;',
  11220. 'begin',
  11221. ' inherited;',
  11222. ' inherited create;',
  11223. ' doglob;',
  11224. 'end;',
  11225. 'class procedure tintclass.doglob;',
  11226. 'begin',
  11227. 'end;',
  11228. 'constructor tobject.create;',
  11229. 'var',
  11230. ' iC: tintclass;',
  11231. 'begin',
  11232. ' ic:=tintclass.create;',
  11233. ' tintclass.doglob;',
  11234. ' ic.doglob;',
  11235. 'end;',
  11236. 'initialization',
  11237. ' tintclass.doglob;',
  11238. '']);
  11239. ConvertUnit;
  11240. CheckSource('TestClass_Implementation',
  11241. LinesToStr([ // statements
  11242. 'var $impl = $mod.$impl;',
  11243. 'rtl.createClass($mod, "TObject", null, function () {',
  11244. ' this.$init = function () {',
  11245. ' };',
  11246. ' this.$final = function () {',
  11247. ' };',
  11248. ' this.Create = function () {',
  11249. ' var iC = null;',
  11250. ' iC = $impl.TIntClass.$create("Create$1");',
  11251. ' $impl.TIntClass.DoGlob();',
  11252. ' iC.$class.DoGlob();',
  11253. ' return this;',
  11254. ' };',
  11255. '});',
  11256. '']),
  11257. LinesToStr([ // $mod.$main
  11258. '$impl.TIntClass.DoGlob();',
  11259. '']),
  11260. LinesToStr([
  11261. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11262. ' this.Create$1 = function () {',
  11263. ' $mod.TObject.Create.call(this);',
  11264. ' $mod.TObject.Create.call(this);',
  11265. ' this.$class.DoGlob();',
  11266. ' return this;',
  11267. ' };',
  11268. ' this.DoGlob = function () {',
  11269. ' };',
  11270. '});',
  11271. '']));
  11272. end;
  11273. procedure TTestModule.TestClass_Inheritance;
  11274. begin
  11275. StartProgram(false);
  11276. Add('type');
  11277. Add(' TObject = class');
  11278. Add(' public');
  11279. Add(' constructor Create;');
  11280. Add(' end;');
  11281. Add(' TClassA = class');
  11282. Add(' end;');
  11283. Add(' TClassB = class(TObject)');
  11284. Add(' procedure ProcB;');
  11285. Add(' end;');
  11286. Add('constructor tobject.create; begin end;');
  11287. Add('procedure tclassb.procb; begin end;');
  11288. Add('var');
  11289. Add(' oO: TObject;');
  11290. Add(' oA: TClassA;');
  11291. Add(' oB: TClassB;');
  11292. Add('begin');
  11293. Add(' oO:=tobject.Create;');
  11294. Add(' oA:=tclassa.Create;');
  11295. Add(' ob:=tclassb.Create;');
  11296. Add(' if oo is tclassa then ;');
  11297. Add(' ob:=oo as tclassb;');
  11298. Add(' (oo as tclassb).procb;');
  11299. ConvertProgram;
  11300. CheckSource('TestClass_Inheritance',
  11301. LinesToStr([ // statements
  11302. 'rtl.createClass($mod,"TObject",null,function(){',
  11303. ' this.$init = function () {',
  11304. ' };',
  11305. ' this.$final = function () {',
  11306. ' };',
  11307. ' this.Create = function () {',
  11308. ' return this;',
  11309. ' };',
  11310. '});',
  11311. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11312. '});',
  11313. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11314. ' this.ProcB = function () {',
  11315. ' };',
  11316. '});',
  11317. 'this.oO = null;',
  11318. 'this.oA = null;',
  11319. 'this.oB = null;'
  11320. ]),
  11321. LinesToStr([ // $mod.$main
  11322. '$mod.oO = $mod.TObject.$create("Create");',
  11323. '$mod.oA = $mod.TClassA.$create("Create");',
  11324. '$mod.oB = $mod.TClassB.$create("Create");',
  11325. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11326. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11327. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11328. ]));
  11329. end;
  11330. procedure TTestModule.TestClass_TypeAlias;
  11331. begin
  11332. StartProgram(false);
  11333. Add([
  11334. '{$interfaces corba}',
  11335. 'type',
  11336. ' IObject = interface',
  11337. ' end;',
  11338. ' IBird = type IObject;',
  11339. ' TObject = class',
  11340. ' end;',
  11341. ' TBird = type TObject;',
  11342. 'var',
  11343. ' oObj: TObject;',
  11344. ' oBird: TBird;',
  11345. ' IntfObj: IObject;',
  11346. ' IntfBird: IBird;',
  11347. 'begin',
  11348. ' oObj:=oBird;',
  11349. '']);
  11350. ConvertProgram;
  11351. CheckSource('TestClass_TypeAlias',
  11352. LinesToStr([ // statements
  11353. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11354. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11355. 'rtl.createClass($mod, "TObject", null, function () {',
  11356. ' this.$init = function () {',
  11357. ' };',
  11358. ' this.$final = function () {',
  11359. ' };',
  11360. '});',
  11361. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11362. '});',
  11363. 'this.oObj = null;',
  11364. 'this.oBird = null;',
  11365. 'this.IntfObj = null;',
  11366. 'this.IntfBird = null;',
  11367. '']),
  11368. LinesToStr([ // $mod.$main
  11369. '$mod.oObj = $mod.oBird;',
  11370. '']));
  11371. end;
  11372. procedure TTestModule.TestClass_AbstractMethod;
  11373. begin
  11374. StartProgram(false);
  11375. Add('type');
  11376. Add(' TObject = class');
  11377. Add(' public');
  11378. Add(' procedure DoIt; virtual; abstract;');
  11379. Add(' end;');
  11380. Add('begin');
  11381. ConvertProgram;
  11382. CheckSource('TestClass_AbstractMethod',
  11383. LinesToStr([ // statements
  11384. 'rtl.createClass($mod,"TObject",null,function(){',
  11385. ' this.$init = function () {',
  11386. ' };',
  11387. ' this.$final = function () {',
  11388. ' };',
  11389. '});'
  11390. ]),
  11391. LinesToStr([ // this.$main
  11392. ''
  11393. ]));
  11394. end;
  11395. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11396. begin
  11397. StartProgram(false);
  11398. Add([
  11399. 'type',
  11400. ' TObject = class',
  11401. ' procedure DoAbstract; virtual; abstract;',
  11402. ' procedure DoVirtual; virtual;',
  11403. ' procedure DoIt;',
  11404. ' end;',
  11405. ' TA = class',
  11406. ' procedure doabstract; override;',
  11407. ' procedure dovirtual; override;',
  11408. ' procedure DoSome;',
  11409. ' end;',
  11410. 'procedure tobject.dovirtual;',
  11411. 'begin',
  11412. ' inherited; // call non existing ancestor -> ignore silently',
  11413. 'end;',
  11414. 'procedure tobject.doit;',
  11415. 'begin',
  11416. 'end;',
  11417. 'procedure ta.doabstract;',
  11418. 'begin',
  11419. ' inherited dovirtual; // call TObject.DoVirtual',
  11420. 'end;',
  11421. 'procedure ta.dovirtual;',
  11422. 'begin',
  11423. ' inherited; // call TObject.DoVirtual',
  11424. ' inherited dovirtual; // call TObject.DoVirtual',
  11425. ' inherited dovirtual(); // call TObject.DoVirtual',
  11426. ' doit;',
  11427. ' doit();',
  11428. 'end;',
  11429. 'procedure ta.dosome;',
  11430. 'begin',
  11431. ' inherited; // call non existing ancestor method -> silently ignore',
  11432. 'end;',
  11433. 'begin']);
  11434. ConvertProgram;
  11435. CheckSource('TestClass_CallInherited_ProcNoParams',
  11436. LinesToStr([ // statements
  11437. 'rtl.createClass($mod,"TObject",null,function(){',
  11438. ' this.$init = function () {',
  11439. ' };',
  11440. ' this.$final = function () {',
  11441. ' };',
  11442. ' this.DoVirtual = function () {',
  11443. ' };',
  11444. ' this.DoIt = function () {',
  11445. ' };',
  11446. '});',
  11447. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11448. ' this.DoAbstract = function () {',
  11449. ' $mod.TObject.DoVirtual.call(this);',
  11450. ' };',
  11451. ' this.DoVirtual = function () {',
  11452. ' $mod.TObject.DoVirtual.call(this);',
  11453. ' $mod.TObject.DoVirtual.call(this);',
  11454. ' $mod.TObject.DoVirtual.call(this);',
  11455. ' this.DoIt();',
  11456. ' this.DoIt();',
  11457. ' };',
  11458. ' this.DoSome = function () {',
  11459. ' };',
  11460. '});'
  11461. ]),
  11462. LinesToStr([ // this.$main
  11463. ''
  11464. ]));
  11465. end;
  11466. procedure TTestModule.TestClass_CallInherited_WithParams;
  11467. begin
  11468. StartProgram(false);
  11469. Add([
  11470. 'type',
  11471. ' TObject = class',
  11472. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11473. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11474. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11475. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11476. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11477. ' end;',
  11478. ' TClassA = class',
  11479. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11480. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11481. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11482. ' end;',
  11483. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11484. 'begin',
  11485. 'end;',
  11486. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11487. 'begin',
  11488. 'end;',
  11489. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11490. 'begin',
  11491. 'end;',
  11492. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11493. 'begin',
  11494. 'end;',
  11495. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11496. 'begin',
  11497. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11498. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11499. 'end;',
  11500. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11501. 'begin',
  11502. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11503. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11504. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11505. ' doit(pa,pb);',
  11506. ' doit(pa);',
  11507. ' doit2(pa);',
  11508. ' doit2;',
  11509. 'end;',
  11510. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11511. 'begin',
  11512. ' pa:=inherited;',
  11513. 'end;',
  11514. 'begin']);
  11515. ConvertProgram;
  11516. CheckSource('TestClass_CallInherited_WithParams',
  11517. LinesToStr([ // statements
  11518. 'rtl.createClass($mod,"TObject",null,function(){',
  11519. ' this.$init = function () {',
  11520. ' };',
  11521. ' this.$final = function () {',
  11522. ' };',
  11523. ' this.DoVirtual = function (pA,pB) {',
  11524. ' };',
  11525. ' this.DoIt = function (pA,pB) {',
  11526. ' };',
  11527. ' this.DoIt2 = function (pA,pB) {',
  11528. ' };',
  11529. ' this.GetIt = function (pA, pB) {',
  11530. ' var Result = 0;',
  11531. ' return Result;',
  11532. ' };',
  11533. '});',
  11534. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11535. ' this.DoAbstract = function (pA,pB) {',
  11536. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11537. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11538. ' };',
  11539. ' this.DoVirtual = function (pA,pB) {',
  11540. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11541. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11542. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11543. ' this.DoIt(pA,pB);',
  11544. ' this.DoIt(pA,0);',
  11545. ' this.DoIt2(pA,2);',
  11546. ' this.DoIt2(1,2);',
  11547. ' };',
  11548. ' this.GetIt$1 = function (pA, pB) {',
  11549. ' var Result = 0;',
  11550. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11551. ' return Result;',
  11552. ' };',
  11553. '});'
  11554. ]),
  11555. LinesToStr([ // this.$main
  11556. ''
  11557. ]));
  11558. end;
  11559. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11560. begin
  11561. StartProgram(false);
  11562. Add('type');
  11563. Add(' TObject = class');
  11564. Add(' constructor Create; virtual;');
  11565. Add(' constructor CreateWithB(b: boolean);');
  11566. Add(' end;');
  11567. Add(' TA = class');
  11568. Add(' constructor Create; override;');
  11569. Add(' constructor CreateWithC(c: char);');
  11570. Add(' procedure DoIt;');
  11571. Add(' class function DoSome: TObject;');
  11572. Add(' end;');
  11573. Add('constructor tobject.create;');
  11574. Add('begin');
  11575. Add(' inherited; // call non existing ancestor -> ignore silently');
  11576. Add('end;');
  11577. Add('constructor tobject.createwithb(b: boolean);');
  11578. Add('begin');
  11579. Add(' inherited; // call non existing ancestor -> ignore silently');
  11580. Add(' create; // normal call');
  11581. Add('end;');
  11582. Add('constructor ta.create;');
  11583. Add('begin');
  11584. Add(' inherited; // normal call TObject.Create');
  11585. Add(' inherited create; // normal call TObject.Create');
  11586. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11587. Add('end;');
  11588. Add('constructor ta.createwithc(c: char);');
  11589. Add('begin');
  11590. Add(' inherited create; // call TObject.Create');
  11591. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11592. Add(' doit;');
  11593. Add(' doit();');
  11594. Add(' dosome;');
  11595. Add('end;');
  11596. Add('procedure ta.doit;');
  11597. Add('begin');
  11598. Add(' create; // normal call');
  11599. Add(' createwithb(false); // normal call');
  11600. Add(' createwithc(''c''); // normal call');
  11601. Add('end;');
  11602. Add('class function ta.dosome: TObject;');
  11603. Add('begin');
  11604. Add(' Result:=create; // constructor');
  11605. Add(' Result:=createwithb(true); // constructor');
  11606. Add(' Result:=createwithc(''c''); // constructor');
  11607. Add('end;');
  11608. Add('begin');
  11609. ConvertProgram;
  11610. CheckSource('TestClass_CallInheritedConstructor',
  11611. LinesToStr([ // statements
  11612. 'rtl.createClass($mod,"TObject",null,function(){',
  11613. ' this.$init = function () {',
  11614. ' };',
  11615. ' this.$final = function () {',
  11616. ' };',
  11617. ' this.Create = function () {',
  11618. ' return this;',
  11619. ' };',
  11620. ' this.CreateWithB = function (b) {',
  11621. ' this.Create();',
  11622. ' return this;',
  11623. ' };',
  11624. '});',
  11625. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11626. ' this.Create = function () {',
  11627. ' $mod.TObject.Create.call(this);',
  11628. ' $mod.TObject.Create.call(this);',
  11629. ' $mod.TObject.CreateWithB.call(this, false);',
  11630. ' return this;',
  11631. ' };',
  11632. ' this.CreateWithC = function (c) {',
  11633. ' $mod.TObject.Create.call(this);',
  11634. ' $mod.TObject.CreateWithB.call(this, true);',
  11635. ' this.DoIt();',
  11636. ' this.DoIt();',
  11637. ' this.$class.DoSome();',
  11638. ' return this;',
  11639. ' };',
  11640. ' this.DoIt = function () {',
  11641. ' this.Create();',
  11642. ' this.CreateWithB(false);',
  11643. ' this.CreateWithC("c");',
  11644. ' };',
  11645. ' this.DoSome = function () {',
  11646. ' var Result = null;',
  11647. ' Result = this.$create("Create");',
  11648. ' Result = this.$create("CreateWithB", [true]);',
  11649. ' Result = this.$create("CreateWithC", ["c"]);',
  11650. ' return Result;',
  11651. ' };',
  11652. '});'
  11653. ]),
  11654. LinesToStr([ // this.$main
  11655. ''
  11656. ]));
  11657. end;
  11658. procedure TTestModule.TestClass_ClassVar_Assign;
  11659. begin
  11660. StartProgram(false);
  11661. Add([
  11662. 'type',
  11663. ' TObject = class',
  11664. ' public',
  11665. ' class var vI: longint;',
  11666. ' class var Sub: TObject;',
  11667. ' constructor Create;',
  11668. ' class function GetIt(var Par: longint): tobject;',
  11669. ' end;',
  11670. 'constructor tobject.create;',
  11671. 'begin',
  11672. ' vi:=vi+1;',
  11673. ' Self.vi:=Self.vi+1;',
  11674. ' inc(vi);',
  11675. 'end;',
  11676. 'class function tobject.getit(var par: longint): tobject;',
  11677. 'begin',
  11678. ' vi:=vi+3;',
  11679. ' Self.vi:=Self.vi+4;',
  11680. ' inc(vi);',
  11681. ' Result:=self.sub;',
  11682. ' GetIt(vi);',
  11683. 'end;',
  11684. 'var Obj: tobject;',
  11685. 'begin',
  11686. ' obj:=tobject.create;',
  11687. ' tobject.vi:=3;',
  11688. ' if tobject.vi=4 then ;',
  11689. ' tobject.sub:=nil;',
  11690. ' obj.sub:=nil;',
  11691. ' obj.sub.sub:=nil;']);
  11692. ConvertProgram;
  11693. CheckSource('TestClass_ClassVar_Assign',
  11694. LinesToStr([ // statements
  11695. 'rtl.createClass($mod,"TObject",null,function(){',
  11696. ' this.vI = 0;',
  11697. ' this.Sub = null;',
  11698. ' this.$init = function () {',
  11699. ' };',
  11700. ' this.$final = function () {',
  11701. ' };',
  11702. ' this.Create = function(){',
  11703. ' $mod.TObject.vI = this.vI+1;',
  11704. ' $mod.TObject.vI = this.vI+1;',
  11705. ' $mod.TObject.vI += 1;',
  11706. ' return this;',
  11707. ' };',
  11708. ' this.GetIt = function(Par){',
  11709. ' var Result = null;',
  11710. ' $mod.TObject.vI = this.vI + 3;',
  11711. ' $mod.TObject.vI = this.vI + 4;',
  11712. ' $mod.TObject.vI += 1;',
  11713. ' Result = this.Sub;',
  11714. ' this.GetIt({',
  11715. ' p: $mod.TObject,',
  11716. ' get: function () {',
  11717. ' return this.p.vI;',
  11718. ' },',
  11719. ' set: function (v) {',
  11720. ' this.p.vI = v;',
  11721. ' }',
  11722. ' });',
  11723. ' return Result;',
  11724. ' };',
  11725. '});',
  11726. 'this.Obj = null;'
  11727. ]),
  11728. LinesToStr([ // $mod.$main
  11729. '$mod.Obj = $mod.TObject.$create("Create");',
  11730. '$mod.TObject.vI = 3;',
  11731. 'if ($mod.TObject.vI === 4);',
  11732. '$mod.TObject.Sub=null;',
  11733. '$mod.TObject.Sub=null;',
  11734. '$mod.TObject.Sub=null;',
  11735. '']));
  11736. end;
  11737. procedure TTestModule.TestClass_CallClassMethod;
  11738. begin
  11739. StartProgram(false);
  11740. Add('type');
  11741. Add(' TObject = class');
  11742. Add(' public');
  11743. Add(' class var vI: longint;');
  11744. Add(' class var Sub: TObject;');
  11745. Add(' constructor Create;');
  11746. Add(' function GetMore(Par: longint): longint;');
  11747. Add(' class function GetIt(Par: longint): tobject;');
  11748. Add(' end;');
  11749. Add('constructor tobject.create;');
  11750. Add('begin');
  11751. Add(' sub:=getit(3);');
  11752. Add(' vi:=getmore(4);');
  11753. Add(' sub:=Self.getit(5);');
  11754. Add(' vi:=Self.getmore(6);');
  11755. Add('end;');
  11756. Add('function tobject.getmore(par: longint): longint;');
  11757. Add('begin');
  11758. Add(' sub:=getit(11);');
  11759. Add(' vi:=getmore(12);');
  11760. Add(' sub:=self.getit(13);');
  11761. Add(' vi:=self.getmore(14);');
  11762. Add('end;');
  11763. Add('class function tobject.getit(par: longint): tobject;');
  11764. Add('begin');
  11765. Add(' sub:=getit(21);');
  11766. Add(' vi:=sub.getmore(22);');
  11767. Add(' sub:=self.getit(23);');
  11768. Add(' vi:=self.sub.getmore(24);');
  11769. Add('end;');
  11770. Add('var Obj: tobject;');
  11771. Add('begin');
  11772. Add(' obj:=tobject.create;');
  11773. Add(' tobject.getit(5);');
  11774. Add(' obj.getit(6);');
  11775. Add(' obj.sub.getit(7);');
  11776. Add(' obj.sub.getit(8).SUB:=nil;');
  11777. Add(' obj.sub.getit(9).GETIT(10);');
  11778. Add(' obj.sub.getit(11).SuB.getit(12);');
  11779. ConvertProgram;
  11780. CheckSource('TestClass_CallClassMethod',
  11781. LinesToStr([ // statements
  11782. 'rtl.createClass($mod,"TObject",null,function(){',
  11783. ' this.vI = 0;',
  11784. ' this.Sub = null;',
  11785. ' this.$init = function () {',
  11786. ' };',
  11787. ' this.$final = function () {',
  11788. ' };',
  11789. ' this.Create = function(){',
  11790. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  11791. ' $mod.TObject.vI = this.GetMore(4);',
  11792. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  11793. ' $mod.TObject.vI = this.GetMore(6);',
  11794. ' return this;',
  11795. ' };',
  11796. ' this.GetMore = function(Par){',
  11797. ' var Result = 0;',
  11798. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  11799. ' $mod.TObject.vI = this.GetMore(12);',
  11800. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  11801. ' $mod.TObject.vI = this.GetMore(14);',
  11802. ' return Result;',
  11803. ' };',
  11804. ' this.GetIt = function(Par){',
  11805. ' var Result = null;',
  11806. ' $mod.TObject.Sub = this.GetIt(21);',
  11807. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  11808. ' $mod.TObject.Sub = this.GetIt(23);',
  11809. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  11810. ' return Result;',
  11811. ' };',
  11812. '});',
  11813. 'this.Obj = null;'
  11814. ]),
  11815. LinesToStr([ // $mod.$main
  11816. '$mod.Obj = $mod.TObject.$create("Create");',
  11817. '$mod.TObject.GetIt(5);',
  11818. '$mod.Obj.$class.GetIt(6);',
  11819. '$mod.Obj.Sub.$class.GetIt(7);',
  11820. '$mod.TObject.Sub=null;',
  11821. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  11822. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  11823. '']));
  11824. end;
  11825. procedure TTestModule.TestClass_Property;
  11826. begin
  11827. StartProgram(false);
  11828. Add('type');
  11829. Add(' TObject = class');
  11830. Add(' Fx: longint;');
  11831. Add(' Fy: longint;');
  11832. Add(' function GetInt: longint;');
  11833. Add(' procedure SetInt(Value: longint);');
  11834. Add(' procedure DoIt;');
  11835. Add(' property IntA: longint read Fx write Fy;');
  11836. Add(' property IntB: longint read GetInt write SetInt;');
  11837. Add(' end;');
  11838. Add('function tobject.getint: longint;');
  11839. Add('begin');
  11840. Add(' result:=fx;');
  11841. Add('end;');
  11842. Add('procedure tobject.setint(value: longint);');
  11843. Add('begin');
  11844. Add(' if value=fy then exit;');
  11845. Add(' fy:=value;');
  11846. Add('end;');
  11847. Add('procedure tobject.doit;');
  11848. Add('begin');
  11849. Add(' IntA:=IntA+1;');
  11850. Add(' Self.IntA:=Self.IntA+1;');
  11851. Add(' IntB:=IntB+1;');
  11852. Add(' Self.IntB:=Self.IntB+1;');
  11853. Add('end;');
  11854. Add('var Obj: tobject;');
  11855. Add('begin');
  11856. Add(' obj.inta:=obj.inta+1;');
  11857. Add(' if obj.intb=2 then;');
  11858. Add(' obj.intb:=obj.intb+2;');
  11859. Add(' obj.setint(obj.inta);');
  11860. ConvertProgram;
  11861. CheckSource('TestClass_Property',
  11862. LinesToStr([ // statements
  11863. 'rtl.createClass($mod, "TObject", null, function () {',
  11864. ' this.$init = function () {',
  11865. ' this.Fx = 0;',
  11866. ' this.Fy = 0;',
  11867. ' };',
  11868. ' this.$final = function () {',
  11869. ' };',
  11870. ' this.GetInt = function () {',
  11871. ' var Result = 0;',
  11872. ' Result = this.Fx;',
  11873. ' return Result;',
  11874. ' };',
  11875. ' this.SetInt = function (Value) {',
  11876. ' if (Value === this.Fy) return;',
  11877. ' this.Fy = Value;',
  11878. ' };',
  11879. ' this.DoIt = function () {',
  11880. ' this.Fy = this.Fx + 1;',
  11881. ' this.Fy = this.Fx + 1;',
  11882. ' this.SetInt(this.GetInt() + 1);',
  11883. ' this.SetInt(this.GetInt() + 1);',
  11884. ' };',
  11885. '});',
  11886. 'this.Obj = null;'
  11887. ]),
  11888. LinesToStr([ // $mod.$main
  11889. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  11890. 'if ($mod.Obj.GetInt() === 2);',
  11891. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  11892. '$mod.Obj.SetInt($mod.Obj.Fx);'
  11893. ]));
  11894. end;
  11895. procedure TTestModule.TestClass_Property_ClassMethod;
  11896. begin
  11897. StartProgram(false);
  11898. Add([
  11899. 'type',
  11900. ' TObject = class',
  11901. ' class var Fx: longint;',
  11902. ' class var Fy: longint;',
  11903. ' class function GetInt: longint;',
  11904. ' class procedure SetInt(Value: longint);',
  11905. ' end;',
  11906. ' TBird = class',
  11907. ' class procedure DoIt;',
  11908. ' class property IntA: longint read Fx write Fy;',
  11909. ' class property IntB: longint read GetInt write SetInt;',
  11910. ' end;',
  11911. 'class function tobject.getint: longint;',
  11912. 'begin',
  11913. ' result:=fx;',
  11914. 'end;',
  11915. 'class procedure tobject.setint(value: longint);',
  11916. 'begin',
  11917. 'end;',
  11918. 'class procedure tbird.doit;',
  11919. 'begin',
  11920. ' FX:=3;',
  11921. ' IntA:=IntA+1;',
  11922. ' Self.IntA:=Self.IntA+1;',
  11923. ' IntB:=IntB+1;',
  11924. ' Self.IntB:=Self.IntB+1;',
  11925. ' with Self do begin',
  11926. ' FX:=11;',
  11927. ' IntA:=IntA+12;',
  11928. ' IntB:=IntB+13;',
  11929. ' end;',
  11930. 'end;',
  11931. 'var Obj: tbird;',
  11932. 'begin',
  11933. ' tbird.fx:=tbird.fx+1;',
  11934. ' tbird.inta:=tbird.inta+1;',
  11935. ' if tbird.intb=2 then;',
  11936. ' tbird.intb:=tbird.intb+2;',
  11937. ' tbird.setint(tbird.inta);',
  11938. ' obj.inta:=obj.inta+1;',
  11939. ' if obj.intb=2 then;',
  11940. ' obj.intb:=obj.intb+2;',
  11941. ' obj.setint(obj.inta);',
  11942. ' with Tbird do begin',
  11943. ' FX:=FY+1;',
  11944. ' inta:=inta+2;',
  11945. ' intb:=intb+3;',
  11946. ' end;',
  11947. ' with Obj do begin',
  11948. ' FX:=FY+1;',
  11949. ' inta:=inta+2;',
  11950. ' intb:=intb+3;',
  11951. ' end;',
  11952. '']);
  11953. ConvertProgram;
  11954. CheckSource('TestClass_Property_ClassMethod',
  11955. LinesToStr([ // statements
  11956. 'rtl.createClass($mod, "TObject", null, function () {',
  11957. ' this.Fx = 0;',
  11958. ' this.Fy = 0;',
  11959. ' this.$init = function () {',
  11960. ' };',
  11961. ' this.$final = function () {',
  11962. ' };',
  11963. ' this.GetInt = function () {',
  11964. ' var Result = 0;',
  11965. ' Result = this.Fx;',
  11966. ' return Result;',
  11967. ' };',
  11968. ' this.SetInt = function (Value) {',
  11969. ' };',
  11970. '});',
  11971. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11972. ' this.DoIt = function () {',
  11973. ' $mod.TObject.Fx = 3;',
  11974. ' $mod.TObject.Fy = this.Fx + 1;',
  11975. ' $mod.TObject.Fy = this.Fx + 1;',
  11976. ' this.SetInt(this.GetInt() + 1);',
  11977. ' this.SetInt(this.GetInt() + 1);',
  11978. ' $mod.TObject.Fx = 11;',
  11979. ' $mod.TObject.Fy = this.Fx + 12;',
  11980. ' this.SetInt(this.GetInt() + 13);',
  11981. ' };',
  11982. '});',
  11983. 'this.Obj = null;'
  11984. ]),
  11985. LinesToStr([ // $mod.$main
  11986. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  11987. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  11988. 'if ($mod.TBird.GetInt() === 2);',
  11989. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  11990. '$mod.TBird.SetInt($mod.TBird.Fx);',
  11991. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  11992. 'if ($mod.Obj.$class.GetInt() === 2);',
  11993. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  11994. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  11995. 'var $with1 = $mod.TBird;',
  11996. '$mod.TObject.Fx = $with1.Fy + 1;',
  11997. '$mod.TObject.Fy = $with1.Fx + 2;',
  11998. '$with1.SetInt($with1.GetInt() + 3);',
  11999. 'var $with2 = $mod.Obj;',
  12000. '$mod.TObject.Fx = $with2.Fy + 1;',
  12001. '$mod.TObject.Fy = $with2.Fx + 2;',
  12002. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  12003. '']));
  12004. end;
  12005. procedure TTestModule.TestClass_Property_Indexed;
  12006. begin
  12007. StartProgram(false);
  12008. Add('type');
  12009. Add(' TObject = class');
  12010. Add(' FItems: array of longint;');
  12011. Add(' function GetItems(Index: longint): longint;');
  12012. Add(' procedure SetItems(Index: longint; Value: longint);');
  12013. Add(' procedure DoIt;');
  12014. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  12015. Add(' end;');
  12016. Add('function tobject.getitems(index: longint): longint;');
  12017. Add('begin');
  12018. Add(' Result:=fitems[index];');
  12019. Add('end;');
  12020. Add('procedure tobject.setitems(index: longint; value: longint);');
  12021. Add('begin');
  12022. Add(' fitems[index]:=value;');
  12023. Add('end;');
  12024. Add('procedure tobject.doit;');
  12025. Add('begin');
  12026. Add(' items[1]:=2;');
  12027. Add(' items[3]:=items[4];');
  12028. Add(' self.items[5]:=self.items[6];');
  12029. Add(' items[items[7]]:=items[items[8]];');
  12030. Add('end;');
  12031. Add('var Obj: tobject;');
  12032. Add('begin');
  12033. Add(' obj.Items[11]:=obj.Items[12];');
  12034. ConvertProgram;
  12035. CheckSource('TestClass_Property_Indexed',
  12036. LinesToStr([ // statements
  12037. 'rtl.createClass($mod, "TObject", null, function () {',
  12038. ' this.$init = function () {',
  12039. ' this.FItems = [];',
  12040. ' };',
  12041. ' this.$final = function () {',
  12042. ' this.FItems = undefined;',
  12043. ' };',
  12044. ' this.GetItems = function (Index) {',
  12045. ' var Result = 0;',
  12046. ' Result = this.FItems[Index];',
  12047. ' return Result;',
  12048. ' };',
  12049. ' this.SetItems = function (Index, Value) {',
  12050. ' this.FItems[Index] = Value;',
  12051. ' };',
  12052. ' this.DoIt = function () {',
  12053. ' this.SetItems(1, 2);',
  12054. ' this.SetItems(3,this.GetItems(4));',
  12055. ' this.SetItems(5,this.GetItems(6));',
  12056. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12057. ' };',
  12058. '});',
  12059. 'this.Obj = null;'
  12060. ]),
  12061. LinesToStr([ // $mod.$main
  12062. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12063. ]));
  12064. end;
  12065. procedure TTestModule.TestClass_Property_IndexSpec;
  12066. begin
  12067. StartProgram(false);
  12068. Add([
  12069. 'type',
  12070. ' TEnum = (red, blue);',
  12071. ' TObject = class',
  12072. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12073. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12074. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12075. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12076. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12077. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12078. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12079. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12080. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12081. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12082. ' end;',
  12083. 'procedure DoIt(b: boolean); begin end;',
  12084. 'var',
  12085. ' o: TObject;',
  12086. 'begin',
  12087. ' o.B1:=o.B1;',
  12088. ' o.B2:=o.B2;',
  12089. ' o.B3:=o.B3;',
  12090. ' o.I1[''a'']:=o.I1[''b''];',
  12091. ' doit(o.b1);',
  12092. ' doit(o.b2);',
  12093. ' doit(o.i1[''c'']);',
  12094. '']);
  12095. ConvertProgram;
  12096. CheckSource('TestClass_Property_IndexSpec',
  12097. LinesToStr([ // statements
  12098. 'this.TEnum = {',
  12099. ' "0": "red",',
  12100. ' red: 0,',
  12101. ' "1": "blue",',
  12102. ' blue: 1',
  12103. '};',
  12104. 'rtl.createClass($mod, "TObject", null, function () {',
  12105. ' this.$init = function () {',
  12106. ' };',
  12107. ' this.$final = function () {',
  12108. ' };',
  12109. '});',
  12110. 'this.DoIt = function (b) {',
  12111. '};',
  12112. 'this.o = null;',
  12113. '']),
  12114. LinesToStr([ // $mod.$main
  12115. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12116. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12117. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12118. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12119. '$mod.DoIt($mod.o.GetIntBool(1));',
  12120. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12121. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12122. '']));
  12123. end;
  12124. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12125. begin
  12126. StartProgram(false);
  12127. Add('type');
  12128. Add(' TArray = array of longint;');
  12129. Add(' TObject = class');
  12130. Add(' FItems: TArray;');
  12131. Add(' function GetItems: tarray;');
  12132. Add(' procedure SetItems(Value: tarray);');
  12133. Add(' property Items: tarray read getitems write setitems;');
  12134. Add(' end;');
  12135. Add('function tobject.getitems: tarray;');
  12136. Add('begin');
  12137. Add(' Result:=fitems;');
  12138. Add('end;');
  12139. Add('procedure tobject.setitems(value: tarray);');
  12140. Add('begin');
  12141. Add(' fitems:=value;');
  12142. Add(' fitems:=nil;');
  12143. Add(' Items:=nil;');
  12144. Add(' Items:=Items;');
  12145. Add(' Items[1]:=2;');
  12146. Add(' fitems[3]:=Items[4];');
  12147. Add(' Items[5]:=Items[6];');
  12148. Add(' Self.Items[7]:=8;');
  12149. Add(' Self.Items[9]:=Self.Items[10];');
  12150. Add(' Items[Items[11]]:=Items[Items[12]];');
  12151. Add('end;');
  12152. Add('var Obj: tobject;');
  12153. Add('begin');
  12154. Add(' obj.items:=nil;');
  12155. Add(' obj.items:=obj.items;');
  12156. Add(' obj.items[11]:=obj.items[12];');
  12157. ConvertProgram;
  12158. CheckSource('TestClass_PropertyOfTypeArray',
  12159. LinesToStr([ // statements
  12160. 'rtl.createClass($mod, "TObject", null, function () {',
  12161. ' this.$init = function () {',
  12162. ' this.FItems = [];',
  12163. ' };',
  12164. ' this.$final = function () {',
  12165. ' this.FItems = undefined;',
  12166. ' };',
  12167. ' this.GetItems = function () {',
  12168. ' var Result = [];',
  12169. ' Result = this.FItems;',
  12170. ' return Result;',
  12171. ' };',
  12172. ' this.SetItems = function (Value) {',
  12173. ' this.FItems = Value;',
  12174. ' this.FItems = [];',
  12175. ' this.SetItems([]);',
  12176. ' this.SetItems(this.GetItems());',
  12177. ' this.GetItems()[1] = 2;',
  12178. ' this.FItems[3] = this.GetItems()[4];',
  12179. ' this.GetItems()[5] = this.GetItems()[6];',
  12180. ' this.GetItems()[7] = 8;',
  12181. ' this.GetItems()[9] = this.GetItems()[10];',
  12182. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12183. ' };',
  12184. '});',
  12185. 'this.Obj = null;'
  12186. ]),
  12187. LinesToStr([ // $mod.$main
  12188. '$mod.Obj.SetItems([]);',
  12189. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12190. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12191. ]));
  12192. end;
  12193. procedure TTestModule.TestClass_PropertyDefault;
  12194. begin
  12195. StartProgram(false);
  12196. Add([
  12197. 'type',
  12198. ' TArray = array of longint;',
  12199. ' TObject = class',
  12200. ' FItems: TArray;',
  12201. ' function GetItems(Index: longint): longint;',
  12202. ' procedure SetItems(Index, Value: longint);',
  12203. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12204. ' end;',
  12205. 'function tobject.getitems(index: longint): longint;',
  12206. 'begin',
  12207. 'end;',
  12208. 'procedure tobject.setitems(index, value: longint);',
  12209. 'begin',
  12210. ' Self[1]:=2;',
  12211. ' Self[3]:=Self[index];',
  12212. ' Self[index]:=Self[Self[value]];',
  12213. ' Self[Self[4]]:=value;',
  12214. 'end;',
  12215. 'var Obj: tobject;',
  12216. 'begin',
  12217. ' obj[11]:=12;',
  12218. ' obj[13]:=obj[14];',
  12219. ' obj[obj[15]]:=obj[obj[15]];',
  12220. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12221. ConvertProgram;
  12222. CheckSource('TestClass_PropertyDefault',
  12223. LinesToStr([ // statements
  12224. 'rtl.createClass($mod, "TObject", null, function () {',
  12225. ' this.$init = function () {',
  12226. ' this.FItems = [];',
  12227. ' };',
  12228. ' this.$final = function () {',
  12229. ' this.FItems = undefined;',
  12230. ' };',
  12231. ' this.GetItems = function (Index) {',
  12232. ' var Result = 0;',
  12233. ' return Result;',
  12234. ' };',
  12235. ' this.SetItems = function (Index, Value) {',
  12236. ' this.SetItems(1, 2);',
  12237. ' this.SetItems(3, this.GetItems(Index));',
  12238. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12239. ' this.SetItems(this.GetItems(4), Value);',
  12240. ' };',
  12241. '});',
  12242. 'this.Obj = null;'
  12243. ]),
  12244. LinesToStr([ // $mod.$main
  12245. '$mod.Obj.SetItems(11, 12);',
  12246. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12247. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12248. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12249. '']));
  12250. end;
  12251. procedure TTestModule.TestClass_PropertyDefault2;
  12252. begin
  12253. StartProgram(false);
  12254. Add([
  12255. 'type',
  12256. ' TObject = class end;',
  12257. ' TAlphaList = class',
  12258. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12259. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12260. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12261. ' end;',
  12262. ' TBetaList = class',
  12263. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12264. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12265. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12266. ' end;',
  12267. ' TBird = class',
  12268. ' procedure DoIt;',
  12269. ' end;',
  12270. 'procedure TBird.DoIt;',
  12271. 'var',
  12272. ' List: TAlphaList;',
  12273. 'begin',
  12274. ' if TBetaList(List[true])[3]=nil then ;',
  12275. ' TBetaList(List[false])[5]:=nil;',
  12276. 'end;',
  12277. 'var',
  12278. ' List: TAlphaList;',
  12279. 'begin',
  12280. ' if TBetaList(List[true])[3]=nil then ;',
  12281. ' TBetaList(List[false])[5]:=nil;',
  12282. '']);
  12283. ConvertProgram;
  12284. CheckSource('TestClass_PropertyDefault2',
  12285. LinesToStr([ // statements
  12286. 'rtl.createClass($mod, "TObject", null, function () {',
  12287. ' this.$init = function () {',
  12288. ' };',
  12289. ' this.$final = function () {',
  12290. ' };',
  12291. '});',
  12292. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12293. '});',
  12294. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12295. '});',
  12296. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12297. ' this.DoIt = function () {',
  12298. ' var List = null;',
  12299. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12300. ' List.GetAlphas(false).SetBetas(5, null);',
  12301. ' };',
  12302. '});',
  12303. 'this.List = null;',
  12304. '']),
  12305. LinesToStr([ // $mod.$main
  12306. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12307. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12308. '']));
  12309. end;
  12310. procedure TTestModule.TestClass_PropertyOverride;
  12311. begin
  12312. StartProgram(false);
  12313. Add('type');
  12314. Add(' integer = longint;');
  12315. Add(' TObject = class');
  12316. Add(' FItem: integer;');
  12317. Add(' function GetItem: integer; external name ''GetItem'';');
  12318. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12319. Add(' property Item: integer read getitem write setitem;');
  12320. Add(' end;');
  12321. Add(' TCar = class');
  12322. Add(' FBag: integer;');
  12323. Add(' function GetBag: integer; external name ''GetBag'';');
  12324. Add(' property Item read getbag;');
  12325. Add(' end;');
  12326. Add('var');
  12327. Add(' Obj: tobject;');
  12328. Add(' Car: tcar;');
  12329. Add('begin');
  12330. Add(' Obj.Item:=Obj.Item;');
  12331. Add(' Car.Item:=Car.Item;');
  12332. ConvertProgram;
  12333. CheckSource('TestClass_PropertyOverride',
  12334. LinesToStr([ // statements
  12335. 'rtl.createClass($mod, "TObject", null, function () {',
  12336. ' this.$init = function () {',
  12337. ' this.FItem = 0;',
  12338. ' };',
  12339. ' this.$final = function () {',
  12340. ' };',
  12341. '});',
  12342. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12343. ' this.$init = function () {',
  12344. ' $mod.TObject.$init.call(this);',
  12345. ' this.FBag = 0;',
  12346. ' };',
  12347. '});',
  12348. 'this.Obj = null;',
  12349. 'this.Car = null;',
  12350. '']),
  12351. LinesToStr([ // $mod.$main
  12352. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12353. '$mod.Car.SetItem($mod.Car.GetBag());',
  12354. '']));
  12355. end;
  12356. procedure TTestModule.TestClass_PropertyIncVisibility;
  12357. begin
  12358. AddModuleWithIntfImplSrc('unit1.pp',
  12359. LinesToStr([
  12360. 'type',
  12361. ' TNumber = longint;',
  12362. ' TInteger = longint;',
  12363. ' TObject = class',
  12364. ' private',
  12365. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12366. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12367. ' protected',
  12368. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12369. ' end;']),
  12370. LinesToStr([
  12371. '']));
  12372. StartProgram(true);
  12373. Add([
  12374. 'uses unit1;',
  12375. 'type',
  12376. ' TBird = class',
  12377. ' public',
  12378. ' property Items;',
  12379. ' end;',
  12380. 'procedure DoIt(i: TInteger);',
  12381. 'begin',
  12382. 'end;',
  12383. 'var b: TBird;',
  12384. 'begin',
  12385. ' b.Items[1]:=2;',
  12386. ' b.Items[3]:=b.Items[4];',
  12387. ' DoIt(b.Items[5]);',
  12388. '']);
  12389. ConvertProgram;
  12390. CheckSource('TestClass_PropertyIncVisibility',
  12391. LinesToStr([ // statements
  12392. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12393. '});',
  12394. 'this.DoIt = function (i) {',
  12395. '};',
  12396. 'this.b = null;'
  12397. ]),
  12398. LinesToStr([ // $mod.$main
  12399. '$mod.b.SetItems(1, 2);',
  12400. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12401. '$mod.DoIt($mod.b.GetItems(5));'
  12402. ]));
  12403. end;
  12404. procedure TTestModule.TestClass_Assigned;
  12405. begin
  12406. StartProgram(false);
  12407. Add('type');
  12408. Add(' TObject = class');
  12409. Add(' end;');
  12410. Add('var');
  12411. Add(' Obj: tobject;');
  12412. Add(' b: boolean;');
  12413. Add('begin');
  12414. Add(' if Assigned(obj) then ;');
  12415. Add(' b:=Assigned(obj) or false;');
  12416. ConvertProgram;
  12417. CheckSource('TestClass_Assigned',
  12418. LinesToStr([ // statements
  12419. 'rtl.createClass($mod, "TObject", null, function () {',
  12420. ' this.$init = function () {',
  12421. ' };',
  12422. ' this.$final = function () {',
  12423. ' };',
  12424. '});',
  12425. 'this.Obj = null;',
  12426. 'this.b = false;'
  12427. ]),
  12428. LinesToStr([ // $mod.$main
  12429. 'if ($mod.Obj != null);',
  12430. '$mod.b = ($mod.Obj != null) || false;'
  12431. ]));
  12432. end;
  12433. procedure TTestModule.TestClass_WithClassDoCreate;
  12434. begin
  12435. StartProgram(false);
  12436. Add('type');
  12437. Add(' TObject = class');
  12438. Add(' aBool: boolean;');
  12439. Add(' Arr: array of boolean;');
  12440. Add(' constructor Create;');
  12441. Add(' end;');
  12442. Add('constructor TObject.Create; begin end;');
  12443. Add('var');
  12444. Add(' Obj: tobject;');
  12445. Add(' b: boolean;');
  12446. Add('begin');
  12447. Add(' with tobject.create do begin');
  12448. Add(' b:=abool;');
  12449. Add(' abool:=b;');
  12450. Add(' b:=arr[1];');
  12451. Add(' arr[2]:=b;');
  12452. Add(' end;');
  12453. Add(' with tobject do');
  12454. Add(' obj:=create;');
  12455. Add(' with obj do begin');
  12456. Add(' create;');
  12457. Add(' b:=abool;');
  12458. Add(' abool:=b;');
  12459. Add(' b:=arr[3];');
  12460. Add(' arr[4]:=b;');
  12461. Add(' end;');
  12462. ConvertProgram;
  12463. CheckSource('TestClass_WithClassDoCreate',
  12464. LinesToStr([ // statements
  12465. 'rtl.createClass($mod, "TObject", null, function () {',
  12466. ' this.$init = function () {',
  12467. ' this.aBool = false;',
  12468. ' this.Arr = [];',
  12469. ' };',
  12470. ' this.$final = function () {',
  12471. ' this.Arr = undefined;',
  12472. ' };',
  12473. ' this.Create = function () {',
  12474. ' return this;',
  12475. ' };',
  12476. '});',
  12477. 'this.Obj = null;',
  12478. 'this.b = false;'
  12479. ]),
  12480. LinesToStr([ // $mod.$main
  12481. 'var $with1 = $mod.TObject.$create("Create");',
  12482. '$mod.b = $with1.aBool;',
  12483. '$with1.aBool = $mod.b;',
  12484. '$mod.b = $with1.Arr[1];',
  12485. '$with1.Arr[2] = $mod.b;',
  12486. 'var $with2 = $mod.TObject;',
  12487. '$mod.Obj = $with2.$create("Create");',
  12488. 'var $with3 = $mod.Obj;',
  12489. '$with3.Create();',
  12490. '$mod.b = $with3.aBool;',
  12491. '$with3.aBool = $mod.b;',
  12492. '$mod.b = $with3.Arr[3];',
  12493. '$with3.Arr[4] = $mod.b;',
  12494. '']));
  12495. end;
  12496. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12497. begin
  12498. StartProgram(false);
  12499. Add('type');
  12500. Add(' TObject = class');
  12501. Add(' FInt: longint;');
  12502. Add(' constructor Create;');
  12503. Add(' function GetSize: longint;');
  12504. Add(' procedure SetSize(Value: longint);');
  12505. Add(' property Int: longint read FInt write FInt;');
  12506. Add(' property Size: longint read GetSize write SetSize;');
  12507. Add(' end;');
  12508. Add('constructor TObject.Create; begin end;');
  12509. Add('function TObject.GetSize: longint; begin; end;');
  12510. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12511. Add('var');
  12512. Add(' Obj: tobject;');
  12513. Add(' i: longint;');
  12514. Add('begin');
  12515. Add(' with TObject.Create do begin');
  12516. Add(' i:=int;');
  12517. Add(' int:=i;');
  12518. Add(' i:=size;');
  12519. Add(' size:=i;');
  12520. Add(' end;');
  12521. Add(' with obj do begin');
  12522. Add(' i:=int;');
  12523. Add(' int:=i;');
  12524. Add(' i:=size;');
  12525. Add(' size:=i;');
  12526. Add(' end;');
  12527. ConvertProgram;
  12528. CheckSource('TestClass_WithClassInstDoProperty',
  12529. LinesToStr([ // statements
  12530. 'rtl.createClass($mod, "TObject", null, function () {',
  12531. ' this.$init = function () {',
  12532. ' this.FInt = 0;',
  12533. ' };',
  12534. ' this.$final = function () {',
  12535. ' };',
  12536. ' this.Create = function () {',
  12537. ' return this;',
  12538. ' };',
  12539. ' this.GetSize = function () {',
  12540. ' var Result = 0;',
  12541. ' return Result;',
  12542. ' };',
  12543. ' this.SetSize = function (Value) {',
  12544. ' };',
  12545. '});',
  12546. 'this.Obj = null;',
  12547. 'this.i = 0;'
  12548. ]),
  12549. LinesToStr([ // $mod.$main
  12550. 'var $with1 = $mod.TObject.$create("Create");',
  12551. '$mod.i = $with1.FInt;',
  12552. '$with1.FInt = $mod.i;',
  12553. '$mod.i = $with1.GetSize();',
  12554. '$with1.SetSize($mod.i);',
  12555. 'var $with2 = $mod.Obj;',
  12556. '$mod.i = $with2.FInt;',
  12557. '$with2.FInt = $mod.i;',
  12558. '$mod.i = $with2.GetSize();',
  12559. '$with2.SetSize($mod.i);',
  12560. '']));
  12561. end;
  12562. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12563. begin
  12564. StartProgram(false);
  12565. Add('type');
  12566. Add(' TObject = class');
  12567. Add(' constructor Create;');
  12568. Add(' function GetItems(Index: longint): longint;');
  12569. Add(' procedure SetItems(Index, Value: longint);');
  12570. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12571. Add(' end;');
  12572. Add('constructor TObject.Create; begin end;');
  12573. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12574. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12575. Add('var');
  12576. Add(' Obj: tobject;');
  12577. Add(' i: longint;');
  12578. Add('begin');
  12579. Add(' with TObject.Create do begin');
  12580. Add(' i:=Items[1];');
  12581. Add(' Items[2]:=i;');
  12582. Add(' end;');
  12583. Add(' with obj do begin');
  12584. Add(' i:=Items[3];');
  12585. Add(' Items[4]:=i;');
  12586. Add(' end;');
  12587. ConvertProgram;
  12588. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12589. LinesToStr([ // statements
  12590. 'rtl.createClass($mod, "TObject", null, function () {',
  12591. ' this.$init = function () {',
  12592. ' };',
  12593. ' this.$final = function () {',
  12594. ' };',
  12595. ' this.Create = function () {',
  12596. ' return this;',
  12597. ' };',
  12598. ' this.GetItems = function (Index) {',
  12599. ' var Result = 0;',
  12600. ' return Result;',
  12601. ' };',
  12602. ' this.SetItems = function (Index, Value) {',
  12603. ' };',
  12604. '});',
  12605. 'this.Obj = null;',
  12606. 'this.i = 0;'
  12607. ]),
  12608. LinesToStr([ // $mod.$main
  12609. 'var $with1 = $mod.TObject.$create("Create");',
  12610. '$mod.i = $with1.GetItems(1);',
  12611. '$with1.SetItems(2, $mod.i);',
  12612. 'var $with2 = $mod.Obj;',
  12613. '$mod.i = $with2.GetItems(3);',
  12614. '$with2.SetItems(4, $mod.i);',
  12615. '']));
  12616. end;
  12617. procedure TTestModule.TestClass_WithClassInstDoFunc;
  12618. begin
  12619. StartProgram(false);
  12620. Add('type');
  12621. Add(' TObject = class');
  12622. Add(' constructor Create;');
  12623. Add(' function GetSize: longint;');
  12624. Add(' procedure SetSize(Value: longint);');
  12625. Add(' end;');
  12626. Add('constructor TObject.Create; begin end;');
  12627. Add('function TObject.GetSize: longint; begin; end;');
  12628. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12629. Add('var');
  12630. Add(' Obj: tobject;');
  12631. Add(' i: longint;');
  12632. Add('begin');
  12633. Add(' with TObject.Create do begin');
  12634. Add(' i:=GetSize;');
  12635. Add(' i:=GetSize();');
  12636. Add(' SetSize(i);');
  12637. Add(' end;');
  12638. Add(' with obj do begin');
  12639. Add(' i:=GetSize;');
  12640. Add(' i:=GetSize();');
  12641. Add(' SetSize(i);');
  12642. Add(' end;');
  12643. ConvertProgram;
  12644. CheckSource('TestClass_WithClassInstDoFunc',
  12645. LinesToStr([ // statements
  12646. 'rtl.createClass($mod, "TObject", null, function () {',
  12647. ' this.$init = function () {',
  12648. ' };',
  12649. ' this.$final = function () {',
  12650. ' };',
  12651. ' this.Create = function () {',
  12652. ' return this;',
  12653. ' };',
  12654. ' this.GetSize = function () {',
  12655. ' var Result = 0;',
  12656. ' return Result;',
  12657. ' };',
  12658. ' this.SetSize = function (Value) {',
  12659. ' };',
  12660. '});',
  12661. 'this.Obj = null;',
  12662. 'this.i = 0;'
  12663. ]),
  12664. LinesToStr([ // $mod.$main
  12665. 'var $with1 = $mod.TObject.$create("Create");',
  12666. '$mod.i = $with1.GetSize();',
  12667. '$mod.i = $with1.GetSize();',
  12668. '$with1.SetSize($mod.i);',
  12669. 'var $with2 = $mod.Obj;',
  12670. '$mod.i = $with2.GetSize();',
  12671. '$mod.i = $with2.GetSize();',
  12672. '$with2.SetSize($mod.i);',
  12673. '']));
  12674. end;
  12675. procedure TTestModule.TestClass_TypeCast;
  12676. begin
  12677. StartProgram(false);
  12678. Add('type');
  12679. Add(' TObject = class');
  12680. Add(' Next: TObject;');
  12681. Add(' constructor Create;');
  12682. Add(' end;');
  12683. Add(' TControl = class(TObject)');
  12684. Add(' Arr: array of TObject;');
  12685. Add(' function GetIt(vI: longint = 0): TObject;');
  12686. Add(' end;');
  12687. Add('constructor tobject.create; begin end;');
  12688. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  12689. Add('var');
  12690. Add(' Obj: tobject;');
  12691. Add('begin');
  12692. Add(' obj:=tcontrol(obj).next;');
  12693. Add(' tcontrol(obj):=nil;');
  12694. Add(' obj:=tcontrol(obj);');
  12695. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  12696. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  12697. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  12698. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  12699. ConvertProgram;
  12700. CheckSource('TestClass_TypeCast',
  12701. LinesToStr([ // statements
  12702. 'rtl.createClass($mod, "TObject", null, function () {',
  12703. ' this.$init = function () {',
  12704. ' this.Next = null;',
  12705. ' };',
  12706. ' this.$final = function () {',
  12707. ' this.Next = undefined;',
  12708. ' };',
  12709. ' this.Create = function () {',
  12710. ' return this;',
  12711. ' };',
  12712. '});',
  12713. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  12714. ' this.$init = function () {',
  12715. ' $mod.TObject.$init.call(this);',
  12716. ' this.Arr = [];',
  12717. ' };',
  12718. ' this.$final = function () {',
  12719. ' this.Arr = undefined;',
  12720. ' $mod.TObject.$final.call(this);',
  12721. ' };',
  12722. ' this.GetIt = function (vI) {',
  12723. ' var Result = null;',
  12724. ' return Result;',
  12725. ' };',
  12726. '});',
  12727. 'this.Obj = null;'
  12728. ]),
  12729. LinesToStr([ // $mod.$main
  12730. '$mod.Obj = $mod.Obj.Next;',
  12731. '$mod.Obj = null;',
  12732. '$mod.Obj = $mod.Obj;',
  12733. '$mod.Obj = $mod.Obj.GetIt(0);',
  12734. '$mod.Obj = $mod.Obj.GetIt(0);',
  12735. '$mod.Obj = $mod.Obj.GetIt(1);',
  12736. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  12737. '']));
  12738. end;
  12739. procedure TTestModule.TestClass_TypeCastUntypedParam;
  12740. begin
  12741. StartProgram(false);
  12742. Add('type');
  12743. Add(' TObject = class end;');
  12744. Add('procedure ProcA(var A);');
  12745. Add('begin');
  12746. Add(' TObject(A):=nil;');
  12747. Add(' TObject(A):=TObject(A);');
  12748. Add(' if TObject(A)=nil then ;');
  12749. Add(' if nil=TObject(A) then ;');
  12750. Add('end;');
  12751. Add('procedure ProcB(out A);');
  12752. Add('begin');
  12753. Add(' TObject(A):=nil;');
  12754. Add(' TObject(A):=TObject(A);');
  12755. Add(' if TObject(A)=nil then ;');
  12756. Add(' if nil=TObject(A) then ;');
  12757. Add('end;');
  12758. Add('procedure ProcC(const A);');
  12759. Add('begin');
  12760. Add(' if TObject(A)=nil then ;');
  12761. Add(' if nil=TObject(A) then ;');
  12762. Add('end;');
  12763. Add('var o: TObject;');
  12764. Add('begin');
  12765. Add(' ProcA(o);');
  12766. Add(' ProcB(o);');
  12767. Add(' ProcC(o);');
  12768. ConvertProgram;
  12769. CheckSource('TestClass_TypeCastUntypedParam',
  12770. LinesToStr([ // statements
  12771. 'rtl.createClass($mod, "TObject", null, function () {',
  12772. ' this.$init = function () {',
  12773. ' };',
  12774. ' this.$final = function () {',
  12775. ' };',
  12776. '});',
  12777. 'this.ProcA = function (A) {',
  12778. ' A.set(null);',
  12779. ' A.set(A.get());',
  12780. ' if (A.get() === null);',
  12781. ' if (null === A.get());',
  12782. '};',
  12783. 'this.ProcB = function (A) {',
  12784. ' A.set(null);',
  12785. ' A.set(A.get());',
  12786. ' if (A.get() === null);',
  12787. ' if (null === A.get());',
  12788. '};',
  12789. 'this.ProcC = function (A) {',
  12790. ' if (A === null);',
  12791. ' if (null === A);',
  12792. '};',
  12793. 'this.o = null;',
  12794. '']),
  12795. LinesToStr([ // $mod.$main
  12796. '$mod.ProcA({',
  12797. ' p: $mod,',
  12798. ' get: function () {',
  12799. ' return this.p.o;',
  12800. ' },',
  12801. ' set: function (v) {',
  12802. ' this.p.o = v;',
  12803. ' }',
  12804. '});',
  12805. '$mod.ProcB({',
  12806. ' p: $mod,',
  12807. ' get: function () {',
  12808. ' return this.p.o;',
  12809. ' },',
  12810. ' set: function (v) {',
  12811. ' this.p.o = v;',
  12812. ' }',
  12813. '});',
  12814. '$mod.ProcC($mod.o);',
  12815. '']));
  12816. end;
  12817. procedure TTestModule.TestClass_Overloads;
  12818. begin
  12819. StartProgram(false);
  12820. Add('type');
  12821. Add(' TObject = class');
  12822. Add(' procedure DoIt;');
  12823. Add(' procedure DoIt(vI: longint);');
  12824. Add(' end;');
  12825. Add('procedure TObject.DoIt;');
  12826. Add('begin');
  12827. Add(' DoIt;');
  12828. Add(' DoIt(1);');
  12829. Add('end;');
  12830. Add('procedure TObject.DoIt(vI: longint); begin end;');
  12831. Add('begin');
  12832. ConvertProgram;
  12833. CheckSource('TestClass_Overloads',
  12834. LinesToStr([ // statements
  12835. 'rtl.createClass($mod, "TObject", null, function () {',
  12836. ' this.$init = function () {',
  12837. ' };',
  12838. ' this.$final = function () {',
  12839. ' };',
  12840. ' this.DoIt = function () {',
  12841. ' this.DoIt();',
  12842. ' this.DoIt$1(1);',
  12843. ' };',
  12844. ' this.DoIt$1 = function (vI) {',
  12845. ' };',
  12846. '});',
  12847. '']),
  12848. LinesToStr([ // $mod.$main
  12849. '']));
  12850. end;
  12851. procedure TTestModule.TestClass_OverloadsAncestor;
  12852. begin
  12853. StartProgram(false);
  12854. Add('type');
  12855. Add(' TObject = class;');
  12856. Add(' TObject = class');
  12857. Add(' procedure DoIt(vA: longint);');
  12858. Add(' procedure DoIt(vA, vB: longint);');
  12859. Add(' end;');
  12860. Add(' TCar = class;');
  12861. Add(' TCar = class');
  12862. Add(' procedure DoIt(vA: longint);');
  12863. Add(' procedure DoIt(vA, vB: longint);');
  12864. Add(' end;');
  12865. Add('procedure tobject.doit(va: longint);');
  12866. Add('begin');
  12867. Add(' doit(1);');
  12868. Add(' doit(1,2);');
  12869. Add('end;');
  12870. Add('procedure tobject.doit(va, vb: longint); begin end;');
  12871. Add('procedure tcar.doit(va: longint);');
  12872. Add('begin');
  12873. Add(' doit(1);');
  12874. Add(' doit(1,2);');
  12875. Add(' inherited doit(1);');
  12876. Add(' inherited doit(1,2);');
  12877. Add('end;');
  12878. Add('procedure tcar.doit(va, vb: longint); begin end;');
  12879. Add('begin');
  12880. ConvertProgram;
  12881. CheckSource('TestClass_OverloadsAncestor',
  12882. LinesToStr([ // statements
  12883. 'rtl.createClass($mod, "TObject", null, function () {',
  12884. ' this.$init = function () {',
  12885. ' };',
  12886. ' this.$final = function () {',
  12887. ' };',
  12888. ' this.DoIt = function (vA) {',
  12889. ' this.DoIt(1);',
  12890. ' this.DoIt$1(1,2);',
  12891. ' };',
  12892. ' this.DoIt$1 = function (vA, vB) {',
  12893. ' };',
  12894. '});',
  12895. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12896. ' this.DoIt$2 = function (vA) {',
  12897. ' this.DoIt$2(1);',
  12898. ' this.DoIt$3(1, 2);',
  12899. ' $mod.TObject.DoIt.call(this, 1);',
  12900. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  12901. ' };',
  12902. ' this.DoIt$3 = function (vA, vB) {',
  12903. ' };',
  12904. '});',
  12905. '']),
  12906. LinesToStr([ // $mod.$main
  12907. '']));
  12908. end;
  12909. procedure TTestModule.TestClass_OverloadConstructor;
  12910. begin
  12911. StartProgram(false);
  12912. Add('type');
  12913. Add(' TObject = class');
  12914. Add(' constructor Create(vA: longint);');
  12915. Add(' constructor Create(vA, vB: longint);');
  12916. Add(' end;');
  12917. Add(' TCar = class');
  12918. Add(' constructor Create(vA: longint);');
  12919. Add(' constructor Create(vA, vB: longint);');
  12920. Add(' end;');
  12921. Add('constructor tobject.create(va: longint);');
  12922. Add('begin');
  12923. Add(' create(1);');
  12924. Add(' create(1,2);');
  12925. Add('end;');
  12926. Add('constructor tobject.create(va, vb: longint); begin end;');
  12927. Add('constructor tcar.create(va: longint);');
  12928. Add('begin');
  12929. Add(' create(1);');
  12930. Add(' create(1,2);');
  12931. Add(' inherited create(1);');
  12932. Add(' inherited create(1,2);');
  12933. Add('end;');
  12934. Add('constructor tcar.create(va, vb: longint); begin end;');
  12935. Add('begin');
  12936. Add(' tobject.create(1);');
  12937. Add(' tobject.create(1,2);');
  12938. Add(' tcar.create(1);');
  12939. Add(' tcar.create(1,2);');
  12940. ConvertProgram;
  12941. CheckSource('TestClass_OverloadConstructor',
  12942. LinesToStr([ // statements
  12943. 'rtl.createClass($mod, "TObject", null, function () {',
  12944. ' this.$init = function () {',
  12945. ' };',
  12946. ' this.$final = function () {',
  12947. ' };',
  12948. ' this.Create = function (vA) {',
  12949. ' this.Create(1);',
  12950. ' this.Create$1(1,2);',
  12951. ' return this;',
  12952. ' };',
  12953. ' this.Create$1 = function (vA, vB) {',
  12954. ' return this;',
  12955. ' };',
  12956. '});',
  12957. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12958. ' this.Create$2 = function (vA) {',
  12959. ' this.Create$2(1);',
  12960. ' this.Create$3(1, 2);',
  12961. ' $mod.TObject.Create.call(this, 1);',
  12962. ' $mod.TObject.Create$1.call(this, 1, 2);',
  12963. ' return this;',
  12964. ' };',
  12965. ' this.Create$3 = function (vA, vB) {',
  12966. ' return this;',
  12967. ' };',
  12968. '});',
  12969. '']),
  12970. LinesToStr([ // $mod.$main
  12971. '$mod.TObject.$create("Create", [1]);',
  12972. '$mod.TObject.$create("Create$1", [1, 2]);',
  12973. '$mod.TCar.$create("Create$2", [1]);',
  12974. '$mod.TCar.$create("Create$3", [1, 2]);',
  12975. '']));
  12976. end;
  12977. procedure TTestModule.TestClass_OverloadDelphiOverride;
  12978. begin
  12979. StartProgram(false);
  12980. Add([
  12981. '{$mode delphi}',
  12982. 'type',
  12983. ' TObject = class end;',
  12984. ' TBird = class',
  12985. ' function {#a}GetValue: longint; overload; virtual;',
  12986. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  12987. ' end;',
  12988. ' TEagle = class(TBird)',
  12989. ' function {#c}GetValue: longint; overload; override;',
  12990. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  12991. ' end;',
  12992. 'function TBird.GetValue: longint;',
  12993. 'begin',
  12994. ' if 3={@a}GetValue then ;',
  12995. ' if 4={@b}GetValue(5) then ;',
  12996. 'end;',
  12997. 'function TBird.GetValue(AValue: longint): longint;',
  12998. 'begin',
  12999. 'end;',
  13000. 'function TEagle.GetValue: longint;',
  13001. 'begin',
  13002. ' if 13={@c}GetValue then ;',
  13003. ' if 14={@d}GetValue(15) then ;',
  13004. ' if 15=inherited {@a}GetValue then ;',
  13005. ' if 16=inherited {@b}GetValue(17) then ;',
  13006. 'end;',
  13007. 'function TEagle.GetValue(AValue: longint): longint;',
  13008. 'begin',
  13009. 'end;',
  13010. 'var',
  13011. ' e: TEagle;',
  13012. 'begin',
  13013. ' if 23=e.{@c}GetValue then ;',
  13014. ' if 24=e.{@d}GetValue(25) then ;']);
  13015. ConvertProgram;
  13016. CheckSource('TestClass_OverloadDelphiOverride',
  13017. LinesToStr([ // statements
  13018. 'rtl.createClass($mod, "TObject", null, function () {',
  13019. ' this.$init = function () {',
  13020. ' };',
  13021. ' this.$final = function () {',
  13022. ' };',
  13023. '});',
  13024. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13025. ' this.GetValue = function () {',
  13026. ' var Result = 0;',
  13027. ' if (3 === this.GetValue()) ;',
  13028. ' if (4 === this.GetValue$1(5)) ;',
  13029. ' return Result;',
  13030. ' };',
  13031. ' this.GetValue$1 = function (AValue) {',
  13032. ' var Result = 0;',
  13033. ' return Result;',
  13034. ' };',
  13035. '});',
  13036. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13037. ' this.GetValue = function () {',
  13038. ' var Result = 0;',
  13039. ' if (13 === this.GetValue()) ;',
  13040. ' if (14 === this.GetValue$1(15)) ;',
  13041. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13042. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13043. ' return Result;',
  13044. ' };',
  13045. ' this.GetValue$1 = function (AValue) {',
  13046. ' var Result = 0;',
  13047. ' return Result;',
  13048. ' };',
  13049. '});',
  13050. 'this.e = null;',
  13051. '']),
  13052. LinesToStr([ // $mod.$main
  13053. 'if (23 === $mod.e.GetValue()) ;',
  13054. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13055. '']));
  13056. end;
  13057. procedure TTestModule.TestClass_ReintroducedVar;
  13058. begin
  13059. StartProgram(false);
  13060. Add('type');
  13061. Add(' TObject = class');
  13062. Add(' strict private');
  13063. Add(' Some: longint;');
  13064. Add(' end;');
  13065. Add(' TMobile = class');
  13066. Add(' strict private');
  13067. Add(' Some: string;');
  13068. Add(' end;');
  13069. Add(' TCar = class(tmobile)');
  13070. Add(' procedure Some;');
  13071. Add(' procedure Some(vA: longint);');
  13072. Add(' end;');
  13073. Add('procedure tcar.some;');
  13074. Add('begin');
  13075. Add(' Some;');
  13076. Add(' Some(1);');
  13077. Add('end;');
  13078. Add('procedure tcar.some(va: longint); begin end;');
  13079. Add('begin');
  13080. ConvertProgram;
  13081. CheckSource('TestClass_ReintroducedVar',
  13082. LinesToStr([ // statements
  13083. 'rtl.createClass($mod, "TObject", null, function () {',
  13084. ' this.$init = function () {',
  13085. ' this.Some = 0;',
  13086. ' };',
  13087. ' this.$final = function () {',
  13088. ' };',
  13089. '});',
  13090. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13091. ' this.$init = function () {',
  13092. ' $mod.TObject.$init.call(this);',
  13093. ' this.Some$1 = "";',
  13094. ' };',
  13095. '});',
  13096. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13097. ' this.Some$2 = function () {',
  13098. ' this.Some$2();',
  13099. ' this.Some$3(1);',
  13100. ' };',
  13101. ' this.Some$3 = function (vA) {',
  13102. ' };',
  13103. '});',
  13104. '']),
  13105. LinesToStr([ // $mod.$main
  13106. '']));
  13107. end;
  13108. procedure TTestModule.TestClass_RaiseDescendant;
  13109. begin
  13110. StartProgram(false);
  13111. Add([
  13112. 'type',
  13113. ' TObject = class',
  13114. ' constructor Create(Msg: string);',
  13115. ' end;',
  13116. ' Exception = class',
  13117. ' end;',
  13118. ' EConvertError = class(Exception)',
  13119. ' end;',
  13120. 'constructor TObject.Create(Msg: string); begin end;',
  13121. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13122. 'begin',
  13123. ' raise Exception.Create(''Bar1'');',
  13124. ' raise EConvertError.Create(''Bar2'');',
  13125. ' raise AssertConv(''Bar2'');',
  13126. ' raise AssertConv;',
  13127. '']);
  13128. ConvertProgram;
  13129. CheckSource('TestClass_RaiseDescendant',
  13130. LinesToStr([ // statements
  13131. 'rtl.createClass($mod, "TObject", null, function () {',
  13132. ' this.$init = function () {',
  13133. ' };',
  13134. ' this.$final = function () {',
  13135. ' };',
  13136. ' this.Create = function (Msg) {',
  13137. ' return this;',
  13138. ' };',
  13139. '});',
  13140. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13141. '});',
  13142. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13143. '});',
  13144. 'this.AssertConv = function (Msg) {',
  13145. ' var Result = null;',
  13146. ' return Result;',
  13147. '};',
  13148. '']),
  13149. LinesToStr([ // $mod.$main
  13150. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13151. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13152. 'throw $mod.AssertConv("Bar2");',
  13153. 'throw $mod.AssertConv("def");',
  13154. '']));
  13155. end;
  13156. procedure TTestModule.TestClass_ExternalMethod;
  13157. begin
  13158. AddModuleWithIntfImplSrc('unit2.pas',
  13159. LinesToStr([
  13160. 'type',
  13161. ' TObject = class',
  13162. ' public',
  13163. ' procedure Intern; external name ''$DoIntern'';',
  13164. ' end;',
  13165. '']),
  13166. LinesToStr([
  13167. '']));
  13168. StartUnit(true);
  13169. Add('interface');
  13170. Add('uses unit2;');
  13171. Add('type');
  13172. Add(' TCar = class(TObject)');
  13173. Add(' public');
  13174. Add(' procedure Intern2; external name ''$DoIntern2'';');
  13175. Add(' procedure DoIt;');
  13176. Add(' end;');
  13177. Add('implementation');
  13178. Add('procedure tcar.doit;');
  13179. Add('begin');
  13180. Add(' Intern;');
  13181. Add(' Intern();');
  13182. Add(' Intern2;');
  13183. Add(' Intern2();');
  13184. Add('end;');
  13185. Add('var Obj: TCar;');
  13186. Add('begin');
  13187. Add(' obj.intern;');
  13188. Add(' obj.intern();');
  13189. Add(' obj.intern2;');
  13190. Add(' obj.intern2();');
  13191. Add(' obj.doit;');
  13192. Add(' obj.doit();');
  13193. Add(' with obj do begin');
  13194. Add(' Intern;');
  13195. Add(' Intern();');
  13196. Add(' Intern2;');
  13197. Add(' Intern2();');
  13198. Add(' end;');
  13199. ConvertUnit;
  13200. CheckSource('TestClass_ExternalMethod',
  13201. LinesToStr([
  13202. 'var $impl = $mod.$impl;',
  13203. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13204. ' this.DoIt = function () {',
  13205. ' this.$DoIntern();',
  13206. ' this.$DoIntern();',
  13207. ' this.$DoIntern2();',
  13208. ' this.$DoIntern2();',
  13209. ' };',
  13210. ' });',
  13211. '']),
  13212. LinesToStr([ // this.$init
  13213. '$impl.Obj.$DoIntern();',
  13214. '$impl.Obj.$DoIntern();',
  13215. '$impl.Obj.$DoIntern2();',
  13216. '$impl.Obj.$DoIntern2();',
  13217. '$impl.Obj.DoIt();',
  13218. '$impl.Obj.DoIt();',
  13219. 'var $with1 = $impl.Obj;',
  13220. '$with1.$DoIntern();',
  13221. '$with1.$DoIntern();',
  13222. '$with1.$DoIntern2();',
  13223. '$with1.$DoIntern2();',
  13224. '']),
  13225. LinesToStr([ // implementation
  13226. '$impl.Obj = null;',
  13227. '']) );
  13228. end;
  13229. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  13230. begin
  13231. StartProgram(false);
  13232. Add('type');
  13233. Add(' TObject = class');
  13234. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13235. Add(' end;');
  13236. Add('begin');
  13237. SetExpectedPasResolverError('Virtual method name must match external',
  13238. nVirtualMethodNameMustMatchExternal);
  13239. ConvertProgram;
  13240. end;
  13241. procedure TTestModule.TestClass_ExternalOverrideFail;
  13242. begin
  13243. StartProgram(false);
  13244. Add('type');
  13245. Add(' TObject = class');
  13246. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13247. Add(' end;');
  13248. Add(' TCar = class');
  13249. Add(' procedure DoIt; override; external name ''DoIt'';');
  13250. Add(' end;');
  13251. Add('begin');
  13252. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13253. nInvalidXModifierY);
  13254. ConvertProgram;
  13255. end;
  13256. procedure TTestModule.TestClass_ExternalVar;
  13257. begin
  13258. AddModuleWithIntfImplSrc('unit2.pas',
  13259. LinesToStr([
  13260. '{$modeswitch externalclass}',
  13261. 'type',
  13262. ' TObject = class',
  13263. ' public',
  13264. ' Intern: longint external name ''$Intern'';',
  13265. ' Bracket: longint external name ''["A B"]'';',
  13266. ' end;',
  13267. '']),
  13268. LinesToStr([
  13269. '']));
  13270. StartUnit(true);
  13271. Add([
  13272. 'interface',
  13273. 'uses unit2;',
  13274. '{$modeswitch externalclass}',
  13275. 'type',
  13276. ' TCar = class(tobject)',
  13277. ' public',
  13278. ' Intern2: longint external name ''$Intern2'';',
  13279. ' procedure DoIt;',
  13280. ' end;',
  13281. 'implementation',
  13282. 'procedure tcar.doit;',
  13283. 'begin',
  13284. ' Intern:=Intern+1;',
  13285. ' Intern2:=Intern2+2;',
  13286. ' Bracket:=Bracket+3;',
  13287. 'end;',
  13288. 'var Obj: TCar;',
  13289. 'begin',
  13290. ' obj.intern:=obj.intern+1;',
  13291. ' obj.intern2:=obj.intern2+2;',
  13292. ' obj.Bracket:=obj.Bracket+3;',
  13293. ' with obj do begin',
  13294. ' intern:=intern+1;',
  13295. ' intern2:=intern2+2;',
  13296. ' Bracket:=Bracket+3;',
  13297. ' end;']);
  13298. ConvertUnit;
  13299. CheckSource('TestClass_ExternalVar',
  13300. LinesToStr([
  13301. 'var $impl = $mod.$impl;',
  13302. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13303. ' this.DoIt = function () {',
  13304. ' this.$Intern = this.$Intern + 1;',
  13305. ' this.$Intern2 = this.$Intern2 + 2;',
  13306. ' this["A B"] = this["A B"] + 3;',
  13307. ' };',
  13308. ' });',
  13309. '']),
  13310. LinesToStr([
  13311. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13312. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13313. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13314. 'var $with1 = $impl.Obj;',
  13315. '$with1.$Intern = $with1.$Intern + 1;',
  13316. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13317. '$with1["A B"] = $with1["A B"] + 3;',
  13318. '']),
  13319. LinesToStr([ // implementation
  13320. '$impl.Obj = null;',
  13321. '']));
  13322. end;
  13323. procedure TTestModule.TestClass_Const;
  13324. begin
  13325. StartProgram(false);
  13326. Add('type');
  13327. Add(' integer = longint;');
  13328. Add(' TClass = class of TObject;');
  13329. Add(' TObject = class');
  13330. Add(' public');
  13331. Add(' const cI: integer = 3;');
  13332. Add(' procedure DoIt;');
  13333. Add(' class procedure DoMore;');
  13334. Add(' end;');
  13335. Add('implementation');
  13336. Add('procedure tobject.doit;');
  13337. Add('begin');
  13338. Add(' if cI=4 then;');
  13339. Add(' if 5=cI then;');
  13340. Add(' if Self.cI=6 then;');
  13341. Add(' if 7=Self.cI then;');
  13342. Add(' with Self do begin');
  13343. Add(' if cI=11 then;');
  13344. Add(' if 12=cI then;');
  13345. Add(' end;');
  13346. Add('end;');
  13347. Add('class procedure tobject.domore;');
  13348. Add('begin');
  13349. Add(' if cI=8 then;');
  13350. Add(' if Self.cI=9 then;');
  13351. Add(' if 10=cI then;');
  13352. Add(' if 11=Self.cI then;');
  13353. Add(' with Self do begin');
  13354. Add(' if cI=13 then;');
  13355. Add(' if 14=cI then;');
  13356. Add(' end;');
  13357. Add('end;');
  13358. Add('var');
  13359. Add(' Obj: TObject;');
  13360. Add(' Cla: TClass;');
  13361. Add('begin');
  13362. Add(' if TObject.cI=21 then ;');
  13363. Add(' if Obj.cI=22 then ;');
  13364. Add(' if Cla.cI=23 then ;');
  13365. Add(' with obj do if ci=24 then;');
  13366. Add(' with TObject do if ci=25 then;');
  13367. Add(' with Cla do if ci=26 then;');
  13368. ConvertProgram;
  13369. CheckSource('TestClass_Const',
  13370. LinesToStr([
  13371. 'rtl.createClass($mod, "TObject", null, function () {',
  13372. ' this.cI = 3;',
  13373. ' this.$init = function () {',
  13374. ' };',
  13375. ' this.$final = function () {',
  13376. ' };',
  13377. ' this.DoIt = function () {',
  13378. ' if (this.cI === 4) ;',
  13379. ' if (5 === this.cI) ;',
  13380. ' if (this.cI === 6) ;',
  13381. ' if (7 === this.cI) ;',
  13382. ' if (this.cI === 11) ;',
  13383. ' if (12 === this.cI) ;',
  13384. ' };',
  13385. ' this.DoMore = function () {',
  13386. ' if (this.cI === 8) ;',
  13387. ' if (this.cI === 9) ;',
  13388. ' if (10 === this.cI) ;',
  13389. ' if (11 === this.cI) ;',
  13390. ' if (this.cI === 13) ;',
  13391. ' if (14 === this.cI) ;',
  13392. ' };',
  13393. '});',
  13394. 'this.Obj = null;',
  13395. 'this.Cla = null;',
  13396. '']),
  13397. LinesToStr([
  13398. 'if ($mod.TObject.cI === 21) ;',
  13399. 'if ($mod.Obj.cI === 22) ;',
  13400. 'if ($mod.Cla.cI === 23) ;',
  13401. 'var $with1 = $mod.Obj;',
  13402. 'if ($with1.cI === 24) ;',
  13403. 'var $with2 = $mod.TObject;',
  13404. 'if ($with2.cI === 25) ;',
  13405. 'var $with3 = $mod.Cla;',
  13406. 'if ($with3.cI === 26) ;',
  13407. '']));
  13408. end;
  13409. procedure TTestModule.TestClass_LocalVarSelfFail;
  13410. begin
  13411. StartProgram(false);
  13412. Add([
  13413. 'type',
  13414. ' TObject = class',
  13415. ' constructor Create;',
  13416. ' end;',
  13417. 'constructor tobject.create;',
  13418. 'var self: longint;',
  13419. 'begin',
  13420. 'end',
  13421. 'begin',
  13422. '']);
  13423. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  13424. ConvertProgram;
  13425. end;
  13426. procedure TTestModule.TestClass_ArgSelfFail;
  13427. begin
  13428. StartProgram(false);
  13429. Add([
  13430. 'type',
  13431. ' TObject = class',
  13432. ' procedure DoIt(Self: longint);',
  13433. ' end;',
  13434. 'procedure tobject.doit(self: longint);',
  13435. 'begin',
  13436. 'end',
  13437. 'begin',
  13438. '']);
  13439. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  13440. ConvertProgram;
  13441. end;
  13442. procedure TTestModule.TestClass_NestedProcSelf;
  13443. begin
  13444. StartProgram(false);
  13445. Add([
  13446. 'type',
  13447. ' TObject = class',
  13448. ' Key: longint;',
  13449. ' class var State: longint;',
  13450. ' procedure DoIt;',
  13451. ' function GetSize: longint; virtual; abstract;',
  13452. ' procedure SetSize(Value: longint); virtual; abstract;',
  13453. ' property Size: longint read GetSize write SetSize;',
  13454. ' end;',
  13455. 'procedure tobject.doit;',
  13456. ' procedure Sub;',
  13457. ' begin',
  13458. ' key:=key+2;',
  13459. ' self.key:=self.key+3;',
  13460. ' state:=state+4;',
  13461. ' self.state:=self.state+5;',
  13462. ' tobject.state:=tobject.state+6;',
  13463. ' size:=size+7;',
  13464. ' self.size:=self.size+8;',
  13465. ' end;',
  13466. 'begin',
  13467. ' sub;',
  13468. ' key:=key+12;',
  13469. ' self.key:=self.key+13;',
  13470. ' state:=state+14;',
  13471. ' self.state:=self.state+15;',
  13472. ' tobject.state:=tobject.state+16;',
  13473. ' size:=size+17;',
  13474. ' self.size:=self.size+18;',
  13475. 'end;',
  13476. 'begin',
  13477. '']);
  13478. ConvertProgram;
  13479. CheckSource('TestClass_NestedProcSelf',
  13480. LinesToStr([ // statements
  13481. 'rtl.createClass($mod, "TObject", null, function () {',
  13482. ' this.State = 0;',
  13483. ' this.$init = function () {',
  13484. ' this.Key = 0;',
  13485. ' };',
  13486. ' this.$final = function () {',
  13487. ' };',
  13488. ' this.DoIt = function () {',
  13489. ' var $Self = this;',
  13490. ' function Sub() {',
  13491. ' $Self.Key = $Self.Key + 2;',
  13492. ' $Self.Key = $Self.Key + 3;',
  13493. ' $mod.TObject.State = $Self.State + 4;',
  13494. ' $mod.TObject.State = $Self.State + 5;',
  13495. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13496. ' $Self.SetSize($Self.GetSize() + 7);',
  13497. ' $Self.SetSize($Self.GetSize() + 8);',
  13498. ' };',
  13499. ' Sub();',
  13500. ' $Self.Key = $Self.Key + 12;',
  13501. ' $Self.Key = $Self.Key + 13;',
  13502. ' $mod.TObject.State = $Self.State + 14;',
  13503. ' $mod.TObject.State = $Self.State + 15;',
  13504. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13505. ' $Self.SetSize($Self.GetSize() + 17);',
  13506. ' $Self.SetSize($Self.GetSize() + 18);',
  13507. ' };',
  13508. '});',
  13509. '']),
  13510. LinesToStr([ // $mod.$main
  13511. '']));
  13512. end;
  13513. procedure TTestModule.TestClass_NestedProcSelf2;
  13514. begin
  13515. StartProgram(false);
  13516. Add([
  13517. 'type',
  13518. ' TObject = class',
  13519. ' Key: longint;',
  13520. ' class var State: longint;',
  13521. ' function GetSize: longint; virtual; abstract;',
  13522. ' procedure SetSize(Value: longint); virtual; abstract;',
  13523. ' property Size: longint read GetSize write SetSize;',
  13524. ' end;',
  13525. ' TBird = class',
  13526. ' procedure DoIt;',
  13527. ' end;',
  13528. 'procedure tbird.doit;',
  13529. ' procedure Sub;',
  13530. ' begin',
  13531. ' key:=key+2;',
  13532. ' self.key:=self.key+3;',
  13533. ' state:=state+4;',
  13534. ' self.state:=self.state+5;',
  13535. ' tobject.state:=tobject.state+6;',
  13536. ' size:=size+7;',
  13537. ' self.size:=self.size+8;',
  13538. ' end;',
  13539. 'begin',
  13540. ' sub;',
  13541. ' key:=key+12;',
  13542. ' self.key:=self.key+13;',
  13543. ' state:=state+14;',
  13544. ' self.state:=self.state+15;',
  13545. ' tobject.state:=tobject.state+16;',
  13546. ' size:=size+17;',
  13547. ' self.size:=self.size+18;',
  13548. 'end;',
  13549. 'begin',
  13550. '']);
  13551. ConvertProgram;
  13552. CheckSource('TestClass_NestedProcSelf2',
  13553. LinesToStr([ // statements
  13554. 'rtl.createClass($mod, "TObject", null, function () {',
  13555. ' this.State = 0;',
  13556. ' this.$init = function () {',
  13557. ' this.Key = 0;',
  13558. ' };',
  13559. ' this.$final = function () {',
  13560. ' };',
  13561. '});',
  13562. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13563. ' this.DoIt = function () {',
  13564. ' var $Self = this;',
  13565. ' function Sub() {',
  13566. ' $Self.Key = $Self.Key + 2;',
  13567. ' $Self.Key = $Self.Key + 3;',
  13568. ' $mod.TObject.State = $Self.State + 4;',
  13569. ' $mod.TObject.State = $Self.State + 5;',
  13570. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13571. ' $Self.SetSize($Self.GetSize() + 7);',
  13572. ' $Self.SetSize($Self.GetSize() + 8);',
  13573. ' };',
  13574. ' Sub();',
  13575. ' $Self.Key = $Self.Key + 12;',
  13576. ' $Self.Key = $Self.Key + 13;',
  13577. ' $mod.TObject.State = $Self.State + 14;',
  13578. ' $mod.TObject.State = $Self.State + 15;',
  13579. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13580. ' $Self.SetSize($Self.GetSize() + 17);',
  13581. ' $Self.SetSize($Self.GetSize() + 18);',
  13582. ' };',
  13583. '});',
  13584. '']),
  13585. LinesToStr([ // $mod.$main
  13586. '']));
  13587. end;
  13588. procedure TTestModule.TestClass_NestedProcClassSelf;
  13589. begin
  13590. StartProgram(false);
  13591. Add([
  13592. 'type',
  13593. ' TObject = class',
  13594. ' class var State: longint;',
  13595. ' class procedure DoIt;',
  13596. ' class function GetSize: longint; virtual; abstract;',
  13597. ' class procedure SetSize(Value: longint); virtual; abstract;',
  13598. ' class property Size: longint read GetSize write SetSize;',
  13599. ' end;',
  13600. 'class procedure tobject.doit;',
  13601. ' procedure Sub;',
  13602. ' begin',
  13603. ' state:=state+2;',
  13604. ' self.state:=self.state+3;',
  13605. ' tobject.state:=tobject.state+4;',
  13606. ' size:=size+5;',
  13607. ' self.size:=self.size+6;',
  13608. ' tobject.size:=tobject.size+7;',
  13609. ' end;',
  13610. 'begin',
  13611. ' sub;',
  13612. ' state:=state+12;',
  13613. ' self.state:=self.state+13;',
  13614. ' tobject.state:=tobject.state+14;',
  13615. ' size:=size+15;',
  13616. ' self.size:=self.size+16;',
  13617. ' tobject.size:=tobject.size+17;',
  13618. 'end;',
  13619. 'begin',
  13620. '']);
  13621. ConvertProgram;
  13622. CheckSource('TestClass_NestedProcClassSelf',
  13623. LinesToStr([ // statements
  13624. 'rtl.createClass($mod, "TObject", null, function () {',
  13625. ' this.State = 0;',
  13626. ' this.$init = function () {',
  13627. ' };',
  13628. ' this.$final = function () {',
  13629. ' };',
  13630. ' this.DoIt = function () {',
  13631. ' var $Self = this;',
  13632. ' function Sub() {',
  13633. ' $mod.TObject.State = $Self.State + 2;',
  13634. ' $mod.TObject.State = $Self.State + 3;',
  13635. ' $mod.TObject.State = $mod.TObject.State + 4;',
  13636. ' $Self.SetSize($Self.GetSize() + 5);',
  13637. ' $Self.SetSize($Self.GetSize() + 6);',
  13638. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  13639. ' };',
  13640. ' Sub();',
  13641. ' $mod.TObject.State = $Self.State + 12;',
  13642. ' $mod.TObject.State = $Self.State + 13;',
  13643. ' $mod.TObject.State = $mod.TObject.State + 14;',
  13644. ' $Self.SetSize($Self.GetSize() + 15);',
  13645. ' $Self.SetSize($Self.GetSize() + 16);',
  13646. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  13647. ' };',
  13648. '});',
  13649. '']),
  13650. LinesToStr([ // $mod.$main
  13651. '']));
  13652. end;
  13653. procedure TTestModule.TestClass_NestedProcCallInherited;
  13654. begin
  13655. StartProgram(false);
  13656. Add([
  13657. 'type',
  13658. ' TObject = class',
  13659. ' function DoIt(k: boolean): longint; virtual;',
  13660. ' end;',
  13661. ' TBird = class',
  13662. ' function DoIt(k: boolean): longint; override;',
  13663. ' end;',
  13664. 'function tobject.doit(k: boolean): longint;',
  13665. 'begin',
  13666. 'end;',
  13667. 'function tbird.doit(k: boolean): longint;',
  13668. ' procedure Sub;',
  13669. ' begin',
  13670. ' inherited DoIt(true);',
  13671. //' if inherited DoIt(false)=4 then ;',
  13672. ' end;',
  13673. 'begin',
  13674. ' Sub;',
  13675. ' inherited;',
  13676. ' inherited DoIt(true);',
  13677. //' if inherited DoIt(false)=14 then ;',
  13678. 'end;',
  13679. 'begin',
  13680. '']);
  13681. ConvertProgram;
  13682. CheckSource('TestClass_NestedProcCallInherited',
  13683. LinesToStr([ // statements
  13684. 'rtl.createClass($mod, "TObject", null, function () {',
  13685. ' this.$init = function () {',
  13686. ' };',
  13687. ' this.$final = function () {',
  13688. ' };',
  13689. ' this.DoIt = function (k) {',
  13690. ' var Result = 0;',
  13691. ' return Result;',
  13692. ' };',
  13693. '});',
  13694. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13695. ' this.DoIt = function (k) {',
  13696. ' var $Self = this;',
  13697. ' var Result = 0;',
  13698. ' function Sub() {',
  13699. ' $mod.TObject.DoIt.call($Self, true);',
  13700. ' };',
  13701. ' Sub();',
  13702. ' $mod.TObject.DoIt.apply($Self, arguments);',
  13703. ' $mod.TObject.DoIt.call($Self, true);',
  13704. ' return Result;',
  13705. ' };',
  13706. '});',
  13707. '']),
  13708. LinesToStr([ // $mod.$main
  13709. '']));
  13710. end;
  13711. procedure TTestModule.TestClass_TObjectFree;
  13712. begin
  13713. StartProgram(false);
  13714. Add([
  13715. 'type',
  13716. ' TObject = class',
  13717. ' Obj: tobject;',
  13718. ' procedure Free;',
  13719. ' procedure Release;',
  13720. ' end;',
  13721. 'procedure tobject.free;',
  13722. 'begin',
  13723. 'end;',
  13724. 'procedure tobject.release;',
  13725. 'begin',
  13726. ' free;',
  13727. ' if true then free;',
  13728. 'end;',
  13729. 'function DoIt(o: tobject): tobject;',
  13730. 'var l: tobject;',
  13731. 'begin',
  13732. ' o.free;',
  13733. ' o.free();',
  13734. ' l.free;',
  13735. ' l.free();',
  13736. ' o.obj.free;',
  13737. ' o.obj.free();',
  13738. ' with o do obj.free;',
  13739. ' with o do obj.free();',
  13740. ' result.Free;',
  13741. ' result.Free();',
  13742. 'end;',
  13743. 'var o: tobject;',
  13744. ' a: array of tobject;',
  13745. 'begin',
  13746. ' o.free;',
  13747. ' o.obj.free;',
  13748. ' a[1+2].free;',
  13749. '']);
  13750. ConvertProgram;
  13751. CheckSource('TestClass_TObjectFree',
  13752. LinesToStr([ // statements
  13753. 'rtl.createClass($mod, "TObject", null, function () {',
  13754. ' this.$init = function () {',
  13755. ' this.Obj = null;',
  13756. ' };',
  13757. ' this.$final = function () {',
  13758. ' this.Obj = undefined;',
  13759. ' };',
  13760. ' this.Free = function () {',
  13761. ' };',
  13762. ' this.Release = function () {',
  13763. ' this.Free();',
  13764. ' if (true) this.Free();',
  13765. ' };',
  13766. '});',
  13767. 'this.DoIt = function (o) {',
  13768. ' var Result = null;',
  13769. ' var l = null;',
  13770. ' o = rtl.freeLoc(o);',
  13771. ' o = rtl.freeLoc(o);',
  13772. ' l = rtl.freeLoc(l);',
  13773. ' l = rtl.freeLoc(l);',
  13774. ' rtl.free(o, "Obj");',
  13775. ' rtl.free(o, "Obj");',
  13776. ' rtl.free(o, "Obj");',
  13777. ' rtl.free(o, "Obj");',
  13778. ' Result = rtl.freeLoc(Result);',
  13779. ' Result = rtl.freeLoc(Result);',
  13780. ' return Result;',
  13781. '};',
  13782. 'this.o = null;',
  13783. 'this.a = [];',
  13784. '']),
  13785. LinesToStr([ // $mod.$main
  13786. 'rtl.free($mod, "o");',
  13787. 'rtl.free($mod.o, "Obj");',
  13788. 'rtl.free($mod.a, 1 + 2);',
  13789. '']));
  13790. end;
  13791. procedure TTestModule.TestClass_TObjectFree_VarArg;
  13792. begin
  13793. StartProgram(false);
  13794. Add([
  13795. 'type',
  13796. ' TObject = class',
  13797. ' Obj: tobject;',
  13798. ' procedure Free;',
  13799. ' end;',
  13800. 'procedure tobject.free;',
  13801. 'begin',
  13802. 'end;',
  13803. 'procedure DoIt(var o: tobject);',
  13804. 'begin',
  13805. ' o.free;',
  13806. ' o.free();',
  13807. 'end;',
  13808. 'begin',
  13809. '']);
  13810. ConvertProgram;
  13811. CheckSource('TestClass_TObjectFree_VarArg',
  13812. LinesToStr([ // statements
  13813. 'rtl.createClass($mod, "TObject", null, function () {',
  13814. ' this.$init = function () {',
  13815. ' this.Obj = null;',
  13816. ' };',
  13817. ' this.$final = function () {',
  13818. ' this.Obj = undefined;',
  13819. ' };',
  13820. ' this.Free = function () {',
  13821. ' };',
  13822. '});',
  13823. 'this.DoIt = function (o) {',
  13824. ' o.set(rtl.freeLoc(o.get()));',
  13825. ' o.set(rtl.freeLoc(o.get()));',
  13826. '};',
  13827. '']),
  13828. LinesToStr([ // $mod.$main
  13829. '']));
  13830. end;
  13831. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  13832. begin
  13833. StartProgram(false);
  13834. Add([
  13835. 'type',
  13836. ' TObject = class',
  13837. ' constructor Create;',
  13838. ' procedure Free;',
  13839. ' end;',
  13840. 'constructor TObject.Create; begin end;',
  13841. 'procedure tobject.free; begin end;',
  13842. 'begin',
  13843. ' with tobject.create do free;',
  13844. '']);
  13845. ConvertProgram;
  13846. CheckSource('TestClass_TObjectFreeNewInstance',
  13847. LinesToStr([ // statements
  13848. 'rtl.createClass($mod, "TObject", null, function () {',
  13849. ' this.$init = function () {',
  13850. ' };',
  13851. ' this.$final = function () {',
  13852. ' };',
  13853. ' this.Create = function () {',
  13854. ' return this;',
  13855. ' };',
  13856. ' this.Free = function () {',
  13857. ' };',
  13858. '});',
  13859. '']),
  13860. LinesToStr([ // $mod.$main
  13861. 'var $with1 = $mod.TObject.$create("Create");',
  13862. '$with1=rtl.freeLoc($with1);',
  13863. '']));
  13864. end;
  13865. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  13866. begin
  13867. StartProgram(false);
  13868. Add([
  13869. 'type',
  13870. ' TObject = class',
  13871. ' destructor Destroy;',
  13872. ' procedure Free;',
  13873. ' end;',
  13874. 'destructor TObject.Destroy; begin end;',
  13875. 'procedure tobject.free; begin end;',
  13876. 'var o: tobject;',
  13877. 'begin',
  13878. ' o.free;',
  13879. '']);
  13880. Converter.UseLowerCase:=true;
  13881. ConvertProgram;
  13882. CheckSource('TestClass_TObjectFreeLowerCase',
  13883. LinesToStr([ // statements
  13884. 'rtl.createClass($mod, "tobject", null, function () {',
  13885. ' this.$init = function () {',
  13886. ' };',
  13887. ' this.$final = function () {',
  13888. ' };',
  13889. ' rtl.tObjectDestroy = "destroy";',
  13890. ' this.destroy = function () {',
  13891. ' };',
  13892. ' this.free = function () {',
  13893. ' };',
  13894. '});',
  13895. 'this.o = null;',
  13896. '']),
  13897. LinesToStr([ // $mod.$main
  13898. 'rtl.free($mod, "o");',
  13899. '']));
  13900. end;
  13901. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  13902. begin
  13903. StartProgram(false);
  13904. Add([
  13905. 'type',
  13906. ' TObject = class',
  13907. ' procedure Free;',
  13908. ' function GetObj: tobject; virtual; abstract;',
  13909. ' end;',
  13910. 'procedure tobject.free;',
  13911. 'begin',
  13912. 'end;',
  13913. 'var o: tobject;',
  13914. 'begin',
  13915. ' o.getobj.free;',
  13916. '']);
  13917. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13918. ConvertProgram;
  13919. end;
  13920. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  13921. begin
  13922. StartProgram(false);
  13923. Add([
  13924. 'type',
  13925. ' TObject = class',
  13926. ' procedure Free;',
  13927. ' FObj: TObject;',
  13928. ' property Obj: tobject read FObj write FObj;',
  13929. ' end;',
  13930. 'procedure tobject.free;',
  13931. 'begin',
  13932. 'end;',
  13933. 'var o: tobject;',
  13934. 'begin',
  13935. ' o.obj.free;',
  13936. '']);
  13937. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13938. ConvertProgram;
  13939. end;
  13940. procedure TTestModule.TestClass_ForIn;
  13941. begin
  13942. StartProgram(false);
  13943. Add([
  13944. 'type',
  13945. ' TObject = class end;',
  13946. ' TItem = TObject;',
  13947. ' TEnumerator = class',
  13948. ' FCurrent: TItem;',
  13949. ' property Current: TItem read FCurrent;',
  13950. ' function MoveNext: boolean;',
  13951. ' end;',
  13952. ' TBird = class',
  13953. ' function GetEnumerator: TEnumerator;',
  13954. ' end;',
  13955. 'function TEnumerator.MoveNext: boolean;',
  13956. 'begin',
  13957. 'end;',
  13958. 'function TBird.GetEnumerator: TEnumerator;',
  13959. 'begin',
  13960. 'end;',
  13961. 'var',
  13962. ' b: TBird;',
  13963. ' i, i2: TItem;',
  13964. 'begin',
  13965. ' for i in b do i2:=i;']);
  13966. ConvertProgram;
  13967. CheckSource('TestClass_ForIn',
  13968. LinesToStr([ // statements
  13969. 'rtl.createClass($mod, "TObject", null, function () {',
  13970. ' this.$init = function () {',
  13971. ' };',
  13972. ' this.$final = function () {',
  13973. ' };',
  13974. '});',
  13975. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  13976. ' this.$init = function () {',
  13977. ' $mod.TObject.$init.call(this);',
  13978. ' this.FCurrent = null;',
  13979. ' };',
  13980. ' this.$final = function () {',
  13981. ' this.FCurrent = undefined;',
  13982. ' $mod.TObject.$final.call(this);',
  13983. ' };',
  13984. ' this.MoveNext = function () {',
  13985. ' var Result = false;',
  13986. ' return Result;',
  13987. ' };',
  13988. '});',
  13989. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13990. ' this.GetEnumerator = function () {',
  13991. ' var Result = null;',
  13992. ' return Result;',
  13993. ' };',
  13994. '});',
  13995. 'this.b = null;',
  13996. 'this.i = null;',
  13997. 'this.i2 = null;'
  13998. ]),
  13999. LinesToStr([ // $mod.$main
  14000. 'var $in1 = $mod.b.GetEnumerator();',
  14001. 'try {',
  14002. ' while ($in1.MoveNext()){',
  14003. ' $mod.i = $in1.FCurrent;',
  14004. ' $mod.i2 = $mod.i;',
  14005. ' }',
  14006. '} finally {',
  14007. ' $in1 = rtl.freeLoc($in1)',
  14008. '};',
  14009. '']));
  14010. end;
  14011. procedure TTestModule.TestClass_Message;
  14012. begin
  14013. StartProgram(false);
  14014. Add([
  14015. 'type',
  14016. ' TObject = class',
  14017. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  14018. ' procedure Run; overload; virtual; abstract;',
  14019. ' procedure Run(var Msg); overload; message ''Fast'';',
  14020. ' end;',
  14021. 'procedure TObject.Run(var Msg);',
  14022. 'begin',
  14023. 'end;',
  14024. 'begin',
  14025. '']);
  14026. ConvertProgram;
  14027. CheckSource('TestClass_Message',
  14028. LinesToStr([ // statements
  14029. 'rtl.createClass($mod, "TObject", null, function () {',
  14030. ' this.$init = function () {',
  14031. ' };',
  14032. ' this.$final = function () {',
  14033. ' };',
  14034. ' this.Run$1 = function (Msg) {',
  14035. ' };',
  14036. ' this.$msgint = {',
  14037. ' "2": "Fly"',
  14038. ' };',
  14039. ' this.$msgstr = {',
  14040. ' Fast: "Run$1"',
  14041. ' };',
  14042. '});',
  14043. '']),
  14044. LinesToStr([ // $mod.$main
  14045. '']));
  14046. end;
  14047. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  14048. begin
  14049. StartProgram(false);
  14050. Add([
  14051. 'type',
  14052. ' TObject = class',
  14053. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  14054. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  14055. ' end;',
  14056. 'begin',
  14057. '']);
  14058. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  14059. ConvertProgram;
  14060. end;
  14061. procedure TTestModule.TestClassOf_Create;
  14062. begin
  14063. StartProgram(false);
  14064. Add('type');
  14065. Add(' TObject = class');
  14066. Add(' constructor Create;');
  14067. Add(' end;');
  14068. Add(' TClass = class of TObject;');
  14069. Add('constructor tobject.create; begin end;');
  14070. Add('var');
  14071. Add(' Obj: tobject;');
  14072. Add(' C: tclass;');
  14073. Add('begin');
  14074. Add(' obj:=C.create;');
  14075. Add(' with c do obj:=create;');
  14076. ConvertProgram;
  14077. CheckSource('TestClassOf_Create',
  14078. LinesToStr([ // statements
  14079. 'rtl.createClass($mod, "TObject", null, function () {',
  14080. ' this.$init = function () {',
  14081. ' };',
  14082. ' this.$final = function () {',
  14083. ' };',
  14084. ' this.Create = function () {',
  14085. ' return this;',
  14086. ' };',
  14087. '});',
  14088. 'this.Obj = null;',
  14089. 'this.C = null;'
  14090. ]),
  14091. LinesToStr([ // $mod.$main
  14092. '$mod.Obj = $mod.C.$create("Create");',
  14093. 'var $with1 = $mod.C;',
  14094. '$mod.Obj = $with1.$create("Create");',
  14095. '']));
  14096. end;
  14097. procedure TTestModule.TestClassOf_Call;
  14098. begin
  14099. StartProgram(false);
  14100. Add('type');
  14101. Add(' TObject = class');
  14102. Add(' class procedure DoIt;');
  14103. Add(' end;');
  14104. Add(' TClass = class of TObject;');
  14105. Add('class procedure tobject.doit; begin end;');
  14106. Add('var');
  14107. Add(' C: tclass;');
  14108. Add('begin');
  14109. Add(' c.doit;');
  14110. Add(' with c do doit;');
  14111. ConvertProgram;
  14112. CheckSource('TestClassOf_Call',
  14113. LinesToStr([ // statements
  14114. 'rtl.createClass($mod, "TObject", null, function () {',
  14115. ' this.$init = function () {',
  14116. ' };',
  14117. ' this.$final = function () {',
  14118. ' };',
  14119. ' this.DoIt = function () {',
  14120. ' };',
  14121. '});',
  14122. 'this.C = null;'
  14123. ]),
  14124. LinesToStr([ // $mod.$main
  14125. '$mod.C.DoIt();',
  14126. 'var $with1 = $mod.C;',
  14127. '$with1.DoIt();',
  14128. '']));
  14129. end;
  14130. procedure TTestModule.TestClassOf_Assign;
  14131. begin
  14132. StartProgram(false);
  14133. Add('type');
  14134. Add(' TClass = class of TObject;');
  14135. Add(' TObject = class');
  14136. Add(' ClassType: TClass; ');
  14137. Add(' end;');
  14138. Add('var');
  14139. Add(' Obj: tobject;');
  14140. Add(' C: tclass;');
  14141. Add('begin');
  14142. Add(' c:=nil;');
  14143. Add(' c:=obj.classtype;');
  14144. ConvertProgram;
  14145. CheckSource('TestClassOf_Assign',
  14146. LinesToStr([ // statements
  14147. 'rtl.createClass($mod, "TObject", null, function () {',
  14148. ' this.$init = function () {',
  14149. ' this.ClassType = null;',
  14150. ' };',
  14151. ' this.$final = function () {',
  14152. ' this.ClassType = undefined;',
  14153. ' };',
  14154. '});',
  14155. 'this.Obj = null;',
  14156. 'this.C = null;'
  14157. ]),
  14158. LinesToStr([ // $mod.$main
  14159. '$mod.C = null;',
  14160. '$mod.C = $mod.Obj.ClassType;',
  14161. '']));
  14162. end;
  14163. procedure TTestModule.TestClassOf_Is;
  14164. begin
  14165. StartProgram(false);
  14166. Add('type');
  14167. Add(' TClass = class of TObject;');
  14168. Add(' TObject = class');
  14169. Add(' end;');
  14170. Add(' TCar = class');
  14171. Add(' end;');
  14172. Add(' TCars = class of TCar;');
  14173. Add('var');
  14174. Add(' Obj: tobject;');
  14175. Add(' C: tclass;');
  14176. Add(' Cars: tcars;');
  14177. Add('begin');
  14178. Add(' if c is tcar then ;');
  14179. Add(' if c is tcars then ;');
  14180. ConvertProgram;
  14181. CheckSource('TestClassOf_Is',
  14182. LinesToStr([ // statements
  14183. 'rtl.createClass($mod, "TObject", null, function () {',
  14184. ' this.$init = function () {',
  14185. ' };',
  14186. ' this.$final = function () {',
  14187. ' };',
  14188. '});',
  14189. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  14190. '});',
  14191. 'this.Obj = null;',
  14192. 'this.C = null;',
  14193. 'this.Cars = null;'
  14194. ]),
  14195. LinesToStr([ // $mod.$main
  14196. 'if(rtl.is($mod.C,$mod.TCar));',
  14197. 'if(rtl.is($mod.C,$mod.TCar));',
  14198. '']));
  14199. end;
  14200. procedure TTestModule.TestClassOf_Compare;
  14201. begin
  14202. StartProgram(false);
  14203. Add('type');
  14204. Add(' TClass = class of TObject;');
  14205. Add(' TObject = class');
  14206. Add(' ClassType: TClass; ');
  14207. Add(' end;');
  14208. Add('var');
  14209. Add(' b: boolean;');
  14210. Add(' Obj: tobject;');
  14211. Add(' C: tclass;');
  14212. Add('begin');
  14213. Add(' b:=c=nil;');
  14214. Add(' b:=nil=c;');
  14215. Add(' b:=c=obj.classtype;');
  14216. Add(' b:=obj.classtype=c;');
  14217. Add(' b:=c=TObject;');
  14218. Add(' b:=TObject=c;');
  14219. Add(' b:=c<>nil;');
  14220. Add(' b:=nil<>c;');
  14221. Add(' b:=c<>obj.classtype;');
  14222. Add(' b:=obj.classtype<>c;');
  14223. Add(' b:=c<>TObject;');
  14224. Add(' b:=TObject<>c;');
  14225. ConvertProgram;
  14226. CheckSource('TestClassOf_Compare',
  14227. LinesToStr([ // statements
  14228. 'rtl.createClass($mod, "TObject", null, function () {',
  14229. ' this.$init = function () {',
  14230. ' this.ClassType = null;',
  14231. ' };',
  14232. ' this.$final = function () {',
  14233. ' this.ClassType = undefined;',
  14234. ' };',
  14235. '});',
  14236. 'this.b = false;',
  14237. 'this.Obj = null;',
  14238. 'this.C = null;'
  14239. ]),
  14240. LinesToStr([ // $mod.$main
  14241. '$mod.b = $mod.C === null;',
  14242. '$mod.b = null === $mod.C;',
  14243. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  14244. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  14245. '$mod.b = $mod.C === $mod.TObject;',
  14246. '$mod.b = $mod.TObject === $mod.C;',
  14247. '$mod.b = $mod.C !== null;',
  14248. '$mod.b = null !== $mod.C;',
  14249. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  14250. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  14251. '$mod.b = $mod.C !== $mod.TObject;',
  14252. '$mod.b = $mod.TObject !== $mod.C;',
  14253. '']));
  14254. end;
  14255. procedure TTestModule.TestClassOf_ClassVar;
  14256. begin
  14257. StartProgram(false);
  14258. Add('type');
  14259. Add(' TObject = class');
  14260. Add(' class var id: longint;');
  14261. Add(' end;');
  14262. Add(' TClass = class of TObject;');
  14263. Add('var');
  14264. Add(' C: tclass;');
  14265. Add('begin');
  14266. Add(' C.id:=C.id;');
  14267. ConvertProgram;
  14268. CheckSource('TestClassOf_ClassVar',
  14269. LinesToStr([ // statements
  14270. 'rtl.createClass($mod, "TObject", null, function () {',
  14271. ' this.id = 0;',
  14272. ' this.$init = function () {',
  14273. ' };',
  14274. ' this.$final = function () {',
  14275. ' };',
  14276. '});',
  14277. 'this.C = null;'
  14278. ]),
  14279. LinesToStr([ // $mod.$main
  14280. '$mod.TObject.id = $mod.C.id;',
  14281. '']));
  14282. end;
  14283. procedure TTestModule.TestClassOf_ClassMethod;
  14284. begin
  14285. StartProgram(false);
  14286. Add('type');
  14287. Add(' TObject = class');
  14288. Add(' class function DoIt(i: longint = 0): longint;');
  14289. Add(' end;');
  14290. Add(' TClass = class of TObject;');
  14291. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  14292. Add('var');
  14293. Add(' i: longint;');
  14294. Add(' C: tclass;');
  14295. Add('begin');
  14296. Add(' C.DoIt;');
  14297. Add(' C.DoIt();');
  14298. Add(' i:=C.DoIt;');
  14299. Add(' i:=C.DoIt();');
  14300. ConvertProgram;
  14301. CheckSource('TestClassOf_ClassMethod',
  14302. LinesToStr([ // statements
  14303. 'rtl.createClass($mod, "TObject", null, function () {',
  14304. ' this.$init = function () {',
  14305. ' };',
  14306. ' this.$final = function () {',
  14307. ' };',
  14308. ' this.DoIt = function (i) {',
  14309. ' var Result = 0;',
  14310. ' return Result;',
  14311. ' };',
  14312. '});',
  14313. 'this.i = 0;',
  14314. 'this.C = null;'
  14315. ]),
  14316. LinesToStr([ // $mod.$main
  14317. '$mod.C.DoIt(0);',
  14318. '$mod.C.DoIt(0);',
  14319. '$mod.i = $mod.C.DoIt(0);',
  14320. '$mod.i = $mod.C.DoIt(0);',
  14321. '']));
  14322. end;
  14323. procedure TTestModule.TestClassOf_ClassProperty;
  14324. begin
  14325. StartProgram(false);
  14326. Add([
  14327. 'type',
  14328. ' TObject = class',
  14329. ' class var FA: longint;',
  14330. ' class function GetA: longint;',
  14331. ' class procedure SetA(Value: longint);',
  14332. ' class property pA: longint read fa write fa;',
  14333. ' class property pB: longint read geta write seta;',
  14334. ' end;',
  14335. ' TObjectClass = class of tobject;',
  14336. 'class function tobject.geta: longint; begin end;',
  14337. 'class procedure tobject.seta(value: longint); begin end;',
  14338. 'var',
  14339. ' b: boolean;',
  14340. ' Obj: tobject;',
  14341. ' Cla: tobjectclass;',
  14342. 'begin',
  14343. ' obj.pa:=obj.pa;',
  14344. ' obj.pb:=obj.pb;',
  14345. ' b:=obj.pa=4;',
  14346. ' b:=obj.pb=obj.pb;',
  14347. ' b:=5=obj.pa;',
  14348. ' cla.pa:=6;',
  14349. ' cla.pa:=cla.pa;',
  14350. ' cla.pb:=cla.pb;',
  14351. ' b:=cla.pa=7;',
  14352. ' b:=cla.pb=cla.pb;',
  14353. ' b:=8=cla.pa;',
  14354. ' tobject.pa:=9;',
  14355. ' tobject.pb:=tobject.pb;',
  14356. ' b:=tobject.pa=10;',
  14357. ' b:=11=tobject.pa;',
  14358. '']);
  14359. ConvertProgram;
  14360. CheckSource('TestClassOf_ClassProperty',
  14361. LinesToStr([ // statements
  14362. 'rtl.createClass($mod, "TObject", null, function () {',
  14363. ' this.FA = 0;',
  14364. ' this.$init = function () {',
  14365. ' };',
  14366. ' this.$final = function () {',
  14367. ' };',
  14368. ' this.GetA = function () {',
  14369. ' var Result = 0;',
  14370. ' return Result;',
  14371. ' };',
  14372. ' this.SetA = function (Value) {',
  14373. ' };',
  14374. '});',
  14375. 'this.b = false;',
  14376. 'this.Obj = null;',
  14377. 'this.Cla = null;'
  14378. ]),
  14379. LinesToStr([ // $mod.$main
  14380. '$mod.TObject.FA = $mod.Obj.FA;',
  14381. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  14382. '$mod.b = $mod.Obj.FA === 4;',
  14383. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  14384. '$mod.b = 5 === $mod.Obj.FA;',
  14385. '$mod.TObject.FA = 6;',
  14386. '$mod.TObject.FA = $mod.Cla.FA;',
  14387. '$mod.Cla.SetA($mod.Cla.GetA());',
  14388. '$mod.b = $mod.Cla.FA === 7;',
  14389. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  14390. '$mod.b = 8 === $mod.Cla.FA;',
  14391. '$mod.TObject.FA = 9;',
  14392. '$mod.TObject.SetA($mod.TObject.GetA());',
  14393. '$mod.b = $mod.TObject.FA === 10;',
  14394. '$mod.b = 11 === $mod.TObject.FA;',
  14395. '']));
  14396. end;
  14397. procedure TTestModule.TestClassOf_ClassMethodSelf;
  14398. begin
  14399. StartProgram(false);
  14400. Add('type');
  14401. Add(' TObject = class');
  14402. Add(' class var GlobalId: longint;');
  14403. Add(' class procedure ProcA;');
  14404. Add(' end;');
  14405. Add('class procedure tobject.proca;');
  14406. Add('var b: boolean;');
  14407. Add('begin');
  14408. Add(' b:=self=nil;');
  14409. Add(' b:=self.globalid=3;');
  14410. Add(' b:=4=self.globalid;');
  14411. Add(' self.globalid:=5;');
  14412. Add(' self.proca;');
  14413. Add('end;');
  14414. Add('begin');
  14415. ConvertProgram;
  14416. CheckSource('TestClassOf_ClassMethodSelf',
  14417. LinesToStr([ // statements
  14418. 'rtl.createClass($mod, "TObject", null, function () {',
  14419. ' this.GlobalId = 0;',
  14420. ' this.$init = function () {',
  14421. ' };',
  14422. ' this.$final = function () {',
  14423. ' };',
  14424. ' this.ProcA = function () {',
  14425. ' var b = false;',
  14426. ' b = this === null;',
  14427. ' b = this.GlobalId === 3;',
  14428. ' b = 4 === this.GlobalId;',
  14429. ' $mod.TObject.GlobalId = 5;',
  14430. ' this.ProcA();',
  14431. ' };',
  14432. '});'
  14433. ]),
  14434. LinesToStr([ // $mod.$main
  14435. '']));
  14436. end;
  14437. procedure TTestModule.TestClassOf_TypeCast;
  14438. begin
  14439. StartProgram(false);
  14440. Add('type');
  14441. Add(' TObject = class');
  14442. Add(' class procedure {#TObject_DoIt}DoIt;');
  14443. Add(' end;');
  14444. Add(' TClass = class of TObject;');
  14445. Add(' TMobile = class');
  14446. Add(' class procedure {#TMobile_DoIt}DoIt;');
  14447. Add(' end;');
  14448. Add(' TMobileClass = class of TMobile;');
  14449. Add(' TCar = class(TMobile)');
  14450. Add(' class procedure {#TCar_DoIt}DoIt;');
  14451. Add(' end;');
  14452. Add(' TCarClass = class of TCar;');
  14453. Add('class procedure TObject.DoIt;');
  14454. Add('begin');
  14455. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14456. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14457. Add('end;');
  14458. Add('class procedure TMobile.DoIt;');
  14459. Add('begin');
  14460. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14461. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14462. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  14463. Add('end;');
  14464. Add('class procedure TCar.DoIt; begin end;');
  14465. Add('var');
  14466. Add(' ObjC: TClass;');
  14467. Add(' MobileC: TMobileClass;');
  14468. Add(' CarC: TCarClass;');
  14469. Add('begin');
  14470. Add(' ObjC.{@TObject_DoIt}DoIt;');
  14471. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  14472. Add(' CarC.{@TCar_DoIt}DoIt;');
  14473. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  14474. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  14475. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  14476. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  14477. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  14478. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  14479. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  14480. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  14481. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  14482. ConvertProgram;
  14483. CheckSource('TestClassOf_TypeCast',
  14484. LinesToStr([ // statements
  14485. 'rtl.createClass($mod, "TObject", null, function () {',
  14486. ' this.$init = function () {',
  14487. ' };',
  14488. ' this.$final = function () {',
  14489. ' };',
  14490. ' this.DoIt = function () {',
  14491. ' this.DoIt();',
  14492. ' this.DoIt$1();',
  14493. ' };',
  14494. '});',
  14495. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  14496. ' this.DoIt$1 = function () {',
  14497. ' this.DoIt();',
  14498. ' this.DoIt$1();',
  14499. ' this.DoIt$2();',
  14500. ' };',
  14501. '});',
  14502. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  14503. ' this.DoIt$2 = function () {',
  14504. ' };',
  14505. '});',
  14506. 'this.ObjC = null;',
  14507. 'this.MobileC = null;',
  14508. 'this.CarC = null;',
  14509. '']),
  14510. LinesToStr([ // $mod.$main
  14511. '$mod.ObjC.DoIt();',
  14512. '$mod.MobileC.DoIt$1();',
  14513. '$mod.CarC.DoIt$2();',
  14514. '$mod.ObjC.DoIt();',
  14515. '$mod.ObjC.DoIt$1();',
  14516. '$mod.ObjC.DoIt$2();',
  14517. '$mod.MobileC.DoIt();',
  14518. '$mod.MobileC.DoIt$1();',
  14519. '$mod.MobileC.DoIt$2();',
  14520. '$mod.CarC.DoIt();',
  14521. '$mod.CarC.DoIt$1();',
  14522. '$mod.CarC.DoIt$2();',
  14523. '']));
  14524. end;
  14525. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  14526. begin
  14527. StartProgram(false);
  14528. Add('type');
  14529. Add(' TObject = class');
  14530. Add(' function CurNow: longint; ');
  14531. Add(' class function Now: longint; ');
  14532. Add(' end;');
  14533. Add('function TObject.CurNow: longint; begin end;');
  14534. Add('class function TObject.Now: longint; begin end;');
  14535. Add('var');
  14536. Add(' Obj: tobject;');
  14537. Add(' vI: longint;');
  14538. Add('begin');
  14539. Add(' obj.curnow;');
  14540. Add(' vi:=obj.curnow;');
  14541. Add(' tobject.now;');
  14542. Add(' vi:=tobject.now;');
  14543. ConvertProgram;
  14544. CheckSource('TestClassOf_ImplicitFunctionCall',
  14545. LinesToStr([ // statements
  14546. 'rtl.createClass($mod, "TObject", null, function () {',
  14547. ' this.$init = function () {',
  14548. ' };',
  14549. ' this.$final = function () {',
  14550. ' };',
  14551. ' this.CurNow = function () {',
  14552. ' var Result = 0;',
  14553. ' return Result;',
  14554. ' };',
  14555. ' this.Now = function () {',
  14556. ' var Result = 0;',
  14557. ' return Result;',
  14558. ' };',
  14559. '});',
  14560. 'this.Obj = null;',
  14561. 'this.vI = 0;',
  14562. '']),
  14563. LinesToStr([ // $mod.$main
  14564. '$mod.Obj.CurNow();',
  14565. '$mod.vI = $mod.Obj.CurNow();',
  14566. '$mod.TObject.Now();',
  14567. '$mod.vI = $mod.TObject.Now();',
  14568. '']));
  14569. end;
  14570. procedure TTestModule.TestClassOf_Const;
  14571. begin
  14572. StartProgram(false);
  14573. Add([
  14574. 'type',
  14575. ' TObject = class',
  14576. ' end;',
  14577. ' TBird = TObject;',
  14578. ' TBirds = class of TBird;',
  14579. ' TEagles = TBirds;',
  14580. ' THawk = class(TBird);',
  14581. 'const',
  14582. ' Hawk: TEagles = THawk;',
  14583. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  14584. ' TBird,',
  14585. ' THawk',
  14586. ' );',
  14587. 'begin']);
  14588. ConvertProgram;
  14589. CheckSource('TestClassOf_Const',
  14590. LinesToStr([ // statements
  14591. 'rtl.createClass($mod, "TObject", null, function () {',
  14592. ' this.$init = function () {',
  14593. ' };',
  14594. ' this.$final = function () {',
  14595. ' };',
  14596. '});',
  14597. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  14598. '});',
  14599. 'this.Hawk = $mod.THawk;',
  14600. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  14601. '']),
  14602. LinesToStr([ // $mod.$main
  14603. '']));
  14604. end;
  14605. procedure TTestModule.TestNestedClass_Alias;
  14606. begin
  14607. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14608. StartProgram(false);
  14609. Add([
  14610. 'type',
  14611. ' TObject = class',
  14612. ' type TNested = type longint;',
  14613. ' end;',
  14614. 'type TAlias = type tobject.tnested;',
  14615. 'var i: tobject.tnested = 3;',
  14616. 'var j: TAlias = 4;',
  14617. 'begin',
  14618. ' if typeinfo(TAlias)=nil then ;',
  14619. ' if typeinfo(tobject.tnested)=nil then ;',
  14620. '']);
  14621. ConvertProgram;
  14622. CheckSource('TestNestedClass_Alias',
  14623. LinesToStr([ // statements
  14624. 'rtl.createClass($mod, "TObject", null, function () {',
  14625. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  14626. ' this.$init = function () {',
  14627. ' };',
  14628. ' this.$final = function () {',
  14629. ' };',
  14630. '});',
  14631. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  14632. 'this.i = 3;',
  14633. 'this.j = 4;',
  14634. '']),
  14635. LinesToStr([ // $mod.$main
  14636. 'if ($mod.$rtti["TAlias"] === null) ;',
  14637. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  14638. '']));
  14639. end;
  14640. procedure TTestModule.TestNestedClass_Record;
  14641. begin
  14642. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14643. StartProgram(false);
  14644. Add([
  14645. 'type',
  14646. ' TObject = class',
  14647. ' type TPoint = record',
  14648. ' x,y: byte;',
  14649. ' end;',
  14650. ' procedure DoIt(t: TPoint);',
  14651. ' end;',
  14652. 'procedure tobject.DoIt(t: TPoint);',
  14653. 'var p: TPoint;',
  14654. 'begin',
  14655. ' t.x:=t.y;',
  14656. ' p:=t;',
  14657. 'end;',
  14658. 'var',
  14659. ' p: tobject.tpoint = (x:2; y:4);',
  14660. ' o: TObject;',
  14661. 'begin',
  14662. ' p:=p;',
  14663. ' o.doit(p);',
  14664. '']);
  14665. ConvertProgram;
  14666. CheckSource('TestNestedClass_Record',
  14667. LinesToStr([ // statements
  14668. 'rtl.createClass($mod, "TObject", null, function () {',
  14669. ' rtl.recNewT(this, "TPoint", function () {',
  14670. ' this.x = 0;',
  14671. ' this.y = 0;',
  14672. ' this.$eq = function (b) {',
  14673. ' return (this.x === b.x) && (this.y === b.y);',
  14674. ' };',
  14675. ' this.$assign = function (s) {',
  14676. ' this.x = s.x;',
  14677. ' this.y = s.y;',
  14678. ' return this;',
  14679. ' };',
  14680. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  14681. ' $r.addField("x", rtl.byte);',
  14682. ' $r.addField("y", rtl.byte);',
  14683. ' });',
  14684. ' this.$init = function () {',
  14685. ' };',
  14686. ' this.$final = function () {',
  14687. ' };',
  14688. ' this.DoIt = function (t) {',
  14689. ' var p = this.TPoint.$new();',
  14690. ' t.x = t.y;',
  14691. ' p.$assign(t);',
  14692. ' };',
  14693. '});',
  14694. 'this.p = $mod.TObject.TPoint.$clone({',
  14695. ' x: 2,',
  14696. ' y: 4',
  14697. '});',
  14698. 'this.o = null;',
  14699. '']),
  14700. LinesToStr([ // $mod.$main
  14701. '$mod.p.$assign($mod.p);',
  14702. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  14703. '']));
  14704. end;
  14705. procedure TTestModule.TestNestedClass_Class;
  14706. begin
  14707. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14708. StartProgram(false);
  14709. Add([
  14710. 'type',
  14711. ' TObject = class end;',
  14712. ' TBird = class',
  14713. ' type TLeg = class',
  14714. ' FId: longint;',
  14715. ' constructor Create;',
  14716. ' function Create(i: longint): TLeg;',
  14717. ' end;',
  14718. ' function DoIt(b: TBird): Tleg;',
  14719. ' end;',
  14720. 'constructor tbird.tleg.create;',
  14721. 'begin',
  14722. ' FId:=3;',
  14723. 'end;',
  14724. 'function tbird.tleg.Create(i: longint): TLeg;',
  14725. 'begin',
  14726. ' Create;',
  14727. ' Result:=TLeg.Create;',
  14728. ' Result:=TBird.TLeg.Create;',
  14729. ' Result:=Create(3);',
  14730. ' FId:=i;',
  14731. 'end;',
  14732. 'function tbird.DoIt(b: tbird): tleg;',
  14733. 'begin',
  14734. ' Result.Create;',
  14735. ' Result:=TLeg.Create;',
  14736. ' Result:=TBird.TLeg.Create;',
  14737. ' Result:=Result.Create(3);',
  14738. 'end;',
  14739. 'var',
  14740. ' b: Tbird.tleg;',
  14741. 'begin',
  14742. ' b.Create;',
  14743. ' b:=TBird.TLeg.Create;',
  14744. ' b:=b.Create(3);',
  14745. '']);
  14746. ConvertProgram;
  14747. CheckSource('TestNestedClass_Class',
  14748. LinesToStr([ // statements
  14749. 'rtl.createClass($mod, "TObject", null, function () {',
  14750. ' this.$init = function () {',
  14751. ' };',
  14752. ' this.$final = function () {',
  14753. ' };',
  14754. '});',
  14755. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14756. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  14757. ' this.$init = function () {',
  14758. ' $mod.TObject.$init.call(this);',
  14759. ' this.FId = 0;',
  14760. ' };',
  14761. ' this.Create = function () {',
  14762. ' this.FId = 3;',
  14763. ' return this;',
  14764. ' };',
  14765. ' this.Create$1 = function (i) {',
  14766. ' var Result = null;',
  14767. ' this.Create();',
  14768. ' Result = $mod.TBird.TLeg.$create("Create");',
  14769. ' Result = $mod.TBird.TLeg.$create("Create");',
  14770. ' Result = this.Create$1(3);',
  14771. ' this.FId = i;',
  14772. ' return Result;',
  14773. ' };',
  14774. ' });',
  14775. ' this.DoIt = function (b) {',
  14776. ' var Result = null;',
  14777. ' Result.Create();',
  14778. ' Result = this.TLeg.$create("Create");',
  14779. ' Result = $mod.TBird.TLeg.$create("Create");',
  14780. ' Result = Result.Create$1(3);',
  14781. ' return Result;',
  14782. ' };',
  14783. '});',
  14784. 'this.b = null;',
  14785. '']),
  14786. LinesToStr([ // $mod.$main
  14787. '$mod.b.Create();',
  14788. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  14789. '$mod.b = $mod.b.Create$1(3);',
  14790. '']));
  14791. end;
  14792. procedure TTestModule.TestExternalClass_Var;
  14793. begin
  14794. StartProgram(false);
  14795. Add([
  14796. '{$modeswitch externalclass}',
  14797. 'type',
  14798. ' TExtA = class external name ''ExtObj''',
  14799. ' Id: longint external name ''$Id'';',
  14800. ' B: longint;',
  14801. ' end;',
  14802. 'var Obj: TExtA;',
  14803. 'begin',
  14804. ' obj.id:=obj.id+1;',
  14805. ' obj.B:=obj.B+1;']);
  14806. ConvertProgram;
  14807. CheckSource('TestExternalClass_Var',
  14808. LinesToStr([ // statements
  14809. 'this.Obj = null;',
  14810. '']),
  14811. LinesToStr([ // $mod.$main
  14812. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  14813. '$mod.Obj.B = $mod.Obj.B + 1;',
  14814. '']));
  14815. end;
  14816. procedure TTestModule.TestExternalClass_Const;
  14817. begin
  14818. StartProgram(false);
  14819. Add([
  14820. '{$modeswitch externalclass}',
  14821. 'type',
  14822. ' TExtA = class external name ''ExtObj''',
  14823. ' const Two: longint = 2;',
  14824. ' const Three = 3;',
  14825. ' const Id: longint;',
  14826. ' end;',
  14827. ' TExtB = class external name ''ExtB''',
  14828. ' A: TExtA;',
  14829. ' end;',
  14830. 'var',
  14831. ' A: texta;',
  14832. ' B: textb;',
  14833. ' i: longint;',
  14834. 'begin',
  14835. ' i:=a.two;',
  14836. ' i:=texta.two;',
  14837. ' i:=a.three;',
  14838. ' i:=texta.three;',
  14839. ' i:=a.id;',
  14840. ' i:=texta.id;',
  14841. '']);
  14842. ConvertProgram;
  14843. CheckSource('TestExternalClass_Const',
  14844. LinesToStr([ // statements
  14845. 'this.A = null;',
  14846. 'this.B = null;',
  14847. 'this.i = 0;',
  14848. '']),
  14849. LinesToStr([ // $mod.$main
  14850. '$mod.i = 2;',
  14851. '$mod.i = 2;',
  14852. '$mod.i = 3;',
  14853. '$mod.i = 3;',
  14854. '$mod.i = $mod.A.Id;',
  14855. '$mod.i = ExtObj.Id;',
  14856. '']));
  14857. end;
  14858. procedure TTestModule.TestExternalClass_Dollar;
  14859. begin
  14860. StartProgram(false);
  14861. Add([
  14862. '{$modeswitch externalclass}',
  14863. 'type',
  14864. ' TExtA = class external name ''$''',
  14865. ' Id: longint external name ''$'';',
  14866. ' function Bla(i: longint): longint; external name ''$'';',
  14867. ' end;',
  14868. 'function dollar(k: longint): longint; external name ''$'';',
  14869. 'var Obj: TExtA;',
  14870. 'begin',
  14871. ' dollar(1);',
  14872. ' obj.id:=obj.id+2;',
  14873. ' obj.Bla(3);',
  14874. '']);
  14875. ConvertProgram;
  14876. CheckSource('TestExternalClass_Dollar',
  14877. LinesToStr([ // statements
  14878. 'this.Obj = null;',
  14879. '']),
  14880. LinesToStr([ // $mod.$main
  14881. '$(1);',
  14882. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  14883. '$mod.Obj.$(3);',
  14884. '']));
  14885. end;
  14886. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  14887. begin
  14888. StartProgram(false);
  14889. Add('{$modeswitch externalclass}');
  14890. Add('type');
  14891. Add(' TExtA = class external name ''ExtA''');
  14892. Add(' Id: longint external name ''$Id'';');
  14893. Add(' end;');
  14894. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  14895. Add(' Id: longint;');
  14896. Add(' end;');
  14897. Add('begin');
  14898. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  14899. ConvertProgram;
  14900. end;
  14901. procedure TTestModule.TestExternalClass_Method;
  14902. begin
  14903. StartProgram(false);
  14904. Add('{$modeswitch externalclass}');
  14905. Add('type');
  14906. Add(' TExtA = class external name ''ExtObj''');
  14907. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  14908. Add(' procedure DoSome(Id: longint = 1);');
  14909. Add(' end;');
  14910. Add('var Obj: texta;');
  14911. Add('begin');
  14912. Add(' obj.doit;');
  14913. Add(' obj.doit();');
  14914. Add(' obj.doit(2);');
  14915. Add(' with obj do begin');
  14916. Add(' doit;');
  14917. Add(' doit();');
  14918. Add(' doit(3);');
  14919. Add(' end;');
  14920. ConvertProgram;
  14921. CheckSource('TestExternalClass_Method',
  14922. LinesToStr([ // statements
  14923. 'this.Obj = null;',
  14924. '']),
  14925. LinesToStr([ // $mod.$main
  14926. '$mod.Obj.$Execute(1);',
  14927. '$mod.Obj.$Execute(1);',
  14928. '$mod.Obj.$Execute(2);',
  14929. 'var $with1 = $mod.Obj;',
  14930. '$with1.$Execute(1);',
  14931. '$with1.$Execute(1);',
  14932. '$with1.$Execute(3);',
  14933. '']));
  14934. end;
  14935. procedure TTestModule.TestExternalClass_ClassMethod;
  14936. begin
  14937. StartProgram(false);
  14938. Add([
  14939. '{$modeswitch externalclass}',
  14940. 'type',
  14941. ' TExtA = class external name ''ExtObj''',
  14942. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  14943. ' end;',
  14944. ' TExtB = TExtA;',
  14945. 'begin',
  14946. ' texta.doit;',
  14947. ' texta.doit();',
  14948. ' texta.doit(2);',
  14949. ' with texta do begin',
  14950. ' doit;',
  14951. ' doit();',
  14952. ' doit(3);',
  14953. ' end;',
  14954. ' textb.doit;',
  14955. ' textb.doit();',
  14956. ' textb.doit(4);',
  14957. ' with textb do begin',
  14958. ' doit;',
  14959. ' doit();',
  14960. ' doit(5);',
  14961. ' end;',
  14962. '']);
  14963. ConvertProgram;
  14964. CheckSource('TestExternalClass_ClassMethod',
  14965. LinesToStr([ // statements
  14966. '']),
  14967. LinesToStr([ // $mod.$main
  14968. 'ExtObj.$Execute(1);',
  14969. 'ExtObj.$Execute(1);',
  14970. 'ExtObj.$Execute(2);',
  14971. 'ExtObj.$Execute(1);',
  14972. 'ExtObj.$Execute(1);',
  14973. 'ExtObj.$Execute(3);',
  14974. 'ExtObj.$Execute(1);',
  14975. 'ExtObj.$Execute(1);',
  14976. 'ExtObj.$Execute(4);',
  14977. 'ExtObj.$Execute(1);',
  14978. 'ExtObj.$Execute(1);',
  14979. 'ExtObj.$Execute(5);',
  14980. '']));
  14981. end;
  14982. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  14983. begin
  14984. StartProgram(false);
  14985. Add([
  14986. '{$modeswitch externalclass}',
  14987. 'type',
  14988. ' TBird = class external name ''Array''',
  14989. ' end;',
  14990. 'function GetPtr: Pointer;',
  14991. 'begin',
  14992. 'end;',
  14993. 'procedure Write(const p);',
  14994. 'begin',
  14995. 'end;',
  14996. 'procedure WriteLn; varargs;',
  14997. 'begin',
  14998. 'end;',
  14999. 'begin',
  15000. ' if TBird(GetPtr)=nil then ;',
  15001. ' Write(GetPtr);',
  15002. ' WriteLn(GetPtr);',
  15003. ' Write(TBird(GetPtr));',
  15004. ' WriteLn(TBird(GetPtr));',
  15005. '']);
  15006. ConvertProgram;
  15007. CheckSource('TestFunctionResultInTypeCast',
  15008. LinesToStr([ // statements
  15009. 'this.GetPtr = function () {',
  15010. ' var Result = null;',
  15011. ' return Result;',
  15012. '};',
  15013. 'this.Write = function (p) {',
  15014. '};',
  15015. 'this.WriteLn = function () {',
  15016. '};',
  15017. '']),
  15018. LinesToStr([
  15019. 'if ($mod.GetPtr() === null) ;',
  15020. '$mod.Write($mod.GetPtr());',
  15021. '$mod.WriteLn($mod.GetPtr());',
  15022. '$mod.Write($mod.GetPtr());',
  15023. '$mod.WriteLn($mod.GetPtr());',
  15024. '']));
  15025. end;
  15026. procedure TTestModule.TestExternalClass_NonExternalOverride;
  15027. begin
  15028. StartProgram(false);
  15029. Add('{$modeswitch externalclass}');
  15030. Add('type');
  15031. Add(' TExtA = class external name ''ExtObjA''');
  15032. Add(' procedure ProcA; virtual;');
  15033. Add(' procedure ProcB; virtual;');
  15034. Add(' end;');
  15035. Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
  15036. Add(' end;');
  15037. Add(' TExtC = class (TExtB)');
  15038. Add(' procedure ProcA; override;');
  15039. Add(' end;');
  15040. Add('procedure TExtC.ProcA;');
  15041. Add('begin');
  15042. Add(' ProcA;');
  15043. Add(' Self.ProcA;');
  15044. Add(' ProcB;');
  15045. Add(' Self.ProcB;');
  15046. Add('end;');
  15047. Add('var');
  15048. Add(' A: texta;');
  15049. Add(' B: textb;');
  15050. Add(' C: textc;');
  15051. Add('begin');
  15052. Add(' a.proca;');
  15053. Add(' b.proca;');
  15054. Add(' c.proca;');
  15055. ConvertProgram;
  15056. CheckSource('TestExternalClass_NonExternalOverride',
  15057. LinesToStr([ // statements
  15058. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  15059. ' this.$init = function () {',
  15060. ' };',
  15061. ' this.$final = function () {',
  15062. ' };',
  15063. ' this.ProcA = function () {',
  15064. ' this.ProcA();',
  15065. ' this.ProcA();',
  15066. ' this.ProcB();',
  15067. ' this.ProcB();',
  15068. ' };',
  15069. '});',
  15070. 'this.A = null;',
  15071. 'this.B = null;',
  15072. 'this.C = null;',
  15073. '']),
  15074. LinesToStr([ // $mod.$main
  15075. '$mod.A.ProcA();',
  15076. '$mod.B.ProcA();',
  15077. '$mod.C.ProcA();',
  15078. '']));
  15079. end;
  15080. procedure TTestModule.TestExternalClass_OverloadHint;
  15081. begin
  15082. StartProgram(false);
  15083. Add([
  15084. '{$modeswitch externalclass}',
  15085. 'type',
  15086. ' TExtA = class external name ''ExtObjA''',
  15087. ' procedure DoIt;',
  15088. ' procedure DoIt(i: longint);',
  15089. ' end;',
  15090. 'begin',
  15091. '']);
  15092. ConvertProgram;
  15093. CheckResolverUnexpectedHints(true);
  15094. CheckSource('TestExternalClass_OverloadHint',
  15095. LinesToStr([ // statements
  15096. '']),
  15097. LinesToStr([ // $mod.$main
  15098. '']));
  15099. end;
  15100. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  15101. begin
  15102. StartProgram(false);
  15103. Add([
  15104. '{$modeswitch externalclass}',
  15105. 'type',
  15106. ' JSwiper = class external name ''Swiper''',
  15107. ' constructor New;',
  15108. ' end;',
  15109. ' TObject = class',
  15110. ' private',
  15111. ' FSwiper: JSwiper;',
  15112. ' published',
  15113. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  15114. ' end;',
  15115. 'begin',
  15116. ' JSwiper.new;',
  15117. '']);
  15118. ConvertProgram;
  15119. CheckSource('TestExternalClass_SameNamePublishedProperty',
  15120. LinesToStr([ // statements
  15121. 'rtl.createClass($mod, "TObject", null, function () {',
  15122. ' this.$init = function () {',
  15123. ' this.FSwiper = null;',
  15124. ' };',
  15125. ' this.$final = function () {',
  15126. ' this.FSwiper = undefined;',
  15127. ' };',
  15128. ' var $r = this.$rtti;',
  15129. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  15130. '});',
  15131. '']),
  15132. LinesToStr([ // $mod.$main
  15133. 'new Swiper();',
  15134. '']));
  15135. end;
  15136. procedure TTestModule.TestExternalClass_Property;
  15137. begin
  15138. StartProgram(false);
  15139. Add([
  15140. '{$modeswitch externalclass}',
  15141. 'type',
  15142. ' TExtA = class external name ''ExtA''',
  15143. ' function getYear: longint;',
  15144. ' procedure setYear(Value: longint);',
  15145. ' property Year: longint read getyear write setyear;',
  15146. ' end;',
  15147. ' TExtB = class (TExtA)',
  15148. ' procedure OtherSetYear(Value: longint);',
  15149. ' property year write othersetyear;',
  15150. ' end;',
  15151. 'procedure textb.othersetyear(value: longint);',
  15152. 'begin',
  15153. ' setYear(Value+4);',
  15154. 'end;',
  15155. 'var',
  15156. ' A: texta;',
  15157. ' B: textb;',
  15158. 'begin',
  15159. ' a.year:=a.year+1;',
  15160. ' b.year:=b.year+2;']);
  15161. ConvertProgram;
  15162. CheckSource('TestExternalClass_NonExternalOverride',
  15163. LinesToStr([ // statements
  15164. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15165. ' this.$init = function () {',
  15166. ' };',
  15167. ' this.$final = function () {',
  15168. ' };',
  15169. ' this.OtherSetYear = function (Value) {',
  15170. ' this.setYear(Value+4);',
  15171. ' };',
  15172. '});',
  15173. 'this.A = null;',
  15174. 'this.B = null;',
  15175. '']),
  15176. LinesToStr([ // $mod.$main
  15177. '$mod.A.setYear($mod.A.getYear()+1);',
  15178. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  15179. '']));
  15180. end;
  15181. procedure TTestModule.TestExternalClass_PropertyDate;
  15182. begin
  15183. StartProgram(false);
  15184. Add([
  15185. '{$modeswitch externalclass}',
  15186. 'type',
  15187. ' TExtA = class external name ''ExtA''',
  15188. ' end;',
  15189. ' TExtB = class (TExtA)',
  15190. ' FDate: string;',
  15191. ' property Date: string read FDate write FDate;',
  15192. ' property ExtA: string read FDate write FDate;',
  15193. ' end;',
  15194. ' {$M+}',
  15195. ' TObject = class',
  15196. ' FDate: string;',
  15197. ' published',
  15198. ' property Date: string read FDate write FDate;',
  15199. ' property ExtA: string read FDate write FDate;',
  15200. ' end;',
  15201. 'var',
  15202. ' B: textb;',
  15203. ' o: TObject;',
  15204. 'begin',
  15205. ' b.date:=b.exta;',
  15206. ' o.date:=o.exta;']);
  15207. ConvertProgram;
  15208. CheckSource('TestExternalClass_PropertyDate',
  15209. LinesToStr([ // statements
  15210. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15211. ' this.$init = function () {',
  15212. ' this.FDate = "";',
  15213. ' };',
  15214. ' this.$final = function () {',
  15215. ' };',
  15216. '});',
  15217. 'rtl.createClass($mod, "TObject", null, function () {',
  15218. ' this.$init = function () {',
  15219. ' this.FDate = "";',
  15220. ' };',
  15221. ' this.$final = function () {',
  15222. ' };',
  15223. ' var $r = this.$rtti;',
  15224. ' $r.addField("FDate", rtl.string);',
  15225. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  15226. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  15227. '});',
  15228. 'this.B = null;',
  15229. 'this.o = null;',
  15230. '']),
  15231. LinesToStr([ // $mod.$main
  15232. '$mod.B.FDate = $mod.B.FDate;',
  15233. '$mod.o.FDate = $mod.o.FDate;',
  15234. '']));
  15235. end;
  15236. procedure TTestModule.TestExternalClass_ClassProperty;
  15237. begin
  15238. StartProgram(false);
  15239. Add('{$modeswitch externalclass}');
  15240. Add('type');
  15241. Add(' TExtA = class external name ''ExtA''');
  15242. Add(' class function getYear: longint;');
  15243. Add(' class procedure setYear(Value: longint);');
  15244. Add(' class property Year: longint read getyear write setyear;');
  15245. Add(' end;');
  15246. Add(' TExtB = class (TExtA)');
  15247. Add(' class function GetCentury: longint;');
  15248. Add(' class procedure SetCentury(Value: longint);');
  15249. Add(' class property Century: longint read getcentury write setcentury;');
  15250. Add(' end;');
  15251. Add('class function textb.getcentury: longint;');
  15252. Add('begin');
  15253. Add('end;');
  15254. Add('class procedure textb.setcentury(value: longint);');
  15255. Add('begin');
  15256. Add(' setyear(value+11);');
  15257. Add(' texta.year:=texta.year+12;');
  15258. Add(' year:=year+13;');
  15259. Add(' textb.century:=textb.century+14;');
  15260. Add(' century:=century+15;');
  15261. Add('end;');
  15262. Add('var');
  15263. Add(' A: texta;');
  15264. Add(' B: textb;');
  15265. Add('begin');
  15266. Add(' texta.year:=texta.year+1;');
  15267. Add(' textb.year:=textb.year+2;');
  15268. Add(' TextA.year:=TextA.year+3;');
  15269. Add(' b.year:=b.year+4;');
  15270. Add(' textb.century:=textb.century+5;');
  15271. Add(' b.century:=b.century+6;');
  15272. ConvertProgram;
  15273. CheckSource('TestExternalClass_ClassProperty',
  15274. LinesToStr([ // statements
  15275. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15276. ' this.$init = function () {',
  15277. ' };',
  15278. ' this.$final = function () {',
  15279. ' };',
  15280. ' this.GetCentury = function () {',
  15281. ' var Result = 0;',
  15282. ' return Result;',
  15283. ' };',
  15284. ' this.SetCentury = function (Value) {',
  15285. ' this.setYear(Value + 11);',
  15286. ' ExtA.setYear(ExtA.getYear() + 12);',
  15287. ' this.setYear(this.getYear() + 13);',
  15288. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  15289. ' this.SetCentury(this.GetCentury() + 15);',
  15290. ' };',
  15291. '});',
  15292. 'this.A = null;',
  15293. 'this.B = null;',
  15294. '']),
  15295. LinesToStr([ // $mod.$main
  15296. 'ExtA.setYear(ExtA.getYear() + 1);',
  15297. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  15298. 'ExtA.setYear(ExtA.getYear() + 3);',
  15299. '$mod.B.setYear($mod.B.getYear() + 4);',
  15300. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  15301. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  15302. '']));
  15303. end;
  15304. procedure TTestModule.TestExternalClass_ClassOf;
  15305. begin
  15306. StartProgram(false);
  15307. Add('{$modeswitch externalclass}');
  15308. Add('type');
  15309. Add(' TExtA = class external name ''ExtA''');
  15310. Add(' procedure ProcA; virtual;');
  15311. Add(' procedure ProcB; virtual;');
  15312. Add(' end;');
  15313. Add(' TExtAClass = class of TExtA;');
  15314. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15315. Add(' end;');
  15316. Add(' TExtBClass = class of TExtB;');
  15317. Add(' TExtC = class (TExtB)');
  15318. Add(' procedure ProcA; override;');
  15319. Add(' end;');
  15320. Add(' TExtCClass = class of TExtC;');
  15321. Add('procedure TExtC.ProcA; begin end;');
  15322. Add('var');
  15323. Add(' A: texta; ClA: TExtAClass;');
  15324. Add(' B: textb; ClB: TExtBClass;');
  15325. Add(' C: textc; ClC: TExtCClass;');
  15326. Add('begin');
  15327. Add(' ClA:=texta;');
  15328. Add(' ClA:=textb;');
  15329. Add(' ClA:=textc;');
  15330. Add(' ClB:=textb;');
  15331. Add(' ClB:=textc;');
  15332. Add(' ClC:=textc;');
  15333. ConvertProgram;
  15334. CheckSource('TestExternalClass_ClassOf',
  15335. LinesToStr([ // statements
  15336. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15337. ' this.$init = function () {',
  15338. ' };',
  15339. ' this.$final = function () {',
  15340. ' };',
  15341. ' this.ProcA = function () {',
  15342. ' };',
  15343. '});',
  15344. 'this.A = null;',
  15345. 'this.ClA = null;',
  15346. 'this.B = null;',
  15347. 'this.ClB = null;',
  15348. 'this.C = null;',
  15349. 'this.ClC = null;',
  15350. '']),
  15351. LinesToStr([ // $mod.$main
  15352. '$mod.ClA = ExtA;',
  15353. '$mod.ClA = ExtB;',
  15354. '$mod.ClA = $mod.TExtC;',
  15355. '$mod.ClB = ExtB;',
  15356. '$mod.ClB = $mod.TExtC;',
  15357. '$mod.ClC = $mod.TExtC;',
  15358. '']));
  15359. end;
  15360. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  15361. begin
  15362. AddModuleWithIntfImplSrc('unit2.pas',
  15363. LinesToStr([
  15364. '{$modeswitch externalclass}',
  15365. 'type',
  15366. ' TExtA = class external name ''ExtA''',
  15367. ' class var Id: longint;',
  15368. ' end;',
  15369. '']),
  15370. '');
  15371. StartUnit(true);
  15372. Add('interface');
  15373. Add('uses unit2;');
  15374. Add('implementation');
  15375. Add('begin');
  15376. Add(' unit2.texta.id:=unit2.texta.id+1;');
  15377. ConvertUnit;
  15378. CheckSource('TestExternalClass_ClassOtherUnit',
  15379. LinesToStr([
  15380. '']),
  15381. LinesToStr([
  15382. 'ExtA.Id = ExtA.Id + 1;',
  15383. '']));
  15384. end;
  15385. procedure TTestModule.TestExternalClass_Is;
  15386. begin
  15387. StartProgram(false);
  15388. Add('{$modeswitch externalclass}');
  15389. Add('type');
  15390. Add(' TExtA = class external name ''ExtA''');
  15391. Add(' end;');
  15392. Add(' TExtAClass = class of TExtA;');
  15393. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15394. Add(' end;');
  15395. Add(' TExtBClass = class of TExtB;');
  15396. Add(' TExtC = class (TExtB)');
  15397. Add(' end;');
  15398. Add(' TExtCClass = class of TExtC;');
  15399. Add('var');
  15400. Add(' A: texta; ClA: TExtAClass;');
  15401. Add(' B: textb; ClB: TExtBClass;');
  15402. Add(' C: textc; ClC: TExtCClass;');
  15403. Add('begin');
  15404. Add(' if a is textb then ;');
  15405. Add(' if a is textc then ;');
  15406. Add(' if b is textc then ;');
  15407. Add(' if cla is textb then ;');
  15408. Add(' if cla is textc then ;');
  15409. Add(' if clb is textc then ;');
  15410. ConvertProgram;
  15411. CheckSource('TestExternalClass_Is',
  15412. LinesToStr([ // statements
  15413. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15414. ' this.$init = function () {',
  15415. ' };',
  15416. ' this.$final = function () {',
  15417. ' };',
  15418. '});',
  15419. 'this.A = null;',
  15420. 'this.ClA = null;',
  15421. 'this.B = null;',
  15422. 'this.ClB = null;',
  15423. 'this.C = null;',
  15424. 'this.ClC = null;',
  15425. '']),
  15426. LinesToStr([ // $mod.$main
  15427. 'if (rtl.isExt($mod.A, ExtB)) ;',
  15428. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  15429. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  15430. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  15431. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  15432. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  15433. '']));
  15434. end;
  15435. procedure TTestModule.TestExternalClass_As;
  15436. begin
  15437. StartProgram(false);
  15438. Add('{$modeswitch externalclass}');
  15439. Add('type');
  15440. Add(' TExtA = class external name ''ExtA''');
  15441. Add(' end;');
  15442. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15443. Add(' end;');
  15444. Add(' TExtC = class (TExtB)');
  15445. Add(' end;');
  15446. Add('var');
  15447. Add(' A: texta;');
  15448. Add(' B: textb;');
  15449. Add(' C: textc;');
  15450. Add('begin');
  15451. Add(' b:=a as textb;');
  15452. Add(' c:=a as textc;');
  15453. Add(' c:=b as textc;');
  15454. ConvertProgram;
  15455. CheckSource('TestExternalClass_Is',
  15456. LinesToStr([ // statements
  15457. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15458. ' this.$init = function () {',
  15459. ' };',
  15460. ' this.$final = function () {',
  15461. ' };',
  15462. '});',
  15463. 'this.A = null;',
  15464. 'this.B = null;',
  15465. 'this.C = null;',
  15466. '']),
  15467. LinesToStr([ // $mod.$main
  15468. '$mod.B = rtl.asExt($mod.A, ExtB);',
  15469. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  15470. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  15471. '']));
  15472. end;
  15473. procedure TTestModule.TestExternalClass_DestructorFail;
  15474. begin
  15475. StartProgram(false);
  15476. Add('{$modeswitch externalclass}');
  15477. Add('type');
  15478. Add(' TExtA = class external name ''ExtA''');
  15479. Add(' destructor Free;');
  15480. Add(' end;');
  15481. SetExpectedPasResolverError('Pascal element not supported: destructor',
  15482. nPasElementNotSupported);
  15483. ConvertProgram;
  15484. end;
  15485. procedure TTestModule.TestExternalClass_New;
  15486. begin
  15487. StartProgram(false);
  15488. Add('{$modeswitch externalclass}');
  15489. Add('type');
  15490. Add(' TExtA = class external name ''ExtA''');
  15491. Add(' constructor New;');
  15492. Add(' constructor New(i: longint; j: longint = 2);');
  15493. Add(' end;');
  15494. Add('var');
  15495. Add(' A: texta;');
  15496. Add('begin');
  15497. Add(' a:=texta.new;');
  15498. Add(' a:=texta.new();');
  15499. Add(' a:=texta.new(1);');
  15500. Add(' with texta do begin');
  15501. Add(' a:=new;');
  15502. Add(' a:=new();');
  15503. Add(' a:=new(2);');
  15504. Add(' end;');
  15505. Add(' a:=test1.texta.new;');
  15506. Add(' a:=test1.texta.new();');
  15507. Add(' a:=test1.texta.new(3);');
  15508. ConvertProgram;
  15509. CheckSource('TestExternalClass_New',
  15510. LinesToStr([ // statements
  15511. 'this.A = null;',
  15512. '']),
  15513. LinesToStr([ // $mod.$main
  15514. '$mod.A = new ExtA();',
  15515. '$mod.A = new ExtA();',
  15516. '$mod.A = new ExtA(1,2);',
  15517. '$mod.A = new ExtA();',
  15518. '$mod.A = new ExtA();',
  15519. '$mod.A = new ExtA(2,2);',
  15520. '$mod.A = new ExtA();',
  15521. '$mod.A = new ExtA();',
  15522. '$mod.A = new ExtA(3,2);',
  15523. '']));
  15524. end;
  15525. procedure TTestModule.TestExternalClass_ClassOf_New;
  15526. begin
  15527. StartProgram(false);
  15528. Add('{$modeswitch externalclass}');
  15529. Add('type');
  15530. Add(' TExtAClass = class of TExtA;');
  15531. Add(' TExtA = class external name ''ExtA''');
  15532. Add(' C: TExtAClass;');
  15533. Add(' constructor New;');
  15534. Add(' end;');
  15535. Add('var');
  15536. Add(' A: texta;');
  15537. Add(' C: textaclass;');
  15538. Add('begin');
  15539. Add(' a:=c.new;');
  15540. Add(' a:=c.new();');
  15541. Add(' with C do begin');
  15542. Add(' a:=new;');
  15543. Add(' a:=new();');
  15544. Add(' end;');
  15545. Add(' a:=test1.c.new;');
  15546. Add(' a:=test1.c.new();');
  15547. Add(' a:=A.c.new();');
  15548. ConvertProgram;
  15549. CheckSource('TestExternalClass_ClassOf_New',
  15550. LinesToStr([ // statements
  15551. 'this.A = null;',
  15552. 'this.C = null;',
  15553. '']),
  15554. LinesToStr([ // $mod.$main
  15555. '$mod.A = new $mod.C();',
  15556. '$mod.A = new $mod.C();',
  15557. 'var $with1 = $mod.C;',
  15558. '$mod.A = new $with1();',
  15559. '$mod.A = new $with1();',
  15560. '$mod.A = new $mod.C();',
  15561. '$mod.A = new $mod.C();',
  15562. '$mod.A = new $mod.A.C();',
  15563. '']));
  15564. end;
  15565. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  15566. begin
  15567. StartProgram(false);
  15568. Add([
  15569. '{$modeswitch externalclass}',
  15570. 'type',
  15571. ' TExtAClass = class of TExtA;',
  15572. ' TExtA = class external name ''ExtA''',
  15573. ' constructor New;',
  15574. ' end;',
  15575. 'function GetCreator: TExtAClass;',
  15576. 'begin',
  15577. ' Result:=TExtA;',
  15578. 'end;',
  15579. 'var',
  15580. ' A: texta;',
  15581. 'begin',
  15582. ' a:=getcreator.new;',
  15583. ' a:=getcreator().new;',
  15584. ' a:=getcreator().new();',
  15585. ' a:=getcreator.new();',
  15586. ' with getcreator do begin',
  15587. ' a:=new;',
  15588. ' a:=new();',
  15589. ' end;']);
  15590. ConvertProgram;
  15591. CheckSource('TestExternalClass_FuncClassOf_New',
  15592. LinesToStr([ // statements
  15593. 'this.GetCreator = function () {',
  15594. ' var Result = null;',
  15595. ' Result = ExtA;',
  15596. ' return Result;',
  15597. '};',
  15598. 'this.A = null;',
  15599. '']),
  15600. LinesToStr([ // $mod.$main
  15601. '$mod.A = new ($mod.GetCreator())();',
  15602. '$mod.A = new ($mod.GetCreator())();',
  15603. '$mod.A = new ($mod.GetCreator())();',
  15604. '$mod.A = new ($mod.GetCreator())();',
  15605. 'var $with1 = $mod.GetCreator();',
  15606. '$mod.A = new $with1();',
  15607. '$mod.A = new $with1();',
  15608. '']));
  15609. end;
  15610. procedure TTestModule.TestExternalClass_New_PasClassFail;
  15611. begin
  15612. StartProgram(false);
  15613. Add([
  15614. '{$modeswitch externalclass}',
  15615. 'type',
  15616. ' TExtA = class external name ''ExtA''',
  15617. ' constructor New;',
  15618. ' end;',
  15619. ' TBird = class(TExtA)',
  15620. ' end;',
  15621. 'begin',
  15622. ' TBird.new;',
  15623. '']);
  15624. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15625. ConvertProgram;
  15626. end;
  15627. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  15628. begin
  15629. StartProgram(false);
  15630. Add([
  15631. '{$modeswitch externalclass}',
  15632. 'type',
  15633. ' TExtA = class external name ''ExtA''',
  15634. ' constructor New;',
  15635. ' end;',
  15636. ' TBird = class(TExtA)',
  15637. ' end;',
  15638. 'begin',
  15639. ' TBird.new();',
  15640. '']);
  15641. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15642. ConvertProgram;
  15643. end;
  15644. procedure TTestModule.TestExternalClass_LocalConstSameName;
  15645. begin
  15646. StartProgram(false);
  15647. Add('{$modeswitch externalclass}');
  15648. Add('type');
  15649. Add(' TExtA = class external name ''ExtA''');
  15650. Add(' constructor New;');
  15651. Add(' end;');
  15652. Add('function DoIt: longint;');
  15653. Add('const ExtA: longint = 3;');
  15654. Add('begin');
  15655. Add(' Result:=ExtA;');
  15656. Add('end;');
  15657. Add('var');
  15658. Add(' A: texta;');
  15659. Add('begin');
  15660. Add(' a:=texta.new;');
  15661. ConvertProgram;
  15662. CheckSource('TestExternalClass_LocalConstSameName',
  15663. LinesToStr([ // statements
  15664. 'var ExtA$1 = 3;',
  15665. 'this.DoIt = function () {',
  15666. ' var Result = 0;',
  15667. ' Result = ExtA$1;',
  15668. ' return Result;',
  15669. '};',
  15670. 'this.A = null;',
  15671. '']),
  15672. LinesToStr([ // $mod.$main
  15673. '$mod.A = new ExtA();',
  15674. '']));
  15675. end;
  15676. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  15677. begin
  15678. StartProgram(false);
  15679. Add('{$modeswitch externalclass}');
  15680. Add('type');
  15681. Add(' TExtA = class external name ''ExtA''');
  15682. Add(' procedure DoIt;');
  15683. Add(' end;');
  15684. Add(' TMyA = class(TExtA)');
  15685. Add(' procedure DoIt;');
  15686. Add(' end;');
  15687. Add('procedure TMyA.DoIt; begin end;');
  15688. Add('begin');
  15689. ConvertProgram;
  15690. CheckSource('TestExternalClass_ReintroduceOverload',
  15691. LinesToStr([ // statements
  15692. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  15693. ' this.$init = function () {',
  15694. ' };',
  15695. ' this.$final = function () {',
  15696. ' };',
  15697. ' this.DoIt$1 = function () {',
  15698. ' };',
  15699. '});',
  15700. '']),
  15701. LinesToStr([ // $mod.$main
  15702. '']));
  15703. end;
  15704. procedure TTestModule.TestExternalClass_Inherited;
  15705. begin
  15706. StartProgram(false);
  15707. Add('{$modeswitch externalclass}');
  15708. Add('type');
  15709. Add(' TExtA = class external name ''ExtA''');
  15710. Add(' procedure DoIt(i: longint = 1); virtual;');
  15711. Add(' procedure DoSome(j: longint = 2);');
  15712. Add(' end;');
  15713. Add(' TExtB = class external name ''ExtB''(TExtA)');
  15714. Add(' end;');
  15715. Add(' TMyC = class(TExtB)');
  15716. Add(' procedure DoIt(i: longint = 1); override;');
  15717. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  15718. Add(' end;');
  15719. Add('procedure TMyC.DoIt(i: longint);');
  15720. Add('begin');
  15721. Add(' inherited;');
  15722. Add(' inherited DoIt;');
  15723. Add(' inherited DoIt();');
  15724. Add(' inherited DoIt(3);');
  15725. Add(' inherited DoSome;');
  15726. Add(' inherited DoSome();');
  15727. Add(' inherited DoSome(4);');
  15728. Add('end;');
  15729. Add('procedure TMyC.DoSome(j: longint);');
  15730. Add('begin');
  15731. Add(' inherited;');
  15732. Add('end;');
  15733. Add('begin');
  15734. ConvertProgram;
  15735. CheckSource('TestExternalClass_ReintroduceOverload',
  15736. LinesToStr([ // statements
  15737. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  15738. ' this.$init = function () {',
  15739. ' };',
  15740. ' this.$final = function () {',
  15741. ' };',
  15742. ' this.DoIt = function (i) {',
  15743. ' ExtB.DoIt.apply(this, arguments);',
  15744. ' ExtB.DoIt.call(this, 1);',
  15745. ' ExtB.DoIt.call(this, 1);',
  15746. ' ExtB.DoIt.call(this, 3);',
  15747. ' ExtB.DoSome.call(this, 2);',
  15748. ' ExtB.DoSome.call(this, 2);',
  15749. ' ExtB.DoSome.call(this, 4);',
  15750. ' };',
  15751. ' this.DoSome$1 = function (j) {',
  15752. ' ExtB.DoSome.apply(this, arguments);',
  15753. ' };',
  15754. '});',
  15755. '']),
  15756. LinesToStr([ // $mod.$main
  15757. '']));
  15758. end;
  15759. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  15760. begin
  15761. StartProgram(false);
  15762. Add('{$modeswitch externalclass}');
  15763. Add('type');
  15764. Add(' TObject = class');
  15765. Add(' end;');
  15766. Add(' TExtA = class external name ''ExtA''(TObject)');
  15767. Add(' end;');
  15768. Add('begin');
  15769. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  15770. ConvertProgram;
  15771. end;
  15772. procedure TTestModule.TestExternalClass_NewInstance;
  15773. begin
  15774. StartProgram(false);
  15775. Add('{$modeswitch externalclass}');
  15776. Add('type');
  15777. Add(' TExtA = class external name ''ExtA''');
  15778. Add(' end;');
  15779. Add(' TMyB = class(TExtA)');
  15780. Add(' protected');
  15781. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  15782. Add(' end;');
  15783. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15784. Add('begin end;');
  15785. Add('begin');
  15786. ConvertProgram;
  15787. CheckSource('TestExternalClass_NewInstance',
  15788. LinesToStr([ // statements
  15789. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  15790. ' this.$init = function () {',
  15791. ' };',
  15792. ' this.$final = function () {',
  15793. ' };',
  15794. ' this.NewInstance = function (fnname, paramarray) {',
  15795. ' var Result = null;',
  15796. ' return Result;',
  15797. ' };',
  15798. '});',
  15799. '']),
  15800. LinesToStr([ // $mod.$main
  15801. '']));
  15802. end;
  15803. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  15804. begin
  15805. StartProgram(false);
  15806. Add('{$modeswitch externalclass}');
  15807. Add('type');
  15808. Add(' TExtA = class external name ''ExtA''');
  15809. Add(' end;');
  15810. Add(' TMyB = class(TExtA)');
  15811. Add(' protected');
  15812. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  15813. Add(' end;');
  15814. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15815. Add('begin end;');
  15816. Add('begin');
  15817. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  15818. ConvertProgram;
  15819. end;
  15820. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  15821. begin
  15822. StartProgram(false);
  15823. Add('{$modeswitch externalclass}');
  15824. Add('type');
  15825. Add(' TExtA = class external name ''ExtA''');
  15826. Add(' end;');
  15827. Add(' TMyB = class(TExtA)');
  15828. Add(' protected');
  15829. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  15830. Add(' end;');
  15831. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  15832. Add('begin end;');
  15833. Add('begin');
  15834. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  15835. nIncompatibleTypeArgNo);
  15836. ConvertProgram;
  15837. end;
  15838. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  15839. begin
  15840. StartProgram(false);
  15841. Add('{$modeswitch externalclass}');
  15842. Add('type');
  15843. Add(' TExtA = class external name ''ExtA''');
  15844. Add(' end;');
  15845. Add(' TMyB = class(TExtA)');
  15846. Add(' protected');
  15847. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  15848. Add(' end;');
  15849. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  15850. Add('begin end;');
  15851. Add('begin');
  15852. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  15853. nIncompatibleTypeArgNo);
  15854. ConvertProgram;
  15855. end;
  15856. procedure TTestModule.TestExternalClass_PascalProperty;
  15857. begin
  15858. StartProgram(false);
  15859. Add('{$modeswitch externalclass}');
  15860. Add('type');
  15861. Add(' TJSElement = class;');
  15862. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  15863. Add(' TJSElement = class external name ''ExtA''');
  15864. Add(' end;');
  15865. Add(' TControl = class(TJSElement)');
  15866. Add(' private');
  15867. Add(' FOnClick: TJSNotifyEvent;');
  15868. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  15869. Add(' procedure Click(Sender: TJSElement);');
  15870. Add(' end;');
  15871. Add('procedure TControl.Click(Sender: TJSElement);');
  15872. Add('begin');
  15873. Add(' OnClick(Self);');
  15874. Add('end;');
  15875. Add('var');
  15876. Add(' Ctrl: TControl;');
  15877. Add('begin');
  15878. Add(' Ctrl.OnClick:[email protected];');
  15879. Add(' Ctrl.OnClick(Ctrl);');
  15880. ConvertProgram;
  15881. CheckSource('TestExternalClass_PascalProperty',
  15882. LinesToStr([ // statements
  15883. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  15884. ' this.$init = function () {',
  15885. ' this.FOnClick = null;',
  15886. ' };',
  15887. ' this.$final = function () {',
  15888. ' this.FOnClick = undefined;',
  15889. ' };',
  15890. ' this.Click = function (Sender) {',
  15891. ' this.FOnClick(this);',
  15892. ' };',
  15893. '});',
  15894. 'this.Ctrl = null;',
  15895. '']),
  15896. LinesToStr([ // $mod.$main
  15897. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  15898. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  15899. '']));
  15900. end;
  15901. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  15902. begin
  15903. StartProgram(false);
  15904. Add([
  15905. '{$modeswitch externalclass}',
  15906. 'type',
  15907. ' IUnknown = interface end;',
  15908. ' TObject = class',
  15909. ' end;',
  15910. ' TChild = class',
  15911. ' end;',
  15912. ' TExtRootA = class external name ''ExtRootA''',
  15913. ' end;',
  15914. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  15915. ' end;',
  15916. ' TExtRootB = class external name ''ExtRootB''',
  15917. ' end;',
  15918. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  15919. ' end;',
  15920. 'var',
  15921. ' Obj: TObject;',
  15922. ' Child: TChild;',
  15923. ' RootA: TExtRootA;',
  15924. ' ChildA: TExtChildA;',
  15925. ' RootB: TExtRootB;',
  15926. ' ChildB: TExtChildB;',
  15927. ' i: IUnknown;',
  15928. 'begin',
  15929. ' obj:=tobject(roota);',
  15930. ' obj:=tobject(childa);',
  15931. ' child:=tchild(tobject(roota));',
  15932. ' roota:=textroota(obj);',
  15933. ' roota:=textroota(child);',
  15934. ' roota:=textroota(rootb);',
  15935. ' roota:=textroota(childb);',
  15936. ' childa:=textchilda(textroota(obj));',
  15937. ' roota:=TExtRootA(i)',
  15938. '']);
  15939. ConvertProgram;
  15940. CheckSource('TestExternalClass_TypeCastToRootClass',
  15941. LinesToStr([ // statements
  15942. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15943. 'rtl.createClass($mod, "TObject", null, function () {',
  15944. ' this.$init = function () {',
  15945. ' };',
  15946. ' this.$final = function () {',
  15947. ' };',
  15948. '});',
  15949. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15950. '});',
  15951. 'this.Obj = null;',
  15952. 'this.Child = null;',
  15953. 'this.RootA = null;',
  15954. 'this.ChildA = null;',
  15955. 'this.RootB = null;',
  15956. 'this.ChildB = null;',
  15957. 'this.i = null;',
  15958. '']),
  15959. LinesToStr([ // $mod.$main
  15960. '$mod.Obj = $mod.RootA;',
  15961. '$mod.Obj = $mod.ChildA;',
  15962. '$mod.Child = $mod.RootA;',
  15963. '$mod.RootA = $mod.Obj;',
  15964. '$mod.RootA = $mod.Child;',
  15965. '$mod.RootA = $mod.RootB;',
  15966. '$mod.RootA = $mod.ChildB;',
  15967. '$mod.ChildA = $mod.Obj;',
  15968. '$mod.RootA = $mod.i;',
  15969. '']));
  15970. end;
  15971. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  15972. begin
  15973. StartProgram(false);
  15974. Add([
  15975. '{$modeswitch externalclass}',
  15976. 'type',
  15977. ' IUnknown = interface end;',
  15978. ' IBird = interface(IUnknown) end;',
  15979. ' TClass = class of TObject;',
  15980. ' TObject = class',
  15981. ' end;',
  15982. ' TChild = class',
  15983. ' end;',
  15984. ' TJSObject = class external name ''Object''',
  15985. ' end;',
  15986. ' TRec = record end;',
  15987. 'var',
  15988. ' Obj: TObject;',
  15989. ' Child: TChild;',
  15990. ' i: IUnknown;',
  15991. ' Bird: IBird;',
  15992. ' j: TJSObject;',
  15993. ' r: TRec;',
  15994. ' c: TClass;',
  15995. 'begin',
  15996. ' j:=tjsobject(IUnknown);',
  15997. ' j:=tjsobject(IBird);',
  15998. ' j:=tjsobject(TObject);',
  15999. ' j:=tjsobject(TChild);',
  16000. ' j:=tjsobject(TRec);',
  16001. ' j:=tjsobject(Obj);',
  16002. ' j:=tjsobject(Child);',
  16003. ' j:=tjsobject(i);',
  16004. ' j:=tjsobject(Bird);',
  16005. ' j:=tjsobject(r);',
  16006. ' j:=tjsobject(c);',
  16007. '']);
  16008. ConvertProgram;
  16009. CheckSource('TestExternalClass_TypeCastToJSObject',
  16010. LinesToStr([ // statements
  16011. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16012. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16013. 'rtl.createClass($mod, "TObject", null, function () {',
  16014. ' this.$init = function () {',
  16015. ' };',
  16016. ' this.$final = function () {',
  16017. ' };',
  16018. '});',
  16019. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16020. '});',
  16021. 'rtl.recNewT($mod, "TRec", function () {',
  16022. ' this.$eq = function (b) {',
  16023. ' return true;',
  16024. ' };',
  16025. ' this.$assign = function (s) {',
  16026. ' return this;',
  16027. ' };',
  16028. '});',
  16029. 'this.Obj = null;',
  16030. 'this.Child = null;',
  16031. 'this.i = null;',
  16032. 'this.Bird = null;',
  16033. 'this.j = null;',
  16034. 'this.r = $mod.TRec.$new();',
  16035. 'this.c = null;',
  16036. '']),
  16037. LinesToStr([ // $mod.$main
  16038. '$mod.j = $mod.IUnknown;',
  16039. '$mod.j = $mod.IBird;',
  16040. '$mod.j = $mod.TObject;',
  16041. '$mod.j = $mod.TChild;',
  16042. '$mod.j = $mod.TRec;',
  16043. '$mod.j = $mod.Obj;',
  16044. '$mod.j = $mod.Child;',
  16045. '$mod.j = $mod.i;',
  16046. '$mod.j = $mod.Bird;',
  16047. '$mod.j = $mod.r;',
  16048. '$mod.j = $mod.c;',
  16049. '']));
  16050. end;
  16051. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  16052. begin
  16053. StartProgram(false);
  16054. Add('{$modeswitch externalclass}');
  16055. Add('type');
  16056. Add(' TJSString = class external name ''String''');
  16057. Add(' class function fromCharCode() : string; varargs;');
  16058. Add(' function anchor(const aName : string) : string;');
  16059. Add(' end;');
  16060. Add('var');
  16061. Add(' s: string;');
  16062. Add('begin');
  16063. Add(' s:=TJSString.fromCharCode(65,66);');
  16064. Add(' s:=TJSString(s).anchor(s);');
  16065. Add(' s:=TJSString(''foo'').anchor(s);');
  16066. ConvertProgram;
  16067. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  16068. LinesToStr([ // statements
  16069. 'this.s = "";',
  16070. '']),
  16071. LinesToStr([ // $mod.$main
  16072. '$mod.s = String.fromCharCode(65, 66);',
  16073. '$mod.s = $mod.s.anchor($mod.s);',
  16074. '$mod.s = "foo".anchor($mod.s);',
  16075. '']));
  16076. end;
  16077. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  16078. begin
  16079. StartProgram(false);
  16080. Add([
  16081. '{$modeswitch externalclass}',
  16082. 'type',
  16083. ' TJSObject = class external name ''Object'' end;',
  16084. ' TJSFunction = class external name ''Function''',
  16085. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  16086. ' function call(thisArg: TJSObject): JSValue; varargs;',
  16087. ' end;',
  16088. ' TObject = class',
  16089. ' procedure DoIt(i: longint);',
  16090. ' end;',
  16091. ' TFuncInt = function(o: TObject): longint;',
  16092. 'function GetIt(o: TObject): longint;',
  16093. ' procedure Sub; begin end;',
  16094. 'var',
  16095. ' f: TJSFunction;',
  16096. ' fi: TFuncInt;',
  16097. 'begin',
  16098. ' fi:=TFuncInt(f);',
  16099. ' f:=TJSFunction(fi);',
  16100. ' f:=TJSFunction(@GetIt);',
  16101. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  16102. ' f:=TJSFunction(@Sub);',
  16103. ' f:=TJSFunction(@o.doit);',
  16104. ' f:=TJSFunction(fi).bind(nil,4)',
  16105. 'end;',
  16106. 'procedure TObject.DoIt(i: longint);',
  16107. ' procedure Sub; begin end;',
  16108. 'var f: TJSFunction;',
  16109. 'begin',
  16110. ' f:=TJSFunction(@DoIt);',
  16111. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  16112. ' f:=TJSFunction(@Sub);',
  16113. ' f:=TJSFunction(@GetIt);',
  16114. 'end;',
  16115. 'begin']);
  16116. ConvertProgram;
  16117. CheckSource('TestExternalClass_TypeCastToJSFunction',
  16118. LinesToStr([ // statements
  16119. 'rtl.createClass($mod, "TObject", null, function () {',
  16120. ' this.$init = function () {',
  16121. ' };',
  16122. ' this.$final = function () {',
  16123. ' };',
  16124. ' this.DoIt = function (i) {',
  16125. ' var $Self = this;',
  16126. ' function Sub() {',
  16127. ' };',
  16128. ' var f = null;',
  16129. ' f = rtl.createCallback($Self, "DoIt");',
  16130. ' f = rtl.createCallback($Self, "DoIt").bind(null, 13);',
  16131. ' f = Sub;',
  16132. ' f = $mod.GetIt;',
  16133. ' };',
  16134. '});',
  16135. 'this.GetIt = function (o) {',
  16136. ' var Result = 0;',
  16137. ' function Sub() {',
  16138. ' };',
  16139. ' var f = null;',
  16140. ' var fi = null;',
  16141. ' fi = f;',
  16142. ' f = fi;',
  16143. ' f = $mod.GetIt;',
  16144. ' f = $mod.GetIt.bind(null, 3);',
  16145. ' f = Sub;',
  16146. ' f = rtl.createCallback(o, "DoIt");',
  16147. ' f = fi.bind(null, 4);',
  16148. ' return Result;',
  16149. '};',
  16150. '']),
  16151. LinesToStr([ // $mod.$main
  16152. '']));
  16153. end;
  16154. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  16155. begin
  16156. StartProgram(false);
  16157. Add([
  16158. '{$mode delphi}',
  16159. '{$modeswitch externalclass}',
  16160. 'type',
  16161. ' TJSObject = class external name ''Object'' end;',
  16162. ' TJSWindow = class external name ''Window''(TJSObject)',
  16163. ' procedure Open;',
  16164. ' end;',
  16165. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  16166. ' procedure Execute;',
  16167. ' end;',
  16168. 'procedure Fly;',
  16169. 'var',
  16170. ' w: TJSWindow;',
  16171. ' e: TJSEventTarget;',
  16172. 'begin',
  16173. ' w:=TJSWindow(e);',
  16174. ' e:=TJSEventTarget(w);',
  16175. 'end;',
  16176. 'begin']);
  16177. ConvertProgram;
  16178. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  16179. LinesToStr([ // statements
  16180. 'this.Fly = function () {',
  16181. ' var w = null;',
  16182. ' var e = null;',
  16183. ' w = e;',
  16184. ' e = w;',
  16185. '};',
  16186. '']),
  16187. LinesToStr([ // $mod.$main
  16188. '']));
  16189. end;
  16190. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  16191. begin
  16192. StartProgram(false);
  16193. Add('{$modeswitch externalclass}');
  16194. Add('type');
  16195. Add(' TJSString = class external name ''String''');
  16196. Add(' class function fromCharCode() : string; varargs;');
  16197. Add(' end;');
  16198. Add('var');
  16199. Add(' s: string;');
  16200. Add(' sObj: TJSString;');
  16201. Add('begin');
  16202. Add(' s:=sObj.fromCharCode(65,66);');
  16203. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  16204. nExternalClassInstanceCannotAccessStaticX);
  16205. ConvertProgram;
  16206. end;
  16207. procedure TTestModule.TestExternalClass_BracketAccessor;
  16208. begin
  16209. StartProgram(false);
  16210. Add([
  16211. '{$modeswitch externalclass}',
  16212. 'type',
  16213. ' TJSArray = class external name ''Array2''',
  16214. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16215. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16216. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16217. ' end;',
  16218. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  16219. 'begin end;',
  16220. 'var',
  16221. ' Arr: tjsarray;',
  16222. ' s: string;',
  16223. ' i: longint;',
  16224. ' v: jsvalue;',
  16225. 'begin',
  16226. ' v:=arr[0];',
  16227. ' v:=arr.items[1];',
  16228. ' arr[2]:=s;',
  16229. ' arr.items[3]:=s;',
  16230. ' arr[4]:=i;',
  16231. ' arr[5]:=arr[6];',
  16232. ' arr.items[7]:=arr.items[8];',
  16233. ' with arr do items[9]:=items[10];',
  16234. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  16235. ' with arr do begin',
  16236. ' v:=GetItems(14);',
  16237. ' setitems(15,16);',
  16238. ' end;',
  16239. ' v:=test1.arr.items[17];',
  16240. ' test1.arr.items[18]:=v;',
  16241. '']);
  16242. ConvertProgram;
  16243. CheckSource('TestExternalClass_BracketAccessor',
  16244. LinesToStr([ // statements
  16245. 'this.DoIt = function (vI, vJ, vK, vL) {',
  16246. '};',
  16247. 'this.Arr = null;',
  16248. 'this.s = "";',
  16249. 'this.i = 0;',
  16250. 'this.v = undefined;',
  16251. '']),
  16252. LinesToStr([ // $mod.$main
  16253. '$mod.v = $mod.Arr[0];',
  16254. '$mod.v = $mod.Arr[1];',
  16255. '$mod.Arr[2] = $mod.s;',
  16256. '$mod.Arr[3] = $mod.s;',
  16257. '$mod.Arr[4] = $mod.i;',
  16258. '$mod.Arr[5] = $mod.Arr[6];',
  16259. '$mod.Arr[7] = $mod.Arr[8];',
  16260. 'var $with1 = $mod.Arr;',
  16261. '$with1[9] = $with1[10];',
  16262. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  16263. ' a: 9,',
  16264. ' p: $mod.Arr,',
  16265. ' get: function () {',
  16266. ' return this.p[this.a];',
  16267. ' },',
  16268. ' set: function (v) {',
  16269. ' this.p[this.a] = v;',
  16270. ' }',
  16271. '}, {',
  16272. ' a: 10,',
  16273. ' p: $mod.Arr,',
  16274. ' get: function () {',
  16275. ' return this.p[this.a];',
  16276. ' },',
  16277. ' set: function (v) {',
  16278. ' this.p[this.a] = v;',
  16279. ' }',
  16280. '});',
  16281. 'var $with2 = $mod.Arr;',
  16282. '$mod.v = $with2[14];',
  16283. '$with2[15] = 16;',
  16284. '$mod.v = $mod.Arr[17];',
  16285. '$mod.Arr[18] = $mod.v;',
  16286. '']));
  16287. end;
  16288. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  16289. begin
  16290. StartProgram(false);
  16291. Add([
  16292. '{$modeswitch externalclass}',
  16293. 'type',
  16294. ' TJSArray = class external name ''Array2''',
  16295. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16296. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16297. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16298. ' end;',
  16299. ' TMyArr = class(TJSArray)',
  16300. ' procedure DoIt;',
  16301. ' end;',
  16302. 'procedure tmyarr.DoIt;',
  16303. 'begin',
  16304. ' Items[1]:=Items[2];',
  16305. ' SetItems(3,getItems(4));',
  16306. 'end;',
  16307. 'var',
  16308. ' Arr: tmyarr;',
  16309. ' s: string;',
  16310. ' i: longint;',
  16311. ' v: jsvalue;',
  16312. 'begin',
  16313. ' v:=arr[0];',
  16314. ' v:=arr.items[1];',
  16315. ' arr[2]:=s;',
  16316. ' arr.items[3]:=s;',
  16317. ' arr[4]:=i;',
  16318. ' arr[5]:=arr[6];',
  16319. ' arr.items[7]:=arr.items[8];',
  16320. ' with arr do items[9]:=items[10];',
  16321. ' with arr do begin',
  16322. ' v:=GetItems(14);',
  16323. ' setitems(15,16);',
  16324. ' end;',
  16325. '']);
  16326. ConvertProgram;
  16327. CheckSource('TestExternalClass_BracketAccessor_Call',
  16328. LinesToStr([ // statements
  16329. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  16330. ' this.$init = function () {',
  16331. ' };',
  16332. ' this.$final = function () {',
  16333. ' };',
  16334. ' this.DoIt = function () {',
  16335. ' this[1] = this[2];',
  16336. ' this[3] = this[4];',
  16337. ' };',
  16338. '});',
  16339. 'this.Arr = null;',
  16340. 'this.s = "";',
  16341. 'this.i = 0;',
  16342. 'this.v = undefined;',
  16343. '']),
  16344. LinesToStr([ // $mod.$main
  16345. '$mod.v = $mod.Arr[0];',
  16346. '$mod.v = $mod.Arr[1];',
  16347. '$mod.Arr[2] = $mod.s;',
  16348. '$mod.Arr[3] = $mod.s;',
  16349. '$mod.Arr[4] = $mod.i;',
  16350. '$mod.Arr[5] = $mod.Arr[6];',
  16351. '$mod.Arr[7] = $mod.Arr[8];',
  16352. 'var $with1 = $mod.Arr;',
  16353. '$with1[9] = $with1[10];',
  16354. 'var $with2 = $mod.Arr;',
  16355. '$mod.v = $with2[14];',
  16356. '$with2[15] = 16;',
  16357. '']));
  16358. end;
  16359. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  16360. begin
  16361. StartProgram(false);
  16362. Add('{$modeswitch externalclass}');
  16363. Add('type');
  16364. Add(' TJSArray = class external name ''Array2''');
  16365. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  16366. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  16367. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  16368. Add(' end;');
  16369. Add('begin');
  16370. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  16371. nBracketAccessorOfExternalClassMustHaveOneParameter);
  16372. ConvertProgram;
  16373. end;
  16374. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  16375. begin
  16376. StartProgram(false);
  16377. Add('{$modeswitch externalclass}');
  16378. Add('type');
  16379. Add(' TJSArray = class external name ''Array2''');
  16380. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16381. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  16382. Add(' end;');
  16383. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  16384. Add('begin end;');
  16385. Add('var');
  16386. Add(' Arr: tjsarray;');
  16387. Add(' v: jsvalue;');
  16388. Add('begin');
  16389. Add(' v:=arr[0];');
  16390. Add(' v:=arr.items[1];');
  16391. Add(' with arr do v:=items[2];');
  16392. Add(' doit(arr[3],arr[4]);');
  16393. ConvertProgram;
  16394. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  16395. LinesToStr([ // statements
  16396. 'this.DoIt = function (vI, vJ) {',
  16397. '};',
  16398. 'this.Arr = null;',
  16399. 'this.v = undefined;',
  16400. '']),
  16401. LinesToStr([ // $mod.$main
  16402. '$mod.v = $mod.Arr[0];',
  16403. '$mod.v = $mod.Arr[1];',
  16404. 'var $with1 = $mod.Arr;',
  16405. '$mod.v = $with1[2];',
  16406. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  16407. '']));
  16408. end;
  16409. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  16410. begin
  16411. StartProgram(false);
  16412. Add('{$modeswitch externalclass}');
  16413. Add('type');
  16414. Add(' TJSArray = class external name ''Array2''');
  16415. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16416. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16417. Add(' end;');
  16418. Add('var');
  16419. Add(' Arr: tjsarray;');
  16420. Add(' s: string;');
  16421. Add(' i: longint;');
  16422. Add(' v: jsvalue;');
  16423. Add('begin');
  16424. Add(' arr[2]:=s;');
  16425. Add(' arr.items[3]:=s;');
  16426. Add(' arr[4]:=i;');
  16427. Add(' with arr do items[5]:=i;');
  16428. ConvertProgram;
  16429. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  16430. LinesToStr([ // statements
  16431. 'this.Arr = null;',
  16432. 'this.s = "";',
  16433. 'this.i = 0;',
  16434. 'this.v = undefined;',
  16435. '']),
  16436. LinesToStr([ // $mod.$main
  16437. '$mod.Arr[2] = $mod.s;',
  16438. '$mod.Arr[3] = $mod.s;',
  16439. '$mod.Arr[4] = $mod.i;',
  16440. 'var $with1 = $mod.Arr;',
  16441. '$with1[5] = $mod.i;',
  16442. '']));
  16443. end;
  16444. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  16445. begin
  16446. StartProgram(false);
  16447. Add('{$modeswitch externalclass}');
  16448. Add('type');
  16449. Add(' TJSArray = class external name ''Array2''');
  16450. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16451. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16452. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  16453. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  16454. Add(' end;');
  16455. Add('var');
  16456. Add(' Arr: tjsarray;');
  16457. Add(' s: string;');
  16458. Add(' i: longint;');
  16459. Add(' v: jsvalue;');
  16460. Add('begin');
  16461. Add(' arr[2]:=s;');
  16462. Add(' arr.items[3]:=s;');
  16463. Add(' arr.numbers[4]:=i;');
  16464. Add(' with arr do items[5]:=i;');
  16465. Add(' with arr do numbers[6]:=i;');
  16466. ConvertProgram;
  16467. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  16468. LinesToStr([ // statements
  16469. 'this.Arr = null;',
  16470. 'this.s = "";',
  16471. 'this.i = 0;',
  16472. 'this.v = undefined;',
  16473. '']),
  16474. LinesToStr([ // $mod.$main
  16475. '$mod.Arr[2] = $mod.s;',
  16476. '$mod.Arr[3] = $mod.s;',
  16477. '$mod.Arr[4] = $mod.i;',
  16478. 'var $with1 = $mod.Arr;',
  16479. '$with1[5] = $mod.i;',
  16480. 'var $with2 = $mod.Arr;',
  16481. '$with2[6] = $mod.i;',
  16482. '']));
  16483. end;
  16484. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  16485. begin
  16486. StartProgram(false);
  16487. Add('{$modeswitch externalclass}');
  16488. Add('type');
  16489. Add(' TJSArray = class external name ''Array2''');
  16490. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16491. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16492. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  16493. Add(' end;');
  16494. Add('var');
  16495. Add(' Arr: tjsarray;');
  16496. Add(' i: longint;');
  16497. Add(' IntArr: array of longint;');
  16498. Add(' v: jsvalue;');
  16499. Add('begin');
  16500. Add(' v:=arr.items[i];');
  16501. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  16502. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  16503. ConvertProgram;
  16504. CheckSource('TestExternalClass_BracketAccessor_Index',
  16505. LinesToStr([ // statements
  16506. 'this.Arr = null;',
  16507. 'this.i = 0;',
  16508. 'this.IntArr = [];',
  16509. 'this.v = undefined;',
  16510. '']),
  16511. LinesToStr([ // $mod.$main
  16512. '$mod.v = $mod.Arr[$mod.i];',
  16513. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  16514. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  16515. '']));
  16516. end;
  16517. procedure TTestModule.TestExternalClass_ForInJSObject;
  16518. begin
  16519. StartProgram(false);
  16520. Add([
  16521. '{$modeswitch externalclass}',
  16522. 'type',
  16523. ' TJSObject = class external name ''Object''',
  16524. ' end;',
  16525. 'var',
  16526. ' o: TJSObject;',
  16527. ' key: string;',
  16528. 'begin',
  16529. ' for key in o do',
  16530. ' if key=''abc'' then ;',
  16531. '']);
  16532. ConvertProgram;
  16533. CheckSource('TestExternalClass_ForInJSObject',
  16534. LinesToStr([ // statements
  16535. 'this.o = null;',
  16536. 'this.key = "";',
  16537. '']),
  16538. LinesToStr([ // $mod.$main
  16539. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  16540. '']));
  16541. end;
  16542. procedure TTestModule.TestExternalClass_ForInJSArray;
  16543. begin
  16544. StartProgram(false);
  16545. Add([
  16546. '{$modeswitch externalclass}',
  16547. 'type',
  16548. ' TJSInt8Array = class external name ''Int8Array''',
  16549. ' private',
  16550. ' flength: NativeInt external name ''length'';',
  16551. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  16552. ' public',
  16553. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  16554. ' property Length: NativeInt read flength;',
  16555. ' end;',
  16556. 'var',
  16557. ' a: TJSInt8Array;',
  16558. ' value: shortint;',
  16559. 'begin',
  16560. ' for value in a do',
  16561. ' if value=3 then ;',
  16562. '']);
  16563. ConvertProgram;
  16564. CheckSource('TestExternalClass_ForInJSArray',
  16565. LinesToStr([ // statements
  16566. 'this.a = null;',
  16567. 'this.value = 0;',
  16568. '']),
  16569. LinesToStr([ // $mod.$main
  16570. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  16571. ' $mod.value = $in1[$l2];',
  16572. ' if ($mod.value === 3) ;',
  16573. '};',
  16574. '']));
  16575. end;
  16576. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  16577. begin
  16578. AddModuleWithIntfImplSrc('unit2.pas',
  16579. LinesToStr([
  16580. '{$modeswitch externalclass}',
  16581. 'type',
  16582. ' TJSBufferSource = class external name ''BufferSource''',
  16583. ' end;',
  16584. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  16585. '']),
  16586. '');
  16587. AddModuleWithIntfImplSrc('unit3.pas',
  16588. LinesToStr([
  16589. '{$modeswitch externalclass}',
  16590. 'type',
  16591. ' TJSBufferSource = class external name ''BufferSource''',
  16592. ' end;',
  16593. '']),
  16594. '');
  16595. StartUnit(true);
  16596. Add([
  16597. 'interface',
  16598. 'uses unit2, unit3;',
  16599. 'procedure DoSome(s: TJSBufferSource);',
  16600. 'implementation',
  16601. 'procedure DoSome(s: TJSBufferSource);',
  16602. 'begin',
  16603. ' DoIt(s);',
  16604. 'end;',
  16605. '']);
  16606. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  16607. nIncompatibleTypeArgNo);
  16608. ConvertUnit;
  16609. end;
  16610. procedure TTestModule.TestClassInterface_Corba;
  16611. begin
  16612. StartProgram(false);
  16613. Add([
  16614. '{$interfaces corba}',
  16615. 'type',
  16616. ' IUnknown = interface;',
  16617. ' IUnknown = interface',
  16618. ' [''{00000000-0000-0000-C000-000000000046}'']',
  16619. ' end;',
  16620. ' IInterface = IUnknown;',
  16621. ' IBird = interface(IInterface)',
  16622. ' function GetSize: longint;',
  16623. ' procedure SetSize(i: longint);',
  16624. ' property Size: longint read GetSize write SetSize;',
  16625. ' procedure DoIt(i: longint);',
  16626. ' end;',
  16627. ' TObject = class',
  16628. ' end;',
  16629. ' TBird = class(TObject,IBird)',
  16630. ' function GetSize: longint; virtual; abstract;',
  16631. ' procedure SetSize(i: longint); virtual; abstract;',
  16632. ' procedure DoIt(i: longint); virtual; abstract;',
  16633. ' end;',
  16634. 'var',
  16635. ' BirdIntf: IBird;',
  16636. 'begin',
  16637. ' BirdIntf.Size:=BirdIntf.Size;',
  16638. '']);
  16639. ConvertProgram;
  16640. CheckSource('TestClassInterface_Corba',
  16641. LinesToStr([ // statements
  16642. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  16643. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  16644. 'rtl.createClass($mod, "TObject", null, function () {',
  16645. ' this.$init = function () {',
  16646. ' };',
  16647. ' this.$final = function () {',
  16648. ' };',
  16649. '});',
  16650. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16651. ' rtl.addIntf(this, $mod.IBird);',
  16652. '});',
  16653. 'this.BirdIntf = null;',
  16654. '']),
  16655. LinesToStr([ // $mod.$main
  16656. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  16657. '']));
  16658. end;
  16659. procedure TTestModule.TestClassInterface_ProcExternalFail;
  16660. begin
  16661. StartProgram(false);
  16662. Add([
  16663. '{$interfaces corba}',
  16664. 'type',
  16665. ' IUnknown = interface',
  16666. ' procedure DoIt; external name ''foo'';',
  16667. ' end;',
  16668. 'begin']);
  16669. SetExpectedParserError(
  16670. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  16671. nParserNoFieldsAllowed);
  16672. ConvertProgram;
  16673. end;
  16674. procedure TTestModule.TestClassInterface_Overloads;
  16675. begin
  16676. StartProgram(false);
  16677. Add([
  16678. '{$interfaces corba}',
  16679. 'type',
  16680. ' integer = longint;',
  16681. ' IUnknown = interface',
  16682. ' procedure DoIt(i: integer);',
  16683. ' procedure DoIt(s: string);',
  16684. ' end;',
  16685. ' IBird = interface(IUnknown)',
  16686. ' procedure DoIt(b: boolean); overload;',
  16687. ' end;',
  16688. ' TObject = class',
  16689. ' end;',
  16690. ' TBird = class(TObject,IBird)',
  16691. ' procedure DoIt(o: TObject);',
  16692. ' procedure DoIt(s: string);',
  16693. ' procedure DoIt(i: integer);',
  16694. ' procedure DoIt(b: boolean);',
  16695. ' end;',
  16696. 'procedure TBird.DoIt(o: TObject); begin end;',
  16697. 'procedure TBird.DoIt(s: string); begin end;',
  16698. 'procedure TBird.DoIt(i: integer); begin end;',
  16699. 'procedure TBird.DoIt(b: boolean); begin end;',
  16700. 'var',
  16701. ' BirdIntf: IBird;',
  16702. 'begin',
  16703. ' BirdIntf.DoIt(3);',
  16704. ' BirdIntf.DoIt(''abc'');',
  16705. ' BirdIntf.DoIt(true);',
  16706. '']);
  16707. ConvertProgram;
  16708. CheckSource('TestClassInterface_Overloads',
  16709. LinesToStr([ // statements
  16710. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  16711. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  16712. 'rtl.createClass($mod, "TObject", null, function () {',
  16713. ' this.$init = function () {',
  16714. ' };',
  16715. ' this.$final = function () {',
  16716. ' };',
  16717. '});',
  16718. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16719. ' this.DoIt = function (o) {',
  16720. ' };',
  16721. ' this.DoIt$1 = function (s) {',
  16722. ' };',
  16723. ' this.DoIt$2 = function (i) {',
  16724. ' };',
  16725. ' this.DoIt$3 = function (b) {',
  16726. ' };',
  16727. ' rtl.addIntf(this, $mod.IBird, {',
  16728. ' DoIt$2: "DoIt$3",',
  16729. ' DoIt: "DoIt$2"',
  16730. ' });',
  16731. '});',
  16732. 'this.BirdIntf = null;',
  16733. '']),
  16734. LinesToStr([ // $mod.$main
  16735. '$mod.BirdIntf.DoIt(3);',
  16736. '$mod.BirdIntf.DoIt$1("abc");',
  16737. '$mod.BirdIntf.DoIt$2(true);',
  16738. '']));
  16739. end;
  16740. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  16741. begin
  16742. StartProgram(false);
  16743. Add([
  16744. '{$interfaces corba}',
  16745. 'type',
  16746. ' IBird = interface',
  16747. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16748. ' end;',
  16749. ' IDog = interface',
  16750. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16751. ' end;',
  16752. ' TObject = class(IBird,IDog)',
  16753. ' end;',
  16754. 'begin']);
  16755. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  16756. nDuplicateGUIDXInYZ);
  16757. ConvertProgram;
  16758. end;
  16759. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  16760. begin
  16761. StartProgram(false);
  16762. Add([
  16763. '{$interfaces corba}',
  16764. 'type',
  16765. ' IAnimal = interface',
  16766. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16767. ' end;',
  16768. ' IBird = interface(IAnimal)',
  16769. ' end;',
  16770. ' IHawk = interface(IBird)',
  16771. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16772. ' end;',
  16773. 'begin']);
  16774. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  16775. nDuplicateGUIDXInYZ);
  16776. ConvertProgram;
  16777. end;
  16778. procedure TTestModule.TestClassInterface_AncestorImpl;
  16779. begin
  16780. StartProgram(false);
  16781. Add([
  16782. '{$interfaces corba}',
  16783. 'type',
  16784. ' integer = longint;',
  16785. ' IUnknown = interface',
  16786. ' procedure DoIt(i: integer);',
  16787. ' end;',
  16788. ' IBird = interface',
  16789. ' procedure Fly(i: integer);',
  16790. ' end;',
  16791. ' TObject = class(IUnknown)',
  16792. ' procedure DoIt(i: integer);',
  16793. ' end;',
  16794. ' TBird = class(IBird)',
  16795. ' procedure Fly(i: integer);',
  16796. ' end;',
  16797. 'procedure TObject.DoIt(i: integer); begin end;',
  16798. 'procedure TBird.Fly(i: integer); begin end;',
  16799. 'begin',
  16800. '']);
  16801. ConvertProgram;
  16802. CheckSource('TestClassInterface_AncestorIntf',
  16803. LinesToStr([ // statements
  16804. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  16805. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  16806. 'rtl.createClass($mod, "TObject", null, function () {',
  16807. ' this.$init = function () {',
  16808. ' };',
  16809. ' this.$final = function () {',
  16810. ' };',
  16811. ' this.DoIt = function (i) {',
  16812. ' };',
  16813. ' rtl.addIntf(this, $mod.IUnknown);',
  16814. '});',
  16815. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16816. ' this.Fly = function (i) {',
  16817. ' };',
  16818. ' rtl.addIntf(this, $mod.IBird);',
  16819. ' rtl.addIntf(this, $mod.IUnknown);',
  16820. '});',
  16821. '']),
  16822. LinesToStr([ // $mod.$main
  16823. '']));
  16824. end;
  16825. procedure TTestModule.TestClassInterface_ImplReintroduce;
  16826. begin
  16827. StartProgram(false);
  16828. Add([
  16829. '{$interfaces corba}',
  16830. 'type',
  16831. ' integer = longint;',
  16832. ' IBird = interface',
  16833. ' procedure DoIt(i: integer);',
  16834. ' end;',
  16835. ' TObject = class',
  16836. ' procedure DoIt(i: integer);',
  16837. ' end;',
  16838. ' TBird = class(IBird)',
  16839. ' procedure DoIt(i: integer); virtual; reintroduce;',
  16840. ' end;',
  16841. 'procedure TObject.DoIt(i: integer); begin end;',
  16842. 'procedure TBird.DoIt(i: integer); begin end;',
  16843. 'begin',
  16844. '']);
  16845. ConvertProgram;
  16846. CheckSource('TestClassInterface_ImplReintroduce',
  16847. LinesToStr([ // statements
  16848. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  16849. 'rtl.createClass($mod, "TObject", null, function () {',
  16850. ' this.$init = function () {',
  16851. ' };',
  16852. ' this.$final = function () {',
  16853. ' };',
  16854. ' this.DoIt = function (i) {',
  16855. ' };',
  16856. '});',
  16857. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16858. ' this.DoIt$1 = function (i) {',
  16859. ' };',
  16860. ' rtl.addIntf(this, $mod.IBird, {',
  16861. ' DoIt: "DoIt$1"',
  16862. ' });',
  16863. '});',
  16864. '']),
  16865. LinesToStr([ // $mod.$main
  16866. '']));
  16867. end;
  16868. procedure TTestModule.TestClassInterface_MethodResolution;
  16869. begin
  16870. StartProgram(false);
  16871. Add([
  16872. '{$interfaces corba}',
  16873. 'type',
  16874. ' IUnknown = interface',
  16875. ' procedure Walk(i: longint);',
  16876. ' end;',
  16877. ' IBird = interface(IUnknown)',
  16878. ' procedure Walk(b: boolean); overload;',
  16879. ' procedure Fly(s: string);',
  16880. ' end;',
  16881. ' TObject = class',
  16882. ' end;',
  16883. ' TBird = class(TObject,IBird)',
  16884. ' procedure IBird.Fly = Move;',
  16885. ' procedure IBird.Walk = Hop;',
  16886. ' procedure Hop(i: longint);',
  16887. ' procedure Move(s: string);',
  16888. ' procedure Hop(b: boolean);',
  16889. ' end;',
  16890. 'procedure TBird.Move(s: string); begin end;',
  16891. 'procedure TBird.Hop(i: longint); begin end;',
  16892. 'procedure TBird.Hop(b: boolean); begin end;',
  16893. 'var',
  16894. ' BirdIntf: IBird;',
  16895. 'begin',
  16896. ' BirdIntf.Walk(3);',
  16897. ' BirdIntf.Walk(true);',
  16898. ' BirdIntf.Fly(''abc'');',
  16899. '']);
  16900. ConvertProgram;
  16901. CheckSource('TestClassInterface_MethodResolution',
  16902. LinesToStr([ // statements
  16903. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  16904. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  16905. 'rtl.createClass($mod, "TObject", null, function () {',
  16906. ' this.$init = function () {',
  16907. ' };',
  16908. ' this.$final = function () {',
  16909. ' };',
  16910. '});',
  16911. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16912. ' this.Hop = function (i) {',
  16913. ' };',
  16914. ' this.Move = function (s) {',
  16915. ' };',
  16916. ' this.Hop$1 = function (b) {',
  16917. ' };',
  16918. ' rtl.addIntf(this, $mod.IBird, {',
  16919. ' Walk$1: "Hop$1",',
  16920. ' Fly: "Move",',
  16921. ' Walk: "Hop"',
  16922. ' });',
  16923. '});',
  16924. 'this.BirdIntf = null;',
  16925. '']),
  16926. LinesToStr([ // $mod.$main
  16927. '$mod.BirdIntf.Walk(3);',
  16928. '$mod.BirdIntf.Walk$1(true);',
  16929. '$mod.BirdIntf.Fly("abc");',
  16930. '']));
  16931. end;
  16932. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  16933. begin
  16934. StartProgram(false);
  16935. Add([
  16936. '{$interfaces com}',
  16937. 'type',
  16938. ' IUnknown = interface',
  16939. ' function _AddRef: longint;',
  16940. ' procedure Walk;',
  16941. ' end;',
  16942. ' IBird = interface end;',
  16943. ' IDog = interface end;',
  16944. ' TObject = class(IBird,IDog)',
  16945. ' function _AddRef: longint; virtual; abstract;',
  16946. ' procedure Walk; virtual; abstract;',
  16947. ' end;',
  16948. ' TBird = class(IUnknown)',
  16949. ' end;',
  16950. 'begin',
  16951. '']);
  16952. ConvertProgram;
  16953. CheckSource('TestClassInterface_COM_AncestorLess',
  16954. LinesToStr([ // statements
  16955. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  16956. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  16957. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  16958. 'rtl.createClass($mod, "TObject", null, function () {',
  16959. ' this.$init = function () {',
  16960. ' };',
  16961. ' this.$final = function () {',
  16962. ' };',
  16963. ' rtl.addIntf(this, $mod.IBird);',
  16964. ' rtl.addIntf(this, $mod.IDog);',
  16965. '});',
  16966. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16967. ' rtl.addIntf(this, $mod.IUnknown);',
  16968. ' rtl.addIntf(this, $mod.IBird);',
  16969. ' rtl.addIntf(this, $mod.IDog);',
  16970. '});',
  16971. '']),
  16972. LinesToStr([ // $mod.$main
  16973. '']));
  16974. end;
  16975. procedure TTestModule.TestClassInterface_MethodOverride;
  16976. begin
  16977. StartProgram(false);
  16978. Add([
  16979. '{$interfaces corba}',
  16980. 'type',
  16981. ' IUnknown = interface',
  16982. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  16983. ' procedure Go;',
  16984. ' end;',
  16985. ' TObject = class(IUnknown)',
  16986. ' procedure Go; virtual; abstract;',
  16987. ' end;',
  16988. ' TBird = class',
  16989. ' procedure Go; override;',
  16990. ' end;',
  16991. ' TCat = class(TObject)',
  16992. ' procedure Go; override;',
  16993. ' end;',
  16994. ' TDog = class(TObject, IUnknown)',
  16995. ' procedure Go; override;',
  16996. ' end;',
  16997. 'procedure TBird.Go; begin end;',
  16998. 'procedure TCat.Go; begin end;',
  16999. 'procedure TDog.Go; begin end;',
  17000. 'begin',
  17001. '']);
  17002. ConvertProgram;
  17003. CheckSource('TestClassInterface_MethodOverride',
  17004. LinesToStr([ // statements
  17005. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  17006. 'rtl.createClass($mod, "TObject", null, function () {',
  17007. ' this.$init = function () {',
  17008. ' };',
  17009. ' this.$final = function () {',
  17010. ' };',
  17011. ' rtl.addIntf(this, $mod.IUnknown);',
  17012. '});',
  17013. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17014. ' this.Go = function () {',
  17015. ' };',
  17016. ' rtl.addIntf(this, $mod.IUnknown);',
  17017. '});',
  17018. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  17019. ' this.Go = function () {',
  17020. ' };',
  17021. ' rtl.addIntf(this, $mod.IUnknown);',
  17022. '});',
  17023. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  17024. ' this.Go = function () {',
  17025. ' };',
  17026. ' rtl.addIntf(this, $mod.IUnknown);',
  17027. '});',
  17028. '']),
  17029. LinesToStr([ // $mod.$main
  17030. '']));
  17031. end;
  17032. procedure TTestModule.TestClassInterface_Corba_Delegation;
  17033. begin
  17034. StartProgram(false);
  17035. Add([
  17036. '{$interfaces corba}',
  17037. 'type',
  17038. ' IUnknown = interface',
  17039. ' end;',
  17040. ' IBird = interface(IUnknown)',
  17041. ' procedure Fly(s: string);',
  17042. ' end;',
  17043. ' IEagle = interface(IBird)',
  17044. ' end;',
  17045. ' IDove = interface(IBird)',
  17046. ' end;',
  17047. ' ISwallow = interface(IBird)',
  17048. ' end;',
  17049. ' TObject = class',
  17050. ' end;',
  17051. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17052. ' procedure Fly(s: string); virtual; abstract;',
  17053. ' end;',
  17054. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17055. ' FBirdIntf: IBird;',
  17056. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17057. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17058. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17059. ' FDoveObj: TBird;',
  17060. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17061. ' function GetSwallowObj: TBird; virtual; abstract;',
  17062. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17063. ' end;',
  17064. 'begin',
  17065. '']);
  17066. ConvertProgram;
  17067. CheckSource('TestClassInterface_Delegation',
  17068. LinesToStr([ // statements
  17069. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17070. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17071. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17072. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17073. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17074. 'rtl.createClass($mod, "TObject", null, function () {',
  17075. ' this.$init = function () {',
  17076. ' };',
  17077. ' this.$final = function () {',
  17078. ' };',
  17079. '});',
  17080. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17081. ' rtl.addIntf(this, $mod.IBird);',
  17082. ' rtl.addIntf(this, $mod.IEagle);',
  17083. ' rtl.addIntf(this, $mod.IDove);',
  17084. ' rtl.addIntf(this, $mod.ISwallow);',
  17085. '});',
  17086. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17087. ' this.$init = function () {',
  17088. ' $mod.TObject.$init.call(this);',
  17089. ' this.FBirdIntf = null;',
  17090. ' this.FDoveObj = null;',
  17091. ' };',
  17092. ' this.$final = function () {',
  17093. ' this.FBirdIntf = undefined;',
  17094. ' this.FDoveObj = undefined;',
  17095. ' $mod.TObject.$final.call(this);',
  17096. ' };',
  17097. ' this.$intfmaps = {',
  17098. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17099. ' return this.FBirdIntf;',
  17100. ' },',
  17101. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17102. ' return this.GetEagleIntf();',
  17103. ' },',
  17104. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17105. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17106. ' },',
  17107. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17108. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17109. ' }',
  17110. ' };',
  17111. '});',
  17112. '']),
  17113. LinesToStr([ // $mod.$main
  17114. '']));
  17115. end;
  17116. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  17117. begin
  17118. StartProgram(false);
  17119. Add([
  17120. '{$interfaces corba}',
  17121. 'type',
  17122. ' IUnknown = interface',
  17123. ' end;',
  17124. ' IBird = interface(IUnknown)',
  17125. ' procedure Fly(s: string);',
  17126. ' end;',
  17127. ' IEagle = interface(IBird)',
  17128. ' end;',
  17129. ' IDove = interface(IBird)',
  17130. ' end;',
  17131. ' ISwallow = interface(IBird)',
  17132. ' end;',
  17133. ' TObject = class',
  17134. ' end;',
  17135. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17136. ' procedure Fly(s: string); virtual; abstract;',
  17137. ' end;',
  17138. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17139. ' private',
  17140. ' class var FBirdIntf: IBird;',
  17141. ' class var FDoveObj: TBird;',
  17142. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  17143. ' class function GetSwallowObj: TBird; virtual; abstract;',
  17144. ' protected',
  17145. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  17146. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17147. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  17148. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17149. ' end;',
  17150. 'begin',
  17151. '']);
  17152. ConvertProgram;
  17153. CheckSource('TestClassInterface_DelegationStatic',
  17154. LinesToStr([ // statements
  17155. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17156. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17157. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17158. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17159. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17160. 'rtl.createClass($mod, "TObject", null, function () {',
  17161. ' this.$init = function () {',
  17162. ' };',
  17163. ' this.$final = function () {',
  17164. ' };',
  17165. '});',
  17166. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17167. ' rtl.addIntf(this, $mod.IBird);',
  17168. ' rtl.addIntf(this, $mod.IEagle);',
  17169. ' rtl.addIntf(this, $mod.IDove);',
  17170. ' rtl.addIntf(this, $mod.ISwallow);',
  17171. '});',
  17172. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17173. ' this.FBirdIntf = null;',
  17174. ' this.FDoveObj = null;',
  17175. ' this.$intfmaps = {',
  17176. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17177. ' return this.FBirdIntf;',
  17178. ' },',
  17179. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17180. ' return this.$class.GetEagleIntf();',
  17181. ' },',
  17182. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17183. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17184. ' },',
  17185. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17186. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  17187. ' }',
  17188. ' };',
  17189. '});',
  17190. '']),
  17191. LinesToStr([ // $mod.$main
  17192. '']));
  17193. end;
  17194. procedure TTestModule.TestClassInterface_Corba_Operators;
  17195. begin
  17196. StartProgram(false);
  17197. Add([
  17198. '{$interfaces corba}',
  17199. 'type',
  17200. ' IUnknown = interface',
  17201. ' end;',
  17202. ' IBird = interface(IUnknown)',
  17203. ' function GetItems(Index: longint): longint;',
  17204. ' procedure SetItems(Index: longint; Value: longint);',
  17205. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  17206. ' end;',
  17207. ' TObject = class',
  17208. ' end;',
  17209. ' TBird = class(TObject,IBird)',
  17210. ' function GetItems(Index: longint): longint; virtual; abstract;',
  17211. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  17212. ' end;',
  17213. 'var',
  17214. ' IntfVar: IBird = nil;',
  17215. ' IntfVar2: IBird;',
  17216. ' ObjVar: TBird;',
  17217. ' v: JSValue;',
  17218. 'begin',
  17219. ' IntfVar:=nil;',
  17220. ' IntfVar[3]:=IntfVar[4];',
  17221. ' if Assigned(IntfVar) then ;',
  17222. ' IntfVar:=IntfVar2;',
  17223. ' IntfVar:=ObjVar;',
  17224. ' if IntfVar=IntfVar2 then ;',
  17225. ' if IntfVar<>IntfVar2 then ;',
  17226. ' if IntfVar is IBird then ;',
  17227. ' if IntfVar is TBird then ;',
  17228. ' if ObjVar is IBird then ;',
  17229. ' IntfVar:=IntfVar2 as IBird;',
  17230. ' ObjVar:=IntfVar2 as TBird;',
  17231. ' IntfVar:=ObjVar as IBird;',
  17232. ' IntfVar:=IBird(IntfVar2);',
  17233. ' ObjVar:=TBird(IntfVar);',
  17234. ' IntfVar:=IBird(ObjVar);',
  17235. ' v:=IntfVar;',
  17236. ' IntfVar:=IBird(v);',
  17237. ' if v is IBird then ;',
  17238. ' v:=JSValue(IntfVar);',
  17239. ' v:=IBird;',
  17240. '']);
  17241. ConvertProgram;
  17242. CheckSource('TestClassInterface_Corba_Operators',
  17243. LinesToStr([ // statements
  17244. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17245. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  17246. 'rtl.createClass($mod, "TObject", null, function () {',
  17247. ' this.$init = function () {',
  17248. ' };',
  17249. ' this.$final = function () {',
  17250. ' };',
  17251. '});',
  17252. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17253. ' rtl.addIntf(this, $mod.IBird);',
  17254. '});',
  17255. 'this.IntfVar = null;',
  17256. 'this.IntfVar2 = null;',
  17257. 'this.ObjVar = null;',
  17258. 'this.v = undefined;',
  17259. '']),
  17260. LinesToStr([ // $mod.$main
  17261. '$mod.IntfVar = null;',
  17262. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  17263. 'if ($mod.IntfVar != null) ;',
  17264. '$mod.IntfVar = $mod.IntfVar2;',
  17265. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  17266. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  17267. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  17268. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  17269. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  17270. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  17271. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  17272. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  17273. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17274. '$mod.IntfVar = $mod.IntfVar2;',
  17275. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  17276. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17277. '$mod.v = $mod.IntfVar;',
  17278. '$mod.IntfVar = rtl.getObject($mod.v);',
  17279. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  17280. '$mod.v = $mod.IntfVar;',
  17281. '$mod.v = $mod.IBird;',
  17282. '']));
  17283. end;
  17284. procedure TTestModule.TestClassInterface_Corba_Args;
  17285. begin
  17286. StartProgram(false);
  17287. Add([
  17288. '{$interfaces corba}',
  17289. 'type',
  17290. ' IUnknown = interface',
  17291. ' end;',
  17292. ' IBird = interface(IUnknown)',
  17293. ' end;',
  17294. ' TObject = class',
  17295. ' end;',
  17296. ' TBird = class(TObject,IBird)',
  17297. ' end;',
  17298. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  17299. 'begin',
  17300. ' DoIt(i,i,i);',
  17301. 'end;',
  17302. 'procedure Change(var i: IBird; out j: IBird);',
  17303. 'begin',
  17304. ' DoIt(i,i,i);',
  17305. ' Change(i,i);',
  17306. 'end;',
  17307. 'var',
  17308. ' i: IBird;',
  17309. ' o: TBird;',
  17310. 'begin',
  17311. ' DoIt(i,i,i);',
  17312. ' Change(i,i);',
  17313. ' DoIt(o,o,o);',
  17314. '']);
  17315. ConvertProgram;
  17316. CheckSource('TestClassInterface_Corba_Args',
  17317. LinesToStr([ // statements
  17318. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17319. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17320. 'rtl.createClass($mod, "TObject", null, function () {',
  17321. ' this.$init = function () {',
  17322. ' };',
  17323. ' this.$final = function () {',
  17324. ' };',
  17325. '});',
  17326. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17327. ' rtl.addIntf(this, $mod.IBird);',
  17328. '});',
  17329. 'this.DoIt = function (u, i, j) {',
  17330. ' $mod.DoIt({',
  17331. ' get: function () {',
  17332. ' return i;',
  17333. ' },',
  17334. ' set: function (v) {',
  17335. ' i = v;',
  17336. ' }',
  17337. ' }, i, i);',
  17338. '};',
  17339. 'this.Change = function (i, j) {',
  17340. ' $mod.DoIt(i, i.get(), i.get());',
  17341. ' $mod.Change(i, i);',
  17342. '};',
  17343. 'this.i = null;',
  17344. 'this.o = null;',
  17345. '']),
  17346. LinesToStr([ // $mod.$main
  17347. '$mod.DoIt({',
  17348. ' p: $mod,',
  17349. ' get: function () {',
  17350. ' return this.p.i;',
  17351. ' },',
  17352. ' set: function (v) {',
  17353. ' this.p.i = v;',
  17354. ' }',
  17355. '}, $mod.i, $mod.i);',
  17356. '$mod.Change({',
  17357. ' p: $mod,',
  17358. ' get: function () {',
  17359. ' return this.p.i;',
  17360. ' },',
  17361. ' set: function (v) {',
  17362. ' this.p.i = v;',
  17363. ' }',
  17364. '}, {',
  17365. ' p: $mod,',
  17366. ' get: function () {',
  17367. ' return this.p.i;',
  17368. ' },',
  17369. ' set: function (v) {',
  17370. ' this.p.i = v;',
  17371. ' }',
  17372. '});',
  17373. '$mod.DoIt({',
  17374. ' p: $mod,',
  17375. ' get: function () {',
  17376. ' return this.p.o;',
  17377. ' },',
  17378. ' set: function (v) {',
  17379. ' this.p.o = v;',
  17380. ' }',
  17381. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  17382. '']));
  17383. end;
  17384. procedure TTestModule.TestClassInterface_Corba_ForIn;
  17385. begin
  17386. StartProgram(false);
  17387. Add([
  17388. '{$interfaces corba}',
  17389. 'type',
  17390. ' IUnknown = interface end;',
  17391. ' TObject = class',
  17392. ' Id: longint;',
  17393. ' end;',
  17394. ' IEnumerator = interface(IUnknown)',
  17395. ' function GetCurrent: TObject;',
  17396. ' function MoveNext: Boolean;',
  17397. ' property Current: TObject read GetCurrent;',
  17398. ' end;',
  17399. ' IEnumerable = interface(IUnknown)',
  17400. ' function GetEnumerator: IEnumerator;',
  17401. ' end;',
  17402. 'var',
  17403. ' o: TObject;',
  17404. ' i: IEnumerable;',
  17405. 'begin',
  17406. ' for o in i do o.Id:=3;',
  17407. '']);
  17408. ConvertProgram;
  17409. CheckSource('TestClassInterface_Corba_ForIn',
  17410. LinesToStr([ // statements
  17411. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17412. 'rtl.createClass($mod, "TObject", null, function () {',
  17413. ' this.$init = function () {',
  17414. ' this.Id = 0;',
  17415. ' };',
  17416. ' this.$final = function () {',
  17417. ' };',
  17418. '});',
  17419. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17420. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17421. 'this.o = null;',
  17422. 'this.i = null;',
  17423. '']),
  17424. LinesToStr([ // $mod.$main
  17425. 'var $in1 = $mod.i.GetEnumerator();',
  17426. 'while ($in1.MoveNext()) {',
  17427. ' $mod.o = $in1.GetCurrent();',
  17428. ' $mod.o.Id = 3;',
  17429. '};',
  17430. '']));
  17431. end;
  17432. procedure TTestModule.TestClassInterface_COM_AssignVar;
  17433. begin
  17434. StartProgram(false);
  17435. Add([
  17436. '{$interfaces com}',
  17437. 'type',
  17438. ' IUnknown = interface',
  17439. ' function _AddRef: longint;',
  17440. ' function _Release: longint;',
  17441. ' end;',
  17442. ' TObject = class(IUnknown)',
  17443. ' function _AddRef: longint; virtual; abstract;',
  17444. ' function _Release: longint; virtual; abstract;',
  17445. ' end;',
  17446. 'var',
  17447. ' i: IUnknown;',
  17448. 'procedure DoGlobal(o: TObject);',
  17449. 'begin',
  17450. ' i:=nil;',
  17451. ' i:=o;',
  17452. ' i:=i;',
  17453. 'end;',
  17454. 'procedure DoLocal(o: TObject);',
  17455. 'const k: IUnknown = nil;',
  17456. 'var j: IUnknown;',
  17457. 'begin',
  17458. ' k:=o;',
  17459. ' k:=i;',
  17460. ' j:=o;',
  17461. ' j:=i;',
  17462. 'end;',
  17463. 'var o: TObject;',
  17464. 'begin',
  17465. ' i:=nil;',
  17466. ' i:=o;',
  17467. '']);
  17468. ConvertProgram;
  17469. CheckSource('TestClassInterface_COM_AssignVar',
  17470. LinesToStr([ // statements
  17471. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17472. 'rtl.createClass($mod, "TObject", null, function () {',
  17473. ' this.$init = function () {',
  17474. ' };',
  17475. ' this.$final = function () {',
  17476. ' };',
  17477. ' rtl.addIntf(this, $mod.IUnknown);',
  17478. '});',
  17479. 'this.i = null;',
  17480. 'this.DoGlobal = function (o) {',
  17481. ' rtl.setIntfP($mod, "i", null);',
  17482. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  17483. ' rtl.setIntfP($mod, "i", $mod.i);',
  17484. '};',
  17485. 'var k = null;',
  17486. 'this.DoLocal = function (o) {',
  17487. ' var j = null;',
  17488. ' try{',
  17489. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17490. ' k = rtl.setIntfL(k, $mod.i);',
  17491. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17492. ' j = rtl.setIntfL(j, $mod.i);',
  17493. ' }finally{',
  17494. ' rtl._Release(j);',
  17495. ' };',
  17496. '};',
  17497. 'this.o = null;',
  17498. '']),
  17499. LinesToStr([ // $mod.$main
  17500. 'rtl.setIntfP($mod, "i", null);',
  17501. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  17502. '']));
  17503. end;
  17504. procedure TTestModule.TestClassInterface_COM_AssignArg;
  17505. begin
  17506. StartProgram(false);
  17507. Add([
  17508. '{$interfaces com}',
  17509. 'type',
  17510. ' IUnknown = interface',
  17511. ' function _AddRef: longint;',
  17512. ' function _Release: longint;',
  17513. ' end;',
  17514. ' TObject = class(IUnknown)',
  17515. ' function _AddRef: longint; virtual; abstract;',
  17516. ' function _Release: longint; virtual; abstract;',
  17517. ' end;',
  17518. 'procedure DoDefault(i, j: IUnknown);',
  17519. 'begin',
  17520. ' i:=nil;',
  17521. ' i:=j;',
  17522. 'end;',
  17523. 'begin',
  17524. '']);
  17525. ConvertProgram;
  17526. CheckSource('TestClassInterface_COM_AssignArg',
  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. ' rtl.addIntf(this, $mod.IUnknown);',
  17535. '});',
  17536. 'this.DoDefault = function (i, j) {',
  17537. ' rtl._AddRef(i);',
  17538. ' try {',
  17539. ' i = rtl.setIntfL(i, null);',
  17540. ' i = rtl.setIntfL(i, j);',
  17541. ' } finally {',
  17542. ' rtl._Release(i);',
  17543. ' };',
  17544. '};',
  17545. '']),
  17546. LinesToStr([ // $mod.$main
  17547. '']));
  17548. end;
  17549. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  17550. begin
  17551. StartProgram(false);
  17552. Add([
  17553. '{$interfaces com}',
  17554. 'type',
  17555. ' IUnknown = interface',
  17556. ' function _AddRef: longint;',
  17557. ' function _Release: longint;',
  17558. ' end;',
  17559. ' TObject = class(IUnknown)',
  17560. ' function _AddRef: longint; virtual; abstract;',
  17561. ' function _Release: longint; virtual; abstract;',
  17562. ' end;',
  17563. 'function DoDefault(i: IUnknown): IUnknown;',
  17564. 'begin',
  17565. ' Result:=i;',
  17566. ' if Result<>nil then exit;',
  17567. 'end;',
  17568. 'begin',
  17569. '']);
  17570. ConvertProgram;
  17571. CheckSource('TestClassInterface_COM_FunctionResult',
  17572. LinesToStr([ // statements
  17573. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17574. 'rtl.createClass($mod, "TObject", null, function () {',
  17575. ' this.$init = function () {',
  17576. ' };',
  17577. ' this.$final = function () {',
  17578. ' };',
  17579. ' rtl.addIntf(this, $mod.IUnknown);',
  17580. '});',
  17581. 'this.DoDefault = function (i) {',
  17582. ' var Result = null;',
  17583. ' var $ok = false;',
  17584. ' try {',
  17585. ' Result = rtl.setIntfL(Result, i);',
  17586. ' if(Result !== null){',
  17587. ' $ok = true;',
  17588. ' return Result;',
  17589. ' };',
  17590. ' $ok = true;',
  17591. ' } finally {',
  17592. ' if(!$ok) rtl._Release(Result);',
  17593. ' };',
  17594. ' return Result;',
  17595. '};',
  17596. '']),
  17597. LinesToStr([ // $mod.$main
  17598. '']));
  17599. end;
  17600. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  17601. begin
  17602. StartProgram(false);
  17603. Add([
  17604. '{$interfaces com}',
  17605. 'type',
  17606. ' IUnknown = interface',
  17607. ' function _AddRef: longint;',
  17608. ' function _Release: longint;',
  17609. ' end;',
  17610. ' TObject = class(IUnknown)',
  17611. ' function _AddRef: longint; virtual; abstract;',
  17612. ' function _Release: longint; virtual; abstract;',
  17613. ' function GetIntf: IUnknown; virtual;',
  17614. ' end;',
  17615. ' TMouse = class',
  17616. ' function GetIntf: IUnknown; override;',
  17617. ' end;',
  17618. 'function TObject.GetIntf: IUnknown; begin end;',
  17619. 'function TMouse.GetIntf: IUnknown;',
  17620. 'var i: IUnknown;',
  17621. 'begin',
  17622. ' inherited;',
  17623. ' inherited GetIntf;',
  17624. ' inherited GetIntf();',
  17625. ' Result:=inherited GetIntf;',
  17626. ' Result:=inherited GetIntf();',
  17627. ' i:=inherited GetIntf;',
  17628. ' i:=inherited GetIntf();',
  17629. 'end;',
  17630. 'begin',
  17631. '']);
  17632. ConvertProgram;
  17633. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  17634. LinesToStr([ // statements
  17635. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17636. 'rtl.createClass($mod, "TObject", null, function () {',
  17637. ' this.$init = function () {',
  17638. ' };',
  17639. ' this.$final = function () {',
  17640. ' };',
  17641. ' this.GetIntf = function () {',
  17642. ' var Result = null;',
  17643. ' return Result;',
  17644. ' };',
  17645. ' rtl.addIntf(this, $mod.IUnknown);',
  17646. '});',
  17647. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  17648. ' this.GetIntf = function () {',
  17649. ' var Result = null;',
  17650. ' var i = null;',
  17651. ' var $ir = rtl.createIntfRefs();',
  17652. ' var $ok = false;',
  17653. ' try {',
  17654. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  17655. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  17656. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  17657. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17658. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17659. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17660. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17661. ' $ok = true;',
  17662. ' } finally {',
  17663. ' $ir.free();',
  17664. ' rtl._Release(i);',
  17665. ' if (!$ok) rtl._Release(Result);',
  17666. ' };',
  17667. ' return Result;',
  17668. ' };',
  17669. ' rtl.addIntf(this, $mod.IUnknown);',
  17670. '});',
  17671. '']),
  17672. LinesToStr([ // $mod.$main
  17673. '']));
  17674. end;
  17675. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  17676. begin
  17677. StartProgram(false);
  17678. Add([
  17679. '{$interfaces com}',
  17680. 'type',
  17681. ' IUnknown = interface',
  17682. ' function _AddRef: longint;',
  17683. ' function _Release: longint;',
  17684. ' end;',
  17685. ' TObject = class(IUnknown)',
  17686. ' function _AddRef: longint; virtual; abstract;',
  17687. ' function _Release: longint; virtual; abstract;',
  17688. ' end;',
  17689. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  17690. 'begin',
  17691. ' if i is IUnknown then ;',
  17692. ' if o is IUnknown then ;',
  17693. ' if i is TObject then ;',
  17694. ' i:=j as IUnknown;',
  17695. ' i:=o as IUnknown;',
  17696. ' o:=j as TObject;',
  17697. ' i:=IUnknown(j);',
  17698. ' i:=IUnknown(o);',
  17699. ' o:=TObject(i);',
  17700. 'end;',
  17701. 'begin',
  17702. '']);
  17703. ConvertProgram;
  17704. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  17705. LinesToStr([ // statements
  17706. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17707. 'rtl.createClass($mod, "TObject", null, function () {',
  17708. ' this.$init = function () {',
  17709. ' };',
  17710. ' this.$final = function () {',
  17711. ' };',
  17712. ' rtl.addIntf(this, $mod.IUnknown);',
  17713. '});',
  17714. 'this.DoDefault = function (i, j, o) {',
  17715. ' rtl._AddRef(i);',
  17716. ' try {',
  17717. ' if ($mod.IUnknown.isPrototypeOf(i)) ;',
  17718. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  17719. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  17720. ' i = rtl.setIntfL(i, rtl.as(j, $mod.IUnknown));',
  17721. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17722. ' o = rtl.intfAsClass(j, $mod.TObject);',
  17723. ' i = rtl.setIntfL(i, j);',
  17724. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17725. ' o = rtl.intfToClass(i, $mod.TObject);',
  17726. ' } finally {',
  17727. ' rtl._Release(i);',
  17728. ' };',
  17729. '};',
  17730. '']),
  17731. LinesToStr([ // $mod.$main
  17732. '']));
  17733. end;
  17734. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  17735. begin
  17736. StartProgram(false);
  17737. Add([
  17738. '{$interfaces com}',
  17739. 'type',
  17740. ' IUnknown = interface',
  17741. ' function _AddRef: longint;',
  17742. ' function _Release: longint;',
  17743. ' end;',
  17744. ' TObject = class(IUnknown)',
  17745. ' function _AddRef: longint; virtual; abstract;',
  17746. ' function _Release: longint; virtual; abstract;',
  17747. ' end;',
  17748. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  17749. 'var o: TObject;',
  17750. 'begin',
  17751. ' DoIt(v,v,v,v);',
  17752. ' DoIt(o,o,k,k);',
  17753. 'end;',
  17754. 'procedure DoSome;',
  17755. 'var v: IUnknown;',
  17756. 'begin',
  17757. ' DoIt(v,v,v,v);',
  17758. 'end;',
  17759. 'var i: IUnknown;',
  17760. 'begin',
  17761. ' DoIt(i,i,i,i);',
  17762. '']);
  17763. ConvertProgram;
  17764. CheckSource('TestClassInterface_COM_PassAsArg',
  17765. LinesToStr([ // statements
  17766. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17767. 'rtl.createClass($mod, "TObject", null, function () {',
  17768. ' this.$init = function () {',
  17769. ' };',
  17770. ' this.$final = function () {',
  17771. ' };',
  17772. ' rtl.addIntf(this, $mod.IUnknown);',
  17773. '});',
  17774. 'this.DoIt = function (v, j, k, l) {',
  17775. ' var o = null;',
  17776. ' var $ir = rtl.createIntfRefs();',
  17777. ' rtl._AddRef(v);',
  17778. ' try {',
  17779. ' $mod.DoIt(v, v, {',
  17780. ' get: function () {',
  17781. ' return v;',
  17782. ' },',
  17783. ' set: function (w) {',
  17784. ' v = rtl.setIntfL(v, w);',
  17785. ' }',
  17786. ' }, {',
  17787. ' get: function () {',
  17788. ' return v;',
  17789. ' },',
  17790. ' set: function (w) {',
  17791. ' v = rtl.setIntfL(v, w);',
  17792. ' }',
  17793. ' });',
  17794. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  17795. ' } finally {',
  17796. ' $ir.free();',
  17797. ' rtl._Release(v);',
  17798. ' };',
  17799. '};',
  17800. 'this.DoSome = function () {',
  17801. ' var v = null;',
  17802. ' try {',
  17803. ' $mod.DoIt(v, v, {',
  17804. ' get: function () {',
  17805. ' return v;',
  17806. ' },',
  17807. ' set: function (w) {',
  17808. ' v = rtl.setIntfL(v, w);',
  17809. ' }',
  17810. ' }, {',
  17811. ' get: function () {',
  17812. ' return v;',
  17813. ' },',
  17814. ' set: function (w) {',
  17815. ' v = rtl.setIntfL(v, w);',
  17816. ' }',
  17817. ' });',
  17818. ' } finally {',
  17819. ' rtl._Release(v);',
  17820. ' };',
  17821. '};',
  17822. 'this.i = null;',
  17823. '']),
  17824. LinesToStr([ // $mod.$main
  17825. '$mod.DoIt($mod.i, $mod.i, {',
  17826. ' p: $mod,',
  17827. ' get: function () {',
  17828. ' return this.p.i;',
  17829. ' },',
  17830. ' set: function (v) {',
  17831. ' rtl.setIntfP(this.p, "i", v);',
  17832. ' }',
  17833. '}, {',
  17834. ' p: $mod,',
  17835. ' get: function () {',
  17836. ' return this.p.i;',
  17837. ' },',
  17838. ' set: function (v) {',
  17839. ' rtl.setIntfP(this.p, "i", v);',
  17840. ' }',
  17841. '});',
  17842. '']));
  17843. end;
  17844. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  17845. begin
  17846. StartProgram(false);
  17847. Add([
  17848. '{$interfaces com}',
  17849. 'type',
  17850. ' IUnknown = interface',
  17851. ' function _AddRef: longint;',
  17852. ' function _Release: longint;',
  17853. ' end;',
  17854. ' TObject = class(IUnknown)',
  17855. ' function _AddRef: longint; virtual; abstract;',
  17856. ' function _Release: longint; virtual; abstract;',
  17857. ' end;',
  17858. 'procedure DoIt(out i);',
  17859. 'begin end;',
  17860. 'procedure DoSome;',
  17861. 'var v: IUnknown;',
  17862. 'begin',
  17863. ' DoIt(v);',
  17864. 'end;',
  17865. 'function GetIt: IUnknown;',
  17866. 'begin',
  17867. ' DoIt(Result);',
  17868. 'end;',
  17869. 'var i: IUnknown;',
  17870. 'begin',
  17871. ' DoIt(i);',
  17872. '']);
  17873. ConvertProgram;
  17874. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  17875. LinesToStr([ // statements
  17876. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17877. 'rtl.createClass($mod, "TObject", null, function () {',
  17878. ' this.$init = function () {',
  17879. ' };',
  17880. ' this.$final = function () {',
  17881. ' };',
  17882. ' rtl.addIntf(this, $mod.IUnknown);',
  17883. '});',
  17884. 'this.DoIt = function (i) {',
  17885. '};',
  17886. 'this.DoSome = function () {',
  17887. ' var v = null;',
  17888. ' try {',
  17889. ' $mod.DoIt({',
  17890. ' get: function () {',
  17891. ' return v;',
  17892. ' },',
  17893. ' set: function (w) {',
  17894. ' v = w;',
  17895. ' }',
  17896. ' });',
  17897. ' } finally {',
  17898. ' rtl._Release(v);',
  17899. ' };',
  17900. '};',
  17901. 'this.GetIt = function () {',
  17902. ' var Result = null;',
  17903. ' var $ok = false;',
  17904. ' try {',
  17905. ' $mod.DoIt({',
  17906. ' get: function () {',
  17907. ' return Result;',
  17908. ' },',
  17909. ' set: function (v) {',
  17910. ' Result = v;',
  17911. ' }',
  17912. ' });',
  17913. ' $ok = true;',
  17914. ' } finally {',
  17915. ' if (!$ok) rtl._Release(Result);',
  17916. ' };',
  17917. ' return Result;',
  17918. '};',
  17919. 'this.i = null;',
  17920. '']),
  17921. LinesToStr([ // $mod.$main
  17922. 'try {',
  17923. ' $mod.DoIt({',
  17924. ' p: $mod,',
  17925. ' get: function () {',
  17926. ' return this.p.i;',
  17927. ' },',
  17928. ' set: function (v) {',
  17929. ' this.p.i = v;',
  17930. ' }',
  17931. ' });',
  17932. '} finally {',
  17933. ' rtl._Release($mod.i);',
  17934. '};',
  17935. '']));
  17936. end;
  17937. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  17938. begin
  17939. StartProgram(false);
  17940. Add([
  17941. '{$interfaces com}',
  17942. 'type',
  17943. ' IUnknown = interface',
  17944. ' function _AddRef: longint;',
  17945. ' function _Release: longint;',
  17946. ' end;',
  17947. ' TObject = class(IUnknown)',
  17948. ' function _AddRef: longint; virtual; abstract;',
  17949. ' function _Release: longint; virtual; abstract;',
  17950. ' end;',
  17951. 'function GetIt: IUnknown;',
  17952. 'begin',
  17953. 'end;',
  17954. 'procedure DoSome;',
  17955. 'var v: IUnknown;',
  17956. ' i: longint;',
  17957. 'begin',
  17958. ' v:=GetIt;',
  17959. ' v:=GetIt();',
  17960. ' GetIt()._AddRef;',
  17961. ' i:=GetIt()._AddRef;',
  17962. 'end;',
  17963. 'var v: IUnknown;',
  17964. ' i: longint;',
  17965. 'begin',
  17966. ' v:=GetIt;',
  17967. ' v:=GetIt();',
  17968. ' GetIt()._AddRef;',
  17969. ' i:=GetIt()._AddRef;',
  17970. '']);
  17971. ConvertProgram;
  17972. CheckSource('TestClassInterface_COM_FunctionInExpr',
  17973. LinesToStr([ // statements
  17974. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17975. 'rtl.createClass($mod, "TObject", null, function () {',
  17976. ' this.$init = function () {',
  17977. ' };',
  17978. ' this.$final = function () {',
  17979. ' };',
  17980. ' rtl.addIntf(this, $mod.IUnknown);',
  17981. '});',
  17982. 'this.GetIt = function () {',
  17983. ' var Result = null;',
  17984. ' return Result;',
  17985. '};',
  17986. 'this.DoSome = function () {',
  17987. ' var v = null;',
  17988. ' var i = 0;',
  17989. ' var $ir = rtl.createIntfRefs();',
  17990. ' try {',
  17991. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17992. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17993. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17994. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17995. ' } finally {',
  17996. ' $ir.free();',
  17997. ' rtl._Release(v);',
  17998. ' };',
  17999. '};',
  18000. 'this.v = null;',
  18001. 'this.i = 0;',
  18002. '']),
  18003. LinesToStr([ // $mod.$main
  18004. 'var $ir = rtl.createIntfRefs();',
  18005. 'try {',
  18006. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18007. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18008. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18009. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18010. '} finally {',
  18011. ' $ir.free();',
  18012. '};',
  18013. '']));
  18014. end;
  18015. procedure TTestModule.TestClassInterface_COM_Property;
  18016. begin
  18017. StartProgram(false);
  18018. Add([
  18019. '{$interfaces com}',
  18020. 'type',
  18021. ' IUnknown = interface',
  18022. ' function _AddRef: longint;',
  18023. ' function _Release: longint;',
  18024. ' end;',
  18025. ' TObject = class(IUnknown)',
  18026. ' FAnt: IUnknown;',
  18027. ' function _AddRef: longint; virtual; abstract;',
  18028. ' function _Release: longint; virtual; abstract;',
  18029. ' function GetBird: IUnknown; virtual; abstract;',
  18030. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18031. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18032. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18033. ' property Ant: IUnknown read FAnt write FAnt;',
  18034. ' property Bird: IUnknown read GetBird write SetBird;',
  18035. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18036. ' end;',
  18037. 'procedure DoIt;',
  18038. 'var',
  18039. ' o: TObject;',
  18040. ' v: IUnknown;',
  18041. 'begin',
  18042. ' v:=o.Ant;',
  18043. ' o.Ant:=v;',
  18044. ' o.Ant:=o.Ant;',
  18045. ' v:=o.Bird;',
  18046. ' o.Bird:=v;',
  18047. ' o.Bird:=o.Bird;',
  18048. ' v:=o.Items[1];',
  18049. ' o.Items[2]:=v;',
  18050. ' o.Items[3]:=o.Items[4];',
  18051. ' v:=o[5];',
  18052. ' o[6]:=v;',
  18053. ' o[7]:=o[8];',
  18054. 'end;',
  18055. 'begin',
  18056. '']);
  18057. ConvertProgram;
  18058. CheckSource('TestClassInterface_COM_Property',
  18059. LinesToStr([ // statements
  18060. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18061. 'rtl.createClass($mod, "TObject", null, function () {',
  18062. ' this.$init = function () {',
  18063. ' this.FAnt = null;',
  18064. ' };',
  18065. ' this.$final = function () {',
  18066. ' this.FAnt = undefined;',
  18067. ' };',
  18068. ' rtl.addIntf(this, $mod.IUnknown);',
  18069. '});',
  18070. 'this.DoIt = function () {',
  18071. ' var o = null;',
  18072. ' var v = null;',
  18073. ' var $ir = rtl.createIntfRefs();',
  18074. ' try {',
  18075. ' v = rtl.setIntfL(v, o.FAnt);',
  18076. ' rtl.setIntfP(o, "FAnt", v);',
  18077. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  18078. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  18079. ' o.SetBird(v);',
  18080. ' o.SetBird($ir.ref(1, o.GetBird()));',
  18081. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  18082. ' o.SetItems(2, v);',
  18083. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  18084. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  18085. ' o.SetItems(6, v);',
  18086. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  18087. ' } finally {',
  18088. ' $ir.free();',
  18089. ' rtl._Release(v);',
  18090. ' };',
  18091. '};',
  18092. '']),
  18093. LinesToStr([ // $mod.$main
  18094. '']));
  18095. end;
  18096. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  18097. begin
  18098. StartProgram(false);
  18099. Add([
  18100. '{$interfaces com}',
  18101. 'type',
  18102. ' IUnknown = interface',
  18103. ' function _AddRef: longint;',
  18104. ' function _Release: longint;',
  18105. ' function GetBird: IUnknown;',
  18106. ' procedure SetBird(Value: IUnknown);',
  18107. ' function GetItems(Index: longint): IUnknown;',
  18108. ' procedure SetItems(Index: longint; Value: IUnknown);',
  18109. ' property Bird: IUnknown read GetBird write SetBird;',
  18110. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18111. ' end;',
  18112. ' TObject = class(IUnknown)',
  18113. ' function _AddRef: longint; virtual; abstract;',
  18114. ' function _Release: longint; virtual; abstract;',
  18115. ' function GetBird: IUnknown; virtual; abstract;',
  18116. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18117. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18118. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18119. ' end;',
  18120. 'procedure DoIt;',
  18121. 'var',
  18122. ' o: TObject;',
  18123. ' v: IUnknown;',
  18124. 'begin',
  18125. ' v:=v.Items[1];',
  18126. ' v.Items[2]:=v;',
  18127. ' v.Items[3]:=v.Items[4];',
  18128. ' v:=v[5];',
  18129. ' v[6]:=v;',
  18130. ' v[7]:=v[8];',
  18131. ' v[9].Bird.Bird:=v;',
  18132. ' v:=v.Bird[10].Bird',
  18133. 'end;',
  18134. 'begin',
  18135. '']);
  18136. ConvertProgram;
  18137. CheckSource('TestClassInterface_COM_IntfProperty',
  18138. LinesToStr([ // statements
  18139. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  18140. ' "_AddRef",',
  18141. ' "_Release",',
  18142. ' "GetBird",',
  18143. ' "SetBird",',
  18144. ' "GetItems",',
  18145. ' "SetItems"',
  18146. '], null);',
  18147. 'rtl.createClass($mod, "TObject", null, function () {',
  18148. ' this.$init = function () {',
  18149. ' };',
  18150. ' this.$final = function () {',
  18151. ' };',
  18152. ' rtl.addIntf(this, $mod.IUnknown);',
  18153. '});',
  18154. 'this.DoIt = function () {',
  18155. ' var o = null;',
  18156. ' var v = null;',
  18157. ' var $ir = rtl.createIntfRefs();',
  18158. ' try {',
  18159. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  18160. ' v.SetItems(2, v);',
  18161. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  18162. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  18163. ' v.SetItems(6, v);',
  18164. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  18165. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  18166. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  18167. ' } finally {',
  18168. ' $ir.free();',
  18169. ' rtl._Release(v);',
  18170. ' };',
  18171. '};',
  18172. '']),
  18173. LinesToStr([ // $mod.$main
  18174. '']));
  18175. end;
  18176. procedure TTestModule.TestClassInterface_COM_Delegation;
  18177. begin
  18178. StartProgram(false);
  18179. Add([
  18180. '{$interfaces com}',
  18181. 'type',
  18182. ' IUnknown = interface',
  18183. ' function _AddRef: longint;',
  18184. ' function _Release: longint;',
  18185. ' end;',
  18186. ' IBird = interface(IUnknown)',
  18187. ' procedure Fly(s: string);',
  18188. ' end;',
  18189. ' IEagle = interface(IBird) end;',
  18190. ' IDove = interface(IBird) end;',
  18191. ' ISwallow = interface(IBird) end;',
  18192. ' TObject = class',
  18193. ' end;',
  18194. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18195. ' function _AddRef: longint; virtual; abstract;',
  18196. ' function _Release: longint; virtual; abstract;',
  18197. ' procedure Fly(s: string); virtual; abstract;',
  18198. ' end;',
  18199. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18200. ' function _AddRef: longint; virtual; abstract;',
  18201. ' function _Release: longint; virtual; abstract;',
  18202. ' FBirdIntf: IBird;',
  18203. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18204. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18205. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18206. ' FDoveObj: TBird;',
  18207. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18208. ' function GetSwallowObj: TBird; virtual; abstract;',
  18209. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18210. ' end;',
  18211. 'begin',
  18212. '']);
  18213. ConvertProgram;
  18214. CheckSource('TestClassInterface_COM_Delegation',
  18215. LinesToStr([ // statements
  18216. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18217. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  18218. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  18219. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  18220. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  18221. 'rtl.createClass($mod, "TObject", null, function () {',
  18222. ' this.$init = function () {',
  18223. ' };',
  18224. ' this.$final = function () {',
  18225. ' };',
  18226. '});',
  18227. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18228. ' rtl.addIntf(this, $mod.IBird);',
  18229. ' rtl.addIntf(this, $mod.IEagle);',
  18230. ' rtl.addIntf(this, $mod.IDove);',
  18231. ' rtl.addIntf(this, $mod.ISwallow);',
  18232. '});',
  18233. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18234. ' this.$init = function () {',
  18235. ' $mod.TObject.$init.call(this);',
  18236. ' this.FBirdIntf = null;',
  18237. ' this.FDoveObj = null;',
  18238. ' };',
  18239. ' this.$final = function () {',
  18240. ' this.FBirdIntf = undefined;',
  18241. ' this.FDoveObj = undefined;',
  18242. ' $mod.TObject.$final.call(this);',
  18243. ' };',
  18244. ' this.$intfmaps = {',
  18245. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  18246. ' return rtl._AddRef(this.FBirdIntf);',
  18247. ' },',
  18248. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  18249. ' return this.GetEagleIntf();',
  18250. ' },',
  18251. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  18252. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  18253. ' },',
  18254. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  18255. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18256. ' }',
  18257. ' };',
  18258. '});',
  18259. '']),
  18260. LinesToStr([ // $mod.$main
  18261. '']));
  18262. end;
  18263. procedure TTestModule.TestClassInterface_COM_With;
  18264. begin
  18265. StartProgram(false);
  18266. Add([
  18267. '{$interfaces com}',
  18268. 'type',
  18269. ' IUnknown = interface',
  18270. ' function _AddRef: longint;',
  18271. ' function _Release: longint;',
  18272. ' function GetAnt: IUnknown;',
  18273. ' property Ant: IUnknown read GetAnt;',
  18274. ' end;',
  18275. ' TObject = class(IUnknown)',
  18276. ' function _AddRef: longint; virtual; abstract;',
  18277. ' function _Release: longint; virtual; abstract;',
  18278. ' function GetAnt: IUnknown; virtual; abstract;',
  18279. ' property Ant: IUnknown read GetAnt;',
  18280. ' end;',
  18281. 'procedure DoIt;',
  18282. 'var',
  18283. ' i: IUnknown;',
  18284. 'begin',
  18285. ' with i do ',
  18286. ' GetAnt;',
  18287. ' with i.Ant, Ant do ',
  18288. ' GetAnt;',
  18289. 'end;',
  18290. 'begin',
  18291. '']);
  18292. ConvertProgram;
  18293. CheckSource('TestClassInterface_COM_With',
  18294. LinesToStr([ // statements
  18295. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  18296. 'rtl.createClass($mod, "TObject", null, function () {',
  18297. ' this.$init = function () {',
  18298. ' };',
  18299. ' this.$final = function () {',
  18300. ' };',
  18301. ' rtl.addIntf(this, $mod.IUnknown);',
  18302. '});',
  18303. 'this.DoIt = function () {',
  18304. ' var i = null;',
  18305. ' var $ir = rtl.createIntfRefs();',
  18306. ' try {',
  18307. ' $ir.ref(1, i.GetAnt());',
  18308. ' var $with1 = $ir.ref(2, i.GetAnt());',
  18309. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  18310. ' $ir.ref(4, $with2.GetAnt());',
  18311. ' } finally {',
  18312. ' $ir.free();',
  18313. ' };',
  18314. '};',
  18315. '']),
  18316. LinesToStr([ // $mod.$main
  18317. '']));
  18318. end;
  18319. procedure TTestModule.TestClassInterface_COM_ForIn;
  18320. begin
  18321. StartProgram(false);
  18322. Add([
  18323. '{$interfaces com}',
  18324. 'type',
  18325. ' IUnknown = interface end;',
  18326. ' TObject = class',
  18327. ' Id: longint;',
  18328. ' end;',
  18329. ' IEnumerator = interface(IUnknown)',
  18330. ' function GetCurrent: TObject;',
  18331. ' function MoveNext: Boolean;',
  18332. ' property Current: TObject read GetCurrent;',
  18333. ' end;',
  18334. ' IEnumerable = interface(IUnknown)',
  18335. ' function GetEnumerator: IEnumerator;',
  18336. ' end;',
  18337. 'var',
  18338. ' o: TObject;',
  18339. ' i: IEnumerable;',
  18340. 'begin',
  18341. ' for o in i do o.Id:=3;',
  18342. '']);
  18343. ConvertProgram;
  18344. CheckSource('TestClassInterface_COM_ForIn',
  18345. LinesToStr([ // statements
  18346. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18347. 'rtl.createClass($mod, "TObject", null, function () {',
  18348. ' this.$init = function () {',
  18349. ' this.Id = 0;',
  18350. ' };',
  18351. ' this.$final = function () {',
  18352. ' };',
  18353. '});',
  18354. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18355. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18356. 'this.o = null;',
  18357. 'this.i = null;',
  18358. '']),
  18359. LinesToStr([ // $mod.$main
  18360. 'var $in1 = $mod.i.GetEnumerator();',
  18361. 'try {',
  18362. ' while ($in1.MoveNext()) {',
  18363. ' $mod.o = $in1.GetCurrent();',
  18364. ' $mod.o.Id = 3;',
  18365. ' }',
  18366. '} finally {',
  18367. ' rtl._Release($in1)',
  18368. '};',
  18369. '']));
  18370. end;
  18371. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  18372. begin
  18373. StartProgram(false);
  18374. Add([
  18375. '{$interfaces com}',
  18376. 'type',
  18377. ' IUnknown = interface',
  18378. ' function _AddRef: longint;',
  18379. ' function _Release: longint;',
  18380. ' end;',
  18381. ' TObject = class',
  18382. ' end;',
  18383. ' TArrOfIntf = array of IUnknown;',
  18384. 'begin',
  18385. '']);
  18386. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  18387. ConvertProgram;
  18388. end;
  18389. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  18390. begin
  18391. StartProgram(false);
  18392. Add([
  18393. '{$interfaces com}',
  18394. 'type',
  18395. ' IUnknown = interface',
  18396. ' function _AddRef: longint;',
  18397. ' function _Release: longint;',
  18398. ' end;',
  18399. ' TRec = record',
  18400. ' i: IUnknown;',
  18401. ' end;',
  18402. 'begin',
  18403. '']);
  18404. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  18405. ConvertProgram;
  18406. end;
  18407. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  18408. begin
  18409. StartUnit(false);
  18410. Add([
  18411. '{$interfaces com}',
  18412. 'interface',
  18413. 'implementation',
  18414. 'type',
  18415. ' IUnknown = interface',
  18416. ' function _AddRef: longint;',
  18417. ' end;',
  18418. ' TObject = class(IUnknown)',
  18419. ' function _AddRef: longint;',
  18420. ' end;',
  18421. 'function TObject._AddRef: longint; begin end;',
  18422. 'var i: IUnknown;',
  18423. ' o: TObject;',
  18424. 'initialization',
  18425. ' i:=nil;',
  18426. ' i:=i;',
  18427. ' i:=o;',
  18428. ' if (o as IUnknown)=nil then ;',
  18429. '']);
  18430. ConvertUnit;
  18431. CheckSource('TestClassInterface_COM_UnitInitialization',
  18432. LinesToStr([ // statements
  18433. 'var $impl = $mod.$impl;',
  18434. '']),
  18435. LinesToStr([ // this.$init
  18436. 'var $ir = rtl.createIntfRefs();',
  18437. 'try {',
  18438. ' rtl.setIntfP($impl, "i", null);',
  18439. ' rtl.setIntfP($impl, "i", $impl.i);',
  18440. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  18441. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  18442. '} finally {',
  18443. ' $ir.free();',
  18444. '};',
  18445. '']),
  18446. LinesToStr([ // implementation
  18447. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  18448. 'rtl.createClass($impl, "TObject", null, function () {',
  18449. ' this.$init = function () {',
  18450. ' };',
  18451. ' this.$final = function () {',
  18452. ' };',
  18453. ' this._AddRef = function () {',
  18454. ' var Result = 0;',
  18455. ' return Result;',
  18456. ' };',
  18457. ' rtl.addIntf(this, $impl.IUnknown);',
  18458. '});',
  18459. '$impl.i = null;',
  18460. '$impl.o = null;',
  18461. ''])
  18462. );
  18463. end;
  18464. procedure TTestModule.TestClassInterface_GUID;
  18465. begin
  18466. StartProgram(false);
  18467. Add([
  18468. '{$interfaces corba}',
  18469. 'type',
  18470. ' IUnknown = interface',
  18471. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18472. ' end;',
  18473. ' TObject = class end;',
  18474. ' TGUID = record D1, D2, D3, D4: word; end;',
  18475. ' TAliasGUID = TGUID;',
  18476. ' TGUIDString = type string;',
  18477. ' TAliasGUIDString = TGUIDString;',
  18478. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  18479. 'begin end;',
  18480. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  18481. 'begin end;',
  18482. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  18483. 'begin end;',
  18484. 'var',
  18485. ' i: IUnknown;',
  18486. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  18487. ' s: TAliasGUIDString;',
  18488. 'begin',
  18489. ' DoConstGUIDIt(IUnknown);',
  18490. ' DoDefGUID(IUnknown);',
  18491. ' DoStr(IUnknown);',
  18492. ' DoConstGUIDIt(i);',
  18493. ' DoDefGUID(i);',
  18494. ' DoStr(i);',
  18495. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18496. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18497. ' DoStr(g);',
  18498. ' g:=i;',
  18499. ' g:=IUnknown;',
  18500. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18501. ' s:=i;',
  18502. ' s:=IUnknown;',
  18503. ' s:=g;',
  18504. ' if g=i then ;',
  18505. ' if i=g then ;',
  18506. ' if g=IUnknown then ;',
  18507. ' if IUnknown=g then ;',
  18508. ' if s=i then ;',
  18509. ' if i=s then ;',
  18510. ' if s=IUnknown then ;',
  18511. ' if IUnknown=s then ;',
  18512. ' if s=g then ;',
  18513. ' if g=s then ;',
  18514. '']);
  18515. ConvertProgram;
  18516. CheckSource('TestClassInterface_GUID',
  18517. LinesToStr([ // statements
  18518. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18519. 'rtl.createClass($mod, "TObject", null, function () {',
  18520. ' this.$init = function () {',
  18521. ' };',
  18522. ' this.$final = function () {',
  18523. ' };',
  18524. '});',
  18525. 'rtl.recNewT($mod, "TGUID", function () {',
  18526. ' this.D1 = 0;',
  18527. ' this.D2 = 0;',
  18528. ' this.D3 = 0;',
  18529. ' this.D4 = 0;',
  18530. ' this.$eq = function (b) {',
  18531. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18532. ' };',
  18533. ' this.$assign = function (s) {',
  18534. ' this.D1 = s.D1;',
  18535. ' this.D2 = s.D2;',
  18536. ' this.D3 = s.D3;',
  18537. ' this.D4 = s.D4;',
  18538. ' return this;',
  18539. ' };',
  18540. '});',
  18541. 'this.DoConstGUIDIt = function (g) {',
  18542. '};',
  18543. 'this.DoDefGUID = function (g) {',
  18544. '};',
  18545. 'this.DoStr = function (s) {',
  18546. '};',
  18547. 'this.i = null;',
  18548. 'this.g = $mod.TGUID.$clone({',
  18549. ' D1: 0xD91C9AF4,',
  18550. ' D2: 0x3C93,',
  18551. ' D3: 0x420F,',
  18552. ' D4: [',
  18553. ' 0xA3,',
  18554. ' 0x03,',
  18555. ' 0xBF,',
  18556. ' 0x5B,',
  18557. ' 0xA8,',
  18558. ' 0x2B,',
  18559. ' 0xFD,',
  18560. ' 0x23',
  18561. ' ]',
  18562. '});',
  18563. 'this.s = "";',
  18564. '']),
  18565. LinesToStr([ // $mod.$main
  18566. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  18567. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  18568. '$mod.DoStr($mod.IUnknown.$guid);',
  18569. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  18570. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  18571. '$mod.DoStr($mod.i.$guid);',
  18572. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18573. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18574. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  18575. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  18576. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  18577. '$mod.g.$assign({',
  18578. ' D1: 0xD91C9AF4,',
  18579. ' D2: 0x3C93,',
  18580. ' D3: 0x420F,',
  18581. ' D4: [',
  18582. ' 0xA3,',
  18583. ' 0x03,',
  18584. ' 0xBF,',
  18585. ' 0x5B,',
  18586. ' 0xA8,',
  18587. ' 0x2B,',
  18588. ' 0xFD,',
  18589. ' 0x23',
  18590. ' ]',
  18591. '});',
  18592. '$mod.s = $mod.i.$guid;',
  18593. '$mod.s = $mod.IUnknown.$guid;',
  18594. '$mod.s = rtl.guidrToStr($mod.g);',
  18595. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18596. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18597. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18598. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18599. 'if ($mod.s === $mod.i.$guid) ;',
  18600. 'if ($mod.i.$guid === $mod.s) ;',
  18601. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  18602. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  18603. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18604. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18605. '']));
  18606. end;
  18607. procedure TTestModule.TestClassInterface_GUIDProperty;
  18608. begin
  18609. StartProgram(false);
  18610. Add([
  18611. '{$interfaces corba}',
  18612. 'type',
  18613. ' IUnknown = interface',
  18614. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18615. ' end;',
  18616. ' TGUID = record D1, D2, D3, D4: word; end;',
  18617. ' TAliasGUID = TGUID;',
  18618. ' TGUIDString = type string;',
  18619. ' TAliasGUIDString = TGUIDString;',
  18620. ' TObject = class',
  18621. ' function GetG: TAliasGUID; virtual; abstract;',
  18622. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  18623. ' function GetS: TAliasGUIDString; virtual; abstract;',
  18624. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  18625. ' property g: TAliasGUID read GetG write SetG;',
  18626. ' property s: TAliasGUIDString read GetS write SetS;',
  18627. ' end;',
  18628. 'var o: TObject;',
  18629. 'begin',
  18630. ' o.g:=IUnknown;',
  18631. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18632. ' o.s:=IUnknown;',
  18633. ' o.s:=o.g;',
  18634. '']);
  18635. ConvertProgram;
  18636. CheckSource('TestClassInterface_GUIDProperty',
  18637. LinesToStr([ // statements
  18638. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18639. 'rtl.recNewT($mod, "TGUID", function () {',
  18640. ' this.D1 = 0;',
  18641. ' this.D2 = 0;',
  18642. ' this.D3 = 0;',
  18643. ' this.D4 = 0;',
  18644. ' this.$eq = function (b) {',
  18645. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18646. ' };',
  18647. ' this.$assign = function (s) {',
  18648. ' this.D1 = s.D1;',
  18649. ' this.D2 = s.D2;',
  18650. ' this.D3 = s.D3;',
  18651. ' this.D4 = s.D4;',
  18652. ' return this;',
  18653. ' };',
  18654. '});',
  18655. 'rtl.createClass($mod, "TObject", null, function () {',
  18656. ' this.$init = function () {',
  18657. ' };',
  18658. ' this.$final = function () {',
  18659. ' };',
  18660. '});',
  18661. 'this.o = null;',
  18662. '']),
  18663. LinesToStr([ // $mod.$main
  18664. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  18665. '$mod.o.SetG({',
  18666. ' D1: 0xD91C9AF4,',
  18667. ' D2: 0x3C93,',
  18668. ' D3: 0x420F,',
  18669. ' D4: [',
  18670. ' 0xA3,',
  18671. ' 0x03,',
  18672. ' 0xBF,',
  18673. ' 0x5B,',
  18674. ' 0xA8,',
  18675. ' 0x2B,',
  18676. ' 0xFD,',
  18677. ' 0x23',
  18678. ' ]',
  18679. '});',
  18680. '$mod.o.SetS($mod.IUnknown.$guid);',
  18681. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  18682. '']));
  18683. end;
  18684. procedure TTestModule.TestClassHelper_ClassVar;
  18685. begin
  18686. StartProgram(false);
  18687. Add([
  18688. 'type',
  18689. ' TObject = class',
  18690. ' end;',
  18691. ' THelper = class helper for TObject',
  18692. ' const',
  18693. ' One = 1;',
  18694. ' Two: word = 2;',
  18695. ' class var',
  18696. ' Glob: word;',
  18697. ' function Foo(w: word): word;',
  18698. ' class function Bar(w: word): word;',
  18699. ' end;',
  18700. 'function THelper.foo(w: word): word;',
  18701. 'begin',
  18702. ' Result:=w;',
  18703. ' Two:=One+w;',
  18704. ' Glob:=Glob;',
  18705. ' Result:=Self.Glob;',
  18706. ' Self.Glob:=Self.Glob;',
  18707. ' with Self do Glob:=Glob;',
  18708. 'end;',
  18709. 'class function THelper.bar(w: word): word;',
  18710. 'begin',
  18711. ' Result:=w;',
  18712. ' Two:=One;',
  18713. ' Glob:=Glob;',
  18714. ' Self.Glob:=Self.Glob;',
  18715. ' with Self do Glob:=Glob;',
  18716. 'end;',
  18717. 'var o: TObject;',
  18718. 'begin',
  18719. ' tobject.two:=tobject.one;',
  18720. ' tobject.Glob:=tobject.Glob;',
  18721. ' with tobject do begin',
  18722. ' two:=one;',
  18723. ' Glob:=Glob;',
  18724. ' end;',
  18725. ' o.two:=o.one;',
  18726. ' o.Glob:=o.Glob;',
  18727. ' with o do begin',
  18728. ' two:=one;',
  18729. ' Glob:=Glob;',
  18730. ' end;',
  18731. '']);
  18732. ConvertProgram;
  18733. CheckSource('TestClassHelper_ClassVar',
  18734. LinesToStr([ // statements
  18735. 'rtl.createClass($mod, "TObject", null, function () {',
  18736. ' this.$init = function () {',
  18737. ' };',
  18738. ' this.$final = function () {',
  18739. ' };',
  18740. '});',
  18741. 'rtl.createHelper($mod, "THelper", null, function () {',
  18742. ' this.One = 1;',
  18743. ' this.Two = 2;',
  18744. ' this.Glob = 0;',
  18745. ' this.Foo = function (w) {',
  18746. ' var Result = 0;',
  18747. ' Result = w;',
  18748. ' $mod.THelper.Two = 1 + w;',
  18749. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18750. ' Result = $mod.THelper.Glob;',
  18751. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18752. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18753. ' return Result;',
  18754. ' };',
  18755. ' this.Bar = function (w) {',
  18756. ' var Result = 0;',
  18757. ' Result = w;',
  18758. ' $mod.THelper.Two = 1;',
  18759. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18760. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18761. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18762. ' return Result;',
  18763. ' };',
  18764. '});',
  18765. 'this.o = null;',
  18766. '']),
  18767. LinesToStr([ // $mod.$main
  18768. '$mod.THelper.Two = 1;',
  18769. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18770. 'var $with1 = $mod.TObject;',
  18771. '$mod.THelper.Two = 1;',
  18772. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18773. '$mod.THelper.Two = 1;',
  18774. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18775. 'var $with2 = $mod.o;',
  18776. '$mod.THelper.Two = 1;',
  18777. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18778. '']));
  18779. end;
  18780. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  18781. begin
  18782. StartProgram(false);
  18783. Add([
  18784. 'type',
  18785. ' TObject = class',
  18786. ' FSize: word;',
  18787. ' property Size: word read FSize write FSize;',
  18788. ' end;',
  18789. ' THelper = class helper for TObject',
  18790. ' function Foo(w: word = 1): word;',
  18791. ' end;',
  18792. 'function THelper.foo(w: word): word;',
  18793. 'begin',
  18794. ' Result:=Size;',
  18795. ' Size:=Size+2;',
  18796. ' Self.Size:=Self.Size+3;',
  18797. ' FSize:=FSize+4;',
  18798. ' Self.FSize:=Self.FSize+5;',
  18799. ' with Self do begin',
  18800. ' Size:=Size+6;',
  18801. ' FSize:=FSize+7;',
  18802. ' FSize:=FSize+8;',
  18803. ' end;',
  18804. 'end;',
  18805. 'begin',
  18806. '']);
  18807. ConvertProgram;
  18808. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  18809. LinesToStr([ // statements
  18810. 'rtl.createClass($mod, "TObject", null, function () {',
  18811. ' this.$init = function () {',
  18812. ' this.FSize = 0;',
  18813. ' };',
  18814. ' this.$final = function () {',
  18815. ' };',
  18816. '});',
  18817. 'rtl.createHelper($mod, "THelper", null, function () {',
  18818. ' this.Foo = function (w) {',
  18819. ' var Result = 0;',
  18820. ' Result = this.FSize;',
  18821. ' this.FSize = this.FSize + 2;',
  18822. ' this.FSize = this.FSize + 3;',
  18823. ' this.FSize = this.FSize + 4;',
  18824. ' this.FSize = this.FSize + 5;',
  18825. ' this.FSize = this.FSize + 6;',
  18826. ' this.FSize = this.FSize + 7;',
  18827. ' this.FSize = this.FSize + 8;',
  18828. ' return Result;',
  18829. ' };',
  18830. '});',
  18831. '']),
  18832. LinesToStr([ // $mod.$main
  18833. '']));
  18834. end;
  18835. procedure TTestModule.TestClassHelper_Method_Call;
  18836. begin
  18837. StartProgram(false);
  18838. Add([
  18839. 'type',
  18840. ' TObject = class',
  18841. ' procedure Run(w: word = 10);',
  18842. ' end;',
  18843. ' THelper = class helper for TObject',
  18844. ' function Foo(w: word = 1): word;',
  18845. ' end;',
  18846. 'procedure TObject.Run(w: word);',
  18847. 'begin',
  18848. ' Foo;',
  18849. ' Foo();',
  18850. ' Foo(2);',
  18851. ' Self.Foo;',
  18852. ' Self.Foo();',
  18853. ' Self.Foo(3);',
  18854. ' with Self do begin',
  18855. ' Foo;',
  18856. ' Foo();',
  18857. ' Foo(4);',
  18858. ' end;',
  18859. 'end;',
  18860. 'function THelper.foo(w: word): word;',
  18861. 'begin',
  18862. ' Run;',
  18863. ' Run();',
  18864. ' Run(11);',
  18865. ' Foo;',
  18866. ' Foo();',
  18867. ' Foo(12);',
  18868. ' Self.Foo;',
  18869. ' Self.Foo();',
  18870. ' Self.Foo(13);',
  18871. ' with Self do begin',
  18872. ' Foo;',
  18873. ' Foo();',
  18874. ' Foo(14);',
  18875. ' end;',
  18876. 'end;',
  18877. 'var Obj: TObject;',
  18878. 'begin',
  18879. ' obj.Foo;',
  18880. ' obj.Foo();',
  18881. ' obj.Foo(21);',
  18882. ' with obj do begin',
  18883. ' Foo;',
  18884. ' Foo();',
  18885. ' Foo(22);',
  18886. ' end;',
  18887. '']);
  18888. ConvertProgram;
  18889. CheckSource('TestClassHelper_Method_Call',
  18890. LinesToStr([ // statements
  18891. 'rtl.createClass($mod, "TObject", null, function () {',
  18892. ' this.$init = function () {',
  18893. ' };',
  18894. ' this.$final = function () {',
  18895. ' };',
  18896. ' this.Run = function (w) {',
  18897. ' $mod.THelper.Foo.call(this, 1);',
  18898. ' $mod.THelper.Foo.call(this, 1);',
  18899. ' $mod.THelper.Foo.call(this, 2);',
  18900. ' $mod.THelper.Foo.call(this, 1);',
  18901. ' $mod.THelper.Foo.call(this, 1);',
  18902. ' $mod.THelper.Foo.call(this, 3);',
  18903. ' $mod.THelper.Foo.call(this, 1);',
  18904. ' $mod.THelper.Foo.call(this, 1);',
  18905. ' $mod.THelper.Foo.call(this, 4);',
  18906. ' };',
  18907. '});',
  18908. 'rtl.createHelper($mod, "THelper", null, function () {',
  18909. ' this.Foo = function (w) {',
  18910. ' var Result = 0;',
  18911. ' this.Run(10);',
  18912. ' this.Run(10);',
  18913. ' this.Run(11);',
  18914. ' $mod.THelper.Foo.call(this, 1);',
  18915. ' $mod.THelper.Foo.call(this, 1);',
  18916. ' $mod.THelper.Foo.call(this, 12);',
  18917. ' $mod.THelper.Foo.call(this, 1);',
  18918. ' $mod.THelper.Foo.call(this, 1);',
  18919. ' $mod.THelper.Foo.call(this, 13);',
  18920. ' $mod.THelper.Foo.call(this, 1);',
  18921. ' $mod.THelper.Foo.call(this, 1);',
  18922. ' $mod.THelper.Foo.call(this, 14);',
  18923. ' return Result;',
  18924. ' };',
  18925. '});',
  18926. 'this.Obj = null;',
  18927. '']),
  18928. LinesToStr([ // $mod.$main
  18929. '$mod.THelper.Foo.call($mod.Obj, 1);',
  18930. '$mod.THelper.Foo.call($mod.Obj, 1);',
  18931. '$mod.THelper.Foo.call($mod.Obj, 21);',
  18932. 'var $with1 = $mod.Obj;',
  18933. '$mod.THelper.Foo.call($with1, 1);',
  18934. '$mod.THelper.Foo.call($with1, 1);',
  18935. '$mod.THelper.Foo.call($with1, 22);',
  18936. '']));
  18937. end;
  18938. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  18939. begin
  18940. StartProgram(false);
  18941. Add([
  18942. 'type',
  18943. ' TObject = class',
  18944. ' procedure Run(w: word = 10);',
  18945. ' end;',
  18946. ' THelper = class helper for TObject',
  18947. ' function Foo(w: word = 1): word;',
  18948. ' end;',
  18949. 'procedure TObject.Run(w: word);',
  18950. ' procedure Sub(Self: TObject);',
  18951. ' begin',
  18952. ' Foo;',
  18953. ' Foo();',
  18954. ' Self.Foo;',
  18955. ' Self.Foo();',
  18956. ' with Self do begin',
  18957. ' Foo;',
  18958. ' Foo();',
  18959. ' end;',
  18960. ' end;',
  18961. 'begin',
  18962. 'end;',
  18963. 'function THelper.foo(w: word): word;',
  18964. ' procedure Sub(Self: TObject);',
  18965. ' begin',
  18966. ' Run;',
  18967. ' Run();',
  18968. ' Foo;',
  18969. ' Foo();',
  18970. ' Self.Foo;',
  18971. ' Self.Foo();',
  18972. ' with Self do begin',
  18973. ' Foo;',
  18974. ' Foo();',
  18975. ' end;',
  18976. ' end;',
  18977. 'begin',
  18978. 'end;',
  18979. 'begin',
  18980. '']);
  18981. ConvertProgram;
  18982. CheckSource('TestClassHelper_Method_Nested_Call',
  18983. LinesToStr([ // statements
  18984. 'rtl.createClass($mod, "TObject", null, function () {',
  18985. ' this.$init = function () {',
  18986. ' };',
  18987. ' this.$final = function () {',
  18988. ' };',
  18989. ' this.Run = function (w) {',
  18990. ' var $Self = this;',
  18991. ' function Sub(Self) {',
  18992. ' $mod.THelper.Foo.call($Self, 1);',
  18993. ' $mod.THelper.Foo.call($Self, 1);',
  18994. ' $mod.THelper.Foo.call(Self, 1);',
  18995. ' $mod.THelper.Foo.call(Self, 1);',
  18996. ' $mod.THelper.Foo.call(Self, 1);',
  18997. ' $mod.THelper.Foo.call($Self, 1);',
  18998. ' };',
  18999. ' };',
  19000. '});',
  19001. 'rtl.createHelper($mod, "THelper", null, function () {',
  19002. ' this.Foo = function (w) {',
  19003. ' var $Self = this;',
  19004. ' var Result = 0;',
  19005. ' function Sub(Self) {',
  19006. ' $Self.Run(10);',
  19007. ' $Self.Run(10);',
  19008. ' $mod.THelper.Foo.call($Self, 1);',
  19009. ' $mod.THelper.Foo.call($Self, 1);',
  19010. ' $mod.THelper.Foo.call(Self, 1);',
  19011. ' $mod.THelper.Foo.call(Self, 1);',
  19012. ' $mod.THelper.Foo.call(Self, 1);',
  19013. ' $mod.THelper.Foo.call($Self, 1);',
  19014. ' };',
  19015. ' return Result;',
  19016. ' };',
  19017. '});',
  19018. '']),
  19019. LinesToStr([ // $mod.$main
  19020. '']));
  19021. end;
  19022. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  19023. begin
  19024. StartProgram(false);
  19025. Add([
  19026. 'type',
  19027. ' TObject = class',
  19028. ' class procedure Run(w: word = 10);',
  19029. ' end;',
  19030. ' THelper = class helper for TObject',
  19031. ' class function Foo(w: word = 1): word;',
  19032. ' end;',
  19033. 'class procedure TObject.Run(w: word);',
  19034. 'begin',
  19035. ' Foo;',
  19036. ' Foo();',
  19037. ' Self.Foo;',
  19038. ' Self.Foo();',
  19039. ' with Self do begin',
  19040. ' Foo;',
  19041. ' Foo();',
  19042. ' end;',
  19043. 'end;',
  19044. 'class function THelper.foo(w: word): word;',
  19045. 'begin',
  19046. ' Run;',
  19047. ' Run();',
  19048. ' Foo;',
  19049. ' Foo();',
  19050. ' Self.Foo;',
  19051. ' Self.Foo();',
  19052. ' with Self do begin',
  19053. ' Foo;',
  19054. ' Foo();',
  19055. ' end;',
  19056. 'end;',
  19057. 'var',
  19058. ' Obj: TObject;',
  19059. 'begin',
  19060. ' obj.Foo;',
  19061. ' obj.Foo();',
  19062. ' with obj do begin',
  19063. ' Foo;',
  19064. ' Foo();',
  19065. ' end;',
  19066. ' tobject.Foo;',
  19067. ' tobject.Foo();',
  19068. ' with tobject do begin',
  19069. ' Foo;',
  19070. ' Foo();',
  19071. ' end;',
  19072. '']);
  19073. ConvertProgram;
  19074. CheckSource('TestClassHelper_ClassMethod_Call',
  19075. LinesToStr([ // statements
  19076. 'rtl.createClass($mod, "TObject", null, function () {',
  19077. ' this.$init = function () {',
  19078. ' };',
  19079. ' this.$final = function () {',
  19080. ' };',
  19081. ' this.Run = function (w) {',
  19082. ' $mod.THelper.Foo.call(this, 1);',
  19083. ' $mod.THelper.Foo.call(this, 1);',
  19084. ' $mod.THelper.Foo.call(this, 1);',
  19085. ' $mod.THelper.Foo.call(this, 1);',
  19086. ' $mod.THelper.Foo.call(this, 1);',
  19087. ' $mod.THelper.Foo.call(this, 1);',
  19088. ' };',
  19089. '});',
  19090. 'rtl.createHelper($mod, "THelper", null, function () {',
  19091. ' this.Foo = function (w) {',
  19092. ' var Result = 0;',
  19093. ' this.Run(10);',
  19094. ' this.Run(10);',
  19095. ' $mod.THelper.Foo.call(this, 1);',
  19096. ' $mod.THelper.Foo.call(this, 1);',
  19097. ' $mod.THelper.Foo.call(this, 1);',
  19098. ' $mod.THelper.Foo.call(this, 1);',
  19099. ' $mod.THelper.Foo.call(this, 1);',
  19100. ' $mod.THelper.Foo.call(this, 1);',
  19101. ' return Result;',
  19102. ' };',
  19103. '});',
  19104. 'this.Obj = null;',
  19105. '']),
  19106. LinesToStr([ // $mod.$main
  19107. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19108. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19109. 'var $with1 = $mod.Obj;',
  19110. '$mod.THelper.Foo.call($with1.$class, 1);',
  19111. '$mod.THelper.Foo.call($with1.$class, 1);',
  19112. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19113. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19114. 'var $with2 = $mod.TObject;',
  19115. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19116. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19117. '']));
  19118. end;
  19119. procedure TTestModule.TestClassHelper_ClassOf;
  19120. begin
  19121. StartProgram(false);
  19122. Add([
  19123. 'type',
  19124. ' TObject = class',
  19125. ' end;',
  19126. ' TClass = class of TObject;',
  19127. ' THelper = class helper for TObject',
  19128. ' class function Foo(w: word = 1): word;',
  19129. ' end;',
  19130. 'class function THelper.foo(w: word): word;',
  19131. 'begin',
  19132. 'end;',
  19133. 'var',
  19134. ' c: TClass;',
  19135. 'begin',
  19136. ' c.Foo;',
  19137. ' c.Foo();',
  19138. ' with c do begin',
  19139. ' Foo;',
  19140. ' Foo();',
  19141. ' end;',
  19142. '']);
  19143. ConvertProgram;
  19144. CheckSource('TestClassHelper_ClassOf',
  19145. LinesToStr([ // statements
  19146. 'rtl.createClass($mod, "TObject", null, function () {',
  19147. ' this.$init = function () {',
  19148. ' };',
  19149. ' this.$final = function () {',
  19150. ' };',
  19151. '});',
  19152. 'rtl.createHelper($mod, "THelper", null, function () {',
  19153. ' this.Foo = function (w) {',
  19154. ' var Result = 0;',
  19155. ' return Result;',
  19156. ' };',
  19157. '});',
  19158. 'this.c = null;',
  19159. '']),
  19160. LinesToStr([ // $mod.$main
  19161. '$mod.THelper.Foo.call($mod.c, 1);',
  19162. '$mod.THelper.Foo.call($mod.c, 1);',
  19163. 'var $with1 = $mod.c;',
  19164. '$mod.THelper.Foo.call($with1, 1);',
  19165. '$mod.THelper.Foo.call($with1, 1);',
  19166. '']));
  19167. end;
  19168. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  19169. begin
  19170. StartProgram(false);
  19171. Add([
  19172. '{$mode objfpc}',
  19173. 'type',
  19174. ' TObject = class',
  19175. ' procedure DoIt;',
  19176. ' end;',
  19177. ' THelper = class helper for TObject',
  19178. ' procedure Fly(w: word = 1);',
  19179. ' class procedure Glide(w: word = 1);',
  19180. ' class procedure Run(w: word = 1); static;',
  19181. ' end;',
  19182. ' TFly = procedure(w: word) of object;',
  19183. ' TGlide = TFly;',
  19184. ' TRun = procedure(w: word);',
  19185. 'var',
  19186. ' f: TFly;',
  19187. ' g: TGlide;',
  19188. ' r: TRun;',
  19189. 'procedure TObject.DoIt;',
  19190. 'begin',
  19191. ' f:=@fly;',
  19192. ' g:=@glide;',
  19193. ' r:=@run;',
  19194. ' f:[email protected];',
  19195. ' g:[email protected];',
  19196. ' r:[email protected];',
  19197. ' with self do begin',
  19198. ' f:=@fly;',
  19199. ' g:=@glide;',
  19200. ' r:=@run;',
  19201. ' end;',
  19202. 'end;',
  19203. 'procedure THelper.fly(w: word);',
  19204. 'begin',
  19205. ' f:=@fly;',
  19206. ' g:=@glide;',
  19207. ' r:=@run;',
  19208. 'end;',
  19209. 'class procedure THelper.glide(w: word);',
  19210. 'begin',
  19211. ' g:=@glide;',
  19212. ' r:=@run;',
  19213. 'end;',
  19214. 'class procedure THelper.run(w: word);',
  19215. 'begin',
  19216. ' g:=@glide;',
  19217. ' r:=@run;',
  19218. 'end;',
  19219. 'var',
  19220. ' Obj: TObject;',
  19221. 'begin',
  19222. ' f:[email protected];',
  19223. ' g:[email protected];',
  19224. ' r:[email protected];',
  19225. ' with obj do begin',
  19226. ' f:=@fly;',
  19227. ' g:=@glide;',
  19228. ' r:=@run;',
  19229. ' end;',
  19230. ' g:[email protected];',
  19231. ' r:[email protected];',
  19232. ' with tobject do begin',
  19233. ' g:=@glide;',
  19234. ' r:=@run;',
  19235. ' end;',
  19236. '']);
  19237. ConvertProgram;
  19238. CheckSource('TestClassHelper_MethodRefObjFPC',
  19239. LinesToStr([ // statements
  19240. 'rtl.createClass($mod, "TObject", null, function () {',
  19241. ' this.$init = function () {',
  19242. ' };',
  19243. ' this.$final = function () {',
  19244. ' };',
  19245. ' this.DoIt = function () {',
  19246. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19247. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19248. ' $mod.r = $mod.THelper.Run;',
  19249. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19250. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19251. ' $mod.r = $mod.THelper.Run;',
  19252. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19253. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19254. ' $mod.r = $mod.THelper.Run;',
  19255. ' };',
  19256. '});',
  19257. 'rtl.createHelper($mod, "THelper", null, function () {',
  19258. ' this.Fly = function (w) {',
  19259. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19260. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19261. ' $mod.r = $mod.THelper.Run;',
  19262. ' };',
  19263. ' this.Glide = function (w) {',
  19264. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  19265. ' $mod.r = $mod.THelper.Run;',
  19266. ' };',
  19267. ' this.Run = function (w) {',
  19268. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  19269. ' $mod.r = $mod.THelper.Run;',
  19270. ' };',
  19271. '});',
  19272. 'this.f = null;',
  19273. 'this.g = null;',
  19274. 'this.r = null;',
  19275. 'this.Obj = null;',
  19276. '']),
  19277. LinesToStr([ // $mod.$main
  19278. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  19279. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  19280. '$mod.r = $mod.THelper.Run;',
  19281. 'var $with1 = $mod.Obj;',
  19282. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  19283. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  19284. '$mod.r = $mod.THelper.Run;',
  19285. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  19286. '$mod.r = $mod.THelper.Run;',
  19287. 'var $with2 = $mod.TObject;',
  19288. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  19289. '$mod.r = $mod.THelper.Run;',
  19290. '']));
  19291. end;
  19292. procedure TTestModule.TestClassHelper_Constructor;
  19293. begin
  19294. StartProgram(false);
  19295. Add([
  19296. 'type',
  19297. ' TObject = class',
  19298. ' constructor Create;',
  19299. ' end;',
  19300. ' TClass = class of TObject;',
  19301. ' THelper = class helper for TObject',
  19302. ' constructor NewHlp(w: word);',
  19303. ' end;',
  19304. 'var',
  19305. ' obj: TObject;',
  19306. ' c: TClass;',
  19307. 'constructor TObject.Create;',
  19308. 'begin',
  19309. ' NewHlp(2);', // normal call
  19310. ' tobject.NewHlp(3);', // new instance
  19311. ' c.newhlp(4);', // new instance
  19312. 'end;',
  19313. 'constructor THelper.NewHlp(w: word);',
  19314. 'begin',
  19315. ' create;', // normal call
  19316. ' tobject.create;', // new instance
  19317. ' NewHlp(2);', // normal call
  19318. ' tobject.NewHlp(3);', // new instance
  19319. ' c.newhlp(4);', // new instance
  19320. 'end;',
  19321. 'begin',
  19322. ' obj.newhlp(2);', // normal call
  19323. ' with Obj do newhlp(12);', // normal call
  19324. ' tobject.newhlp(3);', // new instance
  19325. ' with tobject do newhlp(13);', // new instance
  19326. ' c.newhlp(4);', // new instance
  19327. ' with c do newhlp(14);', // new instance
  19328. '']);
  19329. ConvertProgram;
  19330. CheckSource('TestClassHelper_Constructor',
  19331. LinesToStr([ // statements
  19332. 'rtl.createClass($mod, "TObject", null, function () {',
  19333. ' this.$init = function () {',
  19334. ' };',
  19335. ' this.$final = function () {',
  19336. ' };',
  19337. ' this.Create = function () {',
  19338. ' $mod.THelper.NewHlp.call(this, 2);',
  19339. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19340. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19341. ' return this;',
  19342. ' };',
  19343. '});',
  19344. 'rtl.createHelper($mod, "THelper", null, function () {',
  19345. ' this.NewHlp = function (w) {',
  19346. ' this.Create();',
  19347. ' $mod.TObject.$create("Create");',
  19348. ' $mod.THelper.NewHlp.call(this, 2);',
  19349. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19350. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19351. ' return this;',
  19352. ' };',
  19353. '});',
  19354. 'this.obj = null;',
  19355. 'this.c = null;',
  19356. '']),
  19357. LinesToStr([ // $mod.$main
  19358. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  19359. 'var $with1 = $mod.obj;',
  19360. '$mod.THelper.NewHlp.call($with1, 12);',
  19361. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19362. 'var $with2 = $mod.TObject;',
  19363. '$with2.$create($mod.THelper.NewHlp, [13]);',
  19364. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  19365. 'var $with3 = $mod.c;',
  19366. '$with3.$create($mod.THelper.NewHlp, [14]);',
  19367. '']));
  19368. end;
  19369. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  19370. begin
  19371. StartProgram(false);
  19372. Add([
  19373. 'type',
  19374. ' TObject = class',
  19375. ' procedure Fly;',
  19376. ' end;',
  19377. ' TObjHelper = class helper for TObject',
  19378. ' procedure Fly;',
  19379. ' end;',
  19380. ' TBird = class',
  19381. ' procedure Fly;',
  19382. ' end;',
  19383. ' TBirdHelper = class helper for TBird',
  19384. ' procedure Fly;',
  19385. ' procedure Walk(w: word);',
  19386. ' end;',
  19387. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  19388. ' procedure Fly;',
  19389. ' procedure Walk(w: word);',
  19390. ' end;',
  19391. 'procedure Tobject.fly;',
  19392. 'begin',
  19393. ' inherited;', // ignore
  19394. 'end;',
  19395. 'procedure Tobjhelper.fly;',
  19396. 'begin',
  19397. ' {@TObject_Fly}inherited;',
  19398. ' inherited {@TObject_Fly}Fly;',
  19399. 'end;',
  19400. 'procedure Tbird.fly;',
  19401. 'begin',
  19402. ' {@TObjHelper_Fly}inherited;',
  19403. ' inherited {@TObjHelper_Fly}Fly;',
  19404. 'end;',
  19405. 'procedure Tbirdhelper.fly;',
  19406. 'begin',
  19407. ' {@TBird_Fly}inherited;',
  19408. ' inherited {@TBird_Fly}Fly;',
  19409. 'end;',
  19410. 'procedure Tbirdhelper.walk(w: word);',
  19411. 'begin',
  19412. 'end;',
  19413. 'procedure teagleHelper.fly;',
  19414. 'begin',
  19415. ' {@TBird_Fly}inherited;',
  19416. ' inherited {@TBird_Fly}Fly;',
  19417. 'end;',
  19418. 'procedure teagleHelper.walk(w: word);',
  19419. 'begin',
  19420. ' {@TBirdHelper_Walk}inherited;',
  19421. ' inherited {@TBirdHelper_Walk}Walk(3);',
  19422. 'end;',
  19423. 'begin',
  19424. '']);
  19425. ConvertProgram;
  19426. CheckSource('TestClassHelper_InheritedObjFPC',
  19427. LinesToStr([ // statements
  19428. 'rtl.createClass($mod, "TObject", null, function () {',
  19429. ' this.$init = function () {',
  19430. ' };',
  19431. ' this.$final = function () {',
  19432. ' };',
  19433. ' this.Fly = function () {',
  19434. ' };',
  19435. '});',
  19436. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19437. ' this.Fly = function () {',
  19438. ' $mod.TObject.Fly.call(this);',
  19439. ' $mod.TObject.Fly.call(this);',
  19440. ' };',
  19441. '});',
  19442. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19443. ' this.Fly$1 = function () {',
  19444. ' $mod.TObjHelper.Fly.call(this);',
  19445. ' $mod.TObjHelper.Fly.call(this);',
  19446. ' };',
  19447. '});',
  19448. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19449. ' this.Fly = function () {',
  19450. ' $mod.TBird.Fly$1.call(this);',
  19451. ' $mod.TBird.Fly$1.call(this);',
  19452. ' };',
  19453. ' this.Walk = function (w) {',
  19454. ' };',
  19455. '});',
  19456. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  19457. ' this.Fly$1 = function () {',
  19458. ' $mod.TBird.Fly$1.call(this);',
  19459. ' $mod.TBird.Fly$1.call(this);',
  19460. ' };',
  19461. ' this.Walk$1 = function (w) {',
  19462. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  19463. ' $mod.TBirdHelper.Walk.call(this, 3);',
  19464. ' };',
  19465. '});',
  19466. '']),
  19467. LinesToStr([ // $mod.$main
  19468. '']));
  19469. end;
  19470. procedure TTestModule.TestClassHelper_Property;
  19471. begin
  19472. StartProgram(false);
  19473. Add([
  19474. 'type',
  19475. ' TObject = class',
  19476. ' FSize: word;',
  19477. ' function GetSpeed: word;',
  19478. ' procedure SetSpeed(Value: word);',
  19479. ' end;',
  19480. ' TObjHelper = class helper for TObject',
  19481. ' function GetLeft: word;',
  19482. ' procedure SetLeft(Value: word);',
  19483. ' property Size: word read FSize write FSize;',
  19484. ' property Speed: word read GetSpeed write SetSpeed;',
  19485. ' property Left: word read GetLeft write SetLeft;',
  19486. ' end;',
  19487. ' TBird = class',
  19488. ' property NotRight: word read GetLeft write SetLeft;',
  19489. ' procedure DoIt;',
  19490. ' end;',
  19491. 'var',
  19492. ' b: TBird;',
  19493. 'function Tobject.GetSpeed: word;',
  19494. 'begin',
  19495. ' Size:=Size+11;',
  19496. ' Speed:=Speed+12;',
  19497. ' Result:=Left+13;',
  19498. ' Left:=13;',
  19499. ' Left:=Left+13;',
  19500. ' Self.Size:=Self.Size+21;',
  19501. ' Self.Speed:=Self.Speed+22;',
  19502. ' Self.Left:=Self.Left+23;',
  19503. ' with Self do begin',
  19504. ' Size:=Size+31;',
  19505. ' Speed:=Speed+32;',
  19506. ' Left:=Left+33;',
  19507. ' end;',
  19508. 'end;',
  19509. 'procedure Tobject.SetSpeed(Value: word);',
  19510. 'begin',
  19511. 'end;',
  19512. 'function TObjHelper.GetLeft: word;',
  19513. 'begin',
  19514. ' Size:=Size+11;',
  19515. ' Speed:=Speed+12;',
  19516. ' Left:=Left+13;',
  19517. ' Self.Size:=Self.Size+21;',
  19518. ' Self.Speed:=Self.Speed+22;',
  19519. ' Self.Left:=Self.Left+23;',
  19520. ' with Self do begin',
  19521. ' Size:=Size+31;',
  19522. ' Speed:=Speed+32;',
  19523. ' Left:=Left+33;',
  19524. ' end;',
  19525. 'end;',
  19526. 'procedure TObjHelper.SetLeft(Value: word);',
  19527. 'begin',
  19528. 'end;',
  19529. 'procedure TBird.DoIt;',
  19530. 'begin',
  19531. ' NotRight:=NotRight+11;',
  19532. ' Self.NotRight:=Self.NotRight+21;',
  19533. ' with Self do begin',
  19534. ' NotRight:=NotRight+31;',
  19535. ' end;',
  19536. 'end;',
  19537. 'begin',
  19538. ' b.Size:=b.Size+11;',
  19539. ' b.Speed:=b.Speed+12;',
  19540. ' b.Left:=b.Left+13;',
  19541. ' b.NotRight:=b.NotRight+14;',
  19542. ' with b do begin',
  19543. ' Size:=Size+31;',
  19544. ' Speed:=Speed+32;',
  19545. ' Left:=Left+33;',
  19546. ' NotRight:=NotRight+34;',
  19547. ' end;',
  19548. '']);
  19549. ConvertProgram;
  19550. CheckSource('TestClassHelper_Property',
  19551. LinesToStr([ // statements
  19552. 'rtl.createClass($mod, "TObject", null, function () {',
  19553. ' this.$init = function () {',
  19554. ' this.FSize = 0;',
  19555. ' };',
  19556. ' this.$final = function () {',
  19557. ' };',
  19558. ' this.GetSpeed = function () {',
  19559. ' var Result = 0;',
  19560. ' this.FSize = this.FSize + 11;',
  19561. ' this.SetSpeed(this.GetSpeed() + 12);',
  19562. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  19563. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  19564. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19565. ' this.FSize = this.FSize + 21;',
  19566. ' this.SetSpeed(this.GetSpeed() + 22);',
  19567. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19568. ' this.FSize = this.FSize + 31;',
  19569. ' this.SetSpeed(this.GetSpeed() + 32);',
  19570. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19571. ' return Result;',
  19572. ' };',
  19573. ' this.SetSpeed = function (Value) {',
  19574. ' };',
  19575. '});',
  19576. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19577. ' this.GetLeft = function () {',
  19578. ' var Result = 0;',
  19579. ' this.FSize = this.FSize + 11;',
  19580. ' this.SetSpeed(this.GetSpeed() + 12);',
  19581. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19582. ' this.FSize = this.FSize + 21;',
  19583. ' this.SetSpeed(this.GetSpeed() + 22);',
  19584. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19585. ' this.FSize = this.FSize + 31;',
  19586. ' this.SetSpeed(this.GetSpeed() + 32);',
  19587. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19588. ' return Result;',
  19589. ' };',
  19590. ' this.SetLeft = function (Value) {',
  19591. ' };',
  19592. '});',
  19593. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19594. ' this.DoIt = function () {',
  19595. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19596. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19597. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19598. ' };',
  19599. '});',
  19600. 'this.b = null;',
  19601. '']),
  19602. LinesToStr([ // $mod.$main
  19603. '$mod.b.FSize = $mod.b.FSize + 11;',
  19604. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  19605. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  19606. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  19607. 'var $with1 = $mod.b;',
  19608. '$with1.FSize = $with1.FSize + 31;',
  19609. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  19610. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  19611. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  19612. '']));
  19613. end;
  19614. procedure TTestModule.TestClassHelper_Property_Array;
  19615. begin
  19616. StartProgram(false);
  19617. Add([
  19618. 'type',
  19619. ' TObject = class',
  19620. ' function GetSpeed(Index: boolean): word;',
  19621. ' procedure SetSpeed(Index: boolean; Value: word);',
  19622. ' end;',
  19623. ' TObjHelper = class helper for TObject',
  19624. ' function GetSize(Index: boolean): word;',
  19625. ' procedure SetSize(Index: boolean; Value: word);',
  19626. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  19627. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  19628. ' end;',
  19629. ' TBird = class',
  19630. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  19631. ' procedure DoIt;',
  19632. ' end;',
  19633. 'var',
  19634. ' b: TBird;',
  19635. 'function Tobject.GetSpeed(Index: boolean): word;',
  19636. 'begin',
  19637. ' Result:=Size[false];',
  19638. ' Size[true]:=Size[false]+11;',
  19639. ' Speed[true]:=Speed[false]+12;',
  19640. ' Self.Size[true]:=Self.Size[false]+21;',
  19641. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19642. ' with Self do begin',
  19643. ' Size[true]:=Size[false]+31;',
  19644. ' Speed[true]:=Speed[false]+32;',
  19645. ' end;',
  19646. 'end;',
  19647. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19648. 'begin',
  19649. 'end;',
  19650. 'function TObjHelper.GetSize(Index: boolean): word;',
  19651. 'begin',
  19652. ' Size[true]:=Size[false]+11;',
  19653. ' Speed[true]:=Speed[false]+12;',
  19654. ' Self.Size[true]:=Self.Size[false]+21;',
  19655. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19656. ' with Self do begin',
  19657. ' Size[true]:=Size[false]+31;',
  19658. ' Speed[true]:=Speed[false]+32;',
  19659. ' end;',
  19660. 'end;',
  19661. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  19662. 'begin',
  19663. 'end;',
  19664. 'procedure TBird.DoIt;',
  19665. 'begin',
  19666. ' Items[true]:=Items[false]+11;',
  19667. ' Self.Items[true]:=Self.Items[false]+21;',
  19668. ' with Self do Items[true]:=Items[false]+31;',
  19669. 'end;',
  19670. 'begin',
  19671. ' b.Size[true]:=b.Size[false]+11;',
  19672. ' b.Speed[true]:=b.Speed[false]+12;',
  19673. ' b.Items[true]:=b.Items[false]+13;',
  19674. ' with b do begin',
  19675. ' Size[true]:=Size[false]+21;',
  19676. ' Speed[true]:=Speed[false]+22;',
  19677. ' Items[true]:=Items[false]+23;',
  19678. ' end;',
  19679. '']);
  19680. ConvertProgram;
  19681. CheckSource('TestClassHelper_Property_Array',
  19682. LinesToStr([ // statements
  19683. 'rtl.createClass($mod, "TObject", null, function () {',
  19684. ' this.$init = function () {',
  19685. ' };',
  19686. ' this.$final = function () {',
  19687. ' };',
  19688. ' this.GetSpeed = function (Index) {',
  19689. ' var Result = 0;',
  19690. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  19691. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19692. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19693. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19694. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19695. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19696. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19697. ' return Result;',
  19698. ' };',
  19699. ' this.SetSpeed = function (Index, Value) {',
  19700. ' };',
  19701. '});',
  19702. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19703. ' this.GetSize = function (Index) {',
  19704. ' var Result = 0;',
  19705. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19706. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19707. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19708. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19709. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19710. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19711. ' return Result;',
  19712. ' };',
  19713. ' this.SetSize = function (Index, Value) {',
  19714. ' };',
  19715. '});',
  19716. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19717. ' this.DoIt = function () {',
  19718. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19719. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19720. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19721. ' };',
  19722. '});',
  19723. 'this.b = null;',
  19724. '']),
  19725. LinesToStr([ // $mod.$main
  19726. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  19727. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  19728. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  19729. 'var $with1 = $mod.b;',
  19730. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  19731. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  19732. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  19733. '']));
  19734. end;
  19735. procedure TTestModule.TestClassHelper_Property_Array_Default;
  19736. begin
  19737. StartProgram(false);
  19738. Add([
  19739. 'type',
  19740. ' TObject = class',
  19741. ' function GetSpeed(Index: boolean): word;',
  19742. ' procedure SetSpeed(Index: boolean; Value: word);',
  19743. ' end;',
  19744. ' TObjHelper = class helper for TObject',
  19745. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  19746. ' end;',
  19747. ' TBird = class',
  19748. ' end;',
  19749. ' TBirdHelper = class helper for TBird',
  19750. ' function GetSize(Index: word): boolean;',
  19751. ' procedure SetSize(Index: word; Value: boolean);',
  19752. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  19753. ' end;',
  19754. 'function Tobject.GetSpeed(Index: boolean): word;',
  19755. 'begin',
  19756. ' Self[true]:=Self[false]+1;',
  19757. 'end;',
  19758. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19759. 'begin',
  19760. 'end;',
  19761. 'function TBirdHelper.GetSize(Index: word): boolean;',
  19762. 'begin',
  19763. ' Self[1]:=not Self[2];',
  19764. 'end;',
  19765. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  19766. 'begin',
  19767. 'end;',
  19768. 'var',
  19769. ' o: TObject;',
  19770. ' b: TBird;',
  19771. 'begin',
  19772. ' o[true]:=o[false]+1;',
  19773. ' b[3]:=not b[4];',
  19774. '']);
  19775. ConvertProgram;
  19776. CheckSource('TestClassHelper_Property_Array_Default',
  19777. LinesToStr([ // statements
  19778. 'rtl.createClass($mod, "TObject", null, function () {',
  19779. ' this.$init = function () {',
  19780. ' };',
  19781. ' this.$final = function () {',
  19782. ' };',
  19783. ' this.GetSpeed = function (Index) {',
  19784. ' var Result = 0;',
  19785. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  19786. ' return Result;',
  19787. ' };',
  19788. ' this.SetSpeed = function (Index, Value) {',
  19789. ' };',
  19790. '});',
  19791. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19792. '});',
  19793. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19794. '});',
  19795. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19796. ' this.GetSize = function (Index) {',
  19797. ' var Result = false;',
  19798. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  19799. ' return Result;',
  19800. ' };',
  19801. ' this.SetSize = function (Index, Value) {',
  19802. ' };',
  19803. '});',
  19804. 'this.o = null;',
  19805. 'this.b = null;',
  19806. '']),
  19807. LinesToStr([ // $mod.$main
  19808. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  19809. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  19810. '']));
  19811. end;
  19812. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  19813. begin
  19814. StartProgram(false);
  19815. Add([
  19816. 'type',
  19817. ' TObject = class',
  19818. ' end;',
  19819. ' TObjHelper = class helper for TObject',
  19820. ' function GetItems(Index: word): TObject;',
  19821. ' procedure SetItems(Index: word; Value: TObject);',
  19822. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  19823. ' end;',
  19824. 'function Tobjhelper.GetItems(Index: word): TObject;',
  19825. 'begin',
  19826. ' Self[1][2]:=Self[3][4];',
  19827. 'end;',
  19828. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  19829. 'begin',
  19830. 'end;',
  19831. 'var',
  19832. ' o: TObject;',
  19833. 'begin',
  19834. ' o[1][2]:=o[3][4];',
  19835. '']);
  19836. ConvertProgram;
  19837. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  19838. LinesToStr([ // statements
  19839. 'rtl.createClass($mod, "TObject", null, function () {',
  19840. ' this.$init = function () {',
  19841. ' };',
  19842. ' this.$final = function () {',
  19843. ' };',
  19844. '});',
  19845. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19846. ' this.GetItems = function (Index) {',
  19847. ' var Result = null;',
  19848. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  19849. ' return Result;',
  19850. ' };',
  19851. ' this.SetItems = function (Index, Value) {',
  19852. ' };',
  19853. '});',
  19854. 'this.o = null;',
  19855. '']),
  19856. LinesToStr([ // $mod.$main
  19857. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  19858. '']));
  19859. end;
  19860. procedure TTestModule.TestClassHelper_ClassProperty;
  19861. begin
  19862. StartProgram(false);
  19863. Add([
  19864. 'type',
  19865. ' TObject = class',
  19866. ' class var FSize: word;',
  19867. ' class function GetSpeed: word;',
  19868. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  19869. ' end;',
  19870. ' TObjHelper = class helper for TObject',
  19871. ' class function GetLeft: word;',
  19872. ' class procedure SetLeft(Value: word);',
  19873. ' class property Size: word read FSize write FSize;',
  19874. ' class property Speed: word read GetSpeed write SetSpeed;',
  19875. ' class property Left: word read GetLeft write SetLeft;',
  19876. ' end;',
  19877. ' TBird = class',
  19878. ' class property NotRight: word read GetLeft write SetLeft;',
  19879. ' class procedure DoIt;',
  19880. ' end;',
  19881. ' TBirdClass = class of TBird;',
  19882. 'class function Tobject.GetSpeed: word;',
  19883. 'begin',
  19884. ' Size:=Size+11;',
  19885. ' Speed:=Speed+12;',
  19886. ' Left:=Left+13;',
  19887. ' Self.Size:=Self.Size+21;',
  19888. ' Self.Speed:=Self.Speed+22;',
  19889. ' Self.Left:=Self.Left+23;',
  19890. ' with Self do begin',
  19891. ' Size:=Size+31;',
  19892. ' Speed:=Speed+32;',
  19893. ' Left:=Left+33;',
  19894. ' end;',
  19895. 'end;',
  19896. 'class function TObjHelper.GetLeft: word;',
  19897. 'begin',
  19898. ' Size:=Size+11;',
  19899. ' Speed:=Speed+12;',
  19900. ' Left:=Left+13;',
  19901. ' Self.Size:=Self.Size+21;',
  19902. ' Self.Speed:=Self.Speed+22;',
  19903. ' Self.Left:=Self.Left+23;',
  19904. ' with Self do begin',
  19905. ' Size:=Size+31;',
  19906. ' Speed:=Speed+32;',
  19907. ' Left:=Left+33;',
  19908. ' end;',
  19909. 'end;',
  19910. 'class procedure TObjHelper.SetLeft(Value: word);',
  19911. 'begin',
  19912. 'end;',
  19913. 'class procedure TBird.DoIt;',
  19914. 'begin',
  19915. ' NotRight:=NotRight+11;',
  19916. ' Self.NotRight:=Self.NotRight+21;',
  19917. ' with Self do NotRight:=NotRight+31;',
  19918. 'end;',
  19919. 'var',
  19920. ' b: TBird;',
  19921. ' c: TBirdClass;',
  19922. 'begin',
  19923. ' b.Size:=b.Size+11;',
  19924. ' b.Speed:=b.Speed+12;',
  19925. ' b.Left:=b.Left+13;',
  19926. ' b.NotRight:=b.NotRight+14;',
  19927. ' with b do begin',
  19928. ' Size:=Size+31;',
  19929. ' Speed:=Speed+32;',
  19930. ' Left:=Left+33;',
  19931. ' NotRight:=NotRight+34;',
  19932. ' end;',
  19933. ' c.Size:=c.Size+11;',
  19934. ' c.Speed:=c.Speed+12;',
  19935. ' c.Left:=c.Left+13;',
  19936. ' c.NotRight:=c.NotRight+14;',
  19937. ' with c do begin',
  19938. ' Size:=Size+31;',
  19939. ' Speed:=Speed+32;',
  19940. ' Left:=Left+33;',
  19941. ' NotRight:=NotRight+34;',
  19942. ' end;',
  19943. ' tbird.Size:=tbird.Size+11;',
  19944. ' tbird.Speed:=tbird.Speed+12;',
  19945. ' tbird.Left:=tbird.Left+13;',
  19946. ' tbird.NotRight:=tbird.NotRight+14;',
  19947. ' with tbird do begin',
  19948. ' Size:=Size+31;',
  19949. ' Speed:=Speed+32;',
  19950. ' Left:=Left+33;',
  19951. ' NotRight:=NotRight+34;',
  19952. ' end;',
  19953. '']);
  19954. ConvertProgram;
  19955. CheckSource('TestClassHelper_ClassProperty',
  19956. LinesToStr([ // statements
  19957. 'rtl.createClass($mod, "TObject", null, function () {',
  19958. ' this.FSize = 0;',
  19959. ' this.$init = function () {',
  19960. ' };',
  19961. ' this.$final = function () {',
  19962. ' };',
  19963. ' this.GetSpeed = function () {',
  19964. ' var Result = 0;',
  19965. ' $mod.TObject.FSize = this.FSize + 11;',
  19966. ' this.SetSpeed(this.GetSpeed() + 12);',
  19967. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19968. ' $mod.TObject.FSize = this.FSize + 21;',
  19969. ' this.SetSpeed(this.GetSpeed() + 22);',
  19970. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19971. ' $mod.TObject.FSize = this.FSize + 31;',
  19972. ' this.SetSpeed(this.GetSpeed() + 32);',
  19973. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19974. ' return Result;',
  19975. ' };',
  19976. '});',
  19977. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19978. ' this.GetLeft = function () {',
  19979. ' var Result = 0;',
  19980. ' $mod.TObject.FSize = this.FSize + 11;',
  19981. ' this.SetSpeed(this.GetSpeed() + 12);',
  19982. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19983. ' $mod.TObject.FSize = this.FSize + 21;',
  19984. ' this.SetSpeed(this.GetSpeed() + 22);',
  19985. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19986. ' $mod.TObject.FSize = this.FSize + 31;',
  19987. ' this.SetSpeed(this.GetSpeed() + 32);',
  19988. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19989. ' return Result;',
  19990. ' };',
  19991. ' this.SetLeft = function (Value) {',
  19992. ' };',
  19993. '});',
  19994. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19995. ' this.DoIt = function () {',
  19996. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19997. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19998. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19999. ' };',
  20000. '});',
  20001. 'this.b = null;',
  20002. 'this.c = null;',
  20003. '']),
  20004. LinesToStr([ // $mod.$main
  20005. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  20006. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  20007. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  20008. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  20009. 'var $with1 = $mod.b;',
  20010. '$mod.TObject.FSize = $with1.FSize + 31;',
  20011. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  20012. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  20013. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  20014. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  20015. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20016. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  20017. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  20018. 'var $with2 = $mod.c;',
  20019. '$mod.TObject.FSize = $with2.FSize + 31;',
  20020. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20021. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  20022. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  20023. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  20024. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20025. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  20026. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  20027. 'var $with3 = $mod.TBird;',
  20028. '$mod.TObject.FSize = $with3.FSize + 31;',
  20029. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20030. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  20031. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  20032. '']));
  20033. end;
  20034. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  20035. begin
  20036. StartProgram(false);
  20037. Add([
  20038. 'type',
  20039. ' TObject = class',
  20040. ' class function GetSpeed: word; static;',
  20041. ' class procedure SetSpeed(Value: word); static;',
  20042. ' end;',
  20043. ' TObjHelper = class helper for TObject',
  20044. ' class function GetLeft: word; static;',
  20045. ' class procedure SetLeft(Value: word); static;',
  20046. ' class property Speed: word read GetSpeed write SetSpeed;',
  20047. ' class property Left: word read GetLeft write SetLeft;',
  20048. ' end;',
  20049. ' TBird = class',
  20050. ' class property NotRight: word read GetLeft write SetLeft;',
  20051. ' class procedure DoIt; static;',
  20052. ' class procedure DoSome;',
  20053. ' end;',
  20054. ' TBirdClass = class of TBird;',
  20055. 'class function Tobject.GetSpeed: word;',
  20056. 'begin',
  20057. ' Speed:=Speed+12;',
  20058. ' Left:=Left+13;',
  20059. 'end;',
  20060. 'class procedure TObject.SetSpeed(Value: word);',
  20061. 'begin',
  20062. 'end;',
  20063. 'class function TObjHelper.GetLeft: word;',
  20064. 'begin',
  20065. ' Speed:=Speed+12;',
  20066. ' Left:=Left+13;',
  20067. 'end;',
  20068. 'class procedure TObjHelper.SetLeft(Value: word);',
  20069. 'begin',
  20070. 'end;',
  20071. 'class procedure TBird.DoIt;',
  20072. 'begin',
  20073. ' NotRight:=NotRight+11;',
  20074. 'end;',
  20075. 'class procedure TBird.DoSome;',
  20076. 'begin',
  20077. ' Speed:=Speed+12;',
  20078. ' Left:=Left+13;',
  20079. ' Self.Speed:=Self.Speed+22;',
  20080. ' Self.Left:=Self.Left+23;',
  20081. ' with Self do begin',
  20082. ' Speed:=Speed+32;',
  20083. ' Left:=Left+33;',
  20084. ' end;',
  20085. ' NotRight:=NotRight+11;',
  20086. ' Self.NotRight:=Self.NotRight+21;',
  20087. ' with Self do NotRight:=NotRight+31;',
  20088. 'end;',
  20089. 'var',
  20090. ' b: TBird;',
  20091. ' c: TBirdClass;',
  20092. 'begin',
  20093. ' b.Speed:=b.Speed+12;',
  20094. ' b.Left:=b.Left+13;',
  20095. ' b.NotRight:=b.NotRight+14;',
  20096. ' with b do begin',
  20097. ' Speed:=Speed+32;',
  20098. ' Left:=Left+33;',
  20099. ' NotRight:=NotRight+34;',
  20100. ' end;',
  20101. ' c.Speed:=c.Speed+12;',
  20102. ' c.Left:=c.Left+13;',
  20103. ' c.NotRight:=c.NotRight+14;',
  20104. ' with c do begin',
  20105. ' Speed:=Speed+32;',
  20106. ' Left:=Left+33;',
  20107. ' NotRight:=NotRight+34;',
  20108. ' end;',
  20109. ' tbird.Speed:=tbird.Speed+12;',
  20110. ' tbird.Left:=tbird.Left+13;',
  20111. ' tbird.NotRight:=tbird.NotRight+14;',
  20112. ' with tbird do begin',
  20113. ' Speed:=Speed+32;',
  20114. ' Left:=Left+33;',
  20115. ' NotRight:=NotRight+34;',
  20116. ' end;',
  20117. '']);
  20118. ConvertProgram;
  20119. CheckSource('TestClassHelper_ClassPropertyStatic',
  20120. LinesToStr([ // statements
  20121. 'rtl.createClass($mod, "TObject", null, function () {',
  20122. ' this.$init = function () {',
  20123. ' };',
  20124. ' this.$final = function () {',
  20125. ' };',
  20126. ' this.GetSpeed = function () {',
  20127. ' var Result = 0;',
  20128. ' this.SetSpeed(this.GetSpeed() + 12);',
  20129. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20130. ' return Result;',
  20131. ' };',
  20132. ' this.SetSpeed = function (Value) {',
  20133. ' };',
  20134. '});',
  20135. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20136. ' this.GetLeft = function () {',
  20137. ' var Result = 0;',
  20138. ' this.SetSpeed(this.GetSpeed() + 12);',
  20139. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20140. ' return Result;',
  20141. ' };',
  20142. ' this.SetLeft = function (Value) {',
  20143. ' };',
  20144. '});',
  20145. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20146. ' this.DoIt = function () {',
  20147. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20148. ' };',
  20149. ' this.DoSome = function () {',
  20150. ' this.SetSpeed(this.GetSpeed() + 12);',
  20151. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20152. ' this.SetSpeed(this.GetSpeed() + 22);',
  20153. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  20154. ' this.SetSpeed(this.GetSpeed() + 32);',
  20155. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20156. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20157. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  20158. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  20159. ' };',
  20160. '});',
  20161. 'this.b = null;',
  20162. 'this.c = null;',
  20163. '']),
  20164. LinesToStr([ // $mod.$main
  20165. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20166. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20167. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20168. 'var $with1 = $mod.b;',
  20169. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20170. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20171. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20172. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20173. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20174. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20175. 'var $with2 = $mod.c;',
  20176. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20177. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20178. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20179. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20180. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20181. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20182. 'var $with3 = $mod.TBird;',
  20183. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20184. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20185. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20186. '']));
  20187. end;
  20188. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  20189. begin
  20190. StartProgram(false);
  20191. Add([
  20192. 'type',
  20193. ' TObject = class',
  20194. ' class function GetSpeed(Index: boolean): word;',
  20195. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  20196. ' end;',
  20197. ' TObjHelper = class helper for TObject',
  20198. ' class function GetSize(Index: boolean): word;',
  20199. ' class procedure SetSize(Index: boolean; Value: word);',
  20200. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  20201. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20202. ' end;',
  20203. ' TBird = class',
  20204. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  20205. ' class procedure DoIt;',
  20206. ' end;',
  20207. ' TBirdClass = class of TBird;',
  20208. 'class function Tobject.GetSpeed(Index: boolean): word;',
  20209. 'begin',
  20210. ' Size[true]:=Size[false]+11;',
  20211. ' Speed[true]:=Speed[false]+12;',
  20212. ' Self.Size[true]:=Self.Size[false]+21;',
  20213. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20214. ' with Self do begin',
  20215. ' Size[true]:=Size[false]+31;',
  20216. ' Speed[true]:=Speed[false]+32;',
  20217. ' end;',
  20218. 'end;',
  20219. 'class function TObjHelper.GetSize(Index: boolean): word;',
  20220. 'begin',
  20221. ' Size[true]:=Size[false]+11;',
  20222. ' Speed[true]:=Speed[false]+12;',
  20223. ' Self.Size[true]:=Self.Size[false]+21;',
  20224. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20225. ' with Self do begin',
  20226. ' Size[true]:=Size[false]+31;',
  20227. ' Speed[true]:=Speed[false]+32;',
  20228. ' end;',
  20229. 'end;',
  20230. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20231. 'begin',
  20232. 'end;',
  20233. 'class procedure TBird.DoIt;',
  20234. 'begin',
  20235. ' Items[true]:=Items[false]+11;',
  20236. ' Self.Items[true]:=Self.Items[false]+21;',
  20237. ' with Self do Items[true]:=Items[false]+31;',
  20238. 'end;',
  20239. 'var',
  20240. ' b: TBird;',
  20241. ' c: TBirdClass;',
  20242. 'begin',
  20243. ' b.Size[true]:=b.Size[false]+11;',
  20244. ' b.Speed[true]:=b.Speed[false]+12;',
  20245. ' b.Items[true]:=b.Items[false]+13;',
  20246. ' with b do begin',
  20247. ' Size[true]:=Size[false]+21;',
  20248. ' Speed[true]:=Speed[false]+22;',
  20249. ' Items[true]:=Items[false]+23;',
  20250. ' end;',
  20251. ' c.Size[true]:=c.Size[false]+11;',
  20252. ' c.Speed[true]:=c.Speed[false]+12;',
  20253. ' c.Items[true]:=c.Items[false]+13;',
  20254. ' with c do begin',
  20255. ' Size[true]:=Size[false]+21;',
  20256. ' Speed[true]:=Speed[false]+22;',
  20257. ' Items[true]:=Items[false]+23;',
  20258. ' end;',
  20259. ' TBird.Size[true]:=TBird.Size[false]+11;',
  20260. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  20261. ' TBird.Items[true]:=TBird.Items[false]+13;',
  20262. ' with TBird do begin',
  20263. ' Size[true]:=Size[false]+21;',
  20264. ' Speed[true]:=Speed[false]+22;',
  20265. ' Items[true]:=Items[false]+23;',
  20266. ' end;',
  20267. '']);
  20268. ConvertProgram;
  20269. CheckSource('TestClassHelper_ClassProperty_Array',
  20270. LinesToStr([ // statements
  20271. 'rtl.createClass($mod, "TObject", null, function () {',
  20272. ' this.$init = function () {',
  20273. ' };',
  20274. ' this.$final = function () {',
  20275. ' };',
  20276. ' this.GetSpeed = function (Index) {',
  20277. ' var Result = 0;',
  20278. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20279. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20280. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20281. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20282. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20283. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20284. ' return Result;',
  20285. ' };',
  20286. '});',
  20287. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20288. ' this.GetSize = function (Index) {',
  20289. ' var Result = 0;',
  20290. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20291. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20292. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20293. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20294. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20295. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20296. ' return Result;',
  20297. ' };',
  20298. ' this.SetSize = function (Index, Value) {',
  20299. ' };',
  20300. '});',
  20301. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20302. ' this.DoIt = function () {',
  20303. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20304. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20305. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20306. ' };',
  20307. '});',
  20308. 'this.b = null;',
  20309. 'this.c = null;',
  20310. '']),
  20311. LinesToStr([ // $mod.$main
  20312. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  20313. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  20314. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  20315. 'var $with1 = $mod.b;',
  20316. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  20317. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  20318. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  20319. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  20320. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  20321. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  20322. 'var $with2 = $mod.c;',
  20323. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  20324. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  20325. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  20326. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  20327. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  20328. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  20329. 'var $with3 = $mod.TBird;',
  20330. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  20331. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  20332. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  20333. '']));
  20334. end;
  20335. procedure TTestModule.TestClassHelper_ForIn;
  20336. begin
  20337. StartProgram(false);
  20338. Add([
  20339. 'type',
  20340. ' TObject = class end;',
  20341. ' TItem = TObject;',
  20342. ' TEnumerator = class',
  20343. ' FCurrent: TItem;',
  20344. ' property Current: TItem read FCurrent;',
  20345. ' function MoveNext: boolean;',
  20346. ' end;',
  20347. ' TBird = class',
  20348. ' end;',
  20349. ' TBirdHelper = class helper for TBird',
  20350. ' function GetEnumerator: TEnumerator;',
  20351. ' end;',
  20352. 'function TEnumerator.MoveNext: boolean;',
  20353. 'begin',
  20354. 'end;',
  20355. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  20356. 'begin',
  20357. 'end;',
  20358. 'var',
  20359. ' b: TBird;',
  20360. ' i, i2: TItem;',
  20361. 'begin',
  20362. ' for i in b do i2:=i;']);
  20363. ConvertProgram;
  20364. CheckSource('TestClassHelper_ForIn',
  20365. LinesToStr([ // statements
  20366. 'rtl.createClass($mod, "TObject", null, function () {',
  20367. ' this.$init = function () {',
  20368. ' };',
  20369. ' this.$final = function () {',
  20370. ' };',
  20371. '});',
  20372. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  20373. ' this.$init = function () {',
  20374. ' $mod.TObject.$init.call(this);',
  20375. ' this.FCurrent = null;',
  20376. ' };',
  20377. ' this.$final = function () {',
  20378. ' this.FCurrent = undefined;',
  20379. ' $mod.TObject.$final.call(this);',
  20380. ' };',
  20381. ' this.MoveNext = function () {',
  20382. ' var Result = false;',
  20383. ' return Result;',
  20384. ' };',
  20385. '});',
  20386. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20387. '});',
  20388. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20389. ' this.GetEnumerator = function () {',
  20390. ' var Result = null;',
  20391. ' return Result;',
  20392. ' };',
  20393. '});',
  20394. 'this.b = null;',
  20395. 'this.i = null;',
  20396. 'this.i2 = null;'
  20397. ]),
  20398. LinesToStr([ // $mod.$main
  20399. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  20400. 'try {',
  20401. ' while ($in1.MoveNext()){',
  20402. ' $mod.i = $in1.FCurrent;',
  20403. ' $mod.i2 = $mod.i;',
  20404. ' }',
  20405. '} finally {',
  20406. ' $in1 = rtl.freeLoc($in1)',
  20407. '};',
  20408. '']));
  20409. end;
  20410. procedure TTestModule.TestClassHelper_PassProperty;
  20411. begin
  20412. StartProgram(false);
  20413. Add([
  20414. 'type',
  20415. ' TObject = class',
  20416. ' FField: TObject;',
  20417. ' property Field: TObject read FField write FField;',
  20418. ' end;',
  20419. ' THelper = class helper for TObject',
  20420. ' procedure Fly;',
  20421. ' class procedure Run;',
  20422. ' class procedure Jump; static;',
  20423. ' end;',
  20424. 'procedure THelper.Fly;',
  20425. 'begin',
  20426. ' Field.Fly;',
  20427. ' Field.Run;',
  20428. ' Field.Jump;',
  20429. ' with Field do begin',
  20430. ' Fly;',
  20431. ' Run;',
  20432. ' Jump;',
  20433. ' end;',
  20434. 'end;',
  20435. 'class procedure THelper.Run;',
  20436. 'begin',
  20437. 'end;',
  20438. 'class procedure THelper.Jump;',
  20439. 'begin',
  20440. 'end;',
  20441. 'var',
  20442. ' b: TObject;',
  20443. 'begin',
  20444. ' b.Field.Fly;',
  20445. ' b.Field.Run;',
  20446. ' b.Field.Jump;',
  20447. ' with b do begin',
  20448. ' Field.Run;',
  20449. ' Field.Fly;',
  20450. ' Field.Jump;',
  20451. ' end;',
  20452. ' with b.Field do begin',
  20453. ' Run;',
  20454. ' Fly;',
  20455. ' Jump;',
  20456. ' end;',
  20457. '']);
  20458. ConvertProgram;
  20459. CheckSource('TestClassHelper_PassProperty',
  20460. LinesToStr([ // statements
  20461. 'rtl.createClass($mod, "TObject", null, function () {',
  20462. ' this.$init = function () {',
  20463. ' this.FField = null;',
  20464. ' };',
  20465. ' this.$final = function () {',
  20466. ' this.FField = undefined;',
  20467. ' };',
  20468. '});',
  20469. 'rtl.createHelper($mod, "THelper", null, function () {',
  20470. ' this.Fly = function () {',
  20471. ' $mod.THelper.Fly.call(this.FField);',
  20472. ' $mod.THelper.Run.call(this.FField.$class);',
  20473. ' $mod.THelper.Jump();',
  20474. ' var $with1 = this.FField;',
  20475. ' $mod.THelper.Fly.call($with1);',
  20476. ' $mod.THelper.Run.call($with1.$class);',
  20477. ' $mod.THelper.Jump();',
  20478. ' };',
  20479. ' this.Run = function () {',
  20480. ' };',
  20481. ' this.Jump = function () {',
  20482. ' };',
  20483. '});',
  20484. 'this.b = null;',
  20485. '']),
  20486. LinesToStr([ // $mod.$main
  20487. '$mod.THelper.Fly.call($mod.b.FField);',
  20488. '$mod.THelper.Run.call($mod.b.FField.$class);',
  20489. '$mod.THelper.Jump();',
  20490. 'var $with1 = $mod.b;',
  20491. '$mod.THelper.Run.call($with1.FField.$class);',
  20492. '$mod.THelper.Fly.call($with1.FField);',
  20493. '$mod.THelper.Jump();',
  20494. 'var $with2 = $mod.b.FField;',
  20495. '$mod.THelper.Run.call($with2.$class);',
  20496. '$mod.THelper.Fly.call($with2);',
  20497. '$mod.THelper.Jump();',
  20498. '']));
  20499. end;
  20500. procedure TTestModule.TestExtClassHelper_ClassVar;
  20501. begin
  20502. StartProgram(false);
  20503. Add([
  20504. '{$modeswitch externalclass}',
  20505. 'type',
  20506. ' TExtA = class external name ''ExtObj''',
  20507. ' end;',
  20508. ' THelper = class helper for TExtA',
  20509. ' const',
  20510. ' One = 1;',
  20511. ' Two: word = 2;',
  20512. ' class var',
  20513. ' Glob: word;',
  20514. ' function Foo(w: word): word;',
  20515. ' class function Bar(w: word): word; static;',
  20516. ' end;',
  20517. 'function THelper.foo(w: word): word;',
  20518. 'begin',
  20519. ' Result:=w;',
  20520. ' Two:=One+w;',
  20521. ' Glob:=Glob;',
  20522. ' Result:=Self.Glob;',
  20523. ' Self.Glob:=Self.Glob;',
  20524. ' with Self do Glob:=Glob;',
  20525. 'end;',
  20526. 'class function THelper.bar(w: word): word;',
  20527. 'begin',
  20528. ' Result:=w;',
  20529. ' Two:=One;',
  20530. ' Glob:=Glob;',
  20531. 'end;',
  20532. 'var o: TExtA;',
  20533. 'begin',
  20534. ' texta.two:=texta.one;',
  20535. ' texta.Glob:=texta.Glob;',
  20536. ' with texta do begin',
  20537. ' two:=one;',
  20538. ' Glob:=Glob;',
  20539. ' end;',
  20540. ' o.two:=o.one;',
  20541. ' o.Glob:=o.Glob;',
  20542. ' with o do begin',
  20543. ' two:=one;',
  20544. ' Glob:=Glob;',
  20545. ' end;',
  20546. '']);
  20547. ConvertProgram;
  20548. CheckSource('TestExtClassHelper_ClassVar',
  20549. LinesToStr([ // statements
  20550. 'rtl.createHelper($mod, "THelper", null, function () {',
  20551. ' this.One = 1;',
  20552. ' this.Two = 2;',
  20553. ' this.Glob = 0;',
  20554. ' this.Foo = function (w) {',
  20555. ' var Result = 0;',
  20556. ' Result = w;',
  20557. ' $mod.THelper.Two = 1 + w;',
  20558. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20559. ' Result = $mod.THelper.Glob;',
  20560. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20561. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20562. ' return Result;',
  20563. ' };',
  20564. ' this.Bar = function (w) {',
  20565. ' var Result = 0;',
  20566. ' Result = w;',
  20567. ' $mod.THelper.Two = 1;',
  20568. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20569. ' return Result;',
  20570. ' };',
  20571. '});',
  20572. 'this.o = null;',
  20573. '']),
  20574. LinesToStr([ // $mod.$main
  20575. '$mod.THelper.Two = 1;',
  20576. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20577. '$mod.THelper.Two = 1;',
  20578. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20579. '$mod.THelper.Two = 1;',
  20580. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20581. 'var $with1 = $mod.o;',
  20582. '$mod.THelper.Two = 1;',
  20583. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20584. '']));
  20585. end;
  20586. procedure TTestModule.TestExtClassHelper_Method_Call;
  20587. begin
  20588. StartProgram(false);
  20589. Add([
  20590. '{$modeswitch externalclass}',
  20591. 'type',
  20592. ' TExtA = class external name ''ExtObj''',
  20593. ' procedure Run(w: word = 10);',
  20594. ' end;',
  20595. ' THelper = class helper for TExtA',
  20596. ' function Foo(w: word = 1): word;',
  20597. ' end;',
  20598. 'function THelper.foo(w: word): word;',
  20599. 'begin',
  20600. ' Run;',
  20601. ' Run();',
  20602. ' Run(11);',
  20603. ' Foo;',
  20604. ' Foo();',
  20605. ' Foo(12);',
  20606. ' Self.Foo;',
  20607. ' Self.Foo();',
  20608. ' Self.Foo(13);',
  20609. ' with Self do begin',
  20610. ' Foo;',
  20611. ' Foo();',
  20612. ' Foo(14);',
  20613. ' end;',
  20614. 'end;',
  20615. 'var Obj: TExtA;',
  20616. 'begin',
  20617. ' obj.Foo;',
  20618. ' obj.Foo();',
  20619. ' obj.Foo(21);',
  20620. ' with obj do begin',
  20621. ' Foo;',
  20622. ' Foo();',
  20623. ' Foo(22);',
  20624. ' end;',
  20625. '']);
  20626. ConvertProgram;
  20627. CheckSource('TestExtClassHelper_Method_Call',
  20628. LinesToStr([ // statements
  20629. 'rtl.createHelper($mod, "THelper", null, function () {',
  20630. ' this.Foo = function (w) {',
  20631. ' var Result = 0;',
  20632. ' this.Run(10);',
  20633. ' this.Run(10);',
  20634. ' this.Run(11);',
  20635. ' $mod.THelper.Foo.call(this, 1);',
  20636. ' $mod.THelper.Foo.call(this, 1);',
  20637. ' $mod.THelper.Foo.call(this, 12);',
  20638. ' $mod.THelper.Foo.call(this, 1);',
  20639. ' $mod.THelper.Foo.call(this, 1);',
  20640. ' $mod.THelper.Foo.call(this, 13);',
  20641. ' $mod.THelper.Foo.call(this, 1);',
  20642. ' $mod.THelper.Foo.call(this, 1);',
  20643. ' $mod.THelper.Foo.call(this, 14);',
  20644. ' return Result;',
  20645. ' };',
  20646. '});',
  20647. 'this.Obj = null;',
  20648. '']),
  20649. LinesToStr([ // $mod.$main
  20650. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20651. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20652. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20653. 'var $with1 = $mod.Obj;',
  20654. '$mod.THelper.Foo.call($with1, 1);',
  20655. '$mod.THelper.Foo.call($with1, 1);',
  20656. '$mod.THelper.Foo.call($with1, 22);',
  20657. '']));
  20658. end;
  20659. procedure TTestModule.TestRecordHelper_ClassVar;
  20660. begin
  20661. StartProgram(false);
  20662. Add([
  20663. 'type',
  20664. ' TRec = record',
  20665. ' end;',
  20666. ' THelper = record helper for TRec',
  20667. ' const',
  20668. ' One = 1;',
  20669. ' Two: word = 2;',
  20670. ' class var',
  20671. ' Glob: word;',
  20672. ' function Foo(w: word): word;',
  20673. ' class function Bar(w: word): word; static;',
  20674. ' end;',
  20675. 'function THelper.foo(w: word): word;',
  20676. 'begin',
  20677. ' Result:=w;',
  20678. ' Two:=One+w;',
  20679. ' Glob:=Glob;',
  20680. ' Result:=Self.Glob;',
  20681. ' Self.Glob:=Self.Glob;',
  20682. ' with Self do Glob:=Glob;',
  20683. ' Self:=Self;',
  20684. 'end;',
  20685. 'class function THelper.bar(w: word): word;',
  20686. 'begin',
  20687. ' Result:=w;',
  20688. ' Two:=One;',
  20689. ' Glob:=Glob;',
  20690. 'end;',
  20691. 'var r: TRec;',
  20692. 'begin',
  20693. ' trec.two:=trec.one;',
  20694. ' trec.Glob:=trec.Glob;',
  20695. ' with trec do begin',
  20696. ' two:=one;',
  20697. ' Glob:=Glob;',
  20698. ' end;',
  20699. ' r.two:=r.one;',
  20700. ' r.Glob:=r.Glob;',
  20701. ' with r do begin',
  20702. ' two:=one;',
  20703. ' Glob:=Glob;',
  20704. ' end;',
  20705. '']);
  20706. ConvertProgram;
  20707. CheckSource('TestRecordHelper_ClassVar',
  20708. LinesToStr([ // statements
  20709. 'rtl.recNewT($mod, "TRec", function () {',
  20710. ' this.$eq = function (b) {',
  20711. ' return true;',
  20712. ' };',
  20713. ' this.$assign = function (s) {',
  20714. ' return this;',
  20715. ' };',
  20716. '});',
  20717. 'rtl.createHelper($mod, "THelper", null, function () {',
  20718. ' this.One = 1;',
  20719. ' this.Two = 2;',
  20720. ' this.Glob = 0;',
  20721. ' this.Foo = function (w) {',
  20722. ' var Result = 0;',
  20723. ' Result = w;',
  20724. ' $mod.THelper.Two = 1 + w;',
  20725. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20726. ' Result = $mod.THelper.Glob;',
  20727. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20728. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20729. ' this.$assign(this);',
  20730. ' return Result;',
  20731. ' };',
  20732. ' this.Bar = function (w) {',
  20733. ' var Result = 0;',
  20734. ' Result = w;',
  20735. ' $mod.THelper.Two = 1;',
  20736. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20737. ' return Result;',
  20738. ' };',
  20739. '});',
  20740. 'this.r = $mod.TRec.$new();',
  20741. '']),
  20742. LinesToStr([ // $mod.$main
  20743. '$mod.THelper.Two = 1;',
  20744. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20745. 'var $with1 = $mod.TRec;',
  20746. '$mod.THelper.Two = 1;',
  20747. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20748. '$mod.THelper.Two = 1;',
  20749. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20750. 'var $with2 = $mod.r;',
  20751. '$mod.THelper.Two = 1;',
  20752. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20753. '']));
  20754. end;
  20755. procedure TTestModule.TestRecordHelper_Method_Call;
  20756. begin
  20757. StartProgram(false);
  20758. Add([
  20759. '{$modeswitch AdvancedRecords}',
  20760. 'type',
  20761. ' TRec = record',
  20762. ' procedure Run(w: word = 10);',
  20763. ' end;',
  20764. ' THelper = record helper for TRec',
  20765. ' function Foo(w: word = 1): word;',
  20766. ' end;',
  20767. 'procedure TRec.Run(w: word);',
  20768. 'begin',
  20769. ' Foo;',
  20770. ' Foo();',
  20771. ' Foo(2);',
  20772. ' Self.Foo;',
  20773. ' Self.Foo();',
  20774. ' Self.Foo(3);',
  20775. ' with Self do begin',
  20776. ' Foo;',
  20777. ' Foo();',
  20778. ' Foo(4);',
  20779. ' end;',
  20780. 'end;',
  20781. 'function THelper.foo(w: word): word;',
  20782. 'begin',
  20783. ' Run;',
  20784. ' Run();',
  20785. ' Run(11);',
  20786. ' Foo;',
  20787. ' Foo();',
  20788. ' Foo(12);',
  20789. ' Self.Foo;',
  20790. ' Self.Foo();',
  20791. ' Self.Foo(13);',
  20792. ' with Self do begin',
  20793. ' Foo;',
  20794. ' Foo();',
  20795. ' Foo(14);',
  20796. ' end;',
  20797. 'end;',
  20798. 'var Rec: TRec;',
  20799. 'begin',
  20800. ' Rec.Foo;',
  20801. ' Rec.Foo();',
  20802. ' Rec.Foo(21);',
  20803. ' with Rec do begin',
  20804. ' Foo;',
  20805. ' Foo();',
  20806. ' Foo(22);',
  20807. ' end;',
  20808. '']);
  20809. ConvertProgram;
  20810. CheckSource('TestRecordHelper_Method_Call',
  20811. LinesToStr([ // statements
  20812. 'rtl.recNewT($mod, "TRec", function () {',
  20813. ' this.$eq = function (b) {',
  20814. ' return true;',
  20815. ' };',
  20816. ' this.$assign = function (s) {',
  20817. ' return this;',
  20818. ' };',
  20819. ' this.Run = function (w) {',
  20820. ' $mod.THelper.Foo.call(this, 1);',
  20821. ' $mod.THelper.Foo.call(this, 1);',
  20822. ' $mod.THelper.Foo.call(this, 2);',
  20823. ' $mod.THelper.Foo.call(this, 1);',
  20824. ' $mod.THelper.Foo.call(this, 1);',
  20825. ' $mod.THelper.Foo.call(this, 3);',
  20826. ' $mod.THelper.Foo.call(this, 1);',
  20827. ' $mod.THelper.Foo.call(this, 1);',
  20828. ' $mod.THelper.Foo.call(this, 4);',
  20829. ' };',
  20830. '});',
  20831. 'rtl.createHelper($mod, "THelper", null, function () {',
  20832. ' this.Foo = function (w) {',
  20833. ' var Result = 0;',
  20834. ' this.Run(10);',
  20835. ' this.Run(10);',
  20836. ' this.Run(11);',
  20837. ' $mod.THelper.Foo.call(this, 1);',
  20838. ' $mod.THelper.Foo.call(this, 1);',
  20839. ' $mod.THelper.Foo.call(this, 12);',
  20840. ' $mod.THelper.Foo.call(this, 1);',
  20841. ' $mod.THelper.Foo.call(this, 1);',
  20842. ' $mod.THelper.Foo.call(this, 13);',
  20843. ' $mod.THelper.Foo.call(this, 1);',
  20844. ' $mod.THelper.Foo.call(this, 1);',
  20845. ' $mod.THelper.Foo.call(this, 14);',
  20846. ' return Result;',
  20847. ' };',
  20848. '});',
  20849. 'this.Rec = $mod.TRec.$new();',
  20850. '']),
  20851. LinesToStr([ // $mod.$main
  20852. '$mod.THelper.Foo.call($mod.Rec, 1);',
  20853. '$mod.THelper.Foo.call($mod.Rec, 1);',
  20854. '$mod.THelper.Foo.call($mod.Rec, 21);',
  20855. 'var $with1 = $mod.Rec;',
  20856. '$mod.THelper.Foo.call($with1, 1);',
  20857. '$mod.THelper.Foo.call($with1, 1);',
  20858. '$mod.THelper.Foo.call($with1, 22);',
  20859. '']));
  20860. end;
  20861. procedure TTestModule.TestRecordHelper_Constructor;
  20862. begin
  20863. StartProgram(false);
  20864. Add([
  20865. '{$modeswitch AdvancedRecords}',
  20866. 'type',
  20867. ' TRec = record',
  20868. ' constructor Create(w: word);',
  20869. ' end;',
  20870. ' THelper = record helper for TRec',
  20871. ' constructor NewHlp(w: word);',
  20872. ' end;',
  20873. 'var',
  20874. ' Rec: TRec;',
  20875. 'constructor TRec.Create(w: word);',
  20876. 'begin',
  20877. ' NewHlp(2);', // normal call
  20878. ' trec.NewHlp(3);', // new instance
  20879. 'end;',
  20880. 'constructor THelper.NewHlp(w: word);',
  20881. 'begin',
  20882. ' create(2);', // normal call
  20883. ' trec.create(3);', // new instance
  20884. ' NewHlp(4);', // normal call
  20885. ' trec.NewHlp(5);', // new instance
  20886. 'end;',
  20887. 'begin',
  20888. ' rec.newhlp(2);', // normal call
  20889. ' with rec do newhlp(12);', // normal call
  20890. ' trec.newhlp(3);', // new instance
  20891. ' with trec do newhlp(13);', // new instance
  20892. '']);
  20893. ConvertProgram;
  20894. CheckSource('TestRecordHelper_Constructor',
  20895. LinesToStr([ // statements
  20896. 'rtl.recNewT($mod, "TRec", function () {',
  20897. ' this.$eq = function (b) {',
  20898. ' return true;',
  20899. ' };',
  20900. ' this.$assign = function (s) {',
  20901. ' return this;',
  20902. ' };',
  20903. ' this.Create = function (w) {',
  20904. ' $mod.THelper.NewHlp.call(this, 2);',
  20905. ' $mod.THelper.$new("NewHlp", [3]);',
  20906. ' return this;',
  20907. ' };',
  20908. '}, true);',
  20909. 'rtl.createHelper($mod, "THelper", null, function () {',
  20910. ' this.NewHlp = function (w) {',
  20911. ' this.Create(2);',
  20912. ' $mod.TRec.$create("Create", [3]);',
  20913. ' $mod.THelper.NewHlp.call(this, 4);',
  20914. ' $mod.THelper.$new("NewHlp", [5]);',
  20915. ' return this;',
  20916. ' };',
  20917. ' this.$new = function (fn, args) {',
  20918. ' return this[fn].apply($mod.TRec.$new(), args);',
  20919. ' };',
  20920. '});',
  20921. 'this.Rec = $mod.TRec.$new();',
  20922. '']),
  20923. LinesToStr([ // $mod.$main
  20924. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  20925. 'var $with1 = $mod.Rec;',
  20926. '$mod.THelper.NewHlp.call($with1, 12);',
  20927. '$mod.THelper.$new("NewHlp", [3]);',
  20928. 'var $with2 = $mod.TRec;',
  20929. '$mod.THelper.$new("NewHlp", [13]);',
  20930. '']));
  20931. end;
  20932. procedure TTestModule.TestTypeHelper_ClassVar;
  20933. begin
  20934. StartProgram(false);
  20935. Add([
  20936. '{$modeswitch typehelpers}',
  20937. 'type',
  20938. ' THelper = type helper for byte',
  20939. ' const',
  20940. ' One = 1;',
  20941. ' Two: word = 2;',
  20942. ' class var',
  20943. ' Glob: word;',
  20944. ' function Foo(w: word): word;',
  20945. ' class function Bar(w: word): word; static;',
  20946. ' end;',
  20947. 'function THelper.foo(w: word): word;',
  20948. 'begin',
  20949. ' Result:=w;',
  20950. ' Two:=One+w;',
  20951. ' Glob:=Glob;',
  20952. ' Result:=Self.Glob;',
  20953. ' Self.Glob:=Self.Glob;',
  20954. ' with Self do Glob:=Glob;',
  20955. 'end;',
  20956. 'class function THelper.bar(w: word): word;',
  20957. 'begin',
  20958. ' Result:=w;',
  20959. ' Two:=One;',
  20960. ' Glob:=Glob;',
  20961. 'end;',
  20962. 'var b: byte;',
  20963. 'begin',
  20964. ' byte.two:=byte.one;',
  20965. ' byte.Glob:=byte.Glob;',
  20966. ' with byte do begin',
  20967. ' two:=one;',
  20968. ' Glob:=Glob;',
  20969. ' end;',
  20970. ' b.two:=b.one;',
  20971. ' b.Glob:=b.Glob;',
  20972. ' with b do begin',
  20973. ' two:=one;',
  20974. ' Glob:=Glob;',
  20975. ' end;',
  20976. '']);
  20977. ConvertProgram;
  20978. CheckSource('TestTypeHelper_ClassVar',
  20979. LinesToStr([ // statements
  20980. 'rtl.createHelper($mod, "THelper", null, function () {',
  20981. ' this.One = 1;',
  20982. ' this.Two = 2;',
  20983. ' this.Glob = 0;',
  20984. ' this.Foo = function (w) {',
  20985. ' var Result = 0;',
  20986. ' Result = w;',
  20987. ' $mod.THelper.Two = 1 + w;',
  20988. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20989. ' Result = $mod.THelper.Glob;',
  20990. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20991. ' var $with1 = this.get();',
  20992. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20993. ' return Result;',
  20994. ' };',
  20995. ' this.Bar = function (w) {',
  20996. ' var Result = 0;',
  20997. ' Result = w;',
  20998. ' $mod.THelper.Two = 1;',
  20999. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21000. ' return Result;',
  21001. ' };',
  21002. '});',
  21003. 'this.b = 0;',
  21004. '']),
  21005. LinesToStr([ // $mod.$main
  21006. '$mod.THelper.Two = 1;',
  21007. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21008. '$mod.THelper.Two = 1;',
  21009. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21010. '$mod.THelper.Two = 1;',
  21011. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21012. 'var $with1 = $mod.b;',
  21013. '$mod.THelper.Two = 1;',
  21014. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21015. '']));
  21016. end;
  21017. procedure TTestModule.TestTypeHelper_PassResultElement;
  21018. begin
  21019. StartProgram(false);
  21020. Add([
  21021. '{$modeswitch typehelpers}',
  21022. 'type',
  21023. ' THelper = type helper for word',
  21024. ' procedure DoIt(e: byte = 123);',
  21025. ' class procedure DoSome(e: byte = 456); static;',
  21026. ' end;',
  21027. 'procedure THelper.DoIt(e: byte);',
  21028. 'begin',
  21029. 'end;',
  21030. 'class procedure THelper.DoSome(e: byte);',
  21031. 'begin',
  21032. 'end;',
  21033. 'function Foo(w: word): word;',
  21034. 'begin',
  21035. ' Result.DoIt;',
  21036. ' Result.DoIt();',
  21037. ' Result.DoSome;',
  21038. ' Result.DoSome();',
  21039. ' with Result do begin',
  21040. ' DoIt;',
  21041. ' DoIt();',
  21042. ' DoSome;',
  21043. ' DoSome();',
  21044. ' end;',
  21045. 'end;',
  21046. 'begin',
  21047. '']);
  21048. ConvertProgram;
  21049. CheckSource('TestTypeHelper_PassResultElement',
  21050. LinesToStr([ // statements
  21051. 'rtl.createHelper($mod, "THelper", null, function () {',
  21052. ' this.DoIt = function (e) {',
  21053. ' };',
  21054. ' this.DoSome = function (e) {',
  21055. ' };',
  21056. '});',
  21057. 'this.Foo = function (w) {',
  21058. ' var Result = 0;',
  21059. ' $mod.THelper.DoIt.call({',
  21060. ' get: function () {',
  21061. ' return Result;',
  21062. ' },',
  21063. ' set: function (v) {',
  21064. ' Result = v;',
  21065. ' }',
  21066. ' }, 123);',
  21067. ' $mod.THelper.DoIt.call({',
  21068. ' get: function () {',
  21069. ' return Result;',
  21070. ' },',
  21071. ' set: function (v) {',
  21072. ' Result = v;',
  21073. ' }',
  21074. ' }, 123);',
  21075. ' $mod.THelper.DoSome(456);',
  21076. ' $mod.THelper.DoSome(456);',
  21077. ' $mod.THelper.DoIt.call({',
  21078. ' get: function () {',
  21079. ' return Result;',
  21080. ' },',
  21081. ' set: function (v) {',
  21082. ' Result = v;',
  21083. ' }',
  21084. ' }, 123);',
  21085. ' $mod.THelper.DoIt.call({',
  21086. ' get: function () {',
  21087. ' return Result;',
  21088. ' },',
  21089. ' set: function (v) {',
  21090. ' Result = v;',
  21091. ' }',
  21092. ' }, 123);',
  21093. ' $mod.THelper.DoSome(456);',
  21094. ' $mod.THelper.DoSome(456);',
  21095. ' return Result;',
  21096. '};',
  21097. '']),
  21098. LinesToStr([ // $mod.$main
  21099. '']));
  21100. end;
  21101. procedure TTestModule.TestTypeHelper_PassArgs;
  21102. begin
  21103. StartProgram(false);
  21104. Add([
  21105. '{$modeswitch typehelpers}',
  21106. 'type',
  21107. ' THelper = type helper for word',
  21108. ' procedure DoIt(e: byte = 123);',
  21109. ' end;',
  21110. 'procedure THelper.DoIt(e: byte);',
  21111. 'begin',
  21112. 'end;',
  21113. 'procedure FooDefault(a: word);',
  21114. 'begin',
  21115. ' a.DoIt;',
  21116. ' with a do DoIt;',
  21117. 'end;',
  21118. 'procedure FooConst(const a: word);',
  21119. 'begin',
  21120. ' a.DoIt;',
  21121. ' with a do DoIt;',
  21122. 'end;',
  21123. 'procedure FooVar(var a: word);',
  21124. 'begin',
  21125. ' a.DoIt;',
  21126. ' with a do DoIt;',
  21127. 'end;',
  21128. 'begin',
  21129. '']);
  21130. ConvertProgram;
  21131. CheckSource('TestTypeHelper_PassArgs',
  21132. LinesToStr([ // statements
  21133. 'rtl.createHelper($mod, "THelper", null, function () {',
  21134. ' this.DoIt = function (e) {',
  21135. ' };',
  21136. '});',
  21137. 'this.FooDefault = function (a) {',
  21138. ' $mod.THelper.DoIt.call({',
  21139. ' get: function () {',
  21140. ' return a;',
  21141. ' },',
  21142. ' set: function (v) {',
  21143. ' a = v;',
  21144. ' }',
  21145. ' }, 123);',
  21146. ' $mod.THelper.DoIt.call({',
  21147. ' get: function () {',
  21148. ' return a;',
  21149. ' },',
  21150. ' set: function (v) {',
  21151. ' a = v;',
  21152. ' }',
  21153. ' }, 123);',
  21154. '};',
  21155. 'this.FooConst = function (a) {',
  21156. ' $mod.THelper.DoIt.call({',
  21157. ' get: function () {',
  21158. ' return a;',
  21159. ' },',
  21160. ' set: function (v) {',
  21161. ' rtl.raiseE("EPropReadOnly");',
  21162. ' }',
  21163. ' }, 123);',
  21164. ' $mod.THelper.DoIt.call({',
  21165. ' get: function () {',
  21166. ' return a;',
  21167. ' },',
  21168. ' set: function () {',
  21169. ' rtl.raiseE("EPropReadOnly");',
  21170. ' }',
  21171. ' }, 123);',
  21172. '};',
  21173. 'this.FooVar = function (a) {',
  21174. ' $mod.THelper.DoIt.call(a, 123);',
  21175. ' var $with1 = a.get();',
  21176. ' $mod.THelper.DoIt.call(a, 123);',
  21177. '};',
  21178. '']),
  21179. LinesToStr([ // $mod.$main
  21180. '']));
  21181. end;
  21182. procedure TTestModule.TestTypeHelper_PassVarConst;
  21183. begin
  21184. StartProgram(false);
  21185. Add([
  21186. '{$modeswitch typehelpers}',
  21187. 'type',
  21188. ' THelper = type helper for word',
  21189. ' procedure DoIt(e: byte = 123);',
  21190. ' end;',
  21191. 'procedure THelper.DoIt(e: byte);',
  21192. 'begin',
  21193. 'end;',
  21194. 'var a: word;',
  21195. 'const c: word = 2;',
  21196. '{$writeableconst off}',
  21197. 'const r: word = 3;',
  21198. 'begin',
  21199. ' a.DoIt;',
  21200. ' with a do DoIt;',
  21201. ' c.DoIt;',
  21202. ' with c do DoIt;',
  21203. ' r.DoIt;',
  21204. ' with r do DoIt;',
  21205. '']);
  21206. ConvertProgram;
  21207. CheckSource('TestTypeHelper_PassVarConst',
  21208. LinesToStr([ // statements
  21209. 'rtl.createHelper($mod, "THelper", null, function () {',
  21210. ' this.DoIt = function (e) {',
  21211. ' };',
  21212. '});',
  21213. 'this.a = 0;',
  21214. 'this.c = 2;',
  21215. 'this.r = 3;',
  21216. '']),
  21217. LinesToStr([ // $mod.$main
  21218. '$mod.THelper.DoIt.call({',
  21219. ' p: $mod,',
  21220. ' get: function () {',
  21221. ' return this.p.a;',
  21222. ' },',
  21223. ' set: function (v) {',
  21224. ' this.p.a = v;',
  21225. ' }',
  21226. '}, 123);',
  21227. 'var $with1 = $mod.a;',
  21228. '$mod.THelper.DoIt.call({',
  21229. ' get: function () {',
  21230. ' return $with1;',
  21231. ' },',
  21232. ' set: function (v) {',
  21233. ' $with1 = v;',
  21234. ' }',
  21235. '}, 123);',
  21236. '$mod.THelper.DoIt.call({',
  21237. ' p: $mod,',
  21238. ' get: function () {',
  21239. ' return this.p.c;',
  21240. ' },',
  21241. ' set: function (v) {',
  21242. ' this.p.c = v;',
  21243. ' }',
  21244. '}, 123);',
  21245. 'var $with2 = $mod.c;',
  21246. '$mod.THelper.DoIt.call({',
  21247. ' get: function () {',
  21248. ' return $with2;',
  21249. ' },',
  21250. ' set: function (v) {',
  21251. ' $with2 = v;',
  21252. ' }',
  21253. '}, 123);',
  21254. '$mod.THelper.DoIt.call({',
  21255. ' get: function () {',
  21256. ' return 3;',
  21257. ' },',
  21258. ' set: function (v) {',
  21259. ' rtl.raiseE("EPropReadOnly");',
  21260. ' }',
  21261. '}, 123);',
  21262. 'var $with3 = 3;',
  21263. ' $mod.THelper.DoIt.call({',
  21264. ' get: function () {',
  21265. ' return $with3;',
  21266. ' },',
  21267. ' set: function () {',
  21268. ' rtl.raiseE("EPropReadOnly");',
  21269. ' }',
  21270. ' }, 123);',
  21271. '']));
  21272. end;
  21273. procedure TTestModule.TestTypeHelper_PassFuncResult;
  21274. begin
  21275. StartProgram(false);
  21276. Add([
  21277. '{$modeswitch typehelpers}',
  21278. 'type',
  21279. ' THelper = type helper for word',
  21280. ' procedure DoIt(e: byte = 123);',
  21281. ' end;',
  21282. 'procedure THelper.DoIt(e: byte);',
  21283. 'begin',
  21284. 'end;',
  21285. 'function Foo(b: byte = 1): word;',
  21286. 'begin',
  21287. 'end;',
  21288. 'begin',
  21289. ' Foo.DoIt;',
  21290. ' Foo().DoIt;',
  21291. ' with Foo do DoIt;',
  21292. ' with Foo() do DoIt;',
  21293. '']);
  21294. ConvertProgram;
  21295. CheckSource('TestTypeHelper_PassFuncResult',
  21296. LinesToStr([ // statements
  21297. 'rtl.createHelper($mod, "THelper", null, function () {',
  21298. ' this.DoIt = function (e) {',
  21299. ' };',
  21300. '});',
  21301. 'this.Foo = function (b) {',
  21302. ' var Result = 0;',
  21303. ' return Result;',
  21304. '};',
  21305. '']),
  21306. LinesToStr([ // $mod.$main
  21307. '$mod.THelper.DoIt.call({',
  21308. ' a: $mod.Foo(1),',
  21309. ' get: function () {',
  21310. ' return this.a;',
  21311. ' },',
  21312. ' set: function (v) {',
  21313. ' this.a = v;',
  21314. ' }',
  21315. '}, 123);',
  21316. '$mod.THelper.DoIt.call({',
  21317. ' a: $mod.Foo(1),',
  21318. ' get: function () {',
  21319. ' return this.a;',
  21320. ' },',
  21321. ' set: function (v) {',
  21322. ' this.a = v;',
  21323. ' }',
  21324. '}, 123);',
  21325. 'var $with1 = $mod.Foo(1);',
  21326. '$mod.THelper.DoIt.call({',
  21327. ' get: function () {',
  21328. ' return $with1;',
  21329. ' },',
  21330. ' set: function (v) {',
  21331. ' $with1 = v;',
  21332. ' }',
  21333. '}, 123);',
  21334. 'var $with2 = $mod.Foo(1);',
  21335. '$mod.THelper.DoIt.call({',
  21336. ' get: function () {',
  21337. ' return $with2;',
  21338. ' },',
  21339. ' set: function (v) {',
  21340. ' $with2 = v;',
  21341. ' }',
  21342. '}, 123);',
  21343. '']));
  21344. end;
  21345. procedure TTestModule.TestTypeHelper_PassPropertyField;
  21346. begin
  21347. StartProgram(false);
  21348. Add([
  21349. '{$modeswitch typehelpers}',
  21350. 'type',
  21351. ' TObject = class',
  21352. ' FField: word;',
  21353. ' procedure SetField(Value: word);',
  21354. ' property Field: word read FField write SetField;',
  21355. ' end;',
  21356. ' THelper = type helper for word',
  21357. ' procedure Fly;',
  21358. ' class procedure Run; static;',
  21359. ' end;',
  21360. 'procedure TObject.SetField(Value: word);',
  21361. 'begin',
  21362. ' Field.Fly;',
  21363. ' Field.Run;',
  21364. ' Self.Field.Fly;',
  21365. ' Self.Field.Run;',
  21366. ' with Self do begin',
  21367. ' Field.Fly;',
  21368. ' Field.Run;',
  21369. ' end;',
  21370. ' with Self.Field do begin',
  21371. ' Fly;',
  21372. ' Run;',
  21373. ' end;',
  21374. 'end;',
  21375. 'procedure THelper.Fly;',
  21376. 'begin',
  21377. 'end;',
  21378. 'class procedure THelper.Run;',
  21379. 'begin',
  21380. 'end;',
  21381. 'var',
  21382. ' o: TObject;',
  21383. 'begin',
  21384. ' o.Field.Fly;',
  21385. ' o.Field.Run;',
  21386. ' with o do begin',
  21387. ' Field.Fly;',
  21388. ' Field.Run;',
  21389. ' end;',
  21390. ' with o.Field do begin',
  21391. ' Fly;',
  21392. ' Run;',
  21393. ' end;',
  21394. '']);
  21395. ConvertProgram;
  21396. CheckSource('TestTypeHelper_PassPropertyField',
  21397. LinesToStr([ // statements
  21398. 'rtl.createClass($mod, "TObject", null, function () {',
  21399. ' this.$init = function () {',
  21400. ' this.FField = 0;',
  21401. ' };',
  21402. ' this.$final = function () {',
  21403. ' };',
  21404. ' this.SetField = function (Value) {',
  21405. ' $mod.THelper.Fly.call({',
  21406. ' p: this,',
  21407. ' get: function () {',
  21408. ' return this.p.FField;',
  21409. ' },',
  21410. ' set: function (v) {',
  21411. ' this.p.FField = v;',
  21412. ' }',
  21413. ' });',
  21414. ' $mod.THelper.Run();',
  21415. ' $mod.THelper.Fly.call({',
  21416. ' p: this,',
  21417. ' get: function () {',
  21418. ' return this.p.FField;',
  21419. ' },',
  21420. ' set: function (v) {',
  21421. ' this.p.FField = v;',
  21422. ' }',
  21423. ' });',
  21424. ' $mod.THelper.Run();',
  21425. ' $mod.THelper.Fly.call({',
  21426. ' p: this,',
  21427. ' get: function () {',
  21428. ' return this.p.FField;',
  21429. ' },',
  21430. ' set: function (v) {',
  21431. ' this.p.FField = v;',
  21432. ' }',
  21433. ' });',
  21434. ' $mod.THelper.Run();',
  21435. ' var $with1 = this.FField;',
  21436. ' $mod.THelper.Fly.call({',
  21437. ' get: function () {',
  21438. ' return $with1;',
  21439. ' },',
  21440. ' set: function (v) {',
  21441. ' $with1 = v;',
  21442. ' }',
  21443. ' });',
  21444. ' $mod.THelper.Run();',
  21445. ' };',
  21446. '});',
  21447. 'rtl.createHelper($mod, "THelper", null, function () {',
  21448. ' this.Fly = function () {',
  21449. ' };',
  21450. ' this.Run = function () {',
  21451. ' };',
  21452. '});',
  21453. 'this.o = null;',
  21454. '']),
  21455. LinesToStr([ // $mod.$main
  21456. '$mod.THelper.Fly.call({',
  21457. ' p: $mod.o,',
  21458. ' get: function () {',
  21459. ' return this.p.FField;',
  21460. ' },',
  21461. ' set: function (v) {',
  21462. ' this.p.FField = v;',
  21463. ' }',
  21464. '});',
  21465. '$mod.THelper.Run();',
  21466. 'var $with1 = $mod.o;',
  21467. '$mod.THelper.Fly.call({',
  21468. ' p: $with1,',
  21469. ' get: function () {',
  21470. ' return this.p.FField;',
  21471. ' },',
  21472. ' set: function (v) {',
  21473. ' this.p.FField = v;',
  21474. ' }',
  21475. '});',
  21476. '$mod.THelper.Run();',
  21477. 'var $with2 = $mod.o.FField;',
  21478. '$mod.THelper.Fly.call({',
  21479. ' get: function () {',
  21480. ' return $with2;',
  21481. ' },',
  21482. ' set: function (v) {',
  21483. ' $with2 = v;',
  21484. ' }',
  21485. '});',
  21486. '$mod.THelper.Run();',
  21487. '']));
  21488. end;
  21489. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  21490. begin
  21491. StartProgram(false);
  21492. Add([
  21493. '{$modeswitch typehelpers}',
  21494. 'type',
  21495. ' TObject = class',
  21496. ' FField: word;',
  21497. ' function GetField: word;',
  21498. ' property Field: word read GetField write FField;',
  21499. ' end;',
  21500. ' THelper = type helper for word',
  21501. ' procedure Fly;',
  21502. ' class procedure Run; static;',
  21503. ' end;',
  21504. 'function TObject.GetField: word;',
  21505. 'begin',
  21506. ' Field.Fly;',
  21507. ' Field.Run;',
  21508. ' Self.Field.Fly;',
  21509. ' Self.Field.Run;',
  21510. ' with Self do begin',
  21511. ' Field.Fly;',
  21512. ' Field.Run;',
  21513. ' end;',
  21514. ' with Self.Field do begin',
  21515. ' Fly;',
  21516. ' Run;',
  21517. ' end;',
  21518. 'end;',
  21519. 'procedure THelper.Fly;',
  21520. 'begin',
  21521. 'end;',
  21522. 'class procedure THelper.Run;',
  21523. 'begin',
  21524. 'end;',
  21525. 'var',
  21526. ' o: TObject;',
  21527. 'begin',
  21528. ' o.Field.Fly;',
  21529. ' o.Field.Run;',
  21530. ' with o do begin',
  21531. ' Field.Fly;',
  21532. ' Field.Run;',
  21533. ' end;',
  21534. ' with o.Field do begin',
  21535. ' Fly;',
  21536. ' Run;',
  21537. ' end;',
  21538. '']);
  21539. ConvertProgram;
  21540. CheckSource('TestTypeHelper_PassPropertyGetter',
  21541. LinesToStr([ // statements
  21542. 'rtl.createClass($mod, "TObject", null, function () {',
  21543. ' this.$init = function () {',
  21544. ' this.FField = 0;',
  21545. ' };',
  21546. ' this.$final = function () {',
  21547. ' };',
  21548. ' this.GetField = function () {',
  21549. ' var Result = 0;',
  21550. ' $mod.THelper.Fly.call({',
  21551. ' p: this.GetField(),',
  21552. ' get: function () {',
  21553. ' return this.p;',
  21554. ' },',
  21555. ' set: function (v) {',
  21556. ' this.p = v;',
  21557. ' }',
  21558. ' });',
  21559. ' $mod.THelper.Run();',
  21560. ' $mod.THelper.Fly.call({',
  21561. ' p: this.GetField(),',
  21562. ' get: function () {',
  21563. ' return this.p;',
  21564. ' },',
  21565. ' set: function (v) {',
  21566. ' this.p = v;',
  21567. ' }',
  21568. ' });',
  21569. ' $mod.THelper.Run();',
  21570. ' $mod.THelper.Fly.call({',
  21571. ' p: this.GetField(),',
  21572. ' get: function () {',
  21573. ' return this.p;',
  21574. ' },',
  21575. ' set: function (v) {',
  21576. ' this.p = v;',
  21577. ' }',
  21578. ' });',
  21579. ' $mod.THelper.Run();',
  21580. ' var $with1 = this.GetField();',
  21581. ' $mod.THelper.Fly.call({',
  21582. ' get: function () {',
  21583. ' return $with1;',
  21584. ' },',
  21585. ' set: function (v) {',
  21586. ' $with1 = v;',
  21587. ' }',
  21588. ' });',
  21589. ' $mod.THelper.Run();',
  21590. ' return Result;',
  21591. ' };',
  21592. '});',
  21593. 'rtl.createHelper($mod, "THelper", null, function () {',
  21594. ' this.Fly = function () {',
  21595. ' };',
  21596. ' this.Run = function () {',
  21597. ' };',
  21598. '});',
  21599. 'this.o = null;',
  21600. '']),
  21601. LinesToStr([ // $mod.$main
  21602. '$mod.THelper.Fly.call({',
  21603. ' p: $mod.o.GetField(),',
  21604. ' get: function () {',
  21605. ' return this.p;',
  21606. ' },',
  21607. ' set: function (v) {',
  21608. ' this.p = v;',
  21609. ' }',
  21610. '});',
  21611. '$mod.THelper.Run();',
  21612. 'var $with1 = $mod.o;',
  21613. '$mod.THelper.Fly.call({',
  21614. ' p: $with1.GetField(),',
  21615. ' get: function () {',
  21616. ' return this.p;',
  21617. ' },',
  21618. ' set: function (v) {',
  21619. ' this.p = v;',
  21620. ' }',
  21621. '});',
  21622. '$mod.THelper.Run();',
  21623. 'var $with2 = $mod.o.GetField();',
  21624. '$mod.THelper.Fly.call({',
  21625. ' get: function () {',
  21626. ' return $with2;',
  21627. ' },',
  21628. ' set: function (v) {',
  21629. ' $with2 = v;',
  21630. ' }',
  21631. '});',
  21632. '$mod.THelper.Run();',
  21633. '']));
  21634. end;
  21635. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  21636. begin
  21637. StartProgram(false);
  21638. Add([
  21639. '{$modeswitch typehelpers}',
  21640. 'type',
  21641. ' TObject = class',
  21642. ' class var FField: word;',
  21643. ' class procedure SetField(Value: word);',
  21644. ' class property Field: word read FField write SetField;',
  21645. ' end;',
  21646. ' THelper = type helper for word',
  21647. ' procedure Fly(n: byte);',
  21648. ' end;',
  21649. 'class procedure TObject.SetField(Value: word);',
  21650. 'begin',
  21651. ' Field.Fly(1);',
  21652. ' Self.Field.Fly(2);',
  21653. ' with Self do Field.Fly(3);',
  21654. ' with Self.Field do Fly(4);',
  21655. ' TObject.Field.Fly(5);',
  21656. ' with TObject do Field.Fly(6);',
  21657. ' with TObject.Field do Fly(7);',
  21658. 'end;',
  21659. 'procedure THelper.Fly(n: byte);',
  21660. 'begin',
  21661. 'end;',
  21662. 'var',
  21663. ' o: TObject;',
  21664. 'begin',
  21665. ' o.Field.Fly(11);',
  21666. ' with o do Field.Fly(12);',
  21667. ' with o.Field do Fly(13);',
  21668. ' TObject.Field.Fly(14);',
  21669. ' with TObject do Field.Fly(15);',
  21670. ' with TObject.Field do Fly(16);',
  21671. '']);
  21672. ConvertProgram;
  21673. CheckSource('TestTypeHelper_PassClassPropertyField',
  21674. LinesToStr([ // statements
  21675. 'rtl.createClass($mod, "TObject", null, function () {',
  21676. ' this.FField = 0;',
  21677. ' this.$init = function () {',
  21678. ' };',
  21679. ' this.$final = function () {',
  21680. ' };',
  21681. ' this.SetField = function (Value) {',
  21682. ' $mod.THelper.Fly.call({',
  21683. ' p: this,',
  21684. ' get: function () {',
  21685. ' return this.p.FField;',
  21686. ' },',
  21687. ' set: function (v) {',
  21688. ' $mod.TObject.FField = v;',
  21689. ' }',
  21690. ' }, 1);',
  21691. ' $mod.THelper.Fly.call({',
  21692. ' p: this,',
  21693. ' get: function () {',
  21694. ' return this.p.FField;',
  21695. ' },',
  21696. ' set: function (v) {',
  21697. ' $mod.TObject.FField = v;',
  21698. ' }',
  21699. ' }, 2);',
  21700. ' $mod.THelper.Fly.call({',
  21701. ' p: this,',
  21702. ' get: function () {',
  21703. ' return this.p.FField;',
  21704. ' },',
  21705. ' set: function (v) {',
  21706. ' $mod.TObject.FField = v;',
  21707. ' }',
  21708. ' }, 3);',
  21709. ' var $with1 = this.FField;',
  21710. ' $mod.THelper.Fly.call({',
  21711. ' get: function () {',
  21712. ' return $with1;',
  21713. ' },',
  21714. ' set: function (v) {',
  21715. ' $with1 = v;',
  21716. ' }',
  21717. ' }, 4);',
  21718. ' $mod.THelper.Fly.call({',
  21719. ' p: $mod.TObject,',
  21720. ' get: function () {',
  21721. ' return this.p.FField;',
  21722. ' },',
  21723. ' set: function (v) {',
  21724. ' $mod.TObject.FField = v;',
  21725. ' }',
  21726. ' }, 5);',
  21727. ' var $with2 = $mod.TObject;',
  21728. ' $mod.THelper.Fly.call({',
  21729. ' p: $with2,',
  21730. ' get: function () {',
  21731. ' return this.p.FField;',
  21732. ' },',
  21733. ' set: function (v) {',
  21734. ' $mod.TObject.FField = v;',
  21735. ' }',
  21736. ' }, 6);',
  21737. ' var $with3 = $mod.TObject.FField;',
  21738. ' $mod.THelper.Fly.call({',
  21739. ' get: function () {',
  21740. ' return $with3;',
  21741. ' },',
  21742. ' set: function (v) {',
  21743. ' $with3 = v;',
  21744. ' }',
  21745. ' }, 7);',
  21746. ' };',
  21747. '});',
  21748. 'rtl.createHelper($mod, "THelper", null, function () {',
  21749. ' this.Fly = function (n) {',
  21750. ' };',
  21751. '});',
  21752. 'this.o = null;',
  21753. '']),
  21754. LinesToStr([ // $mod.$main
  21755. '$mod.THelper.Fly.call({',
  21756. ' p: $mod.o,',
  21757. ' get: function () {',
  21758. ' return this.p.FField;',
  21759. ' },',
  21760. ' set: function (v) {',
  21761. ' $mod.TObject.FField = v;',
  21762. ' }',
  21763. '}, 11);',
  21764. 'var $with1 = $mod.o;',
  21765. '$mod.THelper.Fly.call({',
  21766. ' p: $with1,',
  21767. ' get: function () {',
  21768. ' return this.p.FField;',
  21769. ' },',
  21770. ' set: function (v) {',
  21771. ' $mod.TObject.FField = v;',
  21772. ' }',
  21773. '}, 12);',
  21774. 'var $with2 = $mod.o.FField;',
  21775. '$mod.THelper.Fly.call({',
  21776. ' get: function () {',
  21777. ' return $with2;',
  21778. ' },',
  21779. ' set: function (v) {',
  21780. ' $with2 = v;',
  21781. ' }',
  21782. '}, 13);',
  21783. '$mod.THelper.Fly.call({',
  21784. ' p: $mod.TObject,',
  21785. ' get: function () {',
  21786. ' return this.p.FField;',
  21787. ' },',
  21788. ' set: function (v) {',
  21789. ' $mod.TObject.FField = v;',
  21790. ' }',
  21791. '}, 14);',
  21792. 'var $with3 = $mod.TObject;',
  21793. '$mod.THelper.Fly.call({',
  21794. ' p: $with3,',
  21795. ' get: function () {',
  21796. ' return this.p.FField;',
  21797. ' },',
  21798. ' set: function (v) {',
  21799. ' $mod.TObject.FField = v;',
  21800. ' }',
  21801. '}, 15);',
  21802. 'var $with4 = $mod.TObject.FField;',
  21803. '$mod.THelper.Fly.call({',
  21804. ' get: function () {',
  21805. ' return $with4;',
  21806. ' },',
  21807. ' set: function (v) {',
  21808. ' $with4 = v;',
  21809. ' }',
  21810. '}, 16);',
  21811. '']));
  21812. end;
  21813. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  21814. begin
  21815. StartProgram(false);
  21816. Add([
  21817. '{$modeswitch typehelpers}',
  21818. 'type',
  21819. ' TObject = class',
  21820. ' class var FField: word;',
  21821. ' class function GetField: word; static;',
  21822. ' class property Field: word read GetField write FField;',
  21823. ' end;',
  21824. ' THelper = type helper for word',
  21825. ' procedure Fly(n: byte);',
  21826. ' end;',
  21827. 'class function TObject.GetField: word;',
  21828. 'begin',
  21829. ' Field.Fly(1);',
  21830. ' TObject.Field.Fly(5);',
  21831. ' with TObject do Field.Fly(6);',
  21832. ' with TObject.Field do Fly(7);',
  21833. 'end;',
  21834. 'procedure THelper.Fly(n: byte);',
  21835. 'begin',
  21836. 'end;',
  21837. 'var',
  21838. ' o: TObject;',
  21839. 'begin',
  21840. ' o.Field.Fly(11);',
  21841. ' with o do Field.Fly(12);',
  21842. ' with o.Field do Fly(13);',
  21843. '']);
  21844. ConvertProgram;
  21845. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  21846. LinesToStr([ // statements
  21847. 'rtl.createClass($mod, "TObject", null, function () {',
  21848. ' this.FField = 0;',
  21849. ' this.$init = function () {',
  21850. ' };',
  21851. ' this.$final = function () {',
  21852. ' };',
  21853. ' this.GetField = function () {',
  21854. ' var Result = 0;',
  21855. ' $mod.THelper.Fly.call({',
  21856. ' p: this.GetField(),',
  21857. ' get: function () {',
  21858. ' return this.p;',
  21859. ' },',
  21860. ' set: function (v) {',
  21861. ' this.p = v;',
  21862. ' }',
  21863. ' }, 1);',
  21864. ' $mod.THelper.Fly.call({',
  21865. ' p: $mod.TObject.GetField(),',
  21866. ' get: function () {',
  21867. ' return this.p;',
  21868. ' },',
  21869. ' set: function (v) {',
  21870. ' this.p = v;',
  21871. ' }',
  21872. ' }, 5);',
  21873. ' var $with1 = $mod.TObject;',
  21874. ' $mod.THelper.Fly.call({',
  21875. ' p: $with1.GetField(),',
  21876. ' get: function () {',
  21877. ' return this.p;',
  21878. ' },',
  21879. ' set: function (v) {',
  21880. ' this.p = v;',
  21881. ' }',
  21882. ' }, 6);',
  21883. ' var $with2 = $mod.TObject.GetField();',
  21884. ' $mod.THelper.Fly.call({',
  21885. ' get: function () {',
  21886. ' return $with2;',
  21887. ' },',
  21888. ' set: function (v) {',
  21889. ' $with2 = v;',
  21890. ' }',
  21891. ' }, 7);',
  21892. ' return Result;',
  21893. ' };',
  21894. '});',
  21895. 'rtl.createHelper($mod, "THelper", null, function () {',
  21896. ' this.Fly = function (n) {',
  21897. ' };',
  21898. '});',
  21899. 'this.o = null;',
  21900. '']),
  21901. LinesToStr([ // $mod.$main
  21902. '$mod.THelper.Fly.call({',
  21903. ' p: $mod.o.GetField(),',
  21904. ' get: function () {',
  21905. ' return this.p;',
  21906. ' },',
  21907. ' set: function (v) {',
  21908. ' this.p = v;',
  21909. ' }',
  21910. '}, 11);',
  21911. 'var $with1 = $mod.o;',
  21912. '$mod.THelper.Fly.call({',
  21913. ' p: $with1.GetField(),',
  21914. ' get: function () {',
  21915. ' return this.p;',
  21916. ' },',
  21917. ' set: function (v) {',
  21918. ' this.p = v;',
  21919. ' }',
  21920. '}, 12);',
  21921. 'var $with2 = $mod.o.GetField();',
  21922. '$mod.THelper.Fly.call({',
  21923. ' get: function () {',
  21924. ' return $with2;',
  21925. ' },',
  21926. ' set: function (v) {',
  21927. ' $with2 = v;',
  21928. ' }',
  21929. '}, 13);',
  21930. '']));
  21931. end;
  21932. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  21933. begin
  21934. StartProgram(false);
  21935. Add([
  21936. '{$modeswitch typehelpers}',
  21937. 'type',
  21938. ' TObject = class',
  21939. ' class var FField: word;',
  21940. ' class function GetField: word;',
  21941. ' class property Field: word read GetField write FField;',
  21942. ' end;',
  21943. ' TClass = class of TObject;',
  21944. ' THelper = type helper for word',
  21945. ' procedure Fly(n: byte);',
  21946. ' end;',
  21947. 'class function TObject.GetField: word;',
  21948. 'begin',
  21949. ' Field.Fly(1);',
  21950. ' Self.Field.Fly(5);',
  21951. ' with Self do Field.Fly(6);',
  21952. ' with Self.Field do Fly(7);',
  21953. 'end;',
  21954. 'procedure THelper.Fly(n: byte);',
  21955. 'begin',
  21956. 'end;',
  21957. 'var',
  21958. ' o: TObject;',
  21959. ' c: TClass;',
  21960. 'begin',
  21961. ' o.Field.Fly(11);',
  21962. ' with o do Field.Fly(12);',
  21963. ' with o.Field do Fly(13);',
  21964. ' c.Field.Fly(14);',
  21965. ' with c do Field.Fly(15);',
  21966. ' with c.Field do Fly(16);',
  21967. '']);
  21968. ConvertProgram;
  21969. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  21970. LinesToStr([ // statements
  21971. 'rtl.createClass($mod, "TObject", null, function () {',
  21972. ' this.FField = 0;',
  21973. ' this.$init = function () {',
  21974. ' };',
  21975. ' this.$final = function () {',
  21976. ' };',
  21977. ' this.GetField = function () {',
  21978. ' var Result = 0;',
  21979. ' $mod.THelper.Fly.call({',
  21980. ' p: this.GetField(),',
  21981. ' get: function () {',
  21982. ' return this.p;',
  21983. ' },',
  21984. ' set: function (v) {',
  21985. ' this.p = v;',
  21986. ' }',
  21987. ' }, 1);',
  21988. ' $mod.THelper.Fly.call({',
  21989. ' p: this.GetField(),',
  21990. ' get: function () {',
  21991. ' return this.p;',
  21992. ' },',
  21993. ' set: function (v) {',
  21994. ' this.p = v;',
  21995. ' }',
  21996. ' }, 5);',
  21997. ' $mod.THelper.Fly.call({',
  21998. ' p: this.GetField(),',
  21999. ' get: function () {',
  22000. ' return this.p;',
  22001. ' },',
  22002. ' set: function (v) {',
  22003. ' this.p = v;',
  22004. ' }',
  22005. ' }, 6);',
  22006. ' var $with1 = this.GetField();',
  22007. ' $mod.THelper.Fly.call({',
  22008. ' get: function () {',
  22009. ' return $with1;',
  22010. ' },',
  22011. ' set: function (v) {',
  22012. ' $with1 = v;',
  22013. ' }',
  22014. ' }, 7);',
  22015. ' return Result;',
  22016. ' };',
  22017. '});',
  22018. 'rtl.createHelper($mod, "THelper", null, function () {',
  22019. ' this.Fly = function (n) {',
  22020. ' };',
  22021. '});',
  22022. 'this.o = null;',
  22023. 'this.c = null;',
  22024. '']),
  22025. LinesToStr([ // $mod.$main
  22026. '$mod.THelper.Fly.call({',
  22027. ' p: $mod.o.$class.GetField(),',
  22028. ' get: function () {',
  22029. ' return this.p;',
  22030. ' },',
  22031. ' set: function (v) {',
  22032. ' this.p = v;',
  22033. ' }',
  22034. '}, 11);',
  22035. 'var $with1 = $mod.o;',
  22036. '$mod.THelper.Fly.call({',
  22037. ' p: $with1.$class.GetField(),',
  22038. ' get: function () {',
  22039. ' return this.p;',
  22040. ' },',
  22041. ' set: function (v) {',
  22042. ' this.p = v;',
  22043. ' }',
  22044. '}, 12);',
  22045. 'var $with2 = $mod.o.$class.GetField();',
  22046. '$mod.THelper.Fly.call({',
  22047. ' get: function () {',
  22048. ' return $with2;',
  22049. ' },',
  22050. ' set: function (v) {',
  22051. ' $with2 = v;',
  22052. ' }',
  22053. '}, 13);',
  22054. '$mod.THelper.Fly.call({',
  22055. ' p: $mod.c.GetField(),',
  22056. ' get: function () {',
  22057. ' return this.p;',
  22058. ' },',
  22059. ' set: function (v) {',
  22060. ' this.p = v;',
  22061. ' }',
  22062. '}, 14);',
  22063. 'var $with3 = $mod.c;',
  22064. '$mod.THelper.Fly.call({',
  22065. ' p: $with3.GetField(),',
  22066. ' get: function () {',
  22067. ' return this.p;',
  22068. ' },',
  22069. ' set: function (v) {',
  22070. ' this.p = v;',
  22071. ' }',
  22072. '}, 15);',
  22073. 'var $with4 = $mod.c.GetField();',
  22074. '$mod.THelper.Fly.call({',
  22075. ' get: function () {',
  22076. ' return $with4;',
  22077. ' },',
  22078. ' set: function (v) {',
  22079. ' $with4 = v;',
  22080. ' }',
  22081. '}, 16);',
  22082. '']));
  22083. end;
  22084. procedure TTestModule.TestTypeHelper_Property;
  22085. begin
  22086. StartProgram(false);
  22087. Add([
  22088. '{$modeswitch typehelpers}',
  22089. 'type',
  22090. ' THelper = type helper for word',
  22091. ' function GetSize: longint;',
  22092. ' procedure SetSize(Value: longint);',
  22093. ' property Size: longint read GetSize write SetSize;',
  22094. ' end;',
  22095. 'function THelper.GetSize: longint;',
  22096. 'begin',
  22097. ' Result:=Size+1;',
  22098. ' Size:=2;',
  22099. ' Result:=Self.Size+3;',
  22100. ' Self.Size:=4;',
  22101. ' with Self do begin',
  22102. ' Result:=Size+5;',
  22103. ' Size:=6;',
  22104. ' end;',
  22105. 'end;',
  22106. 'procedure THelper.SetSize(Value: longint);',
  22107. 'begin',
  22108. 'end;',
  22109. 'var w: word;',
  22110. 'begin',
  22111. ' w:=w.Size+7;',
  22112. ' w.Size:=w+8;',
  22113. ' with w do begin',
  22114. ' w:=Size+9;',
  22115. ' Size:=w+10;',
  22116. ' end;',
  22117. '']);
  22118. ConvertProgram;
  22119. CheckSource('TestTypeHelper_Property',
  22120. LinesToStr([ // statements
  22121. 'rtl.createHelper($mod, "THelper", null, function () {',
  22122. ' this.GetSize = function () {',
  22123. ' var Result = 0;',
  22124. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  22125. ' $mod.THelper.SetSize.call(this, 2);',
  22126. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  22127. ' $mod.THelper.SetSize.call(this, 4);',
  22128. ' var $with1 = this.get();',
  22129. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  22130. ' $mod.THelper.SetSize.call(this, 6);',
  22131. ' return Result;',
  22132. ' };',
  22133. ' this.SetSize = function (Value) {',
  22134. ' };',
  22135. '});',
  22136. 'this.w = 0;',
  22137. '']),
  22138. LinesToStr([ // $mod.$main
  22139. '$mod.w = $mod.THelper.GetSize.call({',
  22140. ' p: $mod,',
  22141. ' get: function () {',
  22142. ' return this.p.w;',
  22143. ' },',
  22144. ' set: function (v) {',
  22145. ' this.p.w = v;',
  22146. ' }',
  22147. '}) + 7;',
  22148. '$mod.THelper.SetSize.call({',
  22149. ' p: $mod,',
  22150. ' get: function () {',
  22151. ' return this.p.w;',
  22152. ' },',
  22153. ' set: function (v) {',
  22154. ' this.p.w = v;',
  22155. ' }',
  22156. '}, $mod.w + 8);',
  22157. 'var $with1 = $mod.w;',
  22158. '$mod.w = $mod.THelper.GetSize.call({',
  22159. ' get: function () {',
  22160. ' return $with1;',
  22161. ' },',
  22162. ' set: function (v) {',
  22163. ' $with1 = v;',
  22164. ' }',
  22165. '}) + 9;',
  22166. '$mod.THelper.SetSize.call({',
  22167. ' get: function () {',
  22168. ' return $with1;',
  22169. ' },',
  22170. ' set: function (v) {',
  22171. ' $with1 = v;',
  22172. ' }',
  22173. '}, $mod.w + 10);',
  22174. '']));
  22175. end;
  22176. procedure TTestModule.TestTypeHelper_Property_Array;
  22177. begin
  22178. StartProgram(false);
  22179. Add([
  22180. '{$modeswitch typehelpers}',
  22181. 'type',
  22182. ' THelper = type helper for word',
  22183. ' function GetItems(Index: byte): boolean;',
  22184. ' procedure SetItems(Index: byte; Value: boolean);',
  22185. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  22186. ' end;',
  22187. 'function THelper.GetItems(Index: byte): boolean;',
  22188. 'begin',
  22189. ' Result:=Items[1];',
  22190. ' Items[2]:=false;',
  22191. ' Result:=Self.Items[3];',
  22192. ' Self.Items[4]:=true;',
  22193. ' with Self do begin',
  22194. ' Result:=Items[5];',
  22195. ' Items[6]:=false;',
  22196. ' end;',
  22197. 'end;',
  22198. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  22199. 'begin',
  22200. 'end;',
  22201. 'var',
  22202. ' w: word;',
  22203. ' b: boolean;',
  22204. 'begin',
  22205. ' b:=w.Items[1];',
  22206. ' w.Items[2]:=b;',
  22207. ' with w do begin',
  22208. ' b:=Items[3];',
  22209. ' Items[4]:=b;',
  22210. ' end;',
  22211. '']);
  22212. ConvertProgram;
  22213. CheckSource('TestTypeHelper_Property_Array',
  22214. LinesToStr([ // statements
  22215. 'rtl.createHelper($mod, "THelper", null, function () {',
  22216. ' this.GetItems = function (Index) {',
  22217. ' var Result = false;',
  22218. ' Result = $mod.THelper.GetItems.call(this, 1);',
  22219. ' $mod.THelper.SetItems.call(this, 2, false);',
  22220. ' Result = $mod.THelper.GetItems.call(this, 3);',
  22221. ' $mod.THelper.SetItems.call(this, 4, true);',
  22222. ' var $with1 = this.get();',
  22223. ' Result = $mod.THelper.GetItems.call(this, 5);',
  22224. ' $mod.THelper.SetItems.call(this, 6, false);',
  22225. ' return Result;',
  22226. ' };',
  22227. ' this.SetItems = function (Index, Value) {',
  22228. ' };',
  22229. '});',
  22230. 'this.w = 0;',
  22231. 'this.b = false;',
  22232. '']),
  22233. LinesToStr([ // $mod.$main
  22234. '$mod.b = $mod.THelper.GetItems.call({',
  22235. ' p: $mod,',
  22236. ' get: function () {',
  22237. ' return this.p.w;',
  22238. ' },',
  22239. ' set: function (v) {',
  22240. ' this.p.w = v;',
  22241. ' }',
  22242. '}, 1);',
  22243. '$mod.THelper.SetItems.call({',
  22244. ' p: $mod,',
  22245. ' get: function () {',
  22246. ' return this.p.w;',
  22247. ' },',
  22248. ' set: function (v) {',
  22249. ' this.p.w = v;',
  22250. ' }',
  22251. '}, 2, $mod.b);',
  22252. 'var $with1 = $mod.w;',
  22253. '$mod.b = $mod.THelper.GetItems.call({',
  22254. ' get: function () {',
  22255. ' return $with1;',
  22256. ' },',
  22257. ' set: function (v) {',
  22258. ' $with1 = v;',
  22259. ' }',
  22260. '}, 3);',
  22261. '$mod.THelper.SetItems.call({',
  22262. ' get: function () {',
  22263. ' return $with1;',
  22264. ' },',
  22265. ' set: function (v) {',
  22266. ' $with1 = v;',
  22267. ' }',
  22268. '}, 4, $mod.b);',
  22269. '']));
  22270. end;
  22271. procedure TTestModule.TestTypeHelper_ClassProperty;
  22272. begin
  22273. StartProgram(false);
  22274. Add([
  22275. '{$modeswitch typehelpers}',
  22276. 'type',
  22277. ' THelper = type helper for word',
  22278. ' class function GetSize: longint; static;',
  22279. ' class procedure SetSize(Value: longint); static;',
  22280. ' class property Size: longint read GetSize write SetSize;',
  22281. ' end;',
  22282. 'class function THelper.GetSize: longint;',
  22283. 'begin',
  22284. ' Result:=Size+1;',
  22285. ' Size:=2;',
  22286. 'end;',
  22287. 'class procedure THelper.SetSize(Value: longint);',
  22288. 'begin',
  22289. 'end;',
  22290. 'begin',
  22291. '']);
  22292. ConvertProgram;
  22293. CheckSource('TestTypeHelper_ClassProperty',
  22294. LinesToStr([ // statements
  22295. 'rtl.createHelper($mod, "THelper", null, function () {',
  22296. ' this.GetSize = function () {',
  22297. ' var Result = 0;',
  22298. ' Result = $mod.THelper.GetSize() + 1;',
  22299. ' $mod.THelper.SetSize(2);',
  22300. ' return Result;',
  22301. ' };',
  22302. ' this.SetSize = function (Value) {',
  22303. ' };',
  22304. '});',
  22305. '']),
  22306. LinesToStr([ // $mod.$main
  22307. '']));
  22308. end;
  22309. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  22310. begin
  22311. StartProgram(false);
  22312. Add([
  22313. '{$modeswitch typehelpers}',
  22314. 'type',
  22315. ' THelper = type helper for word',
  22316. ' class function GetItems(Index: byte): boolean; static;',
  22317. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  22318. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  22319. ' end;',
  22320. 'class function THelper.GetItems(Index: byte): boolean;',
  22321. 'begin',
  22322. ' Result:=Items[1];',
  22323. ' Items[2]:=false;',
  22324. 'end;',
  22325. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  22326. 'begin',
  22327. 'end;',
  22328. 'var',
  22329. ' w: word;',
  22330. ' b: boolean;',
  22331. 'begin',
  22332. ' b:=w.Items[1];',
  22333. ' w.Items[2]:=b;',
  22334. ' with w do begin',
  22335. ' b:=Items[3];',
  22336. ' Items[4]:=b;',
  22337. ' end;',
  22338. '']);
  22339. ConvertProgram;
  22340. CheckSource('TestTypeHelper_ClassProperty_Array',
  22341. LinesToStr([ // statements
  22342. 'rtl.createHelper($mod, "THelper", null, function () {',
  22343. ' this.GetItems = function (Index) {',
  22344. ' var Result = false;',
  22345. ' Result = $mod.THelper.GetItems(1);',
  22346. ' $mod.THelper.SetItems(2, false);',
  22347. ' return Result;',
  22348. ' };',
  22349. ' this.SetItems = function (Index, Value) {',
  22350. ' };',
  22351. '});',
  22352. 'this.w = 0;',
  22353. 'this.b = false;',
  22354. '']),
  22355. LinesToStr([ // $mod.$main
  22356. '$mod.b = $mod.THelper.GetItems(1);',
  22357. '$mod.THelper.SetItems(2, $mod.b);',
  22358. 'var $with1 = $mod.w;',
  22359. '$mod.b = $mod.THelper.GetItems(3);',
  22360. '$mod.THelper.SetItems(4, $mod.b);',
  22361. '']));
  22362. end;
  22363. procedure TTestModule.TestTypeHelper_ClassMethod;
  22364. begin
  22365. StartProgram(false);
  22366. Add([
  22367. '{$modeswitch typehelpers}',
  22368. 'type',
  22369. ' THelper = type helper for word',
  22370. ' class procedure DoStatic; static;',
  22371. ' end;',
  22372. 'class procedure THelper.DoStatic;',
  22373. 'begin',
  22374. ' DoStatic;',
  22375. ' DoStatic();',
  22376. 'end;',
  22377. 'var w: word;',
  22378. 'begin',
  22379. ' w.DoStatic;',
  22380. ' w.DoStatic();',
  22381. '']);
  22382. ConvertProgram;
  22383. CheckSource('TestTypeHelper_ClassMethod',
  22384. LinesToStr([ // statements
  22385. 'rtl.createHelper($mod, "THelper", null, function () {',
  22386. ' this.DoStatic = function () {',
  22387. ' $mod.THelper.DoStatic();',
  22388. ' $mod.THelper.DoStatic();',
  22389. ' };',
  22390. '});',
  22391. 'this.w = 0;',
  22392. '']),
  22393. LinesToStr([ // $mod.$main
  22394. '$mod.THelper.DoStatic();',
  22395. '$mod.THelper.DoStatic();',
  22396. '']));
  22397. end;
  22398. procedure TTestModule.TestTypeHelper_Constructor;
  22399. begin
  22400. StartProgram(false);
  22401. Add([
  22402. '{$modeswitch typehelpers}',
  22403. 'type',
  22404. ' THelper = type helper for word',
  22405. ' constructor Init(e: longint);',
  22406. ' end;',
  22407. 'constructor THelper.Init(e: longint);',
  22408. 'begin',
  22409. ' Self:=e;',
  22410. ' Init(e+1);',
  22411. 'end;',
  22412. 'var w: word;',
  22413. 'begin',
  22414. ' w:=word.Init(2);',
  22415. ' w:=w.Init(3);',
  22416. ' with word do w:=Init(4);',
  22417. ' with w do w:=Init(5);',
  22418. '']);
  22419. ConvertProgram;
  22420. CheckSource('TestTypeHelper_Constructor',
  22421. LinesToStr([ // statements
  22422. 'rtl.createHelper($mod, "THelper", null, function () {',
  22423. ' this.Init = function (e) {',
  22424. ' this.set(e);',
  22425. ' $mod.THelper.Init.call(this, e + 1);',
  22426. ' return this.get();',
  22427. ' };',
  22428. ' this.$new = function (fn, args) {',
  22429. ' return this[fn].apply({',
  22430. ' p: 0,',
  22431. ' get: function () {',
  22432. ' return this.p;',
  22433. ' },',
  22434. ' set: function (v) {',
  22435. ' this.p = v;',
  22436. ' }',
  22437. ' }, args);',
  22438. ' };',
  22439. '});',
  22440. 'this.w = 0;',
  22441. '']),
  22442. LinesToStr([ // $mod.$main
  22443. '$mod.w = $mod.THelper.$new("Init", [2]);',
  22444. '$mod.w = $mod.THelper.Init.call({',
  22445. ' p: $mod,',
  22446. ' get: function () {',
  22447. ' return this.p.w;',
  22448. ' },',
  22449. ' set: function (v) {',
  22450. ' this.p.w = v;',
  22451. ' }',
  22452. '}, 3);',
  22453. '$mod.w = $mod.THelper.$new("Init", [4]);',
  22454. 'var $with1 = $mod.w;',
  22455. '$mod.w = $mod.THelper.Init.call({',
  22456. ' get: function () {',
  22457. ' return $with1;',
  22458. ' },',
  22459. ' set: function (v) {',
  22460. ' $with1 = v;',
  22461. ' }',
  22462. '}, 5);',
  22463. '']));
  22464. end;
  22465. procedure TTestModule.TestTypeHelper_Word;
  22466. begin
  22467. StartProgram(false);
  22468. Add([
  22469. '{$modeswitch typehelpers}',
  22470. 'type',
  22471. ' THelper = type helper for word',
  22472. ' procedure DoIt(e: byte = 123);',
  22473. ' end;',
  22474. 'procedure THelper.DoIt(e: byte);',
  22475. 'begin',
  22476. ' Self:=e;',
  22477. ' Self:=Self+1;',
  22478. ' with Self do Doit;',
  22479. 'end;',
  22480. 'begin',
  22481. ' word(3).DoIt;',
  22482. '']);
  22483. ConvertProgram;
  22484. CheckSource('TestTypeHelper_Word',
  22485. LinesToStr([ // statements
  22486. 'rtl.createHelper($mod, "THelper", null, function () {',
  22487. ' this.DoIt = function (e) {',
  22488. ' this.set(e);',
  22489. ' this.set(this.get() + 1);',
  22490. ' var $with1 = this.get();',
  22491. ' $mod.THelper.DoIt.call(this, 123);',
  22492. ' };',
  22493. '});',
  22494. '']),
  22495. LinesToStr([ // $mod.$main
  22496. '$mod.THelper.DoIt.call({',
  22497. ' get: function () {',
  22498. ' return 3;',
  22499. ' },',
  22500. ' set: function (v) {',
  22501. ' rtl.raiseE("EPropReadOnly");',
  22502. ' }',
  22503. '}, 123);',
  22504. '']));
  22505. end;
  22506. procedure TTestModule.TestTypeHelper_Double;
  22507. begin
  22508. StartProgram(false);
  22509. Add([
  22510. '{$modeswitch typehelpers}',
  22511. 'type',
  22512. ' Float = type double;',
  22513. ' THelper = type helper for double',
  22514. ' const NPI = 3.141592;',
  22515. ' function ToStr: String;',
  22516. ' end;',
  22517. 'function THelper.ToStr: String;',
  22518. 'begin',
  22519. 'end;',
  22520. 'procedure DoIt(s: string);',
  22521. 'begin',
  22522. 'end;',
  22523. 'var f: Float;',
  22524. 'begin',
  22525. ' DoIt(f.toStr);',
  22526. ' DoIt(f.toStr());',
  22527. ' (f*f).toStr;',
  22528. ' DoIt((f*f).toStr);',
  22529. '']);
  22530. ConvertProgram;
  22531. CheckSource('TestTypeHelper_Double',
  22532. LinesToStr([ // statements
  22533. 'rtl.createHelper($mod, "THelper", null, function () {',
  22534. ' this.NPI = 3.141592;',
  22535. ' this.ToStr = function () {',
  22536. ' var Result = "";',
  22537. ' return Result;',
  22538. ' };',
  22539. '});',
  22540. 'this.DoIt = function (s) {',
  22541. '};',
  22542. 'this.f = 0.0;',
  22543. '']),
  22544. LinesToStr([ // $mod.$main
  22545. '$mod.DoIt($mod.THelper.ToStr.call({',
  22546. ' p: $mod,',
  22547. ' get: function () {',
  22548. ' return this.p.f;',
  22549. ' },',
  22550. ' set: function (v) {',
  22551. ' this.p.f = v;',
  22552. ' }',
  22553. '}));',
  22554. '$mod.DoIt($mod.THelper.ToStr.call({',
  22555. ' p: $mod,',
  22556. ' get: function () {',
  22557. ' return this.p.f;',
  22558. ' },',
  22559. ' set: function (v) {',
  22560. ' this.p.f = v;',
  22561. ' }',
  22562. '}));',
  22563. '$mod.THelper.ToStr.call({',
  22564. ' a: $mod.f * $mod.f,',
  22565. ' get: function () {',
  22566. ' return this.a;',
  22567. ' },',
  22568. ' set: function (v) {',
  22569. ' rtl.raiseE("EPropReadOnly");',
  22570. ' }',
  22571. '});',
  22572. '$mod.DoIt($mod.THelper.ToStr.call({',
  22573. ' a: $mod.f * $mod.f,',
  22574. ' get: function () {',
  22575. ' return this.a;',
  22576. ' },',
  22577. ' set: function (v) {',
  22578. ' rtl.raiseE("EPropReadOnly");',
  22579. ' }',
  22580. '}));',
  22581. '']));
  22582. end;
  22583. procedure TTestModule.TestTypeHelper_StringChar;
  22584. begin
  22585. StartProgram(false);
  22586. Add([
  22587. '{$modeswitch typehelpers}',
  22588. 'type',
  22589. ' TStringHelper = type helper for string',
  22590. ' procedure DoIt(e: byte = 123);',
  22591. ' end;',
  22592. ' TCharHelper = type helper for char',
  22593. ' procedure Fly;',
  22594. ' end;',
  22595. 'procedure TStringHelper.DoIt(e: byte);',
  22596. 'begin',
  22597. ' Self[1]:=''c'';',
  22598. ' Self[2]:=Self[3];',
  22599. 'end;',
  22600. 'procedure TCharHelper.Fly;',
  22601. 'begin',
  22602. ' Self:=''c'';',
  22603. 'end;',
  22604. 'begin',
  22605. ' ''abc''.DoIt;',
  22606. ' ''xyz''.DoIt();',
  22607. ' ''c''.Fly();',
  22608. '']);
  22609. ConvertProgram;
  22610. CheckSource('TestTypeHelper_StringChar',
  22611. LinesToStr([ // statements
  22612. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  22613. ' this.DoIt = function (e) {',
  22614. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  22615. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  22616. ' };',
  22617. '});',
  22618. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  22619. ' this.Fly = function () {',
  22620. ' this.set("c");',
  22621. ' };',
  22622. '});',
  22623. '']),
  22624. LinesToStr([ // $mod.$main
  22625. '$mod.TStringHelper.DoIt.call({',
  22626. ' get: function () {',
  22627. ' return "abc";',
  22628. ' },',
  22629. ' set: function (v) {',
  22630. ' rtl.raiseE("EPropReadOnly");',
  22631. ' }',
  22632. '}, 123);',
  22633. '$mod.TStringHelper.DoIt.call({',
  22634. ' get: function () {',
  22635. ' return "xyz";',
  22636. ' },',
  22637. ' set: function (v) {',
  22638. ' rtl.raiseE("EPropReadOnly");',
  22639. ' }',
  22640. '}, 123);',
  22641. '$mod.TCharHelper.Fly.call({',
  22642. ' get: function () {',
  22643. ' return "c";',
  22644. ' },',
  22645. ' set: function (v) {',
  22646. ' rtl.raiseE("EPropReadOnly");',
  22647. ' }',
  22648. '});',
  22649. '']));
  22650. end;
  22651. procedure TTestModule.TestTypeHelper_Array;
  22652. begin
  22653. StartProgram(false);
  22654. Add([
  22655. '{$modeswitch typehelpers}',
  22656. 'type',
  22657. ' TArrOfBool = array of boolean;',
  22658. ' TArrOfJS = array of jsvalue;',
  22659. ' THelper = type helper for TArrOfBool',
  22660. ' procedure DoIt(e: byte = 123);',
  22661. ' end;',
  22662. 'procedure THelper.DoIt(e: byte);',
  22663. 'begin',
  22664. ' Self[1]:=true;',
  22665. ' Self[2]:=not Self[3];',
  22666. ' SetLength(Self,4);',
  22667. 'end;',
  22668. 'var',
  22669. ' b: TArrOfBool;',
  22670. ' j: TArrOfJS;',
  22671. 'begin',
  22672. ' b.DoIt;',
  22673. ' TArrOfBool(j).DoIt();',
  22674. '']);
  22675. ConvertProgram;
  22676. CheckSource('TestTypeHelper_Array',
  22677. LinesToStr([ // statements
  22678. 'rtl.createHelper($mod, "THelper", null, function () {',
  22679. ' this.DoIt = function (e) {',
  22680. ' this.get()[1] = true;',
  22681. ' this.get()[2] = !this.get()[3];',
  22682. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  22683. ' };',
  22684. '});',
  22685. 'this.b = [];',
  22686. 'this.j = [];',
  22687. '']),
  22688. LinesToStr([ // $mod.$main
  22689. '$mod.THelper.DoIt.call({',
  22690. ' p: $mod,',
  22691. ' get: function () {',
  22692. ' return this.p.b;',
  22693. ' },',
  22694. ' set: function (v) {',
  22695. ' this.p.b = v;',
  22696. ' }',
  22697. '}, 123);',
  22698. '$mod.THelper.DoIt.call({',
  22699. ' p: $mod,',
  22700. ' get: function () {',
  22701. ' return this.p.j;',
  22702. ' },',
  22703. ' set: function (v) {',
  22704. ' this.p.j = v;',
  22705. ' }',
  22706. '}, 123);',
  22707. '']));
  22708. end;
  22709. procedure TTestModule.TestTypeHelper_EnumType;
  22710. begin
  22711. StartProgram(false);
  22712. Add([
  22713. '{$modeswitch typehelpers}',
  22714. 'type',
  22715. ' TEnum = (red,blue);',
  22716. ' THelper = type helper for TEnum',
  22717. ' procedure DoIt(e: byte = 123);',
  22718. ' class procedure Swing(w: word); static;',
  22719. ' end;',
  22720. 'procedure THelper.DoIt(e: byte);',
  22721. 'begin',
  22722. ' Self:=red;',
  22723. ' Self:=succ(Self);',
  22724. ' with Self do Doit;',
  22725. 'end;',
  22726. 'class procedure THelper.Swing(w: word);',
  22727. 'begin',
  22728. 'end;',
  22729. 'var e: TEnum;',
  22730. 'begin',
  22731. ' e.DoIt;',
  22732. ' red.DoIt;',
  22733. ' TEnum.blue.DoIt;',
  22734. ' TEnum(1).DoIt;',
  22735. ' TEnum.Swing(3);',
  22736. '']);
  22737. ConvertProgram;
  22738. CheckSource('TestTypeHelper_EnumType',
  22739. LinesToStr([ // statements
  22740. 'this.TEnum = {',
  22741. ' "0": "red",',
  22742. ' red: 0,',
  22743. ' "1": "blue",',
  22744. ' blue: 1',
  22745. '};',
  22746. 'rtl.createHelper($mod, "THelper", null, function () {',
  22747. ' this.DoIt = function (e) {',
  22748. ' this.set($mod.TEnum.red);',
  22749. ' this.set(this.get() + 1);',
  22750. ' var $with1 = this.get();',
  22751. ' $mod.THelper.DoIt.call(this, 123);',
  22752. ' };',
  22753. ' this.Swing = function (w) {',
  22754. ' };',
  22755. '});',
  22756. 'this.e = 0;',
  22757. '']),
  22758. LinesToStr([ // $mod.$main
  22759. '$mod.THelper.DoIt.call({',
  22760. ' p: $mod,',
  22761. ' get: function () {',
  22762. ' return this.p.e;',
  22763. ' },',
  22764. ' set: function (v) {',
  22765. ' this.p.e = v;',
  22766. ' }',
  22767. '}, 123);',
  22768. '$mod.THelper.DoIt.call({',
  22769. ' p: $mod.TEnum,',
  22770. ' get: function () {',
  22771. ' return this.p.red;',
  22772. ' },',
  22773. ' set: function (v) {',
  22774. ' rtl.raiseE("EPropReadOnly");',
  22775. ' }',
  22776. '}, 123);',
  22777. '$mod.THelper.DoIt.call({',
  22778. ' p: $mod.TEnum,',
  22779. ' get: function () {',
  22780. ' return this.p.blue;',
  22781. ' },',
  22782. ' set: function (v) {',
  22783. ' rtl.raiseE("EPropReadOnly");',
  22784. ' }',
  22785. '}, 123);',
  22786. '$mod.THelper.DoIt.call({',
  22787. ' get: function () {',
  22788. ' return 1;',
  22789. ' },',
  22790. ' set: function (v) {',
  22791. ' rtl.raiseE("EPropReadOnly");',
  22792. ' }',
  22793. '}, 123);',
  22794. '$mod.THelper.Swing(3);',
  22795. '']));
  22796. end;
  22797. procedure TTestModule.TestTypeHelper_SetType;
  22798. begin
  22799. StartProgram(false);
  22800. Add([
  22801. '{$modeswitch typehelpers}',
  22802. 'type',
  22803. ' TEnum = (red,blue);',
  22804. ' TSetOfEnum = set of TEnum;',
  22805. ' THelper = type helper for TSetOfEnum',
  22806. ' procedure DoIt(e: byte = 123);',
  22807. ' constructor Init(e: TEnum);',
  22808. ' constructor InitEmpty;',
  22809. ' end;',
  22810. 'procedure THelper.DoIt(e: byte);',
  22811. 'begin',
  22812. ' Self:=[];',
  22813. ' Self:=[red];',
  22814. ' Include(Self,blue);',
  22815. 'end;',
  22816. 'constructor THelper.Init(e: TEnum);',
  22817. 'begin',
  22818. ' Self:=[];',
  22819. ' Self:=[e];',
  22820. ' Include(Self,blue);',
  22821. 'end;',
  22822. 'constructor THelper.InitEmpty;',
  22823. 'begin',
  22824. 'end;',
  22825. 'var s: TSetOfEnum;',
  22826. 'begin',
  22827. ' s.DoIt;',
  22828. //' [red].DoIt;',
  22829. //' with s do DoIt;',
  22830. //' with [red,blue] do DoIt;',
  22831. ' s:=TSetOfEnum.Init(blue);',
  22832. ' s:=s.Init(blue);',
  22833. '']);
  22834. ConvertProgram;
  22835. CheckSource('TestTypeHelper_SetType',
  22836. LinesToStr([ // statements
  22837. 'this.TEnum = {',
  22838. ' "0": "red",',
  22839. ' red: 0,',
  22840. ' "1": "blue",',
  22841. ' blue: 1',
  22842. '};',
  22843. 'rtl.createHelper($mod, "THelper", null, function () {',
  22844. ' this.DoIt = function (e) {',
  22845. ' this.set({});',
  22846. ' this.set(rtl.createSet($mod.TEnum.red));',
  22847. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  22848. ' };',
  22849. ' this.Init = function (e) {',
  22850. ' this.set({});',
  22851. ' this.set(rtl.createSet(e));',
  22852. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  22853. ' return this.get();',
  22854. ' };',
  22855. ' this.InitEmpty = function () {',
  22856. ' return this.get();',
  22857. ' };',
  22858. ' this.$new = function (fn, args) {',
  22859. ' return this[fn].apply({',
  22860. ' p: {},',
  22861. ' get: function () {',
  22862. ' return this.p;',
  22863. ' },',
  22864. ' set: function (v) {',
  22865. ' this.p = v;',
  22866. ' }',
  22867. ' }, args);',
  22868. ' };',
  22869. '});',
  22870. 'this.s = {};',
  22871. '']),
  22872. LinesToStr([ // $mod.$main
  22873. '$mod.THelper.DoIt.call({',
  22874. ' p: $mod,',
  22875. ' get: function () {',
  22876. ' return this.p.s;',
  22877. ' },',
  22878. ' set: function (v) {',
  22879. ' this.p.s = v;',
  22880. ' }',
  22881. '}, 123);',
  22882. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  22883. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  22884. ' p: $mod,',
  22885. ' get: function () {',
  22886. ' return this.p.s;',
  22887. ' },',
  22888. ' set: function (v) {',
  22889. ' this.p.s = v;',
  22890. ' }',
  22891. '}, $mod.TEnum.blue));',
  22892. '']));
  22893. end;
  22894. procedure TTestModule.TestTypeHelper_InterfaceType;
  22895. begin
  22896. StartProgram(false);
  22897. Add([
  22898. '{$interfaces com}',
  22899. '{$modeswitch typehelpers}',
  22900. 'type',
  22901. ' IUnknown = interface',
  22902. ' function _AddRef: longint;',
  22903. ' function _Release: longint;',
  22904. ' end;',
  22905. ' TObject = class(IUnknown)',
  22906. ' function _AddRef: longint; virtual; abstract;',
  22907. ' function _Release: longint; virtual; abstract;',
  22908. ' end;',
  22909. ' THelper = type helper for IUnknown',
  22910. ' procedure Fly(e: byte = 123);',
  22911. ' class procedure Run; static;',
  22912. ' end;',
  22913. 'var',
  22914. ' i: IUnknown;',
  22915. ' o: TObject;',
  22916. 'procedure THelper.Fly(e: byte);',
  22917. 'begin',
  22918. ' i:=Self;',
  22919. ' o:=Self as TObject;',
  22920. ' Self:=nil;',
  22921. ' Self:=i;',
  22922. ' Self:=o;',
  22923. ' with Self do begin',
  22924. ' Fly;',
  22925. ' Fly();',
  22926. ' end;',
  22927. 'end;',
  22928. 'class procedure THelper.Run;',
  22929. 'var l: IUnknown;',
  22930. 'begin',
  22931. ' l.Fly;',
  22932. ' l.Fly();',
  22933. 'end;',
  22934. 'begin',
  22935. ' i.Fly;',
  22936. ' i.Fly();',
  22937. ' i.Run;',
  22938. ' i.Run();',
  22939. ' IUnknown.Run;',
  22940. ' IUnknown.Run();',
  22941. '']);
  22942. ConvertProgram;
  22943. CheckSource('TestTypeHelper_InterfaceType',
  22944. LinesToStr([ // statements
  22945. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22946. 'rtl.createClass($mod, "TObject", null, function () {',
  22947. ' this.$init = function () {',
  22948. ' };',
  22949. ' this.$final = function () {',
  22950. ' };',
  22951. ' rtl.addIntf(this, $mod.IUnknown);',
  22952. '});',
  22953. 'rtl.createHelper($mod, "THelper", null, function () {',
  22954. ' this.Fly = function (e) {',
  22955. ' var $ir = rtl.createIntfRefs();',
  22956. ' try {',
  22957. ' rtl.setIntfP($mod, "i", this.get());',
  22958. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  22959. ' this.set(null);',
  22960. ' this.set($mod.i);',
  22961. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  22962. ' var $with1 = this.get();',
  22963. ' $mod.THelper.Fly.call(this, 123);',
  22964. ' $mod.THelper.Fly.call(this, 123);',
  22965. ' } finally {',
  22966. ' $ir.free();',
  22967. ' };',
  22968. ' };',
  22969. ' this.Run = function () {',
  22970. ' var l = null;',
  22971. ' try {',
  22972. ' $mod.THelper.Fly.call({',
  22973. ' get: function () {',
  22974. ' return l;',
  22975. ' },',
  22976. ' set: function (v) {',
  22977. ' l = rtl.setIntfL(l, v);',
  22978. ' }',
  22979. ' }, 123);',
  22980. ' $mod.THelper.Fly.call({',
  22981. ' get: function () {',
  22982. ' return l;',
  22983. ' },',
  22984. ' set: function (v) {',
  22985. ' l = rtl.setIntfL(l, v);',
  22986. ' }',
  22987. ' }, 123);',
  22988. ' } finally {',
  22989. ' rtl._Release(l);',
  22990. ' };',
  22991. ' };',
  22992. '});',
  22993. 'this.i = null;',
  22994. 'this.o = null;',
  22995. '']),
  22996. LinesToStr([ // $mod.$main
  22997. '$mod.THelper.Fly.call({',
  22998. ' p: $mod,',
  22999. ' get: function () {',
  23000. ' return this.p.i;',
  23001. ' },',
  23002. ' set: function (v) {',
  23003. ' rtl.setIntfP(this.p, "i", v);',
  23004. ' }',
  23005. '}, 123);',
  23006. '$mod.THelper.Fly.call({',
  23007. ' p: $mod,',
  23008. ' get: function () {',
  23009. ' return this.p.i;',
  23010. ' },',
  23011. ' set: function (v) {',
  23012. ' rtl.setIntfP(this.p, "i", v);',
  23013. ' }',
  23014. '}, 123);',
  23015. '$mod.THelper.Run();',
  23016. '$mod.THelper.Run();',
  23017. '$mod.THelper.Run();',
  23018. '$mod.THelper.Run();',
  23019. '']));
  23020. end;
  23021. procedure TTestModule.TestProcType;
  23022. begin
  23023. StartProgram(false);
  23024. Add([
  23025. 'type',
  23026. ' TProcInt = procedure(vI: longint = 1);',
  23027. 'procedure DoIt(vJ: longint);',
  23028. 'begin end;',
  23029. 'var',
  23030. ' b: boolean;',
  23031. ' vP, vQ: tprocint;',
  23032. 'begin',
  23033. ' vp:=nil;',
  23034. ' vp:=vp;',
  23035. ' vp:=@doit;',
  23036. ' vp;',
  23037. ' vp();',
  23038. ' vp(2);',
  23039. ' b:=vp=nil;',
  23040. ' b:=nil=vp;',
  23041. ' b:=vp=vq;',
  23042. ' b:=vp=@doit;',
  23043. ' b:=@doit=vp;',
  23044. ' b:=vp<>nil;',
  23045. ' b:=nil<>vp;',
  23046. ' b:=vp<>vq;',
  23047. ' b:=vp<>@doit;',
  23048. ' b:=@doit<>vp;',
  23049. ' b:=Assigned(vp);',
  23050. ' if Assigned(vp) then ;']);
  23051. ConvertProgram;
  23052. CheckSource('TestProcType',
  23053. LinesToStr([ // statements
  23054. 'this.DoIt = function(vJ) {',
  23055. '};',
  23056. 'this.b = false;',
  23057. 'this.vP = null;',
  23058. 'this.vQ = null;'
  23059. ]),
  23060. LinesToStr([ // $mod.$main
  23061. '$mod.vP = null;',
  23062. '$mod.vP = $mod.vP;',
  23063. '$mod.vP = $mod.DoIt;',
  23064. '$mod.vP(1);',
  23065. '$mod.vP(1);',
  23066. '$mod.vP(2);',
  23067. '$mod.b = $mod.vP === null;',
  23068. '$mod.b = null === $mod.vP;',
  23069. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23070. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23071. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23072. '$mod.b = $mod.vP !== null;',
  23073. '$mod.b = null !== $mod.vP;',
  23074. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23075. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23076. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23077. '$mod.b = $mod.vP != null;',
  23078. 'if ($mod.vP != null) ;',
  23079. '']));
  23080. end;
  23081. procedure TTestModule.TestProcType_Arg;
  23082. begin
  23083. StartProgram(false);
  23084. Add([
  23085. 'type',
  23086. ' TProcInt = procedure(vI: longint = 1);',
  23087. 'procedure DoIt(vJ: longint); begin end;',
  23088. 'procedure DoSome(vP, vQ: TProcInt);',
  23089. 'var',
  23090. ' b: boolean;',
  23091. 'begin',
  23092. ' vp:=nil;',
  23093. ' vp:=vp;',
  23094. ' vp:=@doit;',
  23095. ' vp;',
  23096. ' vp();',
  23097. ' vp(2);',
  23098. ' b:=vp=nil;',
  23099. ' b:=nil=vp;',
  23100. ' b:=vp=vq;',
  23101. ' b:=vp=@doit;',
  23102. ' b:=@doit=vp;',
  23103. ' b:=vp<>nil;',
  23104. ' b:=nil<>vp;',
  23105. ' b:=vp<>vq;',
  23106. ' b:=vp<>@doit;',
  23107. ' b:=@doit<>vp;',
  23108. ' b:=Assigned(vp);',
  23109. ' if Assigned(vp) then ;',
  23110. 'end;',
  23111. 'begin',
  23112. ' DoSome(@DoIt,nil);']);
  23113. ConvertProgram;
  23114. CheckSource('TestProcType_Arg',
  23115. LinesToStr([ // statements
  23116. 'this.DoIt = function(vJ) {',
  23117. '};',
  23118. 'this.DoSome = function(vP, vQ) {',
  23119. ' var b = false;',
  23120. ' vP = null;',
  23121. ' vP = vP;',
  23122. ' vP = $mod.DoIt;',
  23123. ' vP(1);',
  23124. ' vP(1);',
  23125. ' vP(2);',
  23126. ' b = vP === null;',
  23127. ' b = null === vP;',
  23128. ' b = rtl.eqCallback(vP,vQ);',
  23129. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  23130. ' b = rtl.eqCallback($mod.DoIt, vP);',
  23131. ' b = vP !== null;',
  23132. ' b = null !== vP;',
  23133. ' b = !rtl.eqCallback(vP, vQ);',
  23134. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  23135. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  23136. ' b = vP != null;',
  23137. ' if (vP != null) ;',
  23138. '};',
  23139. '']),
  23140. LinesToStr([ // $mod.$main
  23141. '$mod.DoSome($mod.DoIt,null);',
  23142. '']));
  23143. end;
  23144. procedure TTestModule.TestProcType_FunctionFPC;
  23145. begin
  23146. StartProgram(false);
  23147. Add('type');
  23148. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23149. Add('function DoIt(vI: longint): longint;');
  23150. Add('begin end;');
  23151. Add('var');
  23152. Add(' b: boolean;');
  23153. Add(' vP, vQ: tfuncint;');
  23154. Add('begin');
  23155. Add(' vp:=nil;');
  23156. Add(' vp:=vp;');
  23157. Add(' vp:=@doit;'); // ok in fpc and delphi
  23158. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23159. Add(' vp;'); // ok in fpc and delphi
  23160. Add(' vp();');
  23161. Add(' vp(2);');
  23162. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23163. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23164. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23165. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23166. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23167. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23168. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23169. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23170. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23171. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23172. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23173. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23174. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23175. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23176. Add(' b:=Assigned(vp);');
  23177. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23178. Add(' doit(vp());'); // ok in fpc and delphi
  23179. Add(' doit(vp(2));'); // ok in fpc and delphi
  23180. ConvertProgram;
  23181. CheckSource('TestProcType_FunctionFPC',
  23182. LinesToStr([ // statements
  23183. 'this.DoIt = function(vI) {',
  23184. ' var Result = 0;',
  23185. ' return Result;',
  23186. '};',
  23187. 'this.b = false;',
  23188. 'this.vP = null;',
  23189. 'this.vQ = null;'
  23190. ]),
  23191. LinesToStr([ // $mod.$main
  23192. '$mod.vP = null;',
  23193. '$mod.vP = $mod.vP;',
  23194. '$mod.vP = $mod.DoIt;',
  23195. '$mod.vP(1);',
  23196. '$mod.vP(1);',
  23197. '$mod.vP(2);',
  23198. '$mod.b = $mod.vP === null;',
  23199. '$mod.b = null === $mod.vP;',
  23200. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23201. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23202. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23203. '$mod.b = 4 === $mod.vP(1);',
  23204. '$mod.b = $mod.vP !== null;',
  23205. '$mod.b = null !== $mod.vP;',
  23206. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23207. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23208. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23209. '$mod.b = 6 !== $mod.vP(1);',
  23210. '$mod.b = $mod.vP != null;',
  23211. '$mod.DoIt($mod.vP(1));',
  23212. '$mod.DoIt($mod.vP(2));',
  23213. '']));
  23214. end;
  23215. procedure TTestModule.TestProcType_FunctionDelphi;
  23216. begin
  23217. StartProgram(false);
  23218. Add('{$mode Delphi}');
  23219. Add('type');
  23220. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23221. Add('function DoIt(vI: longint): longint;');
  23222. Add('begin end;');
  23223. Add('var');
  23224. Add(' b: boolean;');
  23225. Add(' vP, vQ: tfuncint;');
  23226. Add('begin');
  23227. Add(' vp:=nil;');
  23228. Add(' vp:=vp;');
  23229. Add(' vp:=@doit;'); // ok in fpc and delphi
  23230. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23231. Add(' vp;'); // ok in fpc and delphi
  23232. Add(' vp();');
  23233. Add(' vp(2);');
  23234. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23235. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23236. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23237. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23238. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23239. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23240. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23241. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23242. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23243. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23244. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23245. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23246. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23247. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23248. Add(' b:=Assigned(vp);');
  23249. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23250. Add(' doit(vp());'); // ok in fpc and delphi
  23251. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  23252. ConvertProgram;
  23253. CheckSource('TestProcType_FunctionDelphi',
  23254. LinesToStr([ // statements
  23255. 'this.DoIt = function(vI) {',
  23256. ' var Result = 0;',
  23257. ' return Result;',
  23258. '};',
  23259. 'this.b = false;',
  23260. 'this.vP = null;',
  23261. 'this.vQ = null;'
  23262. ]),
  23263. LinesToStr([ // $mod.$main
  23264. '$mod.vP = null;',
  23265. '$mod.vP = $mod.vP;',
  23266. '$mod.vP = $mod.DoIt;',
  23267. '$mod.vP = $mod.DoIt;',
  23268. '$mod.vP(1);',
  23269. '$mod.vP(1);',
  23270. '$mod.vP(2);',
  23271. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  23272. '$mod.b = $mod.vP(1) === 3;',
  23273. '$mod.b = 4 === $mod.vP(1);',
  23274. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  23275. '$mod.b = $mod.vP(1) !== 5;',
  23276. '$mod.b = 6 !== $mod.vP(1);',
  23277. '$mod.b = $mod.vP != null;',
  23278. '$mod.DoIt($mod.vP(1));',
  23279. '$mod.DoIt($mod.vP(1));',
  23280. '$mod.DoIt($mod.vP(2));',
  23281. '']));
  23282. end;
  23283. procedure TTestModule.TestProcType_ProcedureDelphi;
  23284. begin
  23285. StartProgram(false);
  23286. Add('{$mode Delphi}');
  23287. Add('type');
  23288. Add(' TProc = procedure;');
  23289. Add('procedure DoIt;');
  23290. Add('begin end;');
  23291. Add('var');
  23292. Add(' b: boolean;');
  23293. Add(' vP, vQ: tproc;');
  23294. Add('begin');
  23295. Add(' vp:=nil;');
  23296. Add(' vp:=vp;');
  23297. Add(' vp:=vq;');
  23298. 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
  23299. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23300. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  23301. Add(' vp;'); // ok in fpc and delphi
  23302. Add(' vp();');
  23303. // equal
  23304. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23305. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  23306. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23307. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23308. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  23309. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23310. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23311. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  23312. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23313. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23314. // unequal
  23315. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23316. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  23317. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23318. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  23319. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23320. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  23321. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23322. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  23323. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23324. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  23325. Add(' b:=Assigned(vp);');
  23326. ConvertProgram;
  23327. CheckSource('TestProcType_ProcedureDelphi',
  23328. LinesToStr([ // statements
  23329. 'this.DoIt = function() {',
  23330. '};',
  23331. 'this.b = false;',
  23332. 'this.vP = null;',
  23333. 'this.vQ = null;'
  23334. ]),
  23335. LinesToStr([ // $mod.$main
  23336. '$mod.vP = null;',
  23337. '$mod.vP = $mod.vP;',
  23338. '$mod.vP = $mod.vQ;',
  23339. '$mod.vP = $mod.DoIt;',
  23340. '$mod.vP = $mod.DoIt;',
  23341. '$mod.vP();',
  23342. '$mod.vP();',
  23343. '$mod.b = $mod.vP === null;',
  23344. '$mod.b = null === $mod.vP;',
  23345. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  23346. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23347. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23348. '$mod.b = $mod.vP !== null;',
  23349. '$mod.b = null !== $mod.vP;',
  23350. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  23351. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23352. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23353. '$mod.b = $mod.vP != null;',
  23354. '']));
  23355. end;
  23356. procedure TTestModule.TestProcType_AsParam;
  23357. begin
  23358. StartProgram(false);
  23359. Add('type');
  23360. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23361. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  23362. Add('var vJ: tfuncint;');
  23363. Add('begin');
  23364. Add(' vg:=vg;');
  23365. Add(' vj:=vh;');
  23366. Add(' vi:=vi;');
  23367. Add(' doit(vg,vg,vg);');
  23368. Add(' doit(vh,vh,vj);');
  23369. Add(' doit(vi,vi,vi);');
  23370. Add(' doit(vj,vj,vj);');
  23371. Add('end;');
  23372. Add('var i: tfuncint;');
  23373. Add('begin');
  23374. Add(' doit(i,i,i);');
  23375. ConvertProgram;
  23376. CheckSource('TestProcType_AsParam',
  23377. LinesToStr([ // statements
  23378. 'this.DoIt = function (vG,vH,vI) {',
  23379. ' var vJ = null;',
  23380. ' vG = vG;',
  23381. ' vJ = vH;',
  23382. ' vI.set(vI.get());',
  23383. ' $mod.DoIt(vG, vG, {',
  23384. ' get: function () {',
  23385. ' return vG;',
  23386. ' },',
  23387. ' set: function (v) {',
  23388. ' vG = v;',
  23389. ' }',
  23390. ' });',
  23391. ' $mod.DoIt(vH, vH, {',
  23392. ' get: function () {',
  23393. ' return vJ;',
  23394. ' },',
  23395. ' set: function (v) {',
  23396. ' vJ = v;',
  23397. ' }',
  23398. ' });',
  23399. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  23400. ' $mod.DoIt(vJ, vJ, {',
  23401. ' get: function () {',
  23402. ' return vJ;',
  23403. ' },',
  23404. ' set: function (v) {',
  23405. ' vJ = v;',
  23406. ' }',
  23407. ' });',
  23408. '};',
  23409. 'this.i = null;'
  23410. ]),
  23411. LinesToStr([
  23412. '$mod.DoIt($mod.i,$mod.i,{',
  23413. ' p: $mod,',
  23414. ' get: function () {',
  23415. ' return this.p.i;',
  23416. ' },',
  23417. ' set: function (v) {',
  23418. ' this.p.i = v;',
  23419. ' }',
  23420. '});'
  23421. ]));
  23422. end;
  23423. procedure TTestModule.TestProcType_MethodFPC;
  23424. begin
  23425. StartProgram(false);
  23426. Add('type');
  23427. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23428. Add(' TObject = class');
  23429. Add(' function DoIt(vA: longint = 1): longint;');
  23430. Add(' end;');
  23431. Add('function TObject.DoIt(vA: longint = 1): longint;');
  23432. Add('begin');
  23433. Add('end;');
  23434. Add('var');
  23435. Add(' Obj: TObject;');
  23436. Add(' vP: tfuncint;');
  23437. Add(' b: boolean;');
  23438. Add('begin');
  23439. Add(' vp:[email protected];'); // ok in fpc and delphi
  23440. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  23441. Add(' vp;'); // ok in fpc and delphi
  23442. Add(' vp();');
  23443. Add(' vp(2);');
  23444. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  23445. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  23446. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  23447. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  23448. ConvertProgram;
  23449. CheckSource('TestProcType_MethodFPC',
  23450. LinesToStr([ // statements
  23451. 'rtl.createClass($mod, "TObject", null, function () {',
  23452. ' this.$init = function () {',
  23453. ' };',
  23454. ' this.$final = function () {',
  23455. ' };',
  23456. ' this.DoIt = function (vA) {',
  23457. ' var Result = 0;',
  23458. ' return Result;',
  23459. ' };',
  23460. '});',
  23461. 'this.Obj = null;',
  23462. 'this.vP = null;',
  23463. 'this.b = false;'
  23464. ]),
  23465. LinesToStr([
  23466. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23467. '$mod.vP(1);',
  23468. '$mod.vP(1);',
  23469. '$mod.vP(2);',
  23470. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  23471. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  23472. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  23473. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  23474. '']));
  23475. end;
  23476. procedure TTestModule.TestProcType_MethodDelphi;
  23477. begin
  23478. StartProgram(false);
  23479. Add('{$mode delphi}');
  23480. Add('type');
  23481. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23482. Add(' TObject = class');
  23483. Add(' function DoIt(vA: longint = 1): longint;');
  23484. Add(' end;');
  23485. Add('function TObject.DoIt(vA: longint = 1): longint;');
  23486. Add('begin');
  23487. Add('end;');
  23488. Add('var');
  23489. Add(' Obj: TObject;');
  23490. Add(' vP: tfuncint;');
  23491. Add(' b: boolean;');
  23492. Add('begin');
  23493. Add(' vp:[email protected];'); // ok in fpc and delphi
  23494. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  23495. Add(' vp;'); // ok in fpc and delphi
  23496. Add(' vp();');
  23497. Add(' vp(2);');
  23498. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  23499. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  23500. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  23501. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  23502. ConvertProgram;
  23503. CheckSource('TestProcType_MethodDelphi',
  23504. LinesToStr([ // statements
  23505. 'rtl.createClass($mod, "TObject", null, function () {',
  23506. ' this.$init = function () {',
  23507. ' };',
  23508. ' this.$final = function () {',
  23509. ' };',
  23510. ' this.DoIt = function (vA) {',
  23511. ' var Result = 0;',
  23512. ' return Result;',
  23513. ' };',
  23514. '});',
  23515. 'this.Obj = null;',
  23516. 'this.vP = null;',
  23517. 'this.b = false;'
  23518. ]),
  23519. LinesToStr([
  23520. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23521. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23522. '$mod.vP(1);',
  23523. '$mod.vP(1);',
  23524. '$mod.vP(2);',
  23525. '']));
  23526. end;
  23527. procedure TTestModule.TestProcType_PropertyFPC;
  23528. begin
  23529. StartProgram(false);
  23530. Add('type');
  23531. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23532. Add(' TObject = class');
  23533. Add(' FOnFoo: TFuncInt;');
  23534. Add(' function DoIt(vA: longint = 1): longint;');
  23535. Add(' function GetFoo: TFuncInt;');
  23536. Add(' procedure SetFoo(const Value: TFuncInt);');
  23537. Add(' function GetEvents(Index: longint): TFuncInt;');
  23538. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  23539. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23540. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23541. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  23542. Add(' end;');
  23543. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23544. Add('function tobject.getfoo: tfuncint; begin end;');
  23545. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23546. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  23547. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  23548. Add('var');
  23549. Add(' Obj: TObject;');
  23550. Add(' vP: tfuncint;');
  23551. Add(' b: boolean;');
  23552. Add('begin');
  23553. Add(' obj.onfoo:=nil;');
  23554. Add(' obj.onbar:=nil;');
  23555. Add(' obj.events[1]:=nil;');
  23556. Add(' obj.onfoo:=obj.onfoo;');
  23557. Add(' obj.onbar:=obj.onbar;');
  23558. Add(' obj.events[2]:=obj.events[3];');
  23559. Add(' obj.onfoo:[email protected];');
  23560. Add(' obj.onbar:[email protected];');
  23561. Add(' obj.events[4]:[email protected];');
  23562. //Add(' obj.onfoo:=obj.doit;'); // delphi
  23563. //Add(' obj.onbar:=obj.doit;'); // delphi
  23564. //Add(' obj.events[4]:=obj.doit;'); // delphi
  23565. Add(' obj.onfoo;');
  23566. Add(' obj.onbar;');
  23567. //Add(' obj.events[5];'); ToDo in pasresolver
  23568. Add(' obj.onfoo();');
  23569. Add(' obj.onbar();');
  23570. Add(' obj.events[6]();');
  23571. Add(' b:=obj.onfoo=nil;');
  23572. Add(' b:=obj.onbar=nil;');
  23573. Add(' b:=obj.events[7]=nil;');
  23574. Add(' b:=obj.onfoo<>nil;');
  23575. Add(' b:=obj.onbar<>nil;');
  23576. Add(' b:=obj.events[8]<>nil;');
  23577. Add(' b:=obj.onfoo=vp;');
  23578. Add(' b:=obj.onbar=vp;');
  23579. Add(' b:=obj.events[9]=vp;');
  23580. Add(' b:=obj.onfoo=obj.onfoo;');
  23581. Add(' b:=obj.onbar=obj.onfoo;');
  23582. Add(' b:=obj.events[10]=obj.onfoo;');
  23583. Add(' b:=obj.onfoo<>obj.onfoo;');
  23584. Add(' b:=obj.onbar<>obj.onfoo;');
  23585. Add(' b:=obj.events[11]<>obj.onfoo;');
  23586. Add(' b:[email protected];');
  23587. Add(' b:[email protected];');
  23588. Add(' b:=obj.events[12][email protected];');
  23589. Add(' b:=obj.onfoo<>@obj.doit;');
  23590. Add(' b:=obj.onbar<>@obj.doit;');
  23591. Add(' b:=obj.events[12]<>@obj.doit;');
  23592. Add(' b:=Assigned(obj.onfoo);');
  23593. Add(' b:=Assigned(obj.onbar);');
  23594. Add(' b:=Assigned(obj.events[13]);');
  23595. ConvertProgram;
  23596. CheckSource('TestProcType_PropertyFPC',
  23597. LinesToStr([ // statements
  23598. 'rtl.createClass($mod, "TObject", null, function () {',
  23599. ' this.$init = function () {',
  23600. ' this.FOnFoo = null;',
  23601. ' };',
  23602. ' this.$final = function () {',
  23603. ' this.FOnFoo = undefined;',
  23604. ' };',
  23605. ' this.DoIt = function (vA) {',
  23606. ' var Result = 0;',
  23607. ' return Result;',
  23608. ' };',
  23609. 'this.GetFoo = function () {',
  23610. ' var Result = null;',
  23611. ' return Result;',
  23612. '};',
  23613. 'this.SetFoo = function (Value) {',
  23614. '};',
  23615. 'this.GetEvents = function (Index) {',
  23616. ' var Result = null;',
  23617. ' return Result;',
  23618. '};',
  23619. 'this.SetEvents = function (Index, Value) {',
  23620. '};',
  23621. '});',
  23622. 'this.Obj = null;',
  23623. 'this.vP = null;',
  23624. 'this.b = false;'
  23625. ]),
  23626. LinesToStr([
  23627. '$mod.Obj.FOnFoo = null;',
  23628. '$mod.Obj.SetFoo(null);',
  23629. '$mod.Obj.SetEvents(1, null);',
  23630. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  23631. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  23632. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  23633. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23634. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23635. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23636. '$mod.Obj.FOnFoo(1);',
  23637. '$mod.Obj.GetFoo();',
  23638. '$mod.Obj.FOnFoo(1);',
  23639. '$mod.Obj.GetFoo()(1);',
  23640. '$mod.Obj.GetEvents(6)(1);',
  23641. '$mod.b = $mod.Obj.FOnFoo === null;',
  23642. '$mod.b = $mod.Obj.GetFoo() === null;',
  23643. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  23644. '$mod.b = $mod.Obj.FOnFoo !== null;',
  23645. '$mod.b = $mod.Obj.GetFoo() !== null;',
  23646. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  23647. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  23648. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  23649. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  23650. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  23651. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  23652. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  23653. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  23654. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  23655. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  23656. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  23657. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  23658. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  23659. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  23660. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  23661. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  23662. '$mod.b = $mod.Obj.FOnFoo != null;',
  23663. '$mod.b = $mod.Obj.GetFoo() != null;',
  23664. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  23665. '']));
  23666. end;
  23667. procedure TTestModule.TestProcType_PropertyDelphi;
  23668. begin
  23669. StartProgram(false);
  23670. Add('{$mode delphi}');
  23671. Add('type');
  23672. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23673. Add(' TObject = class');
  23674. Add(' FOnFoo: TFuncInt;');
  23675. Add(' function DoIt(vA: longint = 1): longint;');
  23676. Add(' function GetFoo: TFuncInt;');
  23677. Add(' procedure SetFoo(const Value: TFuncInt);');
  23678. Add(' function GetEvents(Index: longint): TFuncInt;');
  23679. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  23680. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23681. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23682. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  23683. Add(' end;');
  23684. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23685. Add('function tobject.getfoo: tfuncint; begin end;');
  23686. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23687. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  23688. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  23689. Add('var');
  23690. Add(' Obj: TObject;');
  23691. Add(' vP: tfuncint;');
  23692. Add(' b: boolean;');
  23693. Add('begin');
  23694. Add(' obj.onfoo:=nil;');
  23695. Add(' obj.onbar:=nil;');
  23696. Add(' obj.events[1]:=nil;');
  23697. Add(' obj.onfoo:=obj.onfoo;');
  23698. Add(' obj.onbar:=obj.onbar;');
  23699. Add(' obj.events[2]:=obj.events[3];');
  23700. Add(' obj.onfoo:[email protected];');
  23701. Add(' obj.onbar:[email protected];');
  23702. Add(' obj.events[4]:[email protected];');
  23703. Add(' obj.onfoo:=obj.doit;'); // delphi
  23704. Add(' obj.onbar:=obj.doit;'); // delphi
  23705. Add(' obj.events[4]:=obj.doit;'); // delphi
  23706. Add(' obj.onfoo;');
  23707. Add(' obj.onbar;');
  23708. //Add(' obj.events[5];'); ToDo in pasresolver
  23709. Add(' obj.onfoo();');
  23710. Add(' obj.onbar();');
  23711. Add(' obj.events[6]();');
  23712. //Add(' b:=obj.onfoo=nil;'); // fpc
  23713. //Add(' b:=obj.onbar=nil;'); // fpc
  23714. //Add(' b:=obj.events[7]=nil;'); // fpc
  23715. //Add(' b:=obj.onfoo<>nil;'); // fpc
  23716. //Add(' b:=obj.onbar<>nil;'); // fpc
  23717. //Add(' b:=obj.events[8]<>nil;'); // fpc
  23718. Add(' b:=obj.onfoo=vp;');
  23719. Add(' b:=obj.onbar=vp;');
  23720. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  23721. Add(' b:=obj.onfoo=obj.onfoo;');
  23722. Add(' b:=obj.onbar=obj.onfoo;');
  23723. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  23724. Add(' b:=obj.onfoo<>obj.onfoo;');
  23725. Add(' b:=obj.onbar<>obj.onfoo;');
  23726. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  23727. //Add(' b:[email protected];'); // fpc
  23728. //Add(' b:[email protected];'); // fpc
  23729. //Add(' b:=obj.events[12][email protected];'); // fpc
  23730. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  23731. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  23732. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  23733. Add(' b:=Assigned(obj.onfoo);');
  23734. Add(' b:=Assigned(obj.onbar);');
  23735. Add(' b:=Assigned(obj.events[13]);');
  23736. ConvertProgram;
  23737. CheckSource('TestProcType_PropertyDelphi',
  23738. LinesToStr([ // statements
  23739. 'rtl.createClass($mod, "TObject", null, function () {',
  23740. ' this.$init = function () {',
  23741. ' this.FOnFoo = null;',
  23742. ' };',
  23743. ' this.$final = function () {',
  23744. ' this.FOnFoo = undefined;',
  23745. ' };',
  23746. ' this.DoIt = function (vA) {',
  23747. ' var Result = 0;',
  23748. ' return Result;',
  23749. ' };',
  23750. 'this.GetFoo = function () {',
  23751. ' var Result = null;',
  23752. ' return Result;',
  23753. '};',
  23754. 'this.SetFoo = function (Value) {',
  23755. '};',
  23756. 'this.GetEvents = function (Index) {',
  23757. ' var Result = null;',
  23758. ' return Result;',
  23759. '};',
  23760. 'this.SetEvents = function (Index, Value) {',
  23761. '};',
  23762. '});',
  23763. 'this.Obj = null;',
  23764. 'this.vP = null;',
  23765. 'this.b = false;'
  23766. ]),
  23767. LinesToStr([
  23768. '$mod.Obj.FOnFoo = null;',
  23769. '$mod.Obj.SetFoo(null);',
  23770. '$mod.Obj.SetEvents(1, null);',
  23771. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  23772. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  23773. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  23774. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23775. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23776. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23777. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23778. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23779. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23780. '$mod.Obj.FOnFoo(1);',
  23781. '$mod.Obj.GetFoo();',
  23782. '$mod.Obj.FOnFoo(1);',
  23783. '$mod.Obj.GetFoo()(1);',
  23784. '$mod.Obj.GetEvents(6)(1);',
  23785. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  23786. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  23787. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  23788. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  23789. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  23790. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  23791. '$mod.b = $mod.Obj.FOnFoo != null;',
  23792. '$mod.b = $mod.Obj.GetFoo() != null;',
  23793. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  23794. '']));
  23795. end;
  23796. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  23797. begin
  23798. StartProgram(false);
  23799. Add('type');
  23800. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23801. Add(' TObject = class');
  23802. Add(' FOnFoo: TFuncInt;');
  23803. Add(' function DoIt(vA: longint = 1): longint;');
  23804. Add(' function GetFoo: TFuncInt;');
  23805. Add(' procedure SetFoo(const Value: TFuncInt);');
  23806. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23807. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23808. Add(' end;');
  23809. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23810. Add('function tobject.getfoo: tfuncint; begin end;');
  23811. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23812. Add('var');
  23813. Add(' Obj: TObject;');
  23814. Add(' vP: tfuncint;');
  23815. Add(' b: boolean;');
  23816. Add('begin');
  23817. Add('with obj do begin');
  23818. Add(' fonfoo:=nil;');
  23819. Add(' onfoo:=nil;');
  23820. Add(' onbar:=nil;');
  23821. Add(' fonfoo:=fonfoo;');
  23822. Add(' onfoo:=onfoo;');
  23823. Add(' onbar:=onbar;');
  23824. Add(' fonfoo:=@doit;');
  23825. Add(' onfoo:=@doit;');
  23826. Add(' onbar:=@doit;');
  23827. //Add(' fonfoo:=doit;'); // delphi
  23828. //Add(' onfoo:=doit;'); // delphi
  23829. //Add(' onbar:=doit;'); // delphi
  23830. Add(' fonfoo;');
  23831. Add(' onfoo;');
  23832. Add(' onbar;');
  23833. Add(' fonfoo();');
  23834. Add(' onfoo();');
  23835. Add(' onbar();');
  23836. Add(' b:=fonfoo=nil;');
  23837. Add(' b:=onfoo=nil;');
  23838. Add(' b:=onbar=nil;');
  23839. Add(' b:=fonfoo<>nil;');
  23840. Add(' b:=onfoo<>nil;');
  23841. Add(' b:=onbar<>nil;');
  23842. Add(' b:=fonfoo=vp;');
  23843. Add(' b:=onfoo=vp;');
  23844. Add(' b:=onbar=vp;');
  23845. Add(' b:=fonfoo=fonfoo;');
  23846. Add(' b:=onfoo=onfoo;');
  23847. Add(' b:=onbar=onfoo;');
  23848. Add(' b:=fonfoo<>fonfoo;');
  23849. Add(' b:=onfoo<>onfoo;');
  23850. Add(' b:=onbar<>onfoo;');
  23851. Add(' b:=fonfoo=@doit;');
  23852. Add(' b:=onfoo=@doit;');
  23853. Add(' b:=onbar=@doit;');
  23854. Add(' b:=fonfoo<>@doit;');
  23855. Add(' b:=onfoo<>@doit;');
  23856. Add(' b:=onbar<>@doit;');
  23857. Add(' b:=Assigned(fonfoo);');
  23858. Add(' b:=Assigned(onfoo);');
  23859. Add(' b:=Assigned(onbar);');
  23860. Add('end;');
  23861. ConvertProgram;
  23862. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  23863. LinesToStr([ // statements
  23864. 'rtl.createClass($mod, "TObject", null, function () {',
  23865. ' this.$init = function () {',
  23866. ' this.FOnFoo = null;',
  23867. ' };',
  23868. ' this.$final = function () {',
  23869. ' this.FOnFoo = undefined;',
  23870. ' };',
  23871. ' this.DoIt = function (vA) {',
  23872. ' var Result = 0;',
  23873. ' return Result;',
  23874. ' };',
  23875. ' this.GetFoo = function () {',
  23876. ' var Result = null;',
  23877. ' return Result;',
  23878. ' };',
  23879. ' this.SetFoo = function (Value) {',
  23880. ' };',
  23881. '});',
  23882. 'this.Obj = null;',
  23883. 'this.vP = null;',
  23884. 'this.b = false;'
  23885. ]),
  23886. LinesToStr([
  23887. 'var $with1 = $mod.Obj;',
  23888. '$with1.FOnFoo = null;',
  23889. '$with1.FOnFoo = null;',
  23890. '$with1.SetFoo(null);',
  23891. '$with1.FOnFoo = $with1.FOnFoo;',
  23892. '$with1.FOnFoo = $with1.FOnFoo;',
  23893. '$with1.SetFoo($with1.GetFoo());',
  23894. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  23895. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  23896. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  23897. '$with1.FOnFoo(1);',
  23898. '$with1.FOnFoo(1);',
  23899. '$with1.GetFoo();',
  23900. '$with1.FOnFoo(1);',
  23901. '$with1.FOnFoo(1);',
  23902. '$with1.GetFoo()(1);',
  23903. '$mod.b = $with1.FOnFoo === null;',
  23904. '$mod.b = $with1.FOnFoo === null;',
  23905. '$mod.b = $with1.GetFoo() === null;',
  23906. '$mod.b = $with1.FOnFoo !== null;',
  23907. '$mod.b = $with1.FOnFoo !== null;',
  23908. '$mod.b = $with1.GetFoo() !== null;',
  23909. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  23910. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  23911. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  23912. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23913. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23914. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  23915. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23916. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23917. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  23918. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23919. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23920. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  23921. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23922. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23923. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  23924. '$mod.b = $with1.FOnFoo != null;',
  23925. '$mod.b = $with1.FOnFoo != null;',
  23926. '$mod.b = $with1.GetFoo() != null;',
  23927. '']));
  23928. end;
  23929. procedure TTestModule.TestProcType_Nested;
  23930. begin
  23931. StartProgram(false);
  23932. Add([
  23933. 'type',
  23934. ' TProcInt = procedure(vI: longint = 1);',
  23935. 'procedure DoIt(vJ: longint);',
  23936. 'var aProc: TProcInt;',
  23937. ' b: boolean;',
  23938. ' procedure Sub(vK: longint);',
  23939. ' var aSub: TProcInt;',
  23940. ' procedure SubSub(vK: longint);',
  23941. ' var aSubSub: TProcInt;',
  23942. ' begin;',
  23943. ' aProc:=@DoIt;',
  23944. ' aSub:=@DoIt;',
  23945. ' aSubSub:=@DoIt;',
  23946. ' aProc:=@Sub;',
  23947. ' aSub:=@Sub;',
  23948. ' aSubSub:=@Sub;',
  23949. ' aProc:=@SubSub;',
  23950. ' aSub:=@SubSub;',
  23951. ' aSubSub:=@SubSub;',
  23952. ' end;',
  23953. ' begin;',
  23954. ' end;',
  23955. 'begin;',
  23956. ' aProc:=@Sub;',
  23957. ' b:=aProc=@Sub;',
  23958. ' b:=@Sub=aProc;',
  23959. 'end;',
  23960. 'begin',
  23961. '']);
  23962. ConvertProgram;
  23963. CheckSource('TestProcType_Nested',
  23964. LinesToStr([ // statements
  23965. 'this.DoIt = function (vJ) {',
  23966. ' var aProc = null;',
  23967. ' var b = false;',
  23968. ' function Sub(vK) {',
  23969. ' var aSub = null;',
  23970. ' function SubSub(vK) {',
  23971. ' var aSubSub = null;',
  23972. ' aProc = $mod.DoIt;',
  23973. ' aSub = $mod.DoIt;',
  23974. ' aSubSub = $mod.DoIt;',
  23975. ' aProc = Sub;',
  23976. ' aSub = Sub;',
  23977. ' aSubSub = Sub;',
  23978. ' aProc = SubSub;',
  23979. ' aSub = SubSub;',
  23980. ' aSubSub = SubSub;',
  23981. ' };',
  23982. ' };',
  23983. ' aProc = Sub;',
  23984. ' b = rtl.eqCallback(aProc, Sub);',
  23985. ' b = rtl.eqCallback(Sub, aProc);',
  23986. '};',
  23987. '']),
  23988. LinesToStr([ // $mod.$main
  23989. '']));
  23990. end;
  23991. procedure TTestModule.TestProcType_NestedOfObject;
  23992. begin
  23993. StartProgram(false);
  23994. Add([
  23995. 'type',
  23996. ' TProcInt = procedure(vI: longint = 1) of object;',
  23997. ' TObject = class',
  23998. ' procedure DoIt(vJ: longint);',
  23999. ' end;',
  24000. 'procedure TObject.DoIt(vJ: longint);',
  24001. 'var aProc: TProcInt;',
  24002. ' b: boolean;',
  24003. ' procedure Sub(vK: longint);',
  24004. ' var aSub: TProcInt;',
  24005. ' procedure SubSub(vK: longint);',
  24006. ' var aSubSub: TProcInt;',
  24007. ' begin;',
  24008. ' aProc:=@DoIt;',
  24009. ' aSub:=@DoIt;',
  24010. ' aSubSub:=@DoIt;',
  24011. ' aProc:=@Sub;',
  24012. ' aSub:=@Sub;',
  24013. ' aSubSub:=@Sub;',
  24014. ' aProc:=@SubSub;',
  24015. ' aSub:=@SubSub;',
  24016. ' aSubSub:=@SubSub;',
  24017. ' end;',
  24018. ' begin;',
  24019. ' end;',
  24020. 'begin;',
  24021. ' aProc:=@Sub;',
  24022. ' b:=aProc=@Sub;',
  24023. ' b:=@Sub=aProc;',
  24024. 'end;',
  24025. 'begin',
  24026. '']);
  24027. ConvertProgram;
  24028. CheckSource('TestProcType_Nested',
  24029. LinesToStr([ // statements
  24030. 'rtl.createClass($mod, "TObject", null, function () {',
  24031. ' this.$init = function () {',
  24032. ' };',
  24033. ' this.$final = function () {',
  24034. ' };',
  24035. ' this.DoIt = function (vJ) {',
  24036. ' var $Self = this;',
  24037. ' var aProc = null;',
  24038. ' var b = false;',
  24039. ' function Sub(vK) {',
  24040. ' var aSub = null;',
  24041. ' function SubSub(vK) {',
  24042. ' var aSubSub = null;',
  24043. ' aProc = rtl.createCallback($Self, "DoIt");',
  24044. ' aSub = rtl.createCallback($Self, "DoIt");',
  24045. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  24046. ' aProc = Sub;',
  24047. ' aSub = Sub;',
  24048. ' aSubSub = Sub;',
  24049. ' aProc = SubSub;',
  24050. ' aSub = SubSub;',
  24051. ' aSubSub = SubSub;',
  24052. ' };',
  24053. ' };',
  24054. ' aProc = Sub;',
  24055. ' b = rtl.eqCallback(aProc, Sub);',
  24056. ' b = rtl.eqCallback(Sub, aProc);',
  24057. ' };',
  24058. '});',
  24059. '']),
  24060. LinesToStr([ // $mod.$main
  24061. '']));
  24062. end;
  24063. procedure TTestModule.TestProcType_ReferenceToProc;
  24064. begin
  24065. StartProgram(false);
  24066. Add([
  24067. 'type',
  24068. ' TProcRef = reference to procedure(i: longint = 0);',
  24069. ' TFuncRef = reference to function(i: longint = 0): longint;',
  24070. 'var',
  24071. ' p: TProcRef;',
  24072. ' f: TFuncRef;',
  24073. 'procedure DoIt(i: longint);',
  24074. 'begin',
  24075. 'end;',
  24076. 'function GetIt(i: longint): longint;',
  24077. 'begin',
  24078. ' p:=@DoIt;',
  24079. ' f:=@GetIt;',
  24080. ' f;',
  24081. ' f();',
  24082. ' f(1);',
  24083. 'end;',
  24084. 'begin',
  24085. ' p:=@DoIt;',
  24086. ' f:=@GetIt;',
  24087. ' f;',
  24088. ' f();',
  24089. ' f(1);',
  24090. ' p:=TProcRef(f);',
  24091. '']);
  24092. ConvertProgram;
  24093. CheckSource('TestProcType_ReferenceToProc',
  24094. LinesToStr([ // statements
  24095. 'this.p = null;',
  24096. 'this.f = null;',
  24097. 'this.DoIt = function (i) {',
  24098. '};',
  24099. 'this.GetIt = function (i) {',
  24100. ' var Result = 0;',
  24101. ' $mod.p = $mod.DoIt;',
  24102. ' $mod.f = $mod.GetIt;',
  24103. ' $mod.f(0);',
  24104. ' $mod.f(0);',
  24105. ' $mod.f(1);',
  24106. ' return Result;',
  24107. '};',
  24108. '']),
  24109. LinesToStr([ // $mod.$main
  24110. '$mod.p = $mod.DoIt;',
  24111. '$mod.f = $mod.GetIt;',
  24112. '$mod.f(0);',
  24113. '$mod.f(0);',
  24114. '$mod.f(1);',
  24115. '$mod.p = $mod.f;',
  24116. '']));
  24117. end;
  24118. procedure TTestModule.TestProcType_ReferenceToMethod;
  24119. begin
  24120. StartProgram(false);
  24121. Add([
  24122. 'type',
  24123. ' TFuncRef = reference to function(i: longint = 5): longint;',
  24124. ' TObject = class',
  24125. ' function Grow(s: longint): longint;',
  24126. ' end;',
  24127. 'var',
  24128. ' f: tfuncref;',
  24129. 'function tobject.grow(s: longint): longint;',
  24130. ' function GrowSub(i: longint): longint;',
  24131. ' begin',
  24132. ' f:=@grow;',
  24133. ' f:=@growsub;',
  24134. ' end;',
  24135. 'begin',
  24136. ' f:=@grow;',
  24137. ' f:=@growsub;',
  24138. 'end;',
  24139. 'begin',
  24140. '']);
  24141. ConvertProgram;
  24142. CheckSource('TestProcType_ReferenceToMethod',
  24143. LinesToStr([ // statements
  24144. 'rtl.createClass($mod, "TObject", null, function () {',
  24145. ' this.$init = function () {',
  24146. ' };',
  24147. ' this.$final = function () {',
  24148. ' };',
  24149. ' this.Grow = function (s) {',
  24150. ' var $Self = this;',
  24151. ' var Result = 0;',
  24152. ' function GrowSub(i) {',
  24153. ' var Result = 0;',
  24154. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24155. ' $mod.f = GrowSub;',
  24156. ' return Result;',
  24157. ' };',
  24158. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24159. ' $mod.f = GrowSub;',
  24160. ' return Result;',
  24161. ' };',
  24162. '});',
  24163. 'this.f = null;',
  24164. '']),
  24165. LinesToStr([ // $mod.$main
  24166. '']));
  24167. end;
  24168. procedure TTestModule.TestProcType_Typecast;
  24169. begin
  24170. StartProgram(false);
  24171. Add([
  24172. 'type',
  24173. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  24174. ' TEvent = procedure of object;',
  24175. ' TGetter = function:longint of object;',
  24176. ' TProcA = procedure(i: longint);',
  24177. ' TFuncB = function(i, j: longint): longint;',
  24178. 'procedure DoIt(); varargs; begin end;',
  24179. 'var',
  24180. ' Notify: tnotifyevent;',
  24181. ' Event: tevent;',
  24182. ' Getter: tgetter;',
  24183. ' ProcA: tproca;',
  24184. ' FuncB: tfuncb;',
  24185. ' p: pointer;',
  24186. 'begin',
  24187. ' notify:=tnotifyevent(event);',
  24188. ' event:=tevent(event);',
  24189. ' event:=tevent(notify);',
  24190. ' event:=tevent(getter);',
  24191. ' event:=tevent(proca);',
  24192. ' proca:=tproca(funcb);',
  24193. ' funcb:=tfuncb(funcb);',
  24194. ' funcb:=tfuncb(proca);',
  24195. ' funcb:=tfuncb(getter);',
  24196. ' proca:=tproca(p);',
  24197. ' funcb:=tfuncb(p);',
  24198. ' getter:=tgetter(p);',
  24199. ' p:=pointer(notify);',
  24200. ' p:=notify;',
  24201. ' p:=pointer(proca);',
  24202. ' p:=proca;',
  24203. ' p:=pointer(funcb);',
  24204. ' p:=funcb;',
  24205. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  24206. '']);
  24207. ConvertProgram;
  24208. CheckSource('TestProcType_Typecast',
  24209. LinesToStr([ // statements
  24210. 'this.DoIt = function () {',
  24211. '};',
  24212. 'this.Notify = null;',
  24213. 'this.Event = null;',
  24214. 'this.Getter = null;',
  24215. 'this.ProcA = null;',
  24216. 'this.FuncB = null;',
  24217. 'this.p = null;',
  24218. '']),
  24219. LinesToStr([ // $mod.$main
  24220. '$mod.Notify = $mod.Event;',
  24221. '$mod.Event = $mod.Event;',
  24222. '$mod.Event = $mod.Notify;',
  24223. '$mod.Event = $mod.Getter;',
  24224. '$mod.Event = $mod.ProcA;',
  24225. '$mod.ProcA = $mod.FuncB;',
  24226. '$mod.FuncB = $mod.FuncB;',
  24227. '$mod.FuncB = $mod.ProcA;',
  24228. '$mod.FuncB = $mod.Getter;',
  24229. '$mod.ProcA = $mod.p;',
  24230. '$mod.FuncB = $mod.p;',
  24231. '$mod.Getter = $mod.p;',
  24232. '$mod.p = $mod.Notify;',
  24233. '$mod.p = $mod.Notify;',
  24234. '$mod.p = $mod.ProcA;',
  24235. '$mod.p = $mod.ProcA;',
  24236. '$mod.p = $mod.FuncB;',
  24237. '$mod.p = $mod.FuncB;',
  24238. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  24239. '']));
  24240. end;
  24241. procedure TTestModule.TestProcType_PassProcToUntyped;
  24242. begin
  24243. StartProgram(false);
  24244. Add([
  24245. 'type',
  24246. ' TEvent = procedure of object;',
  24247. ' TFunc = function: longint;',
  24248. 'procedure DoIt(); varargs; begin end;',
  24249. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  24250. 'var',
  24251. ' Event: tevent;',
  24252. ' Func: TFunc;',
  24253. 'begin',
  24254. ' doit(event,func);',
  24255. ' dosome(event,event,event);',
  24256. ' dosome(func,func,func);',
  24257. '']);
  24258. ConvertProgram;
  24259. CheckSource('TestProcType_PassProcToUntyped',
  24260. LinesToStr([ // statements
  24261. 'this.DoIt = function () {',
  24262. '};',
  24263. 'this.DoSome = function (a, b, p) {',
  24264. '};',
  24265. 'this.Event = null;',
  24266. 'this.Func = null;',
  24267. '']),
  24268. LinesToStr([ // $mod.$main
  24269. '$mod.DoIt($mod.Event, $mod.Func);',
  24270. '$mod.DoSome($mod.Event, {',
  24271. ' p: $mod,',
  24272. ' get: function () {',
  24273. ' return this.p.Event;',
  24274. ' },',
  24275. ' set: function (v) {',
  24276. ' this.p.Event = v;',
  24277. ' }',
  24278. '}, $mod.Event);',
  24279. '$mod.DoSome($mod.Func, {',
  24280. ' p: $mod,',
  24281. ' get: function () {',
  24282. ' return this.p.Func;',
  24283. ' },',
  24284. ' set: function (v) {',
  24285. ' this.p.Func = v;',
  24286. ' }',
  24287. '}, $mod.Func);',
  24288. '']));
  24289. end;
  24290. procedure TTestModule.TestProcType_PassProcToArray;
  24291. begin
  24292. StartProgram(false);
  24293. Add([
  24294. 'type',
  24295. ' TFunc = function: longint;',
  24296. ' TArrFunc = array of TFunc;',
  24297. 'procedure DoIt(Arr: TArrFunc); begin end;',
  24298. 'function GetIt: longint; begin end;',
  24299. 'var',
  24300. ' Func: tfunc;',
  24301. 'begin',
  24302. ' doit([]);',
  24303. ' doit([@GetIt]);',
  24304. ' doit([Func]);',
  24305. '']);
  24306. ConvertProgram;
  24307. CheckSource('TestProcType_PassProcToArray',
  24308. LinesToStr([ // statements
  24309. 'this.DoIt = function (Arr) {',
  24310. '};',
  24311. 'this.GetIt = function () {',
  24312. ' var Result = 0;',
  24313. ' return Result;',
  24314. '};',
  24315. 'this.Func = null;',
  24316. '']),
  24317. LinesToStr([ // $mod.$main
  24318. '$mod.DoIt([]);',
  24319. '$mod.DoIt([$mod.GetIt]);',
  24320. '$mod.DoIt([$mod.Func]);',
  24321. '']));
  24322. end;
  24323. procedure TTestModule.TestPointer;
  24324. begin
  24325. StartProgram(false);
  24326. Add(['type',
  24327. ' TObject = class end;',
  24328. ' TClass = class of TObject;',
  24329. ' TArrInt = array of longint;',
  24330. 'const',
  24331. ' n = nil;',
  24332. 'var',
  24333. ' v: jsvalue;',
  24334. ' Obj: tobject;',
  24335. ' C: tclass;',
  24336. ' a: tarrint;',
  24337. ' p: Pointer = nil;',
  24338. ' s: string;',
  24339. 'begin',
  24340. ' p:=p;',
  24341. ' p:=nil;',
  24342. ' if p=nil then;',
  24343. ' if nil=p then;',
  24344. ' if Assigned(p) then;',
  24345. ' p:=Pointer(v);',
  24346. ' p:=obj;',
  24347. ' p:=c;',
  24348. ' p:=a;',
  24349. ' p:=tobject;',
  24350. ' obj:=TObject(p);',
  24351. ' c:=TClass(p);',
  24352. ' a:=TArrInt(p);',
  24353. ' p:=n;',
  24354. ' p:=Pointer(a);',
  24355. ' p:=pointer(s);',
  24356. ' s:=string(p);',
  24357. '']);
  24358. ConvertProgram;
  24359. CheckSource('TestPointer',
  24360. LinesToStr([ // statements
  24361. 'rtl.createClass($mod, "TObject", null, function () {',
  24362. ' this.$init = function () {',
  24363. ' };',
  24364. ' this.$final = function () {',
  24365. ' };',
  24366. '});',
  24367. 'this.n = null;',
  24368. 'this.v = undefined;',
  24369. 'this.Obj = null;',
  24370. 'this.C = null;',
  24371. 'this.a = [];',
  24372. 'this.p = null;',
  24373. 'this.s = "";',
  24374. '']),
  24375. LinesToStr([ // $mod.$main
  24376. '$mod.p = $mod.p;',
  24377. '$mod.p = null;',
  24378. 'if ($mod.p === null) ;',
  24379. 'if (null === $mod.p) ;',
  24380. 'if ($mod.p != null) ;',
  24381. '$mod.p = $mod.v;',
  24382. '$mod.p = $mod.Obj;',
  24383. '$mod.p = $mod.C;',
  24384. '$mod.p = $mod.a;',
  24385. '$mod.p = $mod.TObject;',
  24386. '$mod.Obj = $mod.p;',
  24387. '$mod.C = $mod.p;',
  24388. '$mod.a = $mod.p;',
  24389. '$mod.p = null;',
  24390. '$mod.p = $mod.a;',
  24391. '$mod.p = $mod.s;',
  24392. '$mod.s = $mod.p;',
  24393. '']));
  24394. end;
  24395. procedure TTestModule.TestPointer_Proc;
  24396. begin
  24397. StartProgram(false);
  24398. Add('type');
  24399. Add(' TObject = class');
  24400. Add(' procedure DoIt; virtual; abstract;');
  24401. Add(' end;');
  24402. Add('procedure DoSome; begin end;');
  24403. Add('var');
  24404. Add(' o: TObject;');
  24405. Add(' p: Pointer;');
  24406. Add('begin');
  24407. Add(' p:=@DoSome;');
  24408. Add(' p:[email protected];');
  24409. ConvertProgram;
  24410. CheckSource('TestPointer_Proc',
  24411. LinesToStr([ // statements
  24412. 'rtl.createClass($mod, "TObject", null, function () {',
  24413. ' this.$init = function () {',
  24414. ' };',
  24415. ' this.$final = function () {',
  24416. ' };',
  24417. '});',
  24418. 'this.DoSome = function () {',
  24419. '};',
  24420. 'this.o = null;',
  24421. 'this.p = null;',
  24422. '']),
  24423. LinesToStr([ // $mod.$main
  24424. '$mod.p = $mod.DoSome;',
  24425. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  24426. '']));
  24427. end;
  24428. procedure TTestModule.TestPointer_AssignRecordFail;
  24429. begin
  24430. StartProgram(false);
  24431. Add('type');
  24432. Add(' TRec = record end;');
  24433. Add('var');
  24434. Add(' p: Pointer;');
  24435. Add(' r: TRec;');
  24436. Add('begin');
  24437. Add(' p:=r;');
  24438. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  24439. nIncompatibleTypesGotExpected);
  24440. ConvertProgram;
  24441. end;
  24442. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  24443. begin
  24444. StartProgram(false);
  24445. Add('type');
  24446. Add(' TArr = array[boolean] of longint;');
  24447. Add('var');
  24448. Add(' p: Pointer;');
  24449. Add(' a: TArr;');
  24450. Add('begin');
  24451. Add(' p:=a;');
  24452. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  24453. nIncompatibleTypesGotExpected);
  24454. ConvertProgram;
  24455. end;
  24456. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  24457. begin
  24458. StartProgram(false);
  24459. Add([
  24460. 'procedure DoIt(args: array of jsvalue); begin end;',
  24461. 'procedure DoAll; varargs; begin end;',
  24462. 'var',
  24463. ' v: jsvalue;',
  24464. 'begin',
  24465. ' DoIt([pointer(v)]);',
  24466. ' DoAll(pointer(v));',
  24467. '']);
  24468. ConvertProgram;
  24469. CheckSource('TestPointer_TypeCastJSValueToPointer',
  24470. LinesToStr([ // statements
  24471. 'this.DoIt = function (args) {',
  24472. '};',
  24473. 'this.DoAll = function () {',
  24474. '};',
  24475. 'this.v = undefined;',
  24476. '']),
  24477. LinesToStr([ // $mod.$main
  24478. '$mod.DoIt([$mod.v]);',
  24479. '$mod.DoAll($mod.v);',
  24480. '']));
  24481. end;
  24482. procedure TTestModule.TestPointer_NonRecordFail;
  24483. begin
  24484. StartProgram(false);
  24485. Add([
  24486. 'type',
  24487. ' p = ^longint;',
  24488. 'begin',
  24489. '']);
  24490. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  24491. ConvertProgram;
  24492. end;
  24493. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  24494. begin
  24495. StartProgram(false);
  24496. Add([
  24497. 'procedure DoIt(p: ^longint); begin end;',
  24498. 'begin',
  24499. '']);
  24500. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24501. ConvertProgram;
  24502. end;
  24503. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  24504. begin
  24505. StartProgram(false);
  24506. Add([
  24507. 'var p: ^longint;',
  24508. 'begin',
  24509. '']);
  24510. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24511. ConvertProgram;
  24512. end;
  24513. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  24514. begin
  24515. StartProgram(false);
  24516. Add([
  24517. 'function DoIt: ^longint; begin end;',
  24518. 'begin',
  24519. '']);
  24520. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24521. ConvertProgram;
  24522. end;
  24523. procedure TTestModule.TestPointer_AddrOperatorFail;
  24524. begin
  24525. StartProgram(false);
  24526. Add([
  24527. 'var i: longint;',
  24528. 'begin',
  24529. ' if @i=nil then ;',
  24530. '']);
  24531. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  24532. ConvertProgram;
  24533. end;
  24534. procedure TTestModule.TestPointer_ArrayParamsFail;
  24535. begin
  24536. StartProgram(false);
  24537. Add([
  24538. 'var',
  24539. ' p: Pointer;',
  24540. 'begin',
  24541. ' p:=p[1];',
  24542. '']);
  24543. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  24544. ConvertProgram;
  24545. end;
  24546. procedure TTestModule.TestPointer_PointerAddFail;
  24547. begin
  24548. StartProgram(false);
  24549. Add([
  24550. 'var',
  24551. ' p: Pointer;',
  24552. 'begin',
  24553. ' p:=p+1;',
  24554. '']);
  24555. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  24556. ConvertProgram;
  24557. end;
  24558. procedure TTestModule.TestPointer_IncPointerFail;
  24559. begin
  24560. StartProgram(false);
  24561. Add([
  24562. 'var',
  24563. ' p: Pointer;',
  24564. 'begin',
  24565. ' inc(p,1);',
  24566. '']);
  24567. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  24568. nIncompatibleTypeArgNo);
  24569. ConvertProgram;
  24570. end;
  24571. procedure TTestModule.TestPointer_Record;
  24572. begin
  24573. StartProgram(false);
  24574. Add([
  24575. 'type',
  24576. ' TRec = record x: longint; end;',
  24577. ' PRec = ^TRec;',
  24578. 'var',
  24579. ' r: TRec;',
  24580. ' p: PRec;',
  24581. ' q: ^TRec;',
  24582. ' Ptr: pointer;',
  24583. 'begin',
  24584. ' new(p);',
  24585. ' p:=@r;',
  24586. ' r:=p^;',
  24587. ' r.x:=p^.x;',
  24588. ' p^.x:=r.x;',
  24589. ' if p^.x=3 then ;',
  24590. ' if 4=p^.x then ;',
  24591. ' dispose(p);',
  24592. ' new(q);',
  24593. ' dispose(q);',
  24594. ' Ptr:=p;',
  24595. ' p:=PRec(ptr);',
  24596. '']);
  24597. ConvertProgram;
  24598. CheckSource('TestPointer_Record',
  24599. LinesToStr([ // statements
  24600. 'rtl.recNewT($mod, "TRec", function () {',
  24601. ' this.x = 0;',
  24602. ' this.$eq = function (b) {',
  24603. ' return this.x === b.x;',
  24604. ' };',
  24605. ' this.$assign = function (s) {',
  24606. ' this.x = s.x;',
  24607. ' return this;',
  24608. ' };',
  24609. '});',
  24610. 'this.r = $mod.TRec.$new();',
  24611. 'this.p = null;',
  24612. 'this.q = null;',
  24613. 'this.Ptr = null;',
  24614. '']),
  24615. LinesToStr([ // $mod.$main
  24616. '$mod.p = $mod.TRec.$new();',
  24617. '$mod.p = $mod.r;',
  24618. '$mod.r.$assign($mod.p);',
  24619. '$mod.r.x = $mod.p.x;',
  24620. '$mod.p.x = $mod.r.x;',
  24621. 'if ($mod.p.x === 3) ;',
  24622. 'if (4 === $mod.p.x) ;',
  24623. '$mod.p = null;',
  24624. '$mod.q = $mod.TRec.$new();',
  24625. '$mod.q = null;',
  24626. '$mod.Ptr = $mod.p;',
  24627. '$mod.p = $mod.Ptr;',
  24628. '']));
  24629. end;
  24630. procedure TTestModule.TestPointer_RecordArg;
  24631. begin
  24632. StartProgram(false);
  24633. Add([
  24634. '{$modeswitch autoderef}',
  24635. 'type',
  24636. ' TRec = record x: longint; end;',
  24637. ' PRec = ^TRec;',
  24638. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  24639. 'begin',
  24640. ' a.x:=a.x;',
  24641. ' a^.x:=a^.x;',
  24642. ' with a^ do',
  24643. ' x:=x;',
  24644. 'end;',
  24645. 'function GetIt(p: PRec): PRec;',
  24646. 'begin',
  24647. ' p.x:=p.x;',
  24648. ' p^.x:=p^.x;',
  24649. ' with p^ do',
  24650. ' x:=x;',
  24651. 'end;',
  24652. 'var',
  24653. ' r: TRec;',
  24654. ' p: PRec;',
  24655. 'begin',
  24656. ' p:=GetIt(p);',
  24657. ' p^:=GetIt(@r)^;',
  24658. ' DoIt(p,p,p);',
  24659. ' DoIt(@r,p,p);',
  24660. '']);
  24661. ConvertProgram;
  24662. CheckSource('TestPointer_Record',
  24663. LinesToStr([ // statements
  24664. 'rtl.recNewT($mod, "TRec", function () {',
  24665. ' this.x = 0;',
  24666. ' this.$eq = function (b) {',
  24667. ' return this.x === b.x;',
  24668. ' };',
  24669. ' this.$assign = function (s) {',
  24670. ' this.x = s.x;',
  24671. ' return this;',
  24672. ' };',
  24673. '});',
  24674. 'this.DoIt = function (a, b, c) {',
  24675. ' var Result = $mod.TRec.$new();',
  24676. ' a.x = a.x;',
  24677. ' a.x = a.x;',
  24678. ' a.x = a.x;',
  24679. ' return Result;',
  24680. '};',
  24681. 'this.GetIt = function (p) {',
  24682. ' var Result = null;',
  24683. ' p.x = p.x;',
  24684. ' p.x = p.x;',
  24685. ' p.x = p.x;',
  24686. ' return Result;',
  24687. '};',
  24688. 'this.r = $mod.TRec.$new();',
  24689. 'this.p = null;',
  24690. '']),
  24691. LinesToStr([ // $mod.$main
  24692. '$mod.p = $mod.GetIt($mod.p);',
  24693. '$mod.p.$assign($mod.GetIt($mod.r));',
  24694. '$mod.DoIt($mod.p, {',
  24695. ' p: $mod,',
  24696. ' get: function () {',
  24697. ' return this.p.p;',
  24698. ' },',
  24699. ' set: function (v) {',
  24700. ' this.p.p = v;',
  24701. ' }',
  24702. '}, {',
  24703. ' p: $mod,',
  24704. ' get: function () {',
  24705. ' return this.p.p;',
  24706. ' },',
  24707. ' set: function (v) {',
  24708. ' this.p.p = v;',
  24709. ' }',
  24710. '});',
  24711. '$mod.DoIt($mod.r, {',
  24712. ' p: $mod,',
  24713. ' get: function () {',
  24714. ' return this.p.p;',
  24715. ' },',
  24716. ' set: function (v) {',
  24717. ' this.p.p = v;',
  24718. ' }',
  24719. '}, {',
  24720. ' p: $mod,',
  24721. ' get: function () {',
  24722. ' return this.p.p;',
  24723. ' },',
  24724. ' set: function (v) {',
  24725. ' this.p.p = v;',
  24726. ' }',
  24727. '});',
  24728. '']));
  24729. end;
  24730. procedure TTestModule.TestJSValue_AssignToJSValue;
  24731. begin
  24732. StartProgram(false);
  24733. Add('var');
  24734. Add(' v: jsvalue;');
  24735. Add(' i: longint;');
  24736. Add(' s: string;');
  24737. Add(' b: boolean;');
  24738. Add(' d: double;');
  24739. Add(' p: pointer;');
  24740. Add('begin');
  24741. Add(' v:=v;');
  24742. Add(' v:=1;');
  24743. Add(' v:=i;');
  24744. Add(' v:='''';');
  24745. Add(' v:=''c'';');
  24746. Add(' v:=''foo'';');
  24747. Add(' v:=s;');
  24748. Add(' v:=false;');
  24749. Add(' v:=true;');
  24750. Add(' v:=b;');
  24751. Add(' v:=0.1;');
  24752. Add(' v:=d;');
  24753. Add(' v:=nil;');
  24754. Add(' v:=p;');
  24755. ConvertProgram;
  24756. CheckSource('TestJSValue_AssignToJSValue',
  24757. LinesToStr([ // statements
  24758. 'this.v = undefined;',
  24759. 'this.i = 0;',
  24760. 'this.s = "";',
  24761. 'this.b = false;',
  24762. 'this.d = 0.0;',
  24763. 'this.p = null;',
  24764. '']),
  24765. LinesToStr([ // $mod.$main
  24766. '$mod.v = $mod.v;',
  24767. '$mod.v = 1;',
  24768. '$mod.v = $mod.i;',
  24769. '$mod.v = "";',
  24770. '$mod.v = "c";',
  24771. '$mod.v = "foo";',
  24772. '$mod.v = $mod.s;',
  24773. '$mod.v = false;',
  24774. '$mod.v = true;',
  24775. '$mod.v = $mod.b;',
  24776. '$mod.v = 0.1;',
  24777. '$mod.v = $mod.d;',
  24778. '$mod.v = null;',
  24779. '$mod.v = $mod.p;',
  24780. '']));
  24781. end;
  24782. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  24783. begin
  24784. StartProgram(false);
  24785. Add('type');
  24786. Add(' integer = longint;');
  24787. Add(' TYesNo = boolean;');
  24788. Add(' TFloat = double;');
  24789. Add(' TCaption = string;');
  24790. Add(' TChar = char;');
  24791. Add('var');
  24792. Add(' v: jsvalue;');
  24793. Add(' i: integer;');
  24794. Add(' s: TCaption;');
  24795. Add(' b: TYesNo;');
  24796. Add(' d: TFloat;');
  24797. Add(' c: char;');
  24798. Add('begin');
  24799. Add(' i:=longint(v);');
  24800. Add(' i:=integer(v);');
  24801. Add(' s:=string(v);');
  24802. Add(' s:=TCaption(v);');
  24803. Add(' b:=boolean(v);');
  24804. Add(' b:=TYesNo(v);');
  24805. Add(' d:=double(v);');
  24806. Add(' d:=TFloat(v);');
  24807. Add(' c:=char(v);');
  24808. Add(' c:=TChar(v);');
  24809. ConvertProgram;
  24810. CheckSource('TestJSValue_TypeCastToBaseType',
  24811. LinesToStr([ // statements
  24812. 'this.v = undefined;',
  24813. 'this.i = 0;',
  24814. 'this.s = "";',
  24815. 'this.b = false;',
  24816. 'this.d = 0.0;',
  24817. 'this.c = "";',
  24818. '']),
  24819. LinesToStr([ // $mod.$main
  24820. '$mod.i = Math.floor($mod.v);',
  24821. '$mod.i = Math.floor($mod.v);',
  24822. '$mod.s = "" + $mod.v;',
  24823. '$mod.s = "" + $mod.v;',
  24824. '$mod.b = !($mod.v == false);',
  24825. '$mod.b = !($mod.v == false);',
  24826. '$mod.d = rtl.getNumber($mod.v);',
  24827. '$mod.d = rtl.getNumber($mod.v);',
  24828. '$mod.c = rtl.getChar($mod.v);',
  24829. '$mod.c = rtl.getChar($mod.v);',
  24830. '']));
  24831. end;
  24832. procedure TTestModule.TestJSValue_TypecastToJSValue;
  24833. begin
  24834. StartProgram(false);
  24835. Add([
  24836. 'type',
  24837. ' TArr = array of word;',
  24838. ' TRec = record end;',
  24839. ' TSet = set of boolean;',
  24840. 'procedure Fly(v: jsvalue);',
  24841. 'begin',
  24842. 'end;',
  24843. 'var',
  24844. ' a: TArr;',
  24845. ' r: TRec;',
  24846. ' s: TSet;',
  24847. 'begin',
  24848. ' Fly(jsvalue(a));',
  24849. ' Fly(jsvalue(r));',
  24850. ' Fly(jsvalue(s));',
  24851. '']);
  24852. ConvertProgram;
  24853. CheckSource('TestJSValue_TypecastToJSValue',
  24854. LinesToStr([ // statements
  24855. 'rtl.recNewT($mod, "TRec", function () {',
  24856. ' this.$eq = function (b) {',
  24857. ' return true;',
  24858. ' };',
  24859. ' this.$assign = function (s) {',
  24860. ' return this;',
  24861. ' };',
  24862. '});',
  24863. 'this.Fly = function (v) {',
  24864. '};',
  24865. 'this.a = [];',
  24866. 'this.r = $mod.TRec.$new();',
  24867. 'this.s = {};',
  24868. '']),
  24869. LinesToStr([ // $mod.$main
  24870. '$mod.Fly($mod.a);',
  24871. '$mod.Fly($mod.r);',
  24872. '$mod.Fly($mod.s);',
  24873. '']));
  24874. end;
  24875. procedure TTestModule.TestJSValue_Equal;
  24876. begin
  24877. StartProgram(false);
  24878. Add('type');
  24879. Add(' integer = longint;');
  24880. Add(' TYesNo = boolean;');
  24881. Add(' TFloat = double;');
  24882. Add(' TCaption = string;');
  24883. Add(' TChar = char;');
  24884. Add(' TMulti = JSValue;');
  24885. Add('var');
  24886. Add(' v: jsvalue;');
  24887. Add(' i: integer;');
  24888. Add(' s: TCaption;');
  24889. Add(' b: TYesNo;');
  24890. Add(' d: TFloat;');
  24891. Add(' c: char;');
  24892. Add(' m: TMulti;');
  24893. Add('begin');
  24894. Add(' b:=v=v;');
  24895. Add(' b:=v<>v;');
  24896. Add(' b:=v=1;');
  24897. Add(' b:=v<>1;');
  24898. Add(' b:=2=v;');
  24899. Add(' b:=2<>v;');
  24900. Add(' b:=v=i;');
  24901. Add(' b:=i=v;');
  24902. Add(' b:=v=nil;');
  24903. Add(' b:=nil=v;');
  24904. Add(' b:=v=false;');
  24905. Add(' b:=true=v;');
  24906. Add(' b:=v=b;');
  24907. Add(' b:=b=v;');
  24908. Add(' b:=v=s;');
  24909. Add(' b:=s=v;');
  24910. Add(' b:=v=''foo'';');
  24911. Add(' b:=''''=v;');
  24912. Add(' b:=v=d;');
  24913. Add(' b:=d=v;');
  24914. Add(' b:=v=3.4;');
  24915. Add(' b:=5.6=v;');
  24916. Add(' b:=v=c;');
  24917. Add(' b:=c=v;');
  24918. Add(' b:=m=m;');
  24919. Add(' b:=v=m;');
  24920. Add(' b:=m=v;');
  24921. ConvertProgram;
  24922. CheckSource('TestJSValue_Equal',
  24923. LinesToStr([ // statements
  24924. 'this.v = undefined;',
  24925. 'this.i = 0;',
  24926. 'this.s = "";',
  24927. 'this.b = false;',
  24928. 'this.d = 0.0;',
  24929. 'this.c = "";',
  24930. 'this.m = undefined;',
  24931. '']),
  24932. LinesToStr([ // $mod.$main
  24933. '$mod.b = $mod.v == $mod.v;',
  24934. '$mod.b = $mod.v != $mod.v;',
  24935. '$mod.b = $mod.v == 1;',
  24936. '$mod.b = $mod.v != 1;',
  24937. '$mod.b = 2 == $mod.v;',
  24938. '$mod.b = 2 != $mod.v;',
  24939. '$mod.b = $mod.v == $mod.i;',
  24940. '$mod.b = $mod.i == $mod.v;',
  24941. '$mod.b = $mod.v == null;',
  24942. '$mod.b = null == $mod.v;',
  24943. '$mod.b = $mod.v == false;',
  24944. '$mod.b = true == $mod.v;',
  24945. '$mod.b = $mod.v == $mod.b;',
  24946. '$mod.b = $mod.b == $mod.v;',
  24947. '$mod.b = $mod.v == $mod.s;',
  24948. '$mod.b = $mod.s == $mod.v;',
  24949. '$mod.b = $mod.v == "foo";',
  24950. '$mod.b = "" == $mod.v;',
  24951. '$mod.b = $mod.v == $mod.d;',
  24952. '$mod.b = $mod.d == $mod.v;',
  24953. '$mod.b = $mod.v == 3.4;',
  24954. '$mod.b = 5.6 == $mod.v;',
  24955. '$mod.b = $mod.v == $mod.c;',
  24956. '$mod.b = $mod.c == $mod.v;',
  24957. '$mod.b = $mod.m == $mod.m;',
  24958. '$mod.b = $mod.v == $mod.m;',
  24959. '$mod.b = $mod.m == $mod.v;',
  24960. '']));
  24961. end;
  24962. procedure TTestModule.TestJSValue_If;
  24963. begin
  24964. StartProgram(false);
  24965. Add([
  24966. 'var',
  24967. ' v: jsvalue;',
  24968. 'begin',
  24969. ' if v then ;',
  24970. ' while v do ;',
  24971. ' repeat until v;',
  24972. '']);
  24973. ConvertProgram;
  24974. CheckSource('TestJSValue_If',
  24975. LinesToStr([ // statements
  24976. 'this.v = undefined;',
  24977. '']),
  24978. LinesToStr([ // $mod.$main
  24979. 'if ($mod.v) ;',
  24980. 'while($mod.v){',
  24981. '};',
  24982. 'do{',
  24983. '} while(!$mod.v);',
  24984. '']));
  24985. end;
  24986. procedure TTestModule.TestJSValue_Not;
  24987. begin
  24988. StartProgram(false);
  24989. Add([
  24990. 'var',
  24991. ' v: jsvalue;',
  24992. ' b: boolean;',
  24993. 'begin',
  24994. ' b:=not v;',
  24995. ' if not v then ;',
  24996. ' while not v do ;',
  24997. ' repeat until not v;',
  24998. '']);
  24999. ConvertProgram;
  25000. CheckSource('TestJSValue_If',
  25001. LinesToStr([ // statements
  25002. 'this.v = undefined;',
  25003. 'this.b = false;',
  25004. '']),
  25005. LinesToStr([ // $mod.$main
  25006. '$mod.b=!$mod.v;',
  25007. 'if (!$mod.v) ;',
  25008. 'while(!$mod.v){',
  25009. '};',
  25010. 'do{',
  25011. '} while($mod.v);',
  25012. '']));
  25013. end;
  25014. procedure TTestModule.TestJSValue_Enum;
  25015. begin
  25016. StartProgram(false);
  25017. Add('type');
  25018. Add(' TColor = (red, blue);');
  25019. Add(' TRedBlue = TColor;');
  25020. Add('var');
  25021. Add(' v: jsvalue;');
  25022. Add(' e: TColor;');
  25023. Add('begin');
  25024. Add(' v:=e;');
  25025. Add(' v:=TColor(e);');
  25026. Add(' v:=TRedBlue(e);');
  25027. Add(' e:=TColor(v);');
  25028. Add(' e:=TRedBlue(v);');
  25029. ConvertProgram;
  25030. CheckSource('TestJSValue_Enum',
  25031. LinesToStr([ // statements
  25032. 'this.TColor = {',
  25033. ' "0": "red",',
  25034. ' red: 0,',
  25035. ' "1": "blue",',
  25036. ' blue: 1',
  25037. '};',
  25038. 'this.v = undefined;',
  25039. 'this.e = 0;',
  25040. '']),
  25041. LinesToStr([ // $mod.$main
  25042. '$mod.v = $mod.e;',
  25043. '$mod.v = $mod.e;',
  25044. '$mod.v = $mod.e;',
  25045. '$mod.e = $mod.v;',
  25046. '$mod.e = $mod.v;',
  25047. '']));
  25048. end;
  25049. procedure TTestModule.TestJSValue_ClassInstance;
  25050. begin
  25051. StartProgram(false);
  25052. Add([
  25053. 'type',
  25054. ' TObject = class',
  25055. ' end;',
  25056. ' TBirdObject = TObject;',
  25057. 'var',
  25058. ' v: jsvalue;',
  25059. ' o: TObject;',
  25060. 'begin',
  25061. ' v:=o;',
  25062. ' v:=TObject(o);',
  25063. ' v:=TBirdObject(o);',
  25064. ' o:=TObject(v);',
  25065. ' o:=TBirdObject(v);',
  25066. ' if v is TObject then ;',
  25067. '']);
  25068. ConvertProgram;
  25069. CheckSource('TestJSValue_ClassInstance',
  25070. LinesToStr([ // statements
  25071. 'rtl.createClass($mod, "TObject", null, function () {',
  25072. ' this.$init = function () {',
  25073. ' };',
  25074. ' this.$final = function () {',
  25075. ' };',
  25076. '});',
  25077. 'this.v = undefined;',
  25078. 'this.o = null;',
  25079. '']),
  25080. LinesToStr([ // $mod.$main
  25081. '$mod.v = $mod.o;',
  25082. '$mod.v = $mod.o;',
  25083. '$mod.v = $mod.o;',
  25084. '$mod.o = rtl.getObject($mod.v);',
  25085. '$mod.o = rtl.getObject($mod.v);',
  25086. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  25087. '']));
  25088. end;
  25089. procedure TTestModule.TestJSValue_ClassOf;
  25090. begin
  25091. StartProgram(false);
  25092. Add([
  25093. 'type',
  25094. ' TClass = class of TObject;',
  25095. ' TObject = class',
  25096. ' end;',
  25097. ' TBirds = class of TBird;',
  25098. ' TBird = class(TObject) end;',
  25099. 'var',
  25100. ' v: jsvalue;',
  25101. ' c: TClass;',
  25102. 'begin',
  25103. ' v:=c;',
  25104. ' v:=TObject;',
  25105. ' v:=TClass(c);',
  25106. ' v:=TBirds(c);',
  25107. ' c:=TClass(v);',
  25108. ' c:=TBirds(v);',
  25109. ' if v is TClass then ;',
  25110. '']);
  25111. ConvertProgram;
  25112. CheckSource('TestJSValue_ClassOf',
  25113. LinesToStr([ // statements
  25114. 'rtl.createClass($mod, "TObject", null, function () {',
  25115. ' this.$init = function () {',
  25116. ' };',
  25117. ' this.$final = function () {',
  25118. ' };',
  25119. '});',
  25120. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  25121. '});',
  25122. 'this.v = undefined;',
  25123. 'this.c = null;',
  25124. '']),
  25125. LinesToStr([ // $mod.$main
  25126. '$mod.v = $mod.c;',
  25127. '$mod.v = $mod.TObject;',
  25128. '$mod.v = $mod.c;',
  25129. '$mod.v = $mod.c;',
  25130. '$mod.c = rtl.getObject($mod.v);',
  25131. '$mod.c = rtl.getObject($mod.v);',
  25132. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  25133. '']));
  25134. end;
  25135. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  25136. begin
  25137. StartProgram(false);
  25138. Add([
  25139. 'type',
  25140. ' integer = longint;',
  25141. ' TArray = array of JSValue;',
  25142. ' TArrgh = tarray;',
  25143. ' TArrInt = array of integer;',
  25144. 'var',
  25145. ' v: jsvalue;',
  25146. ' TheArray: tarray = (1,''2'');',
  25147. ' Arr: tarrgh;',
  25148. ' i: integer;',
  25149. ' ArrInt: tarrint;',
  25150. 'begin',
  25151. ' arr:=thearray;',
  25152. ' thearray:=arr;',
  25153. ' setlength(arr,2);',
  25154. ' setlength(thearray,3);',
  25155. ' arr[4]:=v;',
  25156. ' arr[5]:=length(thearray);',
  25157. ' arr[6]:=nil;',
  25158. ' arr[7]:=thearray[8];',
  25159. ' arr[low(arr)]:=high(thearray);',
  25160. ' arr:=arrint;',
  25161. ' arrInt:=tarrint(arr);',
  25162. ' if TheArray = nil then ;',
  25163. ' if nil = TheArray then ;',
  25164. ' if TheArray <> nil then ;',
  25165. ' if nil <> TheArray then ;',
  25166. '']);
  25167. ConvertProgram;
  25168. CheckSource('TestJSValue_ArrayOfJSValue',
  25169. LinesToStr([ // statements
  25170. 'this.v = undefined;',
  25171. 'this.TheArray = [1, "2"];',
  25172. 'this.Arr = [];',
  25173. 'this.i = 0;',
  25174. 'this.ArrInt = [];',
  25175. '']),
  25176. LinesToStr([ // $mod.$main
  25177. '$mod.Arr = $mod.TheArray;',
  25178. '$mod.TheArray = $mod.Arr;',
  25179. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  25180. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  25181. '$mod.Arr[4] = $mod.v;',
  25182. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  25183. '$mod.Arr[6] = null;',
  25184. '$mod.Arr[7] = $mod.TheArray[8];',
  25185. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  25186. '$mod.Arr = $mod.ArrInt;',
  25187. '$mod.ArrInt = $mod.Arr;',
  25188. 'if (rtl.length($mod.TheArray) === 0) ;',
  25189. 'if (rtl.length($mod.TheArray) === 0) ;',
  25190. 'if (rtl.length($mod.TheArray) > 0) ;',
  25191. 'if (rtl.length($mod.TheArray) > 0) ;',
  25192. '']));
  25193. end;
  25194. procedure TTestModule.TestJSValue_ArrayLit;
  25195. begin
  25196. StartProgram(false);
  25197. Add([
  25198. 'type',
  25199. ' TFlag = (big,small);',
  25200. ' TArray = array of JSValue;',
  25201. ' TObject = class end;',
  25202. ' TClass = class of TObject;',
  25203. 'var',
  25204. ' v: jsvalue;',
  25205. ' a: TArray;',
  25206. ' o: TObject;',
  25207. 'begin',
  25208. ' a:=[];',
  25209. ' a:=[1];',
  25210. ' a:=[1,2];',
  25211. ' a:=[big];',
  25212. ' a:=[1,big];',
  25213. ' a:=[o,nil];',
  25214. '']);
  25215. ConvertProgram;
  25216. CheckSource('TestJSValue_ArrayLit',
  25217. LinesToStr([ // statements
  25218. 'this.TFlag = {',
  25219. ' "0": "big",',
  25220. ' big: 0,',
  25221. ' "1": "small",',
  25222. ' small: 1',
  25223. '};',
  25224. 'rtl.createClass($mod, "TObject", null, function () {',
  25225. ' this.$init = function () {',
  25226. ' };',
  25227. ' this.$final = function () {',
  25228. ' };',
  25229. '});',
  25230. 'this.v = undefined;',
  25231. 'this.a = [];',
  25232. 'this.o = null;',
  25233. '']),
  25234. LinesToStr([ // $mod.$main
  25235. '$mod.a = [];',
  25236. '$mod.a = [1];',
  25237. '$mod.a = [1, 2];',
  25238. '$mod.a = [$mod.TFlag.big];',
  25239. '$mod.a = [1, $mod.TFlag.big];',
  25240. '$mod.a = [$mod.o, null];',
  25241. '']));
  25242. end;
  25243. procedure TTestModule.TestJSValue_Params;
  25244. begin
  25245. StartProgram(false);
  25246. Add('type');
  25247. Add(' integer = longint;');
  25248. Add(' TYesNo = boolean;');
  25249. Add(' TFloat = double;');
  25250. Add(' TCaption = string;');
  25251. Add(' TChar = char;');
  25252. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  25253. Add('var');
  25254. Add(' l: jsvalue;');
  25255. Add('begin');
  25256. Add(' a:=a;');
  25257. Add(' l:=b;');
  25258. Add(' c:=c;');
  25259. Add(' d:=d;');
  25260. Add(' Result:=l;');
  25261. Add('end;');
  25262. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  25263. Add('var');
  25264. Add(' v: jsvalue;');
  25265. Add(' i: integer;');
  25266. Add(' b: TYesNo;');
  25267. Add(' d: TFloat;');
  25268. Add(' s: TCaption;');
  25269. Add(' c: TChar;');
  25270. Add('begin');
  25271. Add(' v:=doit(v,v,v,v);');
  25272. Add(' i:=integer(dosome(i,i));');
  25273. Add(' b:=TYesNo(dosome(b,b));');
  25274. Add(' d:=TFloat(dosome(d,d));');
  25275. Add(' s:=TCaption(dosome(s,s));');
  25276. Add(' c:=TChar(dosome(c,c));');
  25277. ConvertProgram;
  25278. CheckSource('TestJSValue_Params',
  25279. LinesToStr([ // statements
  25280. 'this.DoIt = function (a, b, c, d) {',
  25281. ' var Result = undefined;',
  25282. ' var l = undefined;',
  25283. ' a = a;',
  25284. ' l = b;',
  25285. ' c.set(c.get());',
  25286. ' d.set(d.get());',
  25287. ' Result = l;',
  25288. ' return Result;',
  25289. '};',
  25290. 'this.DoSome = function (a, b) {',
  25291. ' var Result = undefined;',
  25292. ' return Result;',
  25293. '};',
  25294. 'this.v = undefined;',
  25295. 'this.i = 0;',
  25296. 'this.b = false;',
  25297. 'this.d = 0.0;',
  25298. 'this.s = "";',
  25299. 'this.c = "";',
  25300. '']),
  25301. LinesToStr([ // $mod.$main
  25302. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  25303. ' p: $mod,',
  25304. ' get: function () {',
  25305. ' return this.p.v;',
  25306. ' },',
  25307. ' set: function (v) {',
  25308. ' this.p.v = v;',
  25309. ' }',
  25310. '}, {',
  25311. ' p: $mod,',
  25312. ' get: function () {',
  25313. ' return this.p.v;',
  25314. ' },',
  25315. ' set: function (v) {',
  25316. ' this.p.v = v;',
  25317. ' }',
  25318. '});',
  25319. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  25320. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  25321. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  25322. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  25323. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  25324. '']));
  25325. end;
  25326. procedure TTestModule.TestJSValue_UntypedParam;
  25327. begin
  25328. StartProgram(false);
  25329. Add('function DoIt(const a; var b; out c): jsvalue;');
  25330. Add('begin');
  25331. Add(' Result:=a;');
  25332. Add(' Result:=b;');
  25333. Add(' Result:=c;');
  25334. Add(' b:=Result;');
  25335. Add(' c:=Result;');
  25336. Add('end;');
  25337. Add('var i: longint;');
  25338. Add('begin');
  25339. Add(' doit(i,i,i);');
  25340. ConvertProgram;
  25341. CheckSource('TestJSValue_UntypedParam',
  25342. LinesToStr([ // statements
  25343. 'this.DoIt = function (a, b, c) {',
  25344. ' var Result = undefined;',
  25345. ' Result = a;',
  25346. ' Result = b.get();',
  25347. ' Result = c.get();',
  25348. ' b.set(Result);',
  25349. ' c.set(Result);',
  25350. ' return Result;',
  25351. '};',
  25352. 'this.i = 0;',
  25353. '']),
  25354. LinesToStr([ // $mod.$main
  25355. '$mod.DoIt($mod.i, {',
  25356. ' p: $mod,',
  25357. ' get: function () {',
  25358. ' return this.p.i;',
  25359. ' },',
  25360. ' set: function (v) {',
  25361. ' this.p.i = v;',
  25362. ' }',
  25363. '}, {',
  25364. ' p: $mod,',
  25365. ' get: function () {',
  25366. ' return this.p.i;',
  25367. ' },',
  25368. ' set: function (v) {',
  25369. ' this.p.i = v;',
  25370. ' }',
  25371. '});',
  25372. '']));
  25373. end;
  25374. procedure TTestModule.TestJSValue_FuncResultType;
  25375. begin
  25376. StartProgram(false);
  25377. Add('type');
  25378. Add(' integer = longint;');
  25379. Add(' TJSValueArray = array of JSValue;');
  25380. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  25381. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  25382. Add('begin');
  25383. Add(' while Compare(P,aList[0])>0 do ;');
  25384. Add('end;');
  25385. Add('var');
  25386. Add(' Compare: TListSortCompare;');
  25387. Add(' V: JSValue;');
  25388. Add(' i: integer;');
  25389. Add('begin');
  25390. Add(' if Compare(V,V)>0 then ;');
  25391. Add(' if Compare(i,i)>1 then ;');
  25392. Add(' if Compare(nil,false)>2 then ;');
  25393. Add(' if Compare(1,true)>3 then ;');
  25394. ConvertProgram;
  25395. CheckSource('TestJSValue_UntypedParam',
  25396. LinesToStr([ // statements
  25397. 'this.Sort = function (P, aList, Compare) {',
  25398. ' while (Compare(P, aList[0]) > 0) {',
  25399. ' };',
  25400. '};',
  25401. 'this.Compare = null;',
  25402. 'this.V = undefined;',
  25403. 'this.i = 0;',
  25404. '']),
  25405. LinesToStr([ // $mod.$main
  25406. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  25407. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  25408. 'if ($mod.Compare(null, false) > 2) ;',
  25409. 'if ($mod.Compare(1, true) > 3) ;',
  25410. '']));
  25411. end;
  25412. procedure TTestModule.TestJSValue_ProcType_Assign;
  25413. begin
  25414. StartProgram(false);
  25415. Add('type');
  25416. Add(' integer = longint;');
  25417. Add(' TObject = class');
  25418. Add(' class function GetGlob: integer;');
  25419. Add(' function Getter: integer;');
  25420. Add(' end;');
  25421. Add('class function TObject.GetGlob: integer;');
  25422. Add('var v1: jsvalue;');
  25423. Add('begin');
  25424. Add(' v1:=@GetGlob;');
  25425. Add(' v1:[email protected];');
  25426. Add('end;');
  25427. Add('function TObject.Getter: integer;');
  25428. Add('var v2: jsvalue;');
  25429. Add('begin');
  25430. Add(' v2:=@Getter;');
  25431. Add(' v2:[email protected];');
  25432. Add(' v2:=@GetGlob;');
  25433. Add(' v2:[email protected];');
  25434. Add('end;');
  25435. Add('function GetIt(i: integer): integer;');
  25436. Add('var v3: jsvalue;');
  25437. Add('begin');
  25438. Add(' v3:=@GetIt;');
  25439. Add('end;');
  25440. Add('var');
  25441. Add(' V: JSValue;');
  25442. Add(' o: TObject;');
  25443. Add('begin');
  25444. Add(' v:=@GetIt;');
  25445. Add(' v:[email protected];');
  25446. Add(' v:[email protected];');
  25447. ConvertProgram;
  25448. CheckSource('TestJSValue_ProcType_Assign',
  25449. LinesToStr([ // statements
  25450. 'rtl.createClass($mod, "TObject", null, function () {',
  25451. ' this.$init = function () {',
  25452. ' };',
  25453. ' this.$final = function () {',
  25454. ' };',
  25455. ' this.GetGlob = function () {',
  25456. ' var Result = 0;',
  25457. ' var v1 = undefined;',
  25458. ' v1 = rtl.createCallback(this, "GetGlob");',
  25459. ' v1 = rtl.createCallback(this, "GetGlob");',
  25460. ' return Result;',
  25461. ' };',
  25462. ' this.Getter = function () {',
  25463. ' var Result = 0;',
  25464. ' var v2 = undefined;',
  25465. ' v2 = rtl.createCallback(this, "Getter");',
  25466. ' v2 = rtl.createCallback(this, "Getter");',
  25467. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  25468. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  25469. ' return Result;',
  25470. ' };',
  25471. '});',
  25472. 'this.GetIt = function (i) {',
  25473. ' var Result = 0;',
  25474. ' var v3 = undefined;',
  25475. ' v3 = $mod.GetIt;',
  25476. ' return Result;',
  25477. '};',
  25478. 'this.V = undefined;',
  25479. 'this.o = null;',
  25480. '']),
  25481. LinesToStr([ // $mod.$main
  25482. '$mod.V = $mod.GetIt;',
  25483. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  25484. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  25485. '']));
  25486. end;
  25487. procedure TTestModule.TestJSValue_ProcType_Equal;
  25488. begin
  25489. StartProgram(false);
  25490. Add('type');
  25491. Add(' integer = longint;');
  25492. Add(' TObject = class');
  25493. Add(' class function GetGlob: integer;');
  25494. Add(' function Getter: integer;');
  25495. Add(' end;');
  25496. Add('class function TObject.GetGlob: integer;');
  25497. Add('var v1: jsvalue;');
  25498. Add('begin');
  25499. Add(' if v1=@GetGlob then;');
  25500. Add(' if [email protected] then ;');
  25501. Add('end;');
  25502. Add('function TObject.Getter: integer;');
  25503. Add('var v2: jsvalue;');
  25504. Add('begin');
  25505. Add(' if v2=@Getter then;');
  25506. Add(' if [email protected] then ;');
  25507. Add(' if v2=@GetGlob then;');
  25508. Add(' if [email protected] then;');
  25509. Add('end;');
  25510. Add('function GetIt(i: integer): integer;');
  25511. Add('var v3: jsvalue;');
  25512. Add('begin');
  25513. Add(' if v3=@GetIt then;');
  25514. Add('end;');
  25515. Add('var');
  25516. Add(' V: JSValue;');
  25517. Add(' o: TObject;');
  25518. Add('begin');
  25519. Add(' if v=@GetIt then;');
  25520. Add(' if [email protected] then;');
  25521. Add(' if [email protected] then;');
  25522. Add(' if @GetIt=v then;');
  25523. Add(' if @o.Getter=v then;');
  25524. Add(' if @o.GetGlob=v then;');
  25525. ConvertProgram;
  25526. CheckSource('TestJSValue_ProcType_Equal',
  25527. LinesToStr([ // statements
  25528. 'rtl.createClass($mod, "TObject", null, function () {',
  25529. ' this.$init = function () {',
  25530. ' };',
  25531. ' this.$final = function () {',
  25532. ' };',
  25533. ' this.GetGlob = function () {',
  25534. ' var Result = 0;',
  25535. ' var v1 = undefined;',
  25536. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  25537. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  25538. ' return Result;',
  25539. ' };',
  25540. ' this.Getter = function () {',
  25541. ' var Result = 0;',
  25542. ' var v2 = undefined;',
  25543. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  25544. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  25545. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  25546. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  25547. ' return Result;',
  25548. ' };',
  25549. '});',
  25550. 'this.GetIt = function (i) {',
  25551. ' var Result = 0;',
  25552. ' var v3 = undefined;',
  25553. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  25554. ' return Result;',
  25555. '};',
  25556. 'this.V = undefined;',
  25557. 'this.o = null;',
  25558. '']),
  25559. LinesToStr([ // $mod.$main
  25560. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  25561. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  25562. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  25563. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  25564. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  25565. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  25566. '']));
  25567. end;
  25568. procedure TTestModule.TestJSValue_ProcType_Param;
  25569. begin
  25570. StartProgram(false);
  25571. Add([
  25572. 'type',
  25573. ' variant = jsvalue;',
  25574. ' TArrVariant = array of variant;',
  25575. ' TArrVar2 = TArrVariant;',
  25576. ' TFuncInt = function: longint;',
  25577. 'function GetIt: longint;',
  25578. 'begin',
  25579. 'end;',
  25580. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  25581. 'var v: variant;',
  25582. 'begin',
  25583. ' v:=arr[1];',
  25584. 'end;',
  25585. 'var s: string;',
  25586. 'begin',
  25587. ' DoIt(GetIt,[]);',
  25588. ' DoIt(@GetIt,[]);',
  25589. ' DoIt(1,[s,GetIt]);',
  25590. ' DoIt(1,[s,@GetIt]);',
  25591. '']);
  25592. ConvertProgram;
  25593. CheckSource('TestJSValue_ProcType_Param',
  25594. LinesToStr([ // statements
  25595. 'this.GetIt = function () {',
  25596. ' var Result = 0;',
  25597. ' return Result;',
  25598. '};',
  25599. 'this.DoIt = function (p, Arr) {',
  25600. ' var v = undefined;',
  25601. ' v = Arr[1];',
  25602. '};',
  25603. 'this.s = "";',
  25604. '']),
  25605. LinesToStr([ // $mod.$main
  25606. '$mod.DoIt($mod.GetIt(), []);',
  25607. '$mod.DoIt($mod.GetIt, []);',
  25608. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  25609. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  25610. '']));
  25611. end;
  25612. procedure TTestModule.TestJSValue_AssignToPointerFail;
  25613. begin
  25614. StartProgram(false);
  25615. Add([
  25616. 'var',
  25617. ' v: JSValue;',
  25618. ' p: Pointer;',
  25619. 'begin',
  25620. ' p:=v;',
  25621. '']);
  25622. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  25623. nIncompatibleTypesGotExpected);
  25624. ConvertProgram;
  25625. end;
  25626. procedure TTestModule.TestJSValue_OverloadDouble;
  25627. begin
  25628. StartProgram(false);
  25629. Add([
  25630. 'type',
  25631. ' integer = longint;',
  25632. ' tdatetime = double;',
  25633. 'procedure DoIt(d: double); begin end;',
  25634. 'procedure DoIt(v: jsvalue); begin end;',
  25635. 'var',
  25636. ' d: double;',
  25637. ' dt: tdatetime;',
  25638. ' i: integer;',
  25639. ' b: byte;',
  25640. ' shi: shortint;',
  25641. ' w: word;',
  25642. ' smi: smallint;',
  25643. ' lw: longword;',
  25644. ' li: longint;',
  25645. ' ni: nativeint;',
  25646. ' nu: nativeuint;',
  25647. 'begin',
  25648. ' DoIt(d);',
  25649. ' DoIt(dt);',
  25650. ' DoIt(i);',
  25651. ' DoIt(b);',
  25652. ' DoIt(shi);',
  25653. ' DoIt(w);',
  25654. ' DoIt(smi);',
  25655. ' DoIt(lw);',
  25656. ' DoIt(li);',
  25657. ' DoIt(ni);',
  25658. ' DoIt(nu);',
  25659. '']);
  25660. ConvertProgram;
  25661. CheckSource('TestJSValue_OverloadDouble',
  25662. LinesToStr([ // statements
  25663. 'this.DoIt = function (d) {',
  25664. '};',
  25665. 'this.DoIt$1 = function (v) {',
  25666. '};',
  25667. 'this.d = 0.0;',
  25668. 'this.dt = 0.0;',
  25669. 'this.i = 0;',
  25670. 'this.b = 0;',
  25671. 'this.shi = 0;',
  25672. 'this.w = 0;',
  25673. 'this.smi = 0;',
  25674. 'this.lw = 0;',
  25675. 'this.li = 0;',
  25676. 'this.ni = 0;',
  25677. 'this.nu = 0;',
  25678. '']),
  25679. LinesToStr([ // $mod.$main
  25680. '$mod.DoIt($mod.d);',
  25681. '$mod.DoIt($mod.dt);',
  25682. '$mod.DoIt$1($mod.i);',
  25683. '$mod.DoIt$1($mod.b);',
  25684. '$mod.DoIt$1($mod.shi);',
  25685. '$mod.DoIt$1($mod.w);',
  25686. '$mod.DoIt$1($mod.smi);',
  25687. '$mod.DoIt$1($mod.lw);',
  25688. '$mod.DoIt$1($mod.li);',
  25689. '$mod.DoIt$1($mod.ni);',
  25690. '$mod.DoIt$1($mod.nu);',
  25691. '']));
  25692. end;
  25693. procedure TTestModule.TestJSValue_OverloadNativeInt;
  25694. begin
  25695. StartProgram(false);
  25696. Add([
  25697. 'type',
  25698. ' integer = longint;',
  25699. ' int53 = nativeint;',
  25700. ' tdatetime = double;',
  25701. 'procedure DoIt(n: nativeint); begin end;',
  25702. 'procedure DoIt(v: jsvalue); begin end;',
  25703. 'var',
  25704. ' d: double;',
  25705. ' dt: tdatetime;',
  25706. ' i: integer;',
  25707. ' b: byte;',
  25708. ' shi: shortint;',
  25709. ' w: word;',
  25710. ' smi: smallint;',
  25711. ' lw: longword;',
  25712. ' li: longint;',
  25713. ' ni: nativeint;',
  25714. ' nu: nativeuint;',
  25715. 'begin',
  25716. ' DoIt(d);',
  25717. ' DoIt(dt);',
  25718. ' DoIt(i);',
  25719. ' DoIt(b);',
  25720. ' DoIt(shi);',
  25721. ' DoIt(w);',
  25722. ' DoIt(smi);',
  25723. ' DoIt(lw);',
  25724. ' DoIt(li);',
  25725. ' DoIt(ni);',
  25726. ' DoIt(nu);',
  25727. '']);
  25728. ConvertProgram;
  25729. CheckSource('TestJSValue_OverloadNativeInt',
  25730. LinesToStr([ // statements
  25731. 'this.DoIt = function (n) {',
  25732. '};',
  25733. 'this.DoIt$1 = function (v) {',
  25734. '};',
  25735. 'this.d = 0.0;',
  25736. 'this.dt = 0.0;',
  25737. 'this.i = 0;',
  25738. 'this.b = 0;',
  25739. 'this.shi = 0;',
  25740. 'this.w = 0;',
  25741. 'this.smi = 0;',
  25742. 'this.lw = 0;',
  25743. 'this.li = 0;',
  25744. 'this.ni = 0;',
  25745. 'this.nu = 0;',
  25746. '']),
  25747. LinesToStr([ // $mod.$main
  25748. '$mod.DoIt$1($mod.d);',
  25749. '$mod.DoIt$1($mod.dt);',
  25750. '$mod.DoIt($mod.i);',
  25751. '$mod.DoIt($mod.b);',
  25752. '$mod.DoIt($mod.shi);',
  25753. '$mod.DoIt($mod.w);',
  25754. '$mod.DoIt($mod.smi);',
  25755. '$mod.DoIt($mod.lw);',
  25756. '$mod.DoIt($mod.li);',
  25757. '$mod.DoIt($mod.ni);',
  25758. '$mod.DoIt($mod.nu);',
  25759. '']));
  25760. end;
  25761. procedure TTestModule.TestJSValue_OverloadWord;
  25762. begin
  25763. StartProgram(false);
  25764. Add([
  25765. 'type',
  25766. ' integer = longint;',
  25767. ' int53 = nativeint;',
  25768. ' tdatetime = double;',
  25769. 'procedure DoIt(w: word); begin end;',
  25770. 'procedure DoIt(v: jsvalue); begin end;',
  25771. 'var',
  25772. ' d: double;',
  25773. ' dt: tdatetime;',
  25774. ' i: integer;',
  25775. ' b: byte;',
  25776. ' shi: shortint;',
  25777. ' w: word;',
  25778. ' smi: smallint;',
  25779. ' lw: longword;',
  25780. ' li: longint;',
  25781. ' ni: nativeint;',
  25782. ' nu: nativeuint;',
  25783. 'begin',
  25784. ' DoIt(d);',
  25785. ' DoIt(dt);',
  25786. ' DoIt(i);',
  25787. ' DoIt(b);',
  25788. ' DoIt(shi);',
  25789. ' DoIt(w);',
  25790. ' DoIt(smi);',
  25791. ' DoIt(lw);',
  25792. ' DoIt(li);',
  25793. ' DoIt(ni);',
  25794. ' DoIt(nu);',
  25795. '']);
  25796. ConvertProgram;
  25797. CheckSource('TestJSValue_OverloadWord',
  25798. LinesToStr([ // statements
  25799. 'this.DoIt = function (w) {',
  25800. '};',
  25801. 'this.DoIt$1 = function (v) {',
  25802. '};',
  25803. 'this.d = 0.0;',
  25804. 'this.dt = 0.0;',
  25805. 'this.i = 0;',
  25806. 'this.b = 0;',
  25807. 'this.shi = 0;',
  25808. 'this.w = 0;',
  25809. 'this.smi = 0;',
  25810. 'this.lw = 0;',
  25811. 'this.li = 0;',
  25812. 'this.ni = 0;',
  25813. 'this.nu = 0;',
  25814. '']),
  25815. LinesToStr([ // $mod.$main
  25816. '$mod.DoIt$1($mod.d);',
  25817. '$mod.DoIt$1($mod.dt);',
  25818. '$mod.DoIt$1($mod.i);',
  25819. '$mod.DoIt($mod.b);',
  25820. '$mod.DoIt($mod.shi);',
  25821. '$mod.DoIt($mod.w);',
  25822. '$mod.DoIt$1($mod.smi);',
  25823. '$mod.DoIt$1($mod.lw);',
  25824. '$mod.DoIt$1($mod.li);',
  25825. '$mod.DoIt$1($mod.ni);',
  25826. '$mod.DoIt$1($mod.nu);',
  25827. '']));
  25828. end;
  25829. procedure TTestModule.TestJSValue_OverloadString;
  25830. begin
  25831. StartProgram(false);
  25832. Add([
  25833. 'type',
  25834. ' uni = string;',
  25835. ' WChar = char;',
  25836. 'procedure DoIt(s: string); begin end;',
  25837. 'procedure DoIt(v: jsvalue); begin end;',
  25838. 'var',
  25839. ' s: string;',
  25840. ' c: char;',
  25841. ' u: uni;',
  25842. 'begin',
  25843. ' DoIt(s);',
  25844. ' DoIt(c);',
  25845. ' DoIt(u);',
  25846. '']);
  25847. ConvertProgram;
  25848. CheckSource('TestJSValue_OverloadString',
  25849. LinesToStr([ // statements
  25850. 'this.DoIt = function (s) {',
  25851. '};',
  25852. 'this.DoIt$1 = function (v) {',
  25853. '};',
  25854. 'this.s = "";',
  25855. 'this.c = "";',
  25856. 'this.u = "";',
  25857. '']),
  25858. LinesToStr([ // $mod.$main
  25859. '$mod.DoIt($mod.s);',
  25860. '$mod.DoIt($mod.c);',
  25861. '$mod.DoIt($mod.u);',
  25862. '']));
  25863. end;
  25864. procedure TTestModule.TestJSValue_OverloadChar;
  25865. begin
  25866. StartProgram(false);
  25867. Add([
  25868. 'type',
  25869. ' uni = string;',
  25870. ' WChar = char;',
  25871. 'procedure DoIt(c: char); begin end;',
  25872. 'procedure DoIt(v: jsvalue); begin end;',
  25873. 'var',
  25874. ' s: string;',
  25875. ' c: char;',
  25876. ' u: uni;',
  25877. 'begin',
  25878. ' DoIt(s);',
  25879. ' DoIt(c);',
  25880. ' DoIt(u);',
  25881. '']);
  25882. ConvertProgram;
  25883. CheckSource('TestJSValue_OverloadChar',
  25884. LinesToStr([ // statements
  25885. 'this.DoIt = function (c) {',
  25886. '};',
  25887. 'this.DoIt$1 = function (v) {',
  25888. '};',
  25889. 'this.s = "";',
  25890. 'this.c = "";',
  25891. 'this.u = "";',
  25892. '']),
  25893. LinesToStr([ // $mod.$main
  25894. '$mod.DoIt$1($mod.s);',
  25895. '$mod.DoIt($mod.c);',
  25896. '$mod.DoIt$1($mod.u);',
  25897. '']));
  25898. end;
  25899. procedure TTestModule.TestJSValue_OverloadPointer;
  25900. begin
  25901. StartProgram(false);
  25902. Add([
  25903. 'type',
  25904. ' TObject = class end;',
  25905. 'procedure DoIt(p: pointer); begin end;',
  25906. 'procedure DoIt(v: jsvalue); begin end;',
  25907. 'var',
  25908. ' o: TObject;',
  25909. 'begin',
  25910. ' DoIt(o);',
  25911. '']);
  25912. ConvertProgram;
  25913. CheckSource('TestJSValue_OverloadPointer',
  25914. LinesToStr([ // statements
  25915. 'rtl.createClass($mod, "TObject", null, function () {',
  25916. ' this.$init = function () {',
  25917. ' };',
  25918. ' this.$final = function () {',
  25919. ' };',
  25920. '});',
  25921. 'this.DoIt = function (p) {',
  25922. '};',
  25923. 'this.DoIt$1 = function (v) {',
  25924. '};',
  25925. 'this.o = null;',
  25926. '']),
  25927. LinesToStr([ // $mod.$main
  25928. '$mod.DoIt($mod.o);',
  25929. '']));
  25930. end;
  25931. procedure TTestModule.TestJSValue_ForIn;
  25932. begin
  25933. StartProgram(false);
  25934. Add([
  25935. 'var',
  25936. ' v: JSValue;',
  25937. ' key: string;',
  25938. 'begin',
  25939. ' for key in v do begin',
  25940. ' if key=''abc'' then ;',
  25941. ' end;',
  25942. '']);
  25943. ConvertProgram;
  25944. CheckSource('TestJSValue_ForIn',
  25945. LinesToStr([ // statements
  25946. 'this.v = undefined;',
  25947. 'this.key = "";',
  25948. '']),
  25949. LinesToStr([ // $mod.$main
  25950. 'for ($mod.key in $mod.v) {',
  25951. ' if ($mod.key === "abc") ;',
  25952. '};',
  25953. '']));
  25954. end;
  25955. procedure TTestModule.TestRTTI_IntRange;
  25956. begin
  25957. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25958. StartProgram(false);
  25959. Add([
  25960. '{$modeswitch externalclass}',
  25961. 'type',
  25962. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  25963. ' end;',
  25964. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  25965. ' end;',
  25966. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  25967. ' TColor = type TGraphicsColor;',
  25968. 'var',
  25969. ' p: TTypeInfo;',
  25970. 'begin',
  25971. ' p:=typeinfo(TGraphicsColor);',
  25972. ' p:=typeinfo(TColor);',
  25973. '']);
  25974. ConvertProgram;
  25975. CheckSource('TestRTTI_IntRange',
  25976. LinesToStr([ // statements
  25977. '$mod.$rtti.$Int("TGraphicsColor", {',
  25978. ' minvalue: -2147483648,',
  25979. ' maxvalue: 2147483647,',
  25980. ' ordtype: 4',
  25981. '});',
  25982. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  25983. 'this.p = null;',
  25984. '']),
  25985. LinesToStr([ // $mod.$main
  25986. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  25987. '$mod.p = $mod.$rtti["TColor"];',
  25988. '']));
  25989. end;
  25990. procedure TTestModule.TestRTTI_Double;
  25991. begin
  25992. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25993. StartProgram(false);
  25994. Add([
  25995. '{$modeswitch externalclass}',
  25996. 'type',
  25997. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  25998. ' end;',
  25999. ' TFloat = type double;',
  26000. 'var',
  26001. ' p: TTypeInfo;',
  26002. 'begin',
  26003. ' p:=typeinfo(double);',
  26004. ' p:=typeinfo(TFloat);',
  26005. '']);
  26006. ConvertProgram;
  26007. CheckSource('TestRTTI_Double',
  26008. LinesToStr([ // statements
  26009. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  26010. 'this.p = null;',
  26011. '']),
  26012. LinesToStr([ // $mod.$main
  26013. '$mod.p = rtl.double;',
  26014. '$mod.p = $mod.$rtti["TFloat"];',
  26015. '']));
  26016. end;
  26017. procedure TTestModule.TestRTTI_ProcType;
  26018. begin
  26019. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26020. StartProgram(false);
  26021. Add('type');
  26022. Add(' TProcA = procedure;');
  26023. Add(' TMethodB = procedure of object;');
  26024. Add(' TProcC = procedure; varargs;');
  26025. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  26026. Add(' TProcE = function: nativeint;');
  26027. Add(' TProcF = function(const p: TProcA): nativeuint;');
  26028. Add('var p: pointer;');
  26029. Add('begin');
  26030. Add(' p:=typeinfo(tproca);');
  26031. ConvertProgram;
  26032. CheckSource('TestRTTI_ProcType',
  26033. LinesToStr([ // statements
  26034. '$mod.$rtti.$ProcVar("TProcA", {',
  26035. ' procsig: rtl.newTIProcSig(null)',
  26036. '});',
  26037. '$mod.$rtti.$MethodVar("TMethodB", {',
  26038. ' procsig: rtl.newTIProcSig(null),',
  26039. ' methodkind: 0',
  26040. '});',
  26041. '$mod.$rtti.$ProcVar("TProcC", {',
  26042. ' procsig: rtl.newTIProcSig(null, 2)',
  26043. '});',
  26044. '$mod.$rtti.$ProcVar("TProcD", {',
  26045. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  26046. '});',
  26047. '$mod.$rtti.$ProcVar("TProcE", {',
  26048. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  26049. '});',
  26050. '$mod.$rtti.$ProcVar("TProcF", {',
  26051. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  26052. '});',
  26053. 'this.p = null;',
  26054. '']),
  26055. LinesToStr([ // $mod.$main
  26056. '$mod.p = $mod.$rtti["TProcA"];',
  26057. '']));
  26058. end;
  26059. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  26060. begin
  26061. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26062. AddModuleWithIntfImplSrc('unit2.pas',
  26063. LinesToStr([
  26064. 'type',
  26065. ' TObject = class end;'
  26066. ]),
  26067. '');
  26068. StartUnit(true);
  26069. Add('interface');
  26070. Add('uses unit2;');
  26071. Add('type');
  26072. Add(' TProcA = function(o: tobject): tobject;');
  26073. Add('implementation');
  26074. Add('type');
  26075. Add(' TProcB = function(o: tobject): tobject;');
  26076. Add('var p: Pointer;');
  26077. Add('initialization');
  26078. Add(' p:=typeinfo(tproca);');
  26079. Add(' p:=typeinfo(tprocb);');
  26080. ConvertUnit;
  26081. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  26082. LinesToStr([ // statements
  26083. 'var $impl = $mod.$impl;',
  26084. '$mod.$rtti.$ProcVar("TProcA", {',
  26085. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26086. '});',
  26087. '']),
  26088. LinesToStr([ // this.$init
  26089. '$impl.p = $mod.$rtti["TProcA"];',
  26090. '$impl.p = $mod.$rtti["TProcB"];',
  26091. '']),
  26092. LinesToStr([ // implementation
  26093. '$mod.$rtti.$ProcVar("TProcB", {',
  26094. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26095. '});',
  26096. '$impl.p = null;',
  26097. '']) );
  26098. end;
  26099. procedure TTestModule.TestRTTI_EnumAndSetType;
  26100. begin
  26101. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26102. StartProgram(false);
  26103. Add('type');
  26104. Add(' TFlag = (light,dark);');
  26105. Add(' TFlags = set of TFlag;');
  26106. Add(' TProc = function(f: TFlags): TFlag;');
  26107. Add('var p: pointer;');
  26108. Add('begin');
  26109. Add(' p:=typeinfo(tflag);');
  26110. Add(' p:=typeinfo(tflags);');
  26111. ConvertProgram;
  26112. CheckSource('TestRTTI_EnumAndType',
  26113. LinesToStr([ // statements
  26114. 'this.TFlag = {',
  26115. ' "0": "light",',
  26116. ' light: 0,',
  26117. ' "1": "dark",',
  26118. ' dark: 1',
  26119. '};',
  26120. '$mod.$rtti.$Enum("TFlag", {',
  26121. ' minvalue: 0,',
  26122. ' maxvalue: 1,',
  26123. ' ordtype: 1,',
  26124. ' enumtype: this.TFlag',
  26125. '});',
  26126. '$mod.$rtti.$Set("TFlags", {',
  26127. ' comptype: $mod.$rtti["TFlag"]',
  26128. '});',
  26129. '$mod.$rtti.$ProcVar("TProc", {',
  26130. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  26131. '});',
  26132. 'this.p = null;',
  26133. '']),
  26134. LinesToStr([ // $mod.$main
  26135. '$mod.p = $mod.$rtti["TFlag"];',
  26136. '$mod.p = $mod.$rtti["TFlags"];',
  26137. '']));
  26138. end;
  26139. procedure TTestModule.TestRTTI_EnumRange;
  26140. begin
  26141. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26142. StartProgram(false);
  26143. Add([
  26144. 'type',
  26145. ' TCol = (red,green,blue);',
  26146. ' TColRg = green..blue;',
  26147. ' TSetOfColRg = set of TColRg;',
  26148. 'var p: pointer;',
  26149. 'begin',
  26150. ' p:=typeinfo(tcolrg);',
  26151. ' p:=typeinfo(tsetofcolrg);',
  26152. '']);
  26153. ConvertProgram;
  26154. end;
  26155. procedure TTestModule.TestRTTI_AnonymousEnumType;
  26156. begin
  26157. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26158. StartProgram(false);
  26159. Add('type');
  26160. Add(' TFlags = set of (red, green);');
  26161. Add('var');
  26162. Add(' f: TFlags;');
  26163. Add('begin');
  26164. Add(' Include(f,red);');
  26165. ConvertProgram;
  26166. CheckSource('TestRTTI_AnonymousEnumType',
  26167. LinesToStr([ // statements
  26168. 'this.TFlags$a = {',
  26169. ' "0": "red",',
  26170. ' red: 0,',
  26171. ' "1": "green",',
  26172. ' green: 1',
  26173. '};',
  26174. '$mod.$rtti.$Enum("TFlags$a", {',
  26175. ' minvalue: 0,',
  26176. ' maxvalue: 1,',
  26177. ' ordtype: 1,',
  26178. ' enumtype: this.TFlags$a',
  26179. '});',
  26180. '$mod.$rtti.$Set("TFlags", {',
  26181. ' comptype: $mod.$rtti["TFlags$a"]',
  26182. '});',
  26183. 'this.f = {};',
  26184. '']),
  26185. LinesToStr([
  26186. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  26187. '']));
  26188. end;
  26189. procedure TTestModule.TestRTTI_StaticArray;
  26190. begin
  26191. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26192. StartProgram(false);
  26193. Add('type');
  26194. Add(' TFlag = (light,dark);');
  26195. Add(' TFlagNames = array[TFlag] of string;');
  26196. Add(' TBoolNames = array[boolean] of string;');
  26197. Add(' TByteArray = array[1..32768] of byte;');
  26198. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  26199. Add('var p: pointer;');
  26200. Add('begin');
  26201. Add(' p:=typeinfo(TFlagNames);');
  26202. Add(' p:=typeinfo(TBoolNames);');
  26203. ConvertProgram;
  26204. CheckSource('TestRTTI_StaticArray',
  26205. LinesToStr([ // statements
  26206. 'this.TFlag = {',
  26207. ' "0": "light",',
  26208. ' light: 0,',
  26209. ' "1": "dark",',
  26210. ' dark: 1',
  26211. '};',
  26212. '$mod.$rtti.$Enum("TFlag", {',
  26213. ' minvalue: 0,',
  26214. ' maxvalue: 1,',
  26215. ' ordtype: 1,',
  26216. ' enumtype: this.TFlag',
  26217. '});',
  26218. '$mod.$rtti.$StaticArray("TFlagNames", {',
  26219. ' dims: [2],',
  26220. ' eltype: rtl.string',
  26221. '});',
  26222. '$mod.$rtti.$StaticArray("TBoolNames", {',
  26223. ' dims: [2],',
  26224. ' eltype: rtl.string',
  26225. '});',
  26226. '$mod.$rtti.$StaticArray("TByteArray", {',
  26227. ' dims: [32768],',
  26228. ' eltype: rtl.byte',
  26229. '});',
  26230. '$mod.$rtti.$ProcVar("TProc", {',
  26231. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  26232. '});',
  26233. 'this.p = null;',
  26234. '']),
  26235. LinesToStr([ // $mod.$main
  26236. '$mod.p = $mod.$rtti["TFlagNames"];',
  26237. '$mod.p = $mod.$rtti["TBoolNames"];',
  26238. '']));
  26239. end;
  26240. procedure TTestModule.TestRTTI_DynArray;
  26241. begin
  26242. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26243. StartProgram(false);
  26244. Add('type');
  26245. Add(' TArrStr = array of string;');
  26246. Add(' TArr2Dim = array of tarrstr;');
  26247. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  26248. Add('var p: pointer;');
  26249. Add('begin');
  26250. Add(' p:=typeinfo(tarrstr);');
  26251. Add(' p:=typeinfo(tarr2dim);');
  26252. ConvertProgram;
  26253. CheckSource('TestRTTI_DynArray',
  26254. LinesToStr([ // statements
  26255. '$mod.$rtti.$DynArray("TArrStr", {',
  26256. ' eltype: rtl.string',
  26257. '});',
  26258. '$mod.$rtti.$DynArray("TArr2Dim", {',
  26259. ' eltype: $mod.$rtti["TArrStr"]',
  26260. '});',
  26261. '$mod.$rtti.$ProcVar("TProc", {',
  26262. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  26263. '});',
  26264. 'this.p = null;',
  26265. '']),
  26266. LinesToStr([ // $mod.$main
  26267. '$mod.p = $mod.$rtti["TArrStr"];',
  26268. '$mod.p = $mod.$rtti["TArr2Dim"];',
  26269. '']));
  26270. end;
  26271. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  26272. begin
  26273. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26274. StartProgram(false);
  26275. Add('type');
  26276. Add(' TArr = array of array of longint;');
  26277. Add('var a: TArr;');
  26278. Add('begin');
  26279. ConvertProgram;
  26280. CheckSource('TestRTTI_ArrayNestedAnonymous',
  26281. LinesToStr([ // statements
  26282. '$mod.$rtti.$DynArray("TArr$a", {',
  26283. ' eltype: rtl.longint',
  26284. '});',
  26285. '$mod.$rtti.$DynArray("TArr", {',
  26286. ' eltype: $mod.$rtti["TArr$a"]',
  26287. '});',
  26288. 'this.a = [];',
  26289. '']),
  26290. LinesToStr([ // $mod.$main
  26291. ]));
  26292. end;
  26293. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  26294. begin
  26295. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26296. StartProgram(false);
  26297. Add('type');
  26298. Add(' TObject = class');
  26299. Add(' published');
  26300. Add(' procedure Proc; virtual; abstract;');
  26301. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  26302. Add(' end;');
  26303. Add('begin');
  26304. SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,19)',
  26305. nDuplicateIdentifier);
  26306. ConvertProgram;
  26307. end;
  26308. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  26309. begin
  26310. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26311. StartProgram(false);
  26312. Add('type');
  26313. Add(' TObject = class');
  26314. Add(' published');
  26315. Add(' procedure Proc; external name ''foo'';');
  26316. Add(' end;');
  26317. Add('begin');
  26318. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26319. nPublishedNameMustMatchExternal);
  26320. ConvertProgram;
  26321. end;
  26322. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  26323. begin
  26324. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26325. StartProgram(false);
  26326. Add('type');
  26327. Add(' TObject = class');
  26328. Add(' class var FA: longint;');
  26329. Add(' published');
  26330. Add(' class property A: longint read FA;');
  26331. Add(' end;');
  26332. Add('begin');
  26333. SetExpectedPasResolverError('Invalid published property modifier "class"',
  26334. nInvalidXModifierY);
  26335. ConvertProgram;
  26336. end;
  26337. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  26338. begin
  26339. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26340. StartProgram(false);
  26341. Add('type');
  26342. Add(' TObject = class');
  26343. Add(' published');
  26344. Add(' class var FA: longint;');
  26345. Add(' end;');
  26346. Add('begin');
  26347. SetExpectedPasResolverError(sSymbolCannotBePublished,
  26348. nSymbolCannotBePublished);
  26349. ConvertProgram;
  26350. end;
  26351. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  26352. begin
  26353. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26354. StartProgram(false);
  26355. Add('{$modeswitch externalclass}');
  26356. Add('type');
  26357. Add(' TObject = class');
  26358. Add(' published');
  26359. Add(' V: longint; external name ''foo'';');
  26360. Add(' end;');
  26361. Add('begin');
  26362. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26363. nPublishedNameMustMatchExternal);
  26364. ConvertProgram;
  26365. end;
  26366. procedure TTestModule.TestRTTI_Class_Field;
  26367. begin
  26368. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26369. StartProgram(false);
  26370. Add('{$modeswitch externalclass}');
  26371. Add('type');
  26372. Add(' TObject = class');
  26373. Add(' private');
  26374. Add(' FPropA: string;');
  26375. Add(' published');
  26376. Add(' VarLI: longint;');
  26377. Add(' VarC: char;');
  26378. Add(' VarS: string;');
  26379. Add(' VarD: double;');
  26380. Add(' VarB: boolean;');
  26381. Add(' VarLW: longword;');
  26382. Add(' VarSmI: smallint;');
  26383. Add(' VarW: word;');
  26384. Add(' VarShI: shortint;');
  26385. Add(' VarBy: byte;');
  26386. Add(' VarExt: longint external name ''VarExt'';');
  26387. Add(' end;');
  26388. Add('var p: pointer;');
  26389. Add(' Obj: tobject;');
  26390. Add('begin');
  26391. Add(' p:=typeinfo(tobject);');
  26392. Add(' p:=typeinfo(p);');
  26393. Add(' p:=typeinfo(obj);');
  26394. ConvertProgram;
  26395. CheckSource('TestRTTI_Class_Field',
  26396. LinesToStr([ // statements
  26397. 'rtl.createClass($mod, "TObject", null, function () {',
  26398. ' this.$init = function () {',
  26399. ' this.FPropA = "";',
  26400. ' this.VarLI = 0;',
  26401. ' this.VarC = "";',
  26402. ' this.VarS = "";',
  26403. ' this.VarD = 0.0;',
  26404. ' this.VarB = false;',
  26405. ' this.VarLW = 0;',
  26406. ' this.VarSmI = 0;',
  26407. ' this.VarW = 0;',
  26408. ' this.VarShI = 0;',
  26409. ' this.VarBy = 0;',
  26410. ' };',
  26411. ' this.$final = function () {',
  26412. ' };',
  26413. ' var $r = this.$rtti;',
  26414. ' $r.addField("VarLI", rtl.longint);',
  26415. ' $r.addField("VarC", rtl.char);',
  26416. ' $r.addField("VarS", rtl.string);',
  26417. ' $r.addField("VarD", rtl.double);',
  26418. ' $r.addField("VarB", rtl.boolean);',
  26419. ' $r.addField("VarLW", rtl.longword);',
  26420. ' $r.addField("VarSmI", rtl.smallint);',
  26421. ' $r.addField("VarW", rtl.word);',
  26422. ' $r.addField("VarShI", rtl.shortint);',
  26423. ' $r.addField("VarBy", rtl.byte);',
  26424. ' $r.addField("VarExt", rtl.longint);',
  26425. '});',
  26426. 'this.p = null;',
  26427. 'this.Obj = null;',
  26428. '']),
  26429. LinesToStr([ // $mod.$main
  26430. '$mod.p = $mod.$rtti["TObject"];',
  26431. '$mod.p = rtl.pointer;',
  26432. '$mod.p = $mod.Obj.$rtti;',
  26433. '']));
  26434. end;
  26435. procedure TTestModule.TestRTTI_Class_Method;
  26436. begin
  26437. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26438. StartProgram(false);
  26439. Add('type');
  26440. Add(' TObject = class');
  26441. Add(' private');
  26442. Add(' procedure Internal; external name ''$intern'';');
  26443. Add(' published');
  26444. Add(' procedure Click; virtual; abstract;');
  26445. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  26446. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  26447. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  26448. Add(' end;');
  26449. Add('begin');
  26450. ConvertProgram;
  26451. CheckSource('TestRTTI_Class_Method',
  26452. LinesToStr([ // statements
  26453. 'rtl.createClass($mod, "TObject", null, function () {',
  26454. ' this.$init = function () {',
  26455. ' };',
  26456. ' this.$final = function () {',
  26457. ' };',
  26458. ' var $r = this.$rtti;',
  26459. ' $r.addMethod("Click", 0, null);',
  26460. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  26461. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  26462. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  26463. ' flags: 2',
  26464. ' });',
  26465. '});',
  26466. '']),
  26467. LinesToStr([ // $mod.$main
  26468. '']));
  26469. end;
  26470. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  26471. begin
  26472. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26473. StartProgram(false);
  26474. Add('type');
  26475. Add(' TObject = class');
  26476. Add(' published');
  26477. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  26478. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  26479. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  26480. Add(' end;');
  26481. Add('begin');
  26482. ConvertProgram;
  26483. CheckSource('TestRTTI_Class_MethodOpenArray',
  26484. LinesToStr([ // statements
  26485. 'rtl.createClass($mod, "TObject", null, function () {',
  26486. ' this.$init = function () {',
  26487. ' };',
  26488. ' this.$final = function () {',
  26489. ' };',
  26490. ' var $r = this.$rtti;',
  26491. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  26492. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  26493. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  26494. '});',
  26495. '']),
  26496. LinesToStr([ // $mod.$main
  26497. '']));
  26498. end;
  26499. procedure TTestModule.TestRTTI_Class_Property;
  26500. begin
  26501. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26502. StartProgram(false);
  26503. Add('{$modeswitch externalclass}');
  26504. Add('type');
  26505. Add(' TObject = class');
  26506. Add(' private');
  26507. Add(' FColor: longint;');
  26508. Add(' FColorStored: boolean;');
  26509. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  26510. Add(' function GetColor: longint; virtual; abstract;');
  26511. Add(' function GetColorStored: boolean; virtual; abstract;');
  26512. Add(' FExtSize: longint external name ''$extSize'';');
  26513. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  26514. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  26515. Add(' function GetExtSize: longint; external name ''$getSize'';');
  26516. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  26517. Add(' published');
  26518. Add(' property ColorA: longint read FColor;');
  26519. Add(' property ColorB: longint write FColor;');
  26520. Add(' property ColorC: longint read GetColor write SetColor;');
  26521. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  26522. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  26523. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  26524. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  26525. Add(' end;');
  26526. Add('begin');
  26527. ConvertProgram;
  26528. CheckSource('TestRTTI_Class_Property',
  26529. LinesToStr([ // statements
  26530. 'rtl.createClass($mod, "TObject", null, function () {',
  26531. ' this.$init = function () {',
  26532. ' this.FColor = 0;',
  26533. ' this.FColorStored = false;',
  26534. ' };',
  26535. ' this.$final = function () {',
  26536. ' };',
  26537. ' var $r = this.$rtti;',
  26538. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  26539. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  26540. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  26541. ' $r.addProperty(',
  26542. ' "ColorD",',
  26543. ' 8,',
  26544. ' rtl.longint,',
  26545. ' "FColor",',
  26546. ' "FColor",',
  26547. ' {',
  26548. ' stored: "FColorStored"',
  26549. ' }',
  26550. ' );',
  26551. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  26552. ' $r.addProperty(',
  26553. ' "ExtSizeB",',
  26554. ' 11,',
  26555. ' rtl.longint,',
  26556. ' "$getSize",',
  26557. ' "$setSize",',
  26558. ' {',
  26559. ' stored: "$extSizeStored"',
  26560. ' }',
  26561. ' );',
  26562. ' $r.addProperty(',
  26563. ' "ExtSizeC",',
  26564. ' 12,',
  26565. ' rtl.longint,',
  26566. ' "$extSize",',
  26567. ' "$extSize",',
  26568. ' {',
  26569. ' stored: "$getExtSizeStored"',
  26570. ' }',
  26571. ' );',
  26572. '});',
  26573. '']),
  26574. LinesToStr([ // $mod.$main
  26575. '']));
  26576. end;
  26577. procedure TTestModule.TestRTTI_Class_PropertyParams;
  26578. begin
  26579. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26580. StartProgram(false);
  26581. Add('{$modeswitch externalclass}');
  26582. Add('type');
  26583. Add(' integer = longint;');
  26584. Add(' TObject = class');
  26585. Add(' private');
  26586. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  26587. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  26588. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  26589. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  26590. Add(' published');
  26591. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  26592. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  26593. Add(' end;');
  26594. Add('begin');
  26595. ConvertProgram;
  26596. CheckSource('TestRTTI_Class_PropertyParams',
  26597. LinesToStr([ // statements
  26598. 'rtl.createClass($mod, "TObject", null, function () {',
  26599. ' this.$init = function () {',
  26600. ' };',
  26601. ' this.$final = function () {',
  26602. ' };',
  26603. ' var $r = this.$rtti;',
  26604. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  26605. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  26606. '});',
  26607. '']),
  26608. LinesToStr([ // $mod.$main
  26609. '']));
  26610. end;
  26611. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  26612. begin
  26613. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26614. AddModuleWithIntfImplSrc('unit1.pas',
  26615. 'type TColor = -5..5;',
  26616. '');
  26617. StartProgram(true);
  26618. Add([
  26619. 'uses unit1;',
  26620. 'type',
  26621. ' TColorAlias = TColor;',
  26622. ' TColorTypeAlias = type TColor;',
  26623. ' TObject = class',
  26624. ' private',
  26625. ' fColor: TColor;',
  26626. ' fAlias: TColorAlias;',
  26627. ' fTypeAlias: TColorTypeAlias;',
  26628. ' published',
  26629. ' property Color: TColor read fcolor;',
  26630. ' property Alias: TColorAlias read falias;',
  26631. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  26632. ' end;',
  26633. 'begin',
  26634. '']);
  26635. ConvertProgram;
  26636. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  26637. LinesToStr([ // statements
  26638. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  26639. 'rtl.createClass($mod, "TObject", null, function () {',
  26640. ' this.$init = function () {',
  26641. ' this.fColor = 0;',
  26642. ' this.fAlias = 0;',
  26643. ' this.fTypeAlias = 0;',
  26644. ' };',
  26645. ' this.$final = function () {',
  26646. ' };',
  26647. ' var $r = this.$rtti;',
  26648. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  26649. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  26650. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  26651. '});',
  26652. '']),
  26653. LinesToStr([ // $mod.$main
  26654. '']));
  26655. end;
  26656. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  26657. begin
  26658. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26659. StartProgram(false);
  26660. Add([
  26661. '{$modeswitch omitrtti}',
  26662. 'type',
  26663. ' TObject = class',
  26664. ' private',
  26665. ' FA: byte;',
  26666. ' published',
  26667. ' property A: byte read FA write FA;',
  26668. ' end;',
  26669. 'begin']);
  26670. ConvertProgram;
  26671. CheckSource('TestRTTI_Class_OmitRTTI',
  26672. LinesToStr([ // statements
  26673. 'rtl.createClass($mod, "TObject", null, function () {',
  26674. ' this.$init = function () {',
  26675. ' this.FA = 0;',
  26676. ' };',
  26677. ' this.$final = function () {',
  26678. ' };',
  26679. '});',
  26680. '']),
  26681. LinesToStr([ // $mod.$main
  26682. '']));
  26683. end;
  26684. procedure TTestModule.TestRTTI_IndexModifier;
  26685. begin
  26686. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26687. StartProgram(false);
  26688. Add([
  26689. 'type',
  26690. ' TEnum = (red, blue);',
  26691. ' TObject = class',
  26692. ' FB: boolean;',
  26693. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  26694. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  26695. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  26696. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  26697. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  26698. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  26699. ' published',
  26700. ' property B1: boolean index 1 read FB write SetIntBool;',
  26701. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  26702. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  26703. ' end;',
  26704. 'begin']);
  26705. ConvertProgram;
  26706. CheckSource('TestRTTI_IndexModifier',
  26707. LinesToStr([ // statements
  26708. 'this.TEnum = {',
  26709. ' "0": "red",',
  26710. ' red: 0,',
  26711. ' "1": "blue",',
  26712. ' blue: 1',
  26713. '};',
  26714. '$mod.$rtti.$Enum("TEnum", {',
  26715. ' minvalue: 0,',
  26716. ' maxvalue: 1,',
  26717. ' ordtype: 1,',
  26718. ' enumtype: this.TEnum',
  26719. '});',
  26720. 'rtl.createClass($mod, "TObject", null, function () {',
  26721. ' this.$init = function () {',
  26722. ' this.FB = false;',
  26723. ' };',
  26724. ' this.$final = function () {',
  26725. ' };',
  26726. ' var $r = this.$rtti;',
  26727. ' $r.addProperty(',
  26728. ' "B1",',
  26729. ' 18,',
  26730. ' rtl.boolean,',
  26731. ' "FB",',
  26732. ' "SetIntBool",',
  26733. ' {',
  26734. ' index: 1',
  26735. ' }',
  26736. ' );',
  26737. ' $r.addProperty(',
  26738. ' "B2",',
  26739. ' 17,',
  26740. ' rtl.boolean,',
  26741. ' "GetEnumBool",',
  26742. ' "FB",',
  26743. ' {',
  26744. ' index: $mod.TEnum.blue',
  26745. ' }',
  26746. ' );',
  26747. ' $r.addProperty(',
  26748. ' "I1",',
  26749. ' 19,',
  26750. ' rtl.boolean,',
  26751. ' "GetStrIntBool",',
  26752. ' "SetStrIntBool",',
  26753. ' {',
  26754. ' index: 2',
  26755. ' }',
  26756. ' );',
  26757. '});',
  26758. '']),
  26759. LinesToStr([ // $mod.$main
  26760. '']));
  26761. end;
  26762. procedure TTestModule.TestRTTI_StoredModifier;
  26763. begin
  26764. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26765. StartProgram(false);
  26766. Add([
  26767. 'const',
  26768. ' ConstB = true;',
  26769. 'type',
  26770. ' TObject = class',
  26771. ' private',
  26772. ' FB: boolean;',
  26773. ' function IsBStored: boolean; virtual; abstract;',
  26774. ' published',
  26775. ' property BoolA: boolean read FB stored true;',
  26776. ' property BoolB: boolean read FB stored false;',
  26777. ' property BoolC: boolean read FB stored FB;',
  26778. ' property BoolD: boolean read FB stored ConstB;',
  26779. ' property BoolE: boolean read FB stored IsBStored;',
  26780. ' end;',
  26781. 'begin']);
  26782. ConvertProgram;
  26783. CheckSource('TestRTTI_StoredModifier',
  26784. LinesToStr([ // statements
  26785. 'this.ConstB = true;',
  26786. 'rtl.createClass($mod, "TObject", null, function () {',
  26787. ' this.$init = function () {',
  26788. ' this.FB = false;',
  26789. ' };',
  26790. ' this.$final = function () {',
  26791. ' };',
  26792. ' var $r = this.$rtti;',
  26793. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  26794. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  26795. ' $r.addProperty(',
  26796. ' "BoolC",',
  26797. ' 8,',
  26798. ' rtl.boolean,',
  26799. ' "FB",',
  26800. ' "",',
  26801. ' {',
  26802. ' stored: "FB"',
  26803. ' }',
  26804. ' );',
  26805. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  26806. ' $r.addProperty(',
  26807. ' "BoolE",',
  26808. ' 12,',
  26809. ' rtl.boolean,',
  26810. ' "FB",',
  26811. ' "",',
  26812. ' {',
  26813. ' stored: "IsBStored"',
  26814. ' }',
  26815. ' );',
  26816. '});',
  26817. '']),
  26818. LinesToStr([ // $mod.$main
  26819. '']));
  26820. end;
  26821. procedure TTestModule.TestRTTI_DefaultValue;
  26822. begin
  26823. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26824. StartProgram(false);
  26825. Add([
  26826. 'type',
  26827. ' TEnum = (red, blue);',
  26828. 'const',
  26829. ' CB = true or false;',
  26830. ' CI = 1+2;',
  26831. 'type',
  26832. ' TObject = class',
  26833. ' FB: boolean;',
  26834. ' FI: longint;',
  26835. ' FE: TEnum;',
  26836. ' published',
  26837. ' property B1: boolean read FB default true;',
  26838. ' property B2: boolean read FB default CB;',
  26839. ' property B3: boolean read FB default test1.cb;',
  26840. ' property I1: longint read FI default 2;',
  26841. ' property I2: longint read FI default CI;',
  26842. ' property E1: TEnum read FE default red;',
  26843. ' property E2: TEnum read FE default TEnum.blue;',
  26844. ' end;',
  26845. 'begin']);
  26846. ConvertProgram;
  26847. CheckSource('TestRTTI_DefaultValue',
  26848. LinesToStr([ // statements
  26849. 'this.TEnum = {',
  26850. ' "0": "red",',
  26851. ' red: 0,',
  26852. ' "1": "blue",',
  26853. ' blue: 1',
  26854. '};',
  26855. '$mod.$rtti.$Enum("TEnum", {',
  26856. ' minvalue: 0,',
  26857. ' maxvalue: 1,',
  26858. ' ordtype: 1,',
  26859. ' enumtype: this.TEnum',
  26860. '});',
  26861. 'this.CB = true || false;',
  26862. 'this.CI = 1 + 2;',
  26863. 'rtl.createClass($mod, "TObject", null, function () {',
  26864. ' this.$init = function () {',
  26865. ' this.FB = false;',
  26866. ' this.FI = 0;',
  26867. ' this.FE = 0;',
  26868. ' };',
  26869. ' this.$final = function () {',
  26870. ' };',
  26871. ' var $r = this.$rtti;',
  26872. ' $r.addProperty(',
  26873. ' "B1",',
  26874. ' 0,',
  26875. ' rtl.boolean,',
  26876. ' "FB",',
  26877. ' "",',
  26878. ' {',
  26879. ' Default: true',
  26880. ' }',
  26881. ' );',
  26882. ' $r.addProperty(',
  26883. ' "B2",',
  26884. ' 0,',
  26885. ' rtl.boolean,',
  26886. ' "FB",',
  26887. ' "",',
  26888. ' {',
  26889. ' Default: true',
  26890. ' }',
  26891. ' );',
  26892. ' $r.addProperty(',
  26893. ' "B3",',
  26894. ' 0,',
  26895. ' rtl.boolean,',
  26896. ' "FB",',
  26897. ' "",',
  26898. ' {',
  26899. ' Default: true',
  26900. ' }',
  26901. ' );',
  26902. ' $r.addProperty(',
  26903. ' "I1",',
  26904. ' 0,',
  26905. ' rtl.longint,',
  26906. ' "FI",',
  26907. ' "",',
  26908. ' {',
  26909. ' Default: 2',
  26910. ' }',
  26911. ' );',
  26912. ' $r.addProperty(',
  26913. ' "I2",',
  26914. ' 0,',
  26915. ' rtl.longint,',
  26916. ' "FI",',
  26917. ' "",',
  26918. ' {',
  26919. ' Default: 3',
  26920. ' }',
  26921. ' );',
  26922. ' $r.addProperty(',
  26923. ' "E1",',
  26924. ' 0,',
  26925. ' $mod.$rtti["TEnum"],',
  26926. ' "FE",',
  26927. ' "",',
  26928. ' {',
  26929. ' Default: $mod.TEnum.red',
  26930. ' }',
  26931. ' );',
  26932. ' $r.addProperty(',
  26933. ' "E2",',
  26934. ' 0,',
  26935. ' $mod.$rtti["TEnum"],',
  26936. ' "FE",',
  26937. ' "",',
  26938. ' {',
  26939. ' Default: $mod.TEnum.blue',
  26940. ' }',
  26941. ' );',
  26942. '});',
  26943. '']),
  26944. LinesToStr([ // $mod.$main
  26945. '']));
  26946. end;
  26947. procedure TTestModule.TestRTTI_DefaultValueSet;
  26948. begin
  26949. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26950. StartProgram(false);
  26951. Add([
  26952. 'type',
  26953. ' TEnum = (red, blue);',
  26954. ' TSet = set of TEnum;',
  26955. 'const',
  26956. ' CSet = [red,blue];',
  26957. 'type',
  26958. ' TObject = class',
  26959. ' FSet: TSet;',
  26960. ' published',
  26961. ' property Set1: TSet read FSet default [];',
  26962. ' property Set2: TSet read FSet default [red];',
  26963. ' property Set3: TSet read FSet default [red,blue];',
  26964. ' property Set4: TSet read FSet default CSet;',
  26965. ' end;',
  26966. 'begin']);
  26967. ConvertProgram;
  26968. CheckSource('TestRTTI_DefaultValueSet',
  26969. LinesToStr([ // statements
  26970. 'this.TEnum = {',
  26971. ' "0": "red",',
  26972. ' red: 0,',
  26973. ' "1": "blue",',
  26974. ' blue: 1',
  26975. '};',
  26976. '$mod.$rtti.$Enum("TEnum", {',
  26977. ' minvalue: 0,',
  26978. ' maxvalue: 1,',
  26979. ' ordtype: 1,',
  26980. ' enumtype: this.TEnum',
  26981. '});',
  26982. '$mod.$rtti.$Set("TSet", {',
  26983. ' comptype: $mod.$rtti["TEnum"]',
  26984. '});',
  26985. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  26986. 'rtl.createClass($mod, "TObject", null, function () {',
  26987. ' this.$init = function () {',
  26988. ' this.FSet = {};',
  26989. ' };',
  26990. ' this.$final = function () {',
  26991. ' this.FSet = undefined;',
  26992. ' };',
  26993. ' var $r = this.$rtti;',
  26994. ' $r.addProperty(',
  26995. ' "Set1",',
  26996. ' 0,',
  26997. ' $mod.$rtti["TSet"],',
  26998. ' "FSet",',
  26999. ' "",',
  27000. ' {',
  27001. ' Default: {}',
  27002. ' }',
  27003. ' );',
  27004. ' $r.addProperty(',
  27005. ' "Set2",',
  27006. ' 0,',
  27007. ' $mod.$rtti["TSet"],',
  27008. ' "FSet",',
  27009. ' "",',
  27010. ' {',
  27011. ' Default: rtl.createSet($mod.TEnum.red)',
  27012. ' }',
  27013. ' );',
  27014. ' $r.addProperty(',
  27015. ' "Set3",',
  27016. ' 0,',
  27017. ' $mod.$rtti["TSet"],',
  27018. ' "FSet",',
  27019. ' "",',
  27020. ' {',
  27021. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  27022. ' }',
  27023. ' );',
  27024. ' $r.addProperty(',
  27025. ' "Set4",',
  27026. ' 0,',
  27027. ' $mod.$rtti["TSet"],',
  27028. ' "FSet",',
  27029. ' "",',
  27030. ' {',
  27031. ' Default: $mod.CSet',
  27032. ' }',
  27033. ' );',
  27034. '});',
  27035. '']),
  27036. LinesToStr([ // $mod.$main
  27037. '']));
  27038. end;
  27039. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  27040. begin
  27041. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27042. StartProgram(false);
  27043. Add([
  27044. 'type',
  27045. ' TRg = -1..1;',
  27046. 'const',
  27047. ' l = low(TRg);',
  27048. ' h = high(TRg);',
  27049. 'type',
  27050. ' TObject = class',
  27051. ' FV: TRg;',
  27052. ' published',
  27053. ' property V1: TRg read FV default -1;',
  27054. ' end;',
  27055. 'begin']);
  27056. ConvertProgram;
  27057. CheckSource('TestRTTI_DefaultValueRangeType',
  27058. LinesToStr([ // statements
  27059. '$mod.$rtti.$Int("TRg", {',
  27060. ' minvalue: -1,',
  27061. ' maxvalue: 1,',
  27062. ' ordtype: 0',
  27063. '});',
  27064. 'this.l = -1;',
  27065. 'this.h = 1;',
  27066. 'rtl.createClass($mod, "TObject", null, function () {',
  27067. ' this.$init = function () {',
  27068. ' this.FV = 0;',
  27069. ' };',
  27070. ' this.$final = function () {',
  27071. ' };',
  27072. ' var $r = this.$rtti;',
  27073. ' $r.addProperty(',
  27074. ' "V1",',
  27075. ' 0,',
  27076. ' $mod.$rtti["TRg"],',
  27077. ' "FV",',
  27078. ' "",',
  27079. ' {',
  27080. ' Default: -1',
  27081. ' }',
  27082. ' );',
  27083. '});',
  27084. '']),
  27085. LinesToStr([ // $mod.$main
  27086. '']));
  27087. end;
  27088. procedure TTestModule.TestRTTI_DefaultValueInherit;
  27089. begin
  27090. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27091. StartProgram(false);
  27092. Add([
  27093. 'type',
  27094. ' TObject = class',
  27095. ' FA, FB: byte;',
  27096. ' property A: byte read FA default 1;',
  27097. ' property B: byte read FB default 2;',
  27098. ' end;',
  27099. ' TBird = class',
  27100. ' published',
  27101. ' property A;',
  27102. ' property B nodefault;',
  27103. ' end;',
  27104. 'begin']);
  27105. ConvertProgram;
  27106. CheckSource('TestRTTI_DefaultValueInherit',
  27107. LinesToStr([ // statements
  27108. 'rtl.createClass($mod, "TObject", null, function () {',
  27109. ' this.$init = function () {',
  27110. ' this.FA = 0;',
  27111. ' this.FB = 0;',
  27112. ' };',
  27113. ' this.$final = function () {',
  27114. ' };',
  27115. '});',
  27116. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27117. ' var $r = this.$rtti;',
  27118. ' $r.addProperty(',
  27119. ' "A",',
  27120. ' 0,',
  27121. ' rtl.byte,',
  27122. ' "FA",',
  27123. ' "",',
  27124. ' {',
  27125. ' Default: 1',
  27126. ' }',
  27127. ' );',
  27128. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  27129. '});',
  27130. '']),
  27131. LinesToStr([ // $mod.$main
  27132. '']));
  27133. end;
  27134. procedure TTestModule.TestRTTI_OverrideMethod;
  27135. begin
  27136. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27137. StartProgram(false);
  27138. Add('type');
  27139. Add(' TObject = class');
  27140. Add(' published');
  27141. Add(' procedure DoIt; virtual; abstract;');
  27142. Add(' end;');
  27143. Add(' TSky = class');
  27144. Add(' published');
  27145. Add(' procedure DoIt; override;');
  27146. Add(' end;');
  27147. Add('procedure TSky.DoIt; begin end;');
  27148. Add('begin');
  27149. ConvertProgram;
  27150. CheckSource('TestRTTI_OverrideMethod',
  27151. LinesToStr([ // statements
  27152. 'rtl.createClass($mod, "TObject", null, function () {',
  27153. ' this.$init = function () {',
  27154. ' };',
  27155. ' this.$final = function () {',
  27156. ' };',
  27157. ' var $r = this.$rtti;',
  27158. ' $r.addMethod("DoIt", 0, null);',
  27159. '});',
  27160. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27161. ' this.DoIt = function () {',
  27162. ' };',
  27163. '});',
  27164. '']),
  27165. LinesToStr([ // $mod.$main
  27166. '']));
  27167. end;
  27168. procedure TTestModule.TestRTTI_OverloadProperty;
  27169. begin
  27170. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27171. StartProgram(false);
  27172. Add('type');
  27173. Add(' TObject = class');
  27174. Add(' protected');
  27175. Add(' FFlag: longint;');
  27176. Add(' published');
  27177. Add(' property Flag: longint read fflag;');
  27178. Add(' end;');
  27179. Add(' TSky = class');
  27180. Add(' published');
  27181. Add(' property FLAG: longint write fflag;');
  27182. Add(' end;');
  27183. Add('begin');
  27184. ConvertProgram;
  27185. CheckSource('TestRTTI_OverrideMethod',
  27186. LinesToStr([ // statements
  27187. 'rtl.createClass($mod, "TObject", null, function () {',
  27188. ' this.$init = function () {',
  27189. ' this.FFlag = 0;',
  27190. ' };',
  27191. ' this.$final = function () {',
  27192. ' };',
  27193. ' var $r = this.$rtti;',
  27194. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  27195. '});',
  27196. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27197. ' var $r = this.$rtti;',
  27198. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  27199. '});',
  27200. '']),
  27201. LinesToStr([ // $mod.$main
  27202. '']));
  27203. end;
  27204. procedure TTestModule.TestRTTI_ClassForward;
  27205. begin
  27206. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27207. StartProgram(false);
  27208. Add('type');
  27209. Add(' TObject = class end;');
  27210. Add(' tbridge = class;');
  27211. Add(' TProc = function: tbridge;');
  27212. Add(' TOger = class');
  27213. Add(' published');
  27214. Add(' FBridge: tbridge;');
  27215. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  27216. Add(' property Bridge: tbridge read fbridge write setbridge;');
  27217. Add(' end;');
  27218. Add(' TBridge = class');
  27219. Add(' FOger: toger;');
  27220. Add(' end;');
  27221. Add('var p: Pointer;');
  27222. Add(' b: tbridge;');
  27223. Add('begin');
  27224. Add(' p:=typeinfo(tbridge);');
  27225. Add(' p:=typeinfo(b);');
  27226. ConvertProgram;
  27227. CheckSource('TestRTTI_ClassForward',
  27228. LinesToStr([ // statements
  27229. 'rtl.createClass($mod, "TObject", null, function () {',
  27230. ' this.$init = function () {',
  27231. ' };',
  27232. ' this.$final = function () {',
  27233. ' };',
  27234. '});',
  27235. '$mod.$rtti.$Class("TBridge");',
  27236. '$mod.$rtti.$ProcVar("TProc", {',
  27237. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  27238. '});',
  27239. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  27240. ' this.$init = function () {',
  27241. ' $mod.TObject.$init.call(this);',
  27242. ' this.FBridge = null;',
  27243. ' };',
  27244. ' this.$final = function () {',
  27245. ' this.FBridge = undefined;',
  27246. ' $mod.TObject.$final.call(this);',
  27247. ' };',
  27248. ' var $r = this.$rtti;',
  27249. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  27250. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  27251. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  27252. '});',
  27253. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  27254. ' this.$init = function () {',
  27255. ' $mod.TObject.$init.call(this);',
  27256. ' this.FOger = null;',
  27257. ' };',
  27258. ' this.$final = function () {',
  27259. ' this.FOger = undefined;',
  27260. ' $mod.TObject.$final.call(this);',
  27261. ' };',
  27262. '});',
  27263. 'this.p = null;',
  27264. 'this.b = null;',
  27265. '']),
  27266. LinesToStr([ // $mod.$main
  27267. '$mod.p = $mod.$rtti["TBridge"];',
  27268. '$mod.p = $mod.b.$rtti;',
  27269. '']));
  27270. end;
  27271. procedure TTestModule.TestRTTI_ClassOf;
  27272. begin
  27273. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27274. StartProgram(false);
  27275. Add('type');
  27276. Add(' TClass = class of tobject;');
  27277. Add(' TProcA = function: TClass;');
  27278. Add(' TObject = class');
  27279. Add(' published');
  27280. Add(' C: tclass;');
  27281. Add(' end;');
  27282. Add(' tfox = class;');
  27283. Add(' TBird = class end;');
  27284. Add(' TBirds = class of tbird;');
  27285. Add(' TFox = class end;');
  27286. Add(' TFoxes = class of tfox;');
  27287. Add(' TCows = class of TCow;');
  27288. Add(' TCow = class;');
  27289. Add(' TCow = class end;');
  27290. Add('begin');
  27291. ConvertProgram;
  27292. CheckSource('TestRTTI_ClassOf',
  27293. LinesToStr([ // statements
  27294. '$mod.$rtti.$Class("TObject");',
  27295. '$mod.$rtti.$ClassRef("TClass", {',
  27296. ' instancetype: $mod.$rtti["TObject"]',
  27297. '});',
  27298. '$mod.$rtti.$ProcVar("TProcA", {',
  27299. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  27300. '});',
  27301. 'rtl.createClass($mod, "TObject", null, function () {',
  27302. ' this.$init = function () {',
  27303. ' this.C = null;',
  27304. ' };',
  27305. ' this.$final = function () {',
  27306. ' this.C = undefined;',
  27307. ' };',
  27308. ' var $r = this.$rtti;',
  27309. ' $r.addField("C", $mod.$rtti["TClass"]);',
  27310. '});',
  27311. '$mod.$rtti.$Class("TFox");',
  27312. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27313. '});',
  27314. '$mod.$rtti.$ClassRef("TBirds", {',
  27315. ' instancetype: $mod.$rtti["TBird"]',
  27316. '});',
  27317. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  27318. '});',
  27319. '$mod.$rtti.$ClassRef("TFoxes", {',
  27320. ' instancetype: $mod.$rtti["TFox"]',
  27321. '});',
  27322. '$mod.$rtti.$Class("TCow");',
  27323. '$mod.$rtti.$ClassRef("TCows", {',
  27324. ' instancetype: $mod.$rtti["TCow"]',
  27325. '});',
  27326. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  27327. '});',
  27328. '']),
  27329. LinesToStr([ // $mod.$main
  27330. '']));
  27331. end;
  27332. procedure TTestModule.TestRTTI_Record;
  27333. begin
  27334. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27335. StartProgram(false);
  27336. Add('type');
  27337. Add(' integer = longint;');
  27338. Add(' TPoint = record');
  27339. Add(' x,y: integer;');
  27340. Add(' end;');
  27341. Add('var p: pointer;');
  27342. Add(' r: tpoint;');
  27343. Add('begin');
  27344. Add(' p:=typeinfo(tpoint);');
  27345. Add(' p:=typeinfo(r);');
  27346. Add(' p:=typeinfo(r.x);');
  27347. ConvertProgram;
  27348. CheckSource('TestRTTI_Record',
  27349. LinesToStr([ // statements
  27350. 'rtl.recNewT($mod, "TPoint", function () {',
  27351. ' this.x = 0;',
  27352. ' this.y = 0;',
  27353. ' this.$eq = function (b) {',
  27354. ' return (this.x === b.x) && (this.y === b.y);',
  27355. ' };',
  27356. ' this.$assign = function (s) {',
  27357. ' this.x = s.x;',
  27358. ' this.y = s.y;',
  27359. ' return this;',
  27360. ' };',
  27361. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  27362. ' $r.addField("x", rtl.longint);',
  27363. ' $r.addField("y", rtl.longint);',
  27364. '});',
  27365. 'this.p = null;',
  27366. 'this.r = $mod.TPoint.$new();',
  27367. '']),
  27368. LinesToStr([ // $mod.$main
  27369. '$mod.p = $mod.$rtti["TPoint"];',
  27370. '$mod.p = $mod.$rtti["TPoint"];',
  27371. '$mod.p = rtl.longint;',
  27372. '']));
  27373. end;
  27374. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  27375. begin
  27376. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27377. StartProgram(false);
  27378. Add('type');
  27379. Add(' TFloatRec = record');
  27380. Add(' d: array of char;');
  27381. // Add(' i: array of array of longint;');
  27382. Add(' end;');
  27383. Add('var p: pointer;');
  27384. Add(' r: tfloatrec;');
  27385. Add('begin');
  27386. Add(' p:=typeinfo(tfloatrec);');
  27387. Add(' p:=typeinfo(r);');
  27388. Add(' p:=typeinfo(r.d);');
  27389. ConvertProgram;
  27390. CheckSource('TestRTTI_Record',
  27391. LinesToStr([ // statements
  27392. 'rtl.recNewT($mod, "TFloatRec", function () {',
  27393. ' this.d = [];',
  27394. ' this.$eq = function (b) {',
  27395. ' return this.d === b.d;',
  27396. ' };',
  27397. ' this.$assign = function (s) {',
  27398. ' this.d = s.d;',
  27399. ' return this;',
  27400. ' };',
  27401. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  27402. ' eltype: rtl.char',
  27403. ' });',
  27404. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  27405. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  27406. '});',
  27407. 'this.p = null;',
  27408. 'this.r = $mod.TFloatRec.$new();',
  27409. '']),
  27410. LinesToStr([ // $mod.$main
  27411. '$mod.p = $mod.$rtti["TFloatRec"];',
  27412. '$mod.p = $mod.$rtti["TFloatRec"];',
  27413. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  27414. '']));
  27415. end;
  27416. procedure TTestModule.TestRTTI_LocalTypes;
  27417. begin
  27418. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27419. StartProgram(false);
  27420. Add([
  27421. 'procedure DoIt;',
  27422. 'type',
  27423. ' integer = longint;',
  27424. ' TPoint = record',
  27425. ' x,y: integer;',
  27426. ' end;',
  27427. 'var p: TPoint;',
  27428. 'begin',
  27429. 'end;',
  27430. 'begin']);
  27431. ConvertProgram;
  27432. CheckSource('TestRTTI_LocalTypes',
  27433. LinesToStr([ // statements
  27434. 'var TPoint = rtl.recNewT(null, "", function () {',
  27435. ' this.x = 0;',
  27436. ' this.y = 0;',
  27437. ' this.$eq = function (b) {',
  27438. ' return (this.x === b.x) && (this.y === b.y);',
  27439. ' };',
  27440. ' this.$assign = function (s) {',
  27441. ' this.x = s.x;',
  27442. ' this.y = s.y;',
  27443. ' return this;',
  27444. ' };',
  27445. '});',
  27446. 'this.DoIt = function () {',
  27447. ' var p = TPoint.$new();',
  27448. '};',
  27449. '']),
  27450. LinesToStr([ // $mod.$main
  27451. '']));
  27452. end;
  27453. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  27454. begin
  27455. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27456. StartProgram(false);
  27457. Add([
  27458. 'type',
  27459. ' TCaption = string;',
  27460. ' TYesNo = boolean;',
  27461. ' TLetter = char;',
  27462. ' TFloat = double;',
  27463. ' TPtr = pointer;',
  27464. ' TShortInt = shortint;',
  27465. ' TByte = byte;',
  27466. ' TSmallInt = smallint;',
  27467. ' TWord = word;',
  27468. ' TInt32 = longint;',
  27469. ' TDWord = longword;',
  27470. ' TValue = jsvalue;',
  27471. 'var p: TPtr;',
  27472. 'begin',
  27473. ' p:=typeinfo(string);',
  27474. ' p:=typeinfo(tcaption);',
  27475. ' p:=typeinfo(boolean);',
  27476. ' p:=typeinfo(tyesno);',
  27477. ' p:=typeinfo(char);',
  27478. ' p:=typeinfo(tletter);',
  27479. ' p:=typeinfo(double);',
  27480. ' p:=typeinfo(tfloat);',
  27481. ' p:=typeinfo(pointer);',
  27482. ' p:=typeinfo(tptr);',
  27483. ' p:=typeinfo(shortint);',
  27484. ' p:=typeinfo(tshortint);',
  27485. ' p:=typeinfo(byte);',
  27486. ' p:=typeinfo(tbyte);',
  27487. ' p:=typeinfo(smallint);',
  27488. ' p:=typeinfo(tsmallint);',
  27489. ' p:=typeinfo(word);',
  27490. ' p:=typeinfo(tword);',
  27491. ' p:=typeinfo(longword);',
  27492. ' p:=typeinfo(tdword);',
  27493. ' p:=typeinfo(jsvalue);',
  27494. ' p:=typeinfo(tvalue);',
  27495. '']);
  27496. ConvertProgram;
  27497. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  27498. LinesToStr([ // statements
  27499. 'this.p = null;',
  27500. '']),
  27501. LinesToStr([ // $mod.$main
  27502. '$mod.p = rtl.string;',
  27503. '$mod.p = rtl.string;',
  27504. '$mod.p = rtl.boolean;',
  27505. '$mod.p = rtl.boolean;',
  27506. '$mod.p = rtl.char;',
  27507. '$mod.p = rtl.char;',
  27508. '$mod.p = rtl.double;',
  27509. '$mod.p = rtl.double;',
  27510. '$mod.p = rtl.pointer;',
  27511. '$mod.p = rtl.pointer;',
  27512. '$mod.p = rtl.shortint;',
  27513. '$mod.p = rtl.shortint;',
  27514. '$mod.p = rtl.byte;',
  27515. '$mod.p = rtl.byte;',
  27516. '$mod.p = rtl.smallint;',
  27517. '$mod.p = rtl.smallint;',
  27518. '$mod.p = rtl.word;',
  27519. '$mod.p = rtl.word;',
  27520. '$mod.p = rtl.longword;',
  27521. '$mod.p = rtl.longword;',
  27522. '$mod.p = rtl.jsvalue;',
  27523. '$mod.p = rtl.jsvalue;',
  27524. '']));
  27525. end;
  27526. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  27527. begin
  27528. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27529. StartProgram(false);
  27530. Add([
  27531. 'type',
  27532. ' TCaption = type string;',
  27533. ' TYesNo = type boolean;',
  27534. ' TLetter = type char;',
  27535. ' TFloat = type double;',
  27536. ' TPtr = type pointer;',
  27537. ' TShortInt = type shortint;',
  27538. ' TByte = type byte;',
  27539. ' TSmallInt = type smallint;',
  27540. ' TWord = type word;',
  27541. ' TInt32 = type longint;',
  27542. ' TDWord = type longword;',
  27543. ' TValue = type jsvalue;',
  27544. ' TAliasValue = type TValue;',
  27545. 'var',
  27546. ' p: TPtr;',
  27547. ' a: TAliasValue;',
  27548. 'begin',
  27549. ' p:=typeinfo(tcaption);',
  27550. ' p:=typeinfo(tyesno);',
  27551. ' p:=typeinfo(tletter);',
  27552. ' p:=typeinfo(tfloat);',
  27553. ' p:=typeinfo(tptr);',
  27554. ' p:=typeinfo(tshortint);',
  27555. ' p:=typeinfo(tbyte);',
  27556. ' p:=typeinfo(tsmallint);',
  27557. ' p:=typeinfo(tword);',
  27558. ' p:=typeinfo(tdword);',
  27559. ' p:=typeinfo(tvalue);',
  27560. ' p:=typeinfo(taliasvalue);',
  27561. ' p:=typeinfo(a);',
  27562. '']);
  27563. ConvertProgram;
  27564. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  27565. LinesToStr([ // statements
  27566. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  27567. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  27568. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  27569. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  27570. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  27571. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  27572. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  27573. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  27574. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  27575. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  27576. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  27577. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  27578. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  27579. 'this.p = null;',
  27580. 'this.a = undefined;',
  27581. '']),
  27582. LinesToStr([ // $mod.$main
  27583. '$mod.p = $mod.$rtti["TCaption"];',
  27584. '$mod.p = $mod.$rtti["TYesNo"];',
  27585. '$mod.p = $mod.$rtti["TLetter"];',
  27586. '$mod.p = $mod.$rtti["TFloat"];',
  27587. '$mod.p = $mod.$rtti["TPtr"];',
  27588. '$mod.p = $mod.$rtti["TShortInt"];',
  27589. '$mod.p = $mod.$rtti["TByte"];',
  27590. '$mod.p = $mod.$rtti["TSmallInt"];',
  27591. '$mod.p = $mod.$rtti["TWord"];',
  27592. '$mod.p = $mod.$rtti["TDWord"];',
  27593. '$mod.p = $mod.$rtti["TValue"];',
  27594. '$mod.p = $mod.$rtti["TAliasValue"];',
  27595. '$mod.p = $mod.$rtti["TAliasValue"];',
  27596. '']));
  27597. end;
  27598. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  27599. begin
  27600. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27601. StartProgram(false);
  27602. Add('procedure DoIt;');
  27603. Add('type');
  27604. Add(' integer = longint;');
  27605. Add(' TPoint = record');
  27606. Add(' x,y: integer;');
  27607. Add(' end;');
  27608. Add('var p: pointer;');
  27609. Add('begin');
  27610. Add(' p:=typeinfo(tpoint);');
  27611. Add('end;');
  27612. Add('begin');
  27613. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  27614. ConvertProgram;
  27615. end;
  27616. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  27617. begin
  27618. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27619. StartProgram(false);
  27620. Add([
  27621. '{$modeswitch externalclass}',
  27622. 'type',
  27623. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27624. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  27625. ' TFlag = (up,down);',
  27626. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  27627. ' TFlags = set of TFlag;',
  27628. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  27629. 'var',
  27630. ' ti: TTypeInfo;',
  27631. ' tiInt: TTypeInfoInteger;',
  27632. ' tiEnum: TTypeInfoEnum;',
  27633. ' tiSet: TTypeInfoSet;',
  27634. 'begin',
  27635. ' ti:=typeinfo(string);',
  27636. ' ti:=typeinfo(boolean);',
  27637. ' ti:=typeinfo(char);',
  27638. ' ti:=typeinfo(double);',
  27639. ' tiInt:=typeinfo(shortint);',
  27640. ' tiInt:=typeinfo(byte);',
  27641. ' tiInt:=typeinfo(smallint);',
  27642. ' tiInt:=typeinfo(word);',
  27643. ' tiInt:=typeinfo(longint);',
  27644. ' tiInt:=typeinfo(longword);',
  27645. ' ti:=typeinfo(jsvalue);',
  27646. ' tiEnum:=typeinfo(tflag);',
  27647. ' tiSet:=typeinfo(tflags);']);
  27648. ConvertProgram;
  27649. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  27650. LinesToStr([ // statements
  27651. 'this.TFlag = {',
  27652. ' "0": "up",',
  27653. ' up: 0,',
  27654. ' "1": "down",',
  27655. ' down: 1',
  27656. '};',
  27657. '$mod.$rtti.$Enum("TFlag", {',
  27658. ' minvalue: 0,',
  27659. ' maxvalue: 1,',
  27660. ' ordtype: 1,',
  27661. ' enumtype: this.TFlag',
  27662. '});',
  27663. '$mod.$rtti.$Set("TFlags", {',
  27664. ' comptype: $mod.$rtti["TFlag"]',
  27665. '});',
  27666. 'this.ti = null;',
  27667. 'this.tiInt = null;',
  27668. 'this.tiEnum = null;',
  27669. 'this.tiSet = null;',
  27670. '']),
  27671. LinesToStr([ // $mod.$main
  27672. '$mod.ti = rtl.string;',
  27673. '$mod.ti = rtl.boolean;',
  27674. '$mod.ti = rtl.char;',
  27675. '$mod.ti = rtl.double;',
  27676. '$mod.tiInt = rtl.shortint;',
  27677. '$mod.tiInt = rtl.byte;',
  27678. '$mod.tiInt = rtl.smallint;',
  27679. '$mod.tiInt = rtl.word;',
  27680. '$mod.tiInt = rtl.longint;',
  27681. '$mod.tiInt = rtl.longword;',
  27682. '$mod.ti = rtl.jsvalue;',
  27683. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  27684. '$mod.tiSet = $mod.$rtti["TFlags"];',
  27685. '']));
  27686. end;
  27687. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  27688. begin
  27689. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27690. StartProgram(false);
  27691. Add('{$modeswitch externalclass}');
  27692. Add('type');
  27693. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  27694. Add(' TStaticArr = array[boolean] of string;');
  27695. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  27696. Add(' TDynArr = array of string;');
  27697. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  27698. Add(' TProc = procedure;');
  27699. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  27700. Add(' TMethod = procedure of object;');
  27701. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  27702. Add('var');
  27703. Add(' StaticArray: TStaticArr;');
  27704. Add(' tiStaticArray: TTypeInfoStaticArray;');
  27705. Add(' DynArray: TDynArr;');
  27706. Add(' tiDynArray: TTypeInfoDynArray;');
  27707. Add(' ProcVar: TProc;');
  27708. Add(' tiProcVar: TTypeInfoProcVar;');
  27709. Add(' MethodVar: TMethod;');
  27710. Add(' tiMethodVar: TTypeInfoMethodVar;');
  27711. Add('begin');
  27712. Add(' tiStaticArray:=typeinfo(StaticArray);');
  27713. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  27714. Add(' tiDynArray:=typeinfo(DynArray);');
  27715. Add(' tiDynArray:=typeinfo(TDynArr);');
  27716. Add(' tiProcVar:=typeinfo(ProcVar);');
  27717. Add(' tiProcVar:=typeinfo(TProc);');
  27718. Add(' tiMethodVar:=typeinfo(MethodVar);');
  27719. Add(' tiMethodVar:=typeinfo(TMethod);');
  27720. ConvertProgram;
  27721. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  27722. LinesToStr([ // statements
  27723. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  27724. ' dims: [2],',
  27725. ' eltype: rtl.string',
  27726. '});',
  27727. '$mod.$rtti.$DynArray("TDynArr", {',
  27728. ' eltype: rtl.string',
  27729. '});',
  27730. '$mod.$rtti.$ProcVar("TProc", {',
  27731. ' procsig: rtl.newTIProcSig(null)',
  27732. '});',
  27733. '$mod.$rtti.$MethodVar("TMethod", {',
  27734. ' procsig: rtl.newTIProcSig(null),',
  27735. ' methodkind: 0',
  27736. '});',
  27737. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  27738. 'this.tiStaticArray = null;',
  27739. 'this.DynArray = [];',
  27740. 'this.tiDynArray = null;',
  27741. 'this.ProcVar = null;',
  27742. 'this.tiProcVar = null;',
  27743. 'this.MethodVar = null;',
  27744. 'this.tiMethodVar = null;',
  27745. '']),
  27746. LinesToStr([ // $mod.$main
  27747. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  27748. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  27749. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  27750. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  27751. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  27752. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  27753. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  27754. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  27755. '']));
  27756. end;
  27757. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  27758. begin
  27759. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27760. StartProgram(false);
  27761. Add('{$modeswitch externalclass}');
  27762. Add('type');
  27763. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  27764. Add(' TRec = record end;');
  27765. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  27766. // ToDo: ^PRec
  27767. Add(' TObject = class end;');
  27768. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  27769. Add(' TClass = class of tobject;');
  27770. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  27771. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  27772. Add('var');
  27773. Add(' Rec: trec;');
  27774. Add(' tiRecord: ttypeinforecord;');
  27775. Add(' Obj: tobject;');
  27776. Add(' tiClass: ttypeinfoclass;');
  27777. Add(' aClass: tclass;');
  27778. Add(' tiClassRef: ttypeinfoclassref;');
  27779. // ToDo: ^PRec
  27780. Add(' tiPointer: ttypeinfopointer;');
  27781. Add('begin');
  27782. Add(' tirecord:=typeinfo(trec);');
  27783. Add(' tirecord:=typeinfo(trec);');
  27784. Add(' ticlass:=typeinfo(obj);');
  27785. Add(' ticlass:=typeinfo(tobject);');
  27786. Add(' ticlass:=typeinfo(aclass);');
  27787. Add(' ticlassref:=typeinfo(tclass);');
  27788. ConvertProgram;
  27789. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  27790. LinesToStr([ // statements
  27791. 'rtl.recNewT($mod, "TRec", function () {',
  27792. ' this.$eq = function (b) {',
  27793. ' return true;',
  27794. ' };',
  27795. ' this.$assign = function (s) {',
  27796. ' return this;',
  27797. ' };',
  27798. ' $mod.$rtti.$Record("TRec", {});',
  27799. '});',
  27800. 'rtl.createClass($mod, "TObject", null, function () {',
  27801. ' this.$init = function () {',
  27802. ' };',
  27803. ' this.$final = function () {',
  27804. ' };',
  27805. '});',
  27806. '$mod.$rtti.$ClassRef("TClass", {',
  27807. ' instancetype: $mod.$rtti["TObject"]',
  27808. '});',
  27809. 'this.Rec = $mod.TRec.$new();',
  27810. 'this.tiRecord = null;',
  27811. 'this.Obj = null;',
  27812. 'this.tiClass = null;',
  27813. 'this.aClass = null;',
  27814. 'this.tiClassRef = null;',
  27815. 'this.tiPointer = null;',
  27816. '']),
  27817. LinesToStr([ // $mod.$main
  27818. '$mod.tiRecord = $mod.$rtti["TRec"];',
  27819. '$mod.tiRecord = $mod.$rtti["TRec"];',
  27820. '$mod.tiClass = $mod.Obj.$rtti;',
  27821. '$mod.tiClass = $mod.$rtti["TObject"];',
  27822. '$mod.tiClass = $mod.aClass.$rtti;',
  27823. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  27824. '']));
  27825. end;
  27826. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  27827. begin
  27828. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27829. StartProgram(false);
  27830. Add([
  27831. '{$modeswitch externalclass}',
  27832. 'type',
  27833. ' TClass = class of tobject;',
  27834. ' TObject = class',
  27835. ' function MyClass: TClass;',
  27836. ' class function ClassType: TClass;',
  27837. ' end;',
  27838. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27839. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  27840. 'function TObject.MyClass: TClass;',
  27841. 'var t: TTypeInfoClass;',
  27842. 'begin',
  27843. ' t:=TypeInfo(Self);',
  27844. ' t:=TypeInfo(Result);',
  27845. ' t:=TypeInfo(TObject);',
  27846. 'end;',
  27847. 'class function TObject.ClassType: TClass;',
  27848. 'var t: TTypeInfoClass;',
  27849. 'begin',
  27850. ' t:=TypeInfo(Self);',
  27851. ' t:=TypeInfo(Result);',
  27852. 'end;',
  27853. 'var',
  27854. ' Obj: TObject;',
  27855. ' t: TTypeInfoClass;',
  27856. 'begin',
  27857. ' t:=TypeInfo(TObject.ClassType);',
  27858. ' t:=TypeInfo(Obj.ClassType);',
  27859. ' t:=TypeInfo(Obj.MyClass);',
  27860. '']);
  27861. ConvertProgram;
  27862. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  27863. LinesToStr([ // statements
  27864. '$mod.$rtti.$Class("TObject");',
  27865. '$mod.$rtti.$ClassRef("TClass", {',
  27866. ' instancetype: $mod.$rtti["TObject"]',
  27867. '});',
  27868. 'rtl.createClass($mod, "TObject", null, function () {',
  27869. ' this.$init = function () {',
  27870. ' };',
  27871. ' this.$final = function () {',
  27872. ' };',
  27873. ' this.MyClass = function () {',
  27874. ' var Result = null;',
  27875. ' var t = null;',
  27876. ' t = this.$rtti;',
  27877. ' t = Result.$rtti;',
  27878. ' t = $mod.$rtti["TObject"];',
  27879. ' return Result;',
  27880. ' };',
  27881. ' this.ClassType = function () {',
  27882. ' var Result = null;',
  27883. ' var t = null;',
  27884. ' t = this.$rtti;',
  27885. ' t = Result.$rtti;',
  27886. ' return Result;',
  27887. ' };',
  27888. '});',
  27889. 'this.Obj = null;',
  27890. 'this.t = null;',
  27891. '']),
  27892. LinesToStr([ // $mod.$main
  27893. '$mod.t = $mod.TObject.ClassType().$rtti;',
  27894. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  27895. '$mod.t = $mod.Obj.MyClass().$rtti;',
  27896. '']));
  27897. end;
  27898. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  27899. begin
  27900. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27901. AddModuleWithIntfImplSrc('typinfo.pas',
  27902. LinesToStr([
  27903. '{$modeswitch externalclass}',
  27904. 'type',
  27905. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27906. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  27907. '']),
  27908. '');
  27909. AddModuleWithIntfImplSrc('unit2.pas',
  27910. LinesToStr([
  27911. 'uses typinfo;',
  27912. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  27913. 'procedure DoPtr(p: PTypeInfo);',
  27914. 'procedure DoInfo(t: TTypeInfo);',
  27915. 'procedure DoInt(t: TTypeInfoInteger);',
  27916. '']),
  27917. LinesToStr([
  27918. 'procedure DoPtr(p: PTypeInfo);',
  27919. 'begin end;',
  27920. 'procedure DoInfo(t: TTypeInfo);',
  27921. 'begin end;',
  27922. 'procedure DoInt(t: TTypeInfoInteger);',
  27923. 'begin end;',
  27924. '']));
  27925. StartUnit(true);
  27926. Add([
  27927. 'interface',
  27928. 'uses unit2;', // does not use unit typinfo
  27929. 'implementation',
  27930. 'var',
  27931. ' i: byte;',
  27932. ' p: pointer;',
  27933. ' t: PTypeInfo;',
  27934. 'initialization',
  27935. ' p:=typeinfo(i);',
  27936. ' t:=typeinfo(i);',
  27937. ' if p=t then ;',
  27938. ' if p=typeinfo(i) then ;',
  27939. ' if typeinfo(i)=p then ;',
  27940. ' if t=typeinfo(i) then ;',
  27941. ' if typeinfo(i)=t then ;',
  27942. ' DoPtr(p);',
  27943. ' DoPtr(t);',
  27944. ' DoPtr(typeinfo(i));',
  27945. ' DoInfo(p);',
  27946. ' DoInfo(t);',
  27947. ' DoInfo(typeinfo(i));',
  27948. ' DoInt(typeinfo(i));',
  27949. '']);
  27950. ConvertUnit;
  27951. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  27952. LinesToStr([ // statements
  27953. 'var $impl = $mod.$impl;',
  27954. '']),
  27955. LinesToStr([ // this.$init
  27956. '$impl.p = rtl.byte;',
  27957. '$impl.t = rtl.byte;',
  27958. 'if ($impl.p === $impl.t) ;',
  27959. 'if ($impl.p === rtl.byte) ;',
  27960. 'if (rtl.byte === $impl.p) ;',
  27961. 'if ($impl.t === rtl.byte) ;',
  27962. 'if (rtl.byte === $impl.t) ;',
  27963. 'pas.unit2.DoPtr($impl.p);',
  27964. 'pas.unit2.DoPtr($impl.t);',
  27965. 'pas.unit2.DoPtr(rtl.byte);',
  27966. 'pas.unit2.DoInfo($impl.p);',
  27967. 'pas.unit2.DoInfo($impl.t);',
  27968. 'pas.unit2.DoInfo(rtl.byte);',
  27969. 'pas.unit2.DoInt(rtl.byte);',
  27970. '']),
  27971. LinesToStr([ // implementation
  27972. '$impl.i = 0;',
  27973. '$impl.p = null;',
  27974. '$impl.t = null;',
  27975. '']) );
  27976. end;
  27977. procedure TTestModule.TestRTTI_Interface_Corba;
  27978. begin
  27979. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27980. StartProgram(false);
  27981. Add([
  27982. '{$interfaces corba}',
  27983. '{$modeswitch externalclass}',
  27984. 'type',
  27985. ' IUnknown = interface',
  27986. ' end;',
  27987. ' IBird = interface',
  27988. ' function GetItem: longint;',
  27989. ' procedure SetItem(Value: longint);',
  27990. ' property Item: longint read GetItem write SetItem;',
  27991. ' end;',
  27992. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27993. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  27994. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  27995. 'var',
  27996. ' i: IBird;',
  27997. ' t: TTypeInfoInterface;',
  27998. 'begin',
  27999. ' t:=TypeInfo(IBird);',
  28000. ' t:=TypeInfo(i);',
  28001. ' DoIt(t);',
  28002. ' DoIt(TypeInfo(IBird));',
  28003. '']);
  28004. ConvertProgram;
  28005. CheckSource('TestRTTI_Interface_Corba',
  28006. LinesToStr([ // statements
  28007. 'rtl.createInterface(',
  28008. ' $mod,',
  28009. ' "IUnknown",',
  28010. ' "{B92D5841-758A-322B-B800-000000000000}",',
  28011. ' [],',
  28012. ' null,',
  28013. ' function () {',
  28014. ' }',
  28015. ');',
  28016. 'rtl.createInterface(',
  28017. ' $mod,',
  28018. ' "IBird",',
  28019. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  28020. ' ["GetItem", "SetItem"],',
  28021. ' null,',
  28022. ' function () {',
  28023. ' var $r = this.$rtti;',
  28024. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28025. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28026. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28027. ' }',
  28028. ');',
  28029. 'this.DoIt = function (t) {',
  28030. '}; ',
  28031. 'this.i = null;',
  28032. 'this.t = null;',
  28033. '']),
  28034. LinesToStr([ // $mod.$main
  28035. '$mod.t = $mod.$rtti["IBird"];',
  28036. '$mod.t = $mod.i.$rtti;',
  28037. '$mod.DoIt($mod.t);',
  28038. '$mod.DoIt($mod.$rtti["IBird"]);',
  28039. '']));
  28040. end;
  28041. procedure TTestModule.TestRTTI_Interface_COM;
  28042. begin
  28043. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28044. StartProgram(false);
  28045. Add([
  28046. '{$interfaces com}',
  28047. '{$modeswitch externalclass}',
  28048. 'type',
  28049. ' TGuid = record end;',
  28050. ' integer = longint;',
  28051. ' IUnknown = interface',
  28052. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  28053. ' function _AddRef: Integer;',
  28054. ' function _Release: Integer;',
  28055. ' end;',
  28056. ' IBird = interface',
  28057. ' function GetItem: longint;',
  28058. ' procedure SetItem(Value: longint);',
  28059. ' property Item: longint read GetItem write SetItem;',
  28060. ' end;',
  28061. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28062. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  28063. 'var',
  28064. ' i: IBird;',
  28065. ' t: TTypeInfoInterface;',
  28066. 'begin',
  28067. ' t:=TypeInfo(IBird);',
  28068. ' t:=TypeInfo(i);',
  28069. '']);
  28070. ConvertProgram;
  28071. CheckSource('TestRTTI_Interface_COM',
  28072. LinesToStr([ // statements
  28073. 'rtl.recNewT($mod, "TGuid", function () {',
  28074. ' this.$eq = function (b) {',
  28075. ' return true;',
  28076. ' };',
  28077. ' this.$assign = function (s) {',
  28078. ' return this;',
  28079. ' };',
  28080. ' $mod.$rtti.$Record("TGuid", {});',
  28081. '});',
  28082. 'rtl.createInterface(',
  28083. ' $mod,',
  28084. ' "IUnknown",',
  28085. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  28086. ' ["QueryInterface", "_AddRef", "_Release"],',
  28087. ' null,',
  28088. ' function () {',
  28089. ' this.$kind = "com";',
  28090. ' var $r = this.$rtti;',
  28091. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  28092. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  28093. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  28094. ' }',
  28095. ');',
  28096. 'rtl.createInterface(',
  28097. ' $mod,',
  28098. ' "IBird",',
  28099. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  28100. ' ["GetItem", "SetItem"],',
  28101. ' $mod.IUnknown,',
  28102. ' function () {',
  28103. ' var $r = this.$rtti;',
  28104. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28105. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28106. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28107. ' }',
  28108. ');',
  28109. 'this.i = null;',
  28110. 'this.t = null;',
  28111. '']),
  28112. LinesToStr([ // $mod.$main
  28113. '$mod.t = $mod.$rtti["IBird"];',
  28114. '$mod.t = $mod.i.$rtti;',
  28115. '']));
  28116. end;
  28117. procedure TTestModule.TestRTTI_ClassHelper;
  28118. begin
  28119. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28120. StartProgram(false);
  28121. Add([
  28122. '{$interfaces com}',
  28123. '{$modeswitch externalclass}',
  28124. 'type',
  28125. ' TObject = class',
  28126. ' end;',
  28127. ' THelper = class helper for TObject',
  28128. ' published',
  28129. ' function GetItem: longint;',
  28130. ' property Item: longint read GetItem;',
  28131. ' end;',
  28132. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28133. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  28134. 'function THelper.GetItem: longint;',
  28135. 'begin',
  28136. 'end;',
  28137. 'var',
  28138. ' t: TTypeInfoHelper;',
  28139. 'begin',
  28140. ' t:=TypeInfo(THelper);',
  28141. '']);
  28142. ConvertProgram;
  28143. CheckSource('TestRTTI_ClassHelper',
  28144. LinesToStr([ // statements
  28145. 'rtl.createClass($mod, "TObject", null, function () {',
  28146. ' this.$init = function () {',
  28147. ' };',
  28148. ' this.$final = function () {',
  28149. ' };',
  28150. '});',
  28151. 'rtl.createHelper($mod, "THelper", null, function () {',
  28152. ' this.GetItem = function () {',
  28153. ' var Result = 0;',
  28154. ' return Result;',
  28155. ' };',
  28156. ' var $r = this.$rtti;',
  28157. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28158. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  28159. '});',
  28160. 'this.t = null;',
  28161. '']),
  28162. LinesToStr([ // $mod.$main
  28163. '$mod.t = $mod.$rtti["THelper"];',
  28164. '']));
  28165. end;
  28166. procedure TTestModule.TestResourcestringProgram;
  28167. begin
  28168. StartProgram(false);
  28169. Add([
  28170. 'const Bar = ''bar'';',
  28171. 'resourcestring',
  28172. ' Red = ''red'';',
  28173. ' Foobar = ''fOo''+bar;',
  28174. 'var s: string;',
  28175. ' c: char;',
  28176. 'begin',
  28177. ' s:=red;',
  28178. ' s:=test1.red;',
  28179. ' c:=red[1];',
  28180. ' c:=test1.red[2];',
  28181. ' if red=foobar then ;',
  28182. ' if red[3]=red[4] then ;']);
  28183. ConvertProgram;
  28184. CheckSource('TestResourcestringProgram',
  28185. LinesToStr([ // statements
  28186. 'this.Bar = "bar";',
  28187. 'this.s = "";',
  28188. 'this.c = "";',
  28189. '$mod.$resourcestrings = {',
  28190. ' Red: {',
  28191. ' org: "red"',
  28192. ' },',
  28193. ' Foobar: {',
  28194. ' org: "fOobar"',
  28195. ' }',
  28196. '};',
  28197. '']),
  28198. LinesToStr([ // $mod.$main
  28199. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28200. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28201. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  28202. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  28203. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  28204. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  28205. '']));
  28206. end;
  28207. procedure TTestModule.TestResourcestringUnit;
  28208. begin
  28209. StartUnit(false);
  28210. Add([
  28211. 'interface',
  28212. 'const Red = ''rEd'';',
  28213. 'resourcestring',
  28214. ' Blue = ''blue'';',
  28215. ' NotRed = ''not''+Red;',
  28216. 'var s: string;',
  28217. 'implementation',
  28218. 'resourcestring',
  28219. ' ImplGreen = ''green'';',
  28220. 'initialization',
  28221. ' s:=blue+ImplGreen;',
  28222. ' s:=test1.blue+test1.implgreen;',
  28223. ' s:=blue[1]+implgreen[2];']);
  28224. ConvertUnit;
  28225. CheckSource('TestResourcestringUnit',
  28226. LinesToStr([ // statements
  28227. 'this.Red = "rEd";',
  28228. 'this.s = "";',
  28229. '$mod.$resourcestrings = {',
  28230. ' Blue: {',
  28231. ' org: "blue"',
  28232. ' },',
  28233. ' NotRed: {',
  28234. ' org: "notrEd"',
  28235. ' },',
  28236. ' ImplGreen: {',
  28237. ' org: "green"',
  28238. ' }',
  28239. '};',
  28240. '']),
  28241. LinesToStr([ // $mod.$main
  28242. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28243. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28244. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  28245. '']));
  28246. end;
  28247. procedure TTestModule.TestResourcestringImplementation;
  28248. begin
  28249. StartUnit(false);
  28250. Add([
  28251. 'interface',
  28252. 'implementation',
  28253. 'resourcestring',
  28254. ' ImplRed = ''red'';']);
  28255. ConvertUnit;
  28256. CheckSource('TestResourcestringImplementation',
  28257. LinesToStr([ // intf statements
  28258. 'var $impl = $mod.$impl;']),
  28259. LinesToStr([ // $mod.$init
  28260. '']),
  28261. LinesToStr([ // impl statements
  28262. '$mod.$resourcestrings = {',
  28263. ' ImplRed: {',
  28264. ' org: "red"',
  28265. ' }',
  28266. '};',
  28267. '']));
  28268. end;
  28269. procedure TTestModule.TestAttributes_Members;
  28270. begin
  28271. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28272. StartProgram(false);
  28273. Add([
  28274. '{$modeswitch PrefixedAttributes}',
  28275. 'type',
  28276. ' TObject = class',
  28277. ' constructor Create;',
  28278. ' end;',
  28279. ' TCustomAttribute = class',
  28280. ' constructor Create(Id: word);',
  28281. ' end;',
  28282. ' [Missing]',
  28283. ' TBird = class',
  28284. ' published',
  28285. ' [Tcustom]',
  28286. ' FField: word;',
  28287. ' [tcustom(14)]',
  28288. ' property Size: word read FField;',
  28289. ' [Tcustom(15)]',
  28290. ' procedure Fly; virtual; abstract;',
  28291. ' end;',
  28292. ' TRec = record',
  28293. ' [Tcustom,tcustom(14)]',
  28294. ' Size: word;',
  28295. ' end;',
  28296. 'constructor TObject.Create; begin end;',
  28297. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  28298. 'begin',
  28299. '']);
  28300. ConvertProgram;
  28301. CheckSource('TestAttributes_Members',
  28302. LinesToStr([ // statements
  28303. 'rtl.createClass($mod, "TObject", null, function () {',
  28304. ' this.$init = function () {',
  28305. ' };',
  28306. ' this.$final = function () {',
  28307. ' };',
  28308. ' this.Create = function () {',
  28309. ' return this;',
  28310. ' };',
  28311. '});',
  28312. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  28313. ' this.Create$1 = function (Id) {',
  28314. ' return this;',
  28315. ' };',
  28316. '});',
  28317. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28318. ' this.$init = function () {',
  28319. ' $mod.TObject.$init.call(this);',
  28320. ' this.FField = 0;',
  28321. ' };',
  28322. ' var $r = this.$rtti;',
  28323. ' $r.addField("FField", rtl.word, {',
  28324. ' attr: [$mod.TCustomAttribute, "Create"]',
  28325. ' });',
  28326. ' $r.addProperty(',
  28327. ' "Size",',
  28328. ' 0,',
  28329. ' rtl.word,',
  28330. ' "FField",',
  28331. ' "",',
  28332. ' {',
  28333. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  28334. ' }',
  28335. ' );',
  28336. ' $r.addMethod("Fly", 0, null, null, {',
  28337. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  28338. ' });',
  28339. '});',
  28340. 'rtl.recNewT($mod, "TRec", function () {',
  28341. ' this.Size = 0;',
  28342. ' this.$eq = function (b) {',
  28343. ' return this.Size === b.Size;',
  28344. ' };',
  28345. ' this.$assign = function (s) {',
  28346. ' this.Size = s.Size;',
  28347. ' return this;',
  28348. ' };',
  28349. ' var $r = $mod.$rtti.$Record("TRec", {});',
  28350. ' $r.addField("Size", rtl.word, {',
  28351. ' attr: [',
  28352. ' $mod.TCustomAttribute,',
  28353. ' "Create",',
  28354. ' $mod.TCustomAttribute,',
  28355. ' "Create$1",',
  28356. ' [14]',
  28357. ' ]',
  28358. ' });',
  28359. '});',
  28360. '']),
  28361. LinesToStr([ // $mod.$main
  28362. '']));
  28363. end;
  28364. procedure TTestModule.TestAttributes_Types;
  28365. begin
  28366. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28367. StartProgram(false);
  28368. Add([
  28369. '{$modeswitch PrefixedAttributes}',
  28370. 'type',
  28371. ' TObject = class',
  28372. ' constructor Create(Id: word);',
  28373. ' end;',
  28374. ' TCustomAttribute = class',
  28375. ' end;',
  28376. ' [TCustom(1)]',
  28377. ' TMyClass = class',
  28378. ' end;',
  28379. ' [TCustom(2)]',
  28380. ' TRec = record',
  28381. ' end;',
  28382. ' [TCustom(3)]',
  28383. ' TInt = type word;',
  28384. 'constructor TObject.Create(Id: word);',
  28385. 'begin',
  28386. 'end;',
  28387. 'var p: pointer;',
  28388. 'begin',
  28389. ' p:=typeinfo(TMyClass);',
  28390. ' p:=typeinfo(TRec);',
  28391. ' p:=typeinfo(TInt);',
  28392. '']);
  28393. ConvertProgram;
  28394. CheckSource('TestAttributes_Types',
  28395. LinesToStr([ // statements
  28396. 'rtl.createClass($mod, "TObject", null, function () {',
  28397. ' this.$init = function () {',
  28398. ' };',
  28399. ' this.$final = function () {',
  28400. ' };',
  28401. ' this.Create = function (Id) {',
  28402. ' return this;',
  28403. ' };',
  28404. '});',
  28405. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  28406. '});',
  28407. 'rtl.createClass($mod, "TMyClass", $mod.TObject, function () {',
  28408. ' var $r = this.$rtti;',
  28409. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  28410. '});',
  28411. 'rtl.recNewT($mod, "TRec", function () {',
  28412. ' this.$eq = function (b) {',
  28413. ' return true;',
  28414. ' };',
  28415. ' this.$assign = function (s) {',
  28416. ' return this;',
  28417. ' };',
  28418. ' $mod.$rtti.$Record("TRec", {',
  28419. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  28420. ' });',
  28421. '});',
  28422. '$mod.$rtti.$inherited("TInt", rtl.word, {',
  28423. ' attr: [$mod.TCustomAttribute, "Create", [3]]',
  28424. '});',
  28425. 'this.p = null;',
  28426. '']),
  28427. LinesToStr([ // $mod.$main
  28428. '$mod.p = $mod.$rtti["TMyClass"];',
  28429. '$mod.p = $mod.$rtti["TRec"];',
  28430. '$mod.p = $mod.$rtti["TInt"];',
  28431. '']));
  28432. end;
  28433. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  28434. begin
  28435. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28436. StartProgram(false);
  28437. Add([
  28438. '{$modeswitch PrefixedAttributes}',
  28439. 'type',
  28440. ' TObject = class',
  28441. ' constructor Create;',
  28442. ' end;',
  28443. ' TCustomAttribute = class',
  28444. ' end;',
  28445. ' THelper = class helper for TCustomAttribute',
  28446. ' constructor Create(Id: word);',
  28447. ' end;',
  28448. ' [TCustom(3)]',
  28449. ' TMyInt = word;',
  28450. 'constructor TObject.Create; begin end;',
  28451. 'constructor THelper.Create(Id: word); begin end;',
  28452. 'begin',
  28453. ' if typeinfo(TMyInt)=nil then ;']);
  28454. //SetExpectedConverterError('aaa',123);
  28455. ConvertProgram;
  28456. end;
  28457. procedure TTestModule.TestAssert;
  28458. begin
  28459. StartProgram(false);
  28460. Add([
  28461. 'procedure DoIt;',
  28462. 'var',
  28463. ' b: boolean;',
  28464. ' s: string;',
  28465. 'begin',
  28466. ' {$Assertions on}',
  28467. ' Assert(b);',
  28468. 'end;',
  28469. 'begin',
  28470. ' DoIt;',
  28471. '']);
  28472. ConvertProgram;
  28473. CheckSource('TestAssert',
  28474. LinesToStr([ // statements
  28475. 'this.DoIt = function () {',
  28476. ' var b = false;',
  28477. ' var s = "";',
  28478. ' if (!b) throw "assert failed";',
  28479. '};',
  28480. '']),
  28481. LinesToStr([ // $mod.$main
  28482. '$mod.DoIt();',
  28483. '']));
  28484. end;
  28485. procedure TTestModule.TestAssert_SysUtils;
  28486. begin
  28487. AddModuleWithIntfImplSrc('SysUtils.pas',
  28488. LinesToStr([
  28489. 'type',
  28490. ' TObject = class',
  28491. ' constructor Create;',
  28492. ' end;',
  28493. ' EAssertionFailed = class',
  28494. ' constructor Create(s: string);',
  28495. ' end;',
  28496. '']),
  28497. LinesToStr([
  28498. 'constructor TObject.Create;',
  28499. 'begin end;',
  28500. 'constructor EAssertionFailed.Create(s: string);',
  28501. 'begin end;',
  28502. '']) );
  28503. StartProgram(true);
  28504. Add([
  28505. 'uses sysutils;',
  28506. 'procedure DoIt;',
  28507. 'var',
  28508. ' b: boolean;',
  28509. ' s: string;',
  28510. 'begin',
  28511. ' {$Assertions on}',
  28512. ' Assert(b);',
  28513. ' Assert(b,''msg'');',
  28514. 'end;',
  28515. 'begin',
  28516. ' DoIt;',
  28517. '']);
  28518. ConvertProgram;
  28519. CheckSource('TestAssert_SysUtils',
  28520. LinesToStr([ // statements
  28521. 'this.DoIt = function () {',
  28522. ' var b = false;',
  28523. ' var s = "";',
  28524. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  28525. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  28526. '};',
  28527. '']),
  28528. LinesToStr([ // $mod.$main
  28529. '$mod.DoIt();',
  28530. '']));
  28531. end;
  28532. procedure TTestModule.TestObjectChecks;
  28533. begin
  28534. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  28535. StartProgram(false);
  28536. Add([
  28537. 'type',
  28538. ' TObject = class',
  28539. ' procedure DoIt;',
  28540. ' end;',
  28541. ' TClass = class of tobject;',
  28542. ' TBird = class',
  28543. ' end;',
  28544. ' TBirdClass = class of TBird;',
  28545. 'var',
  28546. ' o : TObject;',
  28547. ' c: TClass;',
  28548. ' b: TBird;',
  28549. ' bc: TBirdClass;',
  28550. 'procedure TObject.DoIt;',
  28551. 'begin',
  28552. ' b:=TBird(o);',
  28553. 'end;',
  28554. 'begin',
  28555. ' o.DoIt;',
  28556. ' b:=TBird(o);',
  28557. ' bc:=TBirdClass(c);',
  28558. '']);
  28559. ConvertProgram;
  28560. CheckSource('TestCheckMethodCall',
  28561. LinesToStr([ // statements
  28562. 'rtl.createClass($mod, "TObject", null, function () {',
  28563. ' this.$init = function () {',
  28564. ' };',
  28565. ' this.$final = function () {',
  28566. ' };',
  28567. ' this.DoIt = function () {',
  28568. ' rtl.checkMethodCall(this,$mod.TObject);',
  28569. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  28570. ' };',
  28571. '});',
  28572. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28573. '});',
  28574. 'this.o = null;',
  28575. 'this.c = null;',
  28576. 'this.b = null;',
  28577. 'this.bc = null;',
  28578. '']),
  28579. LinesToStr([ // $mod.$main
  28580. '$mod.o.DoIt();',
  28581. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  28582. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  28583. '']));
  28584. end;
  28585. procedure TTestModule.TestOverflowChecks_Int;
  28586. begin
  28587. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  28588. StartProgram(false);
  28589. Add([
  28590. 'procedure DoIt;',
  28591. 'var',
  28592. ' b: byte;',
  28593. ' n: nativeint;',
  28594. ' u: nativeuint;',
  28595. ' c: currency;',
  28596. 'begin',
  28597. ' n:=n+n;',
  28598. ' n:=n-n;',
  28599. ' n:=n+b;',
  28600. ' n:=b-n;',
  28601. ' n:=n*n;',
  28602. ' n:=n*u;',
  28603. ' c:=c+b;',
  28604. ' c:=b+c;',
  28605. ' c:=c*b;',
  28606. ' c:=b*c;',
  28607. 'end;',
  28608. 'begin',
  28609. '']);
  28610. ConvertProgram;
  28611. CheckSource('TestOverflowChecks_Int',
  28612. LinesToStr([ // statements
  28613. 'this.DoIt = function () {',
  28614. ' var b = 0;',
  28615. ' var n = 0;',
  28616. ' var u = 0;',
  28617. ' var c = 0;',
  28618. ' n = rtl.oc(n + n);',
  28619. ' n = rtl.oc(n - n);',
  28620. ' n = rtl.oc(n + b);',
  28621. ' n = rtl.oc(b - n);',
  28622. ' n = rtl.oc(n * n);',
  28623. ' n = rtl.oc(n * u);',
  28624. ' c = rtl.oc(c + (b * 10000));',
  28625. ' c = rtl.oc((b * 10000) + c);',
  28626. ' c = rtl.oc(c * b);',
  28627. ' c = rtl.oc(b * c);',
  28628. '};',
  28629. '']),
  28630. LinesToStr([ // $mod.$main
  28631. '']));
  28632. end;
  28633. procedure TTestModule.TestRangeChecks_AssignInt;
  28634. begin
  28635. Scanner.Options:=Scanner.Options+[po_CAssignments];
  28636. StartProgram(false);
  28637. Add([
  28638. '{$R+}',
  28639. 'var',
  28640. ' b: byte = 2;',
  28641. ' w: word = 3;',
  28642. 'procedure DoIt(p: byte);',
  28643. 'begin',
  28644. ' b:=w;',
  28645. ' b+=w;',
  28646. ' b:=1;',
  28647. 'end;',
  28648. '{$R-}',
  28649. 'procedure DoSome;',
  28650. 'begin',
  28651. ' DoIt(w);',
  28652. ' b:=w;',
  28653. ' b:=2;',
  28654. 'end;',
  28655. 'begin',
  28656. '{$R+}',
  28657. '']);
  28658. ConvertProgram;
  28659. CheckSource('TestRangeChecks_AssignInt',
  28660. LinesToStr([ // statements
  28661. 'this.b = 2;',
  28662. 'this.w = 3;',
  28663. 'this.DoIt = function (p) {',
  28664. ' rtl.rc(p, 0, 255);',
  28665. ' $mod.b = rtl.rc($mod.w,0,255);',
  28666. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  28667. ' $mod.b = 1;',
  28668. '};',
  28669. 'this.DoSome = function () {',
  28670. ' $mod.DoIt($mod.w);',
  28671. ' $mod.b = $mod.w;',
  28672. ' $mod.b = 2;',
  28673. '};',
  28674. '']),
  28675. LinesToStr([ // $mod.$main
  28676. '']));
  28677. end;
  28678. procedure TTestModule.TestRangeChecks_AssignIntRange;
  28679. begin
  28680. Scanner.Options:=Scanner.Options+[po_CAssignments];
  28681. StartProgram(false);
  28682. Add([
  28683. '{$R+}',
  28684. 'type Ten = 1..10;',
  28685. 'var',
  28686. ' b: Ten = 2;',
  28687. ' w: Ten = 3;',
  28688. 'procedure DoIt(p: Ten);',
  28689. 'begin',
  28690. ' b:=w;',
  28691. ' b+=w;',
  28692. ' b:=1;',
  28693. 'end;',
  28694. '{$R-}',
  28695. 'procedure DoSome;',
  28696. 'begin',
  28697. ' DoIt(w);',
  28698. ' b:=w;',
  28699. ' b:=2;',
  28700. 'end;',
  28701. 'begin',
  28702. '{$R+}',
  28703. '']);
  28704. ConvertProgram;
  28705. CheckSource('TestRangeChecks_AssignIntRange',
  28706. LinesToStr([ // statements
  28707. 'this.b = 2;',
  28708. 'this.w = 3;',
  28709. 'this.DoIt = function (p) {',
  28710. ' rtl.rc(p, 1, 10);',
  28711. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  28712. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  28713. ' $mod.b = 1;',
  28714. '};',
  28715. 'this.DoSome = function () {',
  28716. ' $mod.DoIt($mod.w);',
  28717. ' $mod.b = $mod.w;',
  28718. ' $mod.b = 2;',
  28719. '};',
  28720. '']),
  28721. LinesToStr([ // $mod.$main
  28722. '']));
  28723. end;
  28724. procedure TTestModule.TestRangeChecks_AssignEnum;
  28725. begin
  28726. StartProgram(false);
  28727. Add([
  28728. '{$R+}',
  28729. 'type TEnum = (red,green);',
  28730. 'var',
  28731. ' e: TEnum = red;',
  28732. 'procedure DoIt(p: TEnum);',
  28733. 'begin',
  28734. ' e:=p;',
  28735. ' p:=TEnum(0);',
  28736. ' p:=succ(e);',
  28737. 'end;',
  28738. '{$R-}',
  28739. 'procedure DoSome;',
  28740. 'begin',
  28741. ' DoIt(e);',
  28742. ' e:=TEnum(1);',
  28743. ' e:=pred(e);',
  28744. 'end;',
  28745. 'begin',
  28746. '{$R+}',
  28747. '']);
  28748. ConvertProgram;
  28749. CheckSource('TestRangeChecks_AssignEnum',
  28750. LinesToStr([ // statements
  28751. 'this.TEnum = {',
  28752. ' "0": "red",',
  28753. ' red: 0,',
  28754. ' "1": "green",',
  28755. ' green: 1',
  28756. '};',
  28757. 'this.e = $mod.TEnum.red;',
  28758. 'this.DoIt = function (p) {',
  28759. ' rtl.rc(p, 0, 1);',
  28760. ' $mod.e = rtl.rc(p, 0, 1);',
  28761. ' p = 0;',
  28762. ' p = rtl.rc($mod.e + 1, 0, 1);',
  28763. '};',
  28764. 'this.DoSome = function () {',
  28765. ' $mod.DoIt($mod.e);',
  28766. ' $mod.e = 1;',
  28767. ' $mod.e = $mod.e - 1;',
  28768. '};',
  28769. '']),
  28770. LinesToStr([ // $mod.$main
  28771. '']));
  28772. end;
  28773. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  28774. begin
  28775. StartProgram(false);
  28776. Add([
  28777. '{$R+}',
  28778. 'type',
  28779. ' TEnum = (red,green);',
  28780. ' TEnumRg = red..green;',
  28781. 'var',
  28782. ' e: TEnumRg = red;',
  28783. 'procedure DoIt(p: TEnumRg);',
  28784. 'begin',
  28785. ' e:=p;',
  28786. ' p:=TEnumRg(0);',
  28787. ' p:=succ(e);',
  28788. 'end;',
  28789. '{$R-}',
  28790. 'procedure DoSome;',
  28791. 'begin',
  28792. ' DoIt(e);',
  28793. ' e:=TEnum(1);',
  28794. ' e:=pred(e);',
  28795. 'end;',
  28796. 'begin',
  28797. '{$R+}',
  28798. '']);
  28799. ConvertProgram;
  28800. CheckSource('TestRangeChecks_AssignEnumRange',
  28801. LinesToStr([ // statements
  28802. 'this.TEnum = {',
  28803. ' "0": "red",',
  28804. ' red: 0,',
  28805. ' "1": "green",',
  28806. ' green: 1',
  28807. '};',
  28808. 'this.e = $mod.TEnum.red;',
  28809. 'this.DoIt = function (p) {',
  28810. ' rtl.rc(p, 0, 1);',
  28811. ' $mod.e = rtl.rc(p, 0, 1);',
  28812. ' p = 0;',
  28813. ' p = rtl.rc($mod.e + 1, 0, 1);',
  28814. '};',
  28815. 'this.DoSome = function () {',
  28816. ' $mod.DoIt($mod.e);',
  28817. ' $mod.e = 1;',
  28818. ' $mod.e = $mod.e - 1;',
  28819. '};',
  28820. '']),
  28821. LinesToStr([ // $mod.$main
  28822. '']));
  28823. end;
  28824. procedure TTestModule.TestRangeChecks_AssignChar;
  28825. begin
  28826. StartProgram(false);
  28827. Add([
  28828. '{$R+}',
  28829. 'type',
  28830. ' TLetter = char;',
  28831. 'var',
  28832. ' b: TLetter = ''2'';',
  28833. ' w: TLetter = ''3'';',
  28834. 'procedure DoIt(p: TLetter);',
  28835. 'begin',
  28836. ' b:=w;',
  28837. ' b:=''1'';',
  28838. 'end;',
  28839. '{$R-}',
  28840. 'procedure DoSome;',
  28841. 'begin',
  28842. ' DoIt(w);',
  28843. ' b:=w;',
  28844. ' b:=''2'';',
  28845. 'end;',
  28846. 'begin',
  28847. '{$R+}',
  28848. '']);
  28849. ConvertProgram;
  28850. CheckSource('TestRangeChecks_AssignChar',
  28851. LinesToStr([ // statements
  28852. 'this.b = "2";',
  28853. 'this.w = "3";',
  28854. 'this.DoIt = function (p) {',
  28855. ' rtl.rcc(p, 0, 65535);',
  28856. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  28857. ' $mod.b = "1";',
  28858. '};',
  28859. 'this.DoSome = function () {',
  28860. ' $mod.DoIt($mod.w);',
  28861. ' $mod.b = $mod.w;',
  28862. ' $mod.b = "2";',
  28863. '};',
  28864. '']),
  28865. LinesToStr([ // $mod.$main
  28866. '']));
  28867. end;
  28868. procedure TTestModule.TestRangeChecks_AssignCharRange;
  28869. begin
  28870. StartProgram(false);
  28871. Add([
  28872. '{$R+}',
  28873. 'type TDigit = ''0''..''9'';',
  28874. 'var',
  28875. ' b: TDigit = ''2'';',
  28876. ' w: TDigit = ''3'';',
  28877. 'procedure DoIt(p: TDigit);',
  28878. 'begin',
  28879. ' b:=w;',
  28880. ' b:=''1'';',
  28881. 'end;',
  28882. '{$R-}',
  28883. 'procedure DoSome;',
  28884. 'begin',
  28885. ' DoIt(w);',
  28886. ' b:=w;',
  28887. ' b:=''2'';',
  28888. 'end;',
  28889. 'begin',
  28890. '{$R+}',
  28891. '']);
  28892. ConvertProgram;
  28893. CheckSource('TestRangeChecks_AssignCharRange',
  28894. LinesToStr([ // statements
  28895. 'this.b = "2";',
  28896. 'this.w = "3";',
  28897. 'this.DoIt = function (p) {',
  28898. ' rtl.rcc(p, 48, 57);',
  28899. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  28900. ' $mod.b = "1";',
  28901. '};',
  28902. 'this.DoSome = function () {',
  28903. ' $mod.DoIt($mod.w);',
  28904. ' $mod.b = $mod.w;',
  28905. ' $mod.b = "2";',
  28906. '};',
  28907. '']),
  28908. LinesToStr([ // $mod.$main
  28909. '']));
  28910. end;
  28911. procedure TTestModule.TestRangeChecks_ArrayIndex;
  28912. begin
  28913. StartProgram(false);
  28914. Add([
  28915. '{$R+}',
  28916. 'type',
  28917. ' Ten = 1..10;',
  28918. ' TArr = array of Ten;',
  28919. ' TArrArr = array of TArr;',
  28920. ' TArrByte = array[byte] of Ten;',
  28921. ' TArrChar = array[''0''..''9''] of Ten;',
  28922. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  28923. ' TObject = class',
  28924. ' A: TArr;',
  28925. ' end;',
  28926. 'procedure DoIt;',
  28927. 'var',
  28928. ' Arr: TArr;',
  28929. ' ArrArr: TArrArr;',
  28930. ' ArrByte: TArrByte;',
  28931. ' ArrChar: TArrChar;',
  28932. ' ArrByteChar: TArrByteChar;',
  28933. ' i: Ten;',
  28934. ' c: char;',
  28935. ' o: tobject;',
  28936. 'begin',
  28937. ' i:=Arr[1];',
  28938. ' i:=ArrByteChar[1,''2''];',
  28939. ' Arr[1]:=Arr[1];',
  28940. ' Arr[i]:=Arr[i];',
  28941. ' ArrByte[3]:=ArrByte[3];',
  28942. ' ArrByte[i]:=ArrByte[i];',
  28943. ' ArrChar[''5'']:=ArrChar[''5''];',
  28944. ' ArrChar[c]:=ArrChar[c];',
  28945. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  28946. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  28947. ' o.a[i]:=o.a[i];',
  28948. 'end;',
  28949. 'begin',
  28950. '']);
  28951. ConvertProgram;
  28952. CheckSource('TestRangeChecks_ArrayIndex',
  28953. LinesToStr([ // statements
  28954. 'rtl.createClass($mod, "TObject", null, function () {',
  28955. ' this.$init = function () {',
  28956. ' this.A = [];',
  28957. ' };',
  28958. ' this.$final = function () {',
  28959. ' this.A = undefined;',
  28960. ' };',
  28961. '});',
  28962. 'this.DoIt = function () {',
  28963. ' var Arr = [];',
  28964. ' var ArrArr = [];',
  28965. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  28966. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  28967. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  28968. ' var i = 0;',
  28969. ' var c = "";',
  28970. ' var o = null;',
  28971. ' i = rtl.rc(Arr[1], 1, 10);',
  28972. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  28973. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  28974. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  28975. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  28976. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  28977. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  28978. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  28979. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  28980. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  28981. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  28982. '};',
  28983. '']),
  28984. LinesToStr([ // $mod.$main
  28985. '']));
  28986. end;
  28987. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  28988. begin
  28989. StartProgram(false);
  28990. Add([
  28991. '{$R+}',
  28992. 'type',
  28993. ' Ten = 1..10;',
  28994. ' TRec = record x: Ten end;',
  28995. ' TArr = array of TRec;',
  28996. ' TArrArr = array of TArr;',
  28997. ' TObject = class',
  28998. ' A: TArr;',
  28999. ' end;',
  29000. 'procedure DoIt;',
  29001. 'var',
  29002. ' Arr: TArr;',
  29003. ' ArrArr: TArrArr;',
  29004. ' i: Ten;',
  29005. ' o: tobject;',
  29006. 'begin',
  29007. ' Arr[1]:=Arr[1];',
  29008. ' Arr[i]:=Arr[i+1];',
  29009. ' o.a[i]:=o.a[i+2];',
  29010. 'end;',
  29011. 'begin',
  29012. '']);
  29013. ConvertProgram;
  29014. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  29015. LinesToStr([ // statements
  29016. 'rtl.recNewT($mod, "TRec", function () {',
  29017. ' this.x = 0;',
  29018. ' this.$eq = function (b) {',
  29019. ' return this.x === b.x;',
  29020. ' };',
  29021. ' this.$assign = function (s) {',
  29022. ' this.x = s.x;',
  29023. ' return this;',
  29024. ' };',
  29025. '});',
  29026. 'rtl.createClass($mod, "TObject", null, function () {',
  29027. ' this.$init = function () {',
  29028. ' this.A = [];',
  29029. ' };',
  29030. ' this.$final = function () {',
  29031. ' this.A = undefined;',
  29032. ' };',
  29033. '});',
  29034. 'this.DoIt = function () {',
  29035. ' var Arr = [];',
  29036. ' var ArrArr = [];',
  29037. ' var i = 0;',
  29038. ' var o = null;',
  29039. ' Arr[1].$assign(Arr[1]);',
  29040. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  29041. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  29042. '};',
  29043. '']),
  29044. LinesToStr([ // $mod.$main
  29045. '']));
  29046. end;
  29047. procedure TTestModule.TestRangeChecks_StringIndex;
  29048. begin
  29049. StartProgram(false);
  29050. Add([
  29051. 'type',
  29052. ' TObject = class',
  29053. ' S: string;',
  29054. ' end;',
  29055. '{$R+}',
  29056. 'procedure DoIt(var h: string);',
  29057. 'var',
  29058. ' s: string;',
  29059. ' i: longint;',
  29060. ' c: char;',
  29061. ' o: tobject;',
  29062. 'begin',
  29063. ' c:=s[1];',
  29064. ' s[i]:=s[i];',
  29065. ' h[i]:=h[i];',
  29066. ' c:=o.s[i];',
  29067. ' o.s[i]:=c;',
  29068. 'end;',
  29069. 'begin',
  29070. '']);
  29071. ConvertProgram;
  29072. CheckSource('TestRangeChecks_StringIndex',
  29073. LinesToStr([ // statements
  29074. 'rtl.createClass($mod, "TObject", null, function () {',
  29075. ' this.$init = function () {',
  29076. ' this.S = "";',
  29077. ' };',
  29078. ' this.$final = function () {',
  29079. ' };',
  29080. '});',
  29081. 'this.DoIt = function (h) {',
  29082. ' var s = "";',
  29083. ' var i = 0;',
  29084. ' var c = "";',
  29085. ' var o = null;',
  29086. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  29087. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  29088. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  29089. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  29090. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  29091. '};',
  29092. '']),
  29093. LinesToStr([ // $mod.$main
  29094. '']));
  29095. end;
  29096. procedure TTestModule.TestRangeChecks_TypecastInt;
  29097. begin
  29098. StartProgram(false);
  29099. Add([
  29100. '{$R+}',
  29101. 'var',
  29102. ' i: nativeint;',
  29103. ' b: byte;',
  29104. ' sh: shortint;',
  29105. ' w: word;',
  29106. ' sm: smallint;',
  29107. ' lw: longword;',
  29108. ' li: longint;',
  29109. 'begin',
  29110. ' b:=12+byte(i);',
  29111. ' sh:=12+shortint(i);',
  29112. ' w:=12+word(i);',
  29113. ' sm:=12+smallint(i);',
  29114. ' lw:=12+longword(i);',
  29115. ' li:=12+longint(i);',
  29116. '']);
  29117. ConvertProgram;
  29118. CheckSource('TestRangeChecks_TypecastInt',
  29119. LinesToStr([
  29120. 'this.i = 0;',
  29121. 'this.b = 0;',
  29122. 'this.sh = 0;',
  29123. 'this.w = 0;',
  29124. 'this.sm = 0;',
  29125. 'this.lw = 0;',
  29126. 'this.li = 0;',
  29127. '']),
  29128. LinesToStr([
  29129. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  29130. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  29131. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  29132. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  29133. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  29134. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  29135. '']));
  29136. end;
  29137. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  29138. begin
  29139. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29140. StartProgram(false);
  29141. Add([
  29142. '{$modeswitch typehelpers}',
  29143. '{$R+}',
  29144. 'type',
  29145. ' TObject = class',
  29146. ' FSize: byte;',
  29147. ' property Size: byte read FSize;',
  29148. ' end;',
  29149. ' THelper = type helper for byte',
  29150. ' procedure SetIt(w: word);',
  29151. ' end;',
  29152. 'procedure THelper.SetIt(w: word);',
  29153. 'begin',
  29154. ' Self:=w;',
  29155. 'end;',
  29156. 'function GetIt: byte;',
  29157. 'begin',
  29158. ' Result.SetIt(2);',
  29159. 'end;',
  29160. 'var',
  29161. ' b: byte = 3;',
  29162. ' o: TObject;',
  29163. 'begin',
  29164. ' b.SetIt(14);',
  29165. ' with b do SetIt(15);',
  29166. ' o.Size.SetIt(16);',
  29167. '']);
  29168. ConvertProgram;
  29169. CheckSource('TestRangeChecks_AssignInt',
  29170. LinesToStr([ // statements
  29171. 'rtl.createClass($mod, "TObject", null, function () {',
  29172. ' this.$init = function () {',
  29173. ' this.FSize = 0;',
  29174. ' };',
  29175. ' this.$final = function () {',
  29176. ' };',
  29177. '});',
  29178. 'rtl.createHelper($mod, "THelper", null, function () {',
  29179. ' this.SetIt = function (w) {',
  29180. ' rtl.rc(w, 0, 65535);',
  29181. ' this.set(w);',
  29182. ' };',
  29183. '});',
  29184. 'this.GetIt = function () {',
  29185. ' var Result = 0;',
  29186. ' $mod.THelper.SetIt.call({',
  29187. ' get: function () {',
  29188. ' return Result;',
  29189. ' },',
  29190. ' set: function (v) {',
  29191. ' rtl.rc(v, 0, 255);',
  29192. ' Result = v;',
  29193. ' }',
  29194. ' }, 2);',
  29195. ' return Result;',
  29196. '};',
  29197. 'this.b = 3;',
  29198. 'this.o = null;',
  29199. '']),
  29200. LinesToStr([ // $mod.$main
  29201. '$mod.THelper.SetIt.call({',
  29202. ' p: $mod,',
  29203. ' get: function () {',
  29204. ' return this.p.b;',
  29205. ' },',
  29206. ' set: function (v) {',
  29207. ' rtl.rc(v, 0, 255);',
  29208. ' this.p.b = v;',
  29209. ' }',
  29210. '}, 14);',
  29211. 'var $with1 = $mod.b;',
  29212. '$mod.THelper.SetIt.call({',
  29213. ' get: function () {',
  29214. ' return $with1;',
  29215. ' },',
  29216. ' set: function (v) {',
  29217. ' rtl.rc(v, 0, 255);',
  29218. ' $with1 = v;',
  29219. ' }',
  29220. '}, 15);',
  29221. '$mod.THelper.SetIt.call({',
  29222. ' p: $mod.o,',
  29223. ' get: function () {',
  29224. ' return this.p.FSize;',
  29225. ' },',
  29226. ' set: function (v) {',
  29227. ' rtl.rc(v, 0, 255);',
  29228. ' this.p.FSize = v;',
  29229. ' }',
  29230. '}, 16);',
  29231. '']));
  29232. end;
  29233. Initialization
  29234. RegisterTests([TTestModule]);
  29235. end.