tcmodules.pas 861 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804
  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 = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject
  47. );
  48. TSystemUnitParts = set of TSystemUnitPart;
  49. { TTestHintMessage }
  50. TTestHintMessage = class
  51. public
  52. Id: int64;
  53. MsgType: TMessageType;
  54. MsgNumber: integer;
  55. Msg: string;
  56. SourcePos: TPasSourcePos;
  57. end;
  58. { TTestPasParser }
  59. TTestPasParser = Class(TPasParser)
  60. end;
  61. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  62. { TTestEnginePasResolver }
  63. TTestEnginePasResolver = class(TPas2JsResolver)
  64. private
  65. FFilename: string;
  66. FModule: TPasModule;
  67. FOnFindUnit: TOnFindUnit;
  68. FParser: TTestPasParser;
  69. FStreamResolver: TStreamResolver;
  70. FScanner: TPas2jsPasScanner;
  71. FSource: string;
  72. public
  73. destructor Destroy; override;
  74. function FindUnit(const AName, InFilename: String; NameExpr,
  75. InFileExpr: TPasExpr): TPasModule; override;
  76. procedure UsedInterfacesFinished(Section: TPasSection); override;
  77. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  78. property Filename: string read FFilename write FFilename;
  79. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  80. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  81. property Parser: TTestPasParser read FParser write FParser;
  82. property Source: string read FSource write FSource;
  83. property Module: TPasModule read FModule;
  84. end;
  85. { TCustomTestModule }
  86. TCustomTestModule = Class(TTestCase)
  87. private
  88. FConverter: TPasToJSConverter;
  89. FEngine: TTestEnginePasResolver;
  90. FExpectedErrorClass: ExceptClass;
  91. FExpectedErrorMsg: string;
  92. FExpectedErrorNumber: integer;
  93. FFilename: string;
  94. FFileResolver: TStreamResolver;
  95. FHub: TPas2JSResolverHub;
  96. FJSImplementationUses: TJSArrayLiteral;
  97. FJSInitBody: TJSFunctionBody;
  98. FJSImplentationUses: TJSArrayLiteral;
  99. FJSInterfaceUses: TJSArrayLiteral;
  100. FJSModule: TJSSourceElements;
  101. FJSModuleSrc: TJSSourceElements;
  102. FJSSource: TStringList;
  103. FModule: TPasModule;
  104. FJSModuleCallArgs: TJSArguments;
  105. FModules: TObjectList;// list of TTestEnginePasResolver
  106. FParser: TTestPasParser;
  107. FPasProgram: TPasProgram;
  108. FHintMsgs: TObjectList; // list of TTestHintMessage
  109. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  110. FJSRegModuleCall: TJSCallExpression;
  111. FScanner: TPas2jsPasScanner;
  112. FSkipTests: boolean;
  113. FSource: TStringList;
  114. FFirstPasStatement: TPasImplBlock;
  115. FWithTypeInfo: boolean;
  116. {$IFDEF EnablePasTreeGlobalRefCount}
  117. FElementRefCountAtSetup: int64;
  118. {$ENDIF}
  119. function GetMsgCount: integer;
  120. function GetMsgs(Index: integer): TTestHintMessage;
  121. function GetResolverCount: integer;
  122. function GetResolvers(Index: integer): TTestEnginePasResolver;
  123. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  124. procedure OnParserLog(Sender: TObject; const Msg: String);
  125. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  126. procedure OnScannerLog(Sender: TObject; const Msg: String);
  127. procedure SetWithTypeInfo(const AValue: boolean);
  128. protected
  129. procedure SetUp; override;
  130. function CreateConverter: TPasToJSConverter; virtual;
  131. function LoadUnit(const aUnitName: String): TPasModule;
  132. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  133. procedure TearDown; override;
  134. Procedure Add(Line: string); virtual;
  135. Procedure Add(const Lines: array of string);
  136. Procedure StartParsing; virtual;
  137. procedure ParseModuleQueue; virtual;
  138. procedure ParseModule; virtual;
  139. procedure ParseProgram; virtual;
  140. procedure ParseUnit; virtual;
  141. protected
  142. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  143. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  144. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  145. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  146. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  147. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  148. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  149. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  150. procedure ConvertModule; virtual;
  151. procedure ConvertProgram; virtual;
  152. procedure ConvertUnit; virtual;
  153. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  154. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  155. function GetDottedIdentifier(El: TJSElement): string;
  156. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  157. ImplStatements: string = ''); virtual;
  158. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  159. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  160. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  161. Msg: string; Marker: PSrcMarker = nil); virtual;
  162. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  163. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  164. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  166. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  167. function IsErrorExpected(E: Exception): boolean;
  168. procedure HandleScannerError(E: EScannerError);
  169. procedure HandleParserError(E: EParserError);
  170. procedure HandlePasResolveError(E: EPasResolve);
  171. procedure HandlePas2JSError(E: EPas2JS);
  172. procedure HandleException(E: Exception);
  173. procedure FailException(E: Exception);
  174. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  175. function IndexOfResolver(const Filename: string): integer;
  176. function GetResolver(const Filename: string): TTestEnginePasResolver;
  177. function GetDefaultNamespace: string;
  178. property PasProgram: TPasProgram Read FPasProgram;
  179. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  180. property ResolverCount: integer read GetResolverCount;
  181. property Engine: TTestEnginePasResolver read FEngine;
  182. property Filename: string read FFilename;
  183. Property Module: TPasModule Read FModule;
  184. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  185. property Converter: TPasToJSConverter read FConverter;
  186. property JSSource: TStringList read FJSSource;
  187. property JSModule: TJSSourceElements read FJSModule;
  188. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  189. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  190. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  191. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  192. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  193. property JSInitBody: TJSFunctionBody read FJSInitBody;
  194. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  195. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  196. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  197. property SkipTests: boolean read FSkipTests write FSkipTests;
  198. public
  199. constructor Create; override;
  200. destructor Destroy; override;
  201. property Hub: TPas2JSResolverHub read FHub;
  202. property Source: TStringList read FSource;
  203. property FileResolver: TStreamResolver read FFileResolver;
  204. property Scanner: TPas2jsPasScanner read FScanner;
  205. property Parser: TTestPasParser read FParser;
  206. property MsgCount: integer read GetMsgCount;
  207. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  208. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  209. end;
  210. { TTestModule }
  211. TTestModule = class(TCustomTestModule)
  212. Published
  213. Procedure TestReservedWords;
  214. // program/units
  215. Procedure TestEmptyProgram;
  216. Procedure TestEmptyProgramUseStrict;
  217. Procedure TestEmptyUnit;
  218. Procedure TestEmptyUnitUseStrict;
  219. Procedure TestDottedUnitNames;
  220. Procedure TestDottedUnitNameImpl;
  221. Procedure TestDottedUnitExpr;
  222. Procedure Test_ModeFPCFail;
  223. Procedure Test_ModeSwitchCBlocksFail;
  224. Procedure TestUnit_UseSystem;
  225. Procedure TestUnit_Intf1Impl2Intf1;
  226. Procedure TestIncludeVersion;
  227. // vars/const
  228. Procedure TestVarInt;
  229. Procedure TestVarBaseTypes;
  230. Procedure TestBaseTypeSingleFail;
  231. Procedure TestBaseTypeExtendedFail;
  232. Procedure TestConstBaseTypes;
  233. Procedure TestUnitImplVars;
  234. Procedure TestUnitImplConsts;
  235. Procedure TestUnitImplRecord;
  236. Procedure TestRenameJSNameConflict;
  237. Procedure TestLocalConst;
  238. Procedure TestVarExternal;
  239. Procedure TestVarExternalOtherUnit;
  240. Procedure TestVarAbsoluteFail;
  241. Procedure TestConstExternal;
  242. // numbers
  243. Procedure TestDouble;
  244. Procedure TestInteger;
  245. Procedure TestIntegerRange;
  246. Procedure TestIntegerTypecasts;
  247. Procedure TestInteger_BitwiseShrNativeInt;
  248. Procedure TestInteger_BitwiseShlNativeInt;
  249. Procedure TestInteger_SystemFunc;
  250. Procedure TestCurrency;
  251. Procedure TestForBoolDo;
  252. Procedure TestForIntDo;
  253. Procedure TestForIntInDo;
  254. // strings
  255. Procedure TestCharConst;
  256. Procedure TestChar_Compare;
  257. Procedure TestChar_BuiltInProcs;
  258. Procedure TestStringConst;
  259. Procedure TestStringConstSurrogate;
  260. Procedure TestString_Length;
  261. Procedure TestString_Compare;
  262. Procedure TestString_SetLength;
  263. Procedure TestString_CharAt;
  264. Procedure TestStringHMinusFail;
  265. Procedure TestStr;
  266. Procedure TestBaseType_AnsiStringFail;
  267. Procedure TestBaseType_WideStringFail;
  268. Procedure TestBaseType_ShortStringFail;
  269. Procedure TestBaseType_RawByteStringFail;
  270. Procedure TestTypeShortstring_Fail;
  271. Procedure TestCharSet_Custom;
  272. Procedure TestWideChar;
  273. Procedure TestForCharDo;
  274. Procedure TestForCharInDo;
  275. // alias types
  276. Procedure TestAliasTypeRef;
  277. Procedure TestTypeCast_BaseTypes;
  278. Procedure TestTypeCast_AliasBaseTypes;
  279. // functions
  280. Procedure TestEmptyProc;
  281. Procedure TestProcOneParam;
  282. Procedure TestFunctionWithoutParams;
  283. Procedure TestProcedureWithoutParams;
  284. Procedure TestPrgProcVar;
  285. Procedure TestProcTwoArgs;
  286. Procedure TestProc_DefaultValue;
  287. Procedure TestUnitProcVar;
  288. Procedure TestImplProc;
  289. Procedure TestFunctionResult;
  290. Procedure TestNestedProc;
  291. Procedure TestNestedProc_ResultString;
  292. Procedure TestForwardProc;
  293. Procedure TestNestedForwardProc;
  294. Procedure TestAssignFunctionResult;
  295. Procedure TestFunctionResultInCondition;
  296. Procedure TestFunctionResultInForLoop;
  297. Procedure TestFunctionResultInTypeCast;
  298. Procedure TestExit;
  299. Procedure TestExit_ResultInFinally;
  300. Procedure TestBreak;
  301. Procedure TestBreakAsVar;
  302. Procedure TestContinue;
  303. Procedure TestProc_External;
  304. Procedure TestProc_ExternalOtherUnit;
  305. Procedure TestProc_Asm;
  306. Procedure TestProc_Assembler;
  307. Procedure TestProc_VarParam;
  308. Procedure TestProc_VarParamString;
  309. Procedure TestProc_VarParamV;
  310. Procedure TestProc_Overload;
  311. Procedure TestProc_OverloadForward;
  312. Procedure TestProc_OverloadIntfImpl;
  313. Procedure TestProc_OverloadNested;
  314. Procedure TestProc_OverloadNestedForward;
  315. Procedure TestProc_OverloadUnitCycle;
  316. Procedure TestProc_Varargs;
  317. Procedure TestProc_ConstOrder;
  318. Procedure TestProc_DuplicateConst;
  319. Procedure TestProc_LocalVarAbsolute;
  320. Procedure TestProc_LocalVarInit;
  321. Procedure TestProc_ReservedWords;
  322. Procedure TestProc_ConstRefWord;
  323. // anonymous functions
  324. Procedure TestAnonymousProc_Assign_ObjFPC;
  325. Procedure TestAnonymousProc_Assign_Delphi;
  326. Procedure TestAnonymousProc_Arg;
  327. Procedure TestAnonymousProc_Typecast;
  328. Procedure TestAnonymousProc_With;
  329. Procedure TestAnonymousProc_ExceptOn;
  330. Procedure TestAnonymousProc_Nested;
  331. Procedure TestAnonymousProc_NestedAssignResult;
  332. Procedure TestAnonymousProc_Class;
  333. Procedure TestAnonymousProc_ForLoop;
  334. Procedure TestAnonymousProc_AsmDelphi;
  335. // enums, sets
  336. Procedure TestEnum_Name;
  337. Procedure TestEnum_Number;
  338. Procedure TestEnum_ConstFail;
  339. Procedure TestEnum_Functions;
  340. Procedure TestEnumRg_Functions;
  341. Procedure TestEnum_AsParams;
  342. Procedure TestEnumRange_Array;
  343. Procedure TestEnum_ForIn;
  344. Procedure TestEnum_ScopedNumber;
  345. Procedure TestEnum_InFunction;
  346. Procedure TestSet_Enum;
  347. Procedure TestSet_Operators;
  348. Procedure TestSet_Operator_In;
  349. Procedure TestSet_Functions;
  350. Procedure TestSet_PassAsArgClone;
  351. Procedure TestSet_AsParams;
  352. Procedure TestSet_Property;
  353. Procedure TestSet_EnumConst;
  354. Procedure TestSet_IntConst;
  355. Procedure TestSet_AnonymousEnumType;
  356. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  357. Procedure TestSet_ConstEnum;
  358. Procedure TestSet_ConstChar;
  359. Procedure TestSet_ConstInt;
  360. Procedure TestSet_InFunction;
  361. Procedure TestSet_ForIn;
  362. // statements
  363. Procedure TestNestBegin;
  364. Procedure TestIncDec;
  365. Procedure TestLoHiFpcMode;
  366. Procedure TestLoHiDelphiMode;
  367. Procedure TestAssignments;
  368. Procedure TestArithmeticOperators1;
  369. Procedure TestLogicalOperators;
  370. Procedure TestBitwiseOperators;
  371. Procedure TestBitwiseOperatorsLongword;
  372. Procedure TestFunctionInt;
  373. Procedure TestFunctionString;
  374. Procedure TestIfThen;
  375. Procedure TestForLoop;
  376. Procedure TestForLoopInsideFunction;
  377. Procedure TestForLoop_ReadVarAfter;
  378. Procedure TestForLoop_Nested;
  379. Procedure TestRepeatUntil;
  380. Procedure TestAsmBlock;
  381. Procedure TestAsmPas_Impl; // ToDo
  382. Procedure TestTryFinally;
  383. Procedure TestTryExcept;
  384. Procedure TestTryExcept_ReservedWords;
  385. Procedure TestIfThenRaiseElse;
  386. Procedure TestCaseOf;
  387. Procedure TestCaseOf_UseSwitch;
  388. Procedure TestCaseOfNoElse;
  389. Procedure TestCaseOfNoElse_UseSwitch;
  390. Procedure TestCaseOfRange;
  391. Procedure TestCaseOfString;
  392. Procedure TestCaseOfChar;
  393. Procedure TestCaseOfExternalClassConst;
  394. Procedure TestDebugger;
  395. // arrays
  396. Procedure TestArray_Dynamic;
  397. Procedure TestArray_Dynamic_Nil;
  398. Procedure TestArray_DynMultiDimensional;
  399. Procedure TestArray_DynamicAssign;
  400. Procedure TestArray_StaticInt;
  401. Procedure TestArray_StaticBool;
  402. Procedure TestArray_StaticChar;
  403. Procedure TestArray_StaticMultiDim;
  404. Procedure TestArray_StaticInFunction;
  405. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  406. Procedure TestArrayOfRecord;
  407. Procedure TestArray_StaticRecord;
  408. Procedure TestArrayOfSet;
  409. Procedure TestArray_DynAsParam;
  410. Procedure TestArray_StaticAsParam;
  411. Procedure TestArrayElement_AsParams;
  412. Procedure TestArrayElementFromFuncResult_AsParams;
  413. Procedure TestArrayEnumTypeRange;
  414. Procedure TestArray_SetLengthOutArg;
  415. Procedure TestArray_SetLengthProperty;
  416. Procedure TestArray_SetLengthMultiDim;
  417. Procedure TestArray_SetLengthDynOfStatic;
  418. Procedure TestArray_OpenArrayOfString;
  419. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  420. Procedure TestArray_ConstRef;
  421. Procedure TestArray_Concat;
  422. Procedure TestArray_Copy;
  423. Procedure TestArray_InsertDelete;
  424. Procedure TestArray_DynArrayConstObjFPC;
  425. Procedure TestArray_DynArrayConstDelphi;
  426. Procedure TestArray_ArrayLitAsParam;
  427. Procedure TestArray_ArrayLitMultiDimAsParam;
  428. Procedure TestArray_ArrayLitStaticAsParam;
  429. Procedure TestArray_ForInArrOfString;
  430. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  431. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  432. Procedure TestArrayOfConst_TVarRec;
  433. Procedure TestArrayOfConst_PassBaseTypes;
  434. Procedure TestArrayOfConst_PassObj;
  435. // record
  436. Procedure TestRecord_Empty;
  437. Procedure TestRecord_Var;
  438. Procedure TestRecord_VarExternal;
  439. Procedure TestRecord_WithDo;
  440. Procedure TestRecord_Assign;
  441. Procedure TestRecord_AsParams;
  442. Procedure TestRecord_ConstRef;
  443. Procedure TestRecordElement_AsParams;
  444. Procedure TestRecordElementFromFuncResult_AsParams;
  445. Procedure TestRecordElementFromWith_AsParams;
  446. Procedure TestRecord_Equal;
  447. Procedure TestRecord_JSValue;
  448. Procedure TestRecord_VariantFail;
  449. Procedure TestRecord_FieldArray;
  450. Procedure TestRecord_Const;
  451. Procedure TestRecord_TypecastFail;
  452. Procedure TestRecord_InFunction;
  453. Procedure TestRecord_AnonymousFail;
  454. // advanced record
  455. Procedure TestAdvRecord_Function;
  456. Procedure TestAdvRecord_Property;
  457. Procedure TestAdvRecord_PropertyDefault;
  458. Procedure TestAdvRecord_Property_ClassMethod;
  459. Procedure TestAdvRecord_Const;
  460. Procedure TestAdvRecord_ExternalField;
  461. Procedure TestAdvRecord_SubRecord;
  462. Procedure TestAdvRecord_SubClass;
  463. Procedure TestAdvRecord_SubInterfaceFail;
  464. Procedure TestAdvRecord_Constructor;
  465. Procedure TestAdvRecord_ClassConstructor_Program;
  466. Procedure TestAdvRecord_ClassConstructor_Unit;
  467. // classes
  468. Procedure TestClass_TObjectDefaultConstructor;
  469. Procedure TestClass_TObjectConstructorWithParams;
  470. Procedure TestClass_TObjectConstructorWithDefaultParam;
  471. Procedure TestClass_Var;
  472. Procedure TestClass_Method;
  473. Procedure TestClass_Implementation;
  474. Procedure TestClass_Inheritance;
  475. Procedure TestClass_TypeAlias;
  476. Procedure TestClass_AbstractMethod;
  477. Procedure TestClass_CallInherited_ProcNoParams;
  478. Procedure TestClass_CallInherited_WithParams;
  479. Procedure TestClasS_CallInheritedConstructor;
  480. Procedure TestClass_ClassVar_Assign;
  481. Procedure TestClass_CallClassMethod;
  482. Procedure TestClass_Property;
  483. Procedure TestClass_Property_ClassMethod;
  484. Procedure TestClass_Property_Indexed;
  485. Procedure TestClass_Property_IndexSpec;
  486. Procedure TestClass_PropertyOfTypeArray;
  487. Procedure TestClass_PropertyDefault;
  488. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  489. //Procedure TestClass_PropertyDefault;
  490. Procedure TestClass_PropertyOverride;
  491. Procedure TestClass_PropertyIncVisibility;
  492. Procedure TestClass_Assigned;
  493. Procedure TestClass_WithClassDoCreate;
  494. Procedure TestClass_WithClassInstDoProperty;
  495. Procedure TestClass_WithClassInstDoPropertyWithParams;
  496. Procedure TestClass_WithClassInstDoFunc;
  497. Procedure TestClass_TypeCast;
  498. Procedure TestClass_TypeCastUntypedParam;
  499. Procedure TestClass_Overloads;
  500. Procedure TestClass_OverloadsAncestor;
  501. Procedure TestClass_OverloadConstructor;
  502. Procedure TestClass_OverloadDelphiOverride;
  503. Procedure TestClass_ReintroduceVarDelphi;
  504. Procedure TestClass_ReintroducedVar;
  505. Procedure TestClass_RaiseDescendant;
  506. Procedure TestClass_ExternalMethod;
  507. Procedure TestClass_ExternalVirtualNameMismatchFail;
  508. Procedure TestClass_ExternalOverrideFail;
  509. Procedure TestClass_ExternalVar;
  510. Procedure TestClass_Const;
  511. Procedure TestClass_ConstEnum;
  512. Procedure TestClass_LocalConstDuplicate_Prg;
  513. Procedure TestClass_LocalConstDuplicate_Unit;
  514. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  515. Procedure TestClass_LocalVarSelfFail;
  516. Procedure TestClass_ArgSelfFail;
  517. Procedure TestClass_NestedProcSelf;
  518. Procedure TestClass_NestedProcSelf2;
  519. Procedure TestClass_NestedProcClassSelf;
  520. Procedure TestClass_NestedProcCallInherited;
  521. Procedure TestClass_TObjectFree;
  522. Procedure TestClass_TObjectFree_VarArg;
  523. Procedure TestClass_TObjectFreeNewInstance;
  524. Procedure TestClass_TObjectFreeLowerCase;
  525. Procedure TestClass_TObjectFreeFunctionFail;
  526. Procedure TestClass_TObjectFreePropertyFail;
  527. Procedure TestClass_ForIn;
  528. Procedure TestClass_DispatchMessage;
  529. Procedure TestClass_Message_DuplicateIntFail;
  530. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  531. // class of
  532. Procedure TestClassOf_Create;
  533. Procedure TestClassOf_Call;
  534. Procedure TestClassOf_Assign;
  535. Procedure TestClassOf_Is;
  536. Procedure TestClassOf_Compare;
  537. Procedure TestClassOf_ClassVar;
  538. Procedure TestClassOf_ClassMethod;
  539. Procedure TestClassOf_ClassProperty;
  540. Procedure TestClassOf_ClassMethodSelf;
  541. Procedure TestClassOf_TypeCast;
  542. Procedure TestClassOf_ImplicitFunctionCall;
  543. Procedure TestClassOf_Const;
  544. // nested class
  545. Procedure TestNestedClass_Alias;
  546. Procedure TestNestedClass_Record;
  547. Procedure TestNestedClass_Class;
  548. // external class
  549. Procedure TestExternalClass_Var;
  550. Procedure TestExternalClass_Const;
  551. Procedure TestExternalClass_Dollar;
  552. Procedure TestExternalClass_DuplicateVarFail;
  553. Procedure TestExternalClass_Method;
  554. Procedure TestExternalClass_ClassMethod;
  555. Procedure TestExternalClass_ClassMethodStatic;
  556. Procedure TestExternalClass_FunctionResultInTypeCast;
  557. Procedure TestExternalClass_NonExternalOverride;
  558. Procedure TestExternalClass_OverloadHint;
  559. Procedure TestExternalClass_SameNamePublishedProperty;
  560. Procedure TestExternalClass_Property;
  561. Procedure TestExternalClass_PropertyDate;
  562. Procedure TestExternalClass_ClassProperty;
  563. Procedure TestExternalClass_ClassOf;
  564. Procedure TestExternalClass_ClassOtherUnit;
  565. Procedure TestExternalClass_Is;
  566. Procedure TestExternalClass_As;
  567. Procedure TestExternalClass_DestructorFail;
  568. Procedure TestExternalClass_New;
  569. Procedure TestExternalClass_ClassOf_New;
  570. Procedure TestExternalClass_FuncClassOf_New;
  571. Procedure TestExternalClass_New_PasClassFail;
  572. Procedure TestExternalClass_New_PasClassBracketsFail;
  573. Procedure TestExternalClass_NewExtName;
  574. Procedure TestExternalClass_Constructor;
  575. Procedure TestExternalClass_ConstructorBrackets;
  576. Procedure TestExternalClass_LocalConstSameName;
  577. Procedure TestExternalClass_ReintroduceOverload;
  578. Procedure TestExternalClass_Inherited;
  579. Procedure TestExternalClass_PascalAncestorFail;
  580. Procedure TestExternalClass_NewInstance;
  581. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  582. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  583. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  584. Procedure TestExternalClass_JSFunctionPasDescendant;
  585. Procedure TestExternalClass_PascalProperty;
  586. Procedure TestExternalClass_TypeCastToRootClass;
  587. Procedure TestExternalClass_TypeCastToJSObject;
  588. Procedure TestExternalClass_TypeCastStringToExternalString;
  589. Procedure TestExternalClass_TypeCastToJSFunction;
  590. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  591. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  592. Procedure TestExternalClass_BracketAccessor;
  593. Procedure TestExternalClass_BracketAccessor_Call;
  594. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  595. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  596. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  597. Procedure TestExternalClass_BracketAccessor_MultiType;
  598. Procedure TestExternalClass_BracketAccessor_Index;
  599. Procedure TestExternalClass_ForInJSObject;
  600. Procedure TestExternalClass_ForInJSArray;
  601. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  602. // class interfaces
  603. Procedure TestClassInterface_Corba;
  604. Procedure TestClassInterface_ProcExternalFail;
  605. Procedure TestClassInterface_Overloads;
  606. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  607. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  608. Procedure TestClassInterface_AncestorImpl;
  609. Procedure TestClassInterface_ImplReintroduce;
  610. Procedure TestClassInterface_MethodResolution;
  611. Procedure TestClassInterface_AncestorMoreInterfaces;
  612. Procedure TestClassInterface_MethodOverride;
  613. Procedure TestClassInterface_Corba_Delegation;
  614. Procedure TestClassInterface_Corba_DelegationStatic;
  615. Procedure TestClassInterface_Corba_Operators;
  616. Procedure TestClassInterface_Corba_Args;
  617. Procedure TestClassInterface_Corba_ForIn;
  618. Procedure TestClassInterface_COM_AssignVar;
  619. Procedure TestClassInterface_COM_AssignArg;
  620. Procedure TestClassInterface_COM_FunctionResult;
  621. Procedure TestClassInterface_COM_InheritedFuncResult;
  622. Procedure TestClassInterface_COM_IsAsTypeCasts;
  623. Procedure TestClassInterface_COM_PassAsArg;
  624. Procedure TestClassInterface_COM_PassToUntypedParam;
  625. Procedure TestClassInterface_COM_FunctionInExpr;
  626. Procedure TestClassInterface_COM_Property;
  627. Procedure TestClassInterface_COM_IntfProperty;
  628. Procedure TestClassInterface_COM_Delegation;
  629. Procedure TestClassInterface_COM_With;
  630. Procedure TestClassInterface_COM_ForIn;
  631. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  632. Procedure TestClassInterface_COM_RecordIntfFail;
  633. Procedure TestClassInterface_COM_UnitInitialization;
  634. Procedure TestClassInterface_GUID;
  635. Procedure TestClassInterface_GUIDProperty;
  636. // helpers
  637. Procedure TestClassHelper_ClassVar;
  638. Procedure TestClassHelper_Method_AccessInstanceFields;
  639. Procedure TestClassHelper_Method_Call;
  640. Procedure TestClassHelper_Method_Nested_Call;
  641. Procedure TestClassHelper_ClassMethod_Call;
  642. Procedure TestClassHelper_ClassOf;
  643. Procedure TestClassHelper_MethodRefObjFPC;
  644. Procedure TestClassHelper_Constructor;
  645. Procedure TestClassHelper_InheritedObjFPC;
  646. Procedure TestClassHelper_Property;
  647. Procedure TestClassHelper_Property_Array;
  648. Procedure TestClassHelper_Property_Array_Default;
  649. Procedure TestClassHelper_Property_Array_DefaultDefault;
  650. Procedure TestClassHelper_ClassProperty;
  651. Procedure TestClassHelper_ClassPropertyStatic;
  652. Procedure TestClassHelper_ClassProperty_Array;
  653. Procedure TestClassHelper_ForIn;
  654. Procedure TestClassHelper_PassProperty;
  655. Procedure TestExtClassHelper_ClassVar;
  656. Procedure TestExtClassHelper_Method_Call;
  657. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  658. Procedure TestRecordHelper_ClassVar;
  659. Procedure TestRecordHelper_Method_Call;
  660. Procedure TestRecordHelper_Constructor;
  661. Procedure TestTypeHelper_ClassVar;
  662. Procedure TestTypeHelper_PassResultElement;
  663. Procedure TestTypeHelper_PassArgs;
  664. Procedure TestTypeHelper_PassVarConst;
  665. Procedure TestTypeHelper_PassFuncResult;
  666. Procedure TestTypeHelper_PassPropertyField;
  667. Procedure TestTypeHelper_PassPropertyGetter;
  668. Procedure TestTypeHelper_PassClassPropertyField;
  669. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  670. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  671. Procedure TestTypeHelper_Property;
  672. Procedure TestTypeHelper_Property_Array;
  673. Procedure TestTypeHelper_ClassProperty;
  674. Procedure TestTypeHelper_ClassProperty_Array;
  675. Procedure TestTypeHelper_ClassMethod;
  676. Procedure TestTypeHelper_ExtClassMethodFail;
  677. Procedure TestTypeHelper_Constructor;
  678. Procedure TestTypeHelper_Word;
  679. Procedure TestTypeHelper_Boolean;
  680. Procedure TestTypeHelper_WordBool;
  681. Procedure TestTypeHelper_Double;
  682. Procedure TestTypeHelper_NativeInt;
  683. Procedure TestTypeHelper_StringChar;
  684. Procedure TestTypeHelper_JSValue;
  685. Procedure TestTypeHelper_Array;
  686. Procedure TestTypeHelper_EnumType;
  687. Procedure TestTypeHelper_SetType;
  688. Procedure TestTypeHelper_InterfaceType;
  689. Procedure TestTypeHelper_NestedSelf;
  690. // proc types
  691. Procedure TestProcType;
  692. Procedure TestProcType_Arg;
  693. Procedure TestProcType_FunctionFPC;
  694. Procedure TestProcType_FunctionDelphi;
  695. Procedure TestProcType_ProcedureDelphi;
  696. Procedure TestProcType_AsParam;
  697. Procedure TestProcType_MethodFPC;
  698. Procedure TestProcType_MethodDelphi;
  699. Procedure TestProcType_PropertyFPC;
  700. Procedure TestProcType_PropertyDelphi;
  701. Procedure TestProcType_WithClassInstDoPropertyFPC;
  702. Procedure TestProcType_Nested;
  703. Procedure TestProcType_NestedOfObject;
  704. Procedure TestProcType_ReferenceToProc;
  705. Procedure TestProcType_ReferenceToMethod;
  706. Procedure TestProcType_Typecast;
  707. Procedure TestProcType_PassProcToUntyped;
  708. Procedure TestProcType_PassProcToArray;
  709. Procedure TestProcType_SafeCallObjFPC;
  710. Procedure TestProcType_SafeCallDelphi;
  711. // pointer
  712. Procedure TestPointer;
  713. Procedure TestPointer_Proc;
  714. Procedure TestPointer_AssignRecordFail;
  715. Procedure TestPointer_AssignStaticArrayFail;
  716. Procedure TestPointer_TypeCastJSValueToPointer;
  717. Procedure TestPointer_NonRecordFail;
  718. Procedure TestPointer_AnonymousArgTypeFail;
  719. Procedure TestPointer_AnonymousVarTypeFail;
  720. Procedure TestPointer_AnonymousResultTypeFail;
  721. Procedure TestPointer_AddrOperatorFail;
  722. Procedure TestPointer_ArrayParamsFail;
  723. Procedure TestPointer_PointerAddFail;
  724. Procedure TestPointer_IncPointerFail;
  725. Procedure TestPointer_Record;
  726. Procedure TestPointer_RecordArg;
  727. // jsvalue
  728. Procedure TestJSValue_AssignToJSValue;
  729. Procedure TestJSValue_TypeCastToBaseType;
  730. Procedure TestJSValue_TypecastToJSValue;
  731. Procedure TestJSValue_Equal;
  732. Procedure TestJSValue_If;
  733. Procedure TestJSValue_Not;
  734. Procedure TestJSValue_Enum;
  735. Procedure TestJSValue_ClassInstance;
  736. Procedure TestJSValue_ClassOf;
  737. Procedure TestJSValue_ArrayOfJSValue;
  738. Procedure TestJSValue_ArrayLit;
  739. Procedure TestJSValue_Params;
  740. Procedure TestJSValue_UntypedParam;
  741. Procedure TestJSValue_FuncResultType;
  742. Procedure TestJSValue_ProcType_Assign;
  743. Procedure TestJSValue_ProcType_Equal;
  744. Procedure TestJSValue_ProcType_Param;
  745. Procedure TestJSValue_AssignToPointerFail;
  746. Procedure TestJSValue_OverloadDouble;
  747. Procedure TestJSValue_OverloadNativeInt;
  748. Procedure TestJSValue_OverloadWord;
  749. Procedure TestJSValue_OverloadString;
  750. Procedure TestJSValue_OverloadChar;
  751. Procedure TestJSValue_OverloadPointer;
  752. Procedure TestJSValue_ForIn;
  753. // RTTI
  754. Procedure TestRTTI_IntRange;
  755. Procedure TestRTTI_Double;
  756. Procedure TestRTTI_ProcType;
  757. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  758. Procedure TestRTTI_EnumAndSetType;
  759. Procedure TestRTTI_EnumRange;
  760. Procedure TestRTTI_AnonymousEnumType;
  761. Procedure TestRTTI_StaticArray;
  762. Procedure TestRTTI_DynArray;
  763. Procedure TestRTTI_ArrayNestedAnonymous;
  764. Procedure TestRTTI_PublishedMethodOverloadFail;
  765. Procedure TestRTTI_PublishedMethodExternalFail;
  766. Procedure TestRTTI_PublishedClassPropertyFail;
  767. Procedure TestRTTI_PublishedClassFieldFail;
  768. Procedure TestRTTI_PublishedFieldExternalFail;
  769. Procedure TestRTTI_Class_Field;
  770. Procedure TestRTTI_Class_Method;
  771. Procedure TestRTTI_Class_MethodArgFlags;
  772. Procedure TestRTTI_Class_Property;
  773. Procedure TestRTTI_Class_PropertyParams;
  774. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  775. Procedure TestRTTI_Class_OmitRTTI;
  776. Procedure TestRTTI_IndexModifier;
  777. Procedure TestRTTI_StoredModifier;
  778. Procedure TestRTTI_DefaultValue;
  779. Procedure TestRTTI_DefaultValueSet;
  780. Procedure TestRTTI_DefaultValueRangeType;
  781. Procedure TestRTTI_DefaultValueInherit;
  782. Procedure TestRTTI_OverrideMethod;
  783. Procedure TestRTTI_ReintroduceMethod;
  784. Procedure TestRTTI_OverloadProperty;
  785. // ToDo: array argument
  786. Procedure TestRTTI_ClassForward;
  787. Procedure TestRTTI_ClassOf;
  788. Procedure TestRTTI_Record;
  789. Procedure TestRTTI_RecordAnonymousArray;
  790. Procedure TestRTTI_LocalTypes;
  791. Procedure TestRTTI_TypeInfo_BaseTypes;
  792. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  793. Procedure TestRTTI_TypeInfo_LocalFail;
  794. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  795. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  796. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  797. Procedure TestRTTI_TypeInfo_FunctionClassType;
  798. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  799. Procedure TestRTTI_Interface_Corba;
  800. Procedure TestRTTI_Interface_COM;
  801. Procedure TestRTTI_ClassHelper;
  802. Procedure TestRTTI_ExternalClass;
  803. // Resourcestring
  804. Procedure TestResourcestringProgram;
  805. Procedure TestResourcestringUnit;
  806. Procedure TestResourcestringImplementation;
  807. // Attributes
  808. Procedure TestAttributes_Members;
  809. Procedure TestAttributes_Types;
  810. Procedure TestAttributes_HelperConstructor_Fail;
  811. // Assertions, checks
  812. procedure TestAssert;
  813. procedure TestAssert_SysUtils;
  814. procedure TestObjectChecks;
  815. procedure TestOverflowChecks_Int;
  816. procedure TestRangeChecks_AssignInt;
  817. procedure TestRangeChecks_AssignIntRange;
  818. procedure TestRangeChecks_AssignEnum;
  819. procedure TestRangeChecks_AssignEnumRange;
  820. procedure TestRangeChecks_AssignChar;
  821. procedure TestRangeChecks_AssignCharRange;
  822. procedure TestRangeChecks_ArrayIndex;
  823. procedure TestRangeChecks_ArrayOfRecIndex;
  824. procedure TestRangeChecks_StringIndex;
  825. procedure TestRangeChecks_TypecastInt;
  826. procedure TestRangeChecks_TypeHelperInt;
  827. // Async/AWait
  828. Procedure TestAsync_Proc;
  829. Procedure TestAsync_CallResultIsPromise;
  830. Procedure TestAsync_ConstructorFail;
  831. Procedure TestAsync_PropertyGetterFail;
  832. Procedure TestAwait_NonPromiseWithTypeFail;
  833. Procedure TestAwait_AsyncCallTypeMismatch;
  834. Procedure TestAWait_OutsideAsyncFail;
  835. Procedure TestAWait_Result;
  836. Procedure TestAWait_ExternalClassPromise;
  837. Procedure TestAsync_AnonymousProc;
  838. Procedure TestAsync_ProcType;
  839. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  840. Procedure TestAsync_Inherited;
  841. Procedure TestAsync_ClassInterface;
  842. end;
  843. function LinesToStr(Args: array of const): string;
  844. function ExtractFileUnitName(aFilename: string): string;
  845. function JSToStr(El: TJSElement): string;
  846. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  847. implementation
  848. function LinesToStr(Args: array of const): string;
  849. var
  850. s: String;
  851. i: Integer;
  852. begin
  853. s:='';
  854. for i:=Low(Args) to High(Args) do
  855. case Args[i].VType of
  856. vtChar: s += Args[i].VChar+LineEnding;
  857. vtString: s += Args[i].VString^+LineEnding;
  858. vtPChar: s += Args[i].VPChar+LineEnding;
  859. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  860. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  861. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  862. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  863. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  864. end;
  865. Result:=s;
  866. end;
  867. function ExtractFileUnitName(aFilename: string): string;
  868. var
  869. p: Integer;
  870. begin
  871. Result:=ExtractFileName(aFilename);
  872. if Result='' then exit;
  873. for p:=length(Result) downto 1 do
  874. case Result[p] of
  875. '/','\': exit;
  876. '.':
  877. begin
  878. Delete(Result,p,length(Result));
  879. exit;
  880. end;
  881. end;
  882. end;
  883. function JSToStr(El: TJSElement): string;
  884. var
  885. aWriter: TBufferWriter;
  886. aJSWriter: TJSWriter;
  887. begin
  888. aJSWriter:=nil;
  889. aWriter:=TBufferWriter.Create(1000);
  890. try
  891. aJSWriter:=TJSWriter.Create(aWriter);
  892. aJSWriter.IndentSize:=2;
  893. aJSWriter.WriteJS(El);
  894. Result:=aWriter.AsString;
  895. finally
  896. aJSWriter.Free;
  897. aWriter.Free;
  898. end;
  899. end;
  900. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  901. // search diff, ignore changes in spaces
  902. const
  903. SpaceChars = [#9,#10,#13,' '];
  904. var
  905. ExpectedP, ActualP: PChar;
  906. function FindLineEnd(p: PChar): PChar;
  907. begin
  908. Result:=p;
  909. while not (Result^ in [#0,#10,#13]) do inc(Result);
  910. end;
  911. function FindLineStart(p, MinP: PChar): PChar;
  912. begin
  913. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  914. Result:=p;
  915. end;
  916. procedure SkipLineEnd(var p: PChar);
  917. begin
  918. if p^ in [#10,#13] then
  919. begin
  920. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  921. inc(p,2)
  922. else
  923. inc(p);
  924. end;
  925. end;
  926. procedure DiffFound;
  927. var
  928. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  929. ExpLine, ActLine: String;
  930. i, LineNo, DiffLineNo: Integer;
  931. begin
  932. writeln('Diff found "',Msg,'". Lines:');
  933. // write correct lines
  934. p:=PChar(Expected);
  935. LineNo:=0;
  936. DiffLineNo:=0;
  937. repeat
  938. StartPos:=p;
  939. while not (p^ in [#0,#10,#13]) do inc(p);
  940. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  941. SkipLineEnd(p);
  942. inc(LineNo);
  943. if (p<=ExpectedP) and (p^<>#0) then
  944. begin
  945. writeln('= ',ExpLine);
  946. end else begin
  947. // diff line
  948. if DiffLineNo=0 then DiffLineNo:=LineNo;
  949. // write actual line
  950. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  951. ActLineEndP:=FindLineEnd(ActualP);
  952. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  953. writeln('- ',ActLine);
  954. // write expected line
  955. writeln('+ ',ExpLine);
  956. // write empty line with pointer ^
  957. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  958. writeln('^');
  959. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  960. CheckSrcDiff:=false;
  961. // write up to three following actual lines to get some context
  962. for i:=1 to 3 do begin
  963. ActLineStartP:=ActLineEndP;
  964. SkipLineEnd(ActLineStartP);
  965. if ActLineStartP^=#0 then break;
  966. ActLineEndP:=FindLineEnd(ActLineStartP);
  967. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  968. writeln('~ ',ActLine);
  969. end;
  970. exit;
  971. end;
  972. until p^=#0;
  973. writeln('DiffFound Actual:-----------------------');
  974. writeln(Actual);
  975. writeln('DiffFound Expected:---------------------');
  976. writeln(Expected);
  977. writeln('DiffFound ------------------------------');
  978. Msg:='diff found, but lines are the same, internal error';
  979. CheckSrcDiff:=false;
  980. end;
  981. var
  982. IsSpaceNeeded: Boolean;
  983. LastChar, Quote: Char;
  984. begin
  985. Result:=true;
  986. Msg:='';
  987. if Expected='' then Expected:=' ';
  988. if Actual='' then Actual:=' ';
  989. ExpectedP:=PChar(Expected);
  990. ActualP:=PChar(Actual);
  991. repeat
  992. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  993. case ExpectedP^ of
  994. #0:
  995. begin
  996. // check that rest of Actual has only spaces
  997. while ActualP^ in SpaceChars do inc(ActualP);
  998. if ActualP^<>#0 then
  999. begin
  1000. DiffFound;
  1001. exit;
  1002. end;
  1003. exit(true);
  1004. end;
  1005. ' ',#9,#10,#13:
  1006. begin
  1007. // skip space in Expected
  1008. IsSpaceNeeded:=false;
  1009. if ExpectedP>PChar(Expected) then
  1010. LastChar:=ExpectedP[-1]
  1011. else
  1012. LastChar:=#0;
  1013. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1014. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1015. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1016. IsSpaceNeeded:=true;
  1017. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1018. begin
  1019. DiffFound;
  1020. exit;
  1021. end;
  1022. while ActualP^ in SpaceChars do inc(ActualP);
  1023. end;
  1024. '''','"':
  1025. begin
  1026. while ActualP^ in SpaceChars do inc(ActualP);
  1027. if ExpectedP^<>ActualP^ then
  1028. begin
  1029. DiffFound;
  1030. exit;
  1031. end;
  1032. Quote:=ExpectedP^;
  1033. repeat
  1034. inc(ExpectedP);
  1035. inc(ActualP);
  1036. if ExpectedP^<>ActualP^ then
  1037. begin
  1038. DiffFound;
  1039. exit;
  1040. end;
  1041. if (ExpectedP^ in [#0,#10,#13]) then
  1042. break
  1043. else if (ExpectedP^=Quote) then
  1044. begin
  1045. inc(ExpectedP);
  1046. inc(ActualP);
  1047. break;
  1048. end;
  1049. until false;
  1050. end;
  1051. else
  1052. while ActualP^ in SpaceChars do inc(ActualP);
  1053. if ExpectedP^<>ActualP^ then
  1054. begin
  1055. DiffFound;
  1056. exit;
  1057. end;
  1058. inc(ExpectedP);
  1059. inc(ActualP);
  1060. end;
  1061. until false;
  1062. end;
  1063. { TTestEnginePasResolver }
  1064. destructor TTestEnginePasResolver.Destroy;
  1065. begin
  1066. FreeAndNil(FStreamResolver);
  1067. FreeAndNil(FParser);
  1068. FreeAndNil(FScanner);
  1069. FreeAndNil(FStreamResolver);
  1070. if Module<>nil then
  1071. begin
  1072. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1073. FModule:=nil;
  1074. end;
  1075. inherited Destroy;
  1076. end;
  1077. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1078. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1079. begin
  1080. Result:=nil;
  1081. if InFilename<>'' then
  1082. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1083. if Assigned(OnFindUnit) then
  1084. Result:=OnFindUnit(AName);
  1085. if NameExpr=nil then ;
  1086. end;
  1087. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1088. begin
  1089. // do not parse recursively
  1090. // parse via the queue
  1091. if Section=nil then ;
  1092. end;
  1093. { TCustomTestModule }
  1094. function TCustomTestModule.GetMsgCount: integer;
  1095. begin
  1096. Result:=FHintMsgs.Count;
  1097. end;
  1098. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1099. begin
  1100. Result:=TTestHintMessage(FHintMsgs[Index]);
  1101. end;
  1102. function TCustomTestModule.GetResolverCount: integer;
  1103. begin
  1104. Result:=FModules.Count;
  1105. end;
  1106. function TCustomTestModule.GetResolvers(Index: integer
  1107. ): TTestEnginePasResolver;
  1108. begin
  1109. Result:=TTestEnginePasResolver(FModules[Index]);
  1110. end;
  1111. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1112. ): TPasModule;
  1113. var
  1114. DefNamespace: String;
  1115. begin
  1116. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1117. if (Pos('.',aUnitName)<1) then
  1118. begin
  1119. DefNamespace:=GetDefaultNamespace;
  1120. if DefNamespace<>'' then
  1121. begin
  1122. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1123. if Result<>nil then exit;
  1124. end;
  1125. end;
  1126. Result:=LoadUnit(aUnitName);
  1127. if Result<>nil then exit;
  1128. {$IFDEF VerbosePas2JS}
  1129. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1130. {$ENDIF}
  1131. Fail('can''t find unit "'+aUnitName+'"');
  1132. end;
  1133. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1134. var
  1135. aParser: TPasParser;
  1136. Item: TTestHintMessage;
  1137. begin
  1138. aParser:=Sender as TPasParser;
  1139. Item:=TTestHintMessage.Create;
  1140. Item.Id:=aParser.LastMsgNumber;
  1141. Item.MsgType:=aParser.LastMsgType;
  1142. Item.MsgNumber:=aParser.LastMsgNumber;
  1143. Item.Msg:=Msg;
  1144. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1145. {$IFDEF VerbosePas2JS}
  1146. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1147. {$ENDIF}
  1148. FHintMsgs.Add(Item);
  1149. end;
  1150. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1151. );
  1152. var
  1153. aResolver: TTestEnginePasResolver;
  1154. Item: TTestHintMessage;
  1155. begin
  1156. aResolver:=Sender as TTestEnginePasResolver;
  1157. Item:=TTestHintMessage.Create;
  1158. Item.Id:=aResolver.LastMsgId;
  1159. Item.MsgType:=aResolver.LastMsgType;
  1160. Item.MsgNumber:=aResolver.LastMsgNumber;
  1161. Item.Msg:=Msg;
  1162. Item.SourcePos:=aResolver.LastSourcePos;
  1163. {$IFDEF VerbosePas2JS}
  1164. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1165. {$ENDIF}
  1166. FHintMsgs.Add(Item);
  1167. end;
  1168. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1169. var
  1170. Item: TTestHintMessage;
  1171. aScanner: TPas2jsPasScanner;
  1172. begin
  1173. aScanner:=Sender as TPas2jsPasScanner;
  1174. Item:=TTestHintMessage.Create;
  1175. Item.Id:=aScanner.LastMsgNumber;
  1176. Item.MsgType:=aScanner.LastMsgType;
  1177. Item.MsgNumber:=aScanner.LastMsgNumber;
  1178. Item.Msg:=Msg;
  1179. Item.SourcePos:=aScanner.CurSourcePos;
  1180. {$IFDEF VerbosePas2JS}
  1181. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1182. {$ENDIF}
  1183. FHintMsgs.Add(Item);
  1184. end;
  1185. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1186. begin
  1187. if FWithTypeInfo=AValue then Exit;
  1188. FWithTypeInfo:=AValue;
  1189. if AValue then
  1190. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1191. else
  1192. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1193. end;
  1194. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1195. var
  1196. i: Integer;
  1197. CurEngine: TTestEnginePasResolver;
  1198. CurUnitName: String;
  1199. begin
  1200. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1201. Result:=nil;
  1202. if (Module.ClassType=TPasModule)
  1203. and (CompareText(Module.Name,aUnitName)=0) then
  1204. exit(Module);
  1205. for i:=0 to ResolverCount-1 do
  1206. begin
  1207. CurEngine:=Resolvers[i];
  1208. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1209. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1210. if CompareText(aUnitName,CurUnitName)=0 then
  1211. begin
  1212. Result:=CurEngine.Module;
  1213. if Result<>nil then exit;
  1214. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1215. FileResolver.FindSourceFile(aUnitName);
  1216. CurEngine.StreamResolver:=TStreamResolver.Create;
  1217. CurEngine.StreamResolver.OwnsStreams:=True;
  1218. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1219. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1220. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1221. InitScanner(CurEngine.Scanner);
  1222. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1223. CurEngine.Parser.Options:=po_tcmodules;
  1224. if CompareText(CurUnitName,'System')=0 then
  1225. CurEngine.Parser.ImplicitUses.Clear;
  1226. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1227. try
  1228. CurEngine.Parser.NextToken;
  1229. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1230. except
  1231. on E: Exception do
  1232. HandleException(E);
  1233. end;
  1234. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1235. Result:=CurEngine.Module;
  1236. exit;
  1237. end;
  1238. end;
  1239. end;
  1240. procedure TCustomTestModule.SetUp;
  1241. begin
  1242. {$IFDEF EnablePasTreeGlobalRefCount}
  1243. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1244. {$ENDIF}
  1245. if FModules<>nil then
  1246. begin
  1247. writeln('TCustomTestModule.SetUp FModules<>nil');
  1248. Halt;
  1249. end;
  1250. inherited SetUp;
  1251. FSkipTests:=false;
  1252. FWithTypeInfo:=false;
  1253. FSource:=TStringList.Create;
  1254. FHub:=TPas2JSResolverHub.Create(Self);
  1255. FModules:=TObjectList.Create(true);
  1256. FFilename:='test1.pp';
  1257. FFileResolver:=TStreamResolver.Create;
  1258. FFileResolver.OwnsStreams:=True;
  1259. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1260. InitScanner(FScanner);
  1261. FEngine:=AddModule(Filename);
  1262. FEngine.Scanner:=FScanner;
  1263. FScanner.Resolver:=FEngine;
  1264. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1265. FParser.OnLog:=@OnParserLog;
  1266. FEngine.Parser:=FParser;
  1267. Parser.Options:=po_tcmodules;
  1268. FModule:=Nil;
  1269. FConverter:=CreateConverter;
  1270. FExpectedErrorClass:=nil;
  1271. end;
  1272. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1273. var
  1274. Options: TPasToJsConverterOptions;
  1275. begin
  1276. Result:=TPasToJSConverter.Create;
  1277. Options:=co_tcmodules;
  1278. if WithTypeInfo then
  1279. Exclude(Options,coNoTypeInfo)
  1280. else
  1281. Include(Options,coNoTypeInfo);
  1282. Result.Options:=Options;
  1283. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1284. end;
  1285. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1286. begin
  1287. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1288. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1289. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1290. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1291. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1292. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1293. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1294. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1295. aScanner.OnLog:=@OnScannerLog;
  1296. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1297. end;
  1298. procedure TCustomTestModule.TearDown;
  1299. {$IFDEF CheckPasTreeRefCount}
  1300. var
  1301. El: TPasElement;
  1302. {$ENDIF}
  1303. var
  1304. i: Integer;
  1305. CurModule: TPasModule;
  1306. begin
  1307. FHintMsgs.Clear;
  1308. FHintMsgsGood.Clear;
  1309. FSkipTests:=false;
  1310. FWithTypeInfo:=false;
  1311. FJSRegModuleCall:=nil;
  1312. FJSModuleCallArgs:=nil;
  1313. FJSImplentationUses:=nil;
  1314. FJSInterfaceUses:=nil;
  1315. FJSModuleSrc:=nil;
  1316. FJSInitBody:=nil;
  1317. FreeAndNil(FJSSource);
  1318. FreeAndNil(FJSModule);
  1319. FreeAndNil(FConverter);
  1320. Engine.Clear;
  1321. FreeAndNil(FSource);
  1322. FreeAndNil(FFileResolver);
  1323. if FModules<>nil then
  1324. begin
  1325. for i:=0 to FModules.Count-1 do
  1326. begin
  1327. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1328. if CurModule=nil then continue;
  1329. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1330. CurModule.ReleaseUsedUnits;
  1331. end;
  1332. if FModule<>nil then
  1333. FModule.ReleaseUsedUnits;
  1334. for i:=0 to FModules.Count-1 do
  1335. begin
  1336. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1337. if CurModule=nil then continue;
  1338. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1339. end;
  1340. FreeAndNil(FModules);
  1341. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1342. FEngine:=nil;
  1343. end;
  1344. FreeAndNil(FHub);
  1345. inherited TearDown;
  1346. {$IFDEF EnablePasTreeGlobalRefCount}
  1347. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1348. begin
  1349. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1350. {$IFDEF CheckPasTreeRefCount}
  1351. El:=TPasElement.FirstRefEl;
  1352. while El<>nil do
  1353. begin
  1354. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1355. for i:=0 to El.RefIds.Count-1 do
  1356. writeln(' ',El.RefIds[i]);
  1357. El:=El.NextRefEl;
  1358. end;
  1359. {$ENDIF}
  1360. Halt;
  1361. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1362. end;
  1363. {$ENDIF}
  1364. end;
  1365. procedure TCustomTestModule.Add(Line: string);
  1366. begin
  1367. Source.Add(Line);
  1368. end;
  1369. procedure TCustomTestModule.Add(const Lines: array of string);
  1370. var
  1371. i: Integer;
  1372. begin
  1373. for i:=low(Lines) to high(Lines) do
  1374. Add(Lines[i]);
  1375. end;
  1376. procedure TCustomTestModule.StartParsing;
  1377. var
  1378. Src: String;
  1379. begin
  1380. Src:=Source.Text;
  1381. FEngine.Source:=Src;
  1382. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1383. Scanner.OpenFile(FileName);
  1384. Writeln('// Test : ',Self.TestName);
  1385. Writeln(Src);
  1386. end;
  1387. procedure TCustomTestModule.ParseModuleQueue;
  1388. var
  1389. i: Integer;
  1390. CurResolver: TTestEnginePasResolver;
  1391. Found: Boolean;
  1392. Section: TPasSection;
  1393. begin
  1394. // parse til exception or all modules finished
  1395. while not SkipTests do
  1396. begin
  1397. Found:=false;
  1398. for i:=0 to ResolverCount-1 do
  1399. begin
  1400. CurResolver:=Resolvers[i];
  1401. if CurResolver.CurrentParser=nil then continue;
  1402. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1403. continue;
  1404. CurResolver.Parser.ParseContinue;
  1405. Found:=true;
  1406. break;
  1407. end;
  1408. if not Found then break;
  1409. end;
  1410. for i:=0 to ResolverCount-1 do
  1411. begin
  1412. CurResolver:=Resolvers[i];
  1413. if CurResolver.Parser=nil then
  1414. begin
  1415. if CurResolver.CurrentParser<>nil then
  1416. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1417. continue;
  1418. end;
  1419. if CurResolver.Parser.CurModule<>nil then
  1420. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1421. end;
  1422. end;
  1423. procedure TCustomTestModule.ParseModule;
  1424. begin
  1425. if SkipTests then exit;
  1426. FFirstPasStatement:=nil;
  1427. try
  1428. StartParsing;
  1429. Parser.ParseMain(FModule);
  1430. ParseModuleQueue;
  1431. except
  1432. on E: Exception do
  1433. HandleException(E);
  1434. end;
  1435. if SkipTests then exit;
  1436. AssertNotNull('Module resulted in Module',Module);
  1437. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1438. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1439. end;
  1440. procedure TCustomTestModule.ParseProgram;
  1441. begin
  1442. if SkipTests then exit;
  1443. ParseModule;
  1444. if SkipTests then exit;
  1445. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1446. FPasProgram:=TPasProgram(Module);
  1447. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1448. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1449. if (PasProgram.InitializationSection.Elements.Count>0) then
  1450. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1451. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1452. end;
  1453. procedure TCustomTestModule.ParseUnit;
  1454. begin
  1455. if SkipTests then exit;
  1456. ParseModule;
  1457. if SkipTests then exit;
  1458. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1459. AssertNotNull('Has interface section',Module.InterfaceSection);
  1460. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1461. if (Module.InitializationSection<>nil)
  1462. and (Module.InitializationSection.Elements.Count>0)
  1463. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1464. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1465. end;
  1466. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1467. ): TTestEnginePasResolver;
  1468. var
  1469. i: Integer;
  1470. begin
  1471. for i:=0 to ResolverCount-1 do
  1472. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1473. exit(Resolvers[i]);
  1474. Result:=nil;
  1475. end;
  1476. function TCustomTestModule.AddModule(aFilename: string
  1477. ): TTestEnginePasResolver;
  1478. begin
  1479. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1480. if FindModuleWithFilename(aFilename)<>nil then
  1481. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1482. Result:=TTestEnginePasResolver.Create;
  1483. Result.Filename:=aFilename;
  1484. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1485. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1486. Result.OnLog:=@OnPasResolverLog;
  1487. Result.Hub:=Hub;
  1488. FModules.Add(Result);
  1489. end;
  1490. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1491. ): TTestEnginePasResolver;
  1492. begin
  1493. Result:=AddModule(aFilename);
  1494. Result.Source:=Src;
  1495. end;
  1496. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1497. ImplementationSrc: string): TTestEnginePasResolver;
  1498. var
  1499. Src: String;
  1500. begin
  1501. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1502. Src+=LineEnding;
  1503. Src+='interface'+LineEnding;
  1504. Src+=LineEnding;
  1505. Src+=InterfaceSrc;
  1506. Src+='implementation'+LineEnding;
  1507. Src+=LineEnding;
  1508. Src+=ImplementationSrc;
  1509. Src+='end.'+LineEnding;
  1510. Result:=AddModuleWithSrc(aFilename,Src);
  1511. end;
  1512. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1513. var
  1514. Intf, Impl: TStringList;
  1515. begin
  1516. Intf:=TStringList.Create;
  1517. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1518. // unit interface
  1519. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1520. Intf.Add('{$modeswitch externalclass}');
  1521. Intf.Add('type');
  1522. Intf.Add(' integer=longint;');
  1523. Intf.Add(' sizeint=nativeint;');
  1524. //'const',
  1525. //' LineEnding = #10;',
  1526. //' DirectorySeparator = ''/'';',
  1527. //' DriveSeparator = '''';',
  1528. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1529. //' AllowDriveSeparators : set of char = [];',
  1530. if supTObject in Parts then
  1531. Intf.AddStrings([
  1532. 'type',
  1533. ' TClass = class of TObject;',
  1534. ' TObject = class',
  1535. ' constructor Create;',
  1536. ' destructor Destroy; virtual;',
  1537. ' class function ClassType: TClass; assembler;',
  1538. ' class function ClassName: String; assembler;',
  1539. ' class function ClassNameIs(const Name: string): boolean;',
  1540. ' class function ClassParent: TClass; assembler;',
  1541. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1542. ' class function UnitName: String; assembler;',
  1543. ' procedure AfterConstruction; virtual;',
  1544. ' procedure BeforeDestruction;virtual;',
  1545. ' function Equals(Obj: TObject): boolean; virtual;',
  1546. ' function ToString: String; virtual;',
  1547. ' end;']);
  1548. if supTInterfacedObject in Parts then
  1549. Intf.AddStrings([
  1550. ' {$Interfaces COM}',
  1551. ' IUnknown = interface',
  1552. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1553. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1554. ' function _AddRef: Integer;',
  1555. ' function _Release: Integer;',
  1556. ' end;',
  1557. ' IInterface = IUnknown;',
  1558. ' TInterfacedObject = class(TObject,IUnknown)',
  1559. ' protected',
  1560. ' fRefCount: Integer;',
  1561. ' { implement methods of IUnknown }',
  1562. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1563. ' function _AddRef: Integer; virtual;',
  1564. ' function _Release: Integer; virtual;',
  1565. ' end;',
  1566. ' TInterfacedClass = class of TInterfacedObject;',
  1567. '',
  1568. '']);
  1569. if supTVarRec in Parts then
  1570. Intf.AddStrings([
  1571. 'const',
  1572. ' vtInteger = 0;',
  1573. ' vtBoolean = 1;',
  1574. ' vtJSValue = 19;',
  1575. 'type',
  1576. ' PVarRec = ^TVarRec;',
  1577. ' TVarRec = record',
  1578. ' VType : byte;',
  1579. ' VJSValue: JSValue;',
  1580. ' vInteger: longint external name ''VJSValue'';',
  1581. ' vBoolean: boolean external name ''VJSValue'';',
  1582. ' end;',
  1583. ' TVarRecArray = array of TVarRec;',
  1584. 'function VarRecs: TVarRecArray; varargs;',
  1585. '']);
  1586. if supTypeInfo in Parts then
  1587. begin
  1588. Intf.AddStrings([
  1589. 'type',
  1590. ' TTypeKind = (',
  1591. ' tkUnknown, // 0',
  1592. ' tkInteger, // 1',
  1593. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1594. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1595. ' tkEnumeration, // 4',
  1596. ' tkSet, // 5',
  1597. ' tkDouble, // 6',
  1598. ' tkBool, // 7',
  1599. ' tkProcVar, // 8 function or procedure',
  1600. ' tkMethod, // 9 proc var of object',
  1601. ' tkArray, // 10 static array',
  1602. ' tkDynArray, // 11',
  1603. ' tkRecord, // 12',
  1604. ' tkClass, // 13',
  1605. ' tkClassRef, // 14',
  1606. ' tkPointer, // 15',
  1607. ' tkJSValue, // 16',
  1608. ' tkRefToProcVar, // 17 variable of procedure type',
  1609. ' tkInterface, // 18',
  1610. ' //tkObject,',
  1611. ' //tkSString,tkLString,tkAString,tkWString,',
  1612. ' //tkVariant,',
  1613. ' //tkWChar,',
  1614. ' //tkInt64,',
  1615. ' //tkQWord,',
  1616. ' //tkInterfaceRaw,',
  1617. ' //tkUString,tkUChar,',
  1618. ' tkHelper, // 19',
  1619. ' //tkFile,',
  1620. ' tkExtClass // 20',
  1621. ' );',
  1622. ' TTypeKinds = set of TTypeKind;',
  1623. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1624. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1625. ' end;',
  1626. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1627. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1628. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1629. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1630. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1631. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1632. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1633. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1634. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1635. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1636. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1637. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1638. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1639. '']);
  1640. end;
  1641. Intf.Add('var');
  1642. Intf.Add(' ExitCode: Longint = 0;');
  1643. // unit implementation
  1644. Impl:=TStringList.Create;
  1645. if supTObject in Parts then
  1646. Impl.AddStrings([
  1647. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1648. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1649. 'asm',
  1650. 'end;',
  1651. 'constructor TObject.Create; begin end;',
  1652. 'destructor TObject.Destroy; begin end;',
  1653. 'class function TObject.ClassType: TClass; assembler;',
  1654. 'asm',
  1655. 'end;',
  1656. 'class function TObject.ClassName: String; assembler;',
  1657. 'asm',
  1658. 'end;',
  1659. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1660. 'begin',
  1661. ' Result:=SameText(Name,ClassName);',
  1662. 'end;',
  1663. 'class function TObject.ClassParent: TClass; assembler;',
  1664. 'asm',
  1665. 'end;',
  1666. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1667. 'asm',
  1668. 'end;',
  1669. 'class function TObject.UnitName: String; assembler;',
  1670. 'asm',
  1671. 'end;',
  1672. 'procedure TObject.AfterConstruction; begin end;',
  1673. 'procedure TObject.BeforeDestruction; begin end;',
  1674. 'function TObject.Equals(Obj: TObject): boolean;',
  1675. 'begin',
  1676. ' Result:=Obj=Self;',
  1677. 'end;',
  1678. 'function TObject.ToString: String;',
  1679. 'begin',
  1680. ' Result:=ClassName;',
  1681. 'end;'
  1682. ]);
  1683. if supTInterfacedObject in Parts then
  1684. Impl.AddStrings([
  1685. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1686. //'begin',
  1687. //'end;',
  1688. 'function TInterfacedObject._AddRef: Integer;',
  1689. 'begin',
  1690. 'end;',
  1691. 'function TInterfacedObject._Release: Integer;',
  1692. 'begin',
  1693. 'end;',
  1694. '']);
  1695. if supTVarRec in Parts then
  1696. Impl.AddStrings([
  1697. 'function VarRecs: TVarRecArray; varargs;',
  1698. 'var',
  1699. ' v: PVarRec;',
  1700. 'begin',
  1701. ' v^.VType:=1;',
  1702. ' v^.VJSValue:=2;',
  1703. 'end;',
  1704. '']);
  1705. try
  1706. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1707. finally
  1708. Intf.Free;
  1709. Impl.Free;
  1710. end;
  1711. end;
  1712. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1713. SystemUnitParts: TSystemUnitParts);
  1714. begin
  1715. if NeedSystemUnit then
  1716. AddSystemUnit(SystemUnitParts)
  1717. else
  1718. Parser.ImplicitUses.Clear;
  1719. Add('program '+ExtractFileUnitName(Filename)+';');
  1720. Add('');
  1721. end;
  1722. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1723. SystemUnitParts: TSystemUnitParts);
  1724. begin
  1725. if NeedSystemUnit then
  1726. AddSystemUnit(SystemUnitParts)
  1727. else
  1728. Parser.ImplicitUses.Clear;
  1729. Add('unit Test1;');
  1730. Add('');
  1731. end;
  1732. procedure TCustomTestModule.ConvertModule;
  1733. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1734. out UsesLit: TJSArrayLiteral);
  1735. var
  1736. i: Integer;
  1737. Item: TJSElement;
  1738. Lit: TJSLiteral;
  1739. begin
  1740. UsesLit:=nil;
  1741. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1742. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1743. exit; // null is ok
  1744. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1745. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1746. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1747. begin
  1748. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1749. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1750. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1751. Lit:=TJSLiteral(Item);
  1752. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1753. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1754. end;
  1755. end;
  1756. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1757. out Src: TJSSourceElements);
  1758. var
  1759. FunDecl: TJSFunctionDeclarationStatement;
  1760. FunDef: TJSFuncDef;
  1761. FunBody: TJSFunctionBody;
  1762. begin
  1763. Src:=nil;
  1764. AssertNotNull(ParamName,Arg.Expr);
  1765. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1766. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1767. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1768. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1769. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1770. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1771. AssertNotNull(ParamName+' body',FunDef.Body);
  1772. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1773. FunBody:=FunDef.Body as TJSFunctionBody;
  1774. AssertNotNull(ParamName+' body.A',FunBody.A);
  1775. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1776. Src:=FunBody.A as TJSSourceElements;
  1777. end;
  1778. var
  1779. ModuleNameExpr: TJSLiteral;
  1780. InitFunction: TJSFunctionDeclarationStatement;
  1781. InitAssign: TJSSimpleAssignStatement;
  1782. InitName: String;
  1783. LastNode: TJSElement;
  1784. Arg: TJSArrayLiteralElement;
  1785. begin
  1786. if SkipTests then exit;
  1787. try
  1788. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1789. except
  1790. on E: Exception do
  1791. HandleException(E);
  1792. end;
  1793. if SkipTests then exit;
  1794. if ExpectedErrorClass<>nil then
  1795. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1796. FJSSource:=TStringList.Create;
  1797. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1798. {$IFDEF VerbosePas2JS}
  1799. writeln('TTestModule.ConvertModule JS:');
  1800. write(FJSSource.Text);
  1801. {$ENDIF}
  1802. // rtl.module(...
  1803. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1804. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1805. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1806. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1807. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1808. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1809. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1810. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1811. // parameter 'unitname'
  1812. if JSModuleCallArgs.Elements.Count<1 then
  1813. Fail('rtl.module first param unit missing');
  1814. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1815. AssertNotNull('module name param',Arg.Expr);
  1816. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1817. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1818. if Module is TPasProgram then
  1819. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1820. else
  1821. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1822. // main uses section
  1823. if JSModuleCallArgs.Elements.Count<2 then
  1824. Fail('rtl.module second param main uses missing');
  1825. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1826. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1827. // program/library/interface function()
  1828. if JSModuleCallArgs.Elements.Count<3 then
  1829. Fail('rtl.module third param intf-function missing');
  1830. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1831. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1832. // search for $mod.$init or $mod.$main - the last statement
  1833. if Module is TPasProgram then
  1834. begin
  1835. InitName:='$main';
  1836. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1837. end
  1838. else
  1839. InitName:='$init';
  1840. FJSInitBody:=nil;
  1841. if JSModuleSrc.Statements.Count>0 then
  1842. begin
  1843. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1844. if LastNode is TJSSimpleAssignStatement then
  1845. begin
  1846. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1847. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1848. begin
  1849. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1850. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1851. end
  1852. else if Module is TPasProgram then
  1853. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1854. end;
  1855. end;
  1856. // optional: implementation uses section
  1857. if JSModuleCallArgs.Elements.Count<4 then
  1858. exit;
  1859. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1860. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1861. end;
  1862. procedure TCustomTestModule.ConvertProgram;
  1863. begin
  1864. Add('end.');
  1865. ParseProgram;
  1866. ConvertModule;
  1867. end;
  1868. procedure TCustomTestModule.ConvertUnit;
  1869. begin
  1870. Add('end.');
  1871. ParseUnit;
  1872. ConvertModule;
  1873. end;
  1874. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1875. begin
  1876. Result:=tcmodules.JSToStr(El);
  1877. end;
  1878. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1879. DottedName: string);
  1880. begin
  1881. if DottedName='' then
  1882. begin
  1883. AssertNull(Msg,El);
  1884. end
  1885. else
  1886. begin
  1887. AssertNotNull(Msg,El);
  1888. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1889. end;
  1890. end;
  1891. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1892. begin
  1893. if El=nil then
  1894. Result:=''
  1895. else if El is TJSPrimaryExpressionIdent then
  1896. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1897. else if El is TJSDotMemberExpression then
  1898. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1899. else
  1900. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1901. end;
  1902. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1903. InitStatements: string; ImplStatements: string);
  1904. var
  1905. ActualSrc, ExpectedSrc, InitName: String;
  1906. begin
  1907. ActualSrc:=JSToStr(JSModuleSrc);
  1908. if coUseStrict in Converter.Options then
  1909. ExpectedSrc:='"use strict";'+LineEnding
  1910. else
  1911. ExpectedSrc:='';
  1912. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1913. ExpectedSrc:=ExpectedSrc+Statements;
  1914. // unit implementation
  1915. if (Trim(ImplStatements)<>'') then
  1916. ExpectedSrc:=ExpectedSrc+LineEnding
  1917. +'$mod.$implcode = function () {'+LineEnding
  1918. +ImplStatements
  1919. +'};'+LineEnding;
  1920. // program main or unit initialization
  1921. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1922. begin
  1923. if Module is TPasProgram then
  1924. InitName:='$main'
  1925. else
  1926. InitName:='$init';
  1927. ExpectedSrc:=ExpectedSrc+LineEnding
  1928. +'$mod.'+InitName+' = function () {'+LineEnding
  1929. +InitStatements
  1930. +'};'+LineEnding;
  1931. end;
  1932. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1933. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1934. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1935. end;
  1936. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1937. // search diff, ignore changes in spaces
  1938. var
  1939. s: string;
  1940. begin
  1941. if CheckSrcDiff(Expected,Actual,s) then exit;
  1942. Fail(Msg+': '+s);
  1943. end;
  1944. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1945. var
  1946. aResolver: TTestEnginePasResolver;
  1947. aConverter: TPasToJSConverter;
  1948. aJSModule: TJSSourceElements;
  1949. ActualSrc: String;
  1950. begin
  1951. aResolver:=GetResolver(Filename);
  1952. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1953. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1954. {$IFDEF VerbosePas2JS}
  1955. writeln('CheckUnit '+Filename+' converting ...');
  1956. {$ENDIF}
  1957. aConverter:=CreateConverter;
  1958. aJSModule:=nil;
  1959. try
  1960. try
  1961. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1962. except
  1963. on E: Exception do
  1964. HandleException(E);
  1965. end;
  1966. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1967. {$IFDEF VerbosePas2JS}
  1968. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1969. write(aResolver.Source);
  1970. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1971. write(ActualSrc);
  1972. {$ENDIF}
  1973. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1974. finally
  1975. aJSModule.Free;
  1976. aConverter.Free;
  1977. end;
  1978. end;
  1979. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1980. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1981. var
  1982. i: Integer;
  1983. Item: TTestHintMessage;
  1984. Expected,Actual: string;
  1985. begin
  1986. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1987. for i:=0 to MsgCount-1 do
  1988. begin
  1989. Item:=Msgs[i];
  1990. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1991. if (Marker<>nil) then
  1992. begin
  1993. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1994. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1995. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1996. end;
  1997. // found
  1998. FHintMsgsGood.Add(Item);
  1999. str(Item.MsgType,Actual);
  2000. str(MsgType,Expected);
  2001. AssertEquals('MsgType',Expected,Actual);
  2002. exit;
  2003. end;
  2004. // needed message missing -> show emitted messages
  2005. WriteSources('',0,0);
  2006. for i:=0 to MsgCount-1 do
  2007. begin
  2008. Item:=Msgs[i];
  2009. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2010. ' ('+IntToStr(Item.MsgNumber),')');
  2011. if Marker<>nil then
  2012. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2013. writeln(' {',Item.Msg,'}');
  2014. end;
  2015. str(MsgType,Expected);
  2016. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2017. if Marker<>nil then
  2018. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2019. Actual:=Actual+' '+Msg;
  2020. Fail(Actual);
  2021. end;
  2022. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2023. );
  2024. var
  2025. i: Integer;
  2026. s, Txt: String;
  2027. Msg: TTestHintMessage;
  2028. begin
  2029. for i:=0 to MsgCount-1 do
  2030. begin
  2031. Msg:=Msgs[i];
  2032. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2033. s:='';
  2034. str(Msg.MsgType,s);
  2035. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2036. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2037. if WithSourcePos then
  2038. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2039. Txt:=Txt+' {'+Msg.Msg+'}';
  2040. Fail(Txt);
  2041. end;
  2042. end;
  2043. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2044. MsgNumber: integer);
  2045. begin
  2046. ExpectedErrorClass:=EScannerError;
  2047. ExpectedErrorMsg:=Msg;
  2048. ExpectedErrorNumber:=MsgNumber;
  2049. end;
  2050. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2051. MsgNumber: integer);
  2052. begin
  2053. ExpectedErrorClass:=EParserError;
  2054. ExpectedErrorMsg:=Msg;
  2055. ExpectedErrorNumber:=MsgNumber;
  2056. end;
  2057. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2058. MsgNumber: integer);
  2059. begin
  2060. ExpectedErrorClass:=EPasResolve;
  2061. ExpectedErrorMsg:=Msg;
  2062. ExpectedErrorNumber:=MsgNumber;
  2063. end;
  2064. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2065. MsgNumber: integer);
  2066. begin
  2067. ExpectedErrorClass:=EPas2JS;
  2068. ExpectedErrorMsg:=Msg;
  2069. ExpectedErrorNumber:=MsgNumber;
  2070. end;
  2071. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2072. var
  2073. MsgNumber: Integer;
  2074. Msg: String;
  2075. begin
  2076. Result:=false;
  2077. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2078. Msg:=E.Message;
  2079. if E is EPas2JS then
  2080. MsgNumber:=EPas2JS(E).MsgNumber
  2081. else if E is EPasResolve then
  2082. MsgNumber:=EPasResolve(E).MsgNumber
  2083. else if E is EParserError then
  2084. MsgNumber:=Parser.LastMsgNumber
  2085. else if E is EScannerError then
  2086. begin
  2087. MsgNumber:=Scanner.LastMsgNumber;
  2088. Msg:=Scanner.LastMsg;
  2089. end
  2090. else
  2091. MsgNumber:=0;
  2092. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2093. if Result then
  2094. SkipTests:=true;
  2095. end;
  2096. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2097. begin
  2098. if IsErrorExpected(E) then exit;
  2099. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2100. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2101. +' '+Scanner.CurFilename
  2102. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2103. FailException(E);
  2104. end;
  2105. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2106. begin
  2107. if IsErrorExpected(E) then exit;
  2108. WriteSources(E.Filename,E.Row,E.Column);
  2109. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2110. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2111. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2112. );
  2113. FailException(E);
  2114. end;
  2115. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2116. var
  2117. P: TPasSourcePos;
  2118. begin
  2119. if IsErrorExpected(E) then exit;
  2120. P:=E.SourcePos;
  2121. WriteSources(P.FileName,P.Row,P.Column);
  2122. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2123. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2124. FailException(E);
  2125. end;
  2126. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2127. var
  2128. Row, Col: integer;
  2129. begin
  2130. if IsErrorExpected(E) then exit;
  2131. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2132. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2133. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2134. +' '+E.PasElement.SourceFilename
  2135. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2136. FailException(E);
  2137. end;
  2138. procedure TCustomTestModule.HandleException(E: Exception);
  2139. begin
  2140. if E is EScannerError then
  2141. HandleScannerError(EScannerError(E))
  2142. else if E is EParserError then
  2143. HandleParserError(EParserError(E))
  2144. else if E is EPasResolve then
  2145. HandlePasResolveError(EPasResolve(E))
  2146. else if E is EPas2JS then
  2147. HandlePas2JSError(EPas2JS(E))
  2148. else
  2149. begin
  2150. if IsErrorExpected(E) then exit;
  2151. if not (E is EAssertionFailedError) then
  2152. begin
  2153. WriteSources('',0,0);
  2154. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2155. end;
  2156. FailException(E);
  2157. end;
  2158. end;
  2159. procedure TCustomTestModule.FailException(E: Exception);
  2160. var
  2161. MsgNumber: Integer;
  2162. begin
  2163. if ExpectedErrorClass<>nil then
  2164. begin
  2165. if FExpectedErrorClass=E.ClassType then
  2166. begin
  2167. if E is EPas2JS then
  2168. MsgNumber:=EPas2JS(E).MsgNumber
  2169. else if E is EPasResolve then
  2170. MsgNumber:=EPasResolve(E).MsgNumber
  2171. else if E is EParserError then
  2172. MsgNumber:=Parser.LastMsgNumber
  2173. else if E is EScannerError then
  2174. MsgNumber:=Scanner.LastMsgNumber
  2175. else
  2176. MsgNumber:=0;
  2177. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2178. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2179. ExpectedErrorNumber,MsgNumber);
  2180. end else begin
  2181. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2182. end;
  2183. end;
  2184. Fail(E.Message);
  2185. end;
  2186. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2187. aCol: integer);
  2188. var
  2189. IsSrc: Boolean;
  2190. i, j: Integer;
  2191. SrcLines: TStringList;
  2192. Line: string;
  2193. aModule: TTestEnginePasResolver;
  2194. begin
  2195. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2196. for i:=0 to ResolverCount-1 do
  2197. begin
  2198. aModule:=Resolvers[i];
  2199. SrcLines:=TStringList.Create;
  2200. try
  2201. SrcLines.Text:=aModule.Source;
  2202. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2203. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2204. for j:=1 to SrcLines.Count do
  2205. begin
  2206. Line:=SrcLines[j-1];
  2207. if IsSrc and (j=aRow) then
  2208. begin
  2209. write('*');
  2210. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2211. end;
  2212. writeln(Format('%:4d: ',[j]),Line);
  2213. end;
  2214. finally
  2215. SrcLines.Free;
  2216. end;
  2217. end;
  2218. end;
  2219. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2220. var
  2221. i: Integer;
  2222. begin
  2223. for i:=0 to ResolverCount-1 do
  2224. if Filename=Resolvers[i].Filename then exit(i);
  2225. Result:=-1;
  2226. end;
  2227. function TCustomTestModule.GetResolver(const Filename: string
  2228. ): TTestEnginePasResolver;
  2229. var
  2230. i: Integer;
  2231. begin
  2232. i:=IndexOfResolver(Filename);
  2233. if i<0 then exit(nil);
  2234. Result:=Resolvers[i];
  2235. end;
  2236. function TCustomTestModule.GetDefaultNamespace: string;
  2237. var
  2238. C: TClass;
  2239. begin
  2240. Result:='';
  2241. if FModule=nil then exit;
  2242. C:=FModule.ClassType;
  2243. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2244. Result:=Engine.DefaultNameSpace;
  2245. end;
  2246. constructor TCustomTestModule.Create;
  2247. begin
  2248. inherited Create;
  2249. FHintMsgs:=TObjectList.Create(true);
  2250. FHintMsgsGood:=TFPList.Create;
  2251. end;
  2252. destructor TCustomTestModule.Destroy;
  2253. begin
  2254. FreeAndNil(FHintMsgs);
  2255. FreeAndNil(FHintMsgsGood);
  2256. inherited Destroy;
  2257. end;
  2258. { TTestModule }
  2259. procedure TTestModule.TestReservedWords;
  2260. var
  2261. i: integer;
  2262. begin
  2263. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2264. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2265. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2266. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2267. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2268. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2269. end;
  2270. procedure TTestModule.TestEmptyProgram;
  2271. begin
  2272. StartProgram(false);
  2273. Add('begin');
  2274. ConvertProgram;
  2275. CheckSource('TestEmptyProgram','','');
  2276. end;
  2277. procedure TTestModule.TestEmptyProgramUseStrict;
  2278. begin
  2279. Converter.Options:=Converter.Options+[coUseStrict];
  2280. StartProgram(false);
  2281. Add('begin');
  2282. ConvertProgram;
  2283. CheckSource('TestEmptyProgramUseStrict','','');
  2284. end;
  2285. procedure TTestModule.TestEmptyUnit;
  2286. begin
  2287. StartUnit(false);
  2288. Add('interface');
  2289. Add('implementation');
  2290. ConvertUnit;
  2291. CheckSource('TestEmptyUnit',
  2292. LinesToStr([
  2293. ]),
  2294. '');
  2295. end;
  2296. procedure TTestModule.TestEmptyUnitUseStrict;
  2297. begin
  2298. Converter.Options:=Converter.Options+[coUseStrict];
  2299. StartUnit(false);
  2300. Add('interface');
  2301. Add('implementation');
  2302. ConvertUnit;
  2303. CheckSource('TestEmptyUnitUseStrict',
  2304. LinesToStr([
  2305. ''
  2306. ]),
  2307. '');
  2308. end;
  2309. procedure TTestModule.TestDottedUnitNames;
  2310. begin
  2311. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2312. LinesToStr([
  2313. 'var iV: longint;'
  2314. ]),
  2315. '');
  2316. FFilename:='ns1.test1.pp';
  2317. StartProgram(true);
  2318. Add('uses unIt2;');
  2319. Add('var');
  2320. Add(' i: longint;');
  2321. Add('begin');
  2322. Add(' i:=iv;');
  2323. Add(' i:=uNit2.iv;');
  2324. Add(' i:=Ns1.TEst1.i;');
  2325. ConvertProgram;
  2326. CheckSource('TestDottedUnitNames',
  2327. LinesToStr([
  2328. 'this.i = 0;',
  2329. '']),
  2330. LinesToStr([ // this.$init
  2331. '$mod.i = pas["NS1.Unit2"].iV;',
  2332. '$mod.i = pas["NS1.Unit2"].iV;',
  2333. '$mod.i = $mod.i;',
  2334. '']) );
  2335. end;
  2336. procedure TTestModule.TestDottedUnitNameImpl;
  2337. begin
  2338. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2339. LinesToStr([
  2340. 'type',
  2341. ' TObject = class end;',
  2342. ' TTestA = class',
  2343. ' end;'
  2344. ]),
  2345. LinesToStr(['uses TEST.UnitB;'])
  2346. );
  2347. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2348. LinesToStr([
  2349. 'uses TEST.UnitA;',
  2350. 'type TTestB = class(TTestA);'
  2351. ]),
  2352. ''
  2353. );
  2354. StartProgram(true);
  2355. Add('uses TEST.UnitA;');
  2356. Add('begin');
  2357. ConvertProgram;
  2358. CheckSource('TestDottedUnitNameImpl',
  2359. LinesToStr([
  2360. '']),
  2361. LinesToStr([ // this.$init
  2362. '']) );
  2363. CheckUnit('TEST.UnitA.pas',
  2364. LinesToStr([
  2365. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2366. ' var $mod = this;',
  2367. ' rtl.createClass(this, "TObject", null, function () {',
  2368. ' this.$init = function () {',
  2369. ' };',
  2370. ' this.$final = function () {',
  2371. ' };',
  2372. ' });',
  2373. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2374. ' });',
  2375. '}, ["TEST.UnitB"]);'
  2376. ]));
  2377. CheckUnit('TEST.UnitB.pas',
  2378. LinesToStr([
  2379. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2380. ' var $mod = this;',
  2381. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2382. ' });',
  2383. '});'
  2384. ]));
  2385. end;
  2386. procedure TTestModule.TestDottedUnitExpr;
  2387. begin
  2388. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2389. LinesToStr([
  2390. 'procedure DoIt;'
  2391. ]),
  2392. 'procedure DoIt; begin end;');
  2393. FFilename:='Ns1.SubNs1.Test1.pp';
  2394. StartProgram(true);
  2395. Add('uses Ns2.sUbnS2.unIt2;');
  2396. Add('var');
  2397. Add(' i: longint;');
  2398. Add('begin');
  2399. Add(' ns2.subns2.unit2.doit;');
  2400. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2401. ConvertProgram;
  2402. CheckSource('TestDottedUnitExpr',
  2403. LinesToStr([
  2404. 'this.i = 0;',
  2405. '']),
  2406. LinesToStr([ // this.$init
  2407. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2408. '$mod.i = $mod.i;',
  2409. '']) );
  2410. end;
  2411. procedure TTestModule.Test_ModeFPCFail;
  2412. begin
  2413. StartProgram(false);
  2414. Add('{$mode FPC}');
  2415. Add('begin');
  2416. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2417. ConvertProgram;
  2418. end;
  2419. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2420. begin
  2421. StartProgram(false);
  2422. Add('{$modeswitch cblocks-}');
  2423. Add('begin');
  2424. ConvertProgram;
  2425. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2426. CheckResolverUnexpectedHints();
  2427. end;
  2428. procedure TTestModule.TestUnit_UseSystem;
  2429. begin
  2430. StartUnit(true);
  2431. Add([
  2432. 'interface',
  2433. 'var i: integer;',
  2434. 'implementation']);
  2435. ConvertUnit;
  2436. CheckSource('TestUnit_UseSystem',
  2437. LinesToStr([
  2438. 'this.i = 0;',
  2439. '']),
  2440. LinesToStr([
  2441. '']) );
  2442. end;
  2443. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2444. begin
  2445. AddModuleWithIntfImplSrc('unit1.pp',
  2446. LinesToStr([
  2447. 'type number = longint;']),
  2448. LinesToStr([
  2449. 'uses test1;',
  2450. 'procedure DoIt;',
  2451. 'begin',
  2452. ' i:=3;',
  2453. 'end;']));
  2454. StartUnit(true);
  2455. Add([
  2456. 'interface',
  2457. 'uses unit1;',
  2458. 'var i: number;',
  2459. 'implementation']);
  2460. ConvertUnit;
  2461. CheckSource('TestUnit_Intf1Impl2Intf1',
  2462. LinesToStr([
  2463. 'this.i = 0;',
  2464. '']),
  2465. LinesToStr([
  2466. '']) );
  2467. end;
  2468. procedure TTestModule.TestIncludeVersion;
  2469. begin
  2470. StartProgram(false);
  2471. Add([
  2472. 'var',
  2473. ' s: string;',
  2474. ' i: word;',
  2475. 'begin',
  2476. ' s:={$I %line%};',
  2477. ' i:={$I %linenum%};',
  2478. ' s:={$I %currentroutine%};',
  2479. ' s:={$I %pas2jsversion%};',
  2480. ' s:={$I %pas2jstarget%};',
  2481. ' s:={$I %pas2jstargetos%};',
  2482. ' s:={$I %pas2jstargetcpu%};',
  2483. ' s:={$I %file%};',
  2484. '']);
  2485. ConvertProgram;
  2486. CheckSource('TestIncludeVersion',
  2487. LinesToStr([
  2488. 'this.s="";',
  2489. 'this.i = 0;']),
  2490. LinesToStr([
  2491. '$mod.s = "7";',
  2492. '$mod.i = 8;',
  2493. '$mod.s = "<anonymous>";',
  2494. '$mod.s = "Comp.Ver.tcmodules";',
  2495. '$mod.s = "Browser";',
  2496. '$mod.s = "Browser";',
  2497. '$mod.s = "ECMAScript5";',
  2498. '$mod.s = "test1.pp";',
  2499. '']));
  2500. end;
  2501. procedure TTestModule.TestVarInt;
  2502. begin
  2503. StartProgram(false);
  2504. Add('var MyI: longint;');
  2505. Add('begin');
  2506. ConvertProgram;
  2507. CheckSource('TestVarInt','this.MyI=0;','');
  2508. end;
  2509. procedure TTestModule.TestVarBaseTypes;
  2510. begin
  2511. StartProgram(false);
  2512. Add('var');
  2513. Add(' i: longint;');
  2514. Add(' s: string;');
  2515. Add(' c: char;');
  2516. Add(' b: boolean;');
  2517. Add(' d: double;');
  2518. Add(' i2: longint = 3;');
  2519. Add(' s2: string = ''foo'';');
  2520. Add(' c2: char = ''4'';');
  2521. Add(' b2: boolean = true;');
  2522. Add(' d2: double = 5.6;');
  2523. Add(' i3: longint = $707;');
  2524. Add(' i4: nativeint = 9007199254740991;');
  2525. Add(' i5: nativeint = -9007199254740991-1;');
  2526. Add(' i6: nativeint = $fffffffffffff;');
  2527. Add(' i7: nativeint = -$fffffffffffff-1;');
  2528. Add(' i8: byte = 00;');
  2529. Add(' u8: nativeuint = $fffffffffffff;');
  2530. Add(' u9: nativeuint = $0000000000000;');
  2531. Add(' u10: nativeuint = $00ff00;');
  2532. Add('begin');
  2533. ConvertProgram;
  2534. CheckSource('TestVarBaseTypes',
  2535. LinesToStr([
  2536. 'this.i = 0;',
  2537. 'this.s = "";',
  2538. 'this.c = "";',
  2539. 'this.b = false;',
  2540. 'this.d = 0.0;',
  2541. 'this.i2 = 3;',
  2542. 'this.s2 = "foo";',
  2543. 'this.c2 = "4";',
  2544. 'this.b2 = true;',
  2545. 'this.d2 = 5.6;',
  2546. 'this.i3 = 0x707;',
  2547. 'this.i4 = 9007199254740991;',
  2548. 'this.i5 = -9007199254740991-1;',
  2549. 'this.i6 = 0xfffffffffffff;',
  2550. 'this.i7 =-0xfffffffffffff-1;',
  2551. 'this.i8 = 0;',
  2552. 'this.u8 = 0xfffffffffffff;',
  2553. 'this.u9 = 0x0;',
  2554. 'this.u10 = 0xff00;'
  2555. ]),
  2556. '');
  2557. end;
  2558. procedure TTestModule.TestBaseTypeSingleFail;
  2559. begin
  2560. StartProgram(false);
  2561. Add('var s: single;');
  2562. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2563. ConvertProgram;
  2564. end;
  2565. procedure TTestModule.TestBaseTypeExtendedFail;
  2566. begin
  2567. StartProgram(false);
  2568. Add('var e: extended;');
  2569. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2570. ConvertProgram;
  2571. end;
  2572. procedure TTestModule.TestConstBaseTypes;
  2573. begin
  2574. StartProgram(false);
  2575. Add('const');
  2576. Add(' i: longint = 3;');
  2577. Add(' s: string = ''foo'';');
  2578. Add(' c: char = ''4'';');
  2579. Add(' b: boolean = true;');
  2580. Add(' d: double = 5.6;');
  2581. Add(' e = low(word);');
  2582. Add(' f = high(word);');
  2583. Add('begin');
  2584. ConvertProgram;
  2585. CheckSource('TestVarBaseTypes',
  2586. LinesToStr([
  2587. 'this.i=3;',
  2588. 'this.s="foo";',
  2589. 'this.c="4";',
  2590. 'this.b=true;',
  2591. 'this.d=5.6;',
  2592. 'this.e = 0;',
  2593. 'this.f = 65535;'
  2594. ]),
  2595. '');
  2596. end;
  2597. procedure TTestModule.TestAliasTypeRef;
  2598. begin
  2599. StartProgram(false);
  2600. Add('type');
  2601. Add(' a=longint;');
  2602. Add(' b=a;');
  2603. Add('var');
  2604. Add(' c: A;');
  2605. Add(' d: B;');
  2606. Add('begin');
  2607. ConvertProgram;
  2608. CheckSource('TestAliasTypeRef',
  2609. LinesToStr([ // statements
  2610. 'this.c = 0;',
  2611. 'this.d = 0;'
  2612. ]),
  2613. LinesToStr([ // this.$main
  2614. ''
  2615. ]));
  2616. end;
  2617. procedure TTestModule.TestTypeCast_BaseTypes;
  2618. begin
  2619. StartProgram(false);
  2620. Add([
  2621. 'var',
  2622. ' i: longint;',
  2623. ' b: boolean;',
  2624. ' d: double;',
  2625. ' s: string;',
  2626. ' c: char;',
  2627. 'begin',
  2628. ' i:=longint(i);',
  2629. ' i:=longint(b);',
  2630. ' b:=boolean(b);',
  2631. ' b:=boolean(i);',
  2632. ' d:=double(d);',
  2633. ' d:=double(i);',
  2634. ' s:=string(s);',
  2635. ' s:=string(c);',
  2636. ' c:=char(c);',
  2637. ' c:=char(i);',
  2638. ' c:=char(65);',
  2639. ' c:=char(#10);',
  2640. ' c:=char(#$E000);',
  2641. '']);
  2642. ConvertProgram;
  2643. CheckSource('TestAliasTypeRef',
  2644. LinesToStr([ // statements
  2645. 'this.i = 0;',
  2646. 'this.b = false;',
  2647. 'this.d = 0.0;',
  2648. 'this.s = "";',
  2649. 'this.c = "";',
  2650. '']),
  2651. LinesToStr([ // this.$main
  2652. '$mod.i = $mod.i;',
  2653. '$mod.i = ($mod.b ? 1 : 0);',
  2654. '$mod.b = $mod.b;',
  2655. '$mod.b = $mod.i != 0;',
  2656. '$mod.d = $mod.d;',
  2657. '$mod.d = $mod.i;',
  2658. '$mod.s = $mod.s;',
  2659. '$mod.s = $mod.c;',
  2660. '$mod.c = $mod.c;',
  2661. '$mod.c = String.fromCharCode($mod.i);',
  2662. '$mod.c = "A";',
  2663. '$mod.c = "\n";',
  2664. '$mod.c = "";',
  2665. '']));
  2666. end;
  2667. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2668. begin
  2669. StartProgram(false);
  2670. Add('type');
  2671. Add(' integer = longint;');
  2672. Add(' TYesNo = boolean;');
  2673. Add(' TFloat = double;');
  2674. Add(' TCaption = string;');
  2675. Add(' TChar = char;');
  2676. Add('var');
  2677. Add(' i: integer;');
  2678. Add(' b: TYesNo;');
  2679. Add(' d: TFloat;');
  2680. Add(' s: TCaption;');
  2681. Add(' c: TChar;');
  2682. Add('begin');
  2683. Add(' i:=integer(i);');
  2684. Add(' i:=integer(b);');
  2685. Add(' b:=TYesNo(b);');
  2686. Add(' b:=TYesNo(i);');
  2687. Add(' d:=TFloat(d);');
  2688. Add(' d:=TFloat(i);');
  2689. Add(' s:=TCaption(s);');
  2690. Add(' s:=TCaption(c);');
  2691. Add(' c:=TChar(c);');
  2692. ConvertProgram;
  2693. CheckSource('TestAliasTypeRef',
  2694. LinesToStr([ // statements
  2695. 'this.i = 0;',
  2696. 'this.b = false;',
  2697. 'this.d = 0.0;',
  2698. 'this.s = "";',
  2699. 'this.c = "";',
  2700. '']),
  2701. LinesToStr([ // this.$main
  2702. '$mod.i = $mod.i;',
  2703. '$mod.i = ($mod.b ? 1 : 0);',
  2704. '$mod.b = $mod.b;',
  2705. '$mod.b = $mod.i != 0;',
  2706. '$mod.d = $mod.d;',
  2707. '$mod.d = $mod.i;',
  2708. '$mod.s = $mod.s;',
  2709. '$mod.s = $mod.c;',
  2710. '$mod.c = $mod.c;',
  2711. '']));
  2712. end;
  2713. procedure TTestModule.TestEmptyProc;
  2714. begin
  2715. StartProgram(false);
  2716. Add('procedure Test;');
  2717. Add('begin');
  2718. Add('end;');
  2719. Add('begin');
  2720. ConvertProgram;
  2721. CheckSource('TestEmptyProc',
  2722. LinesToStr([ // statements
  2723. 'this.Test = function () {',
  2724. '};'
  2725. ]),
  2726. LinesToStr([ // this.$main
  2727. ''
  2728. ]));
  2729. end;
  2730. procedure TTestModule.TestProcOneParam;
  2731. begin
  2732. StartProgram(false);
  2733. Add('procedure ProcA(i: longint);');
  2734. Add('begin');
  2735. Add('end;');
  2736. Add('begin');
  2737. Add(' PROCA(3);');
  2738. ConvertProgram;
  2739. CheckSource('TestProcOneParam',
  2740. LinesToStr([ // statements
  2741. 'this.ProcA = function (i) {',
  2742. '};'
  2743. ]),
  2744. LinesToStr([ // this.$main
  2745. '$mod.ProcA(3);'
  2746. ]));
  2747. end;
  2748. procedure TTestModule.TestFunctionWithoutParams;
  2749. begin
  2750. StartProgram(false);
  2751. Add('function FuncA: longint;');
  2752. Add('begin');
  2753. Add('end;');
  2754. Add('var i: longint;');
  2755. Add('begin');
  2756. Add(' I:=FUNCA();');
  2757. Add(' I:=FUNCA;');
  2758. Add(' FUNCA();');
  2759. Add(' FUNCA;');
  2760. ConvertProgram;
  2761. CheckSource('TestProcWithoutParams',
  2762. LinesToStr([ // statements
  2763. 'this.FuncA = function () {',
  2764. ' var Result = 0;',
  2765. ' return Result;',
  2766. '};',
  2767. 'this.i=0;'
  2768. ]),
  2769. LinesToStr([ // this.$main
  2770. '$mod.i=$mod.FuncA();',
  2771. '$mod.i=$mod.FuncA();',
  2772. '$mod.FuncA();',
  2773. '$mod.FuncA();'
  2774. ]));
  2775. end;
  2776. procedure TTestModule.TestProcedureWithoutParams;
  2777. begin
  2778. StartProgram(false);
  2779. Add('procedure ProcA;');
  2780. Add('begin');
  2781. Add('end;');
  2782. Add('begin');
  2783. Add(' PROCA();');
  2784. Add(' PROCA;');
  2785. ConvertProgram;
  2786. CheckSource('TestProcWithoutParams',
  2787. LinesToStr([ // statements
  2788. 'this.ProcA = function () {',
  2789. '};'
  2790. ]),
  2791. LinesToStr([ // this.$main
  2792. '$mod.ProcA();',
  2793. '$mod.ProcA();'
  2794. ]));
  2795. end;
  2796. procedure TTestModule.TestIncDec;
  2797. begin
  2798. StartProgram(false);
  2799. Add([
  2800. 'procedure DoIt(var i: longint);',
  2801. 'begin',
  2802. ' inc(i);',
  2803. ' inc(i,2);',
  2804. 'end;',
  2805. 'var',
  2806. ' Bar: longint;',
  2807. 'begin',
  2808. ' inc(bar);',
  2809. ' inc(bar,2);',
  2810. ' dec(bar);',
  2811. ' dec(bar,3);',
  2812. '']);
  2813. ConvertProgram;
  2814. CheckSource('TestIncDec',
  2815. LinesToStr([ // statements
  2816. 'this.DoIt = function (i) {',
  2817. ' i.set(i.get()+1);',
  2818. ' i.set(i.get()+2);',
  2819. '};',
  2820. 'this.Bar = 0;'
  2821. ]),
  2822. LinesToStr([ // this.$main
  2823. '$mod.Bar+=1;',
  2824. '$mod.Bar+=2;',
  2825. '$mod.Bar-=1;',
  2826. '$mod.Bar-=3;'
  2827. ]));
  2828. end;
  2829. procedure TTestModule.TestLoHiFpcMode;
  2830. begin
  2831. StartProgram(false);
  2832. Add([
  2833. '{$mode objfpc}',
  2834. 'const',
  2835. ' LoByte1 = Lo(Word($1234));',
  2836. ' HiByte1 = Hi(Word($1234));',
  2837. ' LoByte2 = Lo(SmallInt($1234));',
  2838. ' HiByte2 = Hi(SmallInt($1234));',
  2839. ' LoWord1 = Lo($1234CDEF);',
  2840. ' HiWord1 = Hi($1234CDEF);',
  2841. ' LoWord2 = Lo(-$1234CDEF);',
  2842. ' HiWord2 = Hi(-$1234CDEF);',
  2843. ' lo4:byte=lo(byte($34));',
  2844. ' hi4:byte=hi(byte($34));',
  2845. ' lo5:byte=lo(shortint(-$34));',
  2846. ' hi5:byte=hi(shortint(-$34));',
  2847. ' lo6:longword=lo($123456789ABCD);',
  2848. ' hi6:longword=hi($123456789ABCD);',
  2849. ' lo7:longword=lo(-$123456789ABCD);',
  2850. ' hi7:longword=hi(-$123456789ABCD);',
  2851. 'var',
  2852. ' b: Byte;',
  2853. ' ss: shortint;',
  2854. ' w: Word;',
  2855. ' si: SmallInt;',
  2856. ' lw: LongWord;',
  2857. ' li: LongInt;',
  2858. ' b2: Byte;',
  2859. ' ni: nativeint;',
  2860. 'begin',
  2861. ' w := $1234;',
  2862. ' ss := -$12;',
  2863. ' b := lo(ss);',
  2864. ' b := HI(ss);',
  2865. ' b := lo(w);',
  2866. ' b := HI(w);',
  2867. ' b2 := lo(b);',
  2868. ' b2 := hi(b);',
  2869. ' lw := $1234CDEF;',
  2870. ' w := lo(lw);',
  2871. ' w := hi(lw);',
  2872. ' ni := $123456789ABCD;',
  2873. ' lw := lo(ni);',
  2874. ' lw := hi(ni);',
  2875. '']);
  2876. ConvertProgram;
  2877. CheckSource('TestLoHiFpcMode',
  2878. LinesToStr([ // statements
  2879. 'this.LoByte1 = 0x1234 & 0xFF;',
  2880. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2881. 'this.LoByte2 = 0x1234 & 0xFF;',
  2882. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2883. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2884. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2885. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2886. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2887. 'this.lo4 = 0x34 & 0xF;',
  2888. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2889. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2890. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2891. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2892. 'this.hi6 = 74565 >>> 0;',
  2893. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2894. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2895. 'this.b = 0;',
  2896. 'this.ss = 0;',
  2897. 'this.w = 0;',
  2898. 'this.si = 0;',
  2899. 'this.lw = 0;',
  2900. 'this.li = 0;',
  2901. 'this.b2 = 0;',
  2902. 'this.ni = 0;',
  2903. '']),
  2904. LinesToStr([ // this.$main
  2905. '$mod.w = 0x1234;',
  2906. '$mod.ss = -0x12;',
  2907. '$mod.b = $mod.ss & 0xFF;',
  2908. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2909. '$mod.b = $mod.w & 0xFF;',
  2910. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2911. '$mod.b2 = $mod.b & 0xF;',
  2912. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2913. '$mod.lw = 0x1234CDEF;',
  2914. '$mod.w = $mod.lw & 0xFFFF;',
  2915. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2916. '$mod.ni = 0x123456789ABCD;',
  2917. '$mod.lw = $mod.ni >>> 0;',
  2918. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2919. '']));
  2920. end;
  2921. procedure TTestModule.TestLoHiDelphiMode;
  2922. begin
  2923. StartProgram(false);
  2924. Add([
  2925. '{$mode delphi}',
  2926. 'const',
  2927. ' LoByte1 = Lo(Word($1234));',
  2928. ' HiByte1 = Hi(Word($1234));',
  2929. ' LoByte2 = Lo(SmallInt($1234));',
  2930. ' HiByte2 = Hi(SmallInt($1234));',
  2931. ' LoByte3 = Lo($1234CDEF);',
  2932. ' HiByte3 = Hi($1234CDEF);',
  2933. ' LoByte4 = Lo(-$1234CDEF);',
  2934. ' HiByte4 = Hi(-$1234CDEF);',
  2935. 'var',
  2936. ' b: Byte;',
  2937. ' w: Word;',
  2938. ' si: SmallInt;',
  2939. ' lw: LongWord;',
  2940. ' li: LongInt;',
  2941. 'begin',
  2942. ' w := $1234;',
  2943. ' b := lo(w);',
  2944. ' b := HI(w);',
  2945. ' lw := $1234CDEF;',
  2946. ' b := lo(lw);',
  2947. ' b := hi(lw);',
  2948. '']);
  2949. ConvertProgram;
  2950. CheckSource('TestLoHiDelphiMode',
  2951. LinesToStr([ // statements
  2952. 'this.LoByte1 = 0x1234 & 0xFF;',
  2953. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2954. 'this.LoByte2 = 0x1234 & 0xFF;',
  2955. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2956. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2957. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2958. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2959. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2960. 'this.b = 0;',
  2961. 'this.w = 0;',
  2962. 'this.si = 0;',
  2963. 'this.lw = 0;',
  2964. 'this.li = 0;'
  2965. ]),
  2966. LinesToStr([ // this.$main
  2967. '$mod.w = 0x1234;',
  2968. '$mod.b = $mod.w & 0xFF;',
  2969. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2970. '$mod.lw = 0x1234CDEF;',
  2971. '$mod.b = $mod.lw & 0xFF;',
  2972. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2973. ]));
  2974. end;
  2975. procedure TTestModule.TestAssignments;
  2976. begin
  2977. StartProgram(false);
  2978. Parser.Options:=Parser.Options+[po_cassignments];
  2979. Add('var');
  2980. Add(' Bar:longint;');
  2981. Add('begin');
  2982. Add(' bar:=3;');
  2983. Add(' bar+=4;');
  2984. Add(' bar-=5;');
  2985. Add(' bar*=6;');
  2986. ConvertProgram;
  2987. CheckSource('TestAssignments',
  2988. LinesToStr([ // statements
  2989. 'this.Bar = 0;'
  2990. ]),
  2991. LinesToStr([ // this.$main
  2992. '$mod.Bar=3;',
  2993. '$mod.Bar+=4;',
  2994. '$mod.Bar-=5;',
  2995. '$mod.Bar*=6;'
  2996. ]));
  2997. end;
  2998. procedure TTestModule.TestArithmeticOperators1;
  2999. begin
  3000. StartProgram(false);
  3001. Add('var');
  3002. Add(' vA,vB,vC:longint;');
  3003. Add('begin');
  3004. Add(' va:=1;');
  3005. Add(' vb:=va+va;');
  3006. Add(' vb:=va div vb;');
  3007. Add(' vb:=va mod vb;');
  3008. Add(' vb:=va+va*vb+va div vb;');
  3009. Add(' vc:=-va;');
  3010. Add(' va:=va-vb;');
  3011. Add(' vb:=va;');
  3012. Add(' if va<vb then vc:=va else vc:=vb;');
  3013. ConvertProgram;
  3014. CheckSource('TestArithmeticOperators1',
  3015. LinesToStr([ // statements
  3016. 'this.vA = 0;',
  3017. 'this.vB = 0;',
  3018. 'this.vC = 0;'
  3019. ]),
  3020. LinesToStr([ // this.$main
  3021. '$mod.vA = 1;',
  3022. '$mod.vB = $mod.vA + $mod.vA;',
  3023. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3024. '$mod.vB = $mod.vA % $mod.vB;',
  3025. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3026. '$mod.vC = -$mod.vA;',
  3027. '$mod.vA = $mod.vA - $mod.vB;',
  3028. '$mod.vB = $mod.vA;',
  3029. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3030. ]));
  3031. end;
  3032. procedure TTestModule.TestLogicalOperators;
  3033. begin
  3034. StartProgram(false);
  3035. Add('var');
  3036. Add(' vA,vB,vC:boolean;');
  3037. Add('begin');
  3038. Add(' va:=vb and vc;');
  3039. Add(' va:=vb or vc;');
  3040. Add(' va:=vb xor vc;');
  3041. Add(' va:=true and vc;');
  3042. Add(' va:=(vb and vc) or (va and vb);');
  3043. Add(' va:=not vb;');
  3044. ConvertProgram;
  3045. CheckSource('TestLogicalOperators',
  3046. LinesToStr([ // statements
  3047. 'this.vA = false;',
  3048. 'this.vB = false;',
  3049. 'this.vC = false;'
  3050. ]),
  3051. LinesToStr([ // this.$main
  3052. '$mod.vA = $mod.vB && $mod.vC;',
  3053. '$mod.vA = $mod.vB || $mod.vC;',
  3054. '$mod.vA = $mod.vB ^ $mod.vC;',
  3055. '$mod.vA = true && $mod.vC;',
  3056. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3057. '$mod.vA = !$mod.vB;'
  3058. ]));
  3059. end;
  3060. procedure TTestModule.TestBitwiseOperators;
  3061. begin
  3062. StartProgram(false);
  3063. Add([
  3064. 'var',
  3065. ' vA,vB,vC:longint;',
  3066. ' X,Y,Z: nativeint;',
  3067. 'begin',
  3068. ' va:=vb and vc;',
  3069. ' va:=vb or vc;',
  3070. ' va:=vb xor vc;',
  3071. ' va:=vb shl vc;',
  3072. ' va:=vb shr vc;',
  3073. ' va:=3 and vc;',
  3074. ' va:=(vb and vc) or (va and vb);',
  3075. ' va:=not vb;',
  3076. ' X:=Y and Z;',
  3077. ' X:=Y and va;',
  3078. ' X:=Y or Z;',
  3079. ' X:=Y or va;',
  3080. ' X:=Y xor Z;',
  3081. ' X:=Y xor va;',
  3082. '']);
  3083. ConvertProgram;
  3084. CheckSource('TestBitwiseOperators',
  3085. LinesToStr([ // statements
  3086. 'this.vA = 0;',
  3087. 'this.vB = 0;',
  3088. 'this.vC = 0;',
  3089. 'this.X = 0;',
  3090. 'this.Y = 0;',
  3091. 'this.Z = 0;',
  3092. '']),
  3093. LinesToStr([ // this.$main
  3094. '$mod.vA = $mod.vB & $mod.vC;',
  3095. '$mod.vA = $mod.vB | $mod.vC;',
  3096. '$mod.vA = $mod.vB ^ $mod.vC;',
  3097. '$mod.vA = $mod.vB << $mod.vC;',
  3098. '$mod.vA = $mod.vB >>> $mod.vC;',
  3099. '$mod.vA = 3 & $mod.vC;',
  3100. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3101. '$mod.vA = ~$mod.vB;',
  3102. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3103. '$mod.X = $mod.Y & $mod.vA;',
  3104. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3105. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3106. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3107. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3108. '']));
  3109. end;
  3110. procedure TTestModule.TestBitwiseOperatorsLongword;
  3111. begin
  3112. StartProgram(false);
  3113. Add([
  3114. 'var',
  3115. ' a,b,c:longword;',
  3116. ' i: longint;',
  3117. 'begin',
  3118. ' a:=$12345678;',
  3119. ' b:=$EDCBA987;',
  3120. ' c:=not a;',
  3121. ' c:=a and b;',
  3122. ' c:=a and $ffff0000;',
  3123. ' c:=a or b;',
  3124. ' c:=a or $ff00ff00;',
  3125. ' c:=a xor b;',
  3126. ' c:=a xor $f0f0f0f0;',
  3127. ' c:=a shl 1;',
  3128. ' c:=a shl 16;',
  3129. ' c:=a shl 24;',
  3130. ' c:=a shl b;',
  3131. ' c:=a shr 1;',
  3132. ' c:=a shr 16;',
  3133. ' c:=a shr 24;',
  3134. ' c:=a shr b;',
  3135. ' c:=(b and c) or (a and b);',
  3136. ' c:=i and a;',
  3137. ' c:=i or a;',
  3138. ' c:=i xor a;',
  3139. '']);
  3140. ConvertProgram;
  3141. CheckSource('TestBitwiseOperatorsLongword',
  3142. LinesToStr([ // statements
  3143. 'this.a = 0;',
  3144. 'this.b = 0;',
  3145. 'this.c = 0;',
  3146. 'this.i = 0;',
  3147. '']),
  3148. LinesToStr([ // this.$main
  3149. '$mod.a = 0x12345678;',
  3150. '$mod.b = 0xEDCBA987;',
  3151. '$mod.c = rtl.lw(~$mod.a);',
  3152. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3153. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3154. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3155. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3156. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3157. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3158. '$mod.c = rtl.lw($mod.a << 1);',
  3159. '$mod.c = rtl.lw($mod.a << 16);',
  3160. '$mod.c = rtl.lw($mod.a << 24);',
  3161. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3162. '$mod.c = rtl.lw($mod.a >>> 1);',
  3163. '$mod.c = rtl.lw($mod.a >>> 16);',
  3164. '$mod.c = rtl.lw($mod.a >>> 24);',
  3165. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3166. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3167. '$mod.c = $mod.i & $mod.a;',
  3168. '$mod.c = $mod.i | $mod.a;',
  3169. '$mod.c = $mod.i ^ $mod.a;',
  3170. '']));
  3171. end;
  3172. procedure TTestModule.TestPrgProcVar;
  3173. begin
  3174. StartProgram(false);
  3175. Add('procedure Proc1;');
  3176. Add('type');
  3177. Add(' t1=longint;');
  3178. Add('var');
  3179. Add(' vA:t1;');
  3180. Add('begin');
  3181. Add('end;');
  3182. Add('begin');
  3183. ConvertProgram;
  3184. CheckSource('TestPrgProcVar',
  3185. LinesToStr([ // statements
  3186. 'this.Proc1 = function () {',
  3187. ' var vA=0;',
  3188. '};'
  3189. ]),
  3190. LinesToStr([ // this.$main
  3191. ''
  3192. ]));
  3193. end;
  3194. procedure TTestModule.TestUnitProcVar;
  3195. begin
  3196. StartUnit(false);
  3197. Add('interface');
  3198. Add('');
  3199. Add('type tA=string; // unit scope');
  3200. Add('procedure Proc1;');
  3201. Add('');
  3202. Add('implementation');
  3203. Add('');
  3204. Add('procedure Proc1;');
  3205. Add('type tA=longint; // local proc scope');
  3206. Add('var v1:tA; // using local tA');
  3207. Add('begin');
  3208. Add('end;');
  3209. Add('var v2:tA; // using interface tA');
  3210. ConvertUnit;
  3211. CheckSource('TestUnitProcVar',
  3212. LinesToStr([ // statements
  3213. 'var $impl = $mod.$impl;',
  3214. 'this.Proc1 = function () {',
  3215. ' var v1 = 0;',
  3216. '};',
  3217. '']),
  3218. // this.$init
  3219. '',
  3220. // implementation
  3221. LinesToStr([
  3222. '$impl.v2 = "";',
  3223. '']));
  3224. end;
  3225. procedure TTestModule.TestImplProc;
  3226. begin
  3227. StartUnit(false);
  3228. Add('interface');
  3229. Add('');
  3230. Add('procedure Proc1;');
  3231. Add('');
  3232. Add('implementation');
  3233. Add('');
  3234. Add('procedure Proc1; begin end;');
  3235. Add('procedure Proc2; begin end;');
  3236. Add('initialization');
  3237. Add(' Proc1;');
  3238. Add(' Proc2;');
  3239. ConvertUnit;
  3240. CheckSource('TestImplProc',
  3241. LinesToStr([ // statements
  3242. 'var $impl = $mod.$impl;',
  3243. 'this.Proc1 = function () {',
  3244. '};',
  3245. '']),
  3246. LinesToStr([ // this.$init
  3247. '$mod.Proc1();',
  3248. '$impl.Proc2();',
  3249. '']),
  3250. LinesToStr([ // implementation
  3251. '$impl.Proc2 = function () {',
  3252. '};',
  3253. ''])
  3254. );
  3255. end;
  3256. procedure TTestModule.TestFunctionResult;
  3257. begin
  3258. StartProgram(false);
  3259. Add('function Func1: longint;');
  3260. Add('begin');
  3261. Add(' Result:=3;');
  3262. Add(' Func1:=4;');
  3263. Add('end;');
  3264. Add('begin');
  3265. ConvertProgram;
  3266. CheckSource('TestFunctionResult',
  3267. LinesToStr([ // statements
  3268. 'this.Func1 = function () {',
  3269. ' var Result = 0;',
  3270. ' Result = 3;',
  3271. ' Result = 4;',
  3272. ' return Result;',
  3273. '};'
  3274. ]),
  3275. '');
  3276. end;
  3277. procedure TTestModule.TestNestedProc;
  3278. begin
  3279. StartProgram(false);
  3280. Add([
  3281. 'var vInUnit: longint;',
  3282. 'function DoIt(pA,pD: longint): longint;',
  3283. 'var',
  3284. ' vB: longint;',
  3285. ' vC: longint;',
  3286. ' function Nesty(pA: longint): longint; ',
  3287. ' var vB: longint;',
  3288. ' begin',
  3289. ' Result:=pa+vb+vc+pd+vInUnit;',
  3290. ' nesty:=3;',
  3291. ' doit:=4;',
  3292. ' exit;',
  3293. ' end;',
  3294. 'begin',
  3295. ' Result:=pa+vb+vc;',
  3296. ' doit:=6;',
  3297. ' exit;',
  3298. 'end;',
  3299. 'begin']);
  3300. ConvertProgram;
  3301. CheckSource('TestNestedProc',
  3302. LinesToStr([ // statements
  3303. 'this.vInUnit = 0;',
  3304. 'this.DoIt = function (pA, pD) {',
  3305. ' var Result = 0;',
  3306. ' var vB = 0;',
  3307. ' var vC = 0;',
  3308. ' function Nesty(pA) {',
  3309. ' var Result$1 = 0;',
  3310. ' var vB = 0;',
  3311. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3312. ' Result$1 = 3;',
  3313. ' Result = 4;',
  3314. ' return Result$1;',
  3315. ' return Result$1;',
  3316. ' };',
  3317. ' Result = pA + vB + vC;',
  3318. ' Result = 6;',
  3319. ' return Result;',
  3320. ' return Result;',
  3321. '};'
  3322. ]),
  3323. '');
  3324. end;
  3325. procedure TTestModule.TestNestedProc_ResultString;
  3326. begin
  3327. StartProgram(false);
  3328. Add([
  3329. 'function DoIt: string;',
  3330. ' function Nesty: string; ',
  3331. ' begin',
  3332. ' nesty:=#65#66;',
  3333. ' nesty[1]:=#67;',
  3334. ' doit:=#68;',
  3335. ' doit[2]:=#69;',
  3336. ' end;',
  3337. 'begin',
  3338. ' doit:=#70;',
  3339. ' doit[3]:=#71;',
  3340. 'end;',
  3341. 'begin']);
  3342. ConvertProgram;
  3343. CheckSource('TestNestedProc_ResultString',
  3344. LinesToStr([ // statements
  3345. 'this.DoIt = function () {',
  3346. ' var Result = "";',
  3347. ' function Nesty() {',
  3348. ' var Result$1 = "";',
  3349. ' Result$1 = "AB";',
  3350. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3351. ' Result = "D";',
  3352. ' Result = rtl.setCharAt(Result, 1, "E");',
  3353. ' return Result$1;',
  3354. ' };',
  3355. ' Result = "F";',
  3356. ' Result = rtl.setCharAt(Result, 2, "G");',
  3357. ' return Result;',
  3358. '};'
  3359. ]),
  3360. '');
  3361. end;
  3362. procedure TTestModule.TestForwardProc;
  3363. begin
  3364. StartProgram(false);
  3365. Add('procedure FuncA(Bar: longint); forward;');
  3366. Add('procedure FuncB(Bar: longint);');
  3367. Add('begin');
  3368. Add(' funca(bar);');
  3369. Add('end;');
  3370. Add('procedure funca(bar: longint);');
  3371. Add('begin');
  3372. Add(' if bar=3 then ;');
  3373. Add('end;');
  3374. Add('begin');
  3375. Add(' funca(4);');
  3376. Add(' funcb(5);');
  3377. ConvertProgram;
  3378. CheckSource('TestForwardProc',
  3379. LinesToStr([ // statements'
  3380. 'this.FuncB = function (Bar) {',
  3381. ' $mod.FuncA(Bar);',
  3382. '};',
  3383. 'this.FuncA = function (Bar) {',
  3384. ' if (Bar === 3);',
  3385. '};'
  3386. ]),
  3387. LinesToStr([
  3388. '$mod.FuncA(4);',
  3389. '$mod.FuncB(5);'
  3390. ])
  3391. );
  3392. end;
  3393. procedure TTestModule.TestNestedForwardProc;
  3394. begin
  3395. StartProgram(false);
  3396. Add('procedure FuncA;');
  3397. Add(' procedure FuncB(i: longint); forward;');
  3398. Add(' procedure FuncC(i: longint);');
  3399. Add(' begin');
  3400. Add(' funcb(i);');
  3401. Add(' end;');
  3402. Add(' procedure FuncB(i: longint);');
  3403. Add(' begin');
  3404. Add(' if i=3 then ;');
  3405. Add(' end;');
  3406. Add('begin');
  3407. Add(' funcc(4)');
  3408. Add('end;');
  3409. Add('begin');
  3410. Add(' funca;');
  3411. ConvertProgram;
  3412. CheckSource('TestNestedForwardProc',
  3413. LinesToStr([ // statements'
  3414. 'this.FuncA = function () {',
  3415. ' function FuncC(i) {',
  3416. ' FuncB(i);',
  3417. ' };',
  3418. ' function FuncB(i) {',
  3419. ' if (i === 3);',
  3420. ' };',
  3421. ' FuncC(4);',
  3422. '};'
  3423. ]),
  3424. LinesToStr([
  3425. '$mod.FuncA();'
  3426. ])
  3427. );
  3428. end;
  3429. procedure TTestModule.TestAssignFunctionResult;
  3430. begin
  3431. StartProgram(false);
  3432. Add('function Func1: longint;');
  3433. Add('begin');
  3434. Add('end;');
  3435. Add('var i: longint;');
  3436. Add('begin');
  3437. Add(' i:=func1();');
  3438. Add(' i:=func1()+func1();');
  3439. ConvertProgram;
  3440. CheckSource('TestAssignFunctionResult',
  3441. LinesToStr([ // statements
  3442. 'this.Func1 = function () {',
  3443. ' var Result = 0;',
  3444. ' return Result;',
  3445. '};',
  3446. 'this.i = 0;'
  3447. ]),
  3448. LinesToStr([
  3449. '$mod.i = $mod.Func1();',
  3450. '$mod.i = $mod.Func1() + $mod.Func1();'
  3451. ]));
  3452. end;
  3453. procedure TTestModule.TestFunctionResultInCondition;
  3454. begin
  3455. StartProgram(false);
  3456. Add('function Func1: longint;');
  3457. Add('begin');
  3458. Add('end;');
  3459. Add('function Func2: boolean;');
  3460. Add('begin');
  3461. Add('end;');
  3462. Add('var i: longint;');
  3463. Add('begin');
  3464. Add(' if func2 then ;');
  3465. Add(' if i=func1() then ;');
  3466. Add(' if i=func1 then ;');
  3467. ConvertProgram;
  3468. CheckSource('TestFunctionResultInCondition',
  3469. LinesToStr([ // statements
  3470. 'this.Func1 = function () {',
  3471. ' var Result = 0;',
  3472. ' return Result;',
  3473. '};',
  3474. 'this.Func2 = function () {',
  3475. ' var Result = false;',
  3476. ' return Result;',
  3477. '};',
  3478. 'this.i = 0;'
  3479. ]),
  3480. LinesToStr([
  3481. 'if ($mod.Func2());',
  3482. 'if ($mod.i === $mod.Func1());',
  3483. 'if ($mod.i === $mod.Func1());'
  3484. ]));
  3485. end;
  3486. procedure TTestModule.TestFunctionResultInForLoop;
  3487. begin
  3488. StartProgram(false);
  3489. Add([
  3490. 'function Func1(a: array of longint): longint;',
  3491. 'begin',
  3492. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3493. ' for Result in a do if a[Result]=0 then exit;',
  3494. 'end;',
  3495. 'begin',
  3496. ' Func1([1,2,3])']);
  3497. ConvertProgram;
  3498. CheckSource('TestFunctionResultInForLoop',
  3499. LinesToStr([ // statements
  3500. 'this.Func1 = function (a) {',
  3501. ' var Result = 0;',
  3502. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3503. ' Result = $l;',
  3504. ' if (a[Result] === 0) return Result;',
  3505. ' };',
  3506. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3507. ' Result = $in[$l1];',
  3508. ' if (a[Result] === 0) return Result;',
  3509. ' };',
  3510. ' return Result;',
  3511. '};',
  3512. '']),
  3513. LinesToStr([
  3514. '$mod.Func1([1, 2, 3]);'
  3515. ]));
  3516. end;
  3517. procedure TTestModule.TestFunctionResultInTypeCast;
  3518. begin
  3519. StartProgram(false);
  3520. Add([
  3521. 'function GetInt: longint;',
  3522. 'begin',
  3523. 'end;',
  3524. 'begin',
  3525. ' if Byte(GetInt)=0 then ;',
  3526. '']);
  3527. ConvertProgram;
  3528. CheckSource('TestFunctionResultInTypeCast',
  3529. LinesToStr([ // statements
  3530. 'this.GetInt = function () {',
  3531. ' var Result = 0;',
  3532. ' return Result;',
  3533. '};',
  3534. '']),
  3535. LinesToStr([
  3536. 'if (($mod.GetInt() & 255) === 0) ;'
  3537. ]));
  3538. end;
  3539. procedure TTestModule.TestExit;
  3540. begin
  3541. StartProgram(false);
  3542. Add('procedure ProcA;');
  3543. Add('begin');
  3544. Add(' exit;');
  3545. Add('end;');
  3546. Add('function FuncB: longint;');
  3547. Add('begin');
  3548. Add(' exit;');
  3549. Add(' exit(3);');
  3550. Add('end;');
  3551. Add('function FuncC: string;');
  3552. Add('begin');
  3553. Add(' exit;');
  3554. Add(' exit(''a'');');
  3555. Add(' exit(''abc'');');
  3556. Add('end;');
  3557. Add('begin');
  3558. Add(' exit;');
  3559. Add(' exit(1);');
  3560. ConvertProgram;
  3561. CheckSource('TestExit',
  3562. LinesToStr([ // statements
  3563. 'this.ProcA = function () {',
  3564. ' return;',
  3565. '};',
  3566. 'this.FuncB = function () {',
  3567. ' var Result = 0;',
  3568. ' return Result;',
  3569. ' return 3;',
  3570. ' return Result;',
  3571. '};',
  3572. 'this.FuncC = function () {',
  3573. ' var Result = "";',
  3574. ' return Result;',
  3575. ' return "a";',
  3576. ' return "abc";',
  3577. ' return Result;',
  3578. '};'
  3579. ]),
  3580. LinesToStr([
  3581. 'return;',
  3582. 'return 1;',
  3583. '']));
  3584. end;
  3585. procedure TTestModule.TestExit_ResultInFinally;
  3586. begin
  3587. StartProgram(false);
  3588. Add([
  3589. 'function Run: word;',
  3590. 'begin',
  3591. ' try',
  3592. ' exit(3);', // no Result in finally -> use return 3
  3593. ' finally',
  3594. ' end;',
  3595. 'end;',
  3596. 'function Fly: word;',
  3597. 'begin',
  3598. ' try',
  3599. ' exit(3);',
  3600. ' finally',
  3601. ' if Result>0 then ;',
  3602. ' end;',
  3603. 'end;',
  3604. 'function Jump: word;',
  3605. 'begin',
  3606. ' try',
  3607. ' try',
  3608. ' exit(4);',
  3609. ' finally',
  3610. ' end;',
  3611. ' finally',
  3612. ' if Result>0 then ;',
  3613. ' end;',
  3614. 'end;',
  3615. 'begin',
  3616. '']);
  3617. ConvertProgram;
  3618. CheckSource('TestExit_ResultInFinally',
  3619. LinesToStr([ // statements
  3620. 'this.Run = function () {',
  3621. ' var Result = 0;',
  3622. ' try {',
  3623. ' return 3;',
  3624. ' } finally {',
  3625. ' };',
  3626. ' return Result;',
  3627. '};',
  3628. 'this.Fly = function () {',
  3629. ' var Result = 0;',
  3630. ' try {',
  3631. ' Result = 3;',
  3632. ' return Result;',
  3633. ' } finally {',
  3634. ' if (Result > 0) ;',
  3635. ' };',
  3636. ' return Result;',
  3637. '};',
  3638. 'this.Jump = function () {',
  3639. ' var Result = 0;',
  3640. ' try {',
  3641. ' try {',
  3642. ' Result = 4;',
  3643. ' return Result;',
  3644. ' } finally {',
  3645. ' };',
  3646. ' } finally {',
  3647. ' if (Result > 0) ;',
  3648. ' };',
  3649. ' return Result;',
  3650. '};',
  3651. '']),
  3652. LinesToStr([
  3653. '']));
  3654. end;
  3655. procedure TTestModule.TestBreak;
  3656. begin
  3657. StartProgram(false);
  3658. Add([
  3659. 'var',
  3660. ' i: longint;',
  3661. 'begin',
  3662. ' repeat',
  3663. ' break;',
  3664. ' until true;',
  3665. ' while true do',
  3666. ' break;',
  3667. ' for i:=1 to 2 do',
  3668. ' break;']);
  3669. ConvertProgram;
  3670. CheckSource('TestBreak',
  3671. LinesToStr([ // statements
  3672. 'this.i = 0;'
  3673. ]),
  3674. LinesToStr([
  3675. 'do {',
  3676. ' break;',
  3677. '} while (!true);',
  3678. 'while (true) break;',
  3679. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3680. '']));
  3681. end;
  3682. procedure TTestModule.TestBreakAsVar;
  3683. begin
  3684. StartProgram(false);
  3685. Add([
  3686. 'procedure DoIt(break: boolean);',
  3687. 'begin',
  3688. ' if break then ;',
  3689. 'end;',
  3690. 'var',
  3691. ' break: boolean;',
  3692. 'begin',
  3693. ' if break then ;']);
  3694. ConvertProgram;
  3695. CheckSource('TestBreakAsVar',
  3696. LinesToStr([ // statements
  3697. 'this.DoIt = function (Break) {',
  3698. ' if (Break) ;',
  3699. '};',
  3700. 'this.Break = false;',
  3701. '']),
  3702. LinesToStr([
  3703. 'if($mod.Break) ;',
  3704. '']));
  3705. end;
  3706. procedure TTestModule.TestContinue;
  3707. begin
  3708. StartProgram(false);
  3709. Add('var i: longint;');
  3710. Add('begin');
  3711. Add(' repeat');
  3712. Add(' continue;');
  3713. Add(' until true;');
  3714. Add(' while true do');
  3715. Add(' continue;');
  3716. Add(' for i:=1 to 2 do');
  3717. Add(' continue;');
  3718. ConvertProgram;
  3719. CheckSource('TestContinue',
  3720. LinesToStr([ // statements
  3721. 'this.i = 0;'
  3722. ]),
  3723. LinesToStr([
  3724. 'do {',
  3725. ' continue;',
  3726. '} while (!true);',
  3727. 'while (true) continue;',
  3728. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3729. '']));
  3730. end;
  3731. procedure TTestModule.TestProc_External;
  3732. begin
  3733. StartProgram(false);
  3734. Add('procedure Foo; external name ''console.log'';');
  3735. Add('function Bar: longint; external name ''get.item'';');
  3736. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3737. Add('var');
  3738. Add(' i: longint;');
  3739. Add('begin');
  3740. Add(' Foo;');
  3741. Add(' i:=Bar;');
  3742. Add(' i:=Bla(''abc'');');
  3743. ConvertProgram;
  3744. CheckSource('TestProc_External',
  3745. LinesToStr([ // statements
  3746. 'this.i = 0;'
  3747. ]),
  3748. LinesToStr([
  3749. 'console.log();',
  3750. '$mod.i = get.item();',
  3751. '$mod.i = apply.something("abc");'
  3752. ]));
  3753. end;
  3754. procedure TTestModule.TestProc_ExternalOtherUnit;
  3755. begin
  3756. AddModuleWithIntfImplSrc('unit2.pas',
  3757. LinesToStr([
  3758. 'procedure Now; external name ''Date.now'';',
  3759. 'procedure DoIt;'
  3760. ]),
  3761. 'procedure doit; begin end;');
  3762. StartUnit(true);
  3763. Add('interface');
  3764. Add('uses unit2;');
  3765. Add('implementation');
  3766. Add('begin');
  3767. Add(' now;');
  3768. Add(' now();');
  3769. Add(' uNit2.now;');
  3770. Add(' uNit2.now();');
  3771. Add(' doit;');
  3772. Add(' uNit2.doit;');
  3773. ConvertUnit;
  3774. CheckSource('TestProc_ExternalOtherUnit',
  3775. LinesToStr([
  3776. '']),
  3777. LinesToStr([
  3778. 'Date.now();',
  3779. 'Date.now();',
  3780. 'Date.now();',
  3781. 'Date.now();',
  3782. 'pas.unit2.DoIt();',
  3783. 'pas.unit2.DoIt();',
  3784. '']));
  3785. end;
  3786. procedure TTestModule.TestProc_Asm;
  3787. begin
  3788. StartProgram(false);
  3789. Add([
  3790. '{$mode delphi}',
  3791. 'function DoIt: longint;',
  3792. 'begin;',
  3793. ' asm',
  3794. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3795. ' end;',
  3796. ' asm console.log(); end;',
  3797. ' asm',
  3798. ' s = "'' ";',
  3799. ' s = ''" '';',
  3800. ' s = s + "world" + "''";',
  3801. ' // end',
  3802. ' s = ''end'';',
  3803. ' s = "end";',
  3804. ' s = "foo\"bar";',
  3805. ' s = ''a\''b'';',
  3806. ' s = `${expr}\`-"-''-`;',
  3807. ' s = `multi',
  3808. 'line`;',
  3809. ' end;',
  3810. 'end;',
  3811. 'procedure Fly;',
  3812. 'asm',
  3813. ' return;',
  3814. 'end;',
  3815. 'begin']);
  3816. ConvertProgram;
  3817. CheckSource('TestProc_Asm',
  3818. LinesToStr([ // statements
  3819. 'this.DoIt = function () {',
  3820. ' var Result = 0;',
  3821. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3822. ' console.log();',
  3823. ' s = "'' ";',
  3824. ' s = ''" '';',
  3825. ' s = s + "world" + "''";',
  3826. ' // end',
  3827. ' s = ''end'';',
  3828. ' s = "end";',
  3829. ' s = "foo\"bar";',
  3830. ' s = ''a\''b'';',
  3831. ' s = `${expr}\`-"-''-`;',
  3832. ' s = `multi',
  3833. 'line`;',
  3834. ' return Result;',
  3835. '};',
  3836. 'this.Fly = function () {',
  3837. ' return;',
  3838. '};',
  3839. '']),
  3840. LinesToStr([
  3841. ''
  3842. ]));
  3843. end;
  3844. procedure TTestModule.TestProc_Assembler;
  3845. begin
  3846. StartProgram(false);
  3847. Add('function DoIt: longint; assembler;');
  3848. Add('asm');
  3849. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3850. Add('end;');
  3851. Add('begin');
  3852. ConvertProgram;
  3853. CheckSource('TestProc_Assembler',
  3854. LinesToStr([ // statements
  3855. 'this.DoIt = function () {',
  3856. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3857. '};'
  3858. ]),
  3859. LinesToStr([
  3860. ''
  3861. ]));
  3862. end;
  3863. procedure TTestModule.TestProc_VarParam;
  3864. begin
  3865. StartProgram(false);
  3866. Add('type integer = longint;');
  3867. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3868. Add('var vJ: integer;');
  3869. Add('begin');
  3870. Add(' vg:=vg+1;');
  3871. Add(' vj:=vh+2;');
  3872. Add(' vi:=vi+3;');
  3873. Add(' doit(vg,vg,vg);');
  3874. Add(' doit(vh,vh,vj);');
  3875. Add(' doit(vi,vi,vi);');
  3876. Add(' doit(vj,vj,vj);');
  3877. Add('end;');
  3878. Add('var i: integer;');
  3879. Add('begin');
  3880. Add(' doit(i,i,i);');
  3881. ConvertProgram;
  3882. CheckSource('TestProc_VarParam',
  3883. LinesToStr([ // statements
  3884. 'this.DoIt = function (vG,vH,vI) {',
  3885. ' var vJ = 0;',
  3886. ' vG = vG + 1;',
  3887. ' vJ = vH + 2;',
  3888. ' vI.set(vI.get()+3);',
  3889. ' $mod.DoIt(vG, vG, {',
  3890. ' get: function () {',
  3891. ' return vG;',
  3892. ' },',
  3893. ' set: function (v) {',
  3894. ' vG = v;',
  3895. ' }',
  3896. ' });',
  3897. ' $mod.DoIt(vH, vH, {',
  3898. ' get: function () {',
  3899. ' return vJ;',
  3900. ' },',
  3901. ' set: function (v) {',
  3902. ' vJ = v;',
  3903. ' }',
  3904. ' });',
  3905. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3906. ' $mod.DoIt(vJ, vJ, {',
  3907. ' get: function () {',
  3908. ' return vJ;',
  3909. ' },',
  3910. ' set: function (v) {',
  3911. ' vJ = v;',
  3912. ' }',
  3913. ' });',
  3914. '};',
  3915. 'this.i = 0;'
  3916. ]),
  3917. LinesToStr([
  3918. '$mod.DoIt($mod.i,$mod.i,{',
  3919. ' p: $mod,',
  3920. ' get: function () {',
  3921. ' return this.p.i;',
  3922. ' },',
  3923. ' set: function (v) {',
  3924. ' this.p.i = v;',
  3925. ' }',
  3926. '});'
  3927. ]));
  3928. end;
  3929. procedure TTestModule.TestProc_VarParamString;
  3930. begin
  3931. StartProgram(false);
  3932. Add(['type TCaption = string;',
  3933. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3934. 'var c: char;',
  3935. 'begin',
  3936. ' va[1]:=c;',
  3937. ' vb[2]:=c;',
  3938. ' vc[3]:=c;',
  3939. 'end;',
  3940. 'begin']);
  3941. ConvertProgram;
  3942. CheckSource('TestProc_VarParamString',
  3943. LinesToStr([ // statements
  3944. 'this.DoIt = function (vA,vB,vC) {',
  3945. ' var c = "";',
  3946. ' vA = rtl.setCharAt(vA, 0, c);',
  3947. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3948. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3949. '};',
  3950. '']),
  3951. LinesToStr([
  3952. ]));
  3953. end;
  3954. procedure TTestModule.TestProc_VarParamV;
  3955. begin
  3956. StartProgram(false);
  3957. Add([
  3958. 'procedure Inc2(var i: longint);',
  3959. 'begin',
  3960. ' i:=i+2;',
  3961. 'end;',
  3962. 'procedure DoIt(v: longint);',
  3963. 'var p: array of longint;',
  3964. 'begin',
  3965. ' Inc2(v);',
  3966. ' Inc2(p[v]);',
  3967. 'end;',
  3968. 'begin']);
  3969. ConvertProgram;
  3970. CheckSource('TestProc_VarParamV',
  3971. LinesToStr([ // statements
  3972. 'this.Inc2 = function (i) {',
  3973. ' i.set(i.get()+2);',
  3974. '};',
  3975. 'this.DoIt = function (v) {',
  3976. ' var p = [];',
  3977. ' $mod.Inc2({get: function () {',
  3978. ' return v;',
  3979. ' }, set: function (w) {',
  3980. ' v = w;',
  3981. ' }});',
  3982. ' $mod.Inc2({',
  3983. ' a: v,',
  3984. ' p: p,',
  3985. ' get: function () {',
  3986. ' return this.p[this.a];',
  3987. ' },',
  3988. ' set: function (v) {',
  3989. ' this.p[this.a] = v;',
  3990. ' }',
  3991. ' });',
  3992. '};',
  3993. '']),
  3994. LinesToStr([
  3995. '']));
  3996. end;
  3997. procedure TTestModule.TestProc_Overload;
  3998. begin
  3999. StartProgram(false);
  4000. Add('procedure DoIt(vI: longint); begin end;');
  4001. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4002. Add('procedure DoIt(vD: double); begin end;');
  4003. Add('begin');
  4004. Add(' DoIt(1);');
  4005. Add(' DoIt(2,3);');
  4006. Add(' DoIt(4.5);');
  4007. ConvertProgram;
  4008. CheckSource('TestProcedureOverload',
  4009. LinesToStr([ // statements
  4010. 'this.DoIt = function (vI) {',
  4011. '};',
  4012. 'this.DoIt$1 = function (vI, vJ) {',
  4013. '};',
  4014. 'this.DoIt$2 = function (vD) {',
  4015. '};',
  4016. '']),
  4017. LinesToStr([
  4018. '$mod.DoIt(1);',
  4019. '$mod.DoIt$1(2, 3);',
  4020. '$mod.DoIt$2(4.5);',
  4021. '']));
  4022. end;
  4023. procedure TTestModule.TestProc_OverloadForward;
  4024. begin
  4025. StartProgram(false);
  4026. Add('procedure DoIt(vI: longint); forward;');
  4027. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4028. Add('procedure doit(vi: longint); begin end;');
  4029. Add('begin');
  4030. Add(' doit(1);');
  4031. Add(' doit(2,3);');
  4032. ConvertProgram;
  4033. CheckSource('TestProcedureOverloadForward',
  4034. LinesToStr([ // statements
  4035. 'this.DoIt$1 = function (vI, vJ) {',
  4036. '};',
  4037. 'this.DoIt = function (vI) {',
  4038. '};',
  4039. '']),
  4040. LinesToStr([
  4041. '$mod.DoIt(1);',
  4042. '$mod.DoIt$1(2, 3);',
  4043. '']));
  4044. end;
  4045. procedure TTestModule.TestProc_OverloadIntfImpl;
  4046. begin
  4047. StartUnit(false);
  4048. Add('interface');
  4049. Add('procedure DoIt(vI: longint);');
  4050. Add('procedure DoIt(vI, vJ: longint);');
  4051. Add('implementation');
  4052. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4053. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4054. Add('procedure DoIt(vi: longint); begin end;');
  4055. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4056. Add('procedure DoIt(vi, vj: longint); begin end;');
  4057. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4058. Add('begin');
  4059. Add(' doit(1);');
  4060. Add(' doit(2,3);');
  4061. Add(' doit(4,5,6);');
  4062. Add(' doit(7,8,9,10);');
  4063. Add(' doit(11,12,13,14,15);');
  4064. ConvertUnit;
  4065. CheckSource('TestProcedureOverloadUnit',
  4066. LinesToStr([ // statements
  4067. 'var $impl = $mod.$impl;',
  4068. 'this.DoIt = function (vI) {',
  4069. '};',
  4070. 'this.DoIt$1 = function (vI, vJ) {',
  4071. '};',
  4072. '']),
  4073. LinesToStr([ // this.$init
  4074. '$mod.DoIt(1);',
  4075. '$mod.DoIt$1(2, 3);',
  4076. '$impl.DoIt$3(4,5,6);',
  4077. '$impl.DoIt$4(7,8,9,10);',
  4078. '$impl.DoIt$2(11,12,13,14,15);',
  4079. '']),
  4080. LinesToStr([ // implementation
  4081. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4082. '};',
  4083. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4084. '};',
  4085. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4086. '};',
  4087. '']));
  4088. end;
  4089. procedure TTestModule.TestProc_OverloadNested;
  4090. begin
  4091. StartProgram(false);
  4092. Add([
  4093. 'procedure doit(vA: longint);',
  4094. ' procedure DoIt(vA, vB: longint); overload;',
  4095. ' begin',
  4096. ' doit(1);',
  4097. ' doit(1,2);',
  4098. ' end;',
  4099. ' procedure doit(vA, vB, vC: longint);',
  4100. ' begin',
  4101. ' doit(1);',
  4102. ' doit(1,2);',
  4103. ' doit(1,2,3);',
  4104. ' end;',
  4105. 'begin',
  4106. ' doit(1);',
  4107. ' doit(1,2);',
  4108. ' doit(1,2,3);',
  4109. 'end;',
  4110. 'begin // main',
  4111. ' doit(1);']);
  4112. ConvertProgram;
  4113. CheckSource('TestProcedureOverloadNested',
  4114. LinesToStr([ // statements
  4115. 'this.doit = function (vA) {',
  4116. ' function DoIt$1(vA, vB) {',
  4117. ' $mod.doit(1);',
  4118. ' DoIt$1(1, 2);',
  4119. ' };',
  4120. ' function doit$2(vA, vB, vC) {',
  4121. ' $mod.doit(1);',
  4122. ' DoIt$1(1, 2);',
  4123. ' doit$2(1, 2, 3);',
  4124. ' };',
  4125. ' $mod.doit(1);',
  4126. ' DoIt$1(1, 2);',
  4127. ' doit$2(1, 2, 3);',
  4128. '};',
  4129. '']),
  4130. LinesToStr([
  4131. '$mod.doit(1);',
  4132. '']));
  4133. end;
  4134. procedure TTestModule.TestProc_OverloadNestedForward;
  4135. begin
  4136. StartProgram(false);
  4137. Add([
  4138. 'procedure DoIt(vA: longint); overload; forward;',
  4139. 'procedure DoIt(vB, vC: longint); overload;',
  4140. 'begin // 2 param overload',
  4141. ' doit(1);',
  4142. ' doit(1,2);',
  4143. 'end;',
  4144. 'procedure doit(vA: longint);',
  4145. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4146. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4147. ' begin // 4 param overload',
  4148. ' doit(1);',
  4149. ' doit(1,2);',
  4150. ' doit(1,2,3);',
  4151. ' doit(1,2,3,4);',
  4152. ' end;',
  4153. ' procedure doit(vA, vB, vC: longint);',
  4154. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4155. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4156. ' begin // 6 param overload',
  4157. ' doit(1);',
  4158. ' doit(1,2);',
  4159. ' doit(1,2,3);',
  4160. ' doit(1,2,3,4);',
  4161. ' doit(1,2,3,4,5);',
  4162. ' doit(1,2,3,4,5,6);',
  4163. ' end;',
  4164. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4165. ' begin // 5 param overload',
  4166. ' doit(1);',
  4167. ' doit(1,2);',
  4168. ' doit(1,2,3);',
  4169. ' doit(1,2,3,4);',
  4170. ' doit(1,2,3,4,5);',
  4171. ' doit(1,2,3,4,5,6);',
  4172. ' end;',
  4173. ' begin // 3 param overload',
  4174. ' doit(1);',
  4175. ' doit(1,2);',
  4176. ' doit(1,2,3);',
  4177. ' doit(1,2,3,4);',
  4178. ' doit(1,2,3,4,5);',
  4179. ' doit(1,2,3,4,5,6);',
  4180. ' end;',
  4181. 'begin // 1 param overload',
  4182. ' doit(1);',
  4183. ' doit(1,2);',
  4184. ' doit(1,2,3);',
  4185. ' doit(1,2,3,4);',
  4186. 'end;',
  4187. 'begin // main',
  4188. ' doit(1);',
  4189. ' doit(1,2);']);
  4190. ConvertProgram;
  4191. CheckSource('TestProc_OverloadNestedForward',
  4192. LinesToStr([ // statements
  4193. 'this.DoIt$1 = function (vB, vC) {',
  4194. ' $mod.DoIt(1);',
  4195. ' $mod.DoIt$1(1, 2);',
  4196. '};',
  4197. 'this.DoIt = function (vA) {',
  4198. ' function DoIt$3(vA, vB, vC, vD) {',
  4199. ' $mod.DoIt(1);',
  4200. ' $mod.DoIt$1(1, 2);',
  4201. ' DoIt$2(1, 2, 3);',
  4202. ' DoIt$3(1, 2, 3, 4);',
  4203. ' };',
  4204. ' function DoIt$2(vA, vB, vC) {',
  4205. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4206. ' $mod.DoIt(1);',
  4207. ' $mod.DoIt$1(1, 2);',
  4208. ' DoIt$2(1, 2, 3);',
  4209. ' DoIt$3(1, 2, 3, 4);',
  4210. ' DoIt$4(1, 2, 3, 4, 5);',
  4211. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4212. ' };',
  4213. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4214. ' $mod.DoIt(1);',
  4215. ' $mod.DoIt$1(1, 2);',
  4216. ' DoIt$2(1, 2, 3);',
  4217. ' DoIt$3(1, 2, 3, 4);',
  4218. ' DoIt$4(1, 2, 3, 4, 5);',
  4219. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4220. ' };',
  4221. ' $mod.DoIt(1);',
  4222. ' $mod.DoIt$1(1, 2);',
  4223. ' DoIt$2(1, 2, 3);',
  4224. ' DoIt$3(1, 2, 3, 4);',
  4225. ' DoIt$4(1, 2, 3, 4, 5);',
  4226. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4227. ' };',
  4228. ' $mod.DoIt(1);',
  4229. ' $mod.DoIt$1(1, 2);',
  4230. ' DoIt$2(1, 2, 3);',
  4231. ' DoIt$3(1, 2, 3, 4);',
  4232. '};',
  4233. '']),
  4234. LinesToStr([
  4235. '$mod.DoIt(1);',
  4236. '$mod.DoIt$1(1, 2);',
  4237. '']));
  4238. end;
  4239. procedure TTestModule.TestProc_OverloadUnitCycle;
  4240. begin
  4241. AddModuleWithIntfImplSrc('Unit2.pas',
  4242. LinesToStr([
  4243. 'type',
  4244. ' TObject = class',
  4245. ' procedure DoIt(b: boolean); virtual; abstract;',
  4246. ' procedure DoIt(i: longint); virtual; abstract;',
  4247. ' end;',
  4248. '']),
  4249. 'uses test1;');
  4250. StartUnit(true);
  4251. Add([
  4252. 'interface',
  4253. 'uses unit2;',
  4254. 'type',
  4255. ' TEagle = class(TObject)',
  4256. ' procedure DoIt(b: boolean); override;',
  4257. ' procedure DoIt(i: longint); override;',
  4258. ' end;',
  4259. 'implementation',
  4260. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4261. 'procedure TEagle.DoIt(i: longint); begin end;',
  4262. '']);
  4263. ConvertUnit;
  4264. CheckSource('TestProc_OverloadUnitCycle',
  4265. LinesToStr([ // statements
  4266. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4267. ' this.DoIt = function (b) {',
  4268. ' };',
  4269. ' this.DoIt$1 = function (i) {',
  4270. ' };',
  4271. '});',
  4272. '']),
  4273. '',
  4274. LinesToStr([
  4275. '']));
  4276. end;
  4277. procedure TTestModule.TestProc_Varargs;
  4278. begin
  4279. StartProgram(false);
  4280. Add([
  4281. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4282. 'procedure ProcB; varargs; external name ''ProcB'';',
  4283. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4284. 'function GetIt: longint; begin end;',
  4285. 'begin',
  4286. ' ProcA(1);',
  4287. ' ProcA(1,2);',
  4288. ' ProcA(1,2.0);',
  4289. ' ProcA(1,2,3);',
  4290. ' ProcA(1,''2'');',
  4291. ' ProcA(2,'''');',
  4292. ' ProcA(3,false);',
  4293. ' ProcB;',
  4294. ' ProcB();',
  4295. ' ProcB(4);',
  4296. ' ProcB(''foo'');',
  4297. ' ProcC;',
  4298. ' ProcC();',
  4299. ' ProcC(4);',
  4300. ' ProcC(5,''foo'');',
  4301. ' ProcB(GetIt);',
  4302. ' ProcB(GetIt());',
  4303. ' ProcB(GetIt,GetIt());']);
  4304. ConvertProgram;
  4305. CheckSource('TestProc_Varargs',
  4306. LinesToStr([ // statements
  4307. 'this.GetIt = function () {',
  4308. ' var Result = 0;',
  4309. ' return Result;',
  4310. '};',
  4311. '']),
  4312. LinesToStr([
  4313. 'ProcA(1);',
  4314. 'ProcA(1, 2);',
  4315. 'ProcA(1, 2.0);',
  4316. 'ProcA(1, 2, 3);',
  4317. 'ProcA(1, "2");',
  4318. 'ProcA(2, "");',
  4319. 'ProcA(3, false);',
  4320. 'ProcB();',
  4321. 'ProcB();',
  4322. 'ProcB(4);',
  4323. 'ProcB("foo");',
  4324. 'ProcC(17);',
  4325. 'ProcC(17);',
  4326. 'ProcC(4);',
  4327. 'ProcC(5, "foo");',
  4328. 'ProcB($mod.GetIt());',
  4329. 'ProcB($mod.GetIt());',
  4330. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4331. '']));
  4332. end;
  4333. procedure TTestModule.TestProc_ConstOrder;
  4334. begin
  4335. StartProgram(false);
  4336. Add([
  4337. 'const A = 3;',
  4338. 'const B = A+1;',
  4339. 'procedure DoIt;',
  4340. 'const C = A+1;',
  4341. 'const D = B+1;',
  4342. 'const E = D+C+B+A;',
  4343. 'begin',
  4344. 'end;',
  4345. 'begin'
  4346. ]);
  4347. ConvertProgram;
  4348. CheckSource('TestProc_ConstOrder',
  4349. LinesToStr([ // statements
  4350. 'this.A = 3;',
  4351. 'this.B = 3 + 1;',
  4352. 'var C = 3 + 1;',
  4353. 'var D = 4 + 1;',
  4354. 'var E = 5 + 4 + 4 + 3;',
  4355. 'this.DoIt = function () {',
  4356. '};',
  4357. '']),
  4358. LinesToStr([
  4359. ''
  4360. ]));
  4361. end;
  4362. procedure TTestModule.TestProc_DuplicateConst;
  4363. begin
  4364. StartProgram(false);
  4365. Add([
  4366. 'const A = 1;',
  4367. 'procedure DoIt;',
  4368. 'const A = 2;',
  4369. ' procedure SubIt;',
  4370. ' const A = 21;',
  4371. ' begin',
  4372. ' end;',
  4373. 'begin',
  4374. 'end;',
  4375. 'procedure DoSome;',
  4376. 'const A = 3;',
  4377. 'begin',
  4378. 'end;',
  4379. 'begin'
  4380. ]);
  4381. ConvertProgram;
  4382. CheckSource('TestProc_DuplicateConst',
  4383. LinesToStr([ // statements
  4384. 'this.A = 1;',
  4385. 'var A$1 = 2;',
  4386. 'var A$2 = 21;',
  4387. 'this.DoIt = function () {',
  4388. ' function SubIt() {',
  4389. ' };',
  4390. '};',
  4391. 'var A$3 = 3;',
  4392. 'this.DoSome = function () {',
  4393. '};',
  4394. '']),
  4395. LinesToStr([
  4396. ''
  4397. ]));
  4398. end;
  4399. procedure TTestModule.TestProc_LocalVarAbsolute;
  4400. begin
  4401. StartProgram(false);
  4402. Add([
  4403. 'type',
  4404. ' TObject = class',
  4405. ' Index: longint;',
  4406. ' procedure DoAbs(Item: pointer);',
  4407. ' end;',
  4408. 'procedure TObject.DoAbs(Item: pointer);',
  4409. 'var',
  4410. ' o: TObject absolute Item;',
  4411. 'begin',
  4412. ' if o.Index<o.Index then o.Index:=o.Index;',
  4413. 'end;',
  4414. 'procedure DoIt(i: longint; p: pointer);',
  4415. 'var',
  4416. ' d: double absolute i;',
  4417. ' s: string absolute d;',
  4418. ' oi: TObject absolute i;',
  4419. ' op: TObject absolute p;',
  4420. 'begin',
  4421. ' if d=d then d:=d;',
  4422. ' if s=s then s:=s;',
  4423. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4424. ' if op.Index=op.Index then op.Index:=op.Index;',
  4425. 'end;',
  4426. 'begin']);
  4427. ConvertProgram;
  4428. CheckSource('TestProc_LocalVarAbsolute',
  4429. LinesToStr([ // statements
  4430. 'rtl.createClass(this, "TObject", null, function () {',
  4431. ' this.$init = function () {',
  4432. ' this.Index = 0;',
  4433. ' };',
  4434. ' this.$final = function () {',
  4435. ' };',
  4436. ' this.DoAbs = function (Item) {',
  4437. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4438. ' };',
  4439. '});',
  4440. 'this.DoIt = function (i, p) {',
  4441. ' if (i === i) i = i;',
  4442. ' if (i === i) i = i;',
  4443. ' if (i.Index < i.Index) i.Index = i.Index;',
  4444. ' if (p.Index === p.Index) p.Index = p.Index;',
  4445. '};'
  4446. ]),
  4447. LinesToStr([
  4448. ]));
  4449. end;
  4450. procedure TTestModule.TestProc_LocalVarInit;
  4451. begin
  4452. StartProgram(false);
  4453. Add([
  4454. 'type TBytes = array of byte;',
  4455. 'procedure DoIt;',
  4456. 'const c = 4;',
  4457. 'var',
  4458. ' b: byte = 1;',
  4459. ' w: word = 2+c;',
  4460. ' p: pointer = nil;',
  4461. ' Buffer: TBytes = nil;',
  4462. 'begin',
  4463. 'end;',
  4464. 'begin']);
  4465. ConvertProgram;
  4466. CheckSource('TestProc_LocalVarInit',
  4467. LinesToStr([ // statements
  4468. 'var c = 4;',
  4469. 'this.DoIt = function () {',
  4470. ' var b = 1;',
  4471. ' var w = 2 + 4;',
  4472. ' var p = null;',
  4473. ' var Buffer = [];',
  4474. '};',
  4475. '']),
  4476. LinesToStr([
  4477. ]));
  4478. end;
  4479. procedure TTestModule.TestProc_ReservedWords;
  4480. begin
  4481. StartProgram(false);
  4482. Add([
  4483. 'procedure Date(ArrayBuffer: longint);',
  4484. 'const',
  4485. ' NaN: longint = 3;',
  4486. 'var',
  4487. ' &Boolean: longint;',
  4488. ' procedure Error(ArrayBuffer: longint);',
  4489. ' begin',
  4490. ' end;',
  4491. 'begin',
  4492. ' Nan:=&bOolean;',
  4493. 'end;',
  4494. 'begin',
  4495. ' Date(1);']);
  4496. ConvertProgram;
  4497. CheckSource('TestProc_ReservedWords',
  4498. LinesToStr([ // statements
  4499. 'var naN = 3;',
  4500. 'this.Date = function (arrayBuffer) {',
  4501. ' var boolean = 0;',
  4502. ' function error(arrayBuffer) {',
  4503. ' };',
  4504. ' naN = boolean;',
  4505. '};',
  4506. '']),
  4507. LinesToStr([
  4508. ' $mod.Date(1);'
  4509. ]));
  4510. end;
  4511. procedure TTestModule.TestProc_ConstRefWord;
  4512. begin
  4513. StartProgram(false);
  4514. Add([
  4515. 'procedure Run(constref w: word);',
  4516. 'var l: word;',
  4517. 'begin',
  4518. ' l:=w;',
  4519. ' Run(w);',
  4520. ' Run(l);',
  4521. 'end;',
  4522. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4523. 'begin',
  4524. ' Run(a);',
  4525. ' Run(b);',
  4526. ' Run(c);',
  4527. ' Run(d);',
  4528. ' Run(e);',
  4529. 'end;',
  4530. 'begin',
  4531. ' Run(1);']);
  4532. ConvertProgram;
  4533. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4534. CheckSource('TestProc_ConstRefWord',
  4535. LinesToStr([ // statements
  4536. 'this.Run = function (w) {',
  4537. ' var l = 0;',
  4538. ' l = w;',
  4539. ' $mod.Run(w);',
  4540. ' $mod.Run(l);',
  4541. '};',
  4542. 'this.Fly = function (a, b, c, d, e) {',
  4543. ' $mod.Run(a);',
  4544. ' $mod.Run(b.get());',
  4545. ' $mod.Run(c.get());',
  4546. ' $mod.Run(d);',
  4547. ' $mod.Run(e);',
  4548. '};',
  4549. '']),
  4550. LinesToStr([
  4551. '$mod.Run(1);'
  4552. ]));
  4553. end;
  4554. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4555. begin
  4556. StartProgram(false);
  4557. Add([
  4558. '{$mode objfpc}',
  4559. 'type',
  4560. ' TFunc = reference to function(x: word): word;',
  4561. 'var Func: TFunc;',
  4562. 'procedure DoIt(a: word);',
  4563. 'begin',
  4564. ' Func:=function(b:word): word',
  4565. ' begin',
  4566. ' Result:=a+b;',
  4567. ' exit(b);',
  4568. ' exit(Result);',
  4569. ' end;',// test semicolon
  4570. ' a:=3;',
  4571. 'end;',
  4572. 'begin',
  4573. ' Func:=function(c:word):word begin',
  4574. ' Result:=3+c;',
  4575. ' exit(c);',
  4576. ' exit(Result);',
  4577. ' end;']);
  4578. ConvertProgram;
  4579. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4580. LinesToStr([ // statements
  4581. 'this.Func = null;',
  4582. 'this.DoIt = function (a) {',
  4583. ' $mod.Func = function (b) {',
  4584. ' var Result = 0;',
  4585. ' Result = a + b;',
  4586. ' return b;',
  4587. ' return Result;',
  4588. ' return Result;',
  4589. ' };',
  4590. ' a = 3;',
  4591. '};',
  4592. '']),
  4593. LinesToStr([
  4594. '$mod.Func = function (c) {',
  4595. ' var Result = 0;',
  4596. ' Result = 3 + c;',
  4597. ' return c;',
  4598. ' return Result;',
  4599. ' return Result;',
  4600. '};',
  4601. '']));
  4602. end;
  4603. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4604. begin
  4605. StartProgram(false);
  4606. Add([
  4607. '{$mode delphi}',
  4608. 'type',
  4609. ' TProc = reference to procedure(x: word);',
  4610. 'procedure DoIt(a: word);',
  4611. 'var Proc: TProc;',
  4612. 'begin',
  4613. ' Proc:=procedure(b:word) begin end;',
  4614. 'end;',
  4615. 'var Proc: TProc;',
  4616. 'begin',
  4617. ' Proc:=procedure(c:word) begin end;',
  4618. '']);
  4619. ConvertProgram;
  4620. CheckSource('TestAnonymousProc_Assign_Delphi',
  4621. LinesToStr([ // statements
  4622. 'this.DoIt = function (a) {',
  4623. ' var Proc = null;',
  4624. ' Proc = function (b) {',
  4625. ' };',
  4626. '};',
  4627. 'this.Proc = null;',
  4628. '']),
  4629. LinesToStr([
  4630. '$mod.Proc = function (c) {',
  4631. '};',
  4632. '']));
  4633. end;
  4634. procedure TTestModule.TestAnonymousProc_Arg;
  4635. begin
  4636. StartProgram(false);
  4637. Add([
  4638. 'type',
  4639. ' TProc = reference to procedure;',
  4640. ' TFunc = reference to function(x: word): word;',
  4641. 'procedure DoMore(f,g: TProc);',
  4642. 'begin',
  4643. 'end;',
  4644. 'procedure DoOdd(v: jsvalue);',
  4645. 'begin',
  4646. 'end;',
  4647. 'procedure DoIt(f: TFunc);',
  4648. 'begin',
  4649. ' DoIt(function(b:word): word',
  4650. ' begin',
  4651. ' Result:=1+b;',
  4652. ' end);',
  4653. ' DoMore(procedure begin end, procedure begin end);',
  4654. ' DoOdd(procedure begin end);',
  4655. 'end;',
  4656. 'begin',
  4657. ' DoMore(procedure begin end,',
  4658. ' procedure assembler asm',
  4659. ' console.log("c");',
  4660. ' end);',
  4661. '']);
  4662. ConvertProgram;
  4663. CheckSource('TestAnonymousProc_Arg',
  4664. LinesToStr([ // statements
  4665. 'this.DoMore = function (f, g) {',
  4666. '};',
  4667. 'this.DoOdd = function (v) {',
  4668. '};',
  4669. 'this.DoIt = function (f) {',
  4670. ' $mod.DoIt(function (b) {',
  4671. ' var Result = 0;',
  4672. ' Result = 1 + b;',
  4673. ' return Result;',
  4674. ' });',
  4675. ' $mod.DoMore(function () {',
  4676. ' }, function () {',
  4677. ' });',
  4678. ' $mod.DoOdd(function () {',
  4679. ' });',
  4680. '};',
  4681. '']),
  4682. LinesToStr([
  4683. '$mod.DoMore(function () {',
  4684. '}, function () {',
  4685. ' console.log("c");',
  4686. '});',
  4687. '']));
  4688. end;
  4689. procedure TTestModule.TestAnonymousProc_Typecast;
  4690. begin
  4691. StartProgram(false);
  4692. Add([
  4693. 'type',
  4694. ' TProc = reference to procedure(w: word);',
  4695. ' TArr = array of word;',
  4696. ' TFuncArr = reference to function: TArr;',
  4697. 'procedure DoIt(p: TProc);',
  4698. 'var',
  4699. ' w: word;',
  4700. ' a: TArr;',
  4701. 'begin',
  4702. ' p:=TProc(procedure(b: smallint) begin end);',
  4703. ' a:=TFuncArr(function: TArr begin end)();',
  4704. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4705. 'end;',
  4706. 'begin']);
  4707. ConvertProgram;
  4708. CheckSource('TestAnonymousProc_Typecast',
  4709. LinesToStr([ // statements
  4710. 'this.DoIt = function (p) {',
  4711. ' var w = 0;',
  4712. ' var a = [];',
  4713. ' p = function (b) {',
  4714. ' };',
  4715. ' a = function () {',
  4716. ' var Result = [];',
  4717. ' return Result;',
  4718. ' }();',
  4719. ' w = function () {',
  4720. ' var Result = [];',
  4721. ' return Result;',
  4722. ' }()[3];',
  4723. '};',
  4724. '']),
  4725. LinesToStr([
  4726. '']));
  4727. end;
  4728. procedure TTestModule.TestAnonymousProc_With;
  4729. begin
  4730. StartProgram(false);
  4731. Add([
  4732. 'type',
  4733. ' TProc = reference to procedure(w: word);',
  4734. ' TObject = class',
  4735. ' b: boolean;',
  4736. ' end;',
  4737. 'var',
  4738. ' p: TProc;',
  4739. ' bird: TObject;',
  4740. 'begin',
  4741. ' with bird do',
  4742. ' p:=procedure(w: word)',
  4743. ' begin',
  4744. ' b:=w>2;',
  4745. ' end;',
  4746. '']);
  4747. ConvertProgram;
  4748. CheckSource('TestAnonymousProc_With',
  4749. LinesToStr([ // statements
  4750. 'rtl.createClass(this, "TObject", null, function () {',
  4751. ' this.$init = function () {',
  4752. ' this.b = false;',
  4753. ' };',
  4754. ' this.$final = function () {',
  4755. ' };',
  4756. '});',
  4757. 'this.p = null;',
  4758. 'this.bird = null;',
  4759. '']),
  4760. LinesToStr([
  4761. 'var $with = $mod.bird;',
  4762. '$mod.p = function (w) {',
  4763. ' $with.b = w > 2;',
  4764. '};',
  4765. '']));
  4766. end;
  4767. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4768. begin
  4769. StartProgram(false);
  4770. Add([
  4771. 'type',
  4772. ' TProc = reference to procedure;',
  4773. ' TObject = class',
  4774. ' b: boolean;',
  4775. ' end;',
  4776. 'procedure DoIt;',
  4777. 'var',
  4778. ' p: TProc;',
  4779. 'begin',
  4780. ' try',
  4781. ' except',
  4782. ' on E: TObject do',
  4783. ' p:=procedure',
  4784. ' begin',
  4785. ' E.b:=true;',
  4786. ' end;',
  4787. ' end;',
  4788. 'end;',
  4789. 'begin']);
  4790. ConvertProgram;
  4791. CheckSource('TestAnonymousProc_ExceptOn',
  4792. LinesToStr([ // statements
  4793. 'rtl.createClass(this, "TObject", null, function () {',
  4794. ' this.$init = function () {',
  4795. ' this.b = false;',
  4796. ' };',
  4797. ' this.$final = function () {',
  4798. ' };',
  4799. '});',
  4800. 'this.DoIt = function () {',
  4801. ' var p = null;',
  4802. ' try {} catch ($e) {',
  4803. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4804. ' var E = $e;',
  4805. ' p = function () {',
  4806. ' E.b = true;',
  4807. ' };',
  4808. ' } else throw $e',
  4809. ' };',
  4810. '};',
  4811. '']),
  4812. LinesToStr([
  4813. '']));
  4814. end;
  4815. procedure TTestModule.TestAnonymousProc_Nested;
  4816. begin
  4817. StartProgram(false);
  4818. Add([
  4819. 'type',
  4820. ' TProc = reference to procedure;',
  4821. ' TObject = class',
  4822. ' i: byte;',
  4823. ' procedure DoIt;',
  4824. ' end;',
  4825. 'procedure TObject.DoIt;',
  4826. 'var',
  4827. ' p: TProc;',
  4828. ' procedure Sub;',
  4829. ' begin',
  4830. ' p:=procedure',
  4831. ' begin',
  4832. ' i:=3;',
  4833. ' Self.i:=4;',
  4834. ' p:=procedure',
  4835. ' procedure SubSub;',
  4836. ' begin',
  4837. ' i:=13;',
  4838. ' Self.i:=14;',
  4839. ' end;',
  4840. ' begin',
  4841. ' i:=13;',
  4842. ' Self.i:=14;',
  4843. ' end;',
  4844. ' end;',
  4845. ' end;',
  4846. 'begin',
  4847. 'end;',
  4848. 'begin']);
  4849. ConvertProgram;
  4850. CheckSource('TestAnonymousProc_Nested',
  4851. LinesToStr([ // statements
  4852. 'rtl.createClass(this, "TObject", null, function () {',
  4853. ' this.$init = function () {',
  4854. ' this.i = 0;',
  4855. ' };',
  4856. ' this.$final = function () {',
  4857. ' };',
  4858. ' this.DoIt = function () {',
  4859. ' var $Self = this;',
  4860. ' var p = null;',
  4861. ' function Sub() {',
  4862. ' p = function () {',
  4863. ' $Self.i = 3;',
  4864. ' $Self.i = 4;',
  4865. ' p = function () {',
  4866. ' function SubSub() {',
  4867. ' $Self.i = 13;',
  4868. ' $Self.i = 14;',
  4869. ' };',
  4870. ' $Self.i = 13;',
  4871. ' $Self.i = 14;',
  4872. ' };',
  4873. ' };',
  4874. ' };',
  4875. ' };',
  4876. '});',
  4877. '']),
  4878. LinesToStr([
  4879. '']));
  4880. end;
  4881. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4882. begin
  4883. StartProgram(false);
  4884. Add([
  4885. 'type',
  4886. ' TProc = reference to procedure;',
  4887. 'function DoIt: TProc;',
  4888. ' function Sub: TProc;',
  4889. ' begin',
  4890. ' Result:=procedure',
  4891. ' begin',
  4892. ' Sub:=procedure',
  4893. ' procedure SubSub;',
  4894. ' begin',
  4895. ' Result:=nil;',
  4896. ' Sub:=nil;',
  4897. ' DoIt:=nil;',
  4898. ' end;',
  4899. ' begin',
  4900. ' Result:=nil;',
  4901. ' Sub:=nil;',
  4902. ' DoIt:=nil;',
  4903. ' end;',
  4904. ' end;',
  4905. ' end;',
  4906. 'begin',
  4907. 'end;',
  4908. 'begin']);
  4909. ConvertProgram;
  4910. CheckSource('TestAnonymousProc_NestedAssignResult',
  4911. LinesToStr([ // statements
  4912. 'this.DoIt = function () {',
  4913. ' var Result = null;',
  4914. ' function Sub() {',
  4915. ' var Result$1 = null;',
  4916. ' Result$1 = function () {',
  4917. ' Result$1 = function () {',
  4918. ' function SubSub() {',
  4919. ' Result$1 = null;',
  4920. ' Result$1 = null;',
  4921. ' Result = null;',
  4922. ' };',
  4923. ' Result$1 = null;',
  4924. ' Result$1 = null;',
  4925. ' Result = null;',
  4926. ' };',
  4927. ' };',
  4928. ' return Result$1;',
  4929. ' };',
  4930. ' return Result;',
  4931. '};',
  4932. '']),
  4933. LinesToStr([
  4934. '']));
  4935. end;
  4936. procedure TTestModule.TestAnonymousProc_Class;
  4937. begin
  4938. StartProgram(false);
  4939. Add([
  4940. 'type',
  4941. ' TProc = reference to procedure;',
  4942. ' TEvent = procedure of object;',
  4943. ' TObject = class',
  4944. ' Size: word;',
  4945. ' function GetIt: TProc;',
  4946. ' procedure DoIt; virtual; abstract;',
  4947. ' end;',
  4948. 'function TObject.GetIt: TProc;',
  4949. 'begin',
  4950. ' Result:=procedure',
  4951. ' var p: TEvent;',
  4952. ' begin',
  4953. ' Size:=Size;',
  4954. ' Size:=Self.Size;',
  4955. ' p:=@DoIt;',
  4956. ' p:[email protected];',
  4957. ' end;',
  4958. 'end;',
  4959. 'begin']);
  4960. ConvertProgram;
  4961. CheckSource('TestAnonymousProc_Class',
  4962. LinesToStr([ // statements
  4963. 'rtl.createClass(this, "TObject", null, function () {',
  4964. ' this.$init = function () {',
  4965. ' this.Size = 0;',
  4966. ' };',
  4967. ' this.$final = function () {',
  4968. ' };',
  4969. ' this.GetIt = function () {',
  4970. ' var $Self = this;',
  4971. ' var Result = null;',
  4972. ' Result = function () {',
  4973. ' var p = null;',
  4974. ' $Self.Size = $Self.Size;',
  4975. ' $Self.Size = $Self.Size;',
  4976. ' p = rtl.createCallback($Self, "DoIt");',
  4977. ' p = rtl.createCallback($Self, "DoIt");',
  4978. ' };',
  4979. ' return Result;',
  4980. ' };',
  4981. '});',
  4982. '']),
  4983. LinesToStr([
  4984. '']));
  4985. end;
  4986. procedure TTestModule.TestAnonymousProc_ForLoop;
  4987. begin
  4988. StartProgram(false);
  4989. Add([
  4990. 'type TProc = reference to procedure;',
  4991. 'procedure Foo(p: TProc);',
  4992. 'begin',
  4993. 'end;',
  4994. 'procedure DoIt;',
  4995. 'var i: word;',
  4996. ' a: word;',
  4997. 'begin',
  4998. ' for i:=1 to 10 do begin',
  4999. ' Foo(procedure begin a:=3; end);',
  5000. ' end;',
  5001. 'end;',
  5002. 'begin',
  5003. ' DoIt;']);
  5004. ConvertProgram;
  5005. CheckSource('TestAnonymousProc_ForLoop',
  5006. LinesToStr([ // statements
  5007. 'this.Foo = function (p) {',
  5008. '};',
  5009. 'this.DoIt = function () {',
  5010. ' var i = 0;',
  5011. ' var a = 0;',
  5012. ' for (i = 1; i <= 10; i++) {',
  5013. ' $mod.Foo(function () {',
  5014. ' a = 3;',
  5015. ' });',
  5016. ' };',
  5017. '};',
  5018. '']),
  5019. LinesToStr([
  5020. '$mod.DoIt();'
  5021. ]));
  5022. end;
  5023. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5024. begin
  5025. StartProgram(false);
  5026. Add([
  5027. '{$mode delphi}',
  5028. 'type',
  5029. ' TProc = reference to procedure;',
  5030. ' TFunc = reference to function(x: word): word;',
  5031. 'procedure Run;',
  5032. 'asm',
  5033. 'end;',
  5034. 'procedure Walk(p: TProc; f: TFunc);',
  5035. 'begin',
  5036. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5037. 'end;',
  5038. 'begin',
  5039. ' Walk(procedure',
  5040. ' asm',
  5041. ' console.log("a");',
  5042. ' end,',
  5043. ' function(x: word): word asm',
  5044. ' console.log("c");',
  5045. ' end);',
  5046. '']);
  5047. ConvertProgram;
  5048. CheckSource('TestAnonymousProc_AsmDelphi',
  5049. LinesToStr([ // statements
  5050. 'this.Run = function () {',
  5051. '};',
  5052. 'this.Walk = function (p, f) {',
  5053. ' $mod.Walk(function () {',
  5054. ' }, function (b) {',
  5055. ' return 1+b;',
  5056. ' });',
  5057. '};',
  5058. '']),
  5059. LinesToStr([
  5060. '$mod.Walk(function () {',
  5061. ' console.log("a");',
  5062. '}, function (x) {',
  5063. ' console.log("c");',
  5064. '});',
  5065. '']));
  5066. end;
  5067. procedure TTestModule.TestEnum_Name;
  5068. begin
  5069. StartProgram(false);
  5070. Add('type TMyEnum = (Red, Green, Blue);');
  5071. Add('var e: TMyEnum;');
  5072. Add('var f: TMyEnum = Blue;');
  5073. Add('begin');
  5074. Add(' e:=green;');
  5075. Add(' e:=default(TMyEnum);');
  5076. ConvertProgram;
  5077. CheckSource('TestEnum_Name',
  5078. LinesToStr([ // statements
  5079. 'this.TMyEnum = {',
  5080. ' "0":"Red",',
  5081. ' Red:0,',
  5082. ' "1":"Green",',
  5083. ' Green:1,',
  5084. ' "2":"Blue",',
  5085. ' Blue:2',
  5086. ' };',
  5087. 'this.e = 0;',
  5088. 'this.f = this.TMyEnum.Blue;'
  5089. ]),
  5090. LinesToStr([
  5091. '$mod.e=$mod.TMyEnum.Green;',
  5092. '$mod.e=$mod.TMyEnum.Red;'
  5093. ]));
  5094. end;
  5095. procedure TTestModule.TestEnum_Number;
  5096. begin
  5097. Converter.Options:=Converter.Options+[coEnumNumbers];
  5098. StartProgram(false);
  5099. Add('type TMyEnum = (Red, Green);');
  5100. Add('var');
  5101. Add(' e: TMyEnum;');
  5102. Add(' f: TMyEnum = Green;');
  5103. Add(' i: longint;');
  5104. Add('begin');
  5105. Add(' e:=green;');
  5106. Add(' i:=longint(e);');
  5107. ConvertProgram;
  5108. CheckSource('TestEnumNumber',
  5109. LinesToStr([ // statements
  5110. 'this.TMyEnum = {',
  5111. ' "0":"Red",',
  5112. ' Red:0,',
  5113. ' "1":"Green",',
  5114. ' Green:1',
  5115. ' };',
  5116. 'this.e = 0;',
  5117. 'this.f = 1;',
  5118. 'this.i = 0;'
  5119. ]),
  5120. LinesToStr([
  5121. '$mod.e=1;',
  5122. '$mod.i=$mod.e;'
  5123. ]));
  5124. end;
  5125. procedure TTestModule.TestEnum_ConstFail;
  5126. begin
  5127. StartProgram(false);
  5128. Add([
  5129. 'type TMyEnum = (Red = 100, Green = 101);',
  5130. 'var',
  5131. ' e: TMyEnum;',
  5132. ' f: TMyEnum = Green;',
  5133. 'begin',
  5134. ' e:=green;']);
  5135. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5136. ConvertProgram;
  5137. end;
  5138. procedure TTestModule.TestEnum_Functions;
  5139. begin
  5140. StartProgram(false);
  5141. Add([
  5142. 'type TMyEnum = (Red, Green);',
  5143. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5144. 'var',
  5145. ' v: longint;',
  5146. ' s: string;',
  5147. 'begin',
  5148. ' val(s,e,v);',
  5149. ' val(s,e,i);',
  5150. 'end;',
  5151. 'var',
  5152. ' e: TMyEnum;',
  5153. ' i: longint;',
  5154. ' s: string;',
  5155. ' b: boolean;',
  5156. 'begin',
  5157. ' i:=ord(red);',
  5158. ' i:=ord(green);',
  5159. ' i:=ord(e);',
  5160. ' i:=ord(b);',
  5161. ' e:=low(tmyenum);',
  5162. ' e:=low(e);',
  5163. ' b:=low(boolean);',
  5164. ' e:=high(tmyenum);',
  5165. ' e:=high(e);',
  5166. ' b:=high(boolean);',
  5167. ' e:=pred(green);',
  5168. ' e:=pred(e);',
  5169. ' b:=pred(b);',
  5170. ' e:=succ(red);',
  5171. ' e:=succ(e);',
  5172. ' b:=succ(b);',
  5173. ' e:=tmyenum(1);',
  5174. ' e:=tmyenum(i);',
  5175. ' s:=str(e);',
  5176. ' str(e,s);',
  5177. ' str(red,s);',
  5178. ' s:=str(e:3);',
  5179. ' writestr(s,e:3,red);',
  5180. ' val(s,e,i);',
  5181. ' i:=longint(e);']);
  5182. ConvertProgram;
  5183. CheckSource('TestEnum_Functions',
  5184. LinesToStr([ // statements
  5185. 'this.TMyEnum = {',
  5186. ' "0":"Red",',
  5187. ' Red:0,',
  5188. ' "1":"Green",',
  5189. ' Green:1',
  5190. ' };',
  5191. 'this.DoIt = function (e, i) {',
  5192. ' var v = 0;',
  5193. ' var s = "";',
  5194. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5195. ' v = w;',
  5196. ' }));',
  5197. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5198. '};',
  5199. 'this.e = 0;',
  5200. 'this.i = 0;',
  5201. 'this.s = "";',
  5202. 'this.b = false;',
  5203. '']),
  5204. LinesToStr([
  5205. '$mod.i=$mod.TMyEnum.Red;',
  5206. '$mod.i=$mod.TMyEnum.Green;',
  5207. '$mod.i=$mod.e;',
  5208. '$mod.i=$mod.b+0;',
  5209. '$mod.e=$mod.TMyEnum.Red;',
  5210. '$mod.e=$mod.TMyEnum.Red;',
  5211. '$mod.b=false;',
  5212. '$mod.e=$mod.TMyEnum.Green;',
  5213. '$mod.e=$mod.TMyEnum.Green;',
  5214. '$mod.b=true;',
  5215. '$mod.e=$mod.TMyEnum.Green-1;',
  5216. '$mod.e=$mod.e-1;',
  5217. '$mod.b=false;',
  5218. '$mod.e=$mod.TMyEnum.Red+1;',
  5219. '$mod.e=$mod.e+1;',
  5220. '$mod.b=true;',
  5221. '$mod.e=1;',
  5222. '$mod.e=$mod.i;',
  5223. '$mod.s = $mod.TMyEnum[$mod.e];',
  5224. '$mod.s = $mod.TMyEnum[$mod.e];',
  5225. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5226. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5227. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5228. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5229. ' $mod.i = v;',
  5230. '});',
  5231. '$mod.i=$mod.e;',
  5232. '']));
  5233. end;
  5234. procedure TTestModule.TestEnumRg_Functions;
  5235. begin
  5236. StartProgram(false);
  5237. Add([
  5238. 'type',
  5239. ' TEnum = (Red, Green, Blue);',
  5240. ' TEnumRg = Green..Blue;',
  5241. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5242. 'var',
  5243. ' v: longint;',
  5244. ' s: string;',
  5245. 'begin',
  5246. ' val(s,e,v);',
  5247. ' val(s,e,i);',
  5248. 'end;',
  5249. 'var',
  5250. ' e: TEnumRg;',
  5251. ' i: longint;',
  5252. ' s: string;',
  5253. 'begin',
  5254. ' i:=ord(green);',
  5255. ' i:=ord(e);',
  5256. ' e:=low(tenumrg);',
  5257. ' e:=low(e);',
  5258. ' e:=high(tenumrg);',
  5259. ' e:=high(e);',
  5260. ' e:=pred(blue);',
  5261. ' e:=pred(e);',
  5262. ' e:=succ(green);',
  5263. ' e:=succ(e);',
  5264. ' e:=tenumrg(1);',
  5265. ' e:=tenumrg(i);',
  5266. ' s:=str(e);',
  5267. ' str(e,s);',
  5268. ' str(red,s);',
  5269. ' s:=str(e:3);',
  5270. ' writestr(s,e:3,blue);',
  5271. ' val(s,e,i);',
  5272. ' i:=longint(e);']);
  5273. ConvertProgram;
  5274. CheckSource('TestEnumRg_Functions',
  5275. LinesToStr([ // statements
  5276. 'this.TEnum = {',
  5277. ' "0":"Red",',
  5278. ' Red:0,',
  5279. ' "1":"Green",',
  5280. ' Green:1,',
  5281. ' "2":"Blue",',
  5282. ' Blue:2',
  5283. ' };',
  5284. 'this.DoIt = function (e, i) {',
  5285. ' var v = 0;',
  5286. ' var s = "";',
  5287. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5288. ' v = w;',
  5289. ' }));',
  5290. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5291. '};',
  5292. 'this.e = this.TEnum.Green;',
  5293. 'this.i = 0;',
  5294. 'this.s = "";',
  5295. '']),
  5296. LinesToStr([
  5297. '$mod.i=$mod.TEnum.Green;',
  5298. '$mod.i=$mod.e;',
  5299. '$mod.e=$mod.TEnum.Green;',
  5300. '$mod.e=$mod.TEnum.Green;',
  5301. '$mod.e=$mod.TEnum.Blue;',
  5302. '$mod.e=$mod.TEnum.Blue;',
  5303. '$mod.e=$mod.TEnum.Blue-1;',
  5304. '$mod.e=$mod.e-1;',
  5305. '$mod.e=$mod.TEnum.Green+1;',
  5306. '$mod.e=$mod.e+1;',
  5307. '$mod.e=1;',
  5308. '$mod.e=$mod.i;',
  5309. '$mod.s = $mod.TEnum[$mod.e];',
  5310. '$mod.s = $mod.TEnum[$mod.e];',
  5311. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5312. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5313. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5314. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5315. ' $mod.i = v;',
  5316. '});',
  5317. '$mod.i=$mod.e;',
  5318. '']));
  5319. end;
  5320. procedure TTestModule.TestEnum_AsParams;
  5321. begin
  5322. StartProgram(false);
  5323. Add('type TEnum = (Red,Blue);');
  5324. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5325. Add('var vJ: TEnum;');
  5326. Add('begin');
  5327. Add(' vg:=vg;');
  5328. Add(' vj:=vh;');
  5329. Add(' vi:=vi;');
  5330. Add(' doit(vg,vg,vg);');
  5331. Add(' doit(vh,vh,vj);');
  5332. Add(' doit(vi,vi,vi);');
  5333. Add(' doit(vj,vj,vj);');
  5334. Add('end;');
  5335. Add('var i: TEnum;');
  5336. Add('begin');
  5337. Add(' doit(i,i,i);');
  5338. ConvertProgram;
  5339. CheckSource('TestEnum_AsParams',
  5340. LinesToStr([ // statements
  5341. 'this.TEnum = {',
  5342. ' "0": "Red",',
  5343. ' Red: 0,',
  5344. ' "1": "Blue",',
  5345. ' Blue: 1',
  5346. '};',
  5347. 'this.DoIt = function (vG,vH,vI) {',
  5348. ' var vJ = 0;',
  5349. ' vG = vG;',
  5350. ' vJ = vH;',
  5351. ' vI.set(vI.get());',
  5352. ' $mod.DoIt(vG, vG, {',
  5353. ' get: function () {',
  5354. ' return vG;',
  5355. ' },',
  5356. ' set: function (v) {',
  5357. ' vG = v;',
  5358. ' }',
  5359. ' });',
  5360. ' $mod.DoIt(vH, vH, {',
  5361. ' get: function () {',
  5362. ' return vJ;',
  5363. ' },',
  5364. ' set: function (v) {',
  5365. ' vJ = v;',
  5366. ' }',
  5367. ' });',
  5368. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5369. ' $mod.DoIt(vJ, vJ, {',
  5370. ' get: function () {',
  5371. ' return vJ;',
  5372. ' },',
  5373. ' set: function (v) {',
  5374. ' vJ = v;',
  5375. ' }',
  5376. ' });',
  5377. '};',
  5378. 'this.i = 0;'
  5379. ]),
  5380. LinesToStr([
  5381. '$mod.DoIt($mod.i,$mod.i,{',
  5382. ' p: $mod,',
  5383. ' get: function () {',
  5384. ' return this.p.i;',
  5385. ' },',
  5386. ' set: function (v) {',
  5387. ' this.p.i = v;',
  5388. ' }',
  5389. '});'
  5390. ]));
  5391. end;
  5392. procedure TTestModule.TestEnumRange_Array;
  5393. begin
  5394. StartProgram(false);
  5395. Add([
  5396. 'type',
  5397. ' TEnum = (Red, Green, Blue);',
  5398. ' TEnumRg = green..blue;',
  5399. ' TArr = array[TEnumRg] of byte;',
  5400. ' TArr2 = array[green..blue] of byte;',
  5401. 'var',
  5402. ' a: TArr;',
  5403. ' b: TArr = (3,4);',
  5404. ' c: TArr2 = (5,6);',
  5405. 'begin',
  5406. ' a[green] := b[blue];',
  5407. ' c[green] := c[blue];',
  5408. '']);
  5409. ConvertProgram;
  5410. CheckSource('TestEnumRange_Array',
  5411. LinesToStr([ // statements
  5412. 'this.TEnum = {',
  5413. ' "0": "Red",',
  5414. ' Red: 0,',
  5415. ' "1": "Green",',
  5416. ' Green: 1,',
  5417. ' "2": "Blue",',
  5418. ' Blue: 2',
  5419. '};',
  5420. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5421. 'this.b = [3, 4];',
  5422. 'this.c = [5, 6];',
  5423. '']),
  5424. LinesToStr([
  5425. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5426. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5427. '']));
  5428. end;
  5429. procedure TTestModule.TestEnum_ForIn;
  5430. begin
  5431. StartProgram(false);
  5432. Add([
  5433. 'type',
  5434. ' TEnum = (Red, Green, Blue);',
  5435. ' TEnumRg = green..blue;',
  5436. ' TArr = array[TEnum] of byte;',
  5437. ' TArrRg = array[TEnumRg] of byte;',
  5438. 'var',
  5439. ' e: TEnum;',
  5440. ' a1: TArr = (3,4,5);',
  5441. ' a2: TArrRg = (11,12);',
  5442. ' b: byte;',
  5443. 'begin',
  5444. ' for e in TEnum do ;',
  5445. ' for e in TEnumRg do ;',
  5446. ' for e in TArr do ;',
  5447. ' for e in TArrRg do ;',
  5448. ' for b in a1 do ;',
  5449. ' for b in a2 do ;',
  5450. '']);
  5451. ConvertProgram;
  5452. CheckSource('TestEnum_ForIn',
  5453. LinesToStr([ // statements
  5454. 'this.TEnum = {',
  5455. ' "0": "Red",',
  5456. ' Red: 0,',
  5457. ' "1": "Green",',
  5458. ' Green: 1,',
  5459. ' "2": "Blue",',
  5460. ' Blue: 2',
  5461. '};',
  5462. 'this.e = 0;',
  5463. 'this.a1 = [3, 4, 5];',
  5464. 'this.a2 = [11, 12];',
  5465. 'this.b = 0;',
  5466. '']),
  5467. LinesToStr([
  5468. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5469. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5470. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5471. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5472. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5473. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5474. '']));
  5475. end;
  5476. procedure TTestModule.TestEnum_ScopedNumber;
  5477. begin
  5478. Converter.Options:=Converter.Options+[coEnumNumbers];
  5479. StartProgram(false);
  5480. Add([
  5481. 'type',
  5482. ' TEnum = (Red, Green);',
  5483. 'var',
  5484. ' e: TEnum;',
  5485. 'begin',
  5486. ' e:=TEnum.Green;',
  5487. '']);
  5488. ConvertProgram;
  5489. CheckSource('TestEnum_ScopedNumber',
  5490. LinesToStr([ // statements
  5491. 'this.TEnum = {',
  5492. ' "0": "Red",',
  5493. ' Red: 0,',
  5494. ' "1": "Green",',
  5495. ' Green: 1',
  5496. '};',
  5497. 'this.e = 0;',
  5498. '']),
  5499. LinesToStr([
  5500. '$mod.e = 1;']));
  5501. end;
  5502. procedure TTestModule.TestEnum_InFunction;
  5503. begin
  5504. StartProgram(false);
  5505. Add([
  5506. 'const TEnum = 3;',
  5507. 'procedure DoIt;',
  5508. 'type',
  5509. ' TEnum = (Red, Green, Blue);',
  5510. ' procedure Sub;',
  5511. ' type',
  5512. ' TEnumSub = (Left, Right);',
  5513. ' var',
  5514. ' es: TEnumSub;',
  5515. ' begin',
  5516. ' es:=Left;',
  5517. ' end;',
  5518. 'var',
  5519. ' e, e2: TEnum;',
  5520. 'begin',
  5521. ' if e in [red,blue] then e2:=e;',
  5522. 'end;',
  5523. 'begin']);
  5524. ConvertProgram;
  5525. CheckSource('TestEnum_InFunction',
  5526. LinesToStr([ // statements
  5527. 'this.TEnum = 3;',
  5528. 'var TEnum$1 = {',
  5529. ' "0":"Red",',
  5530. ' Red:0,',
  5531. ' "1":"Green",',
  5532. ' Green:1,',
  5533. ' "2":"Blue",',
  5534. ' Blue:2',
  5535. ' };',
  5536. 'var TEnumSub = {',
  5537. ' "0": "Left",',
  5538. ' Left: 0,',
  5539. ' "1": "Right",',
  5540. ' Right: 1',
  5541. '};',
  5542. 'this.DoIt = function () {',
  5543. ' function Sub() {',
  5544. ' var es = 0;',
  5545. ' es = TEnumSub.Left;',
  5546. ' };',
  5547. ' var e = 0;',
  5548. ' var e2 = 0;',
  5549. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5550. '};',
  5551. '']),
  5552. LinesToStr([
  5553. '']));
  5554. end;
  5555. procedure TTestModule.TestSet_Enum;
  5556. begin
  5557. StartProgram(false);
  5558. Add([
  5559. 'type',
  5560. ' TColor = (Red, Green, Blue);',
  5561. ' TColors = set of TColor;',
  5562. 'var',
  5563. ' c: TColor;',
  5564. ' s: TColors;',
  5565. ' t: TColors = [];',
  5566. ' u: TColors = [Red];',
  5567. 'begin',
  5568. ' s:=[];',
  5569. ' s:=[Green];',
  5570. ' s:=[Green,Blue];',
  5571. ' s:=[Red..Blue];',
  5572. ' s:=[Red,Green..Blue];',
  5573. ' s:=[Red,c];',
  5574. ' s:=t;',
  5575. ' s:=default(TColors);',
  5576. '']);
  5577. ConvertProgram;
  5578. CheckSource('TestSet',
  5579. LinesToStr([ // statements
  5580. 'this.TColor = {',
  5581. ' "0":"Red",',
  5582. ' Red:0,',
  5583. ' "1":"Green",',
  5584. ' Green:1,',
  5585. ' "2":"Blue",',
  5586. ' Blue:2',
  5587. ' };',
  5588. 'this.c = 0;',
  5589. 'this.s = {};',
  5590. 'this.t = {};',
  5591. 'this.u = rtl.createSet(this.TColor.Red);'
  5592. ]),
  5593. LinesToStr([
  5594. '$mod.s={};',
  5595. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5596. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5597. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5598. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5599. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5600. '$mod.s=rtl.refSet($mod.t);',
  5601. '$mod.s={};',
  5602. '']));
  5603. end;
  5604. procedure TTestModule.TestSet_Operators;
  5605. begin
  5606. StartProgram(false);
  5607. Add('type');
  5608. Add(' TColor = (Red, Green, Blue);');
  5609. Add(' TColors = set of tcolor;');
  5610. Add('var');
  5611. Add(' vC: TColor;');
  5612. Add(' vS: TColors;');
  5613. Add(' vT: TColors;');
  5614. Add(' vU: TColors;');
  5615. Add(' B: boolean;');
  5616. Add('begin');
  5617. Add(' include(vs,green);');
  5618. Add(' exclude(vs,vc);');
  5619. Add(' vs:=vt+vu;');
  5620. Add(' vs:=vt+[red];');
  5621. Add(' vs:=[red]+vt;');
  5622. Add(' vs:=[red]+[green];');
  5623. Add(' vs:=vt-vu;');
  5624. Add(' vs:=vt-[red];');
  5625. Add(' vs:=[red]-vt;');
  5626. Add(' vs:=[red]-[green];');
  5627. Add(' vs:=vt*vu;');
  5628. Add(' vs:=vt*[red];');
  5629. Add(' vs:=[red]*vt;');
  5630. Add(' vs:=[red]*[green];');
  5631. Add(' vs:=vt><vu;');
  5632. Add(' vs:=vt><[red];');
  5633. Add(' vs:=[red]><vt;');
  5634. Add(' vs:=[red]><[green];');
  5635. Add(' b:=vt=vu;');
  5636. Add(' b:=vt=[red];');
  5637. Add(' b:=[red]=vt;');
  5638. Add(' b:=[red]=[green];');
  5639. Add(' b:=vt<>vu;');
  5640. Add(' b:=vt<>[red];');
  5641. Add(' b:=[red]<>vt;');
  5642. Add(' b:=[red]<>[green];');
  5643. Add(' b:=vt<=vu;');
  5644. Add(' b:=vt<=[red];');
  5645. Add(' b:=[red]<=vt;');
  5646. Add(' b:=[red]<=[green];');
  5647. Add(' b:=vt>=vu;');
  5648. Add(' b:=vt>=[red];');
  5649. Add(' b:=[red]>=vt;');
  5650. Add(' b:=[red]>=[green];');
  5651. ConvertProgram;
  5652. CheckSource('TestSet_Operators',
  5653. LinesToStr([ // statements
  5654. 'this.TColor = {',
  5655. ' "0":"Red",',
  5656. ' Red:0,',
  5657. ' "1":"Green",',
  5658. ' Green:1,',
  5659. ' "2":"Blue",',
  5660. ' Blue:2',
  5661. ' };',
  5662. 'this.vC = 0;',
  5663. 'this.vS = {};',
  5664. 'this.vT = {};',
  5665. 'this.vU = {};',
  5666. 'this.B = false;'
  5667. ]),
  5668. LinesToStr([
  5669. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5670. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5671. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5672. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5673. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5674. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5675. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5676. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5677. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5678. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5679. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5680. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5681. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5682. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5683. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5684. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5685. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5686. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5687. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5688. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5689. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5690. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5691. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5692. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5693. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5694. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5695. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5696. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5697. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5698. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5699. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5700. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5701. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5702. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5703. '']));
  5704. end;
  5705. procedure TTestModule.TestSet_Operator_In;
  5706. begin
  5707. StartProgram(false);
  5708. Add([
  5709. 'type',
  5710. ' TColor = (Red, Green, Blue);',
  5711. ' TColors = set of tcolor;',
  5712. ' TColorRg = green..blue;',
  5713. 'var',
  5714. ' vC: tcolor;',
  5715. ' vT: tcolors;',
  5716. ' B: boolean;',
  5717. ' rg: TColorRg;',
  5718. 'begin',
  5719. ' b:=red in vt;',
  5720. ' b:=vc in vt;',
  5721. ' b:=green in [red..blue];',
  5722. ' b:=vc in [red..blue];',
  5723. ' ',
  5724. ' if red in vt then ;',
  5725. ' while vC in vt do ;',
  5726. ' repeat',
  5727. ' until vC in vt;',
  5728. ' if rg in [green..blue] then ;',
  5729. '']);
  5730. ConvertProgram;
  5731. CheckSource('TestSet_Operator_In',
  5732. LinesToStr([ // statements
  5733. 'this.TColor = {',
  5734. ' "0":"Red",',
  5735. ' Red:0,',
  5736. ' "1":"Green",',
  5737. ' Green:1,',
  5738. ' "2":"Blue",',
  5739. ' Blue:2',
  5740. ' };',
  5741. 'this.vC = 0;',
  5742. 'this.vT = {};',
  5743. 'this.B = false;',
  5744. 'this.rg = this.TColor.Green;',
  5745. '']),
  5746. LinesToStr([
  5747. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5748. '$mod.B = $mod.vC in $mod.vT;',
  5749. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5750. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5751. 'if ($mod.TColor.Red in $mod.vT) ;',
  5752. 'while ($mod.vC in $mod.vT) {',
  5753. '};',
  5754. 'do {',
  5755. '} while (!($mod.vC in $mod.vT));',
  5756. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5757. '']));
  5758. end;
  5759. procedure TTestModule.TestSet_Functions;
  5760. begin
  5761. StartProgram(false);
  5762. Add('type');
  5763. Add(' TMyEnum = (Red, Green);');
  5764. Add(' TMyEnums = set of TMyEnum;');
  5765. Add('var');
  5766. Add(' e: TMyEnum;');
  5767. Add(' s: TMyEnums;');
  5768. Add('begin');
  5769. Add(' e:=Low(TMyEnums);');
  5770. Add(' e:=Low(s);');
  5771. Add(' e:=High(TMyEnums);');
  5772. Add(' e:=High(s);');
  5773. ConvertProgram;
  5774. CheckSource('TestSetFunctions',
  5775. LinesToStr([ // statements
  5776. 'this.TMyEnum = {',
  5777. ' "0":"Red",',
  5778. ' Red:0,',
  5779. ' "1":"Green",',
  5780. ' Green:1',
  5781. ' };',
  5782. 'this.e = 0;',
  5783. 'this.s = {};'
  5784. ]),
  5785. LinesToStr([
  5786. '$mod.e=$mod.TMyEnum.Red;',
  5787. '$mod.e=$mod.TMyEnum.Red;',
  5788. '$mod.e=$mod.TMyEnum.Green;',
  5789. '$mod.e=$mod.TMyEnum.Green;',
  5790. '']));
  5791. end;
  5792. procedure TTestModule.TestSet_PassAsArgClone;
  5793. begin
  5794. StartProgram(false);
  5795. Add('type');
  5796. Add(' TMyEnum = (Red, Green);');
  5797. Add(' TMyEnums = set of TMyEnum;');
  5798. Add('procedure DoDefault(s: tmyenums); begin end;');
  5799. Add('procedure DoConst(const s: tmyenums); begin end;');
  5800. Add('var');
  5801. Add(' aSet: tmyenums;');
  5802. Add('begin');
  5803. Add(' dodefault(aset);');
  5804. Add(' doconst(aset);');
  5805. ConvertProgram;
  5806. CheckSource('TestSetFunctions',
  5807. LinesToStr([ // statements
  5808. 'this.TMyEnum = {',
  5809. ' "0":"Red",',
  5810. ' Red:0,',
  5811. ' "1":"Green",',
  5812. ' Green:1',
  5813. ' };',
  5814. 'this.DoDefault = function (s) {',
  5815. '};',
  5816. 'this.DoConst = function (s) {',
  5817. '};',
  5818. 'this.aSet = {};'
  5819. ]),
  5820. LinesToStr([
  5821. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5822. '$mod.DoConst($mod.aSet);',
  5823. '']));
  5824. end;
  5825. procedure TTestModule.TestSet_AsParams;
  5826. begin
  5827. StartProgram(false);
  5828. Add([
  5829. 'type TEnum = (Red,Blue);',
  5830. 'type TEnums = set of TEnum;',
  5831. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5832. 'var vJ: TEnums;',
  5833. 'begin',
  5834. ' Include(vg,red);',
  5835. ' Include(result,blue);',
  5836. ' vg:=vg;',
  5837. ' vj:=vh;',
  5838. ' vi:=vi;',
  5839. ' doit(vg,vg,vg);',
  5840. ' doit(vh,vh,vj);',
  5841. ' doit(vi,vi,vi);',
  5842. ' doit(vj,vj,vj);',
  5843. 'end;',
  5844. 'var i: TEnums;',
  5845. 'begin',
  5846. ' doit(i,i,i);']);
  5847. ConvertProgram;
  5848. CheckSource('TestSet_AsParams',
  5849. LinesToStr([ // statements
  5850. 'this.TEnum = {',
  5851. ' "0": "Red",',
  5852. ' Red: 0,',
  5853. ' "1": "Blue",',
  5854. ' Blue: 1',
  5855. '};',
  5856. 'this.DoIt = function (vG,vH,vI) {',
  5857. ' var Result = {};',
  5858. ' var vJ = {};',
  5859. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5860. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5861. ' vG = rtl.refSet(vG);',
  5862. ' vJ = rtl.refSet(vH);',
  5863. ' vI.set(rtl.refSet(vI.get()));',
  5864. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5865. ' get: function () {',
  5866. ' return vG;',
  5867. ' },',
  5868. ' set: function (v) {',
  5869. ' vG = v;',
  5870. ' }',
  5871. ' });',
  5872. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5873. ' get: function () {',
  5874. ' return vJ;',
  5875. ' },',
  5876. ' set: function (v) {',
  5877. ' vJ = v;',
  5878. ' }',
  5879. ' });',
  5880. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5881. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5882. ' get: function () {',
  5883. ' return vJ;',
  5884. ' },',
  5885. ' set: function (v) {',
  5886. ' vJ = v;',
  5887. ' }',
  5888. ' });',
  5889. ' return Result;',
  5890. '};',
  5891. 'this.i = {};'
  5892. ]),
  5893. LinesToStr([
  5894. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5895. ' p: $mod,',
  5896. ' get: function () {',
  5897. ' return this.p.i;',
  5898. ' },',
  5899. ' set: function (v) {',
  5900. ' this.p.i = v;',
  5901. ' }',
  5902. '});'
  5903. ]));
  5904. end;
  5905. procedure TTestModule.TestSet_Property;
  5906. begin
  5907. StartProgram(false);
  5908. Add('type');
  5909. Add(' TEnum = (Red,Blue);');
  5910. Add(' TEnums = set of TEnum;');
  5911. Add(' TObject = class');
  5912. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5913. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5914. Add(' property Colors: TEnums read GetColors write SetColors;');
  5915. Add(' end;');
  5916. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5917. Add('begin end;');
  5918. Add('var Obj: TObject;');
  5919. Add('begin');
  5920. Add(' Include(Obj.Colors,Red);');
  5921. Add(' Exclude(Obj.Colors,Red);');
  5922. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5923. ConvertProgram;
  5924. CheckSource('TestSet_Property',
  5925. LinesToStr([ // statements
  5926. 'this.TEnum = {',
  5927. ' "0": "Red",',
  5928. ' Red: 0,',
  5929. ' "1": "Blue",',
  5930. ' Blue: 1',
  5931. '};',
  5932. 'rtl.createClass(this, "TObject", null, function () {',
  5933. ' this.$init = function () {',
  5934. ' };',
  5935. ' this.$final = function () {',
  5936. ' };',
  5937. '});',
  5938. 'this.DoIt = function (i, j, k, l) {',
  5939. '};',
  5940. 'this.Obj = null;',
  5941. '']),
  5942. LinesToStr([
  5943. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5944. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5945. '']));
  5946. end;
  5947. procedure TTestModule.TestSet_EnumConst;
  5948. begin
  5949. StartProgram(false);
  5950. Add([
  5951. 'type',
  5952. ' TEnum = (Red,Blue);',
  5953. ' TEnums = set of TEnum;',
  5954. 'const',
  5955. ' Orange = red;',
  5956. 'var',
  5957. ' Enum: tenum;',
  5958. ' Enums: tenums;',
  5959. 'begin',
  5960. ' Include(enums,orange);',
  5961. ' Exclude(enums,orange);',
  5962. ' if orange in enums then;',
  5963. ' if orange in [orange,red] then;']);
  5964. ConvertProgram;
  5965. CheckSource('TestSet_EnumConst',
  5966. LinesToStr([ // statements
  5967. 'this.TEnum = {',
  5968. ' "0": "Red",',
  5969. ' Red: 0,',
  5970. ' "1": "Blue",',
  5971. ' Blue: 1',
  5972. '};',
  5973. 'this.Orange = this.TEnum.Red;',
  5974. 'this.Enum = 0;',
  5975. 'this.Enums = {};',
  5976. '']),
  5977. LinesToStr([
  5978. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5979. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5980. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5981. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5982. '']));
  5983. end;
  5984. procedure TTestModule.TestSet_IntConst;
  5985. begin
  5986. StartProgram(false);
  5987. Add([
  5988. 'type',
  5989. ' TEnums = set of Byte;',
  5990. 'const',
  5991. ' Orange = 0;',
  5992. 'var',
  5993. ' Enum: byte;',
  5994. ' Enums: tenums;',
  5995. 'begin',
  5996. ' Enums:=[];',
  5997. ' Enums:=[0];',
  5998. ' Enums:=[1..2];',
  5999. //' Include(enums,orange);',
  6000. //' Exclude(enums,orange);',
  6001. ' if orange in enums then;',
  6002. ' if orange in [orange,1] then;']);
  6003. ConvertProgram;
  6004. CheckSource('TestSet_IntConst',
  6005. LinesToStr([ // statements
  6006. 'this.Orange = 0;',
  6007. 'this.Enum = 0;',
  6008. 'this.Enums = {};',
  6009. '']),
  6010. LinesToStr([
  6011. '$mod.Enums = {};',
  6012. '$mod.Enums = rtl.createSet(0);',
  6013. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6014. 'if (0 in $mod.Enums) ;',
  6015. 'if (0 in rtl.createSet(0, 1)) ;',
  6016. '']));
  6017. end;
  6018. procedure TTestModule.TestSet_AnonymousEnumType;
  6019. begin
  6020. StartProgram(false);
  6021. Add('type');
  6022. Add(' TFlags = set of (red, green);');
  6023. Add('const');
  6024. Add(' favorite = red;');
  6025. Add('var');
  6026. Add(' f: TFlags;');
  6027. Add(' i: longint;');
  6028. Add('begin');
  6029. Add(' Include(f,red);');
  6030. Add(' Include(f,favorite);');
  6031. Add(' i:=ord(red);');
  6032. Add(' i:=ord(favorite);');
  6033. Add(' i:=ord(low(TFlags));');
  6034. Add(' i:=ord(low(f));');
  6035. Add(' i:=ord(low(favorite));');
  6036. Add(' i:=ord(high(TFlags));');
  6037. Add(' i:=ord(high(f));');
  6038. Add(' i:=ord(high(favorite));');
  6039. Add(' f:=[green,favorite];');
  6040. ConvertProgram;
  6041. CheckSource('TestSet_AnonymousEnumType',
  6042. LinesToStr([ // statements
  6043. 'this.TFlags$a = {',
  6044. ' "0": "red",',
  6045. ' red: 0,',
  6046. ' "1": "green",',
  6047. ' green: 1',
  6048. '};',
  6049. 'this.favorite = this.TFlags$a.red;',
  6050. 'this.f = {};',
  6051. 'this.i = 0;',
  6052. '']),
  6053. LinesToStr([
  6054. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6055. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6056. '$mod.i = $mod.TFlags$a.red;',
  6057. '$mod.i = $mod.TFlags$a.red;',
  6058. '$mod.i = $mod.TFlags$a.red;',
  6059. '$mod.i = $mod.TFlags$a.red;',
  6060. '$mod.i = $mod.TFlags$a.red;',
  6061. '$mod.i = $mod.TFlags$a.green;',
  6062. '$mod.i = $mod.TFlags$a.green;',
  6063. '$mod.i = $mod.TFlags$a.green;',
  6064. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6065. '']));
  6066. end;
  6067. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6068. begin
  6069. exit;
  6070. StartProgram(false);
  6071. Add([
  6072. 'type',
  6073. ' TAtoZ = ''A''..''Z'';',
  6074. ' TSetOfAZ = set of TAtoZ;',
  6075. 'var',
  6076. ' c: char;',
  6077. ' a: TAtoZ;',
  6078. ' s: TSetOfAZ = [''P'',''A''];',
  6079. ' i: longint;',
  6080. 'begin',
  6081. ' Include(s,''S'');',
  6082. ' Include(s,c);',
  6083. ' Include(s,a);',
  6084. ' c:=low(TAtoZ);',
  6085. ' i:=ord(low(TAtoZ));',
  6086. ' a:=high(TAtoZ);',
  6087. ' a:=high(TSetOfAtoZ);',
  6088. ' s:=[a,c,''M''];',
  6089. '']);
  6090. ConvertProgram;
  6091. CheckSource('TestSet_AnonymousEnumTypeChar',
  6092. LinesToStr([ // statements
  6093. '']),
  6094. LinesToStr([
  6095. '']));
  6096. end;
  6097. procedure TTestModule.TestSet_ConstEnum;
  6098. begin
  6099. StartProgram(false);
  6100. Add([
  6101. 'type',
  6102. ' TEnum = (red,blue,green);',
  6103. ' TEnums = set of TEnum;',
  6104. 'const',
  6105. ' teAny = [low(TEnum)..high(TEnum)];',
  6106. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6107. 'var',
  6108. ' e: TEnum;',
  6109. ' s: TEnums;',
  6110. 'begin',
  6111. ' if blue in teAny then;',
  6112. ' if blue in teAny+[e] then;',
  6113. ' if blue in teAny+teRedBlue then;',
  6114. ' if e in [red,blue] then;',
  6115. ' s:=teAny;',
  6116. ' s:=teAny+[e];',
  6117. ' s:=[e]+teAny;',
  6118. ' s:=teAny+teRedBlue;',
  6119. ' s:=teAny+teRedBlue+[e];',
  6120. '']);
  6121. ConvertProgram;
  6122. CheckSource('TestSet_ConstEnum',
  6123. LinesToStr([ // statements
  6124. 'this.TEnum = {',
  6125. ' "0": "red",',
  6126. ' red: 0,',
  6127. ' "1": "blue",',
  6128. ' blue: 1,',
  6129. ' "2": "green",',
  6130. ' green: 2',
  6131. '};',
  6132. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6133. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6134. 'this.e = 0;',
  6135. 'this.s = {};',
  6136. '']),
  6137. LinesToStr([
  6138. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6139. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6140. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6141. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6142. '$mod.s = rtl.refSet($mod.teAny);',
  6143. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6144. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6145. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6146. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6147. '']));
  6148. end;
  6149. procedure TTestModule.TestSet_ConstChar;
  6150. begin
  6151. StartProgram(false);
  6152. Add([
  6153. 'const',
  6154. ' LowChars = [''a''..''z''];',
  6155. ' Chars = LowChars+[''A''..''Z''];',
  6156. ' sc = [''А'', ''Я''];',
  6157. 'var',
  6158. ' c: char;',
  6159. ' s: string;',
  6160. 'begin',
  6161. ' if c in lowchars then ;',
  6162. ' if ''a'' in lowchars then ;',
  6163. ' if s[1] in lowchars then ;',
  6164. ' if c in chars then ;',
  6165. ' if c in [''a''..''z'',''_''] then ;',
  6166. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6167. ' if ''Я'' in sc then ;',
  6168. ' if 3=ord('' '') then ;',
  6169. '']);
  6170. ConvertProgram;
  6171. CheckSource('TestSet_ConstChar',
  6172. LinesToStr([ // statements
  6173. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6174. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6175. 'this.sc = rtl.createSet(1040, 1071);',
  6176. 'this.c = "";',
  6177. 'this.s = "";',
  6178. '']),
  6179. LinesToStr([
  6180. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6181. 'if (97 in $mod.LowChars) ;',
  6182. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6183. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6184. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6185. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6186. 'if (1071 in $mod.sc) ;',
  6187. 'if (3 === 32) ;',
  6188. '']));
  6189. end;
  6190. procedure TTestModule.TestSet_ConstInt;
  6191. begin
  6192. StartProgram(false);
  6193. Add([
  6194. 'const',
  6195. ' Months = [1..12];',
  6196. ' Mirror = [-12..-1]+Months;',
  6197. 'var',
  6198. ' i: smallint;',
  6199. 'begin',
  6200. ' if 3 in Months then;',
  6201. ' if i in Months+[i] then;',
  6202. ' if i in Months+Mirror then;',
  6203. ' if i in [4..6,8] then;',
  6204. '']);
  6205. ConvertProgram;
  6206. CheckSource('TestSet_ConstInt',
  6207. LinesToStr([ // statements
  6208. 'this.Months = rtl.createSet(null, 1, 12);',
  6209. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6210. 'this.i = 0;',
  6211. '']),
  6212. LinesToStr([
  6213. 'if (3 in $mod.Months) ;',
  6214. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6215. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6216. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6217. '']));
  6218. end;
  6219. procedure TTestModule.TestSet_InFunction;
  6220. begin
  6221. StartProgram(false);
  6222. Add([
  6223. 'const',
  6224. ' TEnum = 3;',
  6225. ' TSetOfEnum = 4;',
  6226. ' TSetOfAno = 5;',
  6227. 'procedure DoIt;',
  6228. 'type',
  6229. ' TEnum = (red, blue);',
  6230. ' TSetOfEnum = set of TEnum;',
  6231. ' TSetOfAno = set of (up,down);',
  6232. 'var',
  6233. ' e: TEnum;',
  6234. ' se: TSetOfEnum;',
  6235. ' sa: TSetOfAno;',
  6236. 'begin',
  6237. ' se:=[e];',
  6238. ' sa:=[up];',
  6239. 'end;',
  6240. 'begin',
  6241. '']);
  6242. ConvertProgram;
  6243. CheckSource('TestSet_InFunction',
  6244. LinesToStr([ // statements
  6245. 'this.TEnum = 3;',
  6246. 'this.TSetOfEnum = 4;',
  6247. 'this.TSetOfAno = 5;',
  6248. 'var TEnum$1 = {',
  6249. ' "0": "red",',
  6250. ' red: 0,',
  6251. ' "1": "blue",',
  6252. ' blue: 1',
  6253. '};',
  6254. 'var TSetOfAno$a = {',
  6255. ' "0": "up",',
  6256. ' up: 0,',
  6257. ' "1": "down",',
  6258. ' down: 1',
  6259. '};',
  6260. 'this.DoIt = function () {',
  6261. ' var e = 0;',
  6262. ' var se = {};',
  6263. ' var sa = {};',
  6264. ' se = rtl.createSet(e);',
  6265. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6266. '};',
  6267. '']),
  6268. LinesToStr([
  6269. '']));
  6270. end;
  6271. procedure TTestModule.TestSet_ForIn;
  6272. begin
  6273. StartProgram(false);
  6274. Add([
  6275. 'type',
  6276. ' TEnum = (Red, Green, Blue);',
  6277. ' TEnumRg = green..blue;',
  6278. ' TSetOfEnum = set of TEnum;',
  6279. ' TSetOfEnumRg = set of TEnumRg;',
  6280. 'var',
  6281. ' e, e2: TEnum;',
  6282. ' er: TEnum;',
  6283. ' s: TSetOfEnum;',
  6284. 'begin',
  6285. ' for e in TSetOfEnum do ;',
  6286. ' for e in TSetOfEnumRg do ;',
  6287. ' for e in [] do e2:=e;',
  6288. ' for e in [red..green] do e2:=e;',
  6289. ' for e in [green,blue] do e2:=e;',
  6290. ' for e in [red,blue] do e2:=e;',
  6291. ' for e in s do e2:=e;',
  6292. ' for er in TSetOfEnumRg do ;',
  6293. '']);
  6294. ConvertProgram;
  6295. CheckSource('TestSet_ForIn',
  6296. LinesToStr([ // statements
  6297. 'this.TEnum = {',
  6298. ' "0":"Red",',
  6299. ' Red:0,',
  6300. ' "1":"Green",',
  6301. ' Green:1,',
  6302. ' "2":"Blue",',
  6303. ' Blue:2',
  6304. ' };',
  6305. 'this.e = 0;',
  6306. 'this.e2 = 0;',
  6307. 'this.er = 0;',
  6308. 'this.s = {};',
  6309. '']),
  6310. LinesToStr([
  6311. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6312. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6313. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6314. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6315. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6316. 'for (var $l in $mod.s){',
  6317. ' $mod.e = +$l;',
  6318. ' $mod.e2 = $mod.e;',
  6319. '};',
  6320. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6321. '']));
  6322. end;
  6323. procedure TTestModule.TestNestBegin;
  6324. begin
  6325. StartProgram(false);
  6326. Add('begin');
  6327. Add(' begin');
  6328. Add(' begin');
  6329. Add(' end;');
  6330. Add(' begin');
  6331. Add(' if true then ;');
  6332. Add(' end;');
  6333. Add(' end;');
  6334. ConvertProgram;
  6335. CheckSource('TestNestBegin',
  6336. '',
  6337. 'if (true) ;');
  6338. end;
  6339. procedure TTestModule.TestUnitImplVars;
  6340. begin
  6341. StartUnit(false);
  6342. Add('interface');
  6343. Add('implementation');
  6344. Add('var');
  6345. Add(' V1:longint;');
  6346. Add(' V2:longint = 3;');
  6347. Add(' V3:string = ''abc'';');
  6348. ConvertUnit;
  6349. CheckSource('TestUnitImplVars',
  6350. LinesToStr([ // statements
  6351. 'var $impl = $mod.$impl;',
  6352. '']),
  6353. '', // this.$init
  6354. LinesToStr([ // implementation
  6355. '$impl.V1 = 0;',
  6356. '$impl.V2 = 3;',
  6357. '$impl.V3 = "abc";',
  6358. '']) );
  6359. end;
  6360. procedure TTestModule.TestUnitImplConsts;
  6361. begin
  6362. StartUnit(false);
  6363. Add('interface');
  6364. Add('implementation');
  6365. Add('const');
  6366. Add(' v1 = 3;');
  6367. Add(' v2:longint = 4;');
  6368. Add(' v3:string = ''abc'';');
  6369. ConvertUnit;
  6370. CheckSource('TestUnitImplConsts',
  6371. LinesToStr([ // statements
  6372. 'var $impl = $mod.$impl;',
  6373. '']),
  6374. '', // this.$init
  6375. LinesToStr([ // implementation
  6376. '$impl.v1 = 3;',
  6377. '$impl.v2 = 4;',
  6378. '$impl.v3 = "abc";',
  6379. '']) );
  6380. end;
  6381. procedure TTestModule.TestUnitImplRecord;
  6382. begin
  6383. StartUnit(false);
  6384. Add('interface');
  6385. Add('implementation');
  6386. Add('type');
  6387. Add(' TMyRecord = record');
  6388. Add(' i: longint;');
  6389. Add(' end;');
  6390. Add('var aRec: TMyRecord;');
  6391. Add('initialization');
  6392. Add(' arec.i:=3;');
  6393. ConvertUnit;
  6394. CheckSource('TestUnitImplRecord',
  6395. LinesToStr([ // statements
  6396. 'var $impl = $mod.$impl;',
  6397. '']),
  6398. // this.$init
  6399. '$impl.aRec.i = 3;',
  6400. LinesToStr([ // implementation
  6401. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6402. ' this.i = 0;',
  6403. ' this.$eq = function (b) {',
  6404. ' return this.i === b.i;',
  6405. ' };',
  6406. ' this.$assign = function (s) {',
  6407. ' this.i = s.i;',
  6408. ' return this;',
  6409. ' };',
  6410. '});',
  6411. '$impl.aRec = $impl.TMyRecord.$new();',
  6412. '']) );
  6413. end;
  6414. procedure TTestModule.TestRenameJSNameConflict;
  6415. begin
  6416. StartProgram(false);
  6417. Add('var apply: longint;');
  6418. Add('var bind: longint;');
  6419. Add('var call: longint;');
  6420. Add('begin');
  6421. ConvertProgram;
  6422. CheckSource('TestRenameJSNameConflict',
  6423. LinesToStr([ // statements
  6424. 'this.Apply = 0;',
  6425. 'this.Bind = 0;',
  6426. 'this.Call = 0;'
  6427. ]),
  6428. LinesToStr([ // this.$main
  6429. ''
  6430. ]));
  6431. end;
  6432. procedure TTestModule.TestLocalConst;
  6433. begin
  6434. StartProgram(false);
  6435. Add('procedure DoIt;');
  6436. Add('const');
  6437. Add(' cA: longint = 1;');
  6438. Add(' cB = 2;');
  6439. Add(' procedure Sub;');
  6440. Add(' const');
  6441. Add(' csA = 3;');
  6442. Add(' cB: double = 4;');
  6443. Add(' begin');
  6444. Add(' cb:=cb+csa;');
  6445. Add(' ca:=ca+csa+5;');
  6446. Add(' end;');
  6447. Add('begin');
  6448. Add(' ca:=ca+cb+6;');
  6449. Add('end;');
  6450. Add('begin');
  6451. ConvertProgram;
  6452. CheckSource('TestLocalConst',
  6453. LinesToStr([
  6454. 'var cA = 1;',
  6455. 'var cB = 2;',
  6456. 'var csA = 3;',
  6457. 'var cB$1 = 4;',
  6458. 'this.DoIt = function () {',
  6459. ' function Sub() {',
  6460. ' cB$1 = cB$1 + 3;',
  6461. ' cA = cA + 3 + 5;',
  6462. ' };',
  6463. ' cA = cA + 2 + 6;',
  6464. '};'
  6465. ]),
  6466. LinesToStr([
  6467. ]));
  6468. end;
  6469. procedure TTestModule.TestVarExternal;
  6470. begin
  6471. StartProgram(false);
  6472. Add('var');
  6473. Add(' NaN: double; external name ''Global.NaN'';');
  6474. Add(' d: double;');
  6475. Add('begin');
  6476. Add(' d:=NaN;');
  6477. ConvertProgram;
  6478. CheckSource('TestVarExternal',
  6479. LinesToStr([
  6480. 'this.d = 0.0;'
  6481. ]),
  6482. LinesToStr([
  6483. '$mod.d = Global.NaN;'
  6484. ]));
  6485. end;
  6486. procedure TTestModule.TestVarExternalOtherUnit;
  6487. begin
  6488. AddModuleWithIntfImplSrc('unit2.pas',
  6489. LinesToStr([
  6490. 'var NaN: double; external name ''Global.NaN'';',
  6491. 'var iV: longint;'
  6492. ]),
  6493. '');
  6494. StartUnit(true);
  6495. Add('interface');
  6496. Add('uses unit2;');
  6497. Add('implementation');
  6498. Add('var');
  6499. Add(' d: double;');
  6500. Add(' i: longint; external name ''$i'';');
  6501. Add('begin');
  6502. Add(' d:=nan;');
  6503. Add(' d:=uNit2.nan;');
  6504. Add(' d:=test1.d;');
  6505. Add(' i:=iv;');
  6506. Add(' i:=uNit2.iv;');
  6507. Add(' i:=test1.i;');
  6508. ConvertUnit;
  6509. CheckSource('TestVarExternalOtherUnit',
  6510. LinesToStr([
  6511. 'var $impl = $mod.$impl;',
  6512. '']),
  6513. LinesToStr([ // this.$init
  6514. '$impl.d = Global.NaN;',
  6515. '$impl.d = Global.NaN;',
  6516. '$impl.d = $impl.d;',
  6517. '$i = pas.unit2.iV;',
  6518. '$i = pas.unit2.iV;',
  6519. '$i = $i;',
  6520. '']),
  6521. LinesToStr([ // implementation
  6522. '$impl.d = 0.0;',
  6523. '']) );
  6524. end;
  6525. procedure TTestModule.TestVarAbsoluteFail;
  6526. begin
  6527. StartProgram(false);
  6528. Add([
  6529. 'var',
  6530. ' a: longint;',
  6531. ' b: longword absolute a;',
  6532. 'begin']);
  6533. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6534. ConvertProgram;
  6535. end;
  6536. procedure TTestModule.TestConstExternal;
  6537. begin
  6538. StartProgram(false);
  6539. Add([
  6540. 'const',
  6541. ' PI: double; external name ''Global.PI'';',
  6542. ' Tau = 2*pi;',
  6543. 'var d: double;',
  6544. 'begin',
  6545. ' d:=pi;',
  6546. ' d:=tau+pi;']);
  6547. ConvertProgram;
  6548. CheckSource('TestConstExternal',
  6549. LinesToStr([
  6550. 'this.Tau = 2*Global.PI;',
  6551. 'this.d = 0.0;'
  6552. ]),
  6553. LinesToStr([
  6554. '$mod.d = Global.PI;',
  6555. '$mod.d = $mod.Tau + Global.PI;'
  6556. ]));
  6557. end;
  6558. procedure TTestModule.TestDouble;
  6559. begin
  6560. StartProgram(false);
  6561. Add([
  6562. 'type',
  6563. ' TDateTime = double;',
  6564. 'const',
  6565. ' a = TDateTime(2.7);',
  6566. ' b = a + TDateTime(1.7);',
  6567. ' c = 0.9 + 0.1;',
  6568. ' f0_1 = 0.1;',
  6569. ' f0_3 = 0.3;',
  6570. ' fn0_1 = -0.1;',
  6571. ' fn0_3 = -0.3;',
  6572. ' fn0_003 = -0.003;',
  6573. ' fn0_123456789 = -0.123456789;',
  6574. ' fn300_0 = -300.0;',
  6575. ' fn123456_0 = -123456.0;',
  6576. ' fn1234567_8 = -1234567.8;',
  6577. ' fn12345678_9 = -12345678.9;',
  6578. ' f1_0En12 = 1E-12;',
  6579. ' fn1_0En12 = -1E-12;',
  6580. ' maxdouble = 1.7e+308;',
  6581. ' mindouble = -1.7e+308;',
  6582. ' MinSafeIntDouble = -$1fffffffffffff;',
  6583. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6584. ' MaxSafeIntDouble = $1fffffffffffff;',
  6585. ' DZeroResolution = 1E-12;',
  6586. ' Minus1 = -1E-12;',
  6587. ' EPS = 1E-9;',
  6588. ' DELTA = 0.001;',
  6589. ' Big = 129.789E+100;',
  6590. ' Test0_15 = 0.15;',
  6591. ' Test999 = 2.9999999999999;',
  6592. ' Test111999 = 211199999999999000.0;',
  6593. ' TestMinus111999 = -211199999999999000.0;',
  6594. 'var',
  6595. ' d: double = b;',
  6596. 'begin',
  6597. ' d:=1.0;',
  6598. ' d:=1.0/3.0;',
  6599. ' d:=1/3;',
  6600. ' d:=5.0E-324;',
  6601. ' d:=1.7E308;',
  6602. ' d:=001.00E00;',
  6603. ' d:=002.00E001;',
  6604. ' d:=003.000E000;',
  6605. ' d:=-004.00E-00;',
  6606. ' d:=-005.00E-001;',
  6607. ' d:=10**3;',
  6608. ' d:=10 mod 3;',
  6609. ' d:=10 div 3;',
  6610. ' d:=c;',
  6611. ' d:=f0_1;',
  6612. ' d:=f0_3;',
  6613. ' d:=fn0_1;',
  6614. ' d:=fn0_3;',
  6615. ' d:=fn0_003;',
  6616. ' d:=fn0_123456789;',
  6617. ' d:=fn300_0;',
  6618. ' d:=fn123456_0;',
  6619. ' d:=fn1234567_8;',
  6620. ' d:=fn12345678_9;',
  6621. ' d:=f1_0En12;',
  6622. ' d:=fn1_0En12;',
  6623. ' d:=maxdouble;',
  6624. ' d:=mindouble;',
  6625. ' d:=MinSafeIntDouble;',
  6626. ' d:=double(MinSafeIntDouble);',
  6627. ' d:=MinSafeIntDouble2;',
  6628. ' d:=double(MinSafeIntDouble2);',
  6629. ' d:=MaxSafeIntDouble;',
  6630. ' d:=default(double);',
  6631. '']);
  6632. ConvertProgram;
  6633. CheckSource('TestDouble',
  6634. LinesToStr([
  6635. 'this.a = 2.7;',
  6636. 'this.b = 2.7 + 1.7;',
  6637. 'this.c = 0.9 + 0.1;',
  6638. 'this.f0_1 = 0.1;',
  6639. 'this.f0_3 = 0.3;',
  6640. 'this.fn0_1 = -0.1;',
  6641. 'this.fn0_3 = -0.3;',
  6642. 'this.fn0_003 = -0.003;',
  6643. 'this.fn0_123456789 = -0.123456789;',
  6644. 'this.fn300_0 = -300.0;',
  6645. 'this.fn123456_0 = -123456.0;',
  6646. 'this.fn1234567_8 = -1234567.8;',
  6647. 'this.fn12345678_9 = -12345678.9;',
  6648. 'this.f1_0En12 = 1E-12;',
  6649. 'this.fn1_0En12 = -1E-12;',
  6650. 'this.maxdouble = 1.7e+308;',
  6651. 'this.mindouble = -1.7e+308;',
  6652. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6653. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6654. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6655. 'this.DZeroResolution = 1E-12;',
  6656. 'this.Minus1 = -1E-12;',
  6657. 'this.EPS = 1E-9;',
  6658. 'this.DELTA = 0.001;',
  6659. 'this.Big = 129.789E+100;',
  6660. 'this.Test0_15 = 0.15;',
  6661. 'this.Test999 = 2.9999999999999;',
  6662. 'this.Test111999 = 211199999999999000.0;',
  6663. 'this.TestMinus111999 = -211199999999999000.0;',
  6664. 'this.d = 4.4;'
  6665. ]),
  6666. LinesToStr([
  6667. '$mod.d = 1.0;',
  6668. '$mod.d = 1.0 / 3.0;',
  6669. '$mod.d = 1 / 3;',
  6670. '$mod.d = 5.0E-324;',
  6671. '$mod.d = 1.7E308;',
  6672. '$mod.d = 1.00E0;',
  6673. '$mod.d = 2.00E1;',
  6674. '$mod.d = 3.000E0;',
  6675. '$mod.d = -4.00E-0;',
  6676. '$mod.d = -5.00E-1;',
  6677. '$mod.d = Math.pow(10, 3);',
  6678. '$mod.d = 10 % 3;',
  6679. '$mod.d = rtl.trunc(10 / 3);',
  6680. '$mod.d = 1;',
  6681. '$mod.d = 0.1;',
  6682. '$mod.d = 0.3;',
  6683. '$mod.d = -0.1;',
  6684. '$mod.d = -0.3;',
  6685. '$mod.d = -0.003;',
  6686. '$mod.d = -0.123456789;',
  6687. '$mod.d = -300;',
  6688. '$mod.d = -123456;',
  6689. '$mod.d = -1234567.8;',
  6690. '$mod.d = -1.23456789E7;',
  6691. '$mod.d = 1E-12;',
  6692. '$mod.d = -1E-12;',
  6693. '$mod.d = 1.7E308;',
  6694. '$mod.d = -1.7E308;',
  6695. '$mod.d = -9007199254740991;',
  6696. '$mod.d = -9007199254740991;',
  6697. '$mod.d = -9.007199254740992E15;',
  6698. '$mod.d = -9.007199254740992E15;',
  6699. '$mod.d = 9007199254740991;',
  6700. '$mod.d = 0.0;',
  6701. '']));
  6702. end;
  6703. procedure TTestModule.TestInteger;
  6704. begin
  6705. StartProgram(false);
  6706. Add([
  6707. 'const',
  6708. ' MinInt = low(NativeInt);',
  6709. ' MaxInt = high(NativeInt);',
  6710. 'type',
  6711. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6712. 'const',
  6713. ' a = low(TMyInt)+High(TMyInt);',
  6714. 'var',
  6715. ' i: TMyInt;',
  6716. 'begin',
  6717. ' i:=-MinInt;',
  6718. ' i:=default(TMyInt);',
  6719. ' i:=low(i)+high(i);',
  6720. '']);
  6721. ConvertProgram;
  6722. CheckSource('TestIntegerRange',
  6723. LinesToStr([
  6724. 'this.MinInt = -9007199254740991;',
  6725. 'this.MaxInt = 9007199254740991;',
  6726. 'this.a = -9007199254740991 + 9007199254740991;',
  6727. 'this.i = 0;',
  6728. '']),
  6729. LinesToStr([
  6730. '$mod.i = - -9007199254740991;',
  6731. '$mod.i = -9007199254740991;',
  6732. '$mod.i = -9007199254740991 + 9007199254740991;',
  6733. '']));
  6734. end;
  6735. procedure TTestModule.TestIntegerRange;
  6736. begin
  6737. StartProgram(false);
  6738. Add([
  6739. 'const',
  6740. ' MinInt = -1;',
  6741. ' MaxInt = +1;',
  6742. 'type',
  6743. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6744. ' TInt2 = 1..3;',
  6745. 'const',
  6746. ' a = low(TMyInt)+High(TMyInt);',
  6747. ' b = low(TInt2)+High(TInt2);',
  6748. ' s1 = [1];',
  6749. ' s2 = [1,2];',
  6750. ' s3 = [1..3];',
  6751. ' s4 = [low(shortint)..high(shortint)];',
  6752. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6753. ' s6 = 1 in s2;',
  6754. 'var',
  6755. ' i: TMyInt;',
  6756. ' i2: TInt2;',
  6757. 'begin',
  6758. ' i:=i2;',
  6759. ' i:=default(TMyInt);',
  6760. ' if i=i2 then ;']);
  6761. ConvertProgram;
  6762. CheckSource('TestIntegerRange',
  6763. LinesToStr([
  6764. 'this.MinInt = -1;',
  6765. 'this.MaxInt = +1;',
  6766. 'this.a = -1 + 1;',
  6767. 'this.b = 1 + 3;',
  6768. 'this.s1 = rtl.createSet(1);',
  6769. 'this.s2 = rtl.createSet(1, 2);',
  6770. 'this.s3 = rtl.createSet(null, 1, 3);',
  6771. 'this.s4 = rtl.createSet(null, -128, 127);',
  6772. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6773. 'this.s6 = 1 in this.s2;',
  6774. 'this.i = 0;',
  6775. 'this.i2 = 0;',
  6776. '']),
  6777. LinesToStr([
  6778. '$mod.i = $mod.i2;',
  6779. '$mod.i = -1;',
  6780. 'if ($mod.i === $mod.i2) ;',
  6781. '']));
  6782. end;
  6783. procedure TTestModule.TestIntegerTypecasts;
  6784. begin
  6785. StartProgram(false);
  6786. Add([
  6787. 'var',
  6788. ' i: nativeint;',
  6789. ' b: byte;',
  6790. ' sh: shortint;',
  6791. ' w: word;',
  6792. ' sm: smallint;',
  6793. ' lw: longword;',
  6794. ' li: longint;',
  6795. 'begin',
  6796. ' b:=byte(i);',
  6797. ' sh:=shortint(i);',
  6798. ' w:=word(i);',
  6799. ' sm:=smallint(i);',
  6800. ' lw:=longword(i);',
  6801. ' li:=longint(i);',
  6802. '']);
  6803. ConvertProgram;
  6804. CheckSource('TestIntegerTypecasts',
  6805. LinesToStr([
  6806. 'this.i = 0;',
  6807. 'this.b = 0;',
  6808. 'this.sh = 0;',
  6809. 'this.w = 0;',
  6810. 'this.sm = 0;',
  6811. 'this.lw = 0;',
  6812. 'this.li = 0;',
  6813. '']),
  6814. LinesToStr([
  6815. '$mod.b = $mod.i & 255;',
  6816. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6817. '$mod.w = $mod.i & 65535;',
  6818. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6819. '$mod.lw = $mod.i >>> 0;',
  6820. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6821. '']));
  6822. end;
  6823. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6824. begin
  6825. StartProgram(false);
  6826. Add([
  6827. 'var',
  6828. ' i,j: nativeint;',
  6829. 'begin',
  6830. ' i:=i shr 0;',
  6831. ' i:=i shr 1;',
  6832. ' i:=i shr 3;',
  6833. ' i:=i shr 54;',
  6834. ' i:=j shr i;',
  6835. '']);
  6836. ConvertProgram;
  6837. CheckResolverUnexpectedHints;
  6838. CheckSource('TestInteger_BitwiseShrNativeInt',
  6839. LinesToStr([
  6840. 'this.i = 0;',
  6841. 'this.j = 0;',
  6842. '']),
  6843. LinesToStr([
  6844. '$mod.i = $mod.i;',
  6845. '$mod.i = Math.floor($mod.i / 2);',
  6846. '$mod.i = Math.floor($mod.i / 8);',
  6847. '$mod.i = 0;',
  6848. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6849. '']));
  6850. end;
  6851. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6852. begin
  6853. StartProgram(false);
  6854. Add([
  6855. 'var',
  6856. ' i: nativeint;',
  6857. 'begin',
  6858. ' i:=i shl 0;',
  6859. ' i:=i shl 54;',
  6860. ' i:=123456789012 shl 1;',
  6861. ' i:=i shl 1;',
  6862. '']);
  6863. ConvertProgram;
  6864. CheckResolverUnexpectedHints;
  6865. CheckSource('TestInteger_BitwiseShrNativeInt',
  6866. LinesToStr([
  6867. 'this.i = 0;',
  6868. '']),
  6869. LinesToStr([
  6870. '$mod.i = $mod.i;',
  6871. '$mod.i = 0;',
  6872. '$mod.i = 246913578024;',
  6873. '$mod.i = rtl.shl($mod.i, 1);',
  6874. '']));
  6875. end;
  6876. procedure TTestModule.TestInteger_SystemFunc;
  6877. begin
  6878. StartProgram(true);
  6879. Add([
  6880. 'var',
  6881. ' i: byte;',
  6882. ' s: string;',
  6883. 'begin',
  6884. ' system.inc(i);',
  6885. ' system.str(i,s);',
  6886. ' s:=system.str(i);',
  6887. ' i:=system.low(i);',
  6888. ' i:=system.high(i);',
  6889. ' i:=system.pred(i);',
  6890. ' i:=system.succ(i);',
  6891. '']);
  6892. ConvertProgram;
  6893. CheckResolverUnexpectedHints;
  6894. CheckSource('TestInteger_SystemFunc',
  6895. LinesToStr([
  6896. 'this.i = 0;',
  6897. 'this.s = "";',
  6898. '']),
  6899. LinesToStr([
  6900. '$mod.i += 1;',
  6901. '$mod.s = "" + $mod.i;',
  6902. '$mod.s = "" + $mod.i;',
  6903. '$mod.i = 0;',
  6904. '$mod.i = 255;',
  6905. '$mod.i = $mod.i - 1;',
  6906. '$mod.i = $mod.i + 1;',
  6907. '']));
  6908. end;
  6909. procedure TTestModule.TestCurrency;
  6910. begin
  6911. StartProgram(false);
  6912. Add([
  6913. 'type',
  6914. ' TCoin = currency;',
  6915. 'const',
  6916. ' a = TCoin(2.7);',
  6917. ' b = a + TCoin(1.7);',
  6918. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6919. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6920. 'var',
  6921. ' c: TCoin = b;',
  6922. ' i: nativeint;',
  6923. ' d: double;',
  6924. ' j: jsvalue;',
  6925. 'function DoIt(c: currency): currency; begin end;',
  6926. 'function GetIt(d: double): double; begin end;',
  6927. 'procedure Write(v: jsvalue); begin end;',
  6928. 'begin',
  6929. ' c:=1.0;',
  6930. ' c:=0.1;',
  6931. ' c:=1.0/3.0;',
  6932. ' c:=1/3;',
  6933. ' c:=a;',
  6934. ' d:=c;',
  6935. ' c:=d;',
  6936. ' c:=currency(c);',
  6937. ' c:=currency(d);',
  6938. ' d:=double(c);',
  6939. ' c:=i;',
  6940. ' c:=currency(i);',
  6941. //' i:=c;', not allowed
  6942. ' i:=nativeint(c);',
  6943. ' c:=c+a;',
  6944. ' c:=-c-a;',
  6945. ' c:=d+c;',
  6946. ' c:=c+d;',
  6947. ' c:=d-c;',
  6948. ' c:=c-d;',
  6949. ' c:=c*a;',
  6950. ' c:=a*c;',
  6951. ' c:=d*c;',
  6952. ' c:=c*d;',
  6953. ' c:=c/a;',
  6954. ' c:=a/c;',
  6955. ' c:=d/c;',
  6956. ' c:=c/d;',
  6957. ' c:=c**a;',
  6958. ' c:=a**c;',
  6959. ' c:=d**c;',
  6960. ' c:=c**d;',
  6961. ' if c=c then ;',
  6962. ' if c=a then ;',
  6963. ' if a=c then ;',
  6964. ' if d=c then ;',
  6965. ' if c=d then ;',
  6966. ' c:=DoIt(c);',
  6967. ' c:=DoIt(i);',
  6968. ' c:=DoIt(d);',
  6969. ' c:=GetIt(c);',
  6970. ' j:=c;',
  6971. ' Write(c);',
  6972. ' c:=default(currency);',
  6973. ' j:=str(c);',
  6974. ' j:=str(c:0:3);',
  6975. '']);
  6976. ConvertProgram;
  6977. CheckSource('TestCurrency',
  6978. LinesToStr([
  6979. 'this.a = 27000;',
  6980. 'this.b = this.a + 17000;',
  6981. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6982. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6983. 'this.c = this.b;',
  6984. 'this.i = 0;',
  6985. 'this.d = 0.0;',
  6986. 'this.j = undefined;',
  6987. 'this.DoIt = function (c) {',
  6988. ' var Result = 0;',
  6989. ' return Result;',
  6990. '};',
  6991. 'this.GetIt = function (d) {',
  6992. ' var Result = 0.0;',
  6993. ' return Result;',
  6994. '};',
  6995. 'this.Write = function (v) {',
  6996. '};',
  6997. '']),
  6998. LinesToStr([
  6999. '$mod.c = 10000;',
  7000. '$mod.c = 1000;',
  7001. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7002. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7003. '$mod.c = $mod.a;',
  7004. '$mod.d = $mod.c / 10000;',
  7005. '$mod.c = rtl.trunc($mod.d * 10000);',
  7006. '$mod.c = $mod.c;',
  7007. '$mod.c = $mod.d * 10000;',
  7008. '$mod.d = $mod.c / 10000;',
  7009. '$mod.c = $mod.i * 10000;',
  7010. '$mod.c = $mod.i * 10000;',
  7011. '$mod.i = rtl.trunc($mod.c / 10000);',
  7012. '$mod.c = $mod.c + $mod.a;',
  7013. '$mod.c = -$mod.c - $mod.a;',
  7014. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7015. '$mod.c = $mod.c + ($mod.d * 10000);',
  7016. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7017. '$mod.c = $mod.c - ($mod.d * 10000);',
  7018. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7019. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7020. '$mod.c = $mod.d * $mod.c;',
  7021. '$mod.c = $mod.c * $mod.d;',
  7022. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7023. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7024. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7025. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7026. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7027. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7028. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7029. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7030. 'if ($mod.c === $mod.c) ;',
  7031. 'if ($mod.c === $mod.a) ;',
  7032. 'if ($mod.a === $mod.c) ;',
  7033. 'if (($mod.d * 10000) === $mod.c) ;',
  7034. 'if ($mod.c === ($mod.d * 10000)) ;',
  7035. '$mod.c = $mod.DoIt($mod.c);',
  7036. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7037. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7038. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7039. '$mod.j = $mod.c / 10000;',
  7040. '$mod.Write($mod.c / 10000);',
  7041. '$mod.c = 0;',
  7042. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7043. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7044. '']));
  7045. end;
  7046. procedure TTestModule.TestForBoolDo;
  7047. begin
  7048. StartProgram(false);
  7049. Add([
  7050. 'var b: boolean;',
  7051. 'begin',
  7052. ' for b:=false to true do ;',
  7053. ' for b:=b downto false do ;',
  7054. ' for b in boolean do ;',
  7055. '']);
  7056. ConvertProgram;
  7057. CheckSource('TestForBoolDo',
  7058. LinesToStr([ // statements
  7059. 'this.b = false;']),
  7060. LinesToStr([ // this.$main
  7061. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7062. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7063. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7064. '']));
  7065. end;
  7066. procedure TTestModule.TestForIntDo;
  7067. begin
  7068. StartProgram(false);
  7069. Add([
  7070. 'var i: longint;',
  7071. 'begin',
  7072. ' for i:=3 to 5 do ;',
  7073. ' for i:=i downto 2 do ;',
  7074. ' for i in byte do ;',
  7075. '']);
  7076. ConvertProgram;
  7077. CheckSource('TestForIntDo',
  7078. LinesToStr([ // statements
  7079. 'this.i = 0;']),
  7080. LinesToStr([ // this.$main
  7081. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7082. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7083. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7084. '']));
  7085. end;
  7086. procedure TTestModule.TestForIntInDo;
  7087. begin
  7088. StartProgram(false);
  7089. Add([
  7090. 'type',
  7091. ' TSetOfInt = set of byte;',
  7092. ' TIntRg = 3..7;',
  7093. ' TSetOfIntRg = set of TIntRg;',
  7094. 'var',
  7095. ' i,i2: longint;',
  7096. ' a1: array of byte;',
  7097. ' a2: array[1..3] of byte;',
  7098. ' soi: TSetOfInt;',
  7099. ' soir: TSetOfIntRg;',
  7100. ' ir: TIntRg;',
  7101. 'begin',
  7102. ' for i in byte do ;',
  7103. ' for i in a1 do ;',
  7104. ' for i in a2 do ;',
  7105. ' for i in [11..13] do ;',
  7106. ' for i in TSetOfInt do ;',
  7107. ' for i in TIntRg do ;',
  7108. ' for i in soi do i2:=i;',
  7109. ' for i in TSetOfIntRg do ;',
  7110. ' for i in soir do ;',
  7111. ' for ir in TIntRg do ;',
  7112. ' for ir in TSetOfIntRg do ;',
  7113. ' for ir in soir do ;',
  7114. '']);
  7115. ConvertProgram;
  7116. CheckSource('TestForIntInDo',
  7117. LinesToStr([ // statements
  7118. 'this.i = 0;',
  7119. 'this.i2 = 0;',
  7120. 'this.a1 = [];',
  7121. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7122. 'this.soi = {};',
  7123. 'this.soir = {};',
  7124. 'this.ir = 0;',
  7125. '']),
  7126. LinesToStr([ // this.$main
  7127. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7128. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7129. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7130. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7131. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7132. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7133. 'for (var $l6 in $mod.soi) {',
  7134. ' $mod.i = +$l6;',
  7135. ' $mod.i2 = $mod.i;',
  7136. '};',
  7137. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7138. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7139. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7140. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7141. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7142. '']));
  7143. end;
  7144. procedure TTestModule.TestCharConst;
  7145. begin
  7146. StartProgram(false);
  7147. Add([
  7148. 'const',
  7149. ' a = #$00F3;',
  7150. ' c: char = ''1'';',
  7151. ' wc: widechar = ''ä'';',
  7152. 'begin',
  7153. ' c:=#0;',
  7154. ' c:=#1;',
  7155. ' c:=#9;',
  7156. ' c:=#10;',
  7157. ' c:=#13;',
  7158. ' c:=#31;',
  7159. ' c:=#32;',
  7160. ' c:=#$A;',
  7161. ' c:=#$0A;',
  7162. ' c:=#$b;',
  7163. ' c:=#$0b;',
  7164. ' c:=^A;',
  7165. ' c:=''"'';',
  7166. ' c:=default(char);',
  7167. ' c:=#$00E4;', // ä
  7168. ' c:=''ä'';',
  7169. ' c:=#$E4;', // ä
  7170. ' c:=#$D800;', // invalid UTF-16
  7171. ' c:=#$DFFF;', // invalid UTF-16
  7172. ' c:=#$FFFF;', // last UCS-2
  7173. ' c:=high(c);', // last UCS-2
  7174. '']);
  7175. ConvertProgram;
  7176. CheckSource('TestCharConst',
  7177. LinesToStr([
  7178. 'this.a="ó";',
  7179. 'this.c="1";',
  7180. 'this.wc="ä";'
  7181. ]),
  7182. LinesToStr([
  7183. '$mod.c="\x00";',
  7184. '$mod.c="\x01";',
  7185. '$mod.c="\t";',
  7186. '$mod.c="\n";',
  7187. '$mod.c="\r";',
  7188. '$mod.c="\x1F";',
  7189. '$mod.c=" ";',
  7190. '$mod.c="\n";',
  7191. '$mod.c="\n";',
  7192. '$mod.c="\x0B";',
  7193. '$mod.c="\x0B";',
  7194. '$mod.c="\x01";',
  7195. '$mod.c=''"'';',
  7196. '$mod.c="\x00";',
  7197. '$mod.c = "ä";',
  7198. '$mod.c = "ä";',
  7199. '$mod.c = "ä";',
  7200. '$mod.c="\uD800";',
  7201. '$mod.c="\uDFFF";',
  7202. '$mod.c="\uFFFF";',
  7203. '$mod.c="\uFFFF";',
  7204. '']));
  7205. end;
  7206. procedure TTestModule.TestChar_Compare;
  7207. begin
  7208. StartProgram(false);
  7209. Add('var');
  7210. Add(' c: char;');
  7211. Add(' b: boolean;');
  7212. Add('begin');
  7213. Add(' b:=c=''1'';');
  7214. Add(' b:=''2''=c;');
  7215. Add(' b:=''3''=''4'';');
  7216. Add(' b:=c<>''5'';');
  7217. Add(' b:=''6''<>c;');
  7218. Add(' b:=c>''7'';');
  7219. Add(' b:=''8''>c;');
  7220. Add(' b:=c>=''9'';');
  7221. Add(' b:=''A''>=c;');
  7222. Add(' b:=c<''B'';');
  7223. Add(' b:=''C''<c;');
  7224. Add(' b:=c<=''D'';');
  7225. Add(' b:=''E''<=c;');
  7226. ConvertProgram;
  7227. CheckSource('TestChar_Compare',
  7228. LinesToStr([
  7229. 'this.c="";',
  7230. 'this.b = false;'
  7231. ]),
  7232. LinesToStr([
  7233. '$mod.b = $mod.c === "1";',
  7234. '$mod.b = "2" === $mod.c;',
  7235. '$mod.b = "3" === "4";',
  7236. '$mod.b = $mod.c !== "5";',
  7237. '$mod.b = "6" !== $mod.c;',
  7238. '$mod.b = $mod.c > "7";',
  7239. '$mod.b = "8" > $mod.c;',
  7240. '$mod.b = $mod.c >= "9";',
  7241. '$mod.b = "A" >= $mod.c;',
  7242. '$mod.b = $mod.c < "B";',
  7243. '$mod.b = "C" < $mod.c;',
  7244. '$mod.b = $mod.c <= "D";',
  7245. '$mod.b = "E" <= $mod.c;',
  7246. '']));
  7247. end;
  7248. procedure TTestModule.TestChar_BuiltInProcs;
  7249. begin
  7250. StartProgram(false);
  7251. Add([
  7252. 'var',
  7253. ' c: char;',
  7254. ' i: longint;',
  7255. ' s: string;',
  7256. 'begin',
  7257. ' i:=ord(c);',
  7258. ' i:=ord(s[i]);',
  7259. ' c:=chr(i);',
  7260. ' c:=pred(c);',
  7261. ' c:=succ(c);',
  7262. ' c:=low(c);',
  7263. ' c:=high(c);',
  7264. ' i:=byte(c);',
  7265. ' i:=word(c);',
  7266. ' i:=longint(c);',
  7267. '']);
  7268. ConvertProgram;
  7269. CheckSource('TestChar_BuiltInProcs',
  7270. LinesToStr([
  7271. 'this.c = "";',
  7272. 'this.i = 0;',
  7273. 'this.s = "";'
  7274. ]),
  7275. LinesToStr([
  7276. '$mod.i = $mod.c.charCodeAt();',
  7277. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7278. '$mod.c = String.fromCharCode($mod.i);',
  7279. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7280. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7281. '$mod.c = "\x00";',
  7282. '$mod.c = "\uFFFF";',
  7283. '$mod.i = $mod.c.charCodeAt() & 255;',
  7284. '$mod.i = $mod.c.charCodeAt();',
  7285. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7286. '']));
  7287. end;
  7288. procedure TTestModule.TestStringConst;
  7289. begin
  7290. StartProgram(false);
  7291. Add([
  7292. '{$H+}',
  7293. 'const',
  7294. ' a = #$00F3#$017C;', // first <256, then >=256
  7295. ' b = string(''a'');',
  7296. ' c = string(''ä'');',
  7297. ' d = UnicodeString(''b'');',
  7298. ' e = UnicodeString(''ö'');',
  7299. 'var',
  7300. ' s: string = ''abc'';',
  7301. 'begin',
  7302. ' s:='''';',
  7303. ' s:=#13#10;',
  7304. ' s:=#9''foo'';',
  7305. ' s:=#$A9;',
  7306. ' s:=''foo''#13''bar'';',
  7307. ' s:=''"'';',
  7308. ' s:=''"''''"'';',
  7309. ' s:=#$20AC;', // euro
  7310. ' s:=#$10437;', // outside BMP
  7311. ' s:=default(string);',
  7312. ' s:=concat(s);',
  7313. ' s:=concat(s,''a'',s)',
  7314. '']);
  7315. ConvertProgram;
  7316. CheckSource('TestStringConst',
  7317. LinesToStr([
  7318. 'this.a = "óż";',
  7319. 'this.b = "a";',
  7320. 'this.c = "ä";',
  7321. 'this.d = "b";',
  7322. 'this.e = "ö";',
  7323. 'this.s="abc";',
  7324. '']),
  7325. LinesToStr([
  7326. '$mod.s="";',
  7327. '$mod.s="\r\n";',
  7328. '$mod.s="\tfoo";',
  7329. '$mod.s="©";',
  7330. '$mod.s="foo\rbar";',
  7331. '$mod.s=''"'';',
  7332. '$mod.s=''"\''"'';',
  7333. '$mod.s="€";',
  7334. '$mod.s="'#$F0#$90#$90#$B7'";',
  7335. '$mod.s="";',
  7336. '$mod.s = $mod.s;',
  7337. '$mod.s = $mod.s.concat("a", $mod.s);',
  7338. '']));
  7339. end;
  7340. procedure TTestModule.TestStringConstSurrogate;
  7341. begin
  7342. StartProgram(false);
  7343. Add([
  7344. 'var',
  7345. ' s: string;',
  7346. 'begin',
  7347. ' s:=''😊'';', // 1F60A
  7348. '']);
  7349. ConvertProgram;
  7350. CheckSource('TestStringConstSurrogate',
  7351. LinesToStr([
  7352. 'this.s="";'
  7353. ]),
  7354. LinesToStr([
  7355. '$mod.s="😊";'
  7356. ]));
  7357. end;
  7358. procedure TTestModule.TestString_Length;
  7359. begin
  7360. StartProgram(false);
  7361. Add('const c = ''foo'';');
  7362. Add('var');
  7363. Add(' s: string;');
  7364. Add(' i: longint;');
  7365. Add('begin');
  7366. Add(' i:=length(s);');
  7367. Add(' i:=length(s+s);');
  7368. Add(' i:=length(''abc'');');
  7369. Add(' i:=length(c);');
  7370. ConvertProgram;
  7371. CheckSource('TestString_Length',
  7372. LinesToStr([
  7373. 'this.c = "foo";',
  7374. 'this.s = "";',
  7375. 'this.i = 0;',
  7376. '']),
  7377. LinesToStr([
  7378. '$mod.i = $mod.s.length;',
  7379. '$mod.i = ($mod.s+$mod.s).length;',
  7380. '$mod.i = "abc".length;',
  7381. '$mod.i = $mod.c.length;',
  7382. '']));
  7383. end;
  7384. procedure TTestModule.TestString_Compare;
  7385. begin
  7386. StartProgram(false);
  7387. Add('var');
  7388. Add(' s, t: string;');
  7389. Add(' b: boolean;');
  7390. Add('begin');
  7391. Add(' b:=s=t;');
  7392. Add(' b:=s<>t;');
  7393. Add(' b:=s>t;');
  7394. Add(' b:=s>=t;');
  7395. Add(' b:=s<t;');
  7396. Add(' b:=s<=t;');
  7397. ConvertProgram;
  7398. CheckSource('TestString_Compare',
  7399. LinesToStr([ // statements
  7400. 'this.s = "";',
  7401. 'this.t = "";',
  7402. 'this.b =false;'
  7403. ]),
  7404. LinesToStr([ // this.$main
  7405. '$mod.b = $mod.s === $mod.t;',
  7406. '$mod.b = $mod.s !== $mod.t;',
  7407. '$mod.b = $mod.s > $mod.t;',
  7408. '$mod.b = $mod.s >= $mod.t;',
  7409. '$mod.b = $mod.s < $mod.t;',
  7410. '$mod.b = $mod.s <= $mod.t;',
  7411. '']));
  7412. end;
  7413. procedure TTestModule.TestString_SetLength;
  7414. begin
  7415. StartProgram(false);
  7416. Add([
  7417. 'procedure DoIt(var s: string);',
  7418. 'begin',
  7419. ' SetLength(s,2);',
  7420. 'end;',
  7421. 'var s: string;',
  7422. 'begin',
  7423. ' SetLength(s,3);',
  7424. '']);
  7425. ConvertProgram;
  7426. CheckSource('TestString_SetLength',
  7427. LinesToStr([ // statements
  7428. 'this.DoIt = function (s) {',
  7429. ' s.set(rtl.strSetLength(s.get(), 2));',
  7430. '};',
  7431. 'this.s = "";',
  7432. '']),
  7433. LinesToStr([ // this.$main
  7434. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7435. ]));
  7436. end;
  7437. procedure TTestModule.TestString_CharAt;
  7438. begin
  7439. StartProgram(false);
  7440. Add([
  7441. 'var',
  7442. ' s: string;',
  7443. ' c: char;',
  7444. ' b: boolean;',
  7445. 'begin',
  7446. ' b:= s[1] = c;',
  7447. ' b:= c = s[1];',
  7448. ' b:= c <> s[1];',
  7449. ' b:= c > s[1];',
  7450. ' b:= c >= s[1];',
  7451. ' b:= c < s[2];',
  7452. ' b:= c <= s[1];',
  7453. ' s[1] := c;',
  7454. ' s[2+3] := c;']);
  7455. ConvertProgram;
  7456. CheckSource('TestString_CharAt',
  7457. LinesToStr([ // statements
  7458. 'this.s = "";',
  7459. 'this.c = "";',
  7460. 'this.b = false;'
  7461. ]),
  7462. LinesToStr([ // this.$main
  7463. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7464. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7465. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7466. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7467. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7468. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7469. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7470. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7471. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7472. '']));
  7473. end;
  7474. procedure TTestModule.TestStringHMinusFail;
  7475. begin
  7476. StartProgram(false);
  7477. Add([
  7478. '{$H-}',
  7479. 'var s: string;',
  7480. 'begin']);
  7481. ConvertProgram;
  7482. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7483. end;
  7484. procedure TTestModule.TestStr;
  7485. begin
  7486. StartProgram(false);
  7487. Add('var');
  7488. Add(' b: boolean;');
  7489. Add(' i: longint;');
  7490. Add(' d: double;');
  7491. Add(' s: string;');
  7492. Add('begin');
  7493. Add(' str(b,s);');
  7494. Add(' str(i,s);');
  7495. Add(' str(d,s);');
  7496. Add(' str(i:3,s);');
  7497. Add(' str(d:3:2,s);');
  7498. Add(' Str(12.456:12:1,s);');
  7499. Add(' Str(12.456:12,s);');
  7500. Add(' s:=str(b);');
  7501. Add(' s:=str(i);');
  7502. Add(' s:=str(d);');
  7503. Add(' s:=str(i,i);');
  7504. Add(' s:=str(i:3);');
  7505. Add(' s:=str(d:3:2);');
  7506. Add(' s:=str(i:4,i);');
  7507. Add(' s:=str(i,i:5);');
  7508. Add(' s:=str(i:4,i:5);');
  7509. Add(' s:=str(s,s);');
  7510. Add(' s:=str(s,''foo'');');
  7511. ConvertProgram;
  7512. CheckSource('TestStr',
  7513. LinesToStr([ // statements
  7514. 'this.b = false;',
  7515. 'this.i = 0;',
  7516. 'this.d = 0.0;',
  7517. 'this.s = "";',
  7518. '']),
  7519. LinesToStr([ // this.$main
  7520. '$mod.s = ""+$mod.b;',
  7521. '$mod.s = ""+$mod.i;',
  7522. '$mod.s = rtl.floatToStr($mod.d);',
  7523. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7524. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7525. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7526. '$mod.s = rtl.floatToStr(12.456,12);',
  7527. '$mod.s = ""+$mod.b;',
  7528. '$mod.s = ""+$mod.i;',
  7529. '$mod.s = rtl.floatToStr($mod.d);',
  7530. '$mod.s = ""+$mod.i+$mod.i;',
  7531. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7532. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7533. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7534. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7535. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7536. '$mod.s = $mod.s + $mod.s;',
  7537. '$mod.s = $mod.s + "foo";',
  7538. '']));
  7539. end;
  7540. procedure TTestModule.TestBaseType_AnsiStringFail;
  7541. begin
  7542. StartProgram(false);
  7543. Add('var s: AnsiString');
  7544. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7545. ConvertProgram;
  7546. end;
  7547. procedure TTestModule.TestBaseType_WideStringFail;
  7548. begin
  7549. StartProgram(false);
  7550. Add('var s: WideString');
  7551. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7552. ConvertProgram;
  7553. end;
  7554. procedure TTestModule.TestBaseType_ShortStringFail;
  7555. begin
  7556. StartProgram(false);
  7557. Add('var s: ShortString');
  7558. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7559. ConvertProgram;
  7560. end;
  7561. procedure TTestModule.TestBaseType_RawByteStringFail;
  7562. begin
  7563. StartProgram(false);
  7564. Add('var s: RawByteString');
  7565. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7566. ConvertProgram;
  7567. end;
  7568. procedure TTestModule.TestTypeShortstring_Fail;
  7569. begin
  7570. StartProgram(false);
  7571. Add('type t = string[12];');
  7572. Add('var s: t;');
  7573. Add('begin');
  7574. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7575. ConvertProgram;
  7576. end;
  7577. procedure TTestModule.TestCharSet_Custom;
  7578. begin
  7579. StartProgram(false);
  7580. Add([
  7581. 'type',
  7582. ' TCharRg = ''a''..''z'';',
  7583. ' TSetOfCharRg = set of TCharRg;',
  7584. ' TCharRg2 = ''m''..''p'';',
  7585. 'const',
  7586. ' crg: TCharRg = ''b'';',
  7587. 'var',
  7588. ' c: char;',
  7589. ' crg2: TCharRg2;',
  7590. ' s: TSetOfCharRg;',
  7591. 'begin',
  7592. ' c:=crg;',
  7593. ' crg:=c;',
  7594. ' crg2:=crg;',
  7595. ' if c=crg then ;',
  7596. ' if crg=c then ;',
  7597. ' if crg=crg2 then ;',
  7598. ' if c in s then ;',
  7599. ' if crg2 in s then ;',
  7600. ' c:=default(TCharRg);',
  7601. '']);
  7602. ConvertProgram;
  7603. CheckSource('TestCharSet_Custom',
  7604. LinesToStr([ // statements
  7605. 'this.crg = "b";',
  7606. 'this.c = "";',
  7607. 'this.crg2 = "m";',
  7608. 'this.s = {};',
  7609. '']),
  7610. LinesToStr([ // this.$main
  7611. '$mod.c = $mod.crg;',
  7612. '$mod.crg = $mod.c;',
  7613. '$mod.crg2 = $mod.crg;',
  7614. 'if ($mod.c === $mod.crg) ;',
  7615. 'if ($mod.crg === $mod.c) ;',
  7616. 'if ($mod.crg === $mod.crg2) ;',
  7617. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7618. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7619. '$mod.c = "a";',
  7620. '']));
  7621. end;
  7622. procedure TTestModule.TestWideChar;
  7623. begin
  7624. StartProgram(false);
  7625. Add([
  7626. 'procedure Fly(var c: char);',
  7627. 'begin',
  7628. 'end;',
  7629. 'procedure Run(var c: widechar);',
  7630. 'begin',
  7631. 'end;',
  7632. 'var',
  7633. ' c: char;',
  7634. ' wc: widechar;',
  7635. ' w: word;',
  7636. 'begin',
  7637. ' Fly(wc);',
  7638. ' Run(c);',
  7639. ' wc:=WideChar(w);',
  7640. ' w:=ord(wc);',
  7641. '']);
  7642. ConvertProgram;
  7643. CheckSource('TestWideChar_VarArg',
  7644. LinesToStr([ // statements
  7645. 'this.Fly = function (c) {',
  7646. '};',
  7647. 'this.Run = function (c) {',
  7648. '};',
  7649. 'this.c = "";',
  7650. 'this.wc = "";',
  7651. 'this.w = 0;',
  7652. '']),
  7653. LinesToStr([ // this.$main
  7654. '$mod.Fly({',
  7655. ' p: $mod,',
  7656. ' get: function () {',
  7657. ' return this.p.wc;',
  7658. ' },',
  7659. ' set: function (v) {',
  7660. ' this.p.wc = v;',
  7661. ' }',
  7662. '});',
  7663. '$mod.Run({',
  7664. ' p: $mod,',
  7665. ' get: function () {',
  7666. ' return this.p.c;',
  7667. ' },',
  7668. ' set: function (v) {',
  7669. ' this.p.c = v;',
  7670. ' }',
  7671. '});',
  7672. '$mod.wc = String.fromCharCode($mod.w);',
  7673. '$mod.w = $mod.wc.charCodeAt();',
  7674. '',
  7675. '']));
  7676. end;
  7677. procedure TTestModule.TestForCharDo;
  7678. begin
  7679. StartProgram(false);
  7680. Add([
  7681. 'var c: char;',
  7682. 'begin',
  7683. ' for c:=''a'' to ''c'' do ;',
  7684. ' for c:=c downto ''a'' do ;',
  7685. ' for c:=''Б'' to ''Я'' do ;',
  7686. '']);
  7687. ConvertProgram;
  7688. CheckSource('TestForCharDo',
  7689. LinesToStr([ // statements
  7690. 'this.c = "";']),
  7691. LinesToStr([ // this.$main
  7692. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7693. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7694. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7695. '']));
  7696. end;
  7697. procedure TTestModule.TestForCharInDo;
  7698. begin
  7699. StartProgram(false);
  7700. Add([
  7701. 'type',
  7702. ' TSetOfChar = set of char;',
  7703. ' TCharRg = ''a''..''z'';',
  7704. ' TSetOfCharRg = set of TCharRg;',
  7705. 'const Foo = ''foo'';',
  7706. 'var',
  7707. ' c,c2: char;',
  7708. ' s: string;',
  7709. ' a1: array of char;',
  7710. ' a2: array[1..3] of char;',
  7711. ' soc: TSetOfChar;',
  7712. ' socr: TSetOfCharRg;',
  7713. ' cr: TCharRg;',
  7714. 'begin',
  7715. ' for c in foo do ;',
  7716. ' for c in s do ;',
  7717. ' for c in char do ;',
  7718. ' for c in a1 do ;',
  7719. ' for c in a2 do ;',
  7720. ' for c in [''1''..''3''] do ;',
  7721. ' for c in TSetOfChar do ;',
  7722. ' for c in TCharRg do ;',
  7723. ' for c in soc do c2:=c;',
  7724. ' for c in TSetOfCharRg do ;',
  7725. ' for c in socr do ;',
  7726. ' for cr in TCharRg do ;',
  7727. ' for cr in TSetOfCharRg do ;',
  7728. ' for cr in socr do ;',
  7729. '']);
  7730. ConvertProgram;
  7731. CheckSource('TestForCharInDo',
  7732. LinesToStr([ // statements
  7733. 'this.Foo = "foo";',
  7734. 'this.c = "";',
  7735. 'this.c2 = "";',
  7736. 'this.s = "";',
  7737. 'this.a1 = [];',
  7738. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7739. 'this.soc = {};',
  7740. 'this.socr = {};',
  7741. 'this.cr = "a";',
  7742. '']),
  7743. LinesToStr([ // this.$main
  7744. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7745. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7746. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7747. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7748. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7749. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7750. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7751. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7752. 'for (var $l8 in $mod.soc) {',
  7753. ' $mod.c = String.fromCharCode($l8);',
  7754. ' $mod.c2 = $mod.c;',
  7755. '};',
  7756. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7757. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7758. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7759. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7760. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7761. '']));
  7762. end;
  7763. procedure TTestModule.TestProcTwoArgs;
  7764. begin
  7765. StartProgram(false);
  7766. Add('procedure Test(a,b: longint);');
  7767. Add('begin');
  7768. Add('end;');
  7769. Add('begin');
  7770. ConvertProgram;
  7771. CheckSource('TestProcTwoArgs',
  7772. LinesToStr([ // statements
  7773. 'this.Test = function (a,b) {',
  7774. '};'
  7775. ]),
  7776. LinesToStr([ // this.$main
  7777. ''
  7778. ]));
  7779. end;
  7780. procedure TTestModule.TestProc_DefaultValue;
  7781. begin
  7782. StartProgram(false);
  7783. Add('procedure p1(i: longint = 1);');
  7784. Add('begin');
  7785. Add('end;');
  7786. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7787. Add('begin');
  7788. Add('end;');
  7789. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7790. Add('begin');
  7791. Add('end;');
  7792. Add('begin');
  7793. Add(' p1;');
  7794. Add(' p1();');
  7795. Add(' p1(11);');
  7796. Add(' p2;');
  7797. Add(' p2();');
  7798. Add(' p2(12);');
  7799. Add(' p2(13,''b'');');
  7800. Add(' p3();');
  7801. ConvertProgram;
  7802. CheckSource('TestProc_DefaultValue',
  7803. LinesToStr([ // statements
  7804. 'this.p1 = function (i) {',
  7805. '};',
  7806. 'this.p2 = function (i,c) {',
  7807. '};',
  7808. 'this.p3 = function (d,b,s) {',
  7809. '};'
  7810. ]),
  7811. LinesToStr([ // this.$main
  7812. ' $mod.p1(1);',
  7813. ' $mod.p1(1);',
  7814. ' $mod.p1(11);',
  7815. ' $mod.p2(1,"a");',
  7816. ' $mod.p2(1,"a");',
  7817. ' $mod.p2(12,"a");',
  7818. ' $mod.p2(13,"b");',
  7819. ' $mod.p3(1.0,false,"abc");'
  7820. ]));
  7821. end;
  7822. procedure TTestModule.TestFunctionInt;
  7823. begin
  7824. StartProgram(false);
  7825. Add('function MyTest(Bar: longint): longint;');
  7826. Add('begin');
  7827. Add(' Result:=2*bar');
  7828. Add('end;');
  7829. Add('begin');
  7830. ConvertProgram;
  7831. CheckSource('TestFunctionInt',
  7832. LinesToStr([ // statements
  7833. 'this.MyTest = function (Bar) {',
  7834. ' var Result = 0;',
  7835. ' Result = 2*Bar;',
  7836. ' return Result;',
  7837. '};'
  7838. ]),
  7839. LinesToStr([ // this.$main
  7840. ''
  7841. ]));
  7842. end;
  7843. procedure TTestModule.TestFunctionString;
  7844. begin
  7845. StartProgram(false);
  7846. Add('function Test(Bar: string): string;');
  7847. Add('begin');
  7848. Add(' Result:=bar+BAR');
  7849. Add('end;');
  7850. Add('begin');
  7851. ConvertProgram;
  7852. CheckSource('TestFunctionString',
  7853. LinesToStr([ // statements
  7854. 'this.Test = function (Bar) {',
  7855. ' var Result = "";',
  7856. ' Result = Bar+Bar;',
  7857. ' return Result;',
  7858. '};'
  7859. ]),
  7860. LinesToStr([ // this.$main
  7861. ''
  7862. ]));
  7863. end;
  7864. procedure TTestModule.TestIfThen;
  7865. begin
  7866. StartProgram(false);
  7867. Add([
  7868. 'var b: boolean;',
  7869. 'begin',
  7870. ' if b then ;',
  7871. ' if b then else ;']);
  7872. ConvertProgram;
  7873. CheckSource('TestIfThen',
  7874. LinesToStr([ // statements
  7875. 'this.b = false;',
  7876. '']),
  7877. LinesToStr([ // this.$main
  7878. 'if ($mod.b) ;',
  7879. 'if ($mod.b) ;',
  7880. '']));
  7881. end;
  7882. procedure TTestModule.TestForLoop;
  7883. begin
  7884. StartProgram(false);
  7885. Add('var');
  7886. Add(' vI, vJ, vN: longint;');
  7887. Add('begin');
  7888. Add(' VJ:=0;');
  7889. Add(' VN:=3;');
  7890. Add(' for VI:=1 to VN do');
  7891. Add(' begin');
  7892. Add(' VJ:=VJ+VI;');
  7893. Add(' end;');
  7894. ConvertProgram;
  7895. CheckSource('TestForLoop',
  7896. LinesToStr([ // statements
  7897. 'this.vI = 0;',
  7898. 'this.vJ = 0;',
  7899. 'this.vN = 0;'
  7900. ]),
  7901. LinesToStr([ // this.$main
  7902. ' $mod.vJ = 0;',
  7903. ' $mod.vN = 3;',
  7904. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  7905. ' $mod.vI = $l;',
  7906. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7907. ' };',
  7908. '']));
  7909. end;
  7910. procedure TTestModule.TestForLoopInsideFunction;
  7911. begin
  7912. StartProgram(false);
  7913. Add('function SumNumbers(Count: longint): longint;');
  7914. Add('var');
  7915. Add(' vI, vJ: longint;');
  7916. Add('begin');
  7917. Add(' vj:=0;');
  7918. Add(' for vi:=1 to count do');
  7919. Add(' begin');
  7920. Add(' vj:=vj+vi;');
  7921. Add(' end;');
  7922. Add('end;');
  7923. Add('begin');
  7924. Add(' sumnumbers(3);');
  7925. ConvertProgram;
  7926. CheckSource('TestForLoopInsideFunction',
  7927. LinesToStr([ // statements
  7928. 'this.SumNumbers = function (Count) {',
  7929. ' var Result = 0;',
  7930. ' var vI = 0;',
  7931. ' var vJ = 0;',
  7932. ' vJ = 0;',
  7933. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7934. ' vI = $l;',
  7935. ' vJ = vJ + vI;',
  7936. ' };',
  7937. ' return Result;',
  7938. '};'
  7939. ]),
  7940. LinesToStr([ // $mod.$main
  7941. ' $mod.SumNumbers(3);'
  7942. ]));
  7943. end;
  7944. procedure TTestModule.TestForLoop_ReadVarAfter;
  7945. begin
  7946. StartProgram(false);
  7947. Add('var');
  7948. Add(' vI: longint;');
  7949. Add('begin');
  7950. Add(' for vi:=1 to 2 do ;');
  7951. Add(' if vi=3 then ;');
  7952. ConvertProgram;
  7953. CheckSource('TestForLoop',
  7954. LinesToStr([ // statements
  7955. 'this.vI = 0;'
  7956. ]),
  7957. LinesToStr([ // this.$main
  7958. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7959. ' if ($mod.vI===3) ;'
  7960. ]));
  7961. end;
  7962. procedure TTestModule.TestForLoop_Nested;
  7963. begin
  7964. StartProgram(false);
  7965. Add('function SumNumbers(Count: longint): longint;');
  7966. Add('var');
  7967. Add(' vI, vJ, vK: longint;');
  7968. Add('begin');
  7969. Add(' VK:=0;');
  7970. Add(' for VI:=1 to count do');
  7971. Add(' begin');
  7972. Add(' for vj:=1 to vi do');
  7973. Add(' begin');
  7974. Add(' vk:=VK+VI;');
  7975. Add(' end;');
  7976. Add(' end;');
  7977. Add('end;');
  7978. Add('begin');
  7979. Add(' sumnumbers(3);');
  7980. ConvertProgram;
  7981. CheckSource('TestForLoopInFunction',
  7982. LinesToStr([ // statements
  7983. 'this.SumNumbers = function (Count) {',
  7984. ' var Result = 0;',
  7985. ' var vI = 0;',
  7986. ' var vJ = 0;',
  7987. ' var vK = 0;',
  7988. ' vK = 0;',
  7989. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7990. ' vI = $l;',
  7991. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  7992. ' vJ = $l1;',
  7993. ' vK = vK + vI;',
  7994. ' };',
  7995. ' };',
  7996. ' return Result;',
  7997. '};'
  7998. ]),
  7999. LinesToStr([ // $mod.$main
  8000. ' $mod.SumNumbers(3);'
  8001. ]));
  8002. end;
  8003. procedure TTestModule.TestRepeatUntil;
  8004. begin
  8005. StartProgram(false);
  8006. Add('var');
  8007. Add(' vI, vJ, vN: longint;');
  8008. Add('begin');
  8009. Add(' vn:=3;');
  8010. Add(' vj:=0;');
  8011. Add(' VI:=0;');
  8012. Add(' repeat');
  8013. Add(' VI:=vi+1;');
  8014. Add(' vj:=VJ+vI;');
  8015. Add(' until vi>=vn');
  8016. ConvertProgram;
  8017. CheckSource('TestRepeatUntil',
  8018. LinesToStr([ // statements
  8019. 'this.vI = 0;',
  8020. 'this.vJ = 0;',
  8021. 'this.vN = 0;'
  8022. ]),
  8023. LinesToStr([ // $mod.$main
  8024. ' $mod.vN = 3;',
  8025. ' $mod.vJ = 0;',
  8026. ' $mod.vI = 0;',
  8027. ' do{',
  8028. ' $mod.vI = $mod.vI + 1;',
  8029. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8030. ' }while(!($mod.vI>=$mod.vN));'
  8031. ]));
  8032. end;
  8033. procedure TTestModule.TestAsmBlock;
  8034. begin
  8035. StartProgram(false);
  8036. Add([
  8037. 'var',
  8038. ' vI: longint;',
  8039. 'begin',
  8040. ' vi:=1;',
  8041. ' asm',
  8042. ' if (vI===1) {',
  8043. ' vI=2;',
  8044. //' console.log(''end;'');', ToDo
  8045. ' }',
  8046. ' if (vI===2){ vI=3; }',
  8047. ' end;',
  8048. ' VI:=4;']);
  8049. ConvertProgram;
  8050. CheckSource('TestAsmBlock',
  8051. LinesToStr([ // statements
  8052. 'this.vI = 0;'
  8053. ]),
  8054. LinesToStr([ // $mod.$main
  8055. '$mod.vI = 1;',
  8056. 'if (vI===1) {',
  8057. ' vI=2;',
  8058. '}',
  8059. 'if (vI===2){ vI=3; }',
  8060. ';',
  8061. '$mod.vI = 4;'
  8062. ]));
  8063. end;
  8064. procedure TTestModule.TestAsmPas_Impl;
  8065. begin
  8066. StartUnit(false);
  8067. Add('interface');
  8068. Add('const cIntf: longint = 1;');
  8069. Add('var vIntf: longint;');
  8070. Add('implementation');
  8071. Add('const cImpl: longint = 2;');
  8072. Add('var vImpl: longint;');
  8073. Add('procedure DoIt;');
  8074. Add('const cLoc: longint = 3;');
  8075. Add('var vLoc: longint;');
  8076. Add('begin;');
  8077. Add(' asm');
  8078. //Add(' pas(vIntf)=pas(cIntf);');
  8079. //Add(' pas(vImpl)=pas(cImpl);');
  8080. //Add(' pas(vLoc)=pas(cLoc);');
  8081. Add(' end;');
  8082. Add('end;');
  8083. ConvertUnit;
  8084. CheckSource('TestAsmPas_Impl',
  8085. LinesToStr([
  8086. 'var $impl = $mod.$impl;',
  8087. 'this.cIntf = 1;',
  8088. 'this.vIntf = 0;',
  8089. '']),
  8090. '', // this.$init
  8091. LinesToStr([ // implementation
  8092. '$impl.cImpl = 2;',
  8093. '$impl.vImpl = 0;',
  8094. 'var cLoc = 3;',
  8095. '$impl.DoIt = function () {',
  8096. ' var vLoc = 0;',
  8097. '};',
  8098. '']) );
  8099. end;
  8100. procedure TTestModule.TestTryFinally;
  8101. begin
  8102. StartProgram(false);
  8103. Add('var i: longint;');
  8104. Add('begin');
  8105. Add(' try');
  8106. Add(' i:=0; i:=2 div i;');
  8107. Add(' finally');
  8108. Add(' i:=3');
  8109. Add(' end;');
  8110. ConvertProgram;
  8111. CheckSource('TestTryFinally',
  8112. LinesToStr([ // statements
  8113. 'this.i = 0;'
  8114. ]),
  8115. LinesToStr([ // $mod.$main
  8116. 'try {',
  8117. ' $mod.i = 0;',
  8118. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8119. '} finally {',
  8120. ' $mod.i = 3;',
  8121. '};'
  8122. ]));
  8123. end;
  8124. procedure TTestModule.TestTryExcept;
  8125. begin
  8126. StartProgram(false);
  8127. Add([
  8128. 'type',
  8129. ' TObject = class end;',
  8130. ' Exception = class Msg: string; end;',
  8131. ' EInvalidCast = class(Exception) end;',
  8132. 'var vI: longint;',
  8133. 'begin',
  8134. ' try',
  8135. ' vi:=1;',
  8136. ' except',
  8137. ' vi:=2',
  8138. ' end;',
  8139. ' try',
  8140. ' vi:=3;',
  8141. ' except',
  8142. ' raise;',
  8143. ' end;',
  8144. ' try',
  8145. ' VI:=4;',
  8146. ' except',
  8147. ' on einvalidcast do',
  8148. ' raise;',
  8149. ' on E: exception do',
  8150. ' if e.msg='''' then',
  8151. ' raise e;',
  8152. ' else',
  8153. ' vi:=5',
  8154. ' end;',
  8155. ' try',
  8156. ' VI:=6;',
  8157. ' except',
  8158. ' on einvalidcast do ;',
  8159. ' end;',
  8160. '']);
  8161. ConvertProgram;
  8162. CheckSource('TestTryExcept',
  8163. LinesToStr([ // statements
  8164. 'rtl.createClass(this, "TObject", null, function () {',
  8165. ' this.$init = function () {',
  8166. ' };',
  8167. ' this.$final = function () {',
  8168. ' };',
  8169. '});',
  8170. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8171. ' this.$init = function () {',
  8172. ' $mod.TObject.$init.call(this);',
  8173. ' this.Msg = "";',
  8174. ' };',
  8175. '});',
  8176. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8177. '});',
  8178. 'this.vI = 0;'
  8179. ]),
  8180. LinesToStr([ // $mod.$main
  8181. 'try {',
  8182. ' $mod.vI = 1;',
  8183. '} catch ($e) {',
  8184. ' $mod.vI = 2;',
  8185. '};',
  8186. 'try {',
  8187. ' $mod.vI = 3;',
  8188. '} catch ($e) {',
  8189. ' throw $e;',
  8190. '};',
  8191. 'try {',
  8192. ' $mod.vI = 4;',
  8193. '} catch ($e) {',
  8194. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8195. ' throw $e',
  8196. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8197. ' var E = $e;',
  8198. ' if (E.Msg === "") throw E;',
  8199. ' } else {',
  8200. ' $mod.vI = 5;',
  8201. ' }',
  8202. '};',
  8203. 'try {',
  8204. ' $mod.vI = 6;',
  8205. '} catch ($e) {',
  8206. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8207. ' } else throw $e',
  8208. '};',
  8209. '']));
  8210. end;
  8211. procedure TTestModule.TestTryExcept_ReservedWords;
  8212. begin
  8213. StartProgram(false);
  8214. Add([
  8215. 'type',
  8216. ' TObject = class end;',
  8217. ' Exception = class',
  8218. ' Symbol: string;',
  8219. ' end;',
  8220. 'var &try: longint;',
  8221. 'begin',
  8222. ' try',
  8223. ' &try:=4;',
  8224. ' except',
  8225. ' on Error: exception do',
  8226. ' if errOR.symBol='''' then',
  8227. ' raise ERRor;',
  8228. ' end;',
  8229. '']);
  8230. ConvertProgram;
  8231. CheckSource('TestTryExcept_ReservedWords',
  8232. LinesToStr([ // statements
  8233. 'rtl.createClass(this, "TObject", null, function () {',
  8234. ' this.$init = function () {',
  8235. ' };',
  8236. ' this.$final = function () {',
  8237. ' };',
  8238. '});',
  8239. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8240. ' this.$init = function () {',
  8241. ' $mod.TObject.$init.call(this);',
  8242. ' this.Symbol = "";',
  8243. ' };',
  8244. '});',
  8245. 'this.Try = 0;',
  8246. '']),
  8247. LinesToStr([ // $mod.$main
  8248. 'try {',
  8249. ' $mod.Try = 4;',
  8250. '} catch ($e) {',
  8251. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8252. ' var error = $e;',
  8253. ' if (error.Symbol === "") throw error;',
  8254. ' } else throw $e',
  8255. '};',
  8256. '']));
  8257. end;
  8258. procedure TTestModule.TestIfThenRaiseElse;
  8259. begin
  8260. StartProgram(false);
  8261. Add([
  8262. 'type',
  8263. ' TObject = class',
  8264. ' constructor Create;',
  8265. ' end;',
  8266. 'constructor TObject.Create;',
  8267. 'begin',
  8268. 'end;',
  8269. 'var b: boolean;',
  8270. 'begin',
  8271. ' if b then',
  8272. ' raise TObject.Create',
  8273. ' else',
  8274. ' b:=false;',
  8275. '']);
  8276. ConvertProgram;
  8277. CheckSource('TestIfThenRaiseElse',
  8278. LinesToStr([ // statements
  8279. 'rtl.createClass(this, "TObject", null, function () {',
  8280. ' this.$init = function () {',
  8281. ' };',
  8282. ' this.$final = function () {',
  8283. ' };',
  8284. ' this.Create = function () {',
  8285. ' return this;',
  8286. ' };',
  8287. '});',
  8288. 'this.b = false;',
  8289. '']),
  8290. LinesToStr([ // $mod.$main
  8291. 'if ($mod.b) {',
  8292. ' throw $mod.TObject.$create("Create")}',
  8293. ' else $mod.b = false;',
  8294. '']));
  8295. end;
  8296. procedure TTestModule.TestCaseOf;
  8297. begin
  8298. StartProgram(false);
  8299. Add([
  8300. 'const e: longint; external name ''$e'';',
  8301. 'var vI: longint;',
  8302. 'begin',
  8303. ' case vi of',
  8304. ' 1: ;',
  8305. ' 2: vi:=3;',
  8306. ' e: ;',
  8307. ' else',
  8308. ' VI:=4',
  8309. ' end;']);
  8310. ConvertProgram;
  8311. CheckSource('TestCaseOf',
  8312. LinesToStr([ // statements
  8313. 'this.vI = 0;'
  8314. ]),
  8315. LinesToStr([ // $mod.$main
  8316. 'var $tmp = $mod.vI;',
  8317. 'if ($tmp === 1) {}',
  8318. 'else if ($tmp === 2) {',
  8319. ' $mod.vI = 3}',
  8320. ' else if ($tmp === $e) {}',
  8321. 'else {',
  8322. ' $mod.vI = 4;',
  8323. '};'
  8324. ]));
  8325. end;
  8326. procedure TTestModule.TestCaseOf_UseSwitch;
  8327. begin
  8328. StartProgram(false);
  8329. Converter.UseSwitchStatement:=true;
  8330. Add('var Vi: longint;');
  8331. Add('begin');
  8332. Add(' case vi of');
  8333. Add(' 1: ;');
  8334. Add(' 2: VI:=3;');
  8335. Add(' else');
  8336. Add(' vi:=4');
  8337. Add(' end;');
  8338. ConvertProgram;
  8339. CheckSource('TestCaseOf_UseSwitch',
  8340. LinesToStr([ // statements
  8341. 'this.Vi = 0;'
  8342. ]),
  8343. LinesToStr([ // $mod.$main
  8344. 'switch ($mod.Vi) {',
  8345. 'case 1:',
  8346. ' break;',
  8347. 'case 2:',
  8348. ' $mod.Vi = 3;',
  8349. ' break;',
  8350. 'default:',
  8351. ' $mod.Vi = 4;',
  8352. '};'
  8353. ]));
  8354. end;
  8355. procedure TTestModule.TestCaseOfNoElse;
  8356. begin
  8357. StartProgram(false);
  8358. Add('var Vi: longint;');
  8359. Add('begin');
  8360. Add(' case vi of');
  8361. Add(' 1: begin vi:=2; VI:=3; end;');
  8362. Add(' end;');
  8363. ConvertProgram;
  8364. CheckSource('TestCaseOfNoElse',
  8365. LinesToStr([ // statements
  8366. 'this.Vi = 0;'
  8367. ]),
  8368. LinesToStr([ // $mod.$main
  8369. 'var $tmp = $mod.Vi;',
  8370. 'if ($tmp === 1) {',
  8371. ' $mod.Vi = 2;',
  8372. ' $mod.Vi = 3;',
  8373. '};'
  8374. ]));
  8375. end;
  8376. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8377. begin
  8378. StartProgram(false);
  8379. Converter.UseSwitchStatement:=true;
  8380. Add('var vI: longint;');
  8381. Add('begin');
  8382. Add(' case vi of');
  8383. Add(' 1: begin VI:=2; vi:=3; end;');
  8384. Add(' end;');
  8385. ConvertProgram;
  8386. CheckSource('TestCaseOfNoElse_UseSwitch',
  8387. LinesToStr([ // statements
  8388. 'this.vI = 0;'
  8389. ]),
  8390. LinesToStr([ // $mod.$main
  8391. 'switch ($mod.vI) {',
  8392. 'case 1:',
  8393. ' $mod.vI = 2;',
  8394. ' $mod.vI = 3;',
  8395. ' break;',
  8396. '};'
  8397. ]));
  8398. end;
  8399. procedure TTestModule.TestCaseOfRange;
  8400. begin
  8401. StartProgram(false);
  8402. Add('var vI: longint;');
  8403. Add('begin');
  8404. Add(' case vi of');
  8405. Add(' 1..3: vi:=14;');
  8406. Add(' 4,5: vi:=16;');
  8407. Add(' 6..7,9..10: ;');
  8408. Add(' else ;');
  8409. Add(' end;');
  8410. ConvertProgram;
  8411. CheckSource('TestCaseOfRange',
  8412. LinesToStr([ // statements
  8413. 'this.vI = 0;'
  8414. ]),
  8415. LinesToStr([ // $mod.$main
  8416. 'var $tmp = $mod.vI;',
  8417. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8418. ' $mod.vI = 14',
  8419. '} else if (($tmp === 4) || ($tmp === 5)){',
  8420. ' $mod.vI = 16',
  8421. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8422. ]));
  8423. end;
  8424. procedure TTestModule.TestCaseOfString;
  8425. begin
  8426. StartProgram(false);
  8427. Add([
  8428. 'var s,h: string;',
  8429. 'begin',
  8430. ' case s of',
  8431. ' ''foo'': s:=h;',
  8432. ' ''a''..''z'': h:=s;',
  8433. ' ''ў'', ''ё'': ;',
  8434. ' ''Б''..''Я'': ;',
  8435. ' end;',
  8436. '']);
  8437. ConvertProgram;
  8438. CheckSource('TestCaseOfString',
  8439. LinesToStr([ // statements
  8440. 'this.s = "";',
  8441. 'this.h = "";',
  8442. '']),
  8443. LinesToStr([ // $mod.$main
  8444. 'var $tmp = $mod.s;',
  8445. 'if ($tmp === "foo") {',
  8446. ' $mod.s = $mod.h}',
  8447. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8448. ' $mod.h = $mod.s}',
  8449. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8450. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8451. '']));
  8452. end;
  8453. procedure TTestModule.TestCaseOfChar;
  8454. begin
  8455. StartProgram(false);
  8456. Add([
  8457. 'var s,h: char;',
  8458. 'begin',
  8459. ' case s of',
  8460. ' ''a''..''z'': h:=s;',
  8461. ' ''ä'': ;',
  8462. ' ''ў'', ''ё'': ;',
  8463. ' ''Б''..''Я'': ;',
  8464. ' end;',
  8465. '']);
  8466. ConvertProgram;
  8467. CheckSource('TestCaseOfString',
  8468. LinesToStr([ // statements
  8469. 'this.s = "";',
  8470. 'this.h = "";',
  8471. '']),
  8472. LinesToStr([ // $mod.$main
  8473. 'var $tmp = $mod.s;',
  8474. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8475. ' $mod.h = $mod.s}',
  8476. ' else if ($tmp === "ä") {}',
  8477. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8478. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8479. '']));
  8480. end;
  8481. procedure TTestModule.TestCaseOfExternalClassConst;
  8482. begin
  8483. StartProgram(false);
  8484. Add([
  8485. '{$modeswitch externalclass}',
  8486. 'type',
  8487. ' TBird = class external name ''Bird''',
  8488. ' const e: longint;',
  8489. ' end;',
  8490. 'var vI: longint;',
  8491. 'begin',
  8492. ' case vi of',
  8493. ' 1: vi:=3;',
  8494. ' TBird.e: ;',
  8495. ' end;']);
  8496. ConvertProgram;
  8497. CheckSource('TestCaseOfExternalClassConst',
  8498. LinesToStr([ // statements
  8499. 'this.vI = 0;'
  8500. ]),
  8501. LinesToStr([ // $mod.$main
  8502. 'var $tmp = $mod.vI;',
  8503. 'if ($tmp === 1) {',
  8504. ' $mod.vI = 3}',
  8505. ' else if ($tmp === Bird.e) ;'
  8506. ]));
  8507. end;
  8508. procedure TTestModule.TestDebugger;
  8509. begin
  8510. StartProgram(false);
  8511. Add([
  8512. 'procedure DoIt;',
  8513. 'begin',
  8514. ' deBugger;',
  8515. ' DeBugger();',
  8516. 'end;',
  8517. 'begin',
  8518. ' Debugger;']);
  8519. ConvertProgram;
  8520. CheckSource('TestDebugger',
  8521. LinesToStr([ // statements
  8522. 'this.DoIt = function () {',
  8523. ' debugger;',
  8524. ' debugger;',
  8525. '};',
  8526. '']),
  8527. LinesToStr([ // $mod.$main
  8528. 'debugger;',
  8529. '']));
  8530. end;
  8531. procedure TTestModule.TestArray_Dynamic;
  8532. begin
  8533. StartProgram(false);
  8534. Add([
  8535. 'type',
  8536. ' TArrayInt = array of longint;',
  8537. 'var',
  8538. ' Arr: TArrayInt;',
  8539. ' i: longint;',
  8540. ' b: boolean;',
  8541. 'begin',
  8542. ' SetLength(arr,3);',
  8543. ' arr[0]:=4;',
  8544. ' arr[1]:=length(arr)+arr[0];',
  8545. ' arr[i]:=5;',
  8546. ' arr[arr[i]]:=arr[6];',
  8547. ' i:=low(arr);',
  8548. ' i:=high(arr);',
  8549. ' b:=Assigned(arr);',
  8550. ' Arr:=default(TArrayInt);']);
  8551. ConvertProgram;
  8552. CheckSource('TestArray_Dynamic',
  8553. LinesToStr([ // statements
  8554. 'this.Arr = [];',
  8555. 'this.i = 0;',
  8556. 'this.b = false;'
  8557. ]),
  8558. LinesToStr([ // $mod.$main
  8559. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8560. '$mod.Arr[0] = 4;',
  8561. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8562. '$mod.Arr[$mod.i] = 5;',
  8563. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8564. '$mod.i = 0;',
  8565. '$mod.i = rtl.length($mod.Arr) - 1;',
  8566. '$mod.b = rtl.length($mod.Arr) > 0;',
  8567. '$mod.Arr = [];',
  8568. '']));
  8569. end;
  8570. procedure TTestModule.TestArray_Dynamic_Nil;
  8571. begin
  8572. StartProgram(false);
  8573. Add('type');
  8574. Add(' TArrayInt = array of longint;');
  8575. Add('var');
  8576. Add(' Arr: TArrayInt;');
  8577. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8578. Add('begin');
  8579. Add(' arr:=nil;');
  8580. Add(' if arr=nil then;');
  8581. Add(' if nil=arr then;');
  8582. Add(' if arr<>nil then;');
  8583. Add(' if nil<>arr then;');
  8584. Add(' DoIt(nil,nil);');
  8585. ConvertProgram;
  8586. CheckSource('TestArray_Dynamic',
  8587. LinesToStr([ // statements
  8588. 'this.Arr = [];',
  8589. 'this.DoIt = function(i,j){',
  8590. '};'
  8591. ]),
  8592. LinesToStr([ // $mod.$main
  8593. '$mod.Arr = [];',
  8594. 'if (rtl.length($mod.Arr) === 0) ;',
  8595. 'if (rtl.length($mod.Arr) === 0) ;',
  8596. 'if (rtl.length($mod.Arr) > 0) ;',
  8597. 'if (rtl.length($mod.Arr) > 0) ;',
  8598. '$mod.DoIt([],[]);',
  8599. '']));
  8600. end;
  8601. procedure TTestModule.TestArray_DynMultiDimensional;
  8602. begin
  8603. StartProgram(false);
  8604. Add([
  8605. 'type',
  8606. ' TArrayInt = array of longint;',
  8607. ' TArrayArrayInt = array of TArrayInt;',
  8608. 'var',
  8609. ' Arr: TArrayInt;',
  8610. ' Arr2: TArrayArrayInt;',
  8611. ' i: longint;',
  8612. 'begin',
  8613. ' arr2:=nil;',
  8614. ' if arr2=nil then;',
  8615. ' if nil=arr2 then;',
  8616. ' i:=low(arr2);',
  8617. ' i:=low(arr2[1]);',
  8618. ' i:=high(arr2);',
  8619. ' i:=high(arr2[2]);',
  8620. ' arr2[3]:=arr;',
  8621. ' arr2[4][5]:=i;',
  8622. ' i:=arr2[6][7];',
  8623. ' arr2[8,9]:=i;',
  8624. ' i:=arr2[10,11];',
  8625. ' SetLength(arr2,14);',
  8626. ' SetLength(arr2[15],16);']);
  8627. ConvertProgram;
  8628. CheckSource('TestArray_Dynamic',
  8629. LinesToStr([ // statements
  8630. 'this.Arr = [];',
  8631. 'this.Arr2 = [];',
  8632. 'this.i = 0;'
  8633. ]),
  8634. LinesToStr([ // $mod.$main
  8635. '$mod.Arr2 = [];',
  8636. 'if (rtl.length($mod.Arr2) === 0) ;',
  8637. 'if (rtl.length($mod.Arr2) === 0) ;',
  8638. '$mod.i = 0;',
  8639. '$mod.i = 0;',
  8640. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8641. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8642. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8643. '$mod.Arr2[4][5] = $mod.i;',
  8644. '$mod.i = $mod.Arr2[6][7];',
  8645. '$mod.Arr2[8][9] = $mod.i;',
  8646. '$mod.i = $mod.Arr2[10][11];',
  8647. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8648. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8649. '']));
  8650. end;
  8651. procedure TTestModule.TestArray_DynamicAssign;
  8652. begin
  8653. StartProgram(false);
  8654. Add([
  8655. 'type',
  8656. ' TArrayInt = array of longint;',
  8657. ' TArrayArrayInt = array of TArrayInt;',
  8658. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8659. 'begin',
  8660. 'end;',
  8661. 'procedure Fly(var a: TArrayInt);',
  8662. 'begin',
  8663. 'end;',
  8664. 'var',
  8665. ' Arr: TArrayInt;',
  8666. ' Arr2: TArrayArrayInt;',
  8667. 'begin',
  8668. ' arr:=nil;',
  8669. ' arr2:=nil;',
  8670. ' arr2[1]:=nil;',
  8671. ' arr2[2]:=arr;',
  8672. ' Run(arr,arr,arr);',
  8673. ' Fly(arr);',
  8674. ' Run(arr2[4],arr2[5],arr2[6]);',
  8675. ' Fly(arr2[7]);',
  8676. '']);
  8677. ConvertProgram;
  8678. CheckSource('TestArray_DynamicAssign',
  8679. LinesToStr([ // statements
  8680. 'this.Run = function (a, b, c) {',
  8681. '};',
  8682. 'this.Fly = function (a) {',
  8683. '};',
  8684. 'this.Arr = [];',
  8685. 'this.Arr2 = [];',
  8686. '']),
  8687. LinesToStr([ // $mod.$main
  8688. '$mod.Arr = [];',
  8689. '$mod.Arr2 = [];',
  8690. '$mod.Arr2[1] = [];',
  8691. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8692. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8693. '$mod.Fly({',
  8694. ' p: $mod,',
  8695. ' get: function () {',
  8696. ' return this.p.Arr;',
  8697. ' },',
  8698. ' set: function (v) {',
  8699. ' this.p.Arr = v;',
  8700. ' }',
  8701. '});',
  8702. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8703. '$mod.Fly({',
  8704. ' a: 7,',
  8705. ' p: $mod.Arr2,',
  8706. ' get: function () {',
  8707. ' return this.p[this.a];',
  8708. ' },',
  8709. ' set: function (v) {',
  8710. ' this.p[this.a] = v;',
  8711. ' }',
  8712. '});',
  8713. '']));
  8714. end;
  8715. procedure TTestModule.TestArray_StaticInt;
  8716. begin
  8717. StartProgram(false);
  8718. Add('type');
  8719. Add(' TArrayInt = array[2..4] of longint;');
  8720. Add('var');
  8721. Add(' Arr: TArrayInt;');
  8722. Add(' Arr2: TArrayInt = (5,6,7);');
  8723. Add(' i: longint;');
  8724. Add(' b: boolean;');
  8725. Add('begin');
  8726. Add(' arr[2]:=4;');
  8727. Add(' arr[3]:=arr[2]+arr[3];');
  8728. Add(' arr[i]:=5;');
  8729. Add(' arr[arr[i]]:=arr[high(arr)];');
  8730. Add(' i:=low(arr);');
  8731. Add(' i:=high(arr);');
  8732. Add(' b:=arr[2]=arr[3];');
  8733. Add(' arr:=default(TArrayInt);');
  8734. ConvertProgram;
  8735. CheckSource('TestArray_StaticInt',
  8736. LinesToStr([ // statements
  8737. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8738. 'this.Arr2 = [5, 6, 7];',
  8739. 'this.i = 0;',
  8740. 'this.b = false;'
  8741. ]),
  8742. LinesToStr([ // $mod.$main
  8743. '$mod.Arr[0] = 4;',
  8744. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8745. '$mod.Arr[$mod.i-2] = 5;',
  8746. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8747. '$mod.i = 2;',
  8748. '$mod.i = 4;',
  8749. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8750. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8751. '']));
  8752. end;
  8753. procedure TTestModule.TestArray_StaticBool;
  8754. begin
  8755. StartProgram(false);
  8756. Add('type');
  8757. Add(' TBools = array[boolean] of boolean;');
  8758. Add(' TBool2 = array[true..true] of boolean;');
  8759. Add('var');
  8760. Add(' Arr: TBools;');
  8761. Add(' Arr2: TBool2;');
  8762. Add(' Arr3: TBools = (true,false);');
  8763. Add(' b: boolean;');
  8764. Add('begin');
  8765. Add(' b:=low(arr);');
  8766. Add(' b:=high(arr);');
  8767. Add(' arr[true]:=false;');
  8768. Add(' arr[false]:=arr[b] or arr[true];');
  8769. Add(' arr[b]:=true;');
  8770. Add(' arr[arr[b]]:=arr[high(arr)];');
  8771. Add(' b:=arr[false]=arr[true];');
  8772. Add(' b:=low(arr2);');
  8773. Add(' b:=high(arr2);');
  8774. Add(' arr2[true]:=true;');
  8775. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8776. Add(' arr2[b]:=false;');
  8777. ConvertProgram;
  8778. CheckSource('TestArray_StaticBool',
  8779. LinesToStr([ // statements
  8780. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8781. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8782. 'this.Arr3 = [true, false];',
  8783. 'this.b = false;'
  8784. ]),
  8785. LinesToStr([ // $mod.$main
  8786. '$mod.b = false;',
  8787. '$mod.b = true;',
  8788. '$mod.Arr[1] = false;',
  8789. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8790. '$mod.Arr[+$mod.b] = true;',
  8791. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8792. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8793. '$mod.b = true;',
  8794. '$mod.b = true;',
  8795. '$mod.Arr2[0] = true;',
  8796. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8797. '$mod.Arr2[1-$mod.b] = false;',
  8798. '']));
  8799. end;
  8800. procedure TTestModule.TestArray_StaticChar;
  8801. begin
  8802. StartProgram(false);
  8803. Add([
  8804. 'type',
  8805. ' TChars = array[char] of char;',
  8806. ' TChars2 = array[''a''..''z''] of char;',
  8807. 'var',
  8808. ' Arr: TChars;',
  8809. ' Arr2: TChars2;',
  8810. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8811. ' Arr4: array[11..13] of char = ''pas'';',
  8812. ' Arr5: array[21..22] of char = ''äö'';',
  8813. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8814. ' c: char;',
  8815. ' b: boolean;',
  8816. 'begin',
  8817. ' c:=low(arr);',
  8818. ' c:=high(arr);',
  8819. ' arr[''B'']:=''a'';',
  8820. ' arr[''D'']:=arr[c];',
  8821. ' arr[c]:=arr[''d''];',
  8822. ' arr[arr[c]]:=arr[high(arr)];',
  8823. ' b:=arr[low(arr)]=arr[''e''];',
  8824. ' c:=low(arr2);',
  8825. ' c:=high(arr2);',
  8826. ' arr2[''b'']:=''f'';',
  8827. ' arr2[''a'']:=arr2[c];',
  8828. ' arr2[c]:=arr2[''g''];']);
  8829. ConvertProgram;
  8830. CheckSource('TestArray_StaticChar',
  8831. LinesToStr([ // statements
  8832. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8833. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8834. 'this.Arr3 = ["p", "a", "s"];',
  8835. 'this.Arr4 = ["p", "a", "s"];',
  8836. 'this.Arr5 = ["ä", "ö"];',
  8837. 'this.Arr6 = ["ä", "ö"];',
  8838. 'this.c = "";',
  8839. 'this.b = false;',
  8840. '']),
  8841. LinesToStr([ // $mod.$main
  8842. '$mod.c = "\x00";',
  8843. '$mod.c = "\uFFFF";',
  8844. '$mod.Arr[66] = "a";',
  8845. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8846. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8847. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8848. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8849. '$mod.c = "a";',
  8850. '$mod.c = "z";',
  8851. '$mod.Arr2[1] = "f";',
  8852. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8853. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8854. '']));
  8855. end;
  8856. procedure TTestModule.TestArray_StaticMultiDim;
  8857. begin
  8858. StartProgram(false);
  8859. Add([
  8860. 'type',
  8861. ' TArrayInt = array[1..3] of longint;',
  8862. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8863. 'var',
  8864. ' Arr: TArrayInt;',
  8865. ' Arr2: TArrayArrayInt;',
  8866. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8867. ' i: longint;',
  8868. 'begin',
  8869. ' i:=low(arr);',
  8870. ' i:=low(arr2);',
  8871. ' i:=low(arr2[5]);',
  8872. ' i:=high(arr);',
  8873. ' i:=high(arr2);',
  8874. ' i:=high(arr2[6]);',
  8875. ' arr2[5]:=arr;',
  8876. ' arr2[6][2]:=i;',
  8877. ' i:=arr2[6][3];',
  8878. ' arr2[6,3]:=i;',
  8879. ' i:=arr2[5,2];',
  8880. ' arr2:=arr2;',// clone multi dim static array
  8881. //' arr3:=arr3;',// clone anonymous multi dim static array
  8882. '']);
  8883. ConvertProgram;
  8884. CheckSource('TestArray_StaticMultiDim',
  8885. LinesToStr([ // statements
  8886. 'this.TArrayArrayInt$clone = function (a) {',
  8887. ' var r = [];',
  8888. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8889. ' return r;',
  8890. '};',
  8891. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8892. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8893. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8894. 'this.i = 0;'
  8895. ]),
  8896. LinesToStr([ // $mod.$main
  8897. '$mod.i = 1;',
  8898. '$mod.i = 5;',
  8899. '$mod.i = 1;',
  8900. '$mod.i = 3;',
  8901. '$mod.i = 6;',
  8902. '$mod.i = 3;',
  8903. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8904. '$mod.Arr2[1][1] = $mod.i;',
  8905. '$mod.i = $mod.Arr2[1][2];',
  8906. '$mod.Arr2[1][2] = $mod.i;',
  8907. '$mod.i = $mod.Arr2[0][1];',
  8908. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8909. '']));
  8910. end;
  8911. procedure TTestModule.TestArray_StaticInFunction;
  8912. begin
  8913. StartProgram(false);
  8914. Add([
  8915. 'const TArrayInt = 3;',
  8916. 'const TArrayArrayInt = 4;',
  8917. 'procedure DoIt;',
  8918. 'type',
  8919. ' TArrayInt = array[1..3] of longint;',
  8920. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8921. 'var',
  8922. ' Arr: TArrayInt;',
  8923. ' Arr2: TArrayArrayInt;',
  8924. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8925. ' i: longint;',
  8926. 'begin',
  8927. ' arr2[5]:=arr;',
  8928. ' arr2:=arr2;',// clone multi dim static array
  8929. 'end;',
  8930. 'begin',
  8931. '']);
  8932. ConvertProgram;
  8933. CheckSource('TestArray_StaticInFunction',
  8934. LinesToStr([ // statements
  8935. 'this.TArrayInt = 3;',
  8936. 'this.TArrayArrayInt = 4;',
  8937. 'var TArrayArrayInt$1$clone = function (a) {',
  8938. ' var r = [];',
  8939. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8940. ' return r;',
  8941. '};',
  8942. 'this.DoIt = function () {',
  8943. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8944. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8945. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8946. ' var i = 0;',
  8947. ' Arr2[0] = Arr.slice(0);',
  8948. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8949. '};',
  8950. '']),
  8951. LinesToStr([ // $mod.$main
  8952. '']));
  8953. end;
  8954. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8955. begin
  8956. StartProgram(false);
  8957. Add([
  8958. 'type',
  8959. ' TArrayInt = array[1..3,1..2] of longint;',
  8960. 'var',
  8961. ' a,b: TArrayInt;',
  8962. 'begin',
  8963. ' if a=b then ;',
  8964. '']);
  8965. SetExpectedPasResolverError('compare static array is not supported',
  8966. nXIsNotSupported);
  8967. ConvertProgram;
  8968. end;
  8969. procedure TTestModule.TestArrayOfRecord;
  8970. begin
  8971. StartProgram(false);
  8972. Add([
  8973. 'type',
  8974. ' TRec = record',
  8975. ' Int: longint;',
  8976. ' end;',
  8977. ' TArrayRec = array of TRec;',
  8978. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8979. 'begin',
  8980. 'end;',
  8981. 'var',
  8982. ' Arr: TArrayRec;',
  8983. ' r: TRec;',
  8984. ' i: longint;',
  8985. 'begin',
  8986. ' SetLength(arr,3);',
  8987. ' arr[0].int:=4;',
  8988. ' arr[1].int:=length(arr)+arr[2].int;',
  8989. ' arr[arr[i].int].int:=arr[5].int;',
  8990. ' arr[7]:=r;',
  8991. ' r:=arr[8];',
  8992. ' i:=low(arr);',
  8993. ' i:=high(arr);',
  8994. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8995. ConvertProgram;
  8996. CheckSource('TestArrayOfRecord',
  8997. LinesToStr([ // statements
  8998. 'rtl.recNewT(this, "TRec", function () {',
  8999. ' this.Int = 0;',
  9000. ' this.$eq = function (b) {',
  9001. ' return this.Int === b.Int;',
  9002. ' };',
  9003. ' this.$assign = function (s) {',
  9004. ' this.Int = s.Int;',
  9005. ' return this;',
  9006. ' };',
  9007. '});',
  9008. 'this.DoIt = function (vd, vc, vv) {',
  9009. '};',
  9010. 'this.Arr = [];',
  9011. 'this.r = this.TRec.$new();',
  9012. 'this.i = 0;'
  9013. ]),
  9014. LinesToStr([ // $mod.$main
  9015. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9016. '$mod.Arr[0].Int = 4;',
  9017. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9018. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9019. '$mod.Arr[7].$assign($mod.r);',
  9020. '$mod.r.$assign($mod.Arr[8]);',
  9021. '$mod.i = 0;',
  9022. '$mod.i = rtl.length($mod.Arr)-1;',
  9023. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9024. '']));
  9025. end;
  9026. procedure TTestModule.TestArray_StaticRecord;
  9027. begin
  9028. StartProgram(false);
  9029. Add([
  9030. 'type',
  9031. ' TRec = record',
  9032. ' Int: longint;',
  9033. ' end;',
  9034. ' TArrayRec = array[1..2] of TRec;',
  9035. 'var',
  9036. ' Arr: TArrayRec;',
  9037. 'begin',
  9038. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9039. '']);
  9040. ConvertProgram;
  9041. CheckSource('TestArray_StaticRecord',
  9042. LinesToStr([ // statements
  9043. 'rtl.recNewT(this, "TRec", function () {',
  9044. ' this.Int = 0;',
  9045. ' this.$eq = function (b) {',
  9046. ' return this.Int === b.Int;',
  9047. ' };',
  9048. ' this.$assign = function (s) {',
  9049. ' this.Int = s.Int;',
  9050. ' return this;',
  9051. ' };',
  9052. '});',
  9053. 'this.TArrayRec$clone = function (a) {',
  9054. ' var r = [];',
  9055. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9056. ' return r;',
  9057. '};',
  9058. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9059. '']),
  9060. LinesToStr([ // $mod.$main
  9061. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9062. end;
  9063. procedure TTestModule.TestArrayOfSet;
  9064. begin
  9065. StartProgram(false);
  9066. Add([
  9067. 'type',
  9068. ' TFlag = (big,small);',
  9069. ' TSetOfFlag = set of tflag;',
  9070. ' TArrayFlag = array of TSetOfFlag;',
  9071. 'procedure DoIt(const a: Tarrayflag);',
  9072. 'begin',
  9073. 'end;',
  9074. 'var',
  9075. ' f: TFlag;',
  9076. ' s: TSetOfFlag;',
  9077. ' Arr: TArrayFlag;',
  9078. ' i: longint;',
  9079. 'begin',
  9080. ' SetLength(arr,3);',
  9081. ' arr[0]:=s;',
  9082. ' arr[1]:=[big];',
  9083. ' arr[2]:=[big]+s;',
  9084. ' arr[3]:=s+[big];',
  9085. ' arr[4]:=arr[5];',
  9086. ' s:=arr[6];',
  9087. ' i:=low(arr);',
  9088. ' i:=high(arr);',
  9089. ' DoIt(arr);',
  9090. ' DoIt([s]);',
  9091. ' DoIt([[],s]);',
  9092. ' DoIt([s,[]]);',
  9093. '']);
  9094. ConvertProgram;
  9095. CheckSource('TestArrayOfSet',
  9096. LinesToStr([ // statements
  9097. 'this.TFlag = {',
  9098. ' "0": "big",',
  9099. ' big: 0,',
  9100. ' "1": "small",',
  9101. ' small: 1',
  9102. '};',
  9103. 'this.DoIt = function (a) {',
  9104. '};',
  9105. 'this.f = 0;',
  9106. 'this.s = {};',
  9107. 'this.Arr = [];',
  9108. 'this.i = 0;',
  9109. '']),
  9110. LinesToStr([ // $mod.$main
  9111. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9112. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9113. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9114. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9115. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9116. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9117. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9118. '$mod.i = 0;',
  9119. '$mod.i = rtl.length($mod.Arr) - 1;',
  9120. '$mod.DoIt($mod.Arr);',
  9121. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9122. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9123. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9124. '']));
  9125. end;
  9126. procedure TTestModule.TestArray_DynAsParam;
  9127. begin
  9128. StartProgram(false);
  9129. Add([
  9130. 'type integer = longint;',
  9131. 'type TArrInt = array of integer;',
  9132. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9133. 'var vJ: TArrInt;',
  9134. 'begin',
  9135. ' vg:=vg;',
  9136. ' vj:=vh;',
  9137. ' vi:=vi;',
  9138. ' doit(vg,vg,vg);',
  9139. ' doit(vh,vh,vj);',
  9140. ' doit(vi,vi,vi);',
  9141. ' doit(vj,vj,vj);',
  9142. 'end;',
  9143. 'var i: TArrInt;',
  9144. 'begin',
  9145. ' doit(i,i,i);']);
  9146. ConvertProgram;
  9147. CheckSource('TestArray_DynAsParams',
  9148. LinesToStr([ // statements
  9149. 'this.DoIt = function (vG,vH,vI) {',
  9150. ' var vJ = [];',
  9151. ' vG = rtl.arrayRef(vG);',
  9152. ' vJ = rtl.arrayRef(vH);',
  9153. ' vI.set(rtl.arrayRef(vI.get()));',
  9154. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9155. ' get: function () {',
  9156. ' return vG;',
  9157. ' },',
  9158. ' set: function (v) {',
  9159. ' vG = v;',
  9160. ' }',
  9161. ' });',
  9162. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9163. ' get: function () {',
  9164. ' return vJ;',
  9165. ' },',
  9166. ' set: function (v) {',
  9167. ' vJ = v;',
  9168. ' }',
  9169. ' });',
  9170. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9171. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9172. ' get: function () {',
  9173. ' return vJ;',
  9174. ' },',
  9175. ' set: function (v) {',
  9176. ' vJ = v;',
  9177. ' }',
  9178. ' });',
  9179. '};',
  9180. 'this.i = [];'
  9181. ]),
  9182. LinesToStr([
  9183. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9184. ' p: $mod,',
  9185. ' get: function () {',
  9186. ' return this.p.i;',
  9187. ' },',
  9188. ' set: function (v) {',
  9189. ' this.p.i = v;',
  9190. ' }',
  9191. '});'
  9192. ]));
  9193. end;
  9194. procedure TTestModule.TestArray_StaticAsParam;
  9195. begin
  9196. StartProgram(false);
  9197. Add([
  9198. 'type integer = longint;',
  9199. 'type TArrInt = array[1..2] of integer;',
  9200. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9201. 'var vJ: TArrInt;',
  9202. 'begin',
  9203. ' vg:=vg;',
  9204. ' vj:=vh;',
  9205. ' vi:=vi;',
  9206. ' doit(vg,vg,vg);',
  9207. ' doit(vh,vh,vj);',
  9208. ' doit(vi,vi,vi);',
  9209. ' doit(vj,vj,vj);',
  9210. 'end;',
  9211. 'var i: TArrInt;',
  9212. 'begin',
  9213. ' doit(i,i,i);']);
  9214. ConvertProgram;
  9215. CheckSource('TestArray_StaticAsParams',
  9216. LinesToStr([ // statements
  9217. 'this.DoIt = function (vG,vH,vI) {',
  9218. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9219. ' vG = vG.slice(0);',
  9220. ' vJ = vH.slice(0);',
  9221. ' vI.set(vI.get().slice(0));',
  9222. ' $mod.DoIt(vG.slice(0), vG, {',
  9223. ' get: function () {',
  9224. ' return vG;',
  9225. ' },',
  9226. ' set: function (v) {',
  9227. ' vG = v;',
  9228. ' }',
  9229. ' });',
  9230. ' $mod.DoIt(vH.slice(0), vH, {',
  9231. ' get: function () {',
  9232. ' return vJ;',
  9233. ' },',
  9234. ' set: function (v) {',
  9235. ' vJ = v;',
  9236. ' }',
  9237. ' });',
  9238. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9239. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9240. ' get: function () {',
  9241. ' return vJ;',
  9242. ' },',
  9243. ' set: function (v) {',
  9244. ' vJ = v;',
  9245. ' }',
  9246. ' });',
  9247. '};',
  9248. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9249. ]),
  9250. LinesToStr([
  9251. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9252. ' p: $mod,',
  9253. ' get: function () {',
  9254. ' return this.p.i;',
  9255. ' },',
  9256. ' set: function (v) {',
  9257. ' this.p.i = v;',
  9258. ' }',
  9259. '});'
  9260. ]));
  9261. end;
  9262. procedure TTestModule.TestArrayElement_AsParams;
  9263. begin
  9264. StartProgram(false);
  9265. Add('type integer = longint;');
  9266. Add('type TArrayInt = array of integer;');
  9267. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9268. Add('var vJ: tarrayint;');
  9269. Add('begin');
  9270. Add(' vi:=vi;');
  9271. Add(' doit(vi,vi,vi);');
  9272. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9273. Add('end;');
  9274. Add('var a: TArrayInt;');
  9275. Add('begin');
  9276. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9277. ConvertProgram;
  9278. CheckSource('TestArrayElement_AsParams',
  9279. LinesToStr([ // statements
  9280. 'this.DoIt = function (vG,vH,vI) {',
  9281. ' var vJ = [];',
  9282. ' vI.set(vI.get());',
  9283. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9284. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9285. ' a:1+3,',
  9286. ' p:vJ,',
  9287. ' get: function () {',
  9288. ' return this.p[this.a];',
  9289. ' },',
  9290. ' set: function (v) {',
  9291. ' this.p[this.a] = v;',
  9292. ' }',
  9293. ' });',
  9294. '};',
  9295. 'this.a = [];'
  9296. ]),
  9297. LinesToStr([
  9298. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9299. ' a: 1+6,',
  9300. ' p: $mod.a,',
  9301. ' get: function () {',
  9302. ' return this.p[this.a];',
  9303. ' },',
  9304. ' set: function (v) {',
  9305. ' this.p[this.a] = v;',
  9306. ' }',
  9307. '});'
  9308. ]));
  9309. end;
  9310. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9311. begin
  9312. StartProgram(false);
  9313. Add('type Integer = longint;');
  9314. Add('type TArrayInt = array of integer;');
  9315. Add('function GetArr(vB: integer = 0): tarrayint;');
  9316. Add('begin');
  9317. Add('end;');
  9318. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9319. Add('begin');
  9320. Add('end;');
  9321. Add('begin');
  9322. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9323. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9324. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9325. ConvertProgram;
  9326. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9327. LinesToStr([ // statements
  9328. 'this.GetArr = function (vB) {',
  9329. ' var Result = [];',
  9330. ' return Result;',
  9331. '};',
  9332. 'this.DoIt = function (vG,vH,vI) {',
  9333. '};'
  9334. ]),
  9335. LinesToStr([
  9336. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9337. ' a: 1+3,',
  9338. ' p: $mod.GetArr(0),',
  9339. ' get: function () {',
  9340. ' return this.p[this.a];',
  9341. ' },',
  9342. ' set: function (v) {',
  9343. ' this.p[this.a] = v;',
  9344. ' }',
  9345. '});',
  9346. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9347. ' a: 2+3,',
  9348. ' p: $mod.GetArr(0),',
  9349. ' get: function () {',
  9350. ' return this.p[this.a];',
  9351. ' },',
  9352. ' set: function (v) {',
  9353. ' this.p[this.a] = v;',
  9354. ' }',
  9355. '});',
  9356. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9357. ' a: 3+3,',
  9358. ' p: $mod.GetArr(9),',
  9359. ' get: function () {',
  9360. ' return this.p[this.a];',
  9361. ' },',
  9362. ' set: function (v) {',
  9363. ' this.p[this.a] = v;',
  9364. ' }',
  9365. '});',
  9366. '']));
  9367. end;
  9368. procedure TTestModule.TestArrayEnumTypeRange;
  9369. begin
  9370. StartProgram(false);
  9371. Add([
  9372. 'type',
  9373. ' TEnum = (red,blue);',
  9374. ' TEnumArray = array[TEnum] of longint;',
  9375. 'var',
  9376. ' e: TEnum;',
  9377. ' i: longint;',
  9378. ' a: TEnumArray;',
  9379. ' numbers: TEnumArray = (1,2);',
  9380. ' names: array[TEnum] of string = (''red'',''blue'');',
  9381. 'begin',
  9382. ' e:=low(a);',
  9383. ' e:=high(a);',
  9384. ' i:=a[red];',
  9385. ' a[e]:=a[e];']);
  9386. ConvertProgram;
  9387. CheckSource('TestArrayEnumTypeRange',
  9388. LinesToStr([ // statements
  9389. ' this.TEnum = {',
  9390. ' "0": "red",',
  9391. ' red: 0,',
  9392. ' "1": "blue",',
  9393. ' blue: 1',
  9394. '};',
  9395. 'this.e = 0;',
  9396. 'this.i = 0;',
  9397. 'this.a = rtl.arraySetLength(null,0,2);',
  9398. 'this.numbers = [1, 2];',
  9399. 'this.names = ["red", "blue"];',
  9400. '']),
  9401. LinesToStr([ // $mod.$main
  9402. '$mod.e = $mod.TEnum.red;',
  9403. '$mod.e = $mod.TEnum.blue;',
  9404. '$mod.i = $mod.a[$mod.TEnum.red];',
  9405. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9406. '']));
  9407. end;
  9408. procedure TTestModule.TestArray_SetLengthOutArg;
  9409. begin
  9410. StartProgram(false);
  9411. Add([
  9412. 'type TArrInt = array of longint;',
  9413. 'procedure DoIt(out a: TArrInt);',
  9414. 'begin',
  9415. ' SetLength(a,2);',
  9416. 'end;',
  9417. 'begin',
  9418. '']);
  9419. ConvertProgram;
  9420. CheckSource('TestArray_SetLengthOutArg',
  9421. LinesToStr([ // statements
  9422. 'this.DoIt = function (a) {',
  9423. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9424. '};',
  9425. '']),
  9426. LinesToStr([
  9427. '']));
  9428. end;
  9429. procedure TTestModule.TestArray_SetLengthProperty;
  9430. begin
  9431. StartProgram(false);
  9432. Add('type');
  9433. Add(' TArrInt = array of longint;');
  9434. Add(' TObject = class');
  9435. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9436. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9437. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9438. Add(' end;');
  9439. Add('var Obj: TObject;');
  9440. Add('begin');
  9441. Add(' SetLength(Obj.Colors,2);');
  9442. ConvertProgram;
  9443. CheckSource('TestArray_SetLengthProperty',
  9444. LinesToStr([ // statements
  9445. 'rtl.createClass(this, "TObject", null, function () {',
  9446. ' this.$init = function () {',
  9447. ' };',
  9448. ' this.$final = function () {',
  9449. ' };',
  9450. '});',
  9451. 'this.Obj = null;',
  9452. '']),
  9453. LinesToStr([
  9454. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9455. '']));
  9456. end;
  9457. procedure TTestModule.TestArray_SetLengthMultiDim;
  9458. begin
  9459. StartProgram(false);
  9460. Add([
  9461. 'type',
  9462. ' TArrArrInt = array of array of longint;',
  9463. ' TArrStaInt = array of array[1..2] of longint;',
  9464. 'var',
  9465. ' a: TArrArrInt;',
  9466. ' b: TArrStaInt;',
  9467. 'begin',
  9468. ' SetLength(a,2);',
  9469. ' SetLength(a,3,4);',
  9470. ' SetLength(b,5);',
  9471. '']);
  9472. ConvertProgram;
  9473. CheckSource('TestArray_SetLengthMultiDim',
  9474. LinesToStr([ // statements
  9475. 'this.a = [];',
  9476. 'this.b = [];',
  9477. '']),
  9478. LinesToStr([
  9479. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9480. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9481. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9482. '']));
  9483. end;
  9484. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9485. begin
  9486. StartProgram(false);
  9487. Add([
  9488. 'type',
  9489. ' TStaArr1 = array[1..3] of boolean;',
  9490. //' TStaArr2 = array[5..6] of TStaArr1;',
  9491. ' TDynArr1StaArr1 = array of TStaArr1;',
  9492. //' TDynArr1StaArr2 = array of TStaArr2;',
  9493. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9494. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9495. 'var',
  9496. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9497. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9498. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9499. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9500. 'begin',
  9501. ' SetLength(DynArr1StaArr1,11);',
  9502. ' SetLength(DynArr2StaArr1,12);',
  9503. ' SetLength(DynArr2StaArr1[13],14);',
  9504. ' SetLength(DynArr2StaArr1,15,16);',
  9505. //' SetLength(DynArr1StaArr2,21);',
  9506. //' SetLength(DynArr2StaArr2,22);',
  9507. //' SetLength(DynArr2StaArr2[23],24);',
  9508. //' SetLength(DynArr2StaArr2,25,26);',
  9509. '']);
  9510. ConvertProgram;
  9511. CheckSource('TestArray_DynOfStatic',
  9512. LinesToStr([ // statements
  9513. 'this.DynArr1StaArr1 = [];',
  9514. 'this.DynArr2StaArr1 = [];',
  9515. '']),
  9516. LinesToStr([ // $mod.$main
  9517. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9518. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9519. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9520. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9521. ' $mod.DynArr2StaArr1,',
  9522. ' false,',
  9523. ' 15,',
  9524. ' 16,',
  9525. ' "s",',
  9526. ' 3',
  9527. ');',
  9528. '']));
  9529. end;
  9530. procedure TTestModule.TestArray_OpenArrayOfString;
  9531. begin
  9532. StartProgram(false);
  9533. Add('procedure DoIt(const a: array of String);');
  9534. Add('var');
  9535. Add(' i: longint;');
  9536. Add(' s: string;');
  9537. Add('begin');
  9538. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9539. Add('end;');
  9540. Add('var s: string;');
  9541. Add('begin');
  9542. Add(' DoIt([]);');
  9543. Add(' DoIt([s,''foo'','''',s+s]);');
  9544. ConvertProgram;
  9545. CheckSource('TestArray_OpenArrayOfString',
  9546. LinesToStr([ // statements
  9547. 'this.DoIt = function (a) {',
  9548. ' var i = 0;',
  9549. ' var s = "";',
  9550. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9551. ' i = $l;',
  9552. ' s = a[rtl.length(a) - i - 1];',
  9553. ' };',
  9554. '};',
  9555. 'this.s = "";',
  9556. '']),
  9557. LinesToStr([
  9558. '$mod.DoIt([]);',
  9559. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9560. '']));
  9561. end;
  9562. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9563. begin
  9564. StartProgram(false);
  9565. Add([
  9566. 'type TArr = array of char;',
  9567. 'var',
  9568. ' c: char;',
  9569. ' s: string;',
  9570. ' a: TArr;',
  9571. 'procedure Run(const a: array of char);',
  9572. 'begin',
  9573. ' Run(c);',
  9574. ' Run(s);',
  9575. 'end;',
  9576. 'begin',
  9577. ' a:=c;',
  9578. ' a:=s;',
  9579. ' a:=#13;',
  9580. ' a:=''Foo'';',
  9581. ' Run(c);',
  9582. ' Run(s);',
  9583. '']);
  9584. ConvertProgram;
  9585. CheckSource('TestArray_ArrayOfCharAssignString',
  9586. LinesToStr([ // statements
  9587. 'this.c = "";',
  9588. 'this.s = "";',
  9589. 'this.a = [];',
  9590. 'this.Run = function (a) {',
  9591. ' $mod.Run($mod.c.split(""));',
  9592. ' $mod.Run($mod.s.split(""));',
  9593. '};',
  9594. '']),
  9595. LinesToStr([
  9596. '$mod.a = $mod.c.split("");',
  9597. '$mod.a = $mod.s.split("");',
  9598. '$mod.a = "\r".split("");',
  9599. '$mod.a = "Foo".split("");',
  9600. '$mod.Run($mod.c.split(""));',
  9601. '$mod.Run($mod.s.split(""));',
  9602. '']));
  9603. end;
  9604. procedure TTestModule.TestArray_ConstRef;
  9605. begin
  9606. StartProgram(false);
  9607. Add([
  9608. 'type TArr = array of word;',
  9609. 'procedure Run(constref a: TArr);',
  9610. 'begin',
  9611. 'end;',
  9612. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9613. 'var l: TArr;',
  9614. 'begin',
  9615. ' Run(l);',
  9616. ' Run(a);',
  9617. ' Run(b);',
  9618. ' Run(c);',
  9619. ' Run(d);',
  9620. ' Run(e);',
  9621. 'end;',
  9622. 'begin',
  9623. '']);
  9624. ConvertProgram;
  9625. CheckResolverUnexpectedHints();
  9626. CheckSource('TestArray_ConstRef',
  9627. LinesToStr([ // statements
  9628. 'this.Run = function (a) {',
  9629. '};',
  9630. 'this.Fly = function (a, b, c, d, e) {',
  9631. ' var l = [];',
  9632. ' $mod.Run(l);',
  9633. ' $mod.Run(a);',
  9634. ' $mod.Run(b.get());',
  9635. ' $mod.Run(c.get());',
  9636. ' $mod.Run(d);',
  9637. ' $mod.Run(e);',
  9638. '};',
  9639. '']),
  9640. LinesToStr([
  9641. '']));
  9642. end;
  9643. procedure TTestModule.TestArray_Concat;
  9644. begin
  9645. StartProgram(false);
  9646. Add([
  9647. 'type',
  9648. ' integer = longint;',
  9649. ' TFlag = (big,small);',
  9650. ' TFlags = set of TFlag;',
  9651. ' TRec = record',
  9652. ' i: integer;',
  9653. ' end;',
  9654. ' TArrInt = array of integer;',
  9655. ' TArrRec = array of TRec;',
  9656. ' TArrFlag = array of TFlag;',
  9657. ' TArrSet = array of TFlags;',
  9658. ' TArrJSValue = array of jsvalue;',
  9659. 'var',
  9660. ' ArrInt: tarrint;',
  9661. ' ArrRec: tarrrec;',
  9662. ' ArrFlag: tarrflag;',
  9663. ' ArrSet: tarrset;',
  9664. ' ArrJSValue: tarrjsvalue;',
  9665. 'begin',
  9666. ' arrint:=concat(arrint);',
  9667. ' arrint:=concat(arrint,arrint);',
  9668. ' arrint:=concat(arrint,arrint,arrint);',
  9669. ' arrrec:=concat(arrrec);',
  9670. ' arrrec:=concat(arrrec,arrrec);',
  9671. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9672. ' arrset:=concat(arrset);',
  9673. ' arrset:=concat(arrset,arrset);',
  9674. ' arrset:=concat(arrset,arrset,arrset);',
  9675. ' arrjsvalue:=concat(arrjsvalue);',
  9676. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9677. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9678. ' arrint:=concat([1],arrint);',
  9679. ' arrflag:=concat([big]);',
  9680. ' arrflag:=concat([big],arrflag);',
  9681. ' arrflag:=concat(arrflag,[small]);',
  9682. '']);
  9683. ConvertProgram;
  9684. CheckSource('TestArray_Concat',
  9685. LinesToStr([ // statements
  9686. 'this.TFlag = {',
  9687. ' "0": "big",',
  9688. ' big: 0,',
  9689. ' "1": "small",',
  9690. ' small: 1',
  9691. '};',
  9692. 'rtl.recNewT(this, "TRec", function () {',
  9693. ' this.i = 0;',
  9694. ' this.$eq = function (b) {',
  9695. ' return this.i === b.i;',
  9696. ' };',
  9697. ' this.$assign = function (s) {',
  9698. ' this.i = s.i;',
  9699. ' return this;',
  9700. ' };',
  9701. '});',
  9702. 'this.ArrInt = [];',
  9703. 'this.ArrRec = [];',
  9704. 'this.ArrFlag = [];',
  9705. 'this.ArrSet = [];',
  9706. 'this.ArrJSValue = [];',
  9707. '']),
  9708. LinesToStr([ // $mod.$main
  9709. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9710. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9711. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9712. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9713. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9714. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9715. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9716. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9717. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9718. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9719. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9720. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9721. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9722. '$mod.ArrFlag = [$mod.TFlag.big];',
  9723. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9724. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9725. '']));
  9726. end;
  9727. procedure TTestModule.TestArray_Copy;
  9728. begin
  9729. StartProgram(false);
  9730. Add([
  9731. 'type',
  9732. ' integer = longint;',
  9733. ' TFlag = (big,small);',
  9734. ' TFlags = set of TFlag;',
  9735. ' TRec = record',
  9736. ' i: integer;',
  9737. ' end;',
  9738. ' TArrInt = array of integer;',
  9739. ' TArrRec = array of TRec;',
  9740. ' TArrSet = array of TFlags;',
  9741. ' TArrJSValue = array of jsvalue;',
  9742. 'var',
  9743. ' ArrInt: tarrint;',
  9744. ' ArrRec: tarrrec;',
  9745. ' ArrSet: tarrset;',
  9746. ' ArrJSValue: tarrjsvalue;',
  9747. 'begin',
  9748. ' arrint:=copy(arrint);',
  9749. ' arrint:=copy(arrint,2);',
  9750. ' arrint:=copy(arrint,3,4);',
  9751. ' arrint:=copy([1,1],1,2);',
  9752. ' arrrec:=copy(arrrec);',
  9753. ' arrrec:=copy(arrrec,5);',
  9754. ' arrrec:=copy(arrrec,6,7);',
  9755. ' arrset:=copy(arrset);',
  9756. ' arrset:=copy(arrset,8);',
  9757. ' arrset:=copy(arrset,9,10);',
  9758. ' arrjsvalue:=copy(arrjsvalue);',
  9759. ' arrjsvalue:=copy(arrjsvalue,11);',
  9760. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9761. ' ']);
  9762. ConvertProgram;
  9763. CheckSource('TestArray_Copy',
  9764. LinesToStr([ // statements
  9765. 'this.TFlag = {',
  9766. ' "0": "big",',
  9767. ' big: 0,',
  9768. ' "1": "small",',
  9769. ' small: 1',
  9770. '};',
  9771. 'rtl.recNewT(this, "TRec", function () {',
  9772. ' this.i = 0;',
  9773. ' this.$eq = function (b) {',
  9774. ' return this.i === b.i;',
  9775. ' };',
  9776. ' this.$assign = function (s) {',
  9777. ' this.i = s.i;',
  9778. ' return this;',
  9779. ' };',
  9780. '});',
  9781. 'this.ArrInt = [];',
  9782. 'this.ArrRec = [];',
  9783. 'this.ArrSet = [];',
  9784. 'this.ArrJSValue = [];',
  9785. '']),
  9786. LinesToStr([ // $mod.$main
  9787. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9788. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9789. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9790. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9791. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9792. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9793. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9794. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9795. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9796. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9797. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9798. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9799. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9800. '']));
  9801. end;
  9802. procedure TTestModule.TestArray_InsertDelete;
  9803. begin
  9804. StartProgram(false);
  9805. Add([
  9806. 'type',
  9807. ' integer = longint;',
  9808. ' TFlag = (big,small);',
  9809. ' TFlags = set of TFlag;',
  9810. ' TRec = record',
  9811. ' i: integer;',
  9812. ' end;',
  9813. ' TArrInt = array of integer;',
  9814. ' TArrRec = array of TRec;',
  9815. ' TArrSet = array of TFlags;',
  9816. ' TArrJSValue = array of jsvalue;',
  9817. ' TArrArrInt = array of TArrInt;',
  9818. 'var',
  9819. ' ArrInt: tarrint;',
  9820. ' ArrRec: tarrrec;',
  9821. ' ArrSet: tarrset;',
  9822. ' ArrJSValue: tarrjsvalue;',
  9823. ' ArrArrInt: TArrArrInt;',
  9824. 'begin',
  9825. ' Insert(1,arrint,2);',
  9826. ' Insert(arrint[3],arrint,4);',
  9827. ' Insert(arrrec[5],arrrec,6);',
  9828. ' Insert(arrset[7],arrset,7);',
  9829. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9830. ' Insert(10,arrjsvalue,11);',
  9831. ' Insert([23],arrarrint,22);',
  9832. ' Delete(arrint,12,13);',
  9833. ' Delete(arrrec,14,15);',
  9834. ' Delete(arrset,17,18);',
  9835. ' Delete(arrjsvalue,19,10);']);
  9836. ConvertProgram;
  9837. CheckSource('TestArray_InsertDelete',
  9838. LinesToStr([ // statements
  9839. 'this.TFlag = {',
  9840. ' "0": "big",',
  9841. ' big: 0,',
  9842. ' "1": "small",',
  9843. ' small: 1',
  9844. '};',
  9845. 'rtl.recNewT(this, "TRec", function () {',
  9846. ' this.i = 0;',
  9847. ' this.$eq = function (b) {',
  9848. ' return this.i === b.i;',
  9849. ' };',
  9850. ' this.$assign = function (s) {',
  9851. ' this.i = s.i;',
  9852. ' return this;',
  9853. ' };',
  9854. '});',
  9855. 'this.ArrInt = [];',
  9856. 'this.ArrRec = [];',
  9857. 'this.ArrSet = [];',
  9858. 'this.ArrJSValue = [];',
  9859. 'this.ArrArrInt = [];',
  9860. '']),
  9861. LinesToStr([ // $mod.$main
  9862. '$mod.ArrInt.splice(2, 0, 1);',
  9863. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9864. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9865. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9866. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9867. '$mod.ArrJSValue.splice(11, 0, 10);',
  9868. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9869. '$mod.ArrInt.splice(12, 13);',
  9870. '$mod.ArrRec.splice(14, 15);',
  9871. '$mod.ArrSet.splice(17, 18);',
  9872. '$mod.ArrJSValue.splice(19, 10);',
  9873. '']));
  9874. end;
  9875. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9876. begin
  9877. Parser.Options:=Parser.Options+[po_cassignments];
  9878. StartProgram(false);
  9879. Add([
  9880. '{$modeswitch arrayoperators}',
  9881. 'type',
  9882. ' integer = longint;',
  9883. ' TArrInt = array of integer;',
  9884. ' TArrStr = array of string;',
  9885. 'const',
  9886. ' Ints: TArrInt = (1,2,3);',
  9887. ' Aliases: TarrStr = (''foo'',''b'');',
  9888. ' OneInt: TArrInt = (7);',
  9889. ' OneStr: array of integer = (7);',
  9890. ' Chars: array of char = ''aoc'';',
  9891. ' Names: array of string = (''a'',''foo'');',
  9892. ' NameCount = low(Names)+high(Names)+length(Names);',
  9893. 'var i: integer;',
  9894. 'begin',
  9895. ' Ints:=[];',
  9896. ' Ints:=[1,1];',
  9897. ' Ints:=[1]+[2];',
  9898. ' Ints:=[2];',
  9899. ' Ints:=[]+ints;',
  9900. ' Ints:=Ints+[];',
  9901. ' Ints:=Ints+OneInt;',
  9902. ' Ints:=Ints+[1,1];',
  9903. ' Ints:=[i,i]+Ints;',
  9904. ' Ints:=[1]+[i]+[3];',
  9905. '']);
  9906. ConvertProgram;
  9907. CheckSource('TestArray_DynArrayConstObjFPC',
  9908. LinesToStr([ // statements
  9909. 'this.Ints = [1, 2, 3];',
  9910. 'this.Aliases = ["foo", "b"];',
  9911. 'this.OneInt = [7];',
  9912. 'this.OneStr = [7];',
  9913. 'this.Chars = ["a", "o", "c"];',
  9914. 'this.Names = ["a", "foo"];',
  9915. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  9916. 'this.i = 0;',
  9917. '']),
  9918. LinesToStr([ // $mod.$main
  9919. '$mod.Ints = [];',
  9920. '$mod.Ints = [1, 1];',
  9921. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9922. '$mod.Ints = [2];',
  9923. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9924. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9925. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9926. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9927. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9928. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9929. '']));
  9930. end;
  9931. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9932. begin
  9933. StartProgram(false);
  9934. // Note: const c = [1,1]; defines a set!
  9935. Add([
  9936. '{$mode delphi}',
  9937. 'type',
  9938. ' integer = longint;',
  9939. ' TArrInt = array of integer;',
  9940. ' TArrStr = array of string;',
  9941. 'const',
  9942. ' Ints: TArrInt = [1,1,2];',
  9943. ' Aliases: TarrStr = [''foo'',''b''];',
  9944. ' OneInt: TArrInt = [7];',
  9945. ' OneStr: array of integer = [7]+[8];',
  9946. ' Chars: array of char = ''aoc'';',
  9947. ' Names: array of string = [''a'',''a''];',
  9948. ' NameCount = low(Names)+high(Names)+length(Names);',
  9949. 'begin',
  9950. '']);
  9951. ConvertProgram;
  9952. CheckSource('TestArray_DynArrayConstDelphi',
  9953. LinesToStr([ // statements
  9954. 'this.Ints = [1, 1, 2];',
  9955. 'this.Aliases = ["foo", "b"];',
  9956. 'this.OneInt = [7];',
  9957. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9958. 'this.Chars = ["a", "o", "c"];',
  9959. 'this.Names = ["a", "a"];',
  9960. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  9961. '']),
  9962. LinesToStr([ // $mod.$main
  9963. '']));
  9964. end;
  9965. procedure TTestModule.TestArray_ArrayLitAsParam;
  9966. begin
  9967. StartProgram(false);
  9968. Add([
  9969. '{$modeswitch arrayoperators}',
  9970. 'type',
  9971. ' integer = longint;',
  9972. ' TArrInt = array of integer;',
  9973. ' TArrSet = array of (red,green,blue);',
  9974. 'procedure DoOpenInt(const a: array of integer); forward;',
  9975. 'procedure DoInt(const a: TArrInt);',
  9976. 'begin',
  9977. ' DoInt(a+[1]);',
  9978. ' DoInt([1]+a);',
  9979. ' DoOpenInt(a);',
  9980. ' DoOpenInt(a+[1]);',
  9981. ' DoOpenInt([1]+a);',
  9982. 'end;',
  9983. 'procedure DoOpenInt(const a: array of integer);',
  9984. 'begin',
  9985. ' DoOpenInt(a+[1]);',
  9986. ' DoOpenInt([1]+a);',
  9987. ' DoInt(a);',
  9988. ' DoInt(a+[1]);',
  9989. ' DoInt([1]+a);',
  9990. 'end;',
  9991. 'procedure DoSet(const a: TArrSet);',
  9992. 'begin',
  9993. ' DoSet(a+[red]);',
  9994. ' DoSet([blue]+a);',
  9995. 'end;',
  9996. 'var',
  9997. ' i: TArrInt;',
  9998. ' s: TArrSet;',
  9999. 'begin',
  10000. ' DoInt([1]);',
  10001. ' DoInt([1]+[2]);',
  10002. ' DoInt(i+[1]);',
  10003. ' DoInt([1]+i);',
  10004. ' DoOpenInt([1]);',
  10005. ' DoOpenInt([1]+[2]);',
  10006. ' DoOpenInt(i+[1]);',
  10007. ' DoOpenInt([1]+i);',
  10008. ' DoSet([red]);',
  10009. ' DoSet([blue]+[green]);',
  10010. ' DoSet(s+[blue]);',
  10011. ' DoSet([red]+s);',
  10012. '']);
  10013. ConvertProgram;
  10014. CheckSource('TestArray_ArrayLitAsParam',
  10015. LinesToStr([ // statements
  10016. 'this.TArrSet$a = {',
  10017. ' "0": "red",',
  10018. ' red: 0,',
  10019. ' "1": "green",',
  10020. ' green: 1,',
  10021. ' "2": "blue",',
  10022. ' blue: 2',
  10023. '};',
  10024. 'this.DoInt = function (a) {',
  10025. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10026. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10027. ' $mod.DoOpenInt(a);',
  10028. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10029. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10030. '};',
  10031. 'this.DoOpenInt = function (a) {',
  10032. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10033. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10034. ' $mod.DoInt(a);',
  10035. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10036. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10037. '};',
  10038. 'this.DoSet = function (a) {',
  10039. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10040. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10041. '};',
  10042. 'this.i = [];',
  10043. 'this.s = [];',
  10044. '']),
  10045. LinesToStr([ // $mod.$main
  10046. '$mod.DoInt([1]);',
  10047. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10048. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10049. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10050. '$mod.DoOpenInt([1]);',
  10051. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10052. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10053. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10054. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10055. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10056. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10057. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10058. '']));
  10059. end;
  10060. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10061. begin
  10062. StartProgram(false);
  10063. Add([
  10064. '{$modeswitch arrayoperators}',
  10065. 'type',
  10066. ' integer = longint;',
  10067. ' TArrInt = array of integer;',
  10068. ' TArrArrInt = array of TArrInt;',
  10069. 'procedure DoInt(const a: TArrArrInt);',
  10070. 'begin',
  10071. ' DoInt(a+[[1]]);',
  10072. ' DoInt([[1]]+a);',
  10073. ' DoInt(a);',
  10074. 'end;',
  10075. 'var',
  10076. ' i: TArrInt;',
  10077. ' a: TArrArrInt;',
  10078. 'begin',
  10079. ' a:=[[1]];',
  10080. ' a:=[i];',
  10081. ' a:=a+[i];',
  10082. ' a:=[i]+a;',
  10083. ' a:=[[1]+i];',
  10084. ' a:=[[1]+[2]];',
  10085. ' a:=[i+[2]];',
  10086. ' DoInt([[1]]);',
  10087. ' DoInt([[1]+[2],[3,4],[5]]);',
  10088. ' DoInt([i+[1]]+a);',
  10089. ' DoInt([i]+a);',
  10090. '']);
  10091. ConvertProgram;
  10092. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10093. LinesToStr([ // statements
  10094. 'this.DoInt = function (a) {',
  10095. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10096. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10097. ' $mod.DoInt(a);',
  10098. '};',
  10099. 'this.i = [];',
  10100. 'this.a = [];',
  10101. '']),
  10102. LinesToStr([ // $mod.$main
  10103. '$mod.a = [[1]];',
  10104. '$mod.a = [$mod.i];',
  10105. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10106. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10107. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10108. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10109. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10110. '$mod.DoInt([[1]]);',
  10111. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10112. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10113. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10114. '']));
  10115. end;
  10116. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10117. begin
  10118. StartProgram(false);
  10119. Add([
  10120. '{$modeswitch arrayoperators}',
  10121. 'type',
  10122. ' integer = longint;',
  10123. ' TArrInt = array[1..2] of integer;',
  10124. ' TArrArrInt = array of TArrInt;',
  10125. 'procedure DoInt(const a: TArrArrInt);',
  10126. 'begin',
  10127. ' DoInt(a+[[1,2]]);',
  10128. ' DoInt([[1,2]]+a);',
  10129. ' DoInt(a);',
  10130. 'end;',
  10131. 'var',
  10132. ' i: TArrInt;',
  10133. ' a: TArrArrInt;',
  10134. 'begin',
  10135. ' a:=[[1,1]];',
  10136. ' a:=[i];',
  10137. ' a:=a+[i];',
  10138. ' a:=[i]+a;',
  10139. ' DoInt([[1,1]]);',
  10140. ' DoInt([[1,2],[3,4]]);',
  10141. '']);
  10142. ConvertProgram;
  10143. CheckSource('TestArray_ArrayLitStaticAsParam',
  10144. LinesToStr([ // statements
  10145. 'this.DoInt = function (a) {',
  10146. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10147. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10148. ' $mod.DoInt(a);',
  10149. '};',
  10150. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10151. 'this.a = [];',
  10152. '']),
  10153. LinesToStr([ // $mod.$main
  10154. '$mod.a = [[1, 1]];',
  10155. '$mod.a = [$mod.i.slice(0)];',
  10156. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10157. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10158. '$mod.DoInt([[1, 1]]);',
  10159. '$mod.DoInt([[1, 2], [3, 4]]);',
  10160. '']));
  10161. end;
  10162. procedure TTestModule.TestArray_ForInArrOfString;
  10163. begin
  10164. StartProgram(false);
  10165. Add([
  10166. 'type',
  10167. 'type',
  10168. ' TMonthNameArray = array [1..12] of string;',
  10169. ' TMonthNames = TMonthNameArray;',
  10170. ' TObject = class',
  10171. ' private',
  10172. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10173. ' public',
  10174. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10175. ' end;',
  10176. 'var',
  10177. ' f: TObject;',
  10178. ' Month: string;',
  10179. ' Names: array of string = (''a'',''foo'',''bar'');',
  10180. ' i: longint;',
  10181. 'begin',
  10182. ' for Month in f.LongMonthNames do ;',
  10183. ' for Month in Names do ;',
  10184. ' for i:=low(Names) to high(Names) do ;',
  10185. '']);
  10186. ConvertProgram;
  10187. CheckSource('TestArray_ForInArrOfString',
  10188. LinesToStr([ // statements
  10189. 'rtl.createClass(this, "TObject", null, function () {',
  10190. ' this.$init = function () {',
  10191. ' };',
  10192. ' this.$final = function () {',
  10193. ' };',
  10194. '});',
  10195. 'this.f = null;',
  10196. 'this.Month = "";',
  10197. 'this.Names = ["a", "foo", "bar"];',
  10198. 'this.i = 0;',
  10199. '']),
  10200. LinesToStr([ // $mod.$main
  10201. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10202. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10203. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10204. '']));
  10205. end;
  10206. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10207. begin
  10208. StartProgram(false);
  10209. Add([
  10210. '{$modeswitch externalclass}',
  10211. 'type',
  10212. ' TJSObject = class external name ''Object''',
  10213. ' end;',
  10214. ' TJSArray = class external name ''Array''',
  10215. ' class function isArray(Value: JSValue) : boolean;',
  10216. ' function concat() : TJSArray; varargs;',
  10217. ' end;',
  10218. 'var',
  10219. ' aObj: TJSArray;',
  10220. ' a: array of longint;',
  10221. ' o: TJSObject;',
  10222. 'begin',
  10223. ' if TJSArray.isArray(65) then ;',
  10224. ' aObj:=TJSArray(a).concat(a);',
  10225. ' o:=TJSObject(a);']);
  10226. ConvertProgram;
  10227. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10228. LinesToStr([ // statements
  10229. 'this.aObj = null;',
  10230. 'this.a = [];',
  10231. 'this.o = null;',
  10232. '']),
  10233. LinesToStr([ // $mod.$main
  10234. 'if (Array.isArray(65)) ;',
  10235. '$mod.aObj = $mod.a.concat($mod.a);',
  10236. '$mod.o = $mod.a;',
  10237. '']));
  10238. end;
  10239. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10240. begin
  10241. StartProgram(false);
  10242. Add([
  10243. '{$modeswitch externalclass}',
  10244. 'type',
  10245. ' TArrStr = array of string;',
  10246. ' TJSArray = class external name ''Array''',
  10247. ' end;',
  10248. ' TJSObject = class external name ''Object''',
  10249. ' end;',
  10250. 'var',
  10251. ' aObj: TJSArray;',
  10252. ' a: TArrStr;',
  10253. ' jo: TJSObject;',
  10254. 'begin',
  10255. ' a:=TArrStr(aObj);',
  10256. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10257. ' a:=TarrStr(jo);',
  10258. '']);
  10259. ConvertProgram;
  10260. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10261. LinesToStr([ // statements
  10262. 'this.aObj = null;',
  10263. 'this.a = [];',
  10264. 'this.jo = null;',
  10265. '']),
  10266. LinesToStr([ // $mod.$main
  10267. '$mod.a = $mod.aObj;',
  10268. '$mod.aObj[1] = $mod.aObj[2];',
  10269. '$mod.a = $mod.jo;',
  10270. '']));
  10271. end;
  10272. procedure TTestModule.TestArrayOfConst_TVarRec;
  10273. begin
  10274. StartProgram(true,[supTVarRec]);
  10275. Add([
  10276. 'procedure Say(args: array of const);',
  10277. 'var',
  10278. ' i: longint;',
  10279. ' v: TVarRec;',
  10280. 'begin',
  10281. ' for i:=low(args) to high(args) do begin',
  10282. ' v:=args[i];',
  10283. ' case v.vtype of',
  10284. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10285. ' end;',
  10286. ' end;',
  10287. ' for v in args do ;',
  10288. ' args:=nil;',
  10289. ' SetLength(args,2);',
  10290. 'end;',
  10291. 'begin']);
  10292. ConvertProgram;
  10293. CheckSource('TestArrayOfConst_TVarRec',
  10294. LinesToStr([ // statements
  10295. 'this.Say = function (args) {',
  10296. ' var i = 0;',
  10297. ' var v = pas.system.TVarRec.$new();',
  10298. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10299. ' i = $l;',
  10300. ' v.$assign(args[i]);',
  10301. ' var $tmp = v.VType;',
  10302. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10303. ' };',
  10304. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10305. ' args = [];',
  10306. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10307. '};',
  10308. '']),
  10309. LinesToStr([ // $mod.$main
  10310. ]));
  10311. end;
  10312. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10313. begin
  10314. StartProgram(true,[supTVarRec]);
  10315. Add([
  10316. 'procedure Say(args: array of const);',
  10317. 'begin',
  10318. ' Say(args);',
  10319. 'end;',
  10320. 'var',
  10321. ' p: Pointer;',
  10322. ' j: jsvalue;',
  10323. ' c: currency;',
  10324. 'begin',
  10325. ' Say([]);',
  10326. ' Say([1]);',
  10327. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10328. '']);
  10329. ConvertProgram;
  10330. CheckSource('TestArrayOfConst_PassBaseTypes',
  10331. LinesToStr([ // statements
  10332. 'this.Say = function (args) {',
  10333. ' $mod.Say(args);',
  10334. '};',
  10335. 'this.p = null;',
  10336. 'this.j = undefined;',
  10337. 'this.c = 0;',
  10338. '']),
  10339. LinesToStr([ // $mod.$main
  10340. '$mod.Say([]);',
  10341. '$mod.Say(pas.system.VarRecs(0, 1));',
  10342. '$mod.Say(pas.system.VarRecs(',
  10343. ' 9,',
  10344. ' "c",',
  10345. ' 18,',
  10346. ' "foo",',
  10347. ' 5,',
  10348. ' null,',
  10349. ' 1,',
  10350. ' true,',
  10351. ' 3,',
  10352. ' 1.3,',
  10353. ' 5,',
  10354. ' $mod.p,',
  10355. ' 20,',
  10356. ' $mod.j,',
  10357. ' 12,',
  10358. ' $mod.c',
  10359. ' ));',
  10360. '']));
  10361. end;
  10362. procedure TTestModule.TestArrayOfConst_PassObj;
  10363. begin
  10364. StartProgram(true,[supTVarRec]);
  10365. Add([
  10366. '{$interfaces corba}',
  10367. 'type',
  10368. ' TObject = class',
  10369. ' end;',
  10370. ' TClass = class of TObject;',
  10371. ' IUnknown = interface',
  10372. ' end;',
  10373. 'procedure Say(args: array of const);',
  10374. 'begin',
  10375. 'end;',
  10376. 'var',
  10377. ' o: TObject;',
  10378. ' c: TClass;',
  10379. ' i: IUnknown;',
  10380. 'begin',
  10381. ' Say([o,c,TObject]);',
  10382. ' Say([nil,i]);',
  10383. '']);
  10384. ConvertProgram;
  10385. CheckSource('TestArrayOfConst_PassObj',
  10386. LinesToStr([ // statements
  10387. 'rtl.createClass(this, "TObject", null, function () {',
  10388. ' this.$init = function () {',
  10389. ' };',
  10390. ' this.$final = function () {',
  10391. ' };',
  10392. '});',
  10393. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10394. 'this.Say = function (args) {',
  10395. '};',
  10396. 'this.o = null;',
  10397. 'this.c = null;',
  10398. 'this.i = null;',
  10399. '']),
  10400. LinesToStr([ // $mod.$main
  10401. '$mod.Say(pas.system.VarRecs(',
  10402. ' 7,',
  10403. ' $mod.o,',
  10404. ' 8,',
  10405. ' $mod.c,',
  10406. ' 8,',
  10407. ' $mod.TObject',
  10408. '));',
  10409. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10410. '']));
  10411. end;
  10412. procedure TTestModule.TestRecord_Empty;
  10413. begin
  10414. StartProgram(false);
  10415. Add([
  10416. 'type',
  10417. ' TRecA = record',
  10418. ' end;',
  10419. 'var a,b: TRecA;',
  10420. 'begin',
  10421. ' if a=b then ;']);
  10422. ConvertProgram;
  10423. CheckSource('TestRecord_Empty',
  10424. LinesToStr([ // statements
  10425. 'rtl.recNewT(this, "TRecA", function () {',
  10426. ' this.$eq = function (b) {',
  10427. ' return true;',
  10428. ' };',
  10429. ' this.$assign = function (s) {',
  10430. ' return this;',
  10431. ' };',
  10432. '});',
  10433. 'this.a = this.TRecA.$new();',
  10434. 'this.b = this.TRecA.$new();',
  10435. '']),
  10436. LinesToStr([ // $mod.$main
  10437. 'if ($mod.a.$eq($mod.b)) ;'
  10438. ]));
  10439. end;
  10440. procedure TTestModule.TestRecord_Var;
  10441. begin
  10442. StartProgram(false);
  10443. Add('type');
  10444. Add(' TRecA = record');
  10445. Add(' Bold: longint;');
  10446. Add(' end;');
  10447. Add('var Rec: TRecA;');
  10448. Add('begin');
  10449. Add(' rec.bold:=123');
  10450. ConvertProgram;
  10451. CheckSource('TestRecord_Var',
  10452. LinesToStr([ // statements
  10453. 'rtl.recNewT(this, "TRecA", function () {',
  10454. ' this.Bold = 0;',
  10455. ' this.$eq = function (b) {',
  10456. ' return this.Bold === b.Bold;',
  10457. ' };',
  10458. ' this.$assign = function (s) {',
  10459. ' this.Bold = s.Bold;',
  10460. ' return this;',
  10461. ' };',
  10462. '});',
  10463. 'this.Rec = this.TRecA.$new();',
  10464. '']),
  10465. LinesToStr([ // $mod.$main
  10466. '$mod.Rec.Bold = 123;'
  10467. ]));
  10468. end;
  10469. procedure TTestModule.TestRecord_VarExternal;
  10470. begin
  10471. StartProgram(false);
  10472. Add([
  10473. '{$modeswitch externalclass}',
  10474. 'type',
  10475. ' TRecA = record',
  10476. ' i: byte;',
  10477. ' length_: longint external name ''length'';',
  10478. ' end;',
  10479. 'var Rec: TRecA;',
  10480. 'begin',
  10481. ' rec.length_ := rec.length_',
  10482. '']);
  10483. ConvertProgram;
  10484. CheckSource('TestRecord_VarExternal',
  10485. LinesToStr([ // statements
  10486. 'rtl.recNewT(this, "TRecA", function () {',
  10487. ' this.i = 0;',
  10488. ' this.$eq = function (b) {',
  10489. ' return (this.i === b.i) && (this.length === b.length);',
  10490. ' };',
  10491. ' this.$assign = function (s) {',
  10492. ' this.i = s.i;',
  10493. ' this.length = s.length;',
  10494. ' return this;',
  10495. ' };',
  10496. '});',
  10497. 'this.Rec = this.TRecA.$new();',
  10498. '']),
  10499. LinesToStr([ // $mod.$main
  10500. '$mod.Rec.length = $mod.Rec.length;'
  10501. ]));
  10502. end;
  10503. procedure TTestModule.TestRecord_WithDo;
  10504. begin
  10505. StartProgram(false);
  10506. Add('type');
  10507. Add(' TRec = record');
  10508. Add(' vI: longint;');
  10509. Add(' end;');
  10510. Add('var');
  10511. Add(' Int: longint;');
  10512. Add(' r: TRec;');
  10513. Add('begin');
  10514. Add(' with r do');
  10515. Add(' int:=vi;');
  10516. Add(' with r do begin');
  10517. Add(' int:=vi;');
  10518. Add(' vi:=int;');
  10519. Add(' end;');
  10520. ConvertProgram;
  10521. CheckSource('TestWithRecordDo',
  10522. LinesToStr([ // statements
  10523. 'rtl.recNewT(this, "TRec", function () {',
  10524. ' this.vI = 0;',
  10525. ' this.$eq = function (b) {',
  10526. ' return this.vI === b.vI;',
  10527. ' };',
  10528. ' this.$assign = function (s) {',
  10529. ' this.vI = s.vI;',
  10530. ' return this;',
  10531. ' };',
  10532. '});',
  10533. 'this.Int = 0;',
  10534. 'this.r = this.TRec.$new();',
  10535. '']),
  10536. LinesToStr([ // $mod.$main
  10537. 'var $with = $mod.r;',
  10538. '$mod.Int = $with.vI;',
  10539. 'var $with1 = $mod.r;',
  10540. '$mod.Int = $with1.vI;',
  10541. '$with1.vI = $mod.Int;'
  10542. ]));
  10543. end;
  10544. procedure TTestModule.TestRecord_Assign;
  10545. begin
  10546. StartProgram(false);
  10547. Add('type');
  10548. Add(' TEnum = (red,green);');
  10549. Add(' TEnums = set of TEnum;');
  10550. Add(' TSmallRec = record');
  10551. Add(' N: longint;');
  10552. Add(' end;');
  10553. Add(' TBigRec = record');
  10554. Add(' Int: longint;');
  10555. Add(' D: double;');
  10556. Add(' Arr: array of longint;');
  10557. Add(' Arr2: array[1..2] of longint;');
  10558. Add(' Small: TSmallRec;');
  10559. Add(' Enums: TEnums;');
  10560. Add(' end;');
  10561. Add('var');
  10562. Add(' r, s: TBigRec;');
  10563. Add('begin');
  10564. Add(' r:=s;');
  10565. Add(' r:=default(TBigRec);');
  10566. Add(' r:=default(s);');
  10567. ConvertProgram;
  10568. CheckSource('TestRecord_Assign',
  10569. LinesToStr([ // statements
  10570. 'this.TEnum = {',
  10571. ' "0": "red",',
  10572. ' red: 0,',
  10573. ' "1": "green",',
  10574. ' green: 1',
  10575. '};',
  10576. 'rtl.recNewT(this, "TSmallRec", function () {',
  10577. ' this.N = 0;',
  10578. ' this.$eq = function (b) {',
  10579. ' return this.N === b.N;',
  10580. ' };',
  10581. ' this.$assign = function (s) {',
  10582. ' this.N = s.N;',
  10583. ' return this;',
  10584. ' };',
  10585. '});',
  10586. 'rtl.recNewT(this, "TBigRec", function () {',
  10587. ' this.Int = 0;',
  10588. ' this.D = 0.0;',
  10589. ' this.$new = function () {',
  10590. ' var r = Object.create(this);',
  10591. ' r.Arr = [];',
  10592. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10593. ' r.Small = $mod.TSmallRec.$new();',
  10594. ' r.Enums = {};',
  10595. ' return r;',
  10596. ' };',
  10597. ' this.$eq = function (b) {',
  10598. ' 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);',
  10599. ' };',
  10600. ' this.$assign = function (s) {',
  10601. ' this.Int = s.Int;',
  10602. ' this.D = s.D;',
  10603. ' this.Arr = rtl.arrayRef(s.Arr);',
  10604. ' this.Arr2 = s.Arr2.slice(0);',
  10605. ' this.Small.$assign(s.Small);',
  10606. ' this.Enums = rtl.refSet(s.Enums);',
  10607. ' return this;',
  10608. ' };',
  10609. '});',
  10610. 'this.r = this.TBigRec.$new();',
  10611. 'this.s = this.TBigRec.$new();',
  10612. '']),
  10613. LinesToStr([ // $mod.$main
  10614. '$mod.r.$assign($mod.s);',
  10615. '$mod.r.$assign($mod.TBigRec.$new());',
  10616. '$mod.r.$assign($mod.TBigRec.$new());',
  10617. '']));
  10618. end;
  10619. procedure TTestModule.TestRecord_AsParams;
  10620. begin
  10621. StartProgram(false);
  10622. Add([
  10623. 'type',
  10624. ' integer = longint;',
  10625. ' TRecord = record',
  10626. ' i: integer;',
  10627. ' end;',
  10628. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10629. 'var vL: TRecord;',
  10630. 'begin',
  10631. ' vd:=vd;',
  10632. ' vd.i:=vd.i;',
  10633. ' vl:=vc;',
  10634. ' vv:=vv;',
  10635. ' vv.i:=vv.i;',
  10636. ' U:=vl;',
  10637. ' U:=vd;',
  10638. ' U:=vc;',
  10639. ' U:=vv;',
  10640. ' vl:=TRecord(U);',
  10641. ' vd:=TRecord(U);',
  10642. ' vv:=TRecord(U);',
  10643. ' doit(vd,vd,vd,vd);',
  10644. ' doit(vc,vc,vl,vl);',
  10645. ' doit(vv,vv,vv,vv);',
  10646. ' doit(vl,vl,vl,vl);',
  10647. ' TRecord(U).i:=3;',
  10648. 'end;',
  10649. 'var i: TRecord;',
  10650. 'begin',
  10651. ' doit(i,i,i,i);',
  10652. '']);
  10653. ConvertProgram;
  10654. CheckSource('TestRecord_AsParams',
  10655. LinesToStr([ // statements
  10656. 'rtl.recNewT(this, "TRecord", function () {',
  10657. ' this.i = 0;',
  10658. ' this.$eq = function (b) {',
  10659. ' return this.i === b.i;',
  10660. ' };',
  10661. ' this.$assign = function (s) {',
  10662. ' this.i = s.i;',
  10663. ' return this;',
  10664. ' };',
  10665. '});',
  10666. 'this.DoIt = function (vD, vC, vV, U) {',
  10667. ' var vL = $mod.TRecord.$new();',
  10668. ' vD.$assign(vD);',
  10669. ' vD.i = vD.i;',
  10670. ' vL.$assign(vC);',
  10671. ' vV.$assign(vV);',
  10672. ' vV.i = vV.i;',
  10673. ' U.$assign(vL);',
  10674. ' U.$assign(vD);',
  10675. ' U.$assign(vC);',
  10676. ' U.$assign(vV);',
  10677. ' vL.$assign(U);',
  10678. ' vD.$assign(U);',
  10679. ' vV.$assign(U);',
  10680. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10681. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10682. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10683. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10684. ' U.i = 3;',
  10685. '};',
  10686. 'this.i = this.TRecord.$new();'
  10687. ]),
  10688. LinesToStr([
  10689. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10690. '']));
  10691. end;
  10692. procedure TTestModule.TestRecord_ConstRef;
  10693. begin
  10694. StartProgram(false);
  10695. Add([
  10696. 'type TRec = record i: word; end;',
  10697. 'procedure Run(constref a: TRec);',
  10698. 'begin',
  10699. 'end;',
  10700. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10701. 'var l: TRec;',
  10702. 'begin',
  10703. ' Run(l);',
  10704. ' Run(a);',
  10705. ' Run(b);',
  10706. ' Run(c);',
  10707. ' Run(d);',
  10708. ' Run(e);',
  10709. 'end;',
  10710. 'begin',
  10711. '']);
  10712. ConvertProgram;
  10713. CheckResolverUnexpectedHints();
  10714. CheckSource('TestRecord_ConstRef',
  10715. LinesToStr([ // statements
  10716. 'rtl.recNewT(this, "TRec", function () {',
  10717. ' this.i = 0;',
  10718. ' this.$eq = function (b) {',
  10719. ' return this.i === b.i;',
  10720. ' };',
  10721. ' this.$assign = function (s) {',
  10722. ' this.i = s.i;',
  10723. ' return this;',
  10724. ' };',
  10725. '});',
  10726. 'this.Run = function (a) {',
  10727. '};',
  10728. 'this.Fly = function (a, b, c, d, e) {',
  10729. ' var l = $mod.TRec.$new();',
  10730. ' $mod.Run(l);',
  10731. ' $mod.Run(a);',
  10732. ' $mod.Run(b);',
  10733. ' $mod.Run(c);',
  10734. ' $mod.Run(d);',
  10735. ' $mod.Run(e);',
  10736. '};',
  10737. '']),
  10738. LinesToStr([
  10739. '']));
  10740. end;
  10741. procedure TTestModule.TestRecordElement_AsParams;
  10742. begin
  10743. StartProgram(false);
  10744. Add('type');
  10745. Add(' integer = longint;');
  10746. Add(' TRecord = record');
  10747. Add(' i: integer;');
  10748. Add(' end;');
  10749. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10750. Add('var vJ: TRecord;');
  10751. Add('begin');
  10752. Add(' doit(vj.i,vj.i,vj.i);');
  10753. Add('end;');
  10754. Add('var r: TRecord;');
  10755. Add('begin');
  10756. Add(' doit(r.i,r.i,r.i);');
  10757. ConvertProgram;
  10758. CheckSource('TestRecordElement_AsParams',
  10759. LinesToStr([ // statements
  10760. 'rtl.recNewT(this, "TRecord", function () {',
  10761. ' this.i = 0;',
  10762. ' this.$eq = function (b) {',
  10763. ' return this.i === b.i;',
  10764. ' };',
  10765. ' this.$assign = function (s) {',
  10766. ' this.i = s.i;',
  10767. ' return this;',
  10768. ' };',
  10769. '});',
  10770. 'this.DoIt = function (vG,vH,vI) {',
  10771. ' var vJ = $mod.TRecord.$new();',
  10772. ' $mod.DoIt(vJ.i, vJ.i, {',
  10773. ' p: vJ,',
  10774. ' get: function () {',
  10775. ' return this.p.i;',
  10776. ' },',
  10777. ' set: function (v) {',
  10778. ' this.p.i = v;',
  10779. ' }',
  10780. ' });',
  10781. '};',
  10782. 'this.r = this.TRecord.$new();'
  10783. ]),
  10784. LinesToStr([
  10785. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10786. ' p: $mod.r,',
  10787. ' get: function () {',
  10788. ' return this.p.i;',
  10789. ' },',
  10790. ' set: function (v) {',
  10791. ' this.p.i = v;',
  10792. ' }',
  10793. '});'
  10794. ]));
  10795. end;
  10796. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10797. begin
  10798. StartProgram(false);
  10799. Add('type');
  10800. Add(' integer = longint;');
  10801. Add(' TRecord = record');
  10802. Add(' i: integer;');
  10803. Add(' end;');
  10804. Add('function GetRec(vB: integer = 0): TRecord;');
  10805. Add('begin');
  10806. Add('end;');
  10807. Add('procedure DoIt(vG: integer; const vH: integer);');
  10808. Add('begin');
  10809. Add('end;');
  10810. Add('begin');
  10811. Add(' doit(getrec.i,getrec.i);');
  10812. Add(' doit(getrec().i,getrec().i);');
  10813. Add(' doit(getrec(1).i,getrec(2).i);');
  10814. ConvertProgram;
  10815. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10816. LinesToStr([ // statements
  10817. 'rtl.recNewT(this, "TRecord", function () {',
  10818. ' this.i = 0;',
  10819. ' this.$eq = function (b) {',
  10820. ' return this.i === b.i;',
  10821. ' };',
  10822. ' this.$assign = function (s) {',
  10823. ' this.i = s.i;',
  10824. ' return this;',
  10825. ' };',
  10826. '});',
  10827. 'this.GetRec = function (vB) {',
  10828. ' var Result = $mod.TRecord.$new();',
  10829. ' return Result;',
  10830. '};',
  10831. 'this.DoIt = function (vG, vH) {',
  10832. '};',
  10833. '']),
  10834. LinesToStr([
  10835. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10836. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10837. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10838. '']));
  10839. end;
  10840. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10841. begin
  10842. StartProgram(false);
  10843. Add('type');
  10844. Add(' integer = longint;');
  10845. Add(' TRecord = record');
  10846. Add(' i: integer;');
  10847. Add(' end;');
  10848. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10849. Add('begin');
  10850. Add('end;');
  10851. Add('var r: trecord;');
  10852. Add('begin');
  10853. Add(' with r do ');
  10854. Add(' doit(i,i,i);');
  10855. ConvertProgram;
  10856. CheckSource('TestRecordElementFromWith_AsParams',
  10857. LinesToStr([ // statements
  10858. 'rtl.recNewT(this, "TRecord", function () {',
  10859. ' this.i = 0;',
  10860. ' this.$eq = function (b) {',
  10861. ' return this.i === b.i;',
  10862. ' };',
  10863. ' this.$assign = function (s) {',
  10864. ' this.i = s.i;',
  10865. ' return this;',
  10866. ' };',
  10867. '});',
  10868. 'this.DoIt = function (vG,vH,vI) {',
  10869. '};',
  10870. 'this.r = this.TRecord.$new();'
  10871. ]),
  10872. LinesToStr([
  10873. 'var $with = $mod.r;',
  10874. '$mod.DoIt($with.i,$with.i,{',
  10875. ' p: $with,',
  10876. ' get: function () {',
  10877. ' return this.p.i;',
  10878. ' },',
  10879. ' set: function (v) {',
  10880. ' this.p.i = v;',
  10881. ' }',
  10882. '});',
  10883. '']));
  10884. end;
  10885. procedure TTestModule.TestRecord_Equal;
  10886. begin
  10887. StartProgram(false);
  10888. Add('type');
  10889. Add(' integer = longint;');
  10890. Add(' TFlag = (red,blue);');
  10891. Add(' TFlags = set of TFlag;');
  10892. Add(' TProc = procedure;');
  10893. Add(' TRecord = record');
  10894. Add(' i: integer;');
  10895. Add(' Event: TProc;');
  10896. Add(' f: TFlags;');
  10897. Add(' end;');
  10898. Add(' TNested = record');
  10899. Add(' r: TRecord;');
  10900. Add(' end;');
  10901. Add('var');
  10902. Add(' b: boolean;');
  10903. Add(' r,s: trecord;');
  10904. Add('begin');
  10905. Add(' b:=r=s;');
  10906. Add(' b:=r<>s;');
  10907. ConvertProgram;
  10908. CheckSource('TestRecord_Equal',
  10909. LinesToStr([ // statements
  10910. 'this.TFlag = {',
  10911. ' "0": "red",',
  10912. ' red: 0,',
  10913. ' "1": "blue",',
  10914. ' blue: 1',
  10915. '};',
  10916. 'rtl.recNewT(this, "TRecord", function () {',
  10917. ' this.i = 0;',
  10918. ' this.Event = null;',
  10919. ' this.$new = function () {',
  10920. ' var r = Object.create(this);',
  10921. ' r.f = {};',
  10922. ' return r;',
  10923. ' };',
  10924. ' this.$eq = function (b) {',
  10925. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10926. ' };',
  10927. ' this.$assign = function (s) {',
  10928. ' this.i = s.i;',
  10929. ' this.Event = s.Event;',
  10930. ' this.f = rtl.refSet(s.f);',
  10931. ' return this;',
  10932. ' };',
  10933. '});',
  10934. 'rtl.recNewT(this, "TNested", function () {',
  10935. ' this.$new = function () {',
  10936. ' var r = Object.create(this);',
  10937. ' r.r = $mod.TRecord.$new();',
  10938. ' return r;',
  10939. ' };',
  10940. ' this.$eq = function (b) {',
  10941. ' return this.r.$eq(b.r);',
  10942. ' };',
  10943. ' this.$assign = function (s) {',
  10944. ' this.r.$assign(s.r);',
  10945. ' return this;',
  10946. ' };',
  10947. '});',
  10948. 'this.b = false;',
  10949. 'this.r = this.TRecord.$new();',
  10950. 'this.s = this.TRecord.$new();',
  10951. '']),
  10952. LinesToStr([
  10953. '$mod.b = $mod.r.$eq($mod.s);',
  10954. '$mod.b = !$mod.r.$eq($mod.s);',
  10955. '']));
  10956. end;
  10957. procedure TTestModule.TestRecord_JSValue;
  10958. begin
  10959. StartProgram(false);
  10960. Add([
  10961. 'type',
  10962. ' TRecord = record',
  10963. ' i: longint;',
  10964. ' end;',
  10965. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10966. 'begin',
  10967. 'end;',
  10968. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10969. 'begin',
  10970. ' if jsvalue(d) then ;',
  10971. ' if jsvalue(c) then ;',
  10972. ' if jsvalue(v) then ;',
  10973. 'end;',
  10974. 'var',
  10975. ' Jv: jsvalue;',
  10976. ' Rec: trecord;',
  10977. 'begin',
  10978. ' rec:=trecord(jv);',
  10979. ' jv:=rec;',
  10980. ' Fly(rec,rec);',
  10981. ' Fly(@rec,@rec);',
  10982. ' if jsvalue(Rec) then ;',
  10983. ' Run(trecord(jv),trecord(jv),rec);',
  10984. '']);
  10985. ConvertProgram;
  10986. CheckSource('TestRecord_JSValue',
  10987. LinesToStr([ // statements
  10988. 'rtl.recNewT(this, "TRecord", function () {',
  10989. ' this.i = 0;',
  10990. ' this.$eq = function (b) {',
  10991. ' return this.i === b.i;',
  10992. ' };',
  10993. ' this.$assign = function (s) {',
  10994. ' this.i = s.i;',
  10995. ' return this;',
  10996. ' };',
  10997. '});',
  10998. 'this.Fly = function (d, c) {',
  10999. '};',
  11000. 'this.Run = function (d, c, v) {',
  11001. ' if (d) ;',
  11002. ' if (c) ;',
  11003. ' if (v) ;',
  11004. '};',
  11005. 'this.Jv = undefined;',
  11006. 'this.Rec = this.TRecord.$new();',
  11007. '']),
  11008. LinesToStr([
  11009. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11010. '$mod.Jv = $mod.Rec;',
  11011. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11012. '$mod.Fly($mod.Rec, $mod.Rec);',
  11013. 'if ($mod.Rec) ;',
  11014. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11015. '']));
  11016. end;
  11017. procedure TTestModule.TestRecord_VariantFail;
  11018. begin
  11019. StartProgram(false);
  11020. Add([
  11021. 'type',
  11022. ' TRec = record',
  11023. ' case word of',
  11024. ' 0: (b0, b1: Byte);',
  11025. ' 1: (i: word);',
  11026. ' end;',
  11027. 'begin']);
  11028. SetExpectedPasResolverError('variant record is not supported',
  11029. nXIsNotSupported);
  11030. ConvertProgram;
  11031. end;
  11032. procedure TTestModule.TestRecord_FieldArray;
  11033. begin
  11034. StartProgram(false);
  11035. Add([
  11036. 'type',
  11037. ' TArrInt = array[3..4] of longint;',
  11038. ' TArrArrInt = array[3..4] of longint;',
  11039. ' TRec = record',
  11040. ' a: array of longint;',
  11041. ' s: array[1..2] of longint;',
  11042. ' m: array[1..2,3..4] of longint;',
  11043. ' o: TArrArrInt;',
  11044. ' end;',
  11045. 'begin']);
  11046. ConvertProgram;
  11047. CheckSource('TestRecord_FieldArray',
  11048. LinesToStr([ // statements
  11049. 'rtl.recNewT(this, "TRec", function () {',
  11050. ' this.$new = function () {',
  11051. ' var r = Object.create(this);',
  11052. ' r.a = [];',
  11053. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11054. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11055. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11056. ' return r;',
  11057. ' };',
  11058. ' this.$eq = function (b) {',
  11059. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11060. ' };',
  11061. ' this.$assign = function (s) {',
  11062. ' this.a = rtl.arrayRef(s.a);',
  11063. ' this.s = s.s.slice(0);',
  11064. ' this.m = s.m.slice(0);',
  11065. ' this.o = s.o.slice(0);',
  11066. ' return this;',
  11067. ' };',
  11068. '});',
  11069. '']),
  11070. LinesToStr([ // $mod.$main
  11071. '']));
  11072. end;
  11073. procedure TTestModule.TestRecord_Const;
  11074. begin
  11075. StartProgram(false);
  11076. Add([
  11077. 'type',
  11078. ' TArrInt = array[3..4] of longint;',
  11079. ' TPoint = record x,y: longint; end;',
  11080. ' TRec = record',
  11081. ' i: longint;',
  11082. ' a: array of longint;',
  11083. ' s: array[1..2] of longint;',
  11084. ' m: array[1..2,3..4] of longint;',
  11085. ' p: TPoint;',
  11086. ' end;',
  11087. ' TPoints = array of TPoint;',
  11088. 'const',
  11089. ' r: TRec = (',
  11090. ' i:1;',
  11091. ' a:(2,3);',
  11092. ' s:(4,5);',
  11093. ' m:( (11,12), (13,14) );',
  11094. ' p: (x:21; y:22)',
  11095. ' );',
  11096. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11097. 'begin']);
  11098. ConvertProgram;
  11099. CheckSource('TestRecord_Const',
  11100. LinesToStr([ // statements
  11101. 'rtl.recNewT(this, "TPoint", function () {',
  11102. ' this.x = 0;',
  11103. ' this.y = 0;',
  11104. ' this.$eq = function (b) {',
  11105. ' return (this.x === b.x) && (this.y === b.y);',
  11106. ' };',
  11107. ' this.$assign = function (s) {',
  11108. ' this.x = s.x;',
  11109. ' this.y = s.y;',
  11110. ' return this;',
  11111. ' };',
  11112. '});',
  11113. 'rtl.recNewT(this, "TRec", function () {',
  11114. ' this.i = 0;',
  11115. ' this.$new = function () {',
  11116. ' var r = Object.create(this);',
  11117. ' r.a = [];',
  11118. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11119. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11120. ' r.p = $mod.TPoint.$new();',
  11121. ' return r;',
  11122. ' };',
  11123. ' this.$eq = function (b) {',
  11124. ' 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);',
  11125. ' };',
  11126. ' this.$assign = function (s) {',
  11127. ' this.i = s.i;',
  11128. ' this.a = rtl.arrayRef(s.a);',
  11129. ' this.s = s.s.slice(0);',
  11130. ' this.m = s.m.slice(0);',
  11131. ' this.p.$assign(s.p);',
  11132. ' return this;',
  11133. ' };',
  11134. '});',
  11135. 'this.r = this.TRec.$clone({',
  11136. ' i: 1,',
  11137. ' a: [2, 3],',
  11138. ' s: [4, 5],',
  11139. ' m: [[11, 12], [13, 14]],',
  11140. ' p: this.TPoint.$clone({',
  11141. ' x: 21,',
  11142. ' y: 22',
  11143. ' })',
  11144. '});',
  11145. 'this.p = [this.TPoint.$clone({',
  11146. ' x: 1,',
  11147. ' y: 2',
  11148. '}), this.TPoint.$clone({',
  11149. ' x: 3,',
  11150. ' y: 4',
  11151. '})];',
  11152. '']),
  11153. LinesToStr([ // $mod.$main
  11154. '']));
  11155. end;
  11156. procedure TTestModule.TestRecord_TypecastFail;
  11157. begin
  11158. StartProgram(false);
  11159. Add([
  11160. 'type',
  11161. ' TPoint = record x,y: longint; end;',
  11162. ' TRec = record l: longint end;',
  11163. 'var p: TPoint;',
  11164. 'begin',
  11165. ' if TRec(p).l=2 then ;']);
  11166. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11167. nIllegalTypeConversionTo);
  11168. ConvertProgram;
  11169. end;
  11170. procedure TTestModule.TestRecord_InFunction;
  11171. begin
  11172. StartProgram(false);
  11173. Add([
  11174. 'var TPoint: longint = 3;',
  11175. 'procedure DoIt;',
  11176. 'type',
  11177. ' TPoint = record x,y: longint; end;',
  11178. ' TPoints = array of TPoint;',
  11179. 'var',
  11180. ' r: TPoint;',
  11181. ' p: TPoints;',
  11182. 'begin',
  11183. ' SetLength(p,2);',
  11184. 'end;',
  11185. 'begin']);
  11186. ConvertProgram;
  11187. CheckSource('TestRecord_InFunction',
  11188. LinesToStr([ // statements
  11189. 'this.TPoint = 3;',
  11190. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11191. ' this.x = 0;',
  11192. ' this.y = 0;',
  11193. ' this.$eq = function (b) {',
  11194. ' return (this.x === b.x) && (this.y === b.y);',
  11195. ' };',
  11196. ' this.$assign = function (s) {',
  11197. ' this.x = s.x;',
  11198. ' this.y = s.y;',
  11199. ' return this;',
  11200. ' };',
  11201. '});',
  11202. 'this.DoIt = function () {',
  11203. ' var r = TPoint$1.$new();',
  11204. ' var p = [];',
  11205. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11206. '};',
  11207. '']),
  11208. LinesToStr([ // $mod.$main
  11209. '']));
  11210. end;
  11211. procedure TTestModule.TestRecord_AnonymousFail;
  11212. begin
  11213. StartProgram(false);
  11214. Add([
  11215. 'var',
  11216. ' r: record x: word end;',
  11217. 'begin']);
  11218. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11219. nNotYetImplemented);
  11220. ConvertProgram;
  11221. end;
  11222. procedure TTestModule.TestAdvRecord_Function;
  11223. begin
  11224. StartProgram(false);
  11225. Parser.Options:=Parser.Options+[po_cassignments];
  11226. Add([
  11227. '{$modeswitch AdvancedRecords}',
  11228. 'type',
  11229. ' TPoint = record',
  11230. ' x,y: word;',
  11231. ' function Add(const apt: TPoint): TPoint;',
  11232. ' end;',
  11233. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11234. 'begin',
  11235. ' Result:=Self;',
  11236. ' Result.x+=apt.x;',
  11237. ' Result.y:=Result.y+apt.y;',
  11238. ' Self:=apt;',
  11239. 'end;',
  11240. 'var p,q: TPoint;',
  11241. 'begin',
  11242. ' p.add(q);',
  11243. ' p:=default(TPoint);',
  11244. ' p:=q;',
  11245. '']);
  11246. ConvertProgram;
  11247. CheckSource('TestAdvRecord_Function',
  11248. LinesToStr([ // statements
  11249. 'rtl.recNewT(this, "TPoint", function () {',
  11250. ' this.x = 0;',
  11251. ' this.y = 0;',
  11252. ' this.$eq = function (b) {',
  11253. ' return (this.x === b.x) && (this.y === b.y);',
  11254. ' };',
  11255. ' this.$assign = function (s) {',
  11256. ' this.x = s.x;',
  11257. ' this.y = s.y;',
  11258. ' return this;',
  11259. ' };',
  11260. ' this.Add = function (apt) {',
  11261. ' var Result = $mod.TPoint.$new();',
  11262. ' Result.$assign(this);',
  11263. ' Result.x += apt.x;',
  11264. ' Result.y = Result.y + apt.y;',
  11265. ' this.$assign(apt);',
  11266. ' return Result;',
  11267. ' };',
  11268. '});',
  11269. 'this.p = this.TPoint.$new();',
  11270. 'this.q = this.TPoint.$new();',
  11271. '']),
  11272. LinesToStr([ // $mod.$main
  11273. '$mod.p.Add($mod.q);',
  11274. '$mod.p.$assign($mod.TPoint.$new());',
  11275. '$mod.p.$assign($mod.q);',
  11276. '']));
  11277. end;
  11278. procedure TTestModule.TestAdvRecord_Property;
  11279. begin
  11280. StartProgram(false);
  11281. Add([
  11282. '{$modeswitch AdvancedRecords}',
  11283. 'type',
  11284. ' TPoint = record',
  11285. ' x,y: word;',
  11286. ' strict private',
  11287. ' function GetSize: longword;',
  11288. ' procedure SetSize(Value: longword);',
  11289. ' public',
  11290. ' property Size: longword read GetSize write SetSize;',
  11291. ' property Left: word read x write y;',
  11292. ' end;',
  11293. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11294. 'function TPoint.GetSize: longword;',
  11295. 'begin',
  11296. ' x:=y;',
  11297. ' Size:=Size;',
  11298. ' Left:=Left;',
  11299. 'end;',
  11300. 'procedure TPoint.SetSize(Value: longword);',
  11301. 'begin',
  11302. 'end;',
  11303. 'var p,q: TPoint;',
  11304. 'begin',
  11305. ' p.Size:=q.Size;',
  11306. ' p.Left:=q.Left;',
  11307. '']);
  11308. ConvertProgram;
  11309. CheckSource('TestAdvRecord_Property',
  11310. LinesToStr([ // statements
  11311. 'rtl.recNewT(this, "TPoint", function () {',
  11312. ' this.x = 0;',
  11313. ' this.y = 0;',
  11314. ' this.$eq = function (b) {',
  11315. ' return (this.x === b.x) && (this.y === b.y);',
  11316. ' };',
  11317. ' this.$assign = function (s) {',
  11318. ' this.x = s.x;',
  11319. ' this.y = s.y;',
  11320. ' return this;',
  11321. ' };',
  11322. ' this.GetSize = function () {',
  11323. ' var Result = 0;',
  11324. ' this.x = this.y;',
  11325. ' this.SetSize(this.GetSize());',
  11326. ' this.y = this.x;',
  11327. ' return Result;',
  11328. ' };',
  11329. ' this.SetSize = function (Value) {',
  11330. ' };',
  11331. '});',
  11332. 'this.SetSize = function (Value) {',
  11333. '};',
  11334. 'this.p = this.TPoint.$new();',
  11335. 'this.q = this.TPoint.$new();',
  11336. '']),
  11337. LinesToStr([ // $mod.$main
  11338. '$mod.p.SetSize($mod.q.GetSize());',
  11339. '$mod.p.y = $mod.q.x;',
  11340. '']));
  11341. end;
  11342. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11343. begin
  11344. StartProgram(false);
  11345. Add([
  11346. '{$modeswitch AdvancedRecords}',
  11347. 'type',
  11348. ' TPoint = record',
  11349. ' strict private',
  11350. ' function GetItems(Index: word): word;',
  11351. ' procedure SetItems(Index: word; Value: word);',
  11352. ' public',
  11353. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11354. ' end;',
  11355. 'function TPoint.GetItems(Index: word): word;',
  11356. 'begin',
  11357. ' Items[index]:=Items[index];',
  11358. ' self.Items[index]:=self.Items[index];',
  11359. 'end;',
  11360. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11361. 'begin',
  11362. 'end;',
  11363. 'var p: TPoint;',
  11364. 'begin',
  11365. ' p[1]:=p[2];',
  11366. ' p.Items[3]:=p.Items[4];',
  11367. '']);
  11368. ConvertProgram;
  11369. CheckSource('TestAdvRecord_PropertyDefault',
  11370. LinesToStr([ // statements
  11371. 'rtl.recNewT(this, "TPoint", function () {',
  11372. ' this.$eq = function (b) {',
  11373. ' return true;',
  11374. ' };',
  11375. ' this.$assign = function (s) {',
  11376. ' return this;',
  11377. ' };',
  11378. ' this.GetItems = function (Index) {',
  11379. ' var Result = 0;',
  11380. ' this.SetItems(Index, this.GetItems(Index));',
  11381. ' this.SetItems(Index, this.GetItems(Index));',
  11382. ' return Result;',
  11383. ' };',
  11384. ' this.SetItems = function (Index, Value) {',
  11385. ' };',
  11386. '});',
  11387. 'this.p = this.TPoint.$new();',
  11388. '']),
  11389. LinesToStr([ // $mod.$main
  11390. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11391. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11392. '']));
  11393. end;
  11394. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11395. begin
  11396. StartProgram(false);
  11397. Add([
  11398. '{$modeswitch AdvancedRecords}',
  11399. 'type',
  11400. ' TRec = record',
  11401. ' class var',
  11402. ' Fx: longint;',
  11403. ' Fy: longint;',
  11404. ' class function GetInt: longint; static;',
  11405. ' class procedure SetInt(Value: longint); static;',
  11406. ' class procedure DoIt; static;',
  11407. ' class property IntA: longint read Fx write Fy;',
  11408. ' class property IntB: longint read GetInt write SetInt;',
  11409. ' end;',
  11410. 'class function trec.getint: longint;',
  11411. 'begin',
  11412. ' result:=fx;',
  11413. 'end;',
  11414. 'class procedure trec.setint(value: longint);',
  11415. 'begin',
  11416. 'end;',
  11417. 'class procedure trec.doit;',
  11418. 'begin',
  11419. ' IntA:=IntA+1;',
  11420. ' IntB:=IntB+1;',
  11421. 'end;',
  11422. 'var r: trec;',
  11423. 'begin',
  11424. ' trec.inta:=trec.inta+1;',
  11425. ' if trec.intb=2 then;',
  11426. ' trec.intb:=trec.intb+2;',
  11427. ' trec.setint(trec.inta);',
  11428. ' r.inta:=r.inta+1;',
  11429. ' if r.intb=2 then;',
  11430. ' r.intb:=r.intb+2;',
  11431. ' r.setint(r.inta);']);
  11432. ConvertProgram;
  11433. CheckSource('TestAdvRecord_Property_ClassMethod',
  11434. LinesToStr([ // statements
  11435. 'rtl.recNewT(this, "TRec", function () {',
  11436. ' this.Fx = 0;',
  11437. ' this.Fy = 0;',
  11438. ' this.$eq = function (b) {',
  11439. ' return true;',
  11440. ' };',
  11441. ' this.$assign = function (s) {',
  11442. ' return this;',
  11443. ' };',
  11444. ' this.GetInt = function () {',
  11445. ' var Result = 0;',
  11446. ' Result = $mod.TRec.Fx;',
  11447. ' return Result;',
  11448. ' };',
  11449. ' this.SetInt = function (Value) {',
  11450. ' };',
  11451. ' this.DoIt = function () {',
  11452. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11453. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11454. ' };',
  11455. '}, true);',
  11456. 'this.r = this.TRec.$new();',
  11457. '']),
  11458. LinesToStr([ // $mod.$main
  11459. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11460. 'if ($mod.TRec.GetInt() === 2) ;',
  11461. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11462. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11463. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11464. 'if ($mod.r.GetInt() === 2) ;',
  11465. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11466. '$mod.r.SetInt($mod.r.Fx);',
  11467. '']));
  11468. end;
  11469. procedure TTestModule.TestAdvRecord_Const;
  11470. begin
  11471. StartProgram(false);
  11472. Add([
  11473. '{$modeswitch AdvancedRecords}',
  11474. 'type',
  11475. ' TArrInt = array[3..4] of longint;',
  11476. ' TPoint = record',
  11477. ' x,y: longint;',
  11478. ' class var Count: nativeint;',
  11479. ' end;',
  11480. ' TRec = record',
  11481. ' i: longint;',
  11482. ' a: array of longint;',
  11483. ' s: array[1..2] of longint;',
  11484. ' m: array[1..2,3..4] of longint;',
  11485. ' p: TPoint;',
  11486. ' end;',
  11487. ' TPoints = array of TPoint;',
  11488. 'const',
  11489. ' r: TRec = (',
  11490. ' i:1;',
  11491. ' a:(2,3);',
  11492. ' s:(4,5);',
  11493. ' m:( (11,12), (13,14) );',
  11494. ' p: (x:21)',
  11495. ' );',
  11496. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11497. 'begin']);
  11498. ConvertProgram;
  11499. CheckSource('TestAdvRecord_Const',
  11500. LinesToStr([ // statements
  11501. 'rtl.recNewT(this, "TPoint", function () {',
  11502. ' this.x = 0;',
  11503. ' this.y = 0;',
  11504. ' this.Count = 0;',
  11505. ' this.$eq = function (b) {',
  11506. ' return (this.x === b.x) && (this.y === b.y);',
  11507. ' };',
  11508. ' this.$assign = function (s) {',
  11509. ' this.x = s.x;',
  11510. ' this.y = s.y;',
  11511. ' return this;',
  11512. ' };',
  11513. '}, true);',
  11514. 'rtl.recNewT(this, "TRec", function () {',
  11515. ' this.i = 0;',
  11516. ' this.$new = function () {',
  11517. ' var r = Object.create(this);',
  11518. ' r.a = [];',
  11519. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11520. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11521. ' r.p = $mod.TPoint.$new();',
  11522. ' return r;',
  11523. ' };',
  11524. ' this.$eq = function (b) {',
  11525. ' 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);',
  11526. ' };',
  11527. ' this.$assign = function (s) {',
  11528. ' this.i = s.i;',
  11529. ' this.a = rtl.arrayRef(s.a);',
  11530. ' this.s = s.s.slice(0);',
  11531. ' this.m = s.m.slice(0);',
  11532. ' this.p.$assign(s.p);',
  11533. ' return this;',
  11534. ' };',
  11535. '});',
  11536. 'this.r = this.TRec.$clone({',
  11537. ' i: 1,',
  11538. ' a: [2, 3],',
  11539. ' s: [4, 5],',
  11540. ' m: [[11, 12], [13, 14]],',
  11541. ' p: this.TPoint.$clone({',
  11542. ' x: 21,',
  11543. ' y: 0',
  11544. ' })',
  11545. '});',
  11546. 'this.p = [this.TPoint.$clone({',
  11547. ' x: 1,',
  11548. ' y: 2',
  11549. '}), this.TPoint.$clone({',
  11550. ' x: 3,',
  11551. ' y: 4',
  11552. '})];',
  11553. '']),
  11554. LinesToStr([ // $mod.$main
  11555. '']));
  11556. end;
  11557. procedure TTestModule.TestAdvRecord_ExternalField;
  11558. begin
  11559. StartProgram(false);
  11560. Add([
  11561. '{$modeswitch AdvancedRecords}',
  11562. '{$modeswitch externalclass}',
  11563. 'type',
  11564. ' TCar = record',
  11565. ' public',
  11566. ' Intern: longint external name ''$Intern'';',
  11567. ' Intern2: longint external name ''$Intern2'';',
  11568. ' Bracket: longint external name ''["A B"]'';',
  11569. ' procedure DoIt;',
  11570. ' end;',
  11571. 'procedure tcar.doit;',
  11572. 'begin',
  11573. ' Intern:=Intern+1;',
  11574. ' Intern2:=Intern2+2;',
  11575. ' Bracket:=Bracket+3;',
  11576. 'end;',
  11577. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11578. 'begin',
  11579. ' Rec.intern:=Rec.intern+1;',
  11580. ' Rec.intern2:=Rec.intern2+2;',
  11581. ' Rec.Bracket:=Rec.Bracket+3;',
  11582. ' with Rec do begin',
  11583. ' intern:=intern+1;',
  11584. ' intern2:=intern2+2;',
  11585. ' Bracket:=Bracket+3;',
  11586. ' end;']);
  11587. ConvertProgram;
  11588. CheckSource('TestAdvRecord_ExternalField',
  11589. LinesToStr([ // statements
  11590. 'rtl.recNewT(this, "TCar", function () {',
  11591. ' this.$eq = function (b) {',
  11592. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11593. ' };',
  11594. ' this.$assign = function (s) {',
  11595. ' this.$Intern = s.$Intern;',
  11596. ' this.$Intern2 = s.$Intern2;',
  11597. ' this["A B"] = s["A B"];',
  11598. ' return this;',
  11599. ' };',
  11600. ' this.DoIt = function () {',
  11601. ' this.$Intern = this.$Intern + 1;',
  11602. ' this.$Intern2 = this.$Intern2 + 2;',
  11603. ' this["A B"] = this["A B"] + 3;',
  11604. ' };',
  11605. '});',
  11606. 'this.Rec = this.TCar.$clone({',
  11607. ' $Intern: 11,',
  11608. ' $Intern2: 12,',
  11609. ' "A B": 13',
  11610. '});',
  11611. '']),
  11612. LinesToStr([ // $mod.$main
  11613. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11614. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11615. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11616. 'var $with = $mod.Rec;',
  11617. '$with.$Intern = $with.$Intern + 1;',
  11618. '$with.$Intern2 = $with.$Intern2 + 2;',
  11619. '$with["A B"] = $with["A B"] + 3;',
  11620. '']));
  11621. end;
  11622. procedure TTestModule.TestAdvRecord_SubRecord;
  11623. begin
  11624. StartProgram(false);
  11625. Add([
  11626. '{$modeswitch AdvancedRecords}',
  11627. 'type',
  11628. ' TRec = record',
  11629. ' type',
  11630. ' TPoint = record',
  11631. ' x,y: longint;',
  11632. ' class var Count: nativeint;',
  11633. ' procedure DoIt;',
  11634. ' class procedure DoThat; static;',
  11635. ' end;',
  11636. ' var',
  11637. ' i: longint;',
  11638. ' p: TPoint;',
  11639. ' procedure DoSome;',
  11640. ' end;',
  11641. 'const',
  11642. ' r: TRec = (',
  11643. ' i:1;',
  11644. ' p: (x:21;y:22)',
  11645. ' );',
  11646. 'procedure TRec.DoSome;',
  11647. 'begin',
  11648. ' p.x:=p.y+1;',
  11649. ' p.Count:=p.Count+2;',
  11650. 'end;',
  11651. 'procedure TRec.TPoint.DoIt;',
  11652. 'begin',
  11653. ' Count:=Count+3;',
  11654. 'end;',
  11655. 'class procedure TRec.TPoint.DoThat;',
  11656. 'begin',
  11657. ' Count:=Count+4;',
  11658. 'end;',
  11659. 'begin']);
  11660. ConvertProgram;
  11661. CheckSource('TestAdvRecord_SubRecord',
  11662. LinesToStr([ // statements
  11663. 'rtl.recNewT(this, "TRec", function () {',
  11664. ' rtl.recNewT(this, "TPoint", function () {',
  11665. ' this.x = 0;',
  11666. ' this.y = 0;',
  11667. ' this.Count = 0;',
  11668. ' this.$eq = function (b) {',
  11669. ' return (this.x === b.x) && (this.y === b.y);',
  11670. ' };',
  11671. ' this.$assign = function (s) {',
  11672. ' this.x = s.x;',
  11673. ' this.y = s.y;',
  11674. ' return this;',
  11675. ' };',
  11676. ' this.DoIt = function () {',
  11677. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11678. ' };',
  11679. ' this.DoThat = function () {',
  11680. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11681. ' };',
  11682. ' }, true);',
  11683. ' this.i = 0;',
  11684. ' this.$new = function () {',
  11685. ' var r = Object.create(this);',
  11686. ' r.p = this.TPoint.$new();',
  11687. ' return r;',
  11688. ' };',
  11689. ' this.$eq = function (b) {',
  11690. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11691. ' };',
  11692. ' this.$assign = function (s) {',
  11693. ' this.i = s.i;',
  11694. ' this.p.$assign(s.p);',
  11695. ' return this;',
  11696. ' };',
  11697. ' this.DoSome = function () {',
  11698. ' this.p.x = this.p.y + 1;',
  11699. ' this.TPoint.Count = this.p.Count + 2;',
  11700. ' };',
  11701. '}, true);',
  11702. 'this.r = this.TRec.$clone({',
  11703. ' i: 1,',
  11704. ' p: this.TRec.TPoint.$clone({',
  11705. ' x: 21,',
  11706. ' y: 22',
  11707. ' })',
  11708. '});',
  11709. '']),
  11710. LinesToStr([ // $mod.$main
  11711. '']));
  11712. end;
  11713. procedure TTestModule.TestAdvRecord_SubClass;
  11714. begin
  11715. StartProgram(false);
  11716. Add([
  11717. '{$modeswitch AdvancedRecords}',
  11718. 'type',
  11719. ' TObject = class end;',
  11720. ' TPoint = record',
  11721. ' type',
  11722. ' TBird = class',
  11723. ' procedure DoIt;',
  11724. ' class procedure Glob;',
  11725. ' end;',
  11726. ' procedure DoIt(b: TBird);',
  11727. ' end;',
  11728. 'procedure TPoint.TBird.DoIt;',
  11729. 'begin',
  11730. ' doit;',
  11731. ' self.doit;',
  11732. ' glob;',
  11733. ' self.glob;',
  11734. 'end;',
  11735. 'class procedure TPoint.TBird.Glob;',
  11736. 'begin',
  11737. ' glob;',
  11738. ' self.glob;',
  11739. 'end;',
  11740. 'procedure TPoint.DoIt(b: TBird);',
  11741. 'begin',
  11742. ' b.doit;',
  11743. ' b.glob;',
  11744. ' TBird.glob;',
  11745. 'end;',
  11746. 'begin',
  11747. '']);
  11748. ConvertProgram;
  11749. CheckSource('TestAdvRecord_SubClass',
  11750. LinesToStr([ // statements
  11751. 'rtl.createClass(this, "TObject", null, function () {',
  11752. ' this.$init = function () {',
  11753. ' };',
  11754. ' this.$final = function () {',
  11755. ' };',
  11756. '});',
  11757. 'rtl.recNewT(this, "TPoint", function () {',
  11758. ' rtl.createClass(this, "TBird", this.TObject, function () {',
  11759. ' this.DoIt = function () {',
  11760. ' this.DoIt();',
  11761. ' this.DoIt();',
  11762. ' this.$class.Glob();',
  11763. ' this.$class.Glob();',
  11764. ' };',
  11765. ' this.Glob = function () {',
  11766. ' this.Glob();',
  11767. ' this.Glob();',
  11768. ' };',
  11769. ' }, "TPoint.TBird");',
  11770. ' this.$eq = function (b) {',
  11771. ' return true;',
  11772. ' };',
  11773. ' this.$assign = function (s) {',
  11774. ' return this;',
  11775. ' };',
  11776. ' this.DoIt = function (b) {',
  11777. ' b.DoIt();',
  11778. ' b.$class.Glob();',
  11779. ' this.TBird.Glob();',
  11780. ' };',
  11781. '}, true);',
  11782. '']),
  11783. LinesToStr([ // $mod.$main
  11784. '']));
  11785. end;
  11786. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11787. begin
  11788. StartProgram(false);
  11789. Add([
  11790. '{$modeswitch AdvancedRecords}',
  11791. 'type',
  11792. ' IUnknown = interface end;',
  11793. ' TPoint = record',
  11794. ' type IBird = interface end;',
  11795. ' end;',
  11796. 'begin',
  11797. '']);
  11798. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11799. nNotYetImplemented);
  11800. ParseProgram;
  11801. end;
  11802. procedure TTestModule.TestAdvRecord_Constructor;
  11803. begin
  11804. StartProgram(false);
  11805. Add([
  11806. '{$modeswitch AdvancedRecords}',
  11807. 'type',
  11808. ' TPoint = record',
  11809. ' x,y: longint;',
  11810. ' constructor Create(ax: longint; ay: longint = -1);',
  11811. ' end;',
  11812. 'constructor tpoint.create(ax,ay: longint);',
  11813. 'begin',
  11814. ' x:=ax;',
  11815. ' self.y:=ay;',
  11816. 'end;',
  11817. 'var r: TPoint;',
  11818. 'begin',
  11819. ' r:=TPoint.Create(1,2);',
  11820. ' with TPoint do r:=Create(1,2);',
  11821. ' r.Create(3);',
  11822. ' r:=r.Create(4);',
  11823. '']);
  11824. ConvertProgram;
  11825. CheckSource('TestAdvRecord_Constructor',
  11826. LinesToStr([ // statements
  11827. 'rtl.recNewT(this, "TPoint", function () {',
  11828. ' this.x = 0;',
  11829. ' this.y = 0;',
  11830. ' this.$eq = function (b) {',
  11831. ' return (this.x === b.x) && (this.y === b.y);',
  11832. ' };',
  11833. ' this.$assign = function (s) {',
  11834. ' this.x = s.x;',
  11835. ' this.y = s.y;',
  11836. ' return this;',
  11837. ' };',
  11838. ' this.Create = function (ax, ay) {',
  11839. ' this.x = ax;',
  11840. ' this.y = ay;',
  11841. ' return this;',
  11842. ' };',
  11843. '}, true);',
  11844. 'this.r = this.TPoint.$new();',
  11845. '']),
  11846. LinesToStr([ // $mod.$main
  11847. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11848. 'var $with = $mod.TPoint;',
  11849. '$mod.r.$assign($with.$new().Create(1, 2));',
  11850. '$mod.r.Create(3, -1);',
  11851. '$mod.r.$assign($mod.r.Create(4, -1));',
  11852. '']));
  11853. end;
  11854. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11855. begin
  11856. StartProgram(false);
  11857. Add([
  11858. '{$modeswitch AdvancedRecords}',
  11859. 'type',
  11860. ' TPoint = record',
  11861. ' class var x: longint;',
  11862. ' class procedure Fly; static;',
  11863. ' class constructor Init;',
  11864. ' end;',
  11865. 'var count: word;',
  11866. 'class procedure Tpoint.Fly;',
  11867. 'begin',
  11868. 'end;',
  11869. 'class constructor tpoint.init;',
  11870. 'begin',
  11871. ' count:=count+1;',
  11872. ' x:=x+3;',
  11873. ' tpoint.x:=tpoint.x+4;',
  11874. ' fly;',
  11875. ' tpoint.fly;',
  11876. 'end;',
  11877. 'var r: TPoint;',
  11878. 'begin',
  11879. ' r.x:=r.x+10;',
  11880. ' r.Fly;',
  11881. ' r.Fly();',
  11882. '']);
  11883. ConvertProgram;
  11884. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11885. LinesToStr([ // statements
  11886. 'rtl.recNewT(this, "TPoint", function () {',
  11887. ' this.x = 0;',
  11888. ' this.$eq = function (b) {',
  11889. ' return true;',
  11890. ' };',
  11891. ' this.$assign = function (s) {',
  11892. ' return this;',
  11893. ' };',
  11894. ' this.Fly = function () {',
  11895. ' };',
  11896. '}, true);',
  11897. 'this.count = 0;',
  11898. 'this.r = this.TPoint.$new();',
  11899. '']),
  11900. LinesToStr([ // $mod.$main
  11901. '(function () {',
  11902. ' $mod.count = $mod.count + 1;',
  11903. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11904. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11905. ' $mod.TPoint.Fly();',
  11906. ' $mod.TPoint.Fly();',
  11907. '})();',
  11908. '$mod.TPoint.x = $mod.r.x + 10;',
  11909. '$mod.r.Fly();',
  11910. '$mod.r.Fly();',
  11911. '']));
  11912. end;
  11913. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11914. begin
  11915. StartUnit(false);
  11916. Add([
  11917. 'interface',
  11918. '{$modeswitch AdvancedRecords}',
  11919. 'type',
  11920. ' TPoint = record',
  11921. ' class var x: longint;',
  11922. ' class procedure Fly; static;',
  11923. ' class constructor Init;',
  11924. ' end;',
  11925. 'implementation',
  11926. 'var count: word;',
  11927. 'class procedure Tpoint.Fly;',
  11928. 'begin',
  11929. 'end;',
  11930. 'class constructor tpoint.init;',
  11931. 'begin',
  11932. ' count:=count+1;',
  11933. ' x:=3;',
  11934. ' tpoint.x:=4;',
  11935. ' fly;',
  11936. ' tpoint.fly;',
  11937. 'end;',
  11938. '']);
  11939. ConvertUnit;
  11940. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11941. LinesToStr([ // statements
  11942. 'var $impl = $mod.$impl;',
  11943. 'rtl.recNewT(this, "TPoint", function () {',
  11944. ' this.x = 0;',
  11945. ' this.$eq = function (b) {',
  11946. ' return true;',
  11947. ' };',
  11948. ' this.$assign = function (s) {',
  11949. ' return this;',
  11950. ' };',
  11951. ' this.Fly = function () {',
  11952. ' };',
  11953. '}, true);',
  11954. '']),
  11955. LinesToStr([ // $mod.$init
  11956. '(function () {',
  11957. ' $impl.count = $impl.count + 1;',
  11958. ' $mod.TPoint.x = 3;',
  11959. ' $mod.TPoint.x = 4;',
  11960. ' $mod.TPoint.Fly();',
  11961. ' $mod.TPoint.Fly();',
  11962. '})();',
  11963. '']),
  11964. LinesToStr([ // $mod.$main
  11965. '$impl.count = 0;',
  11966. '']));
  11967. end;
  11968. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11969. begin
  11970. StartProgram(false);
  11971. Add(['type',
  11972. ' TObject = class',
  11973. ' public',
  11974. ' constructor Create;',
  11975. ' destructor Destroy;',
  11976. ' end;',
  11977. ' TBird = TObject;',
  11978. 'constructor tobject.create;',
  11979. 'begin end;',
  11980. 'destructor tobject.destroy;',
  11981. 'begin end;',
  11982. 'var Obj: tobject;',
  11983. 'begin',
  11984. ' obj:=tobject.create;',
  11985. ' obj:=tobject.create();',
  11986. ' obj:=tbird.create;',
  11987. ' obj:=tbird.create();',
  11988. ' obj:=obj.create();',
  11989. ' obj.destroy;',
  11990. '']);
  11991. ConvertProgram;
  11992. CheckSource('TestClass_TObjectDefaultConstructor',
  11993. LinesToStr([ // statements
  11994. 'rtl.createClass(this,"TObject",null,function(){',
  11995. ' this.$init = function () {',
  11996. ' };',
  11997. ' this.$final = function () {',
  11998. ' };',
  11999. ' this.Create = function(){',
  12000. ' return this;',
  12001. ' };',
  12002. ' this.Destroy = function(){',
  12003. ' };',
  12004. '});',
  12005. 'this.Obj = null;'
  12006. ]),
  12007. LinesToStr([ // $mod.$main
  12008. '$mod.Obj = $mod.TObject.$create("Create");',
  12009. '$mod.Obj = $mod.TObject.$create("Create");',
  12010. '$mod.Obj = $mod.TObject.$create("Create");',
  12011. '$mod.Obj = $mod.TObject.$create("Create");',
  12012. '$mod.Obj = $mod.Obj.Create();',
  12013. '$mod.Obj.$destroy("Destroy");',
  12014. '']));
  12015. end;
  12016. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12017. begin
  12018. StartProgram(false);
  12019. Add('type');
  12020. Add(' TObject = class');
  12021. Add(' public');
  12022. Add(' constructor Create(Par: longint);');
  12023. Add(' end;');
  12024. Add('constructor tobject.create(par: longint);');
  12025. Add('begin end;');
  12026. Add('var Obj: tobject;');
  12027. Add('begin');
  12028. Add(' obj:=tobject.create(3);');
  12029. ConvertProgram;
  12030. CheckSource('TestClass_TObjectConstructorWithParams',
  12031. LinesToStr([ // statements
  12032. 'rtl.createClass(this,"TObject",null,function(){',
  12033. ' this.$init = function () {',
  12034. ' };',
  12035. ' this.$final = function () {',
  12036. ' };',
  12037. ' this.Create = function(Par){',
  12038. ' return this;',
  12039. ' };',
  12040. '});',
  12041. 'this.Obj = null;'
  12042. ]),
  12043. LinesToStr([ // $mod.$main
  12044. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12045. ]));
  12046. end;
  12047. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12048. begin
  12049. StartProgram(false);
  12050. Add('type');
  12051. Add(' TObject = class');
  12052. Add(' public');
  12053. Add(' constructor Create;');
  12054. Add(' end;');
  12055. Add(' TTest = class(TObject)');
  12056. Add(' public');
  12057. Add(' constructor Create(const Par: longint = 1);');
  12058. Add(' end;');
  12059. Add('constructor tobject.create;');
  12060. Add('begin end;');
  12061. Add('constructor ttest.create(const par: longint);');
  12062. Add('begin end;');
  12063. Add('var t: ttest;');
  12064. Add('begin');
  12065. Add(' t:=ttest.create;');
  12066. Add(' t:=ttest.create(2);');
  12067. ConvertProgram;
  12068. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12069. LinesToStr([ // statements
  12070. 'rtl.createClass(this,"TObject",null,function(){',
  12071. ' this.$init = function () {',
  12072. ' };',
  12073. ' this.$final = function () {',
  12074. ' };',
  12075. ' this.Create = function(){',
  12076. ' return this;',
  12077. ' };',
  12078. '});',
  12079. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12080. ' this.Create$1 = function (Par) {',
  12081. ' return this;',
  12082. ' };',
  12083. '});',
  12084. 'this.t = null;'
  12085. ]),
  12086. LinesToStr([ // $mod.$main
  12087. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12088. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12089. ]));
  12090. end;
  12091. procedure TTestModule.TestClass_Var;
  12092. begin
  12093. StartProgram(false);
  12094. Add([
  12095. 'type',
  12096. ' TObject = class',
  12097. ' public',
  12098. ' vI: longint;',
  12099. ' constructor Create(Par: longint);',
  12100. ' end;',
  12101. 'constructor tobject.create(par: longint);',
  12102. 'begin',
  12103. ' vi:=par+3',
  12104. 'end;',
  12105. 'var Obj: tobject;',
  12106. 'begin',
  12107. ' obj:=tobject.create(4);',
  12108. ' obj.vi:=obj.VI+5;']);
  12109. ConvertProgram;
  12110. CheckSource('TestClass_Var',
  12111. LinesToStr([ // statements
  12112. 'rtl.createClass(this,"TObject",null,function(){',
  12113. ' this.$init = function () {',
  12114. ' this.vI = 0;',
  12115. ' };',
  12116. ' this.$final = function () {',
  12117. ' };',
  12118. ' this.Create = function(Par){',
  12119. ' this.vI = Par+3;',
  12120. ' return this;',
  12121. ' };',
  12122. '});',
  12123. 'this.Obj = null;'
  12124. ]),
  12125. LinesToStr([ // $mod.$main
  12126. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12127. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12128. ]));
  12129. end;
  12130. procedure TTestModule.TestClass_Method;
  12131. begin
  12132. StartProgram(false);
  12133. Add('type');
  12134. Add(' TObject = class');
  12135. Add(' public');
  12136. Add(' vI: longint;');
  12137. Add(' Sub: TObject;');
  12138. Add(' constructor Create;');
  12139. Add(' function GetIt(Par: longint): tobject;');
  12140. Add(' end;');
  12141. Add('constructor tobject.create; begin end;');
  12142. Add('function tobject.getit(par: longint): tobject;');
  12143. Add('begin');
  12144. Add(' Self.vi:=par+3;');
  12145. Add(' Result:=self.sub;');
  12146. Add('end;');
  12147. Add('var Obj: tobject;');
  12148. Add('begin');
  12149. Add(' obj:=tobject.create;');
  12150. Add(' obj.getit(4);');
  12151. Add(' obj.sub.sub:=nil;');
  12152. Add(' obj.sub.getit(5);');
  12153. Add(' obj.sub.getit(6).SUB:=nil;');
  12154. Add(' obj.sub.getit(7).GETIT(8);');
  12155. Add(' obj.sub.getit(9).SuB.getit(10);');
  12156. ConvertProgram;
  12157. CheckSource('TestClass_Method',
  12158. LinesToStr([ // statements
  12159. 'rtl.createClass(this,"TObject",null,function(){',
  12160. ' this.$init = function () {',
  12161. ' this.vI = 0;',
  12162. ' this.Sub = null;',
  12163. ' };',
  12164. ' this.$final = function () {',
  12165. ' this.Sub = undefined;',
  12166. ' };',
  12167. ' this.Create = function(){',
  12168. ' return this;',
  12169. ' };',
  12170. ' this.GetIt = function(Par){',
  12171. ' var Result = null;',
  12172. ' this.vI = Par + 3;',
  12173. ' Result = this.Sub;',
  12174. ' return Result;',
  12175. ' };',
  12176. '});',
  12177. 'this.Obj = null;'
  12178. ]),
  12179. LinesToStr([ // $mod.$main
  12180. '$mod.Obj = $mod.TObject.$create("Create");',
  12181. '$mod.Obj.GetIt(4);',
  12182. '$mod.Obj.Sub.Sub=null;',
  12183. '$mod.Obj.Sub.GetIt(5);',
  12184. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12185. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12186. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12187. ]));
  12188. end;
  12189. procedure TTestModule.TestClass_Implementation;
  12190. begin
  12191. StartUnit(false);
  12192. Add([
  12193. 'interface',
  12194. 'type',
  12195. ' TObject = class',
  12196. ' constructor Create;',
  12197. ' end;',
  12198. 'implementation',
  12199. 'type',
  12200. ' TIntClass = class',
  12201. ' constructor Create; reintroduce;',
  12202. ' class procedure DoGlob;',
  12203. ' end;',
  12204. 'constructor tintclass.create;',
  12205. 'begin',
  12206. ' inherited;',
  12207. ' inherited create;',
  12208. ' doglob;',
  12209. 'end;',
  12210. 'class procedure tintclass.doglob;',
  12211. 'begin',
  12212. 'end;',
  12213. 'constructor tobject.create;',
  12214. 'var',
  12215. ' iC: tintclass;',
  12216. 'begin',
  12217. ' ic:=tintclass.create;',
  12218. ' tintclass.doglob;',
  12219. ' ic.doglob;',
  12220. 'end;',
  12221. 'initialization',
  12222. ' tintclass.doglob;',
  12223. '']);
  12224. ConvertUnit;
  12225. CheckSource('TestClass_Implementation',
  12226. LinesToStr([ // statements
  12227. 'var $impl = $mod.$impl;',
  12228. 'rtl.createClass(this, "TObject", null, function () {',
  12229. ' this.$init = function () {',
  12230. ' };',
  12231. ' this.$final = function () {',
  12232. ' };',
  12233. ' this.Create = function () {',
  12234. ' var iC = null;',
  12235. ' iC = $impl.TIntClass.$create("Create$1");',
  12236. ' $impl.TIntClass.DoGlob();',
  12237. ' iC.$class.DoGlob();',
  12238. ' return this;',
  12239. ' };',
  12240. '});',
  12241. '']),
  12242. LinesToStr([ // $mod.$main
  12243. '$impl.TIntClass.DoGlob();',
  12244. '']),
  12245. LinesToStr([
  12246. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12247. ' this.Create$1 = function () {',
  12248. ' $mod.TObject.Create.call(this);',
  12249. ' $mod.TObject.Create.call(this);',
  12250. ' this.$class.DoGlob();',
  12251. ' return this;',
  12252. ' };',
  12253. ' this.DoGlob = function () {',
  12254. ' };',
  12255. '});',
  12256. '']));
  12257. end;
  12258. procedure TTestModule.TestClass_Inheritance;
  12259. begin
  12260. StartProgram(false);
  12261. Add('type');
  12262. Add(' TObject = class');
  12263. Add(' public');
  12264. Add(' constructor Create;');
  12265. Add(' end;');
  12266. Add(' TClassA = class');
  12267. Add(' end;');
  12268. Add(' TClassB = class(TObject)');
  12269. Add(' procedure ProcB;');
  12270. Add(' end;');
  12271. Add('constructor tobject.create; begin end;');
  12272. Add('procedure tclassb.procb; begin end;');
  12273. Add('var');
  12274. Add(' oO: TObject;');
  12275. Add(' oA: TClassA;');
  12276. Add(' oB: TClassB;');
  12277. Add('begin');
  12278. Add(' oO:=tobject.Create;');
  12279. Add(' oA:=tclassa.Create;');
  12280. Add(' ob:=tclassb.Create;');
  12281. Add(' if oo is tclassa then ;');
  12282. Add(' ob:=oo as tclassb;');
  12283. Add(' (oo as tclassb).procb;');
  12284. ConvertProgram;
  12285. CheckSource('TestClass_Inheritance',
  12286. LinesToStr([ // statements
  12287. 'rtl.createClass(this,"TObject",null,function(){',
  12288. ' this.$init = function () {',
  12289. ' };',
  12290. ' this.$final = function () {',
  12291. ' };',
  12292. ' this.Create = function () {',
  12293. ' return this;',
  12294. ' };',
  12295. '});',
  12296. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12297. '});',
  12298. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12299. ' this.ProcB = function () {',
  12300. ' };',
  12301. '});',
  12302. 'this.oO = null;',
  12303. 'this.oA = null;',
  12304. 'this.oB = null;'
  12305. ]),
  12306. LinesToStr([ // $mod.$main
  12307. '$mod.oO = $mod.TObject.$create("Create");',
  12308. '$mod.oA = $mod.TClassA.$create("Create");',
  12309. '$mod.oB = $mod.TClassB.$create("Create");',
  12310. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12311. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12312. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12313. ]));
  12314. end;
  12315. procedure TTestModule.TestClass_TypeAlias;
  12316. begin
  12317. StartProgram(false);
  12318. Add([
  12319. '{$interfaces corba}',
  12320. 'type',
  12321. ' IObject = interface',
  12322. ' end;',
  12323. ' IBird = type IObject;',
  12324. ' TObject = class',
  12325. ' end;',
  12326. ' TBird = type TObject;',
  12327. 'var',
  12328. ' oObj: TObject;',
  12329. ' oBird: TBird;',
  12330. ' IntfObj: IObject;',
  12331. ' IntfBird: IBird;',
  12332. 'begin',
  12333. ' oObj:=oBird;',
  12334. '']);
  12335. ConvertProgram;
  12336. CheckSource('TestClass_TypeAlias',
  12337. LinesToStr([ // statements
  12338. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12339. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12340. 'rtl.createClass(this, "TObject", null, function () {',
  12341. ' this.$init = function () {',
  12342. ' };',
  12343. ' this.$final = function () {',
  12344. ' };',
  12345. '});',
  12346. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12347. '});',
  12348. 'this.oObj = null;',
  12349. 'this.oBird = null;',
  12350. 'this.IntfObj = null;',
  12351. 'this.IntfBird = null;',
  12352. '']),
  12353. LinesToStr([ // $mod.$main
  12354. '$mod.oObj = $mod.oBird;',
  12355. '']));
  12356. end;
  12357. procedure TTestModule.TestClass_AbstractMethod;
  12358. begin
  12359. StartProgram(false);
  12360. Add('type');
  12361. Add(' TObject = class');
  12362. Add(' public');
  12363. Add(' procedure DoIt; virtual; abstract;');
  12364. Add(' end;');
  12365. Add('begin');
  12366. ConvertProgram;
  12367. CheckSource('TestClass_AbstractMethod',
  12368. LinesToStr([ // statements
  12369. 'rtl.createClass(this,"TObject",null,function(){',
  12370. ' this.$init = function () {',
  12371. ' };',
  12372. ' this.$final = function () {',
  12373. ' };',
  12374. '});'
  12375. ]),
  12376. LinesToStr([ // this.$main
  12377. ''
  12378. ]));
  12379. end;
  12380. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12381. begin
  12382. StartProgram(false);
  12383. Add([
  12384. 'type',
  12385. ' TObject = class',
  12386. ' procedure DoAbstract; virtual; abstract;',
  12387. ' procedure DoVirtual; virtual;',
  12388. ' procedure DoIt;',
  12389. ' end;',
  12390. ' TA = class',
  12391. ' procedure doabstract; override;',
  12392. ' procedure dovirtual; override;',
  12393. ' procedure DoSome;',
  12394. ' end;',
  12395. 'procedure tobject.dovirtual;',
  12396. 'begin',
  12397. ' inherited; // call non existing ancestor -> ignore silently',
  12398. 'end;',
  12399. 'procedure tobject.doit;',
  12400. 'begin',
  12401. 'end;',
  12402. 'procedure ta.doabstract;',
  12403. 'begin',
  12404. ' inherited dovirtual; // call TObject.DoVirtual',
  12405. 'end;',
  12406. 'procedure ta.dovirtual;',
  12407. 'begin',
  12408. ' inherited; // call TObject.DoVirtual',
  12409. ' inherited dovirtual; // call TObject.DoVirtual',
  12410. ' inherited dovirtual(); // call TObject.DoVirtual',
  12411. ' doit;',
  12412. ' doit();',
  12413. 'end;',
  12414. 'procedure ta.dosome;',
  12415. 'begin',
  12416. ' inherited; // call non existing ancestor method -> silently ignore',
  12417. 'end;',
  12418. 'begin']);
  12419. ConvertProgram;
  12420. CheckSource('TestClass_CallInherited_ProcNoParams',
  12421. LinesToStr([ // statements
  12422. 'rtl.createClass(this,"TObject",null,function(){',
  12423. ' this.$init = function () {',
  12424. ' };',
  12425. ' this.$final = function () {',
  12426. ' };',
  12427. ' this.DoVirtual = function () {',
  12428. ' };',
  12429. ' this.DoIt = function () {',
  12430. ' };',
  12431. '});',
  12432. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12433. ' this.DoAbstract = function () {',
  12434. ' $mod.TObject.DoVirtual.call(this);',
  12435. ' };',
  12436. ' this.DoVirtual = function () {',
  12437. ' $mod.TObject.DoVirtual.call(this);',
  12438. ' $mod.TObject.DoVirtual.call(this);',
  12439. ' $mod.TObject.DoVirtual.call(this);',
  12440. ' this.DoIt();',
  12441. ' this.DoIt();',
  12442. ' };',
  12443. ' this.DoSome = function () {',
  12444. ' };',
  12445. '});'
  12446. ]),
  12447. LinesToStr([ // this.$main
  12448. ''
  12449. ]));
  12450. end;
  12451. procedure TTestModule.TestClass_CallInherited_WithParams;
  12452. begin
  12453. StartProgram(false);
  12454. Add([
  12455. 'type',
  12456. ' TObject = class',
  12457. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12458. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12459. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12460. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12461. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12462. ' end;',
  12463. ' TClassA = class',
  12464. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12465. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12466. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12467. ' end;',
  12468. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12469. 'begin',
  12470. 'end;',
  12471. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12472. 'begin',
  12473. 'end;',
  12474. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12475. 'begin',
  12476. 'end;',
  12477. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12478. 'begin',
  12479. 'end;',
  12480. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12481. 'begin',
  12482. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12483. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12484. 'end;',
  12485. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12486. 'begin',
  12487. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12488. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12489. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12490. ' doit(pa,pb);',
  12491. ' doit(pa);',
  12492. ' doit2(pa);',
  12493. ' doit2;',
  12494. 'end;',
  12495. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12496. 'begin',
  12497. ' pa:=inherited;',
  12498. 'end;',
  12499. 'begin']);
  12500. ConvertProgram;
  12501. CheckSource('TestClass_CallInherited_WithParams',
  12502. LinesToStr([ // statements
  12503. 'rtl.createClass(this,"TObject",null,function(){',
  12504. ' this.$init = function () {',
  12505. ' };',
  12506. ' this.$final = function () {',
  12507. ' };',
  12508. ' this.DoVirtual = function (pA,pB) {',
  12509. ' };',
  12510. ' this.DoIt = function (pA,pB) {',
  12511. ' };',
  12512. ' this.DoIt2 = function (pA,pB) {',
  12513. ' };',
  12514. ' this.GetIt = function (pA, pB) {',
  12515. ' var Result = 0;',
  12516. ' return Result;',
  12517. ' };',
  12518. '});',
  12519. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12520. ' this.DoAbstract = function (pA,pB) {',
  12521. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12522. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12523. ' };',
  12524. ' this.DoVirtual = function (pA,pB) {',
  12525. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12526. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12527. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12528. ' this.DoIt(pA,pB);',
  12529. ' this.DoIt(pA,0);',
  12530. ' this.DoIt2(pA,2);',
  12531. ' this.DoIt2(1,2);',
  12532. ' };',
  12533. ' this.GetIt$1 = function (pA, pB) {',
  12534. ' var Result = 0;',
  12535. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12536. ' return Result;',
  12537. ' };',
  12538. '});'
  12539. ]),
  12540. LinesToStr([ // this.$main
  12541. ''
  12542. ]));
  12543. end;
  12544. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12545. begin
  12546. StartProgram(false);
  12547. Add('type');
  12548. Add(' TObject = class');
  12549. Add(' constructor Create; virtual;');
  12550. Add(' constructor CreateWithB(b: boolean);');
  12551. Add(' end;');
  12552. Add(' TA = class');
  12553. Add(' constructor Create; override;');
  12554. Add(' constructor CreateWithC(c: char);');
  12555. Add(' procedure DoIt;');
  12556. Add(' class function DoSome: TObject;');
  12557. Add(' end;');
  12558. Add('constructor tobject.create;');
  12559. Add('begin');
  12560. Add(' inherited; // call non existing ancestor -> ignore silently');
  12561. Add('end;');
  12562. Add('constructor tobject.createwithb(b: boolean);');
  12563. Add('begin');
  12564. Add(' inherited; // call non existing ancestor -> ignore silently');
  12565. Add(' create; // normal call');
  12566. Add('end;');
  12567. Add('constructor ta.create;');
  12568. Add('begin');
  12569. Add(' inherited; // normal call TObject.Create');
  12570. Add(' inherited create; // normal call TObject.Create');
  12571. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12572. Add('end;');
  12573. Add('constructor ta.createwithc(c: char);');
  12574. Add('begin');
  12575. Add(' inherited create; // call TObject.Create');
  12576. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12577. Add(' doit;');
  12578. Add(' doit();');
  12579. Add(' dosome;');
  12580. Add('end;');
  12581. Add('procedure ta.doit;');
  12582. Add('begin');
  12583. Add(' create; // normal call');
  12584. Add(' createwithb(false); // normal call');
  12585. Add(' createwithc(''c''); // normal call');
  12586. Add('end;');
  12587. Add('class function ta.dosome: TObject;');
  12588. Add('begin');
  12589. Add(' Result:=create; // constructor');
  12590. Add(' Result:=createwithb(true); // constructor');
  12591. Add(' Result:=createwithc(''c''); // constructor');
  12592. Add('end;');
  12593. Add('begin');
  12594. ConvertProgram;
  12595. CheckSource('TestClass_CallInheritedConstructor',
  12596. LinesToStr([ // statements
  12597. 'rtl.createClass(this,"TObject",null,function(){',
  12598. ' this.$init = function () {',
  12599. ' };',
  12600. ' this.$final = function () {',
  12601. ' };',
  12602. ' this.Create = function () {',
  12603. ' return this;',
  12604. ' };',
  12605. ' this.CreateWithB = function (b) {',
  12606. ' this.Create();',
  12607. ' return this;',
  12608. ' };',
  12609. '});',
  12610. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12611. ' this.Create = function () {',
  12612. ' $mod.TObject.Create.call(this);',
  12613. ' $mod.TObject.Create.call(this);',
  12614. ' $mod.TObject.CreateWithB.call(this, false);',
  12615. ' return this;',
  12616. ' };',
  12617. ' this.CreateWithC = function (c) {',
  12618. ' $mod.TObject.Create.call(this);',
  12619. ' $mod.TObject.CreateWithB.call(this, true);',
  12620. ' this.DoIt();',
  12621. ' this.DoIt();',
  12622. ' this.$class.DoSome();',
  12623. ' return this;',
  12624. ' };',
  12625. ' this.DoIt = function () {',
  12626. ' this.Create();',
  12627. ' this.CreateWithB(false);',
  12628. ' this.CreateWithC("c");',
  12629. ' };',
  12630. ' this.DoSome = function () {',
  12631. ' var Result = null;',
  12632. ' Result = this.$create("Create");',
  12633. ' Result = this.$create("CreateWithB", [true]);',
  12634. ' Result = this.$create("CreateWithC", ["c"]);',
  12635. ' return Result;',
  12636. ' };',
  12637. '});'
  12638. ]),
  12639. LinesToStr([ // this.$main
  12640. ''
  12641. ]));
  12642. end;
  12643. procedure TTestModule.TestClass_ClassVar_Assign;
  12644. begin
  12645. StartProgram(false);
  12646. Add([
  12647. 'type',
  12648. ' TObject = class',
  12649. ' public',
  12650. ' class var vI: longint;',
  12651. ' class var Sub: TObject;',
  12652. ' constructor Create;',
  12653. ' class function GetIt(var Par: longint): tobject;',
  12654. ' end;',
  12655. 'constructor tobject.create;',
  12656. 'begin',
  12657. ' vi:=vi+1;',
  12658. ' Self.vi:=Self.vi+1;',
  12659. ' inc(vi);',
  12660. 'end;',
  12661. 'class function tobject.getit(var par: longint): tobject;',
  12662. 'begin',
  12663. ' vi:=vi+3;',
  12664. ' Self.vi:=Self.vi+4;',
  12665. ' inc(vi);',
  12666. ' Result:=self.sub;',
  12667. ' GetIt(vi);',
  12668. 'end;',
  12669. 'var Obj: tobject;',
  12670. 'begin',
  12671. ' obj:=tobject.create;',
  12672. ' tobject.vi:=3;',
  12673. ' if tobject.vi=4 then ;',
  12674. ' tobject.sub:=nil;',
  12675. ' obj.sub:=nil;',
  12676. ' obj.sub.sub:=nil;']);
  12677. ConvertProgram;
  12678. CheckSource('TestClass_ClassVar_Assign',
  12679. LinesToStr([ // statements
  12680. 'rtl.createClass(this,"TObject",null,function(){',
  12681. ' this.vI = 0;',
  12682. ' this.Sub = null;',
  12683. ' this.$init = function () {',
  12684. ' };',
  12685. ' this.$final = function () {',
  12686. ' };',
  12687. ' this.Create = function(){',
  12688. ' $mod.TObject.vI = this.vI+1;',
  12689. ' $mod.TObject.vI = this.vI+1;',
  12690. ' $mod.TObject.vI += 1;',
  12691. ' return this;',
  12692. ' };',
  12693. ' this.GetIt = function(Par){',
  12694. ' var Result = null;',
  12695. ' $mod.TObject.vI = this.vI + 3;',
  12696. ' $mod.TObject.vI = this.vI + 4;',
  12697. ' $mod.TObject.vI += 1;',
  12698. ' Result = this.Sub;',
  12699. ' this.GetIt({',
  12700. ' p: $mod.TObject,',
  12701. ' get: function () {',
  12702. ' return this.p.vI;',
  12703. ' },',
  12704. ' set: function (v) {',
  12705. ' this.p.vI = v;',
  12706. ' }',
  12707. ' });',
  12708. ' return Result;',
  12709. ' };',
  12710. '});',
  12711. 'this.Obj = null;'
  12712. ]),
  12713. LinesToStr([ // $mod.$main
  12714. '$mod.Obj = $mod.TObject.$create("Create");',
  12715. '$mod.TObject.vI = 3;',
  12716. 'if ($mod.TObject.vI === 4);',
  12717. '$mod.TObject.Sub=null;',
  12718. '$mod.TObject.Sub=null;',
  12719. '$mod.TObject.Sub=null;',
  12720. '']));
  12721. end;
  12722. procedure TTestModule.TestClass_CallClassMethod;
  12723. begin
  12724. StartProgram(false);
  12725. Add('type');
  12726. Add(' TObject = class');
  12727. Add(' public');
  12728. Add(' class var vI: longint;');
  12729. Add(' class var Sub: TObject;');
  12730. Add(' constructor Create;');
  12731. Add(' function GetMore(Par: longint): longint;');
  12732. Add(' class function GetIt(Par: longint): tobject;');
  12733. Add(' end;');
  12734. Add('constructor tobject.create;');
  12735. Add('begin');
  12736. Add(' sub:=getit(3);');
  12737. Add(' vi:=getmore(4);');
  12738. Add(' sub:=Self.getit(5);');
  12739. Add(' vi:=Self.getmore(6);');
  12740. Add('end;');
  12741. Add('function tobject.getmore(par: longint): longint;');
  12742. Add('begin');
  12743. Add(' sub:=getit(11);');
  12744. Add(' vi:=getmore(12);');
  12745. Add(' sub:=self.getit(13);');
  12746. Add(' vi:=self.getmore(14);');
  12747. Add('end;');
  12748. Add('class function tobject.getit(par: longint): tobject;');
  12749. Add('begin');
  12750. Add(' sub:=getit(21);');
  12751. Add(' vi:=sub.getmore(22);');
  12752. Add(' sub:=self.getit(23);');
  12753. Add(' vi:=self.sub.getmore(24);');
  12754. Add('end;');
  12755. Add('var Obj: tobject;');
  12756. Add('begin');
  12757. Add(' obj:=tobject.create;');
  12758. Add(' tobject.getit(5);');
  12759. Add(' obj.getit(6);');
  12760. Add(' obj.sub.getit(7);');
  12761. Add(' obj.sub.getit(8).SUB:=nil;');
  12762. Add(' obj.sub.getit(9).GETIT(10);');
  12763. Add(' obj.sub.getit(11).SuB.getit(12);');
  12764. ConvertProgram;
  12765. CheckSource('TestClass_CallClassMethod',
  12766. LinesToStr([ // statements
  12767. 'rtl.createClass(this,"TObject",null,function(){',
  12768. ' this.vI = 0;',
  12769. ' this.Sub = null;',
  12770. ' this.$init = function () {',
  12771. ' };',
  12772. ' this.$final = function () {',
  12773. ' };',
  12774. ' this.Create = function(){',
  12775. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12776. ' $mod.TObject.vI = this.GetMore(4);',
  12777. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12778. ' $mod.TObject.vI = this.GetMore(6);',
  12779. ' return this;',
  12780. ' };',
  12781. ' this.GetMore = function(Par){',
  12782. ' var Result = 0;',
  12783. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12784. ' $mod.TObject.vI = this.GetMore(12);',
  12785. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12786. ' $mod.TObject.vI = this.GetMore(14);',
  12787. ' return Result;',
  12788. ' };',
  12789. ' this.GetIt = function(Par){',
  12790. ' var Result = null;',
  12791. ' $mod.TObject.Sub = this.GetIt(21);',
  12792. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12793. ' $mod.TObject.Sub = this.GetIt(23);',
  12794. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12795. ' return Result;',
  12796. ' };',
  12797. '});',
  12798. 'this.Obj = null;'
  12799. ]),
  12800. LinesToStr([ // $mod.$main
  12801. '$mod.Obj = $mod.TObject.$create("Create");',
  12802. '$mod.TObject.GetIt(5);',
  12803. '$mod.Obj.$class.GetIt(6);',
  12804. '$mod.Obj.Sub.$class.GetIt(7);',
  12805. '$mod.TObject.Sub=null;',
  12806. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12807. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12808. '']));
  12809. end;
  12810. procedure TTestModule.TestClass_Property;
  12811. begin
  12812. StartProgram(false);
  12813. Add('type');
  12814. Add(' TObject = class');
  12815. Add(' Fx: longint;');
  12816. Add(' Fy: longint;');
  12817. Add(' function GetInt: longint;');
  12818. Add(' procedure SetInt(Value: longint);');
  12819. Add(' procedure DoIt;');
  12820. Add(' property IntA: longint read Fx write Fy;');
  12821. Add(' property IntB: longint read GetInt write SetInt;');
  12822. Add(' end;');
  12823. Add('function tobject.getint: longint;');
  12824. Add('begin');
  12825. Add(' result:=fx;');
  12826. Add('end;');
  12827. Add('procedure tobject.setint(value: longint);');
  12828. Add('begin');
  12829. Add(' if value=fy then exit;');
  12830. Add(' fy:=value;');
  12831. Add('end;');
  12832. Add('procedure tobject.doit;');
  12833. Add('begin');
  12834. Add(' IntA:=IntA+1;');
  12835. Add(' Self.IntA:=Self.IntA+1;');
  12836. Add(' IntB:=IntB+1;');
  12837. Add(' Self.IntB:=Self.IntB+1;');
  12838. Add('end;');
  12839. Add('var Obj: tobject;');
  12840. Add('begin');
  12841. Add(' obj.inta:=obj.inta+1;');
  12842. Add(' if obj.intb=2 then;');
  12843. Add(' obj.intb:=obj.intb+2;');
  12844. Add(' obj.setint(obj.inta);');
  12845. ConvertProgram;
  12846. CheckSource('TestClass_Property',
  12847. LinesToStr([ // statements
  12848. 'rtl.createClass(this, "TObject", null, function () {',
  12849. ' this.$init = function () {',
  12850. ' this.Fx = 0;',
  12851. ' this.Fy = 0;',
  12852. ' };',
  12853. ' this.$final = function () {',
  12854. ' };',
  12855. ' this.GetInt = function () {',
  12856. ' var Result = 0;',
  12857. ' Result = this.Fx;',
  12858. ' return Result;',
  12859. ' };',
  12860. ' this.SetInt = function (Value) {',
  12861. ' if (Value === this.Fy) return;',
  12862. ' this.Fy = Value;',
  12863. ' };',
  12864. ' this.DoIt = function () {',
  12865. ' this.Fy = this.Fx + 1;',
  12866. ' this.Fy = this.Fx + 1;',
  12867. ' this.SetInt(this.GetInt() + 1);',
  12868. ' this.SetInt(this.GetInt() + 1);',
  12869. ' };',
  12870. '});',
  12871. 'this.Obj = null;'
  12872. ]),
  12873. LinesToStr([ // $mod.$main
  12874. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12875. 'if ($mod.Obj.GetInt() === 2);',
  12876. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12877. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12878. ]));
  12879. end;
  12880. procedure TTestModule.TestClass_Property_ClassMethod;
  12881. begin
  12882. StartProgram(false);
  12883. Add([
  12884. 'type',
  12885. ' TObject = class',
  12886. ' class var Fx: longint;',
  12887. ' class var Fy: longint;',
  12888. ' class function GetInt: longint;',
  12889. ' class procedure SetInt(Value: longint);',
  12890. ' end;',
  12891. ' TBird = class',
  12892. ' class procedure DoIt;',
  12893. ' class property IntA: longint read Fx write Fy;',
  12894. ' class property IntB: longint read GetInt write SetInt;',
  12895. ' end;',
  12896. 'class function tobject.getint: longint;',
  12897. 'begin',
  12898. ' result:=fx;',
  12899. 'end;',
  12900. 'class procedure tobject.setint(value: longint);',
  12901. 'begin',
  12902. 'end;',
  12903. 'class procedure tbird.doit;',
  12904. 'begin',
  12905. ' FX:=3;',
  12906. ' IntA:=IntA+1;',
  12907. ' Self.IntA:=Self.IntA+1;',
  12908. ' IntB:=IntB+1;',
  12909. ' Self.IntB:=Self.IntB+1;',
  12910. ' with Self do begin',
  12911. ' FX:=11;',
  12912. ' IntA:=IntA+12;',
  12913. ' IntB:=IntB+13;',
  12914. ' end;',
  12915. 'end;',
  12916. 'var Obj: tbird;',
  12917. 'begin',
  12918. ' tbird.fx:=tbird.fx+1;',
  12919. ' tbird.inta:=tbird.inta+1;',
  12920. ' if tbird.intb=2 then;',
  12921. ' tbird.intb:=tbird.intb+2;',
  12922. ' tbird.setint(tbird.inta);',
  12923. ' obj.inta:=obj.inta+1;',
  12924. ' if obj.intb=2 then;',
  12925. ' obj.intb:=obj.intb+2;',
  12926. ' obj.setint(obj.inta);',
  12927. ' with Tbird do begin',
  12928. ' FX:=FY+1;',
  12929. ' inta:=inta+2;',
  12930. ' intb:=intb+3;',
  12931. ' end;',
  12932. ' with Obj do begin',
  12933. ' FX:=FY+1;',
  12934. ' inta:=inta+2;',
  12935. ' intb:=intb+3;',
  12936. ' end;',
  12937. '']);
  12938. ConvertProgram;
  12939. CheckSource('TestClass_Property_ClassMethod',
  12940. LinesToStr([ // statements
  12941. 'rtl.createClass(this, "TObject", null, function () {',
  12942. ' this.Fx = 0;',
  12943. ' this.Fy = 0;',
  12944. ' this.$init = function () {',
  12945. ' };',
  12946. ' this.$final = function () {',
  12947. ' };',
  12948. ' this.GetInt = function () {',
  12949. ' var Result = 0;',
  12950. ' Result = this.Fx;',
  12951. ' return Result;',
  12952. ' };',
  12953. ' this.SetInt = function (Value) {',
  12954. ' };',
  12955. '});',
  12956. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12957. ' this.DoIt = function () {',
  12958. ' $mod.TObject.Fx = 3;',
  12959. ' $mod.TObject.Fy = this.Fx + 1;',
  12960. ' $mod.TObject.Fy = this.Fx + 1;',
  12961. ' this.SetInt(this.GetInt() + 1);',
  12962. ' this.SetInt(this.GetInt() + 1);',
  12963. ' $mod.TObject.Fx = 11;',
  12964. ' $mod.TObject.Fy = this.Fx + 12;',
  12965. ' this.SetInt(this.GetInt() + 13);',
  12966. ' };',
  12967. '});',
  12968. 'this.Obj = null;'
  12969. ]),
  12970. LinesToStr([ // $mod.$main
  12971. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12972. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12973. 'if ($mod.TBird.GetInt() === 2);',
  12974. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12975. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12976. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12977. 'if ($mod.Obj.$class.GetInt() === 2);',
  12978. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12979. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12980. 'var $with = $mod.TBird;',
  12981. '$mod.TObject.Fx = $with.Fy + 1;',
  12982. '$mod.TObject.Fy = $with.Fx + 2;',
  12983. '$with.SetInt($with.GetInt() + 3);',
  12984. 'var $with1 = $mod.Obj;',
  12985. '$mod.TObject.Fx = $with1.Fy + 1;',
  12986. '$mod.TObject.Fy = $with1.Fx + 2;',
  12987. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  12988. '']));
  12989. end;
  12990. procedure TTestModule.TestClass_Property_Indexed;
  12991. begin
  12992. StartProgram(false);
  12993. Add([
  12994. 'type',
  12995. ' TObject = class',
  12996. ' FItems: array of longint;',
  12997. ' function GetItems(Index: longint): longint;',
  12998. ' procedure SetItems(Index: longint; Value: longint);',
  12999. ' procedure DoIt;',
  13000. ' property Items[Index: longint]: longint read getitems write setitems;',
  13001. ' end;',
  13002. 'function tobject.getitems(index: longint): longint;',
  13003. 'begin',
  13004. ' Result:=fitems[index];',
  13005. 'end;',
  13006. 'procedure tobject.setitems(index: longint; value: longint);',
  13007. 'begin',
  13008. ' fitems[index]:=value;',
  13009. 'end;',
  13010. 'procedure tobject.doit;',
  13011. 'begin',
  13012. ' items[1]:=2;',
  13013. ' items[3]:=items[4];',
  13014. ' self.items[5]:=self.items[6];',
  13015. ' items[items[7]]:=items[items[8]];',
  13016. 'end;',
  13017. 'var Obj: tobject;',
  13018. 'begin',
  13019. ' obj.Items[11]:=obj.Items[12];',
  13020. '']);
  13021. ConvertProgram;
  13022. CheckSource('TestClass_Property_Indexed',
  13023. LinesToStr([ // statements
  13024. 'rtl.createClass(this, "TObject", null, function () {',
  13025. ' this.$init = function () {',
  13026. ' this.FItems = [];',
  13027. ' };',
  13028. ' this.$final = function () {',
  13029. ' this.FItems = undefined;',
  13030. ' };',
  13031. ' this.GetItems = function (Index) {',
  13032. ' var Result = 0;',
  13033. ' Result = this.FItems[Index];',
  13034. ' return Result;',
  13035. ' };',
  13036. ' this.SetItems = function (Index, Value) {',
  13037. ' this.FItems[Index] = Value;',
  13038. ' };',
  13039. ' this.DoIt = function () {',
  13040. ' this.SetItems(1, 2);',
  13041. ' this.SetItems(3,this.GetItems(4));',
  13042. ' this.SetItems(5,this.GetItems(6));',
  13043. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13044. ' };',
  13045. '});',
  13046. 'this.Obj = null;'
  13047. ]),
  13048. LinesToStr([ // $mod.$main
  13049. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13050. ]));
  13051. end;
  13052. procedure TTestModule.TestClass_Property_IndexSpec;
  13053. begin
  13054. StartProgram(false);
  13055. Add([
  13056. 'type',
  13057. ' TEnum = (red, blue);',
  13058. ' TObject = class',
  13059. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13060. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13061. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13062. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13063. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13064. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13065. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13066. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13067. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13068. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13069. ' end;',
  13070. 'procedure DoIt(b: boolean); begin end;',
  13071. 'var',
  13072. ' o: TObject;',
  13073. 'begin',
  13074. ' o.B1:=o.B1;',
  13075. ' o.B2:=o.B2;',
  13076. ' o.B3:=o.B3;',
  13077. ' o.I1[''a'']:=o.I1[''b''];',
  13078. ' doit(o.b1);',
  13079. ' doit(o.b2);',
  13080. ' doit(o.i1[''c'']);',
  13081. '']);
  13082. ConvertProgram;
  13083. CheckSource('TestClass_Property_IndexSpec',
  13084. LinesToStr([ // statements
  13085. 'this.TEnum = {',
  13086. ' "0": "red",',
  13087. ' red: 0,',
  13088. ' "1": "blue",',
  13089. ' blue: 1',
  13090. '};',
  13091. 'rtl.createClass(this, "TObject", null, function () {',
  13092. ' this.$init = function () {',
  13093. ' };',
  13094. ' this.$final = function () {',
  13095. ' };',
  13096. '});',
  13097. 'this.DoIt = function (b) {',
  13098. '};',
  13099. 'this.o = null;',
  13100. '']),
  13101. LinesToStr([ // $mod.$main
  13102. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13103. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13104. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13105. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13106. '$mod.DoIt($mod.o.GetIntBool(1));',
  13107. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13108. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13109. '']));
  13110. end;
  13111. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13112. begin
  13113. StartProgram(false);
  13114. Add('type');
  13115. Add(' TArray = array of longint;');
  13116. Add(' TObject = class');
  13117. Add(' FItems: TArray;');
  13118. Add(' function GetItems: tarray;');
  13119. Add(' procedure SetItems(Value: tarray);');
  13120. Add(' property Items: tarray read getitems write setitems;');
  13121. Add(' procedure SetNumbers(const Value: tarray);');
  13122. Add(' property Numbers: tarray write setnumbers;');
  13123. Add(' end;');
  13124. Add('function tobject.getitems: tarray;');
  13125. Add('begin');
  13126. Add(' Result:=fitems;');
  13127. Add('end;');
  13128. Add('procedure tobject.setitems(value: tarray);');
  13129. Add('begin');
  13130. Add(' fitems:=value;');
  13131. Add(' fitems:=nil;');
  13132. Add(' Items:=nil;');
  13133. Add(' Items:=Items;');
  13134. Add(' Items[1]:=2;');
  13135. Add(' fitems[3]:=Items[4];');
  13136. Add(' Items[5]:=Items[6];');
  13137. Add(' Self.Items[7]:=8;');
  13138. Add(' Self.Items[9]:=Self.Items[10];');
  13139. Add(' Items[Items[11]]:=Items[Items[12]];');
  13140. Add('end;');
  13141. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13142. Add('begin;');
  13143. Add(' Numbers:=nil;');
  13144. Add(' Numbers:=Value;');
  13145. Add(' Self.Numbers:=Value;');
  13146. Add('end;');
  13147. Add('var Obj: tobject;');
  13148. Add('begin');
  13149. Add(' obj.items:=nil;');
  13150. Add(' obj.items:=obj.items;');
  13151. Add(' obj.items[11]:=obj.items[12];');
  13152. ConvertProgram;
  13153. CheckSource('TestClass_PropertyOfTypeArray',
  13154. LinesToStr([ // statements
  13155. 'rtl.createClass(this, "TObject", null, function () {',
  13156. ' this.$init = function () {',
  13157. ' this.FItems = [];',
  13158. ' };',
  13159. ' this.$final = function () {',
  13160. ' this.FItems = undefined;',
  13161. ' };',
  13162. ' this.GetItems = function () {',
  13163. ' var Result = [];',
  13164. ' Result = rtl.arrayRef(this.FItems);',
  13165. ' return Result;',
  13166. ' };',
  13167. ' this.SetItems = function (Value) {',
  13168. ' this.FItems = rtl.arrayRef(Value);',
  13169. ' this.FItems = [];',
  13170. ' this.SetItems([]);',
  13171. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13172. ' this.GetItems()[1] = 2;',
  13173. ' this.FItems[3] = this.GetItems()[4];',
  13174. ' this.GetItems()[5] = this.GetItems()[6];',
  13175. ' this.GetItems()[7] = 8;',
  13176. ' this.GetItems()[9] = this.GetItems()[10];',
  13177. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13178. ' };',
  13179. ' this.SetNumbers = function (Value) {',
  13180. ' this.SetNumbers([]);',
  13181. ' this.SetNumbers(Value);',
  13182. ' this.SetNumbers(Value);',
  13183. ' };',
  13184. '});',
  13185. 'this.Obj = null;'
  13186. ]),
  13187. LinesToStr([ // $mod.$main
  13188. '$mod.Obj.SetItems([]);',
  13189. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13190. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13191. ]));
  13192. end;
  13193. procedure TTestModule.TestClass_PropertyDefault;
  13194. begin
  13195. StartProgram(false);
  13196. Add([
  13197. 'type',
  13198. ' TArray = array of longint;',
  13199. ' TObject = class',
  13200. ' end;',
  13201. ' TBird = class',
  13202. ' FItems: TArray;',
  13203. ' function GetItems(Index: longint): longint;',
  13204. ' procedure SetItems(Index, Value: longint);',
  13205. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13206. ' end;',
  13207. 'function TBird.getitems(index: longint): longint;',
  13208. 'begin',
  13209. 'end;',
  13210. 'procedure TBird.setitems(index, value: longint);',
  13211. 'begin',
  13212. ' Self[1]:=2;',
  13213. ' Self[3]:=Self[index];',
  13214. ' Self[index]:=Self[Self[value]];',
  13215. ' Self[Self[4]]:=value;',
  13216. 'end;',
  13217. 'var',
  13218. ' Bird: TBird;',
  13219. ' Obj: TObject;',
  13220. 'begin',
  13221. ' bird[11]:=12;',
  13222. ' bird[13]:=bird[14];',
  13223. ' bird[Bird[15]]:=bird[Bird[15]];',
  13224. ' TBird(obj)[16]:=TBird(obj)[17];',
  13225. ' (obj as tbird)[18]:=19;',
  13226. '']);
  13227. ConvertProgram;
  13228. CheckSource('TestClass_PropertyDefault',
  13229. LinesToStr([ // statements
  13230. 'rtl.createClass(this, "TObject", null, function () {',
  13231. ' this.$init = function () {',
  13232. ' };',
  13233. ' this.$final = function () {',
  13234. ' };',
  13235. '});',
  13236. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13237. ' this.$init = function () {',
  13238. ' $mod.TObject.$init.call(this);',
  13239. ' this.FItems = [];',
  13240. ' };',
  13241. ' this.$final = function () {',
  13242. ' this.FItems = undefined;',
  13243. ' $mod.TObject.$final.call(this);',
  13244. ' };',
  13245. ' this.GetItems = function (Index) {',
  13246. ' var Result = 0;',
  13247. ' return Result;',
  13248. ' };',
  13249. ' this.SetItems = function (Index, Value) {',
  13250. ' this.SetItems(1, 2);',
  13251. ' this.SetItems(3, this.GetItems(Index));',
  13252. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13253. ' this.SetItems(this.GetItems(4), Value);',
  13254. ' };',
  13255. '});',
  13256. 'this.Bird = null;',
  13257. 'this.Obj = null;',
  13258. '']),
  13259. LinesToStr([ // $mod.$main
  13260. '$mod.Bird.SetItems(11, 12);',
  13261. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13262. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13263. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13264. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13265. '']));
  13266. end;
  13267. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13268. begin
  13269. StartProgram(false);
  13270. Add([
  13271. 'type',
  13272. ' TObject = class end;',
  13273. ' TAlphaList = class',
  13274. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13275. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13276. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13277. ' end;',
  13278. ' TBetaList = class',
  13279. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13280. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13281. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13282. ' end;',
  13283. ' TBird = class',
  13284. ' procedure DoIt;',
  13285. ' end;',
  13286. 'procedure TBird.DoIt;',
  13287. 'var',
  13288. ' List: TAlphaList;',
  13289. 'begin',
  13290. ' if TBetaList(List[true])[3]=nil then ;',
  13291. ' TBetaList(List[false])[5]:=nil;',
  13292. 'end;',
  13293. 'var',
  13294. ' List: TAlphaList;',
  13295. 'begin',
  13296. ' if TBetaList(List[true])[3]=nil then ;',
  13297. ' TBetaList(List[false])[5]:=nil;',
  13298. '']);
  13299. ConvertProgram;
  13300. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13301. LinesToStr([ // statements
  13302. 'rtl.createClass(this, "TObject", null, function () {',
  13303. ' this.$init = function () {',
  13304. ' };',
  13305. ' this.$final = function () {',
  13306. ' };',
  13307. '});',
  13308. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13309. '});',
  13310. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13311. '});',
  13312. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13313. ' this.DoIt = function () {',
  13314. ' var List = null;',
  13315. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13316. ' List.GetAlphas(false).SetBetas(5, null);',
  13317. ' };',
  13318. '});',
  13319. 'this.List = null;',
  13320. '']),
  13321. LinesToStr([ // $mod.$main
  13322. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13323. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13324. '']));
  13325. end;
  13326. procedure TTestModule.TestClass_PropertyOverride;
  13327. begin
  13328. StartProgram(false);
  13329. Add('type');
  13330. Add(' integer = longint;');
  13331. Add(' TObject = class');
  13332. Add(' FItem: integer;');
  13333. Add(' function GetItem: integer; external name ''GetItem'';');
  13334. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13335. Add(' property Item: integer read getitem write setitem;');
  13336. Add(' end;');
  13337. Add(' TCar = class');
  13338. Add(' FBag: integer;');
  13339. Add(' function GetBag: integer; external name ''GetBag'';');
  13340. Add(' property Item read getbag;');
  13341. Add(' end;');
  13342. Add('var');
  13343. Add(' Obj: tobject;');
  13344. Add(' Car: tcar;');
  13345. Add('begin');
  13346. Add(' Obj.Item:=Obj.Item;');
  13347. Add(' Car.Item:=Car.Item;');
  13348. ConvertProgram;
  13349. CheckSource('TestClass_PropertyOverride',
  13350. LinesToStr([ // statements
  13351. 'rtl.createClass(this, "TObject", null, function () {',
  13352. ' this.$init = function () {',
  13353. ' this.FItem = 0;',
  13354. ' };',
  13355. ' this.$final = function () {',
  13356. ' };',
  13357. '});',
  13358. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13359. ' this.$init = function () {',
  13360. ' $mod.TObject.$init.call(this);',
  13361. ' this.FBag = 0;',
  13362. ' };',
  13363. '});',
  13364. 'this.Obj = null;',
  13365. 'this.Car = null;',
  13366. '']),
  13367. LinesToStr([ // $mod.$main
  13368. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13369. '$mod.Car.SetItem($mod.Car.GetBag());',
  13370. '']));
  13371. end;
  13372. procedure TTestModule.TestClass_PropertyIncVisibility;
  13373. begin
  13374. AddModuleWithIntfImplSrc('unit1.pp',
  13375. LinesToStr([
  13376. 'type',
  13377. ' TNumber = longint;',
  13378. ' TInteger = longint;',
  13379. ' TObject = class',
  13380. ' private',
  13381. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13382. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13383. ' protected',
  13384. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13385. ' end;']),
  13386. LinesToStr([
  13387. '']));
  13388. StartProgram(true);
  13389. Add([
  13390. 'uses unit1;',
  13391. 'type',
  13392. ' TBird = class',
  13393. ' public',
  13394. ' property Items;',
  13395. ' end;',
  13396. 'procedure DoIt(i: TInteger);',
  13397. 'begin',
  13398. 'end;',
  13399. 'var b: TBird;',
  13400. 'begin',
  13401. ' b.Items[1]:=2;',
  13402. ' b.Items[3]:=b.Items[4];',
  13403. ' DoIt(b.Items[5]);',
  13404. '']);
  13405. ConvertProgram;
  13406. CheckSource('TestClass_PropertyIncVisibility',
  13407. LinesToStr([ // statements
  13408. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13409. '});',
  13410. 'this.DoIt = function (i) {',
  13411. '};',
  13412. 'this.b = null;'
  13413. ]),
  13414. LinesToStr([ // $mod.$main
  13415. '$mod.b.SetItems(1, 2);',
  13416. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13417. '$mod.DoIt($mod.b.GetItems(5));'
  13418. ]));
  13419. end;
  13420. procedure TTestModule.TestClass_Assigned;
  13421. begin
  13422. StartProgram(false);
  13423. Add('type');
  13424. Add(' TObject = class');
  13425. Add(' end;');
  13426. Add('var');
  13427. Add(' Obj: tobject;');
  13428. Add(' b: boolean;');
  13429. Add('begin');
  13430. Add(' if Assigned(obj) then ;');
  13431. Add(' b:=Assigned(obj) or false;');
  13432. ConvertProgram;
  13433. CheckSource('TestClass_Assigned',
  13434. LinesToStr([ // statements
  13435. 'rtl.createClass(this, "TObject", null, function () {',
  13436. ' this.$init = function () {',
  13437. ' };',
  13438. ' this.$final = function () {',
  13439. ' };',
  13440. '});',
  13441. 'this.Obj = null;',
  13442. 'this.b = false;'
  13443. ]),
  13444. LinesToStr([ // $mod.$main
  13445. 'if ($mod.Obj != null);',
  13446. '$mod.b = ($mod.Obj != null) || false;'
  13447. ]));
  13448. end;
  13449. procedure TTestModule.TestClass_WithClassDoCreate;
  13450. begin
  13451. StartProgram(false);
  13452. Add('type');
  13453. Add(' TObject = class');
  13454. Add(' aBool: boolean;');
  13455. Add(' Arr: array of boolean;');
  13456. Add(' constructor Create;');
  13457. Add(' end;');
  13458. Add('constructor TObject.Create; begin end;');
  13459. Add('var');
  13460. Add(' Obj: tobject;');
  13461. Add(' b: boolean;');
  13462. Add('begin');
  13463. Add(' with tobject.create do begin');
  13464. Add(' b:=abool;');
  13465. Add(' abool:=b;');
  13466. Add(' b:=arr[1];');
  13467. Add(' arr[2]:=b;');
  13468. Add(' end;');
  13469. Add(' with tobject do');
  13470. Add(' obj:=create;');
  13471. Add(' with obj do begin');
  13472. Add(' create;');
  13473. Add(' b:=abool;');
  13474. Add(' abool:=b;');
  13475. Add(' b:=arr[3];');
  13476. Add(' arr[4]:=b;');
  13477. Add(' end;');
  13478. ConvertProgram;
  13479. CheckSource('TestClass_WithClassDoCreate',
  13480. LinesToStr([ // statements
  13481. 'rtl.createClass(this, "TObject", null, function () {',
  13482. ' this.$init = function () {',
  13483. ' this.aBool = false;',
  13484. ' this.Arr = [];',
  13485. ' };',
  13486. ' this.$final = function () {',
  13487. ' this.Arr = undefined;',
  13488. ' };',
  13489. ' this.Create = function () {',
  13490. ' return this;',
  13491. ' };',
  13492. '});',
  13493. 'this.Obj = null;',
  13494. 'this.b = false;'
  13495. ]),
  13496. LinesToStr([ // $mod.$main
  13497. 'var $with = $mod.TObject.$create("Create");',
  13498. '$mod.b = $with.aBool;',
  13499. '$with.aBool = $mod.b;',
  13500. '$mod.b = $with.Arr[1];',
  13501. '$with.Arr[2] = $mod.b;',
  13502. 'var $with1 = $mod.TObject;',
  13503. '$mod.Obj = $with1.$create("Create");',
  13504. 'var $with2 = $mod.Obj;',
  13505. '$with2.Create();',
  13506. '$mod.b = $with2.aBool;',
  13507. '$with2.aBool = $mod.b;',
  13508. '$mod.b = $with2.Arr[3];',
  13509. '$with2.Arr[4] = $mod.b;',
  13510. '']));
  13511. end;
  13512. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13513. begin
  13514. StartProgram(false);
  13515. Add('type');
  13516. Add(' TObject = class');
  13517. Add(' FInt: longint;');
  13518. Add(' constructor Create;');
  13519. Add(' function GetSize: longint;');
  13520. Add(' procedure SetSize(Value: longint);');
  13521. Add(' property Int: longint read FInt write FInt;');
  13522. Add(' property Size: longint read GetSize write SetSize;');
  13523. Add(' end;');
  13524. Add('constructor TObject.Create; begin end;');
  13525. Add('function TObject.GetSize: longint; begin; end;');
  13526. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13527. Add('var');
  13528. Add(' Obj: tobject;');
  13529. Add(' i: longint;');
  13530. Add('begin');
  13531. Add(' with TObject.Create do begin');
  13532. Add(' i:=int;');
  13533. Add(' int:=i;');
  13534. Add(' i:=size;');
  13535. Add(' size:=i;');
  13536. Add(' end;');
  13537. Add(' with obj do begin');
  13538. Add(' i:=int;');
  13539. Add(' int:=i;');
  13540. Add(' i:=size;');
  13541. Add(' size:=i;');
  13542. Add(' end;');
  13543. ConvertProgram;
  13544. CheckSource('TestClass_WithClassInstDoProperty',
  13545. LinesToStr([ // statements
  13546. 'rtl.createClass(this, "TObject", null, function () {',
  13547. ' this.$init = function () {',
  13548. ' this.FInt = 0;',
  13549. ' };',
  13550. ' this.$final = function () {',
  13551. ' };',
  13552. ' this.Create = function () {',
  13553. ' return this;',
  13554. ' };',
  13555. ' this.GetSize = function () {',
  13556. ' var Result = 0;',
  13557. ' return Result;',
  13558. ' };',
  13559. ' this.SetSize = function (Value) {',
  13560. ' };',
  13561. '});',
  13562. 'this.Obj = null;',
  13563. 'this.i = 0;'
  13564. ]),
  13565. LinesToStr([ // $mod.$main
  13566. 'var $with = $mod.TObject.$create("Create");',
  13567. '$mod.i = $with.FInt;',
  13568. '$with.FInt = $mod.i;',
  13569. '$mod.i = $with.GetSize();',
  13570. '$with.SetSize($mod.i);',
  13571. 'var $with1 = $mod.Obj;',
  13572. '$mod.i = $with1.FInt;',
  13573. '$with1.FInt = $mod.i;',
  13574. '$mod.i = $with1.GetSize();',
  13575. '$with1.SetSize($mod.i);',
  13576. '']));
  13577. end;
  13578. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13579. begin
  13580. StartProgram(false);
  13581. Add('type');
  13582. Add(' TObject = class');
  13583. Add(' constructor Create;');
  13584. Add(' function GetItems(Index: longint): longint;');
  13585. Add(' procedure SetItems(Index, Value: longint);');
  13586. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13587. Add(' end;');
  13588. Add('constructor TObject.Create; begin end;');
  13589. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13590. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13591. Add('var');
  13592. Add(' Obj: tobject;');
  13593. Add(' i: longint;');
  13594. Add('begin');
  13595. Add(' with TObject.Create do begin');
  13596. Add(' i:=Items[1];');
  13597. Add(' Items[2]:=i;');
  13598. Add(' end;');
  13599. Add(' with obj do begin');
  13600. Add(' i:=Items[3];');
  13601. Add(' Items[4]:=i;');
  13602. Add(' end;');
  13603. ConvertProgram;
  13604. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13605. LinesToStr([ // statements
  13606. 'rtl.createClass(this, "TObject", null, function () {',
  13607. ' this.$init = function () {',
  13608. ' };',
  13609. ' this.$final = function () {',
  13610. ' };',
  13611. ' this.Create = function () {',
  13612. ' return this;',
  13613. ' };',
  13614. ' this.GetItems = function (Index) {',
  13615. ' var Result = 0;',
  13616. ' return Result;',
  13617. ' };',
  13618. ' this.SetItems = function (Index, Value) {',
  13619. ' };',
  13620. '});',
  13621. 'this.Obj = null;',
  13622. 'this.i = 0;'
  13623. ]),
  13624. LinesToStr([ // $mod.$main
  13625. 'var $with = $mod.TObject.$create("Create");',
  13626. '$mod.i = $with.GetItems(1);',
  13627. '$with.SetItems(2, $mod.i);',
  13628. 'var $with1 = $mod.Obj;',
  13629. '$mod.i = $with1.GetItems(3);',
  13630. '$with1.SetItems(4, $mod.i);',
  13631. '']));
  13632. end;
  13633. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13634. begin
  13635. StartProgram(false);
  13636. Add('type');
  13637. Add(' TObject = class');
  13638. Add(' constructor Create;');
  13639. Add(' function GetSize: longint;');
  13640. Add(' procedure SetSize(Value: longint);');
  13641. Add(' end;');
  13642. Add('constructor TObject.Create; begin end;');
  13643. Add('function TObject.GetSize: longint; begin; end;');
  13644. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13645. Add('var');
  13646. Add(' Obj: tobject;');
  13647. Add(' i: longint;');
  13648. Add('begin');
  13649. Add(' with TObject.Create do begin');
  13650. Add(' i:=GetSize;');
  13651. Add(' i:=GetSize();');
  13652. Add(' SetSize(i);');
  13653. Add(' end;');
  13654. Add(' with obj do begin');
  13655. Add(' i:=GetSize;');
  13656. Add(' i:=GetSize();');
  13657. Add(' SetSize(i);');
  13658. Add(' end;');
  13659. ConvertProgram;
  13660. CheckSource('TestClass_WithClassInstDoFunc',
  13661. LinesToStr([ // statements
  13662. 'rtl.createClass(this, "TObject", null, function () {',
  13663. ' this.$init = function () {',
  13664. ' };',
  13665. ' this.$final = function () {',
  13666. ' };',
  13667. ' this.Create = function () {',
  13668. ' return this;',
  13669. ' };',
  13670. ' this.GetSize = function () {',
  13671. ' var Result = 0;',
  13672. ' return Result;',
  13673. ' };',
  13674. ' this.SetSize = function (Value) {',
  13675. ' };',
  13676. '});',
  13677. 'this.Obj = null;',
  13678. 'this.i = 0;'
  13679. ]),
  13680. LinesToStr([ // $mod.$main
  13681. 'var $with = $mod.TObject.$create("Create");',
  13682. '$mod.i = $with.GetSize();',
  13683. '$mod.i = $with.GetSize();',
  13684. '$with.SetSize($mod.i);',
  13685. 'var $with1 = $mod.Obj;',
  13686. '$mod.i = $with1.GetSize();',
  13687. '$mod.i = $with1.GetSize();',
  13688. '$with1.SetSize($mod.i);',
  13689. '']));
  13690. end;
  13691. procedure TTestModule.TestClass_TypeCast;
  13692. begin
  13693. StartProgram(false);
  13694. Add('type');
  13695. Add(' TObject = class');
  13696. Add(' Next: TObject;');
  13697. Add(' constructor Create;');
  13698. Add(' end;');
  13699. Add(' TControl = class(TObject)');
  13700. Add(' Arr: array of TObject;');
  13701. Add(' function GetIt(vI: longint = 0): TObject;');
  13702. Add(' end;');
  13703. Add('constructor tobject.create; begin end;');
  13704. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13705. Add('var');
  13706. Add(' Obj: tobject;');
  13707. Add('begin');
  13708. Add(' obj:=tcontrol(obj).next;');
  13709. Add(' tcontrol(obj):=nil;');
  13710. Add(' obj:=tcontrol(obj);');
  13711. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13712. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13713. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13714. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13715. Add(' obj:=tcontrol(nil);');
  13716. ConvertProgram;
  13717. CheckSource('TestClass_TypeCast',
  13718. LinesToStr([ // statements
  13719. 'rtl.createClass(this, "TObject", null, function () {',
  13720. ' this.$init = function () {',
  13721. ' this.Next = null;',
  13722. ' };',
  13723. ' this.$final = function () {',
  13724. ' this.Next = undefined;',
  13725. ' };',
  13726. ' this.Create = function () {',
  13727. ' return this;',
  13728. ' };',
  13729. '});',
  13730. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  13731. ' this.$init = function () {',
  13732. ' $mod.TObject.$init.call(this);',
  13733. ' this.Arr = [];',
  13734. ' };',
  13735. ' this.$final = function () {',
  13736. ' this.Arr = undefined;',
  13737. ' $mod.TObject.$final.call(this);',
  13738. ' };',
  13739. ' this.GetIt = function (vI) {',
  13740. ' var Result = null;',
  13741. ' return Result;',
  13742. ' };',
  13743. '});',
  13744. 'this.Obj = null;'
  13745. ]),
  13746. LinesToStr([ // $mod.$main
  13747. '$mod.Obj = $mod.Obj.Next;',
  13748. '$mod.Obj = null;',
  13749. '$mod.Obj = $mod.Obj;',
  13750. '$mod.Obj = $mod.Obj.GetIt(0);',
  13751. '$mod.Obj = $mod.Obj.GetIt(0);',
  13752. '$mod.Obj = $mod.Obj.GetIt(1);',
  13753. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13754. '$mod.Obj = null;',
  13755. '']));
  13756. end;
  13757. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13758. begin
  13759. StartProgram(false);
  13760. Add('type');
  13761. Add(' TObject = class end;');
  13762. Add('procedure ProcA(var A);');
  13763. Add('begin');
  13764. Add(' TObject(A):=nil;');
  13765. Add(' TObject(A):=TObject(A);');
  13766. Add(' if TObject(A)=nil then ;');
  13767. Add(' if nil=TObject(A) then ;');
  13768. Add('end;');
  13769. Add('procedure ProcB(out A);');
  13770. Add('begin');
  13771. Add(' TObject(A):=nil;');
  13772. Add(' TObject(A):=TObject(A);');
  13773. Add(' if TObject(A)=nil then ;');
  13774. Add(' if nil=TObject(A) then ;');
  13775. Add('end;');
  13776. Add('procedure ProcC(const A);');
  13777. Add('begin');
  13778. Add(' if TObject(A)=nil then ;');
  13779. Add(' if nil=TObject(A) then ;');
  13780. Add('end;');
  13781. Add('var o: TObject;');
  13782. Add('begin');
  13783. Add(' ProcA(o);');
  13784. Add(' ProcB(o);');
  13785. Add(' ProcC(o);');
  13786. ConvertProgram;
  13787. CheckSource('TestClass_TypeCastUntypedParam',
  13788. LinesToStr([ // statements
  13789. 'rtl.createClass(this, "TObject", null, function () {',
  13790. ' this.$init = function () {',
  13791. ' };',
  13792. ' this.$final = function () {',
  13793. ' };',
  13794. '});',
  13795. 'this.ProcA = function (A) {',
  13796. ' A.set(null);',
  13797. ' A.set(A.get());',
  13798. ' if (A.get() === null);',
  13799. ' if (null === A.get());',
  13800. '};',
  13801. 'this.ProcB = function (A) {',
  13802. ' A.set(null);',
  13803. ' A.set(A.get());',
  13804. ' if (A.get() === null);',
  13805. ' if (null === A.get());',
  13806. '};',
  13807. 'this.ProcC = function (A) {',
  13808. ' if (A === null);',
  13809. ' if (null === A);',
  13810. '};',
  13811. 'this.o = null;',
  13812. '']),
  13813. LinesToStr([ // $mod.$main
  13814. '$mod.ProcA({',
  13815. ' p: $mod,',
  13816. ' get: function () {',
  13817. ' return this.p.o;',
  13818. ' },',
  13819. ' set: function (v) {',
  13820. ' this.p.o = v;',
  13821. ' }',
  13822. '});',
  13823. '$mod.ProcB({',
  13824. ' p: $mod,',
  13825. ' get: function () {',
  13826. ' return this.p.o;',
  13827. ' },',
  13828. ' set: function (v) {',
  13829. ' this.p.o = v;',
  13830. ' }',
  13831. '});',
  13832. '$mod.ProcC($mod.o);',
  13833. '']));
  13834. end;
  13835. procedure TTestModule.TestClass_Overloads;
  13836. begin
  13837. StartProgram(false);
  13838. Add('type');
  13839. Add(' TObject = class');
  13840. Add(' procedure DoIt;');
  13841. Add(' procedure DoIt(vI: longint);');
  13842. Add(' end;');
  13843. Add('procedure TObject.DoIt;');
  13844. Add('begin');
  13845. Add(' DoIt;');
  13846. Add(' DoIt(1);');
  13847. Add('end;');
  13848. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13849. Add('begin');
  13850. ConvertProgram;
  13851. CheckSource('TestClass_Overloads',
  13852. LinesToStr([ // statements
  13853. 'rtl.createClass(this, "TObject", null, function () {',
  13854. ' this.$init = function () {',
  13855. ' };',
  13856. ' this.$final = function () {',
  13857. ' };',
  13858. ' this.DoIt = function () {',
  13859. ' this.DoIt();',
  13860. ' this.DoIt$1(1);',
  13861. ' };',
  13862. ' this.DoIt$1 = function (vI) {',
  13863. ' };',
  13864. '});',
  13865. '']),
  13866. LinesToStr([ // $mod.$main
  13867. '']));
  13868. end;
  13869. procedure TTestModule.TestClass_OverloadsAncestor;
  13870. begin
  13871. StartProgram(false);
  13872. Add('type');
  13873. Add(' TObject = class;');
  13874. Add(' TObject = class');
  13875. Add(' procedure DoIt(vA: longint);');
  13876. Add(' procedure DoIt(vA, vB: longint);');
  13877. Add(' end;');
  13878. Add(' TCar = class;');
  13879. Add(' TCar = class');
  13880. Add(' procedure DoIt(vA: longint);');
  13881. Add(' procedure DoIt(vA, vB: longint);');
  13882. Add(' end;');
  13883. Add('procedure tobject.doit(va: longint);');
  13884. Add('begin');
  13885. Add(' doit(1);');
  13886. Add(' doit(1,2);');
  13887. Add('end;');
  13888. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13889. Add('procedure tcar.doit(va: longint);');
  13890. Add('begin');
  13891. Add(' doit(1);');
  13892. Add(' doit(1,2);');
  13893. Add(' inherited doit(1);');
  13894. Add(' inherited doit(1,2);');
  13895. Add('end;');
  13896. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13897. Add('begin');
  13898. ConvertProgram;
  13899. CheckSource('TestClass_OverloadsAncestor',
  13900. LinesToStr([ // statements
  13901. 'rtl.createClass(this, "TObject", null, function () {',
  13902. ' this.$init = function () {',
  13903. ' };',
  13904. ' this.$final = function () {',
  13905. ' };',
  13906. ' this.DoIt = function (vA) {',
  13907. ' this.DoIt(1);',
  13908. ' this.DoIt$1(1,2);',
  13909. ' };',
  13910. ' this.DoIt$1 = function (vA, vB) {',
  13911. ' };',
  13912. '});',
  13913. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13914. ' this.DoIt$2 = function (vA) {',
  13915. ' this.DoIt$2(1);',
  13916. ' this.DoIt$3(1, 2);',
  13917. ' $mod.TObject.DoIt.call(this, 1);',
  13918. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13919. ' };',
  13920. ' this.DoIt$3 = function (vA, vB) {',
  13921. ' };',
  13922. '});',
  13923. '']),
  13924. LinesToStr([ // $mod.$main
  13925. '']));
  13926. end;
  13927. procedure TTestModule.TestClass_OverloadConstructor;
  13928. begin
  13929. StartProgram(false);
  13930. Add('type');
  13931. Add(' TObject = class');
  13932. Add(' constructor Create(vA: longint);');
  13933. Add(' constructor Create(vA, vB: longint);');
  13934. Add(' end;');
  13935. Add(' TCar = class');
  13936. Add(' constructor Create(vA: longint);');
  13937. Add(' constructor Create(vA, vB: longint);');
  13938. Add(' end;');
  13939. Add('constructor tobject.create(va: longint);');
  13940. Add('begin');
  13941. Add(' create(1);');
  13942. Add(' create(1,2);');
  13943. Add('end;');
  13944. Add('constructor tobject.create(va, vb: longint); begin end;');
  13945. Add('constructor tcar.create(va: longint);');
  13946. Add('begin');
  13947. Add(' create(1);');
  13948. Add(' create(1,2);');
  13949. Add(' inherited create(1);');
  13950. Add(' inherited create(1,2);');
  13951. Add('end;');
  13952. Add('constructor tcar.create(va, vb: longint); begin end;');
  13953. Add('begin');
  13954. Add(' tobject.create(1);');
  13955. Add(' tobject.create(1,2);');
  13956. Add(' tcar.create(1);');
  13957. Add(' tcar.create(1,2);');
  13958. ConvertProgram;
  13959. CheckSource('TestClass_OverloadConstructor',
  13960. LinesToStr([ // statements
  13961. 'rtl.createClass(this, "TObject", null, function () {',
  13962. ' this.$init = function () {',
  13963. ' };',
  13964. ' this.$final = function () {',
  13965. ' };',
  13966. ' this.Create = function (vA) {',
  13967. ' this.Create(1);',
  13968. ' this.Create$1(1,2);',
  13969. ' return this;',
  13970. ' };',
  13971. ' this.Create$1 = function (vA, vB) {',
  13972. ' return this;',
  13973. ' };',
  13974. '});',
  13975. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13976. ' this.Create$2 = function (vA) {',
  13977. ' this.Create$2(1);',
  13978. ' this.Create$3(1, 2);',
  13979. ' $mod.TObject.Create.call(this, 1);',
  13980. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13981. ' return this;',
  13982. ' };',
  13983. ' this.Create$3 = function (vA, vB) {',
  13984. ' return this;',
  13985. ' };',
  13986. '});',
  13987. '']),
  13988. LinesToStr([ // $mod.$main
  13989. '$mod.TObject.$create("Create", [1]);',
  13990. '$mod.TObject.$create("Create$1", [1, 2]);',
  13991. '$mod.TCar.$create("Create$2", [1]);',
  13992. '$mod.TCar.$create("Create$3", [1, 2]);',
  13993. '']));
  13994. end;
  13995. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13996. begin
  13997. StartProgram(false);
  13998. Add([
  13999. '{$mode delphi}',
  14000. 'type',
  14001. ' TObject = class end;',
  14002. ' TBird = class',
  14003. ' function {#a}GetValue: longint; overload; virtual;',
  14004. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14005. ' end;',
  14006. ' TEagle = class(TBird)',
  14007. ' function {#c}GetValue: longint; overload; override;',
  14008. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14009. ' end;',
  14010. 'function TBird.GetValue: longint;',
  14011. 'begin',
  14012. ' if 3={@a}GetValue then ;',
  14013. ' if 4={@b}GetValue(5) then ;',
  14014. 'end;',
  14015. 'function TBird.GetValue(AValue: longint): longint;',
  14016. 'begin',
  14017. 'end;',
  14018. 'function TEagle.GetValue: longint;',
  14019. 'begin',
  14020. ' if 13={@c}GetValue then ;',
  14021. ' if 14={@d}GetValue(15) then ;',
  14022. ' if 15=inherited {@a}GetValue then ;',
  14023. ' if 16=inherited {@b}GetValue(17) then ;',
  14024. 'end;',
  14025. 'function TEagle.GetValue(AValue: longint): longint;',
  14026. 'begin',
  14027. 'end;',
  14028. 'var',
  14029. ' e: TEagle;',
  14030. 'begin',
  14031. ' if 23=e.{@c}GetValue then ;',
  14032. ' if 24=e.{@d}GetValue(25) then ;']);
  14033. ConvertProgram;
  14034. CheckSource('TestClass_OverloadDelphiOverride',
  14035. LinesToStr([ // statements
  14036. 'rtl.createClass(this, "TObject", null, function () {',
  14037. ' this.$init = function () {',
  14038. ' };',
  14039. ' this.$final = function () {',
  14040. ' };',
  14041. '});',
  14042. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14043. ' this.GetValue = function () {',
  14044. ' var Result = 0;',
  14045. ' if (3 === this.GetValue()) ;',
  14046. ' if (4 === this.GetValue$1(5)) ;',
  14047. ' return Result;',
  14048. ' };',
  14049. ' this.GetValue$1 = function (AValue) {',
  14050. ' var Result = 0;',
  14051. ' return Result;',
  14052. ' };',
  14053. '});',
  14054. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14055. ' this.GetValue = function () {',
  14056. ' var Result = 0;',
  14057. ' if (13 === this.GetValue()) ;',
  14058. ' if (14 === this.GetValue$1(15)) ;',
  14059. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14060. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14061. ' return Result;',
  14062. ' };',
  14063. ' this.GetValue$1 = function (AValue) {',
  14064. ' var Result = 0;',
  14065. ' return Result;',
  14066. ' };',
  14067. '});',
  14068. 'this.e = null;',
  14069. '']),
  14070. LinesToStr([ // $mod.$main
  14071. 'if (23 === $mod.e.GetValue()) ;',
  14072. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14073. '']));
  14074. end;
  14075. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14076. begin
  14077. StartProgram(false);
  14078. Add([
  14079. '{$mode delphi}',
  14080. 'type',
  14081. ' TObject = class end;',
  14082. ' TAnimal = class',
  14083. ' public',
  14084. ' {#animal_a}A: longint;',
  14085. ' function {#animal_b}B: longint;',
  14086. ' end;',
  14087. ' TBird = class(TAnimal)',
  14088. ' public',
  14089. ' {#bird_a}A: double;',
  14090. ' {#bird_b}B: boolean;',
  14091. ' end;',
  14092. ' TEagle = class(TBird)',
  14093. ' public',
  14094. ' function {#eagle_a}A: boolean;',
  14095. ' {#eagle_b}B: double;',
  14096. ' end;',
  14097. 'function TAnimal.B: longint;',
  14098. 'begin',
  14099. 'end;',
  14100. 'function TEagle.A: boolean;',
  14101. 'begin',
  14102. ' {@eagle_b}B:=3.3;',
  14103. ' {@eagle_a}A();',
  14104. ' TBird(Self).{@bird_b}B:=true;',
  14105. ' TAnimal(Self).{@animal_a}A:=17;',
  14106. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14107. 'end;',
  14108. 'var',
  14109. ' e: TEagle;',
  14110. 'begin',
  14111. ' e.{@eagle_b}B:=5.3;',
  14112. ' if e.{@eagle_a}A then ;',
  14113. '']);
  14114. ConvertProgram;
  14115. CheckSource('TestClass_ReintroduceVarDelphi',
  14116. LinesToStr([ // statements
  14117. 'rtl.createClass(this, "TObject", null, function () {',
  14118. ' this.$init = function () {',
  14119. ' };',
  14120. ' this.$final = function () {',
  14121. ' };',
  14122. '});',
  14123. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14124. ' this.$init = function () {',
  14125. ' $mod.TObject.$init.call(this);',
  14126. ' this.A = 0;',
  14127. ' };',
  14128. ' this.B = function () {',
  14129. ' var Result = 0;',
  14130. ' return Result;',
  14131. ' };',
  14132. '});',
  14133. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14134. ' this.$init = function () {',
  14135. ' $mod.TAnimal.$init.call(this);',
  14136. ' this.A$1 = 0.0;',
  14137. ' this.B$1 = false;',
  14138. ' };',
  14139. '});',
  14140. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14141. ' this.$init = function () {',
  14142. ' $mod.TBird.$init.call(this);',
  14143. ' this.B$2 = 0.0;',
  14144. ' };',
  14145. ' this.A$2 = function () {',
  14146. ' var Result = false;',
  14147. ' this.B$2 = 3.3;',
  14148. ' this.A$2();',
  14149. ' this.B$1 = true;',
  14150. ' this.A = 17;',
  14151. ' this.B$1 = this.A$1 > 1;',
  14152. ' return Result;',
  14153. ' };',
  14154. '});',
  14155. 'this.e = null;',
  14156. '']),
  14157. LinesToStr([ // $mod.$main
  14158. '$mod.e.B$2 = 5.3;',
  14159. 'if ($mod.e.A$2()) ;',
  14160. '']));
  14161. end;
  14162. procedure TTestModule.TestClass_ReintroducedVar;
  14163. begin
  14164. StartProgram(false);
  14165. Add('type');
  14166. Add(' TObject = class');
  14167. Add(' strict private');
  14168. Add(' Some: longint;');
  14169. Add(' end;');
  14170. Add(' TMobile = class');
  14171. Add(' strict private');
  14172. Add(' Some: string;');
  14173. Add(' end;');
  14174. Add(' TCar = class(tmobile)');
  14175. Add(' procedure Some;');
  14176. Add(' procedure Some(vA: longint);');
  14177. Add(' end;');
  14178. Add('procedure tcar.some;');
  14179. Add('begin');
  14180. Add(' Some;');
  14181. Add(' Some(1);');
  14182. Add('end;');
  14183. Add('procedure tcar.some(va: longint); begin end;');
  14184. Add('begin');
  14185. ConvertProgram;
  14186. CheckSource('TestClass_ReintroducedVar',
  14187. LinesToStr([ // statements
  14188. 'rtl.createClass(this, "TObject", null, function () {',
  14189. ' this.$init = function () {',
  14190. ' this.Some = 0;',
  14191. ' };',
  14192. ' this.$final = function () {',
  14193. ' };',
  14194. '});',
  14195. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14196. ' this.$init = function () {',
  14197. ' $mod.TObject.$init.call(this);',
  14198. ' this.Some$1 = "";',
  14199. ' };',
  14200. '});',
  14201. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14202. ' this.Some$2 = function () {',
  14203. ' this.Some$2();',
  14204. ' this.Some$3(1);',
  14205. ' };',
  14206. ' this.Some$3 = function (vA) {',
  14207. ' };',
  14208. '});',
  14209. '']),
  14210. LinesToStr([ // $mod.$main
  14211. '']));
  14212. end;
  14213. procedure TTestModule.TestClass_RaiseDescendant;
  14214. begin
  14215. StartProgram(false);
  14216. Add([
  14217. 'type',
  14218. ' TObject = class',
  14219. ' constructor Create(Msg: string);',
  14220. ' end;',
  14221. ' Exception = class',
  14222. ' end;',
  14223. ' EConvertError = class(Exception)',
  14224. ' end;',
  14225. 'constructor TObject.Create(Msg: string); begin end;',
  14226. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14227. 'begin',
  14228. ' raise Exception.Create(''Bar1'');',
  14229. ' raise EConvertError.Create(''Bar2'');',
  14230. ' raise AssertConv(''Bar2'');',
  14231. ' raise AssertConv;',
  14232. '']);
  14233. ConvertProgram;
  14234. CheckSource('TestClass_RaiseDescendant',
  14235. LinesToStr([ // statements
  14236. 'rtl.createClass(this, "TObject", null, function () {',
  14237. ' this.$init = function () {',
  14238. ' };',
  14239. ' this.$final = function () {',
  14240. ' };',
  14241. ' this.Create = function (Msg) {',
  14242. ' return this;',
  14243. ' };',
  14244. '});',
  14245. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14246. '});',
  14247. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14248. '});',
  14249. 'this.AssertConv = function (Msg) {',
  14250. ' var Result = null;',
  14251. ' return Result;',
  14252. '};',
  14253. '']),
  14254. LinesToStr([ // $mod.$main
  14255. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14256. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14257. 'throw $mod.AssertConv("Bar2");',
  14258. 'throw $mod.AssertConv("def");',
  14259. '']));
  14260. end;
  14261. procedure TTestModule.TestClass_ExternalMethod;
  14262. begin
  14263. AddModuleWithIntfImplSrc('unit2.pas',
  14264. LinesToStr([
  14265. 'type',
  14266. ' TObject = class',
  14267. ' public',
  14268. ' procedure Intern; external name ''$DoIntern'';',
  14269. ' end;',
  14270. '']),
  14271. LinesToStr([
  14272. '']));
  14273. StartUnit(true);
  14274. Add('interface');
  14275. Add('uses unit2;');
  14276. Add('type');
  14277. Add(' TCar = class(TObject)');
  14278. Add(' public');
  14279. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14280. Add(' procedure DoIt;');
  14281. Add(' end;');
  14282. Add('implementation');
  14283. Add('procedure tcar.doit;');
  14284. Add('begin');
  14285. Add(' Intern;');
  14286. Add(' Intern();');
  14287. Add(' Intern2;');
  14288. Add(' Intern2();');
  14289. Add('end;');
  14290. Add('var Obj: TCar;');
  14291. Add('begin');
  14292. Add(' obj.intern;');
  14293. Add(' obj.intern();');
  14294. Add(' obj.intern2;');
  14295. Add(' obj.intern2();');
  14296. Add(' obj.doit;');
  14297. Add(' obj.doit();');
  14298. Add(' with obj do begin');
  14299. Add(' Intern;');
  14300. Add(' Intern();');
  14301. Add(' Intern2;');
  14302. Add(' Intern2();');
  14303. Add(' end;');
  14304. ConvertUnit;
  14305. CheckSource('TestClass_ExternalMethod',
  14306. LinesToStr([
  14307. 'var $impl = $mod.$impl;',
  14308. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14309. ' this.DoIt = function () {',
  14310. ' this.$DoIntern();',
  14311. ' this.$DoIntern();',
  14312. ' this.$DoIntern2();',
  14313. ' this.$DoIntern2();',
  14314. ' };',
  14315. ' });',
  14316. '']),
  14317. LinesToStr([ // this.$init
  14318. '$impl.Obj.$DoIntern();',
  14319. '$impl.Obj.$DoIntern();',
  14320. '$impl.Obj.$DoIntern2();',
  14321. '$impl.Obj.$DoIntern2();',
  14322. '$impl.Obj.DoIt();',
  14323. '$impl.Obj.DoIt();',
  14324. 'var $with = $impl.Obj;',
  14325. '$with.$DoIntern();',
  14326. '$with.$DoIntern();',
  14327. '$with.$DoIntern2();',
  14328. '$with.$DoIntern2();',
  14329. '']),
  14330. LinesToStr([ // implementation
  14331. '$impl.Obj = null;',
  14332. '']) );
  14333. end;
  14334. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14335. begin
  14336. StartProgram(false);
  14337. Add('type');
  14338. Add(' TObject = class');
  14339. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14340. Add(' end;');
  14341. Add('begin');
  14342. SetExpectedPasResolverError('Virtual method name must match external',
  14343. nVirtualMethodNameMustMatchExternal);
  14344. ConvertProgram;
  14345. end;
  14346. procedure TTestModule.TestClass_ExternalOverrideFail;
  14347. begin
  14348. StartProgram(false);
  14349. Add('type');
  14350. Add(' TObject = class');
  14351. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14352. Add(' end;');
  14353. Add(' TCar = class');
  14354. Add(' procedure DoIt; override; external name ''DoIt'';');
  14355. Add(' end;');
  14356. Add('begin');
  14357. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14358. nInvalidXModifierY);
  14359. ConvertProgram;
  14360. end;
  14361. procedure TTestModule.TestClass_ExternalVar;
  14362. begin
  14363. AddModuleWithIntfImplSrc('unit2.pas',
  14364. LinesToStr([
  14365. '{$modeswitch externalclass}',
  14366. 'type',
  14367. ' TObject = class',
  14368. ' public',
  14369. ' Intern: longint external name ''$Intern'';',
  14370. ' Bracket: longint external name ''["A B"]'';',
  14371. ' end;',
  14372. '']),
  14373. LinesToStr([
  14374. '']));
  14375. StartUnit(true);
  14376. Add([
  14377. 'interface',
  14378. 'uses unit2;',
  14379. '{$modeswitch externalclass}',
  14380. 'type',
  14381. ' TCar = class(tobject)',
  14382. ' public',
  14383. ' Intern2: longint external name ''$Intern2'';',
  14384. ' procedure DoIt;',
  14385. ' end;',
  14386. 'implementation',
  14387. 'procedure tcar.doit;',
  14388. 'begin',
  14389. ' Intern:=Intern+1;',
  14390. ' Intern2:=Intern2+2;',
  14391. ' Bracket:=Bracket+3;',
  14392. 'end;',
  14393. 'var Obj: TCar;',
  14394. 'begin',
  14395. ' obj.intern:=obj.intern+1;',
  14396. ' obj.intern2:=obj.intern2+2;',
  14397. ' obj.Bracket:=obj.Bracket+3;',
  14398. ' with obj do begin',
  14399. ' intern:=intern+1;',
  14400. ' intern2:=intern2+2;',
  14401. ' Bracket:=Bracket+3;',
  14402. ' end;']);
  14403. ConvertUnit;
  14404. CheckSource('TestClass_ExternalVar',
  14405. LinesToStr([
  14406. 'var $impl = $mod.$impl;',
  14407. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14408. ' this.DoIt = function () {',
  14409. ' this.$Intern = this.$Intern + 1;',
  14410. ' this.$Intern2 = this.$Intern2 + 2;',
  14411. ' this["A B"] = this["A B"] + 3;',
  14412. ' };',
  14413. ' });',
  14414. '']),
  14415. LinesToStr([
  14416. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14417. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14418. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14419. 'var $with = $impl.Obj;',
  14420. '$with.$Intern = $with.$Intern + 1;',
  14421. '$with.$Intern2 = $with.$Intern2 + 2;',
  14422. '$with["A B"] = $with["A B"] + 3;',
  14423. '']),
  14424. LinesToStr([ // implementation
  14425. '$impl.Obj = null;',
  14426. '']));
  14427. end;
  14428. procedure TTestModule.TestClass_Const;
  14429. begin
  14430. StartProgram(false);
  14431. Add([
  14432. 'type',
  14433. ' integer = longint;',
  14434. ' TClass = class of TObject;',
  14435. ' TObject = class',
  14436. ' public',
  14437. ' const cI: integer = 3;',
  14438. ' procedure DoIt;',
  14439. ' class procedure DoMore;',
  14440. ' end;',
  14441. 'procedure tobject.doit;',
  14442. 'begin',
  14443. ' if cI=4 then;',
  14444. ' if 5=cI then;',
  14445. ' if Self.cI=6 then;',
  14446. ' if 7=Self.cI then;',
  14447. ' with Self do begin',
  14448. ' if cI=11 then;',
  14449. ' if 12=cI then;',
  14450. ' end;',
  14451. 'end;',
  14452. 'class procedure tobject.domore;',
  14453. 'begin',
  14454. ' if cI=8 then;',
  14455. ' if Self.cI=9 then;',
  14456. ' if 10=cI then;',
  14457. ' if 11=Self.cI then;',
  14458. ' with Self do begin',
  14459. ' if cI=13 then;',
  14460. ' if 14=cI then;',
  14461. ' end;',
  14462. 'end;',
  14463. 'var',
  14464. ' Obj: TObject;',
  14465. ' Cla: TClass;',
  14466. 'begin',
  14467. ' if TObject.cI=21 then ;',
  14468. ' if Obj.cI=22 then ;',
  14469. ' if Cla.cI=23 then ;',
  14470. ' with obj do if ci=24 then;',
  14471. ' with TObject do if ci=25 then;',
  14472. ' with Cla do if ci=26 then;']);
  14473. ConvertProgram;
  14474. CheckSource('TestClass_Const',
  14475. LinesToStr([
  14476. 'rtl.createClass(this, "TObject", null, function () {',
  14477. ' this.cI = 3;',
  14478. ' this.$init = function () {',
  14479. ' };',
  14480. ' this.$final = function () {',
  14481. ' };',
  14482. ' this.DoIt = function () {',
  14483. ' if (this.cI === 4) ;',
  14484. ' if (5 === this.cI) ;',
  14485. ' if (this.cI === 6) ;',
  14486. ' if (7 === this.cI) ;',
  14487. ' if (this.cI === 11) ;',
  14488. ' if (12 === this.cI) ;',
  14489. ' };',
  14490. ' this.DoMore = function () {',
  14491. ' if (this.cI === 8) ;',
  14492. ' if (this.cI === 9) ;',
  14493. ' if (10 === this.cI) ;',
  14494. ' if (11 === this.cI) ;',
  14495. ' if (this.cI === 13) ;',
  14496. ' if (14 === this.cI) ;',
  14497. ' };',
  14498. '});',
  14499. 'this.Obj = null;',
  14500. 'this.Cla = null;',
  14501. '']),
  14502. LinesToStr([
  14503. 'if ($mod.TObject.cI === 21) ;',
  14504. 'if ($mod.Obj.cI === 22) ;',
  14505. 'if ($mod.Cla.cI === 23) ;',
  14506. 'var $with = $mod.Obj;',
  14507. 'if ($with.cI === 24) ;',
  14508. 'var $with1 = $mod.TObject;',
  14509. 'if ($with1.cI === 25) ;',
  14510. 'var $with2 = $mod.Cla;',
  14511. 'if ($with2.cI === 26) ;',
  14512. '']));
  14513. end;
  14514. procedure TTestModule.TestClass_ConstEnum;
  14515. begin
  14516. StartProgram(false);
  14517. Add([
  14518. 'type',
  14519. ' TEnum = (red,blue);',
  14520. ' TObject = class',
  14521. ' end;',
  14522. ' TAnimal = class',
  14523. ' public',
  14524. ' type TSubEnum = (light,dark);',
  14525. ' const a = high(TEnum);',
  14526. ' const b = high(TSubEnum);',
  14527. ' end;',
  14528. ' TBird = class(TAnimal)',
  14529. ' public',
  14530. ' const c = high(TEnum);',
  14531. ' const d = high(TSubEnum);',
  14532. ' end;',
  14533. ' TAnt = class',
  14534. ' public',
  14535. ' const e = high(TEnum);',
  14536. ' const f = high(TBird.TSubEnum);',
  14537. ' end;',
  14538. 'begin',
  14539. '']);
  14540. ConvertProgram;
  14541. CheckSource('TestClass_ConstEnum',
  14542. LinesToStr([
  14543. 'this.TEnum = {',
  14544. ' "0": "red",',
  14545. ' red: 0,',
  14546. ' "1": "blue",',
  14547. ' blue: 1',
  14548. '};',
  14549. 'rtl.createClass(this, "TObject", null, function () {',
  14550. ' this.$init = function () {',
  14551. ' };',
  14552. ' this.$final = function () {',
  14553. ' };',
  14554. '});',
  14555. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14556. ' this.TSubEnum = {',
  14557. ' "0": "light",',
  14558. ' light: 0,',
  14559. ' "1": "dark",',
  14560. ' dark: 1',
  14561. ' };',
  14562. ' this.a = $mod.TEnum.blue;',
  14563. ' this.b = this.TSubEnum.dark;',
  14564. '});',
  14565. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14566. ' this.c = $mod.TEnum.blue;',
  14567. ' this.d = this.TSubEnum.dark;',
  14568. '});',
  14569. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14570. ' this.e = $mod.TEnum.blue;',
  14571. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14572. '});',
  14573. '']),
  14574. LinesToStr([
  14575. '']));
  14576. end;
  14577. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14578. begin
  14579. StartProgram(false);
  14580. Add([
  14581. 'type',
  14582. ' TObject = class',
  14583. ' const cI: longint = 3;',
  14584. ' procedure Fly;',
  14585. ' procedure Run;',
  14586. ' end;',
  14587. ' TBird = class',
  14588. ' procedure Go;',
  14589. ' end;',
  14590. 'procedure tobject.fly;',
  14591. 'const cI: word = 4;',
  14592. 'begin',
  14593. ' if cI=Self.cI then ;',
  14594. 'end;',
  14595. 'procedure tobject.run;',
  14596. 'const cI: word = 5;',
  14597. 'begin',
  14598. ' if cI=Self.cI then ;',
  14599. 'end;',
  14600. 'procedure tbird.go;',
  14601. 'const cI: word = 6;',
  14602. 'begin',
  14603. ' if cI=Self.cI then ;',
  14604. 'end;',
  14605. 'begin',
  14606. '']);
  14607. ConvertProgram;
  14608. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14609. LinesToStr([
  14610. 'rtl.createClass(this, "TObject", null, function () {',
  14611. ' this.cI = 3;',
  14612. ' this.$init = function () {',
  14613. ' };',
  14614. ' this.$final = function () {',
  14615. ' };',
  14616. ' var cI$1 = 4;',
  14617. ' this.Fly = function () {',
  14618. ' if (cI$1 === this.cI) ;',
  14619. ' };',
  14620. ' var cI$2 = 5;',
  14621. ' this.Run = function () {',
  14622. ' if (cI$2 === this.cI) ;',
  14623. ' };',
  14624. '});',
  14625. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14626. ' var cI$3 = 6;',
  14627. ' this.Go = function () {',
  14628. ' if (cI$3 === this.cI) ;',
  14629. ' };',
  14630. '});',
  14631. '']),
  14632. LinesToStr([
  14633. '']));
  14634. end;
  14635. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14636. begin
  14637. StartUnit(false);
  14638. Add([
  14639. 'interface',
  14640. 'type',
  14641. ' TObject = class',
  14642. ' const cI: longint = 3;',
  14643. ' procedure Fly;',
  14644. ' procedure Run;',
  14645. ' end;',
  14646. ' TBird = class',
  14647. ' procedure Go;',
  14648. ' end;',
  14649. 'implementation',
  14650. 'procedure tobject.fly;',
  14651. 'const cI: word = 4;',
  14652. 'begin',
  14653. ' if cI=Self.cI then ;',
  14654. 'end;',
  14655. 'procedure tobject.run;',
  14656. 'const cI: word = 5;',
  14657. 'begin',
  14658. ' if cI=Self.cI then ;',
  14659. 'end;',
  14660. 'procedure tbird.go;',
  14661. 'const cI: word = 6;',
  14662. 'begin',
  14663. ' if cI=Self.cI then ;',
  14664. 'end;',
  14665. '']);
  14666. ConvertUnit;
  14667. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14668. LinesToStr([
  14669. 'rtl.createClass(this, "TObject", null, function () {',
  14670. ' this.cI = 3;',
  14671. ' this.$init = function () {',
  14672. ' };',
  14673. ' this.$final = function () {',
  14674. ' };',
  14675. ' var cI$1 = 4;',
  14676. ' this.Fly = function () {',
  14677. ' if (cI$1 === this.cI) ;',
  14678. ' };',
  14679. ' var cI$2 = 5;',
  14680. ' this.Run = function () {',
  14681. ' if (cI$2 === this.cI) ;',
  14682. ' };',
  14683. '});',
  14684. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14685. ' var cI$3 = 6;',
  14686. ' this.Go = function () {',
  14687. ' if (cI$3 === this.cI) ;',
  14688. ' };',
  14689. '});',
  14690. '']),
  14691. '',
  14692. '');
  14693. end;
  14694. procedure TTestModule.TestClass_LocalVarSelfFail;
  14695. begin
  14696. StartProgram(false);
  14697. Add([
  14698. 'type',
  14699. ' TObject = class',
  14700. ' constructor Create;',
  14701. ' end;',
  14702. 'constructor tobject.create;',
  14703. 'var self: longint;',
  14704. 'begin',
  14705. 'end',
  14706. 'begin',
  14707. '']);
  14708. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14709. ConvertProgram;
  14710. end;
  14711. procedure TTestModule.TestClass_ArgSelfFail;
  14712. begin
  14713. StartProgram(false);
  14714. Add([
  14715. 'type',
  14716. ' TObject = class',
  14717. ' procedure DoIt(Self: longint);',
  14718. ' end;',
  14719. 'procedure tobject.doit(self: longint);',
  14720. 'begin',
  14721. 'end',
  14722. 'begin',
  14723. '']);
  14724. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14725. ConvertProgram;
  14726. end;
  14727. procedure TTestModule.TestClass_NestedProcSelf;
  14728. begin
  14729. StartProgram(false);
  14730. Add([
  14731. 'type',
  14732. ' TObject = class',
  14733. ' Key: longint;',
  14734. ' class var State: longint;',
  14735. ' procedure DoIt;',
  14736. ' function GetSize: longint; virtual; abstract;',
  14737. ' procedure SetSize(Value: longint); virtual; abstract;',
  14738. ' property Size: longint read GetSize write SetSize;',
  14739. ' end;',
  14740. 'procedure tobject.doit;',
  14741. ' procedure Sub;',
  14742. ' begin',
  14743. ' key:=key+2;',
  14744. ' self.key:=self.key+3;',
  14745. ' state:=state+4;',
  14746. ' self.state:=self.state+5;',
  14747. ' tobject.state:=tobject.state+6;',
  14748. ' size:=size+7;',
  14749. ' self.size:=self.size+8;',
  14750. ' end;',
  14751. 'begin',
  14752. ' sub;',
  14753. ' key:=key+12;',
  14754. ' self.key:=self.key+13;',
  14755. ' state:=state+14;',
  14756. ' self.state:=self.state+15;',
  14757. ' tobject.state:=tobject.state+16;',
  14758. ' size:=size+17;',
  14759. ' self.size:=self.size+18;',
  14760. 'end;',
  14761. 'begin',
  14762. '']);
  14763. ConvertProgram;
  14764. CheckSource('TestClass_NestedProcSelf',
  14765. LinesToStr([ // statements
  14766. 'rtl.createClass(this, "TObject", null, function () {',
  14767. ' this.State = 0;',
  14768. ' this.$init = function () {',
  14769. ' this.Key = 0;',
  14770. ' };',
  14771. ' this.$final = function () {',
  14772. ' };',
  14773. ' this.DoIt = function () {',
  14774. ' var $Self = this;',
  14775. ' function Sub() {',
  14776. ' $Self.Key = $Self.Key + 2;',
  14777. ' $Self.Key = $Self.Key + 3;',
  14778. ' $mod.TObject.State = $Self.State + 4;',
  14779. ' $mod.TObject.State = $Self.State + 5;',
  14780. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14781. ' $Self.SetSize($Self.GetSize() + 7);',
  14782. ' $Self.SetSize($Self.GetSize() + 8);',
  14783. ' };',
  14784. ' Sub();',
  14785. ' this.Key = this.Key + 12;',
  14786. ' $Self.Key = $Self.Key + 13;',
  14787. ' $mod.TObject.State = this.State + 14;',
  14788. ' $mod.TObject.State = $Self.State + 15;',
  14789. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14790. ' this.SetSize(this.GetSize() + 17);',
  14791. ' $Self.SetSize($Self.GetSize() + 18);',
  14792. ' };',
  14793. '});',
  14794. '']),
  14795. LinesToStr([ // $mod.$main
  14796. '']));
  14797. end;
  14798. procedure TTestModule.TestClass_NestedProcSelf2;
  14799. begin
  14800. StartProgram(false);
  14801. Add([
  14802. 'type',
  14803. ' TObject = class',
  14804. ' Key: longint;',
  14805. ' class var State: longint;',
  14806. ' function GetSize: longint; virtual; abstract;',
  14807. ' procedure SetSize(Value: longint); virtual; abstract;',
  14808. ' property Size: longint read GetSize write SetSize;',
  14809. ' end;',
  14810. ' TBird = class',
  14811. ' procedure DoIt;',
  14812. ' end;',
  14813. 'procedure tbird.doit;',
  14814. ' procedure Sub;',
  14815. ' begin',
  14816. ' key:=key+2;',
  14817. ' self.key:=self.key+3;',
  14818. ' state:=state+4;',
  14819. ' self.state:=self.state+5;',
  14820. ' tobject.state:=tobject.state+6;',
  14821. ' size:=size+7;',
  14822. ' self.size:=self.size+8;',
  14823. ' end;',
  14824. 'begin',
  14825. ' sub;',
  14826. ' key:=key+12;',
  14827. ' self.key:=self.key+13;',
  14828. ' state:=state+14;',
  14829. ' self.state:=self.state+15;',
  14830. ' tobject.state:=tobject.state+16;',
  14831. ' size:=size+17;',
  14832. ' self.size:=self.size+18;',
  14833. 'end;',
  14834. 'begin',
  14835. '']);
  14836. ConvertProgram;
  14837. CheckSource('TestClass_NestedProcSelf2',
  14838. LinesToStr([ // statements
  14839. 'rtl.createClass(this, "TObject", null, function () {',
  14840. ' this.State = 0;',
  14841. ' this.$init = function () {',
  14842. ' this.Key = 0;',
  14843. ' };',
  14844. ' this.$final = function () {',
  14845. ' };',
  14846. '});',
  14847. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14848. ' this.DoIt = function () {',
  14849. ' var $Self = this;',
  14850. ' function Sub() {',
  14851. ' $Self.Key = $Self.Key + 2;',
  14852. ' $Self.Key = $Self.Key + 3;',
  14853. ' $mod.TObject.State = $Self.State + 4;',
  14854. ' $mod.TObject.State = $Self.State + 5;',
  14855. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14856. ' $Self.SetSize($Self.GetSize() + 7);',
  14857. ' $Self.SetSize($Self.GetSize() + 8);',
  14858. ' };',
  14859. ' Sub();',
  14860. ' this.Key = this.Key + 12;',
  14861. ' $Self.Key = $Self.Key + 13;',
  14862. ' $mod.TObject.State = this.State + 14;',
  14863. ' $mod.TObject.State = $Self.State + 15;',
  14864. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14865. ' this.SetSize(this.GetSize() + 17);',
  14866. ' $Self.SetSize($Self.GetSize() + 18);',
  14867. ' };',
  14868. '});',
  14869. '']),
  14870. LinesToStr([ // $mod.$main
  14871. '']));
  14872. end;
  14873. procedure TTestModule.TestClass_NestedProcClassSelf;
  14874. begin
  14875. StartProgram(false);
  14876. Add([
  14877. 'type',
  14878. ' TObject = class',
  14879. ' class var State: longint;',
  14880. ' class procedure DoIt;',
  14881. ' class function GetSize: longint; virtual; abstract;',
  14882. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14883. ' class property Size: longint read GetSize write SetSize;',
  14884. ' end;',
  14885. 'class procedure tobject.doit;',
  14886. ' procedure Sub;',
  14887. ' begin',
  14888. ' state:=state+2;',
  14889. ' self.state:=self.state+3;',
  14890. ' tobject.state:=tobject.state+4;',
  14891. ' size:=size+5;',
  14892. ' self.size:=self.size+6;',
  14893. ' tobject.size:=tobject.size+7;',
  14894. ' end;',
  14895. 'begin',
  14896. ' sub;',
  14897. ' state:=state+12;',
  14898. ' self.state:=self.state+13;',
  14899. ' tobject.state:=tobject.state+14;',
  14900. ' size:=size+15;',
  14901. ' self.size:=self.size+16;',
  14902. ' tobject.size:=tobject.size+17;',
  14903. 'end;',
  14904. 'begin',
  14905. '']);
  14906. ConvertProgram;
  14907. CheckSource('TestClass_NestedProcClassSelf',
  14908. LinesToStr([ // statements
  14909. 'rtl.createClass(this, "TObject", null, function () {',
  14910. ' this.State = 0;',
  14911. ' this.$init = function () {',
  14912. ' };',
  14913. ' this.$final = function () {',
  14914. ' };',
  14915. ' this.DoIt = function () {',
  14916. ' var $Self = this;',
  14917. ' function Sub() {',
  14918. ' $mod.TObject.State = $Self.State + 2;',
  14919. ' $mod.TObject.State = $Self.State + 3;',
  14920. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14921. ' $Self.SetSize($Self.GetSize() + 5);',
  14922. ' $Self.SetSize($Self.GetSize() + 6);',
  14923. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14924. ' };',
  14925. ' Sub();',
  14926. ' $mod.TObject.State = this.State + 12;',
  14927. ' $mod.TObject.State = $Self.State + 13;',
  14928. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14929. ' this.SetSize(this.GetSize() + 15);',
  14930. ' $Self.SetSize($Self.GetSize() + 16);',
  14931. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14932. ' };',
  14933. '});',
  14934. '']),
  14935. LinesToStr([ // $mod.$main
  14936. '']));
  14937. end;
  14938. procedure TTestModule.TestClass_NestedProcCallInherited;
  14939. begin
  14940. StartProgram(false);
  14941. Add([
  14942. 'type',
  14943. ' TObject = class',
  14944. ' function DoIt(k: boolean): longint; virtual;',
  14945. ' end;',
  14946. ' TBird = class',
  14947. ' function DoIt(k: boolean): longint; override;',
  14948. ' end;',
  14949. 'function tobject.doit(k: boolean): longint;',
  14950. 'begin',
  14951. 'end;',
  14952. 'function tbird.doit(k: boolean): longint;',
  14953. ' procedure Sub;',
  14954. ' begin',
  14955. ' inherited DoIt(true);',
  14956. //' if inherited DoIt(false)=4 then ;',
  14957. ' end;',
  14958. 'begin',
  14959. ' Sub;',
  14960. ' inherited;',
  14961. ' inherited DoIt(true);',
  14962. //' if inherited DoIt(false)=14 then ;',
  14963. 'end;',
  14964. 'begin',
  14965. '']);
  14966. ConvertProgram;
  14967. CheckSource('TestClass_NestedProcCallInherited',
  14968. LinesToStr([ // statements
  14969. 'rtl.createClass(this, "TObject", null, function () {',
  14970. ' this.$init = function () {',
  14971. ' };',
  14972. ' this.$final = function () {',
  14973. ' };',
  14974. ' this.DoIt = function (k) {',
  14975. ' var Result = 0;',
  14976. ' return Result;',
  14977. ' };',
  14978. '});',
  14979. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14980. ' this.DoIt = function (k) {',
  14981. ' var $Self = this;',
  14982. ' var Result = 0;',
  14983. ' function Sub() {',
  14984. ' $mod.TObject.DoIt.call($Self, true);',
  14985. ' };',
  14986. ' Sub();',
  14987. ' $mod.TObject.DoIt.apply(this, arguments);',
  14988. ' $mod.TObject.DoIt.call(this, true);',
  14989. ' return Result;',
  14990. ' };',
  14991. '});',
  14992. '']),
  14993. LinesToStr([ // $mod.$main
  14994. '']));
  14995. end;
  14996. procedure TTestModule.TestClass_TObjectFree;
  14997. begin
  14998. StartProgram(false);
  14999. Add([
  15000. 'type',
  15001. ' TObject = class',
  15002. ' Obj: tobject;',
  15003. ' procedure Free;',
  15004. ' procedure Release;',
  15005. ' end;',
  15006. 'procedure tobject.free;',
  15007. 'begin',
  15008. 'end;',
  15009. 'procedure tobject.release;',
  15010. 'begin',
  15011. ' free;',
  15012. ' if true then free;',
  15013. 'end;',
  15014. 'function DoIt(o: tobject): tobject;',
  15015. 'var l: tobject;',
  15016. 'begin',
  15017. ' o.free;',
  15018. ' o.free();',
  15019. ' l.free;',
  15020. ' l.free();',
  15021. ' o.obj.free;',
  15022. ' o.obj.free();',
  15023. ' with o do obj.free;',
  15024. ' with o do obj.free();',
  15025. ' result.Free;',
  15026. ' result.Free();',
  15027. 'end;',
  15028. 'var o: tobject;',
  15029. ' a: array of tobject;',
  15030. 'begin',
  15031. ' o.free;',
  15032. ' o.obj.free;',
  15033. ' a[1+2].free;',
  15034. '']);
  15035. ConvertProgram;
  15036. CheckSource('TestClass_TObjectFree',
  15037. LinesToStr([ // statements
  15038. 'rtl.createClass(this, "TObject", null, function () {',
  15039. ' this.$init = function () {',
  15040. ' this.Obj = null;',
  15041. ' };',
  15042. ' this.$final = function () {',
  15043. ' this.Obj = undefined;',
  15044. ' };',
  15045. ' this.Free = function () {',
  15046. ' };',
  15047. ' this.Release = function () {',
  15048. ' this.Free();',
  15049. ' if (true) this.Free();',
  15050. ' };',
  15051. '});',
  15052. 'this.DoIt = function (o) {',
  15053. ' var Result = null;',
  15054. ' var l = null;',
  15055. ' o = rtl.freeLoc(o);',
  15056. ' o = rtl.freeLoc(o);',
  15057. ' l = rtl.freeLoc(l);',
  15058. ' l = rtl.freeLoc(l);',
  15059. ' rtl.free(o, "Obj");',
  15060. ' rtl.free(o, "Obj");',
  15061. ' rtl.free(o, "Obj");',
  15062. ' rtl.free(o, "Obj");',
  15063. ' Result = rtl.freeLoc(Result);',
  15064. ' Result = rtl.freeLoc(Result);',
  15065. ' return Result;',
  15066. '};',
  15067. 'this.o = null;',
  15068. 'this.a = [];',
  15069. '']),
  15070. LinesToStr([ // $mod.$main
  15071. 'rtl.free($mod, "o");',
  15072. 'rtl.free($mod.o, "Obj");',
  15073. 'rtl.free($mod.a, 1 + 2);',
  15074. '']));
  15075. end;
  15076. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15077. begin
  15078. StartProgram(false);
  15079. Add([
  15080. 'type',
  15081. ' TObject = class',
  15082. ' Obj: tobject;',
  15083. ' procedure Free;',
  15084. ' end;',
  15085. 'procedure tobject.free;',
  15086. 'begin',
  15087. 'end;',
  15088. 'procedure DoIt(var o: tobject);',
  15089. 'begin',
  15090. ' o.free;',
  15091. ' o.free();',
  15092. 'end;',
  15093. 'begin',
  15094. '']);
  15095. ConvertProgram;
  15096. CheckSource('TestClass_TObjectFree_VarArg',
  15097. LinesToStr([ // statements
  15098. 'rtl.createClass(this, "TObject", null, function () {',
  15099. ' this.$init = function () {',
  15100. ' this.Obj = null;',
  15101. ' };',
  15102. ' this.$final = function () {',
  15103. ' this.Obj = undefined;',
  15104. ' };',
  15105. ' this.Free = function () {',
  15106. ' };',
  15107. '});',
  15108. 'this.DoIt = function (o) {',
  15109. ' o.set(rtl.freeLoc(o.get()));',
  15110. ' o.set(rtl.freeLoc(o.get()));',
  15111. '};',
  15112. '']),
  15113. LinesToStr([ // $mod.$main
  15114. '']));
  15115. end;
  15116. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15117. begin
  15118. StartProgram(false);
  15119. Add([
  15120. 'type',
  15121. ' TObject = class',
  15122. ' constructor Create;',
  15123. ' procedure Free;',
  15124. ' end;',
  15125. 'constructor TObject.Create; begin end;',
  15126. 'procedure tobject.free; begin end;',
  15127. 'begin',
  15128. ' with tobject.create do free;',
  15129. '']);
  15130. ConvertProgram;
  15131. CheckSource('TestClass_TObjectFreeNewInstance',
  15132. LinesToStr([ // statements
  15133. 'rtl.createClass(this, "TObject", null, function () {',
  15134. ' this.$init = function () {',
  15135. ' };',
  15136. ' this.$final = function () {',
  15137. ' };',
  15138. ' this.Create = function () {',
  15139. ' return this;',
  15140. ' };',
  15141. ' this.Free = function () {',
  15142. ' };',
  15143. '});',
  15144. '']),
  15145. LinesToStr([ // $mod.$main
  15146. 'var $with = $mod.TObject.$create("Create");',
  15147. '$with=rtl.freeLoc($with);',
  15148. '']));
  15149. end;
  15150. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15151. begin
  15152. StartProgram(false);
  15153. Add([
  15154. 'type',
  15155. ' TObject = class',
  15156. ' destructor Destroy;',
  15157. ' procedure Free;',
  15158. ' end;',
  15159. 'destructor TObject.Destroy; begin end;',
  15160. 'procedure tobject.free; begin end;',
  15161. 'var o: tobject;',
  15162. 'begin',
  15163. ' o.free;',
  15164. '']);
  15165. Converter.UseLowerCase:=true;
  15166. ConvertProgram;
  15167. CheckSource('TestClass_TObjectFreeLowerCase',
  15168. LinesToStr([ // statements
  15169. 'rtl.createClass(this, "tobject", null, function () {',
  15170. ' this.$init = function () {',
  15171. ' };',
  15172. ' this.$final = function () {',
  15173. ' };',
  15174. ' rtl.tObjectDestroy = "destroy";',
  15175. ' this.destroy = function () {',
  15176. ' };',
  15177. ' this.free = function () {',
  15178. ' };',
  15179. '});',
  15180. 'this.o = null;',
  15181. '']),
  15182. LinesToStr([ // $mod.$main
  15183. 'rtl.free($mod, "o");',
  15184. '']));
  15185. end;
  15186. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15187. begin
  15188. StartProgram(false);
  15189. Add([
  15190. 'type',
  15191. ' TObject = class',
  15192. ' procedure Free;',
  15193. ' function GetObj: tobject; virtual; abstract;',
  15194. ' end;',
  15195. 'procedure tobject.free;',
  15196. 'begin',
  15197. 'end;',
  15198. 'var o: tobject;',
  15199. 'begin',
  15200. ' o.getobj.free;',
  15201. '']);
  15202. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15203. ConvertProgram;
  15204. end;
  15205. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15206. begin
  15207. StartProgram(false);
  15208. Add([
  15209. 'type',
  15210. ' TObject = class',
  15211. ' procedure Free;',
  15212. ' FObj: TObject;',
  15213. ' property Obj: tobject read FObj write FObj;',
  15214. ' end;',
  15215. 'procedure tobject.free;',
  15216. 'begin',
  15217. 'end;',
  15218. 'var o: tobject;',
  15219. 'begin',
  15220. ' o.obj.free;',
  15221. '']);
  15222. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15223. ConvertProgram;
  15224. end;
  15225. procedure TTestModule.TestClass_ForIn;
  15226. begin
  15227. StartProgram(false);
  15228. Add([
  15229. 'type',
  15230. ' TObject = class end;',
  15231. ' TItem = TObject;',
  15232. ' TEnumerator = class',
  15233. ' FCurrent: TItem;',
  15234. ' property Current: TItem read FCurrent;',
  15235. ' function MoveNext: boolean;',
  15236. ' end;',
  15237. ' TBird = class',
  15238. ' function GetEnumerator: TEnumerator;',
  15239. ' end;',
  15240. 'function TEnumerator.MoveNext: boolean;',
  15241. 'begin',
  15242. 'end;',
  15243. 'function TBird.GetEnumerator: TEnumerator;',
  15244. 'begin',
  15245. 'end;',
  15246. 'var',
  15247. ' b: TBird;',
  15248. ' i, i2: TItem;',
  15249. 'begin',
  15250. ' for i in b do i2:=i;']);
  15251. ConvertProgram;
  15252. CheckSource('TestClass_ForIn',
  15253. LinesToStr([ // statements
  15254. 'rtl.createClass(this, "TObject", null, function () {',
  15255. ' this.$init = function () {',
  15256. ' };',
  15257. ' this.$final = function () {',
  15258. ' };',
  15259. '});',
  15260. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15261. ' this.$init = function () {',
  15262. ' $mod.TObject.$init.call(this);',
  15263. ' this.FCurrent = null;',
  15264. ' };',
  15265. ' this.$final = function () {',
  15266. ' this.FCurrent = undefined;',
  15267. ' $mod.TObject.$final.call(this);',
  15268. ' };',
  15269. ' this.MoveNext = function () {',
  15270. ' var Result = false;',
  15271. ' return Result;',
  15272. ' };',
  15273. '});',
  15274. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15275. ' this.GetEnumerator = function () {',
  15276. ' var Result = null;',
  15277. ' return Result;',
  15278. ' };',
  15279. '});',
  15280. 'this.b = null;',
  15281. 'this.i = null;',
  15282. 'this.i2 = null;'
  15283. ]),
  15284. LinesToStr([ // $mod.$main
  15285. 'var $in = $mod.b.GetEnumerator();',
  15286. 'try {',
  15287. ' while ($in.MoveNext()){',
  15288. ' $mod.i = $in.FCurrent;',
  15289. ' $mod.i2 = $mod.i;',
  15290. ' }',
  15291. '} finally {',
  15292. ' $in = rtl.freeLoc($in)',
  15293. '};',
  15294. '']));
  15295. end;
  15296. procedure TTestModule.TestClass_DispatchMessage;
  15297. begin
  15298. StartProgram(false);
  15299. Add([
  15300. 'type',
  15301. ' TObject = class',
  15302. ' {$DispatchField DispInt}',
  15303. ' procedure Dispatch(var Msg); virtual; abstract;',
  15304. ' {$DispatchStrField DispStr}',
  15305. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15306. ' end;',
  15307. ' THopMsg = record',
  15308. ' DispInt: longint;',
  15309. ' end;',
  15310. ' TPutMsg = record',
  15311. ' DispStr: string;',
  15312. ' end;',
  15313. ' TBird = class',
  15314. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15315. ' procedure Run; overload; virtual; abstract;',
  15316. ' procedure Run(var Msg); overload; message ''Fast'';',
  15317. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15318. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15319. ' end;',
  15320. 'procedure TBird.Run(var Msg);',
  15321. 'begin',
  15322. 'end;',
  15323. 'begin',
  15324. '']);
  15325. ConvertProgram;
  15326. CheckSource('TestClass_Message',
  15327. LinesToStr([ // statements
  15328. 'rtl.createClass(this, "TObject", null, function () {',
  15329. ' this.$init = function () {',
  15330. ' };',
  15331. ' this.$final = function () {',
  15332. ' };',
  15333. '});',
  15334. 'rtl.recNewT(this, "THopMsg", function () {',
  15335. ' this.DispInt = 0;',
  15336. ' this.$eq = function (b) {',
  15337. ' return this.DispInt === b.DispInt;',
  15338. ' };',
  15339. ' this.$assign = function (s) {',
  15340. ' this.DispInt = s.DispInt;',
  15341. ' return this;',
  15342. ' };',
  15343. '});',
  15344. 'rtl.recNewT(this, "TPutMsg", function () {',
  15345. ' this.DispStr = "";',
  15346. ' this.$eq = function (b) {',
  15347. ' return this.DispStr === b.DispStr;',
  15348. ' };',
  15349. ' this.$assign = function (s) {',
  15350. ' this.DispStr = s.DispStr;',
  15351. ' return this;',
  15352. ' };',
  15353. '});',
  15354. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15355. ' this.Run$1 = function (Msg) {',
  15356. ' };',
  15357. ' this.$msgint = {',
  15358. ' "2": "Fly",',
  15359. ' "3": "Hop"',
  15360. ' };',
  15361. ' this.$msgstr = {',
  15362. ' Fast: "Run$1",',
  15363. ' foo: "Put"',
  15364. ' };',
  15365. '});',
  15366. '']),
  15367. LinesToStr([ // $mod.$main
  15368. '']));
  15369. end;
  15370. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15371. begin
  15372. StartProgram(false);
  15373. Add([
  15374. 'type',
  15375. ' TObject = class',
  15376. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15377. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15378. ' end;',
  15379. 'begin',
  15380. '']);
  15381. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15382. ConvertProgram;
  15383. end;
  15384. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15385. begin
  15386. StartProgram(false);
  15387. Add([
  15388. 'type',
  15389. ' TObject = class',
  15390. ' {$dispatchfield Msg}',
  15391. ' procedure Dispatch(var Msg); virtual; abstract;',
  15392. ' end;',
  15393. ' TFlyMsg = record',
  15394. ' FlyId: longint;',
  15395. ' end;',
  15396. ' TBird = class',
  15397. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15398. ' end;',
  15399. 'begin',
  15400. '']);
  15401. ConvertProgram;
  15402. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15403. end;
  15404. procedure TTestModule.TestClassOf_Create;
  15405. begin
  15406. StartProgram(false);
  15407. Add('type');
  15408. Add(' TObject = class');
  15409. Add(' constructor Create;');
  15410. Add(' end;');
  15411. Add(' TClass = class of TObject;');
  15412. Add('constructor tobject.create; begin end;');
  15413. Add('var');
  15414. Add(' Obj: tobject;');
  15415. Add(' C: tclass;');
  15416. Add('begin');
  15417. Add(' obj:=C.create;');
  15418. Add(' with c do obj:=create;');
  15419. ConvertProgram;
  15420. CheckSource('TestClassOf_Create',
  15421. LinesToStr([ // statements
  15422. 'rtl.createClass(this, "TObject", null, function () {',
  15423. ' this.$init = function () {',
  15424. ' };',
  15425. ' this.$final = function () {',
  15426. ' };',
  15427. ' this.Create = function () {',
  15428. ' return this;',
  15429. ' };',
  15430. '});',
  15431. 'this.Obj = null;',
  15432. 'this.C = null;'
  15433. ]),
  15434. LinesToStr([ // $mod.$main
  15435. '$mod.Obj = $mod.C.$create("Create");',
  15436. 'var $with = $mod.C;',
  15437. '$mod.Obj = $with.$create("Create");',
  15438. '']));
  15439. end;
  15440. procedure TTestModule.TestClassOf_Call;
  15441. begin
  15442. StartProgram(false);
  15443. Add('type');
  15444. Add(' TObject = class');
  15445. Add(' class procedure DoIt;');
  15446. Add(' end;');
  15447. Add(' TClass = class of TObject;');
  15448. Add('class procedure tobject.doit; begin end;');
  15449. Add('var');
  15450. Add(' C: tclass;');
  15451. Add('begin');
  15452. Add(' c.doit;');
  15453. Add(' with c do doit;');
  15454. ConvertProgram;
  15455. CheckSource('TestClassOf_Call',
  15456. LinesToStr([ // statements
  15457. 'rtl.createClass(this, "TObject", null, function () {',
  15458. ' this.$init = function () {',
  15459. ' };',
  15460. ' this.$final = function () {',
  15461. ' };',
  15462. ' this.DoIt = function () {',
  15463. ' };',
  15464. '});',
  15465. 'this.C = null;'
  15466. ]),
  15467. LinesToStr([ // $mod.$main
  15468. '$mod.C.DoIt();',
  15469. 'var $with = $mod.C;',
  15470. '$with.DoIt();',
  15471. '']));
  15472. end;
  15473. procedure TTestModule.TestClassOf_Assign;
  15474. begin
  15475. StartProgram(false);
  15476. Add('type');
  15477. Add(' TClass = class of TObject;');
  15478. Add(' TObject = class');
  15479. Add(' ClassType: TClass; ');
  15480. Add(' end;');
  15481. Add('var');
  15482. Add(' Obj: tobject;');
  15483. Add(' C: tclass;');
  15484. Add('begin');
  15485. Add(' c:=nil;');
  15486. Add(' c:=obj.classtype;');
  15487. ConvertProgram;
  15488. CheckSource('TestClassOf_Assign',
  15489. LinesToStr([ // statements
  15490. 'rtl.createClass(this, "TObject", null, function () {',
  15491. ' this.$init = function () {',
  15492. ' this.ClassType = null;',
  15493. ' };',
  15494. ' this.$final = function () {',
  15495. ' this.ClassType = undefined;',
  15496. ' };',
  15497. '});',
  15498. 'this.Obj = null;',
  15499. 'this.C = null;'
  15500. ]),
  15501. LinesToStr([ // $mod.$main
  15502. '$mod.C = null;',
  15503. '$mod.C = $mod.Obj.ClassType;',
  15504. '']));
  15505. end;
  15506. procedure TTestModule.TestClassOf_Is;
  15507. begin
  15508. StartProgram(false);
  15509. Add('type');
  15510. Add(' TClass = class of TObject;');
  15511. Add(' TObject = class');
  15512. Add(' end;');
  15513. Add(' TCar = class');
  15514. Add(' end;');
  15515. Add(' TCars = class of TCar;');
  15516. Add('var');
  15517. Add(' Obj: tobject;');
  15518. Add(' C: tclass;');
  15519. Add(' Cars: tcars;');
  15520. Add('begin');
  15521. Add(' if c is tcar then ;');
  15522. Add(' if c is tcars then ;');
  15523. ConvertProgram;
  15524. CheckSource('TestClassOf_Is',
  15525. LinesToStr([ // statements
  15526. 'rtl.createClass(this, "TObject", null, function () {',
  15527. ' this.$init = function () {',
  15528. ' };',
  15529. ' this.$final = function () {',
  15530. ' };',
  15531. '});',
  15532. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15533. '});',
  15534. 'this.Obj = null;',
  15535. 'this.C = null;',
  15536. 'this.Cars = null;'
  15537. ]),
  15538. LinesToStr([ // $mod.$main
  15539. 'if(rtl.is($mod.C,$mod.TCar));',
  15540. 'if(rtl.is($mod.C,$mod.TCar));',
  15541. '']));
  15542. end;
  15543. procedure TTestModule.TestClassOf_Compare;
  15544. begin
  15545. StartProgram(false);
  15546. Add('type');
  15547. Add(' TClass = class of TObject;');
  15548. Add(' TObject = class');
  15549. Add(' ClassType: TClass; ');
  15550. Add(' end;');
  15551. Add('var');
  15552. Add(' b: boolean;');
  15553. Add(' Obj: tobject;');
  15554. Add(' C: tclass;');
  15555. Add('begin');
  15556. Add(' b:=c=nil;');
  15557. Add(' b:=nil=c;');
  15558. Add(' b:=c=obj.classtype;');
  15559. Add(' b:=obj.classtype=c;');
  15560. Add(' b:=c=TObject;');
  15561. Add(' b:=TObject=c;');
  15562. Add(' b:=c<>nil;');
  15563. Add(' b:=nil<>c;');
  15564. Add(' b:=c<>obj.classtype;');
  15565. Add(' b:=obj.classtype<>c;');
  15566. Add(' b:=c<>TObject;');
  15567. Add(' b:=TObject<>c;');
  15568. ConvertProgram;
  15569. CheckSource('TestClassOf_Compare',
  15570. LinesToStr([ // statements
  15571. 'rtl.createClass(this, "TObject", null, function () {',
  15572. ' this.$init = function () {',
  15573. ' this.ClassType = null;',
  15574. ' };',
  15575. ' this.$final = function () {',
  15576. ' this.ClassType = undefined;',
  15577. ' };',
  15578. '});',
  15579. 'this.b = false;',
  15580. 'this.Obj = null;',
  15581. 'this.C = null;'
  15582. ]),
  15583. LinesToStr([ // $mod.$main
  15584. '$mod.b = $mod.C === null;',
  15585. '$mod.b = null === $mod.C;',
  15586. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15587. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15588. '$mod.b = $mod.C === $mod.TObject;',
  15589. '$mod.b = $mod.TObject === $mod.C;',
  15590. '$mod.b = $mod.C !== null;',
  15591. '$mod.b = null !== $mod.C;',
  15592. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15593. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15594. '$mod.b = $mod.C !== $mod.TObject;',
  15595. '$mod.b = $mod.TObject !== $mod.C;',
  15596. '']));
  15597. end;
  15598. procedure TTestModule.TestClassOf_ClassVar;
  15599. begin
  15600. StartProgram(false);
  15601. Add('type');
  15602. Add(' TObject = class');
  15603. Add(' class var id: longint;');
  15604. Add(' end;');
  15605. Add(' TClass = class of TObject;');
  15606. Add('var');
  15607. Add(' C: tclass;');
  15608. Add('begin');
  15609. Add(' C.id:=C.id;');
  15610. ConvertProgram;
  15611. CheckSource('TestClassOf_ClassVar',
  15612. LinesToStr([ // statements
  15613. 'rtl.createClass(this, "TObject", null, function () {',
  15614. ' this.id = 0;',
  15615. ' this.$init = function () {',
  15616. ' };',
  15617. ' this.$final = function () {',
  15618. ' };',
  15619. '});',
  15620. 'this.C = null;'
  15621. ]),
  15622. LinesToStr([ // $mod.$main
  15623. '$mod.TObject.id = $mod.C.id;',
  15624. '']));
  15625. end;
  15626. procedure TTestModule.TestClassOf_ClassMethod;
  15627. begin
  15628. StartProgram(false);
  15629. Add('type');
  15630. Add(' TObject = class');
  15631. Add(' class function DoIt(i: longint = 0): longint;');
  15632. Add(' end;');
  15633. Add(' TClass = class of TObject;');
  15634. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15635. Add('var');
  15636. Add(' i: longint;');
  15637. Add(' C: tclass;');
  15638. Add('begin');
  15639. Add(' C.DoIt;');
  15640. Add(' C.DoIt();');
  15641. Add(' i:=C.DoIt;');
  15642. Add(' i:=C.DoIt();');
  15643. ConvertProgram;
  15644. CheckSource('TestClassOf_ClassMethod',
  15645. LinesToStr([ // statements
  15646. 'rtl.createClass(this, "TObject", null, function () {',
  15647. ' this.$init = function () {',
  15648. ' };',
  15649. ' this.$final = function () {',
  15650. ' };',
  15651. ' this.DoIt = function (i) {',
  15652. ' var Result = 0;',
  15653. ' return Result;',
  15654. ' };',
  15655. '});',
  15656. 'this.i = 0;',
  15657. 'this.C = null;'
  15658. ]),
  15659. LinesToStr([ // $mod.$main
  15660. '$mod.C.DoIt(0);',
  15661. '$mod.C.DoIt(0);',
  15662. '$mod.i = $mod.C.DoIt(0);',
  15663. '$mod.i = $mod.C.DoIt(0);',
  15664. '']));
  15665. end;
  15666. procedure TTestModule.TestClassOf_ClassProperty;
  15667. begin
  15668. StartProgram(false);
  15669. Add([
  15670. 'type',
  15671. ' TObject = class',
  15672. ' class var FA: longint;',
  15673. ' class function GetA: longint;',
  15674. ' class procedure SetA(Value: longint);',
  15675. ' class property pA: longint read fa write fa;',
  15676. ' class property pB: longint read geta write seta;',
  15677. ' end;',
  15678. ' TObjectClass = class of tobject;',
  15679. 'class function tobject.geta: longint; begin end;',
  15680. 'class procedure tobject.seta(value: longint); begin end;',
  15681. 'var',
  15682. ' b: boolean;',
  15683. ' Obj: tobject;',
  15684. ' Cla: tobjectclass;',
  15685. 'begin',
  15686. ' obj.pa:=obj.pa;',
  15687. ' obj.pb:=obj.pb;',
  15688. ' b:=obj.pa=4;',
  15689. ' b:=obj.pb=obj.pb;',
  15690. ' b:=5=obj.pa;',
  15691. ' cla.pa:=6;',
  15692. ' cla.pa:=cla.pa;',
  15693. ' cla.pb:=cla.pb;',
  15694. ' b:=cla.pa=7;',
  15695. ' b:=cla.pb=cla.pb;',
  15696. ' b:=8=cla.pa;',
  15697. ' tobject.pa:=9;',
  15698. ' tobject.pb:=tobject.pb;',
  15699. ' b:=tobject.pa=10;',
  15700. ' b:=11=tobject.pa;',
  15701. '']);
  15702. ConvertProgram;
  15703. CheckSource('TestClassOf_ClassProperty',
  15704. LinesToStr([ // statements
  15705. 'rtl.createClass(this, "TObject", null, function () {',
  15706. ' this.FA = 0;',
  15707. ' this.$init = function () {',
  15708. ' };',
  15709. ' this.$final = function () {',
  15710. ' };',
  15711. ' this.GetA = function () {',
  15712. ' var Result = 0;',
  15713. ' return Result;',
  15714. ' };',
  15715. ' this.SetA = function (Value) {',
  15716. ' };',
  15717. '});',
  15718. 'this.b = false;',
  15719. 'this.Obj = null;',
  15720. 'this.Cla = null;'
  15721. ]),
  15722. LinesToStr([ // $mod.$main
  15723. '$mod.TObject.FA = $mod.Obj.FA;',
  15724. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15725. '$mod.b = $mod.Obj.FA === 4;',
  15726. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15727. '$mod.b = 5 === $mod.Obj.FA;',
  15728. '$mod.TObject.FA = 6;',
  15729. '$mod.TObject.FA = $mod.Cla.FA;',
  15730. '$mod.Cla.SetA($mod.Cla.GetA());',
  15731. '$mod.b = $mod.Cla.FA === 7;',
  15732. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15733. '$mod.b = 8 === $mod.Cla.FA;',
  15734. '$mod.TObject.FA = 9;',
  15735. '$mod.TObject.SetA($mod.TObject.GetA());',
  15736. '$mod.b = $mod.TObject.FA === 10;',
  15737. '$mod.b = 11 === $mod.TObject.FA;',
  15738. '']));
  15739. end;
  15740. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15741. begin
  15742. StartProgram(false);
  15743. Add('type');
  15744. Add(' TObject = class');
  15745. Add(' class var GlobalId: longint;');
  15746. Add(' class procedure ProcA;');
  15747. Add(' end;');
  15748. Add('class procedure tobject.proca;');
  15749. Add('var b: boolean;');
  15750. Add('begin');
  15751. Add(' b:=self=nil;');
  15752. Add(' b:=self.globalid=3;');
  15753. Add(' b:=4=self.globalid;');
  15754. Add(' self.globalid:=5;');
  15755. Add(' self.proca;');
  15756. Add('end;');
  15757. Add('begin');
  15758. ConvertProgram;
  15759. CheckSource('TestClassOf_ClassMethodSelf',
  15760. LinesToStr([ // statements
  15761. 'rtl.createClass(this, "TObject", null, function () {',
  15762. ' this.GlobalId = 0;',
  15763. ' this.$init = function () {',
  15764. ' };',
  15765. ' this.$final = function () {',
  15766. ' };',
  15767. ' this.ProcA = function () {',
  15768. ' var b = false;',
  15769. ' b = this === null;',
  15770. ' b = this.GlobalId === 3;',
  15771. ' b = 4 === this.GlobalId;',
  15772. ' $mod.TObject.GlobalId = 5;',
  15773. ' this.ProcA();',
  15774. ' };',
  15775. '});'
  15776. ]),
  15777. LinesToStr([ // $mod.$main
  15778. '']));
  15779. end;
  15780. procedure TTestModule.TestClassOf_TypeCast;
  15781. begin
  15782. StartProgram(false);
  15783. Add('type');
  15784. Add(' TObject = class');
  15785. Add(' class procedure {#TObject_DoIt}DoIt;');
  15786. Add(' end;');
  15787. Add(' TClass = class of TObject;');
  15788. Add(' TMobile = class');
  15789. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15790. Add(' end;');
  15791. Add(' TMobileClass = class of TMobile;');
  15792. Add(' TCar = class(TMobile)');
  15793. Add(' class procedure {#TCar_DoIt}DoIt;');
  15794. Add(' end;');
  15795. Add(' TCarClass = class of TCar;');
  15796. Add('class procedure TObject.DoIt;');
  15797. Add('begin');
  15798. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15799. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15800. Add('end;');
  15801. Add('class procedure TMobile.DoIt;');
  15802. Add('begin');
  15803. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15804. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15805. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15806. Add('end;');
  15807. Add('class procedure TCar.DoIt; begin end;');
  15808. Add('var');
  15809. Add(' ObjC: TClass;');
  15810. Add(' MobileC: TMobileClass;');
  15811. Add(' CarC: TCarClass;');
  15812. Add('begin');
  15813. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15814. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15815. Add(' CarC.{@TCar_DoIt}DoIt;');
  15816. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15817. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15818. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15819. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15820. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15821. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15822. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15823. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15824. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15825. ConvertProgram;
  15826. CheckSource('TestClassOf_TypeCast',
  15827. LinesToStr([ // statements
  15828. 'rtl.createClass(this, "TObject", null, function () {',
  15829. ' this.$init = function () {',
  15830. ' };',
  15831. ' this.$final = function () {',
  15832. ' };',
  15833. ' this.DoIt = function () {',
  15834. ' this.DoIt();',
  15835. ' this.DoIt$1();',
  15836. ' };',
  15837. '});',
  15838. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15839. ' this.DoIt$1 = function () {',
  15840. ' this.DoIt();',
  15841. ' this.DoIt$1();',
  15842. ' this.DoIt$2();',
  15843. ' };',
  15844. '});',
  15845. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15846. ' this.DoIt$2 = function () {',
  15847. ' };',
  15848. '});',
  15849. 'this.ObjC = null;',
  15850. 'this.MobileC = null;',
  15851. 'this.CarC = null;',
  15852. '']),
  15853. LinesToStr([ // $mod.$main
  15854. '$mod.ObjC.DoIt();',
  15855. '$mod.MobileC.DoIt$1();',
  15856. '$mod.CarC.DoIt$2();',
  15857. '$mod.ObjC.DoIt();',
  15858. '$mod.ObjC.DoIt$1();',
  15859. '$mod.ObjC.DoIt$2();',
  15860. '$mod.MobileC.DoIt();',
  15861. '$mod.MobileC.DoIt$1();',
  15862. '$mod.MobileC.DoIt$2();',
  15863. '$mod.CarC.DoIt();',
  15864. '$mod.CarC.DoIt$1();',
  15865. '$mod.CarC.DoIt$2();',
  15866. '']));
  15867. end;
  15868. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15869. begin
  15870. StartProgram(false);
  15871. Add('type');
  15872. Add(' TObject = class');
  15873. Add(' function CurNow: longint; ');
  15874. Add(' class function Now: longint; ');
  15875. Add(' end;');
  15876. Add('function TObject.CurNow: longint; begin end;');
  15877. Add('class function TObject.Now: longint; begin end;');
  15878. Add('var');
  15879. Add(' Obj: tobject;');
  15880. Add(' vI: longint;');
  15881. Add('begin');
  15882. Add(' obj.curnow;');
  15883. Add(' vi:=obj.curnow;');
  15884. Add(' tobject.now;');
  15885. Add(' vi:=tobject.now;');
  15886. ConvertProgram;
  15887. CheckSource('TestClassOf_ImplicitFunctionCall',
  15888. LinesToStr([ // statements
  15889. 'rtl.createClass(this, "TObject", null, function () {',
  15890. ' this.$init = function () {',
  15891. ' };',
  15892. ' this.$final = function () {',
  15893. ' };',
  15894. ' this.CurNow = function () {',
  15895. ' var Result = 0;',
  15896. ' return Result;',
  15897. ' };',
  15898. ' this.Now = function () {',
  15899. ' var Result = 0;',
  15900. ' return Result;',
  15901. ' };',
  15902. '});',
  15903. 'this.Obj = null;',
  15904. 'this.vI = 0;',
  15905. '']),
  15906. LinesToStr([ // $mod.$main
  15907. '$mod.Obj.CurNow();',
  15908. '$mod.vI = $mod.Obj.CurNow();',
  15909. '$mod.TObject.Now();',
  15910. '$mod.vI = $mod.TObject.Now();',
  15911. '']));
  15912. end;
  15913. procedure TTestModule.TestClassOf_Const;
  15914. begin
  15915. StartProgram(false);
  15916. Add([
  15917. 'type',
  15918. ' TObject = class',
  15919. ' end;',
  15920. ' TBird = TObject;',
  15921. ' TBirds = class of TBird;',
  15922. ' TEagles = TBirds;',
  15923. ' THawk = class(TBird);',
  15924. 'const',
  15925. ' Hawk: TEagles = THawk;',
  15926. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15927. ' TBird,',
  15928. ' THawk',
  15929. ' );',
  15930. 'begin']);
  15931. ConvertProgram;
  15932. CheckSource('TestClassOf_Const',
  15933. LinesToStr([ // statements
  15934. 'rtl.createClass(this, "TObject", null, function () {',
  15935. ' this.$init = function () {',
  15936. ' };',
  15937. ' this.$final = function () {',
  15938. ' };',
  15939. '});',
  15940. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  15941. '});',
  15942. 'this.Hawk = this.THawk;',
  15943. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  15944. '']),
  15945. LinesToStr([ // $mod.$main
  15946. '']));
  15947. end;
  15948. procedure TTestModule.TestNestedClass_Alias;
  15949. begin
  15950. WithTypeInfo:=true;
  15951. StartProgram(false);
  15952. Add([
  15953. 'type',
  15954. ' TObject = class',
  15955. ' type TNested = type longint;',
  15956. ' end;',
  15957. 'type TAlias = type tobject.tnested;',
  15958. 'var i: tobject.tnested = 3;',
  15959. 'var j: TAlias = 4;',
  15960. 'begin',
  15961. ' if typeinfo(TAlias)=nil then ;',
  15962. ' if typeinfo(tobject.tnested)=nil then ;',
  15963. '']);
  15964. ConvertProgram;
  15965. CheckSource('TestNestedClass_Alias',
  15966. LinesToStr([ // statements
  15967. 'rtl.createClass(this, "TObject", null, function () {',
  15968. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15969. ' this.$init = function () {',
  15970. ' };',
  15971. ' this.$final = function () {',
  15972. ' };',
  15973. '});',
  15974. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  15975. 'this.i = 3;',
  15976. 'this.j = 4;',
  15977. '']),
  15978. LinesToStr([ // $mod.$main
  15979. 'if ($mod.$rtti["TAlias"] === null) ;',
  15980. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15981. '']));
  15982. end;
  15983. procedure TTestModule.TestNestedClass_Record;
  15984. begin
  15985. WithTypeInfo:=true;
  15986. StartProgram(false);
  15987. Add([
  15988. 'type',
  15989. ' TObject = class',
  15990. ' type TPoint = record',
  15991. ' x,y: byte;',
  15992. ' end;',
  15993. ' procedure DoIt(t: TPoint);',
  15994. ' end;',
  15995. 'procedure tobject.DoIt(t: TPoint);',
  15996. 'var p: TPoint;',
  15997. 'begin',
  15998. ' t.x:=t.y;',
  15999. ' p:=t;',
  16000. 'end;',
  16001. 'var',
  16002. ' p: tobject.tpoint = (x:2; y:4);',
  16003. ' o: TObject;',
  16004. 'begin',
  16005. ' p:=p;',
  16006. ' o.doit(p);',
  16007. '']);
  16008. ConvertProgram;
  16009. CheckSource('TestNestedClass_Record',
  16010. LinesToStr([ // statements
  16011. 'rtl.createClass(this, "TObject", null, function () {',
  16012. ' rtl.recNewT(this, "TPoint", function () {',
  16013. ' this.x = 0;',
  16014. ' this.y = 0;',
  16015. ' this.$eq = function (b) {',
  16016. ' return (this.x === b.x) && (this.y === b.y);',
  16017. ' };',
  16018. ' this.$assign = function (s) {',
  16019. ' this.x = s.x;',
  16020. ' this.y = s.y;',
  16021. ' return this;',
  16022. ' };',
  16023. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16024. ' $r.addField("x", rtl.byte);',
  16025. ' $r.addField("y", rtl.byte);',
  16026. ' });',
  16027. ' this.$init = function () {',
  16028. ' };',
  16029. ' this.$final = function () {',
  16030. ' };',
  16031. ' this.DoIt = function (t) {',
  16032. ' var p = this.TPoint.$new();',
  16033. ' t.x = t.y;',
  16034. ' p.$assign(t);',
  16035. ' };',
  16036. '});',
  16037. 'this.p = this.TObject.TPoint.$clone({',
  16038. ' x: 2,',
  16039. ' y: 4',
  16040. '});',
  16041. 'this.o = null;',
  16042. '']),
  16043. LinesToStr([ // $mod.$main
  16044. '$mod.p.$assign($mod.p);',
  16045. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16046. '']));
  16047. end;
  16048. procedure TTestModule.TestNestedClass_Class;
  16049. begin
  16050. WithTypeInfo:=true;
  16051. StartProgram(false);
  16052. Add([
  16053. 'type',
  16054. ' TObject = class end;',
  16055. ' TBird = class',
  16056. ' type TLeg = class',
  16057. ' FId: longint;',
  16058. ' constructor Create;',
  16059. ' function Create(i: longint): TLeg;',
  16060. ' end;',
  16061. ' function DoIt(b: TBird): Tleg;',
  16062. ' end;',
  16063. 'constructor tbird.tleg.create;',
  16064. 'begin',
  16065. ' FId:=3;',
  16066. 'end;',
  16067. 'function tbird.tleg.Create(i: longint): TLeg;',
  16068. 'begin',
  16069. ' Create;',
  16070. ' Result:=TLeg.Create;',
  16071. ' Result:=TBird.TLeg.Create;',
  16072. ' Result:=Create(3);',
  16073. ' FId:=i;',
  16074. 'end;',
  16075. 'function tbird.DoIt(b: tbird): tleg;',
  16076. 'begin',
  16077. ' Result.Create;',
  16078. ' Result:=TLeg.Create;',
  16079. ' Result:=TBird.TLeg.Create;',
  16080. ' Result:=Result.Create(3);',
  16081. 'end;',
  16082. 'var',
  16083. ' b: Tbird.tleg;',
  16084. 'begin',
  16085. ' b.Create;',
  16086. ' b:=TBird.TLeg.Create;',
  16087. ' b:=b.Create(3);',
  16088. '']);
  16089. ConvertProgram;
  16090. CheckSource('TestNestedClass_Class',
  16091. LinesToStr([ // statements
  16092. 'rtl.createClass(this, "TObject", null, function () {',
  16093. ' this.$init = function () {',
  16094. ' };',
  16095. ' this.$final = function () {',
  16096. ' };',
  16097. '});',
  16098. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16099. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16100. ' this.$init = function () {',
  16101. ' $mod.TObject.$init.call(this);',
  16102. ' this.FId = 0;',
  16103. ' };',
  16104. ' this.Create = function () {',
  16105. ' this.FId = 3;',
  16106. ' return this;',
  16107. ' };',
  16108. ' this.Create$1 = function (i) {',
  16109. ' var Result = null;',
  16110. ' this.Create();',
  16111. ' Result = $mod.TBird.TLeg.$create("Create");',
  16112. ' Result = $mod.TBird.TLeg.$create("Create");',
  16113. ' Result = this.Create$1(3);',
  16114. ' this.FId = i;',
  16115. ' return Result;',
  16116. ' };',
  16117. ' }, "TBird.TLeg");',
  16118. ' this.DoIt = function (b) {',
  16119. ' var Result = null;',
  16120. ' Result.Create();',
  16121. ' Result = this.TLeg.$create("Create");',
  16122. ' Result = $mod.TBird.TLeg.$create("Create");',
  16123. ' Result = Result.Create$1(3);',
  16124. ' return Result;',
  16125. ' };',
  16126. '});',
  16127. 'this.b = null;',
  16128. '']),
  16129. LinesToStr([ // $mod.$main
  16130. '$mod.b.Create();',
  16131. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16132. '$mod.b = $mod.b.Create$1(3);',
  16133. '']));
  16134. end;
  16135. procedure TTestModule.TestExternalClass_Var;
  16136. begin
  16137. StartProgram(false);
  16138. Add([
  16139. '{$modeswitch externalclass}',
  16140. 'type',
  16141. ' TExtA = class external name ''ExtObj''',
  16142. ' Id: longint external name ''$Id'';',
  16143. ' B: longint;',
  16144. ' end;',
  16145. 'var Obj: TExtA;',
  16146. 'begin',
  16147. ' obj.id:=obj.id+1;',
  16148. ' obj.B:=obj.B+1;']);
  16149. ConvertProgram;
  16150. CheckSource('TestExternalClass_Var',
  16151. LinesToStr([ // statements
  16152. 'this.Obj = null;',
  16153. '']),
  16154. LinesToStr([ // $mod.$main
  16155. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16156. '$mod.Obj.B = $mod.Obj.B + 1;',
  16157. '']));
  16158. end;
  16159. procedure TTestModule.TestExternalClass_Const;
  16160. begin
  16161. StartProgram(false);
  16162. Add([
  16163. '{$modeswitch externalclass}',
  16164. 'type',
  16165. ' TExtA = class external name ''ExtObj''',
  16166. ' const Two: longint = 2;',
  16167. ' const Three = 3;',
  16168. ' const Id: longint;',
  16169. ' end;',
  16170. ' TExtB = class external name ''ExtB''',
  16171. ' A: TExtA;',
  16172. ' end;',
  16173. 'var',
  16174. ' A: texta;',
  16175. ' B: textb;',
  16176. ' i: longint;',
  16177. 'begin',
  16178. ' i:=a.two;',
  16179. ' i:=texta.two;',
  16180. ' i:=a.three;',
  16181. ' i:=texta.three;',
  16182. ' i:=a.id;',
  16183. ' i:=texta.id;',
  16184. '']);
  16185. ConvertProgram;
  16186. CheckSource('TestExternalClass_Const',
  16187. LinesToStr([ // statements
  16188. 'this.A = null;',
  16189. 'this.B = null;',
  16190. 'this.i = 0;',
  16191. '']),
  16192. LinesToStr([ // $mod.$main
  16193. '$mod.i = 2;',
  16194. '$mod.i = 2;',
  16195. '$mod.i = 3;',
  16196. '$mod.i = 3;',
  16197. '$mod.i = $mod.A.Id;',
  16198. '$mod.i = ExtObj.Id;',
  16199. '']));
  16200. end;
  16201. procedure TTestModule.TestExternalClass_Dollar;
  16202. begin
  16203. StartProgram(false);
  16204. Add([
  16205. '{$modeswitch externalclass}',
  16206. 'type',
  16207. ' TExtA = class external name ''$''',
  16208. ' Id: longint external name ''$'';',
  16209. ' function Bla(i: longint): longint; external name ''$'';',
  16210. ' end;',
  16211. 'function dollar(k: longint): longint; external name ''$'';',
  16212. 'var Obj: TExtA;',
  16213. 'begin',
  16214. ' dollar(1);',
  16215. ' obj.id:=obj.id+2;',
  16216. ' obj.Bla(3);',
  16217. '']);
  16218. ConvertProgram;
  16219. CheckSource('TestExternalClass_Dollar',
  16220. LinesToStr([ // statements
  16221. 'this.Obj = null;',
  16222. '']),
  16223. LinesToStr([ // $mod.$main
  16224. '$(1);',
  16225. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16226. '$mod.Obj.$(3);',
  16227. '']));
  16228. end;
  16229. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16230. begin
  16231. StartProgram(false);
  16232. Add('{$modeswitch externalclass}');
  16233. Add('type');
  16234. Add(' TExtA = class external name ''ExtA''');
  16235. Add(' Id: longint external name ''$Id'';');
  16236. Add(' end;');
  16237. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16238. Add(' Id: longint;');
  16239. Add(' end;');
  16240. Add('begin');
  16241. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16242. ConvertProgram;
  16243. end;
  16244. procedure TTestModule.TestExternalClass_Method;
  16245. begin
  16246. StartProgram(false);
  16247. Add(['{$modeswitch externalclass}',
  16248. 'type',
  16249. ' TExtA = class external name ''ExtObj''',
  16250. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16251. ' procedure DoSome(Id: longint = 1);',
  16252. ' end;',
  16253. 'var Obj: texta;',
  16254. 'begin',
  16255. ' obj.doit;',
  16256. ' obj.doit();',
  16257. ' obj.doit(2);',
  16258. ' with obj do begin',
  16259. ' doit;',
  16260. ' doit();',
  16261. ' doit(3);',
  16262. ' end;']);
  16263. ConvertProgram;
  16264. CheckSource('TestExternalClass_Method',
  16265. LinesToStr([ // statements
  16266. 'this.Obj = null;',
  16267. '']),
  16268. LinesToStr([ // $mod.$main
  16269. '$mod.Obj.$Execute(1);',
  16270. '$mod.Obj.$Execute(1);',
  16271. '$mod.Obj.$Execute(2);',
  16272. 'var $with = $mod.Obj;',
  16273. '$with.$Execute(1);',
  16274. '$with.$Execute(1);',
  16275. '$with.$Execute(3);',
  16276. '']));
  16277. end;
  16278. procedure TTestModule.TestExternalClass_ClassMethod;
  16279. begin
  16280. StartProgram(false);
  16281. Add([
  16282. '{$modeswitch externalclass}',
  16283. 'type',
  16284. ' TExtA = class external name ''ExtObj''',
  16285. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16286. ' end;',
  16287. ' TExtB = TExtA;',
  16288. 'var p: Pointer;',
  16289. 'begin',
  16290. ' texta.doit;',
  16291. ' texta.doit();',
  16292. ' texta.doit(2);',
  16293. ' p:[email protected];',
  16294. ' with texta do begin',
  16295. ' doit;',
  16296. ' doit();',
  16297. ' doit(3);',
  16298. ' p:=@DoIt;',
  16299. ' end;',
  16300. ' textb.doit;',
  16301. ' textb.doit();',
  16302. ' textb.doit(4);',
  16303. ' with textb do begin',
  16304. ' doit;',
  16305. ' doit();',
  16306. ' doit(5);',
  16307. ' end;',
  16308. '']);
  16309. ConvertProgram;
  16310. CheckSource('TestExternalClass_ClassMethod',
  16311. LinesToStr([ // statements
  16312. 'this.p = null;',
  16313. '']),
  16314. LinesToStr([ // $mod.$main
  16315. 'ExtObj.$Execute(1);',
  16316. 'ExtObj.$Execute(1);',
  16317. 'ExtObj.$Execute(2);',
  16318. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16319. 'ExtObj.$Execute(1);',
  16320. 'ExtObj.$Execute(1);',
  16321. 'ExtObj.$Execute(3);',
  16322. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16323. 'ExtObj.$Execute(1);',
  16324. 'ExtObj.$Execute(1);',
  16325. 'ExtObj.$Execute(4);',
  16326. 'ExtObj.$Execute(1);',
  16327. 'ExtObj.$Execute(1);',
  16328. 'ExtObj.$Execute(5);',
  16329. '']));
  16330. end;
  16331. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16332. begin
  16333. StartProgram(false);
  16334. Add([
  16335. '{$modeswitch externalclass}',
  16336. 'type',
  16337. ' TExtA = class external name ''ExtObj''',
  16338. ' class procedure DoIt(Id: longint = 1); static;',
  16339. ' end;',
  16340. 'var p: Pointer;',
  16341. 'begin',
  16342. ' texta.doit;',
  16343. ' texta.doit();',
  16344. ' texta.doit(2);',
  16345. ' p:[email protected];',
  16346. ' with texta do begin',
  16347. ' doit;',
  16348. ' doit();',
  16349. ' doit(3);',
  16350. ' p:=@DoIt;',
  16351. ' end;',
  16352. '']);
  16353. ConvertProgram;
  16354. CheckSource('TestExternalClass_ClassMethodStatic',
  16355. LinesToStr([ // statements
  16356. 'this.p = null;',
  16357. '']),
  16358. LinesToStr([ // $mod.$main
  16359. 'ExtObj.DoIt(1);',
  16360. 'ExtObj.DoIt(1);',
  16361. 'ExtObj.DoIt(2);',
  16362. '$mod.p = ExtObj.DoIt;',
  16363. 'ExtObj.DoIt(1);',
  16364. 'ExtObj.DoIt(1);',
  16365. 'ExtObj.DoIt(3);',
  16366. '$mod.p = ExtObj.DoIt;',
  16367. '']));
  16368. end;
  16369. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16370. begin
  16371. StartProgram(false);
  16372. Add([
  16373. '{$modeswitch externalclass}',
  16374. 'type',
  16375. ' TBird = class external name ''Array''',
  16376. ' end;',
  16377. 'function GetPtr: Pointer;',
  16378. 'begin',
  16379. 'end;',
  16380. 'procedure Write(const p);',
  16381. 'begin',
  16382. 'end;',
  16383. 'procedure WriteLn; varargs;',
  16384. 'begin',
  16385. 'end;',
  16386. 'begin',
  16387. ' if TBird(GetPtr)=nil then ;',
  16388. ' Write(GetPtr);',
  16389. ' WriteLn(GetPtr);',
  16390. ' Write(TBird(GetPtr));',
  16391. ' WriteLn(TBird(GetPtr));',
  16392. '']);
  16393. ConvertProgram;
  16394. CheckSource('TestFunctionResultInTypeCast',
  16395. LinesToStr([ // statements
  16396. 'this.GetPtr = function () {',
  16397. ' var Result = null;',
  16398. ' return Result;',
  16399. '};',
  16400. 'this.Write = function (p) {',
  16401. '};',
  16402. 'this.WriteLn = function () {',
  16403. '};',
  16404. '']),
  16405. LinesToStr([
  16406. 'if ($mod.GetPtr() === null) ;',
  16407. '$mod.Write($mod.GetPtr());',
  16408. '$mod.WriteLn($mod.GetPtr());',
  16409. '$mod.Write($mod.GetPtr());',
  16410. '$mod.WriteLn($mod.GetPtr());',
  16411. '']));
  16412. end;
  16413. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16414. begin
  16415. StartProgram(false);
  16416. Add([
  16417. '{$modeswitch externalclass}',
  16418. 'type',
  16419. ' TExtA = class external name ''ExtObjA''',
  16420. ' procedure ProcA; virtual;',
  16421. ' procedure ProcB; virtual;',
  16422. ' end;',
  16423. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16424. ' end;',
  16425. ' TExtC = class (TExtB)',
  16426. ' procedure ProcA; override;',
  16427. ' end;',
  16428. 'procedure TExtC.ProcA;',
  16429. 'begin',
  16430. ' ProcA;',
  16431. ' Self.ProcA;',
  16432. ' ProcB;',
  16433. ' Self.ProcB;',
  16434. 'end;',
  16435. 'var',
  16436. ' A: texta;',
  16437. ' B: textb;',
  16438. ' C: textc;',
  16439. 'begin',
  16440. ' a.proca;',
  16441. ' b.proca;',
  16442. ' c.proca;']);
  16443. ConvertProgram;
  16444. CheckSource('TestExternalClass_NonExternalOverride',
  16445. LinesToStr([ // statements
  16446. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16447. ' this.$init = function () {',
  16448. ' };',
  16449. ' this.$final = function () {',
  16450. ' };',
  16451. ' this.ProcA = function () {',
  16452. ' this.ProcA();',
  16453. ' this.ProcA();',
  16454. ' this.ProcB();',
  16455. ' this.ProcB();',
  16456. ' };',
  16457. '});',
  16458. 'this.A = null;',
  16459. 'this.B = null;',
  16460. 'this.C = null;',
  16461. '']),
  16462. LinesToStr([ // $mod.$main
  16463. '$mod.A.ProcA();',
  16464. '$mod.B.ProcA();',
  16465. '$mod.C.ProcA();',
  16466. '']));
  16467. end;
  16468. procedure TTestModule.TestExternalClass_OverloadHint;
  16469. begin
  16470. StartProgram(false);
  16471. Add([
  16472. '{$modeswitch externalclass}',
  16473. 'type',
  16474. ' TExtA = class external name ''ExtObjA''',
  16475. ' procedure DoIt;',
  16476. ' procedure DoIt(i: longint);',
  16477. ' end;',
  16478. 'begin',
  16479. '']);
  16480. ConvertProgram;
  16481. CheckResolverUnexpectedHints(true);
  16482. CheckSource('TestExternalClass_OverloadHint',
  16483. LinesToStr([ // statements
  16484. '']),
  16485. LinesToStr([ // $mod.$main
  16486. '']));
  16487. end;
  16488. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16489. begin
  16490. StartProgram(false);
  16491. Add([
  16492. '{$modeswitch externalclass}',
  16493. 'type',
  16494. ' JSwiper = class external name ''Swiper''',
  16495. ' constructor New;',
  16496. ' end;',
  16497. ' TObject = class',
  16498. ' private',
  16499. ' FSwiper: JSwiper;',
  16500. ' published',
  16501. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16502. ' end;',
  16503. 'begin',
  16504. ' JSwiper.new;',
  16505. '']);
  16506. ConvertProgram;
  16507. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16508. LinesToStr([ // statements
  16509. 'rtl.createClass(this, "TObject", null, function () {',
  16510. ' this.$init = function () {',
  16511. ' this.FSwiper = null;',
  16512. ' };',
  16513. ' this.$final = function () {',
  16514. ' this.FSwiper = undefined;',
  16515. ' };',
  16516. ' var $r = this.$rtti;',
  16517. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16518. '});',
  16519. '']),
  16520. LinesToStr([ // $mod.$main
  16521. 'new Swiper();',
  16522. '']));
  16523. end;
  16524. procedure TTestModule.TestExternalClass_Property;
  16525. begin
  16526. StartProgram(false);
  16527. Add([
  16528. '{$modeswitch externalclass}',
  16529. 'type',
  16530. ' TExtA = class external name ''ExtA''',
  16531. ' function getYear: longint;',
  16532. ' procedure setYear(Value: longint);',
  16533. ' property Year: longint read getyear write setyear;',
  16534. ' end;',
  16535. ' TExtB = class (TExtA)',
  16536. ' procedure OtherSetYear(Value: longint);',
  16537. ' property year write othersetyear;',
  16538. ' end;',
  16539. 'procedure textb.othersetyear(value: longint);',
  16540. 'begin',
  16541. ' setYear(Value+4);',
  16542. 'end;',
  16543. 'var',
  16544. ' A: texta;',
  16545. ' B: textb;',
  16546. 'begin',
  16547. ' a.year:=a.year+1;',
  16548. ' b.year:=b.year+2;']);
  16549. ConvertProgram;
  16550. CheckSource('TestExternalClass_NonExternalOverride',
  16551. LinesToStr([ // statements
  16552. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16553. ' this.$init = function () {',
  16554. ' };',
  16555. ' this.$final = function () {',
  16556. ' };',
  16557. ' this.OtherSetYear = function (Value) {',
  16558. ' this.setYear(Value+4);',
  16559. ' };',
  16560. '});',
  16561. 'this.A = null;',
  16562. 'this.B = null;',
  16563. '']),
  16564. LinesToStr([ // $mod.$main
  16565. '$mod.A.setYear($mod.A.getYear()+1);',
  16566. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16567. '']));
  16568. end;
  16569. procedure TTestModule.TestExternalClass_PropertyDate;
  16570. begin
  16571. StartProgram(false);
  16572. Add([
  16573. '{$modeswitch externalclass}',
  16574. 'type',
  16575. ' TExtA = class external name ''ExtA''',
  16576. ' end;',
  16577. ' TExtB = class (TExtA)',
  16578. ' FDate: string;',
  16579. ' property Date: string read FDate write FDate;',
  16580. ' property ExtA: string read FDate write FDate;',
  16581. ' end;',
  16582. ' {$M+}',
  16583. ' TObject = class',
  16584. ' FDate: string;',
  16585. ' published',
  16586. ' property Date: string read FDate write FDate;',
  16587. ' property ExtA: string read FDate write FDate;',
  16588. ' end;',
  16589. 'var',
  16590. ' B: textb;',
  16591. ' o: TObject;',
  16592. 'begin',
  16593. ' b.date:=b.exta;',
  16594. ' o.date:=o.exta;']);
  16595. ConvertProgram;
  16596. CheckSource('TestExternalClass_PropertyDate',
  16597. LinesToStr([ // statements
  16598. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16599. ' this.$init = function () {',
  16600. ' this.FDate = "";',
  16601. ' };',
  16602. ' this.$final = function () {',
  16603. ' };',
  16604. '});',
  16605. 'rtl.createClass(this, "TObject", null, function () {',
  16606. ' this.$init = function () {',
  16607. ' this.FDate = "";',
  16608. ' };',
  16609. ' this.$final = function () {',
  16610. ' };',
  16611. ' var $r = this.$rtti;',
  16612. ' $r.addField("FDate", rtl.string);',
  16613. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16614. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16615. '});',
  16616. 'this.B = null;',
  16617. 'this.o = null;',
  16618. '']),
  16619. LinesToStr([ // $mod.$main
  16620. '$mod.B.FDate = $mod.B.FDate;',
  16621. '$mod.o.FDate = $mod.o.FDate;',
  16622. '']));
  16623. end;
  16624. procedure TTestModule.TestExternalClass_ClassProperty;
  16625. begin
  16626. StartProgram(false);
  16627. Add('{$modeswitch externalclass}');
  16628. Add('type');
  16629. Add(' TExtA = class external name ''ExtA''');
  16630. Add(' class function getYear: longint;');
  16631. Add(' class procedure setYear(Value: longint);');
  16632. Add(' class property Year: longint read getyear write setyear;');
  16633. Add(' end;');
  16634. Add(' TExtB = class (TExtA)');
  16635. Add(' class function GetCentury: longint;');
  16636. Add(' class procedure SetCentury(Value: longint);');
  16637. Add(' class property Century: longint read getcentury write setcentury;');
  16638. Add(' end;');
  16639. Add('class function textb.getcentury: longint;');
  16640. Add('begin');
  16641. Add('end;');
  16642. Add('class procedure textb.setcentury(value: longint);');
  16643. Add('begin');
  16644. Add(' setyear(value+11);');
  16645. Add(' texta.year:=texta.year+12;');
  16646. Add(' year:=year+13;');
  16647. Add(' textb.century:=textb.century+14;');
  16648. Add(' century:=century+15;');
  16649. Add('end;');
  16650. Add('var');
  16651. Add(' A: texta;');
  16652. Add(' B: textb;');
  16653. Add('begin');
  16654. Add(' texta.year:=texta.year+1;');
  16655. Add(' textb.year:=textb.year+2;');
  16656. Add(' TextA.year:=TextA.year+3;');
  16657. Add(' b.year:=b.year+4;');
  16658. Add(' textb.century:=textb.century+5;');
  16659. Add(' b.century:=b.century+6;');
  16660. ConvertProgram;
  16661. CheckSource('TestExternalClass_ClassProperty',
  16662. LinesToStr([ // statements
  16663. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16664. ' this.$init = function () {',
  16665. ' };',
  16666. ' this.$final = function () {',
  16667. ' };',
  16668. ' this.GetCentury = function () {',
  16669. ' var Result = 0;',
  16670. ' return Result;',
  16671. ' };',
  16672. ' this.SetCentury = function (Value) {',
  16673. ' this.setYear(Value + 11);',
  16674. ' ExtA.setYear(ExtA.getYear() + 12);',
  16675. ' this.setYear(this.getYear() + 13);',
  16676. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16677. ' this.SetCentury(this.GetCentury() + 15);',
  16678. ' };',
  16679. '});',
  16680. 'this.A = null;',
  16681. 'this.B = null;',
  16682. '']),
  16683. LinesToStr([ // $mod.$main
  16684. 'ExtA.setYear(ExtA.getYear() + 1);',
  16685. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16686. 'ExtA.setYear(ExtA.getYear() + 3);',
  16687. '$mod.B.setYear($mod.B.getYear() + 4);',
  16688. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16689. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16690. '']));
  16691. end;
  16692. procedure TTestModule.TestExternalClass_ClassOf;
  16693. begin
  16694. StartProgram(false);
  16695. Add('{$modeswitch externalclass}');
  16696. Add('type');
  16697. Add(' TExtA = class external name ''ExtA''');
  16698. Add(' procedure ProcA; virtual;');
  16699. Add(' procedure ProcB; virtual;');
  16700. Add(' end;');
  16701. Add(' TExtAClass = class of TExtA;');
  16702. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16703. Add(' end;');
  16704. Add(' TExtBClass = class of TExtB;');
  16705. Add(' TExtC = class (TExtB)');
  16706. Add(' procedure ProcA; override;');
  16707. Add(' end;');
  16708. Add(' TExtCClass = class of TExtC;');
  16709. Add('procedure TExtC.ProcA; begin end;');
  16710. Add('var');
  16711. Add(' A: texta; ClA: TExtAClass;');
  16712. Add(' B: textb; ClB: TExtBClass;');
  16713. Add(' C: textc; ClC: TExtCClass;');
  16714. Add('begin');
  16715. Add(' ClA:=texta;');
  16716. Add(' ClA:=textb;');
  16717. Add(' ClA:=textc;');
  16718. Add(' ClB:=textb;');
  16719. Add(' ClB:=textc;');
  16720. Add(' ClC:=textc;');
  16721. ConvertProgram;
  16722. CheckSource('TestExternalClass_ClassOf',
  16723. LinesToStr([ // statements
  16724. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16725. ' this.$init = function () {',
  16726. ' };',
  16727. ' this.$final = function () {',
  16728. ' };',
  16729. ' this.ProcA = function () {',
  16730. ' };',
  16731. '});',
  16732. 'this.A = null;',
  16733. 'this.ClA = null;',
  16734. 'this.B = null;',
  16735. 'this.ClB = null;',
  16736. 'this.C = null;',
  16737. 'this.ClC = null;',
  16738. '']),
  16739. LinesToStr([ // $mod.$main
  16740. '$mod.ClA = ExtA;',
  16741. '$mod.ClA = ExtB;',
  16742. '$mod.ClA = $mod.TExtC;',
  16743. '$mod.ClB = ExtB;',
  16744. '$mod.ClB = $mod.TExtC;',
  16745. '$mod.ClC = $mod.TExtC;',
  16746. '']));
  16747. end;
  16748. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16749. begin
  16750. AddModuleWithIntfImplSrc('unit2.pas',
  16751. LinesToStr([
  16752. '{$modeswitch externalclass}',
  16753. 'type',
  16754. ' TExtA = class external name ''ExtA''',
  16755. ' class var Id: longint;',
  16756. ' end;',
  16757. '']),
  16758. '');
  16759. StartUnit(true);
  16760. Add('interface');
  16761. Add('uses unit2;');
  16762. Add('implementation');
  16763. Add('begin');
  16764. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16765. ConvertUnit;
  16766. CheckSource('TestExternalClass_ClassOtherUnit',
  16767. LinesToStr([
  16768. '']),
  16769. LinesToStr([
  16770. 'ExtA.Id = ExtA.Id + 1;',
  16771. '']));
  16772. end;
  16773. procedure TTestModule.TestExternalClass_Is;
  16774. begin
  16775. StartProgram(false);
  16776. Add([
  16777. '{$modeswitch externalclass}',
  16778. 'type',
  16779. ' TExtA = class external name ''ExtA''',
  16780. ' end;',
  16781. ' TExtAClass = class of TExtA;',
  16782. ' TExtB = class external name ''ExtB'' (TExtA)',
  16783. ' end;',
  16784. ' TExtBClass = class of TExtB;',
  16785. ' TExtC = class (TExtB)',
  16786. ' end;',
  16787. ' TExtCClass = class of TExtC;',
  16788. 'var',
  16789. ' A: texta; ClA: TExtAClass;',
  16790. ' B: textb; ClB: TExtBClass;',
  16791. ' C: textc; ClC: TExtCClass;',
  16792. 'begin',
  16793. ' if a is textb then ;',
  16794. ' if a is textc then ;',
  16795. ' if b is textc then ;',
  16796. ' if cla is textb then ;',
  16797. ' if cla is textc then ;',
  16798. ' if clb is textc then ;',
  16799. ' try',
  16800. ' except',
  16801. ' on TExtA do ;',
  16802. ' on e: TExtB do ;',
  16803. ' end;',
  16804. '']);
  16805. ConvertProgram;
  16806. CheckSource('TestExternalClass_Is',
  16807. LinesToStr([ // statements
  16808. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16809. ' this.$init = function () {',
  16810. ' };',
  16811. ' this.$final = function () {',
  16812. ' };',
  16813. '});',
  16814. 'this.A = null;',
  16815. 'this.ClA = null;',
  16816. 'this.B = null;',
  16817. 'this.ClB = null;',
  16818. 'this.C = null;',
  16819. 'this.ClC = null;',
  16820. '']),
  16821. LinesToStr([ // $mod.$main
  16822. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16823. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16824. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16825. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16826. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16827. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16828. 'try {} catch ($e) {',
  16829. ' if (rtl.isExt($e,ExtA)) {}',
  16830. ' else if (rtl.isExt($e,ExtB)) {',
  16831. ' var e = $e;',
  16832. ' } else throw $e',
  16833. '};',
  16834. '']));
  16835. end;
  16836. procedure TTestModule.TestExternalClass_As;
  16837. begin
  16838. StartProgram(false);
  16839. Add('{$modeswitch externalclass}');
  16840. Add('type');
  16841. Add(' TExtA = class external name ''ExtA''');
  16842. Add(' end;');
  16843. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16844. Add(' end;');
  16845. Add(' TExtC = class (TExtB)');
  16846. Add(' end;');
  16847. Add('var');
  16848. Add(' A: texta;');
  16849. Add(' B: textb;');
  16850. Add(' C: textc;');
  16851. Add('begin');
  16852. Add(' b:=a as textb;');
  16853. Add(' c:=a as textc;');
  16854. Add(' c:=b as textc;');
  16855. ConvertProgram;
  16856. CheckSource('TestExternalClass_Is',
  16857. LinesToStr([ // statements
  16858. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16859. ' this.$init = function () {',
  16860. ' };',
  16861. ' this.$final = function () {',
  16862. ' };',
  16863. '});',
  16864. 'this.A = null;',
  16865. 'this.B = null;',
  16866. 'this.C = null;',
  16867. '']),
  16868. LinesToStr([ // $mod.$main
  16869. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16870. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16871. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16872. '']));
  16873. end;
  16874. procedure TTestModule.TestExternalClass_DestructorFail;
  16875. begin
  16876. StartProgram(false);
  16877. Add('{$modeswitch externalclass}');
  16878. Add('type');
  16879. Add(' TExtA = class external name ''ExtA''');
  16880. Add(' destructor Free;');
  16881. Add(' end;');
  16882. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16883. nPasElementNotSupported);
  16884. ConvertProgram;
  16885. end;
  16886. procedure TTestModule.TestExternalClass_New;
  16887. begin
  16888. StartProgram(false);
  16889. Add([
  16890. '{$modeswitch externalclass}',
  16891. 'type',
  16892. ' TExtA = class external name ''ExtA''',
  16893. ' constructor New;',
  16894. ' constructor New(i: longint; j: longint = 2);',
  16895. ' end;',
  16896. 'var',
  16897. ' A: texta;',
  16898. 'begin',
  16899. ' a:=texta.new;',
  16900. ' a:=texta(texta.new);',
  16901. ' a:=texta.new();',
  16902. ' a:=texta.new(1);',
  16903. ' with texta do begin',
  16904. ' a:=new;',
  16905. ' a:=new();',
  16906. ' a:=new(2);',
  16907. ' end;',
  16908. ' a:=test1.texta.new;',
  16909. ' a:=test1.texta.new();',
  16910. ' a:=test1.texta.new(3);',
  16911. '']);
  16912. ConvertProgram;
  16913. CheckSource('TestExternalClass_New',
  16914. LinesToStr([ // statements
  16915. 'this.A = null;',
  16916. '']),
  16917. LinesToStr([ // $mod.$main
  16918. '$mod.A = new ExtA();',
  16919. '$mod.A = new ExtA();',
  16920. '$mod.A = new ExtA();',
  16921. '$mod.A = new ExtA(1,2);',
  16922. '$mod.A = new ExtA();',
  16923. '$mod.A = new ExtA();',
  16924. '$mod.A = new ExtA(2,2);',
  16925. '$mod.A = new ExtA();',
  16926. '$mod.A = new ExtA();',
  16927. '$mod.A = new ExtA(3,2);',
  16928. '']));
  16929. end;
  16930. procedure TTestModule.TestExternalClass_ClassOf_New;
  16931. begin
  16932. StartProgram(false);
  16933. Add('{$modeswitch externalclass}');
  16934. Add('type');
  16935. Add(' TExtAClass = class of TExtA;');
  16936. Add(' TExtA = class external name ''ExtA''');
  16937. Add(' C: TExtAClass;');
  16938. Add(' constructor New;');
  16939. Add(' end;');
  16940. Add('var');
  16941. Add(' A: texta;');
  16942. Add(' C: textaclass;');
  16943. Add('begin');
  16944. Add(' a:=c.new;');
  16945. Add(' a:=c.new();');
  16946. Add(' with C do begin');
  16947. Add(' a:=new;');
  16948. Add(' a:=new();');
  16949. Add(' end;');
  16950. Add(' a:=test1.c.new;');
  16951. Add(' a:=test1.c.new();');
  16952. Add(' a:=A.c.new();');
  16953. ConvertProgram;
  16954. CheckSource('TestExternalClass_ClassOf_New',
  16955. LinesToStr([ // statements
  16956. 'this.A = null;',
  16957. 'this.C = null;',
  16958. '']),
  16959. LinesToStr([ // $mod.$main
  16960. '$mod.A = new $mod.C();',
  16961. '$mod.A = new $mod.C();',
  16962. 'var $with = $mod.C;',
  16963. '$mod.A = new $with();',
  16964. '$mod.A = new $with();',
  16965. '$mod.A = new $mod.C();',
  16966. '$mod.A = new $mod.C();',
  16967. '$mod.A = new $mod.A.C();',
  16968. '']));
  16969. end;
  16970. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16971. begin
  16972. StartProgram(false);
  16973. Add([
  16974. '{$modeswitch externalclass}',
  16975. 'type',
  16976. ' TExtAClass = class of TExtA;',
  16977. ' TExtA = class external name ''ExtA''',
  16978. ' constructor New;',
  16979. ' end;',
  16980. 'function GetCreator: TExtAClass;',
  16981. 'begin',
  16982. ' Result:=TExtA;',
  16983. 'end;',
  16984. 'var',
  16985. ' A: texta;',
  16986. 'begin',
  16987. ' a:=getcreator.new;',
  16988. ' a:=getcreator().new;',
  16989. ' a:=getcreator().new();',
  16990. ' a:=getcreator.new();',
  16991. ' with getcreator do begin',
  16992. ' a:=new;',
  16993. ' a:=new();',
  16994. ' end;']);
  16995. ConvertProgram;
  16996. CheckSource('TestExternalClass_FuncClassOf_New',
  16997. LinesToStr([ // statements
  16998. 'this.GetCreator = function () {',
  16999. ' var Result = null;',
  17000. ' Result = ExtA;',
  17001. ' return Result;',
  17002. '};',
  17003. 'this.A = null;',
  17004. '']),
  17005. LinesToStr([ // $mod.$main
  17006. '$mod.A = new ($mod.GetCreator())();',
  17007. '$mod.A = new ($mod.GetCreator())();',
  17008. '$mod.A = new ($mod.GetCreator())();',
  17009. '$mod.A = new ($mod.GetCreator())();',
  17010. 'var $with = $mod.GetCreator();',
  17011. '$mod.A = new $with();',
  17012. '$mod.A = new $with();',
  17013. '']));
  17014. end;
  17015. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17016. begin
  17017. StartProgram(false);
  17018. Add([
  17019. '{$modeswitch externalclass}',
  17020. 'type',
  17021. ' TExtA = class external name ''ExtA''',
  17022. ' constructor New;',
  17023. ' end;',
  17024. ' TBird = class(TExtA)',
  17025. ' end;',
  17026. 'begin',
  17027. ' TBird.new;',
  17028. '']);
  17029. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17030. ConvertProgram;
  17031. end;
  17032. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17033. begin
  17034. StartProgram(false);
  17035. Add([
  17036. '{$modeswitch externalclass}',
  17037. 'type',
  17038. ' TExtA = class external name ''ExtA''',
  17039. ' constructor New;',
  17040. ' end;',
  17041. ' TBird = class(TExtA)',
  17042. ' end;',
  17043. 'begin',
  17044. ' TBird.new();',
  17045. '']);
  17046. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17047. ConvertProgram;
  17048. end;
  17049. procedure TTestModule.TestExternalClass_NewExtName;
  17050. begin
  17051. StartProgram(false);
  17052. Add([
  17053. '{$modeswitch externalclass}',
  17054. 'type',
  17055. ' TExtA = class external name ''ExtA''',
  17056. ' constructor New; external name ''Other'';',
  17057. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17058. ' end;',
  17059. 'var',
  17060. ' A: texta;',
  17061. 'begin',
  17062. ' a:=texta.new;',
  17063. ' a:=texta(texta.new);',
  17064. ' a:=texta.new();',
  17065. ' a:=texta.new(1);',
  17066. ' with texta do begin',
  17067. ' a:=new;',
  17068. ' a:=new();',
  17069. ' a:=new(2);',
  17070. ' end;',
  17071. ' a:=test1.texta.new;',
  17072. ' a:=test1.texta.new();',
  17073. ' a:=test1.texta.new(3);',
  17074. '']);
  17075. ConvertProgram;
  17076. CheckSource('TestExternalClass_NewExtName',
  17077. LinesToStr([ // statements
  17078. 'this.A = null;',
  17079. '']),
  17080. LinesToStr([ // $mod.$main
  17081. '$mod.A = new Other();',
  17082. '$mod.A = new Other();',
  17083. '$mod.A = new Other();',
  17084. '$mod.A = new A.B(1,2);',
  17085. '$mod.A = new Other();',
  17086. '$mod.A = new Other();',
  17087. '$mod.A = new A.B(2,2);',
  17088. '$mod.A = new Other();',
  17089. '$mod.A = new Other();',
  17090. '$mod.A = new A.B(3,2);',
  17091. '']));
  17092. end;
  17093. procedure TTestModule.TestExternalClass_Constructor;
  17094. begin
  17095. StartProgram(false);
  17096. Add([
  17097. '{$modeswitch externalclass}',
  17098. 'type',
  17099. ' TExtA = class external name ''ExtA''',
  17100. ' constructor Create;',
  17101. ' constructor Create(i: longint; j: longint = 2);',
  17102. ' end;',
  17103. 'var',
  17104. ' A: texta;',
  17105. 'begin',
  17106. ' a:=texta.create;',
  17107. ' a:=texta(texta.create);',
  17108. ' a:=texta.create();',
  17109. ' a:=texta.create(1);',
  17110. ' with texta do begin',
  17111. ' a:=create;',
  17112. ' a:=create();',
  17113. ' a:=create(2);',
  17114. ' end;',
  17115. ' a:=test1.texta.create;',
  17116. ' a:=test1.texta.create();',
  17117. ' a:=test1.texta.create(3);',
  17118. '']);
  17119. ConvertProgram;
  17120. CheckSource('TestExternalClass_Constructor',
  17121. LinesToStr([ // statements
  17122. 'this.A = null;',
  17123. '']),
  17124. LinesToStr([ // $mod.$main
  17125. '$mod.A = new ExtA.Create();',
  17126. '$mod.A = new ExtA.Create();',
  17127. '$mod.A = new ExtA.Create();',
  17128. '$mod.A = new ExtA.Create(1,2);',
  17129. '$mod.A = new ExtA.Create();',
  17130. '$mod.A = new ExtA.Create();',
  17131. '$mod.A = new ExtA.Create(2,2);',
  17132. '$mod.A = new ExtA.Create();',
  17133. '$mod.A = new ExtA.Create();',
  17134. '$mod.A = new ExtA.Create(3,2);',
  17135. '']));
  17136. end;
  17137. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17138. begin
  17139. StartProgram(false);
  17140. Add([
  17141. '{$modeswitch externalclass}',
  17142. 'type',
  17143. ' TExtA = class external name ''ExtA''',
  17144. ' constructor Create; external name ''{}'';',
  17145. ' end;',
  17146. 'var',
  17147. ' A: texta;',
  17148. 'begin',
  17149. ' a:=texta.create;',
  17150. ' a:=texta(texta.create);',
  17151. ' a:=texta.create();',
  17152. ' with texta do begin',
  17153. ' a:=create;',
  17154. ' a:=create();',
  17155. ' end;',
  17156. ' a:=test1.texta.create;',
  17157. ' a:=test1.texta.create();',
  17158. '']);
  17159. ConvertProgram;
  17160. CheckSource('TestExternalClass_ConstructorBrackets',
  17161. LinesToStr([ // statements
  17162. 'this.A = null;',
  17163. '']),
  17164. LinesToStr([ // $mod.$main
  17165. '$mod.A = {};',
  17166. '$mod.A = {};',
  17167. '$mod.A = {};',
  17168. '$mod.A = {};',
  17169. '$mod.A = {};',
  17170. '$mod.A = {};',
  17171. '$mod.A = {};',
  17172. '']));
  17173. end;
  17174. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17175. begin
  17176. StartProgram(false);
  17177. Add('{$modeswitch externalclass}');
  17178. Add('type');
  17179. Add(' TExtA = class external name ''ExtA''');
  17180. Add(' constructor New;');
  17181. Add(' end;');
  17182. Add('function DoIt: longint;');
  17183. Add('const ExtA: longint = 3;');
  17184. Add('begin');
  17185. Add(' Result:=ExtA;');
  17186. Add('end;');
  17187. Add('var');
  17188. Add(' A: texta;');
  17189. Add('begin');
  17190. Add(' a:=texta.new;');
  17191. ConvertProgram;
  17192. CheckSource('TestExternalClass_LocalConstSameName',
  17193. LinesToStr([ // statements
  17194. 'var ExtA$1 = 3;',
  17195. 'this.DoIt = function () {',
  17196. ' var Result = 0;',
  17197. ' Result = ExtA$1;',
  17198. ' return Result;',
  17199. '};',
  17200. 'this.A = null;',
  17201. '']),
  17202. LinesToStr([ // $mod.$main
  17203. '$mod.A = new ExtA();',
  17204. '']));
  17205. end;
  17206. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17207. begin
  17208. StartProgram(false);
  17209. Add('{$modeswitch externalclass}');
  17210. Add('type');
  17211. Add(' TExtA = class external name ''ExtA''');
  17212. Add(' procedure DoIt;');
  17213. Add(' end;');
  17214. Add(' TMyA = class(TExtA)');
  17215. Add(' procedure DoIt;');
  17216. Add(' end;');
  17217. Add('procedure TMyA.DoIt; begin end;');
  17218. Add('begin');
  17219. ConvertProgram;
  17220. CheckSource('TestExternalClass_ReintroduceOverload',
  17221. LinesToStr([ // statements
  17222. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17223. ' this.$init = function () {',
  17224. ' };',
  17225. ' this.$final = function () {',
  17226. ' };',
  17227. ' this.DoIt$1 = function () {',
  17228. ' };',
  17229. '});',
  17230. '']),
  17231. LinesToStr([ // $mod.$main
  17232. '']));
  17233. end;
  17234. procedure TTestModule.TestExternalClass_Inherited;
  17235. begin
  17236. StartProgram(false);
  17237. Add('{$modeswitch externalclass}');
  17238. Add('type');
  17239. Add(' TExtA = class external name ''ExtA''');
  17240. Add(' procedure DoIt(i: longint = 1); virtual;');
  17241. Add(' procedure DoSome(j: longint = 2);');
  17242. Add(' end;');
  17243. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17244. Add(' end;');
  17245. Add(' TMyC = class(TExtB)');
  17246. Add(' procedure DoIt(i: longint = 1); override;');
  17247. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17248. Add(' end;');
  17249. Add('procedure TMyC.DoIt(i: longint);');
  17250. Add('begin');
  17251. Add(' inherited;');
  17252. Add(' inherited DoIt;');
  17253. Add(' inherited DoIt();');
  17254. Add(' inherited DoIt(3);');
  17255. Add(' inherited DoSome;');
  17256. Add(' inherited DoSome();');
  17257. Add(' inherited DoSome(4);');
  17258. Add('end;');
  17259. Add('procedure TMyC.DoSome(j: longint);');
  17260. Add('begin');
  17261. Add(' inherited;');
  17262. Add('end;');
  17263. Add('begin');
  17264. ConvertProgram;
  17265. CheckSource('TestExternalClass_ReintroduceOverload',
  17266. LinesToStr([ // statements
  17267. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17268. ' this.$init = function () {',
  17269. ' };',
  17270. ' this.$final = function () {',
  17271. ' };',
  17272. ' this.DoIt = function (i) {',
  17273. ' ExtB.DoIt.apply(this, arguments);',
  17274. ' ExtB.DoIt.call(this, 1);',
  17275. ' ExtB.DoIt.call(this, 1);',
  17276. ' ExtB.DoIt.call(this, 3);',
  17277. ' ExtB.DoSome.call(this, 2);',
  17278. ' ExtB.DoSome.call(this, 2);',
  17279. ' ExtB.DoSome.call(this, 4);',
  17280. ' };',
  17281. ' this.DoSome$1 = function (j) {',
  17282. ' ExtB.DoSome.apply(this, arguments);',
  17283. ' };',
  17284. '});',
  17285. '']),
  17286. LinesToStr([ // $mod.$main
  17287. '']));
  17288. end;
  17289. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17290. begin
  17291. StartProgram(false);
  17292. Add('{$modeswitch externalclass}');
  17293. Add('type');
  17294. Add(' TObject = class');
  17295. Add(' end;');
  17296. Add(' TExtA = class external name ''ExtA''(TObject)');
  17297. Add(' end;');
  17298. Add('begin');
  17299. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17300. ConvertProgram;
  17301. end;
  17302. procedure TTestModule.TestExternalClass_NewInstance;
  17303. begin
  17304. StartProgram(false);
  17305. Add('{$modeswitch externalclass}');
  17306. Add('type');
  17307. Add(' TExtA = class external name ''ExtA''');
  17308. Add(' end;');
  17309. Add(' TMyB = class(TExtA)');
  17310. Add(' protected');
  17311. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17312. Add(' end;');
  17313. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17314. Add('begin end;');
  17315. Add('begin');
  17316. ConvertProgram;
  17317. CheckSource('TestExternalClass_NewInstance',
  17318. LinesToStr([ // statements
  17319. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17320. ' this.$init = function () {',
  17321. ' };',
  17322. ' this.$final = function () {',
  17323. ' };',
  17324. ' this.NewInstance = function (fnname, paramarray) {',
  17325. ' var Result = null;',
  17326. ' return Result;',
  17327. ' };',
  17328. '});',
  17329. '']),
  17330. LinesToStr([ // $mod.$main
  17331. '']));
  17332. end;
  17333. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17334. begin
  17335. StartProgram(false);
  17336. Add('{$modeswitch externalclass}');
  17337. Add('type');
  17338. Add(' TExtA = class external name ''ExtA''');
  17339. Add(' end;');
  17340. Add(' TMyB = class(TExtA)');
  17341. Add(' protected');
  17342. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17343. Add(' end;');
  17344. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17345. Add('begin end;');
  17346. Add('begin');
  17347. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17348. ConvertProgram;
  17349. end;
  17350. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17351. begin
  17352. StartProgram(false);
  17353. Add('{$modeswitch externalclass}');
  17354. Add('type');
  17355. Add(' TExtA = class external name ''ExtA''');
  17356. Add(' end;');
  17357. Add(' TMyB = class(TExtA)');
  17358. Add(' protected');
  17359. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17360. Add(' end;');
  17361. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17362. Add('begin end;');
  17363. Add('begin');
  17364. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17365. nIncompatibleTypeArgNo);
  17366. ConvertProgram;
  17367. end;
  17368. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17369. begin
  17370. StartProgram(false);
  17371. Add('{$modeswitch externalclass}');
  17372. Add('type');
  17373. Add(' TExtA = class external name ''ExtA''');
  17374. Add(' end;');
  17375. Add(' TMyB = class(TExtA)');
  17376. Add(' protected');
  17377. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17378. Add(' end;');
  17379. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17380. Add('begin end;');
  17381. Add('begin');
  17382. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17383. nIncompatibleTypeArgNo);
  17384. ConvertProgram;
  17385. end;
  17386. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17387. begin
  17388. StartProgram(false);
  17389. Add([
  17390. '{$modeswitch externalclass}',
  17391. 'type',
  17392. ' TJSFunction = class external name ''Function''',
  17393. ' end;',
  17394. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17395. ' constructor New(w: word);',
  17396. ' end;',
  17397. ' TBird = class (TExtA)',
  17398. ' public',
  17399. ' Size: word;',
  17400. ' class var Legs: word;',
  17401. ' constructor Create(a: word);',
  17402. ' end;',
  17403. ' TEagle = class (TBird)',
  17404. ' public',
  17405. ' constructor Create(b: word); reintroduce;',
  17406. ' end;',
  17407. 'constructor TBird.Create(a: word);',
  17408. 'begin',
  17409. ' inherited;', // silently ignored
  17410. ' inherited New(a);', // this.$func(a)
  17411. 'end;',
  17412. 'constructor TEagle.Create(b: word);',
  17413. 'begin',
  17414. ' inherited Create(b);',
  17415. 'end;',
  17416. 'var',
  17417. ' Bird: TBird;',
  17418. ' Eagle: TEagle;',
  17419. 'begin',
  17420. ' Bird:=TBird.Create(3);',
  17421. ' Eagle:=TEagle.Create(4);',
  17422. ' Bird.Size:=Bird.Size+5;',
  17423. ' Bird.Legs:=Bird.Legs+6;',
  17424. ' Eagle.Size:=Eagle.Size+5;',
  17425. ' Eagle.Legs:=Eagle.Legs+6;',
  17426. '']);
  17427. ConvertProgram;
  17428. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17429. LinesToStr([ // statements
  17430. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17431. ' this.Legs = 0;',
  17432. ' this.$init = function () {',
  17433. ' this.Size = 0;',
  17434. ' };',
  17435. ' this.$final = function () {',
  17436. ' };',
  17437. ' this.Create = function (a) {',
  17438. ' this.$ancestorfunc(a);',
  17439. ' return this;',
  17440. ' };',
  17441. '});',
  17442. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17443. ' this.Create$1 = function (b) {',
  17444. ' $mod.TBird.Create.call(this, b);',
  17445. ' return this;',
  17446. ' };',
  17447. '});',
  17448. 'this.Bird = null;',
  17449. 'this.Eagle = null;',
  17450. '']),
  17451. LinesToStr([ // $mod.$main
  17452. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17453. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17454. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17455. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17456. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17457. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17458. '']));
  17459. end;
  17460. procedure TTestModule.TestExternalClass_PascalProperty;
  17461. begin
  17462. StartProgram(false);
  17463. Add('{$modeswitch externalclass}');
  17464. Add('type');
  17465. Add(' TJSElement = class;');
  17466. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17467. Add(' TJSElement = class external name ''ExtA''');
  17468. Add(' end;');
  17469. Add(' TControl = class(TJSElement)');
  17470. Add(' private');
  17471. Add(' FOnClick: TJSNotifyEvent;');
  17472. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17473. Add(' procedure Click(Sender: TJSElement);');
  17474. Add(' end;');
  17475. Add('procedure TControl.Click(Sender: TJSElement);');
  17476. Add('begin');
  17477. Add(' OnClick(Self);');
  17478. Add('end;');
  17479. Add('var');
  17480. Add(' Ctrl: TControl;');
  17481. Add('begin');
  17482. Add(' Ctrl.OnClick:[email protected];');
  17483. Add(' Ctrl.OnClick(Ctrl);');
  17484. ConvertProgram;
  17485. CheckSource('TestExternalClass_PascalProperty',
  17486. LinesToStr([ // statements
  17487. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17488. ' this.$init = function () {',
  17489. ' this.FOnClick = null;',
  17490. ' };',
  17491. ' this.$final = function () {',
  17492. ' this.FOnClick = undefined;',
  17493. ' };',
  17494. ' this.Click = function (Sender) {',
  17495. ' this.FOnClick(this);',
  17496. ' };',
  17497. '});',
  17498. 'this.Ctrl = null;',
  17499. '']),
  17500. LinesToStr([ // $mod.$main
  17501. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17502. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17503. '']));
  17504. end;
  17505. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17506. begin
  17507. StartProgram(false);
  17508. Add([
  17509. '{$modeswitch externalclass}',
  17510. 'type',
  17511. ' IUnknown = interface end;',
  17512. ' TObject = class',
  17513. ' end;',
  17514. ' TChild = class',
  17515. ' end;',
  17516. ' TExtRootA = class external name ''ExtRootA''',
  17517. ' end;',
  17518. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17519. ' end;',
  17520. ' TExtRootB = class external name ''ExtRootB''',
  17521. ' end;',
  17522. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17523. ' end;',
  17524. 'var',
  17525. ' Obj: TObject;',
  17526. ' Child: TChild;',
  17527. ' RootA: TExtRootA;',
  17528. ' ChildA: TExtChildA;',
  17529. ' RootB: TExtRootB;',
  17530. ' ChildB: TExtChildB;',
  17531. ' i: IUnknown;',
  17532. 'begin',
  17533. ' obj:=tobject(roota);',
  17534. ' obj:=tobject(childa);',
  17535. ' child:=tchild(tobject(roota));',
  17536. ' roota:=textroota(obj);',
  17537. ' roota:=textroota(child);',
  17538. ' roota:=textroota(rootb);',
  17539. ' roota:=textroota(childb);',
  17540. ' childa:=textchilda(textroota(obj));',
  17541. ' roota:=TExtRootA(i)',
  17542. '']);
  17543. ConvertProgram;
  17544. CheckSource('TestExternalClass_TypeCastToRootClass',
  17545. LinesToStr([ // statements
  17546. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17547. 'rtl.createClass(this, "TObject", null, function () {',
  17548. ' this.$init = function () {',
  17549. ' };',
  17550. ' this.$final = function () {',
  17551. ' };',
  17552. '});',
  17553. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17554. '});',
  17555. 'this.Obj = null;',
  17556. 'this.Child = null;',
  17557. 'this.RootA = null;',
  17558. 'this.ChildA = null;',
  17559. 'this.RootB = null;',
  17560. 'this.ChildB = null;',
  17561. 'this.i = null;',
  17562. '']),
  17563. LinesToStr([ // $mod.$main
  17564. '$mod.Obj = $mod.RootA;',
  17565. '$mod.Obj = $mod.ChildA;',
  17566. '$mod.Child = $mod.RootA;',
  17567. '$mod.RootA = $mod.Obj;',
  17568. '$mod.RootA = $mod.Child;',
  17569. '$mod.RootA = $mod.RootB;',
  17570. '$mod.RootA = $mod.ChildB;',
  17571. '$mod.ChildA = $mod.Obj;',
  17572. '$mod.RootA = $mod.i;',
  17573. '']));
  17574. end;
  17575. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17576. begin
  17577. StartProgram(false);
  17578. Add([
  17579. '{$modeswitch externalclass}',
  17580. 'type',
  17581. ' IUnknown = interface end;',
  17582. ' IBird = interface(IUnknown) end;',
  17583. ' TClass = class of TObject;',
  17584. ' TObject = class',
  17585. ' end;',
  17586. ' TChild = class',
  17587. ' end;',
  17588. ' TJSObject = class external name ''Object''',
  17589. ' end;',
  17590. ' TRec = record end;',
  17591. 'var',
  17592. ' Obj: TObject;',
  17593. ' Child: TChild;',
  17594. ' i: IUnknown;',
  17595. ' Bird: IBird;',
  17596. ' j: TJSObject;',
  17597. ' r: TRec;',
  17598. ' c: TClass;',
  17599. 'begin',
  17600. ' j:=tjsobject(IUnknown);',
  17601. ' j:=tjsobject(IBird);',
  17602. ' j:=tjsobject(TObject);',
  17603. ' j:=tjsobject(TChild);',
  17604. ' j:=tjsobject(TRec);',
  17605. ' j:=tjsobject(Obj);',
  17606. ' j:=tjsobject(Child);',
  17607. ' j:=tjsobject(i);',
  17608. ' j:=tjsobject(Bird);',
  17609. ' j:=tjsobject(r);',
  17610. ' j:=tjsobject(c);',
  17611. '']);
  17612. ConvertProgram;
  17613. CheckSource('TestExternalClass_TypeCastToJSObject',
  17614. LinesToStr([ // statements
  17615. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17616. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17617. 'rtl.createClass(this, "TObject", null, function () {',
  17618. ' this.$init = function () {',
  17619. ' };',
  17620. ' this.$final = function () {',
  17621. ' };',
  17622. '});',
  17623. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17624. '});',
  17625. 'rtl.recNewT(this, "TRec", function () {',
  17626. ' this.$eq = function (b) {',
  17627. ' return true;',
  17628. ' };',
  17629. ' this.$assign = function (s) {',
  17630. ' return this;',
  17631. ' };',
  17632. '});',
  17633. 'this.Obj = null;',
  17634. 'this.Child = null;',
  17635. 'this.i = null;',
  17636. 'this.Bird = null;',
  17637. 'this.j = null;',
  17638. 'this.r = this.TRec.$new();',
  17639. 'this.c = null;',
  17640. '']),
  17641. LinesToStr([ // $mod.$main
  17642. '$mod.j = $mod.IUnknown;',
  17643. '$mod.j = $mod.IBird;',
  17644. '$mod.j = $mod.TObject;',
  17645. '$mod.j = $mod.TChild;',
  17646. '$mod.j = $mod.TRec;',
  17647. '$mod.j = $mod.Obj;',
  17648. '$mod.j = $mod.Child;',
  17649. '$mod.j = $mod.i;',
  17650. '$mod.j = $mod.Bird;',
  17651. '$mod.j = $mod.r;',
  17652. '$mod.j = $mod.c;',
  17653. '']));
  17654. end;
  17655. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17656. begin
  17657. StartProgram(false);
  17658. Add('{$modeswitch externalclass}');
  17659. Add('type');
  17660. Add(' TJSString = class external name ''String''');
  17661. Add(' class function fromCharCode() : string; varargs;');
  17662. Add(' function anchor(const aName : string) : string;');
  17663. Add(' end;');
  17664. Add('var');
  17665. Add(' s: string;');
  17666. Add('begin');
  17667. Add(' s:=TJSString.fromCharCode(65,66);');
  17668. Add(' s:=TJSString(s).anchor(s);');
  17669. Add(' s:=TJSString(''foo'').anchor(s);');
  17670. ConvertProgram;
  17671. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17672. LinesToStr([ // statements
  17673. 'this.s = "";',
  17674. '']),
  17675. LinesToStr([ // $mod.$main
  17676. '$mod.s = String.fromCharCode(65, 66);',
  17677. '$mod.s = $mod.s.anchor($mod.s);',
  17678. '$mod.s = "foo".anchor($mod.s);',
  17679. '']));
  17680. end;
  17681. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17682. begin
  17683. StartProgram(false);
  17684. Add([
  17685. '{$modeswitch externalclass}',
  17686. 'type',
  17687. ' TJSObject = class external name ''Object'' end;',
  17688. ' TJSFunction = class external name ''Function''',
  17689. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17690. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17691. ' end;',
  17692. ' TObject = class',
  17693. ' procedure DoIt(i: longint);',
  17694. ' end;',
  17695. ' TFuncInt = function(o: TObject): longint;',
  17696. 'function GetIt(o: TObject): longint;',
  17697. ' procedure Sub; begin end;',
  17698. 'var',
  17699. ' f: TJSFunction;',
  17700. ' fi: TFuncInt;',
  17701. 'begin',
  17702. ' fi:=TFuncInt(f);',
  17703. ' f:=TJSFunction(fi);',
  17704. ' f:=TJSFunction(@GetIt);',
  17705. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17706. ' f:=TJSFunction(@Sub);',
  17707. ' f:=TJSFunction(@o.doit);',
  17708. ' f:=TJSFunction(fi).bind(nil,4)',
  17709. 'end;',
  17710. 'procedure TObject.DoIt(i: longint);',
  17711. ' procedure Sub; begin end;',
  17712. 'var f: TJSFunction;',
  17713. 'begin',
  17714. ' f:=TJSFunction(@DoIt);',
  17715. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17716. ' f:=TJSFunction(@Sub);',
  17717. ' f:=TJSFunction(@GetIt);',
  17718. 'end;',
  17719. 'begin']);
  17720. ConvertProgram;
  17721. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17722. LinesToStr([ // statements
  17723. 'rtl.createClass(this, "TObject", null, function () {',
  17724. ' this.$init = function () {',
  17725. ' };',
  17726. ' this.$final = function () {',
  17727. ' };',
  17728. ' this.DoIt = function (i) {',
  17729. ' var $Self = this;',
  17730. ' function Sub() {',
  17731. ' };',
  17732. ' var f = null;',
  17733. ' f = this.DoIt;',
  17734. ' f = this.DoIt.bind(null, 13);',
  17735. ' f = Sub;',
  17736. ' f = $mod.GetIt;',
  17737. ' };',
  17738. '});',
  17739. 'this.GetIt = function (o) {',
  17740. ' var Result = 0;',
  17741. ' function Sub() {',
  17742. ' };',
  17743. ' var f = null;',
  17744. ' var fi = null;',
  17745. ' fi = f;',
  17746. ' f = fi;',
  17747. ' f = $mod.GetIt;',
  17748. ' f = $mod.GetIt.bind(null, 3);',
  17749. ' f = Sub;',
  17750. ' f = $mod.TObject.DoIt;',
  17751. ' f = fi.bind(null, 4);',
  17752. ' return Result;',
  17753. '};',
  17754. '']),
  17755. LinesToStr([ // $mod.$main
  17756. '']));
  17757. end;
  17758. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17759. begin
  17760. StartProgram(false);
  17761. Add([
  17762. '{$mode delphi}',
  17763. '{$modeswitch externalclass}',
  17764. 'type',
  17765. ' TJSObject = class external name ''Object'' end;',
  17766. ' TJSWindow = class external name ''Window''(TJSObject)',
  17767. ' procedure Open;',
  17768. ' end;',
  17769. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17770. ' procedure Execute;',
  17771. ' end;',
  17772. 'procedure Fly;',
  17773. 'var',
  17774. ' w: TJSWindow;',
  17775. ' e: TJSEventTarget;',
  17776. 'begin',
  17777. ' w:=TJSWindow(e);',
  17778. ' e:=TJSEventTarget(w);',
  17779. 'end;',
  17780. 'begin']);
  17781. ConvertProgram;
  17782. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17783. LinesToStr([ // statements
  17784. 'this.Fly = function () {',
  17785. ' var w = null;',
  17786. ' var e = null;',
  17787. ' w = e;',
  17788. ' e = w;',
  17789. '};',
  17790. '']),
  17791. LinesToStr([ // $mod.$main
  17792. '']));
  17793. end;
  17794. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17795. begin
  17796. StartProgram(false);
  17797. Add('{$modeswitch externalclass}');
  17798. Add('type');
  17799. Add(' TJSString = class external name ''String''');
  17800. Add(' class function fromCharCode() : string; varargs;');
  17801. Add(' end;');
  17802. Add('var');
  17803. Add(' s: string;');
  17804. Add(' sObj: TJSString;');
  17805. Add('begin');
  17806. Add(' s:=sObj.fromCharCode(65,66);');
  17807. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17808. nExternalClassInstanceCannotAccessStaticX);
  17809. ConvertProgram;
  17810. end;
  17811. procedure TTestModule.TestExternalClass_BracketAccessor;
  17812. begin
  17813. StartProgram(false);
  17814. Add([
  17815. '{$modeswitch externalclass}',
  17816. 'type',
  17817. ' TJSArray = class external name ''Array2''',
  17818. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17819. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17820. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17821. ' end;',
  17822. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17823. 'begin end;',
  17824. 'var',
  17825. ' Arr: tjsarray;',
  17826. ' s: string;',
  17827. ' i: longint;',
  17828. ' v: jsvalue;',
  17829. 'begin',
  17830. ' v:=arr[0];',
  17831. ' v:=arr.items[1];',
  17832. ' arr[2]:=s;',
  17833. ' arr.items[3]:=s;',
  17834. ' arr[4]:=i;',
  17835. ' arr[5]:=arr[6];',
  17836. ' arr.items[7]:=arr.items[8];',
  17837. ' with arr do items[9]:=items[10];',
  17838. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17839. ' with arr do begin',
  17840. ' v:=GetItems(14);',
  17841. ' setitems(15,16);',
  17842. ' end;',
  17843. ' v:=test1.arr.items[17];',
  17844. ' test1.arr.items[18]:=v;',
  17845. '']);
  17846. ConvertProgram;
  17847. CheckSource('TestExternalClass_BracketAccessor',
  17848. LinesToStr([ // statements
  17849. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17850. '};',
  17851. 'this.Arr = null;',
  17852. 'this.s = "";',
  17853. 'this.i = 0;',
  17854. 'this.v = undefined;',
  17855. '']),
  17856. LinesToStr([ // $mod.$main
  17857. '$mod.v = $mod.Arr[0];',
  17858. '$mod.v = $mod.Arr[1];',
  17859. '$mod.Arr[2] = $mod.s;',
  17860. '$mod.Arr[3] = $mod.s;',
  17861. '$mod.Arr[4] = $mod.i;',
  17862. '$mod.Arr[5] = $mod.Arr[6];',
  17863. '$mod.Arr[7] = $mod.Arr[8];',
  17864. 'var $with = $mod.Arr;',
  17865. '$with[9] = $with[10];',
  17866. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17867. ' a: 9,',
  17868. ' p: $mod.Arr,',
  17869. ' get: function () {',
  17870. ' return this.p[this.a];',
  17871. ' },',
  17872. ' set: function (v) {',
  17873. ' this.p[this.a] = v;',
  17874. ' }',
  17875. '}, {',
  17876. ' a: 10,',
  17877. ' p: $mod.Arr,',
  17878. ' get: function () {',
  17879. ' return this.p[this.a];',
  17880. ' },',
  17881. ' set: function (v) {',
  17882. ' this.p[this.a] = v;',
  17883. ' }',
  17884. '});',
  17885. 'var $with1 = $mod.Arr;',
  17886. '$mod.v = $with1[14];',
  17887. '$with1[15] = 16;',
  17888. '$mod.v = $mod.Arr[17];',
  17889. '$mod.Arr[18] = $mod.v;',
  17890. '']));
  17891. end;
  17892. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17893. begin
  17894. StartProgram(false);
  17895. Add([
  17896. '{$modeswitch externalclass}',
  17897. 'type',
  17898. ' TJSArray = class external name ''Array2''',
  17899. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17900. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17901. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17902. ' end;',
  17903. ' TMyArr = class(TJSArray)',
  17904. ' procedure DoIt;',
  17905. ' end;',
  17906. 'procedure tmyarr.DoIt;',
  17907. 'begin',
  17908. ' Items[1]:=Items[2];',
  17909. ' SetItems(3,getItems(4));',
  17910. 'end;',
  17911. 'var',
  17912. ' Arr: tmyarr;',
  17913. ' s: string;',
  17914. ' i: longint;',
  17915. ' v: jsvalue;',
  17916. 'begin',
  17917. ' v:=arr[0];',
  17918. ' v:=arr.items[1];',
  17919. ' arr[2]:=s;',
  17920. ' arr.items[3]:=s;',
  17921. ' arr[4]:=i;',
  17922. ' arr[5]:=arr[6];',
  17923. ' arr.items[7]:=arr.items[8];',
  17924. ' with arr do items[9]:=items[10];',
  17925. ' with arr do begin',
  17926. ' v:=GetItems(14);',
  17927. ' setitems(15,16);',
  17928. ' end;',
  17929. '']);
  17930. ConvertProgram;
  17931. CheckSource('TestExternalClass_BracketAccessor_Call',
  17932. LinesToStr([ // statements
  17933. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  17934. ' this.$init = function () {',
  17935. ' };',
  17936. ' this.$final = function () {',
  17937. ' };',
  17938. ' this.DoIt = function () {',
  17939. ' this[1] = this[2];',
  17940. ' this[3] = this[4];',
  17941. ' };',
  17942. '});',
  17943. 'this.Arr = null;',
  17944. 'this.s = "";',
  17945. 'this.i = 0;',
  17946. 'this.v = undefined;',
  17947. '']),
  17948. LinesToStr([ // $mod.$main
  17949. '$mod.v = $mod.Arr[0];',
  17950. '$mod.v = $mod.Arr[1];',
  17951. '$mod.Arr[2] = $mod.s;',
  17952. '$mod.Arr[3] = $mod.s;',
  17953. '$mod.Arr[4] = $mod.i;',
  17954. '$mod.Arr[5] = $mod.Arr[6];',
  17955. '$mod.Arr[7] = $mod.Arr[8];',
  17956. 'var $with = $mod.Arr;',
  17957. '$with[9] = $with[10];',
  17958. 'var $with1 = $mod.Arr;',
  17959. '$mod.v = $with1[14];',
  17960. '$with1[15] = 16;',
  17961. '']));
  17962. end;
  17963. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  17964. begin
  17965. StartProgram(false);
  17966. Add('{$modeswitch externalclass}');
  17967. Add('type');
  17968. Add(' TJSArray = class external name ''Array2''');
  17969. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  17970. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  17971. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  17972. Add(' end;');
  17973. Add('begin');
  17974. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  17975. nBracketAccessorOfExternalClassMustHaveOneParameter);
  17976. ConvertProgram;
  17977. end;
  17978. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  17979. begin
  17980. StartProgram(false);
  17981. Add('{$modeswitch externalclass}');
  17982. Add('type');
  17983. Add(' TJSArray = class external name ''Array2''');
  17984. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17985. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  17986. Add(' end;');
  17987. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  17988. Add('begin end;');
  17989. Add('var');
  17990. Add(' Arr: tjsarray;');
  17991. Add(' v: jsvalue;');
  17992. Add('begin');
  17993. Add(' v:=arr[0];');
  17994. Add(' v:=arr.items[1];');
  17995. Add(' with arr do v:=items[2];');
  17996. Add(' doit(arr[3],arr[4]);');
  17997. ConvertProgram;
  17998. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  17999. LinesToStr([ // statements
  18000. 'this.DoIt = function (vI, vJ) {',
  18001. '};',
  18002. 'this.Arr = null;',
  18003. 'this.v = undefined;',
  18004. '']),
  18005. LinesToStr([ // $mod.$main
  18006. '$mod.v = $mod.Arr[0];',
  18007. '$mod.v = $mod.Arr[1];',
  18008. 'var $with = $mod.Arr;',
  18009. '$mod.v = $with[2];',
  18010. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18011. '']));
  18012. end;
  18013. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18014. begin
  18015. StartProgram(false);
  18016. Add('{$modeswitch externalclass}');
  18017. Add('type');
  18018. Add(' TJSArray = class external name ''Array2''');
  18019. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18020. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18021. Add(' end;');
  18022. Add('var');
  18023. Add(' Arr: tjsarray;');
  18024. Add(' s: string;');
  18025. Add(' i: longint;');
  18026. Add(' v: jsvalue;');
  18027. Add('begin');
  18028. Add(' arr[2]:=s;');
  18029. Add(' arr.items[3]:=s;');
  18030. Add(' arr[4]:=i;');
  18031. Add(' with arr do items[5]:=i;');
  18032. ConvertProgram;
  18033. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18034. LinesToStr([ // statements
  18035. 'this.Arr = null;',
  18036. 'this.s = "";',
  18037. 'this.i = 0;',
  18038. 'this.v = undefined;',
  18039. '']),
  18040. LinesToStr([ // $mod.$main
  18041. '$mod.Arr[2] = $mod.s;',
  18042. '$mod.Arr[3] = $mod.s;',
  18043. '$mod.Arr[4] = $mod.i;',
  18044. 'var $with = $mod.Arr;',
  18045. '$with[5] = $mod.i;',
  18046. '']));
  18047. end;
  18048. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18049. begin
  18050. StartProgram(false);
  18051. Add('{$modeswitch externalclass}');
  18052. Add('type');
  18053. Add(' TJSArray = class external name ''Array2''');
  18054. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18055. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18056. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18057. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18058. Add(' end;');
  18059. Add('var');
  18060. Add(' Arr: tjsarray;');
  18061. Add(' s: string;');
  18062. Add(' i: longint;');
  18063. Add(' v: jsvalue;');
  18064. Add('begin');
  18065. Add(' arr[2]:=s;');
  18066. Add(' arr.items[3]:=s;');
  18067. Add(' arr.numbers[4]:=i;');
  18068. Add(' with arr do items[5]:=i;');
  18069. Add(' with arr do numbers[6]:=i;');
  18070. ConvertProgram;
  18071. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18072. LinesToStr([ // statements
  18073. 'this.Arr = null;',
  18074. 'this.s = "";',
  18075. 'this.i = 0;',
  18076. 'this.v = undefined;',
  18077. '']),
  18078. LinesToStr([ // $mod.$main
  18079. '$mod.Arr[2] = $mod.s;',
  18080. '$mod.Arr[3] = $mod.s;',
  18081. '$mod.Arr[4] = $mod.i;',
  18082. 'var $with = $mod.Arr;',
  18083. '$with[5] = $mod.i;',
  18084. 'var $with1 = $mod.Arr;',
  18085. '$with1[6] = $mod.i;',
  18086. '']));
  18087. end;
  18088. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18089. begin
  18090. StartProgram(false);
  18091. Add('{$modeswitch externalclass}');
  18092. Add('type');
  18093. Add(' TJSArray = class external name ''Array2''');
  18094. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18095. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18096. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18097. Add(' end;');
  18098. Add('var');
  18099. Add(' Arr: tjsarray;');
  18100. Add(' i: longint;');
  18101. Add(' IntArr: array of longint;');
  18102. Add(' v: jsvalue;');
  18103. Add('begin');
  18104. Add(' v:=arr.items[i];');
  18105. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18106. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18107. ConvertProgram;
  18108. CheckSource('TestExternalClass_BracketAccessor_Index',
  18109. LinesToStr([ // statements
  18110. 'this.Arr = null;',
  18111. 'this.i = 0;',
  18112. 'this.IntArr = [];',
  18113. 'this.v = undefined;',
  18114. '']),
  18115. LinesToStr([ // $mod.$main
  18116. '$mod.v = $mod.Arr[$mod.i];',
  18117. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18118. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18119. '']));
  18120. end;
  18121. procedure TTestModule.TestExternalClass_ForInJSObject;
  18122. begin
  18123. StartProgram(false);
  18124. Add([
  18125. '{$modeswitch externalclass}',
  18126. 'type',
  18127. ' TJSObject = class external name ''Object''',
  18128. ' end;',
  18129. 'var',
  18130. ' o: TJSObject;',
  18131. ' key: string;',
  18132. 'begin',
  18133. ' for key in o do',
  18134. ' if key=''abc'' then ;',
  18135. '']);
  18136. ConvertProgram;
  18137. CheckSource('TestExternalClass_ForInJSObject',
  18138. LinesToStr([ // statements
  18139. 'this.o = null;',
  18140. 'this.key = "";',
  18141. '']),
  18142. LinesToStr([ // $mod.$main
  18143. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18144. '']));
  18145. end;
  18146. procedure TTestModule.TestExternalClass_ForInJSArray;
  18147. begin
  18148. StartProgram(false);
  18149. Add([
  18150. '{$modeswitch externalclass}',
  18151. 'type',
  18152. ' TJSInt8Array = class external name ''Int8Array''',
  18153. ' private',
  18154. ' flength: NativeInt external name ''length'';',
  18155. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18156. ' public',
  18157. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18158. ' property Length: NativeInt read flength;',
  18159. ' end;',
  18160. 'var',
  18161. ' a: TJSInt8Array;',
  18162. ' value: shortint;',
  18163. 'begin',
  18164. ' for value in a do',
  18165. ' if value=3 then ;',
  18166. '']);
  18167. ConvertProgram;
  18168. CheckSource('TestExternalClass_ForInJSArray',
  18169. LinesToStr([ // statements
  18170. 'this.a = null;',
  18171. 'this.value = 0;',
  18172. '']),
  18173. LinesToStr([ // $mod.$main
  18174. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18175. ' $mod.value = $in[$l];',
  18176. ' if ($mod.value === 3) ;',
  18177. '};',
  18178. '']));
  18179. end;
  18180. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18181. begin
  18182. AddModuleWithIntfImplSrc('unit2.pas',
  18183. LinesToStr([
  18184. '{$modeswitch externalclass}',
  18185. 'type',
  18186. ' TJSBufferSource = class external name ''BufferSource''',
  18187. ' end;',
  18188. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18189. '']),
  18190. '');
  18191. AddModuleWithIntfImplSrc('unit3.pas',
  18192. LinesToStr([
  18193. '{$modeswitch externalclass}',
  18194. 'type',
  18195. ' TJSBufferSource = class external name ''BufferSource''',
  18196. ' end;',
  18197. '']),
  18198. '');
  18199. StartUnit(true);
  18200. Add([
  18201. 'interface',
  18202. 'uses unit2, unit3;',
  18203. 'procedure DoSome(s: TJSBufferSource);',
  18204. 'implementation',
  18205. 'procedure DoSome(s: TJSBufferSource);',
  18206. 'begin',
  18207. ' DoIt(s);',
  18208. 'end;',
  18209. '']);
  18210. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18211. nIncompatibleTypeArgNo);
  18212. ConvertUnit;
  18213. end;
  18214. procedure TTestModule.TestClassInterface_Corba;
  18215. begin
  18216. StartProgram(false);
  18217. Add([
  18218. '{$interfaces corba}',
  18219. 'type',
  18220. ' IUnknown = interface;',
  18221. ' IUnknown = interface',
  18222. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18223. ' end;',
  18224. ' IInterface = IUnknown;',
  18225. ' IBird = interface(IInterface)',
  18226. ' function GetSize: longint;',
  18227. ' procedure SetSize(i: longint);',
  18228. ' property Size: longint read GetSize write SetSize;',
  18229. ' procedure DoIt(i: longint);',
  18230. ' end;',
  18231. ' TObject = class',
  18232. ' end;',
  18233. ' TBird = class(TObject,IBird)',
  18234. ' function GetSize: longint; virtual; abstract;',
  18235. ' procedure SetSize(i: longint); virtual; abstract;',
  18236. ' procedure DoIt(i: longint); virtual; abstract;',
  18237. ' end;',
  18238. 'var',
  18239. ' BirdIntf: IBird;',
  18240. 'begin',
  18241. ' BirdIntf.Size:=BirdIntf.Size;',
  18242. '']);
  18243. ConvertProgram;
  18244. CheckSource('TestClassInterface_Corba',
  18245. LinesToStr([ // statements
  18246. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18247. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18248. 'rtl.createClass(this, "TObject", null, function () {',
  18249. ' this.$init = function () {',
  18250. ' };',
  18251. ' this.$final = function () {',
  18252. ' };',
  18253. '});',
  18254. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18255. ' rtl.addIntf(this, $mod.IBird);',
  18256. '});',
  18257. 'this.BirdIntf = null;',
  18258. '']),
  18259. LinesToStr([ // $mod.$main
  18260. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18261. '']));
  18262. end;
  18263. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18264. begin
  18265. StartProgram(false);
  18266. Add([
  18267. '{$interfaces corba}',
  18268. 'type',
  18269. ' IUnknown = interface',
  18270. ' procedure DoIt; external name ''foo'';',
  18271. ' end;',
  18272. 'begin']);
  18273. SetExpectedParserError(
  18274. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18275. nParserNoFieldsAllowed);
  18276. ConvertProgram;
  18277. end;
  18278. procedure TTestModule.TestClassInterface_Overloads;
  18279. begin
  18280. StartProgram(false);
  18281. Add([
  18282. '{$interfaces corba}',
  18283. 'type',
  18284. ' integer = longint;',
  18285. ' IUnknown = interface',
  18286. ' procedure DoIt(i: integer);',
  18287. ' procedure DoIt(s: string);',
  18288. ' end;',
  18289. ' IBird = interface(IUnknown)',
  18290. ' procedure DoIt(b: boolean); overload;',
  18291. ' end;',
  18292. ' TObject = class',
  18293. ' end;',
  18294. ' TBird = class(TObject,IBird)',
  18295. ' procedure DoIt(o: TObject);',
  18296. ' procedure DoIt(s: string);',
  18297. ' procedure DoIt(i: integer);',
  18298. ' procedure DoIt(b: boolean);',
  18299. ' end;',
  18300. 'procedure TBird.DoIt(o: TObject); begin end;',
  18301. 'procedure TBird.DoIt(s: string); begin end;',
  18302. 'procedure TBird.DoIt(i: integer); begin end;',
  18303. 'procedure TBird.DoIt(b: boolean); begin end;',
  18304. 'var',
  18305. ' BirdIntf: IBird;',
  18306. 'begin',
  18307. ' BirdIntf.DoIt(3);',
  18308. ' BirdIntf.DoIt(''abc'');',
  18309. ' BirdIntf.DoIt(true);',
  18310. '']);
  18311. ConvertProgram;
  18312. CheckSource('TestClassInterface_Overloads',
  18313. LinesToStr([ // statements
  18314. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18315. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18316. 'rtl.createClass(this, "TObject", null, function () {',
  18317. ' this.$init = function () {',
  18318. ' };',
  18319. ' this.$final = function () {',
  18320. ' };',
  18321. '});',
  18322. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18323. ' this.DoIt = function (o) {',
  18324. ' };',
  18325. ' this.DoIt$1 = function (s) {',
  18326. ' };',
  18327. ' this.DoIt$2 = function (i) {',
  18328. ' };',
  18329. ' this.DoIt$3 = function (b) {',
  18330. ' };',
  18331. ' rtl.addIntf(this, $mod.IBird, {',
  18332. ' DoIt$2: "DoIt$3",',
  18333. ' DoIt: "DoIt$2"',
  18334. ' });',
  18335. '});',
  18336. 'this.BirdIntf = null;',
  18337. '']),
  18338. LinesToStr([ // $mod.$main
  18339. '$mod.BirdIntf.DoIt(3);',
  18340. '$mod.BirdIntf.DoIt$1("abc");',
  18341. '$mod.BirdIntf.DoIt$2(true);',
  18342. '']));
  18343. end;
  18344. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18345. begin
  18346. StartProgram(false);
  18347. Add([
  18348. '{$interfaces corba}',
  18349. 'type',
  18350. ' IBird = interface',
  18351. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18352. ' end;',
  18353. ' IDog = interface',
  18354. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18355. ' end;',
  18356. ' TObject = class(IBird,IDog)',
  18357. ' end;',
  18358. 'begin']);
  18359. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18360. nDuplicateGUIDXInYZ);
  18361. ConvertProgram;
  18362. end;
  18363. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18364. begin
  18365. StartProgram(false);
  18366. Add([
  18367. '{$interfaces corba}',
  18368. 'type',
  18369. ' IAnimal = interface',
  18370. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18371. ' end;',
  18372. ' IBird = interface(IAnimal)',
  18373. ' end;',
  18374. ' IHawk = interface(IBird)',
  18375. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18376. ' end;',
  18377. 'begin']);
  18378. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18379. nDuplicateGUIDXInYZ);
  18380. ConvertProgram;
  18381. end;
  18382. procedure TTestModule.TestClassInterface_AncestorImpl;
  18383. begin
  18384. StartProgram(false);
  18385. Add([
  18386. '{$interfaces corba}',
  18387. 'type',
  18388. ' integer = longint;',
  18389. ' IUnknown = interface',
  18390. ' procedure DoIt(i: integer);',
  18391. ' end;',
  18392. ' IBird = interface',
  18393. ' procedure Fly(i: integer);',
  18394. ' end;',
  18395. ' TObject = class(IUnknown)',
  18396. ' procedure DoIt(i: integer);',
  18397. ' end;',
  18398. ' TBird = class(IBird)',
  18399. ' procedure Fly(i: integer);',
  18400. ' end;',
  18401. 'procedure TObject.DoIt(i: integer); begin end;',
  18402. 'procedure TBird.Fly(i: integer); begin end;',
  18403. 'begin',
  18404. '']);
  18405. ConvertProgram;
  18406. CheckSource('TestClassInterface_AncestorIntf',
  18407. LinesToStr([ // statements
  18408. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18409. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18410. 'rtl.createClass(this, "TObject", null, function () {',
  18411. ' this.$init = function () {',
  18412. ' };',
  18413. ' this.$final = function () {',
  18414. ' };',
  18415. ' this.DoIt = function (i) {',
  18416. ' };',
  18417. ' rtl.addIntf(this, $mod.IUnknown);',
  18418. '});',
  18419. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18420. ' this.Fly = function (i) {',
  18421. ' };',
  18422. ' rtl.addIntf(this, $mod.IBird);',
  18423. ' rtl.addIntf(this, $mod.IUnknown);',
  18424. '});',
  18425. '']),
  18426. LinesToStr([ // $mod.$main
  18427. '']));
  18428. end;
  18429. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18430. begin
  18431. StartProgram(false);
  18432. Add([
  18433. '{$interfaces corba}',
  18434. 'type',
  18435. ' integer = longint;',
  18436. ' IBird = interface',
  18437. ' procedure DoIt(i: integer);',
  18438. ' end;',
  18439. ' TObject = class',
  18440. ' procedure DoIt(i: integer);',
  18441. ' end;',
  18442. ' TBird = class(IBird)',
  18443. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18444. ' end;',
  18445. 'procedure TObject.DoIt(i: integer); begin end;',
  18446. 'procedure TBird.DoIt(i: integer); begin end;',
  18447. 'begin',
  18448. '']);
  18449. ConvertProgram;
  18450. CheckSource('TestClassInterface_ImplReintroduce',
  18451. LinesToStr([ // statements
  18452. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18453. 'rtl.createClass(this, "TObject", null, function () {',
  18454. ' this.$init = function () {',
  18455. ' };',
  18456. ' this.$final = function () {',
  18457. ' };',
  18458. ' this.DoIt = function (i) {',
  18459. ' };',
  18460. '});',
  18461. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18462. ' this.DoIt$1 = function (i) {',
  18463. ' };',
  18464. ' rtl.addIntf(this, $mod.IBird, {',
  18465. ' DoIt: "DoIt$1"',
  18466. ' });',
  18467. '});',
  18468. '']),
  18469. LinesToStr([ // $mod.$main
  18470. '']));
  18471. end;
  18472. procedure TTestModule.TestClassInterface_MethodResolution;
  18473. begin
  18474. StartProgram(false);
  18475. Add([
  18476. '{$interfaces corba}',
  18477. 'type',
  18478. ' IUnknown = interface',
  18479. ' procedure Walk(i: longint);',
  18480. ' end;',
  18481. ' IBird = interface(IUnknown)',
  18482. ' procedure Walk(b: boolean); overload;',
  18483. ' procedure Fly(s: string);',
  18484. ' end;',
  18485. ' TObject = class',
  18486. ' end;',
  18487. ' TBird = class(TObject,IBird)',
  18488. ' procedure IBird.Fly = Move;',
  18489. ' procedure IBird.Walk = Hop;',
  18490. ' procedure Hop(i: longint);',
  18491. ' procedure Move(s: string);',
  18492. ' procedure Hop(b: boolean);',
  18493. ' end;',
  18494. 'procedure TBird.Move(s: string); begin end;',
  18495. 'procedure TBird.Hop(i: longint); begin end;',
  18496. 'procedure TBird.Hop(b: boolean); begin end;',
  18497. 'var',
  18498. ' BirdIntf: IBird;',
  18499. 'begin',
  18500. ' BirdIntf.Walk(3);',
  18501. ' BirdIntf.Walk(true);',
  18502. ' BirdIntf.Fly(''abc'');',
  18503. '']);
  18504. ConvertProgram;
  18505. CheckSource('TestClassInterface_MethodResolution',
  18506. LinesToStr([ // statements
  18507. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18508. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18509. 'rtl.createClass(this, "TObject", null, function () {',
  18510. ' this.$init = function () {',
  18511. ' };',
  18512. ' this.$final = function () {',
  18513. ' };',
  18514. '});',
  18515. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18516. ' this.Hop = function (i) {',
  18517. ' };',
  18518. ' this.Move = function (s) {',
  18519. ' };',
  18520. ' this.Hop$1 = function (b) {',
  18521. ' };',
  18522. ' rtl.addIntf(this, $mod.IBird, {',
  18523. ' Walk$1: "Hop$1",',
  18524. ' Fly: "Move",',
  18525. ' Walk: "Hop"',
  18526. ' });',
  18527. '});',
  18528. 'this.BirdIntf = null;',
  18529. '']),
  18530. LinesToStr([ // $mod.$main
  18531. '$mod.BirdIntf.Walk(3);',
  18532. '$mod.BirdIntf.Walk$1(true);',
  18533. '$mod.BirdIntf.Fly("abc");',
  18534. '']));
  18535. end;
  18536. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18537. begin
  18538. StartProgram(false);
  18539. Add([
  18540. '{$interfaces com}',
  18541. 'type',
  18542. ' IUnknown = interface',
  18543. ' function _AddRef: longint;',
  18544. ' procedure Walk;',
  18545. ' end;',
  18546. ' IBird = interface end;',
  18547. ' IDog = interface end;',
  18548. ' TObject = class(IBird,IDog)',
  18549. ' function _AddRef: longint; virtual; abstract;',
  18550. ' procedure Walk; virtual; abstract;',
  18551. ' end;',
  18552. ' TBird = class(IUnknown)',
  18553. ' end;',
  18554. 'begin',
  18555. '']);
  18556. ConvertProgram;
  18557. CheckSource('TestClassInterface_COM_AncestorLess',
  18558. LinesToStr([ // statements
  18559. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18560. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18561. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18562. 'rtl.createClass(this, "TObject", null, function () {',
  18563. ' this.$init = function () {',
  18564. ' };',
  18565. ' this.$final = function () {',
  18566. ' };',
  18567. ' rtl.addIntf(this, $mod.IBird);',
  18568. ' rtl.addIntf(this, $mod.IDog);',
  18569. '});',
  18570. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18571. ' rtl.addIntf(this, $mod.IUnknown);',
  18572. ' rtl.addIntf(this, $mod.IBird);',
  18573. ' rtl.addIntf(this, $mod.IDog);',
  18574. '});',
  18575. '']),
  18576. LinesToStr([ // $mod.$main
  18577. '']));
  18578. end;
  18579. procedure TTestModule.TestClassInterface_MethodOverride;
  18580. begin
  18581. StartProgram(false);
  18582. Add([
  18583. '{$interfaces corba}',
  18584. 'type',
  18585. ' IUnknown = interface',
  18586. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18587. ' procedure Go;',
  18588. ' end;',
  18589. ' TObject = class(IUnknown)',
  18590. ' procedure Go; virtual; abstract;',
  18591. ' end;',
  18592. ' TBird = class',
  18593. ' procedure Go; override;',
  18594. ' end;',
  18595. ' TCat = class(TObject)',
  18596. ' procedure Go; override;',
  18597. ' end;',
  18598. ' TDog = class(TObject, IUnknown)',
  18599. ' procedure Go; override;',
  18600. ' end;',
  18601. 'procedure TBird.Go; begin end;',
  18602. 'procedure TCat.Go; begin end;',
  18603. 'procedure TDog.Go; begin end;',
  18604. 'begin',
  18605. '']);
  18606. ConvertProgram;
  18607. CheckSource('TestClassInterface_MethodOverride',
  18608. LinesToStr([ // statements
  18609. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18610. 'rtl.createClass(this, "TObject", null, function () {',
  18611. ' this.$init = function () {',
  18612. ' };',
  18613. ' this.$final = function () {',
  18614. ' };',
  18615. ' rtl.addIntf(this, $mod.IUnknown);',
  18616. '});',
  18617. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18618. ' this.Go = function () {',
  18619. ' };',
  18620. ' rtl.addIntf(this, $mod.IUnknown);',
  18621. '});',
  18622. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18623. ' this.Go = function () {',
  18624. ' };',
  18625. ' rtl.addIntf(this, $mod.IUnknown);',
  18626. '});',
  18627. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18628. ' this.Go = function () {',
  18629. ' };',
  18630. ' rtl.addIntf(this, $mod.IUnknown);',
  18631. '});',
  18632. '']),
  18633. LinesToStr([ // $mod.$main
  18634. '']));
  18635. end;
  18636. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18637. begin
  18638. StartProgram(false);
  18639. Add([
  18640. '{$interfaces corba}',
  18641. 'type',
  18642. ' IUnknown = interface',
  18643. ' end;',
  18644. ' IBird = interface(IUnknown)',
  18645. ' procedure Fly(s: string);',
  18646. ' end;',
  18647. ' IEagle = interface(IBird)',
  18648. ' end;',
  18649. ' IDove = interface(IBird)',
  18650. ' end;',
  18651. ' ISwallow = interface(IBird)',
  18652. ' end;',
  18653. ' TObject = class',
  18654. ' end;',
  18655. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18656. ' procedure Fly(s: string); virtual; abstract;',
  18657. ' end;',
  18658. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18659. ' FBirdIntf: IBird;',
  18660. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18661. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18662. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18663. ' FDoveObj: TBird;',
  18664. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18665. ' function GetSwallowObj: TBird; virtual; abstract;',
  18666. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18667. ' end;',
  18668. 'begin',
  18669. '']);
  18670. ConvertProgram;
  18671. CheckSource('TestClassInterface_Corba_Delegation',
  18672. LinesToStr([ // statements
  18673. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18674. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18675. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18676. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18677. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18678. 'rtl.createClass(this, "TObject", null, function () {',
  18679. ' this.$init = function () {',
  18680. ' };',
  18681. ' this.$final = function () {',
  18682. ' };',
  18683. '});',
  18684. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18685. ' rtl.addIntf(this, $mod.IBird);',
  18686. ' rtl.addIntf(this, $mod.IEagle);',
  18687. ' rtl.addIntf(this, $mod.IDove);',
  18688. ' rtl.addIntf(this, $mod.ISwallow);',
  18689. '});',
  18690. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18691. ' this.$init = function () {',
  18692. ' $mod.TObject.$init.call(this);',
  18693. ' this.FBirdIntf = null;',
  18694. ' this.FDoveObj = null;',
  18695. ' };',
  18696. ' this.$final = function () {',
  18697. ' this.FBirdIntf = undefined;',
  18698. ' this.FDoveObj = undefined;',
  18699. ' $mod.TObject.$final.call(this);',
  18700. ' };',
  18701. ' this.$intfmaps = {',
  18702. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18703. ' return this.FBirdIntf;',
  18704. ' },',
  18705. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18706. ' return this.GetEagleIntf();',
  18707. ' },',
  18708. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18709. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18710. ' },',
  18711. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18712. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18713. ' }',
  18714. ' };',
  18715. '});',
  18716. '']),
  18717. LinesToStr([ // $mod.$main
  18718. '']));
  18719. end;
  18720. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18721. begin
  18722. StartProgram(false);
  18723. Add([
  18724. '{$interfaces corba}',
  18725. 'type',
  18726. ' IUnknown = interface',
  18727. ' end;',
  18728. ' IBird = interface(IUnknown)',
  18729. ' procedure Fly(s: string);',
  18730. ' end;',
  18731. ' IEagle = interface(IBird)',
  18732. ' end;',
  18733. ' IDove = interface(IBird)',
  18734. ' end;',
  18735. ' ISwallow = interface(IBird)',
  18736. ' end;',
  18737. ' TObject = class',
  18738. ' end;',
  18739. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18740. ' procedure Fly(s: string); virtual; abstract;',
  18741. ' end;',
  18742. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18743. ' private',
  18744. ' class var FBirdIntf: IBird;',
  18745. ' class var FDoveObj: TBird;',
  18746. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18747. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18748. ' protected',
  18749. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18750. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18751. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18752. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18753. ' end;',
  18754. 'begin',
  18755. '']);
  18756. ConvertProgram;
  18757. CheckSource('TestClassInterface_Corba_DelegationStatic',
  18758. LinesToStr([ // statements
  18759. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18760. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18761. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18762. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18763. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18764. 'rtl.createClass(this, "TObject", null, function () {',
  18765. ' this.$init = function () {',
  18766. ' };',
  18767. ' this.$final = function () {',
  18768. ' };',
  18769. '});',
  18770. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18771. ' rtl.addIntf(this, $mod.IBird);',
  18772. ' rtl.addIntf(this, $mod.IEagle);',
  18773. ' rtl.addIntf(this, $mod.IDove);',
  18774. ' rtl.addIntf(this, $mod.ISwallow);',
  18775. '});',
  18776. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18777. ' this.FBirdIntf = null;',
  18778. ' this.FDoveObj = null;',
  18779. ' this.$intfmaps = {',
  18780. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18781. ' return this.FBirdIntf;',
  18782. ' },',
  18783. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18784. ' return this.GetEagleIntf();',
  18785. ' },',
  18786. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18787. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18788. ' },',
  18789. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18790. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18791. ' }',
  18792. ' };',
  18793. '});',
  18794. '']),
  18795. LinesToStr([ // $mod.$main
  18796. '']));
  18797. end;
  18798. procedure TTestModule.TestClassInterface_Corba_Operators;
  18799. begin
  18800. StartProgram(false);
  18801. Add([
  18802. '{$interfaces corba}',
  18803. 'type',
  18804. ' IUnknown = interface',
  18805. ' end;',
  18806. ' IBird = interface(IUnknown)',
  18807. ' function GetItems(Index: longint): longint;',
  18808. ' procedure SetItems(Index: longint; Value: longint);',
  18809. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18810. ' end;',
  18811. ' TObject = class',
  18812. ' end;',
  18813. ' TBird = class(TObject,IBird)',
  18814. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18815. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18816. ' end;',
  18817. 'var',
  18818. ' IntfVar: IBird = nil;',
  18819. ' IntfVar2: IBird;',
  18820. ' ObjVar: TBird;',
  18821. ' v: JSValue;',
  18822. 'begin',
  18823. ' IntfVar:=nil;',
  18824. ' IntfVar[3]:=IntfVar[4];',
  18825. ' if Assigned(IntfVar) then ;',
  18826. ' IntfVar:=IntfVar2;',
  18827. ' IntfVar:=ObjVar;',
  18828. ' if IntfVar=IntfVar2 then ;',
  18829. ' if IntfVar<>IntfVar2 then ;',
  18830. ' if IntfVar is IBird then ;',
  18831. ' if IntfVar is TBird then ;',
  18832. ' if ObjVar is IBird then ;',
  18833. ' IntfVar:=IntfVar2 as IBird;',
  18834. ' ObjVar:=IntfVar2 as TBird;',
  18835. ' IntfVar:=ObjVar as IBird;',
  18836. ' IntfVar:=IBird(IntfVar2);',
  18837. ' ObjVar:=TBird(IntfVar);',
  18838. ' IntfVar:=IBird(ObjVar);',
  18839. ' v:=IntfVar;',
  18840. ' IntfVar:=IBird(v);',
  18841. ' if v is IBird then ;',
  18842. ' v:=JSValue(IntfVar);',
  18843. ' v:=IBird;',
  18844. '']);
  18845. ConvertProgram;
  18846. CheckSource('TestClassInterface_Corba_Operators',
  18847. LinesToStr([ // statements
  18848. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18849. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  18850. 'rtl.createClass(this, "TObject", null, function () {',
  18851. ' this.$init = function () {',
  18852. ' };',
  18853. ' this.$final = function () {',
  18854. ' };',
  18855. '});',
  18856. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18857. ' rtl.addIntf(this, $mod.IBird);',
  18858. '});',
  18859. 'this.IntfVar = null;',
  18860. 'this.IntfVar2 = null;',
  18861. 'this.ObjVar = null;',
  18862. 'this.v = undefined;',
  18863. '']),
  18864. LinesToStr([ // $mod.$main
  18865. '$mod.IntfVar = null;',
  18866. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18867. 'if ($mod.IntfVar != null) ;',
  18868. '$mod.IntfVar = $mod.IntfVar2;',
  18869. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18870. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18871. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18872. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18873. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18874. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18875. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18876. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18877. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18878. '$mod.IntfVar = $mod.IntfVar2;',
  18879. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18880. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18881. '$mod.v = $mod.IntfVar;',
  18882. '$mod.IntfVar = rtl.getObject($mod.v);',
  18883. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18884. '$mod.v = $mod.IntfVar;',
  18885. '$mod.v = $mod.IBird;',
  18886. '']));
  18887. end;
  18888. procedure TTestModule.TestClassInterface_Corba_Args;
  18889. begin
  18890. StartProgram(false);
  18891. Add([
  18892. '{$interfaces corba}',
  18893. 'type',
  18894. ' IUnknown = interface',
  18895. ' end;',
  18896. ' IBird = interface(IUnknown)',
  18897. ' end;',
  18898. ' TObject = class',
  18899. ' end;',
  18900. ' TBird = class(TObject,IBird)',
  18901. ' end;',
  18902. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18903. 'begin',
  18904. ' DoIt(i,i,i);',
  18905. 'end;',
  18906. 'procedure Change(var i: IBird; out j: IBird);',
  18907. 'begin',
  18908. ' DoIt(i,i,i);',
  18909. ' Change(i,i);',
  18910. 'end;',
  18911. 'var',
  18912. ' i: IBird;',
  18913. ' o: TBird;',
  18914. 'begin',
  18915. ' DoIt(i,i,i);',
  18916. ' Change(i,i);',
  18917. ' DoIt(o,o,o);',
  18918. '']);
  18919. ConvertProgram;
  18920. CheckSource('TestClassInterface_Corba_Args',
  18921. LinesToStr([ // statements
  18922. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18923. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18924. 'rtl.createClass(this, "TObject", null, function () {',
  18925. ' this.$init = function () {',
  18926. ' };',
  18927. ' this.$final = function () {',
  18928. ' };',
  18929. '});',
  18930. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18931. ' rtl.addIntf(this, $mod.IBird);',
  18932. '});',
  18933. 'this.DoIt = function (u, i, j) {',
  18934. ' $mod.DoIt({',
  18935. ' get: function () {',
  18936. ' return i;',
  18937. ' },',
  18938. ' set: function (v) {',
  18939. ' i = v;',
  18940. ' }',
  18941. ' }, i, i);',
  18942. '};',
  18943. 'this.Change = function (i, j) {',
  18944. ' $mod.DoIt(i, i.get(), i.get());',
  18945. ' $mod.Change(i, i);',
  18946. '};',
  18947. 'this.i = null;',
  18948. 'this.o = null;',
  18949. '']),
  18950. LinesToStr([ // $mod.$main
  18951. '$mod.DoIt({',
  18952. ' p: $mod,',
  18953. ' get: function () {',
  18954. ' return this.p.i;',
  18955. ' },',
  18956. ' set: function (v) {',
  18957. ' this.p.i = v;',
  18958. ' }',
  18959. '}, $mod.i, $mod.i);',
  18960. '$mod.Change({',
  18961. ' p: $mod,',
  18962. ' get: function () {',
  18963. ' return this.p.i;',
  18964. ' },',
  18965. ' set: function (v) {',
  18966. ' this.p.i = v;',
  18967. ' }',
  18968. '}, {',
  18969. ' p: $mod,',
  18970. ' get: function () {',
  18971. ' return this.p.i;',
  18972. ' },',
  18973. ' set: function (v) {',
  18974. ' this.p.i = v;',
  18975. ' }',
  18976. '});',
  18977. '$mod.DoIt({',
  18978. ' p: $mod,',
  18979. ' get: function () {',
  18980. ' return this.p.o;',
  18981. ' },',
  18982. ' set: function (v) {',
  18983. ' this.p.o = v;',
  18984. ' }',
  18985. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  18986. '']));
  18987. end;
  18988. procedure TTestModule.TestClassInterface_Corba_ForIn;
  18989. begin
  18990. StartProgram(false);
  18991. Add([
  18992. '{$interfaces corba}',
  18993. 'type',
  18994. ' IUnknown = interface end;',
  18995. ' TObject = class',
  18996. ' Id: longint;',
  18997. ' end;',
  18998. ' IEnumerator = interface(IUnknown)',
  18999. ' function GetCurrent: TObject;',
  19000. ' function MoveNext: Boolean;',
  19001. ' property Current: TObject read GetCurrent;',
  19002. ' end;',
  19003. ' IEnumerable = interface(IUnknown)',
  19004. ' function GetEnumerator: IEnumerator;',
  19005. ' end;',
  19006. 'var',
  19007. ' o: TObject;',
  19008. ' i: IEnumerable;',
  19009. 'begin',
  19010. ' for o in i do o.Id:=3;',
  19011. '']);
  19012. ConvertProgram;
  19013. CheckSource('TestClassInterface_Corba_ForIn',
  19014. LinesToStr([ // statements
  19015. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19016. 'rtl.createClass(this, "TObject", null, function () {',
  19017. ' this.$init = function () {',
  19018. ' this.Id = 0;',
  19019. ' };',
  19020. ' this.$final = function () {',
  19021. ' };',
  19022. '});',
  19023. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19024. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19025. 'this.o = null;',
  19026. 'this.i = null;',
  19027. '']),
  19028. LinesToStr([ // $mod.$main
  19029. 'var $in = $mod.i.GetEnumerator();',
  19030. 'while ($in.MoveNext()) {',
  19031. ' $mod.o = $in.GetCurrent();',
  19032. ' $mod.o.Id = 3;',
  19033. '};',
  19034. '']));
  19035. end;
  19036. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19037. begin
  19038. StartProgram(false);
  19039. Add([
  19040. '{$interfaces com}',
  19041. 'type',
  19042. ' IUnknown = interface',
  19043. ' function _AddRef: longint;',
  19044. ' function _Release: longint;',
  19045. ' end;',
  19046. ' TObject = class(IUnknown)',
  19047. ' function _AddRef: longint; virtual; abstract;',
  19048. ' function _Release: longint; virtual; abstract;',
  19049. ' end;',
  19050. 'var',
  19051. ' i: IUnknown;',
  19052. 'procedure DoGlobal(o: TObject);',
  19053. 'begin',
  19054. ' i:=nil;',
  19055. ' i:=o;',
  19056. ' i:=i;',
  19057. 'end;',
  19058. 'procedure DoLocal(o: TObject);',
  19059. 'const k: IUnknown = nil;',
  19060. 'var j: IUnknown;',
  19061. 'begin',
  19062. ' k:=o;',
  19063. ' k:=i;',
  19064. ' j:=o;',
  19065. ' j:=i;',
  19066. 'end;',
  19067. 'var o: TObject;',
  19068. 'begin',
  19069. ' i:=nil;',
  19070. ' i:=o;',
  19071. '']);
  19072. ConvertProgram;
  19073. CheckSource('TestClassInterface_COM_AssignVar',
  19074. LinesToStr([ // statements
  19075. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19076. 'rtl.createClass(this, "TObject", null, function () {',
  19077. ' this.$init = function () {',
  19078. ' };',
  19079. ' this.$final = function () {',
  19080. ' };',
  19081. ' rtl.addIntf(this, $mod.IUnknown);',
  19082. '});',
  19083. 'this.i = null;',
  19084. 'this.DoGlobal = function (o) {',
  19085. ' rtl.setIntfP($mod, "i", null);',
  19086. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19087. ' rtl.setIntfP($mod, "i", $mod.i);',
  19088. '};',
  19089. 'var k = null;',
  19090. 'this.DoLocal = function (o) {',
  19091. ' var j = null;',
  19092. ' try{',
  19093. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19094. ' k = rtl.setIntfL(k, $mod.i);',
  19095. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19096. ' j = rtl.setIntfL(j, $mod.i);',
  19097. ' }finally{',
  19098. ' rtl._Release(j);',
  19099. ' };',
  19100. '};',
  19101. 'this.o = null;',
  19102. '']),
  19103. LinesToStr([ // $mod.$main
  19104. 'rtl.setIntfP($mod, "i", null);',
  19105. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19106. '']));
  19107. end;
  19108. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19109. begin
  19110. StartProgram(false);
  19111. Add([
  19112. '{$interfaces com}',
  19113. 'type',
  19114. ' IUnknown = interface',
  19115. ' function _AddRef: longint;',
  19116. ' function _Release: longint;',
  19117. ' end;',
  19118. ' TObject = class(IUnknown)',
  19119. ' function _AddRef: longint; virtual; abstract;',
  19120. ' function _Release: longint; virtual; abstract;',
  19121. ' end;',
  19122. 'procedure DoDefault(i, j: IUnknown);',
  19123. 'begin',
  19124. ' i:=nil;',
  19125. ' i:=j;',
  19126. 'end;',
  19127. 'begin',
  19128. '']);
  19129. ConvertProgram;
  19130. CheckSource('TestClassInterface_COM_AssignArg',
  19131. LinesToStr([ // statements
  19132. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19133. 'rtl.createClass(this, "TObject", null, function () {',
  19134. ' this.$init = function () {',
  19135. ' };',
  19136. ' this.$final = function () {',
  19137. ' };',
  19138. ' rtl.addIntf(this, $mod.IUnknown);',
  19139. '});',
  19140. 'this.DoDefault = function (i, j) {',
  19141. ' rtl._AddRef(i);',
  19142. ' try {',
  19143. ' i = rtl.setIntfL(i, null);',
  19144. ' i = rtl.setIntfL(i, j);',
  19145. ' } finally {',
  19146. ' rtl._Release(i);',
  19147. ' };',
  19148. '};',
  19149. '']),
  19150. LinesToStr([ // $mod.$main
  19151. '']));
  19152. end;
  19153. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19154. begin
  19155. StartProgram(false);
  19156. Add([
  19157. '{$interfaces com}',
  19158. 'type',
  19159. ' IUnknown = interface',
  19160. ' function _AddRef: longint;',
  19161. ' function _Release: longint;',
  19162. ' end;',
  19163. ' TObject = class(IUnknown)',
  19164. ' function _AddRef: longint; virtual; abstract;',
  19165. ' function _Release: longint; virtual; abstract;',
  19166. ' end;',
  19167. 'function DoDefault(i: IUnknown): IUnknown;',
  19168. 'begin',
  19169. ' Result:=i;',
  19170. ' if Result<>nil then exit;',
  19171. 'end;',
  19172. 'begin',
  19173. '']);
  19174. ConvertProgram;
  19175. CheckSource('TestClassInterface_COM_FunctionResult',
  19176. LinesToStr([ // statements
  19177. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19178. 'rtl.createClass(this, "TObject", null, function () {',
  19179. ' this.$init = function () {',
  19180. ' };',
  19181. ' this.$final = function () {',
  19182. ' };',
  19183. ' rtl.addIntf(this, $mod.IUnknown);',
  19184. '});',
  19185. 'this.DoDefault = function (i) {',
  19186. ' var Result = null;',
  19187. ' var $ok = false;',
  19188. ' try {',
  19189. ' Result = rtl.setIntfL(Result, i);',
  19190. ' if(Result !== null){',
  19191. ' $ok = true;',
  19192. ' return Result;',
  19193. ' };',
  19194. ' $ok = true;',
  19195. ' } finally {',
  19196. ' if(!$ok) rtl._Release(Result);',
  19197. ' };',
  19198. ' return Result;',
  19199. '};',
  19200. '']),
  19201. LinesToStr([ // $mod.$main
  19202. '']));
  19203. end;
  19204. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19205. begin
  19206. StartProgram(false);
  19207. Add([
  19208. '{$interfaces com}',
  19209. 'type',
  19210. ' IUnknown = interface',
  19211. ' function _AddRef: longint;',
  19212. ' function _Release: longint;',
  19213. ' end;',
  19214. ' TObject = class(IUnknown)',
  19215. ' function _AddRef: longint; virtual; abstract;',
  19216. ' function _Release: longint; virtual; abstract;',
  19217. ' function GetIntf: IUnknown; virtual;',
  19218. ' end;',
  19219. ' TMouse = class',
  19220. ' function GetIntf: IUnknown; override;',
  19221. ' end;',
  19222. 'function TObject.GetIntf: IUnknown; begin end;',
  19223. 'function TMouse.GetIntf: IUnknown;',
  19224. 'var i: IUnknown;',
  19225. 'begin',
  19226. ' inherited;',
  19227. ' inherited GetIntf;',
  19228. ' inherited GetIntf();',
  19229. ' Result:=inherited GetIntf;',
  19230. ' Result:=inherited GetIntf();',
  19231. ' i:=inherited GetIntf;',
  19232. ' i:=inherited GetIntf();',
  19233. 'end;',
  19234. 'begin',
  19235. '']);
  19236. ConvertProgram;
  19237. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19238. LinesToStr([ // statements
  19239. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19240. 'rtl.createClass(this, "TObject", null, function () {',
  19241. ' this.$init = function () {',
  19242. ' };',
  19243. ' this.$final = function () {',
  19244. ' };',
  19245. ' this.GetIntf = function () {',
  19246. ' var Result = null;',
  19247. ' return Result;',
  19248. ' };',
  19249. ' rtl.addIntf(this, $mod.IUnknown);',
  19250. '});',
  19251. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19252. ' this.GetIntf = function () {',
  19253. ' var Result = null;',
  19254. ' var i = null;',
  19255. ' var $ir = rtl.createIntfRefs();',
  19256. ' var $ok = false;',
  19257. ' try {',
  19258. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19259. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19260. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19261. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19262. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19263. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19264. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19265. ' $ok = true;',
  19266. ' } finally {',
  19267. ' $ir.free();',
  19268. ' rtl._Release(i);',
  19269. ' if (!$ok) rtl._Release(Result);',
  19270. ' };',
  19271. ' return Result;',
  19272. ' };',
  19273. ' rtl.addIntf(this, $mod.IUnknown);',
  19274. '});',
  19275. '']),
  19276. LinesToStr([ // $mod.$main
  19277. '']));
  19278. end;
  19279. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19280. begin
  19281. StartProgram(false);
  19282. Add([
  19283. '{$interfaces com}',
  19284. 'type',
  19285. ' IUnknown = interface',
  19286. ' function _AddRef: longint;',
  19287. ' function _Release: longint;',
  19288. ' end;',
  19289. ' TObject = class(IUnknown)',
  19290. ' function _AddRef: longint; virtual; abstract;',
  19291. ' function _Release: longint; virtual; abstract;',
  19292. ' end;',
  19293. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19294. 'begin',
  19295. ' if i is IUnknown then ;',
  19296. ' if o is IUnknown then ;',
  19297. ' if i is TObject then ;',
  19298. ' i:=j as IUnknown;',
  19299. ' i:=o as IUnknown;',
  19300. ' o:=j as TObject;',
  19301. ' i:=IUnknown(j);',
  19302. ' i:=IUnknown(o);',
  19303. ' o:=TObject(i);',
  19304. 'end;',
  19305. 'begin',
  19306. '']);
  19307. ConvertProgram;
  19308. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19309. LinesToStr([ // statements
  19310. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19311. 'rtl.createClass(this, "TObject", null, function () {',
  19312. ' this.$init = function () {',
  19313. ' };',
  19314. ' this.$final = function () {',
  19315. ' };',
  19316. ' rtl.addIntf(this, $mod.IUnknown);',
  19317. '});',
  19318. 'this.DoDefault = function (i, j, o) {',
  19319. ' rtl._AddRef(i);',
  19320. ' try {',
  19321. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19322. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19323. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19324. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19325. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19326. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19327. ' i = rtl.setIntfL(i, j);',
  19328. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19329. ' o = rtl.intfToClass(i, $mod.TObject);',
  19330. ' } finally {',
  19331. ' rtl._Release(i);',
  19332. ' };',
  19333. '};',
  19334. '']),
  19335. LinesToStr([ // $mod.$main
  19336. '']));
  19337. end;
  19338. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19339. begin
  19340. StartProgram(false);
  19341. Add([
  19342. '{$interfaces com}',
  19343. 'type',
  19344. ' IUnknown = interface',
  19345. ' function _AddRef: longint;',
  19346. ' function _Release: longint;',
  19347. ' end;',
  19348. ' TObject = class(IUnknown)',
  19349. ' function _AddRef: longint; virtual; abstract;',
  19350. ' function _Release: longint; virtual; abstract;',
  19351. ' end;',
  19352. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19353. 'var o: TObject;',
  19354. 'begin',
  19355. ' DoIt(v,v,v,v);',
  19356. ' DoIt(o,o,k,k);',
  19357. 'end;',
  19358. 'procedure DoSome;',
  19359. 'var v: IUnknown;',
  19360. 'begin',
  19361. ' DoIt(v,v,v,v);',
  19362. 'end;',
  19363. 'var i: IUnknown;',
  19364. 'begin',
  19365. ' DoIt(i,i,i,i);',
  19366. '']);
  19367. ConvertProgram;
  19368. CheckSource('TestClassInterface_COM_PassAsArg',
  19369. LinesToStr([ // statements
  19370. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19371. 'rtl.createClass(this, "TObject", null, function () {',
  19372. ' this.$init = function () {',
  19373. ' };',
  19374. ' this.$final = function () {',
  19375. ' };',
  19376. ' rtl.addIntf(this, $mod.IUnknown);',
  19377. '});',
  19378. 'this.DoIt = function (v, j, k, l) {',
  19379. ' var o = null;',
  19380. ' var $ir = rtl.createIntfRefs();',
  19381. ' rtl._AddRef(v);',
  19382. ' try {',
  19383. ' $mod.DoIt(v, v, {',
  19384. ' get: function () {',
  19385. ' return v;',
  19386. ' },',
  19387. ' set: function (w) {',
  19388. ' v = rtl.setIntfL(v, w);',
  19389. ' }',
  19390. ' }, {',
  19391. ' get: function () {',
  19392. ' return v;',
  19393. ' },',
  19394. ' set: function (w) {',
  19395. ' v = rtl.setIntfL(v, w);',
  19396. ' }',
  19397. ' });',
  19398. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19399. ' } finally {',
  19400. ' $ir.free();',
  19401. ' rtl._Release(v);',
  19402. ' };',
  19403. '};',
  19404. 'this.DoSome = function () {',
  19405. ' var v = null;',
  19406. ' try {',
  19407. ' $mod.DoIt(v, v, {',
  19408. ' get: function () {',
  19409. ' return v;',
  19410. ' },',
  19411. ' set: function (w) {',
  19412. ' v = rtl.setIntfL(v, w);',
  19413. ' }',
  19414. ' }, {',
  19415. ' get: function () {',
  19416. ' return v;',
  19417. ' },',
  19418. ' set: function (w) {',
  19419. ' v = rtl.setIntfL(v, w);',
  19420. ' }',
  19421. ' });',
  19422. ' } finally {',
  19423. ' rtl._Release(v);',
  19424. ' };',
  19425. '};',
  19426. 'this.i = null;',
  19427. '']),
  19428. LinesToStr([ // $mod.$main
  19429. '$mod.DoIt($mod.i, $mod.i, {',
  19430. ' p: $mod,',
  19431. ' get: function () {',
  19432. ' return this.p.i;',
  19433. ' },',
  19434. ' set: function (v) {',
  19435. ' rtl.setIntfP(this.p, "i", v);',
  19436. ' }',
  19437. '}, {',
  19438. ' p: $mod,',
  19439. ' get: function () {',
  19440. ' return this.p.i;',
  19441. ' },',
  19442. ' set: function (v) {',
  19443. ' rtl.setIntfP(this.p, "i", v);',
  19444. ' }',
  19445. '});',
  19446. '']));
  19447. end;
  19448. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19449. begin
  19450. StartProgram(false);
  19451. Add([
  19452. '{$interfaces com}',
  19453. 'type',
  19454. ' IUnknown = interface',
  19455. ' function _AddRef: longint;',
  19456. ' function _Release: longint;',
  19457. ' end;',
  19458. ' TObject = class(IUnknown)',
  19459. ' function _AddRef: longint; virtual; abstract;',
  19460. ' function _Release: longint; virtual; abstract;',
  19461. ' end;',
  19462. 'procedure DoIt(out i);',
  19463. 'begin end;',
  19464. 'procedure DoSome;',
  19465. 'var v: IUnknown;',
  19466. 'begin',
  19467. ' DoIt(v);',
  19468. 'end;',
  19469. 'function GetIt: IUnknown;',
  19470. 'begin',
  19471. ' DoIt(Result);',
  19472. 'end;',
  19473. 'var i: IUnknown;',
  19474. 'begin',
  19475. ' DoIt(i);',
  19476. '']);
  19477. ConvertProgram;
  19478. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19479. LinesToStr([ // statements
  19480. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19481. 'rtl.createClass(this, "TObject", null, function () {',
  19482. ' this.$init = function () {',
  19483. ' };',
  19484. ' this.$final = function () {',
  19485. ' };',
  19486. ' rtl.addIntf(this, $mod.IUnknown);',
  19487. '});',
  19488. 'this.DoIt = function (i) {',
  19489. '};',
  19490. 'this.DoSome = function () {',
  19491. ' var v = null;',
  19492. ' try {',
  19493. ' $mod.DoIt({',
  19494. ' get: function () {',
  19495. ' return v;',
  19496. ' },',
  19497. ' set: function (w) {',
  19498. ' v = w;',
  19499. ' }',
  19500. ' });',
  19501. ' } finally {',
  19502. ' rtl._Release(v);',
  19503. ' };',
  19504. '};',
  19505. 'this.GetIt = function () {',
  19506. ' var Result = null;',
  19507. ' var $ok = false;',
  19508. ' try {',
  19509. ' $mod.DoIt({',
  19510. ' get: function () {',
  19511. ' return Result;',
  19512. ' },',
  19513. ' set: function (v) {',
  19514. ' Result = v;',
  19515. ' }',
  19516. ' });',
  19517. ' $ok = true;',
  19518. ' } finally {',
  19519. ' if (!$ok) rtl._Release(Result);',
  19520. ' };',
  19521. ' return Result;',
  19522. '};',
  19523. 'this.i = null;',
  19524. '']),
  19525. LinesToStr([ // $mod.$main
  19526. 'try {',
  19527. ' $mod.DoIt({',
  19528. ' p: $mod,',
  19529. ' get: function () {',
  19530. ' return this.p.i;',
  19531. ' },',
  19532. ' set: function (v) {',
  19533. ' this.p.i = v;',
  19534. ' }',
  19535. ' });',
  19536. '} finally {',
  19537. ' rtl._Release($mod.i);',
  19538. '};',
  19539. '']));
  19540. end;
  19541. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19542. begin
  19543. StartProgram(false);
  19544. Add([
  19545. '{$interfaces com}',
  19546. 'type',
  19547. ' IUnknown = interface',
  19548. ' function _AddRef: longint;',
  19549. ' function _Release: longint;',
  19550. ' end;',
  19551. ' TObject = class(IUnknown)',
  19552. ' function _AddRef: longint; virtual; abstract;',
  19553. ' function _Release: longint; virtual; abstract;',
  19554. ' end;',
  19555. 'function GetIt: IUnknown;',
  19556. 'begin',
  19557. 'end;',
  19558. 'procedure DoSome;',
  19559. 'var v: IUnknown;',
  19560. ' i: longint;',
  19561. 'begin',
  19562. ' v:=GetIt;',
  19563. ' v:=GetIt();',
  19564. ' GetIt()._AddRef;',
  19565. ' i:=GetIt()._AddRef;',
  19566. 'end;',
  19567. 'var v: IUnknown;',
  19568. ' i: longint;',
  19569. 'begin',
  19570. ' v:=GetIt;',
  19571. ' v:=GetIt();',
  19572. ' GetIt()._AddRef;',
  19573. ' i:=GetIt()._AddRef;',
  19574. '']);
  19575. ConvertProgram;
  19576. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19577. LinesToStr([ // statements
  19578. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19579. 'rtl.createClass(this, "TObject", null, function () {',
  19580. ' this.$init = function () {',
  19581. ' };',
  19582. ' this.$final = function () {',
  19583. ' };',
  19584. ' rtl.addIntf(this, $mod.IUnknown);',
  19585. '});',
  19586. 'this.GetIt = function () {',
  19587. ' var Result = null;',
  19588. ' return Result;',
  19589. '};',
  19590. 'this.DoSome = function () {',
  19591. ' var v = null;',
  19592. ' var i = 0;',
  19593. ' var $ir = rtl.createIntfRefs();',
  19594. ' try {',
  19595. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19596. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19597. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19598. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19599. ' } finally {',
  19600. ' $ir.free();',
  19601. ' rtl._Release(v);',
  19602. ' };',
  19603. '};',
  19604. 'this.v = null;',
  19605. 'this.i = 0;',
  19606. '']),
  19607. LinesToStr([ // $mod.$main
  19608. 'var $ir = rtl.createIntfRefs();',
  19609. 'try {',
  19610. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19611. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19612. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19613. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19614. '} finally {',
  19615. ' $ir.free();',
  19616. '};',
  19617. '']));
  19618. end;
  19619. procedure TTestModule.TestClassInterface_COM_Property;
  19620. begin
  19621. StartProgram(false);
  19622. Add([
  19623. '{$interfaces com}',
  19624. 'type',
  19625. ' IUnknown = interface',
  19626. ' function _AddRef: longint;',
  19627. ' function _Release: longint;',
  19628. ' end;',
  19629. ' TObject = class(IUnknown)',
  19630. ' FAnt: IUnknown;',
  19631. ' function _AddRef: longint; virtual; abstract;',
  19632. ' function _Release: longint; virtual; abstract;',
  19633. ' function GetBird: IUnknown; virtual; abstract;',
  19634. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19635. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19636. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19637. ' property Ant: IUnknown read FAnt write FAnt;',
  19638. ' property Bird: IUnknown read GetBird write SetBird;',
  19639. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19640. ' end;',
  19641. 'procedure DoIt;',
  19642. 'var',
  19643. ' o: TObject;',
  19644. ' v: IUnknown;',
  19645. 'begin',
  19646. ' v:=o.Ant;',
  19647. ' o.Ant:=v;',
  19648. ' o.Ant:=o.Ant;',
  19649. ' v:=o.Bird;',
  19650. ' o.Bird:=v;',
  19651. ' o.Bird:=o.Bird;',
  19652. ' v:=o.Items[1];',
  19653. ' o.Items[2]:=v;',
  19654. ' o.Items[3]:=o.Items[4];',
  19655. ' v:=o[5];',
  19656. ' o[6]:=v;',
  19657. ' o[7]:=o[8];',
  19658. 'end;',
  19659. 'begin',
  19660. '']);
  19661. ConvertProgram;
  19662. CheckSource('TestClassInterface_COM_Property',
  19663. LinesToStr([ // statements
  19664. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19665. 'rtl.createClass(this, "TObject", null, function () {',
  19666. ' this.$init = function () {',
  19667. ' this.FAnt = null;',
  19668. ' };',
  19669. ' this.$final = function () {',
  19670. ' this.FAnt = undefined;',
  19671. ' };',
  19672. ' rtl.addIntf(this, $mod.IUnknown);',
  19673. '});',
  19674. 'this.DoIt = function () {',
  19675. ' var o = null;',
  19676. ' var v = null;',
  19677. ' var $ir = rtl.createIntfRefs();',
  19678. ' try {',
  19679. ' v = rtl.setIntfL(v, o.FAnt);',
  19680. ' rtl.setIntfP(o, "FAnt", v);',
  19681. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19682. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19683. ' o.SetBird(v);',
  19684. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19685. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19686. ' o.SetItems(2, v);',
  19687. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19688. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19689. ' o.SetItems(6, v);',
  19690. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19691. ' } finally {',
  19692. ' $ir.free();',
  19693. ' rtl._Release(v);',
  19694. ' };',
  19695. '};',
  19696. '']),
  19697. LinesToStr([ // $mod.$main
  19698. '']));
  19699. end;
  19700. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19701. begin
  19702. StartProgram(false);
  19703. Add([
  19704. '{$interfaces com}',
  19705. 'type',
  19706. ' IUnknown = interface',
  19707. ' function _AddRef: longint;',
  19708. ' function _Release: longint;',
  19709. ' function GetBird: IUnknown;',
  19710. ' procedure SetBird(Value: IUnknown);',
  19711. ' function GetItems(Index: longint): IUnknown;',
  19712. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19713. ' property Bird: IUnknown read GetBird write SetBird;',
  19714. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19715. ' end;',
  19716. ' TObject = class(IUnknown)',
  19717. ' function _AddRef: longint; virtual; abstract;',
  19718. ' function _Release: longint; virtual; abstract;',
  19719. ' function GetBird: IUnknown; virtual; abstract;',
  19720. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19721. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19722. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19723. ' end;',
  19724. 'procedure DoIt;',
  19725. 'var',
  19726. ' o: TObject;',
  19727. ' v: IUnknown;',
  19728. 'begin',
  19729. ' v:=v.Items[1];',
  19730. ' v.Items[2]:=v;',
  19731. ' v.Items[3]:=v.Items[4];',
  19732. ' v:=v[5];',
  19733. ' v[6]:=v;',
  19734. ' v[7]:=v[8];',
  19735. ' v[9].Bird.Bird:=v;',
  19736. ' v:=v.Bird[10].Bird',
  19737. 'end;',
  19738. 'begin',
  19739. '']);
  19740. ConvertProgram;
  19741. CheckSource('TestClassInterface_COM_IntfProperty',
  19742. LinesToStr([ // statements
  19743. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19744. ' "_AddRef",',
  19745. ' "_Release",',
  19746. ' "GetBird",',
  19747. ' "SetBird",',
  19748. ' "GetItems",',
  19749. ' "SetItems"',
  19750. '], null);',
  19751. 'rtl.createClass(this, "TObject", null, function () {',
  19752. ' this.$init = function () {',
  19753. ' };',
  19754. ' this.$final = function () {',
  19755. ' };',
  19756. ' rtl.addIntf(this, $mod.IUnknown);',
  19757. '});',
  19758. 'this.DoIt = function () {',
  19759. ' var o = null;',
  19760. ' var v = null;',
  19761. ' var $ir = rtl.createIntfRefs();',
  19762. ' try {',
  19763. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19764. ' v.SetItems(2, v);',
  19765. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19766. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19767. ' v.SetItems(6, v);',
  19768. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19769. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19770. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19771. ' } finally {',
  19772. ' $ir.free();',
  19773. ' rtl._Release(v);',
  19774. ' };',
  19775. '};',
  19776. '']),
  19777. LinesToStr([ // $mod.$main
  19778. '']));
  19779. end;
  19780. procedure TTestModule.TestClassInterface_COM_Delegation;
  19781. begin
  19782. StartProgram(false);
  19783. Add([
  19784. '{$interfaces com}',
  19785. 'type',
  19786. ' IUnknown = interface',
  19787. ' function _AddRef: longint;',
  19788. ' function _Release: longint;',
  19789. ' end;',
  19790. ' IBird = interface(IUnknown)',
  19791. ' procedure Fly(s: string);',
  19792. ' end;',
  19793. ' IEagle = interface(IBird) end;',
  19794. ' IDove = interface(IBird) end;',
  19795. ' ISwallow = interface(IBird) end;',
  19796. ' TObject = class',
  19797. ' end;',
  19798. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19799. ' function _AddRef: longint; virtual; abstract;',
  19800. ' function _Release: longint; virtual; abstract;',
  19801. ' procedure Fly(s: string); virtual; abstract;',
  19802. ' end;',
  19803. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19804. ' function _AddRef: longint; virtual; abstract;',
  19805. ' function _Release: longint; virtual; abstract;',
  19806. ' FBirdIntf: IBird;',
  19807. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19808. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19809. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19810. ' FDoveObj: TBird;',
  19811. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19812. ' function GetSwallowObj: TBird; virtual; abstract;',
  19813. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19814. ' end;',
  19815. 'begin',
  19816. '']);
  19817. ConvertProgram;
  19818. CheckSource('TestClassInterface_COM_Delegation',
  19819. LinesToStr([ // statements
  19820. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19821. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  19822. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  19823. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  19824. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  19825. 'rtl.createClass(this, "TObject", null, function () {',
  19826. ' this.$init = function () {',
  19827. ' };',
  19828. ' this.$final = function () {',
  19829. ' };',
  19830. '});',
  19831. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19832. ' rtl.addIntf(this, $mod.IBird);',
  19833. ' rtl.addIntf(this, $mod.IEagle);',
  19834. ' rtl.addIntf(this, $mod.IDove);',
  19835. ' rtl.addIntf(this, $mod.ISwallow);',
  19836. '});',
  19837. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19838. ' this.$init = function () {',
  19839. ' $mod.TObject.$init.call(this);',
  19840. ' this.FBirdIntf = null;',
  19841. ' this.FDoveObj = null;',
  19842. ' };',
  19843. ' this.$final = function () {',
  19844. ' this.FBirdIntf = undefined;',
  19845. ' this.FDoveObj = undefined;',
  19846. ' $mod.TObject.$final.call(this);',
  19847. ' };',
  19848. ' this.$intfmaps = {',
  19849. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19850. ' return rtl._AddRef(this.FBirdIntf);',
  19851. ' },',
  19852. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19853. ' return this.GetEagleIntf();',
  19854. ' },',
  19855. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19856. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19857. ' },',
  19858. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19859. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19860. ' }',
  19861. ' };',
  19862. '});',
  19863. '']),
  19864. LinesToStr([ // $mod.$main
  19865. '']));
  19866. end;
  19867. procedure TTestModule.TestClassInterface_COM_With;
  19868. begin
  19869. StartProgram(false);
  19870. Add([
  19871. '{$interfaces com}',
  19872. 'type',
  19873. ' IUnknown = interface',
  19874. ' function _AddRef: longint;',
  19875. ' function _Release: longint;',
  19876. ' function GetAnt: IUnknown;',
  19877. ' property Ant: IUnknown read GetAnt;',
  19878. ' end;',
  19879. ' TObject = class(IUnknown)',
  19880. ' function _AddRef: longint; virtual; abstract;',
  19881. ' function _Release: longint; virtual; abstract;',
  19882. ' function GetAnt: IUnknown; virtual; abstract;',
  19883. ' property Ant: IUnknown read GetAnt;',
  19884. ' end;',
  19885. 'procedure DoIt;',
  19886. 'var',
  19887. ' i: IUnknown;',
  19888. 'begin',
  19889. ' with i do ',
  19890. ' GetAnt;',
  19891. ' with i.Ant, Ant do ',
  19892. ' GetAnt;',
  19893. 'end;',
  19894. 'begin',
  19895. '']);
  19896. ConvertProgram;
  19897. CheckSource('TestClassInterface_COM_With',
  19898. LinesToStr([ // statements
  19899. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19900. 'rtl.createClass(this, "TObject", null, function () {',
  19901. ' this.$init = function () {',
  19902. ' };',
  19903. ' this.$final = function () {',
  19904. ' };',
  19905. ' rtl.addIntf(this, $mod.IUnknown);',
  19906. '});',
  19907. 'this.DoIt = function () {',
  19908. ' var i = null;',
  19909. ' var $ir = rtl.createIntfRefs();',
  19910. ' try {',
  19911. ' $ir.ref(1, i.GetAnt());',
  19912. ' var $with = $ir.ref(2, i.GetAnt());',
  19913. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  19914. ' $ir.ref(4, $with1.GetAnt());',
  19915. ' } finally {',
  19916. ' $ir.free();',
  19917. ' };',
  19918. '};',
  19919. '']),
  19920. LinesToStr([ // $mod.$main
  19921. '']));
  19922. end;
  19923. procedure TTestModule.TestClassInterface_COM_ForIn;
  19924. begin
  19925. StartProgram(false);
  19926. Add([
  19927. '{$interfaces com}',
  19928. 'type',
  19929. ' IUnknown = interface end;',
  19930. ' TObject = class',
  19931. ' Id: longint;',
  19932. ' end;',
  19933. ' IEnumerator = interface(IUnknown)',
  19934. ' function GetCurrent: TObject;',
  19935. ' function MoveNext: Boolean;',
  19936. ' property Current: TObject read GetCurrent;',
  19937. ' end;',
  19938. ' IEnumerable = interface(IUnknown)',
  19939. ' function GetEnumerator: IEnumerator;',
  19940. ' end;',
  19941. 'var',
  19942. ' o: TObject;',
  19943. ' i: IEnumerable;',
  19944. 'begin',
  19945. ' for o in i do o.Id:=3;',
  19946. '']);
  19947. ConvertProgram;
  19948. CheckSource('TestClassInterface_COM_ForIn',
  19949. LinesToStr([ // statements
  19950. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19951. 'rtl.createClass(this, "TObject", null, function () {',
  19952. ' this.$init = function () {',
  19953. ' this.Id = 0;',
  19954. ' };',
  19955. ' this.$final = function () {',
  19956. ' };',
  19957. '});',
  19958. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19959. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19960. 'this.o = null;',
  19961. 'this.i = null;',
  19962. '']),
  19963. LinesToStr([ // $mod.$main
  19964. 'var $in = $mod.i.GetEnumerator();',
  19965. 'try {',
  19966. ' while ($in.MoveNext()) {',
  19967. ' $mod.o = $in.GetCurrent();',
  19968. ' $mod.o.Id = 3;',
  19969. ' }',
  19970. '} finally {',
  19971. ' rtl._Release($in)',
  19972. '};',
  19973. '']));
  19974. end;
  19975. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  19976. begin
  19977. StartProgram(false);
  19978. Add([
  19979. '{$interfaces com}',
  19980. 'type',
  19981. ' IUnknown = interface',
  19982. ' function _AddRef: longint;',
  19983. ' function _Release: longint;',
  19984. ' end;',
  19985. ' TObject = class',
  19986. ' end;',
  19987. ' TArrOfIntf = array of IUnknown;',
  19988. 'begin',
  19989. '']);
  19990. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  19991. ConvertProgram;
  19992. end;
  19993. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  19994. begin
  19995. StartProgram(false);
  19996. Add([
  19997. '{$interfaces com}',
  19998. 'type',
  19999. ' IUnknown = interface',
  20000. ' function _AddRef: longint;',
  20001. ' function _Release: longint;',
  20002. ' end;',
  20003. ' TRec = record',
  20004. ' i: IUnknown;',
  20005. ' end;',
  20006. 'begin',
  20007. '']);
  20008. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20009. ConvertProgram;
  20010. end;
  20011. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20012. begin
  20013. StartUnit(false);
  20014. Add([
  20015. '{$interfaces com}',
  20016. 'interface',
  20017. 'implementation',
  20018. 'type',
  20019. ' IUnknown = interface',
  20020. ' function _AddRef: longint;',
  20021. ' end;',
  20022. ' TObject = class(IUnknown)',
  20023. ' function _AddRef: longint;',
  20024. ' end;',
  20025. 'function TObject._AddRef: longint; begin end;',
  20026. 'var i: IUnknown;',
  20027. ' o: TObject;',
  20028. 'initialization',
  20029. ' i:=nil;',
  20030. ' i:=i;',
  20031. ' i:=o;',
  20032. ' if (o as IUnknown)=nil then ;',
  20033. '']);
  20034. ConvertUnit;
  20035. CheckSource('TestClassInterface_COM_UnitInitialization',
  20036. LinesToStr([ // statements
  20037. 'var $impl = $mod.$impl;',
  20038. '']),
  20039. LinesToStr([ // this.$init
  20040. 'var $ir = rtl.createIntfRefs();',
  20041. 'try {',
  20042. ' rtl.setIntfP($impl, "i", null);',
  20043. ' rtl.setIntfP($impl, "i", $impl.i);',
  20044. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20045. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20046. '} finally {',
  20047. ' $ir.free();',
  20048. '};',
  20049. '']),
  20050. LinesToStr([ // implementation
  20051. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20052. 'rtl.createClass($impl, "TObject", null, function () {',
  20053. ' this.$init = function () {',
  20054. ' };',
  20055. ' this.$final = function () {',
  20056. ' };',
  20057. ' this._AddRef = function () {',
  20058. ' var Result = 0;',
  20059. ' return Result;',
  20060. ' };',
  20061. ' rtl.addIntf(this, $impl.IUnknown);',
  20062. '});',
  20063. '$impl.i = null;',
  20064. '$impl.o = null;',
  20065. ''])
  20066. );
  20067. end;
  20068. procedure TTestModule.TestClassInterface_GUID;
  20069. begin
  20070. StartProgram(false);
  20071. Add([
  20072. '{$interfaces corba}',
  20073. 'type',
  20074. ' IUnknown = interface',
  20075. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20076. ' end;',
  20077. ' TObject = class end;',
  20078. ' TGUID = record D1, D2, D3, D4: word; end;',
  20079. ' TAliasGUID = TGUID;',
  20080. ' TGUIDString = type string;',
  20081. ' TAliasGUIDString = TGUIDString;',
  20082. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20083. 'begin end;',
  20084. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20085. 'begin end;',
  20086. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20087. 'begin end;',
  20088. 'var',
  20089. ' i: IUnknown;',
  20090. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20091. ' s: TAliasGUIDString;',
  20092. 'begin',
  20093. ' DoConstGUIDIt(IUnknown);',
  20094. ' DoDefGUID(IUnknown);',
  20095. ' DoStr(IUnknown);',
  20096. ' DoConstGUIDIt(i);',
  20097. ' DoDefGUID(i);',
  20098. ' DoStr(i);',
  20099. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20100. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20101. ' DoStr(g);',
  20102. ' g:=i;',
  20103. ' g:=IUnknown;',
  20104. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20105. ' s:=i;',
  20106. ' s:=IUnknown;',
  20107. ' s:=g;',
  20108. ' if g=i then ;',
  20109. ' if i=g then ;',
  20110. ' if g=IUnknown then ;',
  20111. ' if IUnknown=g then ;',
  20112. ' if s=i then ;',
  20113. ' if i=s then ;',
  20114. ' if s=IUnknown then ;',
  20115. ' if IUnknown=s then ;',
  20116. ' if s=g then ;',
  20117. ' if g=s then ;',
  20118. '']);
  20119. ConvertProgram;
  20120. CheckSource('TestClassInterface_GUID',
  20121. LinesToStr([ // statements
  20122. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20123. 'rtl.createClass(this, "TObject", null, function () {',
  20124. ' this.$init = function () {',
  20125. ' };',
  20126. ' this.$final = function () {',
  20127. ' };',
  20128. '});',
  20129. 'rtl.recNewT(this, "TGUID", function () {',
  20130. ' this.D1 = 0;',
  20131. ' this.D2 = 0;',
  20132. ' this.D3 = 0;',
  20133. ' this.D4 = 0;',
  20134. ' this.$eq = function (b) {',
  20135. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20136. ' };',
  20137. ' this.$assign = function (s) {',
  20138. ' this.D1 = s.D1;',
  20139. ' this.D2 = s.D2;',
  20140. ' this.D3 = s.D3;',
  20141. ' this.D4 = s.D4;',
  20142. ' return this;',
  20143. ' };',
  20144. '});',
  20145. 'this.DoConstGUIDIt = function (g) {',
  20146. '};',
  20147. 'this.DoDefGUID = function (g) {',
  20148. '};',
  20149. 'this.DoStr = function (s) {',
  20150. '};',
  20151. 'this.i = null;',
  20152. 'this.g = this.TGUID.$clone({',
  20153. ' D1: 0xD91C9AF4,',
  20154. ' D2: 0x3C93,',
  20155. ' D3: 0x420F,',
  20156. ' D4: [',
  20157. ' 0xA3,',
  20158. ' 0x03,',
  20159. ' 0xBF,',
  20160. ' 0x5B,',
  20161. ' 0xA8,',
  20162. ' 0x2B,',
  20163. ' 0xFD,',
  20164. ' 0x23',
  20165. ' ]',
  20166. '});',
  20167. 'this.s = "";',
  20168. '']),
  20169. LinesToStr([ // $mod.$main
  20170. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20171. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20172. '$mod.DoStr($mod.IUnknown.$guid);',
  20173. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20174. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20175. '$mod.DoStr($mod.i.$guid);',
  20176. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20177. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20178. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20179. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20180. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20181. '$mod.g.$assign({',
  20182. ' D1: 0xD91C9AF4,',
  20183. ' D2: 0x3C93,',
  20184. ' D3: 0x420F,',
  20185. ' D4: [',
  20186. ' 0xA3,',
  20187. ' 0x03,',
  20188. ' 0xBF,',
  20189. ' 0x5B,',
  20190. ' 0xA8,',
  20191. ' 0x2B,',
  20192. ' 0xFD,',
  20193. ' 0x23',
  20194. ' ]',
  20195. '});',
  20196. '$mod.s = $mod.i.$guid;',
  20197. '$mod.s = $mod.IUnknown.$guid;',
  20198. '$mod.s = rtl.guidrToStr($mod.g);',
  20199. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20200. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20201. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20202. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20203. 'if ($mod.s === $mod.i.$guid) ;',
  20204. 'if ($mod.i.$guid === $mod.s) ;',
  20205. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20206. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20207. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20208. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20209. '']));
  20210. end;
  20211. procedure TTestModule.TestClassInterface_GUIDProperty;
  20212. begin
  20213. StartProgram(false);
  20214. Add([
  20215. '{$interfaces corba}',
  20216. 'type',
  20217. ' IUnknown = interface',
  20218. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20219. ' end;',
  20220. ' TGUID = record D1, D2, D3, D4: word; end;',
  20221. ' TAliasGUID = TGUID;',
  20222. ' TGUIDString = type string;',
  20223. ' TAliasGUIDString = TGUIDString;',
  20224. ' TObject = class',
  20225. ' function GetG: TAliasGUID; virtual; abstract;',
  20226. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20227. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20228. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20229. ' property g: TAliasGUID read GetG write SetG;',
  20230. ' property s: TAliasGUIDString read GetS write SetS;',
  20231. ' end;',
  20232. 'var o: TObject;',
  20233. 'begin',
  20234. ' o.g:=IUnknown;',
  20235. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20236. ' o.s:=IUnknown;',
  20237. ' o.s:=o.g;',
  20238. '']);
  20239. ConvertProgram;
  20240. CheckSource('TestClassInterface_GUIDProperty',
  20241. LinesToStr([ // statements
  20242. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20243. 'rtl.recNewT(this, "TGUID", function () {',
  20244. ' this.D1 = 0;',
  20245. ' this.D2 = 0;',
  20246. ' this.D3 = 0;',
  20247. ' this.D4 = 0;',
  20248. ' this.$eq = function (b) {',
  20249. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20250. ' };',
  20251. ' this.$assign = function (s) {',
  20252. ' this.D1 = s.D1;',
  20253. ' this.D2 = s.D2;',
  20254. ' this.D3 = s.D3;',
  20255. ' this.D4 = s.D4;',
  20256. ' return this;',
  20257. ' };',
  20258. '});',
  20259. 'rtl.createClass(this, "TObject", null, function () {',
  20260. ' this.$init = function () {',
  20261. ' };',
  20262. ' this.$final = function () {',
  20263. ' };',
  20264. '});',
  20265. 'this.o = null;',
  20266. '']),
  20267. LinesToStr([ // $mod.$main
  20268. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20269. '$mod.o.SetG({',
  20270. ' D1: 0xD91C9AF4,',
  20271. ' D2: 0x3C93,',
  20272. ' D3: 0x420F,',
  20273. ' D4: [',
  20274. ' 0xA3,',
  20275. ' 0x03,',
  20276. ' 0xBF,',
  20277. ' 0x5B,',
  20278. ' 0xA8,',
  20279. ' 0x2B,',
  20280. ' 0xFD,',
  20281. ' 0x23',
  20282. ' ]',
  20283. '});',
  20284. '$mod.o.SetS($mod.IUnknown.$guid);',
  20285. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20286. '']));
  20287. end;
  20288. procedure TTestModule.TestClassHelper_ClassVar;
  20289. begin
  20290. StartProgram(false);
  20291. Add([
  20292. 'type',
  20293. ' TObject = class',
  20294. ' end;',
  20295. ' THelper = class helper for TObject',
  20296. ' const',
  20297. ' One = 1;',
  20298. ' Two: word = 2;',
  20299. ' class var',
  20300. ' Glob: word;',
  20301. ' function Foo(w: word): word;',
  20302. ' class function Bar(w: word): word;',
  20303. ' end;',
  20304. 'function THelper.foo(w: word): word;',
  20305. 'begin',
  20306. ' Result:=w;',
  20307. ' Two:=One+w;',
  20308. ' Glob:=Glob;',
  20309. ' Result:=Self.Glob;',
  20310. ' Self.Glob:=Self.Glob;',
  20311. ' with Self do Glob:=Glob;',
  20312. 'end;',
  20313. 'class function THelper.bar(w: word): word;',
  20314. 'begin',
  20315. ' Result:=w;',
  20316. ' Two:=One;',
  20317. ' Glob:=Glob;',
  20318. ' Self.Glob:=Self.Glob;',
  20319. ' with Self do Glob:=Glob;',
  20320. 'end;',
  20321. 'var o: TObject;',
  20322. 'begin',
  20323. ' tobject.two:=tobject.one;',
  20324. ' tobject.Glob:=tobject.Glob;',
  20325. ' with tobject do begin',
  20326. ' two:=one;',
  20327. ' Glob:=Glob;',
  20328. ' end;',
  20329. ' o.two:=o.one;',
  20330. ' o.Glob:=o.Glob;',
  20331. ' with o do begin',
  20332. ' two:=one;',
  20333. ' Glob:=Glob;',
  20334. ' end;',
  20335. '']);
  20336. ConvertProgram;
  20337. CheckSource('TestClassHelper_ClassVar',
  20338. LinesToStr([ // statements
  20339. 'rtl.createClass(this, "TObject", null, function () {',
  20340. ' this.$init = function () {',
  20341. ' };',
  20342. ' this.$final = function () {',
  20343. ' };',
  20344. '});',
  20345. 'rtl.createHelper(this, "THelper", null, function () {',
  20346. ' this.One = 1;',
  20347. ' this.Two = 2;',
  20348. ' this.Glob = 0;',
  20349. ' this.Foo = function (w) {',
  20350. ' var Result = 0;',
  20351. ' Result = w;',
  20352. ' $mod.THelper.Two = 1 + w;',
  20353. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20354. ' Result = $mod.THelper.Glob;',
  20355. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20356. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20357. ' return Result;',
  20358. ' };',
  20359. ' this.Bar = function (w) {',
  20360. ' var Result = 0;',
  20361. ' Result = w;',
  20362. ' $mod.THelper.Two = 1;',
  20363. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20364. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20365. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20366. ' return Result;',
  20367. ' };',
  20368. '});',
  20369. 'this.o = null;',
  20370. '']),
  20371. LinesToStr([ // $mod.$main
  20372. '$mod.THelper.Two = 1;',
  20373. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20374. 'var $with = $mod.TObject;',
  20375. '$mod.THelper.Two = 1;',
  20376. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20377. '$mod.THelper.Two = 1;',
  20378. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20379. 'var $with1 = $mod.o;',
  20380. '$mod.THelper.Two = 1;',
  20381. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20382. '']));
  20383. end;
  20384. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20385. begin
  20386. StartProgram(false);
  20387. Add([
  20388. 'type',
  20389. ' TObject = class',
  20390. ' FSize: word;',
  20391. ' property Size: word read FSize write FSize;',
  20392. ' end;',
  20393. ' THelper = class helper for TObject',
  20394. ' function Foo(w: word = 1): word;',
  20395. ' end;',
  20396. 'function THelper.foo(w: word): word;',
  20397. 'begin',
  20398. ' Result:=Size;',
  20399. ' Size:=Size+2;',
  20400. ' Self.Size:=Self.Size+3;',
  20401. ' FSize:=FSize+4;',
  20402. ' Self.FSize:=Self.FSize+5;',
  20403. ' with Self do begin',
  20404. ' Size:=Size+6;',
  20405. ' FSize:=FSize+7;',
  20406. ' FSize:=FSize+8;',
  20407. ' end;',
  20408. 'end;',
  20409. 'begin',
  20410. '']);
  20411. ConvertProgram;
  20412. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20413. LinesToStr([ // statements
  20414. 'rtl.createClass(this, "TObject", null, function () {',
  20415. ' this.$init = function () {',
  20416. ' this.FSize = 0;',
  20417. ' };',
  20418. ' this.$final = function () {',
  20419. ' };',
  20420. '});',
  20421. 'rtl.createHelper(this, "THelper", null, function () {',
  20422. ' this.Foo = function (w) {',
  20423. ' var Result = 0;',
  20424. ' Result = this.FSize;',
  20425. ' this.FSize = this.FSize + 2;',
  20426. ' this.FSize = this.FSize + 3;',
  20427. ' this.FSize = this.FSize + 4;',
  20428. ' this.FSize = this.FSize + 5;',
  20429. ' this.FSize = this.FSize + 6;',
  20430. ' this.FSize = this.FSize + 7;',
  20431. ' this.FSize = this.FSize + 8;',
  20432. ' return Result;',
  20433. ' };',
  20434. '});',
  20435. '']),
  20436. LinesToStr([ // $mod.$main
  20437. '']));
  20438. end;
  20439. procedure TTestModule.TestClassHelper_Method_Call;
  20440. begin
  20441. StartProgram(false);
  20442. Add([
  20443. 'type',
  20444. ' TObject = class',
  20445. ' procedure Run(w: word = 10);',
  20446. ' end;',
  20447. ' THelper = class helper for TObject',
  20448. ' function Foo(w: word = 1): word;',
  20449. ' end;',
  20450. 'procedure TObject.Run(w: word);',
  20451. 'var o: TObject;',
  20452. 'begin',
  20453. ' Foo;',
  20454. ' Foo();',
  20455. ' Foo(2);',
  20456. ' Self.Foo;',
  20457. ' Self.Foo();',
  20458. ' Self.Foo(3);',
  20459. ' with Self do begin',
  20460. ' Foo;',
  20461. ' Foo();',
  20462. ' Foo(4);',
  20463. ' end;',
  20464. ' with o do Foo(5);',
  20465. 'end;',
  20466. 'function THelper.foo(w: word): word;',
  20467. 'begin',
  20468. ' Run;',
  20469. ' Run();',
  20470. ' Run(11);',
  20471. ' Foo;',
  20472. ' Foo();',
  20473. ' Foo(12);',
  20474. ' Self.Foo;',
  20475. ' Self.Foo();',
  20476. ' Self.Foo(13);',
  20477. ' with Self do begin',
  20478. ' Foo;',
  20479. ' Foo();',
  20480. ' Foo(14);',
  20481. ' end;',
  20482. 'end;',
  20483. 'var Obj: TObject;',
  20484. 'begin',
  20485. ' obj.Foo;',
  20486. ' obj.Foo();',
  20487. ' obj.Foo(21);',
  20488. ' with obj do begin',
  20489. ' Foo;',
  20490. ' Foo();',
  20491. ' Foo(22);',
  20492. ' end;',
  20493. '']);
  20494. ConvertProgram;
  20495. CheckSource('TestClassHelper_Method_Call',
  20496. LinesToStr([ // statements
  20497. 'rtl.createClass(this, "TObject", null, function () {',
  20498. ' this.$init = function () {',
  20499. ' };',
  20500. ' this.$final = function () {',
  20501. ' };',
  20502. ' this.Run = function (w) {',
  20503. ' var o = null;',
  20504. ' $mod.THelper.Foo.call(this, 1);',
  20505. ' $mod.THelper.Foo.call(this, 1);',
  20506. ' $mod.THelper.Foo.call(this, 2);',
  20507. ' $mod.THelper.Foo.call(this, 1);',
  20508. ' $mod.THelper.Foo.call(this, 1);',
  20509. ' $mod.THelper.Foo.call(this, 3);',
  20510. ' $mod.THelper.Foo.call(this, 1);',
  20511. ' $mod.THelper.Foo.call(this, 1);',
  20512. ' $mod.THelper.Foo.call(this, 4);',
  20513. ' $mod.THelper.Foo.call(o, 5);',
  20514. ' };',
  20515. '});',
  20516. 'rtl.createHelper(this, "THelper", null, function () {',
  20517. ' this.Foo = function (w) {',
  20518. ' var Result = 0;',
  20519. ' this.Run(10);',
  20520. ' this.Run(10);',
  20521. ' this.Run(11);',
  20522. ' $mod.THelper.Foo.call(this, 1);',
  20523. ' $mod.THelper.Foo.call(this, 1);',
  20524. ' $mod.THelper.Foo.call(this, 12);',
  20525. ' $mod.THelper.Foo.call(this, 1);',
  20526. ' $mod.THelper.Foo.call(this, 1);',
  20527. ' $mod.THelper.Foo.call(this, 13);',
  20528. ' $mod.THelper.Foo.call(this, 1);',
  20529. ' $mod.THelper.Foo.call(this, 1);',
  20530. ' $mod.THelper.Foo.call(this, 14);',
  20531. ' return Result;',
  20532. ' };',
  20533. '});',
  20534. 'this.Obj = null;',
  20535. '']),
  20536. LinesToStr([ // $mod.$main
  20537. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20538. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20539. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20540. 'var $with = $mod.Obj;',
  20541. '$mod.THelper.Foo.call($with, 1);',
  20542. '$mod.THelper.Foo.call($with, 1);',
  20543. '$mod.THelper.Foo.call($with, 22);',
  20544. '']));
  20545. end;
  20546. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20547. begin
  20548. StartProgram(false);
  20549. Add([
  20550. 'type',
  20551. ' TObject = class',
  20552. ' procedure Run(w: word = 10);',
  20553. ' end;',
  20554. ' THelper = class helper for TObject',
  20555. ' function Foo(w: word = 1): word;',
  20556. ' end;',
  20557. 'procedure TObject.Run(w: word);',
  20558. ' procedure Sub(Self: TObject);',
  20559. ' begin',
  20560. ' Foo;',
  20561. ' Foo();',
  20562. ' Self.Foo;',
  20563. ' Self.Foo();',
  20564. ' with Self do begin',
  20565. ' Foo;',
  20566. ' Foo();',
  20567. ' end;',
  20568. ' end;',
  20569. 'begin',
  20570. 'end;',
  20571. 'function THelper.foo(w: word): word;',
  20572. ' procedure Sub(Self: TObject);',
  20573. ' begin',
  20574. ' Run;',
  20575. ' Run();',
  20576. ' Foo;',
  20577. ' Foo();',
  20578. ' Self.Foo;',
  20579. ' Self.Foo();',
  20580. ' with Self do begin',
  20581. ' Foo;',
  20582. ' Foo();',
  20583. ' end;',
  20584. ' end;',
  20585. 'begin',
  20586. 'end;',
  20587. 'begin',
  20588. '']);
  20589. ConvertProgram;
  20590. CheckSource('TestClassHelper_Method_Nested_Call',
  20591. LinesToStr([ // statements
  20592. 'rtl.createClass(this, "TObject", null, function () {',
  20593. ' this.$init = function () {',
  20594. ' };',
  20595. ' this.$final = function () {',
  20596. ' };',
  20597. ' this.Run = function (w) {',
  20598. ' var $Self = this;',
  20599. ' function Sub(Self) {',
  20600. ' $mod.THelper.Foo.call($Self, 1);',
  20601. ' $mod.THelper.Foo.call($Self, 1);',
  20602. ' $mod.THelper.Foo.call(Self, 1);',
  20603. ' $mod.THelper.Foo.call(Self, 1);',
  20604. ' $mod.THelper.Foo.call(Self, 1);',
  20605. ' $mod.THelper.Foo.call(Self, 1);',
  20606. ' };',
  20607. ' };',
  20608. '});',
  20609. 'rtl.createHelper(this, "THelper", null, function () {',
  20610. ' this.Foo = function (w) {',
  20611. ' var $Self = this;',
  20612. ' var Result = 0;',
  20613. ' function Sub(Self) {',
  20614. ' $Self.Run(10);',
  20615. ' $Self.Run(10);',
  20616. ' $mod.THelper.Foo.call($Self, 1);',
  20617. ' $mod.THelper.Foo.call($Self, 1);',
  20618. ' $mod.THelper.Foo.call(Self, 1);',
  20619. ' $mod.THelper.Foo.call(Self, 1);',
  20620. ' $mod.THelper.Foo.call(Self, 1);',
  20621. ' $mod.THelper.Foo.call(Self, 1);',
  20622. ' };',
  20623. ' return Result;',
  20624. ' };',
  20625. '});',
  20626. '']),
  20627. LinesToStr([ // $mod.$main
  20628. '']));
  20629. end;
  20630. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20631. begin
  20632. StartProgram(false);
  20633. Add([
  20634. 'type',
  20635. ' TObject = class',
  20636. ' class procedure Run(w: word = 10);',
  20637. ' end;',
  20638. ' THelper = class helper for TObject',
  20639. ' class function Foo(w: word = 1): word;',
  20640. ' end;',
  20641. 'class procedure TObject.Run(w: word);',
  20642. 'begin',
  20643. ' Foo;',
  20644. ' Foo();',
  20645. ' Self.Foo;',
  20646. ' Self.Foo();',
  20647. ' with Self do begin',
  20648. ' Foo;',
  20649. ' Foo();',
  20650. ' end;',
  20651. 'end;',
  20652. 'class function THelper.foo(w: word): word;',
  20653. 'begin',
  20654. ' Run;',
  20655. ' Run();',
  20656. ' Foo;',
  20657. ' Foo();',
  20658. ' Self.Foo;',
  20659. ' Self.Foo();',
  20660. ' with Self do begin',
  20661. ' Foo;',
  20662. ' Foo();',
  20663. ' end;',
  20664. 'end;',
  20665. 'var',
  20666. ' Obj: TObject;',
  20667. 'begin',
  20668. ' obj.Foo;',
  20669. ' obj.Foo();',
  20670. ' with obj do begin',
  20671. ' Foo;',
  20672. ' Foo();',
  20673. ' end;',
  20674. ' tobject.Foo;',
  20675. ' tobject.Foo();',
  20676. ' with tobject do begin',
  20677. ' Foo;',
  20678. ' Foo();',
  20679. ' end;',
  20680. '']);
  20681. ConvertProgram;
  20682. CheckSource('TestClassHelper_ClassMethod_Call',
  20683. LinesToStr([ // statements
  20684. 'rtl.createClass(this, "TObject", null, function () {',
  20685. ' this.$init = function () {',
  20686. ' };',
  20687. ' this.$final = function () {',
  20688. ' };',
  20689. ' this.Run = function (w) {',
  20690. ' $mod.THelper.Foo.call(this, 1);',
  20691. ' $mod.THelper.Foo.call(this, 1);',
  20692. ' $mod.THelper.Foo.call(this, 1);',
  20693. ' $mod.THelper.Foo.call(this, 1);',
  20694. ' $mod.THelper.Foo.call(this, 1);',
  20695. ' $mod.THelper.Foo.call(this, 1);',
  20696. ' };',
  20697. '});',
  20698. 'rtl.createHelper(this, "THelper", null, function () {',
  20699. ' this.Foo = function (w) {',
  20700. ' var Result = 0;',
  20701. ' this.Run(10);',
  20702. ' this.Run(10);',
  20703. ' $mod.THelper.Foo.call(this, 1);',
  20704. ' $mod.THelper.Foo.call(this, 1);',
  20705. ' $mod.THelper.Foo.call(this, 1);',
  20706. ' $mod.THelper.Foo.call(this, 1);',
  20707. ' $mod.THelper.Foo.call(this, 1);',
  20708. ' $mod.THelper.Foo.call(this, 1);',
  20709. ' return Result;',
  20710. ' };',
  20711. '});',
  20712. 'this.Obj = null;',
  20713. '']),
  20714. LinesToStr([ // $mod.$main
  20715. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20716. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20717. 'var $with = $mod.Obj;',
  20718. '$mod.THelper.Foo.call($with.$class, 1);',
  20719. '$mod.THelper.Foo.call($with.$class, 1);',
  20720. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20721. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20722. 'var $with1 = $mod.TObject;',
  20723. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20724. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20725. '']));
  20726. end;
  20727. procedure TTestModule.TestClassHelper_ClassOf;
  20728. begin
  20729. StartProgram(false);
  20730. Add([
  20731. 'type',
  20732. ' TObject = class',
  20733. ' end;',
  20734. ' TClass = class of TObject;',
  20735. ' THelper = class helper for TObject',
  20736. ' class function Foo(w: word = 1): word;',
  20737. ' end;',
  20738. 'class function THelper.foo(w: word): word;',
  20739. 'begin',
  20740. 'end;',
  20741. 'var',
  20742. ' c: TClass;',
  20743. 'begin',
  20744. ' c.Foo;',
  20745. ' c.Foo();',
  20746. ' with c do begin',
  20747. ' Foo;',
  20748. ' Foo();',
  20749. ' end;',
  20750. '']);
  20751. ConvertProgram;
  20752. CheckSource('TestClassHelper_ClassOf',
  20753. LinesToStr([ // statements
  20754. 'rtl.createClass(this, "TObject", null, function () {',
  20755. ' this.$init = function () {',
  20756. ' };',
  20757. ' this.$final = function () {',
  20758. ' };',
  20759. '});',
  20760. 'rtl.createHelper(this, "THelper", null, function () {',
  20761. ' this.Foo = function (w) {',
  20762. ' var Result = 0;',
  20763. ' return Result;',
  20764. ' };',
  20765. '});',
  20766. 'this.c = null;',
  20767. '']),
  20768. LinesToStr([ // $mod.$main
  20769. '$mod.THelper.Foo.call($mod.c, 1);',
  20770. '$mod.THelper.Foo.call($mod.c, 1);',
  20771. 'var $with = $mod.c;',
  20772. '$mod.THelper.Foo.call($with, 1);',
  20773. '$mod.THelper.Foo.call($with, 1);',
  20774. '']));
  20775. end;
  20776. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20777. begin
  20778. StartProgram(false);
  20779. Add([
  20780. '{$mode objfpc}',
  20781. 'type',
  20782. ' TObject = class',
  20783. ' procedure DoIt;',
  20784. ' end;',
  20785. ' THelper = class helper for TObject',
  20786. ' procedure Fly(w: word = 1);',
  20787. ' class procedure Glide(w: word = 1);',
  20788. ' class procedure Run(w: word = 1); static;',
  20789. ' end;',
  20790. ' TFly = procedure(w: word) of object;',
  20791. ' TGlide = TFly;',
  20792. ' TRun = procedure(w: word);',
  20793. 'var',
  20794. ' f: TFly;',
  20795. ' g: TGlide;',
  20796. ' r: TRun;',
  20797. 'procedure TObject.DoIt;',
  20798. 'begin',
  20799. ' f:=@fly;',
  20800. ' g:=@glide;',
  20801. ' r:=@run;',
  20802. ' f:[email protected];',
  20803. ' g:[email protected];',
  20804. ' r:[email protected];',
  20805. ' with self do begin',
  20806. ' f:=@fly;',
  20807. ' g:=@glide;',
  20808. ' r:=@run;',
  20809. ' end;',
  20810. 'end;',
  20811. 'procedure THelper.fly(w: word);',
  20812. 'begin',
  20813. ' f:=@fly;',
  20814. ' g:=@glide;',
  20815. ' r:=@run;',
  20816. 'end;',
  20817. 'class procedure THelper.glide(w: word);',
  20818. 'begin',
  20819. ' g:=@glide;',
  20820. ' r:=@run;',
  20821. 'end;',
  20822. 'class procedure THelper.run(w: word);',
  20823. 'begin',
  20824. ' g:=@glide;',
  20825. ' r:=@run;',
  20826. 'end;',
  20827. 'var',
  20828. ' Obj: TObject;',
  20829. 'begin',
  20830. ' f:[email protected];',
  20831. ' g:[email protected];',
  20832. ' r:[email protected];',
  20833. ' with obj do begin',
  20834. ' f:=@fly;',
  20835. ' g:=@glide;',
  20836. ' r:=@run;',
  20837. ' end;',
  20838. ' g:[email protected];',
  20839. ' r:[email protected];',
  20840. ' with tobject do begin',
  20841. ' g:=@glide;',
  20842. ' r:=@run;',
  20843. ' end;',
  20844. '']);
  20845. ConvertProgram;
  20846. CheckSource('TestClassHelper_MethodRefObjFPC',
  20847. LinesToStr([ // statements
  20848. 'rtl.createClass(this, "TObject", null, function () {',
  20849. ' this.$init = function () {',
  20850. ' };',
  20851. ' this.$final = function () {',
  20852. ' };',
  20853. ' this.DoIt = function () {',
  20854. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20855. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20856. ' $mod.r = $mod.THelper.Run;',
  20857. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20858. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20859. ' $mod.r = $mod.THelper.Run;',
  20860. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20861. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20862. ' $mod.r = $mod.THelper.Run;',
  20863. ' };',
  20864. '});',
  20865. 'rtl.createHelper(this, "THelper", null, function () {',
  20866. ' this.Fly = function (w) {',
  20867. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20868. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20869. ' $mod.r = $mod.THelper.Run;',
  20870. ' };',
  20871. ' this.Glide = function (w) {',
  20872. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20873. ' $mod.r = $mod.THelper.Run;',
  20874. ' };',
  20875. ' this.Run = function (w) {',
  20876. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20877. ' $mod.r = $mod.THelper.Run;',
  20878. ' };',
  20879. '});',
  20880. 'this.f = null;',
  20881. 'this.g = null;',
  20882. 'this.r = null;',
  20883. 'this.Obj = null;',
  20884. '']),
  20885. LinesToStr([ // $mod.$main
  20886. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20887. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20888. '$mod.r = $mod.THelper.Run;',
  20889. 'var $with = $mod.Obj;',
  20890. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  20891. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  20892. '$mod.r = $mod.THelper.Run;',
  20893. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20894. '$mod.r = $mod.THelper.Run;',
  20895. 'var $with1 = $mod.TObject;',
  20896. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  20897. '$mod.r = $mod.THelper.Run;',
  20898. '']));
  20899. end;
  20900. procedure TTestModule.TestClassHelper_Constructor;
  20901. begin
  20902. StartProgram(false);
  20903. Add([
  20904. 'type',
  20905. ' TObject = class',
  20906. ' constructor Create;',
  20907. ' end;',
  20908. ' TClass = class of TObject;',
  20909. ' THelper = class helper for TObject',
  20910. ' constructor NewHlp(w: word);',
  20911. ' end;',
  20912. 'var',
  20913. ' obj: TObject;',
  20914. ' c: TClass;',
  20915. 'constructor TObject.Create;',
  20916. 'begin',
  20917. ' NewHlp(2);', // normal call
  20918. ' tobject.NewHlp(3);', // new instance
  20919. ' c.newhlp(4);', // new instance
  20920. 'end;',
  20921. 'constructor THelper.NewHlp(w: word);',
  20922. 'begin',
  20923. ' create;', // normal call
  20924. ' tobject.create;', // new instance
  20925. ' NewHlp(2);', // normal call
  20926. ' tobject.NewHlp(3);', // new instance
  20927. ' c.newhlp(4);', // new instance
  20928. 'end;',
  20929. 'begin',
  20930. ' obj.newhlp(2);', // normal call
  20931. ' with Obj do newhlp(12);', // normal call
  20932. ' tobject.newhlp(3);', // new instance
  20933. ' with tobject do newhlp(13);', // new instance
  20934. ' c.newhlp(4);', // new instance
  20935. ' with c do newhlp(14);', // new instance
  20936. '']);
  20937. ConvertProgram;
  20938. CheckSource('TestClassHelper_Constructor',
  20939. LinesToStr([ // statements
  20940. 'rtl.createClass(this, "TObject", null, function () {',
  20941. ' this.$init = function () {',
  20942. ' };',
  20943. ' this.$final = function () {',
  20944. ' };',
  20945. ' this.Create = function () {',
  20946. ' $mod.THelper.NewHlp.call(this, 2);',
  20947. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20948. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20949. ' return this;',
  20950. ' };',
  20951. '});',
  20952. 'rtl.createHelper(this, "THelper", null, function () {',
  20953. ' this.NewHlp = function (w) {',
  20954. ' this.Create();',
  20955. ' $mod.TObject.$create("Create");',
  20956. ' $mod.THelper.NewHlp.call(this, 2);',
  20957. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20958. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20959. ' return this;',
  20960. ' };',
  20961. '});',
  20962. 'this.obj = null;',
  20963. 'this.c = null;',
  20964. '']),
  20965. LinesToStr([ // $mod.$main
  20966. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  20967. 'var $with = $mod.obj;',
  20968. '$mod.THelper.NewHlp.call($with, 12);',
  20969. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20970. 'var $with1 = $mod.TObject;',
  20971. '$with1.$create($mod.THelper.NewHlp, [13]);',
  20972. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  20973. 'var $with2 = $mod.c;',
  20974. '$with2.$create($mod.THelper.NewHlp, [14]);',
  20975. '']));
  20976. end;
  20977. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  20978. begin
  20979. StartProgram(false);
  20980. Add([
  20981. 'type',
  20982. ' TObject = class',
  20983. ' procedure Fly;',
  20984. ' end;',
  20985. ' TObjHelper = class helper for TObject',
  20986. ' procedure Fly;',
  20987. ' end;',
  20988. ' TBird = class',
  20989. ' procedure Fly;',
  20990. ' end;',
  20991. ' TBirdHelper = class helper for TBird',
  20992. ' procedure Fly;',
  20993. ' procedure Walk(w: word);',
  20994. ' end;',
  20995. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  20996. ' procedure Fly;',
  20997. ' procedure Walk(w: word);',
  20998. ' end;',
  20999. 'procedure Tobject.fly;',
  21000. 'begin',
  21001. ' inherited;', // ignore
  21002. 'end;',
  21003. 'procedure Tobjhelper.fly;',
  21004. 'begin',
  21005. ' {@TObject_Fly}inherited;',
  21006. ' inherited {@TObject_Fly}Fly;',
  21007. 'end;',
  21008. 'procedure Tbird.fly;',
  21009. 'begin',
  21010. ' {@TObjHelper_Fly}inherited;',
  21011. ' inherited {@TObjHelper_Fly}Fly;',
  21012. 'end;',
  21013. 'procedure Tbirdhelper.fly;',
  21014. 'begin',
  21015. ' {@TBird_Fly}inherited;',
  21016. ' inherited {@TBird_Fly}Fly;',
  21017. 'end;',
  21018. 'procedure Tbirdhelper.walk(w: word);',
  21019. 'begin',
  21020. 'end;',
  21021. 'procedure teagleHelper.fly;',
  21022. 'begin',
  21023. ' {@TBird_Fly}inherited;',
  21024. ' inherited {@TBird_Fly}Fly;',
  21025. 'end;',
  21026. 'procedure teagleHelper.walk(w: word);',
  21027. 'begin',
  21028. ' {@TBirdHelper_Walk}inherited;',
  21029. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21030. 'end;',
  21031. 'begin',
  21032. '']);
  21033. ConvertProgram;
  21034. CheckSource('TestClassHelper_InheritedObjFPC',
  21035. LinesToStr([ // statements
  21036. 'rtl.createClass(this, "TObject", null, function () {',
  21037. ' this.$init = function () {',
  21038. ' };',
  21039. ' this.$final = function () {',
  21040. ' };',
  21041. ' this.Fly = function () {',
  21042. ' };',
  21043. '});',
  21044. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21045. ' this.Fly = function () {',
  21046. ' $mod.TObject.Fly.call(this);',
  21047. ' $mod.TObject.Fly.call(this);',
  21048. ' };',
  21049. '});',
  21050. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21051. ' this.Fly$1 = function () {',
  21052. ' $mod.TObjHelper.Fly.call(this);',
  21053. ' $mod.TObjHelper.Fly.call(this);',
  21054. ' };',
  21055. '});',
  21056. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21057. ' this.Fly = function () {',
  21058. ' $mod.TBird.Fly$1.call(this);',
  21059. ' $mod.TBird.Fly$1.call(this);',
  21060. ' };',
  21061. ' this.Walk = function (w) {',
  21062. ' };',
  21063. '});',
  21064. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21065. ' this.Fly$1 = function () {',
  21066. ' $mod.TBird.Fly$1.call(this);',
  21067. ' $mod.TBird.Fly$1.call(this);',
  21068. ' };',
  21069. ' this.Walk$1 = function (w) {',
  21070. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21071. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21072. ' };',
  21073. '});',
  21074. '']),
  21075. LinesToStr([ // $mod.$main
  21076. '']));
  21077. end;
  21078. procedure TTestModule.TestClassHelper_Property;
  21079. begin
  21080. StartProgram(false);
  21081. Add([
  21082. 'type',
  21083. ' TObject = class',
  21084. ' FSize: word;',
  21085. ' function GetSpeed: word;',
  21086. ' procedure SetSpeed(Value: word);',
  21087. ' end;',
  21088. ' TObjHelper = class helper for TObject',
  21089. ' function GetLeft: word;',
  21090. ' procedure SetLeft(Value: word);',
  21091. ' property Size: word read FSize write FSize;',
  21092. ' property Speed: word read GetSpeed write SetSpeed;',
  21093. ' property Left: word read GetLeft write SetLeft;',
  21094. ' end;',
  21095. ' TBird = class',
  21096. ' property NotRight: word read GetLeft write SetLeft;',
  21097. ' procedure DoIt;',
  21098. ' end;',
  21099. 'var',
  21100. ' b: TBird;',
  21101. 'function Tobject.GetSpeed: word;',
  21102. 'begin',
  21103. ' Size:=Size+11;',
  21104. ' Speed:=Speed+12;',
  21105. ' Result:=Left+13;',
  21106. ' Left:=13;',
  21107. ' Left:=Left+13;',
  21108. ' Self.Size:=Self.Size+21;',
  21109. ' Self.Speed:=Self.Speed+22;',
  21110. ' Self.Left:=Self.Left+23;',
  21111. ' with Self do begin',
  21112. ' Size:=Size+31;',
  21113. ' Speed:=Speed+32;',
  21114. ' Left:=Left+33;',
  21115. ' end;',
  21116. 'end;',
  21117. 'procedure Tobject.SetSpeed(Value: word);',
  21118. 'begin',
  21119. 'end;',
  21120. 'function TObjHelper.GetLeft: word;',
  21121. 'begin',
  21122. ' Size:=Size+11;',
  21123. ' Speed:=Speed+12;',
  21124. ' Left:=Left+13;',
  21125. ' Self.Size:=Self.Size+21;',
  21126. ' Self.Speed:=Self.Speed+22;',
  21127. ' Self.Left:=Self.Left+23;',
  21128. ' with Self do begin',
  21129. ' Size:=Size+31;',
  21130. ' Speed:=Speed+32;',
  21131. ' Left:=Left+33;',
  21132. ' end;',
  21133. 'end;',
  21134. 'procedure TObjHelper.SetLeft(Value: word);',
  21135. 'begin',
  21136. 'end;',
  21137. 'procedure TBird.DoIt;',
  21138. 'begin',
  21139. ' NotRight:=NotRight+11;',
  21140. ' Self.NotRight:=Self.NotRight+21;',
  21141. ' with Self do begin',
  21142. ' NotRight:=NotRight+31;',
  21143. ' end;',
  21144. 'end;',
  21145. 'begin',
  21146. ' b.Size:=b.Size+11;',
  21147. ' b.Speed:=b.Speed+12;',
  21148. ' b.Left:=b.Left+13;',
  21149. ' b.NotRight:=b.NotRight+14;',
  21150. ' with b do begin',
  21151. ' Size:=Size+31;',
  21152. ' Speed:=Speed+32;',
  21153. ' Left:=Left+33;',
  21154. ' NotRight:=NotRight+34;',
  21155. ' end;',
  21156. '']);
  21157. ConvertProgram;
  21158. CheckSource('TestClassHelper_Property',
  21159. LinesToStr([ // statements
  21160. 'rtl.createClass(this, "TObject", null, function () {',
  21161. ' this.$init = function () {',
  21162. ' this.FSize = 0;',
  21163. ' };',
  21164. ' this.$final = function () {',
  21165. ' };',
  21166. ' this.GetSpeed = function () {',
  21167. ' var Result = 0;',
  21168. ' this.FSize = this.FSize + 11;',
  21169. ' this.SetSpeed(this.GetSpeed() + 12);',
  21170. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21171. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21172. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21173. ' this.FSize = this.FSize + 21;',
  21174. ' this.SetSpeed(this.GetSpeed() + 22);',
  21175. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21176. ' this.FSize = this.FSize + 31;',
  21177. ' this.SetSpeed(this.GetSpeed() + 32);',
  21178. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21179. ' return Result;',
  21180. ' };',
  21181. ' this.SetSpeed = function (Value) {',
  21182. ' };',
  21183. '});',
  21184. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21185. ' this.GetLeft = function () {',
  21186. ' var Result = 0;',
  21187. ' this.FSize = this.FSize + 11;',
  21188. ' this.SetSpeed(this.GetSpeed() + 12);',
  21189. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21190. ' this.FSize = this.FSize + 21;',
  21191. ' this.SetSpeed(this.GetSpeed() + 22);',
  21192. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21193. ' this.FSize = this.FSize + 31;',
  21194. ' this.SetSpeed(this.GetSpeed() + 32);',
  21195. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21196. ' return Result;',
  21197. ' };',
  21198. ' this.SetLeft = function (Value) {',
  21199. ' };',
  21200. '});',
  21201. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21202. ' this.DoIt = function () {',
  21203. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21204. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21205. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21206. ' };',
  21207. '});',
  21208. 'this.b = null;',
  21209. '']),
  21210. LinesToStr([ // $mod.$main
  21211. '$mod.b.FSize = $mod.b.FSize + 11;',
  21212. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21213. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21214. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21215. 'var $with = $mod.b;',
  21216. '$with.FSize = $with.FSize + 31;',
  21217. '$with.SetSpeed($with.GetSpeed() + 32);',
  21218. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21219. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21220. '']));
  21221. end;
  21222. procedure TTestModule.TestClassHelper_Property_Array;
  21223. begin
  21224. StartProgram(false);
  21225. Add([
  21226. 'type',
  21227. ' TObject = class',
  21228. ' function GetSpeed(Index: boolean): word;',
  21229. ' procedure SetSpeed(Index: boolean; Value: word);',
  21230. ' end;',
  21231. ' TObjHelper = class helper for TObject',
  21232. ' function GetSize(Index: boolean): word;',
  21233. ' procedure SetSize(Index: boolean; Value: word);',
  21234. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21235. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21236. ' end;',
  21237. ' TBird = class',
  21238. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21239. ' procedure DoIt;',
  21240. ' end;',
  21241. 'var',
  21242. ' b: TBird;',
  21243. 'function Tobject.GetSpeed(Index: boolean): word;',
  21244. 'begin',
  21245. ' Result:=Size[false];',
  21246. ' Size[true]:=Size[false]+11;',
  21247. ' Speed[true]:=Speed[false]+12;',
  21248. ' Self.Size[true]:=Self.Size[false]+21;',
  21249. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21250. ' with Self do begin',
  21251. ' Size[true]:=Size[false]+31;',
  21252. ' Speed[true]:=Speed[false]+32;',
  21253. ' end;',
  21254. 'end;',
  21255. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21256. 'begin',
  21257. 'end;',
  21258. 'function TObjHelper.GetSize(Index: boolean): word;',
  21259. 'begin',
  21260. ' Size[true]:=Size[false]+11;',
  21261. ' Speed[true]:=Speed[false]+12;',
  21262. ' Self.Size[true]:=Self.Size[false]+21;',
  21263. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21264. ' with Self do begin',
  21265. ' Size[true]:=Size[false]+31;',
  21266. ' Speed[true]:=Speed[false]+32;',
  21267. ' end;',
  21268. 'end;',
  21269. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21270. 'begin',
  21271. 'end;',
  21272. 'procedure TBird.DoIt;',
  21273. 'begin',
  21274. ' Items[true]:=Items[false]+11;',
  21275. ' Self.Items[true]:=Self.Items[false]+21;',
  21276. ' with Self do Items[true]:=Items[false]+31;',
  21277. 'end;',
  21278. 'begin',
  21279. ' b.Size[true]:=b.Size[false]+11;',
  21280. ' b.Speed[true]:=b.Speed[false]+12;',
  21281. ' b.Items[true]:=b.Items[false]+13;',
  21282. ' with b do begin',
  21283. ' Size[true]:=Size[false]+21;',
  21284. ' Speed[true]:=Speed[false]+22;',
  21285. ' Items[true]:=Items[false]+23;',
  21286. ' end;',
  21287. '']);
  21288. ConvertProgram;
  21289. CheckSource('TestClassHelper_Property_Array',
  21290. LinesToStr([ // statements
  21291. 'rtl.createClass(this, "TObject", null, function () {',
  21292. ' this.$init = function () {',
  21293. ' };',
  21294. ' this.$final = function () {',
  21295. ' };',
  21296. ' this.GetSpeed = function (Index) {',
  21297. ' var Result = 0;',
  21298. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21299. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21300. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21301. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21302. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21303. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21304. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21305. ' return Result;',
  21306. ' };',
  21307. ' this.SetSpeed = function (Index, Value) {',
  21308. ' };',
  21309. '});',
  21310. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21311. ' this.GetSize = function (Index) {',
  21312. ' var Result = 0;',
  21313. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21314. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21315. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21316. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21317. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21318. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21319. ' return Result;',
  21320. ' };',
  21321. ' this.SetSize = function (Index, Value) {',
  21322. ' };',
  21323. '});',
  21324. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21325. ' this.DoIt = function () {',
  21326. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21327. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21328. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21329. ' };',
  21330. '});',
  21331. 'this.b = null;',
  21332. '']),
  21333. LinesToStr([ // $mod.$main
  21334. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21335. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21336. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21337. 'var $with = $mod.b;',
  21338. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21339. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21340. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21341. '']));
  21342. end;
  21343. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21344. begin
  21345. StartProgram(false);
  21346. Add([
  21347. 'type',
  21348. ' TObject = class',
  21349. ' function GetSpeed(Index: boolean): word;',
  21350. ' procedure SetSpeed(Index: boolean; Value: word);',
  21351. ' end;',
  21352. ' TObjHelper = class helper for TObject',
  21353. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21354. ' end;',
  21355. ' TBird = class',
  21356. ' end;',
  21357. ' TBirdHelper = class helper for TBird',
  21358. ' function GetSize(Index: word): boolean;',
  21359. ' procedure SetSize(Index: word; Value: boolean);',
  21360. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21361. ' end;',
  21362. 'function Tobject.GetSpeed(Index: boolean): word;',
  21363. 'begin',
  21364. ' Self[true]:=Self[false]+1;',
  21365. 'end;',
  21366. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21367. 'begin',
  21368. 'end;',
  21369. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21370. 'begin',
  21371. ' Self[1]:=not Self[2];',
  21372. 'end;',
  21373. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21374. 'begin',
  21375. 'end;',
  21376. 'var',
  21377. ' o: TObject;',
  21378. ' b: TBird;',
  21379. 'begin',
  21380. ' o[true]:=o[false]+1;',
  21381. ' b[3]:=not b[4];',
  21382. '']);
  21383. ConvertProgram;
  21384. CheckSource('TestClassHelper_Property_Array_Default',
  21385. LinesToStr([ // statements
  21386. 'rtl.createClass(this, "TObject", null, function () {',
  21387. ' this.$init = function () {',
  21388. ' };',
  21389. ' this.$final = function () {',
  21390. ' };',
  21391. ' this.GetSpeed = function (Index) {',
  21392. ' var Result = 0;',
  21393. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21394. ' return Result;',
  21395. ' };',
  21396. ' this.SetSpeed = function (Index, Value) {',
  21397. ' };',
  21398. '});',
  21399. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21400. '});',
  21401. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21402. '});',
  21403. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21404. ' this.GetSize = function (Index) {',
  21405. ' var Result = false;',
  21406. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21407. ' return Result;',
  21408. ' };',
  21409. ' this.SetSize = function (Index, Value) {',
  21410. ' };',
  21411. '});',
  21412. 'this.o = null;',
  21413. 'this.b = null;',
  21414. '']),
  21415. LinesToStr([ // $mod.$main
  21416. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21417. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21418. '']));
  21419. end;
  21420. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21421. begin
  21422. StartProgram(false);
  21423. Add([
  21424. 'type',
  21425. ' TObject = class',
  21426. ' end;',
  21427. ' TObjHelper = class helper for TObject',
  21428. ' function GetItems(Index: word): TObject;',
  21429. ' procedure SetItems(Index: word; Value: TObject);',
  21430. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21431. ' end;',
  21432. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21433. 'begin',
  21434. ' Self[1][2]:=Self[3][4];',
  21435. 'end;',
  21436. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21437. 'begin',
  21438. 'end;',
  21439. 'var',
  21440. ' o: TObject;',
  21441. 'begin',
  21442. ' o[1][2]:=o[3][4];',
  21443. '']);
  21444. ConvertProgram;
  21445. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21446. LinesToStr([ // statements
  21447. 'rtl.createClass(this, "TObject", null, function () {',
  21448. ' this.$init = function () {',
  21449. ' };',
  21450. ' this.$final = function () {',
  21451. ' };',
  21452. '});',
  21453. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21454. ' this.GetItems = function (Index) {',
  21455. ' var Result = null;',
  21456. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21457. ' return Result;',
  21458. ' };',
  21459. ' this.SetItems = function (Index, Value) {',
  21460. ' };',
  21461. '});',
  21462. 'this.o = null;',
  21463. '']),
  21464. LinesToStr([ // $mod.$main
  21465. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21466. '']));
  21467. end;
  21468. procedure TTestModule.TestClassHelper_ClassProperty;
  21469. begin
  21470. StartProgram(false);
  21471. Add([
  21472. 'type',
  21473. ' TObject = class',
  21474. ' class var FSize: word;',
  21475. ' class function GetSpeed: word;',
  21476. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21477. ' end;',
  21478. ' TObjHelper = class helper for TObject',
  21479. ' class function GetLeft: word;',
  21480. ' class procedure SetLeft(Value: word);',
  21481. ' class property Size: word read FSize write FSize;',
  21482. ' class property Speed: word read GetSpeed write SetSpeed;',
  21483. ' class property Left: word read GetLeft write SetLeft;',
  21484. ' end;',
  21485. ' TBird = class',
  21486. ' class property NotRight: word read GetLeft write SetLeft;',
  21487. ' class procedure DoIt;',
  21488. ' end;',
  21489. ' TBirdClass = class of TBird;',
  21490. 'class function Tobject.GetSpeed: word;',
  21491. 'begin',
  21492. ' Size:=Size+11;',
  21493. ' Speed:=Speed+12;',
  21494. ' Left:=Left+13;',
  21495. ' Self.Size:=Self.Size+21;',
  21496. ' Self.Speed:=Self.Speed+22;',
  21497. ' Self.Left:=Self.Left+23;',
  21498. ' with Self do begin',
  21499. ' Size:=Size+31;',
  21500. ' Speed:=Speed+32;',
  21501. ' Left:=Left+33;',
  21502. ' end;',
  21503. 'end;',
  21504. 'class function TObjHelper.GetLeft: word;',
  21505. 'begin',
  21506. ' Size:=Size+11;',
  21507. ' Speed:=Speed+12;',
  21508. ' Left:=Left+13;',
  21509. ' Self.Size:=Self.Size+21;',
  21510. ' Self.Speed:=Self.Speed+22;',
  21511. ' Self.Left:=Self.Left+23;',
  21512. ' with Self do begin',
  21513. ' Size:=Size+31;',
  21514. ' Speed:=Speed+32;',
  21515. ' Left:=Left+33;',
  21516. ' end;',
  21517. 'end;',
  21518. 'class procedure TObjHelper.SetLeft(Value: word);',
  21519. 'begin',
  21520. 'end;',
  21521. 'class procedure TBird.DoIt;',
  21522. 'begin',
  21523. ' NotRight:=NotRight+11;',
  21524. ' Self.NotRight:=Self.NotRight+21;',
  21525. ' with Self do NotRight:=NotRight+31;',
  21526. 'end;',
  21527. 'var',
  21528. ' b: TBird;',
  21529. ' c: TBirdClass;',
  21530. 'begin',
  21531. ' b.Size:=b.Size+11;',
  21532. ' b.Speed:=b.Speed+12;',
  21533. ' b.Left:=b.Left+13;',
  21534. ' b.NotRight:=b.NotRight+14;',
  21535. ' with b do begin',
  21536. ' Size:=Size+31;',
  21537. ' Speed:=Speed+32;',
  21538. ' Left:=Left+33;',
  21539. ' NotRight:=NotRight+34;',
  21540. ' end;',
  21541. ' c.Size:=c.Size+11;',
  21542. ' c.Speed:=c.Speed+12;',
  21543. ' c.Left:=c.Left+13;',
  21544. ' c.NotRight:=c.NotRight+14;',
  21545. ' with c do begin',
  21546. ' Size:=Size+31;',
  21547. ' Speed:=Speed+32;',
  21548. ' Left:=Left+33;',
  21549. ' NotRight:=NotRight+34;',
  21550. ' end;',
  21551. ' tbird.Size:=tbird.Size+11;',
  21552. ' tbird.Speed:=tbird.Speed+12;',
  21553. ' tbird.Left:=tbird.Left+13;',
  21554. ' tbird.NotRight:=tbird.NotRight+14;',
  21555. ' with tbird do begin',
  21556. ' Size:=Size+31;',
  21557. ' Speed:=Speed+32;',
  21558. ' Left:=Left+33;',
  21559. ' NotRight:=NotRight+34;',
  21560. ' end;',
  21561. '']);
  21562. ConvertProgram;
  21563. CheckSource('TestClassHelper_ClassProperty',
  21564. LinesToStr([ // statements
  21565. 'rtl.createClass(this, "TObject", null, function () {',
  21566. ' this.FSize = 0;',
  21567. ' this.$init = function () {',
  21568. ' };',
  21569. ' this.$final = function () {',
  21570. ' };',
  21571. ' this.GetSpeed = function () {',
  21572. ' var Result = 0;',
  21573. ' $mod.TObject.FSize = this.FSize + 11;',
  21574. ' this.SetSpeed(this.GetSpeed() + 12);',
  21575. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21576. ' $mod.TObject.FSize = this.FSize + 21;',
  21577. ' this.SetSpeed(this.GetSpeed() + 22);',
  21578. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21579. ' $mod.TObject.FSize = this.FSize + 31;',
  21580. ' this.SetSpeed(this.GetSpeed() + 32);',
  21581. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21582. ' return Result;',
  21583. ' };',
  21584. '});',
  21585. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21586. ' this.GetLeft = function () {',
  21587. ' var Result = 0;',
  21588. ' $mod.TObject.FSize = this.FSize + 11;',
  21589. ' this.SetSpeed(this.GetSpeed() + 12);',
  21590. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21591. ' $mod.TObject.FSize = this.FSize + 21;',
  21592. ' this.SetSpeed(this.GetSpeed() + 22);',
  21593. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21594. ' $mod.TObject.FSize = this.FSize + 31;',
  21595. ' this.SetSpeed(this.GetSpeed() + 32);',
  21596. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21597. ' return Result;',
  21598. ' };',
  21599. ' this.SetLeft = function (Value) {',
  21600. ' };',
  21601. '});',
  21602. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21603. ' this.DoIt = function () {',
  21604. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21605. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21606. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21607. ' };',
  21608. '});',
  21609. 'this.b = null;',
  21610. 'this.c = null;',
  21611. '']),
  21612. LinesToStr([ // $mod.$main
  21613. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21614. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21615. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21616. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21617. 'var $with = $mod.b;',
  21618. '$mod.TObject.FSize = $with.FSize + 31;',
  21619. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21620. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21621. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21622. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21623. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21624. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21625. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21626. 'var $with1 = $mod.c;',
  21627. '$mod.TObject.FSize = $with1.FSize + 31;',
  21628. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21629. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21630. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21631. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21632. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21633. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21634. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21635. 'var $with2 = $mod.TBird;',
  21636. '$mod.TObject.FSize = $with2.FSize + 31;',
  21637. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21638. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21639. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21640. '']));
  21641. end;
  21642. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21643. begin
  21644. StartProgram(false);
  21645. Add([
  21646. 'type',
  21647. ' TObject = class',
  21648. ' class function GetSpeed: word; static;',
  21649. ' class procedure SetSpeed(Value: word); static;',
  21650. ' end;',
  21651. ' TObjHelper = class helper for TObject',
  21652. ' class function GetLeft: word; static;',
  21653. ' class procedure SetLeft(Value: word); static;',
  21654. ' class property Speed: word read GetSpeed write SetSpeed;',
  21655. ' class property Left: word read GetLeft write SetLeft;',
  21656. ' end;',
  21657. ' TBird = class',
  21658. ' class property NotRight: word read GetLeft write SetLeft;',
  21659. ' class procedure DoIt; static;',
  21660. ' class procedure DoSome;',
  21661. ' end;',
  21662. ' TBirdClass = class of TBird;',
  21663. 'class function Tobject.GetSpeed: word;',
  21664. 'begin',
  21665. ' Speed:=Speed+12;',
  21666. ' Left:=Left+13;',
  21667. 'end;',
  21668. 'class procedure TObject.SetSpeed(Value: word);',
  21669. 'begin',
  21670. 'end;',
  21671. 'class function TObjHelper.GetLeft: word;',
  21672. 'begin',
  21673. ' Speed:=Speed+12;',
  21674. ' Left:=Left+13;',
  21675. 'end;',
  21676. 'class procedure TObjHelper.SetLeft(Value: word);',
  21677. 'begin',
  21678. 'end;',
  21679. 'class procedure TBird.DoIt;',
  21680. 'begin',
  21681. ' NotRight:=NotRight+11;',
  21682. 'end;',
  21683. 'class procedure TBird.DoSome;',
  21684. 'begin',
  21685. ' Speed:=Speed+12;',
  21686. ' Left:=Left+13;',
  21687. ' Self.Speed:=Self.Speed+22;',
  21688. ' Self.Left:=Self.Left+23;',
  21689. ' with Self do begin',
  21690. ' Speed:=Speed+32;',
  21691. ' Left:=Left+33;',
  21692. ' end;',
  21693. ' NotRight:=NotRight+11;',
  21694. ' Self.NotRight:=Self.NotRight+21;',
  21695. ' with Self do NotRight:=NotRight+31;',
  21696. 'end;',
  21697. 'var',
  21698. ' b: TBird;',
  21699. ' c: TBirdClass;',
  21700. 'begin',
  21701. ' b.Speed:=b.Speed+12;',
  21702. ' b.Left:=b.Left+13;',
  21703. ' b.NotRight:=b.NotRight+14;',
  21704. ' with b do begin',
  21705. ' Speed:=Speed+32;',
  21706. ' Left:=Left+33;',
  21707. ' NotRight:=NotRight+34;',
  21708. ' end;',
  21709. ' c.Speed:=c.Speed+12;',
  21710. ' c.Left:=c.Left+13;',
  21711. ' c.NotRight:=c.NotRight+14;',
  21712. ' with c do begin',
  21713. ' Speed:=Speed+32;',
  21714. ' Left:=Left+33;',
  21715. ' NotRight:=NotRight+34;',
  21716. ' end;',
  21717. ' tbird.Speed:=tbird.Speed+12;',
  21718. ' tbird.Left:=tbird.Left+13;',
  21719. ' tbird.NotRight:=tbird.NotRight+14;',
  21720. ' with tbird do begin',
  21721. ' Speed:=Speed+32;',
  21722. ' Left:=Left+33;',
  21723. ' NotRight:=NotRight+34;',
  21724. ' end;',
  21725. '']);
  21726. ConvertProgram;
  21727. CheckSource('TestClassHelper_ClassPropertyStatic',
  21728. LinesToStr([ // statements
  21729. 'rtl.createClass(this, "TObject", null, function () {',
  21730. ' this.$init = function () {',
  21731. ' };',
  21732. ' this.$final = function () {',
  21733. ' };',
  21734. ' this.GetSpeed = function () {',
  21735. ' var Result = 0;',
  21736. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21737. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21738. ' return Result;',
  21739. ' };',
  21740. ' this.SetSpeed = function (Value) {',
  21741. ' };',
  21742. '});',
  21743. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21744. ' this.GetLeft = function () {',
  21745. ' var Result = 0;',
  21746. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21747. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21748. ' return Result;',
  21749. ' };',
  21750. ' this.SetLeft = function (Value) {',
  21751. ' };',
  21752. '});',
  21753. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21754. ' this.DoIt = function () {',
  21755. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21756. ' };',
  21757. ' this.DoSome = function () {',
  21758. ' this.SetSpeed(this.GetSpeed() + 12);',
  21759. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21760. ' this.SetSpeed(this.GetSpeed() + 22);',
  21761. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21762. ' this.SetSpeed(this.GetSpeed() + 32);',
  21763. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21764. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21765. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21766. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21767. ' };',
  21768. '});',
  21769. 'this.b = null;',
  21770. 'this.c = null;',
  21771. '']),
  21772. LinesToStr([ // $mod.$main
  21773. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21774. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21775. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21776. 'var $with = $mod.b;',
  21777. '$with.SetSpeed($with.GetSpeed() + 32);',
  21778. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21779. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21780. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21781. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21782. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21783. 'var $with1 = $mod.c;',
  21784. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21785. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21786. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21787. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21788. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21789. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21790. 'var $with2 = $mod.TBird;',
  21791. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21792. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21793. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21794. '']));
  21795. end;
  21796. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21797. begin
  21798. StartProgram(false);
  21799. Add([
  21800. 'type',
  21801. ' TObject = class',
  21802. ' class function GetSpeed(Index: boolean): word;',
  21803. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21804. ' end;',
  21805. ' TObjHelper = class helper for TObject',
  21806. ' class function GetSize(Index: boolean): word;',
  21807. ' class procedure SetSize(Index: boolean; Value: word);',
  21808. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21809. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21810. ' end;',
  21811. ' TBird = class',
  21812. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21813. ' class procedure DoIt;',
  21814. ' end;',
  21815. ' TBirdClass = class of TBird;',
  21816. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21817. 'begin',
  21818. ' Size[true]:=Size[false]+11;',
  21819. ' Speed[true]:=Speed[false]+12;',
  21820. ' Self.Size[true]:=Self.Size[false]+21;',
  21821. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21822. ' with Self do begin',
  21823. ' Size[true]:=Size[false]+31;',
  21824. ' Speed[true]:=Speed[false]+32;',
  21825. ' end;',
  21826. 'end;',
  21827. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21828. 'begin',
  21829. ' Size[true]:=Size[false]+11;',
  21830. ' Speed[true]:=Speed[false]+12;',
  21831. ' Self.Size[true]:=Self.Size[false]+21;',
  21832. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21833. ' with Self do begin',
  21834. ' Size[true]:=Size[false]+31;',
  21835. ' Speed[true]:=Speed[false]+32;',
  21836. ' end;',
  21837. 'end;',
  21838. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21839. 'begin',
  21840. 'end;',
  21841. 'class procedure TBird.DoIt;',
  21842. 'begin',
  21843. ' Items[true]:=Items[false]+11;',
  21844. ' Self.Items[true]:=Self.Items[false]+21;',
  21845. ' with Self do Items[true]:=Items[false]+31;',
  21846. 'end;',
  21847. 'var',
  21848. ' b: TBird;',
  21849. ' c: TBirdClass;',
  21850. 'begin',
  21851. ' b.Size[true]:=b.Size[false]+11;',
  21852. ' b.Speed[true]:=b.Speed[false]+12;',
  21853. ' b.Items[true]:=b.Items[false]+13;',
  21854. ' with b do begin',
  21855. ' Size[true]:=Size[false]+21;',
  21856. ' Speed[true]:=Speed[false]+22;',
  21857. ' Items[true]:=Items[false]+23;',
  21858. ' end;',
  21859. ' c.Size[true]:=c.Size[false]+11;',
  21860. ' c.Speed[true]:=c.Speed[false]+12;',
  21861. ' c.Items[true]:=c.Items[false]+13;',
  21862. ' with c do begin',
  21863. ' Size[true]:=Size[false]+21;',
  21864. ' Speed[true]:=Speed[false]+22;',
  21865. ' Items[true]:=Items[false]+23;',
  21866. ' end;',
  21867. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21868. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21869. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21870. ' with TBird do begin',
  21871. ' Size[true]:=Size[false]+21;',
  21872. ' Speed[true]:=Speed[false]+22;',
  21873. ' Items[true]:=Items[false]+23;',
  21874. ' end;',
  21875. '']);
  21876. ConvertProgram;
  21877. CheckSource('TestClassHelper_ClassProperty_Array',
  21878. LinesToStr([ // statements
  21879. 'rtl.createClass(this, "TObject", null, function () {',
  21880. ' this.$init = function () {',
  21881. ' };',
  21882. ' this.$final = function () {',
  21883. ' };',
  21884. ' this.GetSpeed = function (Index) {',
  21885. ' var Result = 0;',
  21886. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21887. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21888. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21889. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21890. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21891. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21892. ' return Result;',
  21893. ' };',
  21894. '});',
  21895. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21896. ' this.GetSize = function (Index) {',
  21897. ' var Result = 0;',
  21898. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21899. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21900. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21901. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21902. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21903. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21904. ' return Result;',
  21905. ' };',
  21906. ' this.SetSize = function (Index, Value) {',
  21907. ' };',
  21908. '});',
  21909. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21910. ' this.DoIt = function () {',
  21911. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21912. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21913. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21914. ' };',
  21915. '});',
  21916. 'this.b = null;',
  21917. 'this.c = null;',
  21918. '']),
  21919. LinesToStr([ // $mod.$main
  21920. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  21921. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  21922. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  21923. 'var $with = $mod.b;',
  21924. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  21925. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  21926. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  21927. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  21928. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  21929. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  21930. 'var $with1 = $mod.c;',
  21931. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  21932. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  21933. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  21934. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  21935. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  21936. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  21937. 'var $with2 = $mod.TBird;',
  21938. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  21939. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  21940. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  21941. '']));
  21942. end;
  21943. procedure TTestModule.TestClassHelper_ForIn;
  21944. begin
  21945. StartProgram(false);
  21946. Add([
  21947. 'type',
  21948. ' TObject = class end;',
  21949. ' TItem = TObject;',
  21950. ' TEnumerator = class',
  21951. ' FCurrent: TItem;',
  21952. ' property Current: TItem read FCurrent;',
  21953. ' function MoveNext: boolean;',
  21954. ' end;',
  21955. ' TBird = class',
  21956. ' end;',
  21957. ' TBirdHelper = class helper for TBird',
  21958. ' function GetEnumerator: TEnumerator;',
  21959. ' end;',
  21960. 'function TEnumerator.MoveNext: boolean;',
  21961. 'begin',
  21962. 'end;',
  21963. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  21964. 'begin',
  21965. 'end;',
  21966. 'var',
  21967. ' b: TBird;',
  21968. ' i, i2: TItem;',
  21969. 'begin',
  21970. ' for i in b do i2:=i;']);
  21971. ConvertProgram;
  21972. CheckSource('TestClassHelper_ForIn',
  21973. LinesToStr([ // statements
  21974. 'rtl.createClass(this, "TObject", null, function () {',
  21975. ' this.$init = function () {',
  21976. ' };',
  21977. ' this.$final = function () {',
  21978. ' };',
  21979. '});',
  21980. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  21981. ' this.$init = function () {',
  21982. ' $mod.TObject.$init.call(this);',
  21983. ' this.FCurrent = null;',
  21984. ' };',
  21985. ' this.$final = function () {',
  21986. ' this.FCurrent = undefined;',
  21987. ' $mod.TObject.$final.call(this);',
  21988. ' };',
  21989. ' this.MoveNext = function () {',
  21990. ' var Result = false;',
  21991. ' return Result;',
  21992. ' };',
  21993. '});',
  21994. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21995. '});',
  21996. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21997. ' this.GetEnumerator = function () {',
  21998. ' var Result = null;',
  21999. ' return Result;',
  22000. ' };',
  22001. '});',
  22002. 'this.b = null;',
  22003. 'this.i = null;',
  22004. 'this.i2 = null;'
  22005. ]),
  22006. LinesToStr([ // $mod.$main
  22007. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22008. 'try {',
  22009. ' while ($in.MoveNext()){',
  22010. ' $mod.i = $in.FCurrent;',
  22011. ' $mod.i2 = $mod.i;',
  22012. ' }',
  22013. '} finally {',
  22014. ' $in = rtl.freeLoc($in)',
  22015. '};',
  22016. '']));
  22017. end;
  22018. procedure TTestModule.TestClassHelper_PassProperty;
  22019. begin
  22020. StartProgram(false);
  22021. Add([
  22022. 'type',
  22023. ' TObject = class',
  22024. ' FField: TObject;',
  22025. ' property Field: TObject read FField write FField;',
  22026. ' end;',
  22027. ' THelper = class helper for TObject',
  22028. ' procedure Fly;',
  22029. ' class procedure Run;',
  22030. ' class procedure Jump; static;',
  22031. ' end;',
  22032. 'procedure THelper.Fly;',
  22033. 'begin',
  22034. ' Field.Fly;',
  22035. ' Field.Run;',
  22036. ' Field.Jump;',
  22037. ' with Field do begin',
  22038. ' Fly;',
  22039. ' Run;',
  22040. ' Jump;',
  22041. ' end;',
  22042. 'end;',
  22043. 'class procedure THelper.Run;',
  22044. 'begin',
  22045. 'end;',
  22046. 'class procedure THelper.Jump;',
  22047. 'begin',
  22048. 'end;',
  22049. 'var',
  22050. ' b: TObject;',
  22051. 'begin',
  22052. ' b.Field.Fly;',
  22053. ' b.Field.Run;',
  22054. ' b.Field.Jump;',
  22055. ' with b do begin',
  22056. ' Field.Run;',
  22057. ' Field.Fly;',
  22058. ' Field.Jump;',
  22059. ' end;',
  22060. ' with b.Field do begin',
  22061. ' Run;',
  22062. ' Fly;',
  22063. ' Jump;',
  22064. ' end;',
  22065. '']);
  22066. ConvertProgram;
  22067. CheckSource('TestClassHelper_PassProperty',
  22068. LinesToStr([ // statements
  22069. 'rtl.createClass(this, "TObject", null, function () {',
  22070. ' this.$init = function () {',
  22071. ' this.FField = null;',
  22072. ' };',
  22073. ' this.$final = function () {',
  22074. ' this.FField = undefined;',
  22075. ' };',
  22076. '});',
  22077. 'rtl.createHelper(this, "THelper", null, function () {',
  22078. ' this.Fly = function () {',
  22079. ' $mod.THelper.Fly.call(this.FField);',
  22080. ' $mod.THelper.Run.call(this.FField.$class);',
  22081. ' $mod.THelper.Jump();',
  22082. ' var $with = this.FField;',
  22083. ' $mod.THelper.Fly.call($with);',
  22084. ' $mod.THelper.Run.call($with.$class);',
  22085. ' $mod.THelper.Jump();',
  22086. ' };',
  22087. ' this.Run = function () {',
  22088. ' };',
  22089. ' this.Jump = function () {',
  22090. ' };',
  22091. '});',
  22092. 'this.b = null;',
  22093. '']),
  22094. LinesToStr([ // $mod.$main
  22095. '$mod.THelper.Fly.call($mod.b.FField);',
  22096. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22097. '$mod.THelper.Jump();',
  22098. 'var $with = $mod.b;',
  22099. '$mod.THelper.Run.call($with.FField.$class);',
  22100. '$mod.THelper.Fly.call($with.FField);',
  22101. '$mod.THelper.Jump();',
  22102. 'var $with1 = $mod.b.FField;',
  22103. '$mod.THelper.Run.call($with1.$class);',
  22104. '$mod.THelper.Fly.call($with1);',
  22105. '$mod.THelper.Jump();',
  22106. '']));
  22107. end;
  22108. procedure TTestModule.TestExtClassHelper_ClassVar;
  22109. begin
  22110. StartProgram(false);
  22111. Add([
  22112. '{$modeswitch externalclass}',
  22113. 'type',
  22114. ' TExtA = class external name ''ExtObj''',
  22115. ' end;',
  22116. ' THelper = class helper for TExtA',
  22117. ' const',
  22118. ' One = 1;',
  22119. ' Two: word = 2;',
  22120. ' class var',
  22121. ' Glob: word;',
  22122. ' function Foo(w: word): word;',
  22123. ' class function Bar(w: word): word; static;',
  22124. ' end;',
  22125. 'function THelper.foo(w: word): word;',
  22126. 'begin',
  22127. ' Result:=w;',
  22128. ' Two:=One+w;',
  22129. ' Glob:=Glob;',
  22130. ' Result:=Self.Glob;',
  22131. ' Self.Glob:=Self.Glob;',
  22132. ' with Self do Glob:=Glob;',
  22133. 'end;',
  22134. 'class function THelper.bar(w: word): word;',
  22135. 'begin',
  22136. ' Result:=w;',
  22137. ' Two:=One;',
  22138. ' Glob:=Glob;',
  22139. 'end;',
  22140. 'var o: TExtA;',
  22141. 'begin',
  22142. ' texta.two:=texta.one;',
  22143. ' texta.Glob:=texta.Glob;',
  22144. ' with texta do begin',
  22145. ' two:=one;',
  22146. ' Glob:=Glob;',
  22147. ' end;',
  22148. ' o.two:=o.one;',
  22149. ' o.Glob:=o.Glob;',
  22150. ' with o do begin',
  22151. ' two:=one;',
  22152. ' Glob:=Glob;',
  22153. ' end;',
  22154. '']);
  22155. ConvertProgram;
  22156. CheckSource('TestExtClassHelper_ClassVar',
  22157. LinesToStr([ // statements
  22158. 'rtl.createHelper(this, "THelper", null, function () {',
  22159. ' this.One = 1;',
  22160. ' this.Two = 2;',
  22161. ' this.Glob = 0;',
  22162. ' this.Foo = function (w) {',
  22163. ' var Result = 0;',
  22164. ' Result = w;',
  22165. ' $mod.THelper.Two = 1 + w;',
  22166. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22167. ' Result = $mod.THelper.Glob;',
  22168. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22169. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22170. ' return Result;',
  22171. ' };',
  22172. ' this.Bar = function (w) {',
  22173. ' var Result = 0;',
  22174. ' Result = w;',
  22175. ' $mod.THelper.Two = 1;',
  22176. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22177. ' return Result;',
  22178. ' };',
  22179. '});',
  22180. 'this.o = null;',
  22181. '']),
  22182. LinesToStr([ // $mod.$main
  22183. '$mod.THelper.Two = 1;',
  22184. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22185. '$mod.THelper.Two = 1;',
  22186. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22187. '$mod.THelper.Two = 1;',
  22188. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22189. 'var $with = $mod.o;',
  22190. '$mod.THelper.Two = 1;',
  22191. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22192. '']));
  22193. end;
  22194. procedure TTestModule.TestExtClassHelper_Method_Call;
  22195. begin
  22196. StartProgram(false);
  22197. Add([
  22198. '{$modeswitch externalclass}',
  22199. 'type',
  22200. ' TFly = function(w: word): word of object;',
  22201. ' TExtA = class external name ''ExtObj''',
  22202. ' procedure Run(w: word = 10);',
  22203. ' end;',
  22204. ' THelper = class helper for TExtA',
  22205. ' function Foo(w: word = 1): word;',
  22206. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22207. ' end;',
  22208. 'var p: TFly;',
  22209. 'function THelper.foo(w: word): word;',
  22210. 'begin',
  22211. ' Run;',
  22212. ' Run();',
  22213. ' Run(11);',
  22214. ' Foo;',
  22215. ' Foo();',
  22216. ' Foo(12);',
  22217. ' Self.Foo;',
  22218. ' Self.Foo();',
  22219. ' Self.Foo(13);',
  22220. ' Fly;',
  22221. ' Fly();',
  22222. ' with Self do begin',
  22223. ' Foo;',
  22224. ' Foo();',
  22225. ' Foo(14);',
  22226. ' Fly;',
  22227. ' Fly();',
  22228. ' end;',
  22229. ' p:=@Fly;',
  22230. 'end;',
  22231. 'var Obj: TExtA;',
  22232. 'begin',
  22233. ' obj.Foo;',
  22234. ' obj.Foo();',
  22235. ' obj.Foo(21);',
  22236. ' obj.Fly;',
  22237. ' obj.Fly();',
  22238. ' with obj do begin',
  22239. ' Foo;',
  22240. ' Foo();',
  22241. ' Foo(22);',
  22242. ' Fly;',
  22243. ' Fly();',
  22244. ' end;',
  22245. ' p:[email protected];',
  22246. '']);
  22247. ConvertProgram;
  22248. CheckSource('TestExtClassHelper_Method_Call',
  22249. LinesToStr([ // statements
  22250. 'rtl.createHelper(this, "THelper", null, function () {',
  22251. ' this.Foo = function (w) {',
  22252. ' var Result = 0;',
  22253. ' this.Run(10);',
  22254. ' this.Run(10);',
  22255. ' this.Run(11);',
  22256. ' $mod.THelper.Foo.call(this, 1);',
  22257. ' $mod.THelper.Foo.call(this, 1);',
  22258. ' $mod.THelper.Foo.call(this, 12);',
  22259. ' $mod.THelper.Foo.call(this, 1);',
  22260. ' $mod.THelper.Foo.call(this, 1);',
  22261. ' $mod.THelper.Foo.call(this, 13);',
  22262. ' this.Fly(2);',
  22263. ' this.Fly(2);',
  22264. ' $mod.THelper.Foo.call(this, 1);',
  22265. ' $mod.THelper.Foo.call(this, 1);',
  22266. ' $mod.THelper.Foo.call(this, 14);',
  22267. ' this.Fly(2);',
  22268. ' this.Fly(2);',
  22269. ' $mod.p = rtl.createCallback(this, "Fly");',
  22270. ' return Result;',
  22271. ' };',
  22272. '});',
  22273. 'this.p = null;',
  22274. 'this.Obj = null;',
  22275. '']),
  22276. LinesToStr([ // $mod.$main
  22277. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22278. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22279. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22280. '$mod.Obj.Fly(2);',
  22281. '$mod.Obj.Fly(2);',
  22282. 'var $with = $mod.Obj;',
  22283. '$mod.THelper.Foo.call($with, 1);',
  22284. '$mod.THelper.Foo.call($with, 1);',
  22285. '$mod.THelper.Foo.call($with, 22);',
  22286. '$with.Fly(2);',
  22287. '$with.Fly(2);',
  22288. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22289. '']));
  22290. end;
  22291. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22292. begin
  22293. StartProgram(false);
  22294. Add([
  22295. '{$modeswitch externalclass}',
  22296. 'type',
  22297. ' TExtA = class external name ''ExtObj''',
  22298. ' procedure Run(w: word = 10);',
  22299. ' end;',
  22300. ' THelper = class helper for TExtA',
  22301. ' class procedure Fly;',
  22302. ' end;',
  22303. 'class procedure THelper.Fly;',
  22304. 'begin end;',
  22305. 'begin',
  22306. '']);
  22307. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22308. nHelperClassMethodForExtClassMustBeStatic);
  22309. ConvertProgram;
  22310. end;
  22311. procedure TTestModule.TestRecordHelper_ClassVar;
  22312. begin
  22313. StartProgram(false);
  22314. Add([
  22315. 'type',
  22316. ' TRec = record',
  22317. ' end;',
  22318. ' THelper = record helper for TRec',
  22319. ' const',
  22320. ' One = 1;',
  22321. ' Two: word = 2;',
  22322. ' class var',
  22323. ' Glob: word;',
  22324. ' function Foo(w: word): word;',
  22325. ' class function Bar(w: word): word; static;',
  22326. ' end;',
  22327. 'function THelper.foo(w: word): word;',
  22328. 'begin',
  22329. ' Result:=w;',
  22330. ' Two:=One+w;',
  22331. ' Glob:=Glob;',
  22332. ' Result:=Self.Glob;',
  22333. ' Self.Glob:=Self.Glob;',
  22334. ' with Self do Glob:=Glob;',
  22335. ' Self:=Self;',
  22336. 'end;',
  22337. 'class function THelper.bar(w: word): word;',
  22338. 'begin',
  22339. ' Result:=w;',
  22340. ' Two:=One;',
  22341. ' Glob:=Glob;',
  22342. 'end;',
  22343. 'var r: TRec;',
  22344. 'begin',
  22345. ' trec.two:=trec.one;',
  22346. ' trec.Glob:=trec.Glob;',
  22347. ' with trec do begin',
  22348. ' two:=one;',
  22349. ' Glob:=Glob;',
  22350. ' end;',
  22351. ' r.two:=r.one;',
  22352. ' r.Glob:=r.Glob;',
  22353. ' with r do begin',
  22354. ' two:=one;',
  22355. ' Glob:=Glob;',
  22356. ' end;',
  22357. '']);
  22358. ConvertProgram;
  22359. CheckSource('TestRecordHelper_ClassVar',
  22360. LinesToStr([ // statements
  22361. 'rtl.recNewT(this, "TRec", function () {',
  22362. ' this.$eq = function (b) {',
  22363. ' return true;',
  22364. ' };',
  22365. ' this.$assign = function (s) {',
  22366. ' return this;',
  22367. ' };',
  22368. '});',
  22369. 'rtl.createHelper(this, "THelper", null, function () {',
  22370. ' this.One = 1;',
  22371. ' this.Two = 2;',
  22372. ' this.Glob = 0;',
  22373. ' this.Foo = function (w) {',
  22374. ' var Result = 0;',
  22375. ' Result = w;',
  22376. ' $mod.THelper.Two = 1 + w;',
  22377. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22378. ' Result = $mod.THelper.Glob;',
  22379. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22380. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22381. ' this.$assign(this);',
  22382. ' return Result;',
  22383. ' };',
  22384. ' this.Bar = function (w) {',
  22385. ' var Result = 0;',
  22386. ' Result = w;',
  22387. ' $mod.THelper.Two = 1;',
  22388. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22389. ' return Result;',
  22390. ' };',
  22391. '});',
  22392. 'this.r = this.TRec.$new();',
  22393. '']),
  22394. LinesToStr([ // $mod.$main
  22395. '$mod.THelper.Two = 1;',
  22396. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22397. 'var $with = $mod.TRec;',
  22398. '$mod.THelper.Two = 1;',
  22399. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22400. '$mod.THelper.Two = 1;',
  22401. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22402. 'var $with1 = $mod.r;',
  22403. '$mod.THelper.Two = 1;',
  22404. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22405. '']));
  22406. end;
  22407. procedure TTestModule.TestRecordHelper_Method_Call;
  22408. begin
  22409. StartProgram(false);
  22410. Add([
  22411. '{$modeswitch AdvancedRecords}',
  22412. 'type',
  22413. ' TRec = record',
  22414. ' procedure Run(w: word = 10);',
  22415. ' end;',
  22416. ' THelper = record helper for TRec',
  22417. ' function Foo(w: word = 1): word;',
  22418. ' end;',
  22419. 'procedure TRec.Run(w: word);',
  22420. 'begin',
  22421. ' Foo;',
  22422. ' Foo();',
  22423. ' Foo(2);',
  22424. ' Self.Foo;',
  22425. ' Self.Foo();',
  22426. ' Self.Foo(3);',
  22427. ' with Self do begin',
  22428. ' Foo;',
  22429. ' Foo();',
  22430. ' Foo(4);',
  22431. ' end;',
  22432. 'end;',
  22433. 'function THelper.foo(w: word): word;',
  22434. 'begin',
  22435. ' Run;',
  22436. ' Run();',
  22437. ' Run(11);',
  22438. ' Foo;',
  22439. ' Foo();',
  22440. ' Foo(12);',
  22441. ' Self.Foo;',
  22442. ' Self.Foo();',
  22443. ' Self.Foo(13);',
  22444. ' with Self do begin',
  22445. ' Foo;',
  22446. ' Foo();',
  22447. ' Foo(14);',
  22448. ' end;',
  22449. 'end;',
  22450. 'var Rec: TRec;',
  22451. 'begin',
  22452. ' Rec.Foo;',
  22453. ' Rec.Foo();',
  22454. ' Rec.Foo(21);',
  22455. ' with Rec do begin',
  22456. ' Foo;',
  22457. ' Foo();',
  22458. ' Foo(22);',
  22459. ' end;',
  22460. '']);
  22461. ConvertProgram;
  22462. CheckSource('TestRecordHelper_Method_Call',
  22463. LinesToStr([ // statements
  22464. 'rtl.recNewT(this, "TRec", function () {',
  22465. ' this.$eq = function (b) {',
  22466. ' return true;',
  22467. ' };',
  22468. ' this.$assign = function (s) {',
  22469. ' return this;',
  22470. ' };',
  22471. ' this.Run = function (w) {',
  22472. ' $mod.THelper.Foo.call(this, 1);',
  22473. ' $mod.THelper.Foo.call(this, 1);',
  22474. ' $mod.THelper.Foo.call(this, 2);',
  22475. ' $mod.THelper.Foo.call(this, 1);',
  22476. ' $mod.THelper.Foo.call(this, 1);',
  22477. ' $mod.THelper.Foo.call(this, 3);',
  22478. ' $mod.THelper.Foo.call(this, 1);',
  22479. ' $mod.THelper.Foo.call(this, 1);',
  22480. ' $mod.THelper.Foo.call(this, 4);',
  22481. ' };',
  22482. '});',
  22483. 'rtl.createHelper(this, "THelper", null, function () {',
  22484. ' this.Foo = function (w) {',
  22485. ' var Result = 0;',
  22486. ' this.Run(10);',
  22487. ' this.Run(10);',
  22488. ' this.Run(11);',
  22489. ' $mod.THelper.Foo.call(this, 1);',
  22490. ' $mod.THelper.Foo.call(this, 1);',
  22491. ' $mod.THelper.Foo.call(this, 12);',
  22492. ' $mod.THelper.Foo.call(this, 1);',
  22493. ' $mod.THelper.Foo.call(this, 1);',
  22494. ' $mod.THelper.Foo.call(this, 13);',
  22495. ' $mod.THelper.Foo.call(this, 1);',
  22496. ' $mod.THelper.Foo.call(this, 1);',
  22497. ' $mod.THelper.Foo.call(this, 14);',
  22498. ' return Result;',
  22499. ' };',
  22500. '});',
  22501. 'this.Rec = this.TRec.$new();',
  22502. '']),
  22503. LinesToStr([ // $mod.$main
  22504. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22505. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22506. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22507. 'var $with = $mod.Rec;',
  22508. '$mod.THelper.Foo.call($with, 1);',
  22509. '$mod.THelper.Foo.call($with, 1);',
  22510. '$mod.THelper.Foo.call($with, 22);',
  22511. '']));
  22512. end;
  22513. procedure TTestModule.TestRecordHelper_Constructor;
  22514. begin
  22515. StartProgram(false);
  22516. Add([
  22517. '{$modeswitch AdvancedRecords}',
  22518. 'type',
  22519. ' TRec = record',
  22520. ' constructor Create(w: word);',
  22521. ' end;',
  22522. ' THelper = record helper for TRec',
  22523. ' constructor NewHlp(w: word);',
  22524. ' end;',
  22525. 'var',
  22526. ' Rec: TRec;',
  22527. 'constructor TRec.Create(w: word);',
  22528. 'begin',
  22529. ' NewHlp(2);', // normal call
  22530. ' trec.NewHlp(3);', // new instance
  22531. 'end;',
  22532. 'constructor THelper.NewHlp(w: word);',
  22533. 'begin',
  22534. ' create(2);', // normal call
  22535. ' trec.create(3);', // new instance
  22536. ' NewHlp(4);', // normal call
  22537. ' trec.NewHlp(5);', // new instance
  22538. 'end;',
  22539. 'begin',
  22540. ' rec.newhlp(2);', // normal call
  22541. ' with rec do newhlp(12);', // normal call
  22542. ' trec.newhlp(3);', // new instance
  22543. ' with trec do newhlp(13);', // new instance
  22544. '']);
  22545. ConvertProgram;
  22546. CheckSource('TestRecordHelper_Constructor',
  22547. LinesToStr([ // statements
  22548. 'rtl.recNewT(this, "TRec", function () {',
  22549. ' this.$eq = function (b) {',
  22550. ' return true;',
  22551. ' };',
  22552. ' this.$assign = function (s) {',
  22553. ' return this;',
  22554. ' };',
  22555. ' this.Create = function (w) {',
  22556. ' $mod.THelper.NewHlp.call(this, 2);',
  22557. ' $mod.THelper.$new("NewHlp", [3]);',
  22558. ' return this;',
  22559. ' };',
  22560. '}, true);',
  22561. 'rtl.createHelper(this, "THelper", null, function () {',
  22562. ' this.NewHlp = function (w) {',
  22563. ' this.Create(2);',
  22564. ' $mod.TRec.$new().Create(3);',
  22565. ' $mod.THelper.NewHlp.call(this, 4);',
  22566. ' $mod.THelper.$new("NewHlp", [5]);',
  22567. ' return this;',
  22568. ' };',
  22569. ' this.$new = function (fn, args) {',
  22570. ' return this[fn].apply($mod.TRec.$new(), args);',
  22571. ' };',
  22572. '});',
  22573. 'this.Rec = this.TRec.$new();',
  22574. '']),
  22575. LinesToStr([ // $mod.$main
  22576. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22577. 'var $with = $mod.Rec;',
  22578. '$mod.THelper.NewHlp.call($with, 12);',
  22579. '$mod.THelper.$new("NewHlp", [3]);',
  22580. 'var $with1 = $mod.TRec;',
  22581. '$mod.THelper.$new("NewHlp", [13]);',
  22582. '']));
  22583. end;
  22584. procedure TTestModule.TestTypeHelper_ClassVar;
  22585. begin
  22586. StartProgram(false);
  22587. Add([
  22588. '{$modeswitch typehelpers}',
  22589. 'type',
  22590. ' THelper = type helper for byte',
  22591. ' const',
  22592. ' One = 1;',
  22593. ' Two: word = 2;',
  22594. ' class var',
  22595. ' Glob: word;',
  22596. ' function Foo(w: word): word;',
  22597. ' class function Bar(w: word): word; static;',
  22598. ' end;',
  22599. 'function THelper.foo(w: word): word;',
  22600. 'begin',
  22601. ' Result:=w;',
  22602. ' Two:=One+w;',
  22603. ' Glob:=Glob;',
  22604. ' Result:=Self.Glob;',
  22605. ' Self.Glob:=Self.Glob;',
  22606. ' with Self do Glob:=Glob;',
  22607. 'end;',
  22608. 'class function THelper.bar(w: word): word;',
  22609. 'begin',
  22610. ' Result:=w;',
  22611. ' Two:=One;',
  22612. ' Glob:=Glob;',
  22613. 'end;',
  22614. 'var b: byte;',
  22615. 'begin',
  22616. ' byte.two:=byte.one;',
  22617. ' byte.Glob:=byte.Glob;',
  22618. ' with byte do begin',
  22619. ' two:=one;',
  22620. ' Glob:=Glob;',
  22621. ' end;',
  22622. ' b.two:=b.one;',
  22623. ' b.Glob:=b.Glob;',
  22624. ' with b do begin',
  22625. ' two:=one;',
  22626. ' Glob:=Glob;',
  22627. ' end;',
  22628. '']);
  22629. ConvertProgram;
  22630. CheckSource('TestTypeHelper_ClassVar',
  22631. LinesToStr([ // statements
  22632. 'rtl.createHelper(this, "THelper", null, function () {',
  22633. ' this.One = 1;',
  22634. ' this.Two = 2;',
  22635. ' this.Glob = 0;',
  22636. ' this.Foo = function (w) {',
  22637. ' var Result = 0;',
  22638. ' Result = w;',
  22639. ' $mod.THelper.Two = 1 + w;',
  22640. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22641. ' Result = $mod.THelper.Glob;',
  22642. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22643. ' var $with = this.get();',
  22644. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22645. ' return Result;',
  22646. ' };',
  22647. ' this.Bar = function (w) {',
  22648. ' var Result = 0;',
  22649. ' Result = w;',
  22650. ' $mod.THelper.Two = 1;',
  22651. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22652. ' return Result;',
  22653. ' };',
  22654. '});',
  22655. 'this.b = 0;',
  22656. '']),
  22657. LinesToStr([ // $mod.$main
  22658. '$mod.THelper.Two = 1;',
  22659. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22660. '$mod.THelper.Two = 1;',
  22661. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22662. '$mod.THelper.Two = 1;',
  22663. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22664. 'var $with = $mod.b;',
  22665. '$mod.THelper.Two = 1;',
  22666. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22667. '']));
  22668. end;
  22669. procedure TTestModule.TestTypeHelper_PassResultElement;
  22670. begin
  22671. StartProgram(false);
  22672. Add([
  22673. '{$modeswitch typehelpers}',
  22674. 'type',
  22675. ' THelper = type helper for word',
  22676. ' procedure DoIt(e: byte = 123);',
  22677. ' class procedure DoSome(e: byte = 456); static;',
  22678. ' end;',
  22679. 'procedure THelper.DoIt(e: byte);',
  22680. 'begin',
  22681. 'end;',
  22682. 'class procedure THelper.DoSome(e: byte);',
  22683. 'begin',
  22684. 'end;',
  22685. 'function Foo(w: word): word;',
  22686. 'begin',
  22687. ' Result.DoIt;',
  22688. ' Result.DoIt();',
  22689. ' Result.DoSome;',
  22690. ' Result.DoSome();',
  22691. ' with Result do begin',
  22692. ' DoIt;',
  22693. ' DoIt();',
  22694. ' DoSome;',
  22695. ' DoSome();',
  22696. ' end;',
  22697. 'end;',
  22698. 'begin',
  22699. '']);
  22700. ConvertProgram;
  22701. CheckSource('TestTypeHelper_PassResultElement',
  22702. LinesToStr([ // statements
  22703. 'rtl.createHelper(this, "THelper", null, function () {',
  22704. ' this.DoIt = function (e) {',
  22705. ' };',
  22706. ' this.DoSome = function (e) {',
  22707. ' };',
  22708. '});',
  22709. 'this.Foo = function (w) {',
  22710. ' var Result = 0;',
  22711. ' $mod.THelper.DoIt.call({',
  22712. ' get: function () {',
  22713. ' return Result;',
  22714. ' },',
  22715. ' set: function (v) {',
  22716. ' Result = v;',
  22717. ' }',
  22718. ' }, 123);',
  22719. ' $mod.THelper.DoIt.call({',
  22720. ' get: function () {',
  22721. ' return Result;',
  22722. ' },',
  22723. ' set: function (v) {',
  22724. ' Result = v;',
  22725. ' }',
  22726. ' }, 123);',
  22727. ' $mod.THelper.DoSome(456);',
  22728. ' $mod.THelper.DoSome(456);',
  22729. ' $mod.THelper.DoIt.call({',
  22730. ' get: function () {',
  22731. ' return Result;',
  22732. ' },',
  22733. ' set: function (v) {',
  22734. ' Result = v;',
  22735. ' }',
  22736. ' }, 123);',
  22737. ' $mod.THelper.DoIt.call({',
  22738. ' get: function () {',
  22739. ' return Result;',
  22740. ' },',
  22741. ' set: function (v) {',
  22742. ' Result = v;',
  22743. ' }',
  22744. ' }, 123);',
  22745. ' $mod.THelper.DoSome(456);',
  22746. ' $mod.THelper.DoSome(456);',
  22747. ' return Result;',
  22748. '};',
  22749. '']),
  22750. LinesToStr([ // $mod.$main
  22751. '']));
  22752. end;
  22753. procedure TTestModule.TestTypeHelper_PassArgs;
  22754. begin
  22755. StartProgram(false);
  22756. Add([
  22757. '{$modeswitch typehelpers}',
  22758. 'type',
  22759. ' THelper = type helper for word',
  22760. ' procedure DoIt(e: byte = 123);',
  22761. ' end;',
  22762. 'procedure THelper.DoIt(e: byte);',
  22763. 'begin',
  22764. 'end;',
  22765. 'procedure FooDefault(a: word);',
  22766. 'begin',
  22767. ' a.DoIt;',
  22768. ' with a do DoIt;',
  22769. 'end;',
  22770. 'procedure FooConst(const a: word);',
  22771. 'begin',
  22772. ' a.DoIt;',
  22773. ' with a do DoIt;',
  22774. 'end;',
  22775. 'procedure FooVar(var a: word);',
  22776. 'begin',
  22777. ' a.DoIt;',
  22778. ' with a do DoIt;',
  22779. 'end;',
  22780. 'begin',
  22781. '']);
  22782. ConvertProgram;
  22783. CheckSource('TestTypeHelper_PassArgs',
  22784. LinesToStr([ // statements
  22785. 'rtl.createHelper(this, "THelper", null, function () {',
  22786. ' this.DoIt = function (e) {',
  22787. ' };',
  22788. '});',
  22789. 'this.FooDefault = function (a) {',
  22790. ' $mod.THelper.DoIt.call({',
  22791. ' get: function () {',
  22792. ' return a;',
  22793. ' },',
  22794. ' set: function (v) {',
  22795. ' a = v;',
  22796. ' }',
  22797. ' }, 123);',
  22798. ' $mod.THelper.DoIt.call({',
  22799. ' get: function () {',
  22800. ' return a;',
  22801. ' },',
  22802. ' set: function (v) {',
  22803. ' a = v;',
  22804. ' }',
  22805. ' }, 123);',
  22806. '};',
  22807. 'this.FooConst = function (a) {',
  22808. ' $mod.THelper.DoIt.call({',
  22809. ' get: function () {',
  22810. ' return a;',
  22811. ' },',
  22812. ' set: function (v) {',
  22813. ' rtl.raiseE("EPropReadOnly");',
  22814. ' }',
  22815. ' }, 123);',
  22816. ' $mod.THelper.DoIt.call({',
  22817. ' get: function () {',
  22818. ' return a;',
  22819. ' },',
  22820. ' set: function () {',
  22821. ' rtl.raiseE("EPropReadOnly");',
  22822. ' }',
  22823. ' }, 123);',
  22824. '};',
  22825. 'this.FooVar = function (a) {',
  22826. ' $mod.THelper.DoIt.call(a, 123);',
  22827. ' var $with = a.get();',
  22828. ' $mod.THelper.DoIt.call(a, 123);',
  22829. '};',
  22830. '']),
  22831. LinesToStr([ // $mod.$main
  22832. '']));
  22833. end;
  22834. procedure TTestModule.TestTypeHelper_PassVarConst;
  22835. begin
  22836. StartProgram(false);
  22837. Add([
  22838. '{$modeswitch typehelpers}',
  22839. 'type',
  22840. ' THelper = type helper for word',
  22841. ' procedure DoIt(e: byte = 123);',
  22842. ' end;',
  22843. 'procedure THelper.DoIt(e: byte);',
  22844. 'begin',
  22845. 'end;',
  22846. 'var a: word;',
  22847. 'const c: word = 2;',
  22848. '{$writeableconst off}',
  22849. 'const r: word = 3;',
  22850. 'begin',
  22851. ' a.DoIt;',
  22852. ' with a do DoIt;',
  22853. ' c.DoIt;',
  22854. ' with c do DoIt;',
  22855. ' r.DoIt;',
  22856. ' with r do DoIt;',
  22857. '']);
  22858. ConvertProgram;
  22859. CheckSource('TestTypeHelper_PassVarConst',
  22860. LinesToStr([ // statements
  22861. 'rtl.createHelper(this, "THelper", null, function () {',
  22862. ' this.DoIt = function (e) {',
  22863. ' };',
  22864. '});',
  22865. 'this.a = 0;',
  22866. 'this.c = 2;',
  22867. 'this.r = 3;',
  22868. '']),
  22869. LinesToStr([ // $mod.$main
  22870. '$mod.THelper.DoIt.call({',
  22871. ' p: $mod,',
  22872. ' get: function () {',
  22873. ' return this.p.a;',
  22874. ' },',
  22875. ' set: function (v) {',
  22876. ' this.p.a = v;',
  22877. ' }',
  22878. '}, 123);',
  22879. 'var $with = $mod.a;',
  22880. '$mod.THelper.DoIt.call({',
  22881. ' get: function () {',
  22882. ' return $with;',
  22883. ' },',
  22884. ' set: function (v) {',
  22885. ' $with = v;',
  22886. ' }',
  22887. '}, 123);',
  22888. '$mod.THelper.DoIt.call({',
  22889. ' p: $mod,',
  22890. ' get: function () {',
  22891. ' return this.p.c;',
  22892. ' },',
  22893. ' set: function (v) {',
  22894. ' this.p.c = v;',
  22895. ' }',
  22896. '}, 123);',
  22897. 'var $with1 = $mod.c;',
  22898. '$mod.THelper.DoIt.call({',
  22899. ' get: function () {',
  22900. ' return $with1;',
  22901. ' },',
  22902. ' set: function (v) {',
  22903. ' $with1 = v;',
  22904. ' }',
  22905. '}, 123);',
  22906. '$mod.THelper.DoIt.call({',
  22907. ' get: function () {',
  22908. ' return 3;',
  22909. ' },',
  22910. ' set: function (v) {',
  22911. ' rtl.raiseE("EPropReadOnly");',
  22912. ' }',
  22913. '}, 123);',
  22914. 'var $with2 = 3;',
  22915. ' $mod.THelper.DoIt.call({',
  22916. ' get: function () {',
  22917. ' return $with2;',
  22918. ' },',
  22919. ' set: function () {',
  22920. ' rtl.raiseE("EPropReadOnly");',
  22921. ' }',
  22922. ' }, 123);',
  22923. '']));
  22924. end;
  22925. procedure TTestModule.TestTypeHelper_PassFuncResult;
  22926. begin
  22927. StartProgram(false);
  22928. Add([
  22929. '{$modeswitch typehelpers}',
  22930. 'type',
  22931. ' THelper = type helper for word',
  22932. ' procedure DoIt(e: byte = 123);',
  22933. ' end;',
  22934. 'procedure THelper.DoIt(e: byte);',
  22935. 'begin',
  22936. 'end;',
  22937. 'function Foo(b: byte = 1): word;',
  22938. 'begin',
  22939. 'end;',
  22940. 'begin',
  22941. ' Foo.DoIt;',
  22942. ' Foo().DoIt;',
  22943. ' with Foo do DoIt;',
  22944. ' with Foo() do DoIt;',
  22945. '']);
  22946. ConvertProgram;
  22947. CheckSource('TestTypeHelper_PassFuncResult',
  22948. LinesToStr([ // statements
  22949. 'rtl.createHelper(this, "THelper", null, function () {',
  22950. ' this.DoIt = function (e) {',
  22951. ' };',
  22952. '});',
  22953. 'this.Foo = function (b) {',
  22954. ' var Result = 0;',
  22955. ' return Result;',
  22956. '};',
  22957. '']),
  22958. LinesToStr([ // $mod.$main
  22959. '$mod.THelper.DoIt.call({',
  22960. ' a: $mod.Foo(1),',
  22961. ' get: function () {',
  22962. ' return this.a;',
  22963. ' },',
  22964. ' set: function (v) {',
  22965. ' this.a = v;',
  22966. ' }',
  22967. '}, 123);',
  22968. '$mod.THelper.DoIt.call({',
  22969. ' a: $mod.Foo(1),',
  22970. ' get: function () {',
  22971. ' return this.a;',
  22972. ' },',
  22973. ' set: function (v) {',
  22974. ' this.a = v;',
  22975. ' }',
  22976. '}, 123);',
  22977. 'var $with = $mod.Foo(1);',
  22978. '$mod.THelper.DoIt.call({',
  22979. ' get: function () {',
  22980. ' return $with;',
  22981. ' },',
  22982. ' set: function (v) {',
  22983. ' $with = v;',
  22984. ' }',
  22985. '}, 123);',
  22986. 'var $with1 = $mod.Foo(1);',
  22987. '$mod.THelper.DoIt.call({',
  22988. ' get: function () {',
  22989. ' return $with1;',
  22990. ' },',
  22991. ' set: function (v) {',
  22992. ' $with1 = v;',
  22993. ' }',
  22994. '}, 123);',
  22995. '']));
  22996. end;
  22997. procedure TTestModule.TestTypeHelper_PassPropertyField;
  22998. begin
  22999. StartProgram(false);
  23000. Add([
  23001. '{$modeswitch typehelpers}',
  23002. 'type',
  23003. ' TObject = class',
  23004. ' FField: word;',
  23005. ' procedure SetField(Value: word);',
  23006. ' property Field: word read FField write SetField;',
  23007. ' end;',
  23008. ' THelper = type helper for word',
  23009. ' procedure Fly;',
  23010. ' class procedure Run; static;',
  23011. ' end;',
  23012. 'procedure TObject.SetField(Value: word);',
  23013. 'begin',
  23014. ' Field.Fly;',
  23015. ' Field.Run;',
  23016. ' Self.Field.Fly;',
  23017. ' Self.Field.Run;',
  23018. ' with Self do begin',
  23019. ' Field.Fly;',
  23020. ' Field.Run;',
  23021. ' end;',
  23022. ' with Self.Field do begin',
  23023. ' Fly;',
  23024. ' Run;',
  23025. ' end;',
  23026. 'end;',
  23027. 'procedure THelper.Fly;',
  23028. 'begin',
  23029. 'end;',
  23030. 'class procedure THelper.Run;',
  23031. 'begin',
  23032. 'end;',
  23033. 'var',
  23034. ' o: TObject;',
  23035. 'begin',
  23036. ' o.Field.Fly;',
  23037. ' o.Field.Run;',
  23038. ' with o do begin',
  23039. ' Field.Fly;',
  23040. ' Field.Run;',
  23041. ' end;',
  23042. ' with o.Field do begin',
  23043. ' Fly;',
  23044. ' Run;',
  23045. ' end;',
  23046. '']);
  23047. ConvertProgram;
  23048. CheckSource('TestTypeHelper_PassPropertyField',
  23049. LinesToStr([ // statements
  23050. 'rtl.createClass(this, "TObject", null, function () {',
  23051. ' this.$init = function () {',
  23052. ' this.FField = 0;',
  23053. ' };',
  23054. ' this.$final = function () {',
  23055. ' };',
  23056. ' this.SetField = function (Value) {',
  23057. ' $mod.THelper.Fly.call({',
  23058. ' p: this,',
  23059. ' get: function () {',
  23060. ' return this.p.FField;',
  23061. ' },',
  23062. ' set: function (v) {',
  23063. ' this.p.FField = v;',
  23064. ' }',
  23065. ' });',
  23066. ' $mod.THelper.Run();',
  23067. ' $mod.THelper.Fly.call({',
  23068. ' p: this,',
  23069. ' get: function () {',
  23070. ' return this.p.FField;',
  23071. ' },',
  23072. ' set: function (v) {',
  23073. ' this.p.FField = v;',
  23074. ' }',
  23075. ' });',
  23076. ' $mod.THelper.Run();',
  23077. ' $mod.THelper.Fly.call({',
  23078. ' p: this,',
  23079. ' get: function () {',
  23080. ' return this.p.FField;',
  23081. ' },',
  23082. ' set: function (v) {',
  23083. ' this.p.FField = v;',
  23084. ' }',
  23085. ' });',
  23086. ' $mod.THelper.Run();',
  23087. ' var $with = this.FField;',
  23088. ' $mod.THelper.Fly.call({',
  23089. ' get: function () {',
  23090. ' return $with;',
  23091. ' },',
  23092. ' set: function (v) {',
  23093. ' $with = v;',
  23094. ' }',
  23095. ' });',
  23096. ' $mod.THelper.Run();',
  23097. ' };',
  23098. '});',
  23099. 'rtl.createHelper(this, "THelper", null, function () {',
  23100. ' this.Fly = function () {',
  23101. ' };',
  23102. ' this.Run = function () {',
  23103. ' };',
  23104. '});',
  23105. 'this.o = null;',
  23106. '']),
  23107. LinesToStr([ // $mod.$main
  23108. '$mod.THelper.Fly.call({',
  23109. ' p: $mod.o,',
  23110. ' get: function () {',
  23111. ' return this.p.FField;',
  23112. ' },',
  23113. ' set: function (v) {',
  23114. ' this.p.FField = v;',
  23115. ' }',
  23116. '});',
  23117. '$mod.THelper.Run();',
  23118. 'var $with = $mod.o;',
  23119. '$mod.THelper.Fly.call({',
  23120. ' p: $with,',
  23121. ' get: function () {',
  23122. ' return this.p.FField;',
  23123. ' },',
  23124. ' set: function (v) {',
  23125. ' this.p.FField = v;',
  23126. ' }',
  23127. '});',
  23128. '$mod.THelper.Run();',
  23129. 'var $with1 = $mod.o.FField;',
  23130. '$mod.THelper.Fly.call({',
  23131. ' get: function () {',
  23132. ' return $with1;',
  23133. ' },',
  23134. ' set: function (v) {',
  23135. ' $with1 = v;',
  23136. ' }',
  23137. '});',
  23138. '$mod.THelper.Run();',
  23139. '']));
  23140. end;
  23141. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23142. begin
  23143. StartProgram(false);
  23144. Add([
  23145. '{$modeswitch typehelpers}',
  23146. 'type',
  23147. ' TObject = class',
  23148. ' FField: word;',
  23149. ' function GetField: word;',
  23150. ' property Field: word read GetField write FField;',
  23151. ' end;',
  23152. ' THelper = type helper for word',
  23153. ' procedure Fly;',
  23154. ' class procedure Run; static;',
  23155. ' end;',
  23156. 'function TObject.GetField: word;',
  23157. 'begin',
  23158. ' Field.Fly;',
  23159. ' Field.Run;',
  23160. ' Self.Field.Fly;',
  23161. ' Self.Field.Run;',
  23162. ' with Self do begin',
  23163. ' Field.Fly;',
  23164. ' Field.Run;',
  23165. ' end;',
  23166. ' with Self.Field do begin',
  23167. ' Fly;',
  23168. ' Run;',
  23169. ' end;',
  23170. 'end;',
  23171. 'procedure THelper.Fly;',
  23172. 'begin',
  23173. 'end;',
  23174. 'class procedure THelper.Run;',
  23175. 'begin',
  23176. 'end;',
  23177. 'var',
  23178. ' o: TObject;',
  23179. 'begin',
  23180. ' o.Field.Fly;',
  23181. ' o.Field.Run;',
  23182. ' with o do begin',
  23183. ' Field.Fly;',
  23184. ' Field.Run;',
  23185. ' end;',
  23186. ' with o.Field do begin',
  23187. ' Fly;',
  23188. ' Run;',
  23189. ' end;',
  23190. '']);
  23191. ConvertProgram;
  23192. CheckSource('TestTypeHelper_PassPropertyGetter',
  23193. LinesToStr([ // statements
  23194. 'rtl.createClass(this, "TObject", null, function () {',
  23195. ' this.$init = function () {',
  23196. ' this.FField = 0;',
  23197. ' };',
  23198. ' this.$final = function () {',
  23199. ' };',
  23200. ' this.GetField = function () {',
  23201. ' var Result = 0;',
  23202. ' $mod.THelper.Fly.call({',
  23203. ' p: this.GetField(),',
  23204. ' get: function () {',
  23205. ' return this.p;',
  23206. ' },',
  23207. ' set: function (v) {',
  23208. ' this.p = v;',
  23209. ' }',
  23210. ' });',
  23211. ' $mod.THelper.Run();',
  23212. ' $mod.THelper.Fly.call({',
  23213. ' p: this.GetField(),',
  23214. ' get: function () {',
  23215. ' return this.p;',
  23216. ' },',
  23217. ' set: function (v) {',
  23218. ' this.p = v;',
  23219. ' }',
  23220. ' });',
  23221. ' $mod.THelper.Run();',
  23222. ' $mod.THelper.Fly.call({',
  23223. ' p: this.GetField(),',
  23224. ' get: function () {',
  23225. ' return this.p;',
  23226. ' },',
  23227. ' set: function (v) {',
  23228. ' this.p = v;',
  23229. ' }',
  23230. ' });',
  23231. ' $mod.THelper.Run();',
  23232. ' var $with = this.GetField();',
  23233. ' $mod.THelper.Fly.call({',
  23234. ' get: function () {',
  23235. ' return $with;',
  23236. ' },',
  23237. ' set: function (v) {',
  23238. ' $with = v;',
  23239. ' }',
  23240. ' });',
  23241. ' $mod.THelper.Run();',
  23242. ' return Result;',
  23243. ' };',
  23244. '});',
  23245. 'rtl.createHelper(this, "THelper", null, function () {',
  23246. ' this.Fly = function () {',
  23247. ' };',
  23248. ' this.Run = function () {',
  23249. ' };',
  23250. '});',
  23251. 'this.o = null;',
  23252. '']),
  23253. LinesToStr([ // $mod.$main
  23254. '$mod.THelper.Fly.call({',
  23255. ' p: $mod.o.GetField(),',
  23256. ' get: function () {',
  23257. ' return this.p;',
  23258. ' },',
  23259. ' set: function (v) {',
  23260. ' this.p = v;',
  23261. ' }',
  23262. '});',
  23263. '$mod.THelper.Run();',
  23264. 'var $with = $mod.o;',
  23265. '$mod.THelper.Fly.call({',
  23266. ' p: $with.GetField(),',
  23267. ' get: function () {',
  23268. ' return this.p;',
  23269. ' },',
  23270. ' set: function (v) {',
  23271. ' this.p = v;',
  23272. ' }',
  23273. '});',
  23274. '$mod.THelper.Run();',
  23275. 'var $with1 = $mod.o.GetField();',
  23276. '$mod.THelper.Fly.call({',
  23277. ' get: function () {',
  23278. ' return $with1;',
  23279. ' },',
  23280. ' set: function (v) {',
  23281. ' $with1 = v;',
  23282. ' }',
  23283. '});',
  23284. '$mod.THelper.Run();',
  23285. '']));
  23286. end;
  23287. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23288. begin
  23289. StartProgram(false);
  23290. Add([
  23291. '{$modeswitch typehelpers}',
  23292. 'type',
  23293. ' TObject = class',
  23294. ' class var FField: word;',
  23295. ' class procedure SetField(Value: word);',
  23296. ' class property Field: word read FField write SetField;',
  23297. ' end;',
  23298. ' THelper = type helper for word',
  23299. ' procedure Fly(n: byte);',
  23300. ' end;',
  23301. 'class procedure TObject.SetField(Value: word);',
  23302. 'begin',
  23303. ' Field.Fly(1);',
  23304. ' Self.Field.Fly(2);',
  23305. ' with Self do Field.Fly(3);',
  23306. ' with Self.Field do Fly(4);',
  23307. ' TObject.Field.Fly(5);',
  23308. ' with TObject do Field.Fly(6);',
  23309. ' with TObject.Field do Fly(7);',
  23310. 'end;',
  23311. 'procedure THelper.Fly(n: byte);',
  23312. 'begin',
  23313. 'end;',
  23314. 'var',
  23315. ' o: TObject;',
  23316. 'begin',
  23317. ' o.Field.Fly(11);',
  23318. ' with o do Field.Fly(12);',
  23319. ' with o.Field do Fly(13);',
  23320. ' TObject.Field.Fly(14);',
  23321. ' with TObject do Field.Fly(15);',
  23322. ' with TObject.Field do Fly(16);',
  23323. '']);
  23324. ConvertProgram;
  23325. CheckSource('TestTypeHelper_PassClassPropertyField',
  23326. LinesToStr([ // statements
  23327. 'rtl.createClass(this, "TObject", null, function () {',
  23328. ' this.FField = 0;',
  23329. ' this.$init = function () {',
  23330. ' };',
  23331. ' this.$final = function () {',
  23332. ' };',
  23333. ' this.SetField = function (Value) {',
  23334. ' $mod.THelper.Fly.call({',
  23335. ' p: this,',
  23336. ' get: function () {',
  23337. ' return this.p.FField;',
  23338. ' },',
  23339. ' set: function (v) {',
  23340. ' $mod.TObject.FField = v;',
  23341. ' }',
  23342. ' }, 1);',
  23343. ' $mod.THelper.Fly.call({',
  23344. ' p: this,',
  23345. ' get: function () {',
  23346. ' return this.p.FField;',
  23347. ' },',
  23348. ' set: function (v) {',
  23349. ' $mod.TObject.FField = v;',
  23350. ' }',
  23351. ' }, 2);',
  23352. ' $mod.THelper.Fly.call({',
  23353. ' p: this,',
  23354. ' get: function () {',
  23355. ' return this.p.FField;',
  23356. ' },',
  23357. ' set: function (v) {',
  23358. ' $mod.TObject.FField = v;',
  23359. ' }',
  23360. ' }, 3);',
  23361. ' var $with = this.FField;',
  23362. ' $mod.THelper.Fly.call({',
  23363. ' get: function () {',
  23364. ' return $with;',
  23365. ' },',
  23366. ' set: function (v) {',
  23367. ' $with = v;',
  23368. ' }',
  23369. ' }, 4);',
  23370. ' $mod.THelper.Fly.call({',
  23371. ' p: $mod.TObject,',
  23372. ' get: function () {',
  23373. ' return this.p.FField;',
  23374. ' },',
  23375. ' set: function (v) {',
  23376. ' $mod.TObject.FField = v;',
  23377. ' }',
  23378. ' }, 5);',
  23379. ' var $with1 = $mod.TObject;',
  23380. ' $mod.THelper.Fly.call({',
  23381. ' p: $with1,',
  23382. ' get: function () {',
  23383. ' return this.p.FField;',
  23384. ' },',
  23385. ' set: function (v) {',
  23386. ' $mod.TObject.FField = v;',
  23387. ' }',
  23388. ' }, 6);',
  23389. ' var $with2 = $mod.TObject.FField;',
  23390. ' $mod.THelper.Fly.call({',
  23391. ' get: function () {',
  23392. ' return $with2;',
  23393. ' },',
  23394. ' set: function (v) {',
  23395. ' $with2 = v;',
  23396. ' }',
  23397. ' }, 7);',
  23398. ' };',
  23399. '});',
  23400. 'rtl.createHelper(this, "THelper", null, function () {',
  23401. ' this.Fly = function (n) {',
  23402. ' };',
  23403. '});',
  23404. 'this.o = null;',
  23405. '']),
  23406. LinesToStr([ // $mod.$main
  23407. '$mod.THelper.Fly.call({',
  23408. ' p: $mod.o,',
  23409. ' get: function () {',
  23410. ' return this.p.FField;',
  23411. ' },',
  23412. ' set: function (v) {',
  23413. ' $mod.TObject.FField = v;',
  23414. ' }',
  23415. '}, 11);',
  23416. 'var $with = $mod.o;',
  23417. '$mod.THelper.Fly.call({',
  23418. ' p: $with,',
  23419. ' get: function () {',
  23420. ' return this.p.FField;',
  23421. ' },',
  23422. ' set: function (v) {',
  23423. ' $mod.TObject.FField = v;',
  23424. ' }',
  23425. '}, 12);',
  23426. 'var $with1 = $mod.o.FField;',
  23427. '$mod.THelper.Fly.call({',
  23428. ' get: function () {',
  23429. ' return $with1;',
  23430. ' },',
  23431. ' set: function (v) {',
  23432. ' $with1 = v;',
  23433. ' }',
  23434. '}, 13);',
  23435. '$mod.THelper.Fly.call({',
  23436. ' p: $mod.TObject,',
  23437. ' get: function () {',
  23438. ' return this.p.FField;',
  23439. ' },',
  23440. ' set: function (v) {',
  23441. ' $mod.TObject.FField = v;',
  23442. ' }',
  23443. '}, 14);',
  23444. 'var $with2 = $mod.TObject;',
  23445. '$mod.THelper.Fly.call({',
  23446. ' p: $with2,',
  23447. ' get: function () {',
  23448. ' return this.p.FField;',
  23449. ' },',
  23450. ' set: function (v) {',
  23451. ' $mod.TObject.FField = v;',
  23452. ' }',
  23453. '}, 15);',
  23454. 'var $with3 = $mod.TObject.FField;',
  23455. '$mod.THelper.Fly.call({',
  23456. ' get: function () {',
  23457. ' return $with3;',
  23458. ' },',
  23459. ' set: function (v) {',
  23460. ' $with3 = v;',
  23461. ' }',
  23462. '}, 16);',
  23463. '']));
  23464. end;
  23465. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23466. begin
  23467. StartProgram(false);
  23468. Add([
  23469. '{$modeswitch typehelpers}',
  23470. 'type',
  23471. ' TObject = class',
  23472. ' class var FField: word;',
  23473. ' class function GetField: word; static;',
  23474. ' class property Field: word read GetField write FField;',
  23475. ' end;',
  23476. ' THelper = type helper for word',
  23477. ' procedure Fly(n: byte);',
  23478. ' end;',
  23479. 'class function TObject.GetField: word;',
  23480. 'begin',
  23481. ' Field.Fly(1);',
  23482. ' TObject.Field.Fly(5);',
  23483. ' with TObject do Field.Fly(6);',
  23484. ' with TObject.Field do Fly(7);',
  23485. 'end;',
  23486. 'procedure THelper.Fly(n: byte);',
  23487. 'begin',
  23488. 'end;',
  23489. 'var',
  23490. ' o: TObject;',
  23491. 'begin',
  23492. ' o.Field.Fly(11);',
  23493. ' with o do Field.Fly(12);',
  23494. ' with o.Field do Fly(13);',
  23495. '']);
  23496. ConvertProgram;
  23497. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23498. LinesToStr([ // statements
  23499. 'rtl.createClass(this, "TObject", null, function () {',
  23500. ' this.FField = 0;',
  23501. ' this.$init = function () {',
  23502. ' };',
  23503. ' this.$final = function () {',
  23504. ' };',
  23505. ' this.GetField = function () {',
  23506. ' var Result = 0;',
  23507. ' $mod.THelper.Fly.call({',
  23508. ' p: $mod.TObject.GetField(),',
  23509. ' get: function () {',
  23510. ' return this.p;',
  23511. ' },',
  23512. ' set: function (v) {',
  23513. ' this.p = v;',
  23514. ' }',
  23515. ' }, 1);',
  23516. ' $mod.THelper.Fly.call({',
  23517. ' p: $mod.TObject.GetField(),',
  23518. ' get: function () {',
  23519. ' return this.p;',
  23520. ' },',
  23521. ' set: function (v) {',
  23522. ' this.p = v;',
  23523. ' }',
  23524. ' }, 5);',
  23525. ' var $with = $mod.TObject;',
  23526. ' $mod.THelper.Fly.call({',
  23527. ' p: $with.GetField(),',
  23528. ' get: function () {',
  23529. ' return this.p;',
  23530. ' },',
  23531. ' set: function (v) {',
  23532. ' this.p = v;',
  23533. ' }',
  23534. ' }, 6);',
  23535. ' var $with1 = $mod.TObject.GetField();',
  23536. ' $mod.THelper.Fly.call({',
  23537. ' get: function () {',
  23538. ' return $with1;',
  23539. ' },',
  23540. ' set: function (v) {',
  23541. ' $with1 = v;',
  23542. ' }',
  23543. ' }, 7);',
  23544. ' return Result;',
  23545. ' };',
  23546. '});',
  23547. 'rtl.createHelper(this, "THelper", null, function () {',
  23548. ' this.Fly = function (n) {',
  23549. ' };',
  23550. '});',
  23551. 'this.o = null;',
  23552. '']),
  23553. LinesToStr([ // $mod.$main
  23554. '$mod.THelper.Fly.call({',
  23555. ' p: $mod.o.GetField(),',
  23556. ' get: function () {',
  23557. ' return this.p;',
  23558. ' },',
  23559. ' set: function (v) {',
  23560. ' this.p = v;',
  23561. ' }',
  23562. '}, 11);',
  23563. 'var $with = $mod.o;',
  23564. '$mod.THelper.Fly.call({',
  23565. ' p: $with.GetField(),',
  23566. ' get: function () {',
  23567. ' return this.p;',
  23568. ' },',
  23569. ' set: function (v) {',
  23570. ' this.p = v;',
  23571. ' }',
  23572. '}, 12);',
  23573. 'var $with1 = $mod.o.GetField();',
  23574. '$mod.THelper.Fly.call({',
  23575. ' get: function () {',
  23576. ' return $with1;',
  23577. ' },',
  23578. ' set: function (v) {',
  23579. ' $with1 = v;',
  23580. ' }',
  23581. '}, 13);',
  23582. '']));
  23583. end;
  23584. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23585. begin
  23586. StartProgram(false);
  23587. Add([
  23588. '{$modeswitch typehelpers}',
  23589. 'type',
  23590. ' TObject = class',
  23591. ' class var FField: word;',
  23592. ' class function GetField: word;',
  23593. ' class property Field: word read GetField write FField;',
  23594. ' end;',
  23595. ' TClass = class of TObject;',
  23596. ' THelper = type helper for word',
  23597. ' procedure Fly(n: byte);',
  23598. ' end;',
  23599. 'class function TObject.GetField: word;',
  23600. 'begin',
  23601. ' Field.Fly(1);',
  23602. ' Self.Field.Fly(5);',
  23603. ' with Self do Field.Fly(6);',
  23604. ' with Self.Field do Fly(7);',
  23605. 'end;',
  23606. 'procedure THelper.Fly(n: byte);',
  23607. 'begin',
  23608. 'end;',
  23609. 'var',
  23610. ' o: TObject;',
  23611. ' c: TClass;',
  23612. 'begin',
  23613. ' o.Field.Fly(11);',
  23614. ' with o do Field.Fly(12);',
  23615. ' with o.Field do Fly(13);',
  23616. ' c.Field.Fly(14);',
  23617. ' with c do Field.Fly(15);',
  23618. ' with c.Field do Fly(16);',
  23619. '']);
  23620. ConvertProgram;
  23621. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23622. LinesToStr([ // statements
  23623. 'rtl.createClass(this, "TObject", null, function () {',
  23624. ' this.FField = 0;',
  23625. ' this.$init = function () {',
  23626. ' };',
  23627. ' this.$final = function () {',
  23628. ' };',
  23629. ' this.GetField = function () {',
  23630. ' var Result = 0;',
  23631. ' $mod.THelper.Fly.call({',
  23632. ' p: this.GetField(),',
  23633. ' get: function () {',
  23634. ' return this.p;',
  23635. ' },',
  23636. ' set: function (v) {',
  23637. ' this.p = v;',
  23638. ' }',
  23639. ' }, 1);',
  23640. ' $mod.THelper.Fly.call({',
  23641. ' p: this.GetField(),',
  23642. ' get: function () {',
  23643. ' return this.p;',
  23644. ' },',
  23645. ' set: function (v) {',
  23646. ' this.p = v;',
  23647. ' }',
  23648. ' }, 5);',
  23649. ' $mod.THelper.Fly.call({',
  23650. ' p: this.GetField(),',
  23651. ' get: function () {',
  23652. ' return this.p;',
  23653. ' },',
  23654. ' set: function (v) {',
  23655. ' this.p = v;',
  23656. ' }',
  23657. ' }, 6);',
  23658. ' var $with = this.GetField();',
  23659. ' $mod.THelper.Fly.call({',
  23660. ' get: function () {',
  23661. ' return $with;',
  23662. ' },',
  23663. ' set: function (v) {',
  23664. ' $with = v;',
  23665. ' }',
  23666. ' }, 7);',
  23667. ' return Result;',
  23668. ' };',
  23669. '});',
  23670. 'rtl.createHelper(this, "THelper", null, function () {',
  23671. ' this.Fly = function (n) {',
  23672. ' };',
  23673. '});',
  23674. 'this.o = null;',
  23675. 'this.c = null;',
  23676. '']),
  23677. LinesToStr([ // $mod.$main
  23678. '$mod.THelper.Fly.call({',
  23679. ' p: $mod.o.$class.GetField(),',
  23680. ' get: function () {',
  23681. ' return this.p;',
  23682. ' },',
  23683. ' set: function (v) {',
  23684. ' this.p = v;',
  23685. ' }',
  23686. '}, 11);',
  23687. 'var $with = $mod.o;',
  23688. '$mod.THelper.Fly.call({',
  23689. ' p: $with.$class.GetField(),',
  23690. ' get: function () {',
  23691. ' return this.p;',
  23692. ' },',
  23693. ' set: function (v) {',
  23694. ' this.p = v;',
  23695. ' }',
  23696. '}, 12);',
  23697. 'var $with1 = $mod.o.$class.GetField();',
  23698. '$mod.THelper.Fly.call({',
  23699. ' get: function () {',
  23700. ' return $with1;',
  23701. ' },',
  23702. ' set: function (v) {',
  23703. ' $with1 = v;',
  23704. ' }',
  23705. '}, 13);',
  23706. '$mod.THelper.Fly.call({',
  23707. ' p: $mod.c.GetField(),',
  23708. ' get: function () {',
  23709. ' return this.p;',
  23710. ' },',
  23711. ' set: function (v) {',
  23712. ' this.p = v;',
  23713. ' }',
  23714. '}, 14);',
  23715. 'var $with2 = $mod.c;',
  23716. '$mod.THelper.Fly.call({',
  23717. ' p: $with2.GetField(),',
  23718. ' get: function () {',
  23719. ' return this.p;',
  23720. ' },',
  23721. ' set: function (v) {',
  23722. ' this.p = v;',
  23723. ' }',
  23724. '}, 15);',
  23725. 'var $with3 = $mod.c.GetField();',
  23726. '$mod.THelper.Fly.call({',
  23727. ' get: function () {',
  23728. ' return $with3;',
  23729. ' },',
  23730. ' set: function (v) {',
  23731. ' $with3 = v;',
  23732. ' }',
  23733. '}, 16);',
  23734. '']));
  23735. end;
  23736. procedure TTestModule.TestTypeHelper_Property;
  23737. begin
  23738. StartProgram(false);
  23739. Add([
  23740. '{$modeswitch typehelpers}',
  23741. 'type',
  23742. ' THelper = type helper for word',
  23743. ' function GetSize: longint;',
  23744. ' procedure SetSize(Value: longint);',
  23745. ' property Size: longint read GetSize write SetSize;',
  23746. ' end;',
  23747. 'function THelper.GetSize: longint;',
  23748. 'begin',
  23749. ' Result:=Size+1;',
  23750. ' Size:=2;',
  23751. ' Result:=Self.Size+3;',
  23752. ' Self.Size:=4;',
  23753. ' with Self do begin',
  23754. ' Result:=Size+5;',
  23755. ' Size:=6;',
  23756. ' end;',
  23757. 'end;',
  23758. 'procedure THelper.SetSize(Value: longint);',
  23759. 'begin',
  23760. 'end;',
  23761. 'var w: word;',
  23762. 'begin',
  23763. ' w:=w.Size+7;',
  23764. ' w.Size:=w+8;',
  23765. ' with w do begin',
  23766. ' w:=Size+9;',
  23767. ' Size:=w+10;',
  23768. ' end;',
  23769. '']);
  23770. ConvertProgram;
  23771. CheckSource('TestTypeHelper_Property',
  23772. LinesToStr([ // statements
  23773. 'rtl.createHelper(this, "THelper", null, function () {',
  23774. ' this.GetSize = function () {',
  23775. ' var Result = 0;',
  23776. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23777. ' $mod.THelper.SetSize.call(this, 2);',
  23778. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23779. ' $mod.THelper.SetSize.call(this, 4);',
  23780. ' var $with = this.get();',
  23781. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23782. ' $mod.THelper.SetSize.call(this, 6);',
  23783. ' return Result;',
  23784. ' };',
  23785. ' this.SetSize = function (Value) {',
  23786. ' };',
  23787. '});',
  23788. 'this.w = 0;',
  23789. '']),
  23790. LinesToStr([ // $mod.$main
  23791. '$mod.w = $mod.THelper.GetSize.call({',
  23792. ' p: $mod,',
  23793. ' get: function () {',
  23794. ' return this.p.w;',
  23795. ' },',
  23796. ' set: function (v) {',
  23797. ' this.p.w = v;',
  23798. ' }',
  23799. '}) + 7;',
  23800. '$mod.THelper.SetSize.call({',
  23801. ' p: $mod,',
  23802. ' get: function () {',
  23803. ' return this.p.w;',
  23804. ' },',
  23805. ' set: function (v) {',
  23806. ' this.p.w = v;',
  23807. ' }',
  23808. '}, $mod.w + 8);',
  23809. 'var $with = $mod.w;',
  23810. '$mod.w = $mod.THelper.GetSize.call({',
  23811. ' get: function () {',
  23812. ' return $with;',
  23813. ' },',
  23814. ' set: function (v) {',
  23815. ' $with = v;',
  23816. ' }',
  23817. '}) + 9;',
  23818. '$mod.THelper.SetSize.call({',
  23819. ' get: function () {',
  23820. ' return $with;',
  23821. ' },',
  23822. ' set: function (v) {',
  23823. ' $with = v;',
  23824. ' }',
  23825. '}, $mod.w + 10);',
  23826. '']));
  23827. end;
  23828. procedure TTestModule.TestTypeHelper_Property_Array;
  23829. begin
  23830. StartProgram(false);
  23831. Add([
  23832. '{$modeswitch typehelpers}',
  23833. 'type',
  23834. ' THelper = type helper for word',
  23835. ' function GetItems(Index: byte): boolean;',
  23836. ' procedure SetItems(Index: byte; Value: boolean);',
  23837. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23838. ' end;',
  23839. 'function THelper.GetItems(Index: byte): boolean;',
  23840. 'begin',
  23841. ' Result:=Items[1];',
  23842. ' Items[2]:=false;',
  23843. ' Result:=Self.Items[3];',
  23844. ' Self.Items[4]:=true;',
  23845. ' with Self do begin',
  23846. ' Result:=Items[5];',
  23847. ' Items[6]:=false;',
  23848. ' end;',
  23849. 'end;',
  23850. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23851. 'begin',
  23852. 'end;',
  23853. 'var',
  23854. ' w: word;',
  23855. ' b: boolean;',
  23856. 'begin',
  23857. ' b:=w.Items[1];',
  23858. ' w.Items[2]:=b;',
  23859. ' with w do begin',
  23860. ' b:=Items[3];',
  23861. ' Items[4]:=b;',
  23862. ' end;',
  23863. '']);
  23864. ConvertProgram;
  23865. CheckSource('TestTypeHelper_Property_Array',
  23866. LinesToStr([ // statements
  23867. 'rtl.createHelper(this, "THelper", null, function () {',
  23868. ' this.GetItems = function (Index) {',
  23869. ' var Result = false;',
  23870. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23871. ' $mod.THelper.SetItems.call(this, 2, false);',
  23872. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23873. ' $mod.THelper.SetItems.call(this, 4, true);',
  23874. ' var $with = this.get();',
  23875. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23876. ' $mod.THelper.SetItems.call(this, 6, false);',
  23877. ' return Result;',
  23878. ' };',
  23879. ' this.SetItems = function (Index, Value) {',
  23880. ' };',
  23881. '});',
  23882. 'this.w = 0;',
  23883. 'this.b = false;',
  23884. '']),
  23885. LinesToStr([ // $mod.$main
  23886. '$mod.b = $mod.THelper.GetItems.call({',
  23887. ' p: $mod,',
  23888. ' get: function () {',
  23889. ' return this.p.w;',
  23890. ' },',
  23891. ' set: function (v) {',
  23892. ' this.p.w = v;',
  23893. ' }',
  23894. '}, 1);',
  23895. '$mod.THelper.SetItems.call({',
  23896. ' p: $mod,',
  23897. ' get: function () {',
  23898. ' return this.p.w;',
  23899. ' },',
  23900. ' set: function (v) {',
  23901. ' this.p.w = v;',
  23902. ' }',
  23903. '}, 2, $mod.b);',
  23904. 'var $with = $mod.w;',
  23905. '$mod.b = $mod.THelper.GetItems.call({',
  23906. ' get: function () {',
  23907. ' return $with;',
  23908. ' },',
  23909. ' set: function (v) {',
  23910. ' $with = v;',
  23911. ' }',
  23912. '}, 3);',
  23913. '$mod.THelper.SetItems.call({',
  23914. ' get: function () {',
  23915. ' return $with;',
  23916. ' },',
  23917. ' set: function (v) {',
  23918. ' $with = v;',
  23919. ' }',
  23920. '}, 4, $mod.b);',
  23921. '']));
  23922. end;
  23923. procedure TTestModule.TestTypeHelper_ClassProperty;
  23924. begin
  23925. StartProgram(false);
  23926. Add([
  23927. '{$modeswitch typehelpers}',
  23928. 'type',
  23929. ' THelper = type helper for word',
  23930. ' class function GetSize: longint; static;',
  23931. ' class procedure SetSize(Value: longint); static;',
  23932. ' class property Size: longint read GetSize write SetSize;',
  23933. ' end;',
  23934. 'class function THelper.GetSize: longint;',
  23935. 'begin',
  23936. ' Result:=Size+1;',
  23937. ' Size:=2;',
  23938. 'end;',
  23939. 'class procedure THelper.SetSize(Value: longint);',
  23940. 'begin',
  23941. 'end;',
  23942. 'begin',
  23943. '']);
  23944. ConvertProgram;
  23945. CheckSource('TestTypeHelper_ClassProperty',
  23946. LinesToStr([ // statements
  23947. 'rtl.createHelper(this, "THelper", null, function () {',
  23948. ' this.GetSize = function () {',
  23949. ' var Result = 0;',
  23950. ' Result = $mod.THelper.GetSize() + 1;',
  23951. ' $mod.THelper.SetSize(2);',
  23952. ' return Result;',
  23953. ' };',
  23954. ' this.SetSize = function (Value) {',
  23955. ' };',
  23956. '});',
  23957. '']),
  23958. LinesToStr([ // $mod.$main
  23959. '']));
  23960. end;
  23961. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  23962. begin
  23963. StartProgram(false);
  23964. Add([
  23965. '{$modeswitch typehelpers}',
  23966. 'type',
  23967. ' THelper = type helper for word',
  23968. ' class function GetItems(Index: byte): boolean; static;',
  23969. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  23970. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  23971. ' end;',
  23972. 'class function THelper.GetItems(Index: byte): boolean;',
  23973. 'begin',
  23974. ' Result:=Items[1];',
  23975. ' Items[2]:=false;',
  23976. 'end;',
  23977. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  23978. 'begin',
  23979. 'end;',
  23980. 'var',
  23981. ' w: word;',
  23982. ' b: boolean;',
  23983. 'begin',
  23984. ' b:=w.Items[1];',
  23985. ' w.Items[2]:=b;',
  23986. ' with w do begin',
  23987. ' b:=Items[3];',
  23988. ' Items[4]:=b;',
  23989. ' end;',
  23990. '']);
  23991. ConvertProgram;
  23992. CheckSource('TestTypeHelper_ClassProperty_Array',
  23993. LinesToStr([ // statements
  23994. 'rtl.createHelper(this, "THelper", null, function () {',
  23995. ' this.GetItems = function (Index) {',
  23996. ' var Result = false;',
  23997. ' Result = $mod.THelper.GetItems(1);',
  23998. ' $mod.THelper.SetItems(2, false);',
  23999. ' return Result;',
  24000. ' };',
  24001. ' this.SetItems = function (Index, Value) {',
  24002. ' };',
  24003. '});',
  24004. 'this.w = 0;',
  24005. 'this.b = false;',
  24006. '']),
  24007. LinesToStr([ // $mod.$main
  24008. '$mod.b = $mod.THelper.GetItems(1);',
  24009. '$mod.THelper.SetItems(2, $mod.b);',
  24010. 'var $with = $mod.w;',
  24011. '$mod.b = $mod.THelper.GetItems(3);',
  24012. '$mod.THelper.SetItems(4, $mod.b);',
  24013. '']));
  24014. end;
  24015. procedure TTestModule.TestTypeHelper_ClassMethod;
  24016. begin
  24017. StartProgram(false);
  24018. Add([
  24019. '{$modeswitch typehelpers}',
  24020. 'type',
  24021. ' THelper = type helper for word',
  24022. ' class procedure DoStatic; static;',
  24023. ' end;',
  24024. 'class procedure THelper.DoStatic;',
  24025. 'begin',
  24026. ' DoStatic;',
  24027. ' DoStatic();',
  24028. 'end;',
  24029. 'var w: word;',
  24030. 'begin',
  24031. ' w.DoStatic;',
  24032. ' w.DoStatic();',
  24033. '']);
  24034. ConvertProgram;
  24035. CheckSource('TestTypeHelper_ClassMethod',
  24036. LinesToStr([ // statements
  24037. 'rtl.createHelper(this, "THelper", null, function () {',
  24038. ' this.DoStatic = function () {',
  24039. ' $mod.THelper.DoStatic();',
  24040. ' $mod.THelper.DoStatic();',
  24041. ' };',
  24042. '});',
  24043. 'this.w = 0;',
  24044. '']),
  24045. LinesToStr([ // $mod.$main
  24046. '$mod.THelper.DoStatic();',
  24047. '$mod.THelper.DoStatic();',
  24048. '']));
  24049. end;
  24050. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24051. begin
  24052. StartProgram(false);
  24053. Add([
  24054. '{$modeswitch typehelpers}',
  24055. 'type',
  24056. ' THelper = type helper for word',
  24057. ' procedure Run; external name ''Run'';',
  24058. ' end;',
  24059. 'var w: word;',
  24060. 'begin',
  24061. ' w.Run;',
  24062. '']);
  24063. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24064. ConvertProgram;
  24065. end;
  24066. procedure TTestModule.TestTypeHelper_Constructor;
  24067. begin
  24068. StartProgram(false);
  24069. Add([
  24070. '{$modeswitch typehelpers}',
  24071. 'type',
  24072. ' THelper = type helper for word',
  24073. ' constructor Init(e: longint);',
  24074. ' end;',
  24075. 'constructor THelper.Init(e: longint);',
  24076. 'begin',
  24077. ' Self:=e;',
  24078. ' Init(e+1);',
  24079. 'end;',
  24080. 'var w: word;',
  24081. 'begin',
  24082. ' w:=word.Init(2);',
  24083. ' w:=w.Init(3);',
  24084. ' with word do w:=Init(4);',
  24085. ' with w do w:=Init(5);',
  24086. '']);
  24087. ConvertProgram;
  24088. CheckSource('TestTypeHelper_Constructor',
  24089. LinesToStr([ // statements
  24090. 'rtl.createHelper(this, "THelper", null, function () {',
  24091. ' this.Init = function (e) {',
  24092. ' this.set(e);',
  24093. ' $mod.THelper.Init.call(this, e + 1);',
  24094. ' return this.get();',
  24095. ' };',
  24096. ' this.$new = function (fn, args) {',
  24097. ' return this[fn].apply({',
  24098. ' p: 0,',
  24099. ' get: function () {',
  24100. ' return this.p;',
  24101. ' },',
  24102. ' set: function (v) {',
  24103. ' this.p = v;',
  24104. ' }',
  24105. ' }, args);',
  24106. ' };',
  24107. '});',
  24108. 'this.w = 0;',
  24109. '']),
  24110. LinesToStr([ // $mod.$main
  24111. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24112. '$mod.w = $mod.THelper.Init.call({',
  24113. ' p: $mod,',
  24114. ' get: function () {',
  24115. ' return this.p.w;',
  24116. ' },',
  24117. ' set: function (v) {',
  24118. ' this.p.w = v;',
  24119. ' }',
  24120. '}, 3);',
  24121. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24122. 'var $with = $mod.w;',
  24123. '$mod.w = $mod.THelper.Init.call({',
  24124. ' get: function () {',
  24125. ' return $with;',
  24126. ' },',
  24127. ' set: function (v) {',
  24128. ' $with = v;',
  24129. ' }',
  24130. '}, 5);',
  24131. '']));
  24132. end;
  24133. procedure TTestModule.TestTypeHelper_Word;
  24134. begin
  24135. StartProgram(false);
  24136. Add([
  24137. '{$modeswitch typehelpers}',
  24138. 'type',
  24139. ' THelper = type helper for word',
  24140. ' procedure DoIt(e: byte = 123);',
  24141. ' end;',
  24142. 'procedure THelper.DoIt(e: byte);',
  24143. 'begin',
  24144. ' Self:=e;',
  24145. ' Self:=Self+1;',
  24146. ' with Self do Doit;',
  24147. 'end;',
  24148. 'begin',
  24149. ' word(3).DoIt;',
  24150. '']);
  24151. ConvertProgram;
  24152. CheckSource('TestTypeHelper_Word',
  24153. LinesToStr([ // statements
  24154. 'rtl.createHelper(this, "THelper", null, function () {',
  24155. ' this.DoIt = function (e) {',
  24156. ' this.set(e);',
  24157. ' this.set(this.get() + 1);',
  24158. ' var $with = this.get();',
  24159. ' $mod.THelper.DoIt.call(this, 123);',
  24160. ' };',
  24161. '});',
  24162. '']),
  24163. LinesToStr([ // $mod.$main
  24164. '$mod.THelper.DoIt.call({',
  24165. ' get: function () {',
  24166. ' return 3;',
  24167. ' },',
  24168. ' set: function (v) {',
  24169. ' rtl.raiseE("EPropReadOnly");',
  24170. ' }',
  24171. '}, 123);',
  24172. '']));
  24173. end;
  24174. procedure TTestModule.TestTypeHelper_Boolean;
  24175. begin
  24176. StartProgram(false);
  24177. Add([
  24178. '{$modeswitch typehelpers}',
  24179. 'type',
  24180. ' Integer = longint;',
  24181. ' THelper = type helper for boolean',
  24182. ' procedure Run(e: wordbool = true);',
  24183. ' end;',
  24184. 'procedure THelper.Run(e: wordbool);',
  24185. 'begin',
  24186. ' Self:=e;',
  24187. ' Self:=not Self;',
  24188. ' with Self do Run;',
  24189. ' if Integer(Self)=0 then ;',
  24190. 'end;',
  24191. 'begin',
  24192. ' boolean(3).Run;',
  24193. '']);
  24194. ConvertProgram;
  24195. CheckSource('TestTypeHelper_Boolean',
  24196. LinesToStr([ // statements
  24197. 'rtl.createHelper(this, "THelper", null, function () {',
  24198. ' this.Run = function (e) {',
  24199. ' this.set(e);',
  24200. ' this.set(!this.get());',
  24201. ' var $with = this.get();',
  24202. ' $mod.THelper.Run.call(this, true);',
  24203. ' if ((this.get() ? 1 : 0) === 0) ;',
  24204. ' };',
  24205. '});',
  24206. '']),
  24207. LinesToStr([ // $mod.$main
  24208. '$mod.THelper.Run.call({',
  24209. ' a: 3 != 0,',
  24210. ' get: function () {',
  24211. ' return this.a;',
  24212. ' },',
  24213. ' set: function (v) {',
  24214. ' rtl.raiseE("EPropReadOnly");',
  24215. ' }',
  24216. '}, true);',
  24217. '']));
  24218. end;
  24219. procedure TTestModule.TestTypeHelper_WordBool;
  24220. begin
  24221. StartProgram(false);
  24222. Add([
  24223. '{$modeswitch typehelpers}',
  24224. 'type',
  24225. ' Integer = longint;',
  24226. ' THelper = type helper for WordBool',
  24227. ' procedure Run(e: wordbool = true);',
  24228. ' end;',
  24229. 'procedure THelper.Run(e: wordbool);',
  24230. 'var i: integer;',
  24231. 'begin',
  24232. ' i:=Integer(Self);',
  24233. 'end;',
  24234. 'var w: wordbool;',
  24235. 'begin',
  24236. ' w.Run;',
  24237. ' wordbool(3).Run;',
  24238. '']);
  24239. ConvertProgram;
  24240. CheckSource('TestTypeHelper_WordBool',
  24241. LinesToStr([ // statements
  24242. 'rtl.createHelper(this, "THelper", null, function () {',
  24243. ' this.Run = function (e) {',
  24244. ' var i = 0;',
  24245. ' i = (this.get() ? 1 : 0);',
  24246. ' };',
  24247. '});',
  24248. 'this.w = false;',
  24249. '']),
  24250. LinesToStr([ // $mod.$main
  24251. '$mod.THelper.Run.call({',
  24252. ' p: $mod,',
  24253. ' get: function () {',
  24254. ' return this.p.w;',
  24255. ' },',
  24256. ' set: function (v) {',
  24257. ' this.p.w = v;',
  24258. ' }',
  24259. '}, true);',
  24260. '$mod.THelper.Run.call({',
  24261. ' a: 3 != 0,',
  24262. ' get: function () {',
  24263. ' return this.a;',
  24264. ' },',
  24265. ' set: function (v) {',
  24266. ' rtl.raiseE("EPropReadOnly");',
  24267. ' }',
  24268. '}, true);',
  24269. '']));
  24270. end;
  24271. procedure TTestModule.TestTypeHelper_Double;
  24272. begin
  24273. StartProgram(false);
  24274. Add([
  24275. '{$modeswitch typehelpers}',
  24276. 'type',
  24277. ' Float = type double;',
  24278. ' THelper = type helper for Float',
  24279. ' const NPI = 3.141592;',
  24280. ' function ToStr: String;',
  24281. ' end;',
  24282. 'function THelper.ToStr: String;',
  24283. 'begin',
  24284. 'end;',
  24285. 'procedure DoIt(s: string);',
  24286. 'begin',
  24287. 'end;',
  24288. 'var f: Float;',
  24289. 'begin',
  24290. ' DoIt(f.toStr);',
  24291. ' DoIt(f.toStr());',
  24292. ' (f*f).toStr;',
  24293. ' DoIt((f*f).toStr);',
  24294. '']);
  24295. ConvertProgram;
  24296. CheckSource('TestTypeHelper_Double',
  24297. LinesToStr([ // statements
  24298. 'rtl.createHelper(this, "THelper", null, function () {',
  24299. ' this.NPI = 3.141592;',
  24300. ' this.ToStr = function () {',
  24301. ' var Result = "";',
  24302. ' return Result;',
  24303. ' };',
  24304. '});',
  24305. 'this.DoIt = function (s) {',
  24306. '};',
  24307. 'this.f = 0.0;',
  24308. '']),
  24309. LinesToStr([ // $mod.$main
  24310. '$mod.DoIt($mod.THelper.ToStr.call({',
  24311. ' p: $mod,',
  24312. ' get: function () {',
  24313. ' return this.p.f;',
  24314. ' },',
  24315. ' set: function (v) {',
  24316. ' this.p.f = v;',
  24317. ' }',
  24318. '}));',
  24319. '$mod.DoIt($mod.THelper.ToStr.call({',
  24320. ' p: $mod,',
  24321. ' get: function () {',
  24322. ' return this.p.f;',
  24323. ' },',
  24324. ' set: function (v) {',
  24325. ' this.p.f = v;',
  24326. ' }',
  24327. '}));',
  24328. '$mod.THelper.ToStr.call({',
  24329. ' a: $mod.f * $mod.f,',
  24330. ' get: function () {',
  24331. ' return this.a;',
  24332. ' },',
  24333. ' set: function (v) {',
  24334. ' rtl.raiseE("EPropReadOnly");',
  24335. ' }',
  24336. '});',
  24337. '$mod.DoIt($mod.THelper.ToStr.call({',
  24338. ' a: $mod.f * $mod.f,',
  24339. ' get: function () {',
  24340. ' return this.a;',
  24341. ' },',
  24342. ' set: function (v) {',
  24343. ' rtl.raiseE("EPropReadOnly");',
  24344. ' }',
  24345. '}));',
  24346. '']));
  24347. end;
  24348. procedure TTestModule.TestTypeHelper_NativeInt;
  24349. begin
  24350. StartProgram(false);
  24351. Add([
  24352. '{$modeswitch typehelpers}',
  24353. 'type',
  24354. ' MaxInt = type nativeint;',
  24355. ' THelperI = type helper for MaxInt',
  24356. ' function ToStr: String;',
  24357. ' end;',
  24358. ' MaxUInt = type nativeuint;',
  24359. ' THelperU = type helper for MaxUInt',
  24360. ' function ToStr: String;',
  24361. ' end;',
  24362. 'function THelperI.ToStr: String;',
  24363. 'begin',
  24364. ' Result:=str(Self);',
  24365. 'end;',
  24366. 'function THelperU.ToStr: String;',
  24367. 'begin',
  24368. ' Result:=str(Self);',
  24369. 'end;',
  24370. 'procedure DoIt(s: string);',
  24371. 'begin',
  24372. 'end;',
  24373. 'var i: MaxInt;',
  24374. 'begin',
  24375. ' DoIt(i.toStr);',
  24376. ' DoIt(i.toStr());',
  24377. ' (i*i).toStr;',
  24378. ' DoIt((i*i).toStr);',
  24379. '']);
  24380. ConvertProgram;
  24381. CheckSource('TestTypeHelper_NativeInt',
  24382. LinesToStr([ // statements
  24383. 'rtl.createHelper(this, "THelperI", null, function () {',
  24384. ' this.ToStr = function () {',
  24385. ' var Result = "";',
  24386. ' Result = "" + this.get();',
  24387. ' return Result;',
  24388. ' };',
  24389. '});',
  24390. 'rtl.createHelper(this, "THelperU", null, function () {',
  24391. ' this.ToStr = function () {',
  24392. ' var Result = "";',
  24393. ' Result = "" + this.get();',
  24394. ' return Result;',
  24395. ' };',
  24396. '});',
  24397. 'this.DoIt = function (s) {',
  24398. '};',
  24399. 'this.i = 0;',
  24400. '']),
  24401. LinesToStr([ // $mod.$main
  24402. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24403. ' p: $mod,',
  24404. ' get: function () {',
  24405. ' return this.p.i;',
  24406. ' },',
  24407. ' set: function (v) {',
  24408. ' this.p.i = v;',
  24409. ' }',
  24410. '}));',
  24411. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24412. ' p: $mod,',
  24413. ' get: function () {',
  24414. ' return this.p.i;',
  24415. ' },',
  24416. ' set: function (v) {',
  24417. ' this.p.i = v;',
  24418. ' }',
  24419. '}));',
  24420. '$mod.THelperI.ToStr.call({',
  24421. ' a: $mod.i * $mod.i,',
  24422. ' get: function () {',
  24423. ' return this.a;',
  24424. ' },',
  24425. ' set: function (v) {',
  24426. ' rtl.raiseE("EPropReadOnly");',
  24427. ' }',
  24428. '});',
  24429. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24430. ' a: $mod.i * $mod.i,',
  24431. ' get: function () {',
  24432. ' return this.a;',
  24433. ' },',
  24434. ' set: function (v) {',
  24435. ' rtl.raiseE("EPropReadOnly");',
  24436. ' }',
  24437. '}));',
  24438. '']));
  24439. end;
  24440. procedure TTestModule.TestTypeHelper_StringChar;
  24441. begin
  24442. StartProgram(false);
  24443. Add([
  24444. '{$modeswitch typehelpers}',
  24445. 'type',
  24446. ' TStringHelper = type helper for string',
  24447. ' procedure DoIt(e: byte = 123);',
  24448. ' end;',
  24449. ' TCharHelper = type helper for char',
  24450. ' procedure Fly;',
  24451. ' end;',
  24452. 'procedure TStringHelper.DoIt(e: byte);',
  24453. 'begin',
  24454. ' Self[1]:=''c'';',
  24455. ' Self[2]:=Self[3];',
  24456. 'end;',
  24457. 'procedure TCharHelper.Fly;',
  24458. 'begin',
  24459. ' Self:=''c'';',
  24460. 'end;',
  24461. 'begin',
  24462. ' ''abc''.DoIt;',
  24463. ' ''xyz''.DoIt();',
  24464. ' ''c''.Fly();',
  24465. '']);
  24466. ConvertProgram;
  24467. CheckSource('TestTypeHelper_StringChar',
  24468. LinesToStr([ // statements
  24469. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24470. ' this.DoIt = function (e) {',
  24471. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24472. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24473. ' };',
  24474. '});',
  24475. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24476. ' this.Fly = function () {',
  24477. ' this.set("c");',
  24478. ' };',
  24479. '});',
  24480. '']),
  24481. LinesToStr([ // $mod.$main
  24482. '$mod.TStringHelper.DoIt.call({',
  24483. ' get: function () {',
  24484. ' return "abc";',
  24485. ' },',
  24486. ' set: function (v) {',
  24487. ' rtl.raiseE("EPropReadOnly");',
  24488. ' }',
  24489. '}, 123);',
  24490. '$mod.TStringHelper.DoIt.call({',
  24491. ' get: function () {',
  24492. ' return "xyz";',
  24493. ' },',
  24494. ' set: function (v) {',
  24495. ' rtl.raiseE("EPropReadOnly");',
  24496. ' }',
  24497. '}, 123);',
  24498. '$mod.TCharHelper.Fly.call({',
  24499. ' get: function () {',
  24500. ' return "c";',
  24501. ' },',
  24502. ' set: function (v) {',
  24503. ' rtl.raiseE("EPropReadOnly");',
  24504. ' }',
  24505. '});',
  24506. '']));
  24507. end;
  24508. procedure TTestModule.TestTypeHelper_JSValue;
  24509. begin
  24510. StartProgram(false);
  24511. Add([
  24512. '{$modeswitch typehelpers}',
  24513. 'type',
  24514. ' TExtValue = type jsvalue;',
  24515. ' THelper = type helper for TExtValue',
  24516. ' function ToStr: String;',
  24517. ' end;',
  24518. 'function THelper.ToStr: String;',
  24519. 'begin',
  24520. 'end;',
  24521. 'var',
  24522. ' s: string;',
  24523. ' v: TExtValue;',
  24524. 'begin',
  24525. ' s:=v.toStr;',
  24526. ' s:=v.toStr();',
  24527. ' TExtValue(s).toStr;',
  24528. '']);
  24529. ConvertProgram;
  24530. CheckSource('TestTypeHelper_JSValue',
  24531. LinesToStr([ // statements
  24532. 'rtl.createHelper(this, "THelper", null, function () {',
  24533. ' this.ToStr = function () {',
  24534. ' var Result = "";',
  24535. ' return Result;',
  24536. ' };',
  24537. '});',
  24538. 'this.s = "";',
  24539. 'this.v = undefined;',
  24540. '']),
  24541. LinesToStr([ // $mod.$main
  24542. '$mod.s = $mod.THelper.ToStr.call({',
  24543. ' p: $mod,',
  24544. ' get: function () {',
  24545. ' return this.p.v;',
  24546. ' },',
  24547. ' set: function (v) {',
  24548. ' this.p.v = v;',
  24549. ' }',
  24550. '});',
  24551. '$mod.s = $mod.THelper.ToStr.call({',
  24552. ' p: $mod,',
  24553. ' get: function () {',
  24554. ' return this.p.v;',
  24555. ' },',
  24556. ' set: function (v) {',
  24557. ' this.p.v = v;',
  24558. ' }',
  24559. '});',
  24560. '$mod.THelper.ToStr.call({',
  24561. ' p: $mod,',
  24562. ' get: function () {',
  24563. ' return this.p.s;',
  24564. ' },',
  24565. ' set: function (v) {',
  24566. ' rtl.raiseE("EPropReadOnly");',
  24567. ' }',
  24568. '});',
  24569. '']));
  24570. end;
  24571. procedure TTestModule.TestTypeHelper_Array;
  24572. begin
  24573. StartProgram(false);
  24574. Add([
  24575. '{$modeswitch typehelpers}',
  24576. 'type',
  24577. ' TArrOfBool = array of boolean;',
  24578. ' TArrOfJS = array of jsvalue;',
  24579. ' THelper = type helper for TArrOfBool',
  24580. ' procedure DoIt(e: byte = 123);',
  24581. ' end;',
  24582. 'procedure THelper.DoIt(e: byte);',
  24583. 'begin',
  24584. ' Self[1]:=true;',
  24585. ' Self[2]:=not Self[3];',
  24586. ' SetLength(Self,4);',
  24587. 'end;',
  24588. 'var',
  24589. ' b: TArrOfBool;',
  24590. ' j: TArrOfJS;',
  24591. 'begin',
  24592. ' b.DoIt;',
  24593. ' TArrOfBool(j).DoIt();',
  24594. '']);
  24595. ConvertProgram;
  24596. CheckSource('TestTypeHelper_Array',
  24597. LinesToStr([ // statements
  24598. 'rtl.createHelper(this, "THelper", null, function () {',
  24599. ' this.DoIt = function (e) {',
  24600. ' this.get()[1] = true;',
  24601. ' this.get()[2] = !this.get()[3];',
  24602. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24603. ' };',
  24604. '});',
  24605. 'this.b = [];',
  24606. 'this.j = [];',
  24607. '']),
  24608. LinesToStr([ // $mod.$main
  24609. '$mod.THelper.DoIt.call({',
  24610. ' p: $mod,',
  24611. ' get: function () {',
  24612. ' return this.p.b;',
  24613. ' },',
  24614. ' set: function (v) {',
  24615. ' this.p.b = v;',
  24616. ' }',
  24617. '}, 123);',
  24618. '$mod.THelper.DoIt.call({',
  24619. ' p: $mod,',
  24620. ' get: function () {',
  24621. ' return this.p.j;',
  24622. ' },',
  24623. ' set: function (v) {',
  24624. ' this.p.j = v;',
  24625. ' }',
  24626. '}, 123);',
  24627. '']));
  24628. end;
  24629. procedure TTestModule.TestTypeHelper_EnumType;
  24630. begin
  24631. StartProgram(false);
  24632. Add([
  24633. '{$modeswitch typehelpers}',
  24634. 'type',
  24635. ' TEnum = (red,blue);',
  24636. ' THelper = type helper for TEnum',
  24637. ' procedure DoIt(e: byte = 123);',
  24638. ' class procedure Swing(w: word); static;',
  24639. ' end;',
  24640. 'procedure THelper.DoIt(e: byte);',
  24641. 'begin',
  24642. ' Self:=red;',
  24643. ' Self:=succ(Self);',
  24644. ' with Self do Doit;',
  24645. 'end;',
  24646. 'class procedure THelper.Swing(w: word);',
  24647. 'begin',
  24648. 'end;',
  24649. 'var e: TEnum;',
  24650. 'begin',
  24651. ' e.DoIt;',
  24652. ' red.DoIt;',
  24653. ' TEnum.blue.DoIt;',
  24654. ' TEnum(1).DoIt;',
  24655. ' TEnum.Swing(3);',
  24656. '']);
  24657. ConvertProgram;
  24658. CheckSource('TestTypeHelper_EnumType',
  24659. LinesToStr([ // statements
  24660. 'this.TEnum = {',
  24661. ' "0": "red",',
  24662. ' red: 0,',
  24663. ' "1": "blue",',
  24664. ' blue: 1',
  24665. '};',
  24666. 'rtl.createHelper(this, "THelper", null, function () {',
  24667. ' this.DoIt = function (e) {',
  24668. ' this.set($mod.TEnum.red);',
  24669. ' this.set(this.get() + 1);',
  24670. ' var $with = this.get();',
  24671. ' $mod.THelper.DoIt.call(this, 123);',
  24672. ' };',
  24673. ' this.Swing = function (w) {',
  24674. ' };',
  24675. '});',
  24676. 'this.e = 0;',
  24677. '']),
  24678. LinesToStr([ // $mod.$main
  24679. '$mod.THelper.DoIt.call({',
  24680. ' p: $mod,',
  24681. ' get: function () {',
  24682. ' return this.p.e;',
  24683. ' },',
  24684. ' set: function (v) {',
  24685. ' this.p.e = v;',
  24686. ' }',
  24687. '}, 123);',
  24688. '$mod.THelper.DoIt.call({',
  24689. ' p: $mod.TEnum,',
  24690. ' get: function () {',
  24691. ' return this.p.red;',
  24692. ' },',
  24693. ' set: function (v) {',
  24694. ' rtl.raiseE("EPropReadOnly");',
  24695. ' }',
  24696. '}, 123);',
  24697. '$mod.THelper.DoIt.call({',
  24698. ' p: $mod.TEnum,',
  24699. ' get: function () {',
  24700. ' return this.p.blue;',
  24701. ' },',
  24702. ' set: function (v) {',
  24703. ' rtl.raiseE("EPropReadOnly");',
  24704. ' }',
  24705. '}, 123);',
  24706. '$mod.THelper.DoIt.call({',
  24707. ' get: function () {',
  24708. ' return 1;',
  24709. ' },',
  24710. ' set: function (v) {',
  24711. ' rtl.raiseE("EPropReadOnly");',
  24712. ' }',
  24713. '}, 123);',
  24714. '$mod.THelper.Swing(3);',
  24715. '']));
  24716. end;
  24717. procedure TTestModule.TestTypeHelper_SetType;
  24718. begin
  24719. StartProgram(false);
  24720. Add([
  24721. '{$modeswitch typehelpers}',
  24722. 'type',
  24723. ' TEnum = (red,blue);',
  24724. ' TSetOfEnum = set of TEnum;',
  24725. ' THelper = type helper for TSetOfEnum',
  24726. ' procedure DoIt(e: byte = 123);',
  24727. ' constructor Init(e: TEnum);',
  24728. ' constructor InitEmpty;',
  24729. ' end;',
  24730. 'procedure THelper.DoIt(e: byte);',
  24731. 'begin',
  24732. ' Self:=[];',
  24733. ' Self:=[red];',
  24734. ' Include(Self,blue);',
  24735. 'end;',
  24736. 'constructor THelper.Init(e: TEnum);',
  24737. 'begin',
  24738. ' Self:=[];',
  24739. ' Self:=[e];',
  24740. ' Include(Self,blue);',
  24741. 'end;',
  24742. 'constructor THelper.InitEmpty;',
  24743. 'begin',
  24744. 'end;',
  24745. 'var s: TSetOfEnum;',
  24746. 'begin',
  24747. ' s.DoIt;',
  24748. //' [red].DoIt;',
  24749. //' with s do DoIt;',
  24750. //' with [red,blue] do DoIt;',
  24751. ' s:=TSetOfEnum.Init(blue);',
  24752. ' s:=s.Init(blue);',
  24753. '']);
  24754. ConvertProgram;
  24755. CheckSource('TestTypeHelper_SetType',
  24756. LinesToStr([ // statements
  24757. 'this.TEnum = {',
  24758. ' "0": "red",',
  24759. ' red: 0,',
  24760. ' "1": "blue",',
  24761. ' blue: 1',
  24762. '};',
  24763. 'rtl.createHelper(this, "THelper", null, function () {',
  24764. ' this.DoIt = function (e) {',
  24765. ' this.set({});',
  24766. ' this.set(rtl.createSet($mod.TEnum.red));',
  24767. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24768. ' };',
  24769. ' this.Init = function (e) {',
  24770. ' this.set({});',
  24771. ' this.set(rtl.createSet(e));',
  24772. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24773. ' return this.get();',
  24774. ' };',
  24775. ' this.InitEmpty = function () {',
  24776. ' return this.get();',
  24777. ' };',
  24778. ' this.$new = function (fn, args) {',
  24779. ' return this[fn].apply({',
  24780. ' p: {},',
  24781. ' get: function () {',
  24782. ' return this.p;',
  24783. ' },',
  24784. ' set: function (v) {',
  24785. ' this.p = v;',
  24786. ' }',
  24787. ' }, args);',
  24788. ' };',
  24789. '});',
  24790. 'this.s = {};',
  24791. '']),
  24792. LinesToStr([ // $mod.$main
  24793. '$mod.THelper.DoIt.call({',
  24794. ' p: $mod,',
  24795. ' get: function () {',
  24796. ' return this.p.s;',
  24797. ' },',
  24798. ' set: function (v) {',
  24799. ' this.p.s = v;',
  24800. ' }',
  24801. '}, 123);',
  24802. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24803. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24804. ' p: $mod,',
  24805. ' get: function () {',
  24806. ' return this.p.s;',
  24807. ' },',
  24808. ' set: function (v) {',
  24809. ' this.p.s = v;',
  24810. ' }',
  24811. '}, $mod.TEnum.blue));',
  24812. '']));
  24813. end;
  24814. procedure TTestModule.TestTypeHelper_InterfaceType;
  24815. begin
  24816. StartProgram(false);
  24817. Add([
  24818. '{$interfaces com}',
  24819. '{$modeswitch typehelpers}',
  24820. 'type',
  24821. ' IUnknown = interface',
  24822. ' function _AddRef: longint;',
  24823. ' function _Release: longint;',
  24824. ' end;',
  24825. ' TObject = class(IUnknown)',
  24826. ' function _AddRef: longint; virtual; abstract;',
  24827. ' function _Release: longint; virtual; abstract;',
  24828. ' end;',
  24829. ' THelper = type helper for IUnknown',
  24830. ' procedure Fly(e: byte = 123);',
  24831. ' class procedure Run; static;',
  24832. ' end;',
  24833. 'var',
  24834. ' i: IUnknown;',
  24835. ' o: TObject;',
  24836. 'procedure THelper.Fly(e: byte);',
  24837. 'begin',
  24838. ' i:=Self;',
  24839. ' o:=Self as TObject;',
  24840. ' Self:=nil;',
  24841. ' Self:=i;',
  24842. ' Self:=o;',
  24843. ' with Self do begin',
  24844. ' Fly;',
  24845. ' Fly();',
  24846. ' end;',
  24847. 'end;',
  24848. 'class procedure THelper.Run;',
  24849. 'var l: IUnknown;',
  24850. 'begin',
  24851. ' l.Fly;',
  24852. ' l.Fly();',
  24853. 'end;',
  24854. 'begin',
  24855. ' i.Fly;',
  24856. ' i.Fly();',
  24857. ' i.Run;',
  24858. ' i.Run();',
  24859. ' IUnknown.Run;',
  24860. ' IUnknown.Run();',
  24861. '']);
  24862. ConvertProgram;
  24863. CheckSource('TestTypeHelper_InterfaceType',
  24864. LinesToStr([ // statements
  24865. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24866. 'rtl.createClass(this, "TObject", null, function () {',
  24867. ' this.$init = function () {',
  24868. ' };',
  24869. ' this.$final = function () {',
  24870. ' };',
  24871. ' rtl.addIntf(this, $mod.IUnknown);',
  24872. '});',
  24873. 'rtl.createHelper(this, "THelper", null, function () {',
  24874. ' this.Fly = function (e) {',
  24875. ' var $ir = rtl.createIntfRefs();',
  24876. ' try {',
  24877. ' rtl.setIntfP($mod, "i", this.get());',
  24878. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24879. ' this.set(null);',
  24880. ' this.set($mod.i);',
  24881. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24882. ' var $with = this.get();',
  24883. ' $mod.THelper.Fly.call(this, 123);',
  24884. ' $mod.THelper.Fly.call(this, 123);',
  24885. ' } finally {',
  24886. ' $ir.free();',
  24887. ' };',
  24888. ' };',
  24889. ' this.Run = function () {',
  24890. ' var l = null;',
  24891. ' try {',
  24892. ' $mod.THelper.Fly.call({',
  24893. ' get: function () {',
  24894. ' return l;',
  24895. ' },',
  24896. ' set: function (v) {',
  24897. ' l = rtl.setIntfL(l, v);',
  24898. ' }',
  24899. ' }, 123);',
  24900. ' $mod.THelper.Fly.call({',
  24901. ' get: function () {',
  24902. ' return l;',
  24903. ' },',
  24904. ' set: function (v) {',
  24905. ' l = rtl.setIntfL(l, v);',
  24906. ' }',
  24907. ' }, 123);',
  24908. ' } finally {',
  24909. ' rtl._Release(l);',
  24910. ' };',
  24911. ' };',
  24912. '});',
  24913. 'this.i = null;',
  24914. 'this.o = null;',
  24915. '']),
  24916. LinesToStr([ // $mod.$main
  24917. '$mod.THelper.Fly.call({',
  24918. ' p: $mod,',
  24919. ' get: function () {',
  24920. ' return this.p.i;',
  24921. ' },',
  24922. ' set: function (v) {',
  24923. ' rtl.setIntfP(this.p, "i", v);',
  24924. ' }',
  24925. '}, 123);',
  24926. '$mod.THelper.Fly.call({',
  24927. ' p: $mod,',
  24928. ' get: function () {',
  24929. ' return this.p.i;',
  24930. ' },',
  24931. ' set: function (v) {',
  24932. ' rtl.setIntfP(this.p, "i", v);',
  24933. ' }',
  24934. '}, 123);',
  24935. '$mod.THelper.Run();',
  24936. '$mod.THelper.Run();',
  24937. '$mod.THelper.Run();',
  24938. '$mod.THelper.Run();',
  24939. '']));
  24940. end;
  24941. procedure TTestModule.TestTypeHelper_NestedSelf;
  24942. begin
  24943. StartProgram(false);
  24944. Add([
  24945. '{$modeswitch typehelpers}',
  24946. 'type',
  24947. ' THelper = type helper for string',
  24948. ' procedure Run(Value: string);',
  24949. ' end;',
  24950. 'procedure THelper.Run(Value: string);',
  24951. ' function Sub(i: nativeint): boolean;',
  24952. ' begin',
  24953. ' Result:=Self[i+1]=Value[i];',
  24954. ' end;',
  24955. 'begin',
  24956. ' if Self[3]=Value[4] then ;',
  24957. 'end;',
  24958. 'begin',
  24959. '']);
  24960. ConvertProgram;
  24961. CheckSource('TestTypeHelper_NestedSelf',
  24962. LinesToStr([ // statements
  24963. 'rtl.createHelper(this, "THelper", null, function () {',
  24964. ' this.Run = function (Value) {',
  24965. ' var $Self = this;',
  24966. ' function Sub(i) {',
  24967. ' var Result = false;',
  24968. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  24969. ' return Result;',
  24970. ' };',
  24971. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  24972. ' };',
  24973. '});',
  24974. '']),
  24975. LinesToStr([ // $mod.$main
  24976. '']));
  24977. end;
  24978. procedure TTestModule.TestProcType;
  24979. begin
  24980. StartProgram(false);
  24981. Add([
  24982. 'type',
  24983. ' TProcInt = procedure(vI: longint = 1);',
  24984. 'procedure DoIt(vJ: longint);',
  24985. 'begin end;',
  24986. 'var',
  24987. ' b: boolean;',
  24988. ' vP, vQ: tprocint;',
  24989. 'begin',
  24990. ' vp:=nil;',
  24991. ' vp:=vp;',
  24992. ' vp:=@doit;',
  24993. ' vp;',
  24994. ' vp();',
  24995. ' vp(2);',
  24996. ' b:=vp=nil;',
  24997. ' b:=nil=vp;',
  24998. ' b:=vp=vq;',
  24999. ' b:=vp=@doit;',
  25000. ' b:=@doit=vp;',
  25001. ' b:=vp<>nil;',
  25002. ' b:=nil<>vp;',
  25003. ' b:=vp<>vq;',
  25004. ' b:=vp<>@doit;',
  25005. ' b:=@doit<>vp;',
  25006. ' b:=Assigned(vp);',
  25007. ' if Assigned(vp) then ;']);
  25008. ConvertProgram;
  25009. CheckSource('TestProcType',
  25010. LinesToStr([ // statements
  25011. 'this.DoIt = function(vJ) {',
  25012. '};',
  25013. 'this.b = false;',
  25014. 'this.vP = null;',
  25015. 'this.vQ = null;'
  25016. ]),
  25017. LinesToStr([ // $mod.$main
  25018. '$mod.vP = null;',
  25019. '$mod.vP = $mod.vP;',
  25020. '$mod.vP = $mod.DoIt;',
  25021. '$mod.vP(1);',
  25022. '$mod.vP(1);',
  25023. '$mod.vP(2);',
  25024. '$mod.b = $mod.vP === null;',
  25025. '$mod.b = null === $mod.vP;',
  25026. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25027. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25028. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25029. '$mod.b = $mod.vP !== null;',
  25030. '$mod.b = null !== $mod.vP;',
  25031. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25032. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25033. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25034. '$mod.b = $mod.vP != null;',
  25035. 'if ($mod.vP != null) ;',
  25036. '']));
  25037. end;
  25038. procedure TTestModule.TestProcType_Arg;
  25039. begin
  25040. StartProgram(false);
  25041. Add([
  25042. 'type',
  25043. ' TProcInt = procedure(vI: longint = 1);',
  25044. 'procedure DoIt(vJ: longint); begin end;',
  25045. 'procedure DoSome(vP, vQ: TProcInt);',
  25046. 'var',
  25047. ' b: boolean;',
  25048. 'begin',
  25049. ' vp:=nil;',
  25050. ' vp:=vp;',
  25051. ' vp:=@doit;',
  25052. ' vp;',
  25053. ' vp();',
  25054. ' vp(2);',
  25055. ' b:=vp=nil;',
  25056. ' b:=nil=vp;',
  25057. ' b:=vp=vq;',
  25058. ' b:=vp=@doit;',
  25059. ' b:=@doit=vp;',
  25060. ' b:=vp<>nil;',
  25061. ' b:=nil<>vp;',
  25062. ' b:=vp<>vq;',
  25063. ' b:=vp<>@doit;',
  25064. ' b:=@doit<>vp;',
  25065. ' b:=Assigned(vp);',
  25066. ' if Assigned(vp) then ;',
  25067. 'end;',
  25068. 'begin',
  25069. ' DoSome(@DoIt,nil);']);
  25070. ConvertProgram;
  25071. CheckSource('TestProcType_Arg',
  25072. LinesToStr([ // statements
  25073. 'this.DoIt = function(vJ) {',
  25074. '};',
  25075. 'this.DoSome = function(vP, vQ) {',
  25076. ' var b = false;',
  25077. ' vP = null;',
  25078. ' vP = vP;',
  25079. ' vP = $mod.DoIt;',
  25080. ' vP(1);',
  25081. ' vP(1);',
  25082. ' vP(2);',
  25083. ' b = vP === null;',
  25084. ' b = null === vP;',
  25085. ' b = rtl.eqCallback(vP,vQ);',
  25086. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25087. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25088. ' b = vP !== null;',
  25089. ' b = null !== vP;',
  25090. ' b = !rtl.eqCallback(vP, vQ);',
  25091. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25092. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25093. ' b = vP != null;',
  25094. ' if (vP != null) ;',
  25095. '};',
  25096. '']),
  25097. LinesToStr([ // $mod.$main
  25098. '$mod.DoSome($mod.DoIt,null);',
  25099. '']));
  25100. end;
  25101. procedure TTestModule.TestProcType_FunctionFPC;
  25102. begin
  25103. StartProgram(false);
  25104. Add('type');
  25105. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25106. Add('function DoIt(vI: longint): longint;');
  25107. Add('begin end;');
  25108. Add('var');
  25109. Add(' b: boolean;');
  25110. Add(' vP, vQ: tfuncint;');
  25111. Add('begin');
  25112. Add(' vp:=nil;');
  25113. Add(' vp:=vp;');
  25114. Add(' vp:=@doit;'); // ok in fpc and delphi
  25115. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25116. Add(' vp;'); // ok in fpc and delphi
  25117. Add(' vp();');
  25118. Add(' vp(2);');
  25119. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25120. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25121. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25122. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25123. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25124. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25125. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25126. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25127. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25128. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25129. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25130. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25131. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25132. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25133. Add(' b:=Assigned(vp);');
  25134. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25135. Add(' doit(vp());'); // ok in fpc and delphi
  25136. Add(' doit(vp(2));'); // ok in fpc and delphi
  25137. ConvertProgram;
  25138. CheckSource('TestProcType_FunctionFPC',
  25139. LinesToStr([ // statements
  25140. 'this.DoIt = function(vI) {',
  25141. ' var Result = 0;',
  25142. ' return Result;',
  25143. '};',
  25144. 'this.b = false;',
  25145. 'this.vP = null;',
  25146. 'this.vQ = null;'
  25147. ]),
  25148. LinesToStr([ // $mod.$main
  25149. '$mod.vP = null;',
  25150. '$mod.vP = $mod.vP;',
  25151. '$mod.vP = $mod.DoIt;',
  25152. '$mod.vP(1);',
  25153. '$mod.vP(1);',
  25154. '$mod.vP(2);',
  25155. '$mod.b = $mod.vP === null;',
  25156. '$mod.b = null === $mod.vP;',
  25157. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25158. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25159. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25160. '$mod.b = 4 === $mod.vP(1);',
  25161. '$mod.b = $mod.vP !== null;',
  25162. '$mod.b = null !== $mod.vP;',
  25163. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25164. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25165. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25166. '$mod.b = 6 !== $mod.vP(1);',
  25167. '$mod.b = $mod.vP != null;',
  25168. '$mod.DoIt($mod.vP(1));',
  25169. '$mod.DoIt($mod.vP(2));',
  25170. '']));
  25171. end;
  25172. procedure TTestModule.TestProcType_FunctionDelphi;
  25173. begin
  25174. StartProgram(false);
  25175. Add('{$mode Delphi}');
  25176. Add('type');
  25177. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25178. Add('function DoIt(vI: longint): longint;');
  25179. Add('begin end;');
  25180. Add('var');
  25181. Add(' b: boolean;');
  25182. Add(' vP, vQ: tfuncint;');
  25183. Add('begin');
  25184. Add(' vp:=nil;');
  25185. Add(' vp:=vp;');
  25186. Add(' vp:=@doit;'); // ok in fpc and delphi
  25187. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25188. Add(' vp;'); // ok in fpc and delphi
  25189. Add(' vp();');
  25190. Add(' vp(2);');
  25191. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25192. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25193. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25194. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25195. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25196. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25197. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25198. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25199. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25200. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25201. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25202. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25203. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25204. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25205. Add(' b:=Assigned(vp);');
  25206. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25207. Add(' doit(vp());'); // ok in fpc and delphi
  25208. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25209. ConvertProgram;
  25210. CheckSource('TestProcType_FunctionDelphi',
  25211. LinesToStr([ // statements
  25212. 'this.DoIt = function(vI) {',
  25213. ' var Result = 0;',
  25214. ' return Result;',
  25215. '};',
  25216. 'this.b = false;',
  25217. 'this.vP = null;',
  25218. 'this.vQ = null;'
  25219. ]),
  25220. LinesToStr([ // $mod.$main
  25221. '$mod.vP = null;',
  25222. '$mod.vP = $mod.vP;',
  25223. '$mod.vP = $mod.DoIt;',
  25224. '$mod.vP = $mod.DoIt;',
  25225. '$mod.vP(1);',
  25226. '$mod.vP(1);',
  25227. '$mod.vP(2);',
  25228. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25229. '$mod.b = $mod.vP(1) === 3;',
  25230. '$mod.b = 4 === $mod.vP(1);',
  25231. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25232. '$mod.b = $mod.vP(1) !== 5;',
  25233. '$mod.b = 6 !== $mod.vP(1);',
  25234. '$mod.b = $mod.vP != null;',
  25235. '$mod.DoIt($mod.vP(1));',
  25236. '$mod.DoIt($mod.vP(1));',
  25237. '$mod.DoIt($mod.vP(2));',
  25238. '']));
  25239. end;
  25240. procedure TTestModule.TestProcType_ProcedureDelphi;
  25241. begin
  25242. StartProgram(false);
  25243. Add('{$mode Delphi}');
  25244. Add('type');
  25245. Add(' TProc = procedure;');
  25246. Add('procedure DoIt;');
  25247. Add('begin end;');
  25248. Add('var');
  25249. Add(' b: boolean;');
  25250. Add(' vP, vQ: tproc;');
  25251. Add('begin');
  25252. Add(' vp:=nil;');
  25253. Add(' vp:=vp;');
  25254. Add(' vp:=vq;');
  25255. 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
  25256. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25257. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25258. Add(' vp;'); // ok in fpc and delphi
  25259. Add(' vp();');
  25260. // equal
  25261. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25262. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25263. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25264. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25265. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25266. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25267. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25268. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25269. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25270. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25271. // unequal
  25272. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25273. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25274. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25275. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25276. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25277. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25278. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25279. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25280. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25281. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25282. Add(' b:=Assigned(vp);');
  25283. ConvertProgram;
  25284. CheckSource('TestProcType_ProcedureDelphi',
  25285. LinesToStr([ // statements
  25286. 'this.DoIt = function() {',
  25287. '};',
  25288. 'this.b = false;',
  25289. 'this.vP = null;',
  25290. 'this.vQ = null;'
  25291. ]),
  25292. LinesToStr([ // $mod.$main
  25293. '$mod.vP = null;',
  25294. '$mod.vP = $mod.vP;',
  25295. '$mod.vP = $mod.vQ;',
  25296. '$mod.vP = $mod.DoIt;',
  25297. '$mod.vP = $mod.DoIt;',
  25298. '$mod.vP();',
  25299. '$mod.vP();',
  25300. '$mod.b = $mod.vP === null;',
  25301. '$mod.b = null === $mod.vP;',
  25302. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25303. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25304. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25305. '$mod.b = $mod.vP !== null;',
  25306. '$mod.b = null !== $mod.vP;',
  25307. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25308. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25309. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25310. '$mod.b = $mod.vP != null;',
  25311. '']));
  25312. end;
  25313. procedure TTestModule.TestProcType_AsParam;
  25314. begin
  25315. StartProgram(false);
  25316. Add('type');
  25317. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25318. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25319. Add('var vJ: tfuncint;');
  25320. Add('begin');
  25321. Add(' vg:=vg;');
  25322. Add(' vj:=vh;');
  25323. Add(' vi:=vi;');
  25324. Add(' doit(vg,vg,vg);');
  25325. Add(' doit(vh,vh,vj);');
  25326. Add(' doit(vi,vi,vi);');
  25327. Add(' doit(vj,vj,vj);');
  25328. Add('end;');
  25329. Add('var i: tfuncint;');
  25330. Add('begin');
  25331. Add(' doit(i,i,i);');
  25332. ConvertProgram;
  25333. CheckSource('TestProcType_AsParam',
  25334. LinesToStr([ // statements
  25335. 'this.DoIt = function (vG,vH,vI) {',
  25336. ' var vJ = null;',
  25337. ' vG = vG;',
  25338. ' vJ = vH;',
  25339. ' vI.set(vI.get());',
  25340. ' $mod.DoIt(vG, vG, {',
  25341. ' get: function () {',
  25342. ' return vG;',
  25343. ' },',
  25344. ' set: function (v) {',
  25345. ' vG = v;',
  25346. ' }',
  25347. ' });',
  25348. ' $mod.DoIt(vH, vH, {',
  25349. ' get: function () {',
  25350. ' return vJ;',
  25351. ' },',
  25352. ' set: function (v) {',
  25353. ' vJ = v;',
  25354. ' }',
  25355. ' });',
  25356. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25357. ' $mod.DoIt(vJ, vJ, {',
  25358. ' get: function () {',
  25359. ' return vJ;',
  25360. ' },',
  25361. ' set: function (v) {',
  25362. ' vJ = v;',
  25363. ' }',
  25364. ' });',
  25365. '};',
  25366. 'this.i = null;'
  25367. ]),
  25368. LinesToStr([
  25369. '$mod.DoIt($mod.i,$mod.i,{',
  25370. ' p: $mod,',
  25371. ' get: function () {',
  25372. ' return this.p.i;',
  25373. ' },',
  25374. ' set: function (v) {',
  25375. ' this.p.i = v;',
  25376. ' }',
  25377. '});'
  25378. ]));
  25379. end;
  25380. procedure TTestModule.TestProcType_MethodFPC;
  25381. begin
  25382. StartProgram(false);
  25383. Add('type');
  25384. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25385. Add(' TObject = class');
  25386. Add(' function DoIt(vA: longint = 1): longint;');
  25387. Add(' end;');
  25388. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25389. Add('begin');
  25390. Add('end;');
  25391. Add('var');
  25392. Add(' Obj: TObject;');
  25393. Add(' vP: tfuncint;');
  25394. Add(' b: boolean;');
  25395. Add('begin');
  25396. Add(' vp:[email protected];'); // ok in fpc and delphi
  25397. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25398. Add(' vp;'); // ok in fpc and delphi
  25399. Add(' vp();');
  25400. Add(' vp(2);');
  25401. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25402. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25403. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25404. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25405. ConvertProgram;
  25406. CheckSource('TestProcType_MethodFPC',
  25407. LinesToStr([ // statements
  25408. 'rtl.createClass(this, "TObject", null, function () {',
  25409. ' this.$init = function () {',
  25410. ' };',
  25411. ' this.$final = function () {',
  25412. ' };',
  25413. ' this.DoIt = function (vA) {',
  25414. ' var Result = 0;',
  25415. ' return Result;',
  25416. ' };',
  25417. '});',
  25418. 'this.Obj = null;',
  25419. 'this.vP = null;',
  25420. 'this.b = false;'
  25421. ]),
  25422. LinesToStr([
  25423. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25424. '$mod.vP(1);',
  25425. '$mod.vP(1);',
  25426. '$mod.vP(2);',
  25427. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25428. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25429. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25430. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25431. '']));
  25432. end;
  25433. procedure TTestModule.TestProcType_MethodDelphi;
  25434. begin
  25435. StartProgram(false);
  25436. Add([
  25437. '{$mode delphi}',
  25438. 'type',
  25439. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25440. ' TObject = class',
  25441. ' function DoIt(vA: longint = 1): longint;',
  25442. ' end;',
  25443. 'function TObject.DoIt(vA: longint = 1): longint;',
  25444. 'begin',
  25445. 'end;',
  25446. 'var',
  25447. ' Obj: TObject;',
  25448. ' vP: tfuncint;',
  25449. ' b: boolean;',
  25450. 'begin',
  25451. ' vp:[email protected];', // ok in fpc and delphi
  25452. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25453. ' vp;', // ok in fpc and delphi
  25454. ' vp();',
  25455. ' vp(2);',
  25456. //' b:[email protected];', // ok in fpc, illegal in delphi
  25457. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25458. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25459. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25460. '']);
  25461. ConvertProgram;
  25462. CheckSource('TestProcType_MethodDelphi',
  25463. LinesToStr([ // statements
  25464. 'rtl.createClass(this, "TObject", null, function () {',
  25465. ' this.$init = function () {',
  25466. ' };',
  25467. ' this.$final = function () {',
  25468. ' };',
  25469. ' this.DoIt = function (vA) {',
  25470. ' var Result = 0;',
  25471. ' return Result;',
  25472. ' };',
  25473. '});',
  25474. 'this.Obj = null;',
  25475. 'this.vP = null;',
  25476. 'this.b = false;'
  25477. ]),
  25478. LinesToStr([
  25479. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25480. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25481. '$mod.vP(1);',
  25482. '$mod.vP(1);',
  25483. '$mod.vP(2);',
  25484. '']));
  25485. end;
  25486. procedure TTestModule.TestProcType_PropertyFPC;
  25487. begin
  25488. StartProgram(false);
  25489. Add('type');
  25490. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25491. Add(' TObject = class');
  25492. Add(' FOnFoo: TFuncInt;');
  25493. Add(' function DoIt(vA: longint = 1): longint;');
  25494. Add(' function GetFoo: TFuncInt;');
  25495. Add(' procedure SetFoo(const Value: TFuncInt);');
  25496. Add(' function GetEvents(Index: longint): TFuncInt;');
  25497. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25498. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25499. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25500. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25501. Add(' end;');
  25502. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25503. Add('function tobject.getfoo: tfuncint; begin end;');
  25504. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25505. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25506. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25507. Add('var');
  25508. Add(' Obj: TObject;');
  25509. Add(' vP: tfuncint;');
  25510. Add(' b: boolean;');
  25511. Add('begin');
  25512. Add(' obj.onfoo:=nil;');
  25513. Add(' obj.onbar:=nil;');
  25514. Add(' obj.events[1]:=nil;');
  25515. Add(' obj.onfoo:=obj.onfoo;');
  25516. Add(' obj.onbar:=obj.onbar;');
  25517. Add(' obj.events[2]:=obj.events[3];');
  25518. Add(' obj.onfoo:[email protected];');
  25519. Add(' obj.onbar:[email protected];');
  25520. Add(' obj.events[4]:[email protected];');
  25521. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25522. //Add(' obj.onbar:=obj.doit;'); // delphi
  25523. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25524. Add(' obj.onfoo;');
  25525. Add(' obj.onbar;');
  25526. //Add(' obj.events[5];'); ToDo in pasresolver
  25527. Add(' obj.onfoo();');
  25528. Add(' obj.onbar();');
  25529. Add(' obj.events[6]();');
  25530. Add(' b:=obj.onfoo=nil;');
  25531. Add(' b:=obj.onbar=nil;');
  25532. Add(' b:=obj.events[7]=nil;');
  25533. Add(' b:=obj.onfoo<>nil;');
  25534. Add(' b:=obj.onbar<>nil;');
  25535. Add(' b:=obj.events[8]<>nil;');
  25536. Add(' b:=obj.onfoo=vp;');
  25537. Add(' b:=obj.onbar=vp;');
  25538. Add(' b:=obj.events[9]=vp;');
  25539. Add(' b:=obj.onfoo=obj.onfoo;');
  25540. Add(' b:=obj.onbar=obj.onfoo;');
  25541. Add(' b:=obj.events[10]=obj.onfoo;');
  25542. Add(' b:=obj.onfoo<>obj.onfoo;');
  25543. Add(' b:=obj.onbar<>obj.onfoo;');
  25544. Add(' b:=obj.events[11]<>obj.onfoo;');
  25545. Add(' b:[email protected];');
  25546. Add(' b:[email protected];');
  25547. Add(' b:=obj.events[12][email protected];');
  25548. Add(' b:=obj.onfoo<>@obj.doit;');
  25549. Add(' b:=obj.onbar<>@obj.doit;');
  25550. Add(' b:=obj.events[12]<>@obj.doit;');
  25551. Add(' b:=Assigned(obj.onfoo);');
  25552. Add(' b:=Assigned(obj.onbar);');
  25553. Add(' b:=Assigned(obj.events[13]);');
  25554. ConvertProgram;
  25555. CheckSource('TestProcType_PropertyFPC',
  25556. LinesToStr([ // statements
  25557. 'rtl.createClass(this, "TObject", null, function () {',
  25558. ' this.$init = function () {',
  25559. ' this.FOnFoo = null;',
  25560. ' };',
  25561. ' this.$final = function () {',
  25562. ' this.FOnFoo = undefined;',
  25563. ' };',
  25564. ' this.DoIt = function (vA) {',
  25565. ' var Result = 0;',
  25566. ' return Result;',
  25567. ' };',
  25568. 'this.GetFoo = function () {',
  25569. ' var Result = null;',
  25570. ' return Result;',
  25571. '};',
  25572. 'this.SetFoo = function (Value) {',
  25573. '};',
  25574. 'this.GetEvents = function (Index) {',
  25575. ' var Result = null;',
  25576. ' return Result;',
  25577. '};',
  25578. 'this.SetEvents = function (Index, Value) {',
  25579. '};',
  25580. '});',
  25581. 'this.Obj = null;',
  25582. 'this.vP = null;',
  25583. 'this.b = false;'
  25584. ]),
  25585. LinesToStr([
  25586. '$mod.Obj.FOnFoo = null;',
  25587. '$mod.Obj.SetFoo(null);',
  25588. '$mod.Obj.SetEvents(1, null);',
  25589. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25590. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25591. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25592. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25593. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25594. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25595. '$mod.Obj.FOnFoo(1);',
  25596. '$mod.Obj.GetFoo();',
  25597. '$mod.Obj.FOnFoo(1);',
  25598. '$mod.Obj.GetFoo()(1);',
  25599. '$mod.Obj.GetEvents(6)(1);',
  25600. '$mod.b = $mod.Obj.FOnFoo === null;',
  25601. '$mod.b = $mod.Obj.GetFoo() === null;',
  25602. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25603. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25604. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25605. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25606. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25607. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25608. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25609. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25610. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25611. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25612. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25613. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25614. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25615. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25616. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25617. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25618. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25619. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25620. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25621. '$mod.b = $mod.Obj.FOnFoo != null;',
  25622. '$mod.b = $mod.Obj.GetFoo() != null;',
  25623. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25624. '']));
  25625. end;
  25626. procedure TTestModule.TestProcType_PropertyDelphi;
  25627. begin
  25628. StartProgram(false);
  25629. Add('{$mode delphi}');
  25630. Add('type');
  25631. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25632. Add(' TObject = class');
  25633. Add(' FOnFoo: TFuncInt;');
  25634. Add(' function DoIt(vA: longint = 1): longint;');
  25635. Add(' function GetFoo: TFuncInt;');
  25636. Add(' procedure SetFoo(const Value: TFuncInt);');
  25637. Add(' function GetEvents(Index: longint): TFuncInt;');
  25638. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25639. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25640. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25641. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25642. Add(' end;');
  25643. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25644. Add('function tobject.getfoo: tfuncint; begin end;');
  25645. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25646. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25647. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25648. Add('var');
  25649. Add(' Obj: TObject;');
  25650. Add(' vP: tfuncint;');
  25651. Add(' b: boolean;');
  25652. Add('begin');
  25653. Add(' obj.onfoo:=nil;');
  25654. Add(' obj.onbar:=nil;');
  25655. Add(' obj.events[1]:=nil;');
  25656. Add(' obj.onfoo:=obj.onfoo;');
  25657. Add(' obj.onbar:=obj.onbar;');
  25658. Add(' obj.events[2]:=obj.events[3];');
  25659. Add(' obj.onfoo:[email protected];');
  25660. Add(' obj.onbar:[email protected];');
  25661. Add(' obj.events[4]:[email protected];');
  25662. Add(' obj.onfoo:=obj.doit;'); // delphi
  25663. Add(' obj.onbar:=obj.doit;'); // delphi
  25664. Add(' obj.events[4]:=obj.doit;'); // delphi
  25665. Add(' obj.onfoo;');
  25666. Add(' obj.onbar;');
  25667. //Add(' obj.events[5];'); ToDo in pasresolver
  25668. Add(' obj.onfoo();');
  25669. Add(' obj.onbar();');
  25670. Add(' obj.events[6]();');
  25671. //Add(' b:=obj.onfoo=nil;'); // fpc
  25672. //Add(' b:=obj.onbar=nil;'); // fpc
  25673. //Add(' b:=obj.events[7]=nil;'); // fpc
  25674. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25675. //Add(' b:=obj.onbar<>nil;'); // fpc
  25676. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25677. Add(' b:=obj.onfoo=vp;');
  25678. Add(' b:=obj.onbar=vp;');
  25679. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25680. Add(' b:=obj.onfoo=obj.onfoo;');
  25681. Add(' b:=obj.onbar=obj.onfoo;');
  25682. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25683. Add(' b:=obj.onfoo<>obj.onfoo;');
  25684. Add(' b:=obj.onbar<>obj.onfoo;');
  25685. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25686. //Add(' b:[email protected];'); // fpc
  25687. //Add(' b:[email protected];'); // fpc
  25688. //Add(' b:=obj.events[12][email protected];'); // fpc
  25689. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25690. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25691. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25692. Add(' b:=Assigned(obj.onfoo);');
  25693. Add(' b:=Assigned(obj.onbar);');
  25694. Add(' b:=Assigned(obj.events[13]);');
  25695. ConvertProgram;
  25696. CheckSource('TestProcType_PropertyDelphi',
  25697. LinesToStr([ // statements
  25698. 'rtl.createClass(this, "TObject", null, function () {',
  25699. ' this.$init = function () {',
  25700. ' this.FOnFoo = null;',
  25701. ' };',
  25702. ' this.$final = function () {',
  25703. ' this.FOnFoo = undefined;',
  25704. ' };',
  25705. ' this.DoIt = function (vA) {',
  25706. ' var Result = 0;',
  25707. ' return Result;',
  25708. ' };',
  25709. 'this.GetFoo = function () {',
  25710. ' var Result = null;',
  25711. ' return Result;',
  25712. '};',
  25713. 'this.SetFoo = function (Value) {',
  25714. '};',
  25715. 'this.GetEvents = function (Index) {',
  25716. ' var Result = null;',
  25717. ' return Result;',
  25718. '};',
  25719. 'this.SetEvents = function (Index, Value) {',
  25720. '};',
  25721. '});',
  25722. 'this.Obj = null;',
  25723. 'this.vP = null;',
  25724. 'this.b = false;'
  25725. ]),
  25726. LinesToStr([
  25727. '$mod.Obj.FOnFoo = null;',
  25728. '$mod.Obj.SetFoo(null);',
  25729. '$mod.Obj.SetEvents(1, null);',
  25730. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25731. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25732. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25733. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25734. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25735. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25736. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25737. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25738. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25739. '$mod.Obj.FOnFoo(1);',
  25740. '$mod.Obj.GetFoo();',
  25741. '$mod.Obj.FOnFoo(1);',
  25742. '$mod.Obj.GetFoo()(1);',
  25743. '$mod.Obj.GetEvents(6)(1);',
  25744. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25745. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25746. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25747. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25748. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25749. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25750. '$mod.b = $mod.Obj.FOnFoo != null;',
  25751. '$mod.b = $mod.Obj.GetFoo() != null;',
  25752. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25753. '']));
  25754. end;
  25755. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25756. begin
  25757. StartProgram(false);
  25758. Add('type');
  25759. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25760. Add(' TObject = class');
  25761. Add(' FOnFoo: TFuncInt;');
  25762. Add(' function DoIt(vA: longint = 1): longint;');
  25763. Add(' function GetFoo: TFuncInt;');
  25764. Add(' procedure SetFoo(const Value: TFuncInt);');
  25765. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25766. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25767. Add(' end;');
  25768. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25769. Add('function tobject.getfoo: tfuncint; begin end;');
  25770. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25771. Add('var');
  25772. Add(' Obj: TObject;');
  25773. Add(' vP: tfuncint;');
  25774. Add(' b: boolean;');
  25775. Add('begin');
  25776. Add('with obj do begin');
  25777. Add(' fonfoo:=nil;');
  25778. Add(' onfoo:=nil;');
  25779. Add(' onbar:=nil;');
  25780. Add(' fonfoo:=fonfoo;');
  25781. Add(' onfoo:=onfoo;');
  25782. Add(' onbar:=onbar;');
  25783. Add(' fonfoo:=@doit;');
  25784. Add(' onfoo:=@doit;');
  25785. Add(' onbar:=@doit;');
  25786. //Add(' fonfoo:=doit;'); // delphi
  25787. //Add(' onfoo:=doit;'); // delphi
  25788. //Add(' onbar:=doit;'); // delphi
  25789. Add(' fonfoo;');
  25790. Add(' onfoo;');
  25791. Add(' onbar;');
  25792. Add(' fonfoo();');
  25793. Add(' onfoo();');
  25794. Add(' onbar();');
  25795. Add(' b:=fonfoo=nil;');
  25796. Add(' b:=onfoo=nil;');
  25797. Add(' b:=onbar=nil;');
  25798. Add(' b:=fonfoo<>nil;');
  25799. Add(' b:=onfoo<>nil;');
  25800. Add(' b:=onbar<>nil;');
  25801. Add(' b:=fonfoo=vp;');
  25802. Add(' b:=onfoo=vp;');
  25803. Add(' b:=onbar=vp;');
  25804. Add(' b:=fonfoo=fonfoo;');
  25805. Add(' b:=onfoo=onfoo;');
  25806. Add(' b:=onbar=onfoo;');
  25807. Add(' b:=fonfoo<>fonfoo;');
  25808. Add(' b:=onfoo<>onfoo;');
  25809. Add(' b:=onbar<>onfoo;');
  25810. Add(' b:=fonfoo=@doit;');
  25811. Add(' b:=onfoo=@doit;');
  25812. Add(' b:=onbar=@doit;');
  25813. Add(' b:=fonfoo<>@doit;');
  25814. Add(' b:=onfoo<>@doit;');
  25815. Add(' b:=onbar<>@doit;');
  25816. Add(' b:=Assigned(fonfoo);');
  25817. Add(' b:=Assigned(onfoo);');
  25818. Add(' b:=Assigned(onbar);');
  25819. Add('end;');
  25820. ConvertProgram;
  25821. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25822. LinesToStr([ // statements
  25823. 'rtl.createClass(this, "TObject", null, function () {',
  25824. ' this.$init = function () {',
  25825. ' this.FOnFoo = null;',
  25826. ' };',
  25827. ' this.$final = function () {',
  25828. ' this.FOnFoo = undefined;',
  25829. ' };',
  25830. ' this.DoIt = function (vA) {',
  25831. ' var Result = 0;',
  25832. ' return Result;',
  25833. ' };',
  25834. ' this.GetFoo = function () {',
  25835. ' var Result = null;',
  25836. ' return Result;',
  25837. ' };',
  25838. ' this.SetFoo = function (Value) {',
  25839. ' };',
  25840. '});',
  25841. 'this.Obj = null;',
  25842. 'this.vP = null;',
  25843. 'this.b = false;'
  25844. ]),
  25845. LinesToStr([
  25846. 'var $with = $mod.Obj;',
  25847. '$with.FOnFoo = null;',
  25848. '$with.FOnFoo = null;',
  25849. '$with.SetFoo(null);',
  25850. '$with.FOnFoo = $with.FOnFoo;',
  25851. '$with.FOnFoo = $with.FOnFoo;',
  25852. '$with.SetFoo($with.GetFoo());',
  25853. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25854. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25855. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  25856. '$with.FOnFoo(1);',
  25857. '$with.FOnFoo(1);',
  25858. '$with.GetFoo();',
  25859. '$with.FOnFoo(1);',
  25860. '$with.FOnFoo(1);',
  25861. '$with.GetFoo()(1);',
  25862. '$mod.b = $with.FOnFoo === null;',
  25863. '$mod.b = $with.FOnFoo === null;',
  25864. '$mod.b = $with.GetFoo() === null;',
  25865. '$mod.b = $with.FOnFoo !== null;',
  25866. '$mod.b = $with.FOnFoo !== null;',
  25867. '$mod.b = $with.GetFoo() !== null;',
  25868. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25869. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25870. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  25871. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25872. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25873. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25874. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25875. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25876. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25877. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25878. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25879. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25880. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25881. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25882. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25883. '$mod.b = $with.FOnFoo != null;',
  25884. '$mod.b = $with.FOnFoo != null;',
  25885. '$mod.b = $with.GetFoo() != null;',
  25886. '']));
  25887. end;
  25888. procedure TTestModule.TestProcType_Nested;
  25889. begin
  25890. StartProgram(false);
  25891. Add([
  25892. 'type',
  25893. ' TProcInt = procedure(vI: longint = 1);',
  25894. 'procedure DoIt(vJ: longint);',
  25895. 'var aProc: TProcInt;',
  25896. ' b: boolean;',
  25897. ' procedure Sub(vK: longint);',
  25898. ' var aSub: TProcInt;',
  25899. ' procedure SubSub(vK: longint);',
  25900. ' var aSubSub: TProcInt;',
  25901. ' begin;',
  25902. ' aProc:=@DoIt;',
  25903. ' aSub:=@DoIt;',
  25904. ' aSubSub:=@DoIt;',
  25905. ' aProc:=@Sub;',
  25906. ' aSub:=@Sub;',
  25907. ' aSubSub:=@Sub;',
  25908. ' aProc:=@SubSub;',
  25909. ' aSub:=@SubSub;',
  25910. ' aSubSub:=@SubSub;',
  25911. ' end;',
  25912. ' begin;',
  25913. ' end;',
  25914. 'begin;',
  25915. ' aProc:=@Sub;',
  25916. ' b:=aProc=@Sub;',
  25917. ' b:=@Sub=aProc;',
  25918. 'end;',
  25919. 'begin',
  25920. '']);
  25921. ConvertProgram;
  25922. CheckSource('TestProcType_Nested',
  25923. LinesToStr([ // statements
  25924. 'this.DoIt = function (vJ) {',
  25925. ' var aProc = null;',
  25926. ' var b = false;',
  25927. ' function Sub(vK) {',
  25928. ' var aSub = null;',
  25929. ' function SubSub(vK) {',
  25930. ' var aSubSub = null;',
  25931. ' aProc = $mod.DoIt;',
  25932. ' aSub = $mod.DoIt;',
  25933. ' aSubSub = $mod.DoIt;',
  25934. ' aProc = Sub;',
  25935. ' aSub = Sub;',
  25936. ' aSubSub = Sub;',
  25937. ' aProc = SubSub;',
  25938. ' aSub = SubSub;',
  25939. ' aSubSub = SubSub;',
  25940. ' };',
  25941. ' };',
  25942. ' aProc = Sub;',
  25943. ' b = rtl.eqCallback(aProc, Sub);',
  25944. ' b = rtl.eqCallback(Sub, aProc);',
  25945. '};',
  25946. '']),
  25947. LinesToStr([ // $mod.$main
  25948. '']));
  25949. end;
  25950. procedure TTestModule.TestProcType_NestedOfObject;
  25951. begin
  25952. StartProgram(false);
  25953. Add([
  25954. 'type',
  25955. ' TProcInt = procedure(vI: longint = 1) of object;',
  25956. ' TObject = class',
  25957. ' procedure DoIt(vJ: longint);',
  25958. ' end;',
  25959. 'procedure TObject.DoIt(vJ: longint);',
  25960. 'var aProc: TProcInt;',
  25961. ' b: boolean;',
  25962. ' procedure Sub(vK: longint);',
  25963. ' var aSub: TProcInt;',
  25964. ' procedure SubSub(vK: longint);',
  25965. ' var aSubSub: TProcInt;',
  25966. ' begin;',
  25967. ' aProc:=@DoIt;',
  25968. ' aSub:=@DoIt;',
  25969. ' aSubSub:=@DoIt;',
  25970. ' aProc:=@Sub;',
  25971. ' aSub:=@Sub;',
  25972. ' aSubSub:=@Sub;',
  25973. ' aProc:=@SubSub;',
  25974. ' aSub:=@SubSub;',
  25975. ' aSubSub:=@SubSub;',
  25976. ' end;',
  25977. ' begin;',
  25978. ' end;',
  25979. 'begin;',
  25980. ' aProc:=@Sub;',
  25981. ' b:=aProc=@Sub;',
  25982. ' b:=@Sub=aProc;',
  25983. 'end;',
  25984. 'begin',
  25985. '']);
  25986. ConvertProgram;
  25987. CheckSource('TestProcType_Nested',
  25988. LinesToStr([ // statements
  25989. 'rtl.createClass(this, "TObject", null, function () {',
  25990. ' this.$init = function () {',
  25991. ' };',
  25992. ' this.$final = function () {',
  25993. ' };',
  25994. ' this.DoIt = function (vJ) {',
  25995. ' var $Self = this;',
  25996. ' var aProc = null;',
  25997. ' var b = false;',
  25998. ' function Sub(vK) {',
  25999. ' var aSub = null;',
  26000. ' function SubSub(vK) {',
  26001. ' var aSubSub = null;',
  26002. ' aProc = rtl.createCallback($Self, "DoIt");',
  26003. ' aSub = rtl.createCallback($Self, "DoIt");',
  26004. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26005. ' aProc = Sub;',
  26006. ' aSub = Sub;',
  26007. ' aSubSub = Sub;',
  26008. ' aProc = SubSub;',
  26009. ' aSub = SubSub;',
  26010. ' aSubSub = SubSub;',
  26011. ' };',
  26012. ' };',
  26013. ' aProc = Sub;',
  26014. ' b = rtl.eqCallback(aProc, Sub);',
  26015. ' b = rtl.eqCallback(Sub, aProc);',
  26016. ' };',
  26017. '});',
  26018. '']),
  26019. LinesToStr([ // $mod.$main
  26020. '']));
  26021. end;
  26022. procedure TTestModule.TestProcType_ReferenceToProc;
  26023. begin
  26024. StartProgram(false);
  26025. Add([
  26026. 'type',
  26027. ' TProcRef = reference to procedure(i: longint = 0);',
  26028. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26029. 'var',
  26030. ' p: TProcRef;',
  26031. ' f: TFuncRef;',
  26032. 'procedure DoIt(i: longint);',
  26033. 'begin',
  26034. 'end;',
  26035. 'function GetIt(i: longint): longint;',
  26036. 'begin',
  26037. ' p:=@DoIt;',
  26038. ' f:=@GetIt;',
  26039. ' f;',
  26040. ' f();',
  26041. ' f(1);',
  26042. 'end;',
  26043. 'begin',
  26044. ' p:=@DoIt;',
  26045. ' f:=@GetIt;',
  26046. ' f;',
  26047. ' f();',
  26048. ' f(1);',
  26049. ' p:=TProcRef(f);',
  26050. '']);
  26051. ConvertProgram;
  26052. CheckSource('TestProcType_ReferenceToProc',
  26053. LinesToStr([ // statements
  26054. 'this.p = null;',
  26055. 'this.f = null;',
  26056. 'this.DoIt = function (i) {',
  26057. '};',
  26058. 'this.GetIt = function (i) {',
  26059. ' var Result = 0;',
  26060. ' $mod.p = $mod.DoIt;',
  26061. ' $mod.f = $mod.GetIt;',
  26062. ' $mod.f(0);',
  26063. ' $mod.f(0);',
  26064. ' $mod.f(1);',
  26065. ' return Result;',
  26066. '};',
  26067. '']),
  26068. LinesToStr([ // $mod.$main
  26069. '$mod.p = $mod.DoIt;',
  26070. '$mod.f = $mod.GetIt;',
  26071. '$mod.f(0);',
  26072. '$mod.f(0);',
  26073. '$mod.f(1);',
  26074. '$mod.p = $mod.f;',
  26075. '']));
  26076. end;
  26077. procedure TTestModule.TestProcType_ReferenceToMethod;
  26078. begin
  26079. StartProgram(false);
  26080. Add([
  26081. 'type',
  26082. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26083. ' TObject = class',
  26084. ' function Grow(s: longint): longint;',
  26085. ' end;',
  26086. 'var',
  26087. ' f: tfuncref;',
  26088. 'function tobject.grow(s: longint): longint;',
  26089. ' function GrowSub(i: longint): longint;',
  26090. ' begin',
  26091. ' f:=@grow;',
  26092. ' f:=@growsub;',
  26093. ' end;',
  26094. 'begin',
  26095. ' f:=@grow;',
  26096. ' f:=@growsub;',
  26097. 'end;',
  26098. 'begin',
  26099. '']);
  26100. ConvertProgram;
  26101. CheckSource('TestProcType_ReferenceToMethod',
  26102. LinesToStr([ // statements
  26103. 'rtl.createClass(this, "TObject", null, function () {',
  26104. ' this.$init = function () {',
  26105. ' };',
  26106. ' this.$final = function () {',
  26107. ' };',
  26108. ' this.Grow = function (s) {',
  26109. ' var $Self = this;',
  26110. ' var Result = 0;',
  26111. ' function GrowSub(i) {',
  26112. ' var Result = 0;',
  26113. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26114. ' $mod.f = GrowSub;',
  26115. ' return Result;',
  26116. ' };',
  26117. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26118. ' $mod.f = GrowSub;',
  26119. ' return Result;',
  26120. ' };',
  26121. '});',
  26122. 'this.f = null;',
  26123. '']),
  26124. LinesToStr([ // $mod.$main
  26125. '']));
  26126. end;
  26127. procedure TTestModule.TestProcType_Typecast;
  26128. begin
  26129. StartProgram(false);
  26130. Add([
  26131. 'type',
  26132. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26133. ' TEvent = procedure of object;',
  26134. ' TGetter = function:longint of object;',
  26135. ' TProcA = procedure(i: longint);',
  26136. ' TFuncB = function(i, j: longint): longint;',
  26137. 'procedure DoIt(); varargs; begin end;',
  26138. 'var',
  26139. ' Notify: tnotifyevent;',
  26140. ' Event: tevent;',
  26141. ' Getter: tgetter;',
  26142. ' ProcA: tproca;',
  26143. ' FuncB: tfuncb;',
  26144. ' p: pointer;',
  26145. 'begin',
  26146. ' notify:=tnotifyevent(event);',
  26147. ' event:=tevent(event);',
  26148. ' event:=tevent(notify);',
  26149. ' event:=tevent(getter);',
  26150. ' event:=tevent(proca);',
  26151. ' proca:=tproca(funcb);',
  26152. ' funcb:=tfuncb(funcb);',
  26153. ' funcb:=tfuncb(proca);',
  26154. ' funcb:=tfuncb(getter);',
  26155. ' proca:=tproca(p);',
  26156. ' funcb:=tfuncb(p);',
  26157. ' getter:=tgetter(p);',
  26158. ' p:=pointer(notify);',
  26159. ' p:=notify;',
  26160. ' p:=pointer(proca);',
  26161. ' p:=proca;',
  26162. ' p:=pointer(funcb);',
  26163. ' p:=funcb;',
  26164. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26165. '']);
  26166. ConvertProgram;
  26167. CheckSource('TestProcType_Typecast',
  26168. LinesToStr([ // statements
  26169. 'this.DoIt = function () {',
  26170. '};',
  26171. 'this.Notify = null;',
  26172. 'this.Event = null;',
  26173. 'this.Getter = null;',
  26174. 'this.ProcA = null;',
  26175. 'this.FuncB = null;',
  26176. 'this.p = null;',
  26177. '']),
  26178. LinesToStr([ // $mod.$main
  26179. '$mod.Notify = $mod.Event;',
  26180. '$mod.Event = $mod.Event;',
  26181. '$mod.Event = $mod.Notify;',
  26182. '$mod.Event = $mod.Getter;',
  26183. '$mod.Event = $mod.ProcA;',
  26184. '$mod.ProcA = $mod.FuncB;',
  26185. '$mod.FuncB = $mod.FuncB;',
  26186. '$mod.FuncB = $mod.ProcA;',
  26187. '$mod.FuncB = $mod.Getter;',
  26188. '$mod.ProcA = $mod.p;',
  26189. '$mod.FuncB = $mod.p;',
  26190. '$mod.Getter = $mod.p;',
  26191. '$mod.p = $mod.Notify;',
  26192. '$mod.p = $mod.Notify;',
  26193. '$mod.p = $mod.ProcA;',
  26194. '$mod.p = $mod.ProcA;',
  26195. '$mod.p = $mod.FuncB;',
  26196. '$mod.p = $mod.FuncB;',
  26197. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26198. '']));
  26199. end;
  26200. procedure TTestModule.TestProcType_PassProcToUntyped;
  26201. begin
  26202. StartProgram(false);
  26203. Add([
  26204. 'type',
  26205. ' TEvent = procedure of object;',
  26206. ' TFunc = function: longint;',
  26207. 'procedure DoIt(); varargs; begin end;',
  26208. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26209. 'var',
  26210. ' Event: tevent;',
  26211. ' Func: TFunc;',
  26212. 'begin',
  26213. ' doit(event,func);',
  26214. ' dosome(event,event,event);',
  26215. ' dosome(func,func,func);',
  26216. '']);
  26217. ConvertProgram;
  26218. CheckSource('TestProcType_PassProcToUntyped',
  26219. LinesToStr([ // statements
  26220. 'this.DoIt = function () {',
  26221. '};',
  26222. 'this.DoSome = function (a, b, p) {',
  26223. '};',
  26224. 'this.Event = null;',
  26225. 'this.Func = null;',
  26226. '']),
  26227. LinesToStr([ // $mod.$main
  26228. '$mod.DoIt($mod.Event, $mod.Func);',
  26229. '$mod.DoSome($mod.Event, {',
  26230. ' p: $mod,',
  26231. ' get: function () {',
  26232. ' return this.p.Event;',
  26233. ' },',
  26234. ' set: function (v) {',
  26235. ' this.p.Event = v;',
  26236. ' }',
  26237. '}, $mod.Event);',
  26238. '$mod.DoSome($mod.Func, {',
  26239. ' p: $mod,',
  26240. ' get: function () {',
  26241. ' return this.p.Func;',
  26242. ' },',
  26243. ' set: function (v) {',
  26244. ' this.p.Func = v;',
  26245. ' }',
  26246. '}, $mod.Func);',
  26247. '']));
  26248. end;
  26249. procedure TTestModule.TestProcType_PassProcToArray;
  26250. begin
  26251. StartProgram(false);
  26252. Add([
  26253. 'type',
  26254. ' TFunc = function: longint;',
  26255. ' TArrFunc = array of TFunc;',
  26256. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26257. 'function GetIt: longint; begin end;',
  26258. 'var',
  26259. ' Func: tfunc;',
  26260. 'begin',
  26261. ' doit([]);',
  26262. ' doit([@GetIt]);',
  26263. ' doit([Func]);',
  26264. '']);
  26265. ConvertProgram;
  26266. CheckSource('TestProcType_PassProcToArray',
  26267. LinesToStr([ // statements
  26268. 'this.DoIt = function (Arr) {',
  26269. '};',
  26270. 'this.GetIt = function () {',
  26271. ' var Result = 0;',
  26272. ' return Result;',
  26273. '};',
  26274. 'this.Func = null;',
  26275. '']),
  26276. LinesToStr([ // $mod.$main
  26277. '$mod.DoIt([]);',
  26278. '$mod.DoIt([$mod.GetIt]);',
  26279. '$mod.DoIt([$mod.Func]);',
  26280. '']));
  26281. end;
  26282. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26283. begin
  26284. StartProgram(false);
  26285. Add([
  26286. '{$modeswitch externalclass}',
  26287. 'type',
  26288. ' TProc = reference to procedure(i: longint); safecall;',
  26289. ' TEvent = procedure(i: longint) of object; safecall;',
  26290. ' TExtA = class external name ''ExtObj''',
  26291. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26292. ' procedure DoSome(Id: longint = 1);',
  26293. ' procedure SetOnClick(const e: TEvent);',
  26294. ' property OnClick: TEvent write SetOnClick;',
  26295. ' class procedure Fly(Id: longint = 1); static;',
  26296. ' procedure SetOnShow(const p: TProc);',
  26297. ' property OnShow: TProc write SetOnShow;',
  26298. ' end;',
  26299. 'procedure Run(i: longint = 1);',
  26300. 'begin',
  26301. 'end;',
  26302. 'var',
  26303. ' Obj: texta;',
  26304. ' e: TEvent;',
  26305. ' p: TProc;',
  26306. 'begin',
  26307. ' e:=e;',
  26308. ' e:[email protected];',
  26309. ' e:[email protected];',
  26310. ' e:=TEvent(@obj.dosome);', // no safecall
  26311. ' obj.OnClick:[email protected];',
  26312. ' obj.OnClick:[email protected];',
  26313. ' obj.setonclick(@obj.doit);',
  26314. ' obj.setonclick(@obj.dosome);',
  26315. ' p:=@Run;',
  26316. ' p:[email protected];',
  26317. ' obj.OnShow:=@Run;',
  26318. ' obj.OnShow:[email protected];',
  26319. ' obj.setOnShow(@Run);',
  26320. ' obj.setOnShow(@TExtA.Fly);',
  26321. ' with obj do begin',
  26322. ' e:=@doit;',
  26323. ' e:=@dosome;',
  26324. ' OnClick:=@doit;',
  26325. ' OnClick:=@dosome;',
  26326. ' setonclick(@doit);',
  26327. ' setonclick(@dosome);',
  26328. ' OnShow:=@Run;',
  26329. ' setOnShow(@Run);',
  26330. ' end;']);
  26331. ConvertProgram;
  26332. CheckSource('TestProcType_SafeCallObjFPC',
  26333. LinesToStr([ // statements
  26334. 'this.Run = function (i) {',
  26335. '};',
  26336. 'this.Obj = null;',
  26337. 'this.e = null;',
  26338. 'this.p = null;',
  26339. '']),
  26340. LinesToStr([ // $mod.$main
  26341. '$mod.e = $mod.e;',
  26342. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26343. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26344. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26345. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26346. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26347. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26348. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26349. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26350. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26351. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26352. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26353. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26354. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26355. 'var $with = $mod.Obj;',
  26356. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26357. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26358. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26359. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26360. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26361. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26362. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26363. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26364. '']));
  26365. end;
  26366. procedure TTestModule.TestProcType_SafeCallDelphi;
  26367. begin
  26368. StartProgram(false);
  26369. Add([
  26370. '{$mode delphi}',
  26371. '{$modeswitch externalclass}',
  26372. 'type',
  26373. ' TProc = reference to procedure(i: longint); safecall;',
  26374. ' TEvent = procedure(i: longint) of object; safecall;',
  26375. ' TExtA = class external name ''ExtObj''',
  26376. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26377. ' procedure DoSome(Id: longint = 1);',
  26378. ' procedure SetOnClick(const e: TEvent);',
  26379. ' property OnClick: TEvent write SetOnClick;',
  26380. ' class procedure Fly(Id: longint = 1); static;',
  26381. ' procedure SetOnShow(const p: TProc);',
  26382. ' property OnShow: TProc write SetOnShow;',
  26383. ' end;',
  26384. 'procedure Run(i: longint = 1);',
  26385. 'begin',
  26386. 'end;',
  26387. 'var',
  26388. ' Obj: texta;',
  26389. ' e: TEvent;',
  26390. ' p: TProc;',
  26391. 'begin',
  26392. ' e:=e;',
  26393. ' e:=obj.doit;',
  26394. ' e:=obj.dosome;',
  26395. ' e:=TEvent(@obj.dosome);', // no safecall
  26396. ' obj.OnClick:=obj.doit;',
  26397. ' obj.OnClick:=obj.dosome;',
  26398. ' obj.setonclick(obj.doit);',
  26399. ' obj.setonclick(obj.dosome);',
  26400. ' p:=Run;',
  26401. ' p:=TExtA.Fly;',
  26402. ' obj.OnShow:=Run;',
  26403. ' obj.OnShow:=TExtA.Fly;',
  26404. ' obj.setOnShow(Run);',
  26405. ' obj.setOnShow(TExtA.Fly);',
  26406. ' with obj do begin',
  26407. ' e:=doit;',
  26408. ' e:=dosome;',
  26409. ' OnClick:=doit;',
  26410. ' OnClick:=dosome;',
  26411. ' setonclick(doit);',
  26412. ' setonclick(dosome);',
  26413. ' OnShow:=@Run;',
  26414. ' setOnShow(@Run);',
  26415. ' end;']);
  26416. ConvertProgram;
  26417. CheckSource('TestProcType_SafeCallDelphi',
  26418. LinesToStr([ // statements
  26419. 'this.Run = function (i) {',
  26420. '};',
  26421. 'this.Obj = null;',
  26422. 'this.e = null;',
  26423. 'this.p = null;',
  26424. '']),
  26425. LinesToStr([ // $mod.$main
  26426. '$mod.e = $mod.e;',
  26427. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26428. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26429. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26430. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26431. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26432. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26433. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26434. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26435. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26436. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26437. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26438. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26439. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26440. 'var $with = $mod.Obj;',
  26441. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26442. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26443. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26444. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26445. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26446. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26447. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26448. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26449. '']));
  26450. end;
  26451. procedure TTestModule.TestPointer;
  26452. begin
  26453. StartProgram(false);
  26454. Add(['type',
  26455. ' TObject = class end;',
  26456. ' TClass = class of TObject;',
  26457. ' TArrInt = array of longint;',
  26458. 'const',
  26459. ' n = nil;',
  26460. 'var',
  26461. ' v: jsvalue;',
  26462. ' Obj: tobject;',
  26463. ' C: tclass;',
  26464. ' a: tarrint;',
  26465. ' p: Pointer = nil;',
  26466. ' s: string;',
  26467. 'begin',
  26468. ' p:=p;',
  26469. ' p:=nil;',
  26470. ' if p=nil then;',
  26471. ' if nil=p then;',
  26472. ' if Assigned(p) then;',
  26473. ' p:=Pointer(v);',
  26474. ' p:=obj;',
  26475. ' p:=c;',
  26476. ' p:=a;',
  26477. ' p:=tobject;',
  26478. ' obj:=TObject(p);',
  26479. ' c:=TClass(p);',
  26480. ' a:=TArrInt(p);',
  26481. ' p:=n;',
  26482. ' p:=Pointer(a);',
  26483. ' p:=pointer(s);',
  26484. ' s:=string(p);',
  26485. '']);
  26486. ConvertProgram;
  26487. CheckSource('TestPointer',
  26488. LinesToStr([ // statements
  26489. 'rtl.createClass(this, "TObject", null, function () {',
  26490. ' this.$init = function () {',
  26491. ' };',
  26492. ' this.$final = function () {',
  26493. ' };',
  26494. '});',
  26495. 'this.n = null;',
  26496. 'this.v = undefined;',
  26497. 'this.Obj = null;',
  26498. 'this.C = null;',
  26499. 'this.a = [];',
  26500. 'this.p = null;',
  26501. 'this.s = "";',
  26502. '']),
  26503. LinesToStr([ // $mod.$main
  26504. '$mod.p = $mod.p;',
  26505. '$mod.p = null;',
  26506. 'if ($mod.p === null) ;',
  26507. 'if (null === $mod.p) ;',
  26508. 'if ($mod.p != null) ;',
  26509. '$mod.p = $mod.v;',
  26510. '$mod.p = $mod.Obj;',
  26511. '$mod.p = $mod.C;',
  26512. '$mod.p = $mod.a;',
  26513. '$mod.p = $mod.TObject;',
  26514. '$mod.Obj = $mod.p;',
  26515. '$mod.C = $mod.p;',
  26516. '$mod.a = $mod.p;',
  26517. '$mod.p = null;',
  26518. '$mod.p = $mod.a;',
  26519. '$mod.p = $mod.s;',
  26520. '$mod.s = $mod.p;',
  26521. '']));
  26522. end;
  26523. procedure TTestModule.TestPointer_Proc;
  26524. begin
  26525. StartProgram(false);
  26526. Add('type');
  26527. Add(' TObject = class');
  26528. Add(' procedure DoIt; virtual; abstract;');
  26529. Add(' end;');
  26530. Add('procedure DoSome; begin end;');
  26531. Add('var');
  26532. Add(' o: TObject;');
  26533. Add(' p: Pointer;');
  26534. Add('begin');
  26535. Add(' p:=@DoSome;');
  26536. Add(' p:[email protected];');
  26537. ConvertProgram;
  26538. CheckSource('TestPointer_Proc',
  26539. LinesToStr([ // statements
  26540. 'rtl.createClass(this, "TObject", null, function () {',
  26541. ' this.$init = function () {',
  26542. ' };',
  26543. ' this.$final = function () {',
  26544. ' };',
  26545. '});',
  26546. 'this.DoSome = function () {',
  26547. '};',
  26548. 'this.o = null;',
  26549. 'this.p = null;',
  26550. '']),
  26551. LinesToStr([ // $mod.$main
  26552. '$mod.p = $mod.DoSome;',
  26553. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26554. '']));
  26555. end;
  26556. procedure TTestModule.TestPointer_AssignRecordFail;
  26557. begin
  26558. StartProgram(false);
  26559. Add('type');
  26560. Add(' TRec = record end;');
  26561. Add('var');
  26562. Add(' p: Pointer;');
  26563. Add(' r: TRec;');
  26564. Add('begin');
  26565. Add(' p:=r;');
  26566. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26567. nIncompatibleTypesGotExpected);
  26568. ConvertProgram;
  26569. end;
  26570. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26571. begin
  26572. StartProgram(false);
  26573. Add('type');
  26574. Add(' TArr = array[boolean] of longint;');
  26575. Add('var');
  26576. Add(' p: Pointer;');
  26577. Add(' a: TArr;');
  26578. Add('begin');
  26579. Add(' p:=a;');
  26580. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26581. nIncompatibleTypesGotExpected);
  26582. ConvertProgram;
  26583. end;
  26584. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26585. begin
  26586. StartProgram(false);
  26587. Add([
  26588. 'procedure DoIt(args: array of jsvalue); begin end;',
  26589. 'procedure DoAll; varargs; begin end;',
  26590. 'var',
  26591. ' v: jsvalue;',
  26592. 'begin',
  26593. ' DoIt([pointer(v)]);',
  26594. ' DoAll(pointer(v));',
  26595. '']);
  26596. ConvertProgram;
  26597. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26598. LinesToStr([ // statements
  26599. 'this.DoIt = function (args) {',
  26600. '};',
  26601. 'this.DoAll = function () {',
  26602. '};',
  26603. 'this.v = undefined;',
  26604. '']),
  26605. LinesToStr([ // $mod.$main
  26606. '$mod.DoIt([$mod.v]);',
  26607. '$mod.DoAll($mod.v);',
  26608. '']));
  26609. end;
  26610. procedure TTestModule.TestPointer_NonRecordFail;
  26611. begin
  26612. StartProgram(false);
  26613. Add([
  26614. 'type',
  26615. ' p = ^longint;',
  26616. 'begin',
  26617. '']);
  26618. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26619. ConvertProgram;
  26620. end;
  26621. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26622. begin
  26623. StartProgram(false);
  26624. Add([
  26625. 'procedure DoIt(p: ^longint); begin end;',
  26626. 'begin',
  26627. '']);
  26628. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26629. ConvertProgram;
  26630. end;
  26631. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26632. begin
  26633. StartProgram(false);
  26634. Add([
  26635. 'var p: ^longint;',
  26636. 'begin',
  26637. '']);
  26638. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26639. ConvertProgram;
  26640. end;
  26641. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26642. begin
  26643. StartProgram(false);
  26644. Add([
  26645. 'function DoIt: ^longint; begin end;',
  26646. 'begin',
  26647. '']);
  26648. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26649. ConvertProgram;
  26650. end;
  26651. procedure TTestModule.TestPointer_AddrOperatorFail;
  26652. begin
  26653. StartProgram(false);
  26654. Add([
  26655. 'var i: longint;',
  26656. 'begin',
  26657. ' if @i=nil then ;',
  26658. '']);
  26659. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26660. ConvertProgram;
  26661. end;
  26662. procedure TTestModule.TestPointer_ArrayParamsFail;
  26663. begin
  26664. StartProgram(false);
  26665. Add([
  26666. 'var',
  26667. ' p: Pointer;',
  26668. 'begin',
  26669. ' p:=p[1];',
  26670. '']);
  26671. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26672. ConvertProgram;
  26673. end;
  26674. procedure TTestModule.TestPointer_PointerAddFail;
  26675. begin
  26676. StartProgram(false);
  26677. Add([
  26678. 'var',
  26679. ' p: Pointer;',
  26680. 'begin',
  26681. ' p:=p+1;',
  26682. '']);
  26683. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26684. ConvertProgram;
  26685. end;
  26686. procedure TTestModule.TestPointer_IncPointerFail;
  26687. begin
  26688. StartProgram(false);
  26689. Add([
  26690. 'var',
  26691. ' p: Pointer;',
  26692. 'begin',
  26693. ' inc(p,1);',
  26694. '']);
  26695. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26696. nIncompatibleTypeArgNo);
  26697. ConvertProgram;
  26698. end;
  26699. procedure TTestModule.TestPointer_Record;
  26700. begin
  26701. StartProgram(false);
  26702. Add([
  26703. 'type',
  26704. ' TRec = record x: longint; end;',
  26705. ' PRec = ^TRec;',
  26706. 'var',
  26707. ' r: TRec;',
  26708. ' p: PRec;',
  26709. ' q: ^TRec;',
  26710. ' Ptr: pointer;',
  26711. 'begin',
  26712. ' new(p);',
  26713. ' p:=@r;',
  26714. ' r:=p^;',
  26715. ' r.x:=p^.x;',
  26716. ' p^.x:=r.x;',
  26717. ' if p^.x=3 then ;',
  26718. ' if 4=p^.x then ;',
  26719. ' dispose(p);',
  26720. ' new(q);',
  26721. ' dispose(q);',
  26722. ' Ptr:=p;',
  26723. ' p:=PRec(ptr);',
  26724. '']);
  26725. ConvertProgram;
  26726. CheckSource('TestPointer_Record',
  26727. LinesToStr([ // statements
  26728. 'rtl.recNewT(this, "TRec", function () {',
  26729. ' this.x = 0;',
  26730. ' this.$eq = function (b) {',
  26731. ' return this.x === b.x;',
  26732. ' };',
  26733. ' this.$assign = function (s) {',
  26734. ' this.x = s.x;',
  26735. ' return this;',
  26736. ' };',
  26737. '});',
  26738. 'this.r = this.TRec.$new();',
  26739. 'this.p = null;',
  26740. 'this.q = null;',
  26741. 'this.Ptr = null;',
  26742. '']),
  26743. LinesToStr([ // $mod.$main
  26744. '$mod.p = $mod.TRec.$new();',
  26745. '$mod.p = $mod.r;',
  26746. '$mod.r.$assign($mod.p);',
  26747. '$mod.r.x = $mod.p.x;',
  26748. '$mod.p.x = $mod.r.x;',
  26749. 'if ($mod.p.x === 3) ;',
  26750. 'if (4 === $mod.p.x) ;',
  26751. '$mod.p = null;',
  26752. '$mod.q = $mod.TRec.$new();',
  26753. '$mod.q = null;',
  26754. '$mod.Ptr = $mod.p;',
  26755. '$mod.p = $mod.Ptr;',
  26756. '']));
  26757. end;
  26758. procedure TTestModule.TestPointer_RecordArg;
  26759. begin
  26760. StartProgram(false);
  26761. Add([
  26762. '{$modeswitch autoderef}',
  26763. 'type',
  26764. ' TRec = record x: longint; end;',
  26765. ' PRec = ^TRec;',
  26766. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26767. 'begin',
  26768. ' a.x:=a.x;',
  26769. ' a^.x:=a^.x;',
  26770. ' with a^ do',
  26771. ' x:=x;',
  26772. 'end;',
  26773. 'function GetIt(p: PRec): PRec;',
  26774. 'begin',
  26775. ' p.x:=p.x;',
  26776. ' p^.x:=p^.x;',
  26777. ' with p^ do',
  26778. ' x:=x;',
  26779. 'end;',
  26780. 'var',
  26781. ' r: TRec;',
  26782. ' p: PRec;',
  26783. 'begin',
  26784. ' p:=GetIt(p);',
  26785. ' p^:=GetIt(@r)^;',
  26786. ' DoIt(p,p,p);',
  26787. ' DoIt(@r,p,p);',
  26788. '']);
  26789. ConvertProgram;
  26790. CheckSource('TestPointer_RecordArg',
  26791. LinesToStr([ // statements
  26792. 'rtl.recNewT(this, "TRec", function () {',
  26793. ' this.x = 0;',
  26794. ' this.$eq = function (b) {',
  26795. ' return this.x === b.x;',
  26796. ' };',
  26797. ' this.$assign = function (s) {',
  26798. ' this.x = s.x;',
  26799. ' return this;',
  26800. ' };',
  26801. '});',
  26802. 'this.DoIt = function (a, b, c) {',
  26803. ' var Result = $mod.TRec.$new();',
  26804. ' a.x = a.x;',
  26805. ' a.x = a.x;',
  26806. ' a.x = a.x;',
  26807. ' return Result;',
  26808. '};',
  26809. 'this.GetIt = function (p) {',
  26810. ' var Result = null;',
  26811. ' p.x = p.x;',
  26812. ' p.x = p.x;',
  26813. ' p.x = p.x;',
  26814. ' return Result;',
  26815. '};',
  26816. 'this.r = this.TRec.$new();',
  26817. 'this.p = null;',
  26818. '']),
  26819. LinesToStr([ // $mod.$main
  26820. '$mod.p = $mod.GetIt($mod.p);',
  26821. '$mod.p.$assign($mod.GetIt($mod.r));',
  26822. '$mod.DoIt($mod.p, {',
  26823. ' p: $mod,',
  26824. ' get: function () {',
  26825. ' return this.p.p;',
  26826. ' },',
  26827. ' set: function (v) {',
  26828. ' this.p.p = v;',
  26829. ' }',
  26830. '}, {',
  26831. ' p: $mod,',
  26832. ' get: function () {',
  26833. ' return this.p.p;',
  26834. ' },',
  26835. ' set: function (v) {',
  26836. ' this.p.p = v;',
  26837. ' }',
  26838. '});',
  26839. '$mod.DoIt($mod.r, {',
  26840. ' p: $mod,',
  26841. ' get: function () {',
  26842. ' return this.p.p;',
  26843. ' },',
  26844. ' set: function (v) {',
  26845. ' this.p.p = v;',
  26846. ' }',
  26847. '}, {',
  26848. ' p: $mod,',
  26849. ' get: function () {',
  26850. ' return this.p.p;',
  26851. ' },',
  26852. ' set: function (v) {',
  26853. ' this.p.p = v;',
  26854. ' }',
  26855. '});',
  26856. '']));
  26857. end;
  26858. procedure TTestModule.TestJSValue_AssignToJSValue;
  26859. begin
  26860. StartProgram(false);
  26861. Add('var');
  26862. Add(' v: jsvalue;');
  26863. Add(' i: longint;');
  26864. Add(' s: string;');
  26865. Add(' b: boolean;');
  26866. Add(' d: double;');
  26867. Add(' p: pointer;');
  26868. Add('begin');
  26869. Add(' v:=v;');
  26870. Add(' v:=1;');
  26871. Add(' v:=i;');
  26872. Add(' v:='''';');
  26873. Add(' v:=''c'';');
  26874. Add(' v:=''foo'';');
  26875. Add(' v:=s;');
  26876. Add(' v:=false;');
  26877. Add(' v:=true;');
  26878. Add(' v:=b;');
  26879. Add(' v:=0.1;');
  26880. Add(' v:=d;');
  26881. Add(' v:=nil;');
  26882. Add(' v:=p;');
  26883. ConvertProgram;
  26884. CheckSource('TestJSValue_AssignToJSValue',
  26885. LinesToStr([ // statements
  26886. 'this.v = undefined;',
  26887. 'this.i = 0;',
  26888. 'this.s = "";',
  26889. 'this.b = false;',
  26890. 'this.d = 0.0;',
  26891. 'this.p = null;',
  26892. '']),
  26893. LinesToStr([ // $mod.$main
  26894. '$mod.v = $mod.v;',
  26895. '$mod.v = 1;',
  26896. '$mod.v = $mod.i;',
  26897. '$mod.v = "";',
  26898. '$mod.v = "c";',
  26899. '$mod.v = "foo";',
  26900. '$mod.v = $mod.s;',
  26901. '$mod.v = false;',
  26902. '$mod.v = true;',
  26903. '$mod.v = $mod.b;',
  26904. '$mod.v = 0.1;',
  26905. '$mod.v = $mod.d;',
  26906. '$mod.v = null;',
  26907. '$mod.v = $mod.p;',
  26908. '']));
  26909. end;
  26910. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  26911. begin
  26912. StartProgram(false);
  26913. Add('type');
  26914. Add(' integer = longint;');
  26915. Add(' TYesNo = boolean;');
  26916. Add(' TFloat = double;');
  26917. Add(' TCaption = string;');
  26918. Add(' TChar = char;');
  26919. Add('var');
  26920. Add(' v: jsvalue;');
  26921. Add(' i: integer;');
  26922. Add(' s: TCaption;');
  26923. Add(' b: TYesNo;');
  26924. Add(' d: TFloat;');
  26925. Add(' c: char;');
  26926. Add('begin');
  26927. Add(' i:=longint(v);');
  26928. Add(' i:=integer(v);');
  26929. Add(' s:=string(v);');
  26930. Add(' s:=TCaption(v);');
  26931. Add(' b:=boolean(v);');
  26932. Add(' b:=TYesNo(v);');
  26933. Add(' d:=double(v);');
  26934. Add(' d:=TFloat(v);');
  26935. Add(' c:=char(v);');
  26936. Add(' c:=TChar(v);');
  26937. ConvertProgram;
  26938. CheckSource('TestJSValue_TypeCastToBaseType',
  26939. LinesToStr([ // statements
  26940. 'this.v = undefined;',
  26941. 'this.i = 0;',
  26942. 'this.s = "";',
  26943. 'this.b = false;',
  26944. 'this.d = 0.0;',
  26945. 'this.c = "";',
  26946. '']),
  26947. LinesToStr([ // $mod.$main
  26948. '$mod.i = rtl.trunc($mod.v);',
  26949. '$mod.i = rtl.trunc($mod.v);',
  26950. '$mod.s = "" + $mod.v;',
  26951. '$mod.s = "" + $mod.v;',
  26952. '$mod.b = !($mod.v == false);',
  26953. '$mod.b = !($mod.v == false);',
  26954. '$mod.d = rtl.getNumber($mod.v);',
  26955. '$mod.d = rtl.getNumber($mod.v);',
  26956. '$mod.c = rtl.getChar($mod.v);',
  26957. '$mod.c = rtl.getChar($mod.v);',
  26958. '']));
  26959. end;
  26960. procedure TTestModule.TestJSValue_TypecastToJSValue;
  26961. begin
  26962. StartProgram(false);
  26963. Add([
  26964. 'type',
  26965. ' TArr = array of word;',
  26966. ' TRec = record end;',
  26967. ' TSet = set of boolean;',
  26968. 'procedure Fly(v: jsvalue);',
  26969. 'begin',
  26970. 'end;',
  26971. 'var',
  26972. ' a: TArr;',
  26973. ' r: TRec;',
  26974. ' s: TSet;',
  26975. 'begin',
  26976. ' Fly(jsvalue(a));',
  26977. ' Fly(jsvalue(r));',
  26978. ' Fly(jsvalue(s));',
  26979. '']);
  26980. ConvertProgram;
  26981. CheckSource('TestJSValue_TypecastToJSValue',
  26982. LinesToStr([ // statements
  26983. 'rtl.recNewT(this, "TRec", function () {',
  26984. ' this.$eq = function (b) {',
  26985. ' return true;',
  26986. ' };',
  26987. ' this.$assign = function (s) {',
  26988. ' return this;',
  26989. ' };',
  26990. '});',
  26991. 'this.Fly = function (v) {',
  26992. '};',
  26993. 'this.a = [];',
  26994. 'this.r = this.TRec.$new();',
  26995. 'this.s = {};',
  26996. '']),
  26997. LinesToStr([ // $mod.$main
  26998. '$mod.Fly($mod.a);',
  26999. '$mod.Fly($mod.r);',
  27000. '$mod.Fly($mod.s);',
  27001. '']));
  27002. end;
  27003. procedure TTestModule.TestJSValue_Equal;
  27004. begin
  27005. StartProgram(false);
  27006. Add('type');
  27007. Add(' integer = longint;');
  27008. Add(' TYesNo = boolean;');
  27009. Add(' TFloat = double;');
  27010. Add(' TCaption = string;');
  27011. Add(' TChar = char;');
  27012. Add(' TMulti = JSValue;');
  27013. Add('var');
  27014. Add(' v: jsvalue;');
  27015. Add(' i: integer;');
  27016. Add(' s: TCaption;');
  27017. Add(' b: TYesNo;');
  27018. Add(' d: TFloat;');
  27019. Add(' c: char;');
  27020. Add(' m: TMulti;');
  27021. Add('begin');
  27022. Add(' b:=v=v;');
  27023. Add(' b:=v<>v;');
  27024. Add(' b:=v=1;');
  27025. Add(' b:=v<>1;');
  27026. Add(' b:=2=v;');
  27027. Add(' b:=2<>v;');
  27028. Add(' b:=v=i;');
  27029. Add(' b:=i=v;');
  27030. Add(' b:=v=nil;');
  27031. Add(' b:=nil=v;');
  27032. Add(' b:=v=false;');
  27033. Add(' b:=true=v;');
  27034. Add(' b:=v=b;');
  27035. Add(' b:=b=v;');
  27036. Add(' b:=v=s;');
  27037. Add(' b:=s=v;');
  27038. Add(' b:=v=''foo'';');
  27039. Add(' b:=''''=v;');
  27040. Add(' b:=v=d;');
  27041. Add(' b:=d=v;');
  27042. Add(' b:=v=3.4;');
  27043. Add(' b:=5.6=v;');
  27044. Add(' b:=v=c;');
  27045. Add(' b:=c=v;');
  27046. Add(' b:=m=m;');
  27047. Add(' b:=v=m;');
  27048. Add(' b:=m=v;');
  27049. ConvertProgram;
  27050. CheckSource('TestJSValue_Equal',
  27051. LinesToStr([ // statements
  27052. 'this.v = undefined;',
  27053. 'this.i = 0;',
  27054. 'this.s = "";',
  27055. 'this.b = false;',
  27056. 'this.d = 0.0;',
  27057. 'this.c = "";',
  27058. 'this.m = undefined;',
  27059. '']),
  27060. LinesToStr([ // $mod.$main
  27061. '$mod.b = $mod.v == $mod.v;',
  27062. '$mod.b = $mod.v != $mod.v;',
  27063. '$mod.b = $mod.v == 1;',
  27064. '$mod.b = $mod.v != 1;',
  27065. '$mod.b = 2 == $mod.v;',
  27066. '$mod.b = 2 != $mod.v;',
  27067. '$mod.b = $mod.v == $mod.i;',
  27068. '$mod.b = $mod.i == $mod.v;',
  27069. '$mod.b = $mod.v == null;',
  27070. '$mod.b = null == $mod.v;',
  27071. '$mod.b = $mod.v == false;',
  27072. '$mod.b = true == $mod.v;',
  27073. '$mod.b = $mod.v == $mod.b;',
  27074. '$mod.b = $mod.b == $mod.v;',
  27075. '$mod.b = $mod.v == $mod.s;',
  27076. '$mod.b = $mod.s == $mod.v;',
  27077. '$mod.b = $mod.v == "foo";',
  27078. '$mod.b = "" == $mod.v;',
  27079. '$mod.b = $mod.v == $mod.d;',
  27080. '$mod.b = $mod.d == $mod.v;',
  27081. '$mod.b = $mod.v == 3.4;',
  27082. '$mod.b = 5.6 == $mod.v;',
  27083. '$mod.b = $mod.v == $mod.c;',
  27084. '$mod.b = $mod.c == $mod.v;',
  27085. '$mod.b = $mod.m == $mod.m;',
  27086. '$mod.b = $mod.v == $mod.m;',
  27087. '$mod.b = $mod.m == $mod.v;',
  27088. '']));
  27089. end;
  27090. procedure TTestModule.TestJSValue_If;
  27091. begin
  27092. StartProgram(false);
  27093. Add([
  27094. 'procedure Fly(var u);',
  27095. 'begin',
  27096. ' if jsvalue(u) then ;',
  27097. 'end;',
  27098. 'var',
  27099. ' v: jsvalue;',
  27100. 'begin',
  27101. ' if v then ;',
  27102. ' while v do ;',
  27103. ' repeat until v;',
  27104. '']);
  27105. ConvertProgram;
  27106. CheckSource('TestJSValue_If',
  27107. LinesToStr([ // statements
  27108. 'this.Fly = function (u) {',
  27109. ' if (u.get()) ;',
  27110. '};',
  27111. 'this.v = undefined;',
  27112. '']),
  27113. LinesToStr([ // $mod.$main
  27114. 'if ($mod.v) ;',
  27115. 'while($mod.v){',
  27116. '};',
  27117. 'do{',
  27118. '} while(!$mod.v);',
  27119. '']));
  27120. end;
  27121. procedure TTestModule.TestJSValue_Not;
  27122. begin
  27123. StartProgram(false);
  27124. Add([
  27125. 'var',
  27126. ' v: jsvalue;',
  27127. ' b: boolean;',
  27128. 'begin',
  27129. ' b:=not v;',
  27130. ' if not v then ;',
  27131. ' while not v do ;',
  27132. ' repeat until not v;',
  27133. '']);
  27134. ConvertProgram;
  27135. CheckSource('TestJSValue_If',
  27136. LinesToStr([ // statements
  27137. 'this.v = undefined;',
  27138. 'this.b = false;',
  27139. '']),
  27140. LinesToStr([ // $mod.$main
  27141. '$mod.b=!$mod.v;',
  27142. 'if (!$mod.v) ;',
  27143. 'while(!$mod.v){',
  27144. '};',
  27145. 'do{',
  27146. '} while($mod.v);',
  27147. '']));
  27148. end;
  27149. procedure TTestModule.TestJSValue_Enum;
  27150. begin
  27151. StartProgram(false);
  27152. Add('type');
  27153. Add(' TColor = (red, blue);');
  27154. Add(' TRedBlue = TColor;');
  27155. Add('var');
  27156. Add(' v: jsvalue;');
  27157. Add(' e: TColor;');
  27158. Add('begin');
  27159. Add(' v:=e;');
  27160. Add(' v:=TColor(e);');
  27161. Add(' v:=TRedBlue(e);');
  27162. Add(' e:=TColor(v);');
  27163. Add(' e:=TRedBlue(v);');
  27164. ConvertProgram;
  27165. CheckSource('TestJSValue_Enum',
  27166. LinesToStr([ // statements
  27167. 'this.TColor = {',
  27168. ' "0": "red",',
  27169. ' red: 0,',
  27170. ' "1": "blue",',
  27171. ' blue: 1',
  27172. '};',
  27173. 'this.v = undefined;',
  27174. 'this.e = 0;',
  27175. '']),
  27176. LinesToStr([ // $mod.$main
  27177. '$mod.v = $mod.e;',
  27178. '$mod.v = $mod.e;',
  27179. '$mod.v = $mod.e;',
  27180. '$mod.e = $mod.v;',
  27181. '$mod.e = $mod.v;',
  27182. '']));
  27183. end;
  27184. procedure TTestModule.TestJSValue_ClassInstance;
  27185. begin
  27186. StartProgram(false);
  27187. Add([
  27188. 'type',
  27189. ' TObject = class',
  27190. ' end;',
  27191. ' TBirdObject = TObject;',
  27192. 'var',
  27193. ' v: jsvalue;',
  27194. ' o: TObject;',
  27195. 'begin',
  27196. ' v:=o;',
  27197. ' v:=TObject(o);',
  27198. ' v:=TBirdObject(o);',
  27199. ' o:=TObject(v);',
  27200. ' o:=TBirdObject(v);',
  27201. ' if v is TObject then ;',
  27202. '']);
  27203. ConvertProgram;
  27204. CheckSource('TestJSValue_ClassInstance',
  27205. LinesToStr([ // statements
  27206. 'rtl.createClass(this, "TObject", null, function () {',
  27207. ' this.$init = function () {',
  27208. ' };',
  27209. ' this.$final = function () {',
  27210. ' };',
  27211. '});',
  27212. 'this.v = undefined;',
  27213. 'this.o = null;',
  27214. '']),
  27215. LinesToStr([ // $mod.$main
  27216. '$mod.v = $mod.o;',
  27217. '$mod.v = $mod.o;',
  27218. '$mod.v = $mod.o;',
  27219. '$mod.o = rtl.getObject($mod.v);',
  27220. '$mod.o = rtl.getObject($mod.v);',
  27221. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27222. '']));
  27223. end;
  27224. procedure TTestModule.TestJSValue_ClassOf;
  27225. begin
  27226. StartProgram(false);
  27227. Add([
  27228. 'type',
  27229. ' TClass = class of TObject;',
  27230. ' TObject = class',
  27231. ' end;',
  27232. ' TBirds = class of TBird;',
  27233. ' TBird = class(TObject) end;',
  27234. 'var',
  27235. ' v: jsvalue;',
  27236. ' c: TClass;',
  27237. 'begin',
  27238. ' v:=c;',
  27239. ' v:=TObject;',
  27240. ' v:=TClass(c);',
  27241. ' v:=TBirds(c);',
  27242. ' c:=TClass(v);',
  27243. ' c:=TBirds(v);',
  27244. ' if v is TClass then ;',
  27245. '']);
  27246. ConvertProgram;
  27247. CheckSource('TestJSValue_ClassOf',
  27248. LinesToStr([ // statements
  27249. 'rtl.createClass(this, "TObject", null, function () {',
  27250. ' this.$init = function () {',
  27251. ' };',
  27252. ' this.$final = function () {',
  27253. ' };',
  27254. '});',
  27255. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27256. '});',
  27257. 'this.v = undefined;',
  27258. 'this.c = null;',
  27259. '']),
  27260. LinesToStr([ // $mod.$main
  27261. '$mod.v = $mod.c;',
  27262. '$mod.v = $mod.TObject;',
  27263. '$mod.v = $mod.c;',
  27264. '$mod.v = $mod.c;',
  27265. '$mod.c = rtl.getObject($mod.v);',
  27266. '$mod.c = rtl.getObject($mod.v);',
  27267. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27268. '']));
  27269. end;
  27270. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27271. begin
  27272. StartProgram(false);
  27273. Add([
  27274. 'type',
  27275. ' integer = longint;',
  27276. ' TArray = array of JSValue;',
  27277. ' TArrgh = tarray;',
  27278. ' TArrInt = array of integer;',
  27279. 'var',
  27280. ' v: jsvalue;',
  27281. ' TheArray: tarray = (1,''2'');',
  27282. ' Arr: tarrgh;',
  27283. ' i: integer;',
  27284. ' ArrInt: tarrint;',
  27285. 'begin',
  27286. ' arr:=thearray;',
  27287. ' thearray:=arr;',
  27288. ' setlength(arr,2);',
  27289. ' setlength(thearray,3);',
  27290. ' arr[4]:=v;',
  27291. ' arr[5]:=length(thearray);',
  27292. ' arr[6]:=nil;',
  27293. ' arr[7]:=thearray[8];',
  27294. ' arr[low(arr)]:=high(thearray);',
  27295. ' arr:=arrint;',
  27296. ' arrInt:=tarrint(arr);',
  27297. ' if TheArray = nil then ;',
  27298. ' if nil = TheArray then ;',
  27299. ' if TheArray <> nil then ;',
  27300. ' if nil <> TheArray then ;',
  27301. '']);
  27302. ConvertProgram;
  27303. CheckSource('TestJSValue_ArrayOfJSValue',
  27304. LinesToStr([ // statements
  27305. 'this.v = undefined;',
  27306. 'this.TheArray = [1, "2"];',
  27307. 'this.Arr = [];',
  27308. 'this.i = 0;',
  27309. 'this.ArrInt = [];',
  27310. '']),
  27311. LinesToStr([ // $mod.$main
  27312. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27313. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27314. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27315. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27316. '$mod.Arr[4] = $mod.v;',
  27317. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27318. '$mod.Arr[6] = null;',
  27319. '$mod.Arr[7] = $mod.TheArray[8];',
  27320. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27321. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27322. '$mod.ArrInt = $mod.Arr;',
  27323. 'if (rtl.length($mod.TheArray) === 0) ;',
  27324. 'if (rtl.length($mod.TheArray) === 0) ;',
  27325. 'if (rtl.length($mod.TheArray) > 0) ;',
  27326. 'if (rtl.length($mod.TheArray) > 0) ;',
  27327. '']));
  27328. end;
  27329. procedure TTestModule.TestJSValue_ArrayLit;
  27330. begin
  27331. StartProgram(false);
  27332. Add([
  27333. 'type',
  27334. ' TFlag = (big,small);',
  27335. ' TArray = array of JSValue;',
  27336. ' TObject = class end;',
  27337. ' TClass = class of TObject;',
  27338. 'var',
  27339. ' v: jsvalue;',
  27340. ' a: TArray;',
  27341. ' o: TObject;',
  27342. 'begin',
  27343. ' a:=[];',
  27344. ' a:=[1];',
  27345. ' a:=[1,2];',
  27346. ' a:=[big];',
  27347. ' a:=[1,big];',
  27348. ' a:=[o,nil];',
  27349. '']);
  27350. ConvertProgram;
  27351. CheckSource('TestJSValue_ArrayLit',
  27352. LinesToStr([ // statements
  27353. 'this.TFlag = {',
  27354. ' "0": "big",',
  27355. ' big: 0,',
  27356. ' "1": "small",',
  27357. ' small: 1',
  27358. '};',
  27359. 'rtl.createClass(this, "TObject", null, function () {',
  27360. ' this.$init = function () {',
  27361. ' };',
  27362. ' this.$final = function () {',
  27363. ' };',
  27364. '});',
  27365. 'this.v = undefined;',
  27366. 'this.a = [];',
  27367. 'this.o = null;',
  27368. '']),
  27369. LinesToStr([ // $mod.$main
  27370. '$mod.a = [];',
  27371. '$mod.a = [1];',
  27372. '$mod.a = [1, 2];',
  27373. '$mod.a = [$mod.TFlag.big];',
  27374. '$mod.a = [1, $mod.TFlag.big];',
  27375. '$mod.a = [$mod.o, null];',
  27376. '']));
  27377. end;
  27378. procedure TTestModule.TestJSValue_Params;
  27379. begin
  27380. StartProgram(false);
  27381. Add('type');
  27382. Add(' integer = longint;');
  27383. Add(' TYesNo = boolean;');
  27384. Add(' TFloat = double;');
  27385. Add(' TCaption = string;');
  27386. Add(' TChar = char;');
  27387. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27388. Add('var');
  27389. Add(' l: jsvalue;');
  27390. Add('begin');
  27391. Add(' a:=a;');
  27392. Add(' l:=b;');
  27393. Add(' c:=c;');
  27394. Add(' d:=d;');
  27395. Add(' Result:=l;');
  27396. Add('end;');
  27397. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27398. Add('var');
  27399. Add(' v: jsvalue;');
  27400. Add(' i: integer;');
  27401. Add(' b: TYesNo;');
  27402. Add(' d: TFloat;');
  27403. Add(' s: TCaption;');
  27404. Add(' c: TChar;');
  27405. Add('begin');
  27406. Add(' v:=doit(v,v,v,v);');
  27407. Add(' i:=integer(dosome(i,i));');
  27408. Add(' b:=TYesNo(dosome(b,b));');
  27409. Add(' d:=TFloat(dosome(d,d));');
  27410. Add(' s:=TCaption(dosome(s,s));');
  27411. Add(' c:=TChar(dosome(c,c));');
  27412. ConvertProgram;
  27413. CheckSource('TestJSValue_Params',
  27414. LinesToStr([ // statements
  27415. 'this.DoIt = function (a, b, c, d) {',
  27416. ' var Result = undefined;',
  27417. ' var l = undefined;',
  27418. ' a = a;',
  27419. ' l = b;',
  27420. ' c.set(c.get());',
  27421. ' d.set(d.get());',
  27422. ' Result = l;',
  27423. ' return Result;',
  27424. '};',
  27425. 'this.DoSome = function (a, b) {',
  27426. ' var Result = undefined;',
  27427. ' return Result;',
  27428. '};',
  27429. 'this.v = undefined;',
  27430. 'this.i = 0;',
  27431. 'this.b = false;',
  27432. 'this.d = 0.0;',
  27433. 'this.s = "";',
  27434. 'this.c = "";',
  27435. '']),
  27436. LinesToStr([ // $mod.$main
  27437. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27438. ' p: $mod,',
  27439. ' get: function () {',
  27440. ' return this.p.v;',
  27441. ' },',
  27442. ' set: function (v) {',
  27443. ' this.p.v = v;',
  27444. ' }',
  27445. '}, {',
  27446. ' p: $mod,',
  27447. ' get: function () {',
  27448. ' return this.p.v;',
  27449. ' },',
  27450. ' set: function (v) {',
  27451. ' this.p.v = v;',
  27452. ' }',
  27453. '});',
  27454. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27455. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27456. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27457. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27458. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27459. '']));
  27460. end;
  27461. procedure TTestModule.TestJSValue_UntypedParam;
  27462. begin
  27463. StartProgram(false);
  27464. Add('function DoIt(const a; var b; out c): jsvalue;');
  27465. Add('begin');
  27466. Add(' Result:=a;');
  27467. Add(' Result:=b;');
  27468. Add(' Result:=c;');
  27469. Add(' b:=Result;');
  27470. Add(' c:=Result;');
  27471. Add('end;');
  27472. Add('var i: longint;');
  27473. Add('begin');
  27474. Add(' doit(i,i,i);');
  27475. ConvertProgram;
  27476. CheckSource('TestJSValue_UntypedParam',
  27477. LinesToStr([ // statements
  27478. 'this.DoIt = function (a, b, c) {',
  27479. ' var Result = undefined;',
  27480. ' Result = a;',
  27481. ' Result = b.get();',
  27482. ' Result = c.get();',
  27483. ' b.set(Result);',
  27484. ' c.set(Result);',
  27485. ' return Result;',
  27486. '};',
  27487. 'this.i = 0;',
  27488. '']),
  27489. LinesToStr([ // $mod.$main
  27490. '$mod.DoIt($mod.i, {',
  27491. ' p: $mod,',
  27492. ' get: function () {',
  27493. ' return this.p.i;',
  27494. ' },',
  27495. ' set: function (v) {',
  27496. ' this.p.i = v;',
  27497. ' }',
  27498. '}, {',
  27499. ' p: $mod,',
  27500. ' get: function () {',
  27501. ' return this.p.i;',
  27502. ' },',
  27503. ' set: function (v) {',
  27504. ' this.p.i = v;',
  27505. ' }',
  27506. '});',
  27507. '']));
  27508. end;
  27509. procedure TTestModule.TestJSValue_FuncResultType;
  27510. begin
  27511. StartProgram(false);
  27512. Add('type');
  27513. Add(' integer = longint;');
  27514. Add(' TJSValueArray = array of JSValue;');
  27515. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27516. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27517. Add('begin');
  27518. Add(' while Compare(P,aList[0])>0 do ;');
  27519. Add('end;');
  27520. Add('var');
  27521. Add(' Compare: TListSortCompare;');
  27522. Add(' V: JSValue;');
  27523. Add(' i: integer;');
  27524. Add('begin');
  27525. Add(' if Compare(V,V)>0 then ;');
  27526. Add(' if Compare(i,i)>1 then ;');
  27527. Add(' if Compare(nil,false)>2 then ;');
  27528. Add(' if Compare(1,true)>3 then ;');
  27529. ConvertProgram;
  27530. CheckSource('TestJSValue_UntypedParam',
  27531. LinesToStr([ // statements
  27532. 'this.Sort = function (P, aList, Compare) {',
  27533. ' while (Compare(P, aList[0]) > 0) {',
  27534. ' };',
  27535. '};',
  27536. 'this.Compare = null;',
  27537. 'this.V = undefined;',
  27538. 'this.i = 0;',
  27539. '']),
  27540. LinesToStr([ // $mod.$main
  27541. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27542. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27543. 'if ($mod.Compare(null, false) > 2) ;',
  27544. 'if ($mod.Compare(1, true) > 3) ;',
  27545. '']));
  27546. end;
  27547. procedure TTestModule.TestJSValue_ProcType_Assign;
  27548. begin
  27549. StartProgram(false);
  27550. Add('type');
  27551. Add(' integer = longint;');
  27552. Add(' TObject = class');
  27553. Add(' class function GetGlob: integer;');
  27554. Add(' function Getter: integer;');
  27555. Add(' end;');
  27556. Add('class function TObject.GetGlob: integer;');
  27557. Add('var v1: jsvalue;');
  27558. Add('begin');
  27559. Add(' v1:=@GetGlob;');
  27560. Add(' v1:[email protected];');
  27561. Add('end;');
  27562. Add('function TObject.Getter: integer;');
  27563. Add('var v2: jsvalue;');
  27564. Add('begin');
  27565. Add(' v2:=@Getter;');
  27566. Add(' v2:[email protected];');
  27567. Add(' v2:=@GetGlob;');
  27568. Add(' v2:[email protected];');
  27569. Add('end;');
  27570. Add('function GetIt(i: integer): integer;');
  27571. Add('var v3: jsvalue;');
  27572. Add('begin');
  27573. Add(' v3:=@GetIt;');
  27574. Add('end;');
  27575. Add('var');
  27576. Add(' V: JSValue;');
  27577. Add(' o: TObject;');
  27578. Add('begin');
  27579. Add(' v:=@GetIt;');
  27580. Add(' v:[email protected];');
  27581. Add(' v:[email protected];');
  27582. ConvertProgram;
  27583. CheckSource('TestJSValue_ProcType_Assign',
  27584. LinesToStr([ // statements
  27585. 'rtl.createClass(this, "TObject", null, function () {',
  27586. ' this.$init = function () {',
  27587. ' };',
  27588. ' this.$final = function () {',
  27589. ' };',
  27590. ' this.GetGlob = function () {',
  27591. ' var Result = 0;',
  27592. ' var v1 = undefined;',
  27593. ' v1 = rtl.createCallback(this, "GetGlob");',
  27594. ' v1 = rtl.createCallback(this, "GetGlob");',
  27595. ' return Result;',
  27596. ' };',
  27597. ' this.Getter = function () {',
  27598. ' var Result = 0;',
  27599. ' var v2 = undefined;',
  27600. ' v2 = rtl.createCallback(this, "Getter");',
  27601. ' v2 = rtl.createCallback(this, "Getter");',
  27602. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27603. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27604. ' return Result;',
  27605. ' };',
  27606. '});',
  27607. 'this.GetIt = function (i) {',
  27608. ' var Result = 0;',
  27609. ' var v3 = undefined;',
  27610. ' v3 = $mod.GetIt;',
  27611. ' return Result;',
  27612. '};',
  27613. 'this.V = undefined;',
  27614. 'this.o = null;',
  27615. '']),
  27616. LinesToStr([ // $mod.$main
  27617. '$mod.V = $mod.GetIt;',
  27618. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27619. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27620. '']));
  27621. end;
  27622. procedure TTestModule.TestJSValue_ProcType_Equal;
  27623. begin
  27624. StartProgram(false);
  27625. Add('type');
  27626. Add(' integer = longint;');
  27627. Add(' TObject = class');
  27628. Add(' class function GetGlob: integer;');
  27629. Add(' function Getter: integer;');
  27630. Add(' end;');
  27631. Add('class function TObject.GetGlob: integer;');
  27632. Add('var v1: jsvalue;');
  27633. Add('begin');
  27634. Add(' if v1=@GetGlob then;');
  27635. Add(' if [email protected] then ;');
  27636. Add('end;');
  27637. Add('function TObject.Getter: integer;');
  27638. Add('var v2: jsvalue;');
  27639. Add('begin');
  27640. Add(' if v2=@Getter then;');
  27641. Add(' if [email protected] then ;');
  27642. Add(' if v2=@GetGlob then;');
  27643. Add(' if [email protected] then;');
  27644. Add('end;');
  27645. Add('function GetIt(i: integer): integer;');
  27646. Add('var v3: jsvalue;');
  27647. Add('begin');
  27648. Add(' if v3=@GetIt then;');
  27649. Add('end;');
  27650. Add('var');
  27651. Add(' V: JSValue;');
  27652. Add(' o: TObject;');
  27653. Add('begin');
  27654. Add(' if v=@GetIt then;');
  27655. Add(' if [email protected] then;');
  27656. Add(' if [email protected] then;');
  27657. Add(' if @GetIt=v then;');
  27658. Add(' if @o.Getter=v then;');
  27659. Add(' if @o.GetGlob=v then;');
  27660. ConvertProgram;
  27661. CheckSource('TestJSValue_ProcType_Equal',
  27662. LinesToStr([ // statements
  27663. 'rtl.createClass(this, "TObject", null, function () {',
  27664. ' this.$init = function () {',
  27665. ' };',
  27666. ' this.$final = function () {',
  27667. ' };',
  27668. ' this.GetGlob = function () {',
  27669. ' var Result = 0;',
  27670. ' var v1 = undefined;',
  27671. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27672. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27673. ' return Result;',
  27674. ' };',
  27675. ' this.Getter = function () {',
  27676. ' var Result = 0;',
  27677. ' var v2 = undefined;',
  27678. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27679. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27680. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27681. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27682. ' return Result;',
  27683. ' };',
  27684. '});',
  27685. 'this.GetIt = function (i) {',
  27686. ' var Result = 0;',
  27687. ' var v3 = undefined;',
  27688. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27689. ' return Result;',
  27690. '};',
  27691. 'this.V = undefined;',
  27692. 'this.o = null;',
  27693. '']),
  27694. LinesToStr([ // $mod.$main
  27695. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27696. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27697. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27698. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27699. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27700. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27701. '']));
  27702. end;
  27703. procedure TTestModule.TestJSValue_ProcType_Param;
  27704. begin
  27705. StartProgram(false);
  27706. Add([
  27707. 'type',
  27708. ' variant = jsvalue;',
  27709. ' TArrVariant = array of variant;',
  27710. ' TArrVar2 = TArrVariant;',
  27711. ' TFuncInt = function: longint;',
  27712. 'function GetIt: longint;',
  27713. 'begin',
  27714. 'end;',
  27715. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27716. 'var v: variant;',
  27717. 'begin',
  27718. ' v:=arr[1];',
  27719. 'end;',
  27720. 'var s: string;',
  27721. 'begin',
  27722. ' DoIt(GetIt,[]);',
  27723. ' DoIt(@GetIt,[]);',
  27724. ' DoIt(1,[s,GetIt]);',
  27725. ' DoIt(1,[s,@GetIt]);',
  27726. '']);
  27727. ConvertProgram;
  27728. CheckSource('TestJSValue_ProcType_Param',
  27729. LinesToStr([ // statements
  27730. 'this.GetIt = function () {',
  27731. ' var Result = 0;',
  27732. ' return Result;',
  27733. '};',
  27734. 'this.DoIt = function (p, Arr) {',
  27735. ' var v = undefined;',
  27736. ' v = Arr[1];',
  27737. '};',
  27738. 'this.s = "";',
  27739. '']),
  27740. LinesToStr([ // $mod.$main
  27741. '$mod.DoIt($mod.GetIt(), []);',
  27742. '$mod.DoIt($mod.GetIt, []);',
  27743. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27744. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27745. '']));
  27746. end;
  27747. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27748. begin
  27749. StartProgram(false);
  27750. Add([
  27751. 'var',
  27752. ' v: JSValue;',
  27753. ' p: Pointer;',
  27754. 'begin',
  27755. ' p:=v;',
  27756. '']);
  27757. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27758. nIncompatibleTypesGotExpected);
  27759. ConvertProgram;
  27760. end;
  27761. procedure TTestModule.TestJSValue_OverloadDouble;
  27762. begin
  27763. StartProgram(false);
  27764. Add([
  27765. 'type',
  27766. ' integer = longint;',
  27767. ' tdatetime = double;',
  27768. 'procedure DoIt(d: double); begin end;',
  27769. 'procedure DoIt(v: jsvalue); begin end;',
  27770. 'var',
  27771. ' d: double;',
  27772. ' dt: tdatetime;',
  27773. ' i: integer;',
  27774. ' b: byte;',
  27775. ' shi: shortint;',
  27776. ' w: word;',
  27777. ' smi: smallint;',
  27778. ' lw: longword;',
  27779. ' li: longint;',
  27780. ' ni: nativeint;',
  27781. ' nu: nativeuint;',
  27782. 'begin',
  27783. ' DoIt(d);',
  27784. ' DoIt(dt);',
  27785. ' DoIt(i);',
  27786. ' DoIt(b);',
  27787. ' DoIt(shi);',
  27788. ' DoIt(w);',
  27789. ' DoIt(smi);',
  27790. ' DoIt(lw);',
  27791. ' DoIt(li);',
  27792. ' DoIt(ni);',
  27793. ' DoIt(nu);',
  27794. '']);
  27795. ConvertProgram;
  27796. CheckSource('TestJSValue_OverloadDouble',
  27797. LinesToStr([ // statements
  27798. 'this.DoIt = function (d) {',
  27799. '};',
  27800. 'this.DoIt$1 = function (v) {',
  27801. '};',
  27802. 'this.d = 0.0;',
  27803. 'this.dt = 0.0;',
  27804. 'this.i = 0;',
  27805. 'this.b = 0;',
  27806. 'this.shi = 0;',
  27807. 'this.w = 0;',
  27808. 'this.smi = 0;',
  27809. 'this.lw = 0;',
  27810. 'this.li = 0;',
  27811. 'this.ni = 0;',
  27812. 'this.nu = 0;',
  27813. '']),
  27814. LinesToStr([ // $mod.$main
  27815. '$mod.DoIt($mod.d);',
  27816. '$mod.DoIt($mod.dt);',
  27817. '$mod.DoIt$1($mod.i);',
  27818. '$mod.DoIt$1($mod.b);',
  27819. '$mod.DoIt$1($mod.shi);',
  27820. '$mod.DoIt$1($mod.w);',
  27821. '$mod.DoIt$1($mod.smi);',
  27822. '$mod.DoIt$1($mod.lw);',
  27823. '$mod.DoIt$1($mod.li);',
  27824. '$mod.DoIt$1($mod.ni);',
  27825. '$mod.DoIt$1($mod.nu);',
  27826. '']));
  27827. end;
  27828. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27829. begin
  27830. StartProgram(false);
  27831. Add([
  27832. 'type',
  27833. ' integer = longint;',
  27834. ' int53 = nativeint;',
  27835. ' tdatetime = double;',
  27836. 'procedure DoIt(n: nativeint); begin end;',
  27837. 'procedure DoIt(v: jsvalue); begin end;',
  27838. 'var',
  27839. ' d: double;',
  27840. ' dt: tdatetime;',
  27841. ' i: integer;',
  27842. ' b: byte;',
  27843. ' shi: shortint;',
  27844. ' w: word;',
  27845. ' smi: smallint;',
  27846. ' lw: longword;',
  27847. ' li: longint;',
  27848. ' ni: nativeint;',
  27849. ' nu: nativeuint;',
  27850. 'begin',
  27851. ' DoIt(d);',
  27852. ' DoIt(dt);',
  27853. ' DoIt(i);',
  27854. ' DoIt(b);',
  27855. ' DoIt(shi);',
  27856. ' DoIt(w);',
  27857. ' DoIt(smi);',
  27858. ' DoIt(lw);',
  27859. ' DoIt(li);',
  27860. ' DoIt(ni);',
  27861. ' DoIt(nu);',
  27862. '']);
  27863. ConvertProgram;
  27864. CheckSource('TestJSValue_OverloadNativeInt',
  27865. LinesToStr([ // statements
  27866. 'this.DoIt = function (n) {',
  27867. '};',
  27868. 'this.DoIt$1 = function (v) {',
  27869. '};',
  27870. 'this.d = 0.0;',
  27871. 'this.dt = 0.0;',
  27872. 'this.i = 0;',
  27873. 'this.b = 0;',
  27874. 'this.shi = 0;',
  27875. 'this.w = 0;',
  27876. 'this.smi = 0;',
  27877. 'this.lw = 0;',
  27878. 'this.li = 0;',
  27879. 'this.ni = 0;',
  27880. 'this.nu = 0;',
  27881. '']),
  27882. LinesToStr([ // $mod.$main
  27883. '$mod.DoIt$1($mod.d);',
  27884. '$mod.DoIt$1($mod.dt);',
  27885. '$mod.DoIt($mod.i);',
  27886. '$mod.DoIt($mod.b);',
  27887. '$mod.DoIt($mod.shi);',
  27888. '$mod.DoIt($mod.w);',
  27889. '$mod.DoIt($mod.smi);',
  27890. '$mod.DoIt($mod.lw);',
  27891. '$mod.DoIt($mod.li);',
  27892. '$mod.DoIt($mod.ni);',
  27893. '$mod.DoIt($mod.nu);',
  27894. '']));
  27895. end;
  27896. procedure TTestModule.TestJSValue_OverloadWord;
  27897. begin
  27898. StartProgram(false);
  27899. Add([
  27900. 'type',
  27901. ' integer = longint;',
  27902. ' int53 = nativeint;',
  27903. ' tdatetime = double;',
  27904. 'procedure DoIt(w: word); begin end;',
  27905. 'procedure DoIt(v: jsvalue); begin end;',
  27906. 'var',
  27907. ' d: double;',
  27908. ' dt: tdatetime;',
  27909. ' i: integer;',
  27910. ' b: byte;',
  27911. ' shi: shortint;',
  27912. ' w: word;',
  27913. ' smi: smallint;',
  27914. ' lw: longword;',
  27915. ' li: longint;',
  27916. ' ni: nativeint;',
  27917. ' nu: nativeuint;',
  27918. 'begin',
  27919. ' DoIt(d);',
  27920. ' DoIt(dt);',
  27921. ' DoIt(i);',
  27922. ' DoIt(b);',
  27923. ' DoIt(shi);',
  27924. ' DoIt(w);',
  27925. ' DoIt(smi);',
  27926. ' DoIt(lw);',
  27927. ' DoIt(li);',
  27928. ' DoIt(ni);',
  27929. ' DoIt(nu);',
  27930. '']);
  27931. ConvertProgram;
  27932. CheckSource('TestJSValue_OverloadWord',
  27933. LinesToStr([ // statements
  27934. 'this.DoIt = function (w) {',
  27935. '};',
  27936. 'this.DoIt$1 = function (v) {',
  27937. '};',
  27938. 'this.d = 0.0;',
  27939. 'this.dt = 0.0;',
  27940. 'this.i = 0;',
  27941. 'this.b = 0;',
  27942. 'this.shi = 0;',
  27943. 'this.w = 0;',
  27944. 'this.smi = 0;',
  27945. 'this.lw = 0;',
  27946. 'this.li = 0;',
  27947. 'this.ni = 0;',
  27948. 'this.nu = 0;',
  27949. '']),
  27950. LinesToStr([ // $mod.$main
  27951. '$mod.DoIt$1($mod.d);',
  27952. '$mod.DoIt$1($mod.dt);',
  27953. '$mod.DoIt$1($mod.i);',
  27954. '$mod.DoIt($mod.b);',
  27955. '$mod.DoIt($mod.shi);',
  27956. '$mod.DoIt($mod.w);',
  27957. '$mod.DoIt$1($mod.smi);',
  27958. '$mod.DoIt$1($mod.lw);',
  27959. '$mod.DoIt$1($mod.li);',
  27960. '$mod.DoIt$1($mod.ni);',
  27961. '$mod.DoIt$1($mod.nu);',
  27962. '']));
  27963. end;
  27964. procedure TTestModule.TestJSValue_OverloadString;
  27965. begin
  27966. StartProgram(false);
  27967. Add([
  27968. 'type',
  27969. ' uni = string;',
  27970. ' WChar = char;',
  27971. 'procedure DoIt(s: string); begin end;',
  27972. 'procedure DoIt(v: jsvalue); begin end;',
  27973. 'var',
  27974. ' s: string;',
  27975. ' c: char;',
  27976. ' u: uni;',
  27977. 'begin',
  27978. ' DoIt(s);',
  27979. ' DoIt(c);',
  27980. ' DoIt(u);',
  27981. '']);
  27982. ConvertProgram;
  27983. CheckSource('TestJSValue_OverloadString',
  27984. LinesToStr([ // statements
  27985. 'this.DoIt = function (s) {',
  27986. '};',
  27987. 'this.DoIt$1 = function (v) {',
  27988. '};',
  27989. 'this.s = "";',
  27990. 'this.c = "";',
  27991. 'this.u = "";',
  27992. '']),
  27993. LinesToStr([ // $mod.$main
  27994. '$mod.DoIt($mod.s);',
  27995. '$mod.DoIt($mod.c);',
  27996. '$mod.DoIt($mod.u);',
  27997. '']));
  27998. end;
  27999. procedure TTestModule.TestJSValue_OverloadChar;
  28000. begin
  28001. StartProgram(false);
  28002. Add([
  28003. 'type',
  28004. ' uni = string;',
  28005. ' WChar = char;',
  28006. 'procedure DoIt(c: char); begin end;',
  28007. 'procedure DoIt(v: jsvalue); begin end;',
  28008. 'var',
  28009. ' s: string;',
  28010. ' c: char;',
  28011. ' u: uni;',
  28012. 'begin',
  28013. ' DoIt(s);',
  28014. ' DoIt(c);',
  28015. ' DoIt(u);',
  28016. '']);
  28017. ConvertProgram;
  28018. CheckSource('TestJSValue_OverloadChar',
  28019. LinesToStr([ // statements
  28020. 'this.DoIt = function (c) {',
  28021. '};',
  28022. 'this.DoIt$1 = function (v) {',
  28023. '};',
  28024. 'this.s = "";',
  28025. 'this.c = "";',
  28026. 'this.u = "";',
  28027. '']),
  28028. LinesToStr([ // $mod.$main
  28029. '$mod.DoIt$1($mod.s);',
  28030. '$mod.DoIt($mod.c);',
  28031. '$mod.DoIt$1($mod.u);',
  28032. '']));
  28033. end;
  28034. procedure TTestModule.TestJSValue_OverloadPointer;
  28035. begin
  28036. StartProgram(false);
  28037. Add([
  28038. 'type',
  28039. ' TObject = class end;',
  28040. 'procedure DoIt(p: pointer); begin end;',
  28041. 'procedure DoIt(v: jsvalue); begin end;',
  28042. 'var',
  28043. ' o: TObject;',
  28044. 'begin',
  28045. ' DoIt(o);',
  28046. '']);
  28047. ConvertProgram;
  28048. CheckSource('TestJSValue_OverloadPointer',
  28049. LinesToStr([ // statements
  28050. 'rtl.createClass(this, "TObject", null, function () {',
  28051. ' this.$init = function () {',
  28052. ' };',
  28053. ' this.$final = function () {',
  28054. ' };',
  28055. '});',
  28056. 'this.DoIt = function (p) {',
  28057. '};',
  28058. 'this.DoIt$1 = function (v) {',
  28059. '};',
  28060. 'this.o = null;',
  28061. '']),
  28062. LinesToStr([ // $mod.$main
  28063. '$mod.DoIt($mod.o);',
  28064. '']));
  28065. end;
  28066. procedure TTestModule.TestJSValue_ForIn;
  28067. begin
  28068. StartProgram(false);
  28069. Add([
  28070. 'var',
  28071. ' v: JSValue;',
  28072. ' key: string;',
  28073. 'begin',
  28074. ' for key in v do begin',
  28075. ' if key=''abc'' then ;',
  28076. ' end;',
  28077. '']);
  28078. ConvertProgram;
  28079. CheckSource('TestJSValue_ForIn',
  28080. LinesToStr([ // statements
  28081. 'this.v = undefined;',
  28082. 'this.key = "";',
  28083. '']),
  28084. LinesToStr([ // $mod.$main
  28085. 'for ($mod.key in $mod.v) {',
  28086. ' if ($mod.key === "abc") ;',
  28087. '};',
  28088. '']));
  28089. end;
  28090. procedure TTestModule.TestRTTI_IntRange;
  28091. begin
  28092. WithTypeInfo:=true;
  28093. StartProgram(true,[supTypeInfo]);
  28094. Add([
  28095. '{$modeswitch externalclass}',
  28096. 'type',
  28097. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28098. ' TColor = type TGraphicsColor;',
  28099. 'var',
  28100. ' p: TTypeInfo;',
  28101. ' k: TTypeKind;',
  28102. 'begin',
  28103. ' p:=typeinfo(TGraphicsColor);',
  28104. ' p:=typeinfo(TColor);',
  28105. ' k:=GetTypeKind(TGraphicsColor);',
  28106. ' k:=GetTypeKind(TColor);',
  28107. '']);
  28108. ConvertProgram;
  28109. CheckSource('TestRTTI_IntRange',
  28110. LinesToStr([ // statements
  28111. 'this.$rtti.$Int("TGraphicsColor", {',
  28112. ' minvalue: -2147483648,',
  28113. ' maxvalue: 2147483647,',
  28114. ' ordtype: 4',
  28115. '});',
  28116. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28117. 'this.p = null;',
  28118. 'this.k = 0;',
  28119. '']),
  28120. LinesToStr([ // $mod.$main
  28121. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28122. '$mod.p = $mod.$rtti["TColor"];',
  28123. '$mod.k = 1;',
  28124. '$mod.k = 1;',
  28125. '']));
  28126. end;
  28127. procedure TTestModule.TestRTTI_Double;
  28128. begin
  28129. WithTypeInfo:=true;
  28130. StartProgram(true,[supTypeInfo]);
  28131. Add([
  28132. '{$modeswitch externalclass}',
  28133. 'type',
  28134. ' TFloat = type double;',
  28135. 'var',
  28136. ' p: TTypeInfo;',
  28137. 'begin',
  28138. ' p:=typeinfo(double);',
  28139. ' p:=typeinfo(TFloat);',
  28140. '']);
  28141. ConvertProgram;
  28142. CheckSource('TestRTTI_Double',
  28143. LinesToStr([ // statements
  28144. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28145. 'this.p = null;',
  28146. '']),
  28147. LinesToStr([ // $mod.$main
  28148. '$mod.p = rtl.double;',
  28149. '$mod.p = $mod.$rtti["TFloat"];',
  28150. '']));
  28151. end;
  28152. procedure TTestModule.TestRTTI_ProcType;
  28153. begin
  28154. WithTypeInfo:=true;
  28155. StartProgram(false);
  28156. Add('type');
  28157. Add(' TProcA = procedure;');
  28158. Add(' TMethodB = procedure of object;');
  28159. Add(' TProcC = procedure; varargs;');
  28160. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28161. Add(' TProcE = function: nativeint;');
  28162. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28163. Add('var p: pointer;');
  28164. Add('begin');
  28165. Add(' p:=typeinfo(tproca);');
  28166. ConvertProgram;
  28167. CheckSource('TestRTTI_ProcType',
  28168. LinesToStr([ // statements
  28169. 'this.$rtti.$ProcVar("TProcA", {',
  28170. ' procsig: rtl.newTIProcSig(null)',
  28171. '});',
  28172. 'this.$rtti.$MethodVar("TMethodB", {',
  28173. ' procsig: rtl.newTIProcSig(null),',
  28174. ' methodkind: 0',
  28175. '});',
  28176. 'this.$rtti.$ProcVar("TProcC", {',
  28177. ' procsig: rtl.newTIProcSig(null, 2)',
  28178. '});',
  28179. 'this.$rtti.$ProcVar("TProcD", {',
  28180. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28181. '});',
  28182. 'this.$rtti.$ProcVar("TProcE", {',
  28183. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28184. '});',
  28185. 'this.$rtti.$ProcVar("TProcF", {',
  28186. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28187. '});',
  28188. 'this.p = null;',
  28189. '']),
  28190. LinesToStr([ // $mod.$main
  28191. '$mod.p = $mod.$rtti["TProcA"];',
  28192. '']));
  28193. end;
  28194. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28195. begin
  28196. WithTypeInfo:=true;
  28197. AddModuleWithIntfImplSrc('unit2.pas',
  28198. LinesToStr([
  28199. 'type',
  28200. ' TObject = class end;'
  28201. ]),
  28202. '');
  28203. StartUnit(true);
  28204. Add('interface');
  28205. Add('uses unit2;');
  28206. Add('type');
  28207. Add(' TProcA = function(o: tobject): tobject;');
  28208. Add('implementation');
  28209. Add('type');
  28210. Add(' TProcB = function(o: tobject): tobject;');
  28211. Add('var p: Pointer;');
  28212. Add('initialization');
  28213. Add(' p:=typeinfo(tproca);');
  28214. Add(' p:=typeinfo(tprocb);');
  28215. ConvertUnit;
  28216. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28217. LinesToStr([ // statements
  28218. 'var $impl = $mod.$impl;',
  28219. 'this.$rtti.$ProcVar("TProcA", {',
  28220. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28221. '});',
  28222. '']),
  28223. LinesToStr([ // this.$init
  28224. '$impl.p = $mod.$rtti["TProcA"];',
  28225. '$impl.p = $mod.$rtti["TProcB"];',
  28226. '']),
  28227. LinesToStr([ // implementation
  28228. '$mod.$rtti.$ProcVar("TProcB", {',
  28229. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28230. '});',
  28231. '$impl.p = null;',
  28232. '']) );
  28233. end;
  28234. procedure TTestModule.TestRTTI_EnumAndSetType;
  28235. begin
  28236. WithTypeInfo:=true;
  28237. StartProgram(false);
  28238. Add('type');
  28239. Add(' TFlag = (light,dark);');
  28240. Add(' TFlags = set of TFlag;');
  28241. Add(' TProc = function(f: TFlags): TFlag;');
  28242. Add('var p: pointer;');
  28243. Add('begin');
  28244. Add(' p:=typeinfo(tflag);');
  28245. Add(' p:=typeinfo(tflags);');
  28246. ConvertProgram;
  28247. CheckSource('TestRTTI_EnumAndType',
  28248. LinesToStr([ // statements
  28249. 'this.TFlag = {',
  28250. ' "0": "light",',
  28251. ' light: 0,',
  28252. ' "1": "dark",',
  28253. ' dark: 1',
  28254. '};',
  28255. 'this.$rtti.$Enum("TFlag", {',
  28256. ' minvalue: 0,',
  28257. ' maxvalue: 1,',
  28258. ' ordtype: 1,',
  28259. ' enumtype: this.TFlag',
  28260. '});',
  28261. 'this.$rtti.$Set("TFlags", {',
  28262. ' comptype: this.$rtti["TFlag"]',
  28263. '});',
  28264. 'this.$rtti.$ProcVar("TProc", {',
  28265. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28266. '});',
  28267. 'this.p = null;',
  28268. '']),
  28269. LinesToStr([ // $mod.$main
  28270. '$mod.p = $mod.$rtti["TFlag"];',
  28271. '$mod.p = $mod.$rtti["TFlags"];',
  28272. '']));
  28273. end;
  28274. procedure TTestModule.TestRTTI_EnumRange;
  28275. begin
  28276. WithTypeInfo:=true;
  28277. StartProgram(false);
  28278. Add([
  28279. 'type',
  28280. ' TCol = (red,green,blue);',
  28281. ' TColRg = green..blue;',
  28282. ' TSetOfColRg = set of TColRg;',
  28283. 'var p: pointer;',
  28284. 'begin',
  28285. ' p:=typeinfo(tcolrg);',
  28286. ' p:=typeinfo(tsetofcolrg);',
  28287. '']);
  28288. ConvertProgram;
  28289. end;
  28290. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28291. begin
  28292. WithTypeInfo:=true;
  28293. StartProgram(false);
  28294. Add('type');
  28295. Add(' TFlags = set of (red, green);');
  28296. Add('var');
  28297. Add(' f: TFlags;');
  28298. Add('begin');
  28299. Add(' Include(f,red);');
  28300. ConvertProgram;
  28301. CheckSource('TestRTTI_AnonymousEnumType',
  28302. LinesToStr([ // statements
  28303. 'this.TFlags$a = {',
  28304. ' "0": "red",',
  28305. ' red: 0,',
  28306. ' "1": "green",',
  28307. ' green: 1',
  28308. '};',
  28309. 'this.$rtti.$Enum("TFlags$a", {',
  28310. ' minvalue: 0,',
  28311. ' maxvalue: 1,',
  28312. ' ordtype: 1,',
  28313. ' enumtype: this.TFlags$a',
  28314. '});',
  28315. 'this.$rtti.$Set("TFlags", {',
  28316. ' comptype: this.$rtti["TFlags$a"]',
  28317. '});',
  28318. 'this.f = {};',
  28319. '']),
  28320. LinesToStr([
  28321. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28322. '']));
  28323. end;
  28324. procedure TTestModule.TestRTTI_StaticArray;
  28325. begin
  28326. WithTypeInfo:=true;
  28327. StartProgram(false);
  28328. Add('type');
  28329. Add(' TFlag = (light,dark);');
  28330. Add(' TFlagNames = array[TFlag] of string;');
  28331. Add(' TBoolNames = array[boolean] of string;');
  28332. Add(' TByteArray = array[1..32768] of byte;');
  28333. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28334. Add('var p: pointer;');
  28335. Add('begin');
  28336. Add(' p:=typeinfo(TFlagNames);');
  28337. Add(' p:=typeinfo(TBoolNames);');
  28338. ConvertProgram;
  28339. CheckSource('TestRTTI_StaticArray',
  28340. LinesToStr([ // statements
  28341. 'this.TFlag = {',
  28342. ' "0": "light",',
  28343. ' light: 0,',
  28344. ' "1": "dark",',
  28345. ' dark: 1',
  28346. '};',
  28347. 'this.$rtti.$Enum("TFlag", {',
  28348. ' minvalue: 0,',
  28349. ' maxvalue: 1,',
  28350. ' ordtype: 1,',
  28351. ' enumtype: this.TFlag',
  28352. '});',
  28353. 'this.$rtti.$StaticArray("TFlagNames", {',
  28354. ' dims: [2],',
  28355. ' eltype: rtl.string',
  28356. '});',
  28357. 'this.$rtti.$StaticArray("TBoolNames", {',
  28358. ' dims: [2],',
  28359. ' eltype: rtl.string',
  28360. '});',
  28361. 'this.$rtti.$StaticArray("TByteArray", {',
  28362. ' dims: [32768],',
  28363. ' eltype: rtl.byte',
  28364. '});',
  28365. 'this.$rtti.$ProcVar("TProc", {',
  28366. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28367. '});',
  28368. 'this.p = null;',
  28369. '']),
  28370. LinesToStr([ // $mod.$main
  28371. '$mod.p = $mod.$rtti["TFlagNames"];',
  28372. '$mod.p = $mod.$rtti["TBoolNames"];',
  28373. '']));
  28374. end;
  28375. procedure TTestModule.TestRTTI_DynArray;
  28376. begin
  28377. WithTypeInfo:=true;
  28378. StartProgram(false);
  28379. Add('type');
  28380. Add(' TArrStr = array of string;');
  28381. Add(' TArr2Dim = array of tarrstr;');
  28382. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28383. Add('var p: pointer;');
  28384. Add('begin');
  28385. Add(' p:=typeinfo(tarrstr);');
  28386. Add(' p:=typeinfo(tarr2dim);');
  28387. ConvertProgram;
  28388. CheckSource('TestRTTI_DynArray',
  28389. LinesToStr([ // statements
  28390. 'this.$rtti.$DynArray("TArrStr", {',
  28391. ' eltype: rtl.string',
  28392. '});',
  28393. 'this.$rtti.$DynArray("TArr2Dim", {',
  28394. ' eltype: this.$rtti["TArrStr"]',
  28395. '});',
  28396. 'this.$rtti.$ProcVar("TProc", {',
  28397. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28398. '});',
  28399. 'this.p = null;',
  28400. '']),
  28401. LinesToStr([ // $mod.$main
  28402. '$mod.p = $mod.$rtti["TArrStr"];',
  28403. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28404. '']));
  28405. end;
  28406. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28407. begin
  28408. WithTypeInfo:=true;
  28409. StartProgram(false);
  28410. Add('type');
  28411. Add(' TArr = array of array of longint;');
  28412. Add('var a: TArr;');
  28413. Add('begin');
  28414. ConvertProgram;
  28415. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28416. LinesToStr([ // statements
  28417. 'this.$rtti.$DynArray("TArr$a", {',
  28418. ' eltype: rtl.longint',
  28419. '});',
  28420. 'this.$rtti.$DynArray("TArr", {',
  28421. ' eltype: this.$rtti["TArr$a"]',
  28422. '});',
  28423. 'this.a = [];',
  28424. '']),
  28425. LinesToStr([ // $mod.$main
  28426. ]));
  28427. end;
  28428. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28429. begin
  28430. WithTypeInfo:=true;
  28431. StartProgram(false);
  28432. Add('type');
  28433. Add(' TObject = class');
  28434. Add(' published');
  28435. Add(' procedure Proc; virtual; abstract;');
  28436. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28437. Add(' end;');
  28438. Add('begin');
  28439. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28440. nDuplicatePublishedMethodXAtY);
  28441. ConvertProgram;
  28442. end;
  28443. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28444. begin
  28445. WithTypeInfo:=true;
  28446. StartProgram(false);
  28447. Add('type');
  28448. Add(' TObject = class');
  28449. Add(' published');
  28450. Add(' procedure Proc; external name ''foo'';');
  28451. Add(' end;');
  28452. Add('begin');
  28453. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28454. nPublishedNameMustMatchExternal);
  28455. ConvertProgram;
  28456. end;
  28457. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28458. begin
  28459. WithTypeInfo:=true;
  28460. StartProgram(false);
  28461. Add('type');
  28462. Add(' TObject = class');
  28463. Add(' class var FA: longint;');
  28464. Add(' published');
  28465. Add(' class property A: longint read FA;');
  28466. Add(' end;');
  28467. Add('begin');
  28468. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28469. nInvalidXModifierY);
  28470. ConvertProgram;
  28471. end;
  28472. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28473. begin
  28474. WithTypeInfo:=true;
  28475. StartProgram(false);
  28476. Add('type');
  28477. Add(' TObject = class');
  28478. Add(' published');
  28479. Add(' class var FA: longint;');
  28480. Add(' end;');
  28481. Add('begin');
  28482. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28483. nSymbolCannotBePublished);
  28484. ConvertProgram;
  28485. end;
  28486. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28487. begin
  28488. WithTypeInfo:=true;
  28489. StartProgram(false);
  28490. Add('{$modeswitch externalclass}');
  28491. Add('type');
  28492. Add(' TObject = class');
  28493. Add(' published');
  28494. Add(' V: longint; external name ''foo'';');
  28495. Add(' end;');
  28496. Add('begin');
  28497. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28498. nPublishedNameMustMatchExternal);
  28499. ConvertProgram;
  28500. end;
  28501. procedure TTestModule.TestRTTI_Class_Field;
  28502. begin
  28503. WithTypeInfo:=true;
  28504. StartProgram(false);
  28505. Add('{$modeswitch externalclass}');
  28506. Add('type');
  28507. Add(' TObject = class');
  28508. Add(' private');
  28509. Add(' FPropA: string;');
  28510. Add(' published');
  28511. Add(' VarLI: longint;');
  28512. Add(' VarC: char;');
  28513. Add(' VarS: string;');
  28514. Add(' VarD: double;');
  28515. Add(' VarB: boolean;');
  28516. Add(' VarLW: longword;');
  28517. Add(' VarSmI: smallint;');
  28518. Add(' VarW: word;');
  28519. Add(' VarShI: shortint;');
  28520. Add(' VarBy: byte;');
  28521. Add(' VarExt: longint external name ''VarExt'';');
  28522. Add(' ArrA, ArrB: array of byte;');
  28523. Add(' end;');
  28524. Add('var p: pointer;');
  28525. Add(' Obj: tobject;');
  28526. Add('begin');
  28527. Add(' p:=typeinfo(tobject);');
  28528. Add(' p:=typeinfo(p);');
  28529. Add(' p:=typeinfo(obj);');
  28530. ConvertProgram;
  28531. CheckSource('TestRTTI_Class_Field',
  28532. LinesToStr([ // statements
  28533. 'rtl.createClass(this, "TObject", null, function () {',
  28534. ' this.$init = function () {',
  28535. ' this.FPropA = "";',
  28536. ' this.VarLI = 0;',
  28537. ' this.VarC = "";',
  28538. ' this.VarS = "";',
  28539. ' this.VarD = 0.0;',
  28540. ' this.VarB = false;',
  28541. ' this.VarLW = 0;',
  28542. ' this.VarSmI = 0;',
  28543. ' this.VarW = 0;',
  28544. ' this.VarShI = 0;',
  28545. ' this.VarBy = 0;',
  28546. ' this.ArrA = [];',
  28547. ' this.ArrB = [];',
  28548. ' };',
  28549. ' this.$final = function () {',
  28550. ' this.ArrA = undefined;',
  28551. ' this.ArrB = undefined;',
  28552. ' };',
  28553. ' var $r = this.$rtti;',
  28554. ' $r.addField("VarLI", rtl.longint);',
  28555. ' $r.addField("VarC", rtl.char);',
  28556. ' $r.addField("VarS", rtl.string);',
  28557. ' $r.addField("VarD", rtl.double);',
  28558. ' $r.addField("VarB", rtl.boolean);',
  28559. ' $r.addField("VarLW", rtl.longword);',
  28560. ' $r.addField("VarSmI", rtl.smallint);',
  28561. ' $r.addField("VarW", rtl.word);',
  28562. ' $r.addField("VarShI", rtl.shortint);',
  28563. ' $r.addField("VarBy", rtl.byte);',
  28564. ' $r.addField("VarExt", rtl.longint);',
  28565. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28566. ' eltype: rtl.byte',
  28567. ' });',
  28568. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28569. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28570. '});',
  28571. 'this.p = null;',
  28572. 'this.Obj = null;',
  28573. '']),
  28574. LinesToStr([ // $mod.$main
  28575. '$mod.p = $mod.$rtti["TObject"];',
  28576. '$mod.p = rtl.pointer;',
  28577. '$mod.p = $mod.Obj.$rtti;',
  28578. '']));
  28579. end;
  28580. procedure TTestModule.TestRTTI_Class_Method;
  28581. begin
  28582. WithTypeInfo:=true;
  28583. StartProgram(false);
  28584. Add('type');
  28585. Add(' TObject = class');
  28586. Add(' private');
  28587. Add(' procedure Internal; external name ''$intern'';');
  28588. Add(' published');
  28589. Add(' procedure Click; virtual; abstract;');
  28590. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28591. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28592. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28593. Add(' end;');
  28594. Add('begin');
  28595. ConvertProgram;
  28596. CheckSource('TestRTTI_Class_Method',
  28597. LinesToStr([ // statements
  28598. 'rtl.createClass(this, "TObject", null, function () {',
  28599. ' this.$init = function () {',
  28600. ' };',
  28601. ' this.$final = function () {',
  28602. ' };',
  28603. ' var $r = this.$rtti;',
  28604. ' $r.addMethod("Click", 0, null);',
  28605. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28606. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28607. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28608. ' flags: 2',
  28609. ' });',
  28610. '});',
  28611. '']),
  28612. LinesToStr([ // $mod.$main
  28613. '']));
  28614. end;
  28615. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28616. begin
  28617. WithTypeInfo:=true;
  28618. StartProgram(false);
  28619. Add('type');
  28620. Add(' TObject = class');
  28621. Add(' published');
  28622. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28623. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28624. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28625. Add(' end;');
  28626. Add('begin');
  28627. ConvertProgram;
  28628. CheckSource('TestRTTI_Class_MethodOpenArray',
  28629. LinesToStr([ // statements
  28630. 'rtl.createClass(this, "TObject", null, function () {',
  28631. ' this.$init = function () {',
  28632. ' };',
  28633. ' this.$final = function () {',
  28634. ' };',
  28635. ' var $r = this.$rtti;',
  28636. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28637. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28638. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28639. '});',
  28640. '']),
  28641. LinesToStr([ // $mod.$main
  28642. '']));
  28643. end;
  28644. procedure TTestModule.TestRTTI_Class_Property;
  28645. begin
  28646. WithTypeInfo:=true;
  28647. StartProgram(false);
  28648. Add('{$modeswitch externalclass}');
  28649. Add('type');
  28650. Add(' TObject = class');
  28651. Add(' private');
  28652. Add(' FColor: longint;');
  28653. Add(' FColorStored: boolean;');
  28654. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28655. Add(' function GetColor: longint; virtual; abstract;');
  28656. Add(' function GetColorStored: boolean; virtual; abstract;');
  28657. Add(' FExtSize: longint external name ''$extSize'';');
  28658. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28659. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28660. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28661. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28662. Add(' published');
  28663. Add(' property ColorA: longint read FColor;');
  28664. Add(' property ColorB: longint write FColor;');
  28665. Add(' property ColorC: longint read GetColor write SetColor;');
  28666. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28667. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28668. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28669. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28670. Add(' end;');
  28671. Add('begin');
  28672. ConvertProgram;
  28673. CheckSource('TestRTTI_Class_Property',
  28674. LinesToStr([ // statements
  28675. 'rtl.createClass(this, "TObject", null, function () {',
  28676. ' this.$init = function () {',
  28677. ' this.FColor = 0;',
  28678. ' this.FColorStored = false;',
  28679. ' };',
  28680. ' this.$final = function () {',
  28681. ' };',
  28682. ' var $r = this.$rtti;',
  28683. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28684. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28685. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28686. ' $r.addProperty(',
  28687. ' "ColorD",',
  28688. ' 8,',
  28689. ' rtl.longint,',
  28690. ' "FColor",',
  28691. ' "FColor",',
  28692. ' {',
  28693. ' stored: "FColorStored"',
  28694. ' }',
  28695. ' );',
  28696. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28697. ' $r.addProperty(',
  28698. ' "ExtSizeB",',
  28699. ' 11,',
  28700. ' rtl.longint,',
  28701. ' "$getSize",',
  28702. ' "$setSize",',
  28703. ' {',
  28704. ' stored: "$extSizeStored"',
  28705. ' }',
  28706. ' );',
  28707. ' $r.addProperty(',
  28708. ' "ExtSizeC",',
  28709. ' 12,',
  28710. ' rtl.longint,',
  28711. ' "$extSize",',
  28712. ' "$extSize",',
  28713. ' {',
  28714. ' stored: "$getExtSizeStored"',
  28715. ' }',
  28716. ' );',
  28717. '});',
  28718. '']),
  28719. LinesToStr([ // $mod.$main
  28720. '']));
  28721. end;
  28722. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28723. begin
  28724. WithTypeInfo:=true;
  28725. StartProgram(false);
  28726. Add('{$modeswitch externalclass}');
  28727. Add('type');
  28728. Add(' integer = longint;');
  28729. Add(' TObject = class');
  28730. Add(' private');
  28731. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28732. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28733. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28734. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28735. Add(' published');
  28736. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28737. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28738. Add(' end;');
  28739. Add('begin');
  28740. ConvertProgram;
  28741. CheckSource('TestRTTI_Class_PropertyParams',
  28742. LinesToStr([ // statements
  28743. 'rtl.createClass(this, "TObject", null, function () {',
  28744. ' this.$init = function () {',
  28745. ' };',
  28746. ' this.$final = function () {',
  28747. ' };',
  28748. ' var $r = this.$rtti;',
  28749. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28750. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28751. '});',
  28752. '']),
  28753. LinesToStr([ // $mod.$main
  28754. '']));
  28755. end;
  28756. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28757. begin
  28758. WithTypeInfo:=true;
  28759. AddModuleWithIntfImplSrc('unit1.pas',
  28760. 'type TColor = -5..5;',
  28761. '');
  28762. StartProgram(true);
  28763. Add([
  28764. 'uses unit1;',
  28765. 'type',
  28766. ' TColorAlias = TColor;',
  28767. ' TColorTypeAlias = type TColor;',
  28768. ' TObject = class',
  28769. ' private',
  28770. ' fColor: TColor;',
  28771. ' fAlias: TColorAlias;',
  28772. ' fTypeAlias: TColorTypeAlias;',
  28773. ' published',
  28774. ' property Color: TColor read fcolor;',
  28775. ' property Alias: TColorAlias read falias;',
  28776. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28777. ' end;',
  28778. 'begin',
  28779. '']);
  28780. ConvertProgram;
  28781. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28782. LinesToStr([ // statements
  28783. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28784. 'rtl.createClass(this, "TObject", null, function () {',
  28785. ' this.$init = function () {',
  28786. ' this.fColor = 0;',
  28787. ' this.fAlias = 0;',
  28788. ' this.fTypeAlias = 0;',
  28789. ' };',
  28790. ' this.$final = function () {',
  28791. ' };',
  28792. ' var $r = this.$rtti;',
  28793. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28794. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28795. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28796. '});',
  28797. '']),
  28798. LinesToStr([ // $mod.$main
  28799. '']));
  28800. end;
  28801. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28802. begin
  28803. WithTypeInfo:=true;
  28804. StartProgram(false);
  28805. Add([
  28806. '{$modeswitch omitrtti}',
  28807. 'type',
  28808. ' TObject = class',
  28809. ' private',
  28810. ' FA: byte;',
  28811. ' published',
  28812. ' property A: byte read FA write FA;',
  28813. ' end;',
  28814. 'begin']);
  28815. ConvertProgram;
  28816. CheckSource('TestRTTI_Class_OmitRTTI',
  28817. LinesToStr([ // statements
  28818. 'rtl.createClass(this, "TObject", null, function () {',
  28819. ' this.$init = function () {',
  28820. ' this.FA = 0;',
  28821. ' };',
  28822. ' this.$final = function () {',
  28823. ' };',
  28824. '});',
  28825. '']),
  28826. LinesToStr([ // $mod.$main
  28827. '']));
  28828. end;
  28829. procedure TTestModule.TestRTTI_IndexModifier;
  28830. begin
  28831. WithTypeInfo:=true;
  28832. StartProgram(false);
  28833. Add([
  28834. 'type',
  28835. ' TEnum = (red, blue);',
  28836. ' TObject = class',
  28837. ' FB: boolean;',
  28838. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28839. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28840. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28841. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28842. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28843. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28844. ' published',
  28845. ' property B1: boolean index 1 read FB write SetIntBool;',
  28846. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28847. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28848. ' end;',
  28849. 'begin']);
  28850. ConvertProgram;
  28851. CheckSource('TestRTTI_IndexModifier',
  28852. LinesToStr([ // statements
  28853. 'this.TEnum = {',
  28854. ' "0": "red",',
  28855. ' red: 0,',
  28856. ' "1": "blue",',
  28857. ' blue: 1',
  28858. '};',
  28859. 'this.$rtti.$Enum("TEnum", {',
  28860. ' minvalue: 0,',
  28861. ' maxvalue: 1,',
  28862. ' ordtype: 1,',
  28863. ' enumtype: this.TEnum',
  28864. '});',
  28865. 'rtl.createClass(this, "TObject", null, function () {',
  28866. ' this.$init = function () {',
  28867. ' this.FB = false;',
  28868. ' };',
  28869. ' this.$final = function () {',
  28870. ' };',
  28871. ' var $r = this.$rtti;',
  28872. ' $r.addProperty(',
  28873. ' "B1",',
  28874. ' 18,',
  28875. ' rtl.boolean,',
  28876. ' "FB",',
  28877. ' "SetIntBool",',
  28878. ' {',
  28879. ' index: 1',
  28880. ' }',
  28881. ' );',
  28882. ' $r.addProperty(',
  28883. ' "B2",',
  28884. ' 17,',
  28885. ' rtl.boolean,',
  28886. ' "GetEnumBool",',
  28887. ' "FB",',
  28888. ' {',
  28889. ' index: $mod.TEnum.blue',
  28890. ' }',
  28891. ' );',
  28892. ' $r.addProperty(',
  28893. ' "I1",',
  28894. ' 19,',
  28895. ' rtl.boolean,',
  28896. ' "GetStrIntBool",',
  28897. ' "SetStrIntBool",',
  28898. ' {',
  28899. ' index: 2',
  28900. ' }',
  28901. ' );',
  28902. '});',
  28903. '']),
  28904. LinesToStr([ // $mod.$main
  28905. '']));
  28906. end;
  28907. procedure TTestModule.TestRTTI_StoredModifier;
  28908. begin
  28909. WithTypeInfo:=true;
  28910. StartProgram(false);
  28911. Add([
  28912. 'const',
  28913. ' ConstB = true;',
  28914. 'type',
  28915. ' TObject = class',
  28916. ' private',
  28917. ' FB: boolean;',
  28918. ' function IsBStored: boolean; virtual; abstract;',
  28919. ' published',
  28920. ' property BoolA: boolean read FB stored true;',
  28921. ' property BoolB: boolean read FB stored false;',
  28922. ' property BoolC: boolean read FB stored FB;',
  28923. ' property BoolD: boolean read FB stored ConstB;',
  28924. ' property BoolE: boolean read FB stored IsBStored;',
  28925. ' end;',
  28926. 'begin']);
  28927. ConvertProgram;
  28928. CheckSource('TestRTTI_StoredModifier',
  28929. LinesToStr([ // statements
  28930. 'this.ConstB = true;',
  28931. 'rtl.createClass(this, "TObject", null, function () {',
  28932. ' this.$init = function () {',
  28933. ' this.FB = false;',
  28934. ' };',
  28935. ' this.$final = function () {',
  28936. ' };',
  28937. ' var $r = this.$rtti;',
  28938. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  28939. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  28940. ' $r.addProperty(',
  28941. ' "BoolC",',
  28942. ' 8,',
  28943. ' rtl.boolean,',
  28944. ' "FB",',
  28945. ' "",',
  28946. ' {',
  28947. ' stored: "FB"',
  28948. ' }',
  28949. ' );',
  28950. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  28951. ' $r.addProperty(',
  28952. ' "BoolE",',
  28953. ' 12,',
  28954. ' rtl.boolean,',
  28955. ' "FB",',
  28956. ' "",',
  28957. ' {',
  28958. ' stored: "IsBStored"',
  28959. ' }',
  28960. ' );',
  28961. '});',
  28962. '']),
  28963. LinesToStr([ // $mod.$main
  28964. '']));
  28965. end;
  28966. procedure TTestModule.TestRTTI_DefaultValue;
  28967. begin
  28968. WithTypeInfo:=true;
  28969. StartProgram(false);
  28970. Add([
  28971. 'type',
  28972. ' TEnum = (red, blue);',
  28973. 'const',
  28974. ' CB = true or false;',
  28975. ' CI = 1+2;',
  28976. 'type',
  28977. ' TObject = class',
  28978. ' FB: boolean;',
  28979. ' FI: longint;',
  28980. ' FE: TEnum;',
  28981. ' published',
  28982. ' property B1: boolean read FB default true;',
  28983. ' property B2: boolean read FB default CB;',
  28984. ' property B3: boolean read FB default test1.cb;',
  28985. ' property I1: longint read FI default 2;',
  28986. ' property I2: longint read FI default CI;',
  28987. ' property E1: TEnum read FE default red;',
  28988. ' property E2: TEnum read FE default TEnum.blue;',
  28989. ' end;',
  28990. 'begin']);
  28991. ConvertProgram;
  28992. CheckSource('TestRTTI_DefaultValue',
  28993. LinesToStr([ // statements
  28994. 'this.TEnum = {',
  28995. ' "0": "red",',
  28996. ' red: 0,',
  28997. ' "1": "blue",',
  28998. ' blue: 1',
  28999. '};',
  29000. 'this.$rtti.$Enum("TEnum", {',
  29001. ' minvalue: 0,',
  29002. ' maxvalue: 1,',
  29003. ' ordtype: 1,',
  29004. ' enumtype: this.TEnum',
  29005. '});',
  29006. 'this.CB = true || false;',
  29007. 'this.CI = 1 + 2;',
  29008. 'rtl.createClass(this, "TObject", null, function () {',
  29009. ' this.$init = function () {',
  29010. ' this.FB = false;',
  29011. ' this.FI = 0;',
  29012. ' this.FE = 0;',
  29013. ' };',
  29014. ' this.$final = function () {',
  29015. ' };',
  29016. ' var $r = this.$rtti;',
  29017. ' $r.addProperty(',
  29018. ' "B1",',
  29019. ' 0,',
  29020. ' rtl.boolean,',
  29021. ' "FB",',
  29022. ' "",',
  29023. ' {',
  29024. ' Default: true',
  29025. ' }',
  29026. ' );',
  29027. ' $r.addProperty(',
  29028. ' "B2",',
  29029. ' 0,',
  29030. ' rtl.boolean,',
  29031. ' "FB",',
  29032. ' "",',
  29033. ' {',
  29034. ' Default: true',
  29035. ' }',
  29036. ' );',
  29037. ' $r.addProperty(',
  29038. ' "B3",',
  29039. ' 0,',
  29040. ' rtl.boolean,',
  29041. ' "FB",',
  29042. ' "",',
  29043. ' {',
  29044. ' Default: true',
  29045. ' }',
  29046. ' );',
  29047. ' $r.addProperty(',
  29048. ' "I1",',
  29049. ' 0,',
  29050. ' rtl.longint,',
  29051. ' "FI",',
  29052. ' "",',
  29053. ' {',
  29054. ' Default: 2',
  29055. ' }',
  29056. ' );',
  29057. ' $r.addProperty(',
  29058. ' "I2",',
  29059. ' 0,',
  29060. ' rtl.longint,',
  29061. ' "FI",',
  29062. ' "",',
  29063. ' {',
  29064. ' Default: 3',
  29065. ' }',
  29066. ' );',
  29067. ' $r.addProperty(',
  29068. ' "E1",',
  29069. ' 0,',
  29070. ' $mod.$rtti["TEnum"],',
  29071. ' "FE",',
  29072. ' "",',
  29073. ' {',
  29074. ' Default: $mod.TEnum.red',
  29075. ' }',
  29076. ' );',
  29077. ' $r.addProperty(',
  29078. ' "E2",',
  29079. ' 0,',
  29080. ' $mod.$rtti["TEnum"],',
  29081. ' "FE",',
  29082. ' "",',
  29083. ' {',
  29084. ' Default: $mod.TEnum.blue',
  29085. ' }',
  29086. ' );',
  29087. '});',
  29088. '']),
  29089. LinesToStr([ // $mod.$main
  29090. '']));
  29091. end;
  29092. procedure TTestModule.TestRTTI_DefaultValueSet;
  29093. begin
  29094. WithTypeInfo:=true;
  29095. StartProgram(false);
  29096. Add([
  29097. 'type',
  29098. ' TEnum = (red, blue);',
  29099. ' TSet = set of TEnum;',
  29100. 'const',
  29101. ' CSet = [red,blue];',
  29102. 'type',
  29103. ' TObject = class',
  29104. ' FSet: TSet;',
  29105. ' published',
  29106. ' property Set1: TSet read FSet default [];',
  29107. ' property Set2: TSet read FSet default [red];',
  29108. ' property Set3: TSet read FSet default [red,blue];',
  29109. ' property Set4: TSet read FSet default CSet;',
  29110. ' end;',
  29111. 'begin']);
  29112. ConvertProgram;
  29113. CheckSource('TestRTTI_DefaultValueSet',
  29114. LinesToStr([ // statements
  29115. 'this.TEnum = {',
  29116. ' "0": "red",',
  29117. ' red: 0,',
  29118. ' "1": "blue",',
  29119. ' blue: 1',
  29120. '};',
  29121. 'this.$rtti.$Enum("TEnum", {',
  29122. ' minvalue: 0,',
  29123. ' maxvalue: 1,',
  29124. ' ordtype: 1,',
  29125. ' enumtype: this.TEnum',
  29126. '});',
  29127. 'this.$rtti.$Set("TSet", {',
  29128. ' comptype: this.$rtti["TEnum"]',
  29129. '});',
  29130. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29131. 'rtl.createClass(this, "TObject", null, function () {',
  29132. ' this.$init = function () {',
  29133. ' this.FSet = {};',
  29134. ' };',
  29135. ' this.$final = function () {',
  29136. ' this.FSet = undefined;',
  29137. ' };',
  29138. ' var $r = this.$rtti;',
  29139. ' $r.addProperty(',
  29140. ' "Set1",',
  29141. ' 0,',
  29142. ' $mod.$rtti["TSet"],',
  29143. ' "FSet",',
  29144. ' "",',
  29145. ' {',
  29146. ' Default: {}',
  29147. ' }',
  29148. ' );',
  29149. ' $r.addProperty(',
  29150. ' "Set2",',
  29151. ' 0,',
  29152. ' $mod.$rtti["TSet"],',
  29153. ' "FSet",',
  29154. ' "",',
  29155. ' {',
  29156. ' Default: rtl.createSet($mod.TEnum.red)',
  29157. ' }',
  29158. ' );',
  29159. ' $r.addProperty(',
  29160. ' "Set3",',
  29161. ' 0,',
  29162. ' $mod.$rtti["TSet"],',
  29163. ' "FSet",',
  29164. ' "",',
  29165. ' {',
  29166. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29167. ' }',
  29168. ' );',
  29169. ' $r.addProperty(',
  29170. ' "Set4",',
  29171. ' 0,',
  29172. ' $mod.$rtti["TSet"],',
  29173. ' "FSet",',
  29174. ' "",',
  29175. ' {',
  29176. ' Default: $mod.CSet',
  29177. ' }',
  29178. ' );',
  29179. '});',
  29180. '']),
  29181. LinesToStr([ // $mod.$main
  29182. '']));
  29183. end;
  29184. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29185. begin
  29186. WithTypeInfo:=true;
  29187. StartProgram(false);
  29188. Add([
  29189. 'type',
  29190. ' TRg = -1..1;',
  29191. 'const',
  29192. ' l = low(TRg);',
  29193. ' h = high(TRg);',
  29194. 'type',
  29195. ' TObject = class',
  29196. ' FV: TRg;',
  29197. ' published',
  29198. ' property V1: TRg read FV default -1;',
  29199. ' end;',
  29200. 'begin']);
  29201. ConvertProgram;
  29202. CheckSource('TestRTTI_DefaultValueRangeType',
  29203. LinesToStr([ // statements
  29204. 'this.$rtti.$Int("TRg", {',
  29205. ' minvalue: -1,',
  29206. ' maxvalue: 1,',
  29207. ' ordtype: 0',
  29208. '});',
  29209. 'this.l = -1;',
  29210. 'this.h = 1;',
  29211. 'rtl.createClass(this, "TObject", null, function () {',
  29212. ' this.$init = function () {',
  29213. ' this.FV = 0;',
  29214. ' };',
  29215. ' this.$final = function () {',
  29216. ' };',
  29217. ' var $r = this.$rtti;',
  29218. ' $r.addProperty(',
  29219. ' "V1",',
  29220. ' 0,',
  29221. ' $mod.$rtti["TRg"],',
  29222. ' "FV",',
  29223. ' "",',
  29224. ' {',
  29225. ' Default: -1',
  29226. ' }',
  29227. ' );',
  29228. '});',
  29229. '']),
  29230. LinesToStr([ // $mod.$main
  29231. '']));
  29232. end;
  29233. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29234. begin
  29235. WithTypeInfo:=true;
  29236. StartProgram(false);
  29237. Add([
  29238. 'type',
  29239. ' TObject = class',
  29240. ' FA, FB: byte;',
  29241. ' property A: byte read FA default 1;',
  29242. ' property B: byte read FB default 2;',
  29243. ' end;',
  29244. ' TBird = class',
  29245. ' published',
  29246. ' property A;',
  29247. ' property B nodefault;',
  29248. ' end;',
  29249. 'begin']);
  29250. ConvertProgram;
  29251. CheckSource('TestRTTI_DefaultValueInherit',
  29252. LinesToStr([ // statements
  29253. 'rtl.createClass(this, "TObject", null, function () {',
  29254. ' this.$init = function () {',
  29255. ' this.FA = 0;',
  29256. ' this.FB = 0;',
  29257. ' };',
  29258. ' this.$final = function () {',
  29259. ' };',
  29260. '});',
  29261. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29262. ' var $r = this.$rtti;',
  29263. ' $r.addProperty(',
  29264. ' "A",',
  29265. ' 0,',
  29266. ' rtl.byte,',
  29267. ' "FA",',
  29268. ' "",',
  29269. ' {',
  29270. ' Default: 1',
  29271. ' }',
  29272. ' );',
  29273. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29274. '});',
  29275. '']),
  29276. LinesToStr([ // $mod.$main
  29277. '']));
  29278. end;
  29279. procedure TTestModule.TestRTTI_OverrideMethod;
  29280. begin
  29281. WithTypeInfo:=true;
  29282. StartProgram(false);
  29283. Add('type');
  29284. Add(' TObject = class');
  29285. Add(' published');
  29286. Add(' procedure DoIt; virtual; abstract;');
  29287. Add(' end;');
  29288. Add(' TSky = class');
  29289. Add(' published');
  29290. Add(' procedure DoIt; override;');
  29291. Add(' end;');
  29292. Add('procedure TSky.DoIt; begin end;');
  29293. Add('begin');
  29294. ConvertProgram;
  29295. CheckSource('TestRTTI_OverrideMethod',
  29296. LinesToStr([ // statements
  29297. 'rtl.createClass(this, "TObject", null, function () {',
  29298. ' this.$init = function () {',
  29299. ' };',
  29300. ' this.$final = function () {',
  29301. ' };',
  29302. ' var $r = this.$rtti;',
  29303. ' $r.addMethod("DoIt", 0, null);',
  29304. '});',
  29305. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29306. ' this.DoIt = function () {',
  29307. ' };',
  29308. '});',
  29309. '']),
  29310. LinesToStr([ // $mod.$main
  29311. '']));
  29312. end;
  29313. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29314. begin
  29315. WithTypeInfo:=true;
  29316. StartProgram(false);
  29317. Add([
  29318. 'type',
  29319. ' TObject = class',
  29320. ' published',
  29321. ' procedure DoIt;',
  29322. ' end;',
  29323. ' TSky = class',
  29324. ' published',
  29325. ' procedure DoIt; reintroduce;',
  29326. ' end;',
  29327. 'procedure TObject.DoIt; begin end;',
  29328. 'procedure TSky.DoIt;',
  29329. 'begin',
  29330. ' inherited DoIt;',
  29331. 'end;',
  29332. 'begin']);
  29333. ConvertProgram;
  29334. CheckSource('TestRTTI_ReintroduceMethod',
  29335. LinesToStr([ // statements
  29336. 'rtl.createClass(this, "TObject", null, function () {',
  29337. ' this.$init = function () {',
  29338. ' };',
  29339. ' this.$final = function () {',
  29340. ' };',
  29341. ' this.DoIt = function () {',
  29342. ' };',
  29343. ' var $r = this.$rtti;',
  29344. ' $r.addMethod("DoIt", 0, null);',
  29345. '});',
  29346. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29347. ' this.DoIt = function () {',
  29348. ' $mod.TObject.DoIt.call(this);',
  29349. ' };',
  29350. ' var $r = this.$rtti;',
  29351. ' $r.addMethod("DoIt", 0, null);',
  29352. '});',
  29353. '']),
  29354. LinesToStr([ // $mod.$main
  29355. '']));
  29356. end;
  29357. procedure TTestModule.TestRTTI_OverloadProperty;
  29358. begin
  29359. WithTypeInfo:=true;
  29360. StartProgram(false);
  29361. Add('type');
  29362. Add(' TObject = class');
  29363. Add(' protected');
  29364. Add(' FFlag: longint;');
  29365. Add(' published');
  29366. Add(' property Flag: longint read fflag;');
  29367. Add(' end;');
  29368. Add(' TSky = class');
  29369. Add(' published');
  29370. Add(' property FLAG: longint write fflag;');
  29371. Add(' end;');
  29372. Add('begin');
  29373. ConvertProgram;
  29374. CheckSource('TestRTTI_OverrideMethod',
  29375. LinesToStr([ // statements
  29376. 'rtl.createClass(this, "TObject", null, function () {',
  29377. ' this.$init = function () {',
  29378. ' this.FFlag = 0;',
  29379. ' };',
  29380. ' this.$final = function () {',
  29381. ' };',
  29382. ' var $r = this.$rtti;',
  29383. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29384. '});',
  29385. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29386. ' var $r = this.$rtti;',
  29387. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29388. '});',
  29389. '']),
  29390. LinesToStr([ // $mod.$main
  29391. '']));
  29392. end;
  29393. procedure TTestModule.TestRTTI_ClassForward;
  29394. begin
  29395. WithTypeInfo:=true;
  29396. StartProgram(false);
  29397. Add('type');
  29398. Add(' TObject = class end;');
  29399. Add(' tbridge = class;');
  29400. Add(' TProc = function: tbridge;');
  29401. Add(' TOger = class');
  29402. Add(' published');
  29403. Add(' FBridge: tbridge;');
  29404. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29405. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29406. Add(' end;');
  29407. Add(' TBridge = class');
  29408. Add(' FOger: toger;');
  29409. Add(' end;');
  29410. Add('var p: Pointer;');
  29411. Add(' b: tbridge;');
  29412. Add('begin');
  29413. Add(' p:=typeinfo(tbridge);');
  29414. Add(' p:=typeinfo(b);');
  29415. ConvertProgram;
  29416. CheckSource('TestRTTI_ClassForward',
  29417. LinesToStr([ // statements
  29418. 'rtl.createClass(this, "TObject", null, function () {',
  29419. ' this.$init = function () {',
  29420. ' };',
  29421. ' this.$final = function () {',
  29422. ' };',
  29423. '});',
  29424. 'this.$rtti.$Class("TBridge");',
  29425. 'this.$rtti.$ProcVar("TProc", {',
  29426. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29427. '});',
  29428. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29429. ' this.$init = function () {',
  29430. ' $mod.TObject.$init.call(this);',
  29431. ' this.FBridge = null;',
  29432. ' };',
  29433. ' this.$final = function () {',
  29434. ' this.FBridge = undefined;',
  29435. ' $mod.TObject.$final.call(this);',
  29436. ' };',
  29437. ' var $r = this.$rtti;',
  29438. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29439. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29440. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29441. '});',
  29442. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29443. ' this.$init = function () {',
  29444. ' $mod.TObject.$init.call(this);',
  29445. ' this.FOger = null;',
  29446. ' };',
  29447. ' this.$final = function () {',
  29448. ' this.FOger = undefined;',
  29449. ' $mod.TObject.$final.call(this);',
  29450. ' };',
  29451. '});',
  29452. 'this.p = null;',
  29453. 'this.b = null;',
  29454. '']),
  29455. LinesToStr([ // $mod.$main
  29456. '$mod.p = $mod.$rtti["TBridge"];',
  29457. '$mod.p = $mod.b.$rtti;',
  29458. '']));
  29459. end;
  29460. procedure TTestModule.TestRTTI_ClassOf;
  29461. begin
  29462. WithTypeInfo:=true;
  29463. StartProgram(false);
  29464. Add('type');
  29465. Add(' TClass = class of tobject;');
  29466. Add(' TProcA = function: TClass;');
  29467. Add(' TObject = class');
  29468. Add(' published');
  29469. Add(' C: tclass;');
  29470. Add(' end;');
  29471. Add(' tfox = class;');
  29472. Add(' TBird = class end;');
  29473. Add(' TBirds = class of tbird;');
  29474. Add(' TFox = class end;');
  29475. Add(' TFoxes = class of tfox;');
  29476. Add(' TCows = class of TCow;');
  29477. Add(' TCow = class;');
  29478. Add(' TCow = class end;');
  29479. Add('begin');
  29480. ConvertProgram;
  29481. CheckSource('TestRTTI_ClassOf',
  29482. LinesToStr([ // statements
  29483. 'this.$rtti.$Class("TObject");',
  29484. 'this.$rtti.$ClassRef("TClass", {',
  29485. ' instancetype: this.$rtti["TObject"]',
  29486. '});',
  29487. 'this.$rtti.$ProcVar("TProcA", {',
  29488. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29489. '});',
  29490. 'rtl.createClass(this, "TObject", null, function () {',
  29491. ' this.$init = function () {',
  29492. ' this.C = null;',
  29493. ' };',
  29494. ' this.$final = function () {',
  29495. ' this.C = undefined;',
  29496. ' };',
  29497. ' var $r = this.$rtti;',
  29498. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29499. '});',
  29500. 'this.$rtti.$Class("TFox");',
  29501. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29502. '});',
  29503. 'this.$rtti.$ClassRef("TBirds", {',
  29504. ' instancetype: this.$rtti["TBird"]',
  29505. '});',
  29506. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29507. '});',
  29508. 'this.$rtti.$ClassRef("TFoxes", {',
  29509. ' instancetype: this.$rtti["TFox"]',
  29510. '});',
  29511. 'this.$rtti.$Class("TCow");',
  29512. 'this.$rtti.$ClassRef("TCows", {',
  29513. ' instancetype: this.$rtti["TCow"]',
  29514. '});',
  29515. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29516. '});',
  29517. '']),
  29518. LinesToStr([ // $mod.$main
  29519. '']));
  29520. end;
  29521. procedure TTestModule.TestRTTI_Record;
  29522. begin
  29523. WithTypeInfo:=true;
  29524. StartProgram(false);
  29525. Add('type');
  29526. Add(' integer = longint;');
  29527. Add(' TPoint = record');
  29528. Add(' x,y: integer;');
  29529. Add(' end;');
  29530. Add('var p: pointer;');
  29531. Add(' r: tpoint;');
  29532. Add('begin');
  29533. Add(' p:=typeinfo(tpoint);');
  29534. Add(' p:=typeinfo(r);');
  29535. Add(' p:=typeinfo(r.x);');
  29536. ConvertProgram;
  29537. CheckSource('TestRTTI_Record',
  29538. LinesToStr([ // statements
  29539. 'rtl.recNewT(this, "TPoint", function () {',
  29540. ' this.x = 0;',
  29541. ' this.y = 0;',
  29542. ' this.$eq = function (b) {',
  29543. ' return (this.x === b.x) && (this.y === b.y);',
  29544. ' };',
  29545. ' this.$assign = function (s) {',
  29546. ' this.x = s.x;',
  29547. ' this.y = s.y;',
  29548. ' return this;',
  29549. ' };',
  29550. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29551. ' $r.addField("x", rtl.longint);',
  29552. ' $r.addField("y", rtl.longint);',
  29553. '});',
  29554. 'this.p = null;',
  29555. 'this.r = this.TPoint.$new();',
  29556. '']),
  29557. LinesToStr([ // $mod.$main
  29558. '$mod.p = $mod.$rtti["TPoint"];',
  29559. '$mod.p = $mod.$rtti["TPoint"];',
  29560. '$mod.p = rtl.longint;',
  29561. '']));
  29562. end;
  29563. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29564. begin
  29565. WithTypeInfo:=true;
  29566. StartProgram(false);
  29567. Add('type');
  29568. Add(' TFloatRec = record');
  29569. Add(' c,d: array of char;');
  29570. // Add(' i: array of array of longint;');
  29571. Add(' end;');
  29572. Add('var p: pointer;');
  29573. Add(' r: tfloatrec;');
  29574. Add('begin');
  29575. Add(' p:=typeinfo(tfloatrec);');
  29576. Add(' p:=typeinfo(r);');
  29577. Add(' p:=typeinfo(r.d);');
  29578. ConvertProgram;
  29579. CheckSource('TestRTTI_Record',
  29580. LinesToStr([ // statements
  29581. 'rtl.recNewT(this, "TFloatRec", function () {',
  29582. ' this.$new = function () {',
  29583. ' var r = Object.create(this);',
  29584. ' r.c = [];',
  29585. ' r.d = [];',
  29586. ' return r;',
  29587. ' };',
  29588. ' this.$eq = function (b) {',
  29589. ' return (this.c === b.c) && (this.d === b.d);',
  29590. ' };',
  29591. ' this.$assign = function (s) {',
  29592. ' this.c = rtl.arrayRef(s.c);',
  29593. ' this.d = rtl.arrayRef(s.d);',
  29594. ' return this;',
  29595. ' };',
  29596. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29597. ' eltype: rtl.char',
  29598. ' });',
  29599. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29600. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29601. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29602. '});',
  29603. 'this.p = null;',
  29604. 'this.r = this.TFloatRec.$new();',
  29605. '']),
  29606. LinesToStr([ // $mod.$main
  29607. '$mod.p = $mod.$rtti["TFloatRec"];',
  29608. '$mod.p = $mod.$rtti["TFloatRec"];',
  29609. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29610. '']));
  29611. end;
  29612. procedure TTestModule.TestRTTI_LocalTypes;
  29613. begin
  29614. WithTypeInfo:=true;
  29615. StartProgram(false);
  29616. Add([
  29617. 'procedure DoIt;',
  29618. 'type',
  29619. ' integer = longint;',
  29620. ' TPoint = record',
  29621. ' x,y: integer;',
  29622. ' end;',
  29623. 'var p: TPoint;',
  29624. 'begin',
  29625. 'end;',
  29626. 'begin']);
  29627. ConvertProgram;
  29628. CheckSource('TestRTTI_LocalTypes',
  29629. LinesToStr([ // statements
  29630. 'var TPoint = rtl.recNewT(null, "", function () {',
  29631. ' this.x = 0;',
  29632. ' this.y = 0;',
  29633. ' this.$eq = function (b) {',
  29634. ' return (this.x === b.x) && (this.y === b.y);',
  29635. ' };',
  29636. ' this.$assign = function (s) {',
  29637. ' this.x = s.x;',
  29638. ' this.y = s.y;',
  29639. ' return this;',
  29640. ' };',
  29641. '});',
  29642. 'this.DoIt = function () {',
  29643. ' var p = TPoint.$new();',
  29644. '};',
  29645. '']),
  29646. LinesToStr([ // $mod.$main
  29647. '']));
  29648. end;
  29649. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29650. begin
  29651. WithTypeInfo:=true;
  29652. StartProgram(false);
  29653. Add([
  29654. 'type',
  29655. ' TCaption = string;',
  29656. ' TYesNo = boolean;',
  29657. ' TLetter = char;',
  29658. ' TFloat = double;',
  29659. ' TPtr = pointer;',
  29660. ' TShortInt = shortint;',
  29661. ' TByte = byte;',
  29662. ' TSmallInt = smallint;',
  29663. ' TWord = word;',
  29664. ' TInt32 = longint;',
  29665. ' TDWord = longword;',
  29666. ' TValue = jsvalue;',
  29667. 'var p: TPtr;',
  29668. 'begin',
  29669. ' p:=typeinfo(string);',
  29670. ' p:=typeinfo(tcaption);',
  29671. ' p:=typeinfo(boolean);',
  29672. ' p:=typeinfo(tyesno);',
  29673. ' p:=typeinfo(char);',
  29674. ' p:=typeinfo(tletter);',
  29675. ' p:=typeinfo(double);',
  29676. ' p:=typeinfo(tfloat);',
  29677. ' p:=typeinfo(pointer);',
  29678. ' p:=typeinfo(tptr);',
  29679. ' p:=typeinfo(shortint);',
  29680. ' p:=typeinfo(tshortint);',
  29681. ' p:=typeinfo(byte);',
  29682. ' p:=typeinfo(tbyte);',
  29683. ' p:=typeinfo(smallint);',
  29684. ' p:=typeinfo(tsmallint);',
  29685. ' p:=typeinfo(word);',
  29686. ' p:=typeinfo(tword);',
  29687. ' p:=typeinfo(longword);',
  29688. ' p:=typeinfo(tdword);',
  29689. ' p:=typeinfo(jsvalue);',
  29690. ' p:=typeinfo(tvalue);',
  29691. '']);
  29692. ConvertProgram;
  29693. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29694. LinesToStr([ // statements
  29695. 'this.p = null;',
  29696. '']),
  29697. LinesToStr([ // $mod.$main
  29698. '$mod.p = rtl.string;',
  29699. '$mod.p = rtl.string;',
  29700. '$mod.p = rtl.boolean;',
  29701. '$mod.p = rtl.boolean;',
  29702. '$mod.p = rtl.char;',
  29703. '$mod.p = rtl.char;',
  29704. '$mod.p = rtl.double;',
  29705. '$mod.p = rtl.double;',
  29706. '$mod.p = rtl.pointer;',
  29707. '$mod.p = rtl.pointer;',
  29708. '$mod.p = rtl.shortint;',
  29709. '$mod.p = rtl.shortint;',
  29710. '$mod.p = rtl.byte;',
  29711. '$mod.p = rtl.byte;',
  29712. '$mod.p = rtl.smallint;',
  29713. '$mod.p = rtl.smallint;',
  29714. '$mod.p = rtl.word;',
  29715. '$mod.p = rtl.word;',
  29716. '$mod.p = rtl.longword;',
  29717. '$mod.p = rtl.longword;',
  29718. '$mod.p = rtl.jsvalue;',
  29719. '$mod.p = rtl.jsvalue;',
  29720. '']));
  29721. end;
  29722. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29723. begin
  29724. WithTypeInfo:=true;
  29725. StartProgram(false);
  29726. Add([
  29727. 'type',
  29728. ' TCaption = type string;',
  29729. ' TYesNo = type boolean;',
  29730. ' TLetter = type char;',
  29731. ' TFloat = type double;',
  29732. ' TPtr = type pointer;',
  29733. ' TShortInt = type shortint;',
  29734. ' TByte = type byte;',
  29735. ' TSmallInt = type smallint;',
  29736. ' TWord = type word;',
  29737. ' TInt32 = type longint;',
  29738. ' TDWord = type longword;',
  29739. ' TValue = type jsvalue;',
  29740. ' TAliasValue = type TValue;',
  29741. 'var',
  29742. ' p: TPtr;',
  29743. ' a: TAliasValue;',
  29744. 'begin',
  29745. ' p:=typeinfo(tcaption);',
  29746. ' p:=typeinfo(tyesno);',
  29747. ' p:=typeinfo(tletter);',
  29748. ' p:=typeinfo(tfloat);',
  29749. ' p:=typeinfo(tptr);',
  29750. ' p:=typeinfo(tshortint);',
  29751. ' p:=typeinfo(tbyte);',
  29752. ' p:=typeinfo(tsmallint);',
  29753. ' p:=typeinfo(tword);',
  29754. ' p:=typeinfo(tdword);',
  29755. ' p:=typeinfo(tvalue);',
  29756. ' p:=typeinfo(taliasvalue);',
  29757. ' p:=typeinfo(a);',
  29758. '']);
  29759. ConvertProgram;
  29760. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29761. LinesToStr([ // statements
  29762. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  29763. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29764. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  29765. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29766. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29767. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29768. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  29769. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29770. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  29771. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  29772. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  29773. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29774. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  29775. 'this.p = null;',
  29776. 'this.a = undefined;',
  29777. '']),
  29778. LinesToStr([ // $mod.$main
  29779. '$mod.p = $mod.$rtti["TCaption"];',
  29780. '$mod.p = $mod.$rtti["TYesNo"];',
  29781. '$mod.p = $mod.$rtti["TLetter"];',
  29782. '$mod.p = $mod.$rtti["TFloat"];',
  29783. '$mod.p = $mod.$rtti["TPtr"];',
  29784. '$mod.p = $mod.$rtti["TShortInt"];',
  29785. '$mod.p = $mod.$rtti["TByte"];',
  29786. '$mod.p = $mod.$rtti["TSmallInt"];',
  29787. '$mod.p = $mod.$rtti["TWord"];',
  29788. '$mod.p = $mod.$rtti["TDWord"];',
  29789. '$mod.p = $mod.$rtti["TValue"];',
  29790. '$mod.p = $mod.$rtti["TAliasValue"];',
  29791. '$mod.p = $mod.$rtti["TAliasValue"];',
  29792. '']));
  29793. end;
  29794. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29795. begin
  29796. WithTypeInfo:=true;
  29797. StartProgram(false);
  29798. Add('procedure DoIt;');
  29799. Add('type');
  29800. Add(' integer = longint;');
  29801. Add(' TPoint = record');
  29802. Add(' x,y: integer;');
  29803. Add(' end;');
  29804. Add('var p: pointer;');
  29805. Add('begin');
  29806. Add(' p:=typeinfo(tpoint);');
  29807. Add('end;');
  29808. Add('begin');
  29809. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29810. ConvertProgram;
  29811. end;
  29812. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29813. begin
  29814. WithTypeInfo:=true;
  29815. StartProgram(true,[supTypeInfo]);
  29816. Add([
  29817. '{$modeswitch externalclass}',
  29818. 'type',
  29819. ' TFlag = (up,down);',
  29820. ' TFlags = set of TFlag;',
  29821. 'var',
  29822. ' ti: TTypeInfo;',
  29823. ' tiInt: TTypeInfoInteger;',
  29824. ' tiEnum: TTypeInfoEnum;',
  29825. ' tiSet: TTypeInfoSet;',
  29826. 'begin',
  29827. ' ti:=typeinfo(string);',
  29828. ' ti:=typeinfo(boolean);',
  29829. ' ti:=typeinfo(char);',
  29830. ' ti:=typeinfo(double);',
  29831. ' tiInt:=typeinfo(shortint);',
  29832. ' tiInt:=typeinfo(byte);',
  29833. ' tiInt:=typeinfo(smallint);',
  29834. ' tiInt:=typeinfo(word);',
  29835. ' tiInt:=typeinfo(longint);',
  29836. ' tiInt:=typeinfo(longword);',
  29837. ' ti:=typeinfo(jsvalue);',
  29838. ' tiEnum:=typeinfo(tflag);',
  29839. ' tiSet:=typeinfo(tflags);']);
  29840. ConvertProgram;
  29841. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29842. LinesToStr([ // statements
  29843. 'this.TFlag = {',
  29844. ' "0": "up",',
  29845. ' up: 0,',
  29846. ' "1": "down",',
  29847. ' down: 1',
  29848. '};',
  29849. 'this.$rtti.$Enum("TFlag", {',
  29850. ' minvalue: 0,',
  29851. ' maxvalue: 1,',
  29852. ' ordtype: 1,',
  29853. ' enumtype: this.TFlag',
  29854. '});',
  29855. 'this.$rtti.$Set("TFlags", {',
  29856. ' comptype: this.$rtti["TFlag"]',
  29857. '});',
  29858. 'this.ti = null;',
  29859. 'this.tiInt = null;',
  29860. 'this.tiEnum = null;',
  29861. 'this.tiSet = null;',
  29862. '']),
  29863. LinesToStr([ // $mod.$main
  29864. '$mod.ti = rtl.string;',
  29865. '$mod.ti = rtl.boolean;',
  29866. '$mod.ti = rtl.char;',
  29867. '$mod.ti = rtl.double;',
  29868. '$mod.tiInt = rtl.shortint;',
  29869. '$mod.tiInt = rtl.byte;',
  29870. '$mod.tiInt = rtl.smallint;',
  29871. '$mod.tiInt = rtl.word;',
  29872. '$mod.tiInt = rtl.longint;',
  29873. '$mod.tiInt = rtl.longword;',
  29874. '$mod.ti = rtl.jsvalue;',
  29875. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  29876. '$mod.tiSet = $mod.$rtti["TFlags"];',
  29877. '']));
  29878. end;
  29879. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  29880. begin
  29881. WithTypeInfo:=true;
  29882. StartProgram(true,[supTypeInfo]);
  29883. Add('{$modeswitch externalclass}');
  29884. Add('type');
  29885. Add(' TStaticArr = array[boolean] of string;');
  29886. Add(' TDynArr = array of string;');
  29887. Add(' TProc = procedure;');
  29888. Add(' TMethod = procedure of object;');
  29889. Add('var');
  29890. Add(' StaticArray: TStaticArr;');
  29891. Add(' tiStaticArray: TTypeInfoStaticArray;');
  29892. Add(' DynArray: TDynArr;');
  29893. Add(' tiDynArray: TTypeInfoDynArray;');
  29894. Add(' ProcVar: TProc;');
  29895. Add(' tiProcVar: TTypeInfoProcVar;');
  29896. Add(' MethodVar: TMethod;');
  29897. Add(' tiMethodVar: TTypeInfoMethodVar;');
  29898. Add('begin');
  29899. Add(' tiStaticArray:=typeinfo(StaticArray);');
  29900. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  29901. Add(' tiDynArray:=typeinfo(DynArray);');
  29902. Add(' tiDynArray:=typeinfo(TDynArr);');
  29903. Add(' tiProcVar:=typeinfo(ProcVar);');
  29904. Add(' tiProcVar:=typeinfo(TProc);');
  29905. Add(' tiMethodVar:=typeinfo(MethodVar);');
  29906. Add(' tiMethodVar:=typeinfo(TMethod);');
  29907. ConvertProgram;
  29908. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  29909. LinesToStr([ // statements
  29910. 'this.$rtti.$StaticArray("TStaticArr", {',
  29911. ' dims: [2],',
  29912. ' eltype: rtl.string',
  29913. '});',
  29914. 'this.$rtti.$DynArray("TDynArr", {',
  29915. ' eltype: rtl.string',
  29916. '});',
  29917. 'this.$rtti.$ProcVar("TProc", {',
  29918. ' procsig: rtl.newTIProcSig(null)',
  29919. '});',
  29920. 'this.$rtti.$MethodVar("TMethod", {',
  29921. ' procsig: rtl.newTIProcSig(null),',
  29922. ' methodkind: 0',
  29923. '});',
  29924. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  29925. 'this.tiStaticArray = null;',
  29926. 'this.DynArray = [];',
  29927. 'this.tiDynArray = null;',
  29928. 'this.ProcVar = null;',
  29929. 'this.tiProcVar = null;',
  29930. 'this.MethodVar = null;',
  29931. 'this.tiMethodVar = null;',
  29932. '']),
  29933. LinesToStr([ // $mod.$main
  29934. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29935. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29936. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29937. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29938. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29939. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29940. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29941. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29942. '']));
  29943. end;
  29944. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  29945. begin
  29946. WithTypeInfo:=true;
  29947. StartProgram(true,[supTypeInfo]);
  29948. Add('{$modeswitch externalclass}');
  29949. Add('type');
  29950. Add(' TRec = record end;');
  29951. // ToDo: ^PRec
  29952. Add(' TObject = class end;');
  29953. Add(' TClass = class of tobject;');
  29954. Add('var');
  29955. Add(' Rec: trec;');
  29956. Add(' tiRecord: ttypeinforecord;');
  29957. Add(' Obj: tobject;');
  29958. Add(' tiClass: ttypeinfoclass;');
  29959. Add(' aClass: tclass;');
  29960. Add(' tiClassRef: ttypeinfoclassref;');
  29961. // ToDo: ^PRec
  29962. Add(' tiPointer: ttypeinfopointer;');
  29963. Add('begin');
  29964. Add(' tirecord:=typeinfo(trec);');
  29965. Add(' tirecord:=typeinfo(trec);');
  29966. Add(' ticlass:=typeinfo(obj);');
  29967. Add(' ticlass:=typeinfo(tobject);');
  29968. Add(' ticlass:=typeinfo(aclass);');
  29969. Add(' ticlassref:=typeinfo(tclass);');
  29970. ConvertProgram;
  29971. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  29972. LinesToStr([ // statements
  29973. 'rtl.recNewT(this, "TRec", function () {',
  29974. ' this.$eq = function (b) {',
  29975. ' return true;',
  29976. ' };',
  29977. ' this.$assign = function (s) {',
  29978. ' return this;',
  29979. ' };',
  29980. ' $mod.$rtti.$Record("TRec", {});',
  29981. '});',
  29982. 'rtl.createClass(this, "TObject", null, function () {',
  29983. ' this.$init = function () {',
  29984. ' };',
  29985. ' this.$final = function () {',
  29986. ' };',
  29987. '});',
  29988. 'this.$rtti.$ClassRef("TClass", {',
  29989. ' instancetype: this.$rtti["TObject"]',
  29990. '});',
  29991. 'this.Rec = this.TRec.$new();',
  29992. 'this.tiRecord = null;',
  29993. 'this.Obj = null;',
  29994. 'this.tiClass = null;',
  29995. 'this.aClass = null;',
  29996. 'this.tiClassRef = null;',
  29997. 'this.tiPointer = null;',
  29998. '']),
  29999. LinesToStr([ // $mod.$main
  30000. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30001. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30002. '$mod.tiClass = $mod.Obj.$rtti;',
  30003. '$mod.tiClass = $mod.$rtti["TObject"];',
  30004. '$mod.tiClass = $mod.aClass.$rtti;',
  30005. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30006. '']));
  30007. end;
  30008. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30009. begin
  30010. WithTypeInfo:=true;
  30011. StartProgram(true,[supTypeInfo]);
  30012. Add([
  30013. '{$modeswitch externalclass}',
  30014. 'type',
  30015. ' TClass = class of tobject;',
  30016. ' TObject = class',
  30017. ' function MyClass: TClass;',
  30018. ' class function ClassType: TClass;',
  30019. ' end;',
  30020. 'function TObject.MyClass: TClass;',
  30021. 'var t: TTypeInfoClass;',
  30022. 'begin',
  30023. ' t:=TypeInfo(Self);',
  30024. ' t:=TypeInfo(Result);',
  30025. ' t:=TypeInfo(TObject);',
  30026. 'end;',
  30027. 'class function TObject.ClassType: TClass;',
  30028. 'var t: TTypeInfoClass;',
  30029. 'begin',
  30030. ' t:=TypeInfo(Self);',
  30031. ' t:=TypeInfo(Result);',
  30032. 'end;',
  30033. 'var',
  30034. ' Obj: TObject;',
  30035. ' t: TTypeInfoClass;',
  30036. 'begin',
  30037. ' t:=TypeInfo(TObject.ClassType);',
  30038. ' t:=TypeInfo(Obj.ClassType);',
  30039. ' t:=TypeInfo(Obj.MyClass);',
  30040. '']);
  30041. ConvertProgram;
  30042. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30043. LinesToStr([ // statements
  30044. 'this.$rtti.$Class("TObject");',
  30045. 'this.$rtti.$ClassRef("TClass", {',
  30046. ' instancetype: this.$rtti["TObject"]',
  30047. '});',
  30048. 'rtl.createClass(this, "TObject", null, function () {',
  30049. ' this.$init = function () {',
  30050. ' };',
  30051. ' this.$final = function () {',
  30052. ' };',
  30053. ' this.MyClass = function () {',
  30054. ' var Result = null;',
  30055. ' var t = null;',
  30056. ' t = this.$rtti;',
  30057. ' t = Result.$rtti;',
  30058. ' t = $mod.$rtti["TObject"];',
  30059. ' return Result;',
  30060. ' };',
  30061. ' this.ClassType = function () {',
  30062. ' var Result = null;',
  30063. ' var t = null;',
  30064. ' t = this.$rtti;',
  30065. ' t = Result.$rtti;',
  30066. ' return Result;',
  30067. ' };',
  30068. '});',
  30069. 'this.Obj = null;',
  30070. 'this.t = null;',
  30071. '']),
  30072. LinesToStr([ // $mod.$main
  30073. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30074. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30075. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30076. '']));
  30077. end;
  30078. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30079. begin
  30080. WithTypeInfo:=true;
  30081. AddModuleWithIntfImplSrc('typinfo.pas',
  30082. LinesToStr([
  30083. '{$modeswitch externalclass}',
  30084. 'type',
  30085. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30086. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30087. '']),
  30088. '');
  30089. AddModuleWithIntfImplSrc('unit2.pas',
  30090. LinesToStr([
  30091. 'uses typinfo;',
  30092. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30093. 'procedure DoPtr(p: PTypeInfo);',
  30094. 'procedure DoInfo(t: TTypeInfo);',
  30095. 'procedure DoInt(t: TTypeInfoInteger);',
  30096. '']),
  30097. LinesToStr([
  30098. 'procedure DoPtr(p: PTypeInfo);',
  30099. 'begin end;',
  30100. 'procedure DoInfo(t: TTypeInfo);',
  30101. 'begin end;',
  30102. 'procedure DoInt(t: TTypeInfoInteger);',
  30103. 'begin end;',
  30104. '']));
  30105. StartUnit(true);
  30106. Add([
  30107. 'interface',
  30108. 'uses unit2;', // does not use unit typinfo
  30109. 'implementation',
  30110. 'var',
  30111. ' i: byte;',
  30112. ' p: pointer;',
  30113. ' t: PTypeInfo;',
  30114. 'initialization',
  30115. ' p:=typeinfo(i);',
  30116. ' t:=typeinfo(i);',
  30117. ' if p=t then ;',
  30118. ' if p=typeinfo(i) then ;',
  30119. ' if typeinfo(i)=p then ;',
  30120. ' if t=typeinfo(i) then ;',
  30121. ' if typeinfo(i)=t then ;',
  30122. ' DoPtr(p);',
  30123. ' DoPtr(t);',
  30124. ' DoPtr(typeinfo(i));',
  30125. ' DoInfo(p);',
  30126. ' DoInfo(t);',
  30127. ' DoInfo(typeinfo(i));',
  30128. ' DoInt(typeinfo(i));',
  30129. '']);
  30130. ConvertUnit;
  30131. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30132. LinesToStr([ // statements
  30133. 'var $impl = $mod.$impl;',
  30134. '']),
  30135. LinesToStr([ // this.$init
  30136. '$impl.p = rtl.byte;',
  30137. '$impl.t = rtl.byte;',
  30138. 'if ($impl.p === $impl.t) ;',
  30139. 'if ($impl.p === rtl.byte) ;',
  30140. 'if (rtl.byte === $impl.p) ;',
  30141. 'if ($impl.t === rtl.byte) ;',
  30142. 'if (rtl.byte === $impl.t) ;',
  30143. 'pas.unit2.DoPtr($impl.p);',
  30144. 'pas.unit2.DoPtr($impl.t);',
  30145. 'pas.unit2.DoPtr(rtl.byte);',
  30146. 'pas.unit2.DoInfo($impl.p);',
  30147. 'pas.unit2.DoInfo($impl.t);',
  30148. 'pas.unit2.DoInfo(rtl.byte);',
  30149. 'pas.unit2.DoInt(rtl.byte);',
  30150. '']),
  30151. LinesToStr([ // implementation
  30152. '$impl.i = 0;',
  30153. '$impl.p = null;',
  30154. '$impl.t = null;',
  30155. '']) );
  30156. end;
  30157. procedure TTestModule.TestRTTI_Interface_Corba;
  30158. begin
  30159. WithTypeInfo:=true;
  30160. StartProgram(true,[supTypeInfo]);
  30161. Add([
  30162. '{$interfaces corba}',
  30163. '{$modeswitch externalclass}',
  30164. 'type',
  30165. ' IUnknown = interface',
  30166. ' end;',
  30167. ' IBird = interface',
  30168. ' function GetItem: longint;',
  30169. ' procedure SetItem(Value: longint);',
  30170. ' property Item: longint read GetItem write SetItem;',
  30171. ' end;',
  30172. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30173. 'var',
  30174. ' i: IBird;',
  30175. ' t: TTypeInfoInterface;',
  30176. 'begin',
  30177. ' t:=TypeInfo(IBird);',
  30178. ' t:=TypeInfo(i);',
  30179. ' DoIt(t);',
  30180. ' DoIt(TypeInfo(IBird));',
  30181. '']);
  30182. ConvertProgram;
  30183. CheckSource('TestRTTI_Interface_Corba',
  30184. LinesToStr([ // statements
  30185. 'rtl.createInterface(',
  30186. ' this,',
  30187. ' "IUnknown",',
  30188. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30189. ' [],',
  30190. ' null,',
  30191. ' function () {',
  30192. ' }',
  30193. ');',
  30194. 'rtl.createInterface(',
  30195. ' this,',
  30196. ' "IBird",',
  30197. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30198. ' ["GetItem", "SetItem"],',
  30199. ' null,',
  30200. ' function () {',
  30201. ' var $r = this.$rtti;',
  30202. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30203. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30204. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30205. ' }',
  30206. ');',
  30207. 'this.DoIt = function (t) {',
  30208. '}; ',
  30209. 'this.i = null;',
  30210. 'this.t = null;',
  30211. '']),
  30212. LinesToStr([ // $mod.$main
  30213. '$mod.t = $mod.$rtti["IBird"];',
  30214. '$mod.t = $mod.i.$rtti;',
  30215. '$mod.DoIt($mod.t);',
  30216. '$mod.DoIt($mod.$rtti["IBird"]);',
  30217. '']));
  30218. end;
  30219. procedure TTestModule.TestRTTI_Interface_COM;
  30220. begin
  30221. WithTypeInfo:=true;
  30222. StartProgram(true,[supTypeInfo]);
  30223. Add([
  30224. '{$interfaces com}',
  30225. '{$modeswitch externalclass}',
  30226. 'type',
  30227. ' TGuid = record end;',
  30228. ' integer = longint;',
  30229. ' IUnknown = interface',
  30230. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30231. ' function _AddRef: Integer;',
  30232. ' function _Release: Integer;',
  30233. ' end;',
  30234. ' IBird = interface',
  30235. ' function GetItem: longint;',
  30236. ' procedure SetItem(Value: longint);',
  30237. ' property Item: longint read GetItem write SetItem;',
  30238. ' end;',
  30239. 'var',
  30240. ' i: IBird;',
  30241. ' t: TTypeInfoInterface;',
  30242. 'begin',
  30243. ' t:=TypeInfo(IBird);',
  30244. ' t:=TypeInfo(i);',
  30245. '']);
  30246. ConvertProgram;
  30247. CheckSource('TestRTTI_Interface_COM',
  30248. LinesToStr([ // statements
  30249. 'rtl.recNewT(this, "TGuid", function () {',
  30250. ' this.$eq = function (b) {',
  30251. ' return true;',
  30252. ' };',
  30253. ' this.$assign = function (s) {',
  30254. ' return this;',
  30255. ' };',
  30256. ' $mod.$rtti.$Record("TGuid", {});',
  30257. '});',
  30258. 'rtl.createInterface(',
  30259. ' this,',
  30260. ' "IUnknown",',
  30261. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30262. ' ["QueryInterface", "_AddRef", "_Release"],',
  30263. ' null,',
  30264. ' function () {',
  30265. ' this.$kind = "com";',
  30266. ' var $r = this.$rtti;',
  30267. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30268. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30269. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30270. ' }',
  30271. ');',
  30272. 'rtl.createInterface(',
  30273. ' this,',
  30274. ' "IBird",',
  30275. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30276. ' ["GetItem", "SetItem"],',
  30277. ' this.IUnknown,',
  30278. ' function () {',
  30279. ' var $r = this.$rtti;',
  30280. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30281. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30282. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30283. ' }',
  30284. ');',
  30285. 'this.i = null;',
  30286. 'this.t = null;',
  30287. '']),
  30288. LinesToStr([ // $mod.$main
  30289. '$mod.t = $mod.$rtti["IBird"];',
  30290. '$mod.t = $mod.i.$rtti;',
  30291. '']));
  30292. end;
  30293. procedure TTestModule.TestRTTI_ClassHelper;
  30294. begin
  30295. WithTypeInfo:=true;
  30296. StartProgram(true,[supTypeInfo]);
  30297. Add([
  30298. '{$interfaces com}',
  30299. '{$modeswitch externalclass}',
  30300. 'type',
  30301. ' TObject = class',
  30302. ' end;',
  30303. ' THelper = class helper for TObject',
  30304. ' published',
  30305. ' function GetItem: longint;',
  30306. ' property Item: longint read GetItem;',
  30307. ' end;',
  30308. 'function THelper.GetItem: longint;',
  30309. 'begin',
  30310. 'end;',
  30311. 'var',
  30312. ' t: TTypeInfoHelper;',
  30313. 'begin',
  30314. ' t:=TypeInfo(THelper);',
  30315. '']);
  30316. ConvertProgram;
  30317. CheckSource('TestRTTI_ClassHelper',
  30318. LinesToStr([ // statements
  30319. 'rtl.createClass(this, "TObject", null, function () {',
  30320. ' this.$init = function () {',
  30321. ' };',
  30322. ' this.$final = function () {',
  30323. ' };',
  30324. '});',
  30325. 'rtl.createHelper(this, "THelper", null, function () {',
  30326. ' this.GetItem = function () {',
  30327. ' var Result = 0;',
  30328. ' return Result;',
  30329. ' };',
  30330. ' var $r = this.$rtti;',
  30331. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30332. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30333. '});',
  30334. 'this.t = null;',
  30335. '']),
  30336. LinesToStr([ // $mod.$main
  30337. '$mod.t = $mod.$rtti["THelper"];',
  30338. '']));
  30339. end;
  30340. procedure TTestModule.TestRTTI_ExternalClass;
  30341. begin
  30342. WithTypeInfo:=true;
  30343. StartProgram(true,[supTypeInfo]);
  30344. Add([
  30345. '{$modeswitch externalclass}',
  30346. 'type',
  30347. ' TJSObject = class external name ''Object''',
  30348. ' end;',
  30349. ' TJSArray = class external name ''Array'' (TJSObject)',
  30350. ' end;',
  30351. 'var',
  30352. ' p: Pointer;',
  30353. ' tc: TTypeInfoExtClass;',
  30354. 'begin',
  30355. ' p:=typeinfo(TJSArray);']);
  30356. ConvertProgram;
  30357. CheckSource('TestRTTI_ExternalClass',
  30358. LinesToStr([ // statements
  30359. 'this.$rtti.$ExtClass("TJSObject", {',
  30360. ' jsclass: "Object"',
  30361. '});',
  30362. 'this.$rtti.$ExtClass("TJSArray", {',
  30363. ' ancestor: this.$rtti["TJSObject"],',
  30364. ' jsclass: "Array"',
  30365. '});',
  30366. 'this.p = null;',
  30367. 'this.tc = null;',
  30368. '']),
  30369. LinesToStr([ // $mod.$main
  30370. '$mod.p = $mod.$rtti["TJSArray"];',
  30371. '']));
  30372. end;
  30373. procedure TTestModule.TestResourcestringProgram;
  30374. begin
  30375. AddModuleWithIntfImplSrc('unit2.pas',
  30376. LinesToStr([
  30377. 'resourcestring Title = ''Nice'';',
  30378. '']),
  30379. '');
  30380. StartProgram(true);
  30381. Add([
  30382. 'uses unit2;',
  30383. 'const Bar = ''bar'';',
  30384. 'resourcestring',
  30385. ' Red = ''red'';',
  30386. ' Foobar = ''fOo''+bar;',
  30387. 'var s: string;',
  30388. ' c: char;',
  30389. 'begin',
  30390. ' s:=red;',
  30391. ' s:=test1.red;',
  30392. ' s:=Title;',
  30393. ' c:=red[1];',
  30394. ' c:=test1.red[2];',
  30395. ' if red=foobar then ;',
  30396. ' if red[3]=red[4] then ;']);
  30397. ConvertProgram;
  30398. CheckSource('TestResourcestringProgram',
  30399. LinesToStr([ // statements
  30400. 'this.Bar = "bar";',
  30401. 'this.s = "";',
  30402. 'this.c = "";',
  30403. '$mod.$resourcestrings = {',
  30404. ' Red: {',
  30405. ' org: "red"',
  30406. ' },',
  30407. ' Foobar: {',
  30408. ' org: "fOobar"',
  30409. ' }',
  30410. '};',
  30411. '']),
  30412. LinesToStr([ // $mod.$main
  30413. '$mod.s = rtl.getResStr($mod, "Red");',
  30414. '$mod.s = rtl.getResStr($mod, "Red");',
  30415. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30416. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30417. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30418. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30419. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30420. '']));
  30421. end;
  30422. procedure TTestModule.TestResourcestringUnit;
  30423. begin
  30424. AddModuleWithIntfImplSrc('unit2.pas',
  30425. LinesToStr([
  30426. 'resourcestring Title = ''Nice'';',
  30427. '']),
  30428. '');
  30429. StartUnit(true);
  30430. Add([
  30431. 'interface',
  30432. 'uses unit2;',
  30433. 'const Red = ''rEd'';',
  30434. 'resourcestring',
  30435. ' Blue = ''blue'';',
  30436. ' NotRed = ''not''+Red;',
  30437. 'var s: string;',
  30438. 'implementation',
  30439. 'resourcestring',
  30440. ' ImplGreen = ''green'';',
  30441. 'initialization',
  30442. ' s:=blue+ImplGreen;',
  30443. ' s:=test1.blue+test1.implgreen;',
  30444. ' s:=blue[1]+implgreen[2];',
  30445. ' s:=Title;',
  30446. '']);
  30447. ConvertUnit;
  30448. CheckSource('TestResourcestringUnit',
  30449. LinesToStr([ // statements
  30450. 'this.Red = "rEd";',
  30451. 'this.s = "";',
  30452. '$mod.$resourcestrings = {',
  30453. ' Blue: {',
  30454. ' org: "blue"',
  30455. ' },',
  30456. ' NotRed: {',
  30457. ' org: "notrEd"',
  30458. ' },',
  30459. ' ImplGreen: {',
  30460. ' org: "green"',
  30461. ' }',
  30462. '};',
  30463. '']),
  30464. LinesToStr([ // $mod.$main
  30465. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30466. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30467. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30468. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30469. '']));
  30470. end;
  30471. procedure TTestModule.TestResourcestringImplementation;
  30472. begin
  30473. StartUnit(false);
  30474. Add([
  30475. 'interface',
  30476. 'implementation',
  30477. 'resourcestring',
  30478. ' ImplRed = ''red'';']);
  30479. ConvertUnit;
  30480. CheckSource('TestResourcestringImplementation',
  30481. LinesToStr([ // intf statements
  30482. 'var $impl = $mod.$impl;']),
  30483. LinesToStr([ // $mod.$init
  30484. '']),
  30485. LinesToStr([ // impl statements
  30486. '$mod.$resourcestrings = {',
  30487. ' ImplRed: {',
  30488. ' org: "red"',
  30489. ' }',
  30490. '};',
  30491. '']));
  30492. end;
  30493. procedure TTestModule.TestAttributes_Members;
  30494. begin
  30495. WithTypeInfo:=true;
  30496. StartProgram(false);
  30497. Add([
  30498. '{$modeswitch PrefixedAttributes}',
  30499. 'type',
  30500. ' TObject = class',
  30501. ' constructor Create;',
  30502. ' end;',
  30503. ' TCustomAttribute = class',
  30504. ' constructor Create(Id: word);',
  30505. ' end;',
  30506. ' [Missing]',
  30507. ' TBird = class',
  30508. ' published',
  30509. ' [Tcustom]',
  30510. ' FField: word;',
  30511. ' [tcustom(14)]',
  30512. ' property Size: word read FField;',
  30513. ' [Tcustom(15)]',
  30514. ' procedure Fly; virtual; abstract;',
  30515. ' end;',
  30516. ' TRec = record',
  30517. ' [Tcustom,tcustom(14)]',
  30518. ' Size: word;',
  30519. ' end;',
  30520. 'constructor TObject.Create; begin end;',
  30521. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30522. 'begin',
  30523. '']);
  30524. ConvertProgram;
  30525. CheckSource('TestAttributes_Members',
  30526. LinesToStr([ // statements
  30527. 'rtl.createClass(this, "TObject", null, function () {',
  30528. ' this.$init = function () {',
  30529. ' };',
  30530. ' this.$final = function () {',
  30531. ' };',
  30532. ' this.Create = function () {',
  30533. ' return this;',
  30534. ' };',
  30535. '});',
  30536. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30537. ' this.Create$1 = function (Id) {',
  30538. ' return this;',
  30539. ' };',
  30540. '});',
  30541. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30542. ' this.$init = function () {',
  30543. ' $mod.TObject.$init.call(this);',
  30544. ' this.FField = 0;',
  30545. ' };',
  30546. ' var $r = this.$rtti;',
  30547. ' $r.addField("FField", rtl.word, {',
  30548. ' attr: [$mod.TCustomAttribute, "Create"]',
  30549. ' });',
  30550. ' $r.addProperty(',
  30551. ' "Size",',
  30552. ' 0,',
  30553. ' rtl.word,',
  30554. ' "FField",',
  30555. ' "",',
  30556. ' {',
  30557. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30558. ' }',
  30559. ' );',
  30560. ' $r.addMethod("Fly", 0, null, null, {',
  30561. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30562. ' });',
  30563. '});',
  30564. 'rtl.recNewT(this, "TRec", function () {',
  30565. ' this.Size = 0;',
  30566. ' this.$eq = function (b) {',
  30567. ' return this.Size === b.Size;',
  30568. ' };',
  30569. ' this.$assign = function (s) {',
  30570. ' this.Size = s.Size;',
  30571. ' return this;',
  30572. ' };',
  30573. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30574. ' $r.addField("Size", rtl.word, {',
  30575. ' attr: [',
  30576. ' $mod.TCustomAttribute,',
  30577. ' "Create",',
  30578. ' $mod.TCustomAttribute,',
  30579. ' "Create$1",',
  30580. ' [14]',
  30581. ' ]',
  30582. ' });',
  30583. '});',
  30584. '']),
  30585. LinesToStr([ // $mod.$main
  30586. '']));
  30587. end;
  30588. procedure TTestModule.TestAttributes_Types;
  30589. begin
  30590. WithTypeInfo:=true;
  30591. StartProgram(false);
  30592. Add([
  30593. '{$modeswitch PrefixedAttributes}',
  30594. 'type',
  30595. ' TObject = class',
  30596. ' constructor Create(Id: word);',
  30597. ' end;',
  30598. ' TCustomAttribute = class',
  30599. ' end;',
  30600. ' [TCustom(1)]',
  30601. ' TMyClass = class',
  30602. ' end;',
  30603. ' [TCustom(2)]',
  30604. ' TRec = record',
  30605. ' end;',
  30606. ' [TCustom(3)]',
  30607. ' TInt = type word;',
  30608. 'constructor TObject.Create(Id: word);',
  30609. 'begin',
  30610. 'end;',
  30611. 'var p: pointer;',
  30612. 'begin',
  30613. ' p:=typeinfo(TMyClass);',
  30614. ' p:=typeinfo(TRec);',
  30615. ' p:=typeinfo(TInt);',
  30616. '']);
  30617. ConvertProgram;
  30618. CheckSource('TestAttributes_Types',
  30619. LinesToStr([ // statements
  30620. 'rtl.createClass(this, "TObject", null, function () {',
  30621. ' this.$init = function () {',
  30622. ' };',
  30623. ' this.$final = function () {',
  30624. ' };',
  30625. ' this.Create = function (Id) {',
  30626. ' return this;',
  30627. ' };',
  30628. '});',
  30629. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30630. '});',
  30631. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  30632. ' var $r = this.$rtti;',
  30633. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30634. '});',
  30635. 'rtl.recNewT(this, "TRec", function () {',
  30636. ' this.$eq = function (b) {',
  30637. ' return true;',
  30638. ' };',
  30639. ' this.$assign = function (s) {',
  30640. ' return this;',
  30641. ' };',
  30642. ' $mod.$rtti.$Record("TRec", {',
  30643. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30644. ' });',
  30645. '});',
  30646. 'this.$rtti.$inherited("TInt", rtl.word, {',
  30647. ' attr: [this.TCustomAttribute, "Create", [3]]',
  30648. '});',
  30649. 'this.p = null;',
  30650. '']),
  30651. LinesToStr([ // $mod.$main
  30652. '$mod.p = $mod.$rtti["TMyClass"];',
  30653. '$mod.p = $mod.$rtti["TRec"];',
  30654. '$mod.p = $mod.$rtti["TInt"];',
  30655. '']));
  30656. end;
  30657. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30658. begin
  30659. WithTypeInfo:=true;
  30660. StartProgram(false);
  30661. Add([
  30662. '{$modeswitch PrefixedAttributes}',
  30663. 'type',
  30664. ' TObject = class',
  30665. ' constructor Create;',
  30666. ' end;',
  30667. ' TCustomAttribute = class',
  30668. ' end;',
  30669. ' THelper = class helper for TCustomAttribute',
  30670. ' constructor Create(Id: word);',
  30671. ' end;',
  30672. ' [TCustom(3)]',
  30673. ' TMyInt = word;',
  30674. 'constructor TObject.Create; begin end;',
  30675. 'constructor THelper.Create(Id: word); begin end;',
  30676. 'begin',
  30677. ' if typeinfo(TMyInt)=nil then ;']);
  30678. ConvertProgram;
  30679. end;
  30680. procedure TTestModule.TestAssert;
  30681. begin
  30682. StartProgram(false);
  30683. Add([
  30684. 'procedure DoIt;',
  30685. 'var',
  30686. ' b: boolean;',
  30687. ' s: string;',
  30688. 'begin',
  30689. ' {$Assertions on}',
  30690. ' Assert(b);',
  30691. 'end;',
  30692. 'begin',
  30693. ' DoIt;',
  30694. '']);
  30695. ConvertProgram;
  30696. CheckSource('TestAssert',
  30697. LinesToStr([ // statements
  30698. 'this.DoIt = function () {',
  30699. ' var b = false;',
  30700. ' var s = "";',
  30701. ' if (!b) throw "assert failed";',
  30702. '};',
  30703. '']),
  30704. LinesToStr([ // $mod.$main
  30705. '$mod.DoIt();',
  30706. '']));
  30707. end;
  30708. procedure TTestModule.TestAssert_SysUtils;
  30709. begin
  30710. AddModuleWithIntfImplSrc('SysUtils.pas',
  30711. LinesToStr([
  30712. 'type',
  30713. ' TObject = class',
  30714. ' constructor Create;',
  30715. ' end;',
  30716. ' EAssertionFailed = class',
  30717. ' constructor Create(s: string);',
  30718. ' end;',
  30719. '']),
  30720. LinesToStr([
  30721. 'constructor TObject.Create;',
  30722. 'begin end;',
  30723. 'constructor EAssertionFailed.Create(s: string);',
  30724. 'begin end;',
  30725. '']) );
  30726. StartProgram(true);
  30727. Add([
  30728. 'uses sysutils;',
  30729. 'procedure DoIt;',
  30730. 'var',
  30731. ' b: boolean;',
  30732. ' s: string;',
  30733. 'begin',
  30734. ' {$Assertions on}',
  30735. ' Assert(b);',
  30736. ' Assert(b,''msg'');',
  30737. 'end;',
  30738. 'begin',
  30739. ' DoIt;',
  30740. '']);
  30741. ConvertProgram;
  30742. CheckSource('TestAssert_SysUtils',
  30743. LinesToStr([ // statements
  30744. 'this.DoIt = function () {',
  30745. ' var b = false;',
  30746. ' var s = "";',
  30747. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30748. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30749. '};',
  30750. '']),
  30751. LinesToStr([ // $mod.$main
  30752. '$mod.DoIt();',
  30753. '']));
  30754. end;
  30755. procedure TTestModule.TestObjectChecks;
  30756. begin
  30757. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30758. StartProgram(false);
  30759. Add([
  30760. 'type',
  30761. ' TObject = class',
  30762. ' procedure DoIt;',
  30763. ' end;',
  30764. ' TClass = class of tobject;',
  30765. ' TBird = class',
  30766. ' end;',
  30767. ' TBirdClass = class of TBird;',
  30768. 'var',
  30769. ' o : TObject;',
  30770. ' c: TClass;',
  30771. ' b: TBird;',
  30772. ' bc: TBirdClass;',
  30773. 'procedure TObject.DoIt;',
  30774. 'begin',
  30775. ' b:=TBird(o);',
  30776. 'end;',
  30777. 'begin',
  30778. ' o.DoIt;',
  30779. ' b:=TBird(o);',
  30780. ' bc:=TBirdClass(c);',
  30781. '']);
  30782. ConvertProgram;
  30783. CheckSource('TestCheckMethodCall',
  30784. LinesToStr([ // statements
  30785. 'rtl.createClass(this, "TObject", null, function () {',
  30786. ' this.$init = function () {',
  30787. ' };',
  30788. ' this.$final = function () {',
  30789. ' };',
  30790. ' this.DoIt = function () {',
  30791. ' rtl.checkMethodCall(this,$mod.TObject);',
  30792. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30793. ' };',
  30794. '});',
  30795. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30796. '});',
  30797. 'this.o = null;',
  30798. 'this.c = null;',
  30799. 'this.b = null;',
  30800. 'this.bc = null;',
  30801. '']),
  30802. LinesToStr([ // $mod.$main
  30803. '$mod.o.DoIt();',
  30804. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30805. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30806. '']));
  30807. end;
  30808. procedure TTestModule.TestOverflowChecks_Int;
  30809. begin
  30810. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  30811. StartProgram(false);
  30812. Add([
  30813. 'procedure DoIt;',
  30814. 'var',
  30815. ' b: byte;',
  30816. ' n: nativeint;',
  30817. ' u: nativeuint;',
  30818. ' c: currency;',
  30819. 'begin',
  30820. ' n:=n+n;',
  30821. ' n:=n-n;',
  30822. ' n:=n+b;',
  30823. ' n:=b-n;',
  30824. ' n:=n*n;',
  30825. ' n:=n*u;',
  30826. ' c:=c+b;',
  30827. ' c:=b+c;',
  30828. ' c:=c*b;',
  30829. ' c:=b*c;',
  30830. 'end;',
  30831. 'begin',
  30832. '']);
  30833. ConvertProgram;
  30834. CheckSource('TestOverflowChecks_Int',
  30835. LinesToStr([ // statements
  30836. 'this.DoIt = function () {',
  30837. ' var b = 0;',
  30838. ' var n = 0;',
  30839. ' var u = 0;',
  30840. ' var c = 0;',
  30841. ' n = rtl.oc(n + n);',
  30842. ' n = rtl.oc(n - n);',
  30843. ' n = rtl.oc(n + b);',
  30844. ' n = rtl.oc(b - n);',
  30845. ' n = rtl.oc(n * n);',
  30846. ' n = rtl.oc(n * u);',
  30847. ' c = rtl.oc(c + (b * 10000));',
  30848. ' c = rtl.oc((b * 10000) + c);',
  30849. ' c = rtl.oc(c * b);',
  30850. ' c = rtl.oc(b * c);',
  30851. '};',
  30852. '']),
  30853. LinesToStr([ // $mod.$main
  30854. '']));
  30855. end;
  30856. procedure TTestModule.TestRangeChecks_AssignInt;
  30857. begin
  30858. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30859. StartProgram(false);
  30860. Add([
  30861. '{$R+}',
  30862. 'var',
  30863. ' b: byte = 2;',
  30864. ' w: word = 3;',
  30865. 'procedure DoIt(p: byte);',
  30866. 'begin',
  30867. ' b:=w;',
  30868. ' b+=w;',
  30869. ' b:=1;',
  30870. 'end;',
  30871. '{$R-}',
  30872. 'procedure DoSome;',
  30873. 'begin',
  30874. ' DoIt(w);',
  30875. ' b:=w;',
  30876. ' b:=2;',
  30877. 'end;',
  30878. 'begin',
  30879. '{$R+}',
  30880. '']);
  30881. ConvertProgram;
  30882. CheckSource('TestRangeChecks_AssignInt',
  30883. LinesToStr([ // statements
  30884. 'this.b = 2;',
  30885. 'this.w = 3;',
  30886. 'this.DoIt = function (p) {',
  30887. ' rtl.rc(p, 0, 255);',
  30888. ' $mod.b = rtl.rc($mod.w,0,255);',
  30889. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  30890. ' $mod.b = 1;',
  30891. '};',
  30892. 'this.DoSome = function () {',
  30893. ' $mod.DoIt($mod.w);',
  30894. ' $mod.b = $mod.w;',
  30895. ' $mod.b = 2;',
  30896. '};',
  30897. '']),
  30898. LinesToStr([ // $mod.$main
  30899. '']));
  30900. end;
  30901. procedure TTestModule.TestRangeChecks_AssignIntRange;
  30902. begin
  30903. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30904. StartProgram(false);
  30905. Add([
  30906. '{$R+}',
  30907. 'type Ten = 1..10;',
  30908. 'var',
  30909. ' b: Ten = 2;',
  30910. ' w: Ten = 3;',
  30911. 'procedure DoIt(p: Ten);',
  30912. 'begin',
  30913. ' b:=w;',
  30914. ' b+=w;',
  30915. ' b:=1;',
  30916. 'end;',
  30917. '{$R-}',
  30918. 'procedure DoSome;',
  30919. 'begin',
  30920. ' DoIt(w);',
  30921. ' b:=w;',
  30922. ' b:=2;',
  30923. 'end;',
  30924. 'begin',
  30925. '{$R+}',
  30926. '']);
  30927. ConvertProgram;
  30928. CheckSource('TestRangeChecks_AssignIntRange',
  30929. LinesToStr([ // statements
  30930. 'this.b = 2;',
  30931. 'this.w = 3;',
  30932. 'this.DoIt = function (p) {',
  30933. ' rtl.rc(p, 1, 10);',
  30934. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  30935. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  30936. ' $mod.b = 1;',
  30937. '};',
  30938. 'this.DoSome = function () {',
  30939. ' $mod.DoIt($mod.w);',
  30940. ' $mod.b = $mod.w;',
  30941. ' $mod.b = 2;',
  30942. '};',
  30943. '']),
  30944. LinesToStr([ // $mod.$main
  30945. '']));
  30946. end;
  30947. procedure TTestModule.TestRangeChecks_AssignEnum;
  30948. begin
  30949. StartProgram(false);
  30950. Add([
  30951. '{$R+}',
  30952. 'type TEnum = (red,green);',
  30953. 'var',
  30954. ' e: TEnum = red;',
  30955. 'procedure DoIt(p: TEnum);',
  30956. 'begin',
  30957. ' e:=p;',
  30958. ' p:=TEnum(0);',
  30959. ' p:=succ(e);',
  30960. 'end;',
  30961. '{$R-}',
  30962. 'procedure DoSome;',
  30963. 'begin',
  30964. ' DoIt(e);',
  30965. ' e:=TEnum(1);',
  30966. ' e:=pred(e);',
  30967. 'end;',
  30968. 'begin',
  30969. '{$R+}',
  30970. '']);
  30971. ConvertProgram;
  30972. CheckSource('TestRangeChecks_AssignEnum',
  30973. LinesToStr([ // statements
  30974. 'this.TEnum = {',
  30975. ' "0": "red",',
  30976. ' red: 0,',
  30977. ' "1": "green",',
  30978. ' green: 1',
  30979. '};',
  30980. 'this.e = this.TEnum.red;',
  30981. 'this.DoIt = function (p) {',
  30982. ' rtl.rc(p, 0, 1);',
  30983. ' $mod.e = rtl.rc(p, 0, 1);',
  30984. ' p = 0;',
  30985. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30986. '};',
  30987. 'this.DoSome = function () {',
  30988. ' $mod.DoIt($mod.e);',
  30989. ' $mod.e = 1;',
  30990. ' $mod.e = $mod.e - 1;',
  30991. '};',
  30992. '']),
  30993. LinesToStr([ // $mod.$main
  30994. '']));
  30995. end;
  30996. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  30997. begin
  30998. StartProgram(false);
  30999. Add([
  31000. '{$R+}',
  31001. 'type',
  31002. ' TEnum = (red,green);',
  31003. ' TEnumRg = red..green;',
  31004. 'var',
  31005. ' e: TEnumRg = red;',
  31006. 'procedure DoIt(p: TEnumRg);',
  31007. 'begin',
  31008. ' e:=p;',
  31009. ' p:=TEnumRg(0);',
  31010. ' p:=succ(e);',
  31011. 'end;',
  31012. '{$R-}',
  31013. 'procedure DoSome;',
  31014. 'begin',
  31015. ' DoIt(e);',
  31016. ' e:=TEnum(1);',
  31017. ' e:=pred(e);',
  31018. 'end;',
  31019. 'begin',
  31020. '{$R+}',
  31021. '']);
  31022. ConvertProgram;
  31023. CheckSource('TestRangeChecks_AssignEnumRange',
  31024. LinesToStr([ // statements
  31025. 'this.TEnum = {',
  31026. ' "0": "red",',
  31027. ' red: 0,',
  31028. ' "1": "green",',
  31029. ' green: 1',
  31030. '};',
  31031. 'this.e = this.TEnum.red;',
  31032. 'this.DoIt = function (p) {',
  31033. ' rtl.rc(p, 0, 1);',
  31034. ' $mod.e = rtl.rc(p, 0, 1);',
  31035. ' p = 0;',
  31036. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31037. '};',
  31038. 'this.DoSome = function () {',
  31039. ' $mod.DoIt($mod.e);',
  31040. ' $mod.e = 1;',
  31041. ' $mod.e = $mod.e - 1;',
  31042. '};',
  31043. '']),
  31044. LinesToStr([ // $mod.$main
  31045. '']));
  31046. end;
  31047. procedure TTestModule.TestRangeChecks_AssignChar;
  31048. begin
  31049. StartProgram(false);
  31050. Add([
  31051. '{$R+}',
  31052. 'type',
  31053. ' TLetter = char;',
  31054. 'var',
  31055. ' b: TLetter = ''2'';',
  31056. ' w: TLetter = ''3'';',
  31057. 'procedure DoIt(p: TLetter);',
  31058. 'begin',
  31059. ' b:=w;',
  31060. ' b:=''1'';',
  31061. 'end;',
  31062. '{$R-}',
  31063. 'procedure DoSome;',
  31064. 'begin',
  31065. ' DoIt(w);',
  31066. ' b:=w;',
  31067. ' b:=''2'';',
  31068. 'end;',
  31069. 'begin',
  31070. '{$R+}',
  31071. '']);
  31072. ConvertProgram;
  31073. CheckSource('TestRangeChecks_AssignChar',
  31074. LinesToStr([ // statements
  31075. 'this.b = "2";',
  31076. 'this.w = "3";',
  31077. 'this.DoIt = function (p) {',
  31078. ' rtl.rcc(p, 0, 65535);',
  31079. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31080. ' $mod.b = "1";',
  31081. '};',
  31082. 'this.DoSome = function () {',
  31083. ' $mod.DoIt($mod.w);',
  31084. ' $mod.b = $mod.w;',
  31085. ' $mod.b = "2";',
  31086. '};',
  31087. '']),
  31088. LinesToStr([ // $mod.$main
  31089. '']));
  31090. end;
  31091. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31092. begin
  31093. StartProgram(false);
  31094. Add([
  31095. '{$R+}',
  31096. 'type TDigit = ''0''..''9'';',
  31097. 'var',
  31098. ' b: TDigit = ''2'';',
  31099. ' w: TDigit = ''3'';',
  31100. 'procedure DoIt(p: TDigit);',
  31101. 'begin',
  31102. ' b:=w;',
  31103. ' b:=''1'';',
  31104. 'end;',
  31105. '{$R-}',
  31106. 'procedure DoSome;',
  31107. 'begin',
  31108. ' DoIt(w);',
  31109. ' b:=w;',
  31110. ' b:=''2'';',
  31111. 'end;',
  31112. 'begin',
  31113. '{$R+}',
  31114. '']);
  31115. ConvertProgram;
  31116. CheckSource('TestRangeChecks_AssignCharRange',
  31117. LinesToStr([ // statements
  31118. 'this.b = "2";',
  31119. 'this.w = "3";',
  31120. 'this.DoIt = function (p) {',
  31121. ' rtl.rcc(p, 48, 57);',
  31122. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31123. ' $mod.b = "1";',
  31124. '};',
  31125. 'this.DoSome = function () {',
  31126. ' $mod.DoIt($mod.w);',
  31127. ' $mod.b = $mod.w;',
  31128. ' $mod.b = "2";',
  31129. '};',
  31130. '']),
  31131. LinesToStr([ // $mod.$main
  31132. '']));
  31133. end;
  31134. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31135. begin
  31136. StartProgram(false);
  31137. Add([
  31138. '{$R+}',
  31139. 'type',
  31140. ' Ten = 1..10;',
  31141. ' TArr = array of Ten;',
  31142. ' TArrArr = array of TArr;',
  31143. ' TArrByte = array[byte] of Ten;',
  31144. ' TArrChar = array[''0''..''9''] of Ten;',
  31145. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31146. ' TObject = class',
  31147. ' A: TArr;',
  31148. ' end;',
  31149. 'procedure DoIt;',
  31150. 'var',
  31151. ' Arr: TArr;',
  31152. ' ArrArr: TArrArr;',
  31153. ' ArrByte: TArrByte;',
  31154. ' ArrChar: TArrChar;',
  31155. ' ArrByteChar: TArrByteChar;',
  31156. ' i: Ten;',
  31157. ' c: char;',
  31158. ' o: tobject;',
  31159. 'begin',
  31160. ' i:=Arr[1];',
  31161. ' i:=ArrByteChar[1,''2''];',
  31162. ' Arr[1]:=Arr[1];',
  31163. ' Arr[i]:=Arr[i];',
  31164. ' ArrByte[3]:=ArrByte[3];',
  31165. ' ArrByte[i]:=ArrByte[i];',
  31166. ' ArrChar[''5'']:=ArrChar[''5''];',
  31167. ' ArrChar[c]:=ArrChar[c];',
  31168. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31169. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31170. ' o.a[i]:=o.a[i];',
  31171. 'end;',
  31172. 'begin',
  31173. '']);
  31174. ConvertProgram;
  31175. CheckSource('TestRangeChecks_ArrayIndex',
  31176. LinesToStr([ // statements
  31177. 'rtl.createClass(this, "TObject", null, function () {',
  31178. ' this.$init = function () {',
  31179. ' this.A = [];',
  31180. ' };',
  31181. ' this.$final = function () {',
  31182. ' this.A = undefined;',
  31183. ' };',
  31184. '});',
  31185. 'this.DoIt = function () {',
  31186. ' var Arr = [];',
  31187. ' var ArrArr = [];',
  31188. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31189. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31190. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31191. ' var i = 0;',
  31192. ' var c = "";',
  31193. ' var o = null;',
  31194. ' i = rtl.rc(Arr[1], 1, 10);',
  31195. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31196. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31197. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31198. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31199. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31200. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31201. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31202. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31203. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31204. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31205. '};',
  31206. '']),
  31207. LinesToStr([ // $mod.$main
  31208. '']));
  31209. end;
  31210. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31211. begin
  31212. StartProgram(false);
  31213. Add([
  31214. '{$R+}',
  31215. 'type',
  31216. ' Ten = 1..10;',
  31217. ' TRec = record x: Ten end;',
  31218. ' TArr = array of TRec;',
  31219. ' TArrArr = array of TArr;',
  31220. ' TObject = class',
  31221. ' A: TArr;',
  31222. ' end;',
  31223. 'procedure DoIt;',
  31224. 'var',
  31225. ' Arr: TArr;',
  31226. ' ArrArr: TArrArr;',
  31227. ' i: Ten;',
  31228. ' o: tobject;',
  31229. 'begin',
  31230. ' Arr[1]:=Arr[1];',
  31231. ' Arr[i]:=Arr[i+1];',
  31232. ' o.a[i]:=o.a[i+2];',
  31233. 'end;',
  31234. 'begin',
  31235. '']);
  31236. ConvertProgram;
  31237. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31238. LinesToStr([ // statements
  31239. 'rtl.recNewT(this, "TRec", function () {',
  31240. ' this.x = 0;',
  31241. ' this.$eq = function (b) {',
  31242. ' return this.x === b.x;',
  31243. ' };',
  31244. ' this.$assign = function (s) {',
  31245. ' this.x = s.x;',
  31246. ' return this;',
  31247. ' };',
  31248. '});',
  31249. 'rtl.createClass(this, "TObject", null, function () {',
  31250. ' this.$init = function () {',
  31251. ' this.A = [];',
  31252. ' };',
  31253. ' this.$final = function () {',
  31254. ' this.A = undefined;',
  31255. ' };',
  31256. '});',
  31257. 'this.DoIt = function () {',
  31258. ' var Arr = [];',
  31259. ' var ArrArr = [];',
  31260. ' var i = 0;',
  31261. ' var o = null;',
  31262. ' Arr[1].$assign(Arr[1]);',
  31263. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31264. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31265. '};',
  31266. '']),
  31267. LinesToStr([ // $mod.$main
  31268. '']));
  31269. end;
  31270. procedure TTestModule.TestRangeChecks_StringIndex;
  31271. begin
  31272. StartProgram(false);
  31273. Add([
  31274. 'type',
  31275. ' TObject = class',
  31276. ' S: string;',
  31277. ' end;',
  31278. '{$R+}',
  31279. 'procedure DoIt(var h: string);',
  31280. 'var',
  31281. ' s: string;',
  31282. ' i: longint;',
  31283. ' c: char;',
  31284. ' o: tobject;',
  31285. 'begin',
  31286. ' c:=s[1];',
  31287. ' s[i]:=s[i];',
  31288. ' h[i]:=h[i];',
  31289. ' c:=o.s[i];',
  31290. ' o.s[i]:=c;',
  31291. 'end;',
  31292. 'begin',
  31293. '']);
  31294. ConvertProgram;
  31295. CheckSource('TestRangeChecks_StringIndex',
  31296. LinesToStr([ // statements
  31297. 'rtl.createClass(this, "TObject", null, function () {',
  31298. ' this.$init = function () {',
  31299. ' this.S = "";',
  31300. ' };',
  31301. ' this.$final = function () {',
  31302. ' };',
  31303. '});',
  31304. 'this.DoIt = function (h) {',
  31305. ' var s = "";',
  31306. ' var i = 0;',
  31307. ' var c = "";',
  31308. ' var o = null;',
  31309. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31310. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31311. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31312. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31313. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31314. '};',
  31315. '']),
  31316. LinesToStr([ // $mod.$main
  31317. '']));
  31318. end;
  31319. procedure TTestModule.TestRangeChecks_TypecastInt;
  31320. begin
  31321. StartProgram(false);
  31322. Add([
  31323. '{$R+}',
  31324. 'var',
  31325. ' i: nativeint;',
  31326. ' b: byte;',
  31327. ' sh: shortint;',
  31328. ' w: word;',
  31329. ' sm: smallint;',
  31330. ' lw: longword;',
  31331. ' li: longint;',
  31332. 'begin',
  31333. ' b:=12+byte(i);',
  31334. ' sh:=12+shortint(i);',
  31335. ' w:=12+word(i);',
  31336. ' sm:=12+smallint(i);',
  31337. ' lw:=12+longword(i);',
  31338. ' li:=12+longint(i);',
  31339. '']);
  31340. ConvertProgram;
  31341. CheckSource('TestRangeChecks_TypecastInt',
  31342. LinesToStr([
  31343. 'this.i = 0;',
  31344. 'this.b = 0;',
  31345. 'this.sh = 0;',
  31346. 'this.w = 0;',
  31347. 'this.sm = 0;',
  31348. 'this.lw = 0;',
  31349. 'this.li = 0;',
  31350. '']),
  31351. LinesToStr([
  31352. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31353. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31354. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31355. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31356. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31357. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31358. '']));
  31359. end;
  31360. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31361. begin
  31362. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31363. StartProgram(false);
  31364. Add([
  31365. '{$modeswitch typehelpers}',
  31366. '{$R+}',
  31367. 'type',
  31368. ' TObject = class',
  31369. ' FSize: byte;',
  31370. ' property Size: byte read FSize;',
  31371. ' end;',
  31372. ' THelper = type helper for byte',
  31373. ' procedure SetIt(w: word);',
  31374. ' end;',
  31375. 'procedure THelper.SetIt(w: word);',
  31376. 'begin',
  31377. ' Self:=w;',
  31378. 'end;',
  31379. 'function GetIt: byte;',
  31380. 'begin',
  31381. ' Result.SetIt(2);',
  31382. 'end;',
  31383. 'var',
  31384. ' b: byte = 3;',
  31385. ' o: TObject;',
  31386. 'begin',
  31387. ' b.SetIt(14);',
  31388. ' with b do SetIt(15);',
  31389. ' o.Size.SetIt(16);',
  31390. '']);
  31391. ConvertProgram;
  31392. CheckSource('TestRangeChecks_AssignInt',
  31393. LinesToStr([ // statements
  31394. 'rtl.createClass(this, "TObject", null, function () {',
  31395. ' this.$init = function () {',
  31396. ' this.FSize = 0;',
  31397. ' };',
  31398. ' this.$final = function () {',
  31399. ' };',
  31400. '});',
  31401. 'rtl.createHelper(this, "THelper", null, function () {',
  31402. ' this.SetIt = function (w) {',
  31403. ' rtl.rc(w, 0, 65535);',
  31404. ' this.set(w);',
  31405. ' };',
  31406. '});',
  31407. 'this.GetIt = function () {',
  31408. ' var Result = 0;',
  31409. ' $mod.THelper.SetIt.call({',
  31410. ' get: function () {',
  31411. ' return Result;',
  31412. ' },',
  31413. ' set: function (v) {',
  31414. ' rtl.rc(v, 0, 255);',
  31415. ' Result = v;',
  31416. ' }',
  31417. ' }, 2);',
  31418. ' return Result;',
  31419. '};',
  31420. 'this.b = 3;',
  31421. 'this.o = null;',
  31422. '']),
  31423. LinesToStr([ // $mod.$main
  31424. '$mod.THelper.SetIt.call({',
  31425. ' p: $mod,',
  31426. ' get: function () {',
  31427. ' return this.p.b;',
  31428. ' },',
  31429. ' set: function (v) {',
  31430. ' rtl.rc(v, 0, 255);',
  31431. ' this.p.b = v;',
  31432. ' }',
  31433. '}, 14);',
  31434. 'var $with = $mod.b;',
  31435. '$mod.THelper.SetIt.call({',
  31436. ' get: function () {',
  31437. ' return $with;',
  31438. ' },',
  31439. ' set: function (v) {',
  31440. ' rtl.rc(v, 0, 255);',
  31441. ' $with = v;',
  31442. ' }',
  31443. '}, 15);',
  31444. '$mod.THelper.SetIt.call({',
  31445. ' p: $mod.o,',
  31446. ' get: function () {',
  31447. ' return this.p.FSize;',
  31448. ' },',
  31449. ' set: function (v) {',
  31450. ' rtl.rc(v, 0, 255);',
  31451. ' this.p.FSize = v;',
  31452. ' }',
  31453. '}, 16);',
  31454. '']));
  31455. end;
  31456. procedure TTestModule.TestAsync_Proc;
  31457. begin
  31458. StartProgram(false);
  31459. Add([
  31460. 'procedure Fly(w: word = 1); async; forward;',
  31461. 'procedure Run(w: word = 2); async;',
  31462. 'begin',
  31463. ' Fly(w);',
  31464. ' Fly;',
  31465. ' await(Fly(w));',
  31466. ' await(Fly);',
  31467. 'end;',
  31468. 'procedure Fly(w: word); ',
  31469. 'begin',
  31470. 'end;',
  31471. 'begin',
  31472. ' Run;',
  31473. ' Run(3);',
  31474. '']);
  31475. CheckResolverUnexpectedHints();
  31476. ConvertProgram;
  31477. CheckSource('TestAsync_Proc',
  31478. LinesToStr([ // statements
  31479. 'this.Run = async function (w) {',
  31480. ' $mod.Fly(w);',
  31481. ' $mod.Fly(1);',
  31482. ' await $mod.Fly(w);',
  31483. ' await $mod.Fly(1);',
  31484. '};',
  31485. 'this.Fly = async function (w) {',
  31486. '};',
  31487. '']),
  31488. LinesToStr([
  31489. '$mod.Run(2);',
  31490. '$mod.Run(3);',
  31491. '']));
  31492. end;
  31493. procedure TTestModule.TestAsync_CallResultIsPromise;
  31494. begin
  31495. StartProgram(false);
  31496. Add([
  31497. '{$modeswitch externalclass}',
  31498. 'type',
  31499. ' TObject = class',
  31500. ' end;',
  31501. ' TJSPromise = class external name ''Promise''',
  31502. ' end;',
  31503. ' TBird = class',
  31504. ' function Fly: word; async; ',
  31505. ' end;',
  31506. 'function TBird.Fly: word; async; ',
  31507. 'begin',
  31508. ' Result:=3;',
  31509. ' Fly:=4+Result;',
  31510. ' if Result=5 then ;',
  31511. ' exit(6);',
  31512. 'end;',
  31513. 'function Run: word; async;',
  31514. 'begin',
  31515. ' Result:=11+Result;',
  31516. ' inc(Result);',
  31517. 'end;',
  31518. 'var',
  31519. ' p: TJSPromise;',
  31520. ' o: TBird;',
  31521. 'begin',
  31522. ' p:=Run;',
  31523. ' p:=Run();',
  31524. ' if Run=p then ;',
  31525. ' if p=Run then ;',
  31526. ' if Run()=p then ;',
  31527. ' if p=Run() then ;',
  31528. ' p:=o.Fly;',
  31529. ' p:=o.Fly();',
  31530. ' if o.Fly=p then ;',
  31531. ' if o.Fly()=p then ;',
  31532. ' with o do begin',
  31533. ' p:=Fly;',
  31534. ' p:=Fly();',
  31535. ' if Fly=p then ;',
  31536. ' if Fly()=p then ;',
  31537. ' end;',
  31538. '']);
  31539. CheckResolverUnexpectedHints();
  31540. ConvertProgram;
  31541. CheckSource('TestAsync_CallResultIsPromise',
  31542. LinesToStr([ // statements
  31543. 'rtl.createClass(this, "TObject", null, function () {',
  31544. ' this.$init = function () {',
  31545. ' };',
  31546. ' this.$final = function () {',
  31547. ' };',
  31548. '});',
  31549. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31550. ' this.Fly = async function () {',
  31551. ' var Result = 0;',
  31552. ' Result = 3;',
  31553. ' Result = 4 + Result;',
  31554. ' if (Result === 5) ;',
  31555. ' return 6;',
  31556. ' return Result;',
  31557. ' };',
  31558. '});',
  31559. 'this.Run = async function () {',
  31560. ' var Result = 0;',
  31561. ' Result = 11 + Result;',
  31562. ' Result += 1;',
  31563. ' return Result;',
  31564. '};',
  31565. 'this.p = null;',
  31566. 'this.o = null;',
  31567. '']),
  31568. LinesToStr([
  31569. '$mod.p = $mod.Run();',
  31570. '$mod.p = $mod.Run();',
  31571. 'if ($mod.Run() === $mod.p) ;',
  31572. 'if ($mod.p === $mod.Run()) ;',
  31573. 'if ($mod.Run() === $mod.p) ;',
  31574. 'if ($mod.p === $mod.Run()) ;',
  31575. '$mod.p = $mod.o.Fly();',
  31576. '$mod.p = $mod.o.Fly();',
  31577. 'if ($mod.o.Fly() === $mod.p) ;',
  31578. 'if ($mod.o.Fly() === $mod.p) ;',
  31579. 'var $with = $mod.o;',
  31580. '$mod.p = $with.Fly();',
  31581. '$mod.p = $with.Fly();',
  31582. 'if ($with.Fly() === $mod.p) ;',
  31583. 'if ($with.Fly() === $mod.p) ;',
  31584. '']));
  31585. end;
  31586. procedure TTestModule.TestAsync_ConstructorFail;
  31587. begin
  31588. StartProgram(false);
  31589. Add([
  31590. 'type',
  31591. ' TObject = class',
  31592. ' end;',
  31593. ' TBird = class',
  31594. ' constructor Create; async;',
  31595. ' end;',
  31596. 'constructor TBird.Create; async;',
  31597. 'begin',
  31598. 'end;',
  31599. 'begin',
  31600. '']);
  31601. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31602. ConvertProgram;
  31603. end;
  31604. procedure TTestModule.TestAsync_PropertyGetterFail;
  31605. begin
  31606. StartProgram(false);
  31607. Add([
  31608. 'type',
  31609. ' TObject = class',
  31610. ' end;',
  31611. ' TBird = class',
  31612. ' function GetSize: word; async;',
  31613. ' property Size: word read GetSize;',
  31614. ' end;',
  31615. 'function TBird.GetSize: word; async;',
  31616. 'begin',
  31617. 'end;',
  31618. 'begin',
  31619. '']);
  31620. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31621. ConvertProgram;
  31622. end;
  31623. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31624. begin
  31625. StartProgram(false);
  31626. Add([
  31627. 'procedure Run; async;',
  31628. 'begin',
  31629. ' await(word,1);',
  31630. 'end;',
  31631. 'begin',
  31632. '']);
  31633. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31634. ConvertProgram;
  31635. end;
  31636. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  31637. begin
  31638. StartProgram(false);
  31639. Add([
  31640. 'type',
  31641. ' TObject = class',
  31642. ' end;',
  31643. ' TBird = class',
  31644. ' end;',
  31645. 'function Fly: TObject; async;',
  31646. 'begin',
  31647. 'end;',
  31648. 'procedure Run; async;',
  31649. 'begin',
  31650. ' await(TBird,Fly);',
  31651. 'end;',
  31652. 'begin',
  31653. '']);
  31654. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  31655. ConvertProgram;
  31656. end;
  31657. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31658. begin
  31659. StartProgram(false);
  31660. Add([
  31661. 'procedure Crawl(w: double); ',
  31662. 'begin',
  31663. 'end;',
  31664. 'procedure Run(w: double);',
  31665. 'begin',
  31666. ' await(Crawl(w));',
  31667. 'end;',
  31668. 'begin',
  31669. ' Run(1);']);
  31670. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  31671. ConvertProgram;
  31672. end;
  31673. procedure TTestModule.TestAWait_Result;
  31674. begin
  31675. StartProgram(false);
  31676. Add([
  31677. '{$modeswitch externalclass}',
  31678. 'type',
  31679. ' TJSPromise = class external name ''Promise''',
  31680. ' end;',
  31681. 'function Crawl(d: double = 1.3): word; ',
  31682. 'begin',
  31683. 'end;',
  31684. 'function Run(d: double = 1.6): word; async;',
  31685. 'begin',
  31686. ' Result:=await(1);',
  31687. ' Result:=await(Crawl);',
  31688. ' Result:=await(Crawl(4.5));',
  31689. ' Result:=await(Run);',
  31690. ' Result:=await(Run(6.7));',
  31691. 'end;',
  31692. 'begin',
  31693. ' Run(1);']);
  31694. ConvertProgram;
  31695. CheckSource('TestAWait_Result',
  31696. LinesToStr([ // statements
  31697. 'this.Crawl = function (d) {',
  31698. ' var Result = 0;',
  31699. ' return Result;',
  31700. '};',
  31701. 'this.Run = async function (d) {',
  31702. ' var Result = 0;',
  31703. ' Result = await 1;',
  31704. ' Result = await $mod.Crawl(1.3);',
  31705. ' Result = await $mod.Crawl(4.5);',
  31706. ' Result = await $mod.Run(1.6);',
  31707. ' Result = await $mod.Run(6.7);',
  31708. ' return Result;',
  31709. '};',
  31710. '']),
  31711. LinesToStr([
  31712. '$mod.Run(1);'
  31713. ]));
  31714. SetExpectedPasResolverError('Await without promise',nAwaitWithoutPromise);
  31715. end;
  31716. procedure TTestModule.TestAWait_ExternalClassPromise;
  31717. begin
  31718. StartProgram(false);
  31719. Add([
  31720. '{$modeswitch externalclass}',
  31721. 'type',
  31722. ' TJSPromise = class external name ''Promise''',
  31723. ' end;',
  31724. 'function Fly(w: word): TJSPromise;',
  31725. 'begin',
  31726. 'end;',
  31727. 'function Jump(w: word): word; async;',
  31728. 'begin',
  31729. 'end;',
  31730. 'function Eat(w: word): TJSPromise; async;',
  31731. 'begin',
  31732. 'end;',
  31733. 'function Run(d: double): word; async;',
  31734. 'var',
  31735. ' p: TJSPromise;',
  31736. 'begin',
  31737. ' Result:=await(word,p);', // promise needs type
  31738. ' Result:=await(word,Fly(3));', // promise needs type
  31739. ' Result:=await(Jump(4));', // async non promise must omit the type
  31740. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  31741. ' Result:=await(word,Eat(6));', // promise needs type
  31742. 'end;',
  31743. 'begin',
  31744. '']);
  31745. ConvertProgram;
  31746. CheckSource('TestAWait_ExternalClassPromise',
  31747. LinesToStr([ // statements
  31748. 'this.Fly = function (w) {',
  31749. ' var Result = null;',
  31750. ' return Result;',
  31751. '};',
  31752. 'this.Jump = async function (w) {',
  31753. ' var Result = 0;',
  31754. ' return Result;',
  31755. '};',
  31756. 'this.Eat = async function (w) {',
  31757. ' var Result = null;',
  31758. ' return Result;',
  31759. '};',
  31760. 'this.Run = async function (d) {',
  31761. ' var Result = 0;',
  31762. ' var p = null;',
  31763. ' Result = await p;',
  31764. ' Result = await $mod.Fly(3);',
  31765. ' Result = await $mod.Jump(4);',
  31766. ' Result = await $mod.Jump(5);',
  31767. ' Result = await $mod.Eat(6);',
  31768. ' return Result;',
  31769. '};',
  31770. '']),
  31771. LinesToStr([
  31772. ]));
  31773. CheckResolverUnexpectedHints();
  31774. end;
  31775. procedure TTestModule.TestAsync_AnonymousProc;
  31776. begin
  31777. StartProgram(false);
  31778. Add([
  31779. '{$mode objfpc}',
  31780. '{$modeswitch externalclass}',
  31781. 'type',
  31782. ' TJSPromise = class external name ''Promise''',
  31783. ' end;',
  31784. 'type',
  31785. ' TFunc = reference to function(x: double): word; async;',
  31786. 'function Crawl(d: double = 1.3): word; async;',
  31787. 'begin',
  31788. 'end;',
  31789. 'var Func: TFunc;',
  31790. 'begin',
  31791. ' Func:=function(c:double):word async begin',
  31792. ' Result:=await(Crawl(c));',
  31793. ' end;',
  31794. ' Func:=function(c:double):word async assembler asm',
  31795. ' end;',
  31796. ' ']);
  31797. ConvertProgram;
  31798. CheckSource('TestAsync_AnonymousProc',
  31799. LinesToStr([ // statements
  31800. 'this.Crawl = async function (d) {',
  31801. ' var Result = 0;',
  31802. ' return Result;',
  31803. '};',
  31804. 'this.Func = null;',
  31805. '']),
  31806. LinesToStr([
  31807. '$mod.Func = async function (c) {',
  31808. ' var Result = 0;',
  31809. ' Result = await $mod.Crawl(c);',
  31810. ' return Result;',
  31811. '};',
  31812. '$mod.Func = async function (c) {',
  31813. '};',
  31814. '']));
  31815. CheckResolverUnexpectedHints();
  31816. end;
  31817. procedure TTestModule.TestAsync_ProcType;
  31818. begin
  31819. StartProgram(false);
  31820. Add([
  31821. '{$mode objfpc}',
  31822. 'type',
  31823. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  31824. ' TFunc = function(x: double = 1.1): word; async;',
  31825. ' TProc = procedure(x: longint = 7); async;',
  31826. 'function Crawl(d: double): word; async;',
  31827. 'begin',
  31828. 'end;',
  31829. 'procedure Run(e:longint); async;',
  31830. 'begin',
  31831. 'end;',
  31832. 'procedure Fly(p: TProc); async;',
  31833. 'begin',
  31834. ' await(p);',
  31835. ' await(p());',
  31836. 'end;',
  31837. 'var',
  31838. ' RefFunc: TRefFunc;',
  31839. ' Func: TFunc;',
  31840. ' Proc, ProcB: TProc;',
  31841. 'begin',
  31842. ' Func:=@Crawl;',
  31843. ' RefFunc:=@Crawl;',
  31844. ' RefFunc:=function(c:double):word async begin',
  31845. ' Result:=await(RefFunc);',
  31846. ' Result:=await(RefFunc());',
  31847. ' Result:=await(Func);',
  31848. ' Result:=await(Func());',
  31849. ' await(Proc);',
  31850. ' await(Proc());',
  31851. ' await(Proc(13));',
  31852. ' end;',
  31853. ' Proc:=@Run;',
  31854. ' if Proc=ProcB then ;',
  31855. ' ']);
  31856. ConvertProgram;
  31857. CheckResolverUnexpectedHints();
  31858. CheckSource('TestAsync_ProcType',
  31859. LinesToStr([ // statements
  31860. 'this.Crawl = async function (d) {',
  31861. ' var Result = 0;',
  31862. ' return Result;',
  31863. '};',
  31864. 'this.Run = async function (e) {',
  31865. '};',
  31866. 'this.Fly = async function (p) {',
  31867. ' await p(7);',
  31868. ' await p(7);',
  31869. '};',
  31870. 'this.RefFunc = null;',
  31871. 'this.Func = null;',
  31872. 'this.Proc = null;',
  31873. 'this.ProcB = null;',
  31874. '']),
  31875. LinesToStr([
  31876. '$mod.Func = $mod.Crawl;',
  31877. '$mod.RefFunc = $mod.Crawl;',
  31878. '$mod.RefFunc = async function (c) {',
  31879. ' var Result = 0;',
  31880. ' Result = await $mod.RefFunc(1.3);',
  31881. ' Result = await $mod.RefFunc(1.3);',
  31882. ' Result = await $mod.Func(1.1);',
  31883. ' Result = await $mod.Func(1.1);',
  31884. ' await $mod.Proc(7);',
  31885. ' await $mod.Proc(7);',
  31886. ' await $mod.Proc(13);',
  31887. ' return Result;',
  31888. '};',
  31889. '$mod.Proc = $mod.Run;',
  31890. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  31891. '']));
  31892. end;
  31893. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  31894. begin
  31895. StartProgram(false);
  31896. Add([
  31897. '{$mode objfpc}',
  31898. 'type',
  31899. ' TRefFunc = reference to function(x: double = 1.3): word;',
  31900. 'function Crawl(d: double): word; async;',
  31901. 'begin',
  31902. 'end;',
  31903. 'var',
  31904. ' RefFunc: TRefFunc;',
  31905. 'begin',
  31906. ' RefFunc:=@Crawl;',
  31907. ' ']);
  31908. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  31909. ConvertProgram;
  31910. end;
  31911. procedure TTestModule.TestAsync_Inherited;
  31912. begin
  31913. StartProgram(false);
  31914. Add([
  31915. '{$mode objfpc}',
  31916. '{$modeswitch externalclass}',
  31917. 'type',
  31918. ' TJSPromise = class external name ''Promise''',
  31919. ' end;',
  31920. ' TObject = class',
  31921. ' function Run(w: word = 3): word; async; virtual;',
  31922. ' end;',
  31923. ' TBird = class',
  31924. ' function Run(w: word = 3): word; async; override;',
  31925. ' end;',
  31926. 'function TObject.Run(w: word = 3): word; async;',
  31927. 'begin',
  31928. 'end;',
  31929. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  31930. 'var p: TJSPromise;',
  31931. 'begin',
  31932. ' p:=inherited;',
  31933. ' p:=inherited Run;',
  31934. ' p:=inherited Run();',
  31935. ' p:=inherited Run(4);',
  31936. ' exit(p);',
  31937. ' exit(inherited);',
  31938. ' exit(inherited Run);',
  31939. ' exit(inherited Run(5));',
  31940. ' exit(6);',
  31941. 'end;',
  31942. 'begin',
  31943. ' ']);
  31944. ConvertProgram;
  31945. CheckSource('TestAsync_Inherited',
  31946. LinesToStr([ // statements
  31947. 'rtl.createClass(this, "TObject", null, function () {',
  31948. ' this.$init = function () {',
  31949. ' };',
  31950. ' this.$final = function () {',
  31951. ' };',
  31952. ' this.Run = async function (w) {',
  31953. ' var Result = 0;',
  31954. ' return Result;',
  31955. ' };',
  31956. '});',
  31957. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31958. ' this.Run = async function (w) {',
  31959. ' var Result = 0;',
  31960. ' var p = null;',
  31961. ' p = $mod.TObject.Run.apply(this, arguments);',
  31962. ' p = $mod.TObject.Run.call(this, 3);',
  31963. ' p = $mod.TObject.Run.call(this, 3);',
  31964. ' p = $mod.TObject.Run.call(this, 4);',
  31965. ' return p;',
  31966. ' return $mod.TObject.Run.apply(this, arguments);',
  31967. ' return $mod.TObject.Run.call(this, 3);',
  31968. ' return $mod.TObject.Run.call(this, 5);',
  31969. ' return 6;',
  31970. ' return Result;',
  31971. ' };',
  31972. '});',
  31973. '']),
  31974. LinesToStr([
  31975. '']));
  31976. CheckResolverUnexpectedHints();
  31977. end;
  31978. procedure TTestModule.TestAsync_ClassInterface;
  31979. begin
  31980. StartProgram(false);
  31981. Add([
  31982. '{$mode objfpc}',
  31983. '{$modeswitch externalclass}',
  31984. 'type',
  31985. ' TJSPromise = class external name ''Promise''',
  31986. ' end;',
  31987. ' IUnknown = interface',
  31988. ' function _AddRef: longint;',
  31989. ' function _Release: longint;',
  31990. ' end;',
  31991. 'function Run: IUnknown; async;',
  31992. 'begin',
  31993. 'end;',
  31994. 'procedure Fly;',
  31995. 'var p: TJSPromise;',
  31996. 'begin',
  31997. ' Run;',
  31998. ' Run();',
  31999. ' p:=Run;',
  32000. ' p:=Run();',
  32001. 'end;',
  32002. 'begin',
  32003. ' ']);
  32004. ConvertProgram;
  32005. CheckSource('TestAsync_ClassInterface',
  32006. LinesToStr([ // statements
  32007. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32008. 'this.Run = async function () {',
  32009. ' var Result = null;',
  32010. ' return Result;',
  32011. '};',
  32012. 'this.Fly = function () {',
  32013. ' var p = null;',
  32014. ' $mod.Run();',
  32015. ' $mod.Run();',
  32016. ' p = $mod.Run();',
  32017. ' p = $mod.Run();',
  32018. '};',
  32019. '']),
  32020. LinesToStr([
  32021. '']));
  32022. CheckResolverUnexpectedHints();
  32023. end;
  32024. Initialization
  32025. RegisterTests([TTestModule]);
  32026. end.