tcmodules.pas 945 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856
  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, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. JSONNewLine = {$IFDEF Windows}'\r\n'{$ELSE}'\n'{$ENDIF};
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const Filename: string): integer;
  209. function GetResolver(const Filename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestDoubleSmall;
  284. Procedure TestInteger;
  285. Procedure TestIntegerRange;
  286. Procedure TestIntegerTypecasts;
  287. Procedure TestInteger_BitwiseShrNativeInt;
  288. Procedure TestInteger_BitwiseShlNativeInt;
  289. Procedure TestInteger_SystemFunc;
  290. Procedure TestInteger_AssignOutsideConst;
  291. Procedure TestCurrency;
  292. Procedure TestForBoolDo;
  293. Procedure TestForIntDo;
  294. Procedure TestForIntInDo;
  295. // strings
  296. Procedure TestCharConst;
  297. Procedure TestChar_Compare;
  298. Procedure TestChar_BuiltInProcs;
  299. Procedure TestStringConst;
  300. Procedure TestStringConst_InvalidUTF16;
  301. Procedure TestStringConstSurrogate;
  302. Procedure TestStringConst_Multiline;
  303. Procedure TestString_Length;
  304. Procedure TestString_Compare;
  305. Procedure TestString_SetLength;
  306. Procedure TestString_CharAt;
  307. Procedure TestStringHMinusFail;
  308. Procedure TestStr;
  309. Procedure TestBaseType_AnsiStringFail;
  310. Procedure TestBaseType_WideStringFail;
  311. Procedure TestBaseType_ShortStringFail;
  312. Procedure TestBaseType_RawByteStringFail;
  313. Procedure TestTypeShortstring_Fail;
  314. Procedure TestCharSet_Custom;
  315. Procedure TestWideChar;
  316. Procedure TestForCharDo;
  317. Procedure TestForCharInDo;
  318. // alias types
  319. Procedure TestAliasTypeRef;
  320. Procedure TestTypeCast_BaseTypes;
  321. Procedure TestTypeCast_AliasBaseTypes;
  322. // functions
  323. Procedure TestEmptyProc;
  324. Procedure TestProcOneParam;
  325. Procedure TestFunctionWithoutParams;
  326. Procedure TestProcedureWithoutParams;
  327. Procedure TestPrgProcVar;
  328. Procedure TestProcTwoArgs;
  329. Procedure TestProc_DefaultValue;
  330. Procedure TestUnitProcVar;
  331. Procedure TestImplProc;
  332. Procedure TestFunctionResult;
  333. Procedure TestNestedProc;
  334. Procedure TestNestedProc_ResultString;
  335. Procedure TestForwardProc;
  336. Procedure TestNestedForwardProc;
  337. Procedure TestAssignFunctionResult;
  338. Procedure TestFunctionResultInCondition;
  339. Procedure TestFunctionResultInForLoop;
  340. Procedure TestFunctionResultInTypeCast;
  341. Procedure TestExit;
  342. Procedure TestExit_ResultInFinally;
  343. Procedure TestBreak;
  344. Procedure TestBreakAsVar;
  345. Procedure TestContinue;
  346. Procedure TestProc_External;
  347. Procedure TestProc_ExternalOtherUnit;
  348. Procedure TestProc_Asm;
  349. Procedure TestProc_AsmSubBlock;
  350. Procedure TestProc_Assembler;
  351. Procedure TestProc_VarParam;
  352. Procedure TestProc_VarParamString;
  353. Procedure TestProc_VarParamV;
  354. Procedure TestProc_Overload;
  355. Procedure TestProc_OverloadForward;
  356. Procedure TestProc_OverloadIntfImpl;
  357. Procedure TestProc_OverloadNested;
  358. Procedure TestProc_OverloadNestedForward;
  359. Procedure TestProc_OverloadUnitCycle;
  360. Procedure TestProc_Varargs;
  361. Procedure TestProc_ConstOrder;
  362. Procedure TestProc_DuplicateConst;
  363. Procedure TestProc_LocalVarAbsolute;
  364. Procedure TestProc_ResultAbsolute;
  365. Procedure TestProc_LocalVarInit;
  366. Procedure TestProc_ReservedWords;
  367. Procedure TestProc_ConstRefWord;
  368. // anonymous functions
  369. Procedure TestAnonymousProc_Assign_ObjFPC;
  370. Procedure TestAnonymousProc_Assign_Delphi;
  371. Procedure TestAnonymousProc_Arg;
  372. Procedure TestAnonymousProc_Typecast;
  373. Procedure TestAnonymousProc_With;
  374. Procedure TestAnonymousProc_ExceptOn;
  375. Procedure TestAnonymousProc_Nested;
  376. Procedure TestAnonymousProc_NestedAssignResult;
  377. Procedure TestAnonymousProc_Class;
  378. Procedure TestAnonymousProc_ForLoop;
  379. Procedure TestAnonymousProc_AsmDelphi;
  380. // enums, sets
  381. Procedure TestEnum_Name;
  382. Procedure TestEnum_Number;
  383. Procedure TestEnum_ConstFail;
  384. Procedure TestEnum_Functions;
  385. Procedure TestEnumRg_Functions;
  386. Procedure TestEnum_AsParams;
  387. Procedure TestEnumRange_Array;
  388. Procedure TestEnum_ForIn;
  389. Procedure TestEnum_ScopedNumber;
  390. Procedure TestEnum_InFunction;
  391. Procedure TestEnum_Name_Anonymous_Unit;
  392. Procedure TestSet_Enum;
  393. Procedure TestSet_Operators;
  394. Procedure TestSet_Operator_In;
  395. Procedure TestSet_Functions;
  396. Procedure TestSet_PassAsArgClone;
  397. Procedure TestSet_AsParams;
  398. Procedure TestSet_Property;
  399. Procedure TestSet_EnumConst;
  400. Procedure TestSet_IntConst;
  401. Procedure TestSet_IntRange;
  402. Procedure TestSet_AnonymousEnumType;
  403. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  404. Procedure TestSet_ConstEnum;
  405. Procedure TestSet_ConstChar;
  406. Procedure TestSet_ConstInt;
  407. Procedure TestSet_InFunction;
  408. Procedure TestSet_ForIn;
  409. // statements
  410. Procedure TestNestBegin;
  411. Procedure TestIncDec;
  412. Procedure TestLoHiFpcMode;
  413. Procedure TestLoHiDelphiMode;
  414. Procedure TestAssignments;
  415. Procedure TestArithmeticOperators1;
  416. Procedure TestMultiAdd;
  417. Procedure TestLogicalOperators;
  418. Procedure TestBitwiseOperators;
  419. Procedure TestBitwiseOperatorsLongword;
  420. Procedure TestFunctionInt;
  421. Procedure TestFunctionString;
  422. Procedure TestIfThen;
  423. Procedure TestForLoop;
  424. Procedure TestForLoopInsideFunction;
  425. Procedure TestForLoop_ReadVarAfter;
  426. Procedure TestForLoop_Nested;
  427. Procedure TestRepeatUntil;
  428. Procedure TestAsmBlock;
  429. Procedure TestAsmPas_Impl; // ToDo
  430. Procedure TestTryFinally;
  431. Procedure TestTryExcept;
  432. Procedure TestTryExcept_ReservedWords;
  433. Procedure TestIfThenRaiseElse;
  434. Procedure TestCaseOf;
  435. Procedure TestCaseOf_UseSwitch;
  436. Procedure TestCaseOfNoElse;
  437. Procedure TestCaseOfNoElse_UseSwitch;
  438. Procedure TestCaseOfRange;
  439. Procedure TestCaseOfString;
  440. Procedure TestCaseOfChar;
  441. Procedure TestCaseOfExternalClassConst;
  442. Procedure TestDebugger;
  443. // arrays
  444. Procedure TestArray_Dynamic;
  445. Procedure TestArray_Dynamic_Nil;
  446. Procedure TestArray_DynMultiDimensional;
  447. Procedure TestArray_DynamicAssign;
  448. Procedure TestArray_StaticInt;
  449. Procedure TestArray_StaticBool;
  450. Procedure TestArray_StaticChar;
  451. Procedure TestArray_StaticMultiDim;
  452. Procedure TestArray_StaticInFunction;
  453. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  454. Procedure TestArrayOfRecord;
  455. Procedure TestArray_StaticRecord;
  456. Procedure TestArrayOfSet;
  457. Procedure TestArray_DynAsParam;
  458. Procedure TestArray_StaticAsParam;
  459. Procedure TestArrayElement_AsParams;
  460. Procedure TestArrayElementFromFuncResult_AsParams;
  461. Procedure TestArrayEnumTypeRange;
  462. Procedure TestArray_SetLengthOutArg;
  463. Procedure TestArray_SetLengthProperty;
  464. Procedure TestArray_SetLengthMultiDim;
  465. Procedure TestArray_SetLengthDynOfStatic;
  466. Procedure TestArray_OpenArrayOfString;
  467. Procedure TestArray_ArrayOfCharAssignString;
  468. Procedure TestArray_ConstRef;
  469. Procedure TestArray_Concat;
  470. Procedure TestArray_Concat_Append;
  471. Procedure TestArray_Concat_Append_Var;
  472. Procedure TestArray_Copy;
  473. Procedure TestArray_InsertDelete;
  474. Procedure TestArray_Add_Append;
  475. Procedure TestArray_DynArrayConstObjFPC;
  476. Procedure TestArray_DynArrayConstDelphi;
  477. Procedure TestArray_ArrayLitAsParam;
  478. Procedure TestArray_ArrayLitMultiDimAsParam;
  479. Procedure TestArray_ArrayLitStaticAsParam;
  480. Procedure TestArray_ForInArrOfString;
  481. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  482. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  483. Procedure TestArrayOfConst_TVarRec;
  484. Procedure TestArrayOfConst_PassBaseTypes;
  485. Procedure TestArrayOfConst_PassObj;
  486. // record
  487. Procedure TestRecord_Empty;
  488. Procedure TestRecord_Var;
  489. Procedure TestRecord_VarExternal;
  490. Procedure TestRecord_WithDo;
  491. Procedure TestRecord_Assign;
  492. Procedure TestRecord_AsParams;
  493. Procedure TestRecord_ConstRef;
  494. Procedure TestRecordElement_AsParams;
  495. Procedure TestRecordElementFromFuncResult_AsParams;
  496. Procedure TestRecordElementFromWith_AsParams;
  497. Procedure TestRecord_Equal;
  498. Procedure TestRecord_JSValue;
  499. Procedure TestRecord_VariantFail;
  500. Procedure TestRecord_FieldArray;
  501. Procedure TestRecord_Const;
  502. Procedure TestRecord_TypecastFail;
  503. Procedure TestRecord_InFunction;
  504. // anonymous record
  505. Procedure TestRecordAnonym_Field;
  506. Procedure TestRecordAnonym_Assign;
  507. Procedure TestRecordAnonym_Nested;
  508. Procedure TestRecordAnonym_Const;
  509. Procedure TestRecordAnonym_InFunction;
  510. // advanced record
  511. Procedure TestAdvRecord_Function;
  512. Procedure TestAdvRecord_Property;
  513. Procedure TestAdvRecord_PropertyDefault;
  514. Procedure TestAdvRecord_Property_ClassMethod;
  515. Procedure TestAdvRecord_Const;
  516. Procedure TestAdvRecord_ExternalField;
  517. Procedure TestAdvRecord_SubRecord;
  518. Procedure TestAdvRecord_SubClass;
  519. Procedure TestAdvRecord_SubInterfaceFail;
  520. Procedure TestAdvRecord_Constructor;
  521. Procedure TestAdvRecord_ClassConstructor_Program;
  522. Procedure TestAdvRecord_ClassConstructor_Unit;
  523. // classes
  524. Procedure TestClass_TObjectDefaultConstructor;
  525. Procedure TestClass_TObjectConstructorWithParams;
  526. Procedure TestClass_TObjectConstructorWithDefaultParam;
  527. Procedure TestClass_Var;
  528. Procedure TestClass_Method;
  529. Procedure TestClass_Implementation;
  530. Procedure TestClass_Inheritance;
  531. Procedure TestClass_TypeAlias;
  532. Procedure TestClass_AbstractMethod;
  533. Procedure TestClass_CallInherited_ProcNoParams;
  534. Procedure TestClass_CallInherited_WithParams;
  535. Procedure TestClasS_CallInheritedConstructor;
  536. Procedure TestClass_ClassVar_Assign;
  537. Procedure TestClass_CallClassMethod;
  538. Procedure TestClass_CallClassMethodStatic;
  539. Procedure TestClass_Property;
  540. Procedure TestClass_Property_ClassMethod;
  541. Procedure TestClass_Property_ClassMethodStatic;
  542. Procedure TestClass_Property_Indexed;
  543. Procedure TestClass_Property_IndexSpec;
  544. Procedure TestClass_PropertyOfTypeArray;
  545. Procedure TestClass_PropertyDefault;
  546. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  547. //Procedure TestClass_PropertyDefault;
  548. Procedure TestClass_PropertyOverride;
  549. Procedure TestClass_PropertyIncVisibility;
  550. Procedure TestClass_Assigned;
  551. Procedure TestClass_WithClassDoCreate;
  552. Procedure TestClass_WithClassInstDoProperty;
  553. Procedure TestClass_WithClassInstDoPropertyWithParams;
  554. Procedure TestClass_WithClassInstDoFunc;
  555. Procedure TestClass_TypeCast;
  556. Procedure TestClass_TypeCastUntypedParam;
  557. Procedure TestClass_Overloads;
  558. Procedure TestClass_OverloadsAncestor;
  559. Procedure TestClass_OverloadConstructor;
  560. Procedure TestClass_OverloadDelphiOverride;
  561. Procedure TestClass_ReintroduceVarDelphi;
  562. Procedure TestClass_ReintroducedVar;
  563. Procedure TestClass_RaiseDescendant;
  564. Procedure TestClass_ExternalMethod;
  565. Procedure TestClass_ExternalVirtualNameMismatchFail;
  566. Procedure TestClass_ExternalOverrideFail;
  567. Procedure TestClass_ExternalVar;
  568. Procedure TestClass_Const;
  569. Procedure TestClass_ConstEnum;
  570. Procedure TestClass_LocalConstDuplicate_Prg;
  571. Procedure TestClass_LocalConstDuplicate_Unit;
  572. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  573. Procedure TestClass_LocalVarSelfFail;
  574. Procedure TestClass_ArgSelfFail;
  575. Procedure TestClass_NestedProcSelf;
  576. Procedure TestClass_NestedProcSelf2;
  577. Procedure TestClass_NestedProcClassSelf;
  578. Procedure TestClass_NestedProcCallInherited;
  579. Procedure TestClass_TObjectFree;
  580. Procedure TestClass_TObjectFree_VarArg;
  581. Procedure TestClass_TObjectFreeNewInstance;
  582. Procedure TestClass_TObjectFreeLowerCase;
  583. Procedure TestClass_TObjectFreeFunctionFail;
  584. Procedure TestClass_TObjectFreePropertyFail;
  585. Procedure TestClass_ForIn;
  586. Procedure TestClass_DispatchMessage;
  587. Procedure TestClass_Message_DuplicateIntFail;
  588. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  589. // class of
  590. Procedure TestClassOf_Create;
  591. Procedure TestClassOf_Call;
  592. Procedure TestClassOf_Assign;
  593. Procedure TestClassOf_Is;
  594. Procedure TestClassOf_Compare;
  595. Procedure TestClassOf_ClassVar;
  596. Procedure TestClassOf_ClassMethod;
  597. Procedure TestClassOf_ClassProperty;
  598. Procedure TestClassOf_ClassMethodSelf;
  599. Procedure TestClassOf_TypeCast;
  600. Procedure TestClassOf_ImplicitFunctionCall;
  601. Procedure TestClassOf_Const;
  602. // nested class
  603. Procedure TestNestedClass_Alias;
  604. Procedure TestNestedClass_Record;
  605. Procedure TestNestedClass_Class;
  606. Procedure TestNestedClass_CallInherited;
  607. // external class
  608. Procedure TestExternalClass_Var;
  609. Procedure TestExternalClass_Const;
  610. Procedure TestExternalClass_Dollar;
  611. Procedure TestExternalClass_DuplicateVarFail;
  612. Procedure TestExternalClass_Method;
  613. Procedure TestExternalClass_ClassMethod;
  614. Procedure TestExternalClass_ClassMethodStatic;
  615. Procedure TestExternalClass_FunctionResultInTypeCast;
  616. Procedure TestExternalClass_NonExternalOverride;
  617. Procedure TestExternalClass_OverloadHint;
  618. Procedure TestExternalClass_SameNamePublishedProperty;
  619. Procedure TestExternalClass_Property;
  620. Procedure TestExternalClass_PropertyDate;
  621. Procedure TestExternalClass_ClassProperty;
  622. Procedure TestExternalClass_ClassOf;
  623. Procedure TestExternalClass_ClassOtherUnit;
  624. Procedure TestExternalClass_Is;
  625. Procedure TestExternalClass_As;
  626. Procedure TestExternalClass_DestructorFail;
  627. Procedure TestExternalClass_New;
  628. Procedure TestExternalClass_ClassOf_New;
  629. Procedure TestExternalClass_FuncClassOf_New;
  630. Procedure TestExternalClass_New_PasClassFail;
  631. Procedure TestExternalClass_New_PasClassBracketsFail;
  632. Procedure TestExternalClass_NewExtName;
  633. Procedure TestExternalClass_Constructor;
  634. Procedure TestExternalClass_ConstructorBrackets;
  635. Procedure TestExternalClass_LocalConstSameName;
  636. Procedure TestExternalClass_ReintroduceOverload;
  637. Procedure TestExternalClass_Inherited;
  638. Procedure TestExternalClass_PascalAncestorFail;
  639. Procedure TestExternalClass_NewInstance;
  640. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  641. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  642. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  643. Procedure TestExternalClass_JSFunctionPasDescendant;
  644. Procedure TestExternalClass_PascalProperty;
  645. Procedure TestExternalClass_TypeCastToRootClass;
  646. Procedure TestExternalClass_TypeCastToJSObject;
  647. Procedure TestExternalClass_TypeCastStringToExternalString;
  648. Procedure TestExternalClass_TypeCastToJSFunction;
  649. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  650. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  651. Procedure TestExternalClass_BracketAccessor;
  652. Procedure TestExternalClass_BracketAccessor_Call;
  653. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  654. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  655. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  656. Procedure TestExternalClass_BracketAccessor_MultiType;
  657. Procedure TestExternalClass_BracketAccessor_Index;
  658. Procedure TestExternalClass_ForInJSObject;
  659. Procedure TestExternalClass_ForInJSArray;
  660. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  661. Procedure TestExternalClass_NestedConstructor;
  662. // class interfaces
  663. Procedure TestClassInterface_Corba;
  664. Procedure TestClassInterface_Corba_ProcExternalFail;
  665. Procedure TestClassInterface_Corba_Overloads;
  666. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  667. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  668. Procedure TestClassInterface_Corba_AncestorImpl;
  669. Procedure TestClassInterface_Corba_ImplReintroduce;
  670. Procedure TestClassInterface_Corba_MethodResolution;
  671. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  672. Procedure TestClassInterface_Corba_MethodOverride;
  673. Procedure TestClassInterface_Corba_Delegation;
  674. Procedure TestClassInterface_Corba_DelegationStatic;
  675. Procedure TestClassInterface_Corba_Operators;
  676. Procedure TestClassInterface_Corba_Args;
  677. Procedure TestClassInterface_Corba_ForIn;
  678. Procedure TestClassInterface_Corba_ArrayOfIntf;
  679. Procedure TestClassInterface_COM_AssignVar;
  680. Procedure TestClassInterface_COM_AssignArg;
  681. Procedure TestClassInterface_COM_FunctionResult;
  682. Procedure TestClassInterface_COM_InheritedFuncResult;
  683. Procedure TestClassInterface_COM_IsAsTypeCasts;
  684. Procedure TestClassInterface_COM_PassAsArg;
  685. Procedure TestClassInterface_COM_PassToUntypedParam;
  686. Procedure TestClassInterface_COM_FunctionInExpr;
  687. Procedure TestClassInterface_COM_Property;
  688. Procedure TestClassInterface_COM_IntfProperty;
  689. Procedure TestClassInterface_COM_Delegation;
  690. Procedure TestClassInterface_COM_With;
  691. Procedure TestClassInterface_COM_ForIn;
  692. Procedure TestClassInterface_COM_ArrayOfIntf;
  693. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  694. Procedure TestClassInterface_COM_RecordIntfFail;
  695. Procedure TestClassInterface_COM_UnitInitialization;
  696. Procedure TestClassInterface_Corba_GUID;
  697. Procedure TestClassInterface_Corba_GUIDProperty;
  698. // helpers
  699. Procedure TestClassHelper_ClassVar;
  700. Procedure TestClassHelper_Method_AccessInstanceFields;
  701. Procedure TestClassHelper_Method_Call;
  702. Procedure TestClassHelper_Method_Nested_Call;
  703. Procedure TestClassHelper_ClassMethod_Call;
  704. Procedure TestClassHelper_ClassOf;
  705. Procedure TestClassHelper_MethodRefObjFPC;
  706. Procedure TestClassHelper_Constructor;
  707. Procedure TestClassHelper_InheritedObjFPC;
  708. Procedure TestClassHelper_Property;
  709. Procedure TestClassHelper_Property_Array;
  710. Procedure TestClassHelper_Property_Array_Default;
  711. Procedure TestClassHelper_Property_Array_DefaultDefault;
  712. Procedure TestClassHelper_ClassProperty;
  713. Procedure TestClassHelper_ClassPropertyStatic;
  714. Procedure TestClassHelper_ClassProperty_Array;
  715. Procedure TestClassHelper_ForIn;
  716. Procedure TestClassHelper_PassProperty;
  717. Procedure TestExtClassHelper_ClassVar;
  718. Procedure TestExtClassHelper_Method_Call;
  719. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  720. Procedure TestRecordHelper_ClassVar;
  721. Procedure TestRecordHelper_Method_Call;
  722. Procedure TestRecordHelper_Constructor;
  723. Procedure TestTypeHelper_ClassVar;
  724. Procedure TestTypeHelper_PassResultElement;
  725. Procedure TestTypeHelper_PassArgs;
  726. Procedure TestTypeHelper_PassVarConst;
  727. Procedure TestTypeHelper_PassFuncResult;
  728. Procedure TestTypeHelper_PassPropertyField;
  729. Procedure TestTypeHelper_PassPropertyGetter;
  730. Procedure TestTypeHelper_PassClassPropertyField;
  731. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  732. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  733. Procedure TestTypeHelper_Property;
  734. Procedure TestTypeHelper_Property_Array;
  735. Procedure TestTypeHelper_ClassProperty;
  736. Procedure TestTypeHelper_ClassProperty_Array;
  737. Procedure TestTypeHelper_ClassMethod;
  738. Procedure TestTypeHelper_ExtClassMethodFail;
  739. Procedure TestTypeHelper_Constructor;
  740. Procedure TestTypeHelper_Word;
  741. Procedure TestTypeHelper_Boolean;
  742. Procedure TestTypeHelper_WordBool;
  743. Procedure TestTypeHelper_Double;
  744. Procedure TestTypeHelper_NativeInt;
  745. Procedure TestTypeHelper_StringChar;
  746. Procedure TestTypeHelper_JSValue;
  747. Procedure TestTypeHelper_Array;
  748. Procedure TestTypeHelper_EnumType;
  749. Procedure TestTypeHelper_SetType;
  750. Procedure TestTypeHelper_InterfaceType;
  751. Procedure TestTypeHelper_NestedSelf;
  752. // proc types
  753. Procedure TestProcType;
  754. Procedure TestProcType_Arg;
  755. Procedure TestProcType_FunctionFPC;
  756. Procedure TestProcType_FunctionDelphi;
  757. Procedure TestProcType_ProcedureDelphi;
  758. Procedure TestProcType_AsParam;
  759. Procedure TestProcType_MethodFPC;
  760. Procedure TestProcType_MethodDelphi;
  761. Procedure TestProcType_PropertyFPC;
  762. Procedure TestProcType_PropertyDelphi;
  763. Procedure TestProcType_WithClassInstDoPropertyFPC;
  764. Procedure TestProcType_Nested;
  765. Procedure TestProcType_NestedOfObject;
  766. Procedure TestProcType_ReferenceToProc;
  767. Procedure TestProcType_ReferenceToMethod;
  768. Procedure TestProcType_Typecast;
  769. Procedure TestProcType_PassProcToUntyped;
  770. Procedure TestProcType_PassProcToArray;
  771. Procedure TestProcType_SafeCallObjFPC;
  772. Procedure TestProcType_SafeCallDelphi;
  773. Procedure TestProcType_SafeCall_Arg;
  774. // pointer
  775. Procedure TestPointer;
  776. Procedure TestPointer_Proc;
  777. Procedure TestPointer_AssignRecordFail;
  778. Procedure TestPointer_AssignStaticArrayFail;
  779. Procedure TestPointer_TypeCastJSValueToPointer;
  780. Procedure TestPointer_NonRecordFail;
  781. Procedure TestPointer_AnonymousArgTypeFail;
  782. Procedure TestPointer_AnonymousVarTypeFail;
  783. Procedure TestPointer_AnonymousResultTypeFail;
  784. Procedure TestPointer_AddrOperatorFail;
  785. Procedure TestPointer_ArrayParamsFail;
  786. Procedure TestPointer_PointerAddFail;
  787. Procedure TestPointer_IncPointerFail;
  788. Procedure TestPointer_Record;
  789. Procedure TestPointer_RecordArg;
  790. // jsvalue
  791. Procedure TestJSValue_AssignToJSValue;
  792. Procedure TestJSValue_TypeCastToBaseType;
  793. Procedure TestJSValue_TypecastToJSValue;
  794. Procedure TestJSValue_Equal;
  795. Procedure TestJSValue_If;
  796. Procedure TestJSValue_Not;
  797. Procedure TestJSValue_Enum;
  798. Procedure TestJSValue_ClassInstance;
  799. Procedure TestJSValue_ClassOf;
  800. Procedure TestJSValue_ArrayOfJSValue;
  801. Procedure TestJSValue_ArrayLit;
  802. Procedure TestJSValue_Params;
  803. Procedure TestJSValue_UntypedParam;
  804. Procedure TestJSValue_FuncResultType;
  805. Procedure TestJSValue_ProcType_Assign;
  806. Procedure TestJSValue_ProcType_Equal;
  807. Procedure TestJSValue_ProcType_Param;
  808. Procedure TestJSValue_AssignToPointerFail;
  809. Procedure TestJSValue_OverloadDouble;
  810. Procedure TestJSValue_OverloadNativeInt;
  811. Procedure TestJSValue_OverloadWord;
  812. Procedure TestJSValue_OverloadString;
  813. Procedure TestJSValue_OverloadChar;
  814. Procedure TestJSValue_OverloadPointer;
  815. Procedure TestJSValue_ForIn;
  816. // RTTI
  817. Procedure TestRTTI_IntRange;
  818. Procedure TestRTTI_Double;
  819. Procedure TestRTTI_ProcType;
  820. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  821. Procedure TestRTTI_ProcTypeAnonymous;
  822. Procedure TestRTTI_EnumAndSetType;
  823. Procedure TestRTTI_EnumRange;
  824. Procedure TestRTTI_AnonymousEnumType;
  825. Procedure TestRTTI_StaticArray;
  826. Procedure TestRTTI_DynArray;
  827. Procedure TestRTTI_ArrayNestedAnonymous;
  828. Procedure TestRTTI_PublishedMethodOverloadFail;
  829. Procedure TestRTTI_PublishedMethodHideNoHint;
  830. Procedure TestRTTI_PublishedMethodExternalFail;
  831. Procedure TestRTTI_PublishedClassPropertyFail;
  832. Procedure TestRTTI_PublishedClassFieldFail;
  833. Procedure TestRTTI_PublishedFieldExternalFail;
  834. Procedure TestRTTI_Class_Field;
  835. Procedure TestRTTI_Class_Method;
  836. Procedure TestRTTI_Class_MethodArgFlags;
  837. Procedure TestRTTI_Class_Property;
  838. Procedure TestRTTI_Class_PropertyParams;
  839. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  840. Procedure TestRTTI_Class_OmitRTTI;
  841. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  842. Procedure TestRTTI_IndexModifier;
  843. Procedure TestRTTI_StoredModifier;
  844. Procedure TestRTTI_DefaultValue;
  845. Procedure TestRTTI_DefaultValueSet;
  846. Procedure TestRTTI_DefaultValueRangeType;
  847. Procedure TestRTTI_DefaultValueInherit;
  848. Procedure TestRTTI_OverrideMethod;
  849. Procedure TestRTTI_ReintroduceMethod;
  850. Procedure TestRTTI_OverloadProperty;
  851. // ToDo: array argument
  852. Procedure TestRTTI_ClassForward;
  853. Procedure TestRTTI_ClassOf;
  854. Procedure TestRTTI_Record;
  855. Procedure TestRTTI_RecordAnonymousArray;
  856. Procedure TestRTTI_Record_ClassVarType;
  857. Procedure TestRTTI_LocalTypes;
  858. Procedure TestRTTI_TypeInfo_BaseTypes;
  859. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  860. Procedure TestRTTI_TypeInfo_LocalFail;
  861. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  862. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  863. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  864. Procedure TestRTTI_TypeInfo_FunctionClassType;
  865. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  866. Procedure TestRTTI_Interface_Corba;
  867. Procedure TestRTTI_Interface_COM;
  868. Procedure TestRTTI_ClassHelper;
  869. Procedure TestRTTI_ExternalClass;
  870. Procedure TestRTTI_Unit;
  871. // Resourcestring
  872. Procedure TestResourcestringProgram;
  873. Procedure TestResourcestringUnit;
  874. Procedure TestResourcestringImplementation;
  875. // Attributes
  876. Procedure TestAttributes_Members;
  877. Procedure TestAttributes_Types;
  878. Procedure TestAttributes_HelperConstructor_Fail;
  879. Procedure TestAttributes_InterfacesList;
  880. // Assertions, checks
  881. procedure TestAssert;
  882. procedure TestAssert_SysUtils;
  883. procedure TestObjectChecks;
  884. procedure TestOverflowChecks_Int;
  885. procedure TestRangeChecks_AssignInt;
  886. procedure TestRangeChecks_AssignIntRange;
  887. procedure TestRangeChecks_AssignEnum;
  888. procedure TestRangeChecks_AssignEnumRange;
  889. procedure TestRangeChecks_AssignChar;
  890. procedure TestRangeChecks_AssignCharRange;
  891. procedure TestRangeChecks_ArrayIndex;
  892. procedure TestRangeChecks_ArrayOfRecIndex;
  893. procedure TestRangeChecks_StringIndex;
  894. procedure TestRangeChecks_TypecastInt;
  895. procedure TestRangeChecks_TypeHelperInt;
  896. procedure TestRangeChecks_AssignCurrency;
  897. // Async/AWait
  898. Procedure TestAsync_Proc;
  899. Procedure TestAsync_CallResultIsPromise;
  900. Procedure TestAsync_ConstructorFail;
  901. Procedure TestAsync_PropertyGetterFail;
  902. Procedure TestAwait_NonPromiseWithTypeFail;
  903. Procedure TestAwait_AsyncCallTypeMismatch;
  904. Procedure TestAWait_OutsideAsyncFail;
  905. Procedure TestAWait_IntegerFail;
  906. Procedure TestAWait_ExternalClassPromise;
  907. Procedure TestAWait_JSValue;
  908. Procedure TestAWait_Result;
  909. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  910. Procedure TestAsync_AnonymousProc;
  911. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  912. Procedure TestAsync_ProcType;
  913. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  914. Procedure TestAsync_Inherited;
  915. Procedure TestAsync_ClassInterface;
  916. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  917. Procedure TestAWait_ClassAs;
  918. // Library
  919. Procedure TestLibrary_Empty;
  920. Procedure TestLibrary_ExportFunc;
  921. Procedure TestLibrary_ExportFuncOverloadedFail;
  922. Procedure TestLibrary_Export_Index_Fail;
  923. Procedure TestLibrary_ExportVar;
  924. Procedure TestLibrary_ExportUnitFunc;
  925. end;
  926. function LinesToStr(Args: array of const): string;
  927. function ExtractFileUnitName(aFilename: string): string;
  928. function JSToStr(El: TJSElement): string;
  929. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  930. implementation
  931. function LinesToStr(Args: array of const): string;
  932. var
  933. s: String;
  934. i: Integer;
  935. begin
  936. s:='';
  937. for i:=Low(Args) to High(Args) do
  938. case Args[i].VType of
  939. vtChar: s += Args[i].VChar+LineEnding;
  940. vtString: s += Args[i].VString^+LineEnding;
  941. vtPChar: s += Args[i].VPChar+LineEnding;
  942. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  943. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  944. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  945. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  946. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  947. end;
  948. Result:=s;
  949. end;
  950. function ExtractFileUnitName(aFilename: string): string;
  951. var
  952. p: Integer;
  953. begin
  954. Result:=ExtractFileName(aFilename);
  955. if Result='' then exit;
  956. for p:=length(Result) downto 1 do
  957. case Result[p] of
  958. '/','\': exit;
  959. '.':
  960. begin
  961. Delete(Result,p,length(Result));
  962. exit;
  963. end;
  964. end;
  965. end;
  966. function JSToStr(El: TJSElement): string;
  967. var
  968. aWriter: TBufferWriter;
  969. aJSWriter: TJSWriter;
  970. begin
  971. aJSWriter:=nil;
  972. aWriter:=TBufferWriter.Create(1000);
  973. try
  974. aJSWriter:=TJSWriter.Create(aWriter);
  975. aJSWriter.IndentSize:=2;
  976. aJSWriter.WriteJS(El);
  977. Result:=aWriter.AsString;
  978. finally
  979. aJSWriter.Free;
  980. aWriter.Free;
  981. end;
  982. end;
  983. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  984. // search diff, ignore changes in spaces
  985. const
  986. SpaceChars = [#9,#10,#13,' '];
  987. var
  988. ExpectedP, ActualP: PChar;
  989. function FindLineEnd(p: PChar): PChar;
  990. begin
  991. Result:=p;
  992. while not (Result^ in [#0,#10,#13]) do inc(Result);
  993. end;
  994. function FindLineStart(p, MinP: PChar): PChar;
  995. begin
  996. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  997. Result:=p;
  998. end;
  999. procedure SkipLineEnd(var p: PChar);
  1000. begin
  1001. if p^ in [#10,#13] then
  1002. begin
  1003. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1004. inc(p,2)
  1005. else
  1006. inc(p);
  1007. end;
  1008. end;
  1009. function HasSpecialChar(s: string): boolean;
  1010. var
  1011. i: Integer;
  1012. begin
  1013. for i:=1 to length(s) do
  1014. if s[i] in [#0..#31,#127..#255] then
  1015. exit(true);
  1016. Result:=false;
  1017. end;
  1018. function HashSpecialChars(s: string): string;
  1019. var
  1020. i: Integer;
  1021. begin
  1022. Result:='';
  1023. for i:=1 to length(s) do
  1024. if s[i] in [#0..#31,#127..#255] then
  1025. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1026. else
  1027. Result:=Result+s[i];
  1028. end;
  1029. procedure DiffFound;
  1030. var
  1031. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1032. ExpLine, ActLine: String;
  1033. i, LineNo, DiffLineNo: Integer;
  1034. begin
  1035. writeln('Diff found "',Msg,'". Lines:');
  1036. // write correct lines
  1037. p:=PChar(Expected);
  1038. LineNo:=0;
  1039. DiffLineNo:=0;
  1040. repeat
  1041. StartPos:=p;
  1042. while not (p^ in [#0,#10,#13]) do inc(p);
  1043. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1044. SkipLineEnd(p);
  1045. inc(LineNo);
  1046. if (p<=ExpectedP) and (p^<>#0) then
  1047. begin
  1048. writeln('= ',ExpLine);
  1049. end else begin
  1050. // diff line
  1051. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1052. // write actual line
  1053. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1054. ActLineEndP:=FindLineEnd(ActualP);
  1055. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1056. writeln('- ',ActLine);
  1057. if HasSpecialChar(ActLine) then
  1058. writeln('- ',HashSpecialChars(ActLine));
  1059. // write expected line
  1060. writeln('+ ',ExpLine);
  1061. if HasSpecialChar(ExpLine) then
  1062. writeln('- ',HashSpecialChars(ExpLine));
  1063. // write empty line with pointer ^
  1064. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1065. writeln('^');
  1066. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1067. CheckSrcDiff:=false;
  1068. // write up to three following actual lines to get some context
  1069. for i:=1 to 3 do begin
  1070. ActLineStartP:=ActLineEndP;
  1071. SkipLineEnd(ActLineStartP);
  1072. if ActLineStartP^=#0 then break;
  1073. ActLineEndP:=FindLineEnd(ActLineStartP);
  1074. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1075. writeln('~ ',ActLine);
  1076. end;
  1077. exit;
  1078. end;
  1079. until p^=#0;
  1080. writeln('DiffFound Actual:-----------------------');
  1081. writeln(Actual);
  1082. writeln('DiffFound Expected:---------------------');
  1083. writeln(Expected);
  1084. writeln('DiffFound ------------------------------');
  1085. Msg:='diff found, but lines are the same, internal error';
  1086. CheckSrcDiff:=false;
  1087. end;
  1088. var
  1089. IsSpaceNeeded: Boolean;
  1090. LastChar, Quote: Char;
  1091. begin
  1092. Result:=true;
  1093. Msg:='';
  1094. if Expected='' then Expected:=' ';
  1095. if Actual='' then Actual:=' ';
  1096. ExpectedP:=PChar(Expected);
  1097. ActualP:=PChar(Actual);
  1098. repeat
  1099. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1100. case ExpectedP^ of
  1101. #0:
  1102. begin
  1103. // check that rest of Actual has only spaces
  1104. while ActualP^ in SpaceChars do inc(ActualP);
  1105. if ActualP^<>#0 then
  1106. begin
  1107. DiffFound;
  1108. exit;
  1109. end;
  1110. exit(true);
  1111. end;
  1112. ' ',#9,#10,#13:
  1113. begin
  1114. // skip space in Expected
  1115. IsSpaceNeeded:=false;
  1116. if ExpectedP>PChar(Expected) then
  1117. LastChar:=ExpectedP[-1]
  1118. else
  1119. LastChar:=#0;
  1120. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1121. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1122. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1123. IsSpaceNeeded:=true;
  1124. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1125. begin
  1126. DiffFound;
  1127. exit;
  1128. end;
  1129. while ActualP^ in SpaceChars do inc(ActualP);
  1130. end;
  1131. '''','"':
  1132. begin
  1133. while ActualP^ in SpaceChars do inc(ActualP);
  1134. if ExpectedP^<>ActualP^ then
  1135. begin
  1136. DiffFound;
  1137. exit;
  1138. end;
  1139. Quote:=ExpectedP^;
  1140. repeat
  1141. inc(ExpectedP);
  1142. inc(ActualP);
  1143. if ExpectedP^<>ActualP^ then
  1144. begin
  1145. DiffFound;
  1146. exit;
  1147. end;
  1148. if (ExpectedP^ in [#0,#10,#13]) then
  1149. break
  1150. else if (ExpectedP^=Quote) then
  1151. begin
  1152. inc(ExpectedP);
  1153. inc(ActualP);
  1154. break;
  1155. end;
  1156. until false;
  1157. end;
  1158. else
  1159. while ActualP^ in SpaceChars do inc(ActualP);
  1160. if ExpectedP^<>ActualP^ then
  1161. begin
  1162. DiffFound;
  1163. exit;
  1164. end;
  1165. inc(ExpectedP);
  1166. inc(ActualP);
  1167. end;
  1168. until false;
  1169. end;
  1170. { TTestEnginePasResolver }
  1171. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1172. begin
  1173. if FModule=AValue then Exit;
  1174. FModule:=AValue;
  1175. end;
  1176. destructor TTestEnginePasResolver.Destroy;
  1177. begin
  1178. FreeAndNil(FStreamResolver);
  1179. FreeAndNil(FParser);
  1180. FreeAndNil(FScanner);
  1181. FreeAndNil(FStreamResolver);
  1182. Module:=nil;
  1183. inherited Destroy;
  1184. end;
  1185. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1186. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1187. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1188. begin
  1189. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1190. TypeParams);
  1191. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1192. Module:=TPasModule(Result);
  1193. end;
  1194. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1195. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1196. begin
  1197. Result:=nil;
  1198. if InFilename<>'' then
  1199. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1200. if Assigned(OnFindUnit) then
  1201. Result:=OnFindUnit(AName);
  1202. if NameExpr=nil then ;
  1203. end;
  1204. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1205. begin
  1206. // do not parse recursively
  1207. // parse via the queue
  1208. if Section=nil then ;
  1209. end;
  1210. { TCustomTestModule }
  1211. procedure TCustomTestModule.FreeSrcMarkers;
  1212. var
  1213. aMarker, Last: PSrcMarker;
  1214. begin
  1215. aMarker:=FirstSrcMarker;
  1216. while aMarker<>nil do
  1217. begin
  1218. Last:=aMarker;
  1219. aMarker:=aMarker^.Next;
  1220. Dispose(Last);
  1221. end;
  1222. FirstSrcMarker:=nil;
  1223. LastSrcMarker:=nil;
  1224. end;
  1225. function TCustomTestModule.GetResolverCount: integer;
  1226. begin
  1227. Result:=FResolvers.Count;
  1228. end;
  1229. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1230. begin
  1231. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1232. end;
  1233. function TCustomTestModule.GetMsgCount: integer;
  1234. begin
  1235. Result:=FHintMsgs.Count;
  1236. end;
  1237. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1238. begin
  1239. Result:=TTestHintMessage(FHintMsgs[Index]);
  1240. end;
  1241. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1242. ): TPasModule;
  1243. var
  1244. DefNamespace: String;
  1245. begin
  1246. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1247. if (Pos('.',aUnitName)<1) then
  1248. begin
  1249. DefNamespace:=GetDefaultNamespace;
  1250. if DefNamespace<>'' then
  1251. begin
  1252. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1253. if Result<>nil then exit;
  1254. end;
  1255. end;
  1256. Result:=LoadUnit(aUnitName);
  1257. if Result<>nil then exit;
  1258. {$IFDEF VerbosePas2JS}
  1259. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1260. {$ENDIF}
  1261. Fail('can''t find unit "'+aUnitName+'"');
  1262. end;
  1263. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1264. var
  1265. aParser: TPasParser;
  1266. Item: TTestHintMessage;
  1267. begin
  1268. aParser:=Sender as TPasParser;
  1269. Item:=TTestHintMessage.Create;
  1270. Item.Id:=aParser.LastMsgNumber;
  1271. Item.MsgType:=aParser.LastMsgType;
  1272. Item.MsgNumber:=aParser.LastMsgNumber;
  1273. Item.Msg:=Msg;
  1274. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1275. {$IFDEF VerbosePas2JS}
  1276. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1277. {$ENDIF}
  1278. FHintMsgs.Add(Item);
  1279. end;
  1280. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1281. );
  1282. var
  1283. aResolver: TTestEnginePasResolver;
  1284. Item: TTestHintMessage;
  1285. begin
  1286. aResolver:=Sender as TTestEnginePasResolver;
  1287. Item:=TTestHintMessage.Create;
  1288. Item.Id:=aResolver.LastMsgId;
  1289. Item.MsgType:=aResolver.LastMsgType;
  1290. Item.MsgNumber:=aResolver.LastMsgNumber;
  1291. Item.Msg:=Msg;
  1292. Item.SourcePos:=aResolver.LastSourcePos;
  1293. {$IFDEF VerbosePas2JS}
  1294. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1295. {$ENDIF}
  1296. FHintMsgs.Add(Item);
  1297. end;
  1298. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1299. var
  1300. Item: TTestHintMessage;
  1301. aScanner: TPas2jsPasScanner;
  1302. begin
  1303. aScanner:=Sender as TPas2jsPasScanner;
  1304. Item:=TTestHintMessage.Create;
  1305. Item.Id:=aScanner.LastMsgNumber;
  1306. Item.MsgType:=aScanner.LastMsgType;
  1307. Item.MsgNumber:=aScanner.LastMsgNumber;
  1308. Item.Msg:=Msg;
  1309. Item.SourcePos:=aScanner.CurSourcePos;
  1310. {$IFDEF VerbosePas2JS}
  1311. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1312. {$ENDIF}
  1313. FHintMsgs.Add(Item);
  1314. end;
  1315. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1316. var
  1317. SubEl: TPasElement;
  1318. i: Integer;
  1319. procedure E(Msg: string);
  1320. var
  1321. s: String;
  1322. begin
  1323. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1324. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1325. writeln('ERROR: ',s);
  1326. Fail(s);
  1327. end;
  1328. begin
  1329. if arg=nil then ;
  1330. if El=nil then exit;
  1331. if El.Parent=El then
  1332. E('El.Parent=El='+GetObjName(El));
  1333. if El is TBinaryExpr then
  1334. begin
  1335. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1336. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1337. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1338. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1339. end
  1340. else if El is TParamsExpr then
  1341. begin
  1342. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1343. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1344. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1345. if TParamsExpr(El).Params[i].Parent<>El then
  1346. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1347. end
  1348. else if El is TProcedureExpr then
  1349. begin
  1350. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1351. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1352. end
  1353. else if El is TPasDeclarations then
  1354. begin
  1355. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1356. begin
  1357. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1358. if SubEl.Parent<>El then
  1359. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1360. end;
  1361. end
  1362. else if El is TPasImplBlock then
  1363. begin
  1364. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1365. begin
  1366. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1367. if SubEl.Parent<>El then
  1368. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1369. end;
  1370. end
  1371. else if El is TPasImplWithDo then
  1372. begin
  1373. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1374. begin
  1375. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1376. if SubEl.Parent<>El then
  1377. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1378. end;
  1379. end
  1380. else if El is TPasProcedure then
  1381. begin
  1382. if TPasProcedure(El).ProcType.Parent<>El then
  1383. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1384. end
  1385. else if El is TPasProcedureType then
  1386. begin
  1387. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1388. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1389. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1390. end;
  1391. end;
  1392. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1393. var
  1394. Data: PTestResolverReferenceData absolute FindData;
  1395. Line, Col: integer;
  1396. begin
  1397. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1398. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1399. if (Data^.Filename=El.SourceFilename)
  1400. and (Data^.Row=Line)
  1401. and (Data^.StartCol<=Col)
  1402. and (Data^.EndCol>=Col)
  1403. then
  1404. Data^.Found.Add(El);
  1405. end;
  1406. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1407. begin
  1408. if FWithTypeInfo=AValue then Exit;
  1409. FWithTypeInfo:=AValue;
  1410. if AValue then
  1411. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1412. else
  1413. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1414. end;
  1415. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1416. var
  1417. i: Integer;
  1418. CurEngine: TTestEnginePasResolver;
  1419. CurUnitName: String;
  1420. begin
  1421. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1422. Result:=nil;
  1423. if (Module.ClassType=TPasModule)
  1424. and (CompareText(Module.Name,aUnitName)=0) then
  1425. exit(Module);
  1426. for i:=0 to ResolverCount-1 do
  1427. begin
  1428. CurEngine:=Resolvers[i];
  1429. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1430. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1431. if CompareText(aUnitName,CurUnitName)=0 then
  1432. begin
  1433. Result:=CurEngine.Module;
  1434. if Result<>nil then exit;
  1435. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1436. FileResolver.FindSourceFile(aUnitName);
  1437. CurEngine.StreamResolver:=TStreamResolver.Create;
  1438. CurEngine.StreamResolver.OwnsStreams:=True;
  1439. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1440. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1441. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1442. InitScanner(CurEngine.Scanner);
  1443. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1444. CurEngine.Parser.Options:=po_tcmodules;
  1445. if CompareText(CurUnitName,'System')=0 then
  1446. CurEngine.Parser.ImplicitUses.Clear;
  1447. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1448. try
  1449. CurEngine.Parser.NextToken;
  1450. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1451. except
  1452. on E: Exception do
  1453. HandleException(E);
  1454. end;
  1455. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1456. Result:=CurEngine.Module;
  1457. exit;
  1458. end;
  1459. end;
  1460. end;
  1461. procedure TCustomTestModule.SetUp;
  1462. begin
  1463. {$IFDEF EnablePasTreeGlobalRefCount}
  1464. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1465. {$ENDIF}
  1466. if FResolvers<>nil then
  1467. begin
  1468. writeln('TCustomTestModule.SetUp FModules<>nil');
  1469. Halt;
  1470. end;
  1471. inherited SetUp;
  1472. FSkipTests:=false;
  1473. FWithTypeInfo:=false;
  1474. FSource:=TStringList.Create;
  1475. FHub:=TPas2JSResolverHub.Create(Self);
  1476. FResolvers:=TObjectList.Create(true);
  1477. FFilename:='test1.pp';
  1478. FFileResolver:=TStreamResolver.Create;
  1479. FFileResolver.OwnsStreams:=True;
  1480. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1481. InitScanner(FScanner);
  1482. FEngine:=AddModule(Filename);
  1483. FEngine.Scanner:=FScanner;
  1484. FScanner.Resolver:=FEngine;
  1485. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1486. FParser.OnLog:=@OnParserLog;
  1487. FEngine.Parser:=FParser;
  1488. Parser.Options:=po_tcmodules;
  1489. FModule:=Nil;
  1490. FConverter:=CreateConverter;
  1491. FExpectedErrorClass:=nil;
  1492. end;
  1493. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1494. var
  1495. Options: TPasToJsConverterOptions;
  1496. begin
  1497. Result:=TPasToJSConverter.Create;
  1498. Options:=co_tcmodules;
  1499. if WithTypeInfo then
  1500. Exclude(Options,coNoTypeInfo)
  1501. else
  1502. Include(Options,coNoTypeInfo);
  1503. Result.Options:=Options;
  1504. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1505. end;
  1506. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1507. begin
  1508. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1509. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1510. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1511. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1512. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1513. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1514. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1515. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1516. aScanner.OnLog:=@OnScannerLog;
  1517. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1518. end;
  1519. procedure TCustomTestModule.TearDown;
  1520. {$IFDEF CheckPasTreeRefCount}
  1521. var
  1522. El: TPasElement;
  1523. {$ENDIF}
  1524. var
  1525. i: Integer;
  1526. CurModule: TPasModule;
  1527. begin
  1528. FreeSrcMarkers;
  1529. FHintMsgs.Clear;
  1530. FHintMsgsGood.Clear;
  1531. FSkipTests:=false;
  1532. FWithTypeInfo:=false;
  1533. FJSRegModuleCall:=nil;
  1534. FJSModuleCallArgs:=nil;
  1535. FJSImplentationUses:=nil;
  1536. FJSInterfaceUses:=nil;
  1537. FJSModuleSrc:=nil;
  1538. FJSInitBody:=nil;
  1539. FreeAndNil(FJSSource);
  1540. FreeAndNil(FJSModule);
  1541. FreeAndNil(FConverter);
  1542. ResolverEngine.Clear;
  1543. FreeAndNil(FSource);
  1544. FreeAndNil(FFileResolver);
  1545. if FResolvers<>nil then
  1546. begin
  1547. for i:=0 to FResolvers.Count-1 do
  1548. begin
  1549. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1550. if CurModule=nil then continue;
  1551. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1552. end;
  1553. for i:=0 to FResolvers.Count-1 do
  1554. begin
  1555. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1556. if CurModule=nil then continue;
  1557. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1558. end;
  1559. FreeAndNil(FResolvers);
  1560. FModule:=nil;
  1561. FEngine:=nil;
  1562. end;
  1563. FreeAndNil(FHub);
  1564. inherited TearDown;
  1565. {$IFDEF EnablePasTreeGlobalRefCount}
  1566. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1567. begin
  1568. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1569. {$IFDEF CheckPasTreeRefCount}
  1570. El:=TPasElement.FirstRefEl;
  1571. while El<>nil do
  1572. begin
  1573. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1574. for i:=0 to El.RefIds.Count-1 do
  1575. writeln(' ',El.RefIds[i]);
  1576. El:=El.NextRefEl;
  1577. end;
  1578. {$ENDIF}
  1579. Halt;
  1580. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1581. end;
  1582. {$ENDIF}
  1583. end;
  1584. procedure TCustomTestModule.Add(Line: string);
  1585. begin
  1586. Source.Add(Line);
  1587. end;
  1588. procedure TCustomTestModule.Add(const Lines: array of string);
  1589. var
  1590. i: Integer;
  1591. begin
  1592. for i:=low(Lines) to high(Lines) do
  1593. Add(Lines[i]);
  1594. end;
  1595. procedure TCustomTestModule.StartParsing;
  1596. var
  1597. Src: String;
  1598. begin
  1599. Src:=Source.Text;
  1600. FEngine.Source:=Src;
  1601. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1602. Scanner.OpenFile(FileName);
  1603. Writeln('// Test : ',Self.TestName);
  1604. Writeln(Src);
  1605. end;
  1606. procedure TCustomTestModule.ParseModuleQueue;
  1607. var
  1608. i: Integer;
  1609. CurResolver: TTestEnginePasResolver;
  1610. Found: Boolean;
  1611. Section: TPasSection;
  1612. begin
  1613. // parse til exception or all Resolvers finished
  1614. while not SkipTests do
  1615. begin
  1616. Found:=false;
  1617. for i:=0 to ResolverCount-1 do
  1618. begin
  1619. CurResolver:=Resolvers[i];
  1620. if CurResolver.CurrentParser=nil then continue;
  1621. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1622. continue;
  1623. CurResolver.Parser.ParseContinue;
  1624. Found:=true;
  1625. break;
  1626. end;
  1627. if not Found then break;
  1628. end;
  1629. for i:=0 to ResolverCount-1 do
  1630. begin
  1631. CurResolver:=Resolvers[i];
  1632. if CurResolver.Parser=nil then
  1633. begin
  1634. if CurResolver.CurrentParser<>nil then
  1635. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1636. continue;
  1637. end;
  1638. if CurResolver.Parser.CurModule<>nil then
  1639. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1640. end;
  1641. end;
  1642. procedure TCustomTestModule.ParseModule;
  1643. begin
  1644. if SkipTests then exit;
  1645. FFirstPasStatement:=nil;
  1646. try
  1647. StartParsing;
  1648. Parser.ParseMain(FModule);
  1649. ParseModuleQueue;
  1650. except
  1651. on E: Exception do
  1652. HandleException(E);
  1653. end;
  1654. if SkipTests then exit;
  1655. AssertNotNull('Module resulted in Module',Module);
  1656. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1657. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1658. end;
  1659. procedure TCustomTestModule.ParseProgram;
  1660. begin
  1661. if SkipTests then exit;
  1662. ParseModule;
  1663. if SkipTests then exit;
  1664. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1665. FPasProgram:=TPasProgram(Module);
  1666. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1667. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1668. if (PasProgram.InitializationSection.Elements.Count>0) then
  1669. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1670. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1671. end;
  1672. procedure TCustomTestModule.ParseLibrary;
  1673. var
  1674. Init: TInitializationSection;
  1675. begin
  1676. if SkipTests then exit;
  1677. ParseModule;
  1678. if SkipTests then exit;
  1679. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1680. FPasLibrary:=TPasLibrary(Module);
  1681. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1682. Init:=PasLibrary.InitializationSection;
  1683. if (Init<>nil) and (Init.Elements.Count>0) then
  1684. if TObject(Init.Elements[0]) is TPasImplBlock then
  1685. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1686. end;
  1687. procedure TCustomTestModule.ParseUnit;
  1688. begin
  1689. if SkipTests then exit;
  1690. ParseModule;
  1691. if SkipTests then exit;
  1692. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1693. AssertNotNull('Has interface section',Module.InterfaceSection);
  1694. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1695. if (Module.InitializationSection<>nil)
  1696. and (Module.InitializationSection.Elements.Count>0)
  1697. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1698. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1699. end;
  1700. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1701. ): TTestEnginePasResolver;
  1702. var
  1703. i: Integer;
  1704. begin
  1705. for i:=0 to ResolverCount-1 do
  1706. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1707. exit(Resolvers[i]);
  1708. Result:=nil;
  1709. end;
  1710. function TCustomTestModule.AddModule(aFilename: string
  1711. ): TTestEnginePasResolver;
  1712. begin
  1713. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1714. if FindModuleWithFilename(aFilename)<>nil then
  1715. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1716. Result:=TTestEnginePasResolver.Create;
  1717. Result.Filename:=aFilename;
  1718. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1719. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1720. Result.OnLog:=@OnPasResolverLog;
  1721. Result.Hub:=Hub;
  1722. FResolvers.Add(Result);
  1723. end;
  1724. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1725. ): TTestEnginePasResolver;
  1726. begin
  1727. Result:=AddModule(aFilename);
  1728. Result.Source:=Src;
  1729. end;
  1730. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1731. ImplementationSrc: string): TTestEnginePasResolver;
  1732. var
  1733. Src: String;
  1734. begin
  1735. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1736. Src+=LineEnding;
  1737. Src+='interface'+LineEnding;
  1738. Src+=LineEnding;
  1739. Src+=InterfaceSrc;
  1740. Src+='implementation'+LineEnding;
  1741. Src+=LineEnding;
  1742. Src+=ImplementationSrc;
  1743. Src+='end.'+LineEnding;
  1744. Result:=AddModuleWithSrc(aFilename,Src);
  1745. end;
  1746. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1747. var
  1748. Intf, Impl: TStringList;
  1749. begin
  1750. Intf:=TStringList.Create;
  1751. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1752. // unit interface
  1753. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1754. Intf.Add('{$modeswitch externalclass}');
  1755. Intf.Add('type');
  1756. Intf.Add(' integer=longint;');
  1757. Intf.Add(' sizeint=nativeint;');
  1758. //'const',
  1759. //' LineEnding = #10;',
  1760. //' DirectorySeparator = ''/'';',
  1761. //' DriveSeparator = '''';',
  1762. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1763. //' AllowDriveSeparators : set of char = [];',
  1764. if supTObject in Parts then
  1765. Intf.AddStrings([
  1766. 'type',
  1767. ' TClass = class of TObject;',
  1768. ' TObject = class',
  1769. ' constructor Create;',
  1770. ' destructor Destroy; virtual;',
  1771. ' class function ClassType: TClass; assembler;',
  1772. ' class function ClassName: String; assembler;',
  1773. ' class function ClassNameIs(const Name: string): boolean;',
  1774. ' class function ClassParent: TClass; assembler;',
  1775. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1776. ' class function UnitName: String; assembler;',
  1777. ' procedure AfterConstruction; virtual;',
  1778. ' procedure BeforeDestruction;virtual;',
  1779. ' function Equals(Obj: TObject): boolean; virtual;',
  1780. ' function ToString: String; virtual;',
  1781. ' end;']);
  1782. if supTInterfacedObject in Parts then
  1783. Intf.AddStrings([
  1784. ' {$Interfaces COM}',
  1785. ' IUnknown = interface',
  1786. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1787. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1788. ' function _AddRef: Integer;',
  1789. ' function _Release: Integer;',
  1790. ' end;',
  1791. ' IInterface = IUnknown;',
  1792. ' TInterfacedObject = class(TObject,IUnknown)',
  1793. ' protected',
  1794. ' fRefCount: Integer;',
  1795. ' { implement methods of IUnknown }',
  1796. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1797. ' function _AddRef: Integer; virtual;',
  1798. ' function _Release: Integer; virtual;',
  1799. ' end;',
  1800. ' TInterfacedClass = class of TInterfacedObject;',
  1801. '',
  1802. '']);
  1803. if supTVarRec in Parts then
  1804. Intf.AddStrings([
  1805. 'const',
  1806. ' vtInteger = 0;',
  1807. ' vtBoolean = 1;',
  1808. ' vtJSValue = 19;',
  1809. 'type',
  1810. ' PVarRec = ^TVarRec;',
  1811. ' TVarRec = record',
  1812. ' VType : byte;',
  1813. ' VJSValue: JSValue;',
  1814. ' vInteger: longint external name ''VJSValue'';',
  1815. ' vBoolean: boolean external name ''VJSValue'';',
  1816. ' end;',
  1817. ' TVarRecArray = array of TVarRec;',
  1818. 'function VarRecs: TVarRecArray; varargs;',
  1819. '']);
  1820. if supTypeInfo in Parts then
  1821. begin
  1822. Intf.AddStrings([
  1823. 'type',
  1824. ' TTypeKind = (',
  1825. ' tkUnknown, // 0',
  1826. ' tkInteger, // 1',
  1827. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1828. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1829. ' tkEnumeration, // 4',
  1830. ' tkSet, // 5',
  1831. ' tkDouble, // 6',
  1832. ' tkBool, // 7',
  1833. ' tkProcVar, // 8 function or procedure',
  1834. ' tkMethod, // 9 proc var of object',
  1835. ' tkArray, // 10 static array',
  1836. ' tkDynArray, // 11',
  1837. ' tkRecord, // 12',
  1838. ' tkClass, // 13',
  1839. ' tkClassRef, // 14',
  1840. ' tkPointer, // 15',
  1841. ' tkJSValue, // 16',
  1842. ' tkRefToProcVar, // 17 variable of procedure type',
  1843. ' tkInterface, // 18',
  1844. ' //tkObject,',
  1845. ' //tkSString,tkLString,tkAString,tkWString,',
  1846. ' //tkVariant,',
  1847. ' //tkWChar,',
  1848. ' //tkInt64,',
  1849. ' //tkQWord,',
  1850. ' //tkInterfaceRaw,',
  1851. ' //tkUString,tkUChar,',
  1852. ' tkHelper, // 19',
  1853. ' //tkFile,',
  1854. ' tkExtClass // 20',
  1855. ' );',
  1856. ' TTypeKinds = set of TTypeKind;',
  1857. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1858. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1859. ' end;',
  1860. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1861. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1862. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1863. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1864. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1865. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1866. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1867. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1868. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1869. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1870. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1871. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1872. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1873. '']);
  1874. end;
  1875. if supWriteln in Parts then
  1876. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1877. Intf.Add('var');
  1878. Intf.Add(' ExitCode: Longint = 0;');
  1879. // unit implementation
  1880. Impl:=TStringList.Create;
  1881. if supTObject in Parts then
  1882. Impl.AddStrings([
  1883. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1884. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1885. 'asm',
  1886. 'end;',
  1887. 'constructor TObject.Create; begin end;',
  1888. 'destructor TObject.Destroy; begin end;',
  1889. 'class function TObject.ClassType: TClass; assembler;',
  1890. 'asm',
  1891. 'end;',
  1892. 'class function TObject.ClassName: String; assembler;',
  1893. 'asm',
  1894. 'end;',
  1895. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1896. 'begin',
  1897. ' Result:=SameText(Name,ClassName);',
  1898. 'end;',
  1899. 'class function TObject.ClassParent: TClass; assembler;',
  1900. 'asm',
  1901. 'end;',
  1902. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1903. 'asm',
  1904. 'end;',
  1905. 'class function TObject.UnitName: String; assembler;',
  1906. 'asm',
  1907. 'end;',
  1908. 'procedure TObject.AfterConstruction; begin end;',
  1909. 'procedure TObject.BeforeDestruction; begin end;',
  1910. 'function TObject.Equals(Obj: TObject): boolean;',
  1911. 'begin',
  1912. ' Result:=Obj=Self;',
  1913. 'end;',
  1914. 'function TObject.ToString: String;',
  1915. 'begin',
  1916. ' Result:=ClassName;',
  1917. 'end;'
  1918. ]);
  1919. if supTInterfacedObject in Parts then
  1920. Impl.AddStrings([
  1921. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1922. //'begin',
  1923. //'end;',
  1924. 'function TInterfacedObject._AddRef: Integer;',
  1925. 'begin',
  1926. 'end;',
  1927. 'function TInterfacedObject._Release: Integer;',
  1928. 'begin',
  1929. 'end;',
  1930. '']);
  1931. if supTVarRec in Parts then
  1932. Impl.AddStrings([
  1933. 'function VarRecs: TVarRecArray; varargs;',
  1934. 'var',
  1935. ' v: PVarRec;',
  1936. 'begin',
  1937. ' v^.VType:=1;',
  1938. ' v^.VJSValue:=2;',
  1939. 'end;',
  1940. '']);
  1941. try
  1942. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1943. finally
  1944. Intf.Free;
  1945. Impl.Free;
  1946. end;
  1947. end;
  1948. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1949. SystemUnitParts: TSystemUnitParts);
  1950. begin
  1951. if NeedSystemUnit then
  1952. AddSystemUnit(SystemUnitParts)
  1953. else
  1954. Parser.ImplicitUses.Clear;
  1955. Add('program '+ExtractFileUnitName(Filename)+';');
  1956. Add('');
  1957. end;
  1958. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1959. SystemUnitParts: TSystemUnitParts);
  1960. begin
  1961. if NeedSystemUnit then
  1962. AddSystemUnit(SystemUnitParts)
  1963. else
  1964. Parser.ImplicitUses.Clear;
  1965. Add('library '+ExtractFileUnitName(Filename)+';');
  1966. Add('');
  1967. end;
  1968. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1969. SystemUnitParts: TSystemUnitParts);
  1970. begin
  1971. if NeedSystemUnit then
  1972. AddSystemUnit(SystemUnitParts)
  1973. else
  1974. Parser.ImplicitUses.Clear;
  1975. Add('unit Test1;');
  1976. Add('');
  1977. end;
  1978. procedure TCustomTestModule.ConvertModule;
  1979. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1980. out UsesLit: TJSArrayLiteral);
  1981. var
  1982. i: Integer;
  1983. Item: TJSElement;
  1984. Lit: TJSLiteral;
  1985. begin
  1986. UsesLit:=nil;
  1987. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1988. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1989. exit; // null is ok
  1990. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1991. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1992. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1993. begin
  1994. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1995. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1996. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1997. Lit:=TJSLiteral(Item);
  1998. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1999. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2000. end;
  2001. end;
  2002. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2003. out Src: TJSSourceElements);
  2004. var
  2005. FunDecl: TJSFunctionDeclarationStatement;
  2006. FunDef: TJSFuncDef;
  2007. FunBody: TJSFunctionBody;
  2008. begin
  2009. Src:=nil;
  2010. AssertNotNull(ParamName,Arg.Expr);
  2011. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2012. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2013. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2014. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2015. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2016. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2017. AssertNotNull(ParamName+' body',FunDef.Body);
  2018. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2019. FunBody:=FunDef.Body as TJSFunctionBody;
  2020. AssertNotNull(ParamName+' body.A',FunBody.A);
  2021. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2022. Src:=FunBody.A as TJSSourceElements;
  2023. end;
  2024. var
  2025. ModuleNameExpr: TJSLiteral;
  2026. InitFunction: TJSFunctionDeclarationStatement;
  2027. InitAssign: TJSSimpleAssignStatement;
  2028. InitName: String;
  2029. LastNode, FirstNode: TJSElement;
  2030. Arg: TJSArrayLiteralElement;
  2031. IsProg, IsLib: Boolean;
  2032. begin
  2033. if SkipTests then exit;
  2034. IsProg:=false;
  2035. IsLib:=false;
  2036. if Module is TPasProgram then
  2037. IsProg:=true
  2038. else if Module is TPasLibrary then
  2039. IsLib:=true;
  2040. try
  2041. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2042. except
  2043. on E: Exception do
  2044. HandleException(E);
  2045. end;
  2046. if SkipTests then exit;
  2047. if ExpectedErrorClass<>nil then
  2048. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2049. FJSSource:=TStringList.Create;
  2050. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2051. {$IFDEF VerbosePas2JS}
  2052. writeln('TTestModule.ConvertModule JS:');
  2053. write(FJSSource.Text);
  2054. {$ENDIF}
  2055. // rtl.module(...
  2056. if JSModule.Statements.Count<1 then
  2057. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2058. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2059. AssertNotNull('register module call',FirstNode);
  2060. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2061. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2062. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2063. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2064. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2065. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2066. // parameter 'unitname'
  2067. if JSModuleCallArgs.Elements.Count<1 then
  2068. Fail('rtl.module first param unit missing');
  2069. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2070. AssertNotNull('module name param',Arg.Expr);
  2071. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2072. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2073. if IsProg then
  2074. begin
  2075. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2076. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2077. end
  2078. else if IsLib then
  2079. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2080. else
  2081. begin
  2082. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2083. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2084. end;
  2085. // main uses section
  2086. if JSModuleCallArgs.Elements.Count<2 then
  2087. Fail('rtl.module second param main uses missing');
  2088. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2089. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2090. // program/library/interface function()
  2091. if JSModuleCallArgs.Elements.Count<3 then
  2092. Fail('rtl.module third param intf-function missing');
  2093. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2094. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2095. // search for $mod.$init or $mod.$main - the last statement
  2096. if IsProg or IsLib then
  2097. begin
  2098. InitName:='$main';
  2099. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2100. end
  2101. else
  2102. InitName:='$init';
  2103. InitAssign:=nil;
  2104. InitFunction:=nil;
  2105. FJSInitBody:=nil;
  2106. if JSModuleSrc.Statements.Count>0 then
  2107. begin
  2108. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2109. if LastNode is TJSSimpleAssignStatement then
  2110. begin
  2111. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2112. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2113. begin
  2114. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2115. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2116. end
  2117. else if IsProg or IsLib then
  2118. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2119. end;
  2120. end;
  2121. // optional: implementation uses section
  2122. if JSModuleCallArgs.Elements.Count<4 then
  2123. exit;
  2124. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2125. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2126. end;
  2127. procedure TCustomTestModule.ConvertProgram;
  2128. begin
  2129. Add('end.');
  2130. ParseProgram;
  2131. ConvertModule;
  2132. end;
  2133. procedure TCustomTestModule.ConvertLibrary;
  2134. begin
  2135. Add('end.');
  2136. ParseLibrary;
  2137. ConvertModule;
  2138. end;
  2139. procedure TCustomTestModule.ConvertUnit;
  2140. begin
  2141. Add('end.');
  2142. ParseUnit;
  2143. ConvertModule;
  2144. end;
  2145. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2146. begin
  2147. Result:=tcmodules.JSToStr(El);
  2148. end;
  2149. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2150. DottedName: string);
  2151. begin
  2152. if DottedName='' then
  2153. begin
  2154. AssertNull(Msg,El);
  2155. end
  2156. else
  2157. begin
  2158. AssertNotNull(Msg,El);
  2159. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2160. end;
  2161. end;
  2162. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2163. begin
  2164. if El=nil then
  2165. Result:=''
  2166. else if El is TJSPrimaryExpressionIdent then
  2167. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2168. else if El is TJSDotMemberExpression then
  2169. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2170. else
  2171. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2172. end;
  2173. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2174. InitStatements: string; ImplStatements: string);
  2175. var
  2176. ActualSrc, ExpectedSrc, InitName: String;
  2177. IsProg, IsLib: Boolean;
  2178. begin
  2179. ActualSrc:=JSToStr(JSModuleSrc);
  2180. if coUseStrict in Converter.Options then
  2181. ExpectedSrc:='"use strict";'+LineEnding
  2182. else
  2183. ExpectedSrc:='';
  2184. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2185. ExpectedSrc:=ExpectedSrc+Statements;
  2186. // unit implementation
  2187. if (Trim(ImplStatements)<>'') then
  2188. ExpectedSrc:=ExpectedSrc+LineEnding
  2189. +'$mod.$implcode = function () {'+LineEnding
  2190. +ImplStatements
  2191. +'};'+LineEnding;
  2192. // program main or unit initialization
  2193. IsProg:=false;
  2194. IsLib:=false;
  2195. if Module is TPasProgram then
  2196. IsProg:=true
  2197. else if Module is TPasLibrary then
  2198. IsLib:=true;
  2199. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2200. begin
  2201. if IsProg or IsLib then
  2202. InitName:='$main'
  2203. else
  2204. InitName:='$init';
  2205. ExpectedSrc:=ExpectedSrc+LineEnding
  2206. +'$mod.'+InitName+' = function () {'+LineEnding
  2207. +InitStatements
  2208. +'};'+LineEnding;
  2209. end;
  2210. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2211. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2212. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2213. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2214. end;
  2215. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2216. var
  2217. ActualSrc: String;
  2218. begin
  2219. ActualSrc:=JSToStr(JSModule);
  2220. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2221. end;
  2222. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2223. // search diff, ignore changes in spaces
  2224. var
  2225. s: string;
  2226. begin
  2227. if CheckSrcDiff(Expected,Actual,s) then exit;
  2228. Fail(Msg+': '+s);
  2229. end;
  2230. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2231. var
  2232. aResolver: TTestEnginePasResolver;
  2233. aConverter: TPasToJSConverter;
  2234. aJSModule: TJSSourceElements;
  2235. ActualSrc: String;
  2236. begin
  2237. aResolver:=GetResolver(Filename);
  2238. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2239. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2240. {$IFDEF VerbosePas2JS}
  2241. writeln('CheckUnit '+Filename+' converting ...');
  2242. {$ENDIF}
  2243. aConverter:=CreateConverter;
  2244. aJSModule:=nil;
  2245. try
  2246. try
  2247. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2248. except
  2249. on E: Exception do
  2250. HandleException(E);
  2251. end;
  2252. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2253. {$IFDEF VerbosePas2JS}
  2254. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2255. write(aResolver.Source);
  2256. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2257. write(ActualSrc);
  2258. {$ENDIF}
  2259. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2260. finally
  2261. aJSModule.Free;
  2262. aConverter.Free;
  2263. end;
  2264. end;
  2265. procedure TCustomTestModule.CheckReferenceDirectives;
  2266. var
  2267. CurFilename: string;
  2268. LineNumber: Integer;
  2269. SrcLine: String;
  2270. CommentStartP, CommentEndP: PChar;
  2271. procedure RaiseError(Msg: string; p: PChar);
  2272. begin
  2273. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2274. end;
  2275. procedure AddMarker(Marker: PSrcMarker);
  2276. begin
  2277. if LastSrcMarker<>nil then
  2278. LastSrcMarker^.Next:=Marker
  2279. else
  2280. FirstSrcMarker:=Marker;
  2281. LastSrcMarker:=Marker;
  2282. end;
  2283. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2284. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2285. begin
  2286. New(Result);
  2287. Result^.Kind:=Kind;
  2288. Result^.Filename:=aFilename;
  2289. Result^.Row:=aLine;
  2290. Result^.StartCol:=aStartCol;
  2291. Result^.EndCol:=aEndCol;
  2292. Result^.Identifier:=Identifier;
  2293. Result^.Next:=nil;
  2294. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2295. AddMarker(Result);
  2296. end;
  2297. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2298. const Identifier: string): PSrcMarker;
  2299. var
  2300. TokenStart, p: PChar;
  2301. begin
  2302. p:=CommentEndP;
  2303. ReadNextPascalToken(p,TokenStart,false,false);
  2304. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2305. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2306. end;
  2307. function ReadIdentifier(var p: PChar): string;
  2308. var
  2309. StartP: PChar;
  2310. begin
  2311. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2312. RaiseError('identifier expected',p);
  2313. StartP:=p;
  2314. inc(p);
  2315. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2316. Result:='';
  2317. SetLength(Result,p-StartP);
  2318. Move(StartP^,Result[1],length(Result));
  2319. end;
  2320. procedure AddLabel;
  2321. var
  2322. Identifier: String;
  2323. p: PChar;
  2324. begin
  2325. p:=CommentStartP+2;
  2326. Identifier:=ReadIdentifier(p);
  2327. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2328. if FindSrcLabel(Identifier)<>nil then
  2329. RaiseError('duplicate label "'+Identifier+'"',p);
  2330. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2331. end;
  2332. procedure AddResolverReference;
  2333. var
  2334. Identifier: String;
  2335. p: PChar;
  2336. begin
  2337. p:=CommentStartP+2;
  2338. Identifier:=ReadIdentifier(p);
  2339. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2340. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2341. end;
  2342. procedure AddDirectReference;
  2343. var
  2344. Identifier: String;
  2345. p: PChar;
  2346. begin
  2347. p:=CommentStartP+2;
  2348. Identifier:=ReadIdentifier(p);
  2349. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2350. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2351. end;
  2352. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2353. var
  2354. p: PChar;
  2355. IsDirective: Boolean;
  2356. begin
  2357. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2358. CurFilename:=aFilename;
  2359. // parse code, find all labels
  2360. LineNumber:=0;
  2361. while LineNumber<SrcLines.Count do
  2362. begin
  2363. inc(LineNumber);
  2364. SrcLine:=SrcLines[LineNumber-1];
  2365. if SrcLine='' then continue;
  2366. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2367. p:=PChar(SrcLine);
  2368. repeat
  2369. case p^ of
  2370. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2371. '{':
  2372. begin
  2373. CommentStartP:=p;
  2374. inc(p);
  2375. IsDirective:=p^ in ['#','@','='];
  2376. // skip to end of comment
  2377. repeat
  2378. case p^ of
  2379. #0:
  2380. if (p-PChar(SrcLine)=length(SrcLine)) then
  2381. begin
  2382. // multi line comment
  2383. if IsDirective then
  2384. RaiseError('directive missing closing bracket',CommentStartP);
  2385. repeat
  2386. inc(LineNumber);
  2387. if LineNumber>SrcLines.Count then exit;
  2388. SrcLine:=SrcLines[LineNumber-1];
  2389. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2390. until SrcLine<>'';
  2391. p:=PChar(SrcLine);
  2392. continue;
  2393. end;
  2394. '}':
  2395. begin
  2396. inc(p);
  2397. break;
  2398. end;
  2399. end;
  2400. inc(p);
  2401. until false;
  2402. CommentEndP:=p;
  2403. case CommentStartP[1] of
  2404. '#': AddLabel;
  2405. '@': AddResolverReference;
  2406. '=': AddDirectReference;
  2407. end;
  2408. p:=CommentEndP;
  2409. continue;
  2410. end;
  2411. '/':
  2412. if p[1]='/' then
  2413. break; // rest of line is comment -> skip
  2414. end;
  2415. inc(p);
  2416. until false;
  2417. end;
  2418. end;
  2419. procedure CheckResolverReference(aMarker: PSrcMarker);
  2420. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2421. var
  2422. aLabel: PSrcMarker;
  2423. ReferenceElements, LabelElements: TFPList;
  2424. i, j, aLine, aCol: Integer;
  2425. El, Ref, LabelEl: TPasElement;
  2426. begin
  2427. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2428. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2429. if aLabel=nil then
  2430. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2431. LabelElements:=nil;
  2432. ReferenceElements:=nil;
  2433. try
  2434. LabelElements:=FindElementsAt(aLabel);
  2435. ReferenceElements:=FindElementsAt(aMarker);
  2436. for i:=0 to ReferenceElements.Count-1 do
  2437. begin
  2438. El:=TPasElement(ReferenceElements[i]);
  2439. Ref:=nil;
  2440. if El.CustomData is TResolvedReference then
  2441. Ref:=TResolvedReference(El.CustomData).Declaration
  2442. else if El.CustomData is TPasPropertyScope then
  2443. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2444. else if El.CustomData is TPasSpecializeTypeData then
  2445. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2446. if Ref<>nil then
  2447. for j:=0 to LabelElements.Count-1 do
  2448. begin
  2449. LabelEl:=TPasElement(LabelElements[j]);
  2450. if Ref=LabelEl then
  2451. exit; // success
  2452. end;
  2453. end;
  2454. // failure write candidates
  2455. for i:=0 to ReferenceElements.Count-1 do
  2456. begin
  2457. El:=TPasElement(ReferenceElements[i]);
  2458. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2459. write(' El=',GetObjName(El));
  2460. if EL is TPrimitiveExpr then
  2461. begin
  2462. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2463. end;
  2464. Ref:=nil;
  2465. if El.CustomData is TResolvedReference then
  2466. Ref:=TResolvedReference(El.CustomData).Declaration
  2467. else if El.CustomData is TPasPropertyScope then
  2468. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2469. else if El.CustomData is TPasSpecializeTypeData then
  2470. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2471. if Ref<>nil then
  2472. begin
  2473. write(' Decl=',GetObjName(Ref));
  2474. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2475. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2476. end
  2477. else
  2478. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2479. writeln;
  2480. end;
  2481. for i:=0 to LabelElements.Count-1 do
  2482. begin
  2483. El:=TPasElement(LabelElements[i]);
  2484. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2485. write(' El=',GetObjName(El));
  2486. writeln;
  2487. end;
  2488. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2489. finally
  2490. LabelElements.Free;
  2491. ReferenceElements.Free;
  2492. end;
  2493. end;
  2494. procedure CheckDirectReference(aMarker: PSrcMarker);
  2495. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2496. var
  2497. aLabel: PSrcMarker;
  2498. ReferenceElements, LabelElements: TFPList;
  2499. i, LabelLine, LabelCol, j: Integer;
  2500. El, LabelEl: TPasElement;
  2501. DeclEl, TypeEl: TPasType;
  2502. begin
  2503. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2504. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2505. if aLabel=nil then
  2506. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2507. LabelElements:=nil;
  2508. ReferenceElements:=nil;
  2509. try
  2510. //writeln('CheckDirectReference finding elements at label ...');
  2511. LabelElements:=FindElementsAt(aLabel);
  2512. //writeln('CheckDirectReference finding elements at reference ...');
  2513. ReferenceElements:=FindElementsAt(aMarker);
  2514. for i:=0 to ReferenceElements.Count-1 do
  2515. begin
  2516. El:=TPasElement(ReferenceElements[i]);
  2517. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2518. if El.ClassType=TPasVariable then
  2519. begin
  2520. if TPasVariable(El).VarType=nil then
  2521. begin
  2522. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2523. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2524. end;
  2525. TypeEl:=TPasVariable(El).VarType;
  2526. for j:=0 to LabelElements.Count-1 do
  2527. begin
  2528. LabelEl:=TPasElement(LabelElements[j]);
  2529. if TypeEl=LabelEl then
  2530. exit; // success
  2531. end;
  2532. end
  2533. else if El is TPasAliasType then
  2534. begin
  2535. DeclEl:=TPasAliasType(El).DestType;
  2536. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2537. if (aLabel^.Filename=DeclEl.SourceFilename)
  2538. and (integer(aLabel^.Row)=LabelLine)
  2539. and (aLabel^.StartCol<=LabelCol)
  2540. and (aLabel^.EndCol>=LabelCol) then
  2541. exit; // success
  2542. end
  2543. else if El.ClassType=TPasArgument then
  2544. begin
  2545. TypeEl:=TPasArgument(El).ArgType;
  2546. for j:=0 to LabelElements.Count-1 do
  2547. begin
  2548. LabelEl:=TPasElement(LabelElements[j]);
  2549. if TypeEl=LabelEl then
  2550. exit; // success
  2551. end;
  2552. end;
  2553. end;
  2554. // failed -> show candidates
  2555. writeln('CheckDirectReference failed: Labels:');
  2556. for j:=0 to LabelElements.Count-1 do
  2557. begin
  2558. LabelEl:=TPasElement(LabelElements[j]);
  2559. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2560. end;
  2561. writeln('CheckDirectReference failed: References:');
  2562. for i:=0 to ReferenceElements.Count-1 do
  2563. begin
  2564. El:=TPasElement(ReferenceElements[i]);
  2565. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2566. //if EL is TPasVariable then
  2567. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2568. end;
  2569. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2570. finally
  2571. LabelElements.Free;
  2572. ReferenceElements.Free;
  2573. end;
  2574. end;
  2575. var
  2576. aMarker: PSrcMarker;
  2577. i: Integer;
  2578. SrcLines: TStringList;
  2579. begin
  2580. Module.ForEachCall(@OnCheckElementParent,nil);
  2581. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2582. // find all markers
  2583. for i:=0 to FileResolver.Streams.Count-1 do
  2584. begin
  2585. GetSrc(i,SrcLines,CurFilename);
  2586. ParseCode(SrcLines,CurFilename);
  2587. SrcLines.Free;
  2588. end;
  2589. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2590. // check references
  2591. aMarker:=FirstSrcMarker;
  2592. while aMarker<>nil do
  2593. begin
  2594. case aMarker^.Kind of
  2595. mkResolverReference: CheckResolverReference(aMarker);
  2596. mkDirectReference: CheckDirectReference(aMarker);
  2597. end;
  2598. aMarker:=aMarker^.Next;
  2599. end;
  2600. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2601. end;
  2602. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2603. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2604. var
  2605. i: Integer;
  2606. Item: TTestHintMessage;
  2607. Expected,Actual: string;
  2608. begin
  2609. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2610. for i:=0 to MsgCount-1 do
  2611. begin
  2612. Item:=Msgs[i];
  2613. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2614. if (Marker<>nil) then
  2615. begin
  2616. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2617. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2618. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2619. end;
  2620. // found
  2621. FHintMsgsGood.Add(Item);
  2622. str(Item.MsgType,Actual);
  2623. str(MsgType,Expected);
  2624. AssertEquals('MsgType',Expected,Actual);
  2625. exit;
  2626. end;
  2627. // needed message missing -> show emitted messages
  2628. WriteSources('',0,0);
  2629. for i:=0 to MsgCount-1 do
  2630. begin
  2631. Item:=Msgs[i];
  2632. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2633. ' ('+IntToStr(Item.MsgNumber),')');
  2634. if Marker<>nil then
  2635. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2636. writeln(' {',Item.Msg,'}');
  2637. end;
  2638. str(MsgType,Expected);
  2639. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2640. if Marker<>nil then
  2641. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2642. Actual:=Actual+' '+Msg;
  2643. Fail(Actual);
  2644. end;
  2645. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2646. );
  2647. var
  2648. i: Integer;
  2649. s, Txt: String;
  2650. Msg: TTestHintMessage;
  2651. begin
  2652. for i:=0 to MsgCount-1 do
  2653. begin
  2654. Msg:=Msgs[i];
  2655. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2656. s:='';
  2657. str(Msg.MsgType,s);
  2658. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2659. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2660. if WithSourcePos then
  2661. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2662. Txt:=Txt+' {'+Msg.Msg+'}';
  2663. Fail(Txt);
  2664. end;
  2665. end;
  2666. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2667. MsgNumber: integer);
  2668. begin
  2669. ExpectedErrorClass:=EScannerError;
  2670. ExpectedErrorMsg:=Msg;
  2671. ExpectedErrorNumber:=MsgNumber;
  2672. end;
  2673. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2674. MsgNumber: integer);
  2675. begin
  2676. ExpectedErrorClass:=EParserError;
  2677. ExpectedErrorMsg:=Msg;
  2678. ExpectedErrorNumber:=MsgNumber;
  2679. end;
  2680. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2681. MsgNumber: integer);
  2682. begin
  2683. ExpectedErrorClass:=EPasResolve;
  2684. ExpectedErrorMsg:=Msg;
  2685. ExpectedErrorNumber:=MsgNumber;
  2686. end;
  2687. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2688. MsgNumber: integer);
  2689. begin
  2690. ExpectedErrorClass:=EPas2JS;
  2691. ExpectedErrorMsg:=Msg;
  2692. ExpectedErrorNumber:=MsgNumber;
  2693. end;
  2694. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2695. var
  2696. MsgNumber: Integer;
  2697. Msg: String;
  2698. begin
  2699. Result:=false;
  2700. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2701. Msg:=E.Message;
  2702. if E is EPas2JS then
  2703. MsgNumber:=EPas2JS(E).MsgNumber
  2704. else if E is EPasResolve then
  2705. MsgNumber:=EPasResolve(E).MsgNumber
  2706. else if E is EParserError then
  2707. MsgNumber:=Parser.LastMsgNumber
  2708. else if E is EScannerError then
  2709. begin
  2710. MsgNumber:=Scanner.LastMsgNumber;
  2711. Msg:=Scanner.LastMsg;
  2712. end
  2713. else
  2714. MsgNumber:=0;
  2715. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2716. if Result then
  2717. SkipTests:=true;
  2718. end;
  2719. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2720. const aFilename: string; aRow, aCol: integer);
  2721. var
  2722. s: String;
  2723. begin
  2724. WriteSources(aFilename,aRow,aCol);
  2725. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2726. writeln('ERROR: ',s);
  2727. Fail(s);
  2728. end;
  2729. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2730. aMarker: PSrcMarker);
  2731. begin
  2732. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2733. end;
  2734. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2735. begin
  2736. if IsErrorExpected(E) then exit;
  2737. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2738. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2739. +' '+Scanner.CurFilename
  2740. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2741. FailException(E);
  2742. end;
  2743. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2744. begin
  2745. if IsErrorExpected(E) then exit;
  2746. WriteSources(E.Filename,E.Row,E.Column);
  2747. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2748. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2749. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2750. );
  2751. FailException(E);
  2752. end;
  2753. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2754. var
  2755. P: TPasSourcePos;
  2756. begin
  2757. if IsErrorExpected(E) then exit;
  2758. P:=E.SourcePos;
  2759. WriteSources(P.FileName,P.Row,P.Column);
  2760. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2761. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2762. FailException(E);
  2763. end;
  2764. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2765. var
  2766. Row, Col: integer;
  2767. begin
  2768. if IsErrorExpected(E) then exit;
  2769. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2770. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2771. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2772. +' '+E.PasElement.SourceFilename
  2773. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2774. FailException(E);
  2775. end;
  2776. procedure TCustomTestModule.HandleException(E: Exception);
  2777. begin
  2778. if E is EScannerError then
  2779. HandleScannerError(EScannerError(E))
  2780. else if E is EParserError then
  2781. HandleParserError(EParserError(E))
  2782. else if E is EPasResolve then
  2783. HandlePasResolveError(EPasResolve(E))
  2784. else if E is EPas2JS then
  2785. HandlePas2JSError(EPas2JS(E))
  2786. else
  2787. begin
  2788. if IsErrorExpected(E) then exit;
  2789. if not (E is EAssertionFailedError) then
  2790. begin
  2791. WriteSources('',0,0);
  2792. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2793. end;
  2794. FailException(E);
  2795. end;
  2796. end;
  2797. procedure TCustomTestModule.FailException(E: Exception);
  2798. var
  2799. MsgNumber: Integer;
  2800. begin
  2801. if ExpectedErrorClass<>nil then
  2802. begin
  2803. if FExpectedErrorClass=E.ClassType then
  2804. begin
  2805. if E is EPas2JS then
  2806. MsgNumber:=EPas2JS(E).MsgNumber
  2807. else if E is EPasResolve then
  2808. MsgNumber:=EPasResolve(E).MsgNumber
  2809. else if E is EParserError then
  2810. MsgNumber:=Parser.LastMsgNumber
  2811. else if E is EScannerError then
  2812. MsgNumber:=Scanner.LastMsgNumber
  2813. else
  2814. MsgNumber:=0;
  2815. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2816. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2817. ExpectedErrorNumber,MsgNumber);
  2818. end else begin
  2819. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2820. end;
  2821. end;
  2822. Fail(E.Message);
  2823. end;
  2824. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2825. aCol: integer);
  2826. var
  2827. IsSrc: Boolean;
  2828. i, j: Integer;
  2829. SrcLines: TStringList;
  2830. Line: string;
  2831. aModule: TTestEnginePasResolver;
  2832. begin
  2833. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2834. for i:=0 to ResolverCount-1 do
  2835. begin
  2836. aModule:=Resolvers[i];
  2837. SrcLines:=TStringList.Create;
  2838. try
  2839. SrcLines.Text:=aModule.Source;
  2840. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2841. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2842. for j:=1 to SrcLines.Count do
  2843. begin
  2844. Line:=SrcLines[j-1];
  2845. if IsSrc and (j=aRow) then
  2846. begin
  2847. write('*');
  2848. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2849. end;
  2850. writeln(Format('%:4d: ',[j]),Line);
  2851. end;
  2852. finally
  2853. SrcLines.Free;
  2854. end;
  2855. end;
  2856. end;
  2857. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2858. var
  2859. i: Integer;
  2860. begin
  2861. for i:=0 to ResolverCount-1 do
  2862. if Filename=Resolvers[i].Filename then exit(i);
  2863. Result:=-1;
  2864. end;
  2865. function TCustomTestModule.GetResolver(const Filename: string
  2866. ): TTestEnginePasResolver;
  2867. var
  2868. i: Integer;
  2869. begin
  2870. i:=IndexOfResolver(Filename);
  2871. if i<0 then exit(nil);
  2872. Result:=Resolvers[i];
  2873. end;
  2874. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2875. out aFilename: string);
  2876. var
  2877. aStream: TStream;
  2878. begin
  2879. SrcLines:=TStringList.Create;
  2880. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2881. aStream.Position:=0;
  2882. SrcLines.LoadFromStream(aStream);
  2883. aFilename:=FileResolver.Streams[Index];
  2884. end;
  2885. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2886. aEndCol: integer): TFPList;
  2887. var
  2888. ok: Boolean;
  2889. FoundRefs: TTestResolverReferenceData;
  2890. i: Integer;
  2891. CurResolver: TTestEnginePasResolver;
  2892. begin
  2893. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2894. FoundRefs:=Default(TTestResolverReferenceData);
  2895. FoundRefs.Filename:=aFilename;
  2896. FoundRefs.Row:=aLine;
  2897. FoundRefs.StartCol:=aStartCol;
  2898. FoundRefs.EndCol:=aEndCol;
  2899. FoundRefs.Found:=TFPList.Create;
  2900. ok:=false;
  2901. try
  2902. // find all markers
  2903. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2904. for i:=0 to ResolverCount-1 do
  2905. begin
  2906. CurResolver:=Resolvers[i];
  2907. if CurResolver.Module=Module then continue;
  2908. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2909. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2910. end;
  2911. ok:=true;
  2912. finally
  2913. if not ok then
  2914. FreeAndNil(FoundRefs.Found);
  2915. end;
  2916. Result:=FoundRefs.Found;
  2917. FoundRefs.Found:=nil;
  2918. end;
  2919. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2920. ErrorOnNoElements: boolean): TFPList;
  2921. begin
  2922. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2923. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2924. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2925. end;
  2926. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2927. begin
  2928. Result:=FirstSrcMarker;
  2929. while Result<>nil do
  2930. begin
  2931. if (Result^.Kind=mkLabel)
  2932. and (CompareText(Result^.Identifier,Identifier)=0) then
  2933. exit;
  2934. Result:=Result^.Next;
  2935. end;
  2936. end;
  2937. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2938. ErrorOnNoElements: boolean): TFPList;
  2939. var
  2940. SrcLabel: PSrcMarker;
  2941. begin
  2942. SrcLabel:=FindSrcLabel(Identifier);
  2943. if SrcLabel=nil then
  2944. Fail('missing label "'+Identifier+'"');
  2945. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2946. end;
  2947. function TCustomTestModule.GetDefaultNamespace: string;
  2948. var
  2949. C: TClass;
  2950. begin
  2951. Result:='';
  2952. if FModule=nil then exit;
  2953. C:=FModule.ClassType;
  2954. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2955. Result:=ResolverEngine.DefaultNameSpace;
  2956. end;
  2957. constructor TCustomTestModule.Create;
  2958. begin
  2959. inherited Create;
  2960. FHintMsgs:=TObjectList.Create(true);
  2961. FHintMsgsGood:=TFPList.Create;
  2962. end;
  2963. destructor TCustomTestModule.Destroy;
  2964. begin
  2965. FreeAndNil(FHintMsgs);
  2966. FreeAndNil(FHintMsgsGood);
  2967. inherited Destroy;
  2968. end;
  2969. { TTestModule }
  2970. procedure TTestModule.TestReservedWords;
  2971. var
  2972. i: integer;
  2973. begin
  2974. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2975. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2976. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2977. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2978. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2979. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2980. end;
  2981. procedure TTestModule.TestEmptyProgram;
  2982. begin
  2983. StartProgram(false);
  2984. Add('begin');
  2985. ConvertProgram;
  2986. CheckSource('TestEmptyProgram','','');
  2987. end;
  2988. procedure TTestModule.TestEmptyProgramUseStrict;
  2989. begin
  2990. Converter.Options:=Converter.Options+[coUseStrict];
  2991. StartProgram(false);
  2992. Add('begin');
  2993. ConvertProgram;
  2994. CheckSource('TestEmptyProgramUseStrict','','');
  2995. end;
  2996. procedure TTestModule.TestEmptyUnit;
  2997. begin
  2998. StartUnit(false);
  2999. Add('interface');
  3000. Add('implementation');
  3001. ConvertUnit;
  3002. CheckSource('TestEmptyUnit',
  3003. LinesToStr([
  3004. ]),
  3005. '');
  3006. end;
  3007. procedure TTestModule.TestEmptyUnitUseStrict;
  3008. begin
  3009. Converter.Options:=Converter.Options+[coUseStrict];
  3010. StartUnit(false);
  3011. Add('interface');
  3012. Add('implementation');
  3013. ConvertUnit;
  3014. CheckSource('TestEmptyUnitUseStrict',
  3015. LinesToStr([
  3016. ''
  3017. ]),
  3018. '');
  3019. end;
  3020. procedure TTestModule.TestDottedUnitNames;
  3021. begin
  3022. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3023. LinesToStr([
  3024. 'var iV: longint;'
  3025. ]),
  3026. '');
  3027. FFilename:='ns1.test1.pp';
  3028. StartProgram(true);
  3029. Add('uses unIt2;');
  3030. Add('var');
  3031. Add(' i: longint;');
  3032. Add('begin');
  3033. Add(' i:=iv;');
  3034. Add(' i:=uNit2.iv;');
  3035. Add(' i:=Ns1.TEst1.i;');
  3036. ConvertProgram;
  3037. CheckSource('TestDottedUnitNames',
  3038. LinesToStr([
  3039. 'this.i = 0;',
  3040. '']),
  3041. LinesToStr([ // this.$init
  3042. '$mod.i = pas["NS1.Unit2"].iV;',
  3043. '$mod.i = pas["NS1.Unit2"].iV;',
  3044. '$mod.i = $mod.i;',
  3045. '']) );
  3046. end;
  3047. procedure TTestModule.TestDottedUnitNameImpl;
  3048. begin
  3049. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3050. LinesToStr([
  3051. 'type',
  3052. ' TObject = class end;',
  3053. ' TTestA = class',
  3054. ' end;'
  3055. ]),
  3056. LinesToStr(['uses TEST.UnitB;'])
  3057. );
  3058. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3059. LinesToStr([
  3060. 'uses TEST.UnitA;',
  3061. 'type TTestB = class(TTestA);'
  3062. ]),
  3063. ''
  3064. );
  3065. StartProgram(true);
  3066. Add('uses TEST.UnitA;');
  3067. Add('begin');
  3068. ConvertProgram;
  3069. CheckSource('TestDottedUnitNameImpl',
  3070. LinesToStr([
  3071. '']),
  3072. LinesToStr([ // this.$init
  3073. '']) );
  3074. CheckUnit('TEST.UnitA.pas',
  3075. LinesToStr([
  3076. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3077. ' var $mod = this;',
  3078. ' rtl.createClass(this, "TObject", null, function () {',
  3079. ' this.$init = function () {',
  3080. ' };',
  3081. ' this.$final = function () {',
  3082. ' };',
  3083. ' });',
  3084. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3085. ' });',
  3086. '}, ["TEST.UnitB"]);'
  3087. ]));
  3088. CheckUnit('TEST.UnitB.pas',
  3089. LinesToStr([
  3090. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3091. ' var $mod = this;',
  3092. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3093. ' });',
  3094. '});'
  3095. ]));
  3096. end;
  3097. procedure TTestModule.TestDottedUnitExpr;
  3098. begin
  3099. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3100. LinesToStr([
  3101. 'procedure DoIt;'
  3102. ]),
  3103. 'procedure DoIt; begin end;');
  3104. FFilename:='Ns1.SubNs1.Test1.pp';
  3105. StartProgram(true);
  3106. Add('uses Ns2.sUbnS2.unIt2;');
  3107. Add('var');
  3108. Add(' i: longint;');
  3109. Add('begin');
  3110. Add(' ns2.subns2.unit2.doit;');
  3111. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3112. ConvertProgram;
  3113. CheckSource('TestDottedUnitExpr',
  3114. LinesToStr([
  3115. 'this.i = 0;',
  3116. '']),
  3117. LinesToStr([ // this.$init
  3118. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3119. '$mod.i = $mod.i;',
  3120. '']) );
  3121. end;
  3122. procedure TTestModule.Test_ModeFPCFail;
  3123. begin
  3124. StartProgram(false);
  3125. Add('{$mode FPC}');
  3126. Add('begin');
  3127. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3128. ConvertProgram;
  3129. end;
  3130. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3131. begin
  3132. StartProgram(false);
  3133. Add('{$modeswitch cblocks-}');
  3134. Add('begin');
  3135. ConvertProgram;
  3136. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3137. CheckResolverUnexpectedHints();
  3138. end;
  3139. procedure TTestModule.TestUnit_UseSystem;
  3140. begin
  3141. StartUnit(true);
  3142. Add([
  3143. 'interface',
  3144. 'var i: integer;',
  3145. 'implementation']);
  3146. ConvertUnit;
  3147. CheckSource('TestUnit_UseSystem',
  3148. LinesToStr([
  3149. 'this.i = 0;',
  3150. '']),
  3151. LinesToStr([
  3152. '']) );
  3153. end;
  3154. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3155. begin
  3156. AddModuleWithIntfImplSrc('unit1.pp',
  3157. LinesToStr([
  3158. 'type number = longint;']),
  3159. LinesToStr([
  3160. 'uses test1;',
  3161. 'procedure DoIt;',
  3162. 'begin',
  3163. ' i:=3;',
  3164. 'end;']));
  3165. StartUnit(true);
  3166. Add([
  3167. 'interface',
  3168. 'uses unit1;',
  3169. 'var i: number;',
  3170. 'implementation']);
  3171. ConvertUnit;
  3172. CheckSource('TestUnit_Intf1Impl2Intf1',
  3173. LinesToStr([
  3174. 'this.i = 0;',
  3175. '']),
  3176. LinesToStr([
  3177. '']) );
  3178. end;
  3179. procedure TTestModule.TestIncludeVersion;
  3180. begin
  3181. StartProgram(false);
  3182. Add([
  3183. 'var',
  3184. ' s: string;',
  3185. ' i: word;',
  3186. 'begin',
  3187. ' s:={$I %line%};',
  3188. ' i:={$I %linenum%};',
  3189. ' s:={$I %currentroutine%};',
  3190. ' s:={$I %pas2jsversion%};',
  3191. ' s:={$I %pas2jstarget%};',
  3192. ' s:={$I %pas2jstargetos%};',
  3193. ' s:={$I %pas2jstargetcpu%};',
  3194. ' s:={$I %file%};',
  3195. '']);
  3196. ConvertProgram;
  3197. CheckSource('TestIncludeVersion',
  3198. LinesToStr([
  3199. 'this.s="";',
  3200. 'this.i = 0;']),
  3201. LinesToStr([
  3202. '$mod.s = "7";',
  3203. '$mod.i = 8;',
  3204. '$mod.s = "<anonymous>";',
  3205. '$mod.s = "Comp.Ver.tcmodules";',
  3206. '$mod.s = "Browser";',
  3207. '$mod.s = "Browser";',
  3208. '$mod.s = "ECMAScript5";',
  3209. '$mod.s = "test1.pp";',
  3210. '']));
  3211. end;
  3212. procedure TTestModule.TestVarInt;
  3213. begin
  3214. StartProgram(false);
  3215. Add('var MyI: longint;');
  3216. Add('begin');
  3217. ConvertProgram;
  3218. CheckSource('TestVarInt','this.MyI=0;','');
  3219. end;
  3220. procedure TTestModule.TestVarBaseTypes;
  3221. begin
  3222. StartProgram(false);
  3223. Add('var');
  3224. Add(' i: longint;');
  3225. Add(' s: string;');
  3226. Add(' c: char;');
  3227. Add(' b: boolean;');
  3228. Add(' d: double;');
  3229. Add(' i2: longint = 3;');
  3230. Add(' s2: string = ''foo'';');
  3231. Add(' c2: char = ''4'';');
  3232. Add(' b2: boolean = true;');
  3233. Add(' d2: double = 5.6;');
  3234. Add(' i3: longint = $707;');
  3235. Add(' i4: nativeint = 9007199254740991;');
  3236. Add(' i5: nativeint = -9007199254740991-1;');
  3237. Add(' i6: nativeint = $fffffffffffff;');
  3238. Add(' i7: nativeint = -$fffffffffffff-1;');
  3239. Add(' i8: byte = 00;');
  3240. Add(' u8: nativeuint = $fffffffffffff;');
  3241. Add(' u9: nativeuint = $0000000000000;');
  3242. Add(' u10: nativeuint = $00ff00;');
  3243. Add('begin');
  3244. ConvertProgram;
  3245. CheckSource('TestVarBaseTypes',
  3246. LinesToStr([
  3247. 'this.i = 0;',
  3248. 'this.s = "";',
  3249. 'this.c = "";',
  3250. 'this.b = false;',
  3251. 'this.d = 0.0;',
  3252. 'this.i2 = 3;',
  3253. 'this.s2 = "foo";',
  3254. 'this.c2 = "4";',
  3255. 'this.b2 = true;',
  3256. 'this.d2 = 5.6;',
  3257. 'this.i3 = 0x707;',
  3258. 'this.i4 = 9007199254740991;',
  3259. 'this.i5 = -9007199254740991-1;',
  3260. 'this.i6 = 0xfffffffffffff;',
  3261. 'this.i7 =-0xfffffffffffff-1;',
  3262. 'this.i8 = 0;',
  3263. 'this.u8 = 0xfffffffffffff;',
  3264. 'this.u9 = 0x0;',
  3265. 'this.u10 = 0xff00;'
  3266. ]),
  3267. '');
  3268. end;
  3269. procedure TTestModule.TestBaseTypeSingleFail;
  3270. begin
  3271. StartProgram(false);
  3272. Add('var s: single;');
  3273. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3274. ConvertProgram;
  3275. end;
  3276. procedure TTestModule.TestBaseTypeExtendedFail;
  3277. begin
  3278. StartProgram(false);
  3279. Add('var e: extended;');
  3280. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3281. ConvertProgram;
  3282. end;
  3283. procedure TTestModule.TestConstBaseTypes;
  3284. begin
  3285. StartProgram(false);
  3286. Add('const');
  3287. Add(' i: longint = 3;');
  3288. Add(' s: string = ''foo'';');
  3289. Add(' c: char = ''4'';');
  3290. Add(' b: boolean = true;');
  3291. Add(' d: double = 5.6;');
  3292. Add(' e = low(word);');
  3293. Add(' f = high(word);');
  3294. Add('begin');
  3295. ConvertProgram;
  3296. CheckSource('TestVarBaseTypes',
  3297. LinesToStr([
  3298. 'this.i=3;',
  3299. 'this.s="foo";',
  3300. 'this.c="4";',
  3301. 'this.b=true;',
  3302. 'this.d=5.6;',
  3303. 'this.e = 0;',
  3304. 'this.f = 65535;'
  3305. ]),
  3306. '');
  3307. end;
  3308. procedure TTestModule.TestAliasTypeRef;
  3309. begin
  3310. StartProgram(false);
  3311. Add('type');
  3312. Add(' a=longint;');
  3313. Add(' b=a;');
  3314. Add('var');
  3315. Add(' c: A;');
  3316. Add(' d: B;');
  3317. Add('begin');
  3318. ConvertProgram;
  3319. CheckSource('TestAliasTypeRef',
  3320. LinesToStr([ // statements
  3321. 'this.c = 0;',
  3322. 'this.d = 0;'
  3323. ]),
  3324. LinesToStr([ // this.$main
  3325. ''
  3326. ]));
  3327. end;
  3328. procedure TTestModule.TestTypeCast_BaseTypes;
  3329. begin
  3330. StartProgram(false);
  3331. Add([
  3332. 'var',
  3333. ' i: longint;',
  3334. ' b: boolean;',
  3335. ' d: double;',
  3336. ' s: string;',
  3337. ' c: char;',
  3338. 'begin',
  3339. ' i:=longint(i);',
  3340. ' i:=longint(b);',
  3341. ' b:=boolean(b);',
  3342. ' b:=boolean(i);',
  3343. ' d:=double(d);',
  3344. ' d:=double(i);',
  3345. ' s:=string(s);',
  3346. ' s:=string(c);',
  3347. ' c:=char(c);',
  3348. ' c:=char(i);',
  3349. ' c:=char(65);',
  3350. ' c:=char(#10);',
  3351. ' c:=char(#$E000);',
  3352. '']);
  3353. ConvertProgram;
  3354. CheckSource('TestAliasTypeRef',
  3355. LinesToStr([ // statements
  3356. 'this.i = 0;',
  3357. 'this.b = false;',
  3358. 'this.d = 0.0;',
  3359. 'this.s = "";',
  3360. 'this.c = "";',
  3361. '']),
  3362. LinesToStr([ // this.$main
  3363. '$mod.i = $mod.i;',
  3364. '$mod.i = ($mod.b ? 1 : 0);',
  3365. '$mod.b = $mod.b;',
  3366. '$mod.b = $mod.i != 0;',
  3367. '$mod.d = $mod.d;',
  3368. '$mod.d = $mod.i;',
  3369. '$mod.s = $mod.s;',
  3370. '$mod.s = $mod.c;',
  3371. '$mod.c = $mod.c;',
  3372. '$mod.c = String.fromCharCode($mod.i);',
  3373. '$mod.c = "A";',
  3374. '$mod.c = "\n";',
  3375. '$mod.c = "";',
  3376. '']));
  3377. end;
  3378. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3379. begin
  3380. StartProgram(false);
  3381. Add('type');
  3382. Add(' integer = longint;');
  3383. Add(' TYesNo = boolean;');
  3384. Add(' TFloat = double;');
  3385. Add(' TCaption = string;');
  3386. Add(' TChar = char;');
  3387. Add('var');
  3388. Add(' i: integer;');
  3389. Add(' b: TYesNo;');
  3390. Add(' d: TFloat;');
  3391. Add(' s: TCaption;');
  3392. Add(' c: TChar;');
  3393. Add('begin');
  3394. Add(' i:=integer(i);');
  3395. Add(' i:=integer(b);');
  3396. Add(' b:=TYesNo(b);');
  3397. Add(' b:=TYesNo(i);');
  3398. Add(' d:=TFloat(d);');
  3399. Add(' d:=TFloat(i);');
  3400. Add(' s:=TCaption(s);');
  3401. Add(' s:=TCaption(c);');
  3402. Add(' c:=TChar(c);');
  3403. ConvertProgram;
  3404. CheckSource('TestAliasTypeRef',
  3405. LinesToStr([ // statements
  3406. 'this.i = 0;',
  3407. 'this.b = false;',
  3408. 'this.d = 0.0;',
  3409. 'this.s = "";',
  3410. 'this.c = "";',
  3411. '']),
  3412. LinesToStr([ // this.$main
  3413. '$mod.i = $mod.i;',
  3414. '$mod.i = ($mod.b ? 1 : 0);',
  3415. '$mod.b = $mod.b;',
  3416. '$mod.b = $mod.i != 0;',
  3417. '$mod.d = $mod.d;',
  3418. '$mod.d = $mod.i;',
  3419. '$mod.s = $mod.s;',
  3420. '$mod.s = $mod.c;',
  3421. '$mod.c = $mod.c;',
  3422. '']));
  3423. end;
  3424. procedure TTestModule.TestEmptyProc;
  3425. begin
  3426. StartProgram(false);
  3427. Add('procedure Test;');
  3428. Add('begin');
  3429. Add('end;');
  3430. Add('begin');
  3431. ConvertProgram;
  3432. CheckSource('TestEmptyProc',
  3433. LinesToStr([ // statements
  3434. 'this.Test = function () {',
  3435. '};'
  3436. ]),
  3437. LinesToStr([ // this.$main
  3438. ''
  3439. ]));
  3440. end;
  3441. procedure TTestModule.TestProcOneParam;
  3442. begin
  3443. StartProgram(false);
  3444. Add('procedure ProcA(i: longint);');
  3445. Add('begin');
  3446. Add('end;');
  3447. Add('begin');
  3448. Add(' PROCA(3);');
  3449. ConvertProgram;
  3450. CheckSource('TestProcOneParam',
  3451. LinesToStr([ // statements
  3452. 'this.ProcA = function (i) {',
  3453. '};'
  3454. ]),
  3455. LinesToStr([ // this.$main
  3456. '$mod.ProcA(3);'
  3457. ]));
  3458. end;
  3459. procedure TTestModule.TestFunctionWithoutParams;
  3460. begin
  3461. StartProgram(false);
  3462. Add('function FuncA: longint;');
  3463. Add('begin');
  3464. Add('end;');
  3465. Add('var i: longint;');
  3466. Add('begin');
  3467. Add(' I:=FUNCA();');
  3468. Add(' I:=FUNCA;');
  3469. Add(' FUNCA();');
  3470. Add(' FUNCA;');
  3471. ConvertProgram;
  3472. CheckSource('TestProcWithoutParams',
  3473. LinesToStr([ // statements
  3474. 'this.FuncA = function () {',
  3475. ' var Result = 0;',
  3476. ' return Result;',
  3477. '};',
  3478. 'this.i=0;'
  3479. ]),
  3480. LinesToStr([ // this.$main
  3481. '$mod.i=$mod.FuncA();',
  3482. '$mod.i=$mod.FuncA();',
  3483. '$mod.FuncA();',
  3484. '$mod.FuncA();'
  3485. ]));
  3486. end;
  3487. procedure TTestModule.TestProcedureWithoutParams;
  3488. begin
  3489. StartProgram(false);
  3490. Add('procedure ProcA;');
  3491. Add('begin');
  3492. Add('end;');
  3493. Add('begin');
  3494. Add(' PROCA();');
  3495. Add(' PROCA;');
  3496. ConvertProgram;
  3497. CheckSource('TestProcWithoutParams',
  3498. LinesToStr([ // statements
  3499. 'this.ProcA = function () {',
  3500. '};'
  3501. ]),
  3502. LinesToStr([ // this.$main
  3503. '$mod.ProcA();',
  3504. '$mod.ProcA();'
  3505. ]));
  3506. end;
  3507. procedure TTestModule.TestIncDec;
  3508. begin
  3509. StartProgram(false);
  3510. Add([
  3511. 'procedure DoIt(var i: longint);',
  3512. 'begin',
  3513. ' inc(i);',
  3514. ' inc(i,2);',
  3515. 'end;',
  3516. 'var',
  3517. ' Bar: longint;',
  3518. 'begin',
  3519. ' inc(bar);',
  3520. ' inc(bar,2);',
  3521. ' dec(bar);',
  3522. ' dec(bar,3);',
  3523. '']);
  3524. ConvertProgram;
  3525. CheckSource('TestIncDec',
  3526. LinesToStr([ // statements
  3527. 'this.DoIt = function (i) {',
  3528. ' i.set(i.get()+1);',
  3529. ' i.set(i.get()+2);',
  3530. '};',
  3531. 'this.Bar = 0;'
  3532. ]),
  3533. LinesToStr([ // this.$main
  3534. '$mod.Bar+=1;',
  3535. '$mod.Bar+=2;',
  3536. '$mod.Bar-=1;',
  3537. '$mod.Bar-=3;'
  3538. ]));
  3539. end;
  3540. procedure TTestModule.TestLoHiFpcMode;
  3541. begin
  3542. StartProgram(false);
  3543. Add([
  3544. '{$mode objfpc}',
  3545. 'const',
  3546. ' LoByte1 = Lo(Word($1234));',
  3547. ' HiByte1 = Hi(Word($1234));',
  3548. ' LoByte2 = Lo(SmallInt($1234));',
  3549. ' HiByte2 = Hi(SmallInt($1234));',
  3550. ' LoWord1 = Lo($1234CDEF);',
  3551. ' HiWord1 = Hi($1234CDEF);',
  3552. ' LoWord2 = Lo(-$1234CDEF);',
  3553. ' HiWord2 = Hi(-$1234CDEF);',
  3554. ' lo4:byte=lo(byte($34));',
  3555. ' hi4:byte=hi(byte($34));',
  3556. ' lo5:byte=lo(shortint(-$34));',
  3557. ' hi5:byte=hi(shortint(-$34));',
  3558. ' lo6:longword=lo($123456789ABCD);',
  3559. ' hi6:longword=hi($123456789ABCD);',
  3560. ' lo7:longword=lo(-$123456789ABCD);',
  3561. ' hi7:longword=hi(-$123456789ABCD);',
  3562. 'var',
  3563. ' b: Byte;',
  3564. ' ss: shortint;',
  3565. ' w: Word;',
  3566. ' si: SmallInt;',
  3567. ' lw: LongWord;',
  3568. ' li: LongInt;',
  3569. ' b2: Byte;',
  3570. ' ni: nativeint;',
  3571. 'begin',
  3572. ' w := $1234;',
  3573. ' ss := -$12;',
  3574. ' b := lo(ss);',
  3575. ' b := HI(ss);',
  3576. ' b := lo(w);',
  3577. ' b := HI(w);',
  3578. ' b2 := lo(b);',
  3579. ' b2 := hi(b);',
  3580. ' lw := $1234CDEF;',
  3581. ' w := lo(lw);',
  3582. ' w := hi(lw);',
  3583. ' ni := $123456789ABCD;',
  3584. ' lw := lo(ni);',
  3585. ' lw := hi(ni);',
  3586. '']);
  3587. ConvertProgram;
  3588. CheckSource('TestLoHiFpcMode',
  3589. LinesToStr([ // statements
  3590. 'this.LoByte1 = 0x1234 & 0xFF;',
  3591. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3592. 'this.LoByte2 = 0x1234 & 0xFF;',
  3593. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3594. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3595. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3596. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3597. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3598. 'this.lo4 = 0x34 & 0xF;',
  3599. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3600. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3601. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3602. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3603. 'this.hi6 = 74565 >>> 0;',
  3604. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3605. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3606. 'this.b = 0;',
  3607. 'this.ss = 0;',
  3608. 'this.w = 0;',
  3609. 'this.si = 0;',
  3610. 'this.lw = 0;',
  3611. 'this.li = 0;',
  3612. 'this.b2 = 0;',
  3613. 'this.ni = 0;',
  3614. '']),
  3615. LinesToStr([ // this.$main
  3616. '$mod.w = 0x1234;',
  3617. '$mod.ss = -0x12;',
  3618. '$mod.b = $mod.ss & 0xFF;',
  3619. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3620. '$mod.b = $mod.w & 0xFF;',
  3621. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3622. '$mod.b2 = $mod.b & 0xF;',
  3623. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3624. '$mod.lw = 0x1234CDEF;',
  3625. '$mod.w = $mod.lw & 0xFFFF;',
  3626. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3627. '$mod.ni = 0x123456789ABCD;',
  3628. '$mod.lw = $mod.ni >>> 0;',
  3629. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3630. '']));
  3631. end;
  3632. procedure TTestModule.TestLoHiDelphiMode;
  3633. begin
  3634. StartProgram(false);
  3635. Add([
  3636. '{$mode delphi}',
  3637. 'const',
  3638. ' LoByte1 = Lo(Word($1234));',
  3639. ' HiByte1 = Hi(Word($1234));',
  3640. ' LoByte2 = Lo(SmallInt($1234));',
  3641. ' HiByte2 = Hi(SmallInt($1234));',
  3642. ' LoByte3 = Lo($1234CDEF);',
  3643. ' HiByte3 = Hi($1234CDEF);',
  3644. ' LoByte4 = Lo(-$1234CDEF);',
  3645. ' HiByte4 = Hi(-$1234CDEF);',
  3646. 'var',
  3647. ' b: Byte;',
  3648. ' w: Word;',
  3649. ' si: SmallInt;',
  3650. ' lw: LongWord;',
  3651. ' li: LongInt;',
  3652. 'begin',
  3653. ' w := $1234;',
  3654. ' b := lo(w);',
  3655. ' b := HI(w);',
  3656. ' lw := $1234CDEF;',
  3657. ' b := lo(lw);',
  3658. ' b := hi(lw);',
  3659. '']);
  3660. ConvertProgram;
  3661. CheckSource('TestLoHiDelphiMode',
  3662. LinesToStr([ // statements
  3663. 'this.LoByte1 = 0x1234 & 0xFF;',
  3664. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3665. 'this.LoByte2 = 0x1234 & 0xFF;',
  3666. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3667. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3668. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3669. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3670. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3671. 'this.b = 0;',
  3672. 'this.w = 0;',
  3673. 'this.si = 0;',
  3674. 'this.lw = 0;',
  3675. 'this.li = 0;'
  3676. ]),
  3677. LinesToStr([ // this.$main
  3678. '$mod.w = 0x1234;',
  3679. '$mod.b = $mod.w & 0xFF;',
  3680. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3681. '$mod.lw = 0x1234CDEF;',
  3682. '$mod.b = $mod.lw & 0xFF;',
  3683. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3684. ]));
  3685. end;
  3686. procedure TTestModule.TestAssignments;
  3687. begin
  3688. StartProgram(false);
  3689. Parser.Options:=Parser.Options+[po_cassignments];
  3690. Add('var');
  3691. Add(' Bar:longint;');
  3692. Add('begin');
  3693. Add(' bar:=3;');
  3694. Add(' bar+=4;');
  3695. Add(' bar-=5;');
  3696. Add(' bar*=6;');
  3697. ConvertProgram;
  3698. CheckSource('TestAssignments',
  3699. LinesToStr([ // statements
  3700. 'this.Bar = 0;'
  3701. ]),
  3702. LinesToStr([ // this.$main
  3703. '$mod.Bar=3;',
  3704. '$mod.Bar+=4;',
  3705. '$mod.Bar-=5;',
  3706. '$mod.Bar*=6;'
  3707. ]));
  3708. end;
  3709. procedure TTestModule.TestArithmeticOperators1;
  3710. begin
  3711. StartProgram(false);
  3712. Add('var');
  3713. Add(' vA,vB,vC:longint;');
  3714. Add('begin');
  3715. Add(' va:=1;');
  3716. Add(' vb:=va+va;');
  3717. Add(' vb:=va div vb;');
  3718. Add(' vb:=va mod vb;');
  3719. Add(' vb:=va+va*vb+va div vb;');
  3720. Add(' vc:=-va;');
  3721. Add(' va:=va-vb;');
  3722. Add(' vb:=va;');
  3723. Add(' if va<vb then vc:=va else vc:=vb;');
  3724. ConvertProgram;
  3725. CheckSource('TestArithmeticOperators1',
  3726. LinesToStr([ // statements
  3727. 'this.vA = 0;',
  3728. 'this.vB = 0;',
  3729. 'this.vC = 0;'
  3730. ]),
  3731. LinesToStr([ // this.$main
  3732. '$mod.vA = 1;',
  3733. '$mod.vB = $mod.vA + $mod.vA;',
  3734. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3735. '$mod.vB = $mod.vA % $mod.vB;',
  3736. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3737. '$mod.vC = -$mod.vA;',
  3738. '$mod.vA = $mod.vA - $mod.vB;',
  3739. '$mod.vB = $mod.vA;',
  3740. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3741. ]));
  3742. end;
  3743. procedure TTestModule.TestMultiAdd;
  3744. begin
  3745. StartProgram(false);
  3746. Add([
  3747. 'function Fly: string; external name ''fly'';',
  3748. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3749. 'var',
  3750. ' Date: double;',
  3751. 'begin',
  3752. ' Result:=(Year>0) and (Year<10000) and',
  3753. ' (Month >= 1) and (Month<=12) and',
  3754. ' (Day>0) and (Day<=31);',
  3755. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3756. 'end;',
  3757. 'var s: string;',
  3758. 'begin',
  3759. ' s:=''a''+''b''+''c''+''d'';',
  3760. ' s:=s+Fly+''e'';',
  3761. ' s:=Fly+Fly+Fly;',
  3762. '']);
  3763. ConvertProgram;
  3764. CheckSource('TestMultiAdd',
  3765. LinesToStr([ // statements
  3766. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3767. ' var Result = false;',
  3768. ' var date = 0.0;',
  3769. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3770. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3771. ' return Result;',
  3772. '};',
  3773. 'this.s = "";',
  3774. '']),
  3775. LinesToStr([ // this.$main
  3776. '$mod.s = "a" + "b" + "c" + "d";',
  3777. '$mod.s = $mod.s + fly() + "e";',
  3778. '$mod.s = fly() + fly() + fly();',
  3779. '']));
  3780. end;
  3781. procedure TTestModule.TestLogicalOperators;
  3782. begin
  3783. StartProgram(false);
  3784. Add('var');
  3785. Add(' vA,vB,vC:boolean;');
  3786. Add('begin');
  3787. Add(' va:=vb and vc;');
  3788. Add(' va:=vb or vc;');
  3789. Add(' va:=vb xor vc;');
  3790. Add(' va:=true and vc;');
  3791. Add(' va:=(vb and vc) or (va and vb);');
  3792. Add(' va:=not vb;');
  3793. ConvertProgram;
  3794. CheckSource('TestLogicalOperators',
  3795. LinesToStr([ // statements
  3796. 'this.vA = false;',
  3797. 'this.vB = false;',
  3798. 'this.vC = false;'
  3799. ]),
  3800. LinesToStr([ // this.$main
  3801. '$mod.vA = $mod.vB && $mod.vC;',
  3802. '$mod.vA = $mod.vB || $mod.vC;',
  3803. '$mod.vA = $mod.vB ^ $mod.vC;',
  3804. '$mod.vA = true && $mod.vC;',
  3805. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3806. '$mod.vA = !$mod.vB;'
  3807. ]));
  3808. end;
  3809. procedure TTestModule.TestBitwiseOperators;
  3810. begin
  3811. StartProgram(false);
  3812. Add([
  3813. 'var',
  3814. ' vA,vB,vC:longint;',
  3815. ' X,Y,Z: nativeint;',
  3816. 'begin',
  3817. ' va:=vb and vc;',
  3818. ' va:=vb or vc;',
  3819. ' va:=vb xor vc;',
  3820. ' va:=vb shl vc;',
  3821. ' va:=vb shr vc;',
  3822. ' va:=3 and vc;',
  3823. ' va:=(vb and vc) or (va and vb);',
  3824. ' va:=not vb;',
  3825. ' X:=Y and Z;',
  3826. ' X:=Y and va;',
  3827. ' X:=Y or Z;',
  3828. ' X:=Y or va;',
  3829. ' X:=Y xor Z;',
  3830. ' X:=Y xor va;',
  3831. '']);
  3832. ConvertProgram;
  3833. CheckSource('TestBitwiseOperators',
  3834. LinesToStr([ // statements
  3835. 'this.vA = 0;',
  3836. 'this.vB = 0;',
  3837. 'this.vC = 0;',
  3838. 'this.X = 0;',
  3839. 'this.Y = 0;',
  3840. 'this.Z = 0;',
  3841. '']),
  3842. LinesToStr([ // this.$main
  3843. '$mod.vA = $mod.vB & $mod.vC;',
  3844. '$mod.vA = $mod.vB | $mod.vC;',
  3845. '$mod.vA = $mod.vB ^ $mod.vC;',
  3846. '$mod.vA = $mod.vB << $mod.vC;',
  3847. '$mod.vA = $mod.vB >>> $mod.vC;',
  3848. '$mod.vA = 3 & $mod.vC;',
  3849. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3850. '$mod.vA = ~$mod.vB;',
  3851. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3852. '$mod.X = $mod.Y & $mod.vA;',
  3853. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3854. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3855. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3856. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3857. '']));
  3858. end;
  3859. procedure TTestModule.TestBitwiseOperatorsLongword;
  3860. begin
  3861. StartProgram(false);
  3862. Add([
  3863. 'var',
  3864. ' a,b,c:longword;',
  3865. ' i: longint;',
  3866. 'begin',
  3867. ' a:=$12345678;',
  3868. ' b:=$EDCBA987;',
  3869. ' c:=not a;',
  3870. ' c:=a and b;',
  3871. ' c:=a and $ffff0000;',
  3872. ' c:=a or b;',
  3873. ' c:=a or $ff00ff00;',
  3874. ' c:=a xor b;',
  3875. ' c:=a xor $f0f0f0f0;',
  3876. ' c:=a shl 1;',
  3877. ' c:=a shl 16;',
  3878. ' c:=a shl 24;',
  3879. ' c:=a shl b;',
  3880. ' c:=a shr 1;',
  3881. ' c:=a shr 16;',
  3882. ' c:=a shr 24;',
  3883. ' c:=a shr b;',
  3884. ' c:=(b and c) or (a and b);',
  3885. ' c:=i and a;',
  3886. ' c:=i or a;',
  3887. ' c:=i xor a;',
  3888. '']);
  3889. ConvertProgram;
  3890. CheckSource('TestBitwiseOperatorsLongword',
  3891. LinesToStr([ // statements
  3892. 'this.a = 0;',
  3893. 'this.b = 0;',
  3894. 'this.c = 0;',
  3895. 'this.i = 0;',
  3896. '']),
  3897. LinesToStr([ // this.$main
  3898. '$mod.a = 0x12345678;',
  3899. '$mod.b = 0xEDCBA987;',
  3900. '$mod.c = rtl.lw(~$mod.a);',
  3901. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3902. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3903. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3904. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3905. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3906. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3907. '$mod.c = rtl.lw($mod.a << 1);',
  3908. '$mod.c = rtl.lw($mod.a << 16);',
  3909. '$mod.c = rtl.lw($mod.a << 24);',
  3910. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3911. '$mod.c = rtl.lw($mod.a >>> 1);',
  3912. '$mod.c = rtl.lw($mod.a >>> 16);',
  3913. '$mod.c = rtl.lw($mod.a >>> 24);',
  3914. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3915. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3916. '$mod.c = $mod.i & $mod.a;',
  3917. '$mod.c = $mod.i | $mod.a;',
  3918. '$mod.c = $mod.i ^ $mod.a;',
  3919. '']));
  3920. end;
  3921. procedure TTestModule.TestPrgProcVar;
  3922. begin
  3923. StartProgram(false);
  3924. Add('procedure Proc1;');
  3925. Add('type');
  3926. Add(' t1=longint;');
  3927. Add('var');
  3928. Add(' vA:t1;');
  3929. Add('begin');
  3930. Add('end;');
  3931. Add('begin');
  3932. ConvertProgram;
  3933. CheckSource('TestPrgProcVar',
  3934. LinesToStr([ // statements
  3935. 'this.Proc1 = function () {',
  3936. ' var vA=0;',
  3937. '};'
  3938. ]),
  3939. LinesToStr([ // this.$main
  3940. ''
  3941. ]));
  3942. end;
  3943. procedure TTestModule.TestUnitProcVar;
  3944. begin
  3945. StartUnit(false);
  3946. Add('interface');
  3947. Add('');
  3948. Add('type tA=string; // unit scope');
  3949. Add('procedure Proc1;');
  3950. Add('');
  3951. Add('implementation');
  3952. Add('');
  3953. Add('procedure Proc1;');
  3954. Add('type tA=longint; // local proc scope');
  3955. Add('var v1:tA; // using local tA');
  3956. Add('begin');
  3957. Add('end;');
  3958. Add('var v2:tA; // using interface tA');
  3959. ConvertUnit;
  3960. CheckSource('TestUnitProcVar',
  3961. LinesToStr([ // statements
  3962. 'var $impl = $mod.$impl;',
  3963. 'this.Proc1 = function () {',
  3964. ' var v1 = 0;',
  3965. '};',
  3966. '']),
  3967. // this.$init
  3968. '',
  3969. // implementation
  3970. LinesToStr([
  3971. '$impl.v2 = "";',
  3972. '']));
  3973. end;
  3974. procedure TTestModule.TestImplProc;
  3975. begin
  3976. StartUnit(false);
  3977. Add('interface');
  3978. Add('');
  3979. Add('procedure Proc1;');
  3980. Add('');
  3981. Add('implementation');
  3982. Add('');
  3983. Add('procedure Proc1; begin end;');
  3984. Add('procedure Proc2; begin end;');
  3985. Add('initialization');
  3986. Add(' Proc1;');
  3987. Add(' Proc2;');
  3988. ConvertUnit;
  3989. CheckSource('TestImplProc',
  3990. LinesToStr([ // statements
  3991. 'var $impl = $mod.$impl;',
  3992. 'this.Proc1 = function () {',
  3993. '};',
  3994. '']),
  3995. LinesToStr([ // this.$init
  3996. '$mod.Proc1();',
  3997. '$impl.Proc2();',
  3998. '']),
  3999. LinesToStr([ // implementation
  4000. '$impl.Proc2 = function () {',
  4001. '};',
  4002. ''])
  4003. );
  4004. end;
  4005. procedure TTestModule.TestFunctionResult;
  4006. begin
  4007. StartProgram(false);
  4008. Add('function Func1: longint;');
  4009. Add('begin');
  4010. Add(' Result:=3;');
  4011. Add(' Func1:=4;');
  4012. Add('end;');
  4013. Add('begin');
  4014. ConvertProgram;
  4015. CheckSource('TestFunctionResult',
  4016. LinesToStr([ // statements
  4017. 'this.Func1 = function () {',
  4018. ' var Result = 0;',
  4019. ' Result = 3;',
  4020. ' Result = 4;',
  4021. ' return Result;',
  4022. '};'
  4023. ]),
  4024. '');
  4025. end;
  4026. procedure TTestModule.TestNestedProc;
  4027. begin
  4028. StartProgram(false);
  4029. Add([
  4030. 'var vInUnit: longint;',
  4031. 'function DoIt(pA,pD: longint): longint;',
  4032. 'var',
  4033. ' vB: longint;',
  4034. ' vC: longint;',
  4035. ' function Nesty(pA: longint): longint; ',
  4036. ' var vB: longint;',
  4037. ' begin',
  4038. ' Result:=pa+vb+vc+pd+vInUnit;',
  4039. ' nesty:=3;',
  4040. ' doit:=4;',
  4041. ' exit;',
  4042. ' end;',
  4043. 'begin',
  4044. ' Result:=pa+vb+vc;',
  4045. ' doit:=6;',
  4046. ' exit;',
  4047. 'end;',
  4048. 'begin']);
  4049. ConvertProgram;
  4050. CheckSource('TestNestedProc',
  4051. LinesToStr([ // statements
  4052. 'this.vInUnit = 0;',
  4053. 'this.DoIt = function (pA, pD) {',
  4054. ' var Result = 0;',
  4055. ' var vB = 0;',
  4056. ' var vC = 0;',
  4057. ' function Nesty(pA) {',
  4058. ' var Result$1 = 0;',
  4059. ' var vB = 0;',
  4060. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4061. ' Result$1 = 3;',
  4062. ' Result = 4;',
  4063. ' return Result$1;',
  4064. ' return Result$1;',
  4065. ' };',
  4066. ' Result = pA + vB + vC;',
  4067. ' Result = 6;',
  4068. ' return Result;',
  4069. ' return Result;',
  4070. '};'
  4071. ]),
  4072. '');
  4073. end;
  4074. procedure TTestModule.TestNestedProc_ResultString;
  4075. begin
  4076. StartProgram(false);
  4077. Add([
  4078. 'function DoIt: string;',
  4079. ' function Nesty: string; ',
  4080. ' begin',
  4081. ' nesty:=#65#66;',
  4082. ' nesty[1]:=#67;',
  4083. ' doit:=#68;',
  4084. ' doit[2]:=#69;',
  4085. ' end;',
  4086. 'begin',
  4087. ' doit:=#70;',
  4088. ' doit[3]:=#71;',
  4089. 'end;',
  4090. 'begin']);
  4091. ConvertProgram;
  4092. CheckSource('TestNestedProc_ResultString',
  4093. LinesToStr([ // statements
  4094. 'this.DoIt = function () {',
  4095. ' var Result = "";',
  4096. ' function Nesty() {',
  4097. ' var Result$1 = "";',
  4098. ' Result$1 = "AB";',
  4099. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4100. ' Result = "D";',
  4101. ' Result = rtl.setCharAt(Result, 1, "E");',
  4102. ' return Result$1;',
  4103. ' };',
  4104. ' Result = "F";',
  4105. ' Result = rtl.setCharAt(Result, 2, "G");',
  4106. ' return Result;',
  4107. '};'
  4108. ]),
  4109. '');
  4110. end;
  4111. procedure TTestModule.TestForwardProc;
  4112. begin
  4113. StartProgram(false);
  4114. Add('procedure FuncA(Bar: longint); forward;');
  4115. Add('procedure FuncB(Bar: longint);');
  4116. Add('begin');
  4117. Add(' funca(bar);');
  4118. Add('end;');
  4119. Add('procedure funca(bar: longint);');
  4120. Add('begin');
  4121. Add(' if bar=3 then ;');
  4122. Add('end;');
  4123. Add('begin');
  4124. Add(' funca(4);');
  4125. Add(' funcb(5);');
  4126. ConvertProgram;
  4127. CheckSource('TestForwardProc',
  4128. LinesToStr([ // statements'
  4129. 'this.FuncB = function (Bar) {',
  4130. ' $mod.FuncA(Bar);',
  4131. '};',
  4132. 'this.FuncA = function (Bar) {',
  4133. ' if (Bar === 3);',
  4134. '};'
  4135. ]),
  4136. LinesToStr([
  4137. '$mod.FuncA(4);',
  4138. '$mod.FuncB(5);'
  4139. ])
  4140. );
  4141. end;
  4142. procedure TTestModule.TestNestedForwardProc;
  4143. begin
  4144. StartProgram(false);
  4145. Add('procedure FuncA;');
  4146. Add(' procedure FuncB(i: longint); forward;');
  4147. Add(' procedure FuncC(i: longint);');
  4148. Add(' begin');
  4149. Add(' funcb(i);');
  4150. Add(' end;');
  4151. Add(' procedure FuncB(i: longint);');
  4152. Add(' begin');
  4153. Add(' if i=3 then ;');
  4154. Add(' end;');
  4155. Add('begin');
  4156. Add(' funcc(4)');
  4157. Add('end;');
  4158. Add('begin');
  4159. Add(' funca;');
  4160. ConvertProgram;
  4161. CheckSource('TestNestedForwardProc',
  4162. LinesToStr([ // statements'
  4163. 'this.FuncA = function () {',
  4164. ' function FuncC(i) {',
  4165. ' FuncB(i);',
  4166. ' };',
  4167. ' function FuncB(i) {',
  4168. ' if (i === 3);',
  4169. ' };',
  4170. ' FuncC(4);',
  4171. '};'
  4172. ]),
  4173. LinesToStr([
  4174. '$mod.FuncA();'
  4175. ])
  4176. );
  4177. end;
  4178. procedure TTestModule.TestAssignFunctionResult;
  4179. begin
  4180. StartProgram(false);
  4181. Add('function Func1: longint;');
  4182. Add('begin');
  4183. Add('end;');
  4184. Add('var i: longint;');
  4185. Add('begin');
  4186. Add(' i:=func1();');
  4187. Add(' i:=func1()+func1();');
  4188. ConvertProgram;
  4189. CheckSource('TestAssignFunctionResult',
  4190. LinesToStr([ // statements
  4191. 'this.Func1 = function () {',
  4192. ' var Result = 0;',
  4193. ' return Result;',
  4194. '};',
  4195. 'this.i = 0;'
  4196. ]),
  4197. LinesToStr([
  4198. '$mod.i = $mod.Func1();',
  4199. '$mod.i = $mod.Func1() + $mod.Func1();'
  4200. ]));
  4201. end;
  4202. procedure TTestModule.TestFunctionResultInCondition;
  4203. begin
  4204. StartProgram(false);
  4205. Add('function Func1: longint;');
  4206. Add('begin');
  4207. Add('end;');
  4208. Add('function Func2: boolean;');
  4209. Add('begin');
  4210. Add('end;');
  4211. Add('var i: longint;');
  4212. Add('begin');
  4213. Add(' if func2 then ;');
  4214. Add(' if i=func1() then ;');
  4215. Add(' if i=func1 then ;');
  4216. ConvertProgram;
  4217. CheckSource('TestFunctionResultInCondition',
  4218. LinesToStr([ // statements
  4219. 'this.Func1 = function () {',
  4220. ' var Result = 0;',
  4221. ' return Result;',
  4222. '};',
  4223. 'this.Func2 = function () {',
  4224. ' var Result = false;',
  4225. ' return Result;',
  4226. '};',
  4227. 'this.i = 0;'
  4228. ]),
  4229. LinesToStr([
  4230. 'if ($mod.Func2());',
  4231. 'if ($mod.i === $mod.Func1());',
  4232. 'if ($mod.i === $mod.Func1());'
  4233. ]));
  4234. end;
  4235. procedure TTestModule.TestFunctionResultInForLoop;
  4236. begin
  4237. StartProgram(false);
  4238. Add([
  4239. 'function Func1(a: array of longint): longint;',
  4240. 'begin',
  4241. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4242. ' for Result in a do if a[Result]=0 then exit;',
  4243. 'end;',
  4244. 'begin',
  4245. ' Func1([1,2,3])']);
  4246. ConvertProgram;
  4247. CheckSource('TestFunctionResultInForLoop',
  4248. LinesToStr([ // statements
  4249. 'this.Func1 = function (a) {',
  4250. ' var Result = 0;',
  4251. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4252. ' Result = $l;',
  4253. ' if (a[Result] === 0) return Result;',
  4254. ' };',
  4255. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4256. ' Result = $in[$l1];',
  4257. ' if (a[Result] === 0) return Result;',
  4258. ' };',
  4259. ' return Result;',
  4260. '};',
  4261. '']),
  4262. LinesToStr([
  4263. '$mod.Func1([1, 2, 3]);'
  4264. ]));
  4265. end;
  4266. procedure TTestModule.TestFunctionResultInTypeCast;
  4267. begin
  4268. StartProgram(false);
  4269. Add([
  4270. 'function GetInt: longint;',
  4271. 'begin',
  4272. 'end;',
  4273. 'begin',
  4274. ' if Byte(GetInt)=0 then ;',
  4275. '']);
  4276. ConvertProgram;
  4277. CheckSource('TestFunctionResultInTypeCast',
  4278. LinesToStr([ // statements
  4279. 'this.GetInt = function () {',
  4280. ' var Result = 0;',
  4281. ' return Result;',
  4282. '};',
  4283. '']),
  4284. LinesToStr([
  4285. 'if (($mod.GetInt() & 255) === 0) ;'
  4286. ]));
  4287. end;
  4288. procedure TTestModule.TestExit;
  4289. begin
  4290. StartProgram(false);
  4291. Add('procedure ProcA;');
  4292. Add('begin');
  4293. Add(' exit;');
  4294. Add('end;');
  4295. Add('function FuncB: longint;');
  4296. Add('begin');
  4297. Add(' exit;');
  4298. Add(' exit(3);');
  4299. Add('end;');
  4300. Add('function FuncC: string;');
  4301. Add('begin');
  4302. Add(' exit;');
  4303. Add(' exit(''a'');');
  4304. Add(' exit(''abc'');');
  4305. Add('end;');
  4306. Add('begin');
  4307. Add(' exit;');
  4308. Add(' exit(1);');
  4309. ConvertProgram;
  4310. CheckSource('TestExit',
  4311. LinesToStr([ // statements
  4312. 'this.ProcA = function () {',
  4313. ' return;',
  4314. '};',
  4315. 'this.FuncB = function () {',
  4316. ' var Result = 0;',
  4317. ' return Result;',
  4318. ' return 3;',
  4319. ' return Result;',
  4320. '};',
  4321. 'this.FuncC = function () {',
  4322. ' var Result = "";',
  4323. ' return Result;',
  4324. ' return "a";',
  4325. ' return "abc";',
  4326. ' return Result;',
  4327. '};'
  4328. ]),
  4329. LinesToStr([
  4330. 'return;',
  4331. 'return 1;',
  4332. '']));
  4333. end;
  4334. procedure TTestModule.TestExit_ResultInFinally;
  4335. begin
  4336. StartProgram(false);
  4337. Add([
  4338. 'function Run: word;',
  4339. 'begin',
  4340. ' try',
  4341. ' exit(3);', // no Result in finally -> use return 3
  4342. ' finally',
  4343. ' end;',
  4344. 'end;',
  4345. 'function Fly: word;',
  4346. 'begin',
  4347. ' try',
  4348. ' exit(3);',
  4349. ' finally',
  4350. ' if Result>0 then ;',
  4351. ' end;',
  4352. 'end;',
  4353. 'function Jump: word;',
  4354. 'begin',
  4355. ' try',
  4356. ' try',
  4357. ' exit(4);',
  4358. ' finally',
  4359. ' end;',
  4360. ' finally',
  4361. ' if Result>0 then ;',
  4362. ' end;',
  4363. 'end;',
  4364. 'begin',
  4365. '']);
  4366. ConvertProgram;
  4367. CheckSource('TestExit_ResultInFinally',
  4368. LinesToStr([ // statements
  4369. 'this.Run = function () {',
  4370. ' var Result = 0;',
  4371. ' try {',
  4372. ' return 3;',
  4373. ' } finally {',
  4374. ' };',
  4375. ' return Result;',
  4376. '};',
  4377. 'this.Fly = function () {',
  4378. ' var Result = 0;',
  4379. ' try {',
  4380. ' Result = 3;',
  4381. ' return Result;',
  4382. ' } finally {',
  4383. ' if (Result > 0) ;',
  4384. ' };',
  4385. ' return Result;',
  4386. '};',
  4387. 'this.Jump = function () {',
  4388. ' var Result = 0;',
  4389. ' try {',
  4390. ' try {',
  4391. ' Result = 4;',
  4392. ' return Result;',
  4393. ' } finally {',
  4394. ' };',
  4395. ' } finally {',
  4396. ' if (Result > 0) ;',
  4397. ' };',
  4398. ' return Result;',
  4399. '};',
  4400. '']),
  4401. LinesToStr([
  4402. '']));
  4403. end;
  4404. procedure TTestModule.TestBreak;
  4405. begin
  4406. StartProgram(false);
  4407. Add([
  4408. 'var',
  4409. ' i: longint;',
  4410. 'begin',
  4411. ' repeat',
  4412. ' break;',
  4413. ' until true;',
  4414. ' while true do',
  4415. ' break;',
  4416. ' for i:=1 to 2 do',
  4417. ' break;']);
  4418. ConvertProgram;
  4419. CheckSource('TestBreak',
  4420. LinesToStr([ // statements
  4421. 'this.i = 0;'
  4422. ]),
  4423. LinesToStr([
  4424. 'do {',
  4425. ' break;',
  4426. '} while (!true);',
  4427. 'while (true) break;',
  4428. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4429. '']));
  4430. end;
  4431. procedure TTestModule.TestBreakAsVar;
  4432. begin
  4433. StartProgram(false);
  4434. Add([
  4435. 'procedure DoIt(break: boolean);',
  4436. 'begin',
  4437. ' if break then ;',
  4438. 'end;',
  4439. 'var',
  4440. ' break: boolean;',
  4441. 'begin',
  4442. ' if break then ;']);
  4443. ConvertProgram;
  4444. CheckSource('TestBreakAsVar',
  4445. LinesToStr([ // statements
  4446. 'this.DoIt = function (Break) {',
  4447. ' if (Break) ;',
  4448. '};',
  4449. 'this.Break = false;',
  4450. '']),
  4451. LinesToStr([
  4452. 'if($mod.Break) ;',
  4453. '']));
  4454. end;
  4455. procedure TTestModule.TestContinue;
  4456. begin
  4457. StartProgram(false);
  4458. Add('var i: longint;');
  4459. Add('begin');
  4460. Add(' repeat');
  4461. Add(' continue;');
  4462. Add(' until true;');
  4463. Add(' while true do');
  4464. Add(' continue;');
  4465. Add(' for i:=1 to 2 do');
  4466. Add(' continue;');
  4467. ConvertProgram;
  4468. CheckSource('TestContinue',
  4469. LinesToStr([ // statements
  4470. 'this.i = 0;'
  4471. ]),
  4472. LinesToStr([
  4473. 'do {',
  4474. ' continue;',
  4475. '} while (!true);',
  4476. 'while (true) continue;',
  4477. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4478. '']));
  4479. end;
  4480. procedure TTestModule.TestProc_External;
  4481. begin
  4482. StartProgram(false);
  4483. Add('procedure Foo; external name ''console.log'';');
  4484. Add('function Bar: longint; external name ''get.item'';');
  4485. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4486. Add('var');
  4487. Add(' i: longint;');
  4488. Add('begin');
  4489. Add(' Foo;');
  4490. Add(' i:=Bar;');
  4491. Add(' i:=Bla(''abc'');');
  4492. ConvertProgram;
  4493. CheckSource('TestProc_External',
  4494. LinesToStr([ // statements
  4495. 'this.i = 0;'
  4496. ]),
  4497. LinesToStr([
  4498. 'console.log();',
  4499. '$mod.i = get.item();',
  4500. '$mod.i = apply.something("abc");'
  4501. ]));
  4502. end;
  4503. procedure TTestModule.TestProc_ExternalOtherUnit;
  4504. begin
  4505. AddModuleWithIntfImplSrc('unit2.pas',
  4506. LinesToStr([
  4507. 'procedure Now; external name ''Date.now'';',
  4508. 'procedure DoIt;'
  4509. ]),
  4510. 'procedure doit; begin end;');
  4511. StartUnit(true);
  4512. Add('interface');
  4513. Add('uses unit2;');
  4514. Add('implementation');
  4515. Add('begin');
  4516. Add(' now;');
  4517. Add(' now();');
  4518. Add(' uNit2.now;');
  4519. Add(' uNit2.now();');
  4520. Add(' doit;');
  4521. Add(' uNit2.doit;');
  4522. ConvertUnit;
  4523. CheckSource('TestProc_ExternalOtherUnit',
  4524. LinesToStr([
  4525. '']),
  4526. LinesToStr([
  4527. 'Date.now();',
  4528. 'Date.now();',
  4529. 'Date.now();',
  4530. 'Date.now();',
  4531. 'pas.unit2.DoIt();',
  4532. 'pas.unit2.DoIt();',
  4533. '']));
  4534. end;
  4535. procedure TTestModule.TestProc_Asm;
  4536. begin
  4537. StartProgram(false);
  4538. Add([
  4539. '{$mode delphi}',
  4540. 'function DoIt: longint;',
  4541. 'begin;',
  4542. ' asm',
  4543. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4544. ' end;',
  4545. ' asm console.log(); end;',
  4546. ' asm',
  4547. ' s = "'' ";',
  4548. ' s = ''" '';',
  4549. ' s = s + "world" + "''";',
  4550. ' // end',
  4551. ' s = ''end'';',
  4552. ' s = "end";',
  4553. ' s = "foo\"bar";',
  4554. ' s = ''a\''b'';',
  4555. ' s = `${expr}\`-"-''-`;',
  4556. ' s = `multi',
  4557. 'line`;',
  4558. ' end;',
  4559. 'end;',
  4560. 'procedure Fly;',
  4561. 'asm',
  4562. ' return;',
  4563. 'end;',
  4564. 'begin']);
  4565. ConvertProgram;
  4566. CheckSource('TestProc_Asm',
  4567. LinesToStr([ // statements
  4568. 'this.DoIt = function () {',
  4569. ' var Result = 0;',
  4570. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4571. ' console.log();',
  4572. ' s = "'' ";',
  4573. ' s = ''" '';',
  4574. ' s = s + "world" + "''";',
  4575. ' // end',
  4576. ' s = ''end'';',
  4577. ' s = "end";',
  4578. ' s = "foo\"bar";',
  4579. ' s = ''a\''b'';',
  4580. ' s = `${expr}\`-"-''-`;',
  4581. ' s = `multi',
  4582. 'line`;',
  4583. ' return Result;',
  4584. '};',
  4585. 'this.Fly = function () {',
  4586. ' return;',
  4587. '};',
  4588. '']),
  4589. LinesToStr([
  4590. ''
  4591. ]));
  4592. end;
  4593. procedure TTestModule.TestProc_AsmSubBlock;
  4594. begin
  4595. StartProgram(true,[supTObject]);
  4596. Add([
  4597. '{$mode delphi}',
  4598. 'type',
  4599. ' TBird = class end;',
  4600. 'procedure Run(w: word);',
  4601. 'begin;',
  4602. ' if true then asm console.log(); end;',
  4603. ' if w>3 then asm',
  4604. ' var a = w+1;',
  4605. ' w = a+3;',
  4606. ' end;',
  4607. ' while (w>7) do asm',
  4608. ' w+=3; w*=2;',
  4609. ' end;',
  4610. ' try',
  4611. ' except',
  4612. ' on E: TBird do',
  4613. ' asm console.log(E); end;',
  4614. ' on E: TObject do',
  4615. ' asm var i=3; i--; end;',
  4616. ' else asm Fly; High; end;',
  4617. ' end;',
  4618. 'end;',
  4619. 'begin']);
  4620. ConvertProgram;
  4621. CheckSource('TestProc_AsmSubBlock',
  4622. LinesToStr([ // statements
  4623. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4624. '});',
  4625. 'this.Run = function (w) {',
  4626. ' if (true) console.log();',
  4627. ' if (w > 3) {',
  4628. ' var a = w+1;',
  4629. ' w = a+3;',
  4630. ' };',
  4631. ' while (w > 7) {',
  4632. ' w+=3; w*=2;',
  4633. ' };',
  4634. ' try {} catch ($e) {',
  4635. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4636. ' var E = $e;',
  4637. ' console.log(E);',
  4638. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4639. ' var E = $e;',
  4640. ' var i=3; i--;',
  4641. ' } else {',
  4642. ' Fly; High;',
  4643. ' }',
  4644. ' };',
  4645. '};',
  4646. '']),
  4647. LinesToStr([
  4648. ''
  4649. ]));
  4650. end;
  4651. procedure TTestModule.TestProc_Assembler;
  4652. begin
  4653. StartProgram(false);
  4654. Add('function DoIt: longint; assembler;');
  4655. Add('asm');
  4656. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4657. Add('end;');
  4658. Add('begin');
  4659. ConvertProgram;
  4660. CheckSource('TestProc_Assembler',
  4661. LinesToStr([ // statements
  4662. 'this.DoIt = function () {',
  4663. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4664. '};'
  4665. ]),
  4666. LinesToStr([
  4667. ''
  4668. ]));
  4669. end;
  4670. procedure TTestModule.TestProc_VarParam;
  4671. begin
  4672. StartProgram(false);
  4673. Add('type integer = longint;');
  4674. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4675. Add('var vJ: integer;');
  4676. Add('begin');
  4677. Add(' vg:=vg+1;');
  4678. Add(' vj:=vh+2;');
  4679. Add(' vi:=vi+3;');
  4680. Add(' doit(vg,vg,vg);');
  4681. Add(' doit(vh,vh,vj);');
  4682. Add(' doit(vi,vi,vi);');
  4683. Add(' doit(vj,vj,vj);');
  4684. Add('end;');
  4685. Add('var i: integer;');
  4686. Add('begin');
  4687. Add(' doit(i,i,i);');
  4688. ConvertProgram;
  4689. CheckSource('TestProc_VarParam',
  4690. LinesToStr([ // statements
  4691. 'this.DoIt = function (vG,vH,vI) {',
  4692. ' var vJ = 0;',
  4693. ' vG = vG + 1;',
  4694. ' vJ = vH + 2;',
  4695. ' vI.set(vI.get()+3);',
  4696. ' $mod.DoIt(vG, vG, {',
  4697. ' get: function () {',
  4698. ' return vG;',
  4699. ' },',
  4700. ' set: function (v) {',
  4701. ' vG = v;',
  4702. ' }',
  4703. ' });',
  4704. ' $mod.DoIt(vH, vH, {',
  4705. ' get: function () {',
  4706. ' return vJ;',
  4707. ' },',
  4708. ' set: function (v) {',
  4709. ' vJ = v;',
  4710. ' }',
  4711. ' });',
  4712. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4713. ' $mod.DoIt(vJ, vJ, {',
  4714. ' get: function () {',
  4715. ' return vJ;',
  4716. ' },',
  4717. ' set: function (v) {',
  4718. ' vJ = v;',
  4719. ' }',
  4720. ' });',
  4721. '};',
  4722. 'this.i = 0;'
  4723. ]),
  4724. LinesToStr([
  4725. '$mod.DoIt($mod.i,$mod.i,{',
  4726. ' p: $mod,',
  4727. ' get: function () {',
  4728. ' return this.p.i;',
  4729. ' },',
  4730. ' set: function (v) {',
  4731. ' this.p.i = v;',
  4732. ' }',
  4733. '});'
  4734. ]));
  4735. end;
  4736. procedure TTestModule.TestProc_VarParamString;
  4737. begin
  4738. StartProgram(false);
  4739. Add(['type TCaption = string;',
  4740. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4741. 'var c: char;',
  4742. 'begin',
  4743. ' va[1]:=c;',
  4744. ' vb[2]:=c;',
  4745. ' vc[3]:=c;',
  4746. 'end;',
  4747. 'begin']);
  4748. ConvertProgram;
  4749. CheckSource('TestProc_VarParamString',
  4750. LinesToStr([ // statements
  4751. 'this.DoIt = function (vA,vB,vC) {',
  4752. ' var c = "";',
  4753. ' vA = rtl.setCharAt(vA, 0, c);',
  4754. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4755. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4756. '};',
  4757. '']),
  4758. LinesToStr([
  4759. ]));
  4760. end;
  4761. procedure TTestModule.TestProc_VarParamV;
  4762. begin
  4763. StartProgram(false);
  4764. Add([
  4765. 'procedure Inc2(var i: longint);',
  4766. 'begin',
  4767. ' i:=i+2;',
  4768. 'end;',
  4769. 'procedure DoIt(v: longint);',
  4770. 'var p: array of longint;',
  4771. 'begin',
  4772. ' Inc2(v);',
  4773. ' Inc2(p[v]);',
  4774. 'end;',
  4775. 'begin']);
  4776. ConvertProgram;
  4777. CheckSource('TestProc_VarParamV',
  4778. LinesToStr([ // statements
  4779. 'this.Inc2 = function (i) {',
  4780. ' i.set(i.get()+2);',
  4781. '};',
  4782. 'this.DoIt = function (v) {',
  4783. ' var p = [];',
  4784. ' $mod.Inc2({get: function () {',
  4785. ' return v;',
  4786. ' }, set: function (w) {',
  4787. ' v = w;',
  4788. ' }});',
  4789. ' $mod.Inc2({',
  4790. ' a: v,',
  4791. ' p: p,',
  4792. ' get: function () {',
  4793. ' return this.p[this.a];',
  4794. ' },',
  4795. ' set: function (v) {',
  4796. ' this.p[this.a] = v;',
  4797. ' }',
  4798. ' });',
  4799. '};',
  4800. '']),
  4801. LinesToStr([
  4802. '']));
  4803. end;
  4804. procedure TTestModule.TestProc_Overload;
  4805. begin
  4806. StartProgram(false);
  4807. Add('procedure DoIt(vI: longint); begin end;');
  4808. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4809. Add('procedure DoIt(vD: double); begin end;');
  4810. Add('begin');
  4811. Add(' DoIt(1);');
  4812. Add(' DoIt(2,3);');
  4813. Add(' DoIt(4.5);');
  4814. ConvertProgram;
  4815. CheckSource('TestProcedureOverload',
  4816. LinesToStr([ // statements
  4817. 'this.DoIt = function (vI) {',
  4818. '};',
  4819. 'this.DoIt$1 = function (vI, vJ) {',
  4820. '};',
  4821. 'this.DoIt$2 = function (vD) {',
  4822. '};',
  4823. '']),
  4824. LinesToStr([
  4825. '$mod.DoIt(1);',
  4826. '$mod.DoIt$1(2, 3);',
  4827. '$mod.DoIt$2(4.5);',
  4828. '']));
  4829. end;
  4830. procedure TTestModule.TestProc_OverloadForward;
  4831. begin
  4832. StartProgram(false);
  4833. Add('procedure DoIt(vI: longint); forward;');
  4834. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4835. Add('procedure doit(vi: longint); begin end;');
  4836. Add('begin');
  4837. Add(' doit(1);');
  4838. Add(' doit(2,3);');
  4839. ConvertProgram;
  4840. CheckSource('TestProcedureOverloadForward',
  4841. LinesToStr([ // statements
  4842. 'this.DoIt$1 = function (vI, vJ) {',
  4843. '};',
  4844. 'this.DoIt = function (vI) {',
  4845. '};',
  4846. '']),
  4847. LinesToStr([
  4848. '$mod.DoIt(1);',
  4849. '$mod.DoIt$1(2, 3);',
  4850. '']));
  4851. end;
  4852. procedure TTestModule.TestProc_OverloadIntfImpl;
  4853. begin
  4854. StartUnit(false);
  4855. Add('interface');
  4856. Add('procedure DoIt(vI: longint);');
  4857. Add('procedure DoIt(vI, vJ: longint);');
  4858. Add('implementation');
  4859. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4860. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4861. Add('procedure DoIt(vi: longint); begin end;');
  4862. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4863. Add('procedure DoIt(vi, vj: longint); begin end;');
  4864. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4865. Add('begin');
  4866. Add(' doit(1);');
  4867. Add(' doit(2,3);');
  4868. Add(' doit(4,5,6);');
  4869. Add(' doit(7,8,9,10);');
  4870. Add(' doit(11,12,13,14,15);');
  4871. ConvertUnit;
  4872. CheckSource('TestProcedureOverloadUnit',
  4873. LinesToStr([ // statements
  4874. 'var $impl = $mod.$impl;',
  4875. 'this.DoIt = function (vI) {',
  4876. '};',
  4877. 'this.DoIt$1 = function (vI, vJ) {',
  4878. '};',
  4879. '']),
  4880. LinesToStr([ // this.$init
  4881. '$mod.DoIt(1);',
  4882. '$mod.DoIt$1(2, 3);',
  4883. '$impl.DoIt$3(4,5,6);',
  4884. '$impl.DoIt$4(7,8,9,10);',
  4885. '$impl.DoIt$2(11,12,13,14,15);',
  4886. '']),
  4887. LinesToStr([ // implementation
  4888. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4889. '};',
  4890. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4891. '};',
  4892. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4893. '};',
  4894. '']));
  4895. end;
  4896. procedure TTestModule.TestProc_OverloadNested;
  4897. begin
  4898. StartProgram(false);
  4899. Add([
  4900. 'procedure doit(vA: longint);',
  4901. ' procedure DoIt(vA, vB: longint); overload;',
  4902. ' begin',
  4903. ' doit(1);',
  4904. ' doit(1,2);',
  4905. ' end;',
  4906. ' procedure doit(vA, vB, vC: longint);',
  4907. ' begin',
  4908. ' doit(1);',
  4909. ' doit(1,2);',
  4910. ' doit(1,2,3);',
  4911. ' end;',
  4912. 'begin',
  4913. ' doit(1);',
  4914. ' doit(1,2);',
  4915. ' doit(1,2,3);',
  4916. 'end;',
  4917. 'begin // main',
  4918. ' doit(1);']);
  4919. ConvertProgram;
  4920. CheckSource('TestProcedureOverloadNested',
  4921. LinesToStr([ // statements
  4922. 'this.doit = function (vA) {',
  4923. ' function DoIt$1(vA, vB) {',
  4924. ' $mod.doit(1);',
  4925. ' DoIt$1(1, 2);',
  4926. ' };',
  4927. ' function doit$2(vA, vB, vC) {',
  4928. ' $mod.doit(1);',
  4929. ' DoIt$1(1, 2);',
  4930. ' doit$2(1, 2, 3);',
  4931. ' };',
  4932. ' $mod.doit(1);',
  4933. ' DoIt$1(1, 2);',
  4934. ' doit$2(1, 2, 3);',
  4935. '};',
  4936. '']),
  4937. LinesToStr([
  4938. '$mod.doit(1);',
  4939. '']));
  4940. end;
  4941. procedure TTestModule.TestProc_OverloadNestedForward;
  4942. begin
  4943. StartProgram(false);
  4944. Add([
  4945. 'procedure DoIt(vA: longint); overload; forward;',
  4946. 'procedure DoIt(vB, vC: longint); overload;',
  4947. 'begin // 2 param overload',
  4948. ' doit(1);',
  4949. ' doit(1,2);',
  4950. 'end;',
  4951. 'procedure doit(vA: longint);',
  4952. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4953. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4954. ' begin // 4 param overload',
  4955. ' doit(1);',
  4956. ' doit(1,2);',
  4957. ' doit(1,2,3);',
  4958. ' doit(1,2,3,4);',
  4959. ' end;',
  4960. ' procedure doit(vA, vB, vC: longint);',
  4961. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4962. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4963. ' begin // 6 param overload',
  4964. ' doit(1);',
  4965. ' doit(1,2);',
  4966. ' doit(1,2,3);',
  4967. ' doit(1,2,3,4);',
  4968. ' doit(1,2,3,4,5);',
  4969. ' doit(1,2,3,4,5,6);',
  4970. ' end;',
  4971. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4972. ' begin // 5 param overload',
  4973. ' doit(1);',
  4974. ' doit(1,2);',
  4975. ' doit(1,2,3);',
  4976. ' doit(1,2,3,4);',
  4977. ' doit(1,2,3,4,5);',
  4978. ' doit(1,2,3,4,5,6);',
  4979. ' end;',
  4980. ' begin // 3 param overload',
  4981. ' doit(1);',
  4982. ' doit(1,2);',
  4983. ' doit(1,2,3);',
  4984. ' doit(1,2,3,4);',
  4985. ' doit(1,2,3,4,5);',
  4986. ' doit(1,2,3,4,5,6);',
  4987. ' end;',
  4988. 'begin // 1 param overload',
  4989. ' doit(1);',
  4990. ' doit(1,2);',
  4991. ' doit(1,2,3);',
  4992. ' doit(1,2,3,4);',
  4993. 'end;',
  4994. 'begin // main',
  4995. ' doit(1);',
  4996. ' doit(1,2);']);
  4997. ConvertProgram;
  4998. CheckSource('TestProc_OverloadNestedForward',
  4999. LinesToStr([ // statements
  5000. 'this.DoIt$1 = function (vB, vC) {',
  5001. ' $mod.DoIt(1);',
  5002. ' $mod.DoIt$1(1, 2);',
  5003. '};',
  5004. 'this.DoIt = function (vA) {',
  5005. ' function DoIt$3(vA, vB, vC, vD) {',
  5006. ' $mod.DoIt(1);',
  5007. ' $mod.DoIt$1(1, 2);',
  5008. ' DoIt$2(1, 2, 3);',
  5009. ' DoIt$3(1, 2, 3, 4);',
  5010. ' };',
  5011. ' function DoIt$2(vA, vB, vC) {',
  5012. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5013. ' $mod.DoIt(1);',
  5014. ' $mod.DoIt$1(1, 2);',
  5015. ' DoIt$2(1, 2, 3);',
  5016. ' DoIt$3(1, 2, 3, 4);',
  5017. ' DoIt$4(1, 2, 3, 4, 5);',
  5018. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5019. ' };',
  5020. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5021. ' $mod.DoIt(1);',
  5022. ' $mod.DoIt$1(1, 2);',
  5023. ' DoIt$2(1, 2, 3);',
  5024. ' DoIt$3(1, 2, 3, 4);',
  5025. ' DoIt$4(1, 2, 3, 4, 5);',
  5026. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5027. ' };',
  5028. ' $mod.DoIt(1);',
  5029. ' $mod.DoIt$1(1, 2);',
  5030. ' DoIt$2(1, 2, 3);',
  5031. ' DoIt$3(1, 2, 3, 4);',
  5032. ' DoIt$4(1, 2, 3, 4, 5);',
  5033. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5034. ' };',
  5035. ' $mod.DoIt(1);',
  5036. ' $mod.DoIt$1(1, 2);',
  5037. ' DoIt$2(1, 2, 3);',
  5038. ' DoIt$3(1, 2, 3, 4);',
  5039. '};',
  5040. '']),
  5041. LinesToStr([
  5042. '$mod.DoIt(1);',
  5043. '$mod.DoIt$1(1, 2);',
  5044. '']));
  5045. end;
  5046. procedure TTestModule.TestProc_OverloadUnitCycle;
  5047. begin
  5048. AddModuleWithIntfImplSrc('Unit2.pas',
  5049. LinesToStr([
  5050. 'type',
  5051. ' TObject = class',
  5052. ' procedure DoIt(b: boolean); virtual; abstract;',
  5053. ' procedure DoIt(i: longint); virtual; abstract;',
  5054. ' end;',
  5055. '']),
  5056. 'uses test1;');
  5057. StartUnit(true);
  5058. Add([
  5059. 'interface',
  5060. 'uses unit2;',
  5061. 'type',
  5062. ' TEagle = class(TObject)',
  5063. ' procedure DoIt(b: boolean); override;',
  5064. ' procedure DoIt(i: longint); override;',
  5065. ' end;',
  5066. 'implementation',
  5067. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5068. 'procedure TEagle.DoIt(i: longint); begin end;',
  5069. '']);
  5070. ConvertUnit;
  5071. CheckSource('TestProc_OverloadUnitCycle',
  5072. LinesToStr([ // statements
  5073. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5074. ' this.DoIt = function (b) {',
  5075. ' };',
  5076. ' this.DoIt$1 = function (i) {',
  5077. ' };',
  5078. '});',
  5079. '']),
  5080. '',
  5081. LinesToStr([
  5082. '']));
  5083. end;
  5084. procedure TTestModule.TestProc_Varargs;
  5085. begin
  5086. StartProgram(false);
  5087. Add([
  5088. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5089. 'procedure ProcB; varargs; external name ''ProcB'';',
  5090. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5091. 'function GetIt: longint; begin end;',
  5092. 'begin',
  5093. ' ProcA(1);',
  5094. ' ProcA(1,2);',
  5095. ' ProcA(1,2.0);',
  5096. ' ProcA(1,2,3);',
  5097. ' ProcA(1,''2'');',
  5098. ' ProcA(2,'''');',
  5099. ' ProcA(3,false);',
  5100. ' ProcB;',
  5101. ' ProcB();',
  5102. ' ProcB(4);',
  5103. ' ProcB(''foo'');',
  5104. ' ProcC;',
  5105. ' ProcC();',
  5106. ' ProcC(4);',
  5107. ' ProcC(5,''foo'');',
  5108. ' ProcB(GetIt);',
  5109. ' ProcB(GetIt());',
  5110. ' ProcB(GetIt,GetIt());']);
  5111. ConvertProgram;
  5112. CheckSource('TestProc_Varargs',
  5113. LinesToStr([ // statements
  5114. 'this.GetIt = function () {',
  5115. ' var Result = 0;',
  5116. ' return Result;',
  5117. '};',
  5118. '']),
  5119. LinesToStr([
  5120. 'ProcA(1);',
  5121. 'ProcA(1, 2);',
  5122. 'ProcA(1, 2.0);',
  5123. 'ProcA(1, 2, 3);',
  5124. 'ProcA(1, "2");',
  5125. 'ProcA(2, "");',
  5126. 'ProcA(3, false);',
  5127. 'ProcB();',
  5128. 'ProcB();',
  5129. 'ProcB(4);',
  5130. 'ProcB("foo");',
  5131. 'ProcC(17);',
  5132. 'ProcC(17);',
  5133. 'ProcC(4);',
  5134. 'ProcC(5, "foo");',
  5135. 'ProcB($mod.GetIt());',
  5136. 'ProcB($mod.GetIt());',
  5137. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5138. '']));
  5139. end;
  5140. procedure TTestModule.TestProc_ConstOrder;
  5141. begin
  5142. StartProgram(false);
  5143. Add([
  5144. 'const A = 3;',
  5145. 'const B = A+1;',
  5146. 'procedure DoIt;',
  5147. 'const C = A+1;',
  5148. 'const D = B+1;',
  5149. 'const E = D+C+B+A;',
  5150. 'begin',
  5151. 'end;',
  5152. 'begin'
  5153. ]);
  5154. ConvertProgram;
  5155. CheckSource('TestProc_ConstOrder',
  5156. LinesToStr([ // statements
  5157. 'this.A = 3;',
  5158. 'this.B = 3 + 1;',
  5159. 'var C = 3 + 1;',
  5160. 'var D = 4 + 1;',
  5161. 'var E = 5 + 4 + 4 + 3;',
  5162. 'this.DoIt = function () {',
  5163. '};',
  5164. '']),
  5165. LinesToStr([
  5166. ''
  5167. ]));
  5168. end;
  5169. procedure TTestModule.TestProc_DuplicateConst;
  5170. begin
  5171. StartProgram(false);
  5172. Add([
  5173. 'const A = 1;',
  5174. 'procedure DoIt;',
  5175. 'const A = 2;',
  5176. ' procedure SubIt;',
  5177. ' const A = 21;',
  5178. ' begin',
  5179. ' end;',
  5180. 'begin',
  5181. 'end;',
  5182. 'procedure DoSome;',
  5183. 'const A = 3;',
  5184. 'begin',
  5185. 'end;',
  5186. 'begin'
  5187. ]);
  5188. ConvertProgram;
  5189. CheckSource('TestProc_DuplicateConst',
  5190. LinesToStr([ // statements
  5191. 'this.A = 1;',
  5192. 'var A$1 = 2;',
  5193. 'var A$2 = 21;',
  5194. 'this.DoIt = function () {',
  5195. ' function SubIt() {',
  5196. ' };',
  5197. '};',
  5198. 'var A$3 = 3;',
  5199. 'this.DoSome = function () {',
  5200. '};',
  5201. '']),
  5202. LinesToStr([
  5203. ''
  5204. ]));
  5205. end;
  5206. procedure TTestModule.TestProc_LocalVarAbsolute;
  5207. begin
  5208. StartProgram(false);
  5209. Add([
  5210. 'type',
  5211. ' TObject = class',
  5212. ' Index: longint;',
  5213. ' procedure DoAbs(Item: pointer);',
  5214. ' end;',
  5215. 'procedure TObject.DoAbs(Item: pointer);',
  5216. 'var',
  5217. ' o: TObject absolute Item;',
  5218. 'begin',
  5219. ' if o.Index<o.Index then o.Index:=o.Index;',
  5220. 'end;',
  5221. 'procedure DoIt(i: longint; p: pointer);',
  5222. 'var',
  5223. ' d: double absolute i;',
  5224. ' s: string absolute d;',
  5225. ' oi: TObject absolute i;',
  5226. ' op: TObject absolute p;',
  5227. 'begin',
  5228. ' if d=d then d:=d;',
  5229. ' if s=s then s:=s;',
  5230. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5231. ' if op.Index=op.Index then op.Index:=op.Index;',
  5232. 'end;',
  5233. 'begin']);
  5234. ConvertProgram;
  5235. CheckSource('TestProc_LocalVarAbsolute',
  5236. LinesToStr([ // statements
  5237. 'rtl.createClass(this, "TObject", null, function () {',
  5238. ' this.$init = function () {',
  5239. ' this.Index = 0;',
  5240. ' };',
  5241. ' this.$final = function () {',
  5242. ' };',
  5243. ' this.DoAbs = function (Item) {',
  5244. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5245. ' };',
  5246. '});',
  5247. 'this.DoIt = function (i, p) {',
  5248. ' if (i === i) i = i;',
  5249. ' if (i === i) i = i;',
  5250. ' if (i.Index < i.Index) i.Index = i.Index;',
  5251. ' if (p.Index === p.Index) p.Index = p.Index;',
  5252. '};'
  5253. ]),
  5254. LinesToStr([
  5255. ]));
  5256. end;
  5257. procedure TTestModule.TestProc_ResultAbsolute;
  5258. begin
  5259. StartProgram(false);
  5260. Add([
  5261. 'type',
  5262. ' TObject = class',
  5263. ' Index: longint;',
  5264. ' function DoAbs: pointer;',
  5265. ' end;',
  5266. 'function TObject.DoAbs: pointer;',
  5267. 'var',
  5268. ' o: TObject absolute Result;',
  5269. 'begin',
  5270. ' if o.Index<o.Index then o.Index:=o.Index;',
  5271. 'end;',
  5272. 'function DoIt: jsvalue;',
  5273. 'var',
  5274. ' d: double absolute Result;',
  5275. ' s: string absolute Result;',
  5276. ' o: TObject absolute Result;',
  5277. 'begin',
  5278. ' if d=d then d:=d;',
  5279. ' if s=s then s:=s;',
  5280. ' if o.Index<o.Index then o.Index:=o.Index;',
  5281. 'end;',
  5282. 'begin']);
  5283. ConvertProgram;
  5284. CheckSource('TestProc_ResultAbsolute',
  5285. LinesToStr([ // statements
  5286. 'rtl.createClass(this, "TObject", null, function () {',
  5287. ' this.$init = function () {',
  5288. ' this.Index = 0;',
  5289. ' };',
  5290. ' this.$final = function () {',
  5291. ' };',
  5292. ' this.DoAbs = function () {',
  5293. ' var Result = null;',
  5294. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5295. ' return Result;',
  5296. ' };',
  5297. '});',
  5298. 'this.DoIt = function () {',
  5299. ' var Result = undefined;',
  5300. ' if (Result === Result) Result = Result;',
  5301. ' if (Result === Result) Result = Result;',
  5302. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5303. ' return Result;',
  5304. '};',
  5305. '']),
  5306. LinesToStr([
  5307. ]));
  5308. end;
  5309. procedure TTestModule.TestProc_LocalVarInit;
  5310. begin
  5311. StartProgram(false);
  5312. Add([
  5313. 'type TBytes = array of byte;',
  5314. 'procedure DoIt;',
  5315. 'const c = 4;',
  5316. 'var',
  5317. ' b: byte = 1;',
  5318. ' w: word = 2+c;',
  5319. ' p: pointer = nil;',
  5320. ' Buffer: TBytes = nil;',
  5321. 'begin',
  5322. 'end;',
  5323. 'begin']);
  5324. ConvertProgram;
  5325. CheckSource('TestProc_LocalVarInit',
  5326. LinesToStr([ // statements
  5327. 'var c = 4;',
  5328. 'this.DoIt = function () {',
  5329. ' var b = 1;',
  5330. ' var w = 2 + 4;',
  5331. ' var p = null;',
  5332. ' var Buffer = [];',
  5333. '};',
  5334. '']),
  5335. LinesToStr([
  5336. ]));
  5337. end;
  5338. procedure TTestModule.TestProc_ReservedWords;
  5339. begin
  5340. StartProgram(false);
  5341. Add([
  5342. 'procedure Date(ArrayBuffer: longint);',
  5343. 'const',
  5344. ' NaN: longint = 3;',
  5345. 'var',
  5346. ' &Boolean: longint;',
  5347. ' procedure Error(ArrayBuffer: longint);',
  5348. ' begin',
  5349. ' end;',
  5350. 'begin',
  5351. ' Nan:=&bOolean;',
  5352. 'end;',
  5353. 'begin',
  5354. ' Date(1);']);
  5355. ConvertProgram;
  5356. CheckSource('TestProc_ReservedWords',
  5357. LinesToStr([ // statements
  5358. 'var naN = 3;',
  5359. 'this.Date = function (arrayBuffer) {',
  5360. ' var boolean = 0;',
  5361. ' function error(arrayBuffer) {',
  5362. ' };',
  5363. ' naN = boolean;',
  5364. '};',
  5365. '']),
  5366. LinesToStr([
  5367. ' $mod.Date(1);'
  5368. ]));
  5369. end;
  5370. procedure TTestModule.TestProc_ConstRefWord;
  5371. begin
  5372. StartProgram(false);
  5373. Add([
  5374. 'procedure Run(constref w: word);',
  5375. 'var l: word;',
  5376. 'begin',
  5377. ' l:=w;',
  5378. ' Run(w);',
  5379. ' Run(l);',
  5380. 'end;',
  5381. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5382. 'begin',
  5383. ' Run(a);',
  5384. ' Run(b);',
  5385. ' Run(c);',
  5386. ' Run(d);',
  5387. ' Run(e);',
  5388. 'end;',
  5389. 'begin',
  5390. ' Run(1);']);
  5391. ConvertProgram;
  5392. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5393. CheckSource('TestProc_ConstRefWord',
  5394. LinesToStr([ // statements
  5395. 'this.Run = function (w) {',
  5396. ' var l = 0;',
  5397. ' l = w;',
  5398. ' $mod.Run(w);',
  5399. ' $mod.Run(l);',
  5400. '};',
  5401. 'this.Fly = function (a, b, c, d, e) {',
  5402. ' $mod.Run(a);',
  5403. ' $mod.Run(b.get());',
  5404. ' $mod.Run(c.get());',
  5405. ' $mod.Run(d);',
  5406. ' $mod.Run(e);',
  5407. '};',
  5408. '']),
  5409. LinesToStr([
  5410. '$mod.Run(1);'
  5411. ]));
  5412. end;
  5413. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5414. begin
  5415. StartProgram(false);
  5416. Add([
  5417. '{$mode objfpc}',
  5418. 'type',
  5419. ' TFunc = reference to function(x: word): word;',
  5420. 'var Func: TFunc;',
  5421. 'procedure DoIt(a: word);',
  5422. 'begin',
  5423. ' Func:=function(b:word): word',
  5424. ' begin',
  5425. ' Result:=a+b;',
  5426. ' exit(b);',
  5427. ' exit(Result);',
  5428. ' end;',// test semicolon
  5429. ' a:=3;',
  5430. 'end;',
  5431. 'begin',
  5432. ' Func:=function(c:word):word begin',
  5433. ' Result:=3+c;',
  5434. ' exit(c);',
  5435. ' exit(Result);',
  5436. ' end;']);
  5437. ConvertProgram;
  5438. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5439. LinesToStr([ // statements
  5440. 'this.Func = null;',
  5441. 'this.DoIt = function (a) {',
  5442. ' $mod.Func = function (b) {',
  5443. ' var Result = 0;',
  5444. ' Result = a + b;',
  5445. ' return b;',
  5446. ' return Result;',
  5447. ' return Result;',
  5448. ' };',
  5449. ' a = 3;',
  5450. '};',
  5451. '']),
  5452. LinesToStr([
  5453. '$mod.Func = function (c) {',
  5454. ' var Result = 0;',
  5455. ' Result = 3 + c;',
  5456. ' return c;',
  5457. ' return Result;',
  5458. ' return Result;',
  5459. '};',
  5460. '']));
  5461. end;
  5462. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5463. begin
  5464. StartProgram(false);
  5465. Add([
  5466. '{$mode delphi}',
  5467. 'type',
  5468. ' TProc = reference to procedure(x: word);',
  5469. 'procedure DoIt(a: word);',
  5470. 'var Proc: TProc;',
  5471. 'begin',
  5472. ' Proc:=procedure(b:word) begin end;',
  5473. 'end;',
  5474. 'var Proc: TProc;',
  5475. 'begin',
  5476. ' Proc:=procedure(c:word) begin end;',
  5477. '']);
  5478. ConvertProgram;
  5479. CheckSource('TestAnonymousProc_Assign_Delphi',
  5480. LinesToStr([ // statements
  5481. 'this.DoIt = function (a) {',
  5482. ' var Proc = null;',
  5483. ' Proc = function (b) {',
  5484. ' };',
  5485. '};',
  5486. 'this.Proc = null;',
  5487. '']),
  5488. LinesToStr([
  5489. '$mod.Proc = function (c) {',
  5490. '};',
  5491. '']));
  5492. end;
  5493. procedure TTestModule.TestAnonymousProc_Arg;
  5494. begin
  5495. StartProgram(false);
  5496. Add([
  5497. 'type',
  5498. ' TProc = reference to procedure;',
  5499. ' TFunc = reference to function(x: word): word;',
  5500. 'procedure DoMore(f,g: TProc);',
  5501. 'begin',
  5502. 'end;',
  5503. 'procedure DoOdd(v: jsvalue);',
  5504. 'begin',
  5505. 'end;',
  5506. 'procedure DoIt(f: TFunc);',
  5507. 'begin',
  5508. ' DoIt(function(b:word): word',
  5509. ' begin',
  5510. ' Result:=1+b;',
  5511. ' end);',
  5512. ' DoMore(procedure begin end, procedure begin end);',
  5513. ' DoOdd(procedure begin end);',
  5514. 'end;',
  5515. 'begin',
  5516. ' DoMore(procedure begin end,',
  5517. ' procedure assembler asm',
  5518. ' console.log("c");',
  5519. ' end);',
  5520. '']);
  5521. ConvertProgram;
  5522. CheckSource('TestAnonymousProc_Arg',
  5523. LinesToStr([ // statements
  5524. 'this.DoMore = function (f, g) {',
  5525. '};',
  5526. 'this.DoOdd = function (v) {',
  5527. '};',
  5528. 'this.DoIt = function (f) {',
  5529. ' $mod.DoIt(function (b) {',
  5530. ' var Result = 0;',
  5531. ' Result = 1 + b;',
  5532. ' return Result;',
  5533. ' });',
  5534. ' $mod.DoMore(function () {',
  5535. ' }, function () {',
  5536. ' });',
  5537. ' $mod.DoOdd(function () {',
  5538. ' });',
  5539. '};',
  5540. '']),
  5541. LinesToStr([
  5542. '$mod.DoMore(function () {',
  5543. '}, function () {',
  5544. ' console.log("c");',
  5545. '});',
  5546. '']));
  5547. end;
  5548. procedure TTestModule.TestAnonymousProc_Typecast;
  5549. begin
  5550. StartProgram(false);
  5551. Add([
  5552. 'type',
  5553. ' TProc = reference to procedure(w: word);',
  5554. ' TArr = array of word;',
  5555. ' TFuncArr = reference to function: TArr;',
  5556. 'procedure DoIt(p: TProc);',
  5557. 'var',
  5558. ' w: word;',
  5559. ' a: TArr;',
  5560. 'begin',
  5561. ' p:=TProc(procedure(b: smallint) begin end);',
  5562. ' a:=TFuncArr(function: TArr begin end)();',
  5563. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5564. 'end;',
  5565. 'begin']);
  5566. ConvertProgram;
  5567. CheckSource('TestAnonymousProc_Typecast',
  5568. LinesToStr([ // statements
  5569. 'this.DoIt = function (p) {',
  5570. ' var w = 0;',
  5571. ' var a = [];',
  5572. ' p = function (b) {',
  5573. ' };',
  5574. ' a = function () {',
  5575. ' var Result = [];',
  5576. ' return Result;',
  5577. ' }();',
  5578. ' w = function () {',
  5579. ' var Result = [];',
  5580. ' return Result;',
  5581. ' }()[3];',
  5582. '};',
  5583. '']),
  5584. LinesToStr([
  5585. '']));
  5586. end;
  5587. procedure TTestModule.TestAnonymousProc_With;
  5588. begin
  5589. StartProgram(false);
  5590. Add([
  5591. 'type',
  5592. ' TProc = reference to procedure(w: word);',
  5593. ' TObject = class',
  5594. ' b: boolean;',
  5595. ' end;',
  5596. 'var',
  5597. ' p: TProc;',
  5598. ' bird: TObject;',
  5599. 'begin',
  5600. ' with bird do',
  5601. ' p:=procedure(w: word)',
  5602. ' begin',
  5603. ' b:=w>2;',
  5604. ' end;',
  5605. '']);
  5606. ConvertProgram;
  5607. CheckSource('TestAnonymousProc_With',
  5608. LinesToStr([ // statements
  5609. 'rtl.createClass(this, "TObject", null, function () {',
  5610. ' this.$init = function () {',
  5611. ' this.b = false;',
  5612. ' };',
  5613. ' this.$final = function () {',
  5614. ' };',
  5615. '});',
  5616. 'this.p = null;',
  5617. 'this.bird = null;',
  5618. '']),
  5619. LinesToStr([
  5620. 'var $with = $mod.bird;',
  5621. '$mod.p = function (w) {',
  5622. ' $with.b = w > 2;',
  5623. '};',
  5624. '']));
  5625. end;
  5626. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5627. begin
  5628. StartProgram(false);
  5629. Add([
  5630. 'type',
  5631. ' TProc = reference to procedure;',
  5632. ' TObject = class',
  5633. ' b: boolean;',
  5634. ' end;',
  5635. 'procedure DoIt;',
  5636. 'var',
  5637. ' p: TProc;',
  5638. 'begin',
  5639. ' try',
  5640. ' except',
  5641. ' on E: TObject do',
  5642. ' p:=procedure',
  5643. ' begin',
  5644. ' E.b:=true;',
  5645. ' end;',
  5646. ' end;',
  5647. 'end;',
  5648. 'begin']);
  5649. ConvertProgram;
  5650. CheckSource('TestAnonymousProc_ExceptOn',
  5651. LinesToStr([ // statements
  5652. 'rtl.createClass(this, "TObject", null, function () {',
  5653. ' this.$init = function () {',
  5654. ' this.b = false;',
  5655. ' };',
  5656. ' this.$final = function () {',
  5657. ' };',
  5658. '});',
  5659. 'this.DoIt = function () {',
  5660. ' var p = null;',
  5661. ' try {} catch ($e) {',
  5662. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5663. ' var E = $e;',
  5664. ' p = function () {',
  5665. ' E.b = true;',
  5666. ' };',
  5667. ' } else throw $e',
  5668. ' };',
  5669. '};',
  5670. '']),
  5671. LinesToStr([
  5672. '']));
  5673. end;
  5674. procedure TTestModule.TestAnonymousProc_Nested;
  5675. begin
  5676. StartProgram(false);
  5677. Add([
  5678. 'type',
  5679. ' TProc = reference to procedure;',
  5680. ' TObject = class',
  5681. ' i: byte;',
  5682. ' procedure DoIt;',
  5683. ' end;',
  5684. 'procedure TObject.DoIt;',
  5685. 'var',
  5686. ' p: TProc;',
  5687. ' procedure Sub;',
  5688. ' begin',
  5689. ' p:=procedure',
  5690. ' begin',
  5691. ' i:=3;',
  5692. ' Self.i:=4;',
  5693. ' p:=procedure',
  5694. ' procedure SubSub;',
  5695. ' begin',
  5696. ' i:=13;',
  5697. ' Self.i:=14;',
  5698. ' end;',
  5699. ' begin',
  5700. ' i:=13;',
  5701. ' Self.i:=14;',
  5702. ' end;',
  5703. ' end;',
  5704. ' end;',
  5705. 'begin',
  5706. 'end;',
  5707. 'begin']);
  5708. ConvertProgram;
  5709. CheckSource('TestAnonymousProc_Nested',
  5710. LinesToStr([ // statements
  5711. 'rtl.createClass(this, "TObject", null, function () {',
  5712. ' this.$init = function () {',
  5713. ' this.i = 0;',
  5714. ' };',
  5715. ' this.$final = function () {',
  5716. ' };',
  5717. ' this.DoIt = function () {',
  5718. ' var $Self = this;',
  5719. ' var p = null;',
  5720. ' function Sub() {',
  5721. ' p = function () {',
  5722. ' $Self.i = 3;',
  5723. ' $Self.i = 4;',
  5724. ' p = function () {',
  5725. ' function SubSub() {',
  5726. ' $Self.i = 13;',
  5727. ' $Self.i = 14;',
  5728. ' };',
  5729. ' $Self.i = 13;',
  5730. ' $Self.i = 14;',
  5731. ' };',
  5732. ' };',
  5733. ' };',
  5734. ' };',
  5735. '});',
  5736. '']),
  5737. LinesToStr([
  5738. '']));
  5739. end;
  5740. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5741. begin
  5742. StartProgram(false);
  5743. Add([
  5744. 'type',
  5745. ' TProc = reference to procedure;',
  5746. 'function DoIt: TProc;',
  5747. ' function Sub: TProc;',
  5748. ' begin',
  5749. ' Result:=procedure',
  5750. ' begin',
  5751. ' Sub:=procedure',
  5752. ' procedure SubSub;',
  5753. ' begin',
  5754. ' Result:=nil;',
  5755. ' Sub:=nil;',
  5756. ' DoIt:=nil;',
  5757. ' end;',
  5758. ' begin',
  5759. ' Result:=nil;',
  5760. ' Sub:=nil;',
  5761. ' DoIt:=nil;',
  5762. ' end;',
  5763. ' end;',
  5764. ' end;',
  5765. 'begin',
  5766. 'end;',
  5767. 'begin']);
  5768. ConvertProgram;
  5769. CheckSource('TestAnonymousProc_NestedAssignResult',
  5770. LinesToStr([ // statements
  5771. 'this.DoIt = function () {',
  5772. ' var Result = null;',
  5773. ' function Sub() {',
  5774. ' var Result$1 = null;',
  5775. ' Result$1 = function () {',
  5776. ' Result$1 = function () {',
  5777. ' function SubSub() {',
  5778. ' Result$1 = null;',
  5779. ' Result$1 = null;',
  5780. ' Result = null;',
  5781. ' };',
  5782. ' Result$1 = null;',
  5783. ' Result$1 = null;',
  5784. ' Result = null;',
  5785. ' };',
  5786. ' };',
  5787. ' return Result$1;',
  5788. ' };',
  5789. ' return Result;',
  5790. '};',
  5791. '']),
  5792. LinesToStr([
  5793. '']));
  5794. end;
  5795. procedure TTestModule.TestAnonymousProc_Class;
  5796. begin
  5797. StartProgram(false);
  5798. Add([
  5799. 'type',
  5800. ' TProc = reference to procedure;',
  5801. ' TEvent = procedure of object;',
  5802. ' TObject = class',
  5803. ' Size: word;',
  5804. ' function GetIt: TProc;',
  5805. ' procedure DoIt; virtual; abstract;',
  5806. ' end;',
  5807. 'function TObject.GetIt: TProc;',
  5808. 'begin',
  5809. ' Result:=procedure',
  5810. ' var p: TEvent;',
  5811. ' begin',
  5812. ' Size:=Size;',
  5813. ' Size:=Self.Size;',
  5814. ' p:=@DoIt;',
  5815. ' p:[email protected];',
  5816. ' end;',
  5817. 'end;',
  5818. 'begin']);
  5819. ConvertProgram;
  5820. CheckSource('TestAnonymousProc_Class',
  5821. LinesToStr([ // statements
  5822. 'rtl.createClass(this, "TObject", null, function () {',
  5823. ' this.$init = function () {',
  5824. ' this.Size = 0;',
  5825. ' };',
  5826. ' this.$final = function () {',
  5827. ' };',
  5828. ' this.GetIt = function () {',
  5829. ' var $Self = this;',
  5830. ' var Result = null;',
  5831. ' Result = function () {',
  5832. ' var p = null;',
  5833. ' $Self.Size = $Self.Size;',
  5834. ' $Self.Size = $Self.Size;',
  5835. ' p = rtl.createCallback($Self, "DoIt");',
  5836. ' p = rtl.createCallback($Self, "DoIt");',
  5837. ' };',
  5838. ' return Result;',
  5839. ' };',
  5840. '});',
  5841. '']),
  5842. LinesToStr([
  5843. '']));
  5844. end;
  5845. procedure TTestModule.TestAnonymousProc_ForLoop;
  5846. begin
  5847. StartProgram(false);
  5848. Add([
  5849. 'type TProc = reference to procedure;',
  5850. 'procedure Foo(p: TProc);',
  5851. 'begin',
  5852. 'end;',
  5853. 'procedure DoIt;',
  5854. 'var i: word;',
  5855. ' a: word;',
  5856. 'begin',
  5857. ' for i:=1 to 10 do begin',
  5858. ' Foo(procedure begin a:=3; end);',
  5859. ' end;',
  5860. 'end;',
  5861. 'begin',
  5862. ' DoIt;']);
  5863. ConvertProgram;
  5864. CheckSource('TestAnonymousProc_ForLoop',
  5865. LinesToStr([ // statements
  5866. 'this.Foo = function (p) {',
  5867. '};',
  5868. 'this.DoIt = function () {',
  5869. ' var i = 0;',
  5870. ' var a = 0;',
  5871. ' for (i = 1; i <= 10; i++) {',
  5872. ' $mod.Foo(function () {',
  5873. ' a = 3;',
  5874. ' });',
  5875. ' };',
  5876. '};',
  5877. '']),
  5878. LinesToStr([
  5879. '$mod.DoIt();'
  5880. ]));
  5881. end;
  5882. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5883. begin
  5884. StartProgram(false);
  5885. Add([
  5886. '{$mode delphi}',
  5887. 'type',
  5888. ' TProc = reference to procedure;',
  5889. ' TFunc = reference to function(x: word): word;',
  5890. 'procedure Run;',
  5891. 'asm',
  5892. 'end;',
  5893. 'procedure Walk(p: TProc; f: TFunc);',
  5894. 'begin',
  5895. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5896. 'end;',
  5897. 'begin',
  5898. ' Walk(procedure',
  5899. ' asm',
  5900. ' console.log("a");',
  5901. ' end,',
  5902. ' function(x: word): word asm',
  5903. ' console.log("c");',
  5904. ' end);',
  5905. '']);
  5906. ConvertProgram;
  5907. CheckSource('TestAnonymousProc_AsmDelphi',
  5908. LinesToStr([ // statements
  5909. 'this.Run = function () {',
  5910. '};',
  5911. 'this.Walk = function (p, f) {',
  5912. ' $mod.Walk(function () {',
  5913. ' }, function (b) {',
  5914. ' return 1+b;',
  5915. ' });',
  5916. '};',
  5917. '']),
  5918. LinesToStr([
  5919. '$mod.Walk(function () {',
  5920. ' console.log("a");',
  5921. '}, function (x) {',
  5922. ' console.log("c");',
  5923. '});',
  5924. '']));
  5925. end;
  5926. procedure TTestModule.TestEnum_Name;
  5927. begin
  5928. StartProgram(false);
  5929. Add('type TMyEnum = (Red, Green, Blue);');
  5930. Add('var e: TMyEnum;');
  5931. Add('var f: TMyEnum = Blue;');
  5932. Add('begin');
  5933. Add(' e:=green;');
  5934. Add(' e:=default(TMyEnum);');
  5935. ConvertProgram;
  5936. CheckSource('TestEnum_Name',
  5937. LinesToStr([ // statements
  5938. 'this.TMyEnum = {',
  5939. ' "0":"Red",',
  5940. ' Red:0,',
  5941. ' "1":"Green",',
  5942. ' Green:1,',
  5943. ' "2":"Blue",',
  5944. ' Blue:2',
  5945. ' };',
  5946. 'this.e = 0;',
  5947. 'this.f = this.TMyEnum.Blue;'
  5948. ]),
  5949. LinesToStr([
  5950. '$mod.e=$mod.TMyEnum.Green;',
  5951. '$mod.e=$mod.TMyEnum.Red;'
  5952. ]));
  5953. end;
  5954. procedure TTestModule.TestEnum_Number;
  5955. begin
  5956. Converter.Options:=Converter.Options+[coEnumNumbers];
  5957. StartProgram(false);
  5958. Add('type TMyEnum = (Red, Green);');
  5959. Add('var');
  5960. Add(' e: TMyEnum;');
  5961. Add(' f: TMyEnum = Green;');
  5962. Add(' i: longint;');
  5963. Add('begin');
  5964. Add(' e:=green;');
  5965. Add(' i:=longint(e);');
  5966. ConvertProgram;
  5967. CheckSource('TestEnumNumber',
  5968. LinesToStr([ // statements
  5969. 'this.TMyEnum = {',
  5970. ' "0":"Red",',
  5971. ' Red:0,',
  5972. ' "1":"Green",',
  5973. ' Green:1',
  5974. ' };',
  5975. 'this.e = 0;',
  5976. 'this.f = 1;',
  5977. 'this.i = 0;'
  5978. ]),
  5979. LinesToStr([
  5980. '$mod.e=1;',
  5981. '$mod.i=$mod.e;'
  5982. ]));
  5983. end;
  5984. procedure TTestModule.TestEnum_ConstFail;
  5985. begin
  5986. StartProgram(false);
  5987. Add([
  5988. 'type TMyEnum = (Red = 100, Green = 101);',
  5989. 'var',
  5990. ' e: TMyEnum;',
  5991. ' f: TMyEnum = Green;',
  5992. 'begin',
  5993. ' e:=green;']);
  5994. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5995. ConvertProgram;
  5996. end;
  5997. procedure TTestModule.TestEnum_Functions;
  5998. begin
  5999. StartProgram(false);
  6000. Add([
  6001. 'type TMyEnum = (Red, Green);',
  6002. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6003. 'var',
  6004. ' v: longint;',
  6005. ' s: string;',
  6006. 'begin',
  6007. ' val(s,e,v);',
  6008. ' val(s,e,i);',
  6009. 'end;',
  6010. 'var',
  6011. ' e: TMyEnum;',
  6012. ' i: longint;',
  6013. ' s: string;',
  6014. ' b: boolean;',
  6015. 'begin',
  6016. ' i:=ord(red);',
  6017. ' i:=ord(green);',
  6018. ' i:=ord(e);',
  6019. ' i:=ord(b);',
  6020. ' e:=low(tmyenum);',
  6021. ' e:=low(e);',
  6022. ' b:=low(boolean);',
  6023. ' e:=high(tmyenum);',
  6024. ' e:=high(e);',
  6025. ' b:=high(boolean);',
  6026. ' e:=pred(green);',
  6027. ' e:=pred(e);',
  6028. ' b:=pred(b);',
  6029. ' e:=succ(red);',
  6030. ' e:=succ(e);',
  6031. ' b:=succ(b);',
  6032. ' e:=tmyenum(1);',
  6033. ' e:=tmyenum(i);',
  6034. ' s:=str(e);',
  6035. ' str(e,s);',
  6036. ' str(red,s);',
  6037. ' s:=str(e:3);',
  6038. ' writestr(s,e:3,red);',
  6039. ' val(s,e,i);',
  6040. ' i:=longint(e);']);
  6041. ConvertProgram;
  6042. CheckSource('TestEnum_Functions',
  6043. LinesToStr([ // statements
  6044. 'this.TMyEnum = {',
  6045. ' "0":"Red",',
  6046. ' Red:0,',
  6047. ' "1":"Green",',
  6048. ' Green:1',
  6049. ' };',
  6050. 'this.DoIt = function (e, i) {',
  6051. ' var v = 0;',
  6052. ' var s = "";',
  6053. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6054. ' v = w;',
  6055. ' }));',
  6056. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6057. '};',
  6058. 'this.e = 0;',
  6059. 'this.i = 0;',
  6060. 'this.s = "";',
  6061. 'this.b = false;',
  6062. '']),
  6063. LinesToStr([
  6064. '$mod.i=$mod.TMyEnum.Red;',
  6065. '$mod.i=$mod.TMyEnum.Green;',
  6066. '$mod.i=$mod.e;',
  6067. '$mod.i=$mod.b+0;',
  6068. '$mod.e=$mod.TMyEnum.Red;',
  6069. '$mod.e=$mod.TMyEnum.Red;',
  6070. '$mod.b=false;',
  6071. '$mod.e=$mod.TMyEnum.Green;',
  6072. '$mod.e=$mod.TMyEnum.Green;',
  6073. '$mod.b=true;',
  6074. '$mod.e=$mod.TMyEnum.Green-1;',
  6075. '$mod.e=$mod.e-1;',
  6076. '$mod.b=false;',
  6077. '$mod.e=$mod.TMyEnum.Red+1;',
  6078. '$mod.e=$mod.e+1;',
  6079. '$mod.b=true;',
  6080. '$mod.e=1;',
  6081. '$mod.e=$mod.i;',
  6082. '$mod.s = $mod.TMyEnum[$mod.e];',
  6083. '$mod.s = $mod.TMyEnum[$mod.e];',
  6084. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6085. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6086. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6087. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6088. ' $mod.i = v;',
  6089. '});',
  6090. '$mod.i=$mod.e;',
  6091. '']));
  6092. end;
  6093. procedure TTestModule.TestEnumRg_Functions;
  6094. begin
  6095. StartProgram(false);
  6096. Add([
  6097. 'type',
  6098. ' TEnum = (Red, Green, Blue);',
  6099. ' TEnumRg = Green..Blue;',
  6100. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6101. 'var',
  6102. ' v: longint;',
  6103. ' s: string;',
  6104. 'begin',
  6105. ' val(s,e,v);',
  6106. ' val(s,e,i);',
  6107. 'end;',
  6108. 'var',
  6109. ' e: TEnumRg;',
  6110. ' i: longint;',
  6111. ' s: string;',
  6112. 'begin',
  6113. ' i:=ord(green);',
  6114. ' i:=ord(e);',
  6115. ' e:=low(tenumrg);',
  6116. ' e:=low(e);',
  6117. ' e:=high(tenumrg);',
  6118. ' e:=high(e);',
  6119. ' e:=pred(blue);',
  6120. ' e:=pred(e);',
  6121. ' e:=succ(green);',
  6122. ' e:=succ(e);',
  6123. ' e:=tenumrg(1);',
  6124. ' e:=tenumrg(i);',
  6125. ' s:=str(e);',
  6126. ' str(e,s);',
  6127. ' str(red,s);',
  6128. ' s:=str(e:3);',
  6129. ' writestr(s,e:3,blue);',
  6130. ' val(s,e,i);',
  6131. ' i:=longint(e);']);
  6132. ConvertProgram;
  6133. CheckSource('TestEnumRg_Functions',
  6134. LinesToStr([ // statements
  6135. 'this.TEnum = {',
  6136. ' "0":"Red",',
  6137. ' Red:0,',
  6138. ' "1":"Green",',
  6139. ' Green:1,',
  6140. ' "2":"Blue",',
  6141. ' Blue:2',
  6142. ' };',
  6143. 'this.DoIt = function (e, i) {',
  6144. ' var v = 0;',
  6145. ' var s = "";',
  6146. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6147. ' v = w;',
  6148. ' }));',
  6149. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6150. '};',
  6151. 'this.e = this.TEnum.Green;',
  6152. 'this.i = 0;',
  6153. 'this.s = "";',
  6154. '']),
  6155. LinesToStr([
  6156. '$mod.i=$mod.TEnum.Green;',
  6157. '$mod.i=$mod.e;',
  6158. '$mod.e=$mod.TEnum.Green;',
  6159. '$mod.e=$mod.TEnum.Green;',
  6160. '$mod.e=$mod.TEnum.Blue;',
  6161. '$mod.e=$mod.TEnum.Blue;',
  6162. '$mod.e=$mod.TEnum.Blue-1;',
  6163. '$mod.e=$mod.e-1;',
  6164. '$mod.e=$mod.TEnum.Green+1;',
  6165. '$mod.e=$mod.e+1;',
  6166. '$mod.e=1;',
  6167. '$mod.e=$mod.i;',
  6168. '$mod.s = $mod.TEnum[$mod.e];',
  6169. '$mod.s = $mod.TEnum[$mod.e];',
  6170. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6171. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6172. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6173. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6174. ' $mod.i = v;',
  6175. '});',
  6176. '$mod.i=$mod.e;',
  6177. '']));
  6178. end;
  6179. procedure TTestModule.TestEnum_AsParams;
  6180. begin
  6181. StartProgram(false);
  6182. Add('type TEnum = (Red,Blue);');
  6183. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6184. Add('var vJ: TEnum;');
  6185. Add('begin');
  6186. Add(' vg:=vg;');
  6187. Add(' vj:=vh;');
  6188. Add(' vi:=vi;');
  6189. Add(' doit(vg,vg,vg);');
  6190. Add(' doit(vh,vh,vj);');
  6191. Add(' doit(vi,vi,vi);');
  6192. Add(' doit(vj,vj,vj);');
  6193. Add('end;');
  6194. Add('var i: TEnum;');
  6195. Add('begin');
  6196. Add(' doit(i,i,i);');
  6197. ConvertProgram;
  6198. CheckSource('TestEnum_AsParams',
  6199. LinesToStr([ // statements
  6200. 'this.TEnum = {',
  6201. ' "0": "Red",',
  6202. ' Red: 0,',
  6203. ' "1": "Blue",',
  6204. ' Blue: 1',
  6205. '};',
  6206. 'this.DoIt = function (vG,vH,vI) {',
  6207. ' var vJ = 0;',
  6208. ' vG = vG;',
  6209. ' vJ = vH;',
  6210. ' vI.set(vI.get());',
  6211. ' $mod.DoIt(vG, vG, {',
  6212. ' get: function () {',
  6213. ' return vG;',
  6214. ' },',
  6215. ' set: function (v) {',
  6216. ' vG = v;',
  6217. ' }',
  6218. ' });',
  6219. ' $mod.DoIt(vH, vH, {',
  6220. ' get: function () {',
  6221. ' return vJ;',
  6222. ' },',
  6223. ' set: function (v) {',
  6224. ' vJ = v;',
  6225. ' }',
  6226. ' });',
  6227. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6228. ' $mod.DoIt(vJ, vJ, {',
  6229. ' get: function () {',
  6230. ' return vJ;',
  6231. ' },',
  6232. ' set: function (v) {',
  6233. ' vJ = v;',
  6234. ' }',
  6235. ' });',
  6236. '};',
  6237. 'this.i = 0;'
  6238. ]),
  6239. LinesToStr([
  6240. '$mod.DoIt($mod.i,$mod.i,{',
  6241. ' p: $mod,',
  6242. ' get: function () {',
  6243. ' return this.p.i;',
  6244. ' },',
  6245. ' set: function (v) {',
  6246. ' this.p.i = v;',
  6247. ' }',
  6248. '});'
  6249. ]));
  6250. end;
  6251. procedure TTestModule.TestEnumRange_Array;
  6252. begin
  6253. StartProgram(false);
  6254. Add([
  6255. 'type',
  6256. ' TEnum = (Red, Green, Blue);',
  6257. ' TEnumRg = green..blue;',
  6258. ' TArr = array[TEnumRg] of byte;',
  6259. ' TArr2 = array[green..blue] of byte;',
  6260. 'var',
  6261. ' a: TArr;',
  6262. ' b: TArr = (3,4);',
  6263. ' c: TArr2 = (5,6);',
  6264. 'begin',
  6265. ' a[green] := b[blue];',
  6266. ' c[green] := c[blue];',
  6267. '']);
  6268. ConvertProgram;
  6269. CheckSource('TestEnumRange_Array',
  6270. LinesToStr([ // statements
  6271. 'this.TEnum = {',
  6272. ' "0": "Red",',
  6273. ' Red: 0,',
  6274. ' "1": "Green",',
  6275. ' Green: 1,',
  6276. ' "2": "Blue",',
  6277. ' Blue: 2',
  6278. '};',
  6279. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6280. 'this.b = [3, 4];',
  6281. 'this.c = [5, 6];',
  6282. '']),
  6283. LinesToStr([
  6284. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6285. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6286. '']));
  6287. end;
  6288. procedure TTestModule.TestEnum_ForIn;
  6289. begin
  6290. StartProgram(false);
  6291. Add([
  6292. 'type',
  6293. ' TEnum = (Red, Green, Blue);',
  6294. ' TEnumRg = green..blue;',
  6295. ' TArr = array[TEnum] of byte;',
  6296. ' TArrRg = array[TEnumRg] of byte;',
  6297. 'var',
  6298. ' e: TEnum;',
  6299. ' a1: TArr = (3,4,5);',
  6300. ' a2: TArrRg = (11,12);',
  6301. ' b: byte;',
  6302. 'begin',
  6303. ' for e in TEnum do ;',
  6304. ' for e in TEnumRg do ;',
  6305. ' for e in TArr do ;',
  6306. ' for e in TArrRg do ;',
  6307. ' for b in a1 do ;',
  6308. ' for b in a2 do ;',
  6309. '']);
  6310. ConvertProgram;
  6311. CheckSource('TestEnum_ForIn',
  6312. LinesToStr([ // statements
  6313. 'this.TEnum = {',
  6314. ' "0": "Red",',
  6315. ' Red: 0,',
  6316. ' "1": "Green",',
  6317. ' Green: 1,',
  6318. ' "2": "Blue",',
  6319. ' Blue: 2',
  6320. '};',
  6321. 'this.e = 0;',
  6322. 'this.a1 = [3, 4, 5];',
  6323. 'this.a2 = [11, 12];',
  6324. 'this.b = 0;',
  6325. '']),
  6326. LinesToStr([
  6327. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6328. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6329. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6330. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6331. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6332. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6333. '']));
  6334. end;
  6335. procedure TTestModule.TestEnum_ScopedNumber;
  6336. begin
  6337. Converter.Options:=Converter.Options+[coEnumNumbers];
  6338. StartProgram(false);
  6339. Add([
  6340. 'type',
  6341. ' TEnum = (Red, Green);',
  6342. 'var',
  6343. ' e: TEnum;',
  6344. 'begin',
  6345. ' e:=TEnum.Green;',
  6346. '']);
  6347. ConvertProgram;
  6348. CheckSource('TestEnum_ScopedNumber',
  6349. LinesToStr([ // statements
  6350. 'this.TEnum = {',
  6351. ' "0": "Red",',
  6352. ' Red: 0,',
  6353. ' "1": "Green",',
  6354. ' Green: 1',
  6355. '};',
  6356. 'this.e = 0;',
  6357. '']),
  6358. LinesToStr([
  6359. '$mod.e = 1;']));
  6360. end;
  6361. procedure TTestModule.TestEnum_InFunction;
  6362. begin
  6363. StartProgram(false);
  6364. Add([
  6365. 'const TEnum = 3;',
  6366. 'procedure DoIt;',
  6367. 'type',
  6368. ' TEnum = (Red, Green, Blue);',
  6369. ' procedure Sub;',
  6370. ' type',
  6371. ' TEnumSub = (Left, Right);',
  6372. ' var',
  6373. ' es: TEnumSub;',
  6374. ' begin',
  6375. ' es:=Left;',
  6376. ' end;',
  6377. 'var',
  6378. ' e, e2: TEnum;',
  6379. 'begin',
  6380. ' if e in [red,blue] then e2:=e;',
  6381. 'end;',
  6382. 'begin']);
  6383. ConvertProgram;
  6384. CheckSource('TestEnum_InFunction',
  6385. LinesToStr([ // statements
  6386. 'this.TEnum = 3;',
  6387. 'var TEnum$1 = {',
  6388. ' "0":"Red",',
  6389. ' Red:0,',
  6390. ' "1":"Green",',
  6391. ' Green:1,',
  6392. ' "2":"Blue",',
  6393. ' Blue:2',
  6394. ' };',
  6395. 'var TEnumSub = {',
  6396. ' "0": "Left",',
  6397. ' Left: 0,',
  6398. ' "1": "Right",',
  6399. ' Right: 1',
  6400. '};',
  6401. 'this.DoIt = function () {',
  6402. ' function Sub() {',
  6403. ' var es = 0;',
  6404. ' es = TEnumSub.Left;',
  6405. ' };',
  6406. ' var e = 0;',
  6407. ' var e2 = 0;',
  6408. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6409. '};',
  6410. '']),
  6411. LinesToStr([
  6412. '']));
  6413. end;
  6414. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6415. begin
  6416. StartUnit(true);
  6417. Add([
  6418. 'interface',
  6419. 'var color: (red, green);',
  6420. 'implementation',
  6421. 'initialization',
  6422. ' color:=green;',
  6423. '']);
  6424. ConvertUnit;
  6425. CheckSource('TestEnum_Name_Anonymous_Unit',
  6426. LinesToStr([
  6427. 'this.color$a = {',
  6428. ' "0": "red",',
  6429. ' red: 0,',
  6430. ' "1": "green",',
  6431. ' green: 1',
  6432. '};',
  6433. 'this.color = 0;',
  6434. '']),
  6435. LinesToStr([ // this.$init
  6436. '$mod.color = $mod.color$a.green;',
  6437. '']),
  6438. LinesToStr([ // implementation
  6439. '']) );
  6440. end;
  6441. procedure TTestModule.TestSet_Enum;
  6442. begin
  6443. StartProgram(false);
  6444. Add([
  6445. 'type',
  6446. ' TColor = (Red, Green, Blue);',
  6447. ' TColors = set of TColor;',
  6448. 'var',
  6449. ' c: TColor;',
  6450. ' s: TColors;',
  6451. ' t: TColors = [];',
  6452. ' u: TColors = [Red];',
  6453. 'begin',
  6454. ' s:=[];',
  6455. ' s:=[Green];',
  6456. ' s:=[Green,Blue];',
  6457. ' s:=[Red..Blue];',
  6458. ' s:=[Red,Green..Blue];',
  6459. ' s:=[Red,c];',
  6460. ' s:=t;',
  6461. ' s:=default(TColors);',
  6462. '']);
  6463. ConvertProgram;
  6464. CheckSource('TestSet',
  6465. LinesToStr([ // statements
  6466. 'this.TColor = {',
  6467. ' "0":"Red",',
  6468. ' Red:0,',
  6469. ' "1":"Green",',
  6470. ' Green:1,',
  6471. ' "2":"Blue",',
  6472. ' Blue:2',
  6473. ' };',
  6474. 'this.c = 0;',
  6475. 'this.s = {};',
  6476. 'this.t = {};',
  6477. 'this.u = rtl.createSet(this.TColor.Red);'
  6478. ]),
  6479. LinesToStr([
  6480. '$mod.s={};',
  6481. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6482. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6483. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6484. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6485. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6486. '$mod.s=rtl.refSet($mod.t);',
  6487. '$mod.s={};',
  6488. '']));
  6489. end;
  6490. procedure TTestModule.TestSet_Operators;
  6491. begin
  6492. StartProgram(false);
  6493. Add('type');
  6494. Add(' TColor = (Red, Green, Blue);');
  6495. Add(' TColors = set of tcolor;');
  6496. Add('var');
  6497. Add(' vC: TColor;');
  6498. Add(' vS: TColors;');
  6499. Add(' vT: TColors;');
  6500. Add(' vU: TColors;');
  6501. Add(' B: boolean;');
  6502. Add('begin');
  6503. Add(' include(vs,green);');
  6504. Add(' exclude(vs,vc);');
  6505. Add(' vs:=vt+vu;');
  6506. Add(' vs:=vt+[red];');
  6507. Add(' vs:=[red]+vt;');
  6508. Add(' vs:=[red]+[green];');
  6509. Add(' vs:=vt-vu;');
  6510. Add(' vs:=vt-[red];');
  6511. Add(' vs:=[red]-vt;');
  6512. Add(' vs:=[red]-[green];');
  6513. Add(' vs:=vt*vu;');
  6514. Add(' vs:=vt*[red];');
  6515. Add(' vs:=[red]*vt;');
  6516. Add(' vs:=[red]*[green];');
  6517. Add(' vs:=vt><vu;');
  6518. Add(' vs:=vt><[red];');
  6519. Add(' vs:=[red]><vt;');
  6520. Add(' vs:=[red]><[green];');
  6521. Add(' b:=vt=vu;');
  6522. Add(' b:=vt=[red];');
  6523. Add(' b:=[red]=vt;');
  6524. Add(' b:=[red]=[green];');
  6525. Add(' b:=vt<>vu;');
  6526. Add(' b:=vt<>[red];');
  6527. Add(' b:=[red]<>vt;');
  6528. Add(' b:=[red]<>[green];');
  6529. Add(' b:=vt<=vu;');
  6530. Add(' b:=vt<=[red];');
  6531. Add(' b:=[red]<=vt;');
  6532. Add(' b:=[red]<=[green];');
  6533. Add(' b:=vt>=vu;');
  6534. Add(' b:=vt>=[red];');
  6535. Add(' b:=[red]>=vt;');
  6536. Add(' b:=[red]>=[green];');
  6537. ConvertProgram;
  6538. CheckSource('TestSet_Operators',
  6539. LinesToStr([ // statements
  6540. 'this.TColor = {',
  6541. ' "0":"Red",',
  6542. ' Red:0,',
  6543. ' "1":"Green",',
  6544. ' Green:1,',
  6545. ' "2":"Blue",',
  6546. ' Blue:2',
  6547. ' };',
  6548. 'this.vC = 0;',
  6549. 'this.vS = {};',
  6550. 'this.vT = {};',
  6551. 'this.vU = {};',
  6552. 'this.B = false;'
  6553. ]),
  6554. LinesToStr([
  6555. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6556. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6557. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6558. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6559. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6560. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6561. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6562. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6563. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6564. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6565. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6566. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6567. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6568. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6569. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6570. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6571. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6572. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6573. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6574. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6575. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6576. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6577. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6578. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6579. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6580. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6581. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6582. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6583. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6584. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6585. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6586. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6587. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6588. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6589. '']));
  6590. end;
  6591. procedure TTestModule.TestSet_Operator_In;
  6592. begin
  6593. StartProgram(false);
  6594. Add([
  6595. 'type',
  6596. ' TColor = (Red, Green, Blue);',
  6597. ' TColors = set of tcolor;',
  6598. ' TColorRg = green..blue;',
  6599. 'var',
  6600. ' vC: tcolor;',
  6601. ' vT: tcolors;',
  6602. ' B: boolean;',
  6603. ' rg: TColorRg;',
  6604. 'begin',
  6605. ' b:=red in vt;',
  6606. ' b:=vc in vt;',
  6607. ' b:=green in [red..blue];',
  6608. ' b:=vc in [red..blue];',
  6609. ' ',
  6610. ' if red in vt then ;',
  6611. ' while vC in vt do ;',
  6612. ' repeat',
  6613. ' until vC in vt;',
  6614. ' if rg in [green..blue] then ;',
  6615. '']);
  6616. ConvertProgram;
  6617. CheckSource('TestSet_Operator_In',
  6618. LinesToStr([ // statements
  6619. 'this.TColor = {',
  6620. ' "0":"Red",',
  6621. ' Red:0,',
  6622. ' "1":"Green",',
  6623. ' Green:1,',
  6624. ' "2":"Blue",',
  6625. ' Blue:2',
  6626. ' };',
  6627. 'this.vC = 0;',
  6628. 'this.vT = {};',
  6629. 'this.B = false;',
  6630. 'this.rg = this.TColor.Green;',
  6631. '']),
  6632. LinesToStr([
  6633. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6634. '$mod.B = $mod.vC in $mod.vT;',
  6635. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6636. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6637. 'if ($mod.TColor.Red in $mod.vT) ;',
  6638. 'while ($mod.vC in $mod.vT) {',
  6639. '};',
  6640. 'do {',
  6641. '} while (!($mod.vC in $mod.vT));',
  6642. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6643. '']));
  6644. end;
  6645. procedure TTestModule.TestSet_Functions;
  6646. begin
  6647. StartProgram(false);
  6648. Add('type');
  6649. Add(' TMyEnum = (Red, Green);');
  6650. Add(' TMyEnums = set of TMyEnum;');
  6651. Add('var');
  6652. Add(' e: TMyEnum;');
  6653. Add(' s: TMyEnums;');
  6654. Add('begin');
  6655. Add(' e:=Low(TMyEnums);');
  6656. Add(' e:=Low(s);');
  6657. Add(' e:=High(TMyEnums);');
  6658. Add(' e:=High(s);');
  6659. ConvertProgram;
  6660. CheckSource('TestSetFunctions',
  6661. LinesToStr([ // statements
  6662. 'this.TMyEnum = {',
  6663. ' "0":"Red",',
  6664. ' Red:0,',
  6665. ' "1":"Green",',
  6666. ' Green:1',
  6667. ' };',
  6668. 'this.e = 0;',
  6669. 'this.s = {};'
  6670. ]),
  6671. LinesToStr([
  6672. '$mod.e=$mod.TMyEnum.Red;',
  6673. '$mod.e=$mod.TMyEnum.Red;',
  6674. '$mod.e=$mod.TMyEnum.Green;',
  6675. '$mod.e=$mod.TMyEnum.Green;',
  6676. '']));
  6677. end;
  6678. procedure TTestModule.TestSet_PassAsArgClone;
  6679. begin
  6680. StartProgram(false);
  6681. Add('type');
  6682. Add(' TMyEnum = (Red, Green);');
  6683. Add(' TMyEnums = set of TMyEnum;');
  6684. Add('procedure DoDefault(s: tmyenums); begin end;');
  6685. Add('procedure DoConst(const s: tmyenums); begin end;');
  6686. Add('var');
  6687. Add(' aSet: tmyenums;');
  6688. Add('begin');
  6689. Add(' dodefault(aset);');
  6690. Add(' doconst(aset);');
  6691. ConvertProgram;
  6692. CheckSource('TestSetFunctions',
  6693. LinesToStr([ // statements
  6694. 'this.TMyEnum = {',
  6695. ' "0":"Red",',
  6696. ' Red:0,',
  6697. ' "1":"Green",',
  6698. ' Green:1',
  6699. ' };',
  6700. 'this.DoDefault = function (s) {',
  6701. '};',
  6702. 'this.DoConst = function (s) {',
  6703. '};',
  6704. 'this.aSet = {};'
  6705. ]),
  6706. LinesToStr([
  6707. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6708. '$mod.DoConst($mod.aSet);',
  6709. '']));
  6710. end;
  6711. procedure TTestModule.TestSet_AsParams;
  6712. begin
  6713. StartProgram(false);
  6714. Add([
  6715. 'type TEnum = (Red,Blue);',
  6716. 'type TEnums = set of TEnum;',
  6717. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6718. 'var vJ: TEnums;',
  6719. 'begin',
  6720. ' Include(vg,red);',
  6721. ' Include(result,blue);',
  6722. ' vg:=vg;',
  6723. ' vj:=vh;',
  6724. ' vi:=vi;',
  6725. ' doit(vg,vg,vg);',
  6726. ' doit(vh,vh,vj);',
  6727. ' doit(vi,vi,vi);',
  6728. ' doit(vj,vj,vj);',
  6729. 'end;',
  6730. 'var i: TEnums;',
  6731. 'begin',
  6732. ' doit(i,i,i);']);
  6733. ConvertProgram;
  6734. CheckSource('TestSet_AsParams',
  6735. LinesToStr([ // statements
  6736. 'this.TEnum = {',
  6737. ' "0": "Red",',
  6738. ' Red: 0,',
  6739. ' "1": "Blue",',
  6740. ' Blue: 1',
  6741. '};',
  6742. 'this.DoIt = function (vG,vH,vI) {',
  6743. ' var Result = {};',
  6744. ' var vJ = {};',
  6745. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6746. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6747. ' vG = rtl.refSet(vG);',
  6748. ' vJ = rtl.refSet(vH);',
  6749. ' vI.set(rtl.refSet(vI.get()));',
  6750. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6751. ' get: function () {',
  6752. ' return vG;',
  6753. ' },',
  6754. ' set: function (v) {',
  6755. ' vG = v;',
  6756. ' }',
  6757. ' });',
  6758. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6759. ' get: function () {',
  6760. ' return vJ;',
  6761. ' },',
  6762. ' set: function (v) {',
  6763. ' vJ = v;',
  6764. ' }',
  6765. ' });',
  6766. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6767. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6768. ' get: function () {',
  6769. ' return vJ;',
  6770. ' },',
  6771. ' set: function (v) {',
  6772. ' vJ = v;',
  6773. ' }',
  6774. ' });',
  6775. ' return Result;',
  6776. '};',
  6777. 'this.i = {};'
  6778. ]),
  6779. LinesToStr([
  6780. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6781. ' p: $mod,',
  6782. ' get: function () {',
  6783. ' return this.p.i;',
  6784. ' },',
  6785. ' set: function (v) {',
  6786. ' this.p.i = v;',
  6787. ' }',
  6788. '});'
  6789. ]));
  6790. end;
  6791. procedure TTestModule.TestSet_Property;
  6792. begin
  6793. StartProgram(false);
  6794. Add('type');
  6795. Add(' TEnum = (Red,Blue);');
  6796. Add(' TEnums = set of TEnum;');
  6797. Add(' TObject = class');
  6798. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6799. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6800. Add(' property Colors: TEnums read GetColors write SetColors;');
  6801. Add(' end;');
  6802. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6803. Add('begin end;');
  6804. Add('var Obj: TObject;');
  6805. Add('begin');
  6806. Add(' Include(Obj.Colors,Red);');
  6807. Add(' Exclude(Obj.Colors,Red);');
  6808. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6809. ConvertProgram;
  6810. CheckSource('TestSet_Property',
  6811. LinesToStr([ // statements
  6812. 'this.TEnum = {',
  6813. ' "0": "Red",',
  6814. ' Red: 0,',
  6815. ' "1": "Blue",',
  6816. ' Blue: 1',
  6817. '};',
  6818. 'rtl.createClass(this, "TObject", null, function () {',
  6819. ' this.$init = function () {',
  6820. ' };',
  6821. ' this.$final = function () {',
  6822. ' };',
  6823. '});',
  6824. 'this.DoIt = function (i, j, k, l) {',
  6825. '};',
  6826. 'this.Obj = null;',
  6827. '']),
  6828. LinesToStr([
  6829. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6830. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6831. '']));
  6832. end;
  6833. procedure TTestModule.TestSet_EnumConst;
  6834. begin
  6835. StartProgram(false);
  6836. Add([
  6837. 'type',
  6838. ' TEnum = (Red,Blue);',
  6839. ' TEnums = set of TEnum;',
  6840. 'const',
  6841. ' Orange = red;',
  6842. 'var',
  6843. ' Enum: tenum;',
  6844. ' Enums: tenums;',
  6845. 'begin',
  6846. ' Include(enums,orange);',
  6847. ' Exclude(enums,orange);',
  6848. ' if orange in enums then;',
  6849. ' if orange in [orange,red] then;']);
  6850. ConvertProgram;
  6851. CheckSource('TestSet_EnumConst',
  6852. LinesToStr([ // statements
  6853. 'this.TEnum = {',
  6854. ' "0": "Red",',
  6855. ' Red: 0,',
  6856. ' "1": "Blue",',
  6857. ' Blue: 1',
  6858. '};',
  6859. 'this.Orange = this.TEnum.Red;',
  6860. 'this.Enum = 0;',
  6861. 'this.Enums = {};',
  6862. '']),
  6863. LinesToStr([
  6864. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6865. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6866. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6867. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6868. '']));
  6869. end;
  6870. procedure TTestModule.TestSet_IntConst;
  6871. begin
  6872. StartProgram(false);
  6873. Add([
  6874. 'type',
  6875. ' TEnums = set of Byte;',
  6876. 'const',
  6877. ' Orange = 0;',
  6878. 'var',
  6879. ' Enum: byte;',
  6880. ' Enums: tenums;',
  6881. 'begin',
  6882. ' Enum:=ord(1);',
  6883. ' Enums:=[];',
  6884. ' Enums:=[0];',
  6885. ' Enums:=[1..2];',
  6886. //' Include(enums,orange);',
  6887. //' Exclude(enums,orange);',
  6888. ' if orange in enums then;',
  6889. ' if orange in [orange,1] then;']);
  6890. ConvertProgram;
  6891. CheckSource('TestSet_IntConst',
  6892. LinesToStr([ // statements
  6893. 'this.Orange = 0;',
  6894. 'this.Enum = 0;',
  6895. 'this.Enums = {};',
  6896. '']),
  6897. LinesToStr([
  6898. '$mod.Enum = 1;',
  6899. '$mod.Enums = {};',
  6900. '$mod.Enums = rtl.createSet(0);',
  6901. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6902. 'if (0 in $mod.Enums) ;',
  6903. 'if (0 in rtl.createSet(0, 1)) ;',
  6904. '']));
  6905. end;
  6906. procedure TTestModule.TestSet_IntRange;
  6907. begin
  6908. StartProgram(false);
  6909. Add([
  6910. 'type',
  6911. ' TRange = 1..3;',
  6912. ' TEnums = set of TRange;',
  6913. 'const',
  6914. ' Orange = 2;',
  6915. 'var',
  6916. ' Enum: byte;',
  6917. ' Enums: TEnums;',
  6918. 'begin',
  6919. ' Enums:=[];',
  6920. ' Enums:=[1];',
  6921. ' Enums:=[2..3];',
  6922. ' Include(enums,orange);',
  6923. ' Exclude(enums,orange);',
  6924. ' if orange in enums then;',
  6925. ' if orange in [orange,1] then;']);
  6926. ConvertProgram;
  6927. CheckSource('TestSet_IntRange',
  6928. LinesToStr([ // statements
  6929. 'this.Orange = 2;',
  6930. 'this.Enum = 0;',
  6931. 'this.Enums = {};',
  6932. '']),
  6933. LinesToStr([
  6934. '$mod.Enums = {};',
  6935. '$mod.Enums = rtl.createSet(1);',
  6936. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6937. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6938. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6939. 'if (2 in $mod.Enums) ;',
  6940. 'if (2 in rtl.createSet(2, 1)) ;',
  6941. '']));
  6942. end;
  6943. procedure TTestModule.TestSet_AnonymousEnumType;
  6944. begin
  6945. StartProgram(false);
  6946. Add('type');
  6947. Add(' TFlags = set of (red, green);');
  6948. Add('const');
  6949. Add(' favorite = red;');
  6950. Add('var');
  6951. Add(' f: TFlags;');
  6952. Add(' i: longint;');
  6953. Add('begin');
  6954. Add(' Include(f,red);');
  6955. Add(' Include(f,favorite);');
  6956. Add(' i:=ord(red);');
  6957. Add(' i:=ord(favorite);');
  6958. Add(' i:=ord(low(TFlags));');
  6959. Add(' i:=ord(low(f));');
  6960. Add(' i:=ord(low(favorite));');
  6961. Add(' i:=ord(high(TFlags));');
  6962. Add(' i:=ord(high(f));');
  6963. Add(' i:=ord(high(favorite));');
  6964. Add(' f:=[green,favorite];');
  6965. ConvertProgram;
  6966. CheckSource('TestSet_AnonymousEnumType',
  6967. LinesToStr([ // statements
  6968. 'this.TFlags$a = {',
  6969. ' "0": "red",',
  6970. ' red: 0,',
  6971. ' "1": "green",',
  6972. ' green: 1',
  6973. '};',
  6974. 'this.favorite = this.TFlags$a.red;',
  6975. 'this.f = {};',
  6976. 'this.i = 0;',
  6977. '']),
  6978. LinesToStr([
  6979. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6980. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6981. '$mod.i = $mod.TFlags$a.red;',
  6982. '$mod.i = $mod.TFlags$a.red;',
  6983. '$mod.i = $mod.TFlags$a.red;',
  6984. '$mod.i = $mod.TFlags$a.red;',
  6985. '$mod.i = $mod.TFlags$a.red;',
  6986. '$mod.i = $mod.TFlags$a.green;',
  6987. '$mod.i = $mod.TFlags$a.green;',
  6988. '$mod.i = $mod.TFlags$a.green;',
  6989. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6990. '']));
  6991. end;
  6992. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6993. begin
  6994. exit;
  6995. StartProgram(false);
  6996. Add([
  6997. 'type',
  6998. ' TAtoZ = ''A''..''Z'';',
  6999. ' TSetOfAZ = set of TAtoZ;',
  7000. 'var',
  7001. ' c: char;',
  7002. ' a: TAtoZ;',
  7003. ' s: TSetOfAZ = [''P'',''A''];',
  7004. ' i: longint;',
  7005. 'begin',
  7006. ' Include(s,''S'');',
  7007. ' Include(s,c);',
  7008. ' Include(s,a);',
  7009. ' c:=low(TAtoZ);',
  7010. ' i:=ord(low(TAtoZ));',
  7011. ' a:=high(TAtoZ);',
  7012. ' a:=high(TSetOfAtoZ);',
  7013. ' s:=[a,c,''M''];',
  7014. '']);
  7015. ConvertProgram;
  7016. CheckSource('TestSet_AnonymousEnumTypeChar',
  7017. LinesToStr([ // statements
  7018. '']),
  7019. LinesToStr([
  7020. '']));
  7021. end;
  7022. procedure TTestModule.TestSet_ConstEnum;
  7023. begin
  7024. StartProgram(false);
  7025. Add([
  7026. 'type',
  7027. ' TEnum = (red,blue,green);',
  7028. ' TEnums = set of TEnum;',
  7029. 'const',
  7030. ' teAny = [low(TEnum)..high(TEnum)];',
  7031. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7032. 'var',
  7033. ' e: TEnum;',
  7034. ' s: TEnums;',
  7035. 'begin',
  7036. ' if blue in teAny then;',
  7037. ' if blue in teAny+[e] then;',
  7038. ' if blue in teAny+teRedBlue then;',
  7039. ' if e in [red,blue] then;',
  7040. ' s:=teAny;',
  7041. ' s:=teAny+[e];',
  7042. ' s:=[e]+teAny;',
  7043. ' s:=teAny+teRedBlue;',
  7044. ' s:=teAny+teRedBlue+[e];',
  7045. '']);
  7046. ConvertProgram;
  7047. CheckSource('TestSet_ConstEnum',
  7048. LinesToStr([ // statements
  7049. 'this.TEnum = {',
  7050. ' "0": "red",',
  7051. ' red: 0,',
  7052. ' "1": "blue",',
  7053. ' blue: 1,',
  7054. ' "2": "green",',
  7055. ' green: 2',
  7056. '};',
  7057. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7058. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7059. 'this.e = 0;',
  7060. 'this.s = {};',
  7061. '']),
  7062. LinesToStr([
  7063. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7064. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7065. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7066. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7067. '$mod.s = rtl.refSet($mod.teAny);',
  7068. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7069. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7070. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7071. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7072. '']));
  7073. end;
  7074. procedure TTestModule.TestSet_ConstChar;
  7075. begin
  7076. StartProgram(false);
  7077. Add([
  7078. 'const',
  7079. ' LowChars = [''a''..''z''];',
  7080. ' Chars = LowChars+[''A''..''Z''];',
  7081. ' sc = [''А'', ''Я''];',
  7082. 'var',
  7083. ' c: char;',
  7084. ' s: string;',
  7085. 'begin',
  7086. ' if c in lowchars then ;',
  7087. ' if ''a'' in lowchars then ;',
  7088. ' if s[1] in lowchars then ;',
  7089. ' if c in chars then ;',
  7090. ' if c in [''a''..''z'',''_''] then ;',
  7091. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7092. ' if ''Я'' in sc then ;',
  7093. ' if 3=ord('' '') then ;',
  7094. '']);
  7095. ConvertProgram;
  7096. CheckSource('TestSet_ConstChar',
  7097. LinesToStr([ // statements
  7098. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7099. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7100. 'this.sc = rtl.createSet(1040, 1071);',
  7101. 'this.c = "";',
  7102. 'this.s = "";',
  7103. '']),
  7104. LinesToStr([
  7105. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7106. 'if (97 in $mod.LowChars) ;',
  7107. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7108. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7109. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7110. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7111. 'if (1071 in $mod.sc) ;',
  7112. 'if (3 === 32) ;',
  7113. '']));
  7114. end;
  7115. procedure TTestModule.TestSet_ConstInt;
  7116. begin
  7117. StartProgram(false);
  7118. Add([
  7119. 'const',
  7120. ' Months = [1..12];',
  7121. ' Mirror = [-12..-1]+Months;',
  7122. 'var',
  7123. ' i: smallint;',
  7124. 'begin',
  7125. ' if 3 in Months then;',
  7126. ' if i in Months+[i] then;',
  7127. ' if i in Months+Mirror then;',
  7128. ' if i in [4..6,8] then;',
  7129. '']);
  7130. ConvertProgram;
  7131. CheckSource('TestSet_ConstInt',
  7132. LinesToStr([ // statements
  7133. 'this.Months = rtl.createSet(null, 1, 12);',
  7134. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7135. 'this.i = 0;',
  7136. '']),
  7137. LinesToStr([
  7138. 'if (3 in $mod.Months) ;',
  7139. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7140. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7141. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7142. '']));
  7143. end;
  7144. procedure TTestModule.TestSet_InFunction;
  7145. begin
  7146. StartProgram(false);
  7147. Add([
  7148. 'const',
  7149. ' TEnum = 3;',
  7150. ' TSetOfEnum = 4;',
  7151. ' TSetOfAno = 5;',
  7152. 'procedure DoIt;',
  7153. 'type',
  7154. ' TEnum = (red, blue);',
  7155. ' TSetOfEnum = set of TEnum;',
  7156. ' TSetOfAno = set of (up,down);',
  7157. 'var',
  7158. ' e: TEnum;',
  7159. ' se: TSetOfEnum;',
  7160. ' sa: TSetOfAno;',
  7161. 'begin',
  7162. ' se:=[e];',
  7163. ' sa:=[up];',
  7164. 'end;',
  7165. 'begin',
  7166. '']);
  7167. ConvertProgram;
  7168. CheckSource('TestSet_InFunction',
  7169. LinesToStr([ // statements
  7170. 'this.TEnum = 3;',
  7171. 'this.TSetOfEnum = 4;',
  7172. 'this.TSetOfAno = 5;',
  7173. 'var TEnum$1 = {',
  7174. ' "0": "red",',
  7175. ' red: 0,',
  7176. ' "1": "blue",',
  7177. ' blue: 1',
  7178. '};',
  7179. 'var TSetOfAno$a = {',
  7180. ' "0": "up",',
  7181. ' up: 0,',
  7182. ' "1": "down",',
  7183. ' down: 1',
  7184. '};',
  7185. 'this.DoIt = function () {',
  7186. ' var e = 0;',
  7187. ' var se = {};',
  7188. ' var sa = {};',
  7189. ' se = rtl.createSet(e);',
  7190. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7191. '};',
  7192. '']),
  7193. LinesToStr([
  7194. '']));
  7195. end;
  7196. procedure TTestModule.TestSet_ForIn;
  7197. begin
  7198. StartProgram(false);
  7199. Add([
  7200. 'type',
  7201. ' TEnum = (Red, Green, Blue);',
  7202. ' TEnumRg = green..blue;',
  7203. ' TSetOfEnum = set of TEnum;',
  7204. ' TSetOfEnumRg = set of TEnumRg;',
  7205. 'var',
  7206. ' e, e2: TEnum;',
  7207. ' er: TEnum;',
  7208. ' s: TSetOfEnum;',
  7209. 'begin',
  7210. ' for e in TSetOfEnum do ;',
  7211. ' for e in TSetOfEnumRg do ;',
  7212. ' for e in [] do e2:=e;',
  7213. ' for e in [red..green] do e2:=e;',
  7214. ' for e in [green,blue] do e2:=e;',
  7215. ' for e in [red,blue] do e2:=e;',
  7216. ' for e in s do e2:=e;',
  7217. ' for er in TSetOfEnumRg do ;',
  7218. '']);
  7219. ConvertProgram;
  7220. CheckSource('TestSet_ForIn',
  7221. LinesToStr([ // statements
  7222. 'this.TEnum = {',
  7223. ' "0":"Red",',
  7224. ' Red:0,',
  7225. ' "1":"Green",',
  7226. ' Green:1,',
  7227. ' "2":"Blue",',
  7228. ' Blue:2',
  7229. ' };',
  7230. 'this.e = 0;',
  7231. 'this.e2 = 0;',
  7232. 'this.er = 0;',
  7233. 'this.s = {};',
  7234. '']),
  7235. LinesToStr([
  7236. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7237. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7238. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7239. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7240. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7241. 'for (var $l in $mod.s){',
  7242. ' $mod.e = +$l;',
  7243. ' $mod.e2 = $mod.e;',
  7244. '};',
  7245. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7246. '']));
  7247. end;
  7248. procedure TTestModule.TestNestBegin;
  7249. begin
  7250. StartProgram(false);
  7251. Add('begin');
  7252. Add(' begin');
  7253. Add(' begin');
  7254. Add(' end;');
  7255. Add(' begin');
  7256. Add(' if true then ;');
  7257. Add(' end;');
  7258. Add(' end;');
  7259. ConvertProgram;
  7260. CheckSource('TestNestBegin',
  7261. '',
  7262. 'if (true) ;');
  7263. end;
  7264. procedure TTestModule.TestUnitImplVars;
  7265. begin
  7266. StartUnit(false);
  7267. Add('interface');
  7268. Add('implementation');
  7269. Add('var');
  7270. Add(' V1:longint;');
  7271. Add(' V2:longint = 3;');
  7272. Add(' V3:string = ''abc'';');
  7273. ConvertUnit;
  7274. CheckSource('TestUnitImplVars',
  7275. LinesToStr([ // statements
  7276. 'var $impl = $mod.$impl;',
  7277. '']),
  7278. '', // this.$init
  7279. LinesToStr([ // implementation
  7280. '$impl.V1 = 0;',
  7281. '$impl.V2 = 3;',
  7282. '$impl.V3 = "abc";',
  7283. '']) );
  7284. end;
  7285. procedure TTestModule.TestUnitImplConsts;
  7286. begin
  7287. StartUnit(false);
  7288. Add('interface');
  7289. Add('implementation');
  7290. Add('const');
  7291. Add(' v1 = 3;');
  7292. Add(' v2:longint = 4;');
  7293. Add(' v3:string = ''abc'';');
  7294. ConvertUnit;
  7295. CheckSource('TestUnitImplConsts',
  7296. LinesToStr([ // statements
  7297. 'var $impl = $mod.$impl;',
  7298. '']),
  7299. '', // this.$init
  7300. LinesToStr([ // implementation
  7301. '$impl.v1 = 3;',
  7302. '$impl.v2 = 4;',
  7303. '$impl.v3 = "abc";',
  7304. '']) );
  7305. end;
  7306. procedure TTestModule.TestUnitImplRecord;
  7307. begin
  7308. StartUnit(false);
  7309. Add('interface');
  7310. Add('implementation');
  7311. Add('type');
  7312. Add(' TMyRecord = record');
  7313. Add(' i: longint;');
  7314. Add(' end;');
  7315. Add('var aRec: TMyRecord;');
  7316. Add('initialization');
  7317. Add(' arec.i:=3;');
  7318. ConvertUnit;
  7319. CheckSource('TestUnitImplRecord',
  7320. LinesToStr([ // statements
  7321. 'var $impl = $mod.$impl;',
  7322. '']),
  7323. // this.$init
  7324. '$impl.aRec.i = 3;',
  7325. LinesToStr([ // implementation
  7326. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7327. ' this.i = 0;',
  7328. ' this.$eq = function (b) {',
  7329. ' return this.i === b.i;',
  7330. ' };',
  7331. ' this.$assign = function (s) {',
  7332. ' this.i = s.i;',
  7333. ' return this;',
  7334. ' };',
  7335. '});',
  7336. '$impl.aRec = $impl.TMyRecord.$new();',
  7337. '']) );
  7338. end;
  7339. procedure TTestModule.TestRenameJSNameConflict;
  7340. begin
  7341. StartProgram(false);
  7342. Add('var apply: longint;');
  7343. Add('var bind: longint;');
  7344. Add('var call: longint;');
  7345. Add('begin');
  7346. ConvertProgram;
  7347. CheckSource('TestRenameJSNameConflict',
  7348. LinesToStr([ // statements
  7349. 'this.Apply = 0;',
  7350. 'this.Bind = 0;',
  7351. 'this.Call = 0;'
  7352. ]),
  7353. LinesToStr([ // this.$main
  7354. ''
  7355. ]));
  7356. end;
  7357. procedure TTestModule.TestLocalConst;
  7358. begin
  7359. StartProgram(false);
  7360. Add('procedure DoIt;');
  7361. Add('const');
  7362. Add(' cA: longint = 1;');
  7363. Add(' cB = 2;');
  7364. Add(' procedure Sub;');
  7365. Add(' const');
  7366. Add(' csA = 3;');
  7367. Add(' cB: double = 4;');
  7368. Add(' begin');
  7369. Add(' cb:=cb+csa;');
  7370. Add(' ca:=ca+csa+5;');
  7371. Add(' end;');
  7372. Add('begin');
  7373. Add(' ca:=ca+cb+6;');
  7374. Add('end;');
  7375. Add('begin');
  7376. ConvertProgram;
  7377. CheckSource('TestLocalConst',
  7378. LinesToStr([
  7379. 'var cA = 1;',
  7380. 'var cB = 2;',
  7381. 'var csA = 3;',
  7382. 'var cB$1 = 4;',
  7383. 'this.DoIt = function () {',
  7384. ' function Sub() {',
  7385. ' cB$1 = cB$1 + 3;',
  7386. ' cA = cA + 3 + 5;',
  7387. ' };',
  7388. ' cA = cA + 2 + 6;',
  7389. '};'
  7390. ]),
  7391. LinesToStr([
  7392. ]));
  7393. end;
  7394. procedure TTestModule.TestVarExternal;
  7395. begin
  7396. StartProgram(false);
  7397. Add('var');
  7398. Add(' NaN: double; external name ''Global.NaN'';');
  7399. Add(' d: double;');
  7400. Add('begin');
  7401. Add(' d:=NaN;');
  7402. ConvertProgram;
  7403. CheckSource('TestVarExternal',
  7404. LinesToStr([
  7405. 'this.d = 0.0;'
  7406. ]),
  7407. LinesToStr([
  7408. '$mod.d = Global.NaN;'
  7409. ]));
  7410. end;
  7411. procedure TTestModule.TestVarExternalOtherUnit;
  7412. begin
  7413. AddModuleWithIntfImplSrc('unit2.pas',
  7414. LinesToStr([
  7415. 'var NaN: double; external name ''Global.NaN'';',
  7416. 'var iV: longint;'
  7417. ]),
  7418. '');
  7419. StartUnit(true);
  7420. Add('interface');
  7421. Add('uses unit2;');
  7422. Add('implementation');
  7423. Add('var');
  7424. Add(' d: double;');
  7425. Add(' i: longint; external name ''$i'';');
  7426. Add('begin');
  7427. Add(' d:=nan;');
  7428. Add(' d:=uNit2.nan;');
  7429. Add(' d:=test1.d;');
  7430. Add(' i:=iv;');
  7431. Add(' i:=uNit2.iv;');
  7432. Add(' i:=test1.i;');
  7433. ConvertUnit;
  7434. CheckSource('TestVarExternalOtherUnit',
  7435. LinesToStr([
  7436. 'var $impl = $mod.$impl;',
  7437. '']),
  7438. LinesToStr([ // this.$init
  7439. '$impl.d = Global.NaN;',
  7440. '$impl.d = Global.NaN;',
  7441. '$impl.d = $impl.d;',
  7442. '$i = pas.unit2.iV;',
  7443. '$i = pas.unit2.iV;',
  7444. '$i = $i;',
  7445. '']),
  7446. LinesToStr([ // implementation
  7447. '$impl.d = 0.0;',
  7448. '']) );
  7449. end;
  7450. procedure TTestModule.TestVarAbsoluteFail;
  7451. begin
  7452. StartProgram(false);
  7453. Add([
  7454. 'var',
  7455. ' a: longint;',
  7456. ' b: longword absolute a;',
  7457. 'begin']);
  7458. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7459. ConvertProgram;
  7460. end;
  7461. procedure TTestModule.TestConstExternal;
  7462. begin
  7463. StartProgram(false);
  7464. Add([
  7465. 'const',
  7466. ' PI: double; external name ''Global.PI'';',
  7467. ' Tau = 2*pi;',
  7468. 'var d: double;',
  7469. 'begin',
  7470. ' d:=pi;',
  7471. ' d:=tau+pi;']);
  7472. ConvertProgram;
  7473. CheckSource('TestConstExternal',
  7474. LinesToStr([
  7475. 'this.Tau = 2*Global.PI;',
  7476. 'this.d = 0.0;'
  7477. ]),
  7478. LinesToStr([
  7479. '$mod.d = Global.PI;',
  7480. '$mod.d = $mod.Tau + Global.PI;'
  7481. ]));
  7482. end;
  7483. procedure TTestModule.TestDouble;
  7484. begin
  7485. StartProgram(false);
  7486. Add([
  7487. 'type',
  7488. ' TDateTime = double;',
  7489. 'const',
  7490. ' a = TDateTime(2.7);',
  7491. ' b = a + TDateTime(1.7);',
  7492. ' c = 0.9 + 0.1;',
  7493. ' f0_1 = 0.1;',
  7494. ' f0_3 = 0.3;',
  7495. ' fn0_1 = -0.1;',
  7496. ' fn0_3 = -0.3;',
  7497. ' fn0_003 = -0.003;',
  7498. ' fn0_123456789 = -0.123456789;',
  7499. ' fn300_0 = -300.0;',
  7500. ' fn123456_0 = -123456.0;',
  7501. ' fn1234567_8 = -1234567.8;',
  7502. ' fn12345678_9 = -12345678.9;',
  7503. ' f1_0En12 = 1E-12;',
  7504. ' fn1_0En12 = -1E-12;',
  7505. ' maxdouble = 1.7e+308;',
  7506. ' mindouble = -1.7e+308;',
  7507. ' MinSafeIntDouble = -$1fffffffffffff;',
  7508. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7509. ' MaxSafeIntDouble = $1fffffffffffff;',
  7510. ' DZeroResolution = 1E-12;',
  7511. ' Minus1 = -1E-12;',
  7512. ' EPS = 1E-9;',
  7513. ' DELTA = 0.001;',
  7514. ' Big = 129.789E+100;',
  7515. ' Test0_15 = 0.15;',
  7516. ' Test999 = 2.9999999999999;',
  7517. ' Test111999 = 211199999999999000.0;',
  7518. ' TestMinus111999 = -211199999999999000.0;',
  7519. ' Inf = 1.0 / 0.0;',
  7520. ' NegInf = -1.0 / 0.0;',
  7521. 'procedure Run(d: double); external name ''Run'';',
  7522. 'var',
  7523. ' d: double = b;',
  7524. 'begin',
  7525. ' d:=1.0;',
  7526. ' d:=1.0/3.0;',
  7527. ' d:=1.0/(3-2-1);',
  7528. ' d:=1/3;',
  7529. ' d:=5.0E-324;',
  7530. ' d:=1.7E308;',
  7531. ' d:=001.00E00;',
  7532. ' d:=002.00E001;',
  7533. ' d:=003.000E000;',
  7534. ' d:=-004.00E-00;',
  7535. ' d:=-005.00E-001;',
  7536. ' d:=10**3;',
  7537. ' d:=10 mod 3;',
  7538. ' d:=10 div 3;',
  7539. ' d:=c;',
  7540. ' d:=f0_1;',
  7541. ' d:=f0_3;',
  7542. ' d:=fn0_1;',
  7543. ' d:=fn0_3;',
  7544. ' d:=fn0_003;',
  7545. ' d:=fn0_123456789;',
  7546. ' d:=fn300_0;',
  7547. ' d:=fn123456_0;',
  7548. ' d:=fn1234567_8;',
  7549. ' d:=fn12345678_9;',
  7550. ' d:=f1_0En12;',
  7551. ' d:=fn1_0En12;',
  7552. ' d:=maxdouble;',
  7553. ' d:=mindouble;',
  7554. ' d:=MinSafeIntDouble;',
  7555. ' d:=double(MinSafeIntDouble);',
  7556. ' d:=MinSafeIntDouble2;',
  7557. ' d:=double(MinSafeIntDouble2);',
  7558. ' d:=MaxSafeIntDouble;',
  7559. ' d:=default(double);',
  7560. ' Run(Inf);',
  7561. ' Run(NegInf);',
  7562. '']);
  7563. ConvertProgram;
  7564. CheckSource('TestDouble',
  7565. LinesToStr([
  7566. 'this.a = 2.7;',
  7567. 'this.b = 2.7 + 1.7;',
  7568. 'this.c = 0.9 + 0.1;',
  7569. 'this.f0_1 = 0.1;',
  7570. 'this.f0_3 = 0.3;',
  7571. 'this.fn0_1 = -0.1;',
  7572. 'this.fn0_3 = -0.3;',
  7573. 'this.fn0_003 = -0.003;',
  7574. 'this.fn0_123456789 = -0.123456789;',
  7575. 'this.fn300_0 = -300.0;',
  7576. 'this.fn123456_0 = -123456.0;',
  7577. 'this.fn1234567_8 = -1234567.8;',
  7578. 'this.fn12345678_9 = -12345678.9;',
  7579. 'this.f1_0En12 = 1E-12;',
  7580. 'this.fn1_0En12 = -1E-12;',
  7581. 'this.maxdouble = 1.7e+308;',
  7582. 'this.mindouble = -1.7e+308;',
  7583. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7584. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7585. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7586. 'this.DZeroResolution = 1E-12;',
  7587. 'this.Minus1 = -1E-12;',
  7588. 'this.EPS = 1E-9;',
  7589. 'this.DELTA = 0.001;',
  7590. 'this.Big = 129.789E+100;',
  7591. 'this.Test0_15 = 0.15;',
  7592. 'this.Test999 = 2.9999999999999;',
  7593. 'this.Test111999 = 211199999999999000.0;',
  7594. 'this.TestMinus111999 = -211199999999999000.0;',
  7595. 'this.Inf = 1.0 / 0.0;',
  7596. 'this.NegInf = -1.0 / 0.0;',
  7597. 'this.d = 4.4;',
  7598. '']),
  7599. LinesToStr([
  7600. '$mod.d = 1.0;',
  7601. '$mod.d = 1.0 / 3.0;',
  7602. '$mod.d = 1.0 / (3 - 2 - 1);',
  7603. '$mod.d = 1 / 3;',
  7604. '$mod.d = 5.0E-324;',
  7605. '$mod.d = 1.7E308;',
  7606. '$mod.d = 1.00E0;',
  7607. '$mod.d = 2.00E1;',
  7608. '$mod.d = 3.000E0;',
  7609. '$mod.d = -4.00E-0;',
  7610. '$mod.d = -5.00E-1;',
  7611. '$mod.d = Math.pow(10, 3);',
  7612. '$mod.d = 10 % 3;',
  7613. '$mod.d = rtl.trunc(10 / 3);',
  7614. '$mod.d = 1;',
  7615. '$mod.d = 0.1;',
  7616. '$mod.d = 0.3;',
  7617. '$mod.d = -0.1;',
  7618. '$mod.d = -0.3;',
  7619. '$mod.d = -3E-3;',
  7620. '$mod.d = -0.123456789;',
  7621. '$mod.d = -300;',
  7622. '$mod.d = -123456;',
  7623. '$mod.d = -1234567.8;',
  7624. '$mod.d = -1.23456789E7;',
  7625. '$mod.d = 1E-12;',
  7626. '$mod.d = -1E-12;',
  7627. '$mod.d = 1.7E308;',
  7628. '$mod.d = -1.7E308;',
  7629. '$mod.d = -9007199254740991;',
  7630. '$mod.d = -9007199254740991;',
  7631. '$mod.d = -9.007199254740992E15;',
  7632. '$mod.d = -9.007199254740992E15;',
  7633. '$mod.d = 9007199254740991;',
  7634. '$mod.d = 0.0;',
  7635. 'Run(1 / 0);',
  7636. 'Run(-1 / 0);',
  7637. '']));
  7638. end;
  7639. procedure TTestModule.TestDoubleSmall;
  7640. begin
  7641. StartProgram(false);
  7642. Add([
  7643. 'const',
  7644. ' a = 1e-1;',
  7645. ' b = 1e-2;',
  7646. ' c = 1e-3;',
  7647. ' d = 1e-4;',
  7648. ' e = 1e-5;',
  7649. ' f = 1e-6;',
  7650. ' g = 1e-7;',
  7651. ' h = -1e-1;',
  7652. ' i = -1e-2;',
  7653. 'procedure Fly(d: double);',
  7654. 'begin',
  7655. 'end;',
  7656. 'begin',
  7657. ' Fly(a);',
  7658. ' Fly(b);',
  7659. ' Fly(c);',
  7660. ' Fly(d);',
  7661. ' Fly(e);',
  7662. ' Fly(f);',
  7663. ' Fly(g);',
  7664. ' Fly(h);',
  7665. ' Fly(i);',
  7666. '']);
  7667. ConvertProgram;
  7668. CheckSource('TestDoubleSmall',
  7669. LinesToStr([
  7670. 'this.a = 1e-1;',
  7671. 'this.b = 1e-2;',
  7672. 'this.c = 1e-3;',
  7673. 'this.d = 1e-4;',
  7674. 'this.e = 1e-5;',
  7675. 'this.f = 1e-6;',
  7676. 'this.g = 1e-7;',
  7677. 'this.h = -1e-1;',
  7678. 'this.i = -1e-2;',
  7679. 'this.Fly = function (d) {',
  7680. '};',
  7681. '']),
  7682. LinesToStr([
  7683. '$mod.Fly(0.1);',
  7684. '$mod.Fly(0.01);',
  7685. '$mod.Fly(1E-3);',
  7686. '$mod.Fly(1E-4);',
  7687. '$mod.Fly(1E-5);',
  7688. '$mod.Fly(1E-6);',
  7689. '$mod.Fly(1E-7);',
  7690. '$mod.Fly(-0.1);',
  7691. '$mod.Fly(-0.01);',
  7692. '']));
  7693. end;
  7694. procedure TTestModule.TestInteger;
  7695. begin
  7696. StartProgram(false);
  7697. Add([
  7698. 'const',
  7699. ' MinInt = low(NativeInt);',
  7700. ' MaxInt = high(NativeInt);',
  7701. 'type',
  7702. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7703. 'const',
  7704. ' a = low(TMyInt)+High(TMyInt);',
  7705. 'var',
  7706. ' i: TMyInt;',
  7707. 'begin',
  7708. ' i:=-MinInt;',
  7709. ' i:=default(TMyInt);',
  7710. ' i:=low(i)+high(i);',
  7711. '']);
  7712. ConvertProgram;
  7713. CheckSource('TestIntegerRange',
  7714. LinesToStr([
  7715. 'this.MinInt = -9007199254740991;',
  7716. 'this.MaxInt = 9007199254740991;',
  7717. 'this.a = -9007199254740991 + 9007199254740991;',
  7718. 'this.i = 0;',
  7719. '']),
  7720. LinesToStr([
  7721. '$mod.i = - -9007199254740991;',
  7722. '$mod.i = -9007199254740991;',
  7723. '$mod.i = -9007199254740991 + 9007199254740991;',
  7724. '']));
  7725. end;
  7726. procedure TTestModule.TestIntegerRange;
  7727. begin
  7728. StartProgram(false);
  7729. Add([
  7730. 'const',
  7731. ' MinInt = -1;',
  7732. ' MaxInt = +1;',
  7733. 'type',
  7734. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7735. ' TInt2 = 1..3;',
  7736. 'const',
  7737. ' a = low(TMyInt)+High(TMyInt);',
  7738. ' b = low(TInt2)+High(TInt2);',
  7739. ' s1 = [1];',
  7740. ' s2 = [1,2];',
  7741. ' s3 = [1..3];',
  7742. ' s4 = [low(shortint)..high(shortint)];',
  7743. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7744. ' s6 = 1 in s2;',
  7745. 'var',
  7746. ' i: TMyInt;',
  7747. ' i2: TInt2;',
  7748. 'begin',
  7749. ' i:=i2;',
  7750. ' i:=default(TMyInt);',
  7751. ' if i=i2 then ;',
  7752. ' i:=ord(i2);',
  7753. '']);
  7754. ConvertProgram;
  7755. CheckSource('TestIntegerRange',
  7756. LinesToStr([
  7757. 'this.MinInt = -1;',
  7758. 'this.MaxInt = +1;',
  7759. 'this.a = -1 + 1;',
  7760. 'this.b = 1 + 3;',
  7761. 'this.s1 = rtl.createSet(1);',
  7762. 'this.s2 = rtl.createSet(1, 2);',
  7763. 'this.s3 = rtl.createSet(null, 1, 3);',
  7764. 'this.s4 = rtl.createSet(null, -128, 127);',
  7765. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7766. 'this.s6 = 1 in this.s2;',
  7767. 'this.i = 0;',
  7768. 'this.i2 = 0;',
  7769. '']),
  7770. LinesToStr([
  7771. '$mod.i = $mod.i2;',
  7772. '$mod.i = -1;',
  7773. 'if ($mod.i === $mod.i2) ;',
  7774. '$mod.i = $mod.i2;',
  7775. '']));
  7776. end;
  7777. procedure TTestModule.TestIntegerTypecasts;
  7778. begin
  7779. StartProgram(false);
  7780. Add([
  7781. 'var',
  7782. ' i: nativeint;',
  7783. ' b: byte;',
  7784. ' sh: shortint;',
  7785. ' w: word;',
  7786. ' sm: smallint;',
  7787. ' lw: longword;',
  7788. ' li: longint;',
  7789. 'begin',
  7790. ' b:=byte(i);',
  7791. ' sh:=shortint(i);',
  7792. ' w:=word(i);',
  7793. ' sm:=smallint(i);',
  7794. ' lw:=longword(i);',
  7795. ' li:=longint(i);',
  7796. '']);
  7797. ConvertProgram;
  7798. CheckSource('TestIntegerTypecasts',
  7799. LinesToStr([
  7800. 'this.i = 0;',
  7801. 'this.b = 0;',
  7802. 'this.sh = 0;',
  7803. 'this.w = 0;',
  7804. 'this.sm = 0;',
  7805. 'this.lw = 0;',
  7806. 'this.li = 0;',
  7807. '']),
  7808. LinesToStr([
  7809. '$mod.b = $mod.i & 255;',
  7810. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7811. '$mod.w = $mod.i & 65535;',
  7812. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7813. '$mod.lw = $mod.i >>> 0;',
  7814. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7815. '']));
  7816. end;
  7817. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7818. begin
  7819. StartProgram(false);
  7820. Add([
  7821. 'var',
  7822. ' i,j: nativeint;',
  7823. 'begin',
  7824. ' i:=i shr 0;',
  7825. ' i:=i shr 1;',
  7826. ' i:=i shr 3;',
  7827. ' i:=i shr 54;',
  7828. ' i:=j shr i;',
  7829. '']);
  7830. ConvertProgram;
  7831. CheckResolverUnexpectedHints;
  7832. CheckSource('TestInteger_BitwiseShrNativeInt',
  7833. LinesToStr([
  7834. 'this.i = 0;',
  7835. 'this.j = 0;',
  7836. '']),
  7837. LinesToStr([
  7838. '$mod.i = $mod.i;',
  7839. '$mod.i = Math.floor($mod.i / 2);',
  7840. '$mod.i = Math.floor($mod.i / 8);',
  7841. '$mod.i = 0;',
  7842. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7843. '']));
  7844. end;
  7845. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7846. begin
  7847. StartProgram(false);
  7848. Add([
  7849. 'var',
  7850. ' i: nativeint;',
  7851. 'begin',
  7852. ' i:=i shl 0;',
  7853. ' i:=i shl 54;',
  7854. ' i:=123456789012 shl 1;',
  7855. ' i:=i shl 1;',
  7856. '']);
  7857. ConvertProgram;
  7858. CheckResolverUnexpectedHints;
  7859. CheckSource('TestInteger_BitwiseShrNativeInt',
  7860. LinesToStr([
  7861. 'this.i = 0;',
  7862. '']),
  7863. LinesToStr([
  7864. '$mod.i = $mod.i;',
  7865. '$mod.i = 0;',
  7866. '$mod.i = 246913578024;',
  7867. '$mod.i = rtl.shl($mod.i, 1);',
  7868. '']));
  7869. end;
  7870. procedure TTestModule.TestInteger_SystemFunc;
  7871. begin
  7872. StartProgram(true);
  7873. Add([
  7874. 'var',
  7875. ' i: byte;',
  7876. ' s: string;',
  7877. 'begin',
  7878. ' system.inc(i);',
  7879. ' system.str(i,s);',
  7880. ' s:=system.str(i);',
  7881. ' i:=system.low(i);',
  7882. ' i:=system.high(i);',
  7883. ' i:=system.pred(i);',
  7884. ' i:=system.succ(i);',
  7885. ' i:=system.ord(i);',
  7886. '']);
  7887. ConvertProgram;
  7888. CheckResolverUnexpectedHints;
  7889. CheckSource('TestInteger_SystemFunc',
  7890. LinesToStr([
  7891. 'this.i = 0;',
  7892. 'this.s = "";',
  7893. '']),
  7894. LinesToStr([
  7895. '$mod.i += 1;',
  7896. '$mod.s = "" + $mod.i;',
  7897. '$mod.s = "" + $mod.i;',
  7898. '$mod.i = 0;',
  7899. '$mod.i = 255;',
  7900. '$mod.i = $mod.i - 1;',
  7901. '$mod.i = $mod.i + 1;',
  7902. '$mod.i = $mod.i;',
  7903. '']));
  7904. end;
  7905. procedure TTestModule.TestInteger_AssignOutsideConst;
  7906. begin
  7907. StartProgram(false);
  7908. Add([
  7909. 'const',
  7910. ' MinInt = low(longint);',
  7911. ' MaxInt = high(longint);',
  7912. 'type',
  7913. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7914. 'var',
  7915. ' i: TMyInt;',
  7916. ' aByte: byte;',
  7917. ' aShortInt: shortint;',
  7918. ' aWord: word;',
  7919. ' aSmallInt: smallint;',
  7920. ' aLongWord: longword;',
  7921. ' aLongInt: longint;',
  7922. ' aNativeInt: nativeint;',
  7923. ' aNativeUInt: nativeuint;',
  7924. 'begin',
  7925. ' aByte:=$FF;',
  7926. ' aByte:=$100;',
  7927. ' aByte:=-1;',
  7928. ' aByte:=-127;',
  7929. ' aByte:=-128;',
  7930. ' aByte:=-254;',
  7931. ' aByte:=-255;',
  7932. ' aByte:=-256;',
  7933. ' aShortInt:=127;',
  7934. ' aShortInt:=128;',
  7935. ' aShortInt:=-128;',
  7936. ' aShortInt:=-129;',
  7937. ' aWord:=$ffff;',
  7938. ' aWord:=$10000;',
  7939. ' aWord:=-1;',
  7940. ' aWord:=-$ffff;',
  7941. ' aWord:=-$10000;',
  7942. ' aWord:=-$10001;',
  7943. ' aSmallInt:=$7fff;',
  7944. ' aSmallInt:=$8000;',
  7945. ' aSmallInt:=-$8000;',
  7946. ' aSmallInt:=-$8001;',
  7947. ' aLongWord:=$ffffffff;',
  7948. ' aLongWord:=$100000000;',
  7949. ' aLongWord:=-1;',
  7950. ' aLongWord:=-$ffffffff;',
  7951. ' aNativeInt:=$1fffffffffffff;',
  7952. ' aNativeInt:=-$1fffffffffffff;',
  7953. ' aNativeUInt:=$1fffffffffffff;',
  7954. ' aNativeUInt:=-$1fffffffffffff;',
  7955. '']);
  7956. ConvertProgram;
  7957. CheckSource('TestInteger_AssignOutsideConst',
  7958. LinesToStr([
  7959. 'this.MinInt = -2147483648;',
  7960. 'this.MaxInt = 2147483647;',
  7961. 'this.i = 0;',
  7962. 'this.aByte = 0;',
  7963. 'this.aShortInt = 0;',
  7964. 'this.aWord = 0;',
  7965. 'this.aSmallInt = 0;',
  7966. 'this.aLongWord = 0;',
  7967. 'this.aLongInt = 0;',
  7968. 'this.aNativeInt = 0;',
  7969. 'this.aNativeUInt = 0;',
  7970. '']),
  7971. LinesToStr([
  7972. '$mod.aByte = 0xFF;',
  7973. '$mod.aByte = 0;',
  7974. '$mod.aByte = 255;',
  7975. '$mod.aByte = 129;',
  7976. '$mod.aByte = 128;',
  7977. '$mod.aByte = 2;',
  7978. '$mod.aByte = 1;',
  7979. '$mod.aByte = 0;',
  7980. '$mod.aShortInt = 127;',
  7981. '$mod.aShortInt = -128;',
  7982. '$mod.aShortInt = -128;',
  7983. '$mod.aShortInt = 127;',
  7984. '$mod.aWord = 0xffff;',
  7985. '$mod.aWord = 0;',
  7986. '$mod.aWord = 65535;',
  7987. '$mod.aWord = 1;',
  7988. '$mod.aWord = 0;',
  7989. '$mod.aWord = 65535;',
  7990. '$mod.aSmallInt = 0x7fff;',
  7991. '$mod.aSmallInt = -32768;',
  7992. '$mod.aSmallInt = -0x8000;',
  7993. '$mod.aSmallInt = 32767;',
  7994. '$mod.aLongWord = 0xffffffff;',
  7995. '$mod.aLongWord = 0;',
  7996. '$mod.aLongWord = 4294967295;',
  7997. '$mod.aLongWord = 1;',
  7998. '$mod.aNativeInt = 0x1fffffffffffff;',
  7999. '$mod.aNativeInt = -0x1fffffffffffff;',
  8000. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8001. '$mod.aNativeUInt = 1;',
  8002. '']));
  8003. end;
  8004. procedure TTestModule.TestCurrency;
  8005. begin
  8006. StartProgram(false);
  8007. Add([
  8008. 'type',
  8009. ' TCoin = currency;',
  8010. 'const',
  8011. ' a = TCoin(2.7);',
  8012. ' b = a + TCoin(1.7);',
  8013. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8014. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8015. 'var',
  8016. ' c: TCoin = b;',
  8017. ' i: nativeint;',
  8018. ' d: double;',
  8019. ' j: jsvalue;',
  8020. 'function DoIt(c: currency): currency; begin end;',
  8021. 'function GetIt(d: double): double; begin end;',
  8022. 'procedure Write(v: jsvalue); begin end;',
  8023. 'begin',
  8024. ' c:=1.0;',
  8025. ' c:=0.1;',
  8026. ' c:=1.0/3.0;',
  8027. ' c:=1/3;',
  8028. ' c:=a;',
  8029. ' d:=c;',
  8030. ' c:=d;',
  8031. ' c:=currency(c);',
  8032. ' c:=currency(d);',
  8033. ' d:=double(c);',
  8034. ' c:=i;',
  8035. ' c:=currency(i);',
  8036. //' i:=c;', not allowed
  8037. ' i:=nativeint(c);',
  8038. ' c:=c+a;',
  8039. ' c:=-c-a;',
  8040. ' c:=d+c;',
  8041. ' c:=c+d;',
  8042. ' c:=d-c;',
  8043. ' c:=c-d;',
  8044. ' c:=c*a;',
  8045. ' c:=a*c;',
  8046. ' c:=d*c;',
  8047. ' c:=c*d;',
  8048. ' c:=c/a;',
  8049. ' c:=a/c;',
  8050. ' c:=d/c;',
  8051. ' c:=c/d;',
  8052. ' c:=c**a;',
  8053. ' c:=a**c;',
  8054. ' c:=d**c;',
  8055. ' c:=c**d;',
  8056. ' if c=c then ;',
  8057. ' if c=a then ;',
  8058. ' if a=c then ;',
  8059. ' if d=c then ;',
  8060. ' if c=d then ;',
  8061. ' c:=DoIt(c);',
  8062. ' c:=DoIt(i);',
  8063. ' c:=DoIt(d);',
  8064. ' c:=GetIt(c);',
  8065. ' j:=c;',
  8066. ' Write(c);',
  8067. ' c:=default(currency);',
  8068. ' j:=str(c);',
  8069. ' j:=str(c:0:3);',
  8070. '']);
  8071. ConvertProgram;
  8072. CheckSource('TestCurrency',
  8073. LinesToStr([
  8074. 'this.a = 27000;',
  8075. 'this.b = this.a + 17000;',
  8076. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8077. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8078. 'this.c = this.b;',
  8079. 'this.i = 0;',
  8080. 'this.d = 0.0;',
  8081. 'this.j = undefined;',
  8082. 'this.DoIt = function (c) {',
  8083. ' var Result = 0;',
  8084. ' return Result;',
  8085. '};',
  8086. 'this.GetIt = function (d) {',
  8087. ' var Result = 0.0;',
  8088. ' return Result;',
  8089. '};',
  8090. 'this.Write = function (v) {',
  8091. '};',
  8092. '']),
  8093. LinesToStr([
  8094. '$mod.c = 10000;',
  8095. '$mod.c = 1000;',
  8096. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8097. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8098. '$mod.c = $mod.a;',
  8099. '$mod.d = $mod.c / 10000;',
  8100. '$mod.c = rtl.trunc($mod.d * 10000);',
  8101. '$mod.c = $mod.c;',
  8102. '$mod.c = $mod.d * 10000;',
  8103. '$mod.d = $mod.c / 10000;',
  8104. '$mod.c = $mod.i * 10000;',
  8105. '$mod.c = $mod.i * 10000;',
  8106. '$mod.i = rtl.trunc($mod.c / 10000);',
  8107. '$mod.c = $mod.c + $mod.a;',
  8108. '$mod.c = -$mod.c - $mod.a;',
  8109. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8110. '$mod.c = $mod.c + ($mod.d * 10000);',
  8111. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8112. '$mod.c = $mod.c - ($mod.d * 10000);',
  8113. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8114. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8115. '$mod.c = $mod.d * $mod.c;',
  8116. '$mod.c = $mod.c * $mod.d;',
  8117. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8118. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8119. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8120. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8121. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8122. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8123. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8124. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8125. 'if ($mod.c === $mod.c) ;',
  8126. 'if ($mod.c === $mod.a) ;',
  8127. 'if ($mod.a === $mod.c) ;',
  8128. 'if (($mod.d * 10000) === $mod.c) ;',
  8129. 'if ($mod.c === ($mod.d * 10000)) ;',
  8130. '$mod.c = $mod.DoIt($mod.c);',
  8131. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8132. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8133. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8134. '$mod.j = $mod.c / 10000;',
  8135. '$mod.Write($mod.c / 10000);',
  8136. '$mod.c = 0;',
  8137. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8138. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8139. '']));
  8140. end;
  8141. procedure TTestModule.TestForBoolDo;
  8142. begin
  8143. StartProgram(false);
  8144. Add([
  8145. 'var b: boolean;',
  8146. 'begin',
  8147. ' for b:=false to true do ;',
  8148. ' for b:=b downto false do ;',
  8149. ' for b in boolean do ;',
  8150. '']);
  8151. ConvertProgram;
  8152. CheckSource('TestForBoolDo',
  8153. LinesToStr([ // statements
  8154. 'this.b = false;']),
  8155. LinesToStr([ // this.$main
  8156. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8157. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8158. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8159. '']));
  8160. end;
  8161. procedure TTestModule.TestForIntDo;
  8162. begin
  8163. StartProgram(false);
  8164. Add([
  8165. 'var i: longint;',
  8166. 'begin',
  8167. ' for i:=3 to 5 do ;',
  8168. ' for i:=i downto 2 do ;',
  8169. ' for i in byte do ;',
  8170. '']);
  8171. ConvertProgram;
  8172. CheckSource('TestForIntDo',
  8173. LinesToStr([ // statements
  8174. 'this.i = 0;']),
  8175. LinesToStr([ // this.$main
  8176. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8177. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8178. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8179. '']));
  8180. end;
  8181. procedure TTestModule.TestForIntInDo;
  8182. begin
  8183. StartProgram(false);
  8184. Add([
  8185. 'type',
  8186. ' TSetOfInt = set of byte;',
  8187. ' TIntRg = 3..7;',
  8188. ' TSetOfIntRg = set of TIntRg;',
  8189. 'var',
  8190. ' i,i2: longint;',
  8191. ' a1: array of byte;',
  8192. ' a2: array[1..3] of byte;',
  8193. ' soi: TSetOfInt;',
  8194. ' soir: TSetOfIntRg;',
  8195. ' ir: TIntRg;',
  8196. 'begin',
  8197. ' for i in byte do ;',
  8198. ' for i in a1 do ;',
  8199. ' for i in a2 do ;',
  8200. ' for i in [11..13] do ;',
  8201. ' for i in TSetOfInt do ;',
  8202. ' for i in TIntRg do ;',
  8203. ' for i in soi do i2:=i;',
  8204. ' for i in TSetOfIntRg do ;',
  8205. ' for i in soir do ;',
  8206. ' for ir in TIntRg do ;',
  8207. ' for ir in TSetOfIntRg do ;',
  8208. ' for ir in soir do ;',
  8209. '']);
  8210. ConvertProgram;
  8211. CheckSource('TestForIntInDo',
  8212. LinesToStr([ // statements
  8213. 'this.i = 0;',
  8214. 'this.i2 = 0;',
  8215. 'this.a1 = [];',
  8216. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8217. 'this.soi = {};',
  8218. 'this.soir = {};',
  8219. 'this.ir = 0;',
  8220. '']),
  8221. LinesToStr([ // this.$main
  8222. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8223. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8224. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8225. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8226. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8227. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8228. 'for (var $l6 in $mod.soi) {',
  8229. ' $mod.i = +$l6;',
  8230. ' $mod.i2 = $mod.i;',
  8231. '};',
  8232. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8233. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8234. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8235. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8236. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8237. '']));
  8238. end;
  8239. procedure TTestModule.TestCharConst;
  8240. begin
  8241. StartProgram(false);
  8242. Add([
  8243. 'const',
  8244. ' a = #$00F3;',
  8245. ' c: char = ''1'';',
  8246. ' wc: widechar = ''ä'';',
  8247. 'begin',
  8248. ' c:=#0;',
  8249. ' c:=#1;',
  8250. ' c:=#9;',
  8251. ' c:=#10;',
  8252. ' c:=#13;',
  8253. ' c:=#31;',
  8254. ' c:=#32;',
  8255. ' c:=#$A;',
  8256. ' c:=#$0A;',
  8257. ' c:=#$b;',
  8258. ' c:=#$0b;',
  8259. ' c:=^A;',
  8260. ' c:=''"'';',
  8261. ' c:=default(char);',
  8262. ' c:=#$00E4;', // ä
  8263. ' c:=''ä'';',
  8264. ' c:=#$E4;', // ä
  8265. ' c:=#$D800;', // invalid UTF-16
  8266. ' c:=#$DFFF;', // invalid UTF-16
  8267. ' c:=#$FFFF;', // last UCS-2
  8268. ' c:=high(c);', // last UCS-2
  8269. ' c:=#269;',
  8270. '']);
  8271. ConvertProgram;
  8272. CheckSource('TestCharConst',
  8273. LinesToStr([
  8274. 'this.a="ó";',
  8275. 'this.c="1";',
  8276. 'this.wc="ä";'
  8277. ]),
  8278. LinesToStr([
  8279. '$mod.c="\x00";',
  8280. '$mod.c="\x01";',
  8281. '$mod.c="\t";',
  8282. '$mod.c="\n";',
  8283. '$mod.c="\r";',
  8284. '$mod.c="\x1F";',
  8285. '$mod.c=" ";',
  8286. '$mod.c="\n";',
  8287. '$mod.c="\n";',
  8288. '$mod.c="\x0B";',
  8289. '$mod.c="\x0B";',
  8290. '$mod.c="\x01";',
  8291. '$mod.c=''"'';',
  8292. '$mod.c="\x00";',
  8293. '$mod.c = "ä";',
  8294. '$mod.c = "ä";',
  8295. '$mod.c = "ä";',
  8296. '$mod.c="\uD800";',
  8297. '$mod.c="\uDFFF";',
  8298. '$mod.c="\uFFFF";',
  8299. '$mod.c="\uFFFF";',
  8300. '$mod.c = "č";',
  8301. '']));
  8302. end;
  8303. procedure TTestModule.TestChar_Compare;
  8304. begin
  8305. StartProgram(false);
  8306. Add('var');
  8307. Add(' c: char;');
  8308. Add(' b: boolean;');
  8309. Add('begin');
  8310. Add(' b:=c=''1'';');
  8311. Add(' b:=''2''=c;');
  8312. Add(' b:=''3''=''4'';');
  8313. Add(' b:=c<>''5'';');
  8314. Add(' b:=''6''<>c;');
  8315. Add(' b:=c>''7'';');
  8316. Add(' b:=''8''>c;');
  8317. Add(' b:=c>=''9'';');
  8318. Add(' b:=''A''>=c;');
  8319. Add(' b:=c<''B'';');
  8320. Add(' b:=''C''<c;');
  8321. Add(' b:=c<=''D'';');
  8322. Add(' b:=''E''<=c;');
  8323. ConvertProgram;
  8324. CheckSource('TestChar_Compare',
  8325. LinesToStr([
  8326. 'this.c="";',
  8327. 'this.b = false;'
  8328. ]),
  8329. LinesToStr([
  8330. '$mod.b = $mod.c === "1";',
  8331. '$mod.b = "2" === $mod.c;',
  8332. '$mod.b = "3" === "4";',
  8333. '$mod.b = $mod.c !== "5";',
  8334. '$mod.b = "6" !== $mod.c;',
  8335. '$mod.b = $mod.c > "7";',
  8336. '$mod.b = "8" > $mod.c;',
  8337. '$mod.b = $mod.c >= "9";',
  8338. '$mod.b = "A" >= $mod.c;',
  8339. '$mod.b = $mod.c < "B";',
  8340. '$mod.b = "C" < $mod.c;',
  8341. '$mod.b = $mod.c <= "D";',
  8342. '$mod.b = "E" <= $mod.c;',
  8343. '']));
  8344. end;
  8345. procedure TTestModule.TestChar_BuiltInProcs;
  8346. begin
  8347. StartProgram(false);
  8348. Add([
  8349. 'var',
  8350. ' c: char;',
  8351. ' i: longint;',
  8352. ' s: string;',
  8353. 'begin',
  8354. ' i:=ord(c);',
  8355. ' i:=ord(s[i]);',
  8356. ' c:=chr(i);',
  8357. ' c:=pred(c);',
  8358. ' c:=succ(c);',
  8359. ' c:=low(c);',
  8360. ' c:=high(c);',
  8361. ' i:=byte(c);',
  8362. ' i:=word(c);',
  8363. ' i:=longint(c);',
  8364. '']);
  8365. ConvertProgram;
  8366. CheckSource('TestChar_BuiltInProcs',
  8367. LinesToStr([
  8368. 'this.c = "";',
  8369. 'this.i = 0;',
  8370. 'this.s = "";'
  8371. ]),
  8372. LinesToStr([
  8373. '$mod.i = $mod.c.charCodeAt();',
  8374. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8375. '$mod.c = String.fromCharCode($mod.i);',
  8376. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8377. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8378. '$mod.c = "\x00";',
  8379. '$mod.c = "\uFFFF";',
  8380. '$mod.i = $mod.c.charCodeAt() & 255;',
  8381. '$mod.i = $mod.c.charCodeAt();',
  8382. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8383. '']));
  8384. end;
  8385. procedure TTestModule.TestStringConst;
  8386. begin
  8387. StartProgram(false);
  8388. Add([
  8389. '{$H+}',
  8390. 'const',
  8391. ' a = #$00F3#$017C;', // first <256, then >=256
  8392. ' b = string(''a'');',
  8393. ' c = string(''ä'');',
  8394. ' d = UnicodeString(''b'');',
  8395. ' e = UnicodeString(''ö'');',
  8396. ' f = low(a)+high(b);',
  8397. ' g: word = low(a);',
  8398. 'var',
  8399. ' s: string = ''abc'';',
  8400. ' i: longint;',
  8401. 'begin',
  8402. ' s:='''';',
  8403. ' s:=#13#10;',
  8404. ' s:=#9''foo'';',
  8405. ' s:=#$A9;',
  8406. ' s:=''foo''#13''bar'';',
  8407. ' s:=''"'';',
  8408. ' s:=''"''''"'';',
  8409. ' s:=#$20AC;', // euro
  8410. ' s:=#$10437;', // outside BMP
  8411. ' s:=''abc''#$20AC;', // ascii,#
  8412. ' s:=''ä''#$20AC;', // non ascii,#
  8413. ' s:=#$20AC''abc'';', // #, ascii
  8414. ' s:=#$20AC''ä'';', // #, non ascii
  8415. ' s:=default(string);',
  8416. ' s:=concat(s);',
  8417. ' s:=concat(s,''a'',s);',
  8418. ' s:=#250#269;',
  8419. ' i:=low(s)+high(a);',
  8420. ' s:=''a/b'';',
  8421. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8422. '']);
  8423. ConvertProgram;
  8424. CheckSource('TestStringConst',
  8425. LinesToStr([
  8426. 'this.a = "óż";',
  8427. 'this.b = "a";',
  8428. 'this.c = "ä";',
  8429. 'this.d = "b";',
  8430. 'this.e = "ö";',
  8431. 'this.f = 1 + this.b.length;',
  8432. 'this.g = 1;',
  8433. 'this.s="abc";',
  8434. 'this.i = 0;',
  8435. '']),
  8436. LinesToStr([
  8437. '$mod.s="";',
  8438. '$mod.s="\r\n";',
  8439. '$mod.s="\tfoo";',
  8440. '$mod.s="©";',
  8441. '$mod.s="foo\rbar";',
  8442. '$mod.s=''"'';',
  8443. '$mod.s=''"\''"'';',
  8444. '$mod.s="€";',
  8445. '$mod.s="'#$F0#$90#$90#$B7'";',
  8446. '$mod.s = "abc€";',
  8447. '$mod.s = "ä€";',
  8448. '$mod.s = "€abc";',
  8449. '$mod.s = "ۊ";',
  8450. '$mod.s="";',
  8451. '$mod.s = $mod.s;',
  8452. '$mod.s = $mod.s.concat("a", $mod.s);',
  8453. '$mod.s = "úč";',
  8454. '$mod.i = 1 + $mod.a.length;',
  8455. '$mod.s = "a/b";',
  8456. '']));
  8457. end;
  8458. procedure TTestModule.TestStringConst_InvalidUTF16;
  8459. begin
  8460. StartProgram(false);
  8461. Add([
  8462. 'const',
  8463. ' a: char = #$D87E;',
  8464. ' b: string = #$D87E;',
  8465. ' c: string = #$D87E#43;',
  8466. 'begin',
  8467. ' c:=''abc''#$D87E;',
  8468. ' c:=#0#1#2;',
  8469. ' c:=#127;',
  8470. ' c:=#128;',
  8471. ' c:=#255;',
  8472. ' c:=#256;',
  8473. '']);
  8474. ConvertProgram;
  8475. CheckSource('TestStringConst',
  8476. LinesToStr([
  8477. 'this.a = "\uD87E";',
  8478. 'this.b = "\uD87E";',
  8479. 'this.c = "\uD87E+";',
  8480. '']),
  8481. LinesToStr([
  8482. '$mod.c = "abc\uD87E";',
  8483. '$mod.c = "\x00\x01\x02";',
  8484. '$mod.c = "'#127'";',
  8485. '$mod.c = "'#$c2#$80'";',
  8486. '$mod.c = "'#$c3#$BF'";',
  8487. '$mod.c = "'#$c4#$80'";',
  8488. '']));
  8489. end;
  8490. procedure TTestModule.TestStringConstSurrogate;
  8491. begin
  8492. StartProgram(false);
  8493. Add([
  8494. 'var',
  8495. ' s: string;',
  8496. 'begin',
  8497. ' s:=''😊'';', // 1F60A
  8498. '']);
  8499. ConvertProgram;
  8500. CheckSource('TestStringConstSurrogate',
  8501. LinesToStr([
  8502. 'this.s="";'
  8503. ]),
  8504. LinesToStr([
  8505. '$mod.s="😊";'
  8506. ]));
  8507. end;
  8508. procedure TTestModule.TestStringConst_Multiline;
  8509. begin
  8510. StartProgram(false);
  8511. Add([
  8512. '{$modeswitch multilinestrings}',
  8513. 'const',
  8514. ' a = ``;',
  8515. ' b = `',
  8516. 'line`;',
  8517. ' c = `Single`;',
  8518. ' d = ````;',
  8519. ' e = `abc``xyz`;',
  8520. ' f = `first''line',
  8521. ' second''line`#10;',
  8522. 'begin',
  8523. '']);
  8524. ConvertProgram;
  8525. CheckSource('TestStringConst_Multiline',
  8526. LinesToStr([
  8527. 'this.a = "";',
  8528. 'this.b = "'+JSONNewLine+'line";',
  8529. 'this.c = "Single";',
  8530. 'this.d = "`";',
  8531. 'this.e = "abc`xyz";',
  8532. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8533. '']),
  8534. LinesToStr([
  8535. ]));
  8536. end;
  8537. procedure TTestModule.TestString_Length;
  8538. begin
  8539. StartProgram(false);
  8540. Add('const c = ''foo'';');
  8541. Add('var');
  8542. Add(' s: string;');
  8543. Add(' i: longint;');
  8544. Add('begin');
  8545. Add(' i:=length(s);');
  8546. Add(' i:=length(s+s);');
  8547. Add(' i:=length(''abc'');');
  8548. Add(' i:=length(c);');
  8549. ConvertProgram;
  8550. CheckSource('TestString_Length',
  8551. LinesToStr([
  8552. 'this.c = "foo";',
  8553. 'this.s = "";',
  8554. 'this.i = 0;',
  8555. '']),
  8556. LinesToStr([
  8557. '$mod.i = $mod.s.length;',
  8558. '$mod.i = ($mod.s+$mod.s).length;',
  8559. '$mod.i = "abc".length;',
  8560. '$mod.i = $mod.c.length;',
  8561. '']));
  8562. end;
  8563. procedure TTestModule.TestString_Compare;
  8564. begin
  8565. StartProgram(false);
  8566. Add('var');
  8567. Add(' s, t: string;');
  8568. Add(' b: boolean;');
  8569. Add('begin');
  8570. Add(' b:=s=t;');
  8571. Add(' b:=s<>t;');
  8572. Add(' b:=s>t;');
  8573. Add(' b:=s>=t;');
  8574. Add(' b:=s<t;');
  8575. Add(' b:=s<=t;');
  8576. ConvertProgram;
  8577. CheckSource('TestString_Compare',
  8578. LinesToStr([ // statements
  8579. 'this.s = "";',
  8580. 'this.t = "";',
  8581. 'this.b =false;'
  8582. ]),
  8583. LinesToStr([ // this.$main
  8584. '$mod.b = $mod.s === $mod.t;',
  8585. '$mod.b = $mod.s !== $mod.t;',
  8586. '$mod.b = $mod.s > $mod.t;',
  8587. '$mod.b = $mod.s >= $mod.t;',
  8588. '$mod.b = $mod.s < $mod.t;',
  8589. '$mod.b = $mod.s <= $mod.t;',
  8590. '']));
  8591. end;
  8592. procedure TTestModule.TestString_SetLength;
  8593. begin
  8594. StartProgram(false);
  8595. Add([
  8596. 'procedure Fly(var s: string);',
  8597. 'begin',
  8598. ' SetLength(s,1);',
  8599. 'end;',
  8600. 'procedure Run(var s: unicodestring);',
  8601. 'begin',
  8602. ' SetLength(s,2);',
  8603. 'end;',
  8604. 'var s: string;',
  8605. ' u: unicodestring;',
  8606. 'begin',
  8607. ' SetLength(s,3);',
  8608. ' SetLength(u,4);',
  8609. '']);
  8610. ConvertProgram;
  8611. CheckSource('TestString_SetLength',
  8612. LinesToStr([ // statements
  8613. 'this.Fly = function (s) {',
  8614. ' s.set(rtl.strSetLength(s.get(), 1));',
  8615. '};',
  8616. 'this.Run = function (s) {',
  8617. ' s.set(rtl.strSetLength(s.get(), 2));',
  8618. '};',
  8619. 'this.s = "";',
  8620. 'this.u = "";',
  8621. '']),
  8622. LinesToStr([ // this.$main
  8623. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8624. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8625. ]));
  8626. end;
  8627. procedure TTestModule.TestString_CharAt;
  8628. begin
  8629. StartProgram(false);
  8630. Add([
  8631. 'var',
  8632. ' s: string;',
  8633. ' c: char;',
  8634. ' b: boolean;',
  8635. 'begin',
  8636. ' b:= s[1] = c;',
  8637. ' b:= c = s[1];',
  8638. ' b:= c <> s[1];',
  8639. ' b:= c > s[1];',
  8640. ' b:= c >= s[1];',
  8641. ' b:= c < s[2];',
  8642. ' b:= c <= s[1];',
  8643. ' s[1] := c;',
  8644. ' s[2+3] := c;']);
  8645. ConvertProgram;
  8646. CheckSource('TestString_CharAt',
  8647. LinesToStr([ // statements
  8648. 'this.s = "";',
  8649. 'this.c = "";',
  8650. 'this.b = false;'
  8651. ]),
  8652. LinesToStr([ // this.$main
  8653. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8654. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8655. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8656. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8657. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8658. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8659. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8660. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8661. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8662. '']));
  8663. end;
  8664. procedure TTestModule.TestStringHMinusFail;
  8665. begin
  8666. StartProgram(false);
  8667. Add([
  8668. '{$H-}',
  8669. 'var s: string;',
  8670. 'begin']);
  8671. ConvertProgram;
  8672. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8673. end;
  8674. procedure TTestModule.TestStr;
  8675. begin
  8676. StartProgram(false);
  8677. Add('var');
  8678. Add(' b: boolean;');
  8679. Add(' i: longint;');
  8680. Add(' d: double;');
  8681. Add(' s: string;');
  8682. Add('begin');
  8683. Add(' str(b,s);');
  8684. Add(' str(i,s);');
  8685. Add(' str(d,s);');
  8686. Add(' str(i:3,s);');
  8687. Add(' str(d:3:2,s);');
  8688. Add(' Str(12.456:12:1,s);');
  8689. Add(' Str(12.456:12,s);');
  8690. Add(' s:=str(b);');
  8691. Add(' s:=str(i);');
  8692. Add(' s:=str(d);');
  8693. Add(' s:=str(i,i);');
  8694. Add(' s:=str(i:3);');
  8695. Add(' s:=str(d:3:2);');
  8696. Add(' s:=str(i:4,i);');
  8697. Add(' s:=str(i,i:5);');
  8698. Add(' s:=str(i:4,i:5);');
  8699. Add(' s:=str(s,s);');
  8700. Add(' s:=str(s,''foo'');');
  8701. ConvertProgram;
  8702. CheckSource('TestStr',
  8703. LinesToStr([ // statements
  8704. 'this.b = false;',
  8705. 'this.i = 0;',
  8706. 'this.d = 0.0;',
  8707. 'this.s = "";',
  8708. '']),
  8709. LinesToStr([ // this.$main
  8710. '$mod.s = ""+$mod.b;',
  8711. '$mod.s = ""+$mod.i;',
  8712. '$mod.s = rtl.floatToStr($mod.d);',
  8713. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8714. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8715. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8716. '$mod.s = rtl.floatToStr(12.456,12);',
  8717. '$mod.s = ""+$mod.b;',
  8718. '$mod.s = ""+$mod.i;',
  8719. '$mod.s = rtl.floatToStr($mod.d);',
  8720. '$mod.s = ""+$mod.i+$mod.i;',
  8721. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8722. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8723. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8724. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8725. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8726. '$mod.s = $mod.s + $mod.s;',
  8727. '$mod.s = $mod.s + "foo";',
  8728. '']));
  8729. end;
  8730. procedure TTestModule.TestBaseType_AnsiStringFail;
  8731. begin
  8732. StartProgram(false);
  8733. Add('var s: AnsiString');
  8734. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8735. ConvertProgram;
  8736. end;
  8737. procedure TTestModule.TestBaseType_WideStringFail;
  8738. begin
  8739. StartProgram(false);
  8740. Add('var s: WideString');
  8741. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8742. ConvertProgram;
  8743. end;
  8744. procedure TTestModule.TestBaseType_ShortStringFail;
  8745. begin
  8746. StartProgram(false);
  8747. Add('var s: ShortString');
  8748. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8749. ConvertProgram;
  8750. end;
  8751. procedure TTestModule.TestBaseType_RawByteStringFail;
  8752. begin
  8753. StartProgram(false);
  8754. Add('var s: RawByteString');
  8755. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8756. ConvertProgram;
  8757. end;
  8758. procedure TTestModule.TestTypeShortstring_Fail;
  8759. begin
  8760. StartProgram(false);
  8761. Add('type t = string[12];');
  8762. Add('var s: t;');
  8763. Add('begin');
  8764. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8765. ConvertProgram;
  8766. end;
  8767. procedure TTestModule.TestCharSet_Custom;
  8768. begin
  8769. StartProgram(false);
  8770. Add([
  8771. 'type',
  8772. ' TCharRg = ''a''..''z'';',
  8773. ' TSetOfCharRg = set of TCharRg;',
  8774. ' TCharRg2 = ''m''..''p'';',
  8775. 'const',
  8776. ' crg: TCharRg = ''b'';',
  8777. 'var',
  8778. ' c: char;',
  8779. ' crg2: TCharRg2;',
  8780. ' s: TSetOfCharRg;',
  8781. 'begin',
  8782. ' c:=crg;',
  8783. ' crg:=c;',
  8784. ' crg2:=crg;',
  8785. ' if c=crg then ;',
  8786. ' if crg=c then ;',
  8787. ' if crg=crg2 then ;',
  8788. ' if c in s then ;',
  8789. ' if crg2 in s then ;',
  8790. ' c:=default(TCharRg);',
  8791. '']);
  8792. ConvertProgram;
  8793. CheckSource('TestCharSet_Custom',
  8794. LinesToStr([ // statements
  8795. 'this.crg = "b";',
  8796. 'this.c = "";',
  8797. 'this.crg2 = "m";',
  8798. 'this.s = {};',
  8799. '']),
  8800. LinesToStr([ // this.$main
  8801. '$mod.c = $mod.crg;',
  8802. '$mod.crg = $mod.c;',
  8803. '$mod.crg2 = $mod.crg;',
  8804. 'if ($mod.c === $mod.crg) ;',
  8805. 'if ($mod.crg === $mod.c) ;',
  8806. 'if ($mod.crg === $mod.crg2) ;',
  8807. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8808. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8809. '$mod.c = "a";',
  8810. '']));
  8811. end;
  8812. procedure TTestModule.TestWideChar;
  8813. begin
  8814. StartProgram(false);
  8815. Add([
  8816. 'procedure Fly(var c: char);',
  8817. 'begin',
  8818. 'end;',
  8819. 'procedure Run(var c: widechar);',
  8820. 'begin',
  8821. 'end;',
  8822. 'var',
  8823. ' c: char;',
  8824. ' wc: widechar;',
  8825. ' w: word;',
  8826. 'begin',
  8827. ' Fly(wc);',
  8828. ' Run(c);',
  8829. ' wc:=WideChar(w);',
  8830. ' w:=ord(wc);',
  8831. '']);
  8832. ConvertProgram;
  8833. CheckSource('TestWideChar_VarArg',
  8834. LinesToStr([ // statements
  8835. 'this.Fly = function (c) {',
  8836. '};',
  8837. 'this.Run = function (c) {',
  8838. '};',
  8839. 'this.c = "";',
  8840. 'this.wc = "";',
  8841. 'this.w = 0;',
  8842. '']),
  8843. LinesToStr([ // this.$main
  8844. '$mod.Fly({',
  8845. ' p: $mod,',
  8846. ' get: function () {',
  8847. ' return this.p.wc;',
  8848. ' },',
  8849. ' set: function (v) {',
  8850. ' this.p.wc = v;',
  8851. ' }',
  8852. '});',
  8853. '$mod.Run({',
  8854. ' p: $mod,',
  8855. ' get: function () {',
  8856. ' return this.p.c;',
  8857. ' },',
  8858. ' set: function (v) {',
  8859. ' this.p.c = v;',
  8860. ' }',
  8861. '});',
  8862. '$mod.wc = String.fromCharCode($mod.w);',
  8863. '$mod.w = $mod.wc.charCodeAt();',
  8864. '',
  8865. '']));
  8866. end;
  8867. procedure TTestModule.TestForCharDo;
  8868. begin
  8869. StartProgram(false);
  8870. Add([
  8871. 'var c: char;',
  8872. 'begin',
  8873. ' for c:=''a'' to ''c'' do ;',
  8874. ' for c:=c downto ''a'' do ;',
  8875. ' for c:=''Б'' to ''Я'' do ;',
  8876. '']);
  8877. ConvertProgram;
  8878. CheckSource('TestForCharDo',
  8879. LinesToStr([ // statements
  8880. 'this.c = "";']),
  8881. LinesToStr([ // this.$main
  8882. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8883. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8884. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8885. '']));
  8886. end;
  8887. procedure TTestModule.TestForCharInDo;
  8888. begin
  8889. StartProgram(false);
  8890. Add([
  8891. 'type',
  8892. ' TSetOfChar = set of char;',
  8893. ' TCharRg = ''a''..''z'';',
  8894. ' TSetOfCharRg = set of TCharRg;',
  8895. 'const Foo = ''foo'';',
  8896. 'var',
  8897. ' c,c2: char;',
  8898. ' s: string;',
  8899. ' a1: array of char;',
  8900. ' a2: array[1..3] of char;',
  8901. ' soc: TSetOfChar;',
  8902. ' socr: TSetOfCharRg;',
  8903. ' cr: TCharRg;',
  8904. 'begin',
  8905. ' for c in foo do ;',
  8906. ' for c in s do ;',
  8907. ' for c in char do ;',
  8908. ' for c in a1 do ;',
  8909. ' for c in a2 do ;',
  8910. ' for c in [''1''..''3''] do ;',
  8911. ' for c in TSetOfChar do ;',
  8912. ' for c in TCharRg do ;',
  8913. ' for c in soc do c2:=c;',
  8914. ' for c in TSetOfCharRg do ;',
  8915. ' for c in socr do ;',
  8916. ' for cr in TCharRg do ;',
  8917. ' for cr in TSetOfCharRg do ;',
  8918. ' for cr in socr do ;',
  8919. '']);
  8920. ConvertProgram;
  8921. CheckSource('TestForCharInDo',
  8922. LinesToStr([ // statements
  8923. 'this.Foo = "foo";',
  8924. 'this.c = "";',
  8925. 'this.c2 = "";',
  8926. 'this.s = "";',
  8927. 'this.a1 = [];',
  8928. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8929. 'this.soc = {};',
  8930. 'this.socr = {};',
  8931. 'this.cr = "a";',
  8932. '']),
  8933. LinesToStr([ // this.$main
  8934. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8935. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8936. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8937. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8938. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8939. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8940. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8941. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8942. 'for (var $l8 in $mod.soc) {',
  8943. ' $mod.c = String.fromCharCode($l8);',
  8944. ' $mod.c2 = $mod.c;',
  8945. '};',
  8946. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8947. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8948. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8949. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8950. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8951. '']));
  8952. end;
  8953. procedure TTestModule.TestProcTwoArgs;
  8954. begin
  8955. StartProgram(false);
  8956. Add('procedure Test(a,b: longint);');
  8957. Add('begin');
  8958. Add('end;');
  8959. Add('begin');
  8960. ConvertProgram;
  8961. CheckSource('TestProcTwoArgs',
  8962. LinesToStr([ // statements
  8963. 'this.Test = function (a,b) {',
  8964. '};'
  8965. ]),
  8966. LinesToStr([ // this.$main
  8967. ''
  8968. ]));
  8969. end;
  8970. procedure TTestModule.TestProc_DefaultValue;
  8971. begin
  8972. StartProgram(false);
  8973. Add('procedure p1(i: longint = 1);');
  8974. Add('begin');
  8975. Add('end;');
  8976. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8977. Add('begin');
  8978. Add('end;');
  8979. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8980. Add('begin');
  8981. Add('end;');
  8982. Add('begin');
  8983. Add(' p1;');
  8984. Add(' p1();');
  8985. Add(' p1(11);');
  8986. Add(' p2;');
  8987. Add(' p2();');
  8988. Add(' p2(12);');
  8989. Add(' p2(13,''b'');');
  8990. Add(' p3();');
  8991. ConvertProgram;
  8992. CheckSource('TestProc_DefaultValue',
  8993. LinesToStr([ // statements
  8994. 'this.p1 = function (i) {',
  8995. '};',
  8996. 'this.p2 = function (i,c) {',
  8997. '};',
  8998. 'this.p3 = function (d,b,s) {',
  8999. '};'
  9000. ]),
  9001. LinesToStr([ // this.$main
  9002. ' $mod.p1(1);',
  9003. ' $mod.p1(1);',
  9004. ' $mod.p1(11);',
  9005. ' $mod.p2(1,"a");',
  9006. ' $mod.p2(1,"a");',
  9007. ' $mod.p2(12,"a");',
  9008. ' $mod.p2(13,"b");',
  9009. ' $mod.p3(1.0,false,"abc");'
  9010. ]));
  9011. end;
  9012. procedure TTestModule.TestFunctionInt;
  9013. begin
  9014. StartProgram(false);
  9015. Add('function MyTest(Bar: longint): longint;');
  9016. Add('begin');
  9017. Add(' Result:=2*bar');
  9018. Add('end;');
  9019. Add('begin');
  9020. ConvertProgram;
  9021. CheckSource('TestFunctionInt',
  9022. LinesToStr([ // statements
  9023. 'this.MyTest = function (Bar) {',
  9024. ' var Result = 0;',
  9025. ' Result = 2*Bar;',
  9026. ' return Result;',
  9027. '};'
  9028. ]),
  9029. LinesToStr([ // this.$main
  9030. ''
  9031. ]));
  9032. end;
  9033. procedure TTestModule.TestFunctionString;
  9034. begin
  9035. StartProgram(false);
  9036. Add('function Test(Bar: string): string;');
  9037. Add('begin');
  9038. Add(' Result:=bar+BAR');
  9039. Add('end;');
  9040. Add('begin');
  9041. ConvertProgram;
  9042. CheckSource('TestFunctionString',
  9043. LinesToStr([ // statements
  9044. 'this.Test = function (Bar) {',
  9045. ' var Result = "";',
  9046. ' Result = Bar+Bar;',
  9047. ' return Result;',
  9048. '};'
  9049. ]),
  9050. LinesToStr([ // this.$main
  9051. ''
  9052. ]));
  9053. end;
  9054. procedure TTestModule.TestIfThen;
  9055. begin
  9056. StartProgram(false);
  9057. Add([
  9058. 'var b: boolean;',
  9059. 'begin',
  9060. ' if b then ;',
  9061. ' if b then else ;']);
  9062. ConvertProgram;
  9063. CheckSource('TestIfThen',
  9064. LinesToStr([ // statements
  9065. 'this.b = false;',
  9066. '']),
  9067. LinesToStr([ // this.$main
  9068. 'if ($mod.b) ;',
  9069. 'if ($mod.b) ;',
  9070. '']));
  9071. end;
  9072. procedure TTestModule.TestForLoop;
  9073. begin
  9074. StartProgram(false);
  9075. Add('var');
  9076. Add(' vI, vJ, vN: longint;');
  9077. Add('begin');
  9078. Add(' VJ:=0;');
  9079. Add(' VN:=3;');
  9080. Add(' for VI:=1 to VN do');
  9081. Add(' begin');
  9082. Add(' VJ:=VJ+VI;');
  9083. Add(' end;');
  9084. ConvertProgram;
  9085. CheckSource('TestForLoop',
  9086. LinesToStr([ // statements
  9087. 'this.vI = 0;',
  9088. 'this.vJ = 0;',
  9089. 'this.vN = 0;'
  9090. ]),
  9091. LinesToStr([ // this.$main
  9092. ' $mod.vJ = 0;',
  9093. ' $mod.vN = 3;',
  9094. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9095. ' $mod.vI = $l;',
  9096. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9097. ' };',
  9098. '']));
  9099. end;
  9100. procedure TTestModule.TestForLoopInsideFunction;
  9101. begin
  9102. StartProgram(false);
  9103. Add('function SumNumbers(Count: longint): longint;');
  9104. Add('var');
  9105. Add(' vI, vJ: longint;');
  9106. Add('begin');
  9107. Add(' vj:=0;');
  9108. Add(' for vi:=1 to count do');
  9109. Add(' begin');
  9110. Add(' vj:=vj+vi;');
  9111. Add(' end;');
  9112. Add('end;');
  9113. Add('begin');
  9114. Add(' sumnumbers(3);');
  9115. ConvertProgram;
  9116. CheckSource('TestForLoopInsideFunction',
  9117. LinesToStr([ // statements
  9118. 'this.SumNumbers = function (Count) {',
  9119. ' var Result = 0;',
  9120. ' var vI = 0;',
  9121. ' var vJ = 0;',
  9122. ' vJ = 0;',
  9123. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9124. ' vI = $l;',
  9125. ' vJ = vJ + vI;',
  9126. ' };',
  9127. ' return Result;',
  9128. '};'
  9129. ]),
  9130. LinesToStr([ // $mod.$main
  9131. ' $mod.SumNumbers(3);'
  9132. ]));
  9133. end;
  9134. procedure TTestModule.TestForLoop_ReadVarAfter;
  9135. begin
  9136. StartProgram(false);
  9137. Add('var');
  9138. Add(' vI: longint;');
  9139. Add('begin');
  9140. Add(' for vi:=1 to 2 do ;');
  9141. Add(' if vi=3 then ;');
  9142. ConvertProgram;
  9143. CheckSource('TestForLoop',
  9144. LinesToStr([ // statements
  9145. 'this.vI = 0;'
  9146. ]),
  9147. LinesToStr([ // this.$main
  9148. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9149. ' if ($mod.vI===3) ;'
  9150. ]));
  9151. end;
  9152. procedure TTestModule.TestForLoop_Nested;
  9153. begin
  9154. StartProgram(false);
  9155. Add('function SumNumbers(Count: longint): longint;');
  9156. Add('var');
  9157. Add(' vI, vJ, vK: longint;');
  9158. Add('begin');
  9159. Add(' VK:=0;');
  9160. Add(' for VI:=1 to count do');
  9161. Add(' begin');
  9162. Add(' for vj:=1 to vi do');
  9163. Add(' begin');
  9164. Add(' vk:=VK+VI;');
  9165. Add(' end;');
  9166. Add(' end;');
  9167. Add('end;');
  9168. Add('begin');
  9169. Add(' sumnumbers(3);');
  9170. ConvertProgram;
  9171. CheckSource('TestForLoopInFunction',
  9172. LinesToStr([ // statements
  9173. 'this.SumNumbers = function (Count) {',
  9174. ' var Result = 0;',
  9175. ' var vI = 0;',
  9176. ' var vJ = 0;',
  9177. ' var vK = 0;',
  9178. ' vK = 0;',
  9179. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9180. ' vI = $l;',
  9181. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9182. ' vJ = $l1;',
  9183. ' vK = vK + vI;',
  9184. ' };',
  9185. ' };',
  9186. ' return Result;',
  9187. '};'
  9188. ]),
  9189. LinesToStr([ // $mod.$main
  9190. ' $mod.SumNumbers(3);'
  9191. ]));
  9192. end;
  9193. procedure TTestModule.TestRepeatUntil;
  9194. begin
  9195. StartProgram(false);
  9196. Add('var');
  9197. Add(' vI, vJ, vN: longint;');
  9198. Add('begin');
  9199. Add(' vn:=3;');
  9200. Add(' vj:=0;');
  9201. Add(' VI:=0;');
  9202. Add(' repeat');
  9203. Add(' VI:=vi+1;');
  9204. Add(' vj:=VJ+vI;');
  9205. Add(' until vi>=vn');
  9206. ConvertProgram;
  9207. CheckSource('TestRepeatUntil',
  9208. LinesToStr([ // statements
  9209. 'this.vI = 0;',
  9210. 'this.vJ = 0;',
  9211. 'this.vN = 0;'
  9212. ]),
  9213. LinesToStr([ // $mod.$main
  9214. ' $mod.vN = 3;',
  9215. ' $mod.vJ = 0;',
  9216. ' $mod.vI = 0;',
  9217. ' do{',
  9218. ' $mod.vI = $mod.vI + 1;',
  9219. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9220. ' }while(!($mod.vI>=$mod.vN));'
  9221. ]));
  9222. end;
  9223. procedure TTestModule.TestAsmBlock;
  9224. begin
  9225. StartProgram(false);
  9226. Add([
  9227. 'var',
  9228. ' vI: longint;',
  9229. 'begin',
  9230. ' vi:=1;',
  9231. ' asm',
  9232. ' if (vI===1) {',
  9233. ' vI=2;',
  9234. //' console.log(''end;'');', ToDo
  9235. ' }',
  9236. ' if (vI===2){ vI=3; }',
  9237. ' end;',
  9238. ' VI:=4;']);
  9239. ConvertProgram;
  9240. CheckSource('TestAsmBlock',
  9241. LinesToStr([ // statements
  9242. 'this.vI = 0;'
  9243. ]),
  9244. LinesToStr([ // $mod.$main
  9245. '$mod.vI = 1;',
  9246. 'if (vI===1) {',
  9247. ' vI=2;',
  9248. '}',
  9249. 'if (vI===2){ vI=3; }',
  9250. ';',
  9251. '$mod.vI = 4;'
  9252. ]));
  9253. end;
  9254. procedure TTestModule.TestAsmPas_Impl;
  9255. begin
  9256. StartUnit(false);
  9257. Add('interface');
  9258. Add('const cIntf: longint = 1;');
  9259. Add('var vIntf: longint;');
  9260. Add('implementation');
  9261. Add('const cImpl: longint = 2;');
  9262. Add('var vImpl: longint;');
  9263. Add('procedure DoIt;');
  9264. Add('const cLoc: longint = 3;');
  9265. Add('var vLoc: longint;');
  9266. Add('begin;');
  9267. Add(' asm');
  9268. //Add(' pas(vIntf)=pas(cIntf);');
  9269. //Add(' pas(vImpl)=pas(cImpl);');
  9270. //Add(' pas(vLoc)=pas(cLoc);');
  9271. Add(' end;');
  9272. Add('end;');
  9273. ConvertUnit;
  9274. CheckSource('TestAsmPas_Impl',
  9275. LinesToStr([
  9276. 'var $impl = $mod.$impl;',
  9277. 'this.cIntf = 1;',
  9278. 'this.vIntf = 0;',
  9279. '']),
  9280. '', // this.$init
  9281. LinesToStr([ // implementation
  9282. '$impl.cImpl = 2;',
  9283. '$impl.vImpl = 0;',
  9284. 'var cLoc = 3;',
  9285. '$impl.DoIt = function () {',
  9286. ' var vLoc = 0;',
  9287. '};',
  9288. '']) );
  9289. end;
  9290. procedure TTestModule.TestTryFinally;
  9291. begin
  9292. StartProgram(false);
  9293. Add('var i: longint;');
  9294. Add('begin');
  9295. Add(' try');
  9296. Add(' i:=0; i:=2 div i;');
  9297. Add(' finally');
  9298. Add(' i:=3');
  9299. Add(' end;');
  9300. ConvertProgram;
  9301. CheckSource('TestTryFinally',
  9302. LinesToStr([ // statements
  9303. 'this.i = 0;'
  9304. ]),
  9305. LinesToStr([ // $mod.$main
  9306. 'try {',
  9307. ' $mod.i = 0;',
  9308. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9309. '} finally {',
  9310. ' $mod.i = 3;',
  9311. '};'
  9312. ]));
  9313. end;
  9314. procedure TTestModule.TestTryExcept;
  9315. begin
  9316. StartProgram(false);
  9317. Add([
  9318. 'type',
  9319. ' TObject = class end;',
  9320. ' Exception = class Msg: string; end;',
  9321. ' EInvalidCast = class(Exception) end;',
  9322. 'var vI: longint;',
  9323. 'begin',
  9324. ' try',
  9325. ' vi:=1;',
  9326. ' except',
  9327. ' vi:=2',
  9328. ' end;',
  9329. ' try',
  9330. ' vi:=3;',
  9331. ' except',
  9332. ' raise;',
  9333. ' end;',
  9334. ' try',
  9335. ' VI:=4;',
  9336. ' except',
  9337. ' on einvalidcast do',
  9338. ' raise;',
  9339. ' on E: exception do',
  9340. ' if e.msg='''' then',
  9341. ' raise e;',
  9342. ' else',
  9343. ' vi:=5',
  9344. ' end;',
  9345. ' try',
  9346. ' VI:=6;',
  9347. ' except',
  9348. ' on einvalidcast do ;',
  9349. ' end;',
  9350. '']);
  9351. ConvertProgram;
  9352. CheckSource('TestTryExcept',
  9353. LinesToStr([ // statements
  9354. 'rtl.createClass(this, "TObject", null, function () {',
  9355. ' this.$init = function () {',
  9356. ' };',
  9357. ' this.$final = function () {',
  9358. ' };',
  9359. '});',
  9360. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9361. ' this.$init = function () {',
  9362. ' $mod.TObject.$init.call(this);',
  9363. ' this.Msg = "";',
  9364. ' };',
  9365. '});',
  9366. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9367. '});',
  9368. 'this.vI = 0;'
  9369. ]),
  9370. LinesToStr([ // $mod.$main
  9371. 'try {',
  9372. ' $mod.vI = 1;',
  9373. '} catch ($e) {',
  9374. ' $mod.vI = 2;',
  9375. '};',
  9376. 'try {',
  9377. ' $mod.vI = 3;',
  9378. '} catch ($e) {',
  9379. ' throw $e;',
  9380. '};',
  9381. 'try {',
  9382. ' $mod.vI = 4;',
  9383. '} catch ($e) {',
  9384. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9385. ' throw $e',
  9386. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9387. ' var E = $e;',
  9388. ' if (E.Msg === "") throw E;',
  9389. ' } else {',
  9390. ' $mod.vI = 5;',
  9391. ' }',
  9392. '};',
  9393. 'try {',
  9394. ' $mod.vI = 6;',
  9395. '} catch ($e) {',
  9396. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9397. ' } else throw $e',
  9398. '};',
  9399. '']));
  9400. end;
  9401. procedure TTestModule.TestTryExcept_ReservedWords;
  9402. begin
  9403. StartProgram(false);
  9404. Add([
  9405. 'type',
  9406. ' TObject = class end;',
  9407. ' Exception = class',
  9408. ' Symbol: string;',
  9409. ' end;',
  9410. 'var &try: longint;',
  9411. 'begin',
  9412. ' try',
  9413. ' &try:=4;',
  9414. ' except',
  9415. ' on Error: exception do',
  9416. ' if errOR.symBol='''' then',
  9417. ' raise ERRor;',
  9418. ' end;',
  9419. '']);
  9420. ConvertProgram;
  9421. CheckSource('TestTryExcept_ReservedWords',
  9422. LinesToStr([ // statements
  9423. 'rtl.createClass(this, "TObject", null, function () {',
  9424. ' this.$init = function () {',
  9425. ' };',
  9426. ' this.$final = function () {',
  9427. ' };',
  9428. '});',
  9429. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9430. ' this.$init = function () {',
  9431. ' $mod.TObject.$init.call(this);',
  9432. ' this.Symbol = "";',
  9433. ' };',
  9434. '});',
  9435. 'this.Try = 0;',
  9436. '']),
  9437. LinesToStr([ // $mod.$main
  9438. 'try {',
  9439. ' $mod.Try = 4;',
  9440. '} catch ($e) {',
  9441. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9442. ' var error = $e;',
  9443. ' if (error.Symbol === "") throw error;',
  9444. ' } else throw $e',
  9445. '};',
  9446. '']));
  9447. end;
  9448. procedure TTestModule.TestIfThenRaiseElse;
  9449. begin
  9450. StartProgram(false);
  9451. Add([
  9452. 'type',
  9453. ' TObject = class',
  9454. ' constructor Create;',
  9455. ' end;',
  9456. 'constructor TObject.Create;',
  9457. 'begin',
  9458. 'end;',
  9459. 'var b: boolean;',
  9460. 'begin',
  9461. ' if b then',
  9462. ' raise TObject.Create',
  9463. ' else',
  9464. ' b:=false;',
  9465. '']);
  9466. ConvertProgram;
  9467. CheckSource('TestIfThenRaiseElse',
  9468. LinesToStr([ // statements
  9469. 'rtl.createClass(this, "TObject", null, function () {',
  9470. ' this.$init = function () {',
  9471. ' };',
  9472. ' this.$final = function () {',
  9473. ' };',
  9474. ' this.Create = function () {',
  9475. ' return this;',
  9476. ' };',
  9477. '});',
  9478. 'this.b = false;',
  9479. '']),
  9480. LinesToStr([ // $mod.$main
  9481. 'if ($mod.b) {',
  9482. ' throw $mod.TObject.$create("Create")}',
  9483. ' else $mod.b = false;',
  9484. '']));
  9485. end;
  9486. procedure TTestModule.TestCaseOf;
  9487. begin
  9488. StartProgram(false);
  9489. Add([
  9490. 'const e: longint; external name ''$e'';',
  9491. 'var vI: longint;',
  9492. 'begin',
  9493. ' case vi of',
  9494. ' 1: ;',
  9495. ' 2: vi:=3;',
  9496. ' e: ;',
  9497. ' else',
  9498. ' VI:=4',
  9499. ' end;']);
  9500. ConvertProgram;
  9501. CheckSource('TestCaseOf',
  9502. LinesToStr([ // statements
  9503. 'this.vI = 0;'
  9504. ]),
  9505. LinesToStr([ // $mod.$main
  9506. 'var $tmp = $mod.vI;',
  9507. 'if ($tmp === 1) {}',
  9508. 'else if ($tmp === 2) {',
  9509. ' $mod.vI = 3}',
  9510. ' else if ($tmp === $e) {}',
  9511. 'else {',
  9512. ' $mod.vI = 4;',
  9513. '};'
  9514. ]));
  9515. end;
  9516. procedure TTestModule.TestCaseOf_UseSwitch;
  9517. begin
  9518. StartProgram(false);
  9519. Converter.UseSwitchStatement:=true;
  9520. Add('var Vi: longint;');
  9521. Add('begin');
  9522. Add(' case vi of');
  9523. Add(' 1: ;');
  9524. Add(' 2: VI:=3;');
  9525. Add(' else');
  9526. Add(' vi:=4');
  9527. Add(' end;');
  9528. ConvertProgram;
  9529. CheckSource('TestCaseOf_UseSwitch',
  9530. LinesToStr([ // statements
  9531. 'this.Vi = 0;'
  9532. ]),
  9533. LinesToStr([ // $mod.$main
  9534. 'switch ($mod.Vi) {',
  9535. 'case 1:',
  9536. ' break;',
  9537. 'case 2:',
  9538. ' $mod.Vi = 3;',
  9539. ' break;',
  9540. 'default:',
  9541. ' $mod.Vi = 4;',
  9542. '};'
  9543. ]));
  9544. end;
  9545. procedure TTestModule.TestCaseOfNoElse;
  9546. begin
  9547. StartProgram(false);
  9548. Add('var Vi: longint;');
  9549. Add('begin');
  9550. Add(' case vi of');
  9551. Add(' 1: begin vi:=2; VI:=3; end;');
  9552. Add(' end;');
  9553. ConvertProgram;
  9554. CheckSource('TestCaseOfNoElse',
  9555. LinesToStr([ // statements
  9556. 'this.Vi = 0;'
  9557. ]),
  9558. LinesToStr([ // $mod.$main
  9559. 'var $tmp = $mod.Vi;',
  9560. 'if ($tmp === 1) {',
  9561. ' $mod.Vi = 2;',
  9562. ' $mod.Vi = 3;',
  9563. '};'
  9564. ]));
  9565. end;
  9566. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9567. begin
  9568. StartProgram(false);
  9569. Converter.UseSwitchStatement:=true;
  9570. Add('var vI: longint;');
  9571. Add('begin');
  9572. Add(' case vi of');
  9573. Add(' 1: begin VI:=2; vi:=3; end;');
  9574. Add(' end;');
  9575. ConvertProgram;
  9576. CheckSource('TestCaseOfNoElse_UseSwitch',
  9577. LinesToStr([ // statements
  9578. 'this.vI = 0;'
  9579. ]),
  9580. LinesToStr([ // $mod.$main
  9581. 'switch ($mod.vI) {',
  9582. 'case 1:',
  9583. ' $mod.vI = 2;',
  9584. ' $mod.vI = 3;',
  9585. ' break;',
  9586. '};'
  9587. ]));
  9588. end;
  9589. procedure TTestModule.TestCaseOfRange;
  9590. begin
  9591. StartProgram(false);
  9592. Add('var vI: longint;');
  9593. Add('begin');
  9594. Add(' case vi of');
  9595. Add(' 1..3: vi:=14;');
  9596. Add(' 4,5: vi:=16;');
  9597. Add(' 6..7,9..10: ;');
  9598. Add(' else ;');
  9599. Add(' end;');
  9600. ConvertProgram;
  9601. CheckSource('TestCaseOfRange',
  9602. LinesToStr([ // statements
  9603. 'this.vI = 0;'
  9604. ]),
  9605. LinesToStr([ // $mod.$main
  9606. 'var $tmp = $mod.vI;',
  9607. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9608. ' $mod.vI = 14',
  9609. '} else if (($tmp === 4) || ($tmp === 5)){',
  9610. ' $mod.vI = 16',
  9611. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9612. ]));
  9613. end;
  9614. procedure TTestModule.TestCaseOfString;
  9615. begin
  9616. StartProgram(false);
  9617. Add([
  9618. 'var s,h: string;',
  9619. 'begin',
  9620. ' case s of',
  9621. ' ''foo'': s:=h;',
  9622. ' ''a''..''z'': h:=s;',
  9623. ' ''ў'', ''ё'': ;',
  9624. ' ''Б''..''Я'': ;',
  9625. ' end;',
  9626. '']);
  9627. ConvertProgram;
  9628. CheckSource('TestCaseOfString',
  9629. LinesToStr([ // statements
  9630. 'this.s = "";',
  9631. 'this.h = "";',
  9632. '']),
  9633. LinesToStr([ // $mod.$main
  9634. 'var $tmp = $mod.s;',
  9635. 'if ($tmp === "foo") {',
  9636. ' $mod.s = $mod.h}',
  9637. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9638. ' $mod.h = $mod.s}',
  9639. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9640. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9641. '']));
  9642. end;
  9643. procedure TTestModule.TestCaseOfChar;
  9644. begin
  9645. StartProgram(false);
  9646. Add([
  9647. 'var s,h: char;',
  9648. 'begin',
  9649. ' case s of',
  9650. ' ''a''..''z'': h:=s;',
  9651. ' ''ä'': ;',
  9652. ' ''ў'', ''ё'': ;',
  9653. ' ''Б''..''Я'': ;',
  9654. ' end;',
  9655. '']);
  9656. ConvertProgram;
  9657. CheckSource('TestCaseOfString',
  9658. LinesToStr([ // statements
  9659. 'this.s = "";',
  9660. 'this.h = "";',
  9661. '']),
  9662. LinesToStr([ // $mod.$main
  9663. 'var $tmp = $mod.s;',
  9664. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9665. ' $mod.h = $mod.s}',
  9666. ' else if ($tmp === "ä") {}',
  9667. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9668. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9669. '']));
  9670. end;
  9671. procedure TTestModule.TestCaseOfExternalClassConst;
  9672. begin
  9673. StartProgram(false);
  9674. Add([
  9675. '{$modeswitch externalclass}',
  9676. 'type',
  9677. ' TBird = class external name ''Bird''',
  9678. ' const e: longint;',
  9679. ' end;',
  9680. 'var vI: longint;',
  9681. 'begin',
  9682. ' case vi of',
  9683. ' 1: vi:=3;',
  9684. ' TBird.e: ;',
  9685. ' end;']);
  9686. ConvertProgram;
  9687. CheckSource('TestCaseOfExternalClassConst',
  9688. LinesToStr([ // statements
  9689. 'this.vI = 0;'
  9690. ]),
  9691. LinesToStr([ // $mod.$main
  9692. 'var $tmp = $mod.vI;',
  9693. 'if ($tmp === 1) {',
  9694. ' $mod.vI = 3}',
  9695. ' else if ($tmp === Bird.e) ;'
  9696. ]));
  9697. end;
  9698. procedure TTestModule.TestDebugger;
  9699. begin
  9700. StartProgram(false);
  9701. Add([
  9702. 'procedure DoIt;',
  9703. 'begin',
  9704. ' deBugger;',
  9705. ' DeBugger();',
  9706. 'end;',
  9707. 'begin',
  9708. ' Debugger;']);
  9709. ConvertProgram;
  9710. CheckSource('TestDebugger',
  9711. LinesToStr([ // statements
  9712. 'this.DoIt = function () {',
  9713. ' debugger;',
  9714. ' debugger;',
  9715. '};',
  9716. '']),
  9717. LinesToStr([ // $mod.$main
  9718. 'debugger;',
  9719. '']));
  9720. end;
  9721. procedure TTestModule.TestArray_Dynamic;
  9722. begin
  9723. StartProgram(false);
  9724. Add([
  9725. 'type',
  9726. ' TArrayInt = array of longint;',
  9727. 'var',
  9728. ' Arr: TArrayInt;',
  9729. ' i: longint;',
  9730. ' b: boolean;',
  9731. 'begin',
  9732. ' SetLength(arr,3);',
  9733. ' arr[0]:=4;',
  9734. ' arr[1]:=length(arr)+arr[0];',
  9735. ' arr[i]:=5;',
  9736. ' arr[arr[i]]:=arr[6];',
  9737. ' i:=low(arr);',
  9738. ' i:=high(arr);',
  9739. ' b:=Assigned(arr);',
  9740. ' Arr:=default(TArrayInt);']);
  9741. ConvertProgram;
  9742. CheckSource('TestArray_Dynamic',
  9743. LinesToStr([ // statements
  9744. 'this.Arr = [];',
  9745. 'this.i = 0;',
  9746. 'this.b = false;'
  9747. ]),
  9748. LinesToStr([ // $mod.$main
  9749. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9750. '$mod.Arr[0] = 4;',
  9751. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9752. '$mod.Arr[$mod.i] = 5;',
  9753. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9754. '$mod.i = 0;',
  9755. '$mod.i = rtl.length($mod.Arr) - 1;',
  9756. '$mod.b = rtl.length($mod.Arr) > 0;',
  9757. '$mod.Arr = [];',
  9758. '']));
  9759. end;
  9760. procedure TTestModule.TestArray_Dynamic_Nil;
  9761. begin
  9762. StartProgram(false);
  9763. Add('type');
  9764. Add(' TArrayInt = array of longint;');
  9765. Add('var');
  9766. Add(' Arr: TArrayInt;');
  9767. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9768. Add('begin');
  9769. Add(' arr:=nil;');
  9770. Add(' if arr=nil then;');
  9771. Add(' if nil=arr then;');
  9772. Add(' if arr<>nil then;');
  9773. Add(' if nil<>arr then;');
  9774. Add(' DoIt(nil,nil);');
  9775. ConvertProgram;
  9776. CheckSource('TestArray_Dynamic',
  9777. LinesToStr([ // statements
  9778. 'this.Arr = [];',
  9779. 'this.DoIt = function(i,j){',
  9780. '};'
  9781. ]),
  9782. LinesToStr([ // $mod.$main
  9783. '$mod.Arr = [];',
  9784. 'if (rtl.length($mod.Arr) === 0) ;',
  9785. 'if (rtl.length($mod.Arr) === 0) ;',
  9786. 'if (rtl.length($mod.Arr) > 0) ;',
  9787. 'if (rtl.length($mod.Arr) > 0) ;',
  9788. '$mod.DoIt([],[]);',
  9789. '']));
  9790. end;
  9791. procedure TTestModule.TestArray_DynMultiDimensional;
  9792. begin
  9793. StartProgram(false);
  9794. Add([
  9795. 'type',
  9796. ' TArrayInt = array of longint;',
  9797. ' TArrayArrayInt = array of TArrayInt;',
  9798. 'var',
  9799. ' Arr: TArrayInt;',
  9800. ' Arr2: TArrayArrayInt;',
  9801. ' i: longint;',
  9802. 'begin',
  9803. ' arr2:=nil;',
  9804. ' if arr2=nil then;',
  9805. ' if nil=arr2 then;',
  9806. ' i:=low(arr2);',
  9807. ' i:=low(arr2[1]);',
  9808. ' i:=high(arr2);',
  9809. ' i:=high(arr2[2]);',
  9810. ' arr2[3]:=arr;',
  9811. ' arr2[4][5]:=i;',
  9812. ' i:=arr2[6][7];',
  9813. ' arr2[8,9]:=i;',
  9814. ' i:=arr2[10,11];',
  9815. ' SetLength(arr2,14);',
  9816. ' SetLength(arr2[15],16);']);
  9817. ConvertProgram;
  9818. CheckSource('TestArray_Dynamic',
  9819. LinesToStr([ // statements
  9820. 'this.Arr = [];',
  9821. 'this.Arr2 = [];',
  9822. 'this.i = 0;'
  9823. ]),
  9824. LinesToStr([ // $mod.$main
  9825. '$mod.Arr2 = [];',
  9826. 'if (rtl.length($mod.Arr2) === 0) ;',
  9827. 'if (rtl.length($mod.Arr2) === 0) ;',
  9828. '$mod.i = 0;',
  9829. '$mod.i = 0;',
  9830. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9831. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9832. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9833. '$mod.Arr2[4][5] = $mod.i;',
  9834. '$mod.i = $mod.Arr2[6][7];',
  9835. '$mod.Arr2[8][9] = $mod.i;',
  9836. '$mod.i = $mod.Arr2[10][11];',
  9837. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9838. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9839. '']));
  9840. end;
  9841. procedure TTestModule.TestArray_DynamicAssign;
  9842. begin
  9843. StartProgram(false);
  9844. Add([
  9845. 'type',
  9846. ' TArrayInt = array of longint;',
  9847. ' TArrayArrayInt = array of TArrayInt;',
  9848. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9849. 'begin',
  9850. 'end;',
  9851. 'procedure Fly(var a: TArrayInt);',
  9852. 'begin',
  9853. 'end;',
  9854. 'var',
  9855. ' Arr: TArrayInt;',
  9856. ' Arr2: TArrayArrayInt;',
  9857. 'begin',
  9858. ' arr:=nil;',
  9859. ' arr2:=nil;',
  9860. ' arr2[1]:=nil;',
  9861. ' arr2[2]:=arr;',
  9862. ' Run(arr,arr,arr);',
  9863. ' Fly(arr);',
  9864. ' Run(arr2[4],arr2[5],arr2[6]);',
  9865. ' Fly(arr2[7]);',
  9866. '']);
  9867. ConvertProgram;
  9868. CheckSource('TestArray_DynamicAssign',
  9869. LinesToStr([ // statements
  9870. 'this.Run = function (a, b, c) {',
  9871. '};',
  9872. 'this.Fly = function (a) {',
  9873. '};',
  9874. 'this.Arr = [];',
  9875. 'this.Arr2 = [];',
  9876. '']),
  9877. LinesToStr([ // $mod.$main
  9878. '$mod.Arr = [];',
  9879. '$mod.Arr2 = [];',
  9880. '$mod.Arr2[1] = [];',
  9881. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9882. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9883. '$mod.Fly({',
  9884. ' p: $mod,',
  9885. ' get: function () {',
  9886. ' return this.p.Arr;',
  9887. ' },',
  9888. ' set: function (v) {',
  9889. ' this.p.Arr = v;',
  9890. ' }',
  9891. '});',
  9892. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9893. '$mod.Fly({',
  9894. ' a: 7,',
  9895. ' p: $mod.Arr2,',
  9896. ' get: function () {',
  9897. ' return this.p[this.a];',
  9898. ' },',
  9899. ' set: function (v) {',
  9900. ' this.p[this.a] = v;',
  9901. ' }',
  9902. '});',
  9903. '']));
  9904. end;
  9905. procedure TTestModule.TestArray_StaticInt;
  9906. begin
  9907. StartProgram(false);
  9908. Add('type');
  9909. Add(' TArrayInt = array[2..4] of longint;');
  9910. Add('var');
  9911. Add(' Arr: TArrayInt;');
  9912. Add(' Arr2: TArrayInt = (5,6,7);');
  9913. Add(' i: longint;');
  9914. Add(' b: boolean;');
  9915. Add('begin');
  9916. Add(' arr[2]:=4;');
  9917. Add(' arr[3]:=arr[2]+arr[3];');
  9918. Add(' arr[i]:=5;');
  9919. Add(' arr[arr[i]]:=arr[high(arr)];');
  9920. Add(' i:=low(arr);');
  9921. Add(' i:=high(arr);');
  9922. Add(' b:=arr[2]=arr[3];');
  9923. Add(' arr:=default(TArrayInt);');
  9924. ConvertProgram;
  9925. CheckSource('TestArray_StaticInt',
  9926. LinesToStr([ // statements
  9927. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9928. 'this.Arr2 = [5, 6, 7];',
  9929. 'this.i = 0;',
  9930. 'this.b = false;'
  9931. ]),
  9932. LinesToStr([ // $mod.$main
  9933. '$mod.Arr[0] = 4;',
  9934. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9935. '$mod.Arr[$mod.i-2] = 5;',
  9936. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9937. '$mod.i = 2;',
  9938. '$mod.i = 4;',
  9939. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9940. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9941. '']));
  9942. end;
  9943. procedure TTestModule.TestArray_StaticBool;
  9944. begin
  9945. StartProgram(false);
  9946. Add('type');
  9947. Add(' TBools = array[boolean] of boolean;');
  9948. Add(' TBool2 = array[true..true] of boolean;');
  9949. Add('var');
  9950. Add(' Arr: TBools;');
  9951. Add(' Arr2: TBool2;');
  9952. Add(' Arr3: TBools = (true,false);');
  9953. Add(' b: boolean;');
  9954. Add('begin');
  9955. Add(' b:=low(arr);');
  9956. Add(' b:=high(arr);');
  9957. Add(' arr[true]:=false;');
  9958. Add(' arr[false]:=arr[b] or arr[true];');
  9959. Add(' arr[b]:=true;');
  9960. Add(' arr[arr[b]]:=arr[high(arr)];');
  9961. Add(' b:=arr[false]=arr[true];');
  9962. Add(' b:=low(arr2);');
  9963. Add(' b:=high(arr2);');
  9964. Add(' arr2[true]:=true;');
  9965. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9966. Add(' arr2[b]:=false;');
  9967. ConvertProgram;
  9968. CheckSource('TestArray_StaticBool',
  9969. LinesToStr([ // statements
  9970. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9971. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9972. 'this.Arr3 = [true, false];',
  9973. 'this.b = false;'
  9974. ]),
  9975. LinesToStr([ // $mod.$main
  9976. '$mod.b = false;',
  9977. '$mod.b = true;',
  9978. '$mod.Arr[1] = false;',
  9979. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9980. '$mod.Arr[+$mod.b] = true;',
  9981. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9982. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9983. '$mod.b = true;',
  9984. '$mod.b = true;',
  9985. '$mod.Arr2[0] = true;',
  9986. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9987. '$mod.Arr2[1-$mod.b] = false;',
  9988. '']));
  9989. end;
  9990. procedure TTestModule.TestArray_StaticChar;
  9991. begin
  9992. StartProgram(false);
  9993. Add([
  9994. 'type',
  9995. ' TChars = array[char] of char;',
  9996. ' TChars2 = array[''a''..''z''] of char;',
  9997. 'var',
  9998. ' Arr: TChars;',
  9999. ' Arr2: TChars2;',
  10000. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10001. ' Arr4: array[11..13] of char = ''pas'';',
  10002. ' Arr5: array[21..22] of char = ''äö'';',
  10003. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10004. ' c: char;',
  10005. ' b: boolean;',
  10006. 'begin',
  10007. ' c:=low(arr);',
  10008. ' c:=high(arr);',
  10009. ' arr[''B'']:=''a'';',
  10010. ' arr[''D'']:=arr[c];',
  10011. ' arr[c]:=arr[''d''];',
  10012. ' arr[arr[c]]:=arr[high(arr)];',
  10013. ' b:=arr[low(arr)]=arr[''e''];',
  10014. ' c:=low(arr2);',
  10015. ' c:=high(arr2);',
  10016. ' arr2[''b'']:=''f'';',
  10017. ' arr2[''a'']:=arr2[c];',
  10018. ' arr2[c]:=arr2[''g''];']);
  10019. ConvertProgram;
  10020. CheckSource('TestArray_StaticChar',
  10021. LinesToStr([ // statements
  10022. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  10023. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  10024. 'this.Arr3 = ["p", "a", "s"];',
  10025. 'this.Arr4 = ["p", "a", "s"];',
  10026. 'this.Arr5 = ["ä", "ö"];',
  10027. 'this.Arr6 = ["ä", "ö"];',
  10028. 'this.c = "";',
  10029. 'this.b = false;',
  10030. '']),
  10031. LinesToStr([ // $mod.$main
  10032. '$mod.c = "\x00";',
  10033. '$mod.c = "\uFFFF";',
  10034. '$mod.Arr[66] = "a";',
  10035. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10036. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10037. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10038. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10039. '$mod.c = "a";',
  10040. '$mod.c = "z";',
  10041. '$mod.Arr2[1] = "f";',
  10042. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10043. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10044. '']));
  10045. end;
  10046. procedure TTestModule.TestArray_StaticMultiDim;
  10047. begin
  10048. StartProgram(false);
  10049. Add([
  10050. 'type',
  10051. ' TArrayInt = array[1..3] of longint;',
  10052. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10053. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10054. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10055. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10056. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10057. 'var',
  10058. ' Arr: TArrayInt;',
  10059. ' Arr2: TArrayArrayInt;',
  10060. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10061. ' Arr4: TArrayArrayInt;',
  10062. ' ArrDim2: TArrayDim2Int;',
  10063. ' ArrDim3: TArrayDim3Int;',
  10064. ' ArrDim4: TArrayDim4Int;',
  10065. ' i: longint;',
  10066. 'begin',
  10067. ' i:=low(arr);',
  10068. ' i:=low(arr2);',
  10069. ' i:=low(arr2[5]);',
  10070. ' i:=high(arr);',
  10071. ' i:=high(arr2);',
  10072. ' i:=high(arr2[6]);',
  10073. ' arr2[5]:=arr;',
  10074. ' arr2[6][2]:=i;',
  10075. ' i:=arr2[6][3];',
  10076. ' arr2[6,3]:=i;',
  10077. ' i:=arr2[5,2];',
  10078. ' arr2:=arr2;',// clone multi dim static array
  10079. ' arr3:=arr3;',// clone anonymous multi dim static array
  10080. ' arr4:=arr4;',
  10081. ' Arr:=Arr;',
  10082. ' ArrDim2:=ArrDim2;',
  10083. ' ArrDim3:=ArrDim3;',
  10084. ' ArrDim4:=ArrDim4;',
  10085. '']);
  10086. ConvertProgram;
  10087. CheckSource('TestArray_StaticMultiDim',
  10088. LinesToStr([ // statements
  10089. 'this.TArrayArrayInt$clone = function (a) {',
  10090. ' var b = [];',
  10091. ' b.length = 2;',
  10092. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10093. ' return b;',
  10094. '};',
  10095. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10096. ' var b = [];',
  10097. ' b.length = 2;',
  10098. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10099. ' return b;',
  10100. '};',
  10101. 'this.TArrayDim2Int$clone = function (a) {',
  10102. ' var b = [];',
  10103. ' b.length = 2;',
  10104. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10105. ' return b;',
  10106. '};',
  10107. 'this.TArrayDim3Int$clone = function (a) {',
  10108. ' var b = [];',
  10109. ' b.length = 2;',
  10110. ' for (var c = 0; c < 2; c++) {',
  10111. ' var d = b[c] = [];',
  10112. ' d.length = 3;',
  10113. ' var e = a[c];',
  10114. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10115. ' };',
  10116. ' return b;',
  10117. '};',
  10118. 'this.TArrayDim4Int$clone = function (a) {',
  10119. ' var b = [];',
  10120. ' b.length = 2;',
  10121. ' for (var c = 0; c < 2; c++) {',
  10122. ' var d = b[c] = [];',
  10123. ' d.length = 3;',
  10124. ' var e = a[c];',
  10125. ' for (var f = 0; f < 3; f++) {',
  10126. ' var g = d[f] = [];',
  10127. ' g.length = 4;',
  10128. ' var h = e[f];',
  10129. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10130. ' };',
  10131. ' };',
  10132. ' return b;',
  10133. '};',
  10134. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10135. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10136. 'this.Arr3$a$clone = function (a) {',
  10137. ' var b = [];',
  10138. ' b.length = 2;',
  10139. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10140. ' return b;',
  10141. '};',
  10142. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10143. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10144. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10145. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10146. 'this.ArrDim4 = rtl.arraySetLength(',
  10147. ' null,',
  10148. ' 0,',
  10149. ' 2,',
  10150. ' 3,',
  10151. ' 4,',
  10152. ' 5',
  10153. ');',
  10154. 'this.i = 0;'
  10155. ]),
  10156. LinesToStr([ // $mod.$main
  10157. '$mod.i = 1;',
  10158. '$mod.i = 5;',
  10159. '$mod.i = 1;',
  10160. '$mod.i = 3;',
  10161. '$mod.i = 6;',
  10162. '$mod.i = 3;',
  10163. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10164. '$mod.Arr2[1][1] = $mod.i;',
  10165. '$mod.i = $mod.Arr2[1][2];',
  10166. '$mod.Arr2[1][2] = $mod.i;',
  10167. '$mod.i = $mod.Arr2[0][1];',
  10168. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10169. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10170. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10171. '$mod.Arr = $mod.Arr.slice(0);',
  10172. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10173. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10174. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10175. '']));
  10176. end;
  10177. procedure TTestModule.TestArray_StaticInFunction;
  10178. begin
  10179. StartProgram(false);
  10180. Add([
  10181. 'const TArrayInt = 3;',
  10182. 'const TArrayArrayInt = 4;',
  10183. 'procedure DoIt;',
  10184. 'type',
  10185. ' TArrayInt = array[1..3] of longint;',
  10186. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10187. 'var',
  10188. ' Arr: TArrayInt;',
  10189. ' Arr2: TArrayArrayInt;',
  10190. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10191. ' i: longint;',
  10192. 'begin',
  10193. ' arr2[5]:=arr;',
  10194. ' arr2:=arr2;',// clone multi dim static array
  10195. ' arr3:=arr3;',// clone multi dim anonymous static array
  10196. 'end;',
  10197. 'begin',
  10198. '']);
  10199. ConvertProgram;
  10200. CheckSource('TestArray_StaticInFunction',
  10201. LinesToStr([ // statements
  10202. 'this.TArrayInt = 3;',
  10203. 'this.TArrayArrayInt = 4;',
  10204. 'var TArrayArrayInt$1$clone = function (a) {',
  10205. ' var b = [];',
  10206. ' b.length = 2;',
  10207. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10208. ' return b;',
  10209. '};',
  10210. 'var Arr3$a$clone = function (a) {',
  10211. ' var b = [];',
  10212. ' b.length = 2;',
  10213. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10214. ' return b;',
  10215. '};',
  10216. 'this.DoIt = function () {',
  10217. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10218. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10219. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10220. ' var i = 0;',
  10221. ' Arr2[0] = Arr.slice(0);',
  10222. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10223. ' Arr3 = Arr3$a$clone(Arr3);',
  10224. '};',
  10225. '']),
  10226. LinesToStr([ // $mod.$main
  10227. '']));
  10228. end;
  10229. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10230. begin
  10231. StartProgram(false);
  10232. Add([
  10233. 'type',
  10234. ' TArrayInt = array[1..3,1..2] of longint;',
  10235. 'var',
  10236. ' a,b: TArrayInt;',
  10237. 'begin',
  10238. ' if a=b then ;',
  10239. '']);
  10240. SetExpectedPasResolverError('compare static array is not supported',
  10241. nXIsNotSupported);
  10242. ConvertProgram;
  10243. end;
  10244. procedure TTestModule.TestArrayOfRecord;
  10245. begin
  10246. StartProgram(false);
  10247. Add([
  10248. 'type',
  10249. ' TRec = record',
  10250. ' Int: longint;',
  10251. ' end;',
  10252. ' TArrayRec = array of TRec;',
  10253. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10254. 'begin',
  10255. 'end;',
  10256. 'var',
  10257. ' Arr: TArrayRec;',
  10258. ' r: TRec;',
  10259. ' i: longint;',
  10260. 'begin',
  10261. ' SetLength(arr,3);',
  10262. ' arr[0].int:=4;',
  10263. ' arr[1].int:=length(arr)+arr[2].int;',
  10264. ' arr[arr[i].int].int:=arr[5].int;',
  10265. ' arr[7]:=r;',
  10266. ' r:=arr[8];',
  10267. ' i:=low(arr);',
  10268. ' i:=high(arr);',
  10269. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10270. ConvertProgram;
  10271. CheckSource('TestArrayOfRecord',
  10272. LinesToStr([ // statements
  10273. 'rtl.recNewT(this, "TRec", function () {',
  10274. ' this.Int = 0;',
  10275. ' this.$eq = function (b) {',
  10276. ' return this.Int === b.Int;',
  10277. ' };',
  10278. ' this.$assign = function (s) {',
  10279. ' this.Int = s.Int;',
  10280. ' return this;',
  10281. ' };',
  10282. '});',
  10283. 'this.DoIt = function (vd, vc, vv) {',
  10284. '};',
  10285. 'this.Arr = [];',
  10286. 'this.r = this.TRec.$new();',
  10287. 'this.i = 0;'
  10288. ]),
  10289. LinesToStr([ // $mod.$main
  10290. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10291. '$mod.Arr[0].Int = 4;',
  10292. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10293. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10294. '$mod.Arr[7].$assign($mod.r);',
  10295. '$mod.r.$assign($mod.Arr[8]);',
  10296. '$mod.i = 0;',
  10297. '$mod.i = rtl.length($mod.Arr)-1;',
  10298. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10299. '']));
  10300. end;
  10301. procedure TTestModule.TestArray_StaticRecord;
  10302. begin
  10303. StartProgram(false);
  10304. Add([
  10305. 'type',
  10306. ' TRec = record',
  10307. ' Int: longint;',
  10308. ' end;',
  10309. ' TArrayRec = array[1..2] of TRec;',
  10310. 'var',
  10311. ' Arr: TArrayRec;',
  10312. 'begin',
  10313. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10314. '']);
  10315. ConvertProgram;
  10316. CheckSource('TestArray_StaticRecord',
  10317. LinesToStr([ // statements
  10318. 'rtl.recNewT(this, "TRec", function () {',
  10319. ' this.Int = 0;',
  10320. ' this.$eq = function (b) {',
  10321. ' return this.Int === b.Int;',
  10322. ' };',
  10323. ' this.$assign = function (s) {',
  10324. ' this.Int = s.Int;',
  10325. ' return this;',
  10326. ' };',
  10327. '});',
  10328. 'this.TArrayRec$clone = function (a) {',
  10329. ' var b = [];',
  10330. ' b.length = 2;',
  10331. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10332. ' return b;',
  10333. '};',
  10334. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10335. '']),
  10336. LinesToStr([ // $mod.$main
  10337. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10338. end;
  10339. procedure TTestModule.TestArrayOfSet;
  10340. begin
  10341. StartProgram(false);
  10342. Add([
  10343. 'type',
  10344. ' TFlag = (big,small);',
  10345. ' TSetOfFlag = set of tflag;',
  10346. ' TArrayFlag = array of TSetOfFlag;',
  10347. 'procedure DoIt(const a: Tarrayflag);',
  10348. 'begin',
  10349. 'end;',
  10350. 'var',
  10351. ' f: TFlag;',
  10352. ' s: TSetOfFlag;',
  10353. ' Arr: TArrayFlag;',
  10354. ' i: longint;',
  10355. 'begin',
  10356. ' SetLength(arr,3);',
  10357. ' arr[0]:=s;',
  10358. ' arr[1]:=[big];',
  10359. ' arr[2]:=[big]+s;',
  10360. ' arr[3]:=s+[big];',
  10361. ' arr[4]:=arr[5];',
  10362. ' s:=arr[6];',
  10363. ' i:=low(arr);',
  10364. ' i:=high(arr);',
  10365. ' DoIt(arr);',
  10366. ' DoIt([s]);',
  10367. ' DoIt([[],s]);',
  10368. ' DoIt([s,[]]);',
  10369. '']);
  10370. ConvertProgram;
  10371. CheckSource('TestArrayOfSet',
  10372. LinesToStr([ // statements
  10373. 'this.TFlag = {',
  10374. ' "0": "big",',
  10375. ' big: 0,',
  10376. ' "1": "small",',
  10377. ' small: 1',
  10378. '};',
  10379. 'this.DoIt = function (a) {',
  10380. '};',
  10381. 'this.f = 0;',
  10382. 'this.s = {};',
  10383. 'this.Arr = [];',
  10384. 'this.i = 0;',
  10385. '']),
  10386. LinesToStr([ // $mod.$main
  10387. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10388. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10389. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10390. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10391. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10392. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10393. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10394. '$mod.i = 0;',
  10395. '$mod.i = rtl.length($mod.Arr) - 1;',
  10396. '$mod.DoIt($mod.Arr);',
  10397. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10398. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10399. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10400. '']));
  10401. end;
  10402. procedure TTestModule.TestArray_DynAsParam;
  10403. begin
  10404. StartProgram(false);
  10405. Add([
  10406. 'type integer = longint;',
  10407. 'type TArrInt = array of integer;',
  10408. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10409. 'var vJ: TArrInt;',
  10410. 'begin',
  10411. ' vg:=vg;',
  10412. ' vj:=vh;',
  10413. ' vi:=vi;',
  10414. ' doit(vg,vg,vg);',
  10415. ' doit(vh,vh,vj);',
  10416. ' doit(vi,vi,vi);',
  10417. ' doit(vj,vj,vj);',
  10418. 'end;',
  10419. 'var i: TArrInt;',
  10420. 'begin',
  10421. ' doit(i,i,i);']);
  10422. ConvertProgram;
  10423. CheckSource('TestArray_DynAsParams',
  10424. LinesToStr([ // statements
  10425. 'this.DoIt = function (vG,vH,vI) {',
  10426. ' var vJ = [];',
  10427. ' vG = rtl.arrayRef(vG);',
  10428. ' vJ = rtl.arrayRef(vH);',
  10429. ' vI.set(rtl.arrayRef(vI.get()));',
  10430. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10431. ' get: function () {',
  10432. ' return vG;',
  10433. ' },',
  10434. ' set: function (v) {',
  10435. ' vG = v;',
  10436. ' }',
  10437. ' });',
  10438. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10439. ' get: function () {',
  10440. ' return vJ;',
  10441. ' },',
  10442. ' set: function (v) {',
  10443. ' vJ = v;',
  10444. ' }',
  10445. ' });',
  10446. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10447. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10448. ' get: function () {',
  10449. ' return vJ;',
  10450. ' },',
  10451. ' set: function (v) {',
  10452. ' vJ = v;',
  10453. ' }',
  10454. ' });',
  10455. '};',
  10456. 'this.i = [];'
  10457. ]),
  10458. LinesToStr([
  10459. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10460. ' p: $mod,',
  10461. ' get: function () {',
  10462. ' return this.p.i;',
  10463. ' },',
  10464. ' set: function (v) {',
  10465. ' this.p.i = v;',
  10466. ' }',
  10467. '});'
  10468. ]));
  10469. end;
  10470. procedure TTestModule.TestArray_StaticAsParam;
  10471. begin
  10472. StartProgram(false);
  10473. Add([
  10474. 'type integer = longint;',
  10475. 'type TArrInt = array[1..2] of integer;',
  10476. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10477. 'var vJ: TArrInt;',
  10478. 'begin',
  10479. ' vg:=vg;',
  10480. ' vj:=vh;',
  10481. ' vi:=vi;',
  10482. ' doit(vg,vg,vg);',
  10483. ' doit(vh,vh,vj);',
  10484. ' doit(vi,vi,vi);',
  10485. ' doit(vj,vj,vj);',
  10486. 'end;',
  10487. 'var i: TArrInt;',
  10488. 'begin',
  10489. ' doit(i,i,i);']);
  10490. ConvertProgram;
  10491. CheckSource('TestArray_StaticAsParams',
  10492. LinesToStr([ // statements
  10493. 'this.DoIt = function (vG,vH,vI) {',
  10494. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10495. ' vG = vG.slice(0);',
  10496. ' vJ = vH.slice(0);',
  10497. ' vI.set(vI.get().slice(0));',
  10498. ' $mod.DoIt(vG.slice(0), vG, {',
  10499. ' get: function () {',
  10500. ' return vG;',
  10501. ' },',
  10502. ' set: function (v) {',
  10503. ' vG = v;',
  10504. ' }',
  10505. ' });',
  10506. ' $mod.DoIt(vH.slice(0), vH, {',
  10507. ' get: function () {',
  10508. ' return vJ;',
  10509. ' },',
  10510. ' set: function (v) {',
  10511. ' vJ = v;',
  10512. ' }',
  10513. ' });',
  10514. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10515. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10516. ' get: function () {',
  10517. ' return vJ;',
  10518. ' },',
  10519. ' set: function (v) {',
  10520. ' vJ = v;',
  10521. ' }',
  10522. ' });',
  10523. '};',
  10524. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10525. ]),
  10526. LinesToStr([
  10527. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10528. ' p: $mod,',
  10529. ' get: function () {',
  10530. ' return this.p.i;',
  10531. ' },',
  10532. ' set: function (v) {',
  10533. ' this.p.i = v;',
  10534. ' }',
  10535. '});'
  10536. ]));
  10537. end;
  10538. procedure TTestModule.TestArrayElement_AsParams;
  10539. begin
  10540. StartProgram(false);
  10541. Add('type integer = longint;');
  10542. Add('type TArrayInt = array of integer;');
  10543. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10544. Add('var vJ: tarrayint;');
  10545. Add('begin');
  10546. Add(' vi:=vi;');
  10547. Add(' doit(vi,vi,vi);');
  10548. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10549. Add('end;');
  10550. Add('var a: TArrayInt;');
  10551. Add('begin');
  10552. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10553. ConvertProgram;
  10554. CheckSource('TestArrayElement_AsParams',
  10555. LinesToStr([ // statements
  10556. 'this.DoIt = function (vG,vH,vI) {',
  10557. ' var vJ = [];',
  10558. ' vI.set(vI.get());',
  10559. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10560. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10561. ' a:1+3,',
  10562. ' p:vJ,',
  10563. ' get: function () {',
  10564. ' return this.p[this.a];',
  10565. ' },',
  10566. ' set: function (v) {',
  10567. ' this.p[this.a] = v;',
  10568. ' }',
  10569. ' });',
  10570. '};',
  10571. 'this.a = [];'
  10572. ]),
  10573. LinesToStr([
  10574. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10575. ' a: 1+6,',
  10576. ' p: $mod.a,',
  10577. ' get: function () {',
  10578. ' return this.p[this.a];',
  10579. ' },',
  10580. ' set: function (v) {',
  10581. ' this.p[this.a] = v;',
  10582. ' }',
  10583. '});'
  10584. ]));
  10585. end;
  10586. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10587. begin
  10588. StartProgram(false);
  10589. Add('type Integer = longint;');
  10590. Add('type TArrayInt = array of integer;');
  10591. Add('function GetArr(vB: integer = 0): tarrayint;');
  10592. Add('begin');
  10593. Add('end;');
  10594. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10595. Add('begin');
  10596. Add('end;');
  10597. Add('begin');
  10598. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10599. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10600. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10601. ConvertProgram;
  10602. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10603. LinesToStr([ // statements
  10604. 'this.GetArr = function (vB) {',
  10605. ' var Result = [];',
  10606. ' return Result;',
  10607. '};',
  10608. 'this.DoIt = function (vG,vH,vI) {',
  10609. '};'
  10610. ]),
  10611. LinesToStr([
  10612. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10613. ' a: 1+3,',
  10614. ' p: $mod.GetArr(0),',
  10615. ' get: function () {',
  10616. ' return this.p[this.a];',
  10617. ' },',
  10618. ' set: function (v) {',
  10619. ' this.p[this.a] = v;',
  10620. ' }',
  10621. '});',
  10622. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10623. ' a: 2+3,',
  10624. ' p: $mod.GetArr(0),',
  10625. ' get: function () {',
  10626. ' return this.p[this.a];',
  10627. ' },',
  10628. ' set: function (v) {',
  10629. ' this.p[this.a] = v;',
  10630. ' }',
  10631. '});',
  10632. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10633. ' a: 3+3,',
  10634. ' p: $mod.GetArr(9),',
  10635. ' get: function () {',
  10636. ' return this.p[this.a];',
  10637. ' },',
  10638. ' set: function (v) {',
  10639. ' this.p[this.a] = v;',
  10640. ' }',
  10641. '});',
  10642. '']));
  10643. end;
  10644. procedure TTestModule.TestArrayEnumTypeRange;
  10645. begin
  10646. StartProgram(false);
  10647. Add([
  10648. 'type',
  10649. ' TEnum = (red,blue);',
  10650. ' TEnumArray = array[TEnum] of longint;',
  10651. 'var',
  10652. ' e: TEnum;',
  10653. ' i: longint;',
  10654. ' a: TEnumArray;',
  10655. ' numbers: TEnumArray = (1,2);',
  10656. ' names: array[TEnum] of string = (''red'',''blue'');',
  10657. 'begin',
  10658. ' e:=low(a);',
  10659. ' e:=high(a);',
  10660. ' i:=a[red];',
  10661. ' a[e]:=a[e];']);
  10662. ConvertProgram;
  10663. CheckSource('TestArrayEnumTypeRange',
  10664. LinesToStr([ // statements
  10665. ' this.TEnum = {',
  10666. ' "0": "red",',
  10667. ' red: 0,',
  10668. ' "1": "blue",',
  10669. ' blue: 1',
  10670. '};',
  10671. 'this.e = 0;',
  10672. 'this.i = 0;',
  10673. 'this.a = rtl.arraySetLength(null,0,2);',
  10674. 'this.numbers = [1, 2];',
  10675. 'this.names = ["red", "blue"];',
  10676. '']),
  10677. LinesToStr([ // $mod.$main
  10678. '$mod.e = $mod.TEnum.red;',
  10679. '$mod.e = $mod.TEnum.blue;',
  10680. '$mod.i = $mod.a[$mod.TEnum.red];',
  10681. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10682. '']));
  10683. end;
  10684. procedure TTestModule.TestArray_SetLengthOutArg;
  10685. begin
  10686. StartProgram(false);
  10687. Add([
  10688. 'type TArrInt = array of longint;',
  10689. 'procedure DoIt(out a: TArrInt);',
  10690. 'begin',
  10691. ' SetLength(a,2);',
  10692. 'end;',
  10693. 'begin',
  10694. '']);
  10695. ConvertProgram;
  10696. CheckSource('TestArray_SetLengthOutArg',
  10697. LinesToStr([ // statements
  10698. 'this.DoIt = function (a) {',
  10699. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10700. '};',
  10701. '']),
  10702. LinesToStr([
  10703. '']));
  10704. end;
  10705. procedure TTestModule.TestArray_SetLengthProperty;
  10706. begin
  10707. StartProgram(false);
  10708. Add('type');
  10709. Add(' TArrInt = array of longint;');
  10710. Add(' TObject = class');
  10711. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10712. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10713. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10714. Add(' end;');
  10715. Add('var Obj: TObject;');
  10716. Add('begin');
  10717. Add(' SetLength(Obj.Colors,2);');
  10718. ConvertProgram;
  10719. CheckSource('TestArray_SetLengthProperty',
  10720. LinesToStr([ // statements
  10721. 'rtl.createClass(this, "TObject", null, function () {',
  10722. ' this.$init = function () {',
  10723. ' };',
  10724. ' this.$final = function () {',
  10725. ' };',
  10726. '});',
  10727. 'this.Obj = null;',
  10728. '']),
  10729. LinesToStr([
  10730. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10731. '']));
  10732. end;
  10733. procedure TTestModule.TestArray_SetLengthMultiDim;
  10734. begin
  10735. StartProgram(false);
  10736. Add([
  10737. 'type',
  10738. ' TArrArrInt = array of array of longint;',
  10739. ' TArrStaInt = array of array[1..2] of longint;',
  10740. 'var',
  10741. ' a: TArrArrInt;',
  10742. ' b: TArrStaInt;',
  10743. 'begin',
  10744. ' SetLength(a,2);',
  10745. ' SetLength(a,3,4);',
  10746. ' SetLength(b,5);',
  10747. '']);
  10748. ConvertProgram;
  10749. CheckSource('TestArray_SetLengthMultiDim',
  10750. LinesToStr([ // statements
  10751. 'this.a = [];',
  10752. 'this.b = [];',
  10753. '']),
  10754. LinesToStr([
  10755. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10756. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10757. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10758. '']));
  10759. end;
  10760. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10761. begin
  10762. StartProgram(false);
  10763. Add([
  10764. 'type',
  10765. ' TStaArr1 = array[1..3] of boolean;',
  10766. //' TStaArr2 = array[5..6] of TStaArr1;',
  10767. ' TDynArr1StaArr1 = array of TStaArr1;',
  10768. //' TDynArr1StaArr2 = array of TStaArr2;',
  10769. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10770. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10771. 'var',
  10772. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10773. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10774. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10775. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10776. 'begin',
  10777. ' SetLength(DynArr1StaArr1,11);',
  10778. ' SetLength(DynArr2StaArr1,12);',
  10779. ' SetLength(DynArr2StaArr1[13],14);',
  10780. ' SetLength(DynArr2StaArr1,15,16);',
  10781. //' SetLength(DynArr1StaArr2,21);',
  10782. //' SetLength(DynArr2StaArr2,22);',
  10783. //' SetLength(DynArr2StaArr2[23],24);',
  10784. //' SetLength(DynArr2StaArr2,25,26);',
  10785. '']);
  10786. ConvertProgram;
  10787. CheckSource('TestArray_DynOfStatic',
  10788. LinesToStr([ // statements
  10789. 'this.DynArr1StaArr1 = [];',
  10790. 'this.DynArr2StaArr1 = [];',
  10791. '']),
  10792. LinesToStr([ // $mod.$main
  10793. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10794. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10795. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10796. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10797. ' $mod.DynArr2StaArr1,',
  10798. ' false,',
  10799. ' 15,',
  10800. ' 16,',
  10801. ' "s",',
  10802. ' 3',
  10803. ');',
  10804. '']));
  10805. end;
  10806. procedure TTestModule.TestArray_OpenArrayOfString;
  10807. begin
  10808. StartProgram(false);
  10809. Add('procedure DoIt(const a: array of String);');
  10810. Add('var');
  10811. Add(' i: longint;');
  10812. Add(' s: string;');
  10813. Add('begin');
  10814. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10815. Add('end;');
  10816. Add('var s: string;');
  10817. Add('begin');
  10818. Add(' DoIt([]);');
  10819. Add(' DoIt([s,''foo'','''',s+s]);');
  10820. ConvertProgram;
  10821. CheckSource('TestArray_OpenArrayOfString',
  10822. LinesToStr([ // statements
  10823. 'this.DoIt = function (a) {',
  10824. ' var i = 0;',
  10825. ' var s = "";',
  10826. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10827. ' i = $l;',
  10828. ' s = a[rtl.length(a) - i - 1];',
  10829. ' };',
  10830. '};',
  10831. 'this.s = "";',
  10832. '']),
  10833. LinesToStr([
  10834. '$mod.DoIt([]);',
  10835. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10836. '']));
  10837. end;
  10838. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10839. begin
  10840. StartProgram(false);
  10841. Add([
  10842. 'type TArr = array of char;',
  10843. 'var',
  10844. ' c: char;',
  10845. ' s: string;',
  10846. ' a: TArr;',
  10847. 'procedure Run(const a: array of char);',
  10848. 'begin',
  10849. ' Run(c);',
  10850. ' Run(s);',
  10851. 'end;',
  10852. 'begin',
  10853. ' a:=c;',
  10854. ' a:=s;',
  10855. ' a:=#13;',
  10856. ' a:=''Foo'';',
  10857. ' Run(c);',
  10858. ' Run(s);',
  10859. '']);
  10860. ConvertProgram;
  10861. CheckSource('TestArray_ArrayOfCharAssignString',
  10862. LinesToStr([ // statements
  10863. 'this.c = "";',
  10864. 'this.s = "";',
  10865. 'this.a = [];',
  10866. 'this.Run = function (a) {',
  10867. ' $mod.Run($mod.c.split(""));',
  10868. ' $mod.Run($mod.s.split(""));',
  10869. '};',
  10870. '']),
  10871. LinesToStr([
  10872. '$mod.a = $mod.c.split("");',
  10873. '$mod.a = $mod.s.split("");',
  10874. '$mod.a = "\r".split("");',
  10875. '$mod.a = "Foo".split("");',
  10876. '$mod.Run($mod.c.split(""));',
  10877. '$mod.Run($mod.s.split(""));',
  10878. '']));
  10879. end;
  10880. procedure TTestModule.TestArray_ConstRef;
  10881. begin
  10882. StartProgram(false);
  10883. Add([
  10884. 'type TArr = array of word;',
  10885. 'procedure Run(constref a: TArr);',
  10886. 'begin',
  10887. 'end;',
  10888. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10889. 'var l: TArr;',
  10890. 'begin',
  10891. ' Run(l);',
  10892. ' Run(a);',
  10893. ' Run(b);',
  10894. ' Run(c);',
  10895. ' Run(d);',
  10896. ' Run(e);',
  10897. 'end;',
  10898. 'begin',
  10899. '']);
  10900. ConvertProgram;
  10901. CheckResolverUnexpectedHints();
  10902. CheckSource('TestArray_ConstRef',
  10903. LinesToStr([ // statements
  10904. 'this.Run = function (a) {',
  10905. '};',
  10906. 'this.Fly = function (a, b, c, d, e) {',
  10907. ' var l = [];',
  10908. ' $mod.Run(l);',
  10909. ' $mod.Run(a);',
  10910. ' $mod.Run(b.get());',
  10911. ' $mod.Run(c.get());',
  10912. ' $mod.Run(d);',
  10913. ' $mod.Run(e);',
  10914. '};',
  10915. '']),
  10916. LinesToStr([
  10917. '']));
  10918. end;
  10919. procedure TTestModule.TestArray_Concat;
  10920. begin
  10921. StartProgram(false);
  10922. Add([
  10923. 'type',
  10924. ' integer = longint;',
  10925. ' TFlag = (big,small);',
  10926. ' TFlags = set of TFlag;',
  10927. ' TRec = record',
  10928. ' i: integer;',
  10929. ' end;',
  10930. ' TArrInt = array of integer;',
  10931. ' TArrRec = array of TRec;',
  10932. ' TArrFlag = array of TFlag;',
  10933. ' TArrSet = array of TFlags;',
  10934. ' TArrJSValue = array of jsvalue;',
  10935. 'var',
  10936. ' ArrInt1, ArrInt2: tarrint;',
  10937. ' ArrRec1, ArrRec2: tarrrec;',
  10938. ' ArrFlag1, ArrFlag2: tarrflag;',
  10939. ' ArrSet1, ArrSet2: tarrset;',
  10940. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10941. 'begin',
  10942. ' arrint1:=concat(arrint2);',
  10943. ' arrint1:=concat(arrint2,arrint2);',
  10944. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10945. ' arrrec1:=concat(arrrec2);',
  10946. ' arrrec1:=concat(arrrec2,arrrec2);',
  10947. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10948. ' arrset1:=concat(arrset2);',
  10949. ' arrset1:=concat(arrset2,arrset2);',
  10950. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10951. ' arrjsvalue1:=concat(arrjsvalue2);',
  10952. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10953. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10954. ' arrint1:=concat([1],arrint2);',
  10955. ' arrflag1:=concat([big]);',
  10956. ' arrflag1:=concat([big],arrflag2);',
  10957. ' arrflag1:=concat(arrflag2,[small]);',
  10958. '']);
  10959. ConvertProgram;
  10960. CheckSource('TestArray_Concat',
  10961. LinesToStr([ // statements
  10962. 'this.TFlag = {',
  10963. ' "0": "big",',
  10964. ' big: 0,',
  10965. ' "1": "small",',
  10966. ' small: 1',
  10967. '};',
  10968. 'rtl.recNewT(this, "TRec", function () {',
  10969. ' this.i = 0;',
  10970. ' this.$eq = function (b) {',
  10971. ' return this.i === b.i;',
  10972. ' };',
  10973. ' this.$assign = function (s) {',
  10974. ' this.i = s.i;',
  10975. ' return this;',
  10976. ' };',
  10977. '});',
  10978. 'this.ArrInt1 = [];',
  10979. 'this.ArrInt2 = [];',
  10980. 'this.ArrRec1 = [];',
  10981. 'this.ArrRec2 = [];',
  10982. 'this.ArrFlag1 = [];',
  10983. 'this.ArrFlag2 = [];',
  10984. 'this.ArrSet1 = [];',
  10985. 'this.ArrSet2 = [];',
  10986. 'this.ArrJSValue1 = [];',
  10987. 'this.ArrJSValue2 = [];',
  10988. '']),
  10989. LinesToStr([ // $mod.$main
  10990. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10991. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10992. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10993. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10994. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10995. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10996. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10997. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  10998. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  10999. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11000. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11001. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11002. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11003. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11004. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11005. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11006. '']));
  11007. end;
  11008. procedure TTestModule.TestArray_Concat_Append;
  11009. begin
  11010. StartProgram(false);
  11011. Add([
  11012. 'type',
  11013. ' integer = longint;',
  11014. ' TFlag = (big,small);',
  11015. ' TFlags = set of TFlag;',
  11016. ' TRec = record',
  11017. ' i: integer;',
  11018. ' end;',
  11019. ' TArrInt = array of integer;',
  11020. ' TArrRec = array of TRec;',
  11021. ' TArrFlag = array of TFlag;',
  11022. ' TArrSet = array of TFlags;',
  11023. ' TArrJSValue = array of jsvalue;',
  11024. 'var',
  11025. ' ArrInt: tarrint;',
  11026. ' ArrRec: tarrrec;',
  11027. ' ArrFlag: tarrflag;',
  11028. ' ArrSet: tarrset;',
  11029. ' ArrJSValue: tarrjsvalue;',
  11030. ' r: TRec;',
  11031. ' f: TFlags;',
  11032. 'begin',
  11033. ' // append',
  11034. ' arrint:=concat(arrint);',
  11035. ' arrint:=concat(arrint,[2]);',
  11036. ' arrint:=concat(arrint,[3,4]);',
  11037. ' arrrec:=concat(arrrec);',
  11038. ' arrrec:=concat(arrrec,[r]);',
  11039. ' arrrec:=concat(arrrec,[r,r]);',
  11040. ' arrset:=concat(arrset);',
  11041. ' arrset:=concat(arrset,[f]);',
  11042. ' arrset:=concat(arrset,[f,f]);',
  11043. ' arrjsvalue:=concat(arrjsvalue);',
  11044. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11045. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11046. ' arrflag:=concat(arrflag);',
  11047. ' arrflag:=concat(arrflag,[small]);',
  11048. ' arrflag:=concat(arrflag,[small,big]);',
  11049. '']);
  11050. ConvertProgram;
  11051. CheckSource('TestArray_Concat_Append',
  11052. LinesToStr([ // statements
  11053. 'this.TFlag = {',
  11054. ' "0": "big",',
  11055. ' big: 0,',
  11056. ' "1": "small",',
  11057. ' small: 1',
  11058. '};',
  11059. 'rtl.recNewT(this, "TRec", function () {',
  11060. ' this.i = 0;',
  11061. ' this.$eq = function (b) {',
  11062. ' return this.i === b.i;',
  11063. ' };',
  11064. ' this.$assign = function (s) {',
  11065. ' this.i = s.i;',
  11066. ' return this;',
  11067. ' };',
  11068. '});',
  11069. 'this.ArrInt = [];',
  11070. 'this.ArrRec = [];',
  11071. 'this.ArrFlag = [];',
  11072. 'this.ArrSet = [];',
  11073. 'this.ArrJSValue = [];',
  11074. 'this.r = this.TRec.$new();',
  11075. 'this.f = {};',
  11076. '']),
  11077. LinesToStr([ // $mod.$main
  11078. '$mod.ArrInt = $mod.ArrInt;',
  11079. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11080. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11081. '$mod.ArrRec = $mod.ArrRec;',
  11082. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11083. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11084. '$mod.ArrSet = $mod.ArrSet;',
  11085. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11086. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11087. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11088. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11089. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11090. '$mod.ArrFlag = $mod.ArrFlag;',
  11091. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11092. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11093. '']));
  11094. end;
  11095. procedure TTestModule.TestArray_Concat_Append_Var;
  11096. begin
  11097. StartProgram(false);
  11098. Add([
  11099. 'type',
  11100. ' TArrInt = array of word;',
  11101. '',
  11102. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11103. 'begin',
  11104. ' a:=concat(a,[2]);',
  11105. ' b:=concat(b,[2]);',
  11106. 'end;',
  11107. 'var',
  11108. ' ArrInt: tarrint;',
  11109. 'begin',
  11110. ' Fly(ArrInt,ArrInt);',
  11111. '']);
  11112. ConvertProgram;
  11113. CheckSource('TestArray_Concat_Append_Var',
  11114. LinesToStr([ // statements
  11115. 'this.Fly = function (a, b) {',
  11116. ' a = rtl.arrayPushN(a, 2);',
  11117. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11118. '};',
  11119. 'this.ArrInt = [];',
  11120. '']),
  11121. LinesToStr([ // $mod.$main
  11122. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11123. ' p: $mod,',
  11124. ' get: function () {',
  11125. ' return this.p.ArrInt;',
  11126. ' },',
  11127. ' set: function (v) {',
  11128. ' this.p.ArrInt = v;',
  11129. ' }',
  11130. '});',
  11131. '']));
  11132. end;
  11133. procedure TTestModule.TestArray_Copy;
  11134. begin
  11135. StartProgram(false);
  11136. Add([
  11137. 'type',
  11138. ' integer = longint;',
  11139. ' TFlag = (big,small);',
  11140. ' TFlags = set of TFlag;',
  11141. ' TRec = record',
  11142. ' i: integer;',
  11143. ' end;',
  11144. ' TArrInt = array of integer;',
  11145. ' TArrRec = array of TRec;',
  11146. ' TArrSet = array of TFlags;',
  11147. ' TArrJSValue = array of jsvalue;',
  11148. 'var',
  11149. ' ArrInt: tarrint;',
  11150. ' ArrRec: tarrrec;',
  11151. ' ArrSet: tarrset;',
  11152. ' ArrJSValue: tarrjsvalue;',
  11153. 'begin',
  11154. ' arrint:=copy(arrint);',
  11155. ' arrint:=copy(arrint,2);',
  11156. ' arrint:=copy(arrint,3,4);',
  11157. ' arrint:=copy([1,1],1,2);',
  11158. ' arrrec:=copy(arrrec);',
  11159. ' arrrec:=copy(arrrec,5);',
  11160. ' arrrec:=copy(arrrec,6,7);',
  11161. ' arrset:=copy(arrset);',
  11162. ' arrset:=copy(arrset,8);',
  11163. ' arrset:=copy(arrset,9,10);',
  11164. ' arrjsvalue:=copy(arrjsvalue);',
  11165. ' arrjsvalue:=copy(arrjsvalue,11);',
  11166. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11167. ' ']);
  11168. ConvertProgram;
  11169. CheckSource('TestArray_Copy',
  11170. LinesToStr([ // statements
  11171. 'this.TFlag = {',
  11172. ' "0": "big",',
  11173. ' big: 0,',
  11174. ' "1": "small",',
  11175. ' small: 1',
  11176. '};',
  11177. 'rtl.recNewT(this, "TRec", function () {',
  11178. ' this.i = 0;',
  11179. ' this.$eq = function (b) {',
  11180. ' return this.i === b.i;',
  11181. ' };',
  11182. ' this.$assign = function (s) {',
  11183. ' this.i = s.i;',
  11184. ' return this;',
  11185. ' };',
  11186. '});',
  11187. 'this.ArrInt = [];',
  11188. 'this.ArrRec = [];',
  11189. 'this.ArrSet = [];',
  11190. 'this.ArrJSValue = [];',
  11191. '']),
  11192. LinesToStr([ // $mod.$main
  11193. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11194. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11195. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11196. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11197. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11198. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11199. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11200. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11201. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11202. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11203. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11204. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11205. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11206. '']));
  11207. end;
  11208. procedure TTestModule.TestArray_InsertDelete;
  11209. begin
  11210. StartProgram(false);
  11211. Add([
  11212. 'type',
  11213. ' integer = longint;',
  11214. ' TFlag = (big,small);',
  11215. ' TFlags = set of TFlag;',
  11216. ' TRec = record',
  11217. ' i: integer;',
  11218. ' end;',
  11219. ' TArrInt = array of integer;',
  11220. ' TArrRec = array of TRec;',
  11221. ' TArrSet = array of TFlags;',
  11222. ' TArrJSValue = array of jsvalue;',
  11223. ' TArrArrInt = array of TArrInt;',
  11224. 'var',
  11225. ' ArrInt: tarrint;',
  11226. ' ArrRec: tarrrec;',
  11227. ' ArrSet: tarrset;',
  11228. ' ArrJSValue: tarrjsvalue;',
  11229. ' ArrArrInt: TArrArrInt;',
  11230. 'begin',
  11231. ' Insert(1,arrint,2);',
  11232. ' Insert(arrint[3],arrint,4);',
  11233. ' Insert(arrrec[5],arrrec,6);',
  11234. ' Insert(arrset[7],arrset,7);',
  11235. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11236. ' Insert(10,arrjsvalue,11);',
  11237. ' Insert([23],arrarrint,22);',
  11238. ' Delete(arrint,12,13);',
  11239. ' Delete(arrrec,14,15);',
  11240. ' Delete(arrset,17,18);',
  11241. ' Delete(arrjsvalue,19,10);']);
  11242. ConvertProgram;
  11243. CheckSource('TestArray_InsertDelete',
  11244. LinesToStr([ // statements
  11245. 'this.TFlag = {',
  11246. ' "0": "big",',
  11247. ' big: 0,',
  11248. ' "1": "small",',
  11249. ' small: 1',
  11250. '};',
  11251. 'rtl.recNewT(this, "TRec", function () {',
  11252. ' this.i = 0;',
  11253. ' this.$eq = function (b) {',
  11254. ' return this.i === b.i;',
  11255. ' };',
  11256. ' this.$assign = function (s) {',
  11257. ' this.i = s.i;',
  11258. ' return this;',
  11259. ' };',
  11260. '});',
  11261. 'this.ArrInt = [];',
  11262. 'this.ArrRec = [];',
  11263. 'this.ArrSet = [];',
  11264. 'this.ArrJSValue = [];',
  11265. 'this.ArrArrInt = [];',
  11266. '']),
  11267. LinesToStr([ // $mod.$main
  11268. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11269. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11270. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11271. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11272. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11273. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11274. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11275. '$mod.ArrInt.splice(12, 13);',
  11276. '$mod.ArrRec.splice(14, 15);',
  11277. '$mod.ArrSet.splice(17, 18);',
  11278. '$mod.ArrJSValue.splice(19, 10);',
  11279. '']));
  11280. end;
  11281. procedure TTestModule.TestArray_Add_Append;
  11282. begin
  11283. StartProgram(false);
  11284. Add([
  11285. '{$modeswitch arrayoperators}',
  11286. 'type',
  11287. ' integer = longint;',
  11288. ' TFlag = (big,small);',
  11289. ' TFlags = set of TFlag;',
  11290. ' TRec = record',
  11291. ' i: integer;',
  11292. ' end;',
  11293. ' TArrInt = array of integer;',
  11294. ' TArrRec = array of TRec;',
  11295. ' TArrFlag = array of TFlag;',
  11296. ' TArrSet = array of TFlags;',
  11297. ' TArrJSValue = array of jsvalue;',
  11298. 'var',
  11299. ' ArrInt: tarrint;',
  11300. ' ArrRec: tarrrec;',
  11301. ' ArrFlag: tarrflag;',
  11302. ' ArrSet: tarrset;',
  11303. ' ArrJSValue: tarrjsvalue;',
  11304. ' r: TRec;',
  11305. ' f: TFlags;',
  11306. 'begin',
  11307. ' // append',
  11308. ' arrint:=arrint+[2];',
  11309. ' arrint:=arrint+[3,4];',
  11310. ' arrrec:=arrrec+[r];',
  11311. ' arrrec:=arrrec+[r,r];',
  11312. ' arrset:=arrset+[f];',
  11313. ' arrset:=arrset+[f,f];',
  11314. ' arrjsvalue:=arrjsvalue+[11];',
  11315. ' arrjsvalue:=arrjsvalue+[12,13];',
  11316. ' arrflag:=arrflag+[small];',
  11317. ' arrflag:=arrflag+[small,big];',
  11318. '']);
  11319. ConvertProgram;
  11320. CheckSource('TestArray_Add_Append',
  11321. LinesToStr([ // statements
  11322. 'this.TFlag = {',
  11323. ' "0": "big",',
  11324. ' big: 0,',
  11325. ' "1": "small",',
  11326. ' small: 1',
  11327. '};',
  11328. 'rtl.recNewT(this, "TRec", function () {',
  11329. ' this.i = 0;',
  11330. ' this.$eq = function (b) {',
  11331. ' return this.i === b.i;',
  11332. ' };',
  11333. ' this.$assign = function (s) {',
  11334. ' this.i = s.i;',
  11335. ' return this;',
  11336. ' };',
  11337. '});',
  11338. 'this.ArrInt = [];',
  11339. 'this.ArrRec = [];',
  11340. 'this.ArrFlag = [];',
  11341. 'this.ArrSet = [];',
  11342. 'this.ArrJSValue = [];',
  11343. 'this.r = this.TRec.$new();',
  11344. 'this.f = {};',
  11345. '']),
  11346. LinesToStr([ // $mod.$main
  11347. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11348. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11349. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11350. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11351. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11352. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11353. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11354. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11355. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11356. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11357. '']));
  11358. end;
  11359. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11360. begin
  11361. Parser.Options:=Parser.Options+[po_cassignments];
  11362. StartProgram(false);
  11363. Add([
  11364. '{$modeswitch arrayoperators}',
  11365. 'type',
  11366. ' integer = longint;',
  11367. ' TArrInt = array of integer;',
  11368. ' TArrStr = array of string;',
  11369. 'const',
  11370. ' Ints: TArrInt = (1,2,3);',
  11371. ' Aliases: TarrStr = (''foo'',''b'');',
  11372. ' OneInt: TArrInt = (7);',
  11373. ' OneStr: array of integer = (7);',
  11374. ' Chars: array of char = ''aoc'';',
  11375. ' Names: array of string = (''a'',''foo'');',
  11376. ' NameCount = low(Names)+high(Names)+length(Names);',
  11377. 'var i: integer;',
  11378. 'begin',
  11379. ' Ints:=[];',
  11380. ' Ints:=[1,1];',
  11381. ' Ints:=[1]+[2];',
  11382. ' Ints:=[2];',
  11383. ' Ints:=[]+ints;',
  11384. ' Ints:=Ints+[];',
  11385. ' Ints:=Ints+OneInt;',
  11386. ' Ints:=Ints+[1,1];',
  11387. ' Ints:=[i,i]+Ints;',
  11388. ' Ints:=[1]+[i]+[3];',
  11389. '']);
  11390. ConvertProgram;
  11391. CheckSource('TestArray_DynArrayConstObjFPC',
  11392. LinesToStr([ // statements
  11393. 'this.Ints = [1, 2, 3];',
  11394. 'this.Aliases = ["foo", "b"];',
  11395. 'this.OneInt = [7];',
  11396. 'this.OneStr = [7];',
  11397. 'this.Chars = ["a", "o", "c"];',
  11398. 'this.Names = ["a", "foo"];',
  11399. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11400. 'this.i = 0;',
  11401. '']),
  11402. LinesToStr([ // $mod.$main
  11403. '$mod.Ints = [];',
  11404. '$mod.Ints = [1, 1];',
  11405. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11406. '$mod.Ints = [2];',
  11407. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11408. '$mod.Ints = $mod.Ints;',
  11409. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11410. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11411. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11412. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11413. '']));
  11414. end;
  11415. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11416. begin
  11417. StartProgram(false);
  11418. // Note: const c = [1,1]; defines a set!
  11419. Add([
  11420. '{$mode delphi}',
  11421. 'type',
  11422. ' integer = longint;',
  11423. ' TArrInt = array of integer;',
  11424. ' TArrStr = array of string;',
  11425. 'const',
  11426. ' Ints: TArrInt = [1,1,2];',
  11427. ' Aliases: TarrStr = [''foo'',''b''];',
  11428. ' OneInt: TArrInt = [7];',
  11429. ' OneStr: array of integer = [7]+[8];',
  11430. ' Chars: array of char = ''aoc'';',
  11431. ' Names: array of string = [''a'',''a''];',
  11432. ' NameCount = low(Names)+high(Names)+length(Names);',
  11433. 'begin',
  11434. '']);
  11435. ConvertProgram;
  11436. CheckSource('TestArray_DynArrayConstDelphi',
  11437. LinesToStr([ // statements
  11438. 'this.Ints = [1, 1, 2];',
  11439. 'this.Aliases = ["foo", "b"];',
  11440. 'this.OneInt = [7];',
  11441. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11442. 'this.Chars = ["a", "o", "c"];',
  11443. 'this.Names = ["a", "a"];',
  11444. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11445. '']),
  11446. LinesToStr([ // $mod.$main
  11447. '']));
  11448. end;
  11449. procedure TTestModule.TestArray_ArrayLitAsParam;
  11450. begin
  11451. StartProgram(false);
  11452. Add([
  11453. '{$modeswitch arrayoperators}',
  11454. 'type',
  11455. ' integer = longint;',
  11456. ' TArrInt = array of integer;',
  11457. ' TArrSet = array of (red,green,blue);',
  11458. 'procedure DoOpenInt(const a: array of integer); forward;',
  11459. 'procedure DoInt(const a: TArrInt);',
  11460. 'begin',
  11461. ' DoInt(a+[1]);',
  11462. ' DoInt([1]+a);',
  11463. ' DoOpenInt(a);',
  11464. ' DoOpenInt(a+[1]);',
  11465. ' DoOpenInt([1]+a);',
  11466. 'end;',
  11467. 'procedure DoOpenInt(const a: array of integer);',
  11468. 'begin',
  11469. ' DoOpenInt(a+[1]);',
  11470. ' DoOpenInt([1]+a);',
  11471. ' DoInt(a);',
  11472. ' DoInt(a+[1]);',
  11473. ' DoInt([1]+a);',
  11474. 'end;',
  11475. 'procedure DoSet(const a: TArrSet);',
  11476. 'begin',
  11477. ' DoSet(a+[red]);',
  11478. ' DoSet([blue]+a);',
  11479. 'end;',
  11480. 'var',
  11481. ' i: TArrInt;',
  11482. ' s: TArrSet;',
  11483. 'begin',
  11484. ' DoInt([1]);',
  11485. ' DoInt([1]+[2]);',
  11486. ' DoInt(i+[1]);',
  11487. ' DoInt([1]+i);',
  11488. ' DoOpenInt([1]);',
  11489. ' DoOpenInt([1]+[2]);',
  11490. ' DoOpenInt(i+[1]);',
  11491. ' DoOpenInt([1]+i);',
  11492. ' DoSet([red]);',
  11493. ' DoSet([blue]+[green]);',
  11494. ' DoSet(s+[blue]);',
  11495. ' DoSet([red]+s);',
  11496. '']);
  11497. ConvertProgram;
  11498. CheckSource('TestArray_ArrayLitAsParam',
  11499. LinesToStr([ // statements
  11500. 'this.TArrSet$a = {',
  11501. ' "0": "red",',
  11502. ' red: 0,',
  11503. ' "1": "green",',
  11504. ' green: 1,',
  11505. ' "2": "blue",',
  11506. ' blue: 2',
  11507. '};',
  11508. 'this.DoInt = function (a) {',
  11509. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11510. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11511. ' $mod.DoOpenInt(a);',
  11512. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11513. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11514. '};',
  11515. 'this.DoOpenInt = function (a) {',
  11516. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11517. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11518. ' $mod.DoInt(a);',
  11519. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11520. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11521. '};',
  11522. 'this.DoSet = function (a) {',
  11523. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11524. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11525. '};',
  11526. 'this.i = [];',
  11527. 'this.s = [];',
  11528. '']),
  11529. LinesToStr([ // $mod.$main
  11530. '$mod.DoInt([1]);',
  11531. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11532. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11533. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11534. '$mod.DoOpenInt([1]);',
  11535. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11536. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11537. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11538. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11539. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11540. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11541. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11542. '']));
  11543. end;
  11544. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11545. begin
  11546. StartProgram(false);
  11547. Add([
  11548. '{$modeswitch arrayoperators}',
  11549. 'type',
  11550. ' integer = longint;',
  11551. ' TArrInt = array of integer;',
  11552. ' TArrArrInt = array of TArrInt;',
  11553. 'procedure DoInt(const a: TArrArrInt);',
  11554. 'begin',
  11555. ' DoInt(a+[[1]]);',
  11556. ' DoInt([[1]]+a);',
  11557. ' DoInt(a);',
  11558. 'end;',
  11559. 'var',
  11560. ' i: TArrInt;',
  11561. ' a: TArrArrInt;',
  11562. 'begin',
  11563. ' a:=[[1]];',
  11564. ' a:=[i];',
  11565. ' a:=a+[i];',
  11566. ' a:=[i]+a;',
  11567. ' a:=[[1]+i];',
  11568. ' a:=[[1]+[2]];',
  11569. ' a:=[i+[2]];',
  11570. ' DoInt([[1]]);',
  11571. ' DoInt([[1]+[2],[3,4],[5]]);',
  11572. ' DoInt([i+[1]]+a);',
  11573. ' DoInt([i]+a);',
  11574. '']);
  11575. ConvertProgram;
  11576. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11577. LinesToStr([ // statements
  11578. 'this.DoInt = function (a) {',
  11579. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11580. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11581. ' $mod.DoInt(a);',
  11582. '};',
  11583. 'this.i = [];',
  11584. 'this.a = [];',
  11585. '']),
  11586. LinesToStr([ // $mod.$main
  11587. '$mod.a = [[1]];',
  11588. '$mod.a = [$mod.i];',
  11589. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11590. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11591. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11592. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11593. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11594. '$mod.DoInt([[1]]);',
  11595. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11596. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11597. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11598. '']));
  11599. end;
  11600. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11601. begin
  11602. StartProgram(false);
  11603. Add([
  11604. '{$modeswitch arrayoperators}',
  11605. 'type',
  11606. ' integer = longint;',
  11607. ' TArrInt = array[1..2] of integer;',
  11608. ' TArrArrInt = array of TArrInt;',
  11609. 'procedure DoInt(const a: TArrArrInt);',
  11610. 'begin',
  11611. ' DoInt(a+[[1,2]]);',
  11612. ' DoInt([[1,2]]+a);',
  11613. ' DoInt(a);',
  11614. 'end;',
  11615. 'var',
  11616. ' i: TArrInt;',
  11617. ' a: TArrArrInt;',
  11618. 'begin',
  11619. ' a:=[[1,1]];',
  11620. ' a:=[i];',
  11621. ' a:=a+[i];',
  11622. ' a:=[i]+a;',
  11623. ' DoInt([[1,1]]);',
  11624. ' DoInt([[1,2],[3,4]]);',
  11625. '']);
  11626. ConvertProgram;
  11627. CheckSource('TestArray_ArrayLitStaticAsParam',
  11628. LinesToStr([ // statements
  11629. 'this.DoInt = function (a) {',
  11630. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11631. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11632. ' $mod.DoInt(a);',
  11633. '};',
  11634. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11635. 'this.a = [];',
  11636. '']),
  11637. LinesToStr([ // $mod.$main
  11638. '$mod.a = [[1, 1]];',
  11639. '$mod.a = [$mod.i.slice(0)];',
  11640. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11641. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11642. '$mod.DoInt([[1, 1]]);',
  11643. '$mod.DoInt([[1, 2], [3, 4]]);',
  11644. '']));
  11645. end;
  11646. procedure TTestModule.TestArray_ForInArrOfString;
  11647. begin
  11648. StartProgram(false);
  11649. Add([
  11650. 'type',
  11651. 'type',
  11652. ' TMonthNameArray = array [1..12] of string;',
  11653. ' TMonthNames = TMonthNameArray;',
  11654. ' TObject = class',
  11655. ' private',
  11656. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11657. ' public',
  11658. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11659. ' end;',
  11660. 'var',
  11661. ' f: TObject;',
  11662. ' Month: string;',
  11663. ' Names: array of string = (''a'',''foo'',''bar'');',
  11664. ' i: longint;',
  11665. 'begin',
  11666. ' for Month in f.LongMonthNames do ;',
  11667. ' for Month in Names do ;',
  11668. ' for i:=low(Names) to high(Names) do ;',
  11669. '']);
  11670. ConvertProgram;
  11671. CheckSource('TestArray_ForInArrOfString',
  11672. LinesToStr([ // statements
  11673. 'rtl.createClass(this, "TObject", null, function () {',
  11674. ' this.$init = function () {',
  11675. ' };',
  11676. ' this.$final = function () {',
  11677. ' };',
  11678. '});',
  11679. 'this.f = null;',
  11680. 'this.Month = "";',
  11681. 'this.Names = ["a", "foo", "bar"];',
  11682. 'this.i = 0;',
  11683. '']),
  11684. LinesToStr([ // $mod.$main
  11685. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11686. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11687. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11688. '']));
  11689. end;
  11690. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11691. begin
  11692. StartProgram(false);
  11693. Add([
  11694. '{$modeswitch externalclass}',
  11695. 'type',
  11696. ' TJSObject = class external name ''Object''',
  11697. ' end;',
  11698. ' TJSArray = class external name ''Array''',
  11699. ' class function isArray(Value: JSValue) : boolean;',
  11700. ' function concat() : TJSArray; varargs;',
  11701. ' end;',
  11702. 'var',
  11703. ' aObj: TJSArray;',
  11704. ' a: array of longint;',
  11705. ' o: TJSObject;',
  11706. 'begin',
  11707. ' if TJSArray.isArray(65) then ;',
  11708. ' aObj:=TJSArray(a).concat(a);',
  11709. ' o:=TJSObject(a);',
  11710. ' aObj:=TJSArray([''bird'',''ant'']);',
  11711. '']);
  11712. ConvertProgram;
  11713. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11714. LinesToStr([ // statements
  11715. 'this.aObj = null;',
  11716. 'this.a = [];',
  11717. 'this.o = null;',
  11718. '']),
  11719. LinesToStr([ // $mod.$main
  11720. 'if (Array.isArray(65)) ;',
  11721. '$mod.aObj = $mod.a.concat($mod.a);',
  11722. '$mod.o = $mod.a;',
  11723. '$mod.aObj = ["bird", "ant"];',
  11724. '']));
  11725. end;
  11726. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11727. begin
  11728. StartProgram(false);
  11729. Add([
  11730. '{$modeswitch externalclass}',
  11731. 'type',
  11732. ' TArrStr = array of string;',
  11733. ' TJSArray = class external name ''Array''',
  11734. ' end;',
  11735. ' TJSObject = class external name ''Object''',
  11736. ' end;',
  11737. 'var',
  11738. ' aObj: TJSArray;',
  11739. ' a: TArrStr;',
  11740. ' jo: TJSObject;',
  11741. 'begin',
  11742. ' a:=TArrStr(aObj);',
  11743. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11744. ' a:=TarrStr(jo);',
  11745. '']);
  11746. ConvertProgram;
  11747. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11748. LinesToStr([ // statements
  11749. 'this.aObj = null;',
  11750. 'this.a = [];',
  11751. 'this.jo = null;',
  11752. '']),
  11753. LinesToStr([ // $mod.$main
  11754. '$mod.a = $mod.aObj;',
  11755. '$mod.aObj[1] = $mod.aObj[2];',
  11756. '$mod.a = $mod.jo;',
  11757. '']));
  11758. end;
  11759. procedure TTestModule.TestArrayOfConst_TVarRec;
  11760. begin
  11761. StartProgram(true,[supTVarRec]);
  11762. Add([
  11763. 'procedure Say(args: array of const);',
  11764. 'var',
  11765. ' i: longint;',
  11766. ' v: TVarRec;',
  11767. 'begin',
  11768. ' for i:=low(args) to high(args) do begin',
  11769. ' v:=args[i];',
  11770. ' case v.vtype of',
  11771. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11772. ' end;',
  11773. ' end;',
  11774. ' for v in args do ;',
  11775. ' args:=nil;',
  11776. ' SetLength(args,2);',
  11777. 'end;',
  11778. 'begin']);
  11779. ConvertProgram;
  11780. CheckSource('TestArrayOfConst_TVarRec',
  11781. LinesToStr([ // statements
  11782. 'this.Say = function (args) {',
  11783. ' var i = 0;',
  11784. ' var v = pas.system.TVarRec.$new();',
  11785. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11786. ' i = $l;',
  11787. ' v.$assign(args[i]);',
  11788. ' var $tmp = v.VType;',
  11789. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11790. ' };',
  11791. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11792. ' args = [];',
  11793. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11794. '};',
  11795. '']),
  11796. LinesToStr([ // $mod.$main
  11797. ]));
  11798. end;
  11799. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11800. begin
  11801. StartProgram(true,[supTVarRec]);
  11802. Add([
  11803. 'procedure Say(args: array of const);',
  11804. 'begin',
  11805. ' Say(args);',
  11806. 'end;',
  11807. 'var',
  11808. ' p: Pointer;',
  11809. ' j: jsvalue;',
  11810. ' c: currency;',
  11811. 'begin',
  11812. ' Say([]);',
  11813. ' Say([1]);',
  11814. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11815. '']);
  11816. ConvertProgram;
  11817. CheckSource('TestArrayOfConst_PassBaseTypes',
  11818. LinesToStr([ // statements
  11819. 'this.Say = function (args) {',
  11820. ' $mod.Say(args);',
  11821. '};',
  11822. 'this.p = null;',
  11823. 'this.j = undefined;',
  11824. 'this.c = 0;',
  11825. '']),
  11826. LinesToStr([ // $mod.$main
  11827. '$mod.Say([]);',
  11828. '$mod.Say(pas.system.VarRecs(0, 1));',
  11829. '$mod.Say(pas.system.VarRecs(',
  11830. ' 9,',
  11831. ' "c",',
  11832. ' 18,',
  11833. ' "foo",',
  11834. ' 5,',
  11835. ' null,',
  11836. ' 1,',
  11837. ' true,',
  11838. ' 3,',
  11839. ' 1.3,',
  11840. ' 5,',
  11841. ' $mod.p,',
  11842. ' 20,',
  11843. ' $mod.j,',
  11844. ' 12,',
  11845. ' $mod.c',
  11846. ' ));',
  11847. '']));
  11848. end;
  11849. procedure TTestModule.TestArrayOfConst_PassObj;
  11850. begin
  11851. StartProgram(true,[supTVarRec]);
  11852. Add([
  11853. '{$interfaces corba}',
  11854. 'type',
  11855. ' TObject = class',
  11856. ' end;',
  11857. ' TClass = class of TObject;',
  11858. ' IUnknown = interface',
  11859. ' end;',
  11860. 'procedure Say(args: array of const);',
  11861. 'begin',
  11862. 'end;',
  11863. 'var',
  11864. ' o: TObject;',
  11865. ' c: TClass;',
  11866. ' i: IUnknown;',
  11867. 'begin',
  11868. ' Say([o,c,TObject]);',
  11869. ' Say([nil,i]);',
  11870. '']);
  11871. ConvertProgram;
  11872. CheckSource('TestArrayOfConst_PassObj',
  11873. LinesToStr([ // statements
  11874. 'rtl.createClass(this, "TObject", null, function () {',
  11875. ' this.$init = function () {',
  11876. ' };',
  11877. ' this.$final = function () {',
  11878. ' };',
  11879. '});',
  11880. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11881. 'this.Say = function (args) {',
  11882. '};',
  11883. 'this.o = null;',
  11884. 'this.c = null;',
  11885. 'this.i = null;',
  11886. '']),
  11887. LinesToStr([ // $mod.$main
  11888. '$mod.Say(pas.system.VarRecs(',
  11889. ' 7,',
  11890. ' $mod.o,',
  11891. ' 8,',
  11892. ' $mod.c,',
  11893. ' 8,',
  11894. ' $mod.TObject',
  11895. '));',
  11896. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11897. '']));
  11898. end;
  11899. procedure TTestModule.TestRecord_Empty;
  11900. begin
  11901. StartProgram(false);
  11902. Add([
  11903. 'type',
  11904. ' TRecA = record',
  11905. ' end;',
  11906. 'var a,b: TRecA;',
  11907. 'begin',
  11908. ' if a=b then ;']);
  11909. ConvertProgram;
  11910. CheckSource('TestRecord_Empty',
  11911. LinesToStr([ // statements
  11912. 'rtl.recNewT(this, "TRecA", function () {',
  11913. ' this.$eq = function (b) {',
  11914. ' return true;',
  11915. ' };',
  11916. ' this.$assign = function (s) {',
  11917. ' return this;',
  11918. ' };',
  11919. '});',
  11920. 'this.a = this.TRecA.$new();',
  11921. 'this.b = this.TRecA.$new();',
  11922. '']),
  11923. LinesToStr([ // $mod.$main
  11924. 'if ($mod.a.$eq($mod.b)) ;'
  11925. ]));
  11926. end;
  11927. procedure TTestModule.TestRecord_Var;
  11928. begin
  11929. StartProgram(false);
  11930. Add('type');
  11931. Add(' TRecA = record');
  11932. Add(' Bold: longint;');
  11933. Add(' end;');
  11934. Add('var Rec: TRecA;');
  11935. Add('begin');
  11936. Add(' rec.bold:=123');
  11937. ConvertProgram;
  11938. CheckSource('TestRecord_Var',
  11939. LinesToStr([ // statements
  11940. 'rtl.recNewT(this, "TRecA", function () {',
  11941. ' this.Bold = 0;',
  11942. ' this.$eq = function (b) {',
  11943. ' return this.Bold === b.Bold;',
  11944. ' };',
  11945. ' this.$assign = function (s) {',
  11946. ' this.Bold = s.Bold;',
  11947. ' return this;',
  11948. ' };',
  11949. '});',
  11950. 'this.Rec = this.TRecA.$new();',
  11951. '']),
  11952. LinesToStr([ // $mod.$main
  11953. '$mod.Rec.Bold = 123;'
  11954. ]));
  11955. end;
  11956. procedure TTestModule.TestRecord_VarExternal;
  11957. begin
  11958. StartProgram(false);
  11959. Add([
  11960. '{$modeswitch externalclass}',
  11961. 'type',
  11962. ' TRecA = record',
  11963. ' i: byte;',
  11964. ' length_: longint external name ''length'';',
  11965. ' end;',
  11966. 'var Rec: TRecA;',
  11967. 'begin',
  11968. ' rec.length_ := rec.length_',
  11969. '']);
  11970. ConvertProgram;
  11971. CheckSource('TestRecord_VarExternal',
  11972. LinesToStr([ // statements
  11973. 'rtl.recNewT(this, "TRecA", function () {',
  11974. ' this.i = 0;',
  11975. ' this.$eq = function (b) {',
  11976. ' return (this.i === b.i) && (this.length === b.length);',
  11977. ' };',
  11978. ' this.$assign = function (s) {',
  11979. ' this.i = s.i;',
  11980. ' this.length = s.length;',
  11981. ' return this;',
  11982. ' };',
  11983. '});',
  11984. 'this.Rec = this.TRecA.$new();',
  11985. '']),
  11986. LinesToStr([ // $mod.$main
  11987. '$mod.Rec.length = $mod.Rec.length;'
  11988. ]));
  11989. end;
  11990. procedure TTestModule.TestRecord_WithDo;
  11991. begin
  11992. StartProgram(false);
  11993. Add('type');
  11994. Add(' TRec = record');
  11995. Add(' vI: longint;');
  11996. Add(' end;');
  11997. Add('var');
  11998. Add(' Int: longint;');
  11999. Add(' r: TRec;');
  12000. Add('begin');
  12001. Add(' with r do');
  12002. Add(' int:=vi;');
  12003. Add(' with r do begin');
  12004. Add(' int:=vi;');
  12005. Add(' vi:=int;');
  12006. Add(' end;');
  12007. ConvertProgram;
  12008. CheckSource('TestWithRecordDo',
  12009. LinesToStr([ // statements
  12010. 'rtl.recNewT(this, "TRec", function () {',
  12011. ' this.vI = 0;',
  12012. ' this.$eq = function (b) {',
  12013. ' return this.vI === b.vI;',
  12014. ' };',
  12015. ' this.$assign = function (s) {',
  12016. ' this.vI = s.vI;',
  12017. ' return this;',
  12018. ' };',
  12019. '});',
  12020. 'this.Int = 0;',
  12021. 'this.r = this.TRec.$new();',
  12022. '']),
  12023. LinesToStr([ // $mod.$main
  12024. 'var $with = $mod.r;',
  12025. '$mod.Int = $with.vI;',
  12026. 'var $with1 = $mod.r;',
  12027. '$mod.Int = $with1.vI;',
  12028. '$with1.vI = $mod.Int;'
  12029. ]));
  12030. end;
  12031. procedure TTestModule.TestRecord_Assign;
  12032. begin
  12033. StartProgram(false);
  12034. Add([
  12035. 'type',
  12036. ' TEnum = (red,green);',
  12037. ' TEnums = set of TEnum;',
  12038. ' TSmallRec = record',
  12039. ' N: longint;',
  12040. ' end;',
  12041. ' TBigRec = record',
  12042. ' Int: longint;',
  12043. ' D: double;',
  12044. ' Arr: array of longint;',
  12045. ' Arr2: array[1..2] of longint;',
  12046. ' Small: TSmallRec;',
  12047. ' Enums: TEnums;',
  12048. ' end;',
  12049. 'var',
  12050. ' r, s: TBigRec;',
  12051. 'begin',
  12052. ' r:=s;',
  12053. ' r:=default(TBigRec);',
  12054. ' r:=default(s);',
  12055. '']);
  12056. ConvertProgram;
  12057. CheckSource('TestRecord_Assign',
  12058. LinesToStr([ // statements
  12059. 'this.TEnum = {',
  12060. ' "0": "red",',
  12061. ' red: 0,',
  12062. ' "1": "green",',
  12063. ' green: 1',
  12064. '};',
  12065. 'rtl.recNewT(this, "TSmallRec", function () {',
  12066. ' this.N = 0;',
  12067. ' this.$eq = function (b) {',
  12068. ' return this.N === b.N;',
  12069. ' };',
  12070. ' this.$assign = function (s) {',
  12071. ' this.N = s.N;',
  12072. ' return this;',
  12073. ' };',
  12074. '});',
  12075. 'rtl.recNewT(this, "TBigRec", function () {',
  12076. ' this.Int = 0;',
  12077. ' this.D = 0.0;',
  12078. ' this.$new = function () {',
  12079. ' var r = Object.create(this);',
  12080. ' r.Arr = [];',
  12081. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12082. ' r.Small = $mod.TSmallRec.$new();',
  12083. ' r.Enums = {};',
  12084. ' return r;',
  12085. ' };',
  12086. ' this.$eq = function (b) {',
  12087. ' 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);',
  12088. ' };',
  12089. ' this.$assign = function (s) {',
  12090. ' this.Int = s.Int;',
  12091. ' this.D = s.D;',
  12092. ' this.Arr = rtl.arrayRef(s.Arr);',
  12093. ' this.Arr2 = s.Arr2.slice(0);',
  12094. ' this.Small.$assign(s.Small);',
  12095. ' this.Enums = rtl.refSet(s.Enums);',
  12096. ' return this;',
  12097. ' };',
  12098. '});',
  12099. 'this.r = this.TBigRec.$new();',
  12100. 'this.s = this.TBigRec.$new();',
  12101. '']),
  12102. LinesToStr([ // $mod.$main
  12103. '$mod.r.$assign($mod.s);',
  12104. '$mod.r.$assign($mod.TBigRec.$new());',
  12105. '$mod.r.$assign($mod.TBigRec.$new());',
  12106. '']));
  12107. end;
  12108. procedure TTestModule.TestRecord_AsParams;
  12109. begin
  12110. StartProgram(false);
  12111. Add([
  12112. 'type',
  12113. ' integer = longint;',
  12114. ' TRecord = record',
  12115. ' i: integer;',
  12116. ' end;',
  12117. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12118. 'var vL: TRecord;',
  12119. 'begin',
  12120. ' vd:=vd;',
  12121. ' vd.i:=vd.i;',
  12122. ' vl:=vc;',
  12123. ' vv:=vv;',
  12124. ' vv.i:=vv.i;',
  12125. ' U:=vl;',
  12126. ' U:=vd;',
  12127. ' U:=vc;',
  12128. ' U:=vv;',
  12129. ' vl:=TRecord(U);',
  12130. ' vd:=TRecord(U);',
  12131. ' vv:=TRecord(U);',
  12132. ' doit(vd,vd,vd,vd);',
  12133. ' doit(vc,vc,vl,vl);',
  12134. ' doit(vv,vv,vv,vv);',
  12135. ' doit(vl,vl,vl,vl);',
  12136. ' TRecord(U).i:=3;',
  12137. 'end;',
  12138. 'var i: TRecord;',
  12139. 'begin',
  12140. ' doit(i,i,i,i);',
  12141. '']);
  12142. ConvertProgram;
  12143. CheckSource('TestRecord_AsParams',
  12144. LinesToStr([ // statements
  12145. 'rtl.recNewT(this, "TRecord", function () {',
  12146. ' this.i = 0;',
  12147. ' this.$eq = function (b) {',
  12148. ' return this.i === b.i;',
  12149. ' };',
  12150. ' this.$assign = function (s) {',
  12151. ' this.i = s.i;',
  12152. ' return this;',
  12153. ' };',
  12154. '});',
  12155. 'this.DoIt = function (vD, vC, vV, U) {',
  12156. ' var vL = $mod.TRecord.$new();',
  12157. ' vD.$assign(vD);',
  12158. ' vD.i = vD.i;',
  12159. ' vL.$assign(vC);',
  12160. ' vV.$assign(vV);',
  12161. ' vV.i = vV.i;',
  12162. ' U.$assign(vL);',
  12163. ' U.$assign(vD);',
  12164. ' U.$assign(vC);',
  12165. ' U.$assign(vV);',
  12166. ' vL.$assign(U);',
  12167. ' vD.$assign(U);',
  12168. ' vV.$assign(U);',
  12169. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12170. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12171. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12172. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12173. ' U.i = 3;',
  12174. '};',
  12175. 'this.i = this.TRecord.$new();'
  12176. ]),
  12177. LinesToStr([
  12178. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12179. '']));
  12180. end;
  12181. procedure TTestModule.TestRecord_ConstRef;
  12182. begin
  12183. StartProgram(false);
  12184. Add([
  12185. 'type TRec = record i: word; end;',
  12186. 'procedure Run(constref a: TRec);',
  12187. 'begin',
  12188. 'end;',
  12189. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12190. 'var l: TRec;',
  12191. 'begin',
  12192. ' Run(l);',
  12193. ' Run(a);',
  12194. ' Run(b);',
  12195. ' Run(c);',
  12196. ' Run(d);',
  12197. ' Run(e);',
  12198. 'end;',
  12199. 'begin',
  12200. '']);
  12201. ConvertProgram;
  12202. CheckResolverUnexpectedHints();
  12203. CheckSource('TestRecord_ConstRef',
  12204. LinesToStr([ // statements
  12205. 'rtl.recNewT(this, "TRec", function () {',
  12206. ' this.i = 0;',
  12207. ' this.$eq = function (b) {',
  12208. ' return this.i === b.i;',
  12209. ' };',
  12210. ' this.$assign = function (s) {',
  12211. ' this.i = s.i;',
  12212. ' return this;',
  12213. ' };',
  12214. '});',
  12215. 'this.Run = function (a) {',
  12216. '};',
  12217. 'this.Fly = function (a, b, c, d, e) {',
  12218. ' var l = $mod.TRec.$new();',
  12219. ' $mod.Run(l);',
  12220. ' $mod.Run(a);',
  12221. ' $mod.Run(b);',
  12222. ' $mod.Run(c);',
  12223. ' $mod.Run(d);',
  12224. ' $mod.Run(e);',
  12225. '};',
  12226. '']),
  12227. LinesToStr([
  12228. '']));
  12229. end;
  12230. procedure TTestModule.TestRecordElement_AsParams;
  12231. begin
  12232. StartProgram(false);
  12233. Add('type');
  12234. Add(' integer = longint;');
  12235. Add(' TRecord = record');
  12236. Add(' i: integer;');
  12237. Add(' end;');
  12238. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12239. Add('var vJ: TRecord;');
  12240. Add('begin');
  12241. Add(' doit(vj.i,vj.i,vj.i);');
  12242. Add('end;');
  12243. Add('var r: TRecord;');
  12244. Add('begin');
  12245. Add(' doit(r.i,r.i,r.i);');
  12246. ConvertProgram;
  12247. CheckSource('TestRecordElement_AsParams',
  12248. LinesToStr([ // statements
  12249. 'rtl.recNewT(this, "TRecord", function () {',
  12250. ' this.i = 0;',
  12251. ' this.$eq = function (b) {',
  12252. ' return this.i === b.i;',
  12253. ' };',
  12254. ' this.$assign = function (s) {',
  12255. ' this.i = s.i;',
  12256. ' return this;',
  12257. ' };',
  12258. '});',
  12259. 'this.DoIt = function (vG,vH,vI) {',
  12260. ' var vJ = $mod.TRecord.$new();',
  12261. ' $mod.DoIt(vJ.i, vJ.i, {',
  12262. ' p: vJ,',
  12263. ' get: function () {',
  12264. ' return this.p.i;',
  12265. ' },',
  12266. ' set: function (v) {',
  12267. ' this.p.i = v;',
  12268. ' }',
  12269. ' });',
  12270. '};',
  12271. 'this.r = this.TRecord.$new();'
  12272. ]),
  12273. LinesToStr([
  12274. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12275. ' p: $mod.r,',
  12276. ' get: function () {',
  12277. ' return this.p.i;',
  12278. ' },',
  12279. ' set: function (v) {',
  12280. ' this.p.i = v;',
  12281. ' }',
  12282. '});'
  12283. ]));
  12284. end;
  12285. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12286. begin
  12287. StartProgram(false);
  12288. Add('type');
  12289. Add(' integer = longint;');
  12290. Add(' TRecord = record');
  12291. Add(' i: integer;');
  12292. Add(' end;');
  12293. Add('function GetRec(vB: integer = 0): TRecord;');
  12294. Add('begin');
  12295. Add('end;');
  12296. Add('procedure DoIt(vG: integer; const vH: integer);');
  12297. Add('begin');
  12298. Add('end;');
  12299. Add('begin');
  12300. Add(' doit(getrec.i,getrec.i);');
  12301. Add(' doit(getrec().i,getrec().i);');
  12302. Add(' doit(getrec(1).i,getrec(2).i);');
  12303. ConvertProgram;
  12304. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12305. LinesToStr([ // statements
  12306. 'rtl.recNewT(this, "TRecord", function () {',
  12307. ' this.i = 0;',
  12308. ' this.$eq = function (b) {',
  12309. ' return this.i === b.i;',
  12310. ' };',
  12311. ' this.$assign = function (s) {',
  12312. ' this.i = s.i;',
  12313. ' return this;',
  12314. ' };',
  12315. '});',
  12316. 'this.GetRec = function (vB) {',
  12317. ' var Result = $mod.TRecord.$new();',
  12318. ' return Result;',
  12319. '};',
  12320. 'this.DoIt = function (vG, vH) {',
  12321. '};',
  12322. '']),
  12323. LinesToStr([
  12324. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12325. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12326. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12327. '']));
  12328. end;
  12329. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12330. begin
  12331. StartProgram(false);
  12332. Add('type');
  12333. Add(' integer = longint;');
  12334. Add(' TRecord = record');
  12335. Add(' i: integer;');
  12336. Add(' end;');
  12337. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12338. Add('begin');
  12339. Add('end;');
  12340. Add('var r: trecord;');
  12341. Add('begin');
  12342. Add(' with r do ');
  12343. Add(' doit(i,i,i);');
  12344. ConvertProgram;
  12345. CheckSource('TestRecordElementFromWith_AsParams',
  12346. LinesToStr([ // statements
  12347. 'rtl.recNewT(this, "TRecord", function () {',
  12348. ' this.i = 0;',
  12349. ' this.$eq = function (b) {',
  12350. ' return this.i === b.i;',
  12351. ' };',
  12352. ' this.$assign = function (s) {',
  12353. ' this.i = s.i;',
  12354. ' return this;',
  12355. ' };',
  12356. '});',
  12357. 'this.DoIt = function (vG,vH,vI) {',
  12358. '};',
  12359. 'this.r = this.TRecord.$new();'
  12360. ]),
  12361. LinesToStr([
  12362. 'var $with = $mod.r;',
  12363. '$mod.DoIt($with.i,$with.i,{',
  12364. ' p: $with,',
  12365. ' get: function () {',
  12366. ' return this.p.i;',
  12367. ' },',
  12368. ' set: function (v) {',
  12369. ' this.p.i = v;',
  12370. ' }',
  12371. '});',
  12372. '']));
  12373. end;
  12374. procedure TTestModule.TestRecord_Equal;
  12375. begin
  12376. StartProgram(false);
  12377. Add('type');
  12378. Add(' integer = longint;');
  12379. Add(' TFlag = (red,blue);');
  12380. Add(' TFlags = set of TFlag;');
  12381. Add(' TProc = procedure;');
  12382. Add(' TRecord = record');
  12383. Add(' i: integer;');
  12384. Add(' Event: TProc;');
  12385. Add(' f: TFlags;');
  12386. Add(' end;');
  12387. Add(' TNested = record');
  12388. Add(' r: TRecord;');
  12389. Add(' end;');
  12390. Add('var');
  12391. Add(' b: boolean;');
  12392. Add(' r,s: trecord;');
  12393. Add('begin');
  12394. Add(' b:=r=s;');
  12395. Add(' b:=r<>s;');
  12396. ConvertProgram;
  12397. CheckSource('TestRecord_Equal',
  12398. LinesToStr([ // statements
  12399. 'this.TFlag = {',
  12400. ' "0": "red",',
  12401. ' red: 0,',
  12402. ' "1": "blue",',
  12403. ' blue: 1',
  12404. '};',
  12405. 'rtl.recNewT(this, "TRecord", function () {',
  12406. ' this.i = 0;',
  12407. ' this.Event = null;',
  12408. ' this.$new = function () {',
  12409. ' var r = Object.create(this);',
  12410. ' r.f = {};',
  12411. ' return r;',
  12412. ' };',
  12413. ' this.$eq = function (b) {',
  12414. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12415. ' };',
  12416. ' this.$assign = function (s) {',
  12417. ' this.i = s.i;',
  12418. ' this.Event = s.Event;',
  12419. ' this.f = rtl.refSet(s.f);',
  12420. ' return this;',
  12421. ' };',
  12422. '});',
  12423. 'rtl.recNewT(this, "TNested", function () {',
  12424. ' this.$new = function () {',
  12425. ' var r = Object.create(this);',
  12426. ' r.r = $mod.TRecord.$new();',
  12427. ' return r;',
  12428. ' };',
  12429. ' this.$eq = function (b) {',
  12430. ' return this.r.$eq(b.r);',
  12431. ' };',
  12432. ' this.$assign = function (s) {',
  12433. ' this.r.$assign(s.r);',
  12434. ' return this;',
  12435. ' };',
  12436. '});',
  12437. 'this.b = false;',
  12438. 'this.r = this.TRecord.$new();',
  12439. 'this.s = this.TRecord.$new();',
  12440. '']),
  12441. LinesToStr([
  12442. '$mod.b = $mod.r.$eq($mod.s);',
  12443. '$mod.b = !$mod.r.$eq($mod.s);',
  12444. '']));
  12445. end;
  12446. procedure TTestModule.TestRecord_JSValue;
  12447. begin
  12448. StartProgram(false);
  12449. Add([
  12450. 'type',
  12451. ' TRecord = record',
  12452. ' i: longint;',
  12453. ' end;',
  12454. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12455. 'begin',
  12456. 'end;',
  12457. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12458. 'begin',
  12459. ' if jsvalue(d) then ;',
  12460. ' if jsvalue(c) then ;',
  12461. ' if jsvalue(v) then ;',
  12462. 'end;',
  12463. 'var',
  12464. ' Jv: jsvalue;',
  12465. ' Rec: trecord;',
  12466. 'begin',
  12467. ' rec:=trecord(jv);',
  12468. ' jv:=rec;',
  12469. ' Fly(rec,rec);',
  12470. ' Fly(@rec,@rec);',
  12471. ' if jsvalue(Rec) then ;',
  12472. ' Run(trecord(jv),trecord(jv),rec);',
  12473. '']);
  12474. ConvertProgram;
  12475. CheckSource('TestRecord_JSValue',
  12476. LinesToStr([ // statements
  12477. 'rtl.recNewT(this, "TRecord", function () {',
  12478. ' this.i = 0;',
  12479. ' this.$eq = function (b) {',
  12480. ' return this.i === b.i;',
  12481. ' };',
  12482. ' this.$assign = function (s) {',
  12483. ' this.i = s.i;',
  12484. ' return this;',
  12485. ' };',
  12486. '});',
  12487. 'this.Fly = function (d, c) {',
  12488. '};',
  12489. 'this.Run = function (d, c, v) {',
  12490. ' if (d) ;',
  12491. ' if (c) ;',
  12492. ' if (v) ;',
  12493. '};',
  12494. 'this.Jv = undefined;',
  12495. 'this.Rec = this.TRecord.$new();',
  12496. '']),
  12497. LinesToStr([
  12498. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12499. '$mod.Jv = $mod.Rec;',
  12500. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12501. '$mod.Fly($mod.Rec, $mod.Rec);',
  12502. 'if ($mod.Rec) ;',
  12503. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12504. '']));
  12505. end;
  12506. procedure TTestModule.TestRecord_VariantFail;
  12507. begin
  12508. StartProgram(false);
  12509. Add([
  12510. 'type',
  12511. ' TRec = record',
  12512. ' case word of',
  12513. ' 0: (b0, b1: Byte);',
  12514. ' 1: (i: word);',
  12515. ' end;',
  12516. 'begin']);
  12517. SetExpectedPasResolverError('Not supported: variant record',
  12518. nNotSupportedX);
  12519. ConvertProgram;
  12520. end;
  12521. procedure TTestModule.TestRecord_FieldArray;
  12522. begin
  12523. StartProgram(false);
  12524. Add([
  12525. 'type',
  12526. ' TArrInt = array[3..4] of longint;',
  12527. ' TArrArrInt = array[3..4] of longint;',
  12528. ' TRec = record',
  12529. ' a: array of longint;',
  12530. ' s: array[1..2] of longint;',
  12531. ' m: array[1..2,3..4] of longint;',
  12532. ' o: TArrArrInt;',
  12533. ' end;',
  12534. 'begin']);
  12535. ConvertProgram;
  12536. CheckSource('TestRecord_FieldArray',
  12537. LinesToStr([ // statements
  12538. 'rtl.recNewT(this, "TRec", function () {',
  12539. ' this.m$a$clone = function (a) {',
  12540. ' var b = [];',
  12541. ' b.length = 2;',
  12542. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12543. ' return b;',
  12544. ' };',
  12545. ' this.$new = function () {',
  12546. ' var r = Object.create(this);',
  12547. ' r.a = [];',
  12548. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12549. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12550. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12551. ' return r;',
  12552. ' };',
  12553. ' this.$eq = function (b) {',
  12554. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12555. ' };',
  12556. ' this.$assign = function (s) {',
  12557. ' this.a = rtl.arrayRef(s.a);',
  12558. ' this.s = s.s.slice(0);',
  12559. ' this.m = this.m$a$clone(s.m);',
  12560. ' this.o = s.o.slice(0);',
  12561. ' return this;',
  12562. ' };',
  12563. '});',
  12564. '']),
  12565. LinesToStr([ // $mod.$main
  12566. '']));
  12567. end;
  12568. procedure TTestModule.TestRecord_Const;
  12569. begin
  12570. StartProgram(false);
  12571. Add([
  12572. 'type',
  12573. ' TArrInt = array[3..4] of longint;',
  12574. ' TPoint = record x,y: longint; end;',
  12575. ' TRec = record',
  12576. ' i: longint;',
  12577. ' a: array of longint;',
  12578. ' s: array[1..2] of longint;',
  12579. ' m: array[1..2,3..4] of longint;',
  12580. ' p: TPoint;',
  12581. ' end;',
  12582. ' TPoints = array of TPoint;',
  12583. 'const',
  12584. ' r: TRec = (',
  12585. ' i:1;',
  12586. ' a:(2,3);',
  12587. ' s:(4,5);',
  12588. ' m:( (11,12), (13,14) );',
  12589. ' p: (x:21; y:22)',
  12590. ' );',
  12591. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12592. 'begin']);
  12593. ConvertProgram;
  12594. CheckSource('TestRecord_Const',
  12595. LinesToStr([ // statements
  12596. 'rtl.recNewT(this, "TPoint", function () {',
  12597. ' this.x = 0;',
  12598. ' this.y = 0;',
  12599. ' this.$eq = function (b) {',
  12600. ' return (this.x === b.x) && (this.y === b.y);',
  12601. ' };',
  12602. ' this.$assign = function (s) {',
  12603. ' this.x = s.x;',
  12604. ' this.y = s.y;',
  12605. ' return this;',
  12606. ' };',
  12607. '});',
  12608. 'rtl.recNewT(this, "TRec", function () {',
  12609. ' this.i = 0;',
  12610. ' this.m$a$clone = function (a) {',
  12611. ' var b = [];',
  12612. ' b.length = 2;',
  12613. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12614. ' return b;',
  12615. ' };',
  12616. ' this.$new = function () {',
  12617. ' var r = Object.create(this);',
  12618. ' r.a = [];',
  12619. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12620. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12621. ' r.p = $mod.TPoint.$new();',
  12622. ' return r;',
  12623. ' };',
  12624. ' this.$eq = function (b) {',
  12625. ' 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);',
  12626. ' };',
  12627. ' this.$assign = function (s) {',
  12628. ' this.i = s.i;',
  12629. ' this.a = rtl.arrayRef(s.a);',
  12630. ' this.s = s.s.slice(0);',
  12631. ' this.m = this.m$a$clone(s.m);',
  12632. ' this.p.$assign(s.p);',
  12633. ' return this;',
  12634. ' };',
  12635. '});',
  12636. 'this.r = this.TRec.$clone({',
  12637. ' i: 1,',
  12638. ' a: [2, 3],',
  12639. ' s: [4, 5],',
  12640. ' m: [[11, 12], [13, 14]],',
  12641. ' p: this.TPoint.$clone({',
  12642. ' x: 21,',
  12643. ' y: 22',
  12644. ' })',
  12645. '});',
  12646. 'this.p = [this.TPoint.$clone({',
  12647. ' x: 1,',
  12648. ' y: 2',
  12649. '}), this.TPoint.$clone({',
  12650. ' x: 3,',
  12651. ' y: 4',
  12652. '})];',
  12653. '']),
  12654. LinesToStr([ // $mod.$main
  12655. '']));
  12656. end;
  12657. procedure TTestModule.TestRecord_TypecastFail;
  12658. begin
  12659. StartProgram(false);
  12660. Add([
  12661. 'type',
  12662. ' TPoint = record x,y: longint; end;',
  12663. ' TRec = record l: longint end;',
  12664. 'var p: TPoint;',
  12665. 'begin',
  12666. ' if TRec(p).l=2 then ;']);
  12667. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12668. nIllegalTypeConversionTo);
  12669. ConvertProgram;
  12670. end;
  12671. procedure TTestModule.TestRecord_InFunction;
  12672. begin
  12673. StartProgram(false);
  12674. Add([
  12675. 'var TPoint: longint = 3;',
  12676. 'procedure DoIt;',
  12677. 'type',
  12678. ' TPoint = record x,y: longint; end;',
  12679. ' TPoints = array of TPoint;',
  12680. 'var',
  12681. ' r: TPoint;',
  12682. ' p: TPoints;',
  12683. 'begin',
  12684. ' SetLength(p,2);',
  12685. 'end;',
  12686. 'begin']);
  12687. ConvertProgram;
  12688. CheckSource('TestRecord_InFunction',
  12689. LinesToStr([ // statements
  12690. 'this.TPoint = 3;',
  12691. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12692. ' this.x = 0;',
  12693. ' this.y = 0;',
  12694. ' this.$eq = function (b) {',
  12695. ' return (this.x === b.x) && (this.y === b.y);',
  12696. ' };',
  12697. ' this.$assign = function (s) {',
  12698. ' this.x = s.x;',
  12699. ' this.y = s.y;',
  12700. ' return this;',
  12701. ' };',
  12702. '});',
  12703. 'this.DoIt = function () {',
  12704. ' var r = TPoint$1.$new();',
  12705. ' var p = [];',
  12706. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12707. '};',
  12708. '']),
  12709. LinesToStr([ // $mod.$main
  12710. '']));
  12711. end;
  12712. procedure TTestModule.TestRecordAnonym_Field;
  12713. begin
  12714. StartProgram(false);
  12715. Add(['',
  12716. 'var Rec: record',
  12717. ' Bold: longint;',
  12718. ' end;',
  12719. 'begin',
  12720. ' rec.bold:=123;',
  12721. ' rec.bold:=rec.bold+7;',
  12722. '']);
  12723. ConvertProgram;
  12724. CheckSource('TestRecordAnonym_Field',
  12725. LinesToStr([ // statements
  12726. 'rtl.recNewT(this, "Rec$a", function () {',
  12727. ' this.Bold = 0;',
  12728. ' this.$eq = function (b) {',
  12729. ' return this.Bold === b.Bold;',
  12730. ' };',
  12731. ' this.$assign = function (s) {',
  12732. ' this.Bold = s.Bold;',
  12733. ' return this;',
  12734. ' };',
  12735. '});',
  12736. 'this.Rec = this.Rec$a.$new();',
  12737. '']),
  12738. LinesToStr([ // $mod.$main
  12739. '$mod.Rec.Bold = 123;',
  12740. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12741. '']));
  12742. end;
  12743. procedure TTestModule.TestRecordAnonym_Assign;
  12744. begin
  12745. StartProgram(false);
  12746. Add(['',
  12747. 'var S,T: record',
  12748. ' Bold: longint;',
  12749. ' end;',
  12750. ' b: boolean;',
  12751. 'begin',
  12752. ' S:=T;',
  12753. ' b:=s=t;',
  12754. '']);
  12755. ConvertProgram;
  12756. CheckSource('TestRecordAnonym_Assign',
  12757. LinesToStr([ // statements
  12758. 'rtl.recNewT(this, "T$a", function () {',
  12759. ' this.Bold = 0;',
  12760. ' this.$eq = function (b) {',
  12761. ' return this.Bold === b.Bold;',
  12762. ' };',
  12763. ' this.$assign = function (s) {',
  12764. ' this.Bold = s.Bold;',
  12765. ' return this;',
  12766. ' };',
  12767. '});',
  12768. 'this.S = this.T$a.$new();',
  12769. 'this.T = this.T$a.$new();',
  12770. 'this.b = false;',
  12771. '']),
  12772. LinesToStr([ // $mod.$main
  12773. '$mod.S.$assign($mod.T);',
  12774. '$mod.b = $mod.S.$eq($mod.T);',
  12775. '']));
  12776. end;
  12777. procedure TTestModule.TestRecordAnonym_Nested;
  12778. begin
  12779. StartProgram(false);
  12780. Add(['',
  12781. 'var S,T: record',
  12782. ' Bold: longint;',
  12783. ' Sub: record',
  12784. ' Color: word;',
  12785. ' end;',
  12786. ' end;',
  12787. ' b: boolean;',
  12788. 'begin',
  12789. ' S:=T;',
  12790. ' S.Sub:=T.Sub;',
  12791. ' S.Sub.Color:=T.Sub.Color+3;',
  12792. ' b:=s=t;',
  12793. ' b:=s.Sub=t.Sub;',
  12794. '']);
  12795. ConvertProgram;
  12796. CheckSource('TestRecordAnonym_Nested',
  12797. LinesToStr([ // statements
  12798. 'rtl.recNewT(this, "T$a", function () {',
  12799. ' this.Bold = 0;',
  12800. ' rtl.recNewT(this, "Sub$a", function () {',
  12801. ' this.Color = 0;',
  12802. ' this.$eq = function (b) {',
  12803. ' return this.Color === b.Color;',
  12804. ' };',
  12805. ' this.$assign = function (s) {',
  12806. ' this.Color = s.Color;',
  12807. ' return this;',
  12808. ' };',
  12809. ' });',
  12810. ' this.$new = function () {',
  12811. ' var r = Object.create(this);',
  12812. ' r.Sub = this.Sub$a.$new();',
  12813. ' return r;',
  12814. ' };',
  12815. ' this.$eq = function (b) {',
  12816. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12817. ' };',
  12818. ' this.$assign = function (s) {',
  12819. ' this.Bold = s.Bold;',
  12820. ' this.Sub.$assign(s.Sub);',
  12821. ' return this;',
  12822. ' };',
  12823. '}, true);',
  12824. 'this.S = this.T$a.$new();',
  12825. 'this.T = this.T$a.$new();',
  12826. 'this.b = false;',
  12827. '']),
  12828. LinesToStr([ // $mod.$main
  12829. '$mod.S.$assign($mod.T);',
  12830. '$mod.S.Sub.$assign($mod.T.Sub);',
  12831. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12832. '$mod.b = $mod.S.$eq($mod.T);',
  12833. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12834. '']));
  12835. end;
  12836. procedure TTestModule.TestRecordAnonym_Const;
  12837. begin
  12838. StartProgram(false);
  12839. Add(['',
  12840. 'var T: record',
  12841. ' Bold: longint;',
  12842. ' Sub: record',
  12843. ' Color: word;',
  12844. ' end;',
  12845. ' end = (Bold: 2; Sub: (Color: 3));',
  12846. 'begin',
  12847. '']);
  12848. ConvertProgram;
  12849. CheckSource('TestRecordAnonym_Const',
  12850. LinesToStr([ // statements
  12851. 'rtl.recNewT(this, "T$a", function () {',
  12852. ' this.Bold = 0;',
  12853. ' rtl.recNewT(this, "Sub$a", function () {',
  12854. ' this.Color = 0;',
  12855. ' this.$eq = function (b) {',
  12856. ' return this.Color === b.Color;',
  12857. ' };',
  12858. ' this.$assign = function (s) {',
  12859. ' this.Color = s.Color;',
  12860. ' return this;',
  12861. ' };',
  12862. ' });',
  12863. ' this.$new = function () {',
  12864. ' var r = Object.create(this);',
  12865. ' r.Sub = this.Sub$a.$new();',
  12866. ' return r;',
  12867. ' };',
  12868. ' this.$eq = function (b) {',
  12869. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12870. ' };',
  12871. ' this.$assign = function (s) {',
  12872. ' this.Bold = s.Bold;',
  12873. ' this.Sub.$assign(s.Sub);',
  12874. ' return this;',
  12875. ' };',
  12876. '}, true);',
  12877. 'this.T = this.T$a.$clone({',
  12878. ' Bold: 2,',
  12879. ' Sub: this.T$a.Sub$a.$clone({',
  12880. ' Color: 3',
  12881. ' })',
  12882. '});',
  12883. '']),
  12884. LinesToStr([ // $mod.$main
  12885. '']));
  12886. end;
  12887. procedure TTestModule.TestRecordAnonym_InFunction;
  12888. begin
  12889. StartProgram(false);
  12890. Add(['',
  12891. 'procedure Fly;',
  12892. 'var T: record',
  12893. ' Bold: longint;',
  12894. ' Sub: record',
  12895. ' Color: word;',
  12896. ' end;',
  12897. ' end = (Bold: 2; Sub: (Color: 3));',
  12898. 'begin',
  12899. 'end;',
  12900. 'begin',
  12901. '']);
  12902. ConvertProgram;
  12903. CheckSource('TestRecordAnonym_InFunction',
  12904. LinesToStr([ // statements
  12905. 'var T$a = rtl.recNewT(null, "", function () {',
  12906. ' this.Bold = 0;',
  12907. ' rtl.recNewT(this, "Sub$a", function () {',
  12908. ' this.Color = 0;',
  12909. ' this.$eq = function (b) {',
  12910. ' return this.Color === b.Color;',
  12911. ' };',
  12912. ' this.$assign = function (s) {',
  12913. ' this.Color = s.Color;',
  12914. ' return this;',
  12915. ' };',
  12916. ' });',
  12917. ' this.$new = function () {',
  12918. ' var r = Object.create(this);',
  12919. ' r.Sub = this.Sub$a.$new();',
  12920. ' return r;',
  12921. ' };',
  12922. ' this.$eq = function (b) {',
  12923. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12924. ' };',
  12925. ' this.$assign = function (s) {',
  12926. ' this.Bold = s.Bold;',
  12927. ' this.Sub.$assign(s.Sub);',
  12928. ' return this;',
  12929. ' };',
  12930. '}, true);',
  12931. 'this.Fly = function () {',
  12932. ' var T = T$a.$clone({',
  12933. ' Bold: 2,',
  12934. ' Sub: T$a.Sub$a.$clone({',
  12935. ' Color: 3',
  12936. ' })',
  12937. ' });',
  12938. '};',
  12939. '']),
  12940. LinesToStr([ // $mod.$main
  12941. '']));
  12942. end;
  12943. procedure TTestModule.TestAdvRecord_Function;
  12944. begin
  12945. StartProgram(false);
  12946. Parser.Options:=Parser.Options+[po_cassignments];
  12947. Add([
  12948. '{$modeswitch AdvancedRecords}',
  12949. 'type',
  12950. ' TPoint = record',
  12951. ' x,y: word;',
  12952. ' function Add(const apt: TPoint): TPoint;',
  12953. ' end;',
  12954. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12955. 'begin',
  12956. ' Result:=Self;',
  12957. ' Result.x+=apt.x;',
  12958. ' Result.y:=Result.y+apt.y;',
  12959. ' Self:=apt;',
  12960. 'end;',
  12961. 'var p,q: TPoint;',
  12962. 'begin',
  12963. ' p.add(q);',
  12964. ' p:=default(TPoint);',
  12965. ' p:=q;',
  12966. '']);
  12967. ConvertProgram;
  12968. CheckSource('TestAdvRecord_Function',
  12969. LinesToStr([ // statements
  12970. 'rtl.recNewT(this, "TPoint", function () {',
  12971. ' this.x = 0;',
  12972. ' this.y = 0;',
  12973. ' this.$eq = function (b) {',
  12974. ' return (this.x === b.x) && (this.y === b.y);',
  12975. ' };',
  12976. ' this.$assign = function (s) {',
  12977. ' this.x = s.x;',
  12978. ' this.y = s.y;',
  12979. ' return this;',
  12980. ' };',
  12981. ' this.Add = function (apt) {',
  12982. ' var Result = $mod.TPoint.$new();',
  12983. ' Result.$assign(this);',
  12984. ' Result.x += apt.x;',
  12985. ' Result.y = Result.y + apt.y;',
  12986. ' this.$assign(apt);',
  12987. ' return Result;',
  12988. ' };',
  12989. '});',
  12990. 'this.p = this.TPoint.$new();',
  12991. 'this.q = this.TPoint.$new();',
  12992. '']),
  12993. LinesToStr([ // $mod.$main
  12994. '$mod.p.Add($mod.q);',
  12995. '$mod.p.$assign($mod.TPoint.$new());',
  12996. '$mod.p.$assign($mod.q);',
  12997. '']));
  12998. end;
  12999. procedure TTestModule.TestAdvRecord_Property;
  13000. begin
  13001. StartProgram(false);
  13002. Add([
  13003. '{$modeswitch AdvancedRecords}',
  13004. 'type',
  13005. ' TPoint = record',
  13006. ' x,y: word;',
  13007. ' strict private',
  13008. ' function GetSize: longword;',
  13009. ' procedure SetSize(Value: longword);',
  13010. ' public',
  13011. ' property Size: longword read GetSize write SetSize;',
  13012. ' property Left: word read x write y;',
  13013. ' end;',
  13014. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13015. 'function TPoint.GetSize: longword;',
  13016. 'begin',
  13017. ' x:=y;',
  13018. ' Size:=Size;',
  13019. ' Left:=Left;',
  13020. 'end;',
  13021. 'procedure TPoint.SetSize(Value: longword);',
  13022. 'begin',
  13023. 'end;',
  13024. 'var p,q: TPoint;',
  13025. 'begin',
  13026. ' p.Size:=q.Size;',
  13027. ' p.Left:=q.Left;',
  13028. '']);
  13029. ConvertProgram;
  13030. CheckSource('TestAdvRecord_Property',
  13031. LinesToStr([ // statements
  13032. 'rtl.recNewT(this, "TPoint", function () {',
  13033. ' this.x = 0;',
  13034. ' this.y = 0;',
  13035. ' this.$eq = function (b) {',
  13036. ' return (this.x === b.x) && (this.y === b.y);',
  13037. ' };',
  13038. ' this.$assign = function (s) {',
  13039. ' this.x = s.x;',
  13040. ' this.y = s.y;',
  13041. ' return this;',
  13042. ' };',
  13043. ' this.GetSize = function () {',
  13044. ' var Result = 0;',
  13045. ' this.x = this.y;',
  13046. ' this.SetSize(this.GetSize());',
  13047. ' this.y = this.x;',
  13048. ' return Result;',
  13049. ' };',
  13050. ' this.SetSize = function (Value) {',
  13051. ' };',
  13052. '});',
  13053. 'this.SetSize = function (Value) {',
  13054. '};',
  13055. 'this.p = this.TPoint.$new();',
  13056. 'this.q = this.TPoint.$new();',
  13057. '']),
  13058. LinesToStr([ // $mod.$main
  13059. '$mod.p.SetSize($mod.q.GetSize());',
  13060. '$mod.p.y = $mod.q.x;',
  13061. '']));
  13062. end;
  13063. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13064. begin
  13065. StartProgram(false);
  13066. Add([
  13067. '{$modeswitch AdvancedRecords}',
  13068. 'type',
  13069. ' TPoint = record',
  13070. ' strict private',
  13071. ' function GetItems(Index: word): word;',
  13072. ' procedure SetItems(Index: word; Value: word);',
  13073. ' public',
  13074. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13075. ' end;',
  13076. 'function TPoint.GetItems(Index: word): word;',
  13077. 'begin',
  13078. ' Items[index]:=Items[index];',
  13079. ' self.Items[index]:=self.Items[index];',
  13080. 'end;',
  13081. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13082. 'begin',
  13083. 'end;',
  13084. 'var p: TPoint;',
  13085. 'begin',
  13086. ' p[1]:=p[2];',
  13087. ' p.Items[3]:=p.Items[4];',
  13088. '']);
  13089. ConvertProgram;
  13090. CheckSource('TestAdvRecord_PropertyDefault',
  13091. LinesToStr([ // statements
  13092. 'rtl.recNewT(this, "TPoint", function () {',
  13093. ' this.$eq = function (b) {',
  13094. ' return true;',
  13095. ' };',
  13096. ' this.$assign = function (s) {',
  13097. ' return this;',
  13098. ' };',
  13099. ' this.GetItems = function (Index) {',
  13100. ' var Result = 0;',
  13101. ' this.SetItems(Index, this.GetItems(Index));',
  13102. ' this.SetItems(Index, this.GetItems(Index));',
  13103. ' return Result;',
  13104. ' };',
  13105. ' this.SetItems = function (Index, Value) {',
  13106. ' };',
  13107. '});',
  13108. 'this.p = this.TPoint.$new();',
  13109. '']),
  13110. LinesToStr([ // $mod.$main
  13111. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13112. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13113. '']));
  13114. end;
  13115. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13116. begin
  13117. StartProgram(false);
  13118. Add([
  13119. '{$modeswitch AdvancedRecords}',
  13120. 'type',
  13121. ' TRec = record',
  13122. ' class var',
  13123. ' Fx: longint;',
  13124. ' Fy: longint;',
  13125. ' class function GetInt: longint; static;',
  13126. ' class procedure SetInt(Value: longint); static;',
  13127. ' class procedure DoIt; static;',
  13128. ' class property IntA: longint read Fx write Fy;',
  13129. ' class property IntB: longint read GetInt write SetInt;',
  13130. ' end;',
  13131. 'class function trec.getint: longint;',
  13132. 'begin',
  13133. ' result:=fx;',
  13134. 'end;',
  13135. 'class procedure trec.setint(value: longint);',
  13136. 'begin',
  13137. 'end;',
  13138. 'class procedure trec.doit;',
  13139. 'begin',
  13140. ' IntA:=IntA+1;',
  13141. ' IntB:=IntB+1;',
  13142. 'end;',
  13143. 'var r: trec;',
  13144. 'begin',
  13145. ' trec.inta:=trec.inta+1;',
  13146. ' if trec.intb=2 then;',
  13147. ' trec.intb:=trec.intb+2;',
  13148. ' trec.setint(trec.inta);',
  13149. ' r.inta:=r.inta+1;',
  13150. ' if r.intb=2 then;',
  13151. ' r.intb:=r.intb+2;',
  13152. ' r.setint(r.inta);']);
  13153. ConvertProgram;
  13154. CheckSource('TestAdvRecord_Property_ClassMethod',
  13155. LinesToStr([ // statements
  13156. 'rtl.recNewT(this, "TRec", function () {',
  13157. ' this.Fx = 0;',
  13158. ' this.Fy = 0;',
  13159. ' this.$eq = function (b) {',
  13160. ' return true;',
  13161. ' };',
  13162. ' this.$assign = function (s) {',
  13163. ' return this;',
  13164. ' };',
  13165. ' this.GetInt = function () {',
  13166. ' var Result = 0;',
  13167. ' Result = $mod.TRec.Fx;',
  13168. ' return Result;',
  13169. ' };',
  13170. ' this.SetInt = function (Value) {',
  13171. ' };',
  13172. ' this.DoIt = function () {',
  13173. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13174. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13175. ' };',
  13176. '}, true);',
  13177. 'this.r = this.TRec.$new();',
  13178. '']),
  13179. LinesToStr([ // $mod.$main
  13180. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13181. 'if ($mod.TRec.GetInt() === 2) ;',
  13182. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13183. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13184. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13185. 'if ($mod.TRec.GetInt() === 2) ;',
  13186. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13187. '$mod.TRec.SetInt($mod.r.Fx);',
  13188. '']));
  13189. end;
  13190. procedure TTestModule.TestAdvRecord_Const;
  13191. begin
  13192. StartProgram(false);
  13193. Add([
  13194. '{$modeswitch AdvancedRecords}',
  13195. 'type',
  13196. ' TArrInt = array[3..4] of longint;',
  13197. ' TPoint = record',
  13198. ' x,y: longint;',
  13199. ' class var Count: nativeint;',
  13200. ' end;',
  13201. ' TRec = record',
  13202. ' i: longint;',
  13203. ' a: array of longint;',
  13204. ' s: array[1..2] of longint;',
  13205. ' m: array[1..2,3..4] of longint;',
  13206. ' p: TPoint;',
  13207. ' end;',
  13208. ' TPoints = array of TPoint;',
  13209. 'const',
  13210. ' r: TRec = (',
  13211. ' i:1;',
  13212. ' a:(2,3);',
  13213. ' s:(4,5);',
  13214. ' m:( (11,12), (13,14) );',
  13215. ' p: (x:21)',
  13216. ' );',
  13217. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13218. 'begin']);
  13219. ConvertProgram;
  13220. CheckSource('TestAdvRecord_Const',
  13221. LinesToStr([ // statements
  13222. 'rtl.recNewT(this, "TPoint", function () {',
  13223. ' this.x = 0;',
  13224. ' this.y = 0;',
  13225. ' this.Count = 0;',
  13226. ' this.$eq = function (b) {',
  13227. ' return (this.x === b.x) && (this.y === b.y);',
  13228. ' };',
  13229. ' this.$assign = function (s) {',
  13230. ' this.x = s.x;',
  13231. ' this.y = s.y;',
  13232. ' return this;',
  13233. ' };',
  13234. '}, true);',
  13235. 'rtl.recNewT(this, "TRec", function () {',
  13236. ' this.i = 0;',
  13237. ' this.m$a$clone = function (a) {',
  13238. ' var b = [];',
  13239. ' b.length = 2;',
  13240. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13241. ' return b;',
  13242. ' };',
  13243. ' this.$new = function () {',
  13244. ' var r = Object.create(this);',
  13245. ' r.a = [];',
  13246. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13247. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13248. ' r.p = $mod.TPoint.$new();',
  13249. ' return r;',
  13250. ' };',
  13251. ' this.$eq = function (b) {',
  13252. ' 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);',
  13253. ' };',
  13254. ' this.$assign = function (s) {',
  13255. ' this.i = s.i;',
  13256. ' this.a = rtl.arrayRef(s.a);',
  13257. ' this.s = s.s.slice(0);',
  13258. ' this.m = this.m$a$clone(s.m);',
  13259. ' this.p.$assign(s.p);',
  13260. ' return this;',
  13261. ' };',
  13262. '});',
  13263. 'this.r = this.TRec.$clone({',
  13264. ' i: 1,',
  13265. ' a: [2, 3],',
  13266. ' s: [4, 5],',
  13267. ' m: [[11, 12], [13, 14]],',
  13268. ' p: this.TPoint.$clone({',
  13269. ' x: 21,',
  13270. ' y: 0',
  13271. ' })',
  13272. '});',
  13273. 'this.p = [this.TPoint.$clone({',
  13274. ' x: 1,',
  13275. ' y: 2',
  13276. '}), this.TPoint.$clone({',
  13277. ' x: 3,',
  13278. ' y: 4',
  13279. '})];',
  13280. '']),
  13281. LinesToStr([ // $mod.$main
  13282. '']));
  13283. end;
  13284. procedure TTestModule.TestAdvRecord_ExternalField;
  13285. begin
  13286. StartProgram(false);
  13287. Add([
  13288. '{$modeswitch AdvancedRecords}',
  13289. '{$modeswitch externalclass}',
  13290. 'type',
  13291. ' TCar = record',
  13292. ' public',
  13293. ' Intern: longint external name ''$Intern'';',
  13294. ' Intern2: longint external name ''$Intern2'';',
  13295. ' Bracket: longint external name ''["A B"]'';',
  13296. ' procedure DoIt;',
  13297. ' end;',
  13298. 'procedure tcar.doit;',
  13299. 'begin',
  13300. ' Intern:=Intern+1;',
  13301. ' Intern2:=Intern2+2;',
  13302. ' Bracket:=Bracket+3;',
  13303. 'end;',
  13304. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13305. 'begin',
  13306. ' Rec.intern:=Rec.intern+1;',
  13307. ' Rec.intern2:=Rec.intern2+2;',
  13308. ' Rec.Bracket:=Rec.Bracket+3;',
  13309. ' with Rec do begin',
  13310. ' intern:=intern+1;',
  13311. ' intern2:=intern2+2;',
  13312. ' Bracket:=Bracket+3;',
  13313. ' end;']);
  13314. ConvertProgram;
  13315. CheckSource('TestAdvRecord_ExternalField',
  13316. LinesToStr([ // statements
  13317. 'rtl.recNewT(this, "TCar", function () {',
  13318. ' this.$eq = function (b) {',
  13319. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13320. ' };',
  13321. ' this.$assign = function (s) {',
  13322. ' this.$Intern = s.$Intern;',
  13323. ' this.$Intern2 = s.$Intern2;',
  13324. ' this["A B"] = s["A B"];',
  13325. ' return this;',
  13326. ' };',
  13327. ' this.DoIt = function () {',
  13328. ' this.$Intern = this.$Intern + 1;',
  13329. ' this.$Intern2 = this.$Intern2 + 2;',
  13330. ' this["A B"] = this["A B"] + 3;',
  13331. ' };',
  13332. '});',
  13333. 'this.Rec = this.TCar.$clone({',
  13334. ' $Intern: 11,',
  13335. ' $Intern2: 12,',
  13336. ' "A B": 13',
  13337. '});',
  13338. '']),
  13339. LinesToStr([ // $mod.$main
  13340. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13341. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13342. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13343. 'var $with = $mod.Rec;',
  13344. '$with.$Intern = $with.$Intern + 1;',
  13345. '$with.$Intern2 = $with.$Intern2 + 2;',
  13346. '$with["A B"] = $with["A B"] + 3;',
  13347. '']));
  13348. end;
  13349. procedure TTestModule.TestAdvRecord_SubRecord;
  13350. begin
  13351. StartProgram(false);
  13352. Add([
  13353. '{$modeswitch AdvancedRecords}',
  13354. 'type',
  13355. ' TRec = record',
  13356. ' type',
  13357. ' TPoint = record',
  13358. ' x,y: longint;',
  13359. ' class var Count: nativeint;',
  13360. ' procedure DoIt;',
  13361. ' class procedure DoThat; static;',
  13362. ' end;',
  13363. ' var',
  13364. ' i: longint;',
  13365. ' p: TPoint;',
  13366. ' procedure DoSome;',
  13367. ' end;',
  13368. 'const',
  13369. ' r: TRec = (',
  13370. ' i:1;',
  13371. ' p: (x:21;y:22)',
  13372. ' );',
  13373. 'procedure TRec.DoSome;',
  13374. 'begin',
  13375. ' p.x:=p.y+1;',
  13376. ' p.Count:=p.Count+2;',
  13377. 'end;',
  13378. 'procedure TRec.TPoint.DoIt;',
  13379. 'begin',
  13380. ' Count:=Count+3;',
  13381. 'end;',
  13382. 'class procedure TRec.TPoint.DoThat;',
  13383. 'begin',
  13384. ' Count:=Count+4;',
  13385. 'end;',
  13386. 'begin']);
  13387. ConvertProgram;
  13388. CheckSource('TestAdvRecord_SubRecord',
  13389. LinesToStr([ // statements
  13390. 'rtl.recNewT(this, "TRec", function () {',
  13391. ' rtl.recNewT(this, "TPoint", function () {',
  13392. ' this.x = 0;',
  13393. ' this.y = 0;',
  13394. ' this.Count = 0;',
  13395. ' this.$eq = function (b) {',
  13396. ' return (this.x === b.x) && (this.y === b.y);',
  13397. ' };',
  13398. ' this.$assign = function (s) {',
  13399. ' this.x = s.x;',
  13400. ' this.y = s.y;',
  13401. ' return this;',
  13402. ' };',
  13403. ' this.DoIt = function () {',
  13404. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13405. ' };',
  13406. ' this.DoThat = function () {',
  13407. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13408. ' };',
  13409. ' }, true);',
  13410. ' this.i = 0;',
  13411. ' this.$new = function () {',
  13412. ' var r = Object.create(this);',
  13413. ' r.p = this.TPoint.$new();',
  13414. ' return r;',
  13415. ' };',
  13416. ' this.$eq = function (b) {',
  13417. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13418. ' };',
  13419. ' this.$assign = function (s) {',
  13420. ' this.i = s.i;',
  13421. ' this.p.$assign(s.p);',
  13422. ' return this;',
  13423. ' };',
  13424. ' this.DoSome = function () {',
  13425. ' this.p.x = this.p.y + 1;',
  13426. ' this.TPoint.Count = this.p.Count + 2;',
  13427. ' };',
  13428. '}, true);',
  13429. 'this.r = this.TRec.$clone({',
  13430. ' i: 1,',
  13431. ' p: this.TRec.TPoint.$clone({',
  13432. ' x: 21,',
  13433. ' y: 22',
  13434. ' })',
  13435. '});',
  13436. '']),
  13437. LinesToStr([ // $mod.$main
  13438. '']));
  13439. end;
  13440. procedure TTestModule.TestAdvRecord_SubClass;
  13441. begin
  13442. StartProgram(false);
  13443. Add([
  13444. '{$modeswitch AdvancedRecords}',
  13445. 'type',
  13446. ' TObject = class end;',
  13447. ' TPoint = record',
  13448. ' type',
  13449. ' TBird = class',
  13450. ' procedure DoIt;',
  13451. ' class procedure Glob;',
  13452. ' end;',
  13453. ' procedure DoIt(b: TBird);',
  13454. ' end;',
  13455. 'procedure TPoint.TBird.DoIt;',
  13456. 'begin',
  13457. ' doit;',
  13458. ' self.doit;',
  13459. ' glob;',
  13460. ' self.glob;',
  13461. 'end;',
  13462. 'class procedure TPoint.TBird.Glob;',
  13463. 'begin',
  13464. ' glob;',
  13465. ' self.glob;',
  13466. 'end;',
  13467. 'procedure TPoint.DoIt(b: TBird);',
  13468. 'begin',
  13469. ' b.doit;',
  13470. ' b.glob;',
  13471. ' TBird.glob;',
  13472. 'end;',
  13473. 'begin',
  13474. '']);
  13475. ConvertProgram;
  13476. CheckSource('TestAdvRecord_SubClass',
  13477. LinesToStr([ // statements
  13478. 'rtl.createClass(this, "TObject", null, function () {',
  13479. ' this.$init = function () {',
  13480. ' };',
  13481. ' this.$final = function () {',
  13482. ' };',
  13483. '});',
  13484. 'rtl.recNewT(this, "TPoint", function () {',
  13485. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13486. ' this.DoIt = function () {',
  13487. ' this.DoIt();',
  13488. ' this.DoIt();',
  13489. ' this.$class.Glob();',
  13490. ' this.$class.Glob();',
  13491. ' };',
  13492. ' this.Glob = function () {',
  13493. ' this.Glob();',
  13494. ' this.Glob();',
  13495. ' };',
  13496. ' }, "TPoint.TBird");',
  13497. ' this.$eq = function (b) {',
  13498. ' return true;',
  13499. ' };',
  13500. ' this.$assign = function (s) {',
  13501. ' return this;',
  13502. ' };',
  13503. ' this.DoIt = function (b) {',
  13504. ' b.DoIt();',
  13505. ' b.$class.Glob();',
  13506. ' this.TBird.Glob();',
  13507. ' };',
  13508. '}, true);',
  13509. '']),
  13510. LinesToStr([ // $mod.$main
  13511. '']));
  13512. end;
  13513. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13514. begin
  13515. StartProgram(false);
  13516. Add([
  13517. '{$modeswitch AdvancedRecords}',
  13518. 'type',
  13519. ' IUnknown = interface end;',
  13520. ' TPoint = record',
  13521. ' type IBird = interface end;',
  13522. ' end;',
  13523. 'begin',
  13524. '']);
  13525. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13526. nNotYetImplemented);
  13527. ParseProgram;
  13528. end;
  13529. procedure TTestModule.TestAdvRecord_Constructor;
  13530. begin
  13531. StartProgram(false);
  13532. Add([
  13533. '{$modeswitch AdvancedRecords}',
  13534. 'type',
  13535. ' TPoint = record',
  13536. ' x,y: longint;',
  13537. ' class procedure Run(w: longint = 13); static;',
  13538. ' constructor Create(ax: longint; ay: longint = -1);',
  13539. ' end;',
  13540. 'class procedure tpoint.run(w: longint);',
  13541. 'begin',
  13542. ' run;',
  13543. ' run();',
  13544. 'end;',
  13545. 'constructor tpoint.create(ax,ay: longint);',
  13546. 'begin',
  13547. ' x:=ax;',
  13548. ' self.y:=ay;',
  13549. ' run;',
  13550. ' run(ax);',
  13551. 'end;',
  13552. 'var r: TPoint;',
  13553. 'begin',
  13554. ' r:=TPoint.Create(1,2);',
  13555. ' with TPoint do r:=Create(1,2);',
  13556. ' r.Create(3);',
  13557. ' r:=r.Create(4);',
  13558. '']);
  13559. ConvertProgram;
  13560. CheckSource('TestAdvRecord_Constructor',
  13561. LinesToStr([ // statements
  13562. 'rtl.recNewT(this, "TPoint", function () {',
  13563. ' this.x = 0;',
  13564. ' this.y = 0;',
  13565. ' this.$eq = function (b) {',
  13566. ' return (this.x === b.x) && (this.y === b.y);',
  13567. ' };',
  13568. ' this.$assign = function (s) {',
  13569. ' this.x = s.x;',
  13570. ' this.y = s.y;',
  13571. ' return this;',
  13572. ' };',
  13573. ' this.Run = function (w) {',
  13574. ' $mod.TPoint.Run(13);',
  13575. ' $mod.TPoint.Run(13);',
  13576. ' };',
  13577. ' this.Create = function (ax, ay) {',
  13578. ' this.x = ax;',
  13579. ' this.y = ay;',
  13580. ' this.Run(13);',
  13581. ' this.Run(ax);',
  13582. ' return this;',
  13583. ' };',
  13584. '});',
  13585. 'this.r = this.TPoint.$new();',
  13586. '']),
  13587. LinesToStr([ // $mod.$main
  13588. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13589. 'var $with = $mod.TPoint;',
  13590. '$mod.r.$assign($with.$new().Create(1, 2));',
  13591. '$mod.r.Create(3, -1);',
  13592. '$mod.r.$assign($mod.r.Create(4, -1));',
  13593. '']));
  13594. end;
  13595. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13596. begin
  13597. StartProgram(false);
  13598. Add([
  13599. '{$modeswitch AdvancedRecords}',
  13600. 'type',
  13601. ' TPoint = record',
  13602. ' class var x: longint;',
  13603. ' class procedure Fly; static;',
  13604. ' class constructor Init;',
  13605. ' end;',
  13606. 'var count: word;',
  13607. 'class procedure Tpoint.Fly;',
  13608. 'begin',
  13609. 'end;',
  13610. 'class constructor tpoint.init;',
  13611. 'begin',
  13612. ' count:=count+1;',
  13613. ' x:=x+3;',
  13614. ' tpoint.x:=tpoint.x+4;',
  13615. ' fly;',
  13616. ' tpoint.fly;',
  13617. 'end;',
  13618. 'var r: TPoint;',
  13619. 'begin',
  13620. ' r.x:=r.x+10;',
  13621. ' r.Fly;',
  13622. ' r.Fly();',
  13623. '']);
  13624. ConvertProgram;
  13625. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13626. LinesToStr([ // statements
  13627. 'rtl.recNewT(this, "TPoint", function () {',
  13628. ' this.x = 0;',
  13629. ' this.$eq = function (b) {',
  13630. ' return true;',
  13631. ' };',
  13632. ' this.$assign = function (s) {',
  13633. ' return this;',
  13634. ' };',
  13635. ' this.Fly = function () {',
  13636. ' };',
  13637. '}, true);',
  13638. 'this.count = 0;',
  13639. 'this.r = this.TPoint.$new();',
  13640. '']),
  13641. LinesToStr([ // $mod.$main
  13642. '(function () {',
  13643. ' $mod.count = $mod.count + 1;',
  13644. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13645. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13646. ' $mod.TPoint.Fly();',
  13647. ' $mod.TPoint.Fly();',
  13648. '})();',
  13649. '$mod.TPoint.x = $mod.r.x + 10;',
  13650. '$mod.TPoint.Fly();',
  13651. '$mod.TPoint.Fly();',
  13652. '']));
  13653. end;
  13654. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13655. begin
  13656. StartUnit(false);
  13657. Add([
  13658. 'interface',
  13659. '{$modeswitch AdvancedRecords}',
  13660. 'type',
  13661. ' TPoint = record',
  13662. ' class var x: longint;',
  13663. ' class procedure Fly; static;',
  13664. ' class constructor Init;',
  13665. ' end;',
  13666. 'implementation',
  13667. 'var count: word;',
  13668. 'class procedure Tpoint.Fly;',
  13669. 'begin',
  13670. 'end;',
  13671. 'class constructor tpoint.init;',
  13672. 'begin',
  13673. ' count:=count+1;',
  13674. ' x:=3;',
  13675. ' tpoint.x:=4;',
  13676. ' fly;',
  13677. ' tpoint.fly;',
  13678. 'end;',
  13679. '']);
  13680. ConvertUnit;
  13681. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13682. LinesToStr([ // statements
  13683. 'var $impl = $mod.$impl;',
  13684. 'rtl.recNewT(this, "TPoint", function () {',
  13685. ' this.x = 0;',
  13686. ' this.$eq = function (b) {',
  13687. ' return true;',
  13688. ' };',
  13689. ' this.$assign = function (s) {',
  13690. ' return this;',
  13691. ' };',
  13692. ' this.Fly = function () {',
  13693. ' };',
  13694. '}, true);',
  13695. '']),
  13696. LinesToStr([ // $mod.$init
  13697. '(function () {',
  13698. ' $impl.count = $impl.count + 1;',
  13699. ' $mod.TPoint.x = 3;',
  13700. ' $mod.TPoint.x = 4;',
  13701. ' $mod.TPoint.Fly();',
  13702. ' $mod.TPoint.Fly();',
  13703. '})();',
  13704. '']),
  13705. LinesToStr([ // $mod.$main
  13706. '$impl.count = 0;',
  13707. '']));
  13708. end;
  13709. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13710. begin
  13711. StartProgram(false);
  13712. Add(['type',
  13713. ' TObject = class',
  13714. ' public',
  13715. ' constructor Create;',
  13716. ' destructor Destroy;',
  13717. ' end;',
  13718. ' TBird = TObject;',
  13719. 'constructor tobject.create;',
  13720. 'begin end;',
  13721. 'destructor tobject.destroy;',
  13722. 'begin end;',
  13723. 'var Obj: tobject;',
  13724. 'begin',
  13725. ' obj:=tobject.create;',
  13726. ' obj:=tobject.create();',
  13727. ' obj:=tbird.create;',
  13728. ' obj:=tbird.create();',
  13729. ' obj:=obj.create();',
  13730. ' obj.destroy;',
  13731. '']);
  13732. ConvertProgram;
  13733. CheckSource('TestClass_TObjectDefaultConstructor',
  13734. LinesToStr([ // statements
  13735. 'rtl.createClass(this,"TObject",null,function(){',
  13736. ' this.$init = function () {',
  13737. ' };',
  13738. ' this.$final = function () {',
  13739. ' };',
  13740. ' this.Create = function(){',
  13741. ' return this;',
  13742. ' };',
  13743. ' this.Destroy = function(){',
  13744. ' };',
  13745. '});',
  13746. 'this.Obj = null;'
  13747. ]),
  13748. LinesToStr([ // $mod.$main
  13749. '$mod.Obj = $mod.TObject.$create("Create");',
  13750. '$mod.Obj = $mod.TObject.$create("Create");',
  13751. '$mod.Obj = $mod.TObject.$create("Create");',
  13752. '$mod.Obj = $mod.TObject.$create("Create");',
  13753. '$mod.Obj = $mod.Obj.Create();',
  13754. '$mod.Obj.$destroy("Destroy");',
  13755. '']));
  13756. end;
  13757. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13758. begin
  13759. StartProgram(false);
  13760. Add('type');
  13761. Add(' TObject = class');
  13762. Add(' public');
  13763. Add(' constructor Create(Par: longint);');
  13764. Add(' end;');
  13765. Add('constructor tobject.create(par: longint);');
  13766. Add('begin end;');
  13767. Add('var Obj: tobject;');
  13768. Add('begin');
  13769. Add(' obj:=tobject.create(3);');
  13770. ConvertProgram;
  13771. CheckSource('TestClass_TObjectConstructorWithParams',
  13772. LinesToStr([ // statements
  13773. 'rtl.createClass(this,"TObject",null,function(){',
  13774. ' this.$init = function () {',
  13775. ' };',
  13776. ' this.$final = function () {',
  13777. ' };',
  13778. ' this.Create = function(Par){',
  13779. ' return this;',
  13780. ' };',
  13781. '});',
  13782. 'this.Obj = null;'
  13783. ]),
  13784. LinesToStr([ // $mod.$main
  13785. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13786. ]));
  13787. end;
  13788. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13789. begin
  13790. StartProgram(false);
  13791. Add('type');
  13792. Add(' TObject = class');
  13793. Add(' public');
  13794. Add(' constructor Create;');
  13795. Add(' end;');
  13796. Add(' TTest = class(TObject)');
  13797. Add(' public');
  13798. Add(' constructor Create(const Par: longint = 1);');
  13799. Add(' end;');
  13800. Add('constructor tobject.create;');
  13801. Add('begin end;');
  13802. Add('constructor ttest.create(const par: longint);');
  13803. Add('begin end;');
  13804. Add('var t: ttest;');
  13805. Add('begin');
  13806. Add(' t:=ttest.create;');
  13807. Add(' t:=ttest.create(2);');
  13808. ConvertProgram;
  13809. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13810. LinesToStr([ // statements
  13811. 'rtl.createClass(this,"TObject",null,function(){',
  13812. ' this.$init = function () {',
  13813. ' };',
  13814. ' this.$final = function () {',
  13815. ' };',
  13816. ' this.Create = function(){',
  13817. ' return this;',
  13818. ' };',
  13819. '});',
  13820. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13821. ' this.Create$1 = function (Par) {',
  13822. ' return this;',
  13823. ' };',
  13824. '});',
  13825. 'this.t = null;'
  13826. ]),
  13827. LinesToStr([ // $mod.$main
  13828. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13829. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13830. ]));
  13831. end;
  13832. procedure TTestModule.TestClass_Var;
  13833. begin
  13834. StartProgram(false);
  13835. Add([
  13836. 'type',
  13837. ' TObject = class',
  13838. ' public',
  13839. ' vI: longint;',
  13840. ' constructor Create(Par: longint);',
  13841. ' end;',
  13842. 'constructor tobject.create(par: longint);',
  13843. 'begin',
  13844. ' vi:=par+3',
  13845. 'end;',
  13846. 'var Obj: tobject;',
  13847. 'begin',
  13848. ' obj:=tobject.create(4);',
  13849. ' obj.vi:=obj.VI+5;']);
  13850. ConvertProgram;
  13851. CheckSource('TestClass_Var',
  13852. LinesToStr([ // statements
  13853. 'rtl.createClass(this,"TObject",null,function(){',
  13854. ' this.$init = function () {',
  13855. ' this.vI = 0;',
  13856. ' };',
  13857. ' this.$final = function () {',
  13858. ' };',
  13859. ' this.Create = function(Par){',
  13860. ' this.vI = Par+3;',
  13861. ' return this;',
  13862. ' };',
  13863. '});',
  13864. 'this.Obj = null;'
  13865. ]),
  13866. LinesToStr([ // $mod.$main
  13867. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13868. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13869. ]));
  13870. end;
  13871. procedure TTestModule.TestClass_Method;
  13872. begin
  13873. StartProgram(false);
  13874. Add('type');
  13875. Add(' TObject = class');
  13876. Add(' public');
  13877. Add(' vI: longint;');
  13878. Add(' Sub: TObject;');
  13879. Add(' constructor Create;');
  13880. Add(' function GetIt(Par: longint): tobject;');
  13881. Add(' end;');
  13882. Add('constructor tobject.create; begin end;');
  13883. Add('function tobject.getit(par: longint): tobject;');
  13884. Add('begin');
  13885. Add(' Self.vi:=par+3;');
  13886. Add(' Result:=self.sub;');
  13887. Add('end;');
  13888. Add('var Obj: tobject;');
  13889. Add('begin');
  13890. Add(' obj:=tobject.create;');
  13891. Add(' obj.getit(4);');
  13892. Add(' obj.sub.sub:=nil;');
  13893. Add(' obj.sub.getit(5);');
  13894. Add(' obj.sub.getit(6).SUB:=nil;');
  13895. Add(' obj.sub.getit(7).GETIT(8);');
  13896. Add(' obj.sub.getit(9).SuB.getit(10);');
  13897. ConvertProgram;
  13898. CheckSource('TestClass_Method',
  13899. LinesToStr([ // statements
  13900. 'rtl.createClass(this,"TObject",null,function(){',
  13901. ' this.$init = function () {',
  13902. ' this.vI = 0;',
  13903. ' this.Sub = null;',
  13904. ' };',
  13905. ' this.$final = function () {',
  13906. ' this.Sub = undefined;',
  13907. ' };',
  13908. ' this.Create = function(){',
  13909. ' return this;',
  13910. ' };',
  13911. ' this.GetIt = function(Par){',
  13912. ' var Result = null;',
  13913. ' this.vI = Par + 3;',
  13914. ' Result = this.Sub;',
  13915. ' return Result;',
  13916. ' };',
  13917. '});',
  13918. 'this.Obj = null;'
  13919. ]),
  13920. LinesToStr([ // $mod.$main
  13921. '$mod.Obj = $mod.TObject.$create("Create");',
  13922. '$mod.Obj.GetIt(4);',
  13923. '$mod.Obj.Sub.Sub=null;',
  13924. '$mod.Obj.Sub.GetIt(5);',
  13925. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13926. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13927. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13928. ]));
  13929. end;
  13930. procedure TTestModule.TestClass_Implementation;
  13931. begin
  13932. StartUnit(false);
  13933. Add([
  13934. 'interface',
  13935. 'type',
  13936. ' TObject = class',
  13937. ' constructor Create;',
  13938. ' end;',
  13939. 'implementation',
  13940. 'type',
  13941. ' TIntClass = class',
  13942. ' constructor Create; reintroduce;',
  13943. ' class procedure DoGlob;',
  13944. ' end;',
  13945. 'constructor tintclass.create;',
  13946. 'begin',
  13947. ' inherited;',
  13948. ' inherited create;',
  13949. ' doglob;',
  13950. 'end;',
  13951. 'class procedure tintclass.doglob;',
  13952. 'begin',
  13953. 'end;',
  13954. 'constructor tobject.create;',
  13955. 'var',
  13956. ' iC: tintclass;',
  13957. 'begin',
  13958. ' ic:=tintclass.create;',
  13959. ' tintclass.doglob;',
  13960. ' ic.doglob;',
  13961. 'end;',
  13962. 'initialization',
  13963. ' tintclass.doglob;',
  13964. '']);
  13965. ConvertUnit;
  13966. CheckSource('TestClass_Implementation',
  13967. LinesToStr([ // statements
  13968. 'var $impl = $mod.$impl;',
  13969. 'rtl.createClass(this, "TObject", null, function () {',
  13970. ' this.$init = function () {',
  13971. ' };',
  13972. ' this.$final = function () {',
  13973. ' };',
  13974. ' this.Create = function () {',
  13975. ' var iC = null;',
  13976. ' iC = $impl.TIntClass.$create("Create$1");',
  13977. ' $impl.TIntClass.DoGlob();',
  13978. ' iC.$class.DoGlob();',
  13979. ' return this;',
  13980. ' };',
  13981. '});',
  13982. '']),
  13983. LinesToStr([ // $mod.$main
  13984. '$impl.TIntClass.DoGlob();',
  13985. '']),
  13986. LinesToStr([
  13987. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13988. ' this.Create$1 = function () {',
  13989. ' $mod.TObject.Create.call(this);',
  13990. ' $mod.TObject.Create.call(this);',
  13991. ' this.$class.DoGlob();',
  13992. ' return this;',
  13993. ' };',
  13994. ' this.DoGlob = function () {',
  13995. ' };',
  13996. '});',
  13997. '']));
  13998. end;
  13999. procedure TTestModule.TestClass_Inheritance;
  14000. begin
  14001. StartProgram(false);
  14002. Add('type');
  14003. Add(' TObject = class');
  14004. Add(' public');
  14005. Add(' constructor Create;');
  14006. Add(' end;');
  14007. Add(' TClassA = class');
  14008. Add(' end;');
  14009. Add(' TClassB = class(TObject)');
  14010. Add(' procedure ProcB;');
  14011. Add(' end;');
  14012. Add('constructor tobject.create; begin end;');
  14013. Add('procedure tclassb.procb; begin end;');
  14014. Add('var');
  14015. Add(' oO: TObject;');
  14016. Add(' oA: TClassA;');
  14017. Add(' oB: TClassB;');
  14018. Add('begin');
  14019. Add(' oO:=tobject.Create;');
  14020. Add(' oA:=tclassa.Create;');
  14021. Add(' ob:=tclassb.Create;');
  14022. Add(' if oo is tclassa then ;');
  14023. Add(' ob:=oo as tclassb;');
  14024. Add(' (oo as tclassb).procb;');
  14025. ConvertProgram;
  14026. CheckSource('TestClass_Inheritance',
  14027. LinesToStr([ // statements
  14028. 'rtl.createClass(this,"TObject",null,function(){',
  14029. ' this.$init = function () {',
  14030. ' };',
  14031. ' this.$final = function () {',
  14032. ' };',
  14033. ' this.Create = function () {',
  14034. ' return this;',
  14035. ' };',
  14036. '});',
  14037. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14038. '});',
  14039. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14040. ' this.ProcB = function () {',
  14041. ' };',
  14042. '});',
  14043. 'this.oO = null;',
  14044. 'this.oA = null;',
  14045. 'this.oB = null;'
  14046. ]),
  14047. LinesToStr([ // $mod.$main
  14048. '$mod.oO = $mod.TObject.$create("Create");',
  14049. '$mod.oA = $mod.TClassA.$create("Create");',
  14050. '$mod.oB = $mod.TClassB.$create("Create");',
  14051. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14052. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14053. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14054. ]));
  14055. end;
  14056. procedure TTestModule.TestClass_TypeAlias;
  14057. begin
  14058. StartProgram(false);
  14059. Add([
  14060. '{$interfaces corba}',
  14061. 'type',
  14062. ' IObject = interface',
  14063. ' end;',
  14064. ' IBird = type IObject;',
  14065. ' TObject = class',
  14066. ' end;',
  14067. ' TBird = type TObject;',
  14068. 'var',
  14069. ' oObj: TObject;',
  14070. ' oBird: TBird;',
  14071. ' IntfObj: IObject;',
  14072. ' IntfBird: IBird;',
  14073. 'begin',
  14074. ' oObj:=oBird;',
  14075. '']);
  14076. ConvertProgram;
  14077. CheckSource('TestClass_TypeAlias',
  14078. LinesToStr([ // statements
  14079. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14080. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14081. 'rtl.createClass(this, "TObject", null, function () {',
  14082. ' this.$init = function () {',
  14083. ' };',
  14084. ' this.$final = function () {',
  14085. ' };',
  14086. '});',
  14087. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14088. '});',
  14089. 'this.oObj = null;',
  14090. 'this.oBird = null;',
  14091. 'this.IntfObj = null;',
  14092. 'this.IntfBird = null;',
  14093. '']),
  14094. LinesToStr([ // $mod.$main
  14095. '$mod.oObj = $mod.oBird;',
  14096. '']));
  14097. end;
  14098. procedure TTestModule.TestClass_AbstractMethod;
  14099. begin
  14100. StartProgram(false);
  14101. Add('type');
  14102. Add(' TObject = class');
  14103. Add(' public');
  14104. Add(' procedure DoIt; virtual; abstract;');
  14105. Add(' end;');
  14106. Add('begin');
  14107. ConvertProgram;
  14108. CheckSource('TestClass_AbstractMethod',
  14109. LinesToStr([ // statements
  14110. 'rtl.createClass(this,"TObject",null,function(){',
  14111. ' this.$init = function () {',
  14112. ' };',
  14113. ' this.$final = function () {',
  14114. ' };',
  14115. '});'
  14116. ]),
  14117. LinesToStr([ // this.$main
  14118. ''
  14119. ]));
  14120. end;
  14121. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14122. begin
  14123. StartProgram(false);
  14124. Add([
  14125. 'type',
  14126. ' TObject = class',
  14127. ' procedure DoAbstract; virtual; abstract;',
  14128. ' procedure DoVirtual; virtual;',
  14129. ' procedure DoIt;',
  14130. ' end;',
  14131. ' TA = class',
  14132. ' procedure doabstract; override;',
  14133. ' procedure dovirtual; override;',
  14134. ' procedure DoSome;',
  14135. ' end;',
  14136. 'procedure tobject.dovirtual;',
  14137. 'begin',
  14138. ' inherited; // call non existing ancestor -> ignore silently',
  14139. 'end;',
  14140. 'procedure tobject.doit;',
  14141. 'begin',
  14142. 'end;',
  14143. 'procedure ta.doabstract;',
  14144. 'begin',
  14145. ' inherited dovirtual; // call TObject.DoVirtual',
  14146. 'end;',
  14147. 'procedure ta.dovirtual;',
  14148. 'begin',
  14149. ' inherited; // call TObject.DoVirtual',
  14150. ' inherited dovirtual; // call TObject.DoVirtual',
  14151. ' inherited dovirtual(); // call TObject.DoVirtual',
  14152. ' doit;',
  14153. ' doit();',
  14154. 'end;',
  14155. 'procedure ta.dosome;',
  14156. 'begin',
  14157. ' inherited; // call non existing ancestor method -> silently ignore',
  14158. 'end;',
  14159. 'begin']);
  14160. ConvertProgram;
  14161. CheckSource('TestClass_CallInherited_ProcNoParams',
  14162. LinesToStr([ // statements
  14163. 'rtl.createClass(this,"TObject",null,function(){',
  14164. ' this.$init = function () {',
  14165. ' };',
  14166. ' this.$final = function () {',
  14167. ' };',
  14168. ' this.DoVirtual = function () {',
  14169. ' };',
  14170. ' this.DoIt = function () {',
  14171. ' };',
  14172. '});',
  14173. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14174. ' this.DoAbstract = function () {',
  14175. ' $mod.TObject.DoVirtual.call(this);',
  14176. ' };',
  14177. ' this.DoVirtual = function () {',
  14178. ' $mod.TObject.DoVirtual.call(this);',
  14179. ' $mod.TObject.DoVirtual.call(this);',
  14180. ' $mod.TObject.DoVirtual.call(this);',
  14181. ' this.DoIt();',
  14182. ' this.DoIt();',
  14183. ' };',
  14184. ' this.DoSome = function () {',
  14185. ' };',
  14186. '});'
  14187. ]),
  14188. LinesToStr([ // this.$main
  14189. ''
  14190. ]));
  14191. end;
  14192. procedure TTestModule.TestClass_CallInherited_WithParams;
  14193. begin
  14194. StartProgram(false);
  14195. Add([
  14196. 'type',
  14197. ' TObject = class',
  14198. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14199. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14200. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14201. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14202. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14203. ' end;',
  14204. ' TClassA = class',
  14205. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14206. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14207. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14208. ' end;',
  14209. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14210. 'begin',
  14211. 'end;',
  14212. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14213. 'begin',
  14214. 'end;',
  14215. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14216. 'begin',
  14217. 'end;',
  14218. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14219. 'begin',
  14220. 'end;',
  14221. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14222. 'begin',
  14223. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14224. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14225. 'end;',
  14226. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14227. 'begin',
  14228. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14229. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14230. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14231. ' doit(pa,pb);',
  14232. ' doit(pa);',
  14233. ' doit2(pa);',
  14234. ' doit2;',
  14235. 'end;',
  14236. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14237. 'begin',
  14238. ' pa:=inherited;',
  14239. 'end;',
  14240. 'begin']);
  14241. ConvertProgram;
  14242. CheckSource('TestClass_CallInherited_WithParams',
  14243. LinesToStr([ // statements
  14244. 'rtl.createClass(this,"TObject",null,function(){',
  14245. ' this.$init = function () {',
  14246. ' };',
  14247. ' this.$final = function () {',
  14248. ' };',
  14249. ' this.DoVirtual = function (pA,pB) {',
  14250. ' };',
  14251. ' this.DoIt = function (pA,pB) {',
  14252. ' };',
  14253. ' this.DoIt2 = function (pA,pB) {',
  14254. ' };',
  14255. ' this.GetIt = function (pA, pB) {',
  14256. ' var Result = 0;',
  14257. ' return Result;',
  14258. ' };',
  14259. '});',
  14260. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14261. ' this.DoAbstract = function (pA,pB) {',
  14262. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14263. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14264. ' };',
  14265. ' this.DoVirtual = function (pA,pB) {',
  14266. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14267. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14268. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14269. ' this.DoIt(pA,pB);',
  14270. ' this.DoIt(pA,0);',
  14271. ' this.DoIt2(pA,2);',
  14272. ' this.DoIt2(1,2);',
  14273. ' };',
  14274. ' this.GetIt$1 = function (pA, pB) {',
  14275. ' var Result = 0;',
  14276. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14277. ' return Result;',
  14278. ' };',
  14279. '});'
  14280. ]),
  14281. LinesToStr([ // this.$main
  14282. ''
  14283. ]));
  14284. end;
  14285. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14286. begin
  14287. StartProgram(false);
  14288. Add('type');
  14289. Add(' TObject = class');
  14290. Add(' constructor Create; virtual;');
  14291. Add(' constructor CreateWithB(b: boolean);');
  14292. Add(' end;');
  14293. Add(' TA = class');
  14294. Add(' constructor Create; override;');
  14295. Add(' constructor CreateWithC(c: char);');
  14296. Add(' procedure DoIt;');
  14297. Add(' class function DoSome: TObject;');
  14298. Add(' end;');
  14299. Add('constructor tobject.create;');
  14300. Add('begin');
  14301. Add(' inherited; // call non existing ancestor -> ignore silently');
  14302. Add('end;');
  14303. Add('constructor tobject.createwithb(b: boolean);');
  14304. Add('begin');
  14305. Add(' inherited; // call non existing ancestor -> ignore silently');
  14306. Add(' create; // normal call');
  14307. Add('end;');
  14308. Add('constructor ta.create;');
  14309. Add('begin');
  14310. Add(' inherited; // normal call TObject.Create');
  14311. Add(' inherited create; // normal call TObject.Create');
  14312. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14313. Add('end;');
  14314. Add('constructor ta.createwithc(c: char);');
  14315. Add('begin');
  14316. Add(' inherited create; // call TObject.Create');
  14317. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14318. Add(' doit;');
  14319. Add(' doit();');
  14320. Add(' dosome;');
  14321. Add('end;');
  14322. Add('procedure ta.doit;');
  14323. Add('begin');
  14324. Add(' create; // normal call');
  14325. Add(' createwithb(false); // normal call');
  14326. Add(' createwithc(''c''); // normal call');
  14327. Add('end;');
  14328. Add('class function ta.dosome: TObject;');
  14329. Add('begin');
  14330. Add(' Result:=create; // constructor');
  14331. Add(' Result:=createwithb(true); // constructor');
  14332. Add(' Result:=createwithc(''c''); // constructor');
  14333. Add('end;');
  14334. Add('begin');
  14335. ConvertProgram;
  14336. CheckSource('TestClass_CallInheritedConstructor',
  14337. LinesToStr([ // statements
  14338. 'rtl.createClass(this,"TObject",null,function(){',
  14339. ' this.$init = function () {',
  14340. ' };',
  14341. ' this.$final = function () {',
  14342. ' };',
  14343. ' this.Create = function () {',
  14344. ' return this;',
  14345. ' };',
  14346. ' this.CreateWithB = function (b) {',
  14347. ' this.Create();',
  14348. ' return this;',
  14349. ' };',
  14350. '});',
  14351. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14352. ' this.Create = function () {',
  14353. ' $mod.TObject.Create.call(this);',
  14354. ' $mod.TObject.Create.call(this);',
  14355. ' $mod.TObject.CreateWithB.call(this, false);',
  14356. ' return this;',
  14357. ' };',
  14358. ' this.CreateWithC = function (c) {',
  14359. ' $mod.TObject.Create.call(this);',
  14360. ' $mod.TObject.CreateWithB.call(this, true);',
  14361. ' this.DoIt();',
  14362. ' this.DoIt();',
  14363. ' this.$class.DoSome();',
  14364. ' return this;',
  14365. ' };',
  14366. ' this.DoIt = function () {',
  14367. ' this.Create();',
  14368. ' this.CreateWithB(false);',
  14369. ' this.CreateWithC("c");',
  14370. ' };',
  14371. ' this.DoSome = function () {',
  14372. ' var Result = null;',
  14373. ' Result = this.$create("Create");',
  14374. ' Result = this.$create("CreateWithB", [true]);',
  14375. ' Result = this.$create("CreateWithC", ["c"]);',
  14376. ' return Result;',
  14377. ' };',
  14378. '});'
  14379. ]),
  14380. LinesToStr([ // this.$main
  14381. ''
  14382. ]));
  14383. end;
  14384. procedure TTestModule.TestClass_ClassVar_Assign;
  14385. begin
  14386. StartProgram(false);
  14387. Add([
  14388. 'type',
  14389. ' TObject = class',
  14390. ' public',
  14391. ' class var vI: longint;',
  14392. ' class var Sub: TObject;',
  14393. ' constructor Create;',
  14394. ' class function GetIt(var Par: longint): tobject;',
  14395. ' end;',
  14396. 'constructor tobject.create;',
  14397. 'begin',
  14398. ' vi:=vi+1;',
  14399. ' Self.vi:=Self.vi+1;',
  14400. ' inc(vi);',
  14401. 'end;',
  14402. 'class function tobject.getit(var par: longint): tobject;',
  14403. 'begin',
  14404. ' vi:=vi+3;',
  14405. ' Self.vi:=Self.vi+4;',
  14406. ' inc(vi);',
  14407. ' Result:=self.sub;',
  14408. ' GetIt(vi);',
  14409. 'end;',
  14410. 'var Obj: tobject;',
  14411. 'begin',
  14412. ' obj:=tobject.create;',
  14413. ' tobject.vi:=3;',
  14414. ' if tobject.vi=4 then ;',
  14415. ' tobject.sub:=nil;',
  14416. ' obj.sub:=nil;',
  14417. ' obj.sub.sub:=nil;']);
  14418. ConvertProgram;
  14419. CheckSource('TestClass_ClassVar_Assign',
  14420. LinesToStr([ // statements
  14421. 'rtl.createClass(this,"TObject",null,function(){',
  14422. ' this.vI = 0;',
  14423. ' this.Sub = null;',
  14424. ' this.$init = function () {',
  14425. ' };',
  14426. ' this.$final = function () {',
  14427. ' };',
  14428. ' this.Create = function(){',
  14429. ' $mod.TObject.vI = this.vI+1;',
  14430. ' $mod.TObject.vI = this.vI+1;',
  14431. ' $mod.TObject.vI += 1;',
  14432. ' return this;',
  14433. ' };',
  14434. ' this.GetIt = function(Par){',
  14435. ' var Result = null;',
  14436. ' $mod.TObject.vI = this.vI + 3;',
  14437. ' $mod.TObject.vI = this.vI + 4;',
  14438. ' $mod.TObject.vI += 1;',
  14439. ' Result = this.Sub;',
  14440. ' this.GetIt({',
  14441. ' p: $mod.TObject,',
  14442. ' get: function () {',
  14443. ' return this.p.vI;',
  14444. ' },',
  14445. ' set: function (v) {',
  14446. ' this.p.vI = v;',
  14447. ' }',
  14448. ' });',
  14449. ' return Result;',
  14450. ' };',
  14451. '});',
  14452. 'this.Obj = null;'
  14453. ]),
  14454. LinesToStr([ // $mod.$main
  14455. '$mod.Obj = $mod.TObject.$create("Create");',
  14456. '$mod.TObject.vI = 3;',
  14457. 'if ($mod.TObject.vI === 4);',
  14458. '$mod.TObject.Sub=null;',
  14459. '$mod.TObject.Sub=null;',
  14460. '$mod.TObject.Sub=null;',
  14461. '']));
  14462. end;
  14463. procedure TTestModule.TestClass_CallClassMethod;
  14464. begin
  14465. StartProgram(false);
  14466. Add('type');
  14467. Add(' TObject = class');
  14468. Add(' public');
  14469. Add(' class var vI: longint;');
  14470. Add(' class var Sub: TObject;');
  14471. Add(' constructor Create;');
  14472. Add(' function GetMore(Par: longint): longint;');
  14473. Add(' class function GetIt(Par: longint): tobject;');
  14474. Add(' end;');
  14475. Add('constructor tobject.create;');
  14476. Add('begin');
  14477. Add(' sub:=getit(3);');
  14478. Add(' vi:=getmore(4);');
  14479. Add(' sub:=Self.getit(5);');
  14480. Add(' vi:=Self.getmore(6);');
  14481. Add('end;');
  14482. Add('function tobject.getmore(par: longint): longint;');
  14483. Add('begin');
  14484. Add(' sub:=getit(11);');
  14485. Add(' vi:=getmore(12);');
  14486. Add(' sub:=self.getit(13);');
  14487. Add(' vi:=self.getmore(14);');
  14488. Add('end;');
  14489. Add('class function tobject.getit(par: longint): tobject;');
  14490. Add('begin');
  14491. Add(' sub:=getit(21);');
  14492. Add(' vi:=sub.getmore(22);');
  14493. Add(' sub:=self.getit(23);');
  14494. Add(' vi:=self.sub.getmore(24);');
  14495. Add('end;');
  14496. Add('var Obj: tobject;');
  14497. Add('begin');
  14498. Add(' obj:=tobject.create;');
  14499. Add(' tobject.getit(5);');
  14500. Add(' obj.getit(6);');
  14501. Add(' obj.sub.getit(7);');
  14502. Add(' obj.sub.getit(8).SUB:=nil;');
  14503. Add(' obj.sub.getit(9).GETIT(10);');
  14504. Add(' obj.sub.getit(11).SuB.getit(12);');
  14505. ConvertProgram;
  14506. CheckSource('TestClass_CallClassMethod',
  14507. LinesToStr([ // statements
  14508. 'rtl.createClass(this,"TObject",null,function(){',
  14509. ' this.vI = 0;',
  14510. ' this.Sub = null;',
  14511. ' this.$init = function () {',
  14512. ' };',
  14513. ' this.$final = function () {',
  14514. ' };',
  14515. ' this.Create = function(){',
  14516. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14517. ' $mod.TObject.vI = this.GetMore(4);',
  14518. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14519. ' $mod.TObject.vI = this.GetMore(6);',
  14520. ' return this;',
  14521. ' };',
  14522. ' this.GetMore = function(Par){',
  14523. ' var Result = 0;',
  14524. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14525. ' $mod.TObject.vI = this.GetMore(12);',
  14526. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14527. ' $mod.TObject.vI = this.GetMore(14);',
  14528. ' return Result;',
  14529. ' };',
  14530. ' this.GetIt = function(Par){',
  14531. ' var Result = null;',
  14532. ' $mod.TObject.Sub = this.GetIt(21);',
  14533. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14534. ' $mod.TObject.Sub = this.GetIt(23);',
  14535. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14536. ' return Result;',
  14537. ' };',
  14538. '});',
  14539. 'this.Obj = null;'
  14540. ]),
  14541. LinesToStr([ // $mod.$main
  14542. '$mod.Obj = $mod.TObject.$create("Create");',
  14543. '$mod.TObject.GetIt(5);',
  14544. '$mod.Obj.$class.GetIt(6);',
  14545. '$mod.Obj.Sub.$class.GetIt(7);',
  14546. '$mod.TObject.Sub=null;',
  14547. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14548. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14549. '']));
  14550. end;
  14551. procedure TTestModule.TestClass_CallClassMethodStatic;
  14552. begin
  14553. StartProgram(false);
  14554. Add([
  14555. 'type',
  14556. ' TObject = class',
  14557. ' public',
  14558. ' class function Fly: tobject; static;',
  14559. ' end;',
  14560. 'class function tobject.Fly: tobject;',
  14561. 'begin',
  14562. ' Result.Fly;',
  14563. ' Result.Fly();',
  14564. ' Fly;',
  14565. ' Fly();',
  14566. ' Fly.Fly;',
  14567. ' Fly.Fly();',
  14568. 'end;',
  14569. 'var Obj: tobject;',
  14570. 'begin',
  14571. ' obj.Fly;',
  14572. ' obj.Fly();',
  14573. ' with obj do begin',
  14574. ' Fly;',
  14575. ' Fly();',
  14576. ' end;',
  14577. '']);
  14578. ConvertProgram;
  14579. CheckSource('TestClass_CallClassMethodStatic',
  14580. LinesToStr([ // statements
  14581. 'rtl.createClass(this, "TObject", null, function () {',
  14582. ' this.$init = function () {',
  14583. ' };',
  14584. ' this.$final = function () {',
  14585. ' };',
  14586. ' this.Fly = function () {',
  14587. ' var Result = null;',
  14588. ' $mod.TObject.Fly();',
  14589. ' $mod.TObject.Fly();',
  14590. ' $mod.TObject.Fly();',
  14591. ' $mod.TObject.Fly();',
  14592. ' $mod.TObject.Fly();',
  14593. ' $mod.TObject.Fly();',
  14594. ' return Result;',
  14595. ' };',
  14596. '});',
  14597. 'this.Obj = null;'
  14598. ]),
  14599. LinesToStr([ // $mod.$main
  14600. '$mod.TObject.Fly();',
  14601. '$mod.TObject.Fly();',
  14602. 'var $with = $mod.Obj;',
  14603. '$with.Fly();',
  14604. '$with.Fly();',
  14605. '']));
  14606. end;
  14607. procedure TTestModule.TestClass_Property;
  14608. begin
  14609. StartProgram(false);
  14610. Add('type');
  14611. Add(' TObject = class');
  14612. Add(' Fx: longint;');
  14613. Add(' Fy: longint;');
  14614. Add(' function GetInt: longint;');
  14615. Add(' procedure SetInt(Value: longint);');
  14616. Add(' procedure DoIt;');
  14617. Add(' property IntA: longint read Fx write Fy;');
  14618. Add(' property IntB: longint read GetInt write SetInt;');
  14619. Add(' end;');
  14620. Add('function tobject.getint: longint;');
  14621. Add('begin');
  14622. Add(' result:=fx;');
  14623. Add('end;');
  14624. Add('procedure tobject.setint(value: longint);');
  14625. Add('begin');
  14626. Add(' if value=fy then exit;');
  14627. Add(' fy:=value;');
  14628. Add('end;');
  14629. Add('procedure tobject.doit;');
  14630. Add('begin');
  14631. Add(' IntA:=IntA+1;');
  14632. Add(' Self.IntA:=Self.IntA+1;');
  14633. Add(' IntB:=IntB+1;');
  14634. Add(' Self.IntB:=Self.IntB+1;');
  14635. Add('end;');
  14636. Add('var Obj: tobject;');
  14637. Add('begin');
  14638. Add(' obj.inta:=obj.inta+1;');
  14639. Add(' if obj.intb=2 then;');
  14640. Add(' obj.intb:=obj.intb+2;');
  14641. Add(' obj.setint(obj.inta);');
  14642. ConvertProgram;
  14643. CheckSource('TestClass_Property',
  14644. LinesToStr([ // statements
  14645. 'rtl.createClass(this, "TObject", null, function () {',
  14646. ' this.$init = function () {',
  14647. ' this.Fx = 0;',
  14648. ' this.Fy = 0;',
  14649. ' };',
  14650. ' this.$final = function () {',
  14651. ' };',
  14652. ' this.GetInt = function () {',
  14653. ' var Result = 0;',
  14654. ' Result = this.Fx;',
  14655. ' return Result;',
  14656. ' };',
  14657. ' this.SetInt = function (Value) {',
  14658. ' if (Value === this.Fy) return;',
  14659. ' this.Fy = Value;',
  14660. ' };',
  14661. ' this.DoIt = function () {',
  14662. ' this.Fy = this.Fx + 1;',
  14663. ' this.Fy = this.Fx + 1;',
  14664. ' this.SetInt(this.GetInt() + 1);',
  14665. ' this.SetInt(this.GetInt() + 1);',
  14666. ' };',
  14667. '});',
  14668. 'this.Obj = null;'
  14669. ]),
  14670. LinesToStr([ // $mod.$main
  14671. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14672. 'if ($mod.Obj.GetInt() === 2);',
  14673. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14674. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14675. ]));
  14676. end;
  14677. procedure TTestModule.TestClass_Property_ClassMethod;
  14678. begin
  14679. StartProgram(false);
  14680. Add([
  14681. 'type',
  14682. ' TObject = class',
  14683. ' class var Fx: longint;',
  14684. ' class var Fy: longint;',
  14685. ' class function GetInt: longint;',
  14686. ' class procedure SetInt(Value: longint);',
  14687. ' end;',
  14688. ' TBird = class',
  14689. ' class procedure DoIt;',
  14690. ' class property IntA: longint read Fx write Fy;',
  14691. ' class property IntB: longint read GetInt write SetInt;',
  14692. ' end;',
  14693. 'class function tobject.getint: longint;',
  14694. 'begin',
  14695. ' result:=fx;',
  14696. 'end;',
  14697. 'class procedure tobject.setint(value: longint);',
  14698. 'begin',
  14699. 'end;',
  14700. 'class procedure tbird.doit;',
  14701. 'begin',
  14702. ' FX:=3;',
  14703. ' IntA:=IntA+1;',
  14704. ' Self.IntA:=Self.IntA+1;',
  14705. ' IntB:=IntB+1;',
  14706. ' Self.IntB:=Self.IntB+1;',
  14707. ' with Self do begin',
  14708. ' FX:=11;',
  14709. ' IntA:=IntA+12;',
  14710. ' IntB:=IntB+13;',
  14711. ' end;',
  14712. 'end;',
  14713. 'var Obj: tbird;',
  14714. 'begin',
  14715. ' tbird.fx:=tbird.fx+1;',
  14716. ' tbird.inta:=tbird.inta+1;',
  14717. ' if tbird.intb=2 then;',
  14718. ' tbird.intb:=tbird.intb+2;',
  14719. ' tbird.setint(tbird.inta);',
  14720. ' obj.inta:=obj.inta+1;',
  14721. ' if obj.intb=2 then;',
  14722. ' obj.intb:=obj.intb+2;',
  14723. ' obj.setint(obj.inta);',
  14724. ' with Tbird do begin',
  14725. ' FX:=FY+1;',
  14726. ' inta:=inta+2;',
  14727. ' intb:=intb+3;',
  14728. ' end;',
  14729. ' with Obj do begin',
  14730. ' FX:=FY+1;',
  14731. ' inta:=inta+2;',
  14732. ' intb:=intb+3;',
  14733. ' end;',
  14734. '']);
  14735. ConvertProgram;
  14736. CheckSource('TestClass_Property_ClassMethod',
  14737. LinesToStr([ // statements
  14738. 'rtl.createClass(this, "TObject", null, function () {',
  14739. ' this.Fx = 0;',
  14740. ' this.Fy = 0;',
  14741. ' this.$init = function () {',
  14742. ' };',
  14743. ' this.$final = function () {',
  14744. ' };',
  14745. ' this.GetInt = function () {',
  14746. ' var Result = 0;',
  14747. ' Result = this.Fx;',
  14748. ' return Result;',
  14749. ' };',
  14750. ' this.SetInt = function (Value) {',
  14751. ' };',
  14752. '});',
  14753. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14754. ' this.DoIt = function () {',
  14755. ' $mod.TObject.Fx = 3;',
  14756. ' $mod.TObject.Fy = this.Fx + 1;',
  14757. ' $mod.TObject.Fy = this.Fx + 1;',
  14758. ' this.SetInt(this.GetInt() + 1);',
  14759. ' this.SetInt(this.GetInt() + 1);',
  14760. ' $mod.TObject.Fx = 11;',
  14761. ' $mod.TObject.Fy = this.Fx + 12;',
  14762. ' this.SetInt(this.GetInt() + 13);',
  14763. ' };',
  14764. '});',
  14765. 'this.Obj = null;'
  14766. ]),
  14767. LinesToStr([ // $mod.$main
  14768. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14769. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14770. 'if ($mod.TBird.GetInt() === 2);',
  14771. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14772. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14773. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14774. 'if ($mod.Obj.$class.GetInt() === 2);',
  14775. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14776. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14777. 'var $with = $mod.TBird;',
  14778. '$mod.TObject.Fx = $with.Fy + 1;',
  14779. '$mod.TObject.Fy = $with.Fx + 2;',
  14780. '$with.SetInt($with.GetInt() + 3);',
  14781. 'var $with1 = $mod.Obj;',
  14782. '$mod.TObject.Fx = $with1.Fy + 1;',
  14783. '$mod.TObject.Fy = $with1.Fx + 2;',
  14784. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14785. '']));
  14786. end;
  14787. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14788. begin
  14789. StartProgram(false);
  14790. Add([
  14791. 'type',
  14792. ' TObject = class',
  14793. ' class function GetInt: longint; static;',
  14794. ' class procedure SetInt(Value: longint); static;',
  14795. ' class function GetItems(Index: word): longint; static;',
  14796. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14797. ' end;',
  14798. ' TBird = class',
  14799. ' class procedure Fly;',
  14800. ' class property IntA: longint read GetInt write SetInt;',
  14801. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14802. ' end;',
  14803. 'class function tobject.getint: longint;',
  14804. 'begin',
  14805. 'end;',
  14806. 'class procedure tobject.setint(value: longint);',
  14807. 'begin',
  14808. 'end;',
  14809. 'class function tobject.GetItems(Index: word): longint;',
  14810. 'begin',
  14811. 'end;',
  14812. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14813. 'begin',
  14814. 'end;',
  14815. 'class procedure tbird.fly;',
  14816. 'var w: longint;',
  14817. 'begin',
  14818. ' inta:=inta+51;',
  14819. ' w:=items[52];',
  14820. ' items[53]:=54;',
  14821. 'end;',
  14822. 'var Obj: tbird;',
  14823. ' i: longint;',
  14824. 'begin',
  14825. ' tbird.inta:=tbird.inta+1;',
  14826. ' i:=tbird.items[2];',
  14827. ' tbird.items[3]:=4;',
  14828. ' obj.inta:=obj.inta+11;',
  14829. ' i:=obj.items[12];',
  14830. ' obj.items[13]:=14;',
  14831. ' with Tbird do begin',
  14832. ' inta:=inta+21;',
  14833. ' i:=items[22];',
  14834. ' items[23]:=24;',
  14835. ' end;',
  14836. ' with Obj do begin',
  14837. ' inta:=inta+31;',
  14838. ' i:=items[32];',
  14839. ' items[33]:=34;',
  14840. ' end;',
  14841. '']);
  14842. ConvertProgram;
  14843. CheckSource('TestClass_Property_ClassMethod',
  14844. LinesToStr([ // statements
  14845. 'rtl.createClass(this, "TObject", null, function () {',
  14846. ' this.$init = function () {',
  14847. ' };',
  14848. ' this.$final = function () {',
  14849. ' };',
  14850. ' this.GetInt = function () {',
  14851. ' var Result = 0;',
  14852. ' return Result;',
  14853. ' };',
  14854. ' this.SetInt = function (Value) {',
  14855. ' };',
  14856. ' this.GetItems = function (Index) {',
  14857. ' var Result = 0;',
  14858. ' return Result;',
  14859. ' };',
  14860. ' this.SetItems = function (Index, Value) {',
  14861. ' };',
  14862. '});',
  14863. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14864. ' this.Fly = function () {',
  14865. ' var w = 0;',
  14866. ' this.SetInt(this.GetInt() + 51);',
  14867. ' w = this.GetItems(52);',
  14868. ' this.SetItems(53, 54);',
  14869. ' };',
  14870. '});',
  14871. 'this.Obj = null;',
  14872. 'this.i = 0;',
  14873. '']),
  14874. LinesToStr([ // $mod.$main
  14875. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14876. '$mod.i = $mod.TObject.GetItems(2);',
  14877. '$mod.TObject.SetItems(3, 4);',
  14878. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14879. '$mod.i = $mod.TObject.GetItems(12);',
  14880. '$mod.TObject.SetItems(13, 14);',
  14881. 'var $with = $mod.TBird;',
  14882. '$with.SetInt($with.GetInt() + 21);',
  14883. '$mod.i = $with.GetItems(22);',
  14884. '$with.SetItems(23, 24);',
  14885. 'var $with1 = $mod.Obj;',
  14886. '$with1.SetInt($with1.GetInt() + 31);',
  14887. '$mod.i = $with1.GetItems(32);',
  14888. '$with1.SetItems(33, 34);',
  14889. '']));
  14890. end;
  14891. procedure TTestModule.TestClass_Property_Indexed;
  14892. begin
  14893. StartProgram(false);
  14894. Add([
  14895. 'type',
  14896. ' TObject = class',
  14897. ' FItems: array of longint;',
  14898. ' function GetItems(Index: longint): longint;',
  14899. ' procedure SetItems(Index: longint; Value: longint);',
  14900. ' procedure DoIt;',
  14901. ' property Items[Index: longint]: longint read getitems write setitems;',
  14902. ' end;',
  14903. 'function tobject.getitems(index: longint): longint;',
  14904. 'begin',
  14905. ' Result:=fitems[index];',
  14906. 'end;',
  14907. 'procedure tobject.setitems(index: longint; value: longint);',
  14908. 'begin',
  14909. ' fitems[index]:=value;',
  14910. 'end;',
  14911. 'procedure tobject.doit;',
  14912. 'begin',
  14913. ' items[1]:=2;',
  14914. ' items[3]:=items[4];',
  14915. ' self.items[5]:=self.items[6];',
  14916. ' items[items[7]]:=items[items[8]];',
  14917. 'end;',
  14918. 'var Obj: tobject;',
  14919. 'begin',
  14920. ' obj.Items[11]:=obj.Items[12];',
  14921. '']);
  14922. ConvertProgram;
  14923. CheckSource('TestClass_Property_Indexed',
  14924. LinesToStr([ // statements
  14925. 'rtl.createClass(this, "TObject", null, function () {',
  14926. ' this.$init = function () {',
  14927. ' this.FItems = [];',
  14928. ' };',
  14929. ' this.$final = function () {',
  14930. ' this.FItems = undefined;',
  14931. ' };',
  14932. ' this.GetItems = function (Index) {',
  14933. ' var Result = 0;',
  14934. ' Result = this.FItems[Index];',
  14935. ' return Result;',
  14936. ' };',
  14937. ' this.SetItems = function (Index, Value) {',
  14938. ' this.FItems[Index] = Value;',
  14939. ' };',
  14940. ' this.DoIt = function () {',
  14941. ' this.SetItems(1, 2);',
  14942. ' this.SetItems(3,this.GetItems(4));',
  14943. ' this.SetItems(5,this.GetItems(6));',
  14944. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14945. ' };',
  14946. '});',
  14947. 'this.Obj = null;'
  14948. ]),
  14949. LinesToStr([ // $mod.$main
  14950. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14951. ]));
  14952. end;
  14953. procedure TTestModule.TestClass_Property_IndexSpec;
  14954. begin
  14955. StartProgram(false);
  14956. Add([
  14957. 'type',
  14958. ' TEnum = (red, blue);',
  14959. ' TObject = class',
  14960. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14961. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14962. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14963. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14964. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14965. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14966. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14967. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14968. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14969. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14970. ' end;',
  14971. 'procedure DoIt(b: boolean); begin end;',
  14972. 'var',
  14973. ' o: TObject;',
  14974. 'begin',
  14975. ' o.B1:=o.B1;',
  14976. ' o.B2:=o.B2;',
  14977. ' o.B3:=o.B3;',
  14978. ' o.I1[''a'']:=o.I1[''b''];',
  14979. ' doit(o.b1);',
  14980. ' doit(o.b2);',
  14981. ' doit(o.i1[''c'']);',
  14982. '']);
  14983. ConvertProgram;
  14984. CheckSource('TestClass_Property_IndexSpec',
  14985. LinesToStr([ // statements
  14986. 'this.TEnum = {',
  14987. ' "0": "red",',
  14988. ' red: 0,',
  14989. ' "1": "blue",',
  14990. ' blue: 1',
  14991. '};',
  14992. 'rtl.createClass(this, "TObject", null, function () {',
  14993. ' this.$init = function () {',
  14994. ' };',
  14995. ' this.$final = function () {',
  14996. ' };',
  14997. '});',
  14998. 'this.DoIt = function (b) {',
  14999. '};',
  15000. 'this.o = null;',
  15001. '']),
  15002. LinesToStr([ // $mod.$main
  15003. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15004. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15005. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15006. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15007. '$mod.DoIt($mod.o.GetIntBool(1));',
  15008. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15009. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15010. '']));
  15011. end;
  15012. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15013. begin
  15014. StartProgram(false);
  15015. Add('type');
  15016. Add(' TArray = array of longint;');
  15017. Add(' TObject = class');
  15018. Add(' FItems: TArray;');
  15019. Add(' function GetItems: tarray;');
  15020. Add(' procedure SetItems(Value: tarray);');
  15021. Add(' property Items: tarray read getitems write setitems;');
  15022. Add(' procedure SetNumbers(const Value: tarray);');
  15023. Add(' property Numbers: tarray write setnumbers;');
  15024. Add(' end;');
  15025. Add('function tobject.getitems: tarray;');
  15026. Add('begin');
  15027. Add(' Result:=fitems;');
  15028. Add('end;');
  15029. Add('procedure tobject.setitems(value: tarray);');
  15030. Add('begin');
  15031. Add(' fitems:=value;');
  15032. Add(' fitems:=nil;');
  15033. Add(' Items:=nil;');
  15034. Add(' Items:=Items;');
  15035. Add(' Items[1]:=2;');
  15036. Add(' fitems[3]:=Items[4];');
  15037. Add(' Items[5]:=Items[6];');
  15038. Add(' Self.Items[7]:=8;');
  15039. Add(' Self.Items[9]:=Self.Items[10];');
  15040. Add(' Items[Items[11]]:=Items[Items[12]];');
  15041. Add('end;');
  15042. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15043. Add('begin;');
  15044. Add(' Numbers:=nil;');
  15045. Add(' Numbers:=Value;');
  15046. Add(' Self.Numbers:=Value;');
  15047. Add('end;');
  15048. Add('var Obj: tobject;');
  15049. Add('begin');
  15050. Add(' obj.items:=nil;');
  15051. Add(' obj.items:=obj.items;');
  15052. Add(' obj.items[11]:=obj.items[12];');
  15053. ConvertProgram;
  15054. CheckSource('TestClass_PropertyOfTypeArray',
  15055. LinesToStr([ // statements
  15056. 'rtl.createClass(this, "TObject", null, function () {',
  15057. ' this.$init = function () {',
  15058. ' this.FItems = [];',
  15059. ' };',
  15060. ' this.$final = function () {',
  15061. ' this.FItems = undefined;',
  15062. ' };',
  15063. ' this.GetItems = function () {',
  15064. ' var Result = [];',
  15065. ' Result = rtl.arrayRef(this.FItems);',
  15066. ' return Result;',
  15067. ' };',
  15068. ' this.SetItems = function (Value) {',
  15069. ' this.FItems = rtl.arrayRef(Value);',
  15070. ' this.FItems = [];',
  15071. ' this.SetItems([]);',
  15072. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15073. ' this.GetItems()[1] = 2;',
  15074. ' this.FItems[3] = this.GetItems()[4];',
  15075. ' this.GetItems()[5] = this.GetItems()[6];',
  15076. ' this.GetItems()[7] = 8;',
  15077. ' this.GetItems()[9] = this.GetItems()[10];',
  15078. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15079. ' };',
  15080. ' this.SetNumbers = function (Value) {',
  15081. ' this.SetNumbers([]);',
  15082. ' this.SetNumbers(Value);',
  15083. ' this.SetNumbers(Value);',
  15084. ' };',
  15085. '});',
  15086. 'this.Obj = null;'
  15087. ]),
  15088. LinesToStr([ // $mod.$main
  15089. '$mod.Obj.SetItems([]);',
  15090. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15091. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15092. ]));
  15093. end;
  15094. procedure TTestModule.TestClass_PropertyDefault;
  15095. begin
  15096. StartProgram(false);
  15097. Add([
  15098. 'type',
  15099. ' TArray = array of longint;',
  15100. ' TObject = class',
  15101. ' end;',
  15102. ' TBird = class',
  15103. ' FItems: TArray;',
  15104. ' function GetItems(Index: longint): longint;',
  15105. ' procedure SetItems(Index, Value: longint);',
  15106. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15107. ' end;',
  15108. 'function TBird.getitems(index: longint): longint;',
  15109. 'begin',
  15110. 'end;',
  15111. 'procedure TBird.setitems(index, value: longint);',
  15112. 'begin',
  15113. ' Self[1]:=2;',
  15114. ' Self[3]:=Self[index];',
  15115. ' Self[index]:=Self[Self[value]];',
  15116. ' Self[Self[4]]:=value;',
  15117. 'end;',
  15118. 'var',
  15119. ' Bird: TBird;',
  15120. ' Obj: TObject;',
  15121. 'begin',
  15122. ' bird[11]:=12;',
  15123. ' bird[13]:=bird[14];',
  15124. ' bird[Bird[15]]:=bird[Bird[15]];',
  15125. ' TBird(obj)[16]:=TBird(obj)[17];',
  15126. ' (obj as tbird)[18]:=19;',
  15127. '']);
  15128. ConvertProgram;
  15129. CheckSource('TestClass_PropertyDefault',
  15130. LinesToStr([ // statements
  15131. 'rtl.createClass(this, "TObject", null, function () {',
  15132. ' this.$init = function () {',
  15133. ' };',
  15134. ' this.$final = function () {',
  15135. ' };',
  15136. '});',
  15137. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15138. ' this.$init = function () {',
  15139. ' $mod.TObject.$init.call(this);',
  15140. ' this.FItems = [];',
  15141. ' };',
  15142. ' this.$final = function () {',
  15143. ' this.FItems = undefined;',
  15144. ' $mod.TObject.$final.call(this);',
  15145. ' };',
  15146. ' this.GetItems = function (Index) {',
  15147. ' var Result = 0;',
  15148. ' return Result;',
  15149. ' };',
  15150. ' this.SetItems = function (Index, Value) {',
  15151. ' this.SetItems(1, 2);',
  15152. ' this.SetItems(3, this.GetItems(Index));',
  15153. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15154. ' this.SetItems(this.GetItems(4), Value);',
  15155. ' };',
  15156. '});',
  15157. 'this.Bird = null;',
  15158. 'this.Obj = null;',
  15159. '']),
  15160. LinesToStr([ // $mod.$main
  15161. '$mod.Bird.SetItems(11, 12);',
  15162. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15163. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15164. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15165. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15166. '']));
  15167. end;
  15168. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15169. begin
  15170. StartProgram(false);
  15171. Add([
  15172. 'type',
  15173. ' TObject = class end;',
  15174. ' TAlphaList = class',
  15175. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15176. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15177. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15178. ' end;',
  15179. ' TBetaList = class',
  15180. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15181. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15182. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15183. ' end;',
  15184. ' TBird = class',
  15185. ' procedure DoIt;',
  15186. ' end;',
  15187. 'procedure TBird.DoIt;',
  15188. 'var',
  15189. ' List: TAlphaList;',
  15190. 'begin',
  15191. ' if TBetaList(List[true])[3]=nil then ;',
  15192. ' TBetaList(List[false])[5]:=nil;',
  15193. 'end;',
  15194. 'var',
  15195. ' List: TAlphaList;',
  15196. 'begin',
  15197. ' if TBetaList(List[true])[3]=nil then ;',
  15198. ' TBetaList(List[false])[5]:=nil;',
  15199. '']);
  15200. ConvertProgram;
  15201. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15202. LinesToStr([ // statements
  15203. 'rtl.createClass(this, "TObject", null, function () {',
  15204. ' this.$init = function () {',
  15205. ' };',
  15206. ' this.$final = function () {',
  15207. ' };',
  15208. '});',
  15209. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15210. '});',
  15211. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15212. '});',
  15213. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15214. ' this.DoIt = function () {',
  15215. ' var List = null;',
  15216. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15217. ' List.GetAlphas(false).SetBetas(5, null);',
  15218. ' };',
  15219. '});',
  15220. 'this.List = null;',
  15221. '']),
  15222. LinesToStr([ // $mod.$main
  15223. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15224. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15225. '']));
  15226. end;
  15227. procedure TTestModule.TestClass_PropertyOverride;
  15228. begin
  15229. StartProgram(false);
  15230. Add('type');
  15231. Add(' integer = longint;');
  15232. Add(' TObject = class');
  15233. Add(' FItem: integer;');
  15234. Add(' function GetItem: integer; external name ''GetItem'';');
  15235. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15236. Add(' property Item: integer read getitem write setitem;');
  15237. Add(' end;');
  15238. Add(' TCar = class');
  15239. Add(' FBag: integer;');
  15240. Add(' function GetBag: integer; external name ''GetBag'';');
  15241. Add(' property Item read getbag;');
  15242. Add(' end;');
  15243. Add('var');
  15244. Add(' Obj: tobject;');
  15245. Add(' Car: tcar;');
  15246. Add('begin');
  15247. Add(' Obj.Item:=Obj.Item;');
  15248. Add(' Car.Item:=Car.Item;');
  15249. ConvertProgram;
  15250. CheckSource('TestClass_PropertyOverride',
  15251. LinesToStr([ // statements
  15252. 'rtl.createClass(this, "TObject", null, function () {',
  15253. ' this.$init = function () {',
  15254. ' this.FItem = 0;',
  15255. ' };',
  15256. ' this.$final = function () {',
  15257. ' };',
  15258. '});',
  15259. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15260. ' this.$init = function () {',
  15261. ' $mod.TObject.$init.call(this);',
  15262. ' this.FBag = 0;',
  15263. ' };',
  15264. '});',
  15265. 'this.Obj = null;',
  15266. 'this.Car = null;',
  15267. '']),
  15268. LinesToStr([ // $mod.$main
  15269. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15270. '$mod.Car.SetItem($mod.Car.GetBag());',
  15271. '']));
  15272. end;
  15273. procedure TTestModule.TestClass_PropertyIncVisibility;
  15274. begin
  15275. AddModuleWithIntfImplSrc('unit1.pp',
  15276. LinesToStr([
  15277. 'type',
  15278. ' TNumber = longint;',
  15279. ' TInteger = longint;',
  15280. ' TObject = class',
  15281. ' private',
  15282. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15283. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15284. ' protected',
  15285. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15286. ' end;']),
  15287. LinesToStr([
  15288. '']));
  15289. StartProgram(true);
  15290. Add([
  15291. 'uses unit1;',
  15292. 'type',
  15293. ' TBird = class',
  15294. ' public',
  15295. ' property Items;',
  15296. ' end;',
  15297. 'procedure DoIt(i: TInteger);',
  15298. 'begin',
  15299. 'end;',
  15300. 'var b: TBird;',
  15301. 'begin',
  15302. ' b.Items[1]:=2;',
  15303. ' b.Items[3]:=b.Items[4];',
  15304. ' DoIt(b.Items[5]);',
  15305. '']);
  15306. ConvertProgram;
  15307. CheckSource('TestClass_PropertyIncVisibility',
  15308. LinesToStr([ // statements
  15309. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15310. '});',
  15311. 'this.DoIt = function (i) {',
  15312. '};',
  15313. 'this.b = null;'
  15314. ]),
  15315. LinesToStr([ // $mod.$main
  15316. '$mod.b.SetItems(1, 2);',
  15317. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15318. '$mod.DoIt($mod.b.GetItems(5));'
  15319. ]));
  15320. end;
  15321. procedure TTestModule.TestClass_Assigned;
  15322. begin
  15323. StartProgram(false);
  15324. Add('type');
  15325. Add(' TObject = class');
  15326. Add(' end;');
  15327. Add('var');
  15328. Add(' Obj: tobject;');
  15329. Add(' b: boolean;');
  15330. Add('begin');
  15331. Add(' if Assigned(obj) then ;');
  15332. Add(' b:=Assigned(obj) or false;');
  15333. ConvertProgram;
  15334. CheckSource('TestClass_Assigned',
  15335. LinesToStr([ // statements
  15336. 'rtl.createClass(this, "TObject", null, function () {',
  15337. ' this.$init = function () {',
  15338. ' };',
  15339. ' this.$final = function () {',
  15340. ' };',
  15341. '});',
  15342. 'this.Obj = null;',
  15343. 'this.b = false;'
  15344. ]),
  15345. LinesToStr([ // $mod.$main
  15346. 'if ($mod.Obj != null);',
  15347. '$mod.b = ($mod.Obj != null) || false;'
  15348. ]));
  15349. end;
  15350. procedure TTestModule.TestClass_WithClassDoCreate;
  15351. begin
  15352. StartProgram(false);
  15353. Add('type');
  15354. Add(' TObject = class');
  15355. Add(' aBool: boolean;');
  15356. Add(' Arr: array of boolean;');
  15357. Add(' constructor Create;');
  15358. Add(' end;');
  15359. Add('constructor TObject.Create; begin end;');
  15360. Add('var');
  15361. Add(' Obj: tobject;');
  15362. Add(' b: boolean;');
  15363. Add('begin');
  15364. Add(' with tobject.create do begin');
  15365. Add(' b:=abool;');
  15366. Add(' abool:=b;');
  15367. Add(' b:=arr[1];');
  15368. Add(' arr[2]:=b;');
  15369. Add(' end;');
  15370. Add(' with tobject do');
  15371. Add(' obj:=create;');
  15372. Add(' with obj do begin');
  15373. Add(' create;');
  15374. Add(' b:=abool;');
  15375. Add(' abool:=b;');
  15376. Add(' b:=arr[3];');
  15377. Add(' arr[4]:=b;');
  15378. Add(' end;');
  15379. ConvertProgram;
  15380. CheckSource('TestClass_WithClassDoCreate',
  15381. LinesToStr([ // statements
  15382. 'rtl.createClass(this, "TObject", null, function () {',
  15383. ' this.$init = function () {',
  15384. ' this.aBool = false;',
  15385. ' this.Arr = [];',
  15386. ' };',
  15387. ' this.$final = function () {',
  15388. ' this.Arr = undefined;',
  15389. ' };',
  15390. ' this.Create = function () {',
  15391. ' return this;',
  15392. ' };',
  15393. '});',
  15394. 'this.Obj = null;',
  15395. 'this.b = false;'
  15396. ]),
  15397. LinesToStr([ // $mod.$main
  15398. 'var $with = $mod.TObject.$create("Create");',
  15399. '$mod.b = $with.aBool;',
  15400. '$with.aBool = $mod.b;',
  15401. '$mod.b = $with.Arr[1];',
  15402. '$with.Arr[2] = $mod.b;',
  15403. 'var $with1 = $mod.TObject;',
  15404. '$mod.Obj = $with1.$create("Create");',
  15405. 'var $with2 = $mod.Obj;',
  15406. '$with2.Create();',
  15407. '$mod.b = $with2.aBool;',
  15408. '$with2.aBool = $mod.b;',
  15409. '$mod.b = $with2.Arr[3];',
  15410. '$with2.Arr[4] = $mod.b;',
  15411. '']));
  15412. end;
  15413. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15414. begin
  15415. StartProgram(false);
  15416. Add('type');
  15417. Add(' TObject = class');
  15418. Add(' FInt: longint;');
  15419. Add(' constructor Create;');
  15420. Add(' function GetSize: longint;');
  15421. Add(' procedure SetSize(Value: longint);');
  15422. Add(' property Int: longint read FInt write FInt;');
  15423. Add(' property Size: longint read GetSize write SetSize;');
  15424. Add(' end;');
  15425. Add('constructor TObject.Create; begin end;');
  15426. Add('function TObject.GetSize: longint; begin; end;');
  15427. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15428. Add('var');
  15429. Add(' Obj: tobject;');
  15430. Add(' i: longint;');
  15431. Add('begin');
  15432. Add(' with TObject.Create do begin');
  15433. Add(' i:=int;');
  15434. Add(' int:=i;');
  15435. Add(' i:=size;');
  15436. Add(' size:=i;');
  15437. Add(' end;');
  15438. Add(' with obj do begin');
  15439. Add(' i:=int;');
  15440. Add(' int:=i;');
  15441. Add(' i:=size;');
  15442. Add(' size:=i;');
  15443. Add(' end;');
  15444. ConvertProgram;
  15445. CheckSource('TestClass_WithClassInstDoProperty',
  15446. LinesToStr([ // statements
  15447. 'rtl.createClass(this, "TObject", null, function () {',
  15448. ' this.$init = function () {',
  15449. ' this.FInt = 0;',
  15450. ' };',
  15451. ' this.$final = function () {',
  15452. ' };',
  15453. ' this.Create = function () {',
  15454. ' return this;',
  15455. ' };',
  15456. ' this.GetSize = function () {',
  15457. ' var Result = 0;',
  15458. ' return Result;',
  15459. ' };',
  15460. ' this.SetSize = function (Value) {',
  15461. ' };',
  15462. '});',
  15463. 'this.Obj = null;',
  15464. 'this.i = 0;'
  15465. ]),
  15466. LinesToStr([ // $mod.$main
  15467. 'var $with = $mod.TObject.$create("Create");',
  15468. '$mod.i = $with.FInt;',
  15469. '$with.FInt = $mod.i;',
  15470. '$mod.i = $with.GetSize();',
  15471. '$with.SetSize($mod.i);',
  15472. 'var $with1 = $mod.Obj;',
  15473. '$mod.i = $with1.FInt;',
  15474. '$with1.FInt = $mod.i;',
  15475. '$mod.i = $with1.GetSize();',
  15476. '$with1.SetSize($mod.i);',
  15477. '']));
  15478. end;
  15479. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15480. begin
  15481. StartProgram(false);
  15482. Add('type');
  15483. Add(' TObject = class');
  15484. Add(' constructor Create;');
  15485. Add(' function GetItems(Index: longint): longint;');
  15486. Add(' procedure SetItems(Index, Value: longint);');
  15487. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15488. Add(' end;');
  15489. Add('constructor TObject.Create; begin end;');
  15490. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15491. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15492. Add('var');
  15493. Add(' Obj: tobject;');
  15494. Add(' i: longint;');
  15495. Add('begin');
  15496. Add(' with TObject.Create do begin');
  15497. Add(' i:=Items[1];');
  15498. Add(' Items[2]:=i;');
  15499. Add(' end;');
  15500. Add(' with obj do begin');
  15501. Add(' i:=Items[3];');
  15502. Add(' Items[4]:=i;');
  15503. Add(' end;');
  15504. ConvertProgram;
  15505. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15506. LinesToStr([ // statements
  15507. 'rtl.createClass(this, "TObject", null, function () {',
  15508. ' this.$init = function () {',
  15509. ' };',
  15510. ' this.$final = function () {',
  15511. ' };',
  15512. ' this.Create = function () {',
  15513. ' return this;',
  15514. ' };',
  15515. ' this.GetItems = function (Index) {',
  15516. ' var Result = 0;',
  15517. ' return Result;',
  15518. ' };',
  15519. ' this.SetItems = function (Index, Value) {',
  15520. ' };',
  15521. '});',
  15522. 'this.Obj = null;',
  15523. 'this.i = 0;'
  15524. ]),
  15525. LinesToStr([ // $mod.$main
  15526. 'var $with = $mod.TObject.$create("Create");',
  15527. '$mod.i = $with.GetItems(1);',
  15528. '$with.SetItems(2, $mod.i);',
  15529. 'var $with1 = $mod.Obj;',
  15530. '$mod.i = $with1.GetItems(3);',
  15531. '$with1.SetItems(4, $mod.i);',
  15532. '']));
  15533. end;
  15534. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15535. begin
  15536. StartProgram(false);
  15537. Add('type');
  15538. Add(' TObject = class');
  15539. Add(' constructor Create;');
  15540. Add(' function GetSize: longint;');
  15541. Add(' procedure SetSize(Value: longint);');
  15542. Add(' end;');
  15543. Add('constructor TObject.Create; begin end;');
  15544. Add('function TObject.GetSize: longint; begin; end;');
  15545. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15546. Add('var');
  15547. Add(' Obj: tobject;');
  15548. Add(' i: longint;');
  15549. Add('begin');
  15550. Add(' with TObject.Create do begin');
  15551. Add(' i:=GetSize;');
  15552. Add(' i:=GetSize();');
  15553. Add(' SetSize(i);');
  15554. Add(' end;');
  15555. Add(' with obj do begin');
  15556. Add(' i:=GetSize;');
  15557. Add(' i:=GetSize();');
  15558. Add(' SetSize(i);');
  15559. Add(' end;');
  15560. ConvertProgram;
  15561. CheckSource('TestClass_WithClassInstDoFunc',
  15562. LinesToStr([ // statements
  15563. 'rtl.createClass(this, "TObject", null, function () {',
  15564. ' this.$init = function () {',
  15565. ' };',
  15566. ' this.$final = function () {',
  15567. ' };',
  15568. ' this.Create = function () {',
  15569. ' return this;',
  15570. ' };',
  15571. ' this.GetSize = function () {',
  15572. ' var Result = 0;',
  15573. ' return Result;',
  15574. ' };',
  15575. ' this.SetSize = function (Value) {',
  15576. ' };',
  15577. '});',
  15578. 'this.Obj = null;',
  15579. 'this.i = 0;'
  15580. ]),
  15581. LinesToStr([ // $mod.$main
  15582. 'var $with = $mod.TObject.$create("Create");',
  15583. '$mod.i = $with.GetSize();',
  15584. '$mod.i = $with.GetSize();',
  15585. '$with.SetSize($mod.i);',
  15586. 'var $with1 = $mod.Obj;',
  15587. '$mod.i = $with1.GetSize();',
  15588. '$mod.i = $with1.GetSize();',
  15589. '$with1.SetSize($mod.i);',
  15590. '']));
  15591. end;
  15592. procedure TTestModule.TestClass_TypeCast;
  15593. begin
  15594. StartProgram(false);
  15595. Add('type');
  15596. Add(' TObject = class');
  15597. Add(' Next: TObject;');
  15598. Add(' constructor Create;');
  15599. Add(' end;');
  15600. Add(' TControl = class(TObject)');
  15601. Add(' Arr: array of TObject;');
  15602. Add(' function GetIt(vI: longint = 0): TObject;');
  15603. Add(' end;');
  15604. Add('constructor tobject.create; begin end;');
  15605. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15606. Add('var');
  15607. Add(' Obj: tobject;');
  15608. Add('begin');
  15609. Add(' obj:=tcontrol(obj).next;');
  15610. Add(' tcontrol(obj):=nil;');
  15611. Add(' obj:=tcontrol(obj);');
  15612. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15613. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15614. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15615. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15616. Add(' obj:=tcontrol(nil);');
  15617. ConvertProgram;
  15618. CheckSource('TestClass_TypeCast',
  15619. LinesToStr([ // statements
  15620. 'rtl.createClass(this, "TObject", null, function () {',
  15621. ' this.$init = function () {',
  15622. ' this.Next = null;',
  15623. ' };',
  15624. ' this.$final = function () {',
  15625. ' this.Next = undefined;',
  15626. ' };',
  15627. ' this.Create = function () {',
  15628. ' return this;',
  15629. ' };',
  15630. '});',
  15631. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15632. ' this.$init = function () {',
  15633. ' $mod.TObject.$init.call(this);',
  15634. ' this.Arr = [];',
  15635. ' };',
  15636. ' this.$final = function () {',
  15637. ' this.Arr = undefined;',
  15638. ' $mod.TObject.$final.call(this);',
  15639. ' };',
  15640. ' this.GetIt = function (vI) {',
  15641. ' var Result = null;',
  15642. ' return Result;',
  15643. ' };',
  15644. '});',
  15645. 'this.Obj = null;'
  15646. ]),
  15647. LinesToStr([ // $mod.$main
  15648. '$mod.Obj = $mod.Obj.Next;',
  15649. '$mod.Obj = null;',
  15650. '$mod.Obj = $mod.Obj;',
  15651. '$mod.Obj = $mod.Obj.GetIt(0);',
  15652. '$mod.Obj = $mod.Obj.GetIt(0);',
  15653. '$mod.Obj = $mod.Obj.GetIt(1);',
  15654. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15655. '$mod.Obj = null;',
  15656. '']));
  15657. end;
  15658. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15659. begin
  15660. StartProgram(false);
  15661. Add('type');
  15662. Add(' TObject = class end;');
  15663. Add('procedure ProcA(var A);');
  15664. Add('begin');
  15665. Add(' TObject(A):=nil;');
  15666. Add(' TObject(A):=TObject(A);');
  15667. Add(' if TObject(A)=nil then ;');
  15668. Add(' if nil=TObject(A) then ;');
  15669. Add('end;');
  15670. Add('procedure ProcB(out A);');
  15671. Add('begin');
  15672. Add(' TObject(A):=nil;');
  15673. Add(' TObject(A):=TObject(A);');
  15674. Add(' if TObject(A)=nil then ;');
  15675. Add(' if nil=TObject(A) then ;');
  15676. Add('end;');
  15677. Add('procedure ProcC(const A);');
  15678. Add('begin');
  15679. Add(' if TObject(A)=nil then ;');
  15680. Add(' if nil=TObject(A) then ;');
  15681. Add('end;');
  15682. Add('var o: TObject;');
  15683. Add('begin');
  15684. Add(' ProcA(o);');
  15685. Add(' ProcB(o);');
  15686. Add(' ProcC(o);');
  15687. ConvertProgram;
  15688. CheckSource('TestClass_TypeCastUntypedParam',
  15689. LinesToStr([ // statements
  15690. 'rtl.createClass(this, "TObject", null, function () {',
  15691. ' this.$init = function () {',
  15692. ' };',
  15693. ' this.$final = function () {',
  15694. ' };',
  15695. '});',
  15696. 'this.ProcA = function (A) {',
  15697. ' A.set(null);',
  15698. ' A.set(A.get());',
  15699. ' if (A.get() === null);',
  15700. ' if (null === A.get());',
  15701. '};',
  15702. 'this.ProcB = function (A) {',
  15703. ' A.set(null);',
  15704. ' A.set(A.get());',
  15705. ' if (A.get() === null);',
  15706. ' if (null === A.get());',
  15707. '};',
  15708. 'this.ProcC = function (A) {',
  15709. ' if (A === null);',
  15710. ' if (null === A);',
  15711. '};',
  15712. 'this.o = null;',
  15713. '']),
  15714. LinesToStr([ // $mod.$main
  15715. '$mod.ProcA({',
  15716. ' p: $mod,',
  15717. ' get: function () {',
  15718. ' return this.p.o;',
  15719. ' },',
  15720. ' set: function (v) {',
  15721. ' this.p.o = v;',
  15722. ' }',
  15723. '});',
  15724. '$mod.ProcB({',
  15725. ' p: $mod,',
  15726. ' get: function () {',
  15727. ' return this.p.o;',
  15728. ' },',
  15729. ' set: function (v) {',
  15730. ' this.p.o = v;',
  15731. ' }',
  15732. '});',
  15733. '$mod.ProcC($mod.o);',
  15734. '']));
  15735. end;
  15736. procedure TTestModule.TestClass_Overloads;
  15737. begin
  15738. StartProgram(false);
  15739. Add('type');
  15740. Add(' TObject = class');
  15741. Add(' procedure DoIt;');
  15742. Add(' procedure DoIt(vI: longint);');
  15743. Add(' end;');
  15744. Add('procedure TObject.DoIt;');
  15745. Add('begin');
  15746. Add(' DoIt;');
  15747. Add(' DoIt(1);');
  15748. Add('end;');
  15749. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15750. Add('begin');
  15751. ConvertProgram;
  15752. CheckSource('TestClass_Overloads',
  15753. LinesToStr([ // statements
  15754. 'rtl.createClass(this, "TObject", null, function () {',
  15755. ' this.$init = function () {',
  15756. ' };',
  15757. ' this.$final = function () {',
  15758. ' };',
  15759. ' this.DoIt = function () {',
  15760. ' this.DoIt();',
  15761. ' this.DoIt$1(1);',
  15762. ' };',
  15763. ' this.DoIt$1 = function (vI) {',
  15764. ' };',
  15765. '});',
  15766. '']),
  15767. LinesToStr([ // $mod.$main
  15768. '']));
  15769. end;
  15770. procedure TTestModule.TestClass_OverloadsAncestor;
  15771. begin
  15772. StartProgram(false);
  15773. Add('type');
  15774. Add(' TObject = class;');
  15775. Add(' TObject = class');
  15776. Add(' procedure DoIt(vA: longint);');
  15777. Add(' procedure DoIt(vA, vB: longint);');
  15778. Add(' end;');
  15779. Add(' TCar = class;');
  15780. Add(' TCar = class');
  15781. Add(' procedure DoIt(vA: longint);');
  15782. Add(' procedure DoIt(vA, vB: longint);');
  15783. Add(' end;');
  15784. Add('procedure tobject.doit(va: longint);');
  15785. Add('begin');
  15786. Add(' doit(1);');
  15787. Add(' doit(1,2);');
  15788. Add('end;');
  15789. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15790. Add('procedure tcar.doit(va: longint);');
  15791. Add('begin');
  15792. Add(' doit(1);');
  15793. Add(' doit(1,2);');
  15794. Add(' inherited doit(1);');
  15795. Add(' inherited doit(1,2);');
  15796. Add('end;');
  15797. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15798. Add('begin');
  15799. ConvertProgram;
  15800. CheckSource('TestClass_OverloadsAncestor',
  15801. LinesToStr([ // statements
  15802. 'rtl.createClass(this, "TObject", null, function () {',
  15803. ' this.$init = function () {',
  15804. ' };',
  15805. ' this.$final = function () {',
  15806. ' };',
  15807. ' this.DoIt = function (vA) {',
  15808. ' this.DoIt(1);',
  15809. ' this.DoIt$1(1,2);',
  15810. ' };',
  15811. ' this.DoIt$1 = function (vA, vB) {',
  15812. ' };',
  15813. '});',
  15814. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15815. ' this.DoIt$2 = function (vA) {',
  15816. ' this.DoIt$2(1);',
  15817. ' this.DoIt$3(1, 2);',
  15818. ' $mod.TObject.DoIt.call(this, 1);',
  15819. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15820. ' };',
  15821. ' this.DoIt$3 = function (vA, vB) {',
  15822. ' };',
  15823. '});',
  15824. '']),
  15825. LinesToStr([ // $mod.$main
  15826. '']));
  15827. end;
  15828. procedure TTestModule.TestClass_OverloadConstructor;
  15829. begin
  15830. StartProgram(false);
  15831. Add('type');
  15832. Add(' TObject = class');
  15833. Add(' constructor Create(vA: longint);');
  15834. Add(' constructor Create(vA, vB: longint);');
  15835. Add(' end;');
  15836. Add(' TCar = class');
  15837. Add(' constructor Create(vA: longint);');
  15838. Add(' constructor Create(vA, vB: longint);');
  15839. Add(' end;');
  15840. Add('constructor tobject.create(va: longint);');
  15841. Add('begin');
  15842. Add(' create(1);');
  15843. Add(' create(1,2);');
  15844. Add('end;');
  15845. Add('constructor tobject.create(va, vb: longint); begin end;');
  15846. Add('constructor tcar.create(va: longint);');
  15847. Add('begin');
  15848. Add(' create(1);');
  15849. Add(' create(1,2);');
  15850. Add(' inherited create(1);');
  15851. Add(' inherited create(1,2);');
  15852. Add('end;');
  15853. Add('constructor tcar.create(va, vb: longint); begin end;');
  15854. Add('begin');
  15855. Add(' tobject.create(1);');
  15856. Add(' tobject.create(1,2);');
  15857. Add(' tcar.create(1);');
  15858. Add(' tcar.create(1,2);');
  15859. ConvertProgram;
  15860. CheckSource('TestClass_OverloadConstructor',
  15861. LinesToStr([ // statements
  15862. 'rtl.createClass(this, "TObject", null, function () {',
  15863. ' this.$init = function () {',
  15864. ' };',
  15865. ' this.$final = function () {',
  15866. ' };',
  15867. ' this.Create = function (vA) {',
  15868. ' this.Create(1);',
  15869. ' this.Create$1(1,2);',
  15870. ' return this;',
  15871. ' };',
  15872. ' this.Create$1 = function (vA, vB) {',
  15873. ' return this;',
  15874. ' };',
  15875. '});',
  15876. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15877. ' this.Create$2 = function (vA) {',
  15878. ' this.Create$2(1);',
  15879. ' this.Create$3(1, 2);',
  15880. ' $mod.TObject.Create.call(this, 1);',
  15881. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15882. ' return this;',
  15883. ' };',
  15884. ' this.Create$3 = function (vA, vB) {',
  15885. ' return this;',
  15886. ' };',
  15887. '});',
  15888. '']),
  15889. LinesToStr([ // $mod.$main
  15890. '$mod.TObject.$create("Create", [1]);',
  15891. '$mod.TObject.$create("Create$1", [1, 2]);',
  15892. '$mod.TCar.$create("Create$2", [1]);',
  15893. '$mod.TCar.$create("Create$3", [1, 2]);',
  15894. '']));
  15895. end;
  15896. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15897. begin
  15898. StartProgram(false);
  15899. Add([
  15900. '{$mode delphi}',
  15901. 'type',
  15902. ' TObject = class end;',
  15903. ' TBird = class',
  15904. ' function {#a}GetValue: longint; overload; virtual;',
  15905. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15906. ' end;',
  15907. ' TEagle = class(TBird)',
  15908. ' function {#c}GetValue: longint; overload; override;',
  15909. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15910. ' end;',
  15911. 'function TBird.GetValue: longint;',
  15912. 'begin',
  15913. ' if 3={@a}GetValue then ;',
  15914. ' if 4={@b}GetValue(5) then ;',
  15915. 'end;',
  15916. 'function TBird.GetValue(AValue: longint): longint;',
  15917. 'begin',
  15918. 'end;',
  15919. 'function TEagle.GetValue: longint;',
  15920. 'begin',
  15921. ' if 13={@c}GetValue then ;',
  15922. ' if 14={@d}GetValue(15) then ;',
  15923. ' if 15=inherited {@a}GetValue then ;',
  15924. ' if 16=inherited {@b}GetValue(17) then ;',
  15925. 'end;',
  15926. 'function TEagle.GetValue(AValue: longint): longint;',
  15927. 'begin',
  15928. 'end;',
  15929. 'var',
  15930. ' e: TEagle;',
  15931. 'begin',
  15932. ' if 23=e.{@c}GetValue then ;',
  15933. ' if 24=e.{@d}GetValue(25) then ;']);
  15934. ConvertProgram;
  15935. CheckSource('TestClass_OverloadDelphiOverride',
  15936. LinesToStr([ // statements
  15937. 'rtl.createClass(this, "TObject", null, function () {',
  15938. ' this.$init = function () {',
  15939. ' };',
  15940. ' this.$final = function () {',
  15941. ' };',
  15942. '});',
  15943. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15944. ' this.GetValue = function () {',
  15945. ' var Result = 0;',
  15946. ' if (3 === this.GetValue()) ;',
  15947. ' if (4 === this.GetValue$1(5)) ;',
  15948. ' return Result;',
  15949. ' };',
  15950. ' this.GetValue$1 = function (AValue) {',
  15951. ' var Result = 0;',
  15952. ' return Result;',
  15953. ' };',
  15954. '});',
  15955. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15956. ' this.GetValue = function () {',
  15957. ' var Result = 0;',
  15958. ' if (13 === this.GetValue()) ;',
  15959. ' if (14 === this.GetValue$1(15)) ;',
  15960. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15961. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15962. ' return Result;',
  15963. ' };',
  15964. ' this.GetValue$1 = function (AValue) {',
  15965. ' var Result = 0;',
  15966. ' return Result;',
  15967. ' };',
  15968. '});',
  15969. 'this.e = null;',
  15970. '']),
  15971. LinesToStr([ // $mod.$main
  15972. 'if (23 === $mod.e.GetValue()) ;',
  15973. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15974. '']));
  15975. end;
  15976. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15977. begin
  15978. StartProgram(false);
  15979. Add([
  15980. '{$mode delphi}',
  15981. 'type',
  15982. ' TObject = class end;',
  15983. ' TAnimal = class',
  15984. ' public',
  15985. ' {#animal_a}A: longint;',
  15986. ' function {#animal_b}B: longint;',
  15987. ' end;',
  15988. ' TBird = class(TAnimal)',
  15989. ' public',
  15990. ' {#bird_a}A: double;',
  15991. ' {#bird_b}B: boolean;',
  15992. ' end;',
  15993. ' TEagle = class(TBird)',
  15994. ' public',
  15995. ' function {#eagle_a}A: boolean;',
  15996. ' {#eagle_b}B: double;',
  15997. ' end;',
  15998. 'function TAnimal.B: longint;',
  15999. 'begin',
  16000. 'end;',
  16001. 'function TEagle.A: boolean;',
  16002. 'begin',
  16003. ' {@eagle_b}B:=3.3;',
  16004. ' {@eagle_a}A();',
  16005. ' TBird(Self).{@bird_b}B:=true;',
  16006. ' TAnimal(Self).{@animal_a}A:=17;',
  16007. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16008. 'end;',
  16009. 'var',
  16010. ' e: TEagle;',
  16011. 'begin',
  16012. ' e.{@eagle_b}B:=5.3;',
  16013. ' if e.{@eagle_a}A then ;',
  16014. '']);
  16015. ConvertProgram;
  16016. CheckSource('TestClass_ReintroduceVarDelphi',
  16017. LinesToStr([ // statements
  16018. 'rtl.createClass(this, "TObject", null, function () {',
  16019. ' this.$init = function () {',
  16020. ' };',
  16021. ' this.$final = function () {',
  16022. ' };',
  16023. '});',
  16024. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16025. ' this.$init = function () {',
  16026. ' $mod.TObject.$init.call(this);',
  16027. ' this.A = 0;',
  16028. ' };',
  16029. ' this.B = function () {',
  16030. ' var Result = 0;',
  16031. ' return Result;',
  16032. ' };',
  16033. '});',
  16034. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16035. ' this.$init = function () {',
  16036. ' $mod.TAnimal.$init.call(this);',
  16037. ' this.A$1 = 0.0;',
  16038. ' this.B$1 = false;',
  16039. ' };',
  16040. '});',
  16041. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16042. ' this.$init = function () {',
  16043. ' $mod.TBird.$init.call(this);',
  16044. ' this.B$2 = 0.0;',
  16045. ' };',
  16046. ' this.A$2 = function () {',
  16047. ' var Result = false;',
  16048. ' this.B$2 = 3.3;',
  16049. ' this.A$2();',
  16050. ' this.B$1 = true;',
  16051. ' this.A = 17;',
  16052. ' this.B$1 = this.A$1 > 1;',
  16053. ' return Result;',
  16054. ' };',
  16055. '});',
  16056. 'this.e = null;',
  16057. '']),
  16058. LinesToStr([ // $mod.$main
  16059. '$mod.e.B$2 = 5.3;',
  16060. 'if ($mod.e.A$2()) ;',
  16061. '']));
  16062. end;
  16063. procedure TTestModule.TestClass_ReintroducedVar;
  16064. begin
  16065. StartProgram(false);
  16066. Add('type');
  16067. Add(' TObject = class');
  16068. Add(' strict private');
  16069. Add(' Some: longint;');
  16070. Add(' end;');
  16071. Add(' TMobile = class');
  16072. Add(' strict private');
  16073. Add(' Some: string;');
  16074. Add(' end;');
  16075. Add(' TCar = class(tmobile)');
  16076. Add(' procedure Some;');
  16077. Add(' procedure Some(vA: longint);');
  16078. Add(' end;');
  16079. Add('procedure tcar.some;');
  16080. Add('begin');
  16081. Add(' Some;');
  16082. Add(' Some(1);');
  16083. Add('end;');
  16084. Add('procedure tcar.some(va: longint); begin end;');
  16085. Add('begin');
  16086. ConvertProgram;
  16087. CheckSource('TestClass_ReintroducedVar',
  16088. LinesToStr([ // statements
  16089. 'rtl.createClass(this, "TObject", null, function () {',
  16090. ' this.$init = function () {',
  16091. ' this.Some = 0;',
  16092. ' };',
  16093. ' this.$final = function () {',
  16094. ' };',
  16095. '});',
  16096. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16097. ' this.$init = function () {',
  16098. ' $mod.TObject.$init.call(this);',
  16099. ' this.Some$1 = "";',
  16100. ' };',
  16101. '});',
  16102. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16103. ' this.Some$2 = function () {',
  16104. ' this.Some$2();',
  16105. ' this.Some$3(1);',
  16106. ' };',
  16107. ' this.Some$3 = function (vA) {',
  16108. ' };',
  16109. '});',
  16110. '']),
  16111. LinesToStr([ // $mod.$main
  16112. '']));
  16113. end;
  16114. procedure TTestModule.TestClass_RaiseDescendant;
  16115. begin
  16116. StartProgram(false);
  16117. Add([
  16118. 'type',
  16119. ' TObject = class',
  16120. ' constructor Create(Msg: string);',
  16121. ' end;',
  16122. ' Exception = class',
  16123. ' end;',
  16124. ' EConvertError = class(Exception)',
  16125. ' end;',
  16126. 'constructor TObject.Create(Msg: string); begin end;',
  16127. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16128. 'begin',
  16129. ' raise Exception.Create(''Bar1'');',
  16130. ' raise EConvertError.Create(''Bar2'');',
  16131. ' raise AssertConv(''Bar2'');',
  16132. ' raise AssertConv;',
  16133. '']);
  16134. ConvertProgram;
  16135. CheckSource('TestClass_RaiseDescendant',
  16136. LinesToStr([ // statements
  16137. 'rtl.createClass(this, "TObject", null, function () {',
  16138. ' this.$init = function () {',
  16139. ' };',
  16140. ' this.$final = function () {',
  16141. ' };',
  16142. ' this.Create = function (Msg) {',
  16143. ' return this;',
  16144. ' };',
  16145. '});',
  16146. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16147. '});',
  16148. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16149. '});',
  16150. 'this.AssertConv = function (Msg) {',
  16151. ' var Result = null;',
  16152. ' return Result;',
  16153. '};',
  16154. '']),
  16155. LinesToStr([ // $mod.$main
  16156. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16157. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16158. 'throw $mod.AssertConv("Bar2");',
  16159. 'throw $mod.AssertConv("def");',
  16160. '']));
  16161. end;
  16162. procedure TTestModule.TestClass_ExternalMethod;
  16163. begin
  16164. AddModuleWithIntfImplSrc('unit2.pas',
  16165. LinesToStr([
  16166. 'type',
  16167. ' TObject = class',
  16168. ' public',
  16169. ' procedure Intern; external name ''$DoIntern'';',
  16170. ' end;',
  16171. '']),
  16172. LinesToStr([
  16173. '']));
  16174. StartUnit(true);
  16175. Add('interface');
  16176. Add('uses unit2;');
  16177. Add('type');
  16178. Add(' TCar = class(TObject)');
  16179. Add(' public');
  16180. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16181. Add(' procedure DoIt;');
  16182. Add(' end;');
  16183. Add('implementation');
  16184. Add('procedure tcar.doit;');
  16185. Add('begin');
  16186. Add(' Intern;');
  16187. Add(' Intern();');
  16188. Add(' Intern2;');
  16189. Add(' Intern2();');
  16190. Add('end;');
  16191. Add('var Obj: TCar;');
  16192. Add('begin');
  16193. Add(' obj.intern;');
  16194. Add(' obj.intern();');
  16195. Add(' obj.intern2;');
  16196. Add(' obj.intern2();');
  16197. Add(' obj.doit;');
  16198. Add(' obj.doit();');
  16199. Add(' with obj do begin');
  16200. Add(' Intern;');
  16201. Add(' Intern();');
  16202. Add(' Intern2;');
  16203. Add(' Intern2();');
  16204. Add(' end;');
  16205. ConvertUnit;
  16206. CheckSource('TestClass_ExternalMethod',
  16207. LinesToStr([
  16208. 'var $impl = $mod.$impl;',
  16209. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16210. ' this.DoIt = function () {',
  16211. ' this.$DoIntern();',
  16212. ' this.$DoIntern();',
  16213. ' this.$DoIntern2();',
  16214. ' this.$DoIntern2();',
  16215. ' };',
  16216. ' });',
  16217. '']),
  16218. LinesToStr([ // this.$init
  16219. '$impl.Obj.$DoIntern();',
  16220. '$impl.Obj.$DoIntern();',
  16221. '$impl.Obj.$DoIntern2();',
  16222. '$impl.Obj.$DoIntern2();',
  16223. '$impl.Obj.DoIt();',
  16224. '$impl.Obj.DoIt();',
  16225. 'var $with = $impl.Obj;',
  16226. '$with.$DoIntern();',
  16227. '$with.$DoIntern();',
  16228. '$with.$DoIntern2();',
  16229. '$with.$DoIntern2();',
  16230. '']),
  16231. LinesToStr([ // implementation
  16232. '$impl.Obj = null;',
  16233. '']) );
  16234. end;
  16235. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16236. begin
  16237. StartProgram(false);
  16238. Add('type');
  16239. Add(' TObject = class');
  16240. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16241. Add(' end;');
  16242. Add('begin');
  16243. SetExpectedPasResolverError('Virtual method name must match external',
  16244. nVirtualMethodNameMustMatchExternal);
  16245. ConvertProgram;
  16246. end;
  16247. procedure TTestModule.TestClass_ExternalOverrideFail;
  16248. begin
  16249. StartProgram(false);
  16250. Add('type');
  16251. Add(' TObject = class');
  16252. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16253. Add(' end;');
  16254. Add(' TCar = class');
  16255. Add(' procedure DoIt; override; external name ''DoIt'';');
  16256. Add(' end;');
  16257. Add('begin');
  16258. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16259. nInvalidXModifierY);
  16260. ConvertProgram;
  16261. end;
  16262. procedure TTestModule.TestClass_ExternalVar;
  16263. begin
  16264. AddModuleWithIntfImplSrc('unit2.pas',
  16265. LinesToStr([
  16266. '{$modeswitch externalclass}',
  16267. 'type',
  16268. ' TObject = class',
  16269. ' public',
  16270. ' Intern: longint external name ''$Intern'';',
  16271. ' Bracket: longint external name ''["A B"]'';',
  16272. ' end;',
  16273. '']),
  16274. LinesToStr([
  16275. '']));
  16276. StartUnit(true);
  16277. Add([
  16278. 'interface',
  16279. 'uses unit2;',
  16280. '{$modeswitch externalclass}',
  16281. 'type',
  16282. ' TCar = class(tobject)',
  16283. ' public',
  16284. ' Intern2: longint external name ''$Intern2'';',
  16285. ' procedure DoIt;',
  16286. ' end;',
  16287. 'implementation',
  16288. 'procedure tcar.doit;',
  16289. 'begin',
  16290. ' Intern:=Intern+1;',
  16291. ' Intern2:=Intern2+2;',
  16292. ' Bracket:=Bracket+3;',
  16293. 'end;',
  16294. 'var Obj: TCar;',
  16295. 'begin',
  16296. ' obj.intern:=obj.intern+1;',
  16297. ' obj.intern2:=obj.intern2+2;',
  16298. ' obj.Bracket:=obj.Bracket+3;',
  16299. ' with obj do begin',
  16300. ' intern:=intern+1;',
  16301. ' intern2:=intern2+2;',
  16302. ' Bracket:=Bracket+3;',
  16303. ' end;']);
  16304. ConvertUnit;
  16305. CheckSource('TestClass_ExternalVar',
  16306. LinesToStr([
  16307. 'var $impl = $mod.$impl;',
  16308. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16309. ' this.DoIt = function () {',
  16310. ' this.$Intern = this.$Intern + 1;',
  16311. ' this.$Intern2 = this.$Intern2 + 2;',
  16312. ' this["A B"] = this["A B"] + 3;',
  16313. ' };',
  16314. ' });',
  16315. '']),
  16316. LinesToStr([
  16317. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16318. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16319. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16320. 'var $with = $impl.Obj;',
  16321. '$with.$Intern = $with.$Intern + 1;',
  16322. '$with.$Intern2 = $with.$Intern2 + 2;',
  16323. '$with["A B"] = $with["A B"] + 3;',
  16324. '']),
  16325. LinesToStr([ // implementation
  16326. '$impl.Obj = null;',
  16327. '']));
  16328. end;
  16329. procedure TTestModule.TestClass_Const;
  16330. begin
  16331. StartProgram(false);
  16332. Add([
  16333. 'type',
  16334. ' integer = longint;',
  16335. ' TClass = class of TObject;',
  16336. ' TObject = class',
  16337. ' public',
  16338. ' const cI: integer = 3;',
  16339. ' procedure DoIt;',
  16340. ' class procedure DoMore;',
  16341. ' end;',
  16342. 'procedure tobject.doit;',
  16343. 'begin',
  16344. ' if cI=4 then;',
  16345. ' if 5=cI then;',
  16346. ' if Self.cI=6 then;',
  16347. ' if 7=Self.cI then;',
  16348. ' with Self do begin',
  16349. ' if cI=11 then;',
  16350. ' if 12=cI then;',
  16351. ' end;',
  16352. 'end;',
  16353. 'class procedure tobject.domore;',
  16354. 'begin',
  16355. ' if cI=8 then;',
  16356. ' if Self.cI=9 then;',
  16357. ' if 10=cI then;',
  16358. ' if 11=Self.cI then;',
  16359. ' with Self do begin',
  16360. ' if cI=13 then;',
  16361. ' if 14=cI then;',
  16362. ' end;',
  16363. 'end;',
  16364. 'var',
  16365. ' Obj: TObject;',
  16366. ' Cla: TClass;',
  16367. 'begin',
  16368. ' if TObject.cI=21 then ;',
  16369. ' if Obj.cI=22 then ;',
  16370. ' if Cla.cI=23 then ;',
  16371. ' with obj do if ci=24 then;',
  16372. ' with TObject do if ci=25 then;',
  16373. ' with Cla do if ci=26 then;']);
  16374. ConvertProgram;
  16375. CheckSource('TestClass_Const',
  16376. LinesToStr([
  16377. 'rtl.createClass(this, "TObject", null, function () {',
  16378. ' this.cI = 3;',
  16379. ' this.$init = function () {',
  16380. ' };',
  16381. ' this.$final = function () {',
  16382. ' };',
  16383. ' this.DoIt = function () {',
  16384. ' if (this.cI === 4) ;',
  16385. ' if (5 === this.cI) ;',
  16386. ' if (this.cI === 6) ;',
  16387. ' if (7 === this.cI) ;',
  16388. ' if (this.cI === 11) ;',
  16389. ' if (12 === this.cI) ;',
  16390. ' };',
  16391. ' this.DoMore = function () {',
  16392. ' if (this.cI === 8) ;',
  16393. ' if (this.cI === 9) ;',
  16394. ' if (10 === this.cI) ;',
  16395. ' if (11 === this.cI) ;',
  16396. ' if (this.cI === 13) ;',
  16397. ' if (14 === this.cI) ;',
  16398. ' };',
  16399. '});',
  16400. 'this.Obj = null;',
  16401. 'this.Cla = null;',
  16402. '']),
  16403. LinesToStr([
  16404. 'if ($mod.TObject.cI === 21) ;',
  16405. 'if ($mod.Obj.cI === 22) ;',
  16406. 'if ($mod.Cla.cI === 23) ;',
  16407. 'var $with = $mod.Obj;',
  16408. 'if ($with.cI === 24) ;',
  16409. 'var $with1 = $mod.TObject;',
  16410. 'if ($with1.cI === 25) ;',
  16411. 'var $with2 = $mod.Cla;',
  16412. 'if ($with2.cI === 26) ;',
  16413. '']));
  16414. end;
  16415. procedure TTestModule.TestClass_ConstEnum;
  16416. begin
  16417. StartProgram(false);
  16418. Add([
  16419. 'type',
  16420. ' TEnum = (red,blue);',
  16421. ' TObject = class',
  16422. ' end;',
  16423. ' TAnimal = class',
  16424. ' public',
  16425. ' type TSubEnum = (light,dark);',
  16426. ' const a = high(TEnum);',
  16427. ' const b = high(TSubEnum);',
  16428. ' end;',
  16429. ' TBird = class(TAnimal)',
  16430. ' public',
  16431. ' const c = high(TEnum);',
  16432. ' const d = high(TSubEnum);',
  16433. ' end;',
  16434. ' TAnt = class',
  16435. ' public',
  16436. ' const e = high(TEnum);',
  16437. ' const f = high(TBird.TSubEnum);',
  16438. ' end;',
  16439. 'begin',
  16440. '']);
  16441. ConvertProgram;
  16442. CheckSource('TestClass_ConstEnum',
  16443. LinesToStr([
  16444. 'this.TEnum = {',
  16445. ' "0": "red",',
  16446. ' red: 0,',
  16447. ' "1": "blue",',
  16448. ' blue: 1',
  16449. '};',
  16450. 'rtl.createClass(this, "TObject", null, function () {',
  16451. ' this.$init = function () {',
  16452. ' };',
  16453. ' this.$final = function () {',
  16454. ' };',
  16455. '});',
  16456. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16457. ' this.TSubEnum = {',
  16458. ' "0": "light",',
  16459. ' light: 0,',
  16460. ' "1": "dark",',
  16461. ' dark: 1',
  16462. ' };',
  16463. ' this.a = $mod.TEnum.blue;',
  16464. ' this.b = this.TSubEnum.dark;',
  16465. '});',
  16466. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16467. ' this.c = $mod.TEnum.blue;',
  16468. ' this.d = this.TSubEnum.dark;',
  16469. '});',
  16470. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16471. ' this.e = $mod.TEnum.blue;',
  16472. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16473. '});',
  16474. '']),
  16475. LinesToStr([
  16476. '']));
  16477. end;
  16478. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16479. begin
  16480. StartProgram(false);
  16481. Add([
  16482. 'type',
  16483. ' TObject = class',
  16484. ' const cI: longint = 3;',
  16485. ' procedure Fly;',
  16486. ' procedure Run;',
  16487. ' end;',
  16488. ' TBird = class',
  16489. ' procedure Go;',
  16490. ' end;',
  16491. 'procedure tobject.fly;',
  16492. 'const cI: word = 4;',
  16493. 'begin',
  16494. ' if cI=Self.cI then ;',
  16495. 'end;',
  16496. 'procedure tobject.run;',
  16497. 'const cI: word = 5;',
  16498. 'begin',
  16499. ' if cI=Self.cI then ;',
  16500. 'end;',
  16501. 'procedure tbird.go;',
  16502. 'const cI: word = 6;',
  16503. 'begin',
  16504. ' if cI=Self.cI then ;',
  16505. 'end;',
  16506. 'begin',
  16507. '']);
  16508. ConvertProgram;
  16509. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16510. LinesToStr([
  16511. 'rtl.createClass(this, "TObject", null, function () {',
  16512. ' this.cI = 3;',
  16513. ' this.$init = function () {',
  16514. ' };',
  16515. ' this.$final = function () {',
  16516. ' };',
  16517. ' var cI$1 = 4;',
  16518. ' this.Fly = function () {',
  16519. ' if (cI$1 === this.cI) ;',
  16520. ' };',
  16521. ' var cI$2 = 5;',
  16522. ' this.Run = function () {',
  16523. ' if (cI$2 === this.cI) ;',
  16524. ' };',
  16525. '});',
  16526. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16527. ' var cI$3 = 6;',
  16528. ' this.Go = function () {',
  16529. ' if (cI$3 === this.cI) ;',
  16530. ' };',
  16531. '});',
  16532. '']),
  16533. LinesToStr([
  16534. '']));
  16535. end;
  16536. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16537. begin
  16538. StartUnit(false);
  16539. Add([
  16540. 'interface',
  16541. 'type',
  16542. ' TObject = class',
  16543. ' const cI: longint = 3;',
  16544. ' procedure Fly;',
  16545. ' procedure Run;',
  16546. ' end;',
  16547. ' TBird = class',
  16548. ' procedure Go;',
  16549. ' end;',
  16550. 'implementation',
  16551. 'procedure tobject.fly;',
  16552. 'const cI: word = 4;',
  16553. 'begin',
  16554. ' if cI=Self.cI then ;',
  16555. 'end;',
  16556. 'procedure tobject.run;',
  16557. 'const cI: word = 5;',
  16558. 'begin',
  16559. ' if cI=Self.cI then ;',
  16560. 'end;',
  16561. 'procedure tbird.go;',
  16562. 'const cI: word = 6;',
  16563. 'begin',
  16564. ' if cI=Self.cI then ;',
  16565. 'end;',
  16566. '']);
  16567. ConvertUnit;
  16568. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16569. LinesToStr([
  16570. 'rtl.createClass(this, "TObject", null, function () {',
  16571. ' this.cI = 3;',
  16572. ' this.$init = function () {',
  16573. ' };',
  16574. ' this.$final = function () {',
  16575. ' };',
  16576. ' var cI$1 = 4;',
  16577. ' this.Fly = function () {',
  16578. ' if (cI$1 === this.cI) ;',
  16579. ' };',
  16580. ' var cI$2 = 5;',
  16581. ' this.Run = function () {',
  16582. ' if (cI$2 === this.cI) ;',
  16583. ' };',
  16584. '});',
  16585. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16586. ' var cI$3 = 6;',
  16587. ' this.Go = function () {',
  16588. ' if (cI$3 === this.cI) ;',
  16589. ' };',
  16590. '});',
  16591. '']),
  16592. '',
  16593. '');
  16594. end;
  16595. procedure TTestModule.TestClass_LocalVarSelfFail;
  16596. begin
  16597. StartProgram(false);
  16598. Add([
  16599. 'type',
  16600. ' TObject = class',
  16601. ' constructor Create;',
  16602. ' end;',
  16603. 'constructor tobject.create;',
  16604. 'var self: longint;',
  16605. 'begin',
  16606. 'end',
  16607. 'begin',
  16608. '']);
  16609. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16610. ConvertProgram;
  16611. end;
  16612. procedure TTestModule.TestClass_ArgSelfFail;
  16613. begin
  16614. StartProgram(false);
  16615. Add([
  16616. 'type',
  16617. ' TObject = class',
  16618. ' procedure DoIt(Self: longint);',
  16619. ' end;',
  16620. 'procedure tobject.doit(self: longint);',
  16621. 'begin',
  16622. 'end',
  16623. 'begin',
  16624. '']);
  16625. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16626. ConvertProgram;
  16627. end;
  16628. procedure TTestModule.TestClass_NestedProcSelf;
  16629. begin
  16630. StartProgram(false);
  16631. Add([
  16632. 'type',
  16633. ' TObject = class',
  16634. ' Key: longint;',
  16635. ' class var State: longint;',
  16636. ' procedure DoIt;',
  16637. ' function GetSize: longint; virtual; abstract;',
  16638. ' procedure SetSize(Value: longint); virtual; abstract;',
  16639. ' property Size: longint read GetSize write SetSize;',
  16640. ' end;',
  16641. 'procedure tobject.doit;',
  16642. ' procedure Sub;',
  16643. ' begin',
  16644. ' key:=key+2;',
  16645. ' self.key:=self.key+3;',
  16646. ' state:=state+4;',
  16647. ' self.state:=self.state+5;',
  16648. ' tobject.state:=tobject.state+6;',
  16649. ' size:=size+7;',
  16650. ' self.size:=self.size+8;',
  16651. ' end;',
  16652. 'begin',
  16653. ' sub;',
  16654. ' key:=key+12;',
  16655. ' self.key:=self.key+13;',
  16656. ' state:=state+14;',
  16657. ' self.state:=self.state+15;',
  16658. ' tobject.state:=tobject.state+16;',
  16659. ' size:=size+17;',
  16660. ' self.size:=self.size+18;',
  16661. 'end;',
  16662. 'begin',
  16663. '']);
  16664. ConvertProgram;
  16665. CheckSource('TestClass_NestedProcSelf',
  16666. LinesToStr([ // statements
  16667. 'rtl.createClass(this, "TObject", null, function () {',
  16668. ' this.State = 0;',
  16669. ' this.$init = function () {',
  16670. ' this.Key = 0;',
  16671. ' };',
  16672. ' this.$final = function () {',
  16673. ' };',
  16674. ' this.DoIt = function () {',
  16675. ' var $Self = this;',
  16676. ' function Sub() {',
  16677. ' $Self.Key = $Self.Key + 2;',
  16678. ' $Self.Key = $Self.Key + 3;',
  16679. ' $mod.TObject.State = $Self.State + 4;',
  16680. ' $mod.TObject.State = $Self.State + 5;',
  16681. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16682. ' $Self.SetSize($Self.GetSize() + 7);',
  16683. ' $Self.SetSize($Self.GetSize() + 8);',
  16684. ' };',
  16685. ' Sub();',
  16686. ' this.Key = this.Key + 12;',
  16687. ' $Self.Key = $Self.Key + 13;',
  16688. ' $mod.TObject.State = this.State + 14;',
  16689. ' $mod.TObject.State = $Self.State + 15;',
  16690. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16691. ' this.SetSize(this.GetSize() + 17);',
  16692. ' $Self.SetSize($Self.GetSize() + 18);',
  16693. ' };',
  16694. '});',
  16695. '']),
  16696. LinesToStr([ // $mod.$main
  16697. '']));
  16698. end;
  16699. procedure TTestModule.TestClass_NestedProcSelf2;
  16700. begin
  16701. StartProgram(false);
  16702. Add([
  16703. 'type',
  16704. ' TObject = class',
  16705. ' Key: longint;',
  16706. ' class var State: longint;',
  16707. ' function GetSize: longint; virtual; abstract;',
  16708. ' procedure SetSize(Value: longint); virtual; abstract;',
  16709. ' property Size: longint read GetSize write SetSize;',
  16710. ' end;',
  16711. ' TBird = class',
  16712. ' procedure DoIt;',
  16713. ' end;',
  16714. 'procedure tbird.doit;',
  16715. ' procedure Sub;',
  16716. ' begin',
  16717. ' key:=key+2;',
  16718. ' self.key:=self.key+3;',
  16719. ' state:=state+4;',
  16720. ' self.state:=self.state+5;',
  16721. ' tobject.state:=tobject.state+6;',
  16722. ' size:=size+7;',
  16723. ' self.size:=self.size+8;',
  16724. ' end;',
  16725. 'begin',
  16726. ' sub;',
  16727. ' key:=key+12;',
  16728. ' self.key:=self.key+13;',
  16729. ' state:=state+14;',
  16730. ' self.state:=self.state+15;',
  16731. ' tobject.state:=tobject.state+16;',
  16732. ' size:=size+17;',
  16733. ' self.size:=self.size+18;',
  16734. 'end;',
  16735. 'begin',
  16736. '']);
  16737. ConvertProgram;
  16738. CheckSource('TestClass_NestedProcSelf2',
  16739. LinesToStr([ // statements
  16740. 'rtl.createClass(this, "TObject", null, function () {',
  16741. ' this.State = 0;',
  16742. ' this.$init = function () {',
  16743. ' this.Key = 0;',
  16744. ' };',
  16745. ' this.$final = function () {',
  16746. ' };',
  16747. '});',
  16748. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16749. ' this.DoIt = function () {',
  16750. ' var $Self = this;',
  16751. ' function Sub() {',
  16752. ' $Self.Key = $Self.Key + 2;',
  16753. ' $Self.Key = $Self.Key + 3;',
  16754. ' $mod.TObject.State = $Self.State + 4;',
  16755. ' $mod.TObject.State = $Self.State + 5;',
  16756. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16757. ' $Self.SetSize($Self.GetSize() + 7);',
  16758. ' $Self.SetSize($Self.GetSize() + 8);',
  16759. ' };',
  16760. ' Sub();',
  16761. ' this.Key = this.Key + 12;',
  16762. ' $Self.Key = $Self.Key + 13;',
  16763. ' $mod.TObject.State = this.State + 14;',
  16764. ' $mod.TObject.State = $Self.State + 15;',
  16765. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16766. ' this.SetSize(this.GetSize() + 17);',
  16767. ' $Self.SetSize($Self.GetSize() + 18);',
  16768. ' };',
  16769. '});',
  16770. '']),
  16771. LinesToStr([ // $mod.$main
  16772. '']));
  16773. end;
  16774. procedure TTestModule.TestClass_NestedProcClassSelf;
  16775. begin
  16776. StartProgram(false);
  16777. Add([
  16778. 'type',
  16779. ' TObject = class',
  16780. ' class var State: longint;',
  16781. ' class procedure DoIt;',
  16782. ' class function GetSize: longint; virtual; abstract;',
  16783. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16784. ' class property Size: longint read GetSize write SetSize;',
  16785. ' end;',
  16786. 'class procedure tobject.doit;',
  16787. ' procedure Sub;',
  16788. ' begin',
  16789. ' state:=state+2;',
  16790. ' self.state:=self.state+3;',
  16791. ' tobject.state:=tobject.state+4;',
  16792. ' size:=size+5;',
  16793. ' self.size:=self.size+6;',
  16794. ' tobject.size:=tobject.size+7;',
  16795. ' end;',
  16796. 'begin',
  16797. ' sub;',
  16798. ' state:=state+12;',
  16799. ' self.state:=self.state+13;',
  16800. ' tobject.state:=tobject.state+14;',
  16801. ' size:=size+15;',
  16802. ' self.size:=self.size+16;',
  16803. ' tobject.size:=tobject.size+17;',
  16804. 'end;',
  16805. 'begin',
  16806. '']);
  16807. ConvertProgram;
  16808. CheckSource('TestClass_NestedProcClassSelf',
  16809. LinesToStr([ // statements
  16810. 'rtl.createClass(this, "TObject", null, function () {',
  16811. ' this.State = 0;',
  16812. ' this.$init = function () {',
  16813. ' };',
  16814. ' this.$final = function () {',
  16815. ' };',
  16816. ' this.DoIt = function () {',
  16817. ' var $Self = this;',
  16818. ' function Sub() {',
  16819. ' $mod.TObject.State = $Self.State + 2;',
  16820. ' $mod.TObject.State = $Self.State + 3;',
  16821. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16822. ' $Self.SetSize($Self.GetSize() + 5);',
  16823. ' $Self.SetSize($Self.GetSize() + 6);',
  16824. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16825. ' };',
  16826. ' Sub();',
  16827. ' $mod.TObject.State = this.State + 12;',
  16828. ' $mod.TObject.State = $Self.State + 13;',
  16829. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16830. ' this.SetSize(this.GetSize() + 15);',
  16831. ' $Self.SetSize($Self.GetSize() + 16);',
  16832. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16833. ' };',
  16834. '});',
  16835. '']),
  16836. LinesToStr([ // $mod.$main
  16837. '']));
  16838. end;
  16839. procedure TTestModule.TestClass_NestedProcCallInherited;
  16840. begin
  16841. StartProgram(false);
  16842. Add([
  16843. 'type',
  16844. ' TObject = class',
  16845. ' function DoIt(k: boolean): longint; virtual;',
  16846. ' end;',
  16847. ' TBird = class',
  16848. ' function DoIt(k: boolean): longint; override;',
  16849. ' end;',
  16850. 'function tobject.doit(k: boolean): longint;',
  16851. 'begin',
  16852. 'end;',
  16853. 'function tbird.doit(k: boolean): longint;',
  16854. ' procedure Sub;',
  16855. ' begin',
  16856. ' inherited DoIt(true);',
  16857. //' if inherited DoIt(false)=4 then ;',
  16858. ' end;',
  16859. 'begin',
  16860. ' Sub;',
  16861. ' inherited;',
  16862. ' inherited DoIt(true);',
  16863. //' if inherited DoIt(false)=14 then ;',
  16864. 'end;',
  16865. 'begin',
  16866. '']);
  16867. ConvertProgram;
  16868. CheckSource('TestClass_NestedProcCallInherited',
  16869. LinesToStr([ // statements
  16870. 'rtl.createClass(this, "TObject", null, function () {',
  16871. ' this.$init = function () {',
  16872. ' };',
  16873. ' this.$final = function () {',
  16874. ' };',
  16875. ' this.DoIt = function (k) {',
  16876. ' var Result = 0;',
  16877. ' return Result;',
  16878. ' };',
  16879. '});',
  16880. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16881. ' this.DoIt = function (k) {',
  16882. ' var $Self = this;',
  16883. ' var Result = 0;',
  16884. ' function Sub() {',
  16885. ' $mod.TObject.DoIt.call($Self, true);',
  16886. ' };',
  16887. ' Sub();',
  16888. ' $mod.TObject.DoIt.apply(this, arguments);',
  16889. ' $mod.TObject.DoIt.call(this, true);',
  16890. ' return Result;',
  16891. ' };',
  16892. '});',
  16893. '']),
  16894. LinesToStr([ // $mod.$main
  16895. '']));
  16896. end;
  16897. procedure TTestModule.TestClass_TObjectFree;
  16898. begin
  16899. StartProgram(false);
  16900. Add([
  16901. 'type',
  16902. ' TObject = class',
  16903. ' Obj: tobject;',
  16904. ' procedure Free;',
  16905. ' procedure Release;',
  16906. ' end;',
  16907. 'procedure tobject.free;',
  16908. 'begin',
  16909. 'end;',
  16910. 'procedure tobject.release;',
  16911. 'begin',
  16912. ' free;',
  16913. ' if true then free;',
  16914. 'end;',
  16915. 'function DoIt(o: tobject): tobject;',
  16916. 'var l: tobject;',
  16917. 'begin',
  16918. ' o.free;',
  16919. ' o.free();',
  16920. ' l.free;',
  16921. ' l.free();',
  16922. ' o.obj.free;',
  16923. ' o.obj.free();',
  16924. ' with o do obj.free;',
  16925. ' with o do obj.free();',
  16926. ' result.Free;',
  16927. ' result.Free();',
  16928. 'end;',
  16929. 'var o: tobject;',
  16930. ' a: array of tobject;',
  16931. 'begin',
  16932. ' o.free;',
  16933. ' o.obj.free;',
  16934. ' a[1+2].free;',
  16935. '']);
  16936. ConvertProgram;
  16937. CheckSource('TestClass_TObjectFree',
  16938. LinesToStr([ // statements
  16939. 'rtl.createClass(this, "TObject", null, function () {',
  16940. ' this.$init = function () {',
  16941. ' this.Obj = null;',
  16942. ' };',
  16943. ' this.$final = function () {',
  16944. ' this.Obj = undefined;',
  16945. ' };',
  16946. ' this.Free = function () {',
  16947. ' };',
  16948. ' this.Release = function () {',
  16949. ' this.Free();',
  16950. ' if (true) this.Free();',
  16951. ' };',
  16952. '});',
  16953. 'this.DoIt = function (o) {',
  16954. ' var Result = null;',
  16955. ' var l = null;',
  16956. ' o = rtl.freeLoc(o);',
  16957. ' o = rtl.freeLoc(o);',
  16958. ' l = rtl.freeLoc(l);',
  16959. ' l = rtl.freeLoc(l);',
  16960. ' rtl.free(o, "Obj");',
  16961. ' rtl.free(o, "Obj");',
  16962. ' rtl.free(o, "Obj");',
  16963. ' rtl.free(o, "Obj");',
  16964. ' Result = rtl.freeLoc(Result);',
  16965. ' Result = rtl.freeLoc(Result);',
  16966. ' return Result;',
  16967. '};',
  16968. 'this.o = null;',
  16969. 'this.a = [];',
  16970. '']),
  16971. LinesToStr([ // $mod.$main
  16972. 'rtl.free($mod, "o");',
  16973. 'rtl.free($mod.o, "Obj");',
  16974. 'rtl.free($mod.a, 1 + 2);',
  16975. '']));
  16976. end;
  16977. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16978. begin
  16979. StartProgram(false);
  16980. Add([
  16981. 'type',
  16982. ' TObject = class',
  16983. ' Obj: tobject;',
  16984. ' procedure Free;',
  16985. ' end;',
  16986. 'procedure tobject.free;',
  16987. 'begin',
  16988. 'end;',
  16989. 'procedure DoIt(var o: tobject);',
  16990. 'begin',
  16991. ' o.free;',
  16992. ' o.free();',
  16993. 'end;',
  16994. 'begin',
  16995. '']);
  16996. ConvertProgram;
  16997. CheckSource('TestClass_TObjectFree_VarArg',
  16998. LinesToStr([ // statements
  16999. 'rtl.createClass(this, "TObject", null, function () {',
  17000. ' this.$init = function () {',
  17001. ' this.Obj = null;',
  17002. ' };',
  17003. ' this.$final = function () {',
  17004. ' this.Obj = undefined;',
  17005. ' };',
  17006. ' this.Free = function () {',
  17007. ' };',
  17008. '});',
  17009. 'this.DoIt = function (o) {',
  17010. ' o.set(rtl.freeLoc(o.get()));',
  17011. ' o.set(rtl.freeLoc(o.get()));',
  17012. '};',
  17013. '']),
  17014. LinesToStr([ // $mod.$main
  17015. '']));
  17016. end;
  17017. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17018. begin
  17019. StartProgram(false);
  17020. Add([
  17021. 'type',
  17022. ' TObject = class',
  17023. ' constructor Create;',
  17024. ' procedure Free;',
  17025. ' end;',
  17026. 'constructor TObject.Create; begin end;',
  17027. 'procedure tobject.free; begin end;',
  17028. 'begin',
  17029. ' with tobject.create do free;',
  17030. '']);
  17031. ConvertProgram;
  17032. CheckSource('TestClass_TObjectFreeNewInstance',
  17033. LinesToStr([ // statements
  17034. 'rtl.createClass(this, "TObject", null, function () {',
  17035. ' this.$init = function () {',
  17036. ' };',
  17037. ' this.$final = function () {',
  17038. ' };',
  17039. ' this.Create = function () {',
  17040. ' return this;',
  17041. ' };',
  17042. ' this.Free = function () {',
  17043. ' };',
  17044. '});',
  17045. '']),
  17046. LinesToStr([ // $mod.$main
  17047. 'var $with = $mod.TObject.$create("Create");',
  17048. '$with=rtl.freeLoc($with);',
  17049. '']));
  17050. end;
  17051. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17052. begin
  17053. StartProgram(false);
  17054. Add([
  17055. 'type',
  17056. ' TObject = class',
  17057. ' destructor Destroy;',
  17058. ' procedure Free;',
  17059. ' end;',
  17060. 'destructor TObject.Destroy; begin end;',
  17061. 'procedure tobject.free; begin end;',
  17062. 'var o: tobject;',
  17063. 'begin',
  17064. ' o.free;',
  17065. '']);
  17066. Converter.UseLowerCase:=true;
  17067. ConvertProgram;
  17068. CheckSource('TestClass_TObjectFreeLowerCase',
  17069. LinesToStr([ // statements
  17070. 'rtl.createClass(this, "tobject", null, function () {',
  17071. ' this.$init = function () {',
  17072. ' };',
  17073. ' this.$final = function () {',
  17074. ' };',
  17075. ' rtl.tObjectDestroy = "destroy";',
  17076. ' this.destroy = function () {',
  17077. ' };',
  17078. ' this.free = function () {',
  17079. ' };',
  17080. '});',
  17081. 'this.o = null;',
  17082. '']),
  17083. LinesToStr([ // $mod.$main
  17084. 'rtl.free($mod, "o");',
  17085. '']));
  17086. end;
  17087. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17088. begin
  17089. StartProgram(false);
  17090. Add([
  17091. 'type',
  17092. ' TObject = class',
  17093. ' procedure Free;',
  17094. ' function GetObj: tobject; virtual; abstract;',
  17095. ' end;',
  17096. 'procedure tobject.free;',
  17097. 'begin',
  17098. 'end;',
  17099. 'var o: tobject;',
  17100. 'begin',
  17101. ' o.getobj.free;',
  17102. '']);
  17103. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17104. ConvertProgram;
  17105. end;
  17106. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17107. begin
  17108. StartProgram(false);
  17109. Add([
  17110. 'type',
  17111. ' TObject = class',
  17112. ' procedure Free;',
  17113. ' FObj: TObject;',
  17114. ' property Obj: tobject read FObj write FObj;',
  17115. ' end;',
  17116. 'procedure tobject.free;',
  17117. 'begin',
  17118. 'end;',
  17119. 'var o: tobject;',
  17120. 'begin',
  17121. ' o.obj.free;',
  17122. '']);
  17123. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17124. ConvertProgram;
  17125. end;
  17126. procedure TTestModule.TestClass_ForIn;
  17127. begin
  17128. StartProgram(false);
  17129. Add([
  17130. 'type',
  17131. ' TObject = class end;',
  17132. ' TItem = TObject;',
  17133. ' TEnumerator = class',
  17134. ' FCurrent: TItem;',
  17135. ' property Current: TItem read FCurrent;',
  17136. ' function MoveNext: boolean;',
  17137. ' end;',
  17138. ' TBird = class',
  17139. ' function GetEnumerator: TEnumerator;',
  17140. ' end;',
  17141. 'function TEnumerator.MoveNext: boolean;',
  17142. 'begin',
  17143. 'end;',
  17144. 'function TBird.GetEnumerator: TEnumerator;',
  17145. 'begin',
  17146. 'end;',
  17147. 'var',
  17148. ' b: TBird;',
  17149. ' i, i2: TItem;',
  17150. 'begin',
  17151. ' for i in b do i2:=i;']);
  17152. ConvertProgram;
  17153. CheckSource('TestClass_ForIn',
  17154. LinesToStr([ // statements
  17155. 'rtl.createClass(this, "TObject", null, function () {',
  17156. ' this.$init = function () {',
  17157. ' };',
  17158. ' this.$final = function () {',
  17159. ' };',
  17160. '});',
  17161. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17162. ' this.$init = function () {',
  17163. ' $mod.TObject.$init.call(this);',
  17164. ' this.FCurrent = null;',
  17165. ' };',
  17166. ' this.$final = function () {',
  17167. ' this.FCurrent = undefined;',
  17168. ' $mod.TObject.$final.call(this);',
  17169. ' };',
  17170. ' this.MoveNext = function () {',
  17171. ' var Result = false;',
  17172. ' return Result;',
  17173. ' };',
  17174. '});',
  17175. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17176. ' this.GetEnumerator = function () {',
  17177. ' var Result = null;',
  17178. ' return Result;',
  17179. ' };',
  17180. '});',
  17181. 'this.b = null;',
  17182. 'this.i = null;',
  17183. 'this.i2 = null;'
  17184. ]),
  17185. LinesToStr([ // $mod.$main
  17186. 'var $in = $mod.b.GetEnumerator();',
  17187. 'try {',
  17188. ' while ($in.MoveNext()){',
  17189. ' $mod.i = $in.FCurrent;',
  17190. ' $mod.i2 = $mod.i;',
  17191. ' }',
  17192. '} finally {',
  17193. ' $in = rtl.freeLoc($in)',
  17194. '};',
  17195. '']));
  17196. end;
  17197. procedure TTestModule.TestClass_DispatchMessage;
  17198. begin
  17199. StartProgram(false);
  17200. Add([
  17201. 'type',
  17202. ' {$DispatchField DispInt}',
  17203. ' {$DispatchStrField DispStr}',
  17204. ' TObject = class',
  17205. ' procedure Dispatch(var Msg); virtual; abstract;',
  17206. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17207. ' end;',
  17208. ' THopMsg = record',
  17209. ' DispInt: longint;',
  17210. ' end;',
  17211. ' TPutMsg = record',
  17212. ' DispStr: string;',
  17213. ' end;',
  17214. ' TBird = class',
  17215. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17216. ' procedure Run; overload; virtual; abstract;',
  17217. ' procedure Run(var Msg); overload; message ''Fast'';',
  17218. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17219. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17220. ' end;',
  17221. 'procedure TBird.Run(var Msg);',
  17222. 'begin',
  17223. 'end;',
  17224. 'begin',
  17225. '']);
  17226. ConvertProgram;
  17227. CheckResolverUnexpectedHints(true);
  17228. CheckSource('TestClass_Message',
  17229. LinesToStr([ // statements
  17230. 'rtl.createClass(this, "TObject", null, function () {',
  17231. ' this.$init = function () {',
  17232. ' };',
  17233. ' this.$final = function () {',
  17234. ' };',
  17235. '});',
  17236. 'rtl.recNewT(this, "THopMsg", function () {',
  17237. ' this.DispInt = 0;',
  17238. ' this.$eq = function (b) {',
  17239. ' return this.DispInt === b.DispInt;',
  17240. ' };',
  17241. ' this.$assign = function (s) {',
  17242. ' this.DispInt = s.DispInt;',
  17243. ' return this;',
  17244. ' };',
  17245. '});',
  17246. 'rtl.recNewT(this, "TPutMsg", function () {',
  17247. ' this.DispStr = "";',
  17248. ' this.$eq = function (b) {',
  17249. ' return this.DispStr === b.DispStr;',
  17250. ' };',
  17251. ' this.$assign = function (s) {',
  17252. ' this.DispStr = s.DispStr;',
  17253. ' return this;',
  17254. ' };',
  17255. '});',
  17256. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17257. ' this.Run$1 = function (Msg) {',
  17258. ' };',
  17259. ' this.$msgint = {',
  17260. ' "2": "Fly",',
  17261. ' "3": "Hop"',
  17262. ' };',
  17263. ' this.$msgstr = {',
  17264. ' Fast: "Run$1",',
  17265. ' foo: "Put"',
  17266. ' };',
  17267. '});',
  17268. '']),
  17269. LinesToStr([ // $mod.$main
  17270. '']));
  17271. end;
  17272. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17273. begin
  17274. StartProgram(false);
  17275. Add([
  17276. 'type',
  17277. ' TObject = class',
  17278. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17279. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17280. ' end;',
  17281. 'begin',
  17282. '']);
  17283. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17284. ConvertProgram;
  17285. end;
  17286. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17287. begin
  17288. StartProgram(false);
  17289. Add([
  17290. 'type',
  17291. ' TObject = class',
  17292. ' procedure Dispatch(var Msg); virtual; abstract;',
  17293. ' end;',
  17294. ' TFlyMsg = record',
  17295. ' FlyId: longint;',
  17296. ' end;',
  17297. ' TBird = class',
  17298. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17299. ' end;',
  17300. 'begin',
  17301. '']);
  17302. ConvertProgram;
  17303. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17304. end;
  17305. procedure TTestModule.TestClassOf_Create;
  17306. begin
  17307. StartProgram(false);
  17308. Add('type');
  17309. Add(' TObject = class');
  17310. Add(' constructor Create;');
  17311. Add(' end;');
  17312. Add(' TClass = class of TObject;');
  17313. Add('constructor tobject.create; begin end;');
  17314. Add('var');
  17315. Add(' Obj: tobject;');
  17316. Add(' C: tclass;');
  17317. Add('begin');
  17318. Add(' obj:=C.create;');
  17319. Add(' with c do obj:=create;');
  17320. ConvertProgram;
  17321. CheckSource('TestClassOf_Create',
  17322. LinesToStr([ // statements
  17323. 'rtl.createClass(this, "TObject", null, function () {',
  17324. ' this.$init = function () {',
  17325. ' };',
  17326. ' this.$final = function () {',
  17327. ' };',
  17328. ' this.Create = function () {',
  17329. ' return this;',
  17330. ' };',
  17331. '});',
  17332. 'this.Obj = null;',
  17333. 'this.C = null;'
  17334. ]),
  17335. LinesToStr([ // $mod.$main
  17336. '$mod.Obj = $mod.C.$create("Create");',
  17337. 'var $with = $mod.C;',
  17338. '$mod.Obj = $with.$create("Create");',
  17339. '']));
  17340. end;
  17341. procedure TTestModule.TestClassOf_Call;
  17342. begin
  17343. StartProgram(false);
  17344. Add('type');
  17345. Add(' TObject = class');
  17346. Add(' class procedure DoIt;');
  17347. Add(' end;');
  17348. Add(' TClass = class of TObject;');
  17349. Add('class procedure tobject.doit; begin end;');
  17350. Add('var');
  17351. Add(' C: tclass;');
  17352. Add('begin');
  17353. Add(' c.doit;');
  17354. Add(' with c do doit;');
  17355. ConvertProgram;
  17356. CheckSource('TestClassOf_Call',
  17357. LinesToStr([ // statements
  17358. 'rtl.createClass(this, "TObject", null, function () {',
  17359. ' this.$init = function () {',
  17360. ' };',
  17361. ' this.$final = function () {',
  17362. ' };',
  17363. ' this.DoIt = function () {',
  17364. ' };',
  17365. '});',
  17366. 'this.C = null;'
  17367. ]),
  17368. LinesToStr([ // $mod.$main
  17369. '$mod.C.DoIt();',
  17370. 'var $with = $mod.C;',
  17371. '$with.DoIt();',
  17372. '']));
  17373. end;
  17374. procedure TTestModule.TestClassOf_Assign;
  17375. begin
  17376. StartProgram(false);
  17377. Add('type');
  17378. Add(' TClass = class of TObject;');
  17379. Add(' TObject = class');
  17380. Add(' ClassType: TClass; ');
  17381. Add(' end;');
  17382. Add('var');
  17383. Add(' Obj: tobject;');
  17384. Add(' C: tclass;');
  17385. Add('begin');
  17386. Add(' c:=nil;');
  17387. Add(' c:=obj.classtype;');
  17388. ConvertProgram;
  17389. CheckSource('TestClassOf_Assign',
  17390. LinesToStr([ // statements
  17391. 'rtl.createClass(this, "TObject", null, function () {',
  17392. ' this.$init = function () {',
  17393. ' this.ClassType = null;',
  17394. ' };',
  17395. ' this.$final = function () {',
  17396. ' this.ClassType = undefined;',
  17397. ' };',
  17398. '});',
  17399. 'this.Obj = null;',
  17400. 'this.C = null;'
  17401. ]),
  17402. LinesToStr([ // $mod.$main
  17403. '$mod.C = null;',
  17404. '$mod.C = $mod.Obj.ClassType;',
  17405. '']));
  17406. end;
  17407. procedure TTestModule.TestClassOf_Is;
  17408. begin
  17409. StartProgram(false);
  17410. Add('type');
  17411. Add(' TClass = class of TObject;');
  17412. Add(' TObject = class');
  17413. Add(' end;');
  17414. Add(' TCar = class');
  17415. Add(' end;');
  17416. Add(' TCars = class of TCar;');
  17417. Add('var');
  17418. Add(' Obj: tobject;');
  17419. Add(' C: tclass;');
  17420. Add(' Cars: tcars;');
  17421. Add('begin');
  17422. Add(' if c is tcar then ;');
  17423. Add(' if c is tcars then ;');
  17424. ConvertProgram;
  17425. CheckSource('TestClassOf_Is',
  17426. LinesToStr([ // statements
  17427. 'rtl.createClass(this, "TObject", null, function () {',
  17428. ' this.$init = function () {',
  17429. ' };',
  17430. ' this.$final = function () {',
  17431. ' };',
  17432. '});',
  17433. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17434. '});',
  17435. 'this.Obj = null;',
  17436. 'this.C = null;',
  17437. 'this.Cars = null;'
  17438. ]),
  17439. LinesToStr([ // $mod.$main
  17440. 'if(rtl.is($mod.C,$mod.TCar));',
  17441. 'if(rtl.is($mod.C,$mod.TCar));',
  17442. '']));
  17443. end;
  17444. procedure TTestModule.TestClassOf_Compare;
  17445. begin
  17446. StartProgram(false);
  17447. Add('type');
  17448. Add(' TClass = class of TObject;');
  17449. Add(' TObject = class');
  17450. Add(' ClassType: TClass; ');
  17451. Add(' end;');
  17452. Add('var');
  17453. Add(' b: boolean;');
  17454. Add(' Obj: tobject;');
  17455. Add(' C: tclass;');
  17456. Add('begin');
  17457. Add(' b:=c=nil;');
  17458. Add(' b:=nil=c;');
  17459. Add(' b:=c=obj.classtype;');
  17460. Add(' b:=obj.classtype=c;');
  17461. Add(' b:=c=TObject;');
  17462. Add(' b:=TObject=c;');
  17463. Add(' b:=c<>nil;');
  17464. Add(' b:=nil<>c;');
  17465. Add(' b:=c<>obj.classtype;');
  17466. Add(' b:=obj.classtype<>c;');
  17467. Add(' b:=c<>TObject;');
  17468. Add(' b:=TObject<>c;');
  17469. ConvertProgram;
  17470. CheckSource('TestClassOf_Compare',
  17471. LinesToStr([ // statements
  17472. 'rtl.createClass(this, "TObject", null, function () {',
  17473. ' this.$init = function () {',
  17474. ' this.ClassType = null;',
  17475. ' };',
  17476. ' this.$final = function () {',
  17477. ' this.ClassType = undefined;',
  17478. ' };',
  17479. '});',
  17480. 'this.b = false;',
  17481. 'this.Obj = null;',
  17482. 'this.C = null;'
  17483. ]),
  17484. LinesToStr([ // $mod.$main
  17485. '$mod.b = $mod.C === null;',
  17486. '$mod.b = null === $mod.C;',
  17487. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17488. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17489. '$mod.b = $mod.C === $mod.TObject;',
  17490. '$mod.b = $mod.TObject === $mod.C;',
  17491. '$mod.b = $mod.C !== null;',
  17492. '$mod.b = null !== $mod.C;',
  17493. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17494. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17495. '$mod.b = $mod.C !== $mod.TObject;',
  17496. '$mod.b = $mod.TObject !== $mod.C;',
  17497. '']));
  17498. end;
  17499. procedure TTestModule.TestClassOf_ClassVar;
  17500. begin
  17501. StartProgram(false);
  17502. Add('type');
  17503. Add(' TObject = class');
  17504. Add(' class var id: longint;');
  17505. Add(' end;');
  17506. Add(' TClass = class of TObject;');
  17507. Add('var');
  17508. Add(' C: tclass;');
  17509. Add('begin');
  17510. Add(' C.id:=C.id;');
  17511. ConvertProgram;
  17512. CheckSource('TestClassOf_ClassVar',
  17513. LinesToStr([ // statements
  17514. 'rtl.createClass(this, "TObject", null, function () {',
  17515. ' this.id = 0;',
  17516. ' this.$init = function () {',
  17517. ' };',
  17518. ' this.$final = function () {',
  17519. ' };',
  17520. '});',
  17521. 'this.C = null;'
  17522. ]),
  17523. LinesToStr([ // $mod.$main
  17524. '$mod.TObject.id = $mod.C.id;',
  17525. '']));
  17526. end;
  17527. procedure TTestModule.TestClassOf_ClassMethod;
  17528. begin
  17529. StartProgram(false);
  17530. Add('type');
  17531. Add(' TObject = class');
  17532. Add(' class function DoIt(i: longint = 0): longint;');
  17533. Add(' end;');
  17534. Add(' TClass = class of TObject;');
  17535. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17536. Add('var');
  17537. Add(' i: longint;');
  17538. Add(' C: tclass;');
  17539. Add('begin');
  17540. Add(' C.DoIt;');
  17541. Add(' C.DoIt();');
  17542. Add(' i:=C.DoIt;');
  17543. Add(' i:=C.DoIt();');
  17544. ConvertProgram;
  17545. CheckSource('TestClassOf_ClassMethod',
  17546. LinesToStr([ // statements
  17547. 'rtl.createClass(this, "TObject", null, function () {',
  17548. ' this.$init = function () {',
  17549. ' };',
  17550. ' this.$final = function () {',
  17551. ' };',
  17552. ' this.DoIt = function (i) {',
  17553. ' var Result = 0;',
  17554. ' return Result;',
  17555. ' };',
  17556. '});',
  17557. 'this.i = 0;',
  17558. 'this.C = null;'
  17559. ]),
  17560. LinesToStr([ // $mod.$main
  17561. '$mod.C.DoIt(0);',
  17562. '$mod.C.DoIt(0);',
  17563. '$mod.i = $mod.C.DoIt(0);',
  17564. '$mod.i = $mod.C.DoIt(0);',
  17565. '']));
  17566. end;
  17567. procedure TTestModule.TestClassOf_ClassProperty;
  17568. begin
  17569. StartProgram(false);
  17570. Add([
  17571. 'type',
  17572. ' TObject = class',
  17573. ' class var FA: longint;',
  17574. ' class function GetA: longint;',
  17575. ' class procedure SetA(Value: longint);',
  17576. ' class property pA: longint read fa write fa;',
  17577. ' class property pB: longint read geta write seta;',
  17578. ' end;',
  17579. ' TObjectClass = class of tobject;',
  17580. 'class function tobject.geta: longint; begin end;',
  17581. 'class procedure tobject.seta(value: longint); begin end;',
  17582. 'var',
  17583. ' b: boolean;',
  17584. ' Obj: tobject;',
  17585. ' Cla: tobjectclass;',
  17586. 'begin',
  17587. ' obj.pa:=obj.pa;',
  17588. ' obj.pb:=obj.pb;',
  17589. ' b:=obj.pa=4;',
  17590. ' b:=obj.pb=obj.pb;',
  17591. ' b:=5=obj.pa;',
  17592. ' cla.pa:=6;',
  17593. ' cla.pa:=cla.pa;',
  17594. ' cla.pb:=cla.pb;',
  17595. ' b:=cla.pa=7;',
  17596. ' b:=cla.pb=cla.pb;',
  17597. ' b:=8=cla.pa;',
  17598. ' tobject.pa:=9;',
  17599. ' tobject.pb:=tobject.pb;',
  17600. ' b:=tobject.pa=10;',
  17601. ' b:=11=tobject.pa;',
  17602. '']);
  17603. ConvertProgram;
  17604. CheckSource('TestClassOf_ClassProperty',
  17605. LinesToStr([ // statements
  17606. 'rtl.createClass(this, "TObject", null, function () {',
  17607. ' this.FA = 0;',
  17608. ' this.$init = function () {',
  17609. ' };',
  17610. ' this.$final = function () {',
  17611. ' };',
  17612. ' this.GetA = function () {',
  17613. ' var Result = 0;',
  17614. ' return Result;',
  17615. ' };',
  17616. ' this.SetA = function (Value) {',
  17617. ' };',
  17618. '});',
  17619. 'this.b = false;',
  17620. 'this.Obj = null;',
  17621. 'this.Cla = null;'
  17622. ]),
  17623. LinesToStr([ // $mod.$main
  17624. '$mod.TObject.FA = $mod.Obj.FA;',
  17625. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17626. '$mod.b = $mod.Obj.FA === 4;',
  17627. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17628. '$mod.b = 5 === $mod.Obj.FA;',
  17629. '$mod.TObject.FA = 6;',
  17630. '$mod.TObject.FA = $mod.Cla.FA;',
  17631. '$mod.Cla.SetA($mod.Cla.GetA());',
  17632. '$mod.b = $mod.Cla.FA === 7;',
  17633. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17634. '$mod.b = 8 === $mod.Cla.FA;',
  17635. '$mod.TObject.FA = 9;',
  17636. '$mod.TObject.SetA($mod.TObject.GetA());',
  17637. '$mod.b = $mod.TObject.FA === 10;',
  17638. '$mod.b = 11 === $mod.TObject.FA;',
  17639. '']));
  17640. end;
  17641. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17642. begin
  17643. StartProgram(false);
  17644. Add('type');
  17645. Add(' TObject = class');
  17646. Add(' class var GlobalId: longint;');
  17647. Add(' class procedure ProcA;');
  17648. Add(' end;');
  17649. Add('class procedure tobject.proca;');
  17650. Add('var b: boolean;');
  17651. Add('begin');
  17652. Add(' b:=self=nil;');
  17653. Add(' b:=self.globalid=3;');
  17654. Add(' b:=4=self.globalid;');
  17655. Add(' self.globalid:=5;');
  17656. Add(' self.proca;');
  17657. Add('end;');
  17658. Add('begin');
  17659. ConvertProgram;
  17660. CheckSource('TestClassOf_ClassMethodSelf',
  17661. LinesToStr([ // statements
  17662. 'rtl.createClass(this, "TObject", null, function () {',
  17663. ' this.GlobalId = 0;',
  17664. ' this.$init = function () {',
  17665. ' };',
  17666. ' this.$final = function () {',
  17667. ' };',
  17668. ' this.ProcA = function () {',
  17669. ' var b = false;',
  17670. ' b = this === null;',
  17671. ' b = this.GlobalId === 3;',
  17672. ' b = 4 === this.GlobalId;',
  17673. ' $mod.TObject.GlobalId = 5;',
  17674. ' this.ProcA();',
  17675. ' };',
  17676. '});'
  17677. ]),
  17678. LinesToStr([ // $mod.$main
  17679. '']));
  17680. end;
  17681. procedure TTestModule.TestClassOf_TypeCast;
  17682. begin
  17683. StartProgram(false);
  17684. Add('type');
  17685. Add(' TObject = class');
  17686. Add(' class procedure {#TObject_DoIt}DoIt;');
  17687. Add(' end;');
  17688. Add(' TClass = class of TObject;');
  17689. Add(' TMobile = class');
  17690. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17691. Add(' end;');
  17692. Add(' TMobileClass = class of TMobile;');
  17693. Add(' TCar = class(TMobile)');
  17694. Add(' class procedure {#TCar_DoIt}DoIt;');
  17695. Add(' end;');
  17696. Add(' TCarClass = class of TCar;');
  17697. Add('class procedure TObject.DoIt;');
  17698. Add('begin');
  17699. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17700. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17701. Add('end;');
  17702. Add('class procedure TMobile.DoIt;');
  17703. Add('begin');
  17704. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17705. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17706. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17707. Add('end;');
  17708. Add('class procedure TCar.DoIt; begin end;');
  17709. Add('var');
  17710. Add(' ObjC: TClass;');
  17711. Add(' MobileC: TMobileClass;');
  17712. Add(' CarC: TCarClass;');
  17713. Add('begin');
  17714. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17715. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17716. Add(' CarC.{@TCar_DoIt}DoIt;');
  17717. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17718. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17719. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17720. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17721. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17722. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17723. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17724. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17725. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17726. ConvertProgram;
  17727. CheckSource('TestClassOf_TypeCast',
  17728. LinesToStr([ // statements
  17729. 'rtl.createClass(this, "TObject", null, function () {',
  17730. ' this.$init = function () {',
  17731. ' };',
  17732. ' this.$final = function () {',
  17733. ' };',
  17734. ' this.DoIt = function () {',
  17735. ' this.DoIt();',
  17736. ' this.DoIt$1();',
  17737. ' };',
  17738. '});',
  17739. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17740. ' this.DoIt$1 = function () {',
  17741. ' this.DoIt();',
  17742. ' this.DoIt$1();',
  17743. ' this.DoIt$2();',
  17744. ' };',
  17745. '});',
  17746. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17747. ' this.DoIt$2 = function () {',
  17748. ' };',
  17749. '});',
  17750. 'this.ObjC = null;',
  17751. 'this.MobileC = null;',
  17752. 'this.CarC = null;',
  17753. '']),
  17754. LinesToStr([ // $mod.$main
  17755. '$mod.ObjC.DoIt();',
  17756. '$mod.MobileC.DoIt$1();',
  17757. '$mod.CarC.DoIt$2();',
  17758. '$mod.ObjC.DoIt();',
  17759. '$mod.ObjC.DoIt$1();',
  17760. '$mod.ObjC.DoIt$2();',
  17761. '$mod.MobileC.DoIt();',
  17762. '$mod.MobileC.DoIt$1();',
  17763. '$mod.MobileC.DoIt$2();',
  17764. '$mod.CarC.DoIt();',
  17765. '$mod.CarC.DoIt$1();',
  17766. '$mod.CarC.DoIt$2();',
  17767. '']));
  17768. end;
  17769. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17770. begin
  17771. StartProgram(false);
  17772. Add('type');
  17773. Add(' TObject = class');
  17774. Add(' function CurNow: longint; ');
  17775. Add(' class function Now: longint; ');
  17776. Add(' end;');
  17777. Add('function TObject.CurNow: longint; begin end;');
  17778. Add('class function TObject.Now: longint; begin end;');
  17779. Add('var');
  17780. Add(' Obj: tobject;');
  17781. Add(' vI: longint;');
  17782. Add('begin');
  17783. Add(' obj.curnow;');
  17784. Add(' vi:=obj.curnow;');
  17785. Add(' tobject.now;');
  17786. Add(' vi:=tobject.now;');
  17787. ConvertProgram;
  17788. CheckSource('TestClassOf_ImplicitFunctionCall',
  17789. LinesToStr([ // statements
  17790. 'rtl.createClass(this, "TObject", null, function () {',
  17791. ' this.$init = function () {',
  17792. ' };',
  17793. ' this.$final = function () {',
  17794. ' };',
  17795. ' this.CurNow = function () {',
  17796. ' var Result = 0;',
  17797. ' return Result;',
  17798. ' };',
  17799. ' this.Now = function () {',
  17800. ' var Result = 0;',
  17801. ' return Result;',
  17802. ' };',
  17803. '});',
  17804. 'this.Obj = null;',
  17805. 'this.vI = 0;',
  17806. '']),
  17807. LinesToStr([ // $mod.$main
  17808. '$mod.Obj.CurNow();',
  17809. '$mod.vI = $mod.Obj.CurNow();',
  17810. '$mod.TObject.Now();',
  17811. '$mod.vI = $mod.TObject.Now();',
  17812. '']));
  17813. end;
  17814. procedure TTestModule.TestClassOf_Const;
  17815. begin
  17816. StartProgram(false);
  17817. Add([
  17818. 'type',
  17819. ' TObject = class',
  17820. ' end;',
  17821. ' TBird = TObject;',
  17822. ' TBirds = class of TBird;',
  17823. ' TEagles = TBirds;',
  17824. ' THawk = class(TBird);',
  17825. 'const',
  17826. ' Hawk: TEagles = THawk;',
  17827. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17828. ' TBird,',
  17829. ' THawk',
  17830. ' );',
  17831. 'begin']);
  17832. ConvertProgram;
  17833. CheckSource('TestClassOf_Const',
  17834. LinesToStr([ // statements
  17835. 'rtl.createClass(this, "TObject", null, function () {',
  17836. ' this.$init = function () {',
  17837. ' };',
  17838. ' this.$final = function () {',
  17839. ' };',
  17840. '});',
  17841. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17842. '});',
  17843. 'this.Hawk = this.THawk;',
  17844. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17845. '']),
  17846. LinesToStr([ // $mod.$main
  17847. '']));
  17848. end;
  17849. procedure TTestModule.TestNestedClass_Alias;
  17850. begin
  17851. WithTypeInfo:=true;
  17852. StartProgram(false);
  17853. Add([
  17854. 'type',
  17855. ' TObject = class',
  17856. ' type TNested = type longint;',
  17857. ' end;',
  17858. 'type TAlias = type tobject.tnested;',
  17859. 'var i: tobject.tnested = 3;',
  17860. 'var j: TAlias = 4;',
  17861. 'begin',
  17862. ' if typeinfo(TAlias)=nil then ;',
  17863. ' if typeinfo(tobject.tnested)=nil then ;',
  17864. '']);
  17865. ConvertProgram;
  17866. CheckSource('TestNestedClass_Alias',
  17867. LinesToStr([ // statements
  17868. 'rtl.createClass(this, "TObject", null, function () {',
  17869. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17870. ' this.$init = function () {',
  17871. ' };',
  17872. ' this.$final = function () {',
  17873. ' };',
  17874. '});',
  17875. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17876. 'this.i = 3;',
  17877. 'this.j = 4;',
  17878. '']),
  17879. LinesToStr([ // $mod.$main
  17880. 'if ($mod.$rtti["TAlias"] === null) ;',
  17881. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17882. '']));
  17883. end;
  17884. procedure TTestModule.TestNestedClass_Record;
  17885. begin
  17886. WithTypeInfo:=true;
  17887. StartProgram(false);
  17888. Add([
  17889. 'type',
  17890. ' TObject = class',
  17891. ' type TPoint = record',
  17892. ' x,y: byte;',
  17893. ' end;',
  17894. ' procedure DoIt(t: TPoint);',
  17895. ' end;',
  17896. 'procedure tobject.DoIt(t: TPoint);',
  17897. 'var p: TPoint;',
  17898. 'begin',
  17899. ' t.x:=t.y;',
  17900. ' p:=t;',
  17901. 'end;',
  17902. 'var',
  17903. ' p: tobject.tpoint = (x:2; y:4);',
  17904. ' o: TObject;',
  17905. 'begin',
  17906. ' p:=p;',
  17907. ' o.doit(p);',
  17908. '']);
  17909. ConvertProgram;
  17910. CheckSource('TestNestedClass_Record',
  17911. LinesToStr([ // statements
  17912. 'rtl.createClass(this, "TObject", null, function () {',
  17913. ' rtl.recNewT(this, "TPoint", function () {',
  17914. ' this.x = 0;',
  17915. ' this.y = 0;',
  17916. ' this.$eq = function (b) {',
  17917. ' return (this.x === b.x) && (this.y === b.y);',
  17918. ' };',
  17919. ' this.$assign = function (s) {',
  17920. ' this.x = s.x;',
  17921. ' this.y = s.y;',
  17922. ' return this;',
  17923. ' };',
  17924. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17925. ' $r.addField("x", rtl.byte);',
  17926. ' $r.addField("y", rtl.byte);',
  17927. ' });',
  17928. ' this.$init = function () {',
  17929. ' };',
  17930. ' this.$final = function () {',
  17931. ' };',
  17932. ' this.DoIt = function (t) {',
  17933. ' var p = this.TPoint.$new();',
  17934. ' t.x = t.y;',
  17935. ' p.$assign(t);',
  17936. ' };',
  17937. '});',
  17938. 'this.p = this.TObject.TPoint.$clone({',
  17939. ' x: 2,',
  17940. ' y: 4',
  17941. '});',
  17942. 'this.o = null;',
  17943. '']),
  17944. LinesToStr([ // $mod.$main
  17945. '$mod.p.$assign($mod.p);',
  17946. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17947. '']));
  17948. end;
  17949. procedure TTestModule.TestNestedClass_Class;
  17950. begin
  17951. StartProgram(false);
  17952. Add([
  17953. 'type',
  17954. ' TObject = class end;',
  17955. ' TBird = class',
  17956. ' type TLeg = class',
  17957. ' FId: longint;',
  17958. ' constructor Create;',
  17959. ' function Create(i: longint): TLeg;',
  17960. ' end;',
  17961. ' function DoIt(b: TBird): Tleg;',
  17962. ' end;',
  17963. 'constructor tbird.tleg.create;',
  17964. 'begin',
  17965. ' FId:=3;',
  17966. 'end;',
  17967. 'function tbird.tleg.Create(i: longint): TLeg;',
  17968. 'begin',
  17969. ' Create;',
  17970. ' Result:=TLeg.Create;',
  17971. ' Result:=TBird.TLeg.Create;',
  17972. ' Result:=Create(3);',
  17973. ' FId:=i;',
  17974. 'end;',
  17975. 'function tbird.DoIt(b: tbird): tleg;',
  17976. 'begin',
  17977. ' Result.Create;',
  17978. ' Result:=TLeg.Create;',
  17979. ' Result:=TBird.TLeg.Create;',
  17980. ' Result:=Result.Create(3);',
  17981. 'end;',
  17982. 'var',
  17983. ' b: Tbird.tleg;',
  17984. 'begin',
  17985. ' b.Create;',
  17986. ' b:=TBird.TLeg.Create;',
  17987. ' b:=b.Create(3);',
  17988. '']);
  17989. ConvertProgram;
  17990. CheckSource('TestNestedClass_Class',
  17991. LinesToStr([ // statements
  17992. 'rtl.createClass(this, "TObject", null, function () {',
  17993. ' this.$init = function () {',
  17994. ' };',
  17995. ' this.$final = function () {',
  17996. ' };',
  17997. '});',
  17998. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17999. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18000. ' this.$init = function () {',
  18001. ' $mod.TObject.$init.call(this);',
  18002. ' this.FId = 0;',
  18003. ' };',
  18004. ' this.Create = function () {',
  18005. ' this.FId = 3;',
  18006. ' return this;',
  18007. ' };',
  18008. ' this.Create$1 = function (i) {',
  18009. ' var Result = null;',
  18010. ' this.Create();',
  18011. ' Result = $mod.TBird.TLeg.$create("Create");',
  18012. ' Result = $mod.TBird.TLeg.$create("Create");',
  18013. ' Result = this.Create$1(3);',
  18014. ' this.FId = i;',
  18015. ' return Result;',
  18016. ' };',
  18017. ' }, "TBird.TLeg");',
  18018. ' this.DoIt = function (b) {',
  18019. ' var Result = null;',
  18020. ' Result.Create();',
  18021. ' Result = this.TLeg.$create("Create");',
  18022. ' Result = $mod.TBird.TLeg.$create("Create");',
  18023. ' Result = Result.Create$1(3);',
  18024. ' return Result;',
  18025. ' };',
  18026. '});',
  18027. 'this.b = null;',
  18028. '']),
  18029. LinesToStr([ // $mod.$main
  18030. '$mod.b.Create();',
  18031. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18032. '$mod.b = $mod.b.Create$1(3);',
  18033. '']));
  18034. end;
  18035. procedure TTestModule.TestNestedClass_CallInherited;
  18036. begin
  18037. StartProgram(false);
  18038. Add([
  18039. 'type',
  18040. ' TObject = class end;',
  18041. ' TBird = class',
  18042. ' type',
  18043. ' TWing = class',
  18044. ' function Fly(w: word = 17): word; virtual;',
  18045. ' end;',
  18046. ' end;',
  18047. ' TEagle = class(TBird)',
  18048. ' type',
  18049. ' TEagleWing = class(TWing)',
  18050. ' function Fly(w: word): word; override;',
  18051. ' end;',
  18052. ' end;',
  18053. 'function TBird.TWing.Fly(w: word): word;',
  18054. 'begin',
  18055. 'end;',
  18056. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18057. 'begin',
  18058. ' inherited;',
  18059. ' inherited Fly;',
  18060. ' inherited Fly(3);',
  18061. ' Result:=inherited Fly;',
  18062. ' Result:=inherited Fly(4);',
  18063. 'end;',
  18064. 'begin',
  18065. '']);
  18066. ConvertProgram;
  18067. CheckSource('TestNestedClass_CallInherited',
  18068. LinesToStr([ // statements
  18069. 'rtl.createClass(this, "TObject", null, function () {',
  18070. ' this.$init = function () {',
  18071. ' };',
  18072. ' this.$final = function () {',
  18073. ' };',
  18074. '});',
  18075. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18076. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18077. ' this.Fly = function (w) {',
  18078. ' var Result = 0;',
  18079. ' return Result;',
  18080. ' };',
  18081. ' }, "TBird.TWing");',
  18082. '});',
  18083. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18084. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18085. ' this.Fly = function (w) {',
  18086. ' var Result = 0;',
  18087. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18088. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18089. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18090. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18091. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18092. ' return Result;',
  18093. ' };',
  18094. ' }, "TEagle.TEagleWing");',
  18095. '});',
  18096. '']),
  18097. LinesToStr([ // $mod.$main
  18098. '']));
  18099. end;
  18100. procedure TTestModule.TestExternalClass_Var;
  18101. begin
  18102. StartProgram(false);
  18103. Add([
  18104. '{$modeswitch externalclass}',
  18105. 'type',
  18106. ' TExtA = class external name ''ExtObj''',
  18107. ' Id: longint external name ''$Id'';',
  18108. ' B: longint;',
  18109. ' end;',
  18110. 'var Obj: TExtA;',
  18111. 'begin',
  18112. ' obj.id:=obj.id+1;',
  18113. ' obj.B:=obj.B+1;']);
  18114. ConvertProgram;
  18115. CheckSource('TestExternalClass_Var',
  18116. LinesToStr([ // statements
  18117. 'this.Obj = null;',
  18118. '']),
  18119. LinesToStr([ // $mod.$main
  18120. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18121. '$mod.Obj.B = $mod.Obj.B + 1;',
  18122. '']));
  18123. end;
  18124. procedure TTestModule.TestExternalClass_Const;
  18125. begin
  18126. StartProgram(false);
  18127. Add([
  18128. '{$modeswitch externalclass}',
  18129. 'type',
  18130. ' TExtA = class external name ''ExtObj''',
  18131. ' const Two: longint = 2;',
  18132. ' const Three = 3;',
  18133. ' const Id: longint;',
  18134. ' end;',
  18135. ' TExtB = class external name ''ExtB''',
  18136. ' A: TExtA;',
  18137. ' end;',
  18138. 'var',
  18139. ' A: texta;',
  18140. ' B: textb;',
  18141. ' i: longint;',
  18142. 'begin',
  18143. ' i:=a.two;',
  18144. ' i:=texta.two;',
  18145. ' i:=a.three;',
  18146. ' i:=texta.three;',
  18147. ' i:=a.id;',
  18148. ' i:=texta.id;',
  18149. '']);
  18150. ConvertProgram;
  18151. CheckSource('TestExternalClass_Const',
  18152. LinesToStr([ // statements
  18153. 'this.A = null;',
  18154. 'this.B = null;',
  18155. 'this.i = 0;',
  18156. '']),
  18157. LinesToStr([ // $mod.$main
  18158. '$mod.i = 2;',
  18159. '$mod.i = 2;',
  18160. '$mod.i = 3;',
  18161. '$mod.i = 3;',
  18162. '$mod.i = $mod.A.Id;',
  18163. '$mod.i = ExtObj.Id;',
  18164. '']));
  18165. end;
  18166. procedure TTestModule.TestExternalClass_Dollar;
  18167. begin
  18168. StartProgram(false);
  18169. Add([
  18170. '{$modeswitch externalclass}',
  18171. 'type',
  18172. ' TExtA = class external name ''$''',
  18173. ' Id: longint external name ''$'';',
  18174. ' function Bla(i: longint): longint; external name ''$'';',
  18175. ' end;',
  18176. 'function dollar(k: longint): longint; external name ''$'';',
  18177. 'var Obj: TExtA;',
  18178. 'begin',
  18179. ' dollar(1);',
  18180. ' obj.id:=obj.id+2;',
  18181. ' obj.Bla(3);',
  18182. '']);
  18183. ConvertProgram;
  18184. CheckSource('TestExternalClass_Dollar',
  18185. LinesToStr([ // statements
  18186. 'this.Obj = null;',
  18187. '']),
  18188. LinesToStr([ // $mod.$main
  18189. '$(1);',
  18190. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18191. '$mod.Obj.$(3);',
  18192. '']));
  18193. end;
  18194. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18195. begin
  18196. StartProgram(false);
  18197. Add('{$modeswitch externalclass}');
  18198. Add('type');
  18199. Add(' TExtA = class external name ''ExtA''');
  18200. Add(' Id: longint external name ''$Id'';');
  18201. Add(' end;');
  18202. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18203. Add(' Id: longint;');
  18204. Add(' end;');
  18205. Add('begin');
  18206. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18207. ConvertProgram;
  18208. end;
  18209. procedure TTestModule.TestExternalClass_Method;
  18210. begin
  18211. StartProgram(false);
  18212. Add(['{$modeswitch externalclass}',
  18213. 'type',
  18214. ' TExtA = class external name ''ExtObj''',
  18215. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18216. ' procedure DoSome(Id: longint = 1);',
  18217. ' end;',
  18218. 'var Obj: texta;',
  18219. 'begin',
  18220. ' obj.doit;',
  18221. ' obj.doit();',
  18222. ' obj.doit(2);',
  18223. ' with obj do begin',
  18224. ' doit;',
  18225. ' doit();',
  18226. ' doit(3);',
  18227. ' end;']);
  18228. ConvertProgram;
  18229. CheckSource('TestExternalClass_Method',
  18230. LinesToStr([ // statements
  18231. 'this.Obj = null;',
  18232. '']),
  18233. LinesToStr([ // $mod.$main
  18234. '$mod.Obj.$Execute(1);',
  18235. '$mod.Obj.$Execute(1);',
  18236. '$mod.Obj.$Execute(2);',
  18237. 'var $with = $mod.Obj;',
  18238. '$with.$Execute(1);',
  18239. '$with.$Execute(1);',
  18240. '$with.$Execute(3);',
  18241. '']));
  18242. end;
  18243. procedure TTestModule.TestExternalClass_ClassMethod;
  18244. begin
  18245. StartProgram(false);
  18246. Add([
  18247. '{$modeswitch externalclass}',
  18248. 'type',
  18249. ' TExtA = class external name ''ExtObj''',
  18250. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18251. ' end;',
  18252. ' TExtB = TExtA;',
  18253. 'var p: Pointer;',
  18254. 'begin',
  18255. ' texta.doit;',
  18256. ' texta.doit();',
  18257. ' texta.doit(2);',
  18258. ' p:[email protected];',
  18259. ' with texta do begin',
  18260. ' doit;',
  18261. ' doit();',
  18262. ' doit(3);',
  18263. ' p:=@DoIt;',
  18264. ' end;',
  18265. ' textb.doit;',
  18266. ' textb.doit();',
  18267. ' textb.doit(4);',
  18268. ' with textb do begin',
  18269. ' doit;',
  18270. ' doit();',
  18271. ' doit(5);',
  18272. ' end;',
  18273. '']);
  18274. ConvertProgram;
  18275. CheckSource('TestExternalClass_ClassMethod',
  18276. LinesToStr([ // statements
  18277. 'this.p = null;',
  18278. '']),
  18279. LinesToStr([ // $mod.$main
  18280. 'ExtObj.$Execute(1);',
  18281. 'ExtObj.$Execute(1);',
  18282. 'ExtObj.$Execute(2);',
  18283. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18284. 'ExtObj.$Execute(1);',
  18285. 'ExtObj.$Execute(1);',
  18286. 'ExtObj.$Execute(3);',
  18287. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18288. 'ExtObj.$Execute(1);',
  18289. 'ExtObj.$Execute(1);',
  18290. 'ExtObj.$Execute(4);',
  18291. 'ExtObj.$Execute(1);',
  18292. 'ExtObj.$Execute(1);',
  18293. 'ExtObj.$Execute(5);',
  18294. '']));
  18295. end;
  18296. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18297. begin
  18298. StartProgram(false);
  18299. Add([
  18300. '{$modeswitch externalclass}',
  18301. 'type',
  18302. ' TExtA = class external name ''ExtObj''',
  18303. ' class procedure DoIt(Id: longint = 1); static;',
  18304. ' end;',
  18305. 'var p: Pointer;',
  18306. 'begin',
  18307. ' texta.doit;',
  18308. ' texta.doit();',
  18309. ' texta.doit(2);',
  18310. ' p:[email protected];',
  18311. ' with texta do begin',
  18312. ' doit;',
  18313. ' doit();',
  18314. ' doit(3);',
  18315. ' p:=@DoIt;',
  18316. ' end;',
  18317. '']);
  18318. ConvertProgram;
  18319. CheckSource('TestExternalClass_ClassMethodStatic',
  18320. LinesToStr([ // statements
  18321. 'this.p = null;',
  18322. '']),
  18323. LinesToStr([ // $mod.$main
  18324. 'ExtObj.DoIt(1);',
  18325. 'ExtObj.DoIt(1);',
  18326. 'ExtObj.DoIt(2);',
  18327. '$mod.p = ExtObj.DoIt;',
  18328. 'ExtObj.DoIt(1);',
  18329. 'ExtObj.DoIt(1);',
  18330. 'ExtObj.DoIt(3);',
  18331. '$mod.p = ExtObj.DoIt;',
  18332. '']));
  18333. end;
  18334. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18335. begin
  18336. StartProgram(false);
  18337. Add([
  18338. '{$modeswitch externalclass}',
  18339. 'type',
  18340. ' TBird = class external name ''Array''',
  18341. ' end;',
  18342. 'function GetPtr: Pointer;',
  18343. 'begin',
  18344. 'end;',
  18345. 'procedure Write(const p);',
  18346. 'begin',
  18347. 'end;',
  18348. 'procedure WriteLn; varargs;',
  18349. 'begin',
  18350. 'end;',
  18351. 'begin',
  18352. ' if TBird(GetPtr)=nil then ;',
  18353. ' Write(GetPtr);',
  18354. ' WriteLn(GetPtr);',
  18355. ' Write(TBird(GetPtr));',
  18356. ' WriteLn(TBird(GetPtr));',
  18357. '']);
  18358. ConvertProgram;
  18359. CheckSource('TestFunctionResultInTypeCast',
  18360. LinesToStr([ // statements
  18361. 'this.GetPtr = function () {',
  18362. ' var Result = null;',
  18363. ' return Result;',
  18364. '};',
  18365. 'this.Write = function (p) {',
  18366. '};',
  18367. 'this.WriteLn = function () {',
  18368. '};',
  18369. '']),
  18370. LinesToStr([
  18371. 'if ($mod.GetPtr() === null) ;',
  18372. '$mod.Write($mod.GetPtr());',
  18373. '$mod.WriteLn($mod.GetPtr());',
  18374. '$mod.Write($mod.GetPtr());',
  18375. '$mod.WriteLn($mod.GetPtr());',
  18376. '']));
  18377. end;
  18378. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18379. begin
  18380. StartProgram(false);
  18381. Add([
  18382. '{$modeswitch externalclass}',
  18383. 'type',
  18384. ' TExtA = class external name ''ExtObjA''',
  18385. ' procedure ProcA; virtual;',
  18386. ' procedure ProcB; virtual;',
  18387. ' end;',
  18388. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18389. ' end;',
  18390. ' TExtC = class (TExtB)',
  18391. ' procedure ProcA; override;',
  18392. ' end;',
  18393. 'procedure TExtC.ProcA;',
  18394. 'begin',
  18395. ' ProcA;',
  18396. ' Self.ProcA;',
  18397. ' ProcB;',
  18398. ' Self.ProcB;',
  18399. 'end;',
  18400. 'var',
  18401. ' A: texta;',
  18402. ' B: textb;',
  18403. ' C: textc;',
  18404. 'begin',
  18405. ' a.proca;',
  18406. ' b.proca;',
  18407. ' c.proca;']);
  18408. ConvertProgram;
  18409. CheckSource('TestExternalClass_NonExternalOverride',
  18410. LinesToStr([ // statements
  18411. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18412. ' this.$init = function () {',
  18413. ' };',
  18414. ' this.$final = function () {',
  18415. ' };',
  18416. ' this.ProcA = function () {',
  18417. ' this.ProcA();',
  18418. ' this.ProcA();',
  18419. ' this.ProcB();',
  18420. ' this.ProcB();',
  18421. ' };',
  18422. '});',
  18423. 'this.A = null;',
  18424. 'this.B = null;',
  18425. 'this.C = null;',
  18426. '']),
  18427. LinesToStr([ // $mod.$main
  18428. '$mod.A.ProcA();',
  18429. '$mod.B.ProcA();',
  18430. '$mod.C.ProcA();',
  18431. '']));
  18432. end;
  18433. procedure TTestModule.TestExternalClass_OverloadHint;
  18434. begin
  18435. StartProgram(false);
  18436. Add([
  18437. '{$modeswitch externalclass}',
  18438. 'type',
  18439. ' TExtA = class external name ''ExtObjA''',
  18440. ' procedure DoIt;',
  18441. ' procedure DoIt(i: longint);',
  18442. ' end;',
  18443. 'begin',
  18444. '']);
  18445. ConvertProgram;
  18446. CheckResolverUnexpectedHints(true);
  18447. CheckSource('TestExternalClass_OverloadHint',
  18448. LinesToStr([ // statements
  18449. '']),
  18450. LinesToStr([ // $mod.$main
  18451. '']));
  18452. end;
  18453. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18454. begin
  18455. WithTypeInfo:=true;
  18456. StartProgram(false);
  18457. Add([
  18458. '{$modeswitch externalclass}',
  18459. 'type',
  18460. ' JSwiper = class external name ''Swiper''',
  18461. ' constructor New;',
  18462. ' end;',
  18463. ' TObject = class',
  18464. ' private',
  18465. ' FSwiper: JSwiper;',
  18466. ' published',
  18467. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18468. ' end;',
  18469. 'begin',
  18470. ' JSwiper.new;',
  18471. '']);
  18472. ConvertProgram;
  18473. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18474. LinesToStr([ // statements
  18475. 'this.$rtti.$ExtClass("JSwiper", {',
  18476. ' jsclass: "Swiper"',
  18477. '});',
  18478. 'rtl.createClass(this, "TObject", null, function () {',
  18479. ' this.$init = function () {',
  18480. ' this.FSwiper = null;',
  18481. ' };',
  18482. ' this.$final = function () {',
  18483. ' this.FSwiper = undefined;',
  18484. ' };',
  18485. ' var $r = this.$rtti;',
  18486. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18487. '});',
  18488. '']),
  18489. LinesToStr([ // $mod.$main
  18490. 'new Swiper();',
  18491. '']));
  18492. end;
  18493. procedure TTestModule.TestExternalClass_Property;
  18494. begin
  18495. StartProgram(false);
  18496. Add([
  18497. '{$modeswitch externalclass}',
  18498. 'type',
  18499. ' TExtA = class external name ''ExtA''',
  18500. ' function getYear: longint;',
  18501. ' procedure setYear(Value: longint);',
  18502. ' property Year: longint read getyear write setyear;',
  18503. ' end;',
  18504. ' TExtB = class (TExtA)',
  18505. ' procedure OtherSetYear(Value: longint);',
  18506. ' property year write othersetyear;',
  18507. ' end;',
  18508. 'procedure textb.othersetyear(value: longint);',
  18509. 'begin',
  18510. ' setYear(Value+4);',
  18511. 'end;',
  18512. 'var',
  18513. ' A: texta;',
  18514. ' B: textb;',
  18515. 'begin',
  18516. ' a.year:=a.year+1;',
  18517. ' b.year:=b.year+2;']);
  18518. ConvertProgram;
  18519. CheckSource('TestExternalClass_NonExternalOverride',
  18520. LinesToStr([ // statements
  18521. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18522. ' this.$init = function () {',
  18523. ' };',
  18524. ' this.$final = function () {',
  18525. ' };',
  18526. ' this.OtherSetYear = function (Value) {',
  18527. ' this.setYear(Value+4);',
  18528. ' };',
  18529. '});',
  18530. 'this.A = null;',
  18531. 'this.B = null;',
  18532. '']),
  18533. LinesToStr([ // $mod.$main
  18534. '$mod.A.setYear($mod.A.getYear()+1);',
  18535. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18536. '']));
  18537. end;
  18538. procedure TTestModule.TestExternalClass_PropertyDate;
  18539. begin
  18540. StartProgram(false);
  18541. Add([
  18542. '{$modeswitch externalclass}',
  18543. 'type',
  18544. ' TExtA = class external name ''ExtA''',
  18545. ' end;',
  18546. ' TExtB = class (TExtA)',
  18547. ' FDate: string;',
  18548. ' property Date: string read FDate write FDate;',
  18549. ' property ExtA: string read FDate write FDate;',
  18550. ' end;',
  18551. ' {$M+}',
  18552. ' TObject = class',
  18553. ' FDate: string;',
  18554. ' published',
  18555. ' property Date: string read FDate write FDate;',
  18556. ' property ExtA: string read FDate write FDate;',
  18557. ' end;',
  18558. 'var',
  18559. ' B: textb;',
  18560. ' o: TObject;',
  18561. 'begin',
  18562. ' b.date:=b.exta;',
  18563. ' o.date:=o.exta;']);
  18564. ConvertProgram;
  18565. CheckSource('TestExternalClass_PropertyDate',
  18566. LinesToStr([ // statements
  18567. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18568. ' this.$init = function () {',
  18569. ' this.FDate = "";',
  18570. ' };',
  18571. ' this.$final = function () {',
  18572. ' };',
  18573. '});',
  18574. 'rtl.createClass(this, "TObject", null, function () {',
  18575. ' this.$init = function () {',
  18576. ' this.FDate = "";',
  18577. ' };',
  18578. ' this.$final = function () {',
  18579. ' };',
  18580. ' var $r = this.$rtti;',
  18581. ' $r.addField("FDate", rtl.string);',
  18582. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18583. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18584. '});',
  18585. 'this.B = null;',
  18586. 'this.o = null;',
  18587. '']),
  18588. LinesToStr([ // $mod.$main
  18589. '$mod.B.FDate = $mod.B.FDate;',
  18590. '$mod.o.FDate = $mod.o.FDate;',
  18591. '']));
  18592. end;
  18593. procedure TTestModule.TestExternalClass_ClassProperty;
  18594. begin
  18595. StartProgram(false);
  18596. Add('{$modeswitch externalclass}');
  18597. Add('type');
  18598. Add(' TExtA = class external name ''ExtA''');
  18599. Add(' class function getYear: longint;');
  18600. Add(' class procedure setYear(Value: longint);');
  18601. Add(' class property Year: longint read getyear write setyear;');
  18602. Add(' end;');
  18603. Add(' TExtB = class (TExtA)');
  18604. Add(' class function GetCentury: longint;');
  18605. Add(' class procedure SetCentury(Value: longint);');
  18606. Add(' class property Century: longint read getcentury write setcentury;');
  18607. Add(' end;');
  18608. Add('class function textb.getcentury: longint;');
  18609. Add('begin');
  18610. Add('end;');
  18611. Add('class procedure textb.setcentury(value: longint);');
  18612. Add('begin');
  18613. Add(' setyear(value+11);');
  18614. Add(' texta.year:=texta.year+12;');
  18615. Add(' year:=year+13;');
  18616. Add(' textb.century:=textb.century+14;');
  18617. Add(' century:=century+15;');
  18618. Add('end;');
  18619. Add('var');
  18620. Add(' A: texta;');
  18621. Add(' B: textb;');
  18622. Add('begin');
  18623. Add(' texta.year:=texta.year+1;');
  18624. Add(' textb.year:=textb.year+2;');
  18625. Add(' TextA.year:=TextA.year+3;');
  18626. Add(' b.year:=b.year+4;');
  18627. Add(' textb.century:=textb.century+5;');
  18628. Add(' b.century:=b.century+6;');
  18629. ConvertProgram;
  18630. CheckSource('TestExternalClass_ClassProperty',
  18631. LinesToStr([ // statements
  18632. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18633. ' this.$init = function () {',
  18634. ' };',
  18635. ' this.$final = function () {',
  18636. ' };',
  18637. ' this.GetCentury = function () {',
  18638. ' var Result = 0;',
  18639. ' return Result;',
  18640. ' };',
  18641. ' this.SetCentury = function (Value) {',
  18642. ' this.setYear(Value + 11);',
  18643. ' ExtA.setYear(ExtA.getYear() + 12);',
  18644. ' this.setYear(this.getYear() + 13);',
  18645. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18646. ' this.SetCentury(this.GetCentury() + 15);',
  18647. ' };',
  18648. '});',
  18649. 'this.A = null;',
  18650. 'this.B = null;',
  18651. '']),
  18652. LinesToStr([ // $mod.$main
  18653. 'ExtA.setYear(ExtA.getYear() + 1);',
  18654. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18655. 'ExtA.setYear(ExtA.getYear() + 3);',
  18656. '$mod.B.setYear($mod.B.getYear() + 4);',
  18657. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18658. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18659. '']));
  18660. end;
  18661. procedure TTestModule.TestExternalClass_ClassOf;
  18662. begin
  18663. StartProgram(false);
  18664. Add('{$modeswitch externalclass}');
  18665. Add('type');
  18666. Add(' TExtA = class external name ''ExtA''');
  18667. Add(' procedure ProcA; virtual;');
  18668. Add(' procedure ProcB; virtual;');
  18669. Add(' end;');
  18670. Add(' TExtAClass = class of TExtA;');
  18671. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18672. Add(' end;');
  18673. Add(' TExtBClass = class of TExtB;');
  18674. Add(' TExtC = class (TExtB)');
  18675. Add(' procedure ProcA; override;');
  18676. Add(' end;');
  18677. Add(' TExtCClass = class of TExtC;');
  18678. Add('procedure TExtC.ProcA; begin end;');
  18679. Add('var');
  18680. Add(' A: texta; ClA: TExtAClass;');
  18681. Add(' B: textb; ClB: TExtBClass;');
  18682. Add(' C: textc; ClC: TExtCClass;');
  18683. Add('begin');
  18684. Add(' ClA:=texta;');
  18685. Add(' ClA:=textb;');
  18686. Add(' ClA:=textc;');
  18687. Add(' ClB:=textb;');
  18688. Add(' ClB:=textc;');
  18689. Add(' ClC:=textc;');
  18690. ConvertProgram;
  18691. CheckSource('TestExternalClass_ClassOf',
  18692. LinesToStr([ // statements
  18693. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18694. ' this.$init = function () {',
  18695. ' };',
  18696. ' this.$final = function () {',
  18697. ' };',
  18698. ' this.ProcA = function () {',
  18699. ' };',
  18700. '});',
  18701. 'this.A = null;',
  18702. 'this.ClA = null;',
  18703. 'this.B = null;',
  18704. 'this.ClB = null;',
  18705. 'this.C = null;',
  18706. 'this.ClC = null;',
  18707. '']),
  18708. LinesToStr([ // $mod.$main
  18709. '$mod.ClA = ExtA;',
  18710. '$mod.ClA = ExtB;',
  18711. '$mod.ClA = $mod.TExtC;',
  18712. '$mod.ClB = ExtB;',
  18713. '$mod.ClB = $mod.TExtC;',
  18714. '$mod.ClC = $mod.TExtC;',
  18715. '']));
  18716. end;
  18717. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18718. begin
  18719. AddModuleWithIntfImplSrc('unit2.pas',
  18720. LinesToStr([
  18721. '{$modeswitch externalclass}',
  18722. 'type',
  18723. ' TExtA = class external name ''ExtA''',
  18724. ' class var Id: longint;',
  18725. ' end;',
  18726. '']),
  18727. '');
  18728. StartUnit(true);
  18729. Add('interface');
  18730. Add('uses unit2;');
  18731. Add('implementation');
  18732. Add('begin');
  18733. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18734. ConvertUnit;
  18735. CheckSource('TestExternalClass_ClassOtherUnit',
  18736. LinesToStr([
  18737. '']),
  18738. LinesToStr([
  18739. 'ExtA.Id = ExtA.Id + 1;',
  18740. '']));
  18741. end;
  18742. procedure TTestModule.TestExternalClass_Is;
  18743. begin
  18744. StartProgram(false);
  18745. Add([
  18746. '{$modeswitch externalclass}',
  18747. 'type',
  18748. ' TExtA = class external name ''ExtA''',
  18749. ' end;',
  18750. ' TExtAClass = class of TExtA;',
  18751. ' TExtB = class external name ''ExtB'' (TExtA)',
  18752. ' end;',
  18753. ' TExtBClass = class of TExtB;',
  18754. ' TExtC = class (TExtB)',
  18755. ' end;',
  18756. ' TExtCClass = class of TExtC;',
  18757. 'var',
  18758. ' A: texta; ClA: TExtAClass;',
  18759. ' B: textb; ClB: TExtBClass;',
  18760. ' C: textc; ClC: TExtCClass;',
  18761. 'begin',
  18762. ' if a is textb then ;',
  18763. ' if a is textc then ;',
  18764. ' if b is textc then ;',
  18765. ' if cla is textb then ;',
  18766. ' if cla is textc then ;',
  18767. ' if clb is textc then ;',
  18768. ' try',
  18769. ' except',
  18770. ' on TExtA do ;',
  18771. ' on e: TExtB do ;',
  18772. ' end;',
  18773. '']);
  18774. ConvertProgram;
  18775. CheckSource('TestExternalClass_Is',
  18776. LinesToStr([ // statements
  18777. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18778. ' this.$init = function () {',
  18779. ' };',
  18780. ' this.$final = function () {',
  18781. ' };',
  18782. '});',
  18783. 'this.A = null;',
  18784. 'this.ClA = null;',
  18785. 'this.B = null;',
  18786. 'this.ClB = null;',
  18787. 'this.C = null;',
  18788. 'this.ClC = null;',
  18789. '']),
  18790. LinesToStr([ // $mod.$main
  18791. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18792. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18793. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18794. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18795. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18796. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18797. 'try {} catch ($e) {',
  18798. ' if (rtl.isExt($e,ExtA)) {}',
  18799. ' else if (rtl.isExt($e,ExtB)) {',
  18800. ' var e = $e;',
  18801. ' } else throw $e',
  18802. '};',
  18803. '']));
  18804. end;
  18805. procedure TTestModule.TestExternalClass_As;
  18806. begin
  18807. StartProgram(false);
  18808. Add('{$modeswitch externalclass}');
  18809. Add('type');
  18810. Add(' TExtA = class external name ''ExtA''');
  18811. Add(' end;');
  18812. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18813. Add(' end;');
  18814. Add(' TExtC = class (TExtB)');
  18815. Add(' end;');
  18816. Add('var');
  18817. Add(' A: texta;');
  18818. Add(' B: textb;');
  18819. Add(' C: textc;');
  18820. Add('begin');
  18821. Add(' b:=a as textb;');
  18822. Add(' c:=a as textc;');
  18823. Add(' c:=b as textc;');
  18824. ConvertProgram;
  18825. CheckSource('TestExternalClass_Is',
  18826. LinesToStr([ // statements
  18827. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18828. ' this.$init = function () {',
  18829. ' };',
  18830. ' this.$final = function () {',
  18831. ' };',
  18832. '});',
  18833. 'this.A = null;',
  18834. 'this.B = null;',
  18835. 'this.C = null;',
  18836. '']),
  18837. LinesToStr([ // $mod.$main
  18838. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18839. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18840. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18841. '']));
  18842. end;
  18843. procedure TTestModule.TestExternalClass_DestructorFail;
  18844. begin
  18845. StartProgram(false);
  18846. Add('{$modeswitch externalclass}');
  18847. Add('type');
  18848. Add(' TExtA = class external name ''ExtA''');
  18849. Add(' destructor Free;');
  18850. Add(' end;');
  18851. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18852. nPasElementNotSupported);
  18853. ConvertProgram;
  18854. end;
  18855. procedure TTestModule.TestExternalClass_New;
  18856. begin
  18857. StartProgram(false);
  18858. Add([
  18859. '{$modeswitch externalclass}',
  18860. 'type',
  18861. ' TExtA = class external name ''ExtA''',
  18862. ' constructor New;',
  18863. ' constructor New(i: longint; j: longint = 2);',
  18864. ' end;',
  18865. 'var',
  18866. ' A: texta;',
  18867. 'begin',
  18868. ' a:=texta.new;',
  18869. ' a:=texta(texta.new);',
  18870. ' a:=texta.new();',
  18871. ' a:=texta.new(1);',
  18872. ' with texta do begin',
  18873. ' a:=new;',
  18874. ' a:=new();',
  18875. ' a:=new(2);',
  18876. ' end;',
  18877. ' a:=test1.texta.new;',
  18878. ' a:=test1.texta.new();',
  18879. ' a:=test1.texta.new(3);',
  18880. '']);
  18881. ConvertProgram;
  18882. CheckSource('TestExternalClass_New',
  18883. LinesToStr([ // statements
  18884. 'this.A = null;',
  18885. '']),
  18886. LinesToStr([ // $mod.$main
  18887. '$mod.A = new ExtA();',
  18888. '$mod.A = new ExtA();',
  18889. '$mod.A = new ExtA();',
  18890. '$mod.A = new ExtA(1,2);',
  18891. '$mod.A = new ExtA();',
  18892. '$mod.A = new ExtA();',
  18893. '$mod.A = new ExtA(2,2);',
  18894. '$mod.A = new ExtA();',
  18895. '$mod.A = new ExtA();',
  18896. '$mod.A = new ExtA(3,2);',
  18897. '']));
  18898. end;
  18899. procedure TTestModule.TestExternalClass_ClassOf_New;
  18900. begin
  18901. StartProgram(false);
  18902. Add('{$modeswitch externalclass}');
  18903. Add('type');
  18904. Add(' TExtAClass = class of TExtA;');
  18905. Add(' TExtA = class external name ''ExtA''');
  18906. Add(' C: TExtAClass;');
  18907. Add(' constructor New;');
  18908. Add(' end;');
  18909. Add('var');
  18910. Add(' A: texta;');
  18911. Add(' C: textaclass;');
  18912. Add('begin');
  18913. Add(' a:=c.new;');
  18914. Add(' a:=c.new();');
  18915. Add(' with C do begin');
  18916. Add(' a:=new;');
  18917. Add(' a:=new();');
  18918. Add(' end;');
  18919. Add(' a:=test1.c.new;');
  18920. Add(' a:=test1.c.new();');
  18921. Add(' a:=A.c.new();');
  18922. ConvertProgram;
  18923. CheckSource('TestExternalClass_ClassOf_New',
  18924. LinesToStr([ // statements
  18925. 'this.A = null;',
  18926. 'this.C = null;',
  18927. '']),
  18928. LinesToStr([ // $mod.$main
  18929. '$mod.A = new $mod.C();',
  18930. '$mod.A = new $mod.C();',
  18931. 'var $with = $mod.C;',
  18932. '$mod.A = new $with();',
  18933. '$mod.A = new $with();',
  18934. '$mod.A = new $mod.C();',
  18935. '$mod.A = new $mod.C();',
  18936. '$mod.A = new $mod.A.C();',
  18937. '']));
  18938. end;
  18939. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18940. begin
  18941. StartProgram(false);
  18942. Add([
  18943. '{$modeswitch externalclass}',
  18944. 'type',
  18945. ' TExtAClass = class of TExtA;',
  18946. ' TExtA = class external name ''ExtA''',
  18947. ' constructor New;',
  18948. ' end;',
  18949. 'function GetCreator: TExtAClass;',
  18950. 'begin',
  18951. ' Result:=TExtA;',
  18952. 'end;',
  18953. 'var',
  18954. ' A: texta;',
  18955. 'begin',
  18956. ' a:=getcreator.new;',
  18957. ' a:=getcreator().new;',
  18958. ' a:=getcreator().new();',
  18959. ' a:=getcreator.new();',
  18960. ' with getcreator do begin',
  18961. ' a:=new;',
  18962. ' a:=new();',
  18963. ' end;']);
  18964. ConvertProgram;
  18965. CheckSource('TestExternalClass_FuncClassOf_New',
  18966. LinesToStr([ // statements
  18967. 'this.GetCreator = function () {',
  18968. ' var Result = null;',
  18969. ' Result = ExtA;',
  18970. ' return Result;',
  18971. '};',
  18972. 'this.A = null;',
  18973. '']),
  18974. LinesToStr([ // $mod.$main
  18975. '$mod.A = new ($mod.GetCreator())();',
  18976. '$mod.A = new ($mod.GetCreator())();',
  18977. '$mod.A = new ($mod.GetCreator())();',
  18978. '$mod.A = new ($mod.GetCreator())();',
  18979. 'var $with = $mod.GetCreator();',
  18980. '$mod.A = new $with();',
  18981. '$mod.A = new $with();',
  18982. '']));
  18983. end;
  18984. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18985. begin
  18986. StartProgram(false);
  18987. Add([
  18988. '{$modeswitch externalclass}',
  18989. 'type',
  18990. ' TExtA = class external name ''ExtA''',
  18991. ' constructor New;',
  18992. ' end;',
  18993. ' TBird = class(TExtA)',
  18994. ' end;',
  18995. 'begin',
  18996. ' TBird.new;',
  18997. '']);
  18998. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18999. ConvertProgram;
  19000. end;
  19001. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19002. begin
  19003. StartProgram(false);
  19004. Add([
  19005. '{$modeswitch externalclass}',
  19006. 'type',
  19007. ' TExtA = class external name ''ExtA''',
  19008. ' constructor New;',
  19009. ' end;',
  19010. ' TBird = class(TExtA)',
  19011. ' end;',
  19012. 'begin',
  19013. ' TBird.new();',
  19014. '']);
  19015. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19016. ConvertProgram;
  19017. end;
  19018. procedure TTestModule.TestExternalClass_NewExtName;
  19019. begin
  19020. StartProgram(false);
  19021. Add([
  19022. '{$modeswitch externalclass}',
  19023. 'type',
  19024. ' TExtA = class external name ''ExtA''',
  19025. ' constructor New; external name ''Other'';',
  19026. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19027. ' end;',
  19028. 'var',
  19029. ' A: texta;',
  19030. 'begin',
  19031. ' a:=texta.new;',
  19032. ' a:=texta(texta.new);',
  19033. ' a:=texta.new();',
  19034. ' a:=texta.new(1);',
  19035. ' with texta do begin',
  19036. ' a:=new;',
  19037. ' a:=new();',
  19038. ' a:=new(2);',
  19039. ' end;',
  19040. ' a:=test1.texta.new;',
  19041. ' a:=test1.texta.new();',
  19042. ' a:=test1.texta.new(3);',
  19043. '']);
  19044. ConvertProgram;
  19045. CheckSource('TestExternalClass_NewExtName',
  19046. LinesToStr([ // statements
  19047. 'this.A = null;',
  19048. '']),
  19049. LinesToStr([ // $mod.$main
  19050. '$mod.A = new Other();',
  19051. '$mod.A = new Other();',
  19052. '$mod.A = new Other();',
  19053. '$mod.A = new A.B(1,2);',
  19054. '$mod.A = new Other();',
  19055. '$mod.A = new Other();',
  19056. '$mod.A = new A.B(2,2);',
  19057. '$mod.A = new Other();',
  19058. '$mod.A = new Other();',
  19059. '$mod.A = new A.B(3,2);',
  19060. '']));
  19061. end;
  19062. procedure TTestModule.TestExternalClass_Constructor;
  19063. begin
  19064. StartProgram(false);
  19065. Add([
  19066. '{$modeswitch externalclass}',
  19067. 'type',
  19068. ' TExtA = class external name ''ExtA''',
  19069. ' public type',
  19070. ' TExtB = class external name ''ExtB''',
  19071. ' public type',
  19072. ' TExtC = class external name ''ExtC''',
  19073. ' constructor New;',
  19074. ' constructor New(i: word);',
  19075. ' end;',
  19076. ' end;',
  19077. ' constructor Create;',
  19078. ' constructor Create(i: longint; j: longint = 2);',
  19079. ' end;',
  19080. 'var',
  19081. ' A: texta;',
  19082. ' C: texta.textb.textc;',
  19083. 'begin',
  19084. ' a:=texta.create;',
  19085. ' a:=texta(texta.create);',
  19086. ' a:=texta.create();',
  19087. ' a:=texta.create(1);',
  19088. ' with texta do begin',
  19089. ' a:=create;',
  19090. ' a:=create();',
  19091. ' a:=create(2);',
  19092. ' end;',
  19093. ' a:=test1.texta.create;',
  19094. ' a:=test1.texta.create();',
  19095. ' a:=test1.texta.create(3);',
  19096. ' c:=texta.textb.textc.new;',
  19097. ' c:=texta.textb.textc.new();',
  19098. ' c:=texta.textb.textc.new(4);',
  19099. '']);
  19100. ConvertProgram;
  19101. CheckSource('TestExternalClass_Constructor',
  19102. LinesToStr([ // statements
  19103. 'this.A = null;',
  19104. 'this.C = null;',
  19105. '']),
  19106. LinesToStr([ // $mod.$main
  19107. '$mod.A = new ExtA.Create();',
  19108. '$mod.A = new ExtA.Create();',
  19109. '$mod.A = new ExtA.Create();',
  19110. '$mod.A = new ExtA.Create(1,2);',
  19111. '$mod.A = new ExtA.Create();',
  19112. '$mod.A = new ExtA.Create();',
  19113. '$mod.A = new ExtA.Create(2,2);',
  19114. '$mod.A = new ExtA.Create();',
  19115. '$mod.A = new ExtA.Create();',
  19116. '$mod.A = new ExtA.Create(3,2);',
  19117. '$mod.C = new ExtA.ExtB.ExtC();',
  19118. '$mod.C = new ExtA.ExtB.ExtC();',
  19119. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19120. '']));
  19121. end;
  19122. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19123. begin
  19124. StartProgram(false);
  19125. Add([
  19126. '{$modeswitch externalclass}',
  19127. 'type',
  19128. ' TExtA = class external name ''ExtA''',
  19129. ' constructor Create; external name ''{}'';',
  19130. ' end;',
  19131. 'var',
  19132. ' A: texta;',
  19133. 'begin',
  19134. ' a:=texta.create;',
  19135. ' a:=texta(texta.create);',
  19136. ' a:=texta.create();',
  19137. ' with texta do begin',
  19138. ' a:=create;',
  19139. ' a:=create();',
  19140. ' end;',
  19141. ' a:=test1.texta.create;',
  19142. ' a:=test1.texta.create();',
  19143. '']);
  19144. ConvertProgram;
  19145. CheckSource('TestExternalClass_ConstructorBrackets',
  19146. LinesToStr([ // statements
  19147. 'this.A = null;',
  19148. '']),
  19149. LinesToStr([ // $mod.$main
  19150. '$mod.A = {};',
  19151. '$mod.A = {};',
  19152. '$mod.A = {};',
  19153. '$mod.A = {};',
  19154. '$mod.A = {};',
  19155. '$mod.A = {};',
  19156. '$mod.A = {};',
  19157. '']));
  19158. end;
  19159. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19160. begin
  19161. StartProgram(false);
  19162. Add('{$modeswitch externalclass}');
  19163. Add('type');
  19164. Add(' TExtA = class external name ''ExtA''');
  19165. Add(' constructor New;');
  19166. Add(' end;');
  19167. Add('function DoIt: longint;');
  19168. Add('const ExtA: longint = 3;');
  19169. Add('begin');
  19170. Add(' Result:=ExtA;');
  19171. Add('end;');
  19172. Add('var');
  19173. Add(' A: texta;');
  19174. Add('begin');
  19175. Add(' a:=texta.new;');
  19176. ConvertProgram;
  19177. CheckSource('TestExternalClass_LocalConstSameName',
  19178. LinesToStr([ // statements
  19179. 'var ExtA$1 = 3;',
  19180. 'this.DoIt = function () {',
  19181. ' var Result = 0;',
  19182. ' Result = ExtA$1;',
  19183. ' return Result;',
  19184. '};',
  19185. 'this.A = null;',
  19186. '']),
  19187. LinesToStr([ // $mod.$main
  19188. '$mod.A = new ExtA();',
  19189. '']));
  19190. end;
  19191. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19192. begin
  19193. StartProgram(false);
  19194. Add('{$modeswitch externalclass}');
  19195. Add('type');
  19196. Add(' TExtA = class external name ''ExtA''');
  19197. Add(' procedure DoIt;');
  19198. Add(' end;');
  19199. Add(' TMyA = class(TExtA)');
  19200. Add(' procedure DoIt;');
  19201. Add(' end;');
  19202. Add('procedure TMyA.DoIt; begin end;');
  19203. Add('begin');
  19204. ConvertProgram;
  19205. CheckSource('TestExternalClass_ReintroduceOverload',
  19206. LinesToStr([ // statements
  19207. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19208. ' this.$init = function () {',
  19209. ' };',
  19210. ' this.$final = function () {',
  19211. ' };',
  19212. ' this.DoIt$1 = function () {',
  19213. ' };',
  19214. '});',
  19215. '']),
  19216. LinesToStr([ // $mod.$main
  19217. '']));
  19218. end;
  19219. procedure TTestModule.TestExternalClass_Inherited;
  19220. begin
  19221. StartProgram(false);
  19222. Add('{$modeswitch externalclass}');
  19223. Add('type');
  19224. Add(' TExtA = class external name ''ExtA''');
  19225. Add(' procedure DoIt(i: longint = 1); virtual;');
  19226. Add(' procedure DoSome(j: longint = 2);');
  19227. Add(' end;');
  19228. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19229. Add(' end;');
  19230. Add(' TMyC = class(TExtB)');
  19231. Add(' procedure DoIt(i: longint = 1); override;');
  19232. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19233. Add(' end;');
  19234. Add('procedure TMyC.DoIt(i: longint);');
  19235. Add('begin');
  19236. Add(' inherited;');
  19237. Add(' inherited DoIt;');
  19238. Add(' inherited DoIt();');
  19239. Add(' inherited DoIt(3);');
  19240. Add(' inherited DoSome;');
  19241. Add(' inherited DoSome();');
  19242. Add(' inherited DoSome(4);');
  19243. Add('end;');
  19244. Add('procedure TMyC.DoSome(j: longint);');
  19245. Add('begin');
  19246. Add(' inherited;');
  19247. Add('end;');
  19248. Add('begin');
  19249. ConvertProgram;
  19250. CheckSource('TestExternalClass_ReintroduceOverload',
  19251. LinesToStr([ // statements
  19252. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19253. ' this.$init = function () {',
  19254. ' };',
  19255. ' this.$final = function () {',
  19256. ' };',
  19257. ' this.DoIt = function (i) {',
  19258. ' ExtB.DoIt.apply(this, arguments);',
  19259. ' ExtB.DoIt.call(this, 1);',
  19260. ' ExtB.DoIt.call(this, 1);',
  19261. ' ExtB.DoIt.call(this, 3);',
  19262. ' ExtB.DoSome.call(this, 2);',
  19263. ' ExtB.DoSome.call(this, 2);',
  19264. ' ExtB.DoSome.call(this, 4);',
  19265. ' };',
  19266. ' this.DoSome$1 = function (j) {',
  19267. ' ExtB.DoSome.apply(this, arguments);',
  19268. ' };',
  19269. '});',
  19270. '']),
  19271. LinesToStr([ // $mod.$main
  19272. '']));
  19273. end;
  19274. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19275. begin
  19276. StartProgram(false);
  19277. Add('{$modeswitch externalclass}');
  19278. Add('type');
  19279. Add(' TObject = class');
  19280. Add(' end;');
  19281. Add(' TExtA = class external name ''ExtA''(TObject)');
  19282. Add(' end;');
  19283. Add('begin');
  19284. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19285. ConvertProgram;
  19286. end;
  19287. procedure TTestModule.TestExternalClass_NewInstance;
  19288. begin
  19289. StartProgram(false);
  19290. Add('{$modeswitch externalclass}');
  19291. Add('type');
  19292. Add(' TExtA = class external name ''ExtA''');
  19293. Add(' end;');
  19294. Add(' TMyB = class(TExtA)');
  19295. Add(' protected');
  19296. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19297. Add(' end;');
  19298. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19299. Add('begin end;');
  19300. Add('begin');
  19301. ConvertProgram;
  19302. CheckSource('TestExternalClass_NewInstance',
  19303. LinesToStr([ // statements
  19304. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19305. ' this.$init = function () {',
  19306. ' };',
  19307. ' this.$final = function () {',
  19308. ' };',
  19309. ' this.NewInstance = function (fnname, paramarray) {',
  19310. ' var Result = null;',
  19311. ' return Result;',
  19312. ' };',
  19313. '});',
  19314. '']),
  19315. LinesToStr([ // $mod.$main
  19316. '']));
  19317. end;
  19318. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19319. begin
  19320. StartProgram(false);
  19321. Add('{$modeswitch externalclass}');
  19322. Add('type');
  19323. Add(' TExtA = class external name ''ExtA''');
  19324. Add(' end;');
  19325. Add(' TMyB = class(TExtA)');
  19326. Add(' protected');
  19327. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19328. Add(' end;');
  19329. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19330. Add('begin end;');
  19331. Add('begin');
  19332. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19333. ConvertProgram;
  19334. end;
  19335. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19336. begin
  19337. StartProgram(false);
  19338. Add('{$modeswitch externalclass}');
  19339. Add('type');
  19340. Add(' TExtA = class external name ''ExtA''');
  19341. Add(' end;');
  19342. Add(' TMyB = class(TExtA)');
  19343. Add(' protected');
  19344. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19345. Add(' end;');
  19346. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19347. Add('begin end;');
  19348. Add('begin');
  19349. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19350. nIncompatibleTypeArgNo);
  19351. ConvertProgram;
  19352. end;
  19353. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19354. begin
  19355. StartProgram(false);
  19356. Add('{$modeswitch externalclass}');
  19357. Add('type');
  19358. Add(' TExtA = class external name ''ExtA''');
  19359. Add(' end;');
  19360. Add(' TMyB = class(TExtA)');
  19361. Add(' protected');
  19362. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19363. Add(' end;');
  19364. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19365. Add('begin end;');
  19366. Add('begin');
  19367. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19368. nIncompatibleTypeArgNo);
  19369. ConvertProgram;
  19370. end;
  19371. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19372. begin
  19373. StartProgram(false);
  19374. Add([
  19375. '{$modeswitch externalclass}',
  19376. 'type',
  19377. ' TJSFunction = class external name ''Function''',
  19378. ' end;',
  19379. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19380. ' constructor New(w: word);',
  19381. ' end;',
  19382. ' TBird = class (TExtA)',
  19383. ' public',
  19384. ' Size: word;',
  19385. ' class var Legs: word;',
  19386. ' constructor Create(a: word);',
  19387. ' end;',
  19388. ' TEagle = class (TBird)',
  19389. ' public',
  19390. ' constructor Create(b: word); reintroduce;',
  19391. ' end;',
  19392. 'constructor TBird.Create(a: word);',
  19393. 'begin',
  19394. ' inherited;', // silently ignored
  19395. ' inherited New(a);', // this.$func(a)
  19396. 'end;',
  19397. 'constructor TEagle.Create(b: word);',
  19398. 'begin',
  19399. ' inherited Create(b);',
  19400. 'end;',
  19401. 'var',
  19402. ' Bird: TBird;',
  19403. ' Eagle: TEagle;',
  19404. 'begin',
  19405. ' Bird:=TBird.Create(3);',
  19406. ' Eagle:=TEagle.Create(4);',
  19407. ' Bird.Size:=Bird.Size+5;',
  19408. ' Bird.Legs:=Bird.Legs+6;',
  19409. ' Eagle.Size:=Eagle.Size+5;',
  19410. ' Eagle.Legs:=Eagle.Legs+6;',
  19411. '']);
  19412. ConvertProgram;
  19413. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19414. LinesToStr([ // statements
  19415. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19416. ' this.Legs = 0;',
  19417. ' this.$init = function () {',
  19418. ' this.Size = 0;',
  19419. ' };',
  19420. ' this.$final = function () {',
  19421. ' };',
  19422. ' this.Create = function (a) {',
  19423. ' this.$ancestorfunc(a);',
  19424. ' return this;',
  19425. ' };',
  19426. '});',
  19427. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19428. ' this.Create$1 = function (b) {',
  19429. ' $mod.TBird.Create.call(this, b);',
  19430. ' return this;',
  19431. ' };',
  19432. '});',
  19433. 'this.Bird = null;',
  19434. 'this.Eagle = null;',
  19435. '']),
  19436. LinesToStr([ // $mod.$main
  19437. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19438. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19439. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19440. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19441. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19442. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19443. '']));
  19444. end;
  19445. procedure TTestModule.TestExternalClass_PascalProperty;
  19446. begin
  19447. StartProgram(false);
  19448. Add('{$modeswitch externalclass}');
  19449. Add('type');
  19450. Add(' TJSElement = class;');
  19451. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19452. Add(' TJSElement = class external name ''ExtA''');
  19453. Add(' end;');
  19454. Add(' TControl = class(TJSElement)');
  19455. Add(' private');
  19456. Add(' FOnClick: TJSNotifyEvent;');
  19457. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19458. Add(' procedure Click(Sender: TJSElement);');
  19459. Add(' end;');
  19460. Add('procedure TControl.Click(Sender: TJSElement);');
  19461. Add('begin');
  19462. Add(' OnClick(Self);');
  19463. Add('end;');
  19464. Add('var');
  19465. Add(' Ctrl: TControl;');
  19466. Add('begin');
  19467. Add(' Ctrl.OnClick:[email protected];');
  19468. Add(' Ctrl.OnClick(Ctrl);');
  19469. ConvertProgram;
  19470. CheckSource('TestExternalClass_PascalProperty',
  19471. LinesToStr([ // statements
  19472. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19473. ' this.$init = function () {',
  19474. ' this.FOnClick = null;',
  19475. ' };',
  19476. ' this.$final = function () {',
  19477. ' this.FOnClick = undefined;',
  19478. ' };',
  19479. ' this.Click = function (Sender) {',
  19480. ' this.FOnClick(this);',
  19481. ' };',
  19482. '});',
  19483. 'this.Ctrl = null;',
  19484. '']),
  19485. LinesToStr([ // $mod.$main
  19486. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19487. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19488. '']));
  19489. end;
  19490. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19491. begin
  19492. StartProgram(false);
  19493. Add([
  19494. '{$modeswitch externalclass}',
  19495. 'type',
  19496. ' IUnknown = interface end;',
  19497. ' TObject = class',
  19498. ' end;',
  19499. ' TChild = class',
  19500. ' end;',
  19501. ' TExtRootA = class external name ''ExtRootA''',
  19502. ' end;',
  19503. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19504. ' end;',
  19505. ' TExtRootB = class external name ''ExtRootB''',
  19506. ' end;',
  19507. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19508. ' end;',
  19509. ' TExtString = class external name ''String''',
  19510. ' function charAt(aIndex : NativeInt) : string;',
  19511. ' end;',
  19512. 'var',
  19513. ' Obj: TObject;',
  19514. ' Child: TChild;',
  19515. ' RootA: TExtRootA;',
  19516. ' ChildA: TExtChildA;',
  19517. ' RootB: TExtRootB;',
  19518. ' ChildB: TExtChildB;',
  19519. ' i: IUnknown;',
  19520. ' s: string;',
  19521. ' v: jsvalue;',
  19522. 'begin',
  19523. ' obj:=tobject(roota);',
  19524. ' obj:=tobject(childa);',
  19525. ' child:=tchild(tobject(roota));',
  19526. ' roota:=textroota(obj);',
  19527. ' roota:=textroota(child);',
  19528. ' roota:=textroota(rootb);',
  19529. ' roota:=textroota(childb);',
  19530. ' childa:=textchilda(textroota(obj));',
  19531. ' roota:=TExtRootA(i);',
  19532. ' s:=TExtString(s).charAt(7);',
  19533. ' s:=TExtString(v).charAt(8);',
  19534. '']);
  19535. ConvertProgram;
  19536. CheckSource('TestExternalClass_TypeCastToRootClass',
  19537. LinesToStr([ // statements
  19538. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19539. 'rtl.createClass(this, "TObject", null, function () {',
  19540. ' this.$init = function () {',
  19541. ' };',
  19542. ' this.$final = function () {',
  19543. ' };',
  19544. '});',
  19545. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19546. '});',
  19547. 'this.Obj = null;',
  19548. 'this.Child = null;',
  19549. 'this.RootA = null;',
  19550. 'this.ChildA = null;',
  19551. 'this.RootB = null;',
  19552. 'this.ChildB = null;',
  19553. 'this.i = null;',
  19554. 'this.s = "";',
  19555. 'this.v = undefined;',
  19556. '']),
  19557. LinesToStr([ // $mod.$main
  19558. '$mod.Obj = $mod.RootA;',
  19559. '$mod.Obj = $mod.ChildA;',
  19560. '$mod.Child = $mod.RootA;',
  19561. '$mod.RootA = $mod.Obj;',
  19562. '$mod.RootA = $mod.Child;',
  19563. '$mod.RootA = $mod.RootB;',
  19564. '$mod.RootA = $mod.ChildB;',
  19565. '$mod.ChildA = $mod.Obj;',
  19566. '$mod.RootA = $mod.i;',
  19567. '$mod.s = $mod.s.charAt(7);',
  19568. '$mod.s = $mod.v.charAt(8);',
  19569. '']));
  19570. end;
  19571. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19572. begin
  19573. StartProgram(false);
  19574. Add([
  19575. '{$modeswitch externalclass}',
  19576. 'type',
  19577. ' IUnknown = interface end;',
  19578. ' IBird = interface(IUnknown) end;',
  19579. ' TClass = class of TObject;',
  19580. ' TObject = class',
  19581. ' end;',
  19582. ' TChild = class',
  19583. ' end;',
  19584. ' TJSObject = class external name ''Object''',
  19585. ' end;',
  19586. ' TRec = record end;',
  19587. 'var',
  19588. ' Obj: TObject;',
  19589. ' Child: TChild;',
  19590. ' i: IUnknown;',
  19591. ' Bird: IBird;',
  19592. ' j: TJSObject;',
  19593. ' r: TRec;',
  19594. ' c: TClass;',
  19595. 'begin',
  19596. ' j:=tjsobject(IUnknown);',
  19597. ' j:=tjsobject(IBird);',
  19598. ' j:=tjsobject(TObject);',
  19599. ' j:=tjsobject(TChild);',
  19600. ' j:=tjsobject(TRec);',
  19601. ' j:=tjsobject(Obj);',
  19602. ' j:=tjsobject(Child);',
  19603. ' j:=tjsobject(i);',
  19604. ' j:=tjsobject(Bird);',
  19605. ' j:=tjsobject(r);',
  19606. ' j:=tjsobject(c);',
  19607. '']);
  19608. ConvertProgram;
  19609. CheckSource('TestExternalClass_TypeCastToJSObject',
  19610. LinesToStr([ // statements
  19611. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19612. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19613. 'rtl.createClass(this, "TObject", null, function () {',
  19614. ' this.$init = function () {',
  19615. ' };',
  19616. ' this.$final = function () {',
  19617. ' };',
  19618. '});',
  19619. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19620. '});',
  19621. 'rtl.recNewT(this, "TRec", function () {',
  19622. ' this.$eq = function (b) {',
  19623. ' return true;',
  19624. ' };',
  19625. ' this.$assign = function (s) {',
  19626. ' return this;',
  19627. ' };',
  19628. '});',
  19629. 'this.Obj = null;',
  19630. 'this.Child = null;',
  19631. 'this.i = null;',
  19632. 'this.Bird = null;',
  19633. 'this.j = null;',
  19634. 'this.r = this.TRec.$new();',
  19635. 'this.c = null;',
  19636. '']),
  19637. LinesToStr([ // $mod.$main
  19638. '$mod.j = $mod.IUnknown;',
  19639. '$mod.j = $mod.IBird;',
  19640. '$mod.j = $mod.TObject;',
  19641. '$mod.j = $mod.TChild;',
  19642. '$mod.j = $mod.TRec;',
  19643. '$mod.j = $mod.Obj;',
  19644. '$mod.j = $mod.Child;',
  19645. '$mod.j = $mod.i;',
  19646. '$mod.j = $mod.Bird;',
  19647. '$mod.j = $mod.r;',
  19648. '$mod.j = $mod.c;',
  19649. '']));
  19650. end;
  19651. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19652. begin
  19653. StartProgram(false);
  19654. Add('{$modeswitch externalclass}');
  19655. Add('type');
  19656. Add(' TJSString = class external name ''String''');
  19657. Add(' class function fromCharCode() : string; varargs;');
  19658. Add(' function anchor(const aName : string) : string;');
  19659. Add(' end;');
  19660. Add('var');
  19661. Add(' s: string;');
  19662. Add('begin');
  19663. Add(' s:=TJSString.fromCharCode(65,66);');
  19664. Add(' s:=TJSString(s).anchor(s);');
  19665. Add(' s:=TJSString(''foo'').anchor(s);');
  19666. ConvertProgram;
  19667. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19668. LinesToStr([ // statements
  19669. 'this.s = "";',
  19670. '']),
  19671. LinesToStr([ // $mod.$main
  19672. '$mod.s = String.fromCharCode(65, 66);',
  19673. '$mod.s = $mod.s.anchor($mod.s);',
  19674. '$mod.s = "foo".anchor($mod.s);',
  19675. '']));
  19676. end;
  19677. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19678. begin
  19679. StartProgram(false);
  19680. Add([
  19681. '{$modeswitch externalclass}',
  19682. 'type',
  19683. ' TJSObject = class external name ''Object'' end;',
  19684. ' TJSFunction = class external name ''Function''',
  19685. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19686. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19687. ' end;',
  19688. ' TObject = class',
  19689. ' procedure DoIt(i: longint);',
  19690. ' end;',
  19691. ' TFuncInt = function(o: TObject): longint;',
  19692. 'function GetIt(o: TObject): longint;',
  19693. ' procedure Sub; begin end;',
  19694. 'var',
  19695. ' f: TJSFunction;',
  19696. ' fi: TFuncInt;',
  19697. 'begin',
  19698. ' fi:=TFuncInt(f);',
  19699. ' f:=TJSFunction(fi);',
  19700. ' f:=TJSFunction(@GetIt);',
  19701. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19702. ' f:=TJSFunction(@Sub);',
  19703. ' f:=TJSFunction(@o.doit);',
  19704. ' f:=TJSFunction(fi).bind(nil,4)',
  19705. 'end;',
  19706. 'procedure TObject.DoIt(i: longint);',
  19707. ' procedure Sub; begin end;',
  19708. 'var f: TJSFunction;',
  19709. 'begin',
  19710. ' f:=TJSFunction(@DoIt);',
  19711. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19712. ' f:=TJSFunction(@Sub);',
  19713. ' f:=TJSFunction(@GetIt);',
  19714. 'end;',
  19715. 'begin']);
  19716. ConvertProgram;
  19717. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19718. LinesToStr([ // statements
  19719. 'rtl.createClass(this, "TObject", null, function () {',
  19720. ' this.$init = function () {',
  19721. ' };',
  19722. ' this.$final = function () {',
  19723. ' };',
  19724. ' this.DoIt = function (i) {',
  19725. ' var $Self = this;',
  19726. ' function Sub() {',
  19727. ' };',
  19728. ' var f = null;',
  19729. ' f = this.DoIt;',
  19730. ' f = this.DoIt.bind(null, 13);',
  19731. ' f = Sub;',
  19732. ' f = $mod.GetIt;',
  19733. ' };',
  19734. '});',
  19735. 'this.GetIt = function (o) {',
  19736. ' var Result = 0;',
  19737. ' function Sub() {',
  19738. ' };',
  19739. ' var f = null;',
  19740. ' var fi = null;',
  19741. ' fi = f;',
  19742. ' f = fi;',
  19743. ' f = $mod.GetIt;',
  19744. ' f = $mod.GetIt.bind(null, 3);',
  19745. ' f = Sub;',
  19746. ' f = $mod.TObject.DoIt;',
  19747. ' f = fi.bind(null, 4);',
  19748. ' return Result;',
  19749. '};',
  19750. '']),
  19751. LinesToStr([ // $mod.$main
  19752. '']));
  19753. end;
  19754. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19755. begin
  19756. StartProgram(false);
  19757. Add([
  19758. '{$mode delphi}',
  19759. '{$modeswitch externalclass}',
  19760. 'type',
  19761. ' TJSObject = class external name ''Object'' end;',
  19762. ' TJSWindow = class external name ''Window''(TJSObject)',
  19763. ' procedure Open;',
  19764. ' end;',
  19765. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19766. ' procedure Execute;',
  19767. ' end;',
  19768. 'procedure Fly;',
  19769. 'var',
  19770. ' w: TJSWindow;',
  19771. ' e: TJSEventTarget;',
  19772. 'begin',
  19773. ' w:=TJSWindow(e);',
  19774. ' e:=TJSEventTarget(w);',
  19775. 'end;',
  19776. 'begin']);
  19777. ConvertProgram;
  19778. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19779. LinesToStr([ // statements
  19780. 'this.Fly = function () {',
  19781. ' var w = null;',
  19782. ' var e = null;',
  19783. ' w = e;',
  19784. ' e = w;',
  19785. '};',
  19786. '']),
  19787. LinesToStr([ // $mod.$main
  19788. '']));
  19789. end;
  19790. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19791. begin
  19792. StartProgram(false);
  19793. Add('{$modeswitch externalclass}');
  19794. Add('type');
  19795. Add(' TJSString = class external name ''String''');
  19796. Add(' class function fromCharCode() : string; varargs;');
  19797. Add(' end;');
  19798. Add('var');
  19799. Add(' s: string;');
  19800. Add(' sObj: TJSString;');
  19801. Add('begin');
  19802. Add(' s:=sObj.fromCharCode(65,66);');
  19803. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19804. nExternalClassInstanceCannotAccessStaticX);
  19805. ConvertProgram;
  19806. end;
  19807. procedure TTestModule.TestExternalClass_BracketAccessor;
  19808. begin
  19809. StartProgram(false);
  19810. Add([
  19811. '{$modeswitch externalclass}',
  19812. 'type',
  19813. ' TJSArray = class external name ''Array2''',
  19814. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19815. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19816. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19817. ' end;',
  19818. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19819. 'begin end;',
  19820. 'var',
  19821. ' Arr: tjsarray;',
  19822. ' s: string;',
  19823. ' i: longint;',
  19824. ' v: jsvalue;',
  19825. 'begin',
  19826. ' v:=arr[0];',
  19827. ' v:=arr.items[1];',
  19828. ' arr[2]:=s;',
  19829. ' arr.items[3]:=s;',
  19830. ' arr[4]:=i;',
  19831. ' arr[5]:=arr[6];',
  19832. ' arr.items[7]:=arr.items[8];',
  19833. ' with arr do items[9]:=items[10];',
  19834. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19835. ' with arr do begin',
  19836. ' v:=GetItems(14);',
  19837. ' setitems(15,16);',
  19838. ' end;',
  19839. ' v:=test1.arr.items[17];',
  19840. ' test1.arr.items[18]:=v;',
  19841. '']);
  19842. ConvertProgram;
  19843. CheckSource('TestExternalClass_BracketAccessor',
  19844. LinesToStr([ // statements
  19845. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19846. '};',
  19847. 'this.Arr = null;',
  19848. 'this.s = "";',
  19849. 'this.i = 0;',
  19850. 'this.v = undefined;',
  19851. '']),
  19852. LinesToStr([ // $mod.$main
  19853. '$mod.v = $mod.Arr[0];',
  19854. '$mod.v = $mod.Arr[1];',
  19855. '$mod.Arr[2] = $mod.s;',
  19856. '$mod.Arr[3] = $mod.s;',
  19857. '$mod.Arr[4] = $mod.i;',
  19858. '$mod.Arr[5] = $mod.Arr[6];',
  19859. '$mod.Arr[7] = $mod.Arr[8];',
  19860. 'var $with = $mod.Arr;',
  19861. '$with[9] = $with[10];',
  19862. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19863. ' a: 9,',
  19864. ' p: $mod.Arr,',
  19865. ' get: function () {',
  19866. ' return this.p[this.a];',
  19867. ' },',
  19868. ' set: function (v) {',
  19869. ' this.p[this.a] = v;',
  19870. ' }',
  19871. '}, {',
  19872. ' a: 10,',
  19873. ' p: $mod.Arr,',
  19874. ' get: function () {',
  19875. ' return this.p[this.a];',
  19876. ' },',
  19877. ' set: function (v) {',
  19878. ' this.p[this.a] = v;',
  19879. ' }',
  19880. '});',
  19881. 'var $with1 = $mod.Arr;',
  19882. '$mod.v = $with1[14];',
  19883. '$with1[15] = 16;',
  19884. '$mod.v = $mod.Arr[17];',
  19885. '$mod.Arr[18] = $mod.v;',
  19886. '']));
  19887. end;
  19888. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19889. begin
  19890. StartProgram(false);
  19891. Add([
  19892. '{$modeswitch externalclass}',
  19893. 'type',
  19894. ' TJSArray = class external name ''Array2''',
  19895. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19896. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19897. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19898. ' end;',
  19899. ' TMyArr = class(TJSArray)',
  19900. ' procedure DoIt;',
  19901. ' end;',
  19902. 'procedure tmyarr.DoIt;',
  19903. 'begin',
  19904. ' Items[1]:=Items[2];',
  19905. ' SetItems(3,getItems(4));',
  19906. 'end;',
  19907. 'var',
  19908. ' Arr: tmyarr;',
  19909. ' s: string;',
  19910. ' i: longint;',
  19911. ' v: jsvalue;',
  19912. 'begin',
  19913. ' v:=arr[0];',
  19914. ' v:=arr.items[1];',
  19915. ' arr[2]:=s;',
  19916. ' arr.items[3]:=s;',
  19917. ' arr[4]:=i;',
  19918. ' arr[5]:=arr[6];',
  19919. ' arr.items[7]:=arr.items[8];',
  19920. ' with arr do items[9]:=items[10];',
  19921. ' with arr do begin',
  19922. ' v:=GetItems(14);',
  19923. ' setitems(15,16);',
  19924. ' end;',
  19925. '']);
  19926. ConvertProgram;
  19927. CheckSource('TestExternalClass_BracketAccessor_Call',
  19928. LinesToStr([ // statements
  19929. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19930. ' this.$init = function () {',
  19931. ' };',
  19932. ' this.$final = function () {',
  19933. ' };',
  19934. ' this.DoIt = function () {',
  19935. ' this[1] = this[2];',
  19936. ' this[3] = this[4];',
  19937. ' };',
  19938. '});',
  19939. 'this.Arr = null;',
  19940. 'this.s = "";',
  19941. 'this.i = 0;',
  19942. 'this.v = undefined;',
  19943. '']),
  19944. LinesToStr([ // $mod.$main
  19945. '$mod.v = $mod.Arr[0];',
  19946. '$mod.v = $mod.Arr[1];',
  19947. '$mod.Arr[2] = $mod.s;',
  19948. '$mod.Arr[3] = $mod.s;',
  19949. '$mod.Arr[4] = $mod.i;',
  19950. '$mod.Arr[5] = $mod.Arr[6];',
  19951. '$mod.Arr[7] = $mod.Arr[8];',
  19952. 'var $with = $mod.Arr;',
  19953. '$with[9] = $with[10];',
  19954. 'var $with1 = $mod.Arr;',
  19955. '$mod.v = $with1[14];',
  19956. '$with1[15] = 16;',
  19957. '']));
  19958. end;
  19959. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19960. begin
  19961. StartProgram(false);
  19962. Add('{$modeswitch externalclass}');
  19963. Add('type');
  19964. Add(' TJSArray = class external name ''Array2''');
  19965. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19966. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19967. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19968. Add(' end;');
  19969. Add('begin');
  19970. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19971. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19972. ConvertProgram;
  19973. end;
  19974. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19975. begin
  19976. StartProgram(false);
  19977. Add('{$modeswitch externalclass}');
  19978. Add('type');
  19979. Add(' TJSArray = class external name ''Array2''');
  19980. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19981. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19982. Add(' end;');
  19983. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19984. Add('begin end;');
  19985. Add('var');
  19986. Add(' Arr: tjsarray;');
  19987. Add(' v: jsvalue;');
  19988. Add('begin');
  19989. Add(' v:=arr[0];');
  19990. Add(' v:=arr.items[1];');
  19991. Add(' with arr do v:=items[2];');
  19992. Add(' doit(arr[3],arr[4]);');
  19993. ConvertProgram;
  19994. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19995. LinesToStr([ // statements
  19996. 'this.DoIt = function (vI, vJ) {',
  19997. '};',
  19998. 'this.Arr = null;',
  19999. 'this.v = undefined;',
  20000. '']),
  20001. LinesToStr([ // $mod.$main
  20002. '$mod.v = $mod.Arr[0];',
  20003. '$mod.v = $mod.Arr[1];',
  20004. 'var $with = $mod.Arr;',
  20005. '$mod.v = $with[2];',
  20006. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20007. '']));
  20008. end;
  20009. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20010. begin
  20011. StartProgram(false);
  20012. Add('{$modeswitch externalclass}');
  20013. Add('type');
  20014. Add(' TJSArray = class external name ''Array2''');
  20015. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20016. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20017. Add(' end;');
  20018. Add('var');
  20019. Add(' Arr: tjsarray;');
  20020. Add(' s: string;');
  20021. Add(' i: longint;');
  20022. Add(' v: jsvalue;');
  20023. Add('begin');
  20024. Add(' arr[2]:=s;');
  20025. Add(' arr.items[3]:=s;');
  20026. Add(' arr[4]:=i;');
  20027. Add(' with arr do items[5]:=i;');
  20028. ConvertProgram;
  20029. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20030. LinesToStr([ // statements
  20031. 'this.Arr = null;',
  20032. 'this.s = "";',
  20033. 'this.i = 0;',
  20034. 'this.v = undefined;',
  20035. '']),
  20036. LinesToStr([ // $mod.$main
  20037. '$mod.Arr[2] = $mod.s;',
  20038. '$mod.Arr[3] = $mod.s;',
  20039. '$mod.Arr[4] = $mod.i;',
  20040. 'var $with = $mod.Arr;',
  20041. '$with[5] = $mod.i;',
  20042. '']));
  20043. end;
  20044. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20045. begin
  20046. StartProgram(false);
  20047. Add('{$modeswitch externalclass}');
  20048. Add('type');
  20049. Add(' TJSArray = class external name ''Array2''');
  20050. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20051. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20052. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20053. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20054. Add(' end;');
  20055. Add('var');
  20056. Add(' Arr: tjsarray;');
  20057. Add(' s: string;');
  20058. Add(' i: longint;');
  20059. Add(' v: jsvalue;');
  20060. Add('begin');
  20061. Add(' arr[2]:=s;');
  20062. Add(' arr.items[3]:=s;');
  20063. Add(' arr.numbers[4]:=i;');
  20064. Add(' with arr do items[5]:=i;');
  20065. Add(' with arr do numbers[6]:=i;');
  20066. ConvertProgram;
  20067. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20068. LinesToStr([ // statements
  20069. 'this.Arr = null;',
  20070. 'this.s = "";',
  20071. 'this.i = 0;',
  20072. 'this.v = undefined;',
  20073. '']),
  20074. LinesToStr([ // $mod.$main
  20075. '$mod.Arr[2] = $mod.s;',
  20076. '$mod.Arr[3] = $mod.s;',
  20077. '$mod.Arr[4] = $mod.i;',
  20078. 'var $with = $mod.Arr;',
  20079. '$with[5] = $mod.i;',
  20080. 'var $with1 = $mod.Arr;',
  20081. '$with1[6] = $mod.i;',
  20082. '']));
  20083. end;
  20084. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20085. begin
  20086. StartProgram(false);
  20087. Add('{$modeswitch externalclass}');
  20088. Add('type');
  20089. Add(' TJSArray = class external name ''Array2''');
  20090. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20091. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20092. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20093. Add(' end;');
  20094. Add('var');
  20095. Add(' Arr: tjsarray;');
  20096. Add(' i: longint;');
  20097. Add(' IntArr: array of longint;');
  20098. Add(' v: jsvalue;');
  20099. Add('begin');
  20100. Add(' v:=arr.items[i];');
  20101. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20102. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20103. ConvertProgram;
  20104. CheckSource('TestExternalClass_BracketAccessor_Index',
  20105. LinesToStr([ // statements
  20106. 'this.Arr = null;',
  20107. 'this.i = 0;',
  20108. 'this.IntArr = [];',
  20109. 'this.v = undefined;',
  20110. '']),
  20111. LinesToStr([ // $mod.$main
  20112. '$mod.v = $mod.Arr[$mod.i];',
  20113. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20114. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20115. '']));
  20116. end;
  20117. procedure TTestModule.TestExternalClass_ForInJSObject;
  20118. begin
  20119. StartProgram(false);
  20120. Add([
  20121. '{$modeswitch externalclass}',
  20122. 'type',
  20123. ' TJSObject = class external name ''Object''',
  20124. ' end;',
  20125. 'var',
  20126. ' o: TJSObject;',
  20127. ' key: string;',
  20128. 'begin',
  20129. ' for key in o do',
  20130. ' if key=''abc'' then ;',
  20131. '']);
  20132. ConvertProgram;
  20133. CheckSource('TestExternalClass_ForInJSObject',
  20134. LinesToStr([ // statements
  20135. 'this.o = null;',
  20136. 'this.key = "";',
  20137. '']),
  20138. LinesToStr([ // $mod.$main
  20139. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20140. '']));
  20141. end;
  20142. procedure TTestModule.TestExternalClass_ForInJSArray;
  20143. begin
  20144. StartProgram(false);
  20145. Add([
  20146. '{$modeswitch externalclass}',
  20147. 'type',
  20148. ' TJSInt8Array = class external name ''Int8Array''',
  20149. ' private',
  20150. ' flength: NativeInt external name ''length'';',
  20151. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20152. ' public',
  20153. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20154. ' property Length: NativeInt read flength;',
  20155. ' end;',
  20156. 'var',
  20157. ' a: TJSInt8Array;',
  20158. ' value: shortint;',
  20159. 'begin',
  20160. ' for value in a do',
  20161. ' if value=3 then ;',
  20162. '']);
  20163. ConvertProgram;
  20164. CheckSource('TestExternalClass_ForInJSArray',
  20165. LinesToStr([ // statements
  20166. 'this.a = null;',
  20167. 'this.value = 0;',
  20168. '']),
  20169. LinesToStr([ // $mod.$main
  20170. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20171. ' $mod.value = $in[$l];',
  20172. ' if ($mod.value === 3) ;',
  20173. '};',
  20174. '']));
  20175. end;
  20176. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20177. begin
  20178. AddModuleWithIntfImplSrc('unit2.pas',
  20179. LinesToStr([
  20180. '{$modeswitch externalclass}',
  20181. 'type',
  20182. ' TJSBufferSource = class external name ''BufferSource''',
  20183. ' end;',
  20184. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20185. '']),
  20186. '');
  20187. AddModuleWithIntfImplSrc('unit3.pas',
  20188. LinesToStr([
  20189. '{$modeswitch externalclass}',
  20190. 'type',
  20191. ' TJSBufferSource = class external name ''BufferSource''',
  20192. ' end;',
  20193. '']),
  20194. '');
  20195. StartUnit(true);
  20196. Add([
  20197. 'interface',
  20198. 'uses unit2, unit3;',
  20199. 'procedure DoSome(s: TJSBufferSource);',
  20200. 'implementation',
  20201. 'procedure DoSome(s: TJSBufferSource);',
  20202. 'begin',
  20203. ' DoIt(s);',
  20204. 'end;',
  20205. '']);
  20206. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20207. nIncompatibleTypeArgNo);
  20208. ConvertUnit;
  20209. end;
  20210. procedure TTestModule.TestExternalClass_NestedConstructor;
  20211. begin
  20212. StartProgram(false);
  20213. Add([
  20214. '{$modeswitch externalclass}',
  20215. 'type',
  20216. ' TJSObject = class external name ''Object''',
  20217. ' type TBird = class external name ''Bird''',
  20218. ' type TWing = class external name ''Wing''',
  20219. ' constructor New;',
  20220. ' constructor Create(w: word = 3);',
  20221. ' end;',
  20222. ' end;',
  20223. ' end;',
  20224. 'var',
  20225. ' w: TJSObject.TBird.TWing;',
  20226. 'begin',
  20227. ' w:=tjsobject.tbird.twing.new;',
  20228. ' w:=tjsobject.tbird.twing.new();',
  20229. ' w:=tjsobject.tbird.twing.create;',
  20230. ' w:=tjsobject.tbird.twing.create(4);',
  20231. ' with tjsobject do begin',
  20232. ' w:=tbird.twing.new;',
  20233. ' w:=tbird.twing.new();',
  20234. ' w:=tbird.twing.create;',
  20235. ' w:=tbird.twing.create(11);',
  20236. ' end;',
  20237. ' with tjsobject.tbird do begin',
  20238. ' w:=twing.new;',
  20239. ' w:=twing.new();',
  20240. ' w:=twing.create;',
  20241. ' w:=twing.create(21);',
  20242. ' end;',
  20243. ' with tjsobject.tbird.twing do begin',
  20244. ' w:=new;',
  20245. ' w:=new();',
  20246. ' w:=create;',
  20247. ' w:=create(31);',
  20248. ' end;',
  20249. '']);
  20250. ConvertProgram;
  20251. CheckSource('TestExternalClass_NestedConstructor',
  20252. LinesToStr([ // statements
  20253. 'this.w = null;',
  20254. '']),
  20255. LinesToStr([ // $mod.$main
  20256. '$mod.w = new Object.Bird.Wing();',
  20257. '$mod.w = new Object.Bird.Wing();',
  20258. '$mod.w = new Object.Bird.Wing.Create();',
  20259. '$mod.w = new Object.Bird.Wing.Create(4);',
  20260. '$mod.w = new Object.Bird.Wing();',
  20261. '$mod.w = new Object.Bird.Wing();',
  20262. '$mod.w = new Object.Bird.Wing.Create();',
  20263. '$mod.w = new Object.Bird.Wing.Create(11);',
  20264. 'var $with = Object.Bird;',
  20265. '$mod.w = new Object.Bird.Wing();',
  20266. '$mod.w = new Object.Bird.Wing();',
  20267. '$mod.w = new Object.Bird.Wing.Create();',
  20268. '$mod.w = new Object.Bird.Wing.Create(21);',
  20269. 'var $with1 = Object.Bird.Wing;',
  20270. '$mod.w = new $with1();',
  20271. '$mod.w = new $with1();',
  20272. '$mod.w = new Object.Bird.Wing.Create();',
  20273. '$mod.w = new Object.Bird.Wing.Create(31);',
  20274. '']));
  20275. end;
  20276. procedure TTestModule.TestClassInterface_Corba;
  20277. begin
  20278. StartProgram(false);
  20279. Add([
  20280. '{$interfaces corba}',
  20281. 'type',
  20282. ' IUnknown = interface;',
  20283. ' IUnknown = interface',
  20284. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20285. ' end;',
  20286. ' IInterface = IUnknown;',
  20287. ' IBird = interface(IInterface)',
  20288. ' function GetSize: longint;',
  20289. ' procedure SetSize(i: longint);',
  20290. ' property Size: longint read GetSize write SetSize;',
  20291. ' procedure DoIt(i: longint);',
  20292. ' end;',
  20293. ' TObject = class',
  20294. ' end;',
  20295. ' TBird = class(TObject,IBird)',
  20296. ' function GetSize: longint; virtual; abstract;',
  20297. ' procedure SetSize(i: longint); virtual; abstract;',
  20298. ' procedure DoIt(i: longint); virtual; abstract;',
  20299. ' end;',
  20300. 'var',
  20301. ' BirdIntf: IBird;',
  20302. 'begin',
  20303. ' BirdIntf.Size:=BirdIntf.Size;',
  20304. '']);
  20305. ConvertProgram;
  20306. CheckSource('TestClassInterface_Corba',
  20307. LinesToStr([ // statements
  20308. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20309. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20310. 'rtl.createClass(this, "TObject", null, function () {',
  20311. ' this.$init = function () {',
  20312. ' };',
  20313. ' this.$final = function () {',
  20314. ' };',
  20315. '});',
  20316. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20317. ' rtl.addIntf(this, $mod.IBird);',
  20318. '});',
  20319. 'this.BirdIntf = null;',
  20320. '']),
  20321. LinesToStr([ // $mod.$main
  20322. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20323. '']));
  20324. end;
  20325. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20326. begin
  20327. StartProgram(false);
  20328. Add([
  20329. '{$interfaces corba}',
  20330. 'type',
  20331. ' IUnknown = interface',
  20332. ' procedure DoIt; external name ''foo'';',
  20333. ' end;',
  20334. 'begin']);
  20335. SetExpectedParserError(
  20336. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20337. nParserNoFieldsAllowed);
  20338. ConvertProgram;
  20339. end;
  20340. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20341. begin
  20342. StartProgram(false);
  20343. Add([
  20344. '{$interfaces corba}',
  20345. 'type',
  20346. ' integer = longint;',
  20347. ' IUnknown = interface',
  20348. ' procedure DoIt(i: integer);',
  20349. ' procedure DoIt(s: string);',
  20350. ' end;',
  20351. ' IBird = interface(IUnknown)',
  20352. ' procedure DoIt(b: boolean); overload;',
  20353. ' end;',
  20354. ' TObject = class',
  20355. ' end;',
  20356. ' TBird = class(TObject,IBird)',
  20357. ' procedure DoIt(o: TObject);',
  20358. ' procedure DoIt(s: string);',
  20359. ' procedure DoIt(i: integer);',
  20360. ' procedure DoIt(b: boolean);',
  20361. ' end;',
  20362. 'procedure TBird.DoIt(o: TObject); begin end;',
  20363. 'procedure TBird.DoIt(s: string); begin end;',
  20364. 'procedure TBird.DoIt(i: integer); begin end;',
  20365. 'procedure TBird.DoIt(b: boolean); begin end;',
  20366. 'var',
  20367. ' BirdIntf: IBird;',
  20368. 'begin',
  20369. ' BirdIntf.DoIt(3);',
  20370. ' BirdIntf.DoIt(''abc'');',
  20371. ' BirdIntf.DoIt(true);',
  20372. '']);
  20373. ConvertProgram;
  20374. CheckSource('TestClassInterface_Overloads',
  20375. LinesToStr([ // statements
  20376. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20377. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20378. 'rtl.createClass(this, "TObject", null, function () {',
  20379. ' this.$init = function () {',
  20380. ' };',
  20381. ' this.$final = function () {',
  20382. ' };',
  20383. '});',
  20384. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20385. ' this.DoIt = function (o) {',
  20386. ' };',
  20387. ' this.DoIt$1 = function (s) {',
  20388. ' };',
  20389. ' this.DoIt$2 = function (i) {',
  20390. ' };',
  20391. ' this.DoIt$3 = function (b) {',
  20392. ' };',
  20393. ' rtl.addIntf(this, $mod.IBird, {',
  20394. ' DoIt$2: "DoIt$3",',
  20395. ' DoIt: "DoIt$2"',
  20396. ' });',
  20397. '});',
  20398. 'this.BirdIntf = null;',
  20399. '']),
  20400. LinesToStr([ // $mod.$main
  20401. '$mod.BirdIntf.DoIt(3);',
  20402. '$mod.BirdIntf.DoIt$1("abc");',
  20403. '$mod.BirdIntf.DoIt$2(true);',
  20404. '']));
  20405. end;
  20406. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20407. begin
  20408. StartProgram(false);
  20409. Add([
  20410. '{$interfaces corba}',
  20411. 'type',
  20412. ' IBird = interface',
  20413. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20414. ' end;',
  20415. ' IDog = interface',
  20416. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20417. ' end;',
  20418. ' TObject = class(IBird,IDog)',
  20419. ' end;',
  20420. 'begin']);
  20421. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20422. nDuplicateGUIDXInYZ);
  20423. ConvertProgram;
  20424. end;
  20425. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20426. begin
  20427. StartProgram(false);
  20428. Add([
  20429. '{$interfaces corba}',
  20430. 'type',
  20431. ' IAnimal = interface',
  20432. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20433. ' end;',
  20434. ' IBird = interface(IAnimal)',
  20435. ' end;',
  20436. ' IHawk = interface(IBird)',
  20437. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20438. ' end;',
  20439. 'begin']);
  20440. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20441. nDuplicateGUIDXInYZ);
  20442. ConvertProgram;
  20443. end;
  20444. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20445. begin
  20446. StartProgram(false);
  20447. Add([
  20448. '{$interfaces corba}',
  20449. 'type',
  20450. ' integer = longint;',
  20451. ' IUnknown = interface',
  20452. ' procedure DoIt(i: integer);',
  20453. ' end;',
  20454. ' IBird = interface',
  20455. ' procedure Fly(i: integer);',
  20456. ' end;',
  20457. ' TObject = class(IUnknown)',
  20458. ' procedure DoIt(i: integer);',
  20459. ' end;',
  20460. ' TBird = class(IBird)',
  20461. ' procedure Fly(i: integer);',
  20462. ' end;',
  20463. 'procedure TObject.DoIt(i: integer); begin end;',
  20464. 'procedure TBird.Fly(i: integer); begin end;',
  20465. 'begin',
  20466. '']);
  20467. ConvertProgram;
  20468. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20469. LinesToStr([ // statements
  20470. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20471. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20472. 'rtl.createClass(this, "TObject", null, function () {',
  20473. ' this.$init = function () {',
  20474. ' };',
  20475. ' this.$final = function () {',
  20476. ' };',
  20477. ' this.DoIt = function (i) {',
  20478. ' };',
  20479. ' rtl.addIntf(this, $mod.IUnknown);',
  20480. '});',
  20481. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20482. ' this.Fly = function (i) {',
  20483. ' };',
  20484. ' rtl.addIntf(this, $mod.IBird);',
  20485. ' rtl.addIntf(this, $mod.IUnknown);',
  20486. '});',
  20487. '']),
  20488. LinesToStr([ // $mod.$main
  20489. '']));
  20490. end;
  20491. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20492. begin
  20493. StartProgram(false);
  20494. Add([
  20495. '{$interfaces corba}',
  20496. 'type',
  20497. ' integer = longint;',
  20498. ' IBird = interface',
  20499. ' procedure DoIt(i: integer);',
  20500. ' end;',
  20501. ' TObject = class',
  20502. ' procedure DoIt(i: integer);',
  20503. ' end;',
  20504. ' TBird = class(IBird)',
  20505. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20506. ' end;',
  20507. 'procedure TObject.DoIt(i: integer); begin end;',
  20508. 'procedure TBird.DoIt(i: integer); begin end;',
  20509. 'begin',
  20510. '']);
  20511. ConvertProgram;
  20512. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20513. LinesToStr([ // statements
  20514. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20515. 'rtl.createClass(this, "TObject", null, function () {',
  20516. ' this.$init = function () {',
  20517. ' };',
  20518. ' this.$final = function () {',
  20519. ' };',
  20520. ' this.DoIt = function (i) {',
  20521. ' };',
  20522. '});',
  20523. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20524. ' this.DoIt$1 = function (i) {',
  20525. ' };',
  20526. ' rtl.addIntf(this, $mod.IBird, {',
  20527. ' DoIt: "DoIt$1"',
  20528. ' });',
  20529. '});',
  20530. '']),
  20531. LinesToStr([ // $mod.$main
  20532. '']));
  20533. end;
  20534. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20535. begin
  20536. StartProgram(false);
  20537. Add([
  20538. '{$interfaces corba}',
  20539. 'type',
  20540. ' IUnknown = interface',
  20541. ' procedure Walk(i: longint);',
  20542. ' end;',
  20543. ' IBird = interface(IUnknown)',
  20544. ' procedure Walk(b: boolean); overload;',
  20545. ' procedure Fly(s: string);',
  20546. ' end;',
  20547. ' TObject = class',
  20548. ' end;',
  20549. ' TBird = class(TObject,IBird)',
  20550. ' procedure IBird.Fly = Move;',
  20551. ' procedure IBird.Walk = Hop;',
  20552. ' procedure Hop(i: longint);',
  20553. ' procedure Move(s: string);',
  20554. ' procedure Hop(b: boolean);',
  20555. ' end;',
  20556. 'procedure TBird.Move(s: string); begin end;',
  20557. 'procedure TBird.Hop(i: longint); begin end;',
  20558. 'procedure TBird.Hop(b: boolean); begin end;',
  20559. 'var',
  20560. ' BirdIntf: IBird;',
  20561. 'begin',
  20562. ' BirdIntf.Walk(3);',
  20563. ' BirdIntf.Walk(true);',
  20564. ' BirdIntf.Fly(''abc'');',
  20565. '']);
  20566. ConvertProgram;
  20567. CheckSource('TestClassInterface_Corba_MethodResolution',
  20568. LinesToStr([ // statements
  20569. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20570. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20571. 'rtl.createClass(this, "TObject", null, function () {',
  20572. ' this.$init = function () {',
  20573. ' };',
  20574. ' this.$final = function () {',
  20575. ' };',
  20576. '});',
  20577. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20578. ' this.Hop = function (i) {',
  20579. ' };',
  20580. ' this.Move = function (s) {',
  20581. ' };',
  20582. ' this.Hop$1 = function (b) {',
  20583. ' };',
  20584. ' rtl.addIntf(this, $mod.IBird, {',
  20585. ' Walk$1: "Hop$1",',
  20586. ' Fly: "Move",',
  20587. ' Walk: "Hop"',
  20588. ' });',
  20589. '});',
  20590. 'this.BirdIntf = null;',
  20591. '']),
  20592. LinesToStr([ // $mod.$main
  20593. '$mod.BirdIntf.Walk(3);',
  20594. '$mod.BirdIntf.Walk$1(true);',
  20595. '$mod.BirdIntf.Fly("abc");',
  20596. '']));
  20597. end;
  20598. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20599. begin
  20600. StartProgram(false);
  20601. Add([
  20602. '{$interfaces com}',
  20603. 'type',
  20604. ' IUnknown = interface',
  20605. ' function _AddRef: longint;',
  20606. ' procedure Walk;',
  20607. ' end;',
  20608. ' IBird = interface end;',
  20609. ' IDog = interface end;',
  20610. ' TObject = class(IBird,IDog)',
  20611. ' function _AddRef: longint; virtual; abstract;',
  20612. ' procedure Walk; virtual; abstract;',
  20613. ' end;',
  20614. ' TBird = class(IUnknown)',
  20615. ' end;',
  20616. 'begin',
  20617. '']);
  20618. ConvertProgram;
  20619. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20620. LinesToStr([ // statements
  20621. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20622. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20623. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20624. 'rtl.createClass(this, "TObject", null, function () {',
  20625. ' this.$init = function () {',
  20626. ' };',
  20627. ' this.$final = function () {',
  20628. ' };',
  20629. ' rtl.addIntf(this, $mod.IBird);',
  20630. ' rtl.addIntf(this, $mod.IDog);',
  20631. '});',
  20632. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20633. ' rtl.addIntf(this, $mod.IUnknown);',
  20634. ' rtl.addIntf(this, $mod.IBird);',
  20635. ' rtl.addIntf(this, $mod.IDog);',
  20636. '});',
  20637. '']),
  20638. LinesToStr([ // $mod.$main
  20639. '']));
  20640. end;
  20641. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20642. begin
  20643. StartProgram(false);
  20644. Add([
  20645. '{$interfaces corba}',
  20646. 'type',
  20647. ' IUnknown = interface',
  20648. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20649. ' procedure Go;',
  20650. ' end;',
  20651. ' TObject = class(IUnknown)',
  20652. ' procedure Go; virtual; abstract;',
  20653. ' end;',
  20654. ' TBird = class',
  20655. ' procedure Go; override;',
  20656. ' end;',
  20657. ' TCat = class(TObject)',
  20658. ' procedure Go; override;',
  20659. ' end;',
  20660. ' TDog = class(TObject, IUnknown)',
  20661. ' procedure Go; override;',
  20662. ' end;',
  20663. 'procedure TBird.Go; begin end;',
  20664. 'procedure TCat.Go; begin end;',
  20665. 'procedure TDog.Go; begin end;',
  20666. 'begin',
  20667. '']);
  20668. ConvertProgram;
  20669. CheckSource('TestClassInterface_Corba_MethodOverride',
  20670. LinesToStr([ // statements
  20671. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20672. 'rtl.createClass(this, "TObject", null, function () {',
  20673. ' this.$init = function () {',
  20674. ' };',
  20675. ' this.$final = function () {',
  20676. ' };',
  20677. ' rtl.addIntf(this, $mod.IUnknown);',
  20678. '});',
  20679. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20680. ' this.Go = function () {',
  20681. ' };',
  20682. ' rtl.addIntf(this, $mod.IUnknown);',
  20683. '});',
  20684. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20685. ' this.Go = function () {',
  20686. ' };',
  20687. ' rtl.addIntf(this, $mod.IUnknown);',
  20688. '});',
  20689. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20690. ' this.Go = function () {',
  20691. ' };',
  20692. ' rtl.addIntf(this, $mod.IUnknown);',
  20693. '});',
  20694. '']),
  20695. LinesToStr([ // $mod.$main
  20696. '']));
  20697. end;
  20698. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20699. begin
  20700. StartProgram(false);
  20701. Add([
  20702. '{$interfaces corba}',
  20703. 'type',
  20704. ' IUnknown = interface',
  20705. ' end;',
  20706. ' IBird = interface(IUnknown)',
  20707. ' procedure Fly(s: string);',
  20708. ' end;',
  20709. ' IEagle = interface(IBird)',
  20710. ' end;',
  20711. ' IDove = interface(IBird)',
  20712. ' end;',
  20713. ' ISwallow = interface(IBird)',
  20714. ' end;',
  20715. ' TObject = class',
  20716. ' end;',
  20717. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20718. ' procedure Fly(s: string); virtual; abstract;',
  20719. ' end;',
  20720. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20721. ' FBirdIntf: IBird;',
  20722. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20723. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20724. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20725. ' FDoveObj: TBird;',
  20726. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20727. ' function GetSwallowObj: TBird; virtual; abstract;',
  20728. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20729. ' end;',
  20730. 'begin',
  20731. '']);
  20732. ConvertProgram;
  20733. CheckSource('TestClassInterface_Corba_Delegation',
  20734. LinesToStr([ // statements
  20735. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20736. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20737. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20738. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20739. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20740. 'rtl.createClass(this, "TObject", null, function () {',
  20741. ' this.$init = function () {',
  20742. ' };',
  20743. ' this.$final = function () {',
  20744. ' };',
  20745. '});',
  20746. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20747. ' rtl.addIntf(this, $mod.IBird);',
  20748. ' rtl.addIntf(this, $mod.IEagle);',
  20749. ' rtl.addIntf(this, $mod.IDove);',
  20750. ' rtl.addIntf(this, $mod.ISwallow);',
  20751. '});',
  20752. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20753. ' this.$init = function () {',
  20754. ' $mod.TObject.$init.call(this);',
  20755. ' this.FBirdIntf = null;',
  20756. ' this.FDoveObj = null;',
  20757. ' };',
  20758. ' this.$final = function () {',
  20759. ' this.FBirdIntf = undefined;',
  20760. ' this.FDoveObj = undefined;',
  20761. ' $mod.TObject.$final.call(this);',
  20762. ' };',
  20763. ' this.$intfmaps = {',
  20764. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20765. ' return this.FBirdIntf;',
  20766. ' },',
  20767. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20768. ' return this.GetEagleIntf();',
  20769. ' },',
  20770. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20771. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20772. ' },',
  20773. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20774. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20775. ' }',
  20776. ' };',
  20777. '});',
  20778. '']),
  20779. LinesToStr([ // $mod.$main
  20780. '']));
  20781. end;
  20782. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20783. begin
  20784. StartProgram(false);
  20785. Add([
  20786. '{$interfaces corba}',
  20787. 'type',
  20788. ' IUnknown = interface',
  20789. ' end;',
  20790. ' IBird = interface(IUnknown)',
  20791. ' procedure Fly(s: string);',
  20792. ' end;',
  20793. ' IEagle = interface(IBird)',
  20794. ' end;',
  20795. ' IDove = interface(IBird)',
  20796. ' end;',
  20797. ' ISwallow = interface(IBird)',
  20798. ' end;',
  20799. ' TObject = class',
  20800. ' end;',
  20801. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20802. ' procedure Fly(s: string); virtual; abstract;',
  20803. ' end;',
  20804. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20805. ' private',
  20806. ' class var FBirdIntf: IBird;',
  20807. ' class var FDoveObj: TBird;',
  20808. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20809. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20810. ' protected',
  20811. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20812. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20813. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20814. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20815. ' end;',
  20816. 'begin',
  20817. '']);
  20818. ConvertProgram;
  20819. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20820. LinesToStr([ // statements
  20821. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20822. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20823. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20824. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20825. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20826. 'rtl.createClass(this, "TObject", null, function () {',
  20827. ' this.$init = function () {',
  20828. ' };',
  20829. ' this.$final = function () {',
  20830. ' };',
  20831. '});',
  20832. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20833. ' rtl.addIntf(this, $mod.IBird);',
  20834. ' rtl.addIntf(this, $mod.IEagle);',
  20835. ' rtl.addIntf(this, $mod.IDove);',
  20836. ' rtl.addIntf(this, $mod.ISwallow);',
  20837. '});',
  20838. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20839. ' this.FBirdIntf = null;',
  20840. ' this.FDoveObj = null;',
  20841. ' this.$intfmaps = {',
  20842. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20843. ' return this.FBirdIntf;',
  20844. ' },',
  20845. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20846. ' return this.GetEagleIntf();',
  20847. ' },',
  20848. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20849. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20850. ' },',
  20851. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20852. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20853. ' }',
  20854. ' };',
  20855. '});',
  20856. '']),
  20857. LinesToStr([ // $mod.$main
  20858. '']));
  20859. end;
  20860. procedure TTestModule.TestClassInterface_Corba_Operators;
  20861. begin
  20862. StartProgram(false);
  20863. Add([
  20864. '{$interfaces corba}',
  20865. 'type',
  20866. ' IUnknown = interface',
  20867. ' end;',
  20868. ' IBird = interface(IUnknown)',
  20869. ' function GetItems(Index: longint): longint;',
  20870. ' procedure SetItems(Index: longint; Value: longint);',
  20871. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20872. ' end;',
  20873. ' TObject = class',
  20874. ' end;',
  20875. ' TBird = class(TObject,IBird)',
  20876. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20877. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20878. ' end;',
  20879. 'var',
  20880. ' IntfVar: IBird = nil;',
  20881. ' IntfVar2: IBird;',
  20882. ' ObjVar: TBird;',
  20883. ' v: JSValue;',
  20884. 'begin',
  20885. ' IntfVar:=nil;',
  20886. ' IntfVar[3]:=IntfVar[4];',
  20887. ' if Assigned(IntfVar) then ;',
  20888. ' IntfVar:=IntfVar2;',
  20889. ' IntfVar:=ObjVar;',
  20890. ' if IntfVar=IntfVar2 then ;',
  20891. ' if IntfVar<>IntfVar2 then ;',
  20892. ' if IntfVar is IBird then ;',
  20893. ' if IntfVar is TBird then ;',
  20894. ' if ObjVar is IBird then ;',
  20895. ' IntfVar:=IntfVar2 as IBird;',
  20896. ' ObjVar:=IntfVar2 as TBird;',
  20897. ' IntfVar:=ObjVar as IBird;',
  20898. ' IntfVar:=IBird(IntfVar2);',
  20899. ' ObjVar:=TBird(IntfVar);',
  20900. ' IntfVar:=IBird(ObjVar);',
  20901. ' v:=IntfVar;',
  20902. ' IntfVar:=IBird(v);',
  20903. ' if v is IBird then ;',
  20904. ' v:=JSValue(IntfVar);',
  20905. ' v:=IBird;',
  20906. '']);
  20907. ConvertProgram;
  20908. CheckSource('TestClassInterface_Corba_Operators',
  20909. LinesToStr([ // statements
  20910. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20911. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20912. 'rtl.createClass(this, "TObject", null, function () {',
  20913. ' this.$init = function () {',
  20914. ' };',
  20915. ' this.$final = function () {',
  20916. ' };',
  20917. '});',
  20918. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20919. ' rtl.addIntf(this, $mod.IBird);',
  20920. '});',
  20921. 'this.IntfVar = null;',
  20922. 'this.IntfVar2 = null;',
  20923. 'this.ObjVar = null;',
  20924. 'this.v = undefined;',
  20925. '']),
  20926. LinesToStr([ // $mod.$main
  20927. '$mod.IntfVar = null;',
  20928. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20929. 'if ($mod.IntfVar != null) ;',
  20930. '$mod.IntfVar = $mod.IntfVar2;',
  20931. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20932. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20933. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20934. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20935. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20936. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20937. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20938. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20939. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20940. '$mod.IntfVar = $mod.IntfVar2;',
  20941. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20942. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20943. '$mod.v = $mod.IntfVar;',
  20944. '$mod.IntfVar = rtl.getObject($mod.v);',
  20945. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20946. '$mod.v = $mod.IntfVar;',
  20947. '$mod.v = $mod.IBird;',
  20948. '']));
  20949. end;
  20950. procedure TTestModule.TestClassInterface_Corba_Args;
  20951. begin
  20952. StartProgram(false);
  20953. Add([
  20954. '{$interfaces corba}',
  20955. 'type',
  20956. ' IUnknown = interface',
  20957. ' end;',
  20958. ' IBird = interface(IUnknown)',
  20959. ' end;',
  20960. ' TObject = class',
  20961. ' end;',
  20962. ' TBird = class(TObject,IBird)',
  20963. ' end;',
  20964. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20965. 'begin',
  20966. ' DoIt(i,i,i);',
  20967. 'end;',
  20968. 'procedure Change(var i: IBird; out j: IBird);',
  20969. 'begin',
  20970. ' DoIt(i,i,i);',
  20971. ' Change(i,i);',
  20972. 'end;',
  20973. 'var',
  20974. ' i: IBird;',
  20975. ' o: TBird;',
  20976. 'begin',
  20977. ' DoIt(i,i,i);',
  20978. ' Change(i,i);',
  20979. ' DoIt(o,o,o);',
  20980. '']);
  20981. ConvertProgram;
  20982. CheckSource('TestClassInterface_Corba_Args',
  20983. LinesToStr([ // statements
  20984. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20985. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20986. 'rtl.createClass(this, "TObject", null, function () {',
  20987. ' this.$init = function () {',
  20988. ' };',
  20989. ' this.$final = function () {',
  20990. ' };',
  20991. '});',
  20992. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20993. ' rtl.addIntf(this, $mod.IBird);',
  20994. '});',
  20995. 'this.DoIt = function (u, i, j) {',
  20996. ' $mod.DoIt({',
  20997. ' get: function () {',
  20998. ' return i;',
  20999. ' },',
  21000. ' set: function (v) {',
  21001. ' i = v;',
  21002. ' }',
  21003. ' }, i, i);',
  21004. '};',
  21005. 'this.Change = function (i, j) {',
  21006. ' $mod.DoIt(i, i.get(), i.get());',
  21007. ' $mod.Change(i, i);',
  21008. '};',
  21009. 'this.i = null;',
  21010. 'this.o = null;',
  21011. '']),
  21012. LinesToStr([ // $mod.$main
  21013. '$mod.DoIt({',
  21014. ' p: $mod,',
  21015. ' get: function () {',
  21016. ' return this.p.i;',
  21017. ' },',
  21018. ' set: function (v) {',
  21019. ' this.p.i = v;',
  21020. ' }',
  21021. '}, $mod.i, $mod.i);',
  21022. '$mod.Change({',
  21023. ' p: $mod,',
  21024. ' get: function () {',
  21025. ' return this.p.i;',
  21026. ' },',
  21027. ' set: function (v) {',
  21028. ' this.p.i = v;',
  21029. ' }',
  21030. '}, {',
  21031. ' p: $mod,',
  21032. ' get: function () {',
  21033. ' return this.p.i;',
  21034. ' },',
  21035. ' set: function (v) {',
  21036. ' this.p.i = v;',
  21037. ' }',
  21038. '});',
  21039. '$mod.DoIt({',
  21040. ' p: $mod,',
  21041. ' get: function () {',
  21042. ' return this.p.o;',
  21043. ' },',
  21044. ' set: function (v) {',
  21045. ' this.p.o = v;',
  21046. ' }',
  21047. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21048. '']));
  21049. end;
  21050. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21051. begin
  21052. StartProgram(false);
  21053. Add([
  21054. '{$interfaces corba}',
  21055. 'type',
  21056. ' IUnknown = interface end;',
  21057. ' TObject = class',
  21058. ' Id: longint;',
  21059. ' end;',
  21060. ' IEnumerator = interface(IUnknown)',
  21061. ' function GetCurrent: TObject;',
  21062. ' function MoveNext: Boolean;',
  21063. ' property Current: TObject read GetCurrent;',
  21064. ' end;',
  21065. ' IEnumerable = interface(IUnknown)',
  21066. ' function GetEnumerator: IEnumerator;',
  21067. ' end;',
  21068. 'var',
  21069. ' o: TObject;',
  21070. ' i: IEnumerable;',
  21071. 'begin',
  21072. ' for o in i do o.Id:=3;',
  21073. '']);
  21074. ConvertProgram;
  21075. CheckSource('TestClassInterface_Corba_ForIn',
  21076. LinesToStr([ // statements
  21077. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21078. 'rtl.createClass(this, "TObject", null, function () {',
  21079. ' this.$init = function () {',
  21080. ' this.Id = 0;',
  21081. ' };',
  21082. ' this.$final = function () {',
  21083. ' };',
  21084. '});',
  21085. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21086. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21087. 'this.o = null;',
  21088. 'this.i = null;',
  21089. '']),
  21090. LinesToStr([ // $mod.$main
  21091. 'var $in = $mod.i.GetEnumerator();',
  21092. 'while ($in.MoveNext()) {',
  21093. ' $mod.o = $in.GetCurrent();',
  21094. ' $mod.o.Id = 3;',
  21095. '};',
  21096. '']));
  21097. end;
  21098. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21099. begin
  21100. StartProgram(false);
  21101. Add([
  21102. '{$interfaces corba}',
  21103. 'type',
  21104. ' IUnknown = interface end;',
  21105. ' IBird = interface(IUnknown)',
  21106. ' function Fly(w: word): word;',
  21107. ' end;',
  21108. ' TBirdArray = array of IBird;',
  21109. 'var',
  21110. ' i: IBird;',
  21111. ' a: TBirdArray;',
  21112. 'begin',
  21113. ' SetLength(a,3);',
  21114. ' i:=a[1];',
  21115. ' a[2]:=i;',
  21116. ' for i in a do i.fly(3);',
  21117. '']);
  21118. ConvertProgram;
  21119. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21120. LinesToStr([ // statements
  21121. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21122. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21123. 'this.i = null;',
  21124. 'this.a = [];',
  21125. '']),
  21126. LinesToStr([ // $mod.$main
  21127. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21128. '$mod.i = $mod.a[1];',
  21129. '$mod.a[2] = $mod.i;',
  21130. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21131. ' $mod.i = $in[$l];',
  21132. ' $mod.i.Fly(3);',
  21133. '};',
  21134. '']));
  21135. end;
  21136. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21137. begin
  21138. StartProgram(false);
  21139. Add([
  21140. '{$interfaces com}',
  21141. 'type',
  21142. ' IUnknown = interface',
  21143. ' function _AddRef: longint;',
  21144. ' function _Release: longint;',
  21145. ' end;',
  21146. ' TObject = class(IUnknown)',
  21147. ' function _AddRef: longint; virtual; abstract;',
  21148. ' function _Release: longint; virtual; abstract;',
  21149. ' end;',
  21150. 'var',
  21151. ' i: IUnknown;',
  21152. 'procedure DoGlobal(o: TObject);',
  21153. 'begin',
  21154. ' i:=nil;',
  21155. ' i:=o;',
  21156. ' i:=i;',
  21157. 'end;',
  21158. 'procedure DoLocal(o: TObject);',
  21159. 'const k: IUnknown = nil;',
  21160. 'var j: IUnknown;',
  21161. 'begin',
  21162. ' k:=o;',
  21163. ' k:=i;',
  21164. ' j:=o;',
  21165. ' j:=i;',
  21166. 'end;',
  21167. 'var o: TObject;',
  21168. 'begin',
  21169. ' i:=nil;',
  21170. ' i:=o;',
  21171. '']);
  21172. ConvertProgram;
  21173. CheckSource('TestClassInterface_COM_AssignVar',
  21174. LinesToStr([ // statements
  21175. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21176. 'rtl.createClass(this, "TObject", null, function () {',
  21177. ' this.$init = function () {',
  21178. ' };',
  21179. ' this.$final = function () {',
  21180. ' };',
  21181. ' rtl.addIntf(this, $mod.IUnknown);',
  21182. '});',
  21183. 'this.i = null;',
  21184. 'this.DoGlobal = function (o) {',
  21185. ' rtl.setIntfP($mod, "i", null);',
  21186. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21187. ' rtl.setIntfP($mod, "i", $mod.i);',
  21188. '};',
  21189. 'var k = null;',
  21190. 'this.DoLocal = function (o) {',
  21191. ' var j = null;',
  21192. ' try{',
  21193. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21194. ' k = rtl.setIntfL(k, $mod.i);',
  21195. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21196. ' j = rtl.setIntfL(j, $mod.i);',
  21197. ' }finally{',
  21198. ' rtl._Release(j);',
  21199. ' };',
  21200. '};',
  21201. 'this.o = null;',
  21202. '']),
  21203. LinesToStr([ // $mod.$main
  21204. 'rtl.setIntfP($mod, "i", null);',
  21205. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21206. '']));
  21207. end;
  21208. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21209. begin
  21210. StartProgram(false);
  21211. Add([
  21212. '{$interfaces com}',
  21213. 'type',
  21214. ' IUnknown = interface',
  21215. ' function _AddRef: longint;',
  21216. ' function _Release: longint;',
  21217. ' end;',
  21218. ' TObject = class(IUnknown)',
  21219. ' function _AddRef: longint; virtual; abstract;',
  21220. ' function _Release: longint; virtual; abstract;',
  21221. ' end;',
  21222. 'procedure DoDefault(i, j: IUnknown);',
  21223. 'begin',
  21224. ' i:=nil;',
  21225. ' i:=j;',
  21226. 'end;',
  21227. 'begin',
  21228. '']);
  21229. ConvertProgram;
  21230. CheckSource('TestClassInterface_COM_AssignArg',
  21231. LinesToStr([ // statements
  21232. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21233. 'rtl.createClass(this, "TObject", null, function () {',
  21234. ' this.$init = function () {',
  21235. ' };',
  21236. ' this.$final = function () {',
  21237. ' };',
  21238. ' rtl.addIntf(this, $mod.IUnknown);',
  21239. '});',
  21240. 'this.DoDefault = function (i, j) {',
  21241. ' rtl._AddRef(i);',
  21242. ' try {',
  21243. ' i = rtl.setIntfL(i, null);',
  21244. ' i = rtl.setIntfL(i, j);',
  21245. ' } finally {',
  21246. ' rtl._Release(i);',
  21247. ' };',
  21248. '};',
  21249. '']),
  21250. LinesToStr([ // $mod.$main
  21251. '']));
  21252. end;
  21253. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21254. begin
  21255. StartProgram(false);
  21256. Add([
  21257. '{$interfaces com}',
  21258. 'type',
  21259. ' IUnknown = interface',
  21260. ' function _AddRef: longint;',
  21261. ' function _Release: longint;',
  21262. ' end;',
  21263. ' TObject = class(IUnknown)',
  21264. ' function _AddRef: longint; virtual; abstract;',
  21265. ' function _Release: longint; virtual; abstract;',
  21266. ' end;',
  21267. 'function DoDefault(i: IUnknown): IUnknown;',
  21268. 'begin',
  21269. ' Result:=i;',
  21270. ' if Result<>nil then exit;',
  21271. 'end;',
  21272. 'begin',
  21273. '']);
  21274. ConvertProgram;
  21275. CheckSource('TestClassInterface_COM_FunctionResult',
  21276. LinesToStr([ // statements
  21277. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21278. 'rtl.createClass(this, "TObject", null, function () {',
  21279. ' this.$init = function () {',
  21280. ' };',
  21281. ' this.$final = function () {',
  21282. ' };',
  21283. ' rtl.addIntf(this, $mod.IUnknown);',
  21284. '});',
  21285. 'this.DoDefault = function (i) {',
  21286. ' var Result = null;',
  21287. ' var $ok = false;',
  21288. ' try {',
  21289. ' Result = rtl.setIntfL(Result, i);',
  21290. ' if(Result !== null){',
  21291. ' $ok = true;',
  21292. ' return Result;',
  21293. ' };',
  21294. ' $ok = true;',
  21295. ' } finally {',
  21296. ' if(!$ok) rtl._Release(Result);',
  21297. ' };',
  21298. ' return Result;',
  21299. '};',
  21300. '']),
  21301. LinesToStr([ // $mod.$main
  21302. '']));
  21303. end;
  21304. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21305. begin
  21306. StartProgram(false);
  21307. Add([
  21308. '{$interfaces com}',
  21309. 'type',
  21310. ' IUnknown = interface',
  21311. ' function _AddRef: longint;',
  21312. ' function _Release: longint;',
  21313. ' end;',
  21314. ' TObject = class(IUnknown)',
  21315. ' function _AddRef: longint; virtual; abstract;',
  21316. ' function _Release: longint; virtual; abstract;',
  21317. ' function GetIntf: IUnknown; virtual;',
  21318. ' end;',
  21319. ' TMouse = class',
  21320. ' function GetIntf: IUnknown; override;',
  21321. ' end;',
  21322. 'function TObject.GetIntf: IUnknown; begin end;',
  21323. 'function TMouse.GetIntf: IUnknown;',
  21324. 'var i: IUnknown;',
  21325. 'begin',
  21326. ' inherited;',
  21327. ' inherited GetIntf;',
  21328. ' inherited GetIntf();',
  21329. ' Result:=inherited GetIntf;',
  21330. ' Result:=inherited GetIntf();',
  21331. ' i:=inherited GetIntf;',
  21332. ' i:=inherited GetIntf();',
  21333. 'end;',
  21334. 'begin',
  21335. '']);
  21336. ConvertProgram;
  21337. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21338. LinesToStr([ // statements
  21339. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21340. 'rtl.createClass(this, "TObject", null, function () {',
  21341. ' this.$init = function () {',
  21342. ' };',
  21343. ' this.$final = function () {',
  21344. ' };',
  21345. ' this.GetIntf = function () {',
  21346. ' var Result = null;',
  21347. ' return Result;',
  21348. ' };',
  21349. ' rtl.addIntf(this, $mod.IUnknown);',
  21350. '});',
  21351. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21352. ' this.GetIntf = function () {',
  21353. ' var Result = null;',
  21354. ' var i = null;',
  21355. ' var $ir = rtl.createIntfRefs();',
  21356. ' var $ok = false;',
  21357. ' try {',
  21358. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21359. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21360. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21361. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21362. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21363. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21364. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21365. ' $ok = true;',
  21366. ' } finally {',
  21367. ' $ir.free();',
  21368. ' rtl._Release(i);',
  21369. ' if (!$ok) rtl._Release(Result);',
  21370. ' };',
  21371. ' return Result;',
  21372. ' };',
  21373. ' rtl.addIntf(this, $mod.IUnknown);',
  21374. '});',
  21375. '']),
  21376. LinesToStr([ // $mod.$main
  21377. '']));
  21378. end;
  21379. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21380. begin
  21381. StartProgram(false);
  21382. Add([
  21383. '{$interfaces com}',
  21384. 'type',
  21385. ' IUnknown = interface',
  21386. ' function _AddRef: longint;',
  21387. ' function _Release: longint;',
  21388. ' end;',
  21389. ' TObject = class(IUnknown)',
  21390. ' function _AddRef: longint; virtual; abstract;',
  21391. ' function _Release: longint; virtual; abstract;',
  21392. ' end;',
  21393. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21394. 'begin',
  21395. ' if i is IUnknown then ;',
  21396. ' if o is IUnknown then ;',
  21397. ' if i is TObject then ;',
  21398. ' i:=j as IUnknown;',
  21399. ' i:=o as IUnknown;',
  21400. ' o:=j as TObject;',
  21401. ' i:=IUnknown(j);',
  21402. ' i:=IUnknown(o);',
  21403. ' o:=TObject(i);',
  21404. 'end;',
  21405. 'begin',
  21406. '']);
  21407. ConvertProgram;
  21408. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21409. LinesToStr([ // statements
  21410. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21411. 'rtl.createClass(this, "TObject", null, function () {',
  21412. ' this.$init = function () {',
  21413. ' };',
  21414. ' this.$final = function () {',
  21415. ' };',
  21416. ' rtl.addIntf(this, $mod.IUnknown);',
  21417. '});',
  21418. 'this.DoDefault = function (i, j, o) {',
  21419. ' rtl._AddRef(i);',
  21420. ' try {',
  21421. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21422. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21423. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21424. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21425. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21426. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21427. ' i = rtl.setIntfL(i, j);',
  21428. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21429. ' o = rtl.intfToClass(i, $mod.TObject);',
  21430. ' } finally {',
  21431. ' rtl._Release(i);',
  21432. ' };',
  21433. '};',
  21434. '']),
  21435. LinesToStr([ // $mod.$main
  21436. '']));
  21437. end;
  21438. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21439. begin
  21440. StartProgram(false);
  21441. Add([
  21442. '{$interfaces com}',
  21443. 'type',
  21444. ' IUnknown = interface',
  21445. ' function _AddRef: longint;',
  21446. ' function _Release: longint;',
  21447. ' end;',
  21448. ' TObject = class(IUnknown)',
  21449. ' function _AddRef: longint; virtual; abstract;',
  21450. ' function _Release: longint; virtual; abstract;',
  21451. ' end;',
  21452. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21453. 'var o: TObject;',
  21454. 'begin',
  21455. ' DoIt(v,v,v,v);',
  21456. ' DoIt(o,o,k,k);',
  21457. 'end;',
  21458. 'procedure DoSome;',
  21459. 'var v: IUnknown;',
  21460. 'begin',
  21461. ' DoIt(v,v,v,v);',
  21462. 'end;',
  21463. 'var i: IUnknown;',
  21464. 'begin',
  21465. ' DoIt(i,i,i,i);',
  21466. '']);
  21467. ConvertProgram;
  21468. CheckSource('TestClassInterface_COM_PassAsArg',
  21469. LinesToStr([ // statements
  21470. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21471. 'rtl.createClass(this, "TObject", null, function () {',
  21472. ' this.$init = function () {',
  21473. ' };',
  21474. ' this.$final = function () {',
  21475. ' };',
  21476. ' rtl.addIntf(this, $mod.IUnknown);',
  21477. '});',
  21478. 'this.DoIt = function (v, j, k, l) {',
  21479. ' var o = null;',
  21480. ' var $ir = rtl.createIntfRefs();',
  21481. ' rtl._AddRef(v);',
  21482. ' try {',
  21483. ' $mod.DoIt(v, v, {',
  21484. ' get: function () {',
  21485. ' return v;',
  21486. ' },',
  21487. ' set: function (w) {',
  21488. ' v = rtl.setIntfL(v, w);',
  21489. ' }',
  21490. ' }, {',
  21491. ' get: function () {',
  21492. ' return v;',
  21493. ' },',
  21494. ' set: function (w) {',
  21495. ' v = rtl.setIntfL(v, w);',
  21496. ' }',
  21497. ' });',
  21498. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21499. ' } finally {',
  21500. ' $ir.free();',
  21501. ' rtl._Release(v);',
  21502. ' };',
  21503. '};',
  21504. 'this.DoSome = function () {',
  21505. ' var v = null;',
  21506. ' try {',
  21507. ' $mod.DoIt(v, v, {',
  21508. ' get: function () {',
  21509. ' return v;',
  21510. ' },',
  21511. ' set: function (w) {',
  21512. ' v = rtl.setIntfL(v, w);',
  21513. ' }',
  21514. ' }, {',
  21515. ' get: function () {',
  21516. ' return v;',
  21517. ' },',
  21518. ' set: function (w) {',
  21519. ' v = rtl.setIntfL(v, w);',
  21520. ' }',
  21521. ' });',
  21522. ' } finally {',
  21523. ' rtl._Release(v);',
  21524. ' };',
  21525. '};',
  21526. 'this.i = null;',
  21527. '']),
  21528. LinesToStr([ // $mod.$main
  21529. '$mod.DoIt($mod.i, $mod.i, {',
  21530. ' p: $mod,',
  21531. ' get: function () {',
  21532. ' return this.p.i;',
  21533. ' },',
  21534. ' set: function (v) {',
  21535. ' rtl.setIntfP(this.p, "i", v);',
  21536. ' }',
  21537. '}, {',
  21538. ' p: $mod,',
  21539. ' get: function () {',
  21540. ' return this.p.i;',
  21541. ' },',
  21542. ' set: function (v) {',
  21543. ' rtl.setIntfP(this.p, "i", v);',
  21544. ' }',
  21545. '});',
  21546. '']));
  21547. end;
  21548. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21549. begin
  21550. StartProgram(false);
  21551. Add([
  21552. '{$interfaces com}',
  21553. 'type',
  21554. ' IUnknown = interface',
  21555. ' function _AddRef: longint;',
  21556. ' function _Release: longint;',
  21557. ' end;',
  21558. ' TObject = class(IUnknown)',
  21559. ' function _AddRef: longint; virtual; abstract;',
  21560. ' function _Release: longint; virtual; abstract;',
  21561. ' end;',
  21562. 'procedure DoIt(out i);',
  21563. 'begin end;',
  21564. 'procedure DoSome;',
  21565. 'var v: IUnknown;',
  21566. 'begin',
  21567. ' DoIt(v);',
  21568. 'end;',
  21569. 'function GetIt: IUnknown;',
  21570. 'begin',
  21571. ' DoIt(Result);',
  21572. 'end;',
  21573. 'var i: IUnknown;',
  21574. 'begin',
  21575. ' DoIt(i);',
  21576. '']);
  21577. ConvertProgram;
  21578. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21579. LinesToStr([ // statements
  21580. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21581. 'rtl.createClass(this, "TObject", null, function () {',
  21582. ' this.$init = function () {',
  21583. ' };',
  21584. ' this.$final = function () {',
  21585. ' };',
  21586. ' rtl.addIntf(this, $mod.IUnknown);',
  21587. '});',
  21588. 'this.DoIt = function (i) {',
  21589. '};',
  21590. 'this.DoSome = function () {',
  21591. ' var v = null;',
  21592. ' try {',
  21593. ' $mod.DoIt({',
  21594. ' get: function () {',
  21595. ' return v;',
  21596. ' },',
  21597. ' set: function (w) {',
  21598. ' v = w;',
  21599. ' }',
  21600. ' });',
  21601. ' } finally {',
  21602. ' rtl._Release(v);',
  21603. ' };',
  21604. '};',
  21605. 'this.GetIt = function () {',
  21606. ' var Result = null;',
  21607. ' var $ok = false;',
  21608. ' try {',
  21609. ' $mod.DoIt({',
  21610. ' get: function () {',
  21611. ' return Result;',
  21612. ' },',
  21613. ' set: function (v) {',
  21614. ' Result = v;',
  21615. ' }',
  21616. ' });',
  21617. ' $ok = true;',
  21618. ' } finally {',
  21619. ' if (!$ok) rtl._Release(Result);',
  21620. ' };',
  21621. ' return Result;',
  21622. '};',
  21623. 'this.i = null;',
  21624. '']),
  21625. LinesToStr([ // $mod.$main
  21626. 'try {',
  21627. ' $mod.DoIt({',
  21628. ' p: $mod,',
  21629. ' get: function () {',
  21630. ' return this.p.i;',
  21631. ' },',
  21632. ' set: function (v) {',
  21633. ' this.p.i = v;',
  21634. ' }',
  21635. ' });',
  21636. '} finally {',
  21637. ' rtl._Release($mod.i);',
  21638. '};',
  21639. '']));
  21640. end;
  21641. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21642. begin
  21643. StartProgram(false);
  21644. Add([
  21645. '{$interfaces com}',
  21646. 'type',
  21647. ' IUnknown = interface',
  21648. ' function _AddRef: longint;',
  21649. ' function _Release: longint;',
  21650. ' end;',
  21651. ' TObject = class(IUnknown)',
  21652. ' function _AddRef: longint; virtual; abstract;',
  21653. ' function _Release: longint; virtual; abstract;',
  21654. ' end;',
  21655. 'function GetIt: IUnknown;',
  21656. 'begin',
  21657. 'end;',
  21658. 'procedure DoSome;',
  21659. 'var v: IUnknown;',
  21660. ' i: longint;',
  21661. 'begin',
  21662. ' v:=GetIt;',
  21663. ' v:=GetIt();',
  21664. ' GetIt()._AddRef;',
  21665. ' i:=GetIt()._AddRef;',
  21666. 'end;',
  21667. 'var v: IUnknown;',
  21668. ' i: longint;',
  21669. 'begin',
  21670. ' v:=GetIt;',
  21671. ' v:=GetIt();',
  21672. ' GetIt()._AddRef;',
  21673. ' i:=GetIt()._AddRef;',
  21674. '']);
  21675. ConvertProgram;
  21676. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21677. LinesToStr([ // statements
  21678. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21679. 'rtl.createClass(this, "TObject", null, function () {',
  21680. ' this.$init = function () {',
  21681. ' };',
  21682. ' this.$final = function () {',
  21683. ' };',
  21684. ' rtl.addIntf(this, $mod.IUnknown);',
  21685. '});',
  21686. 'this.GetIt = function () {',
  21687. ' var Result = null;',
  21688. ' return Result;',
  21689. '};',
  21690. 'this.DoSome = function () {',
  21691. ' var v = null;',
  21692. ' var i = 0;',
  21693. ' var $ir = rtl.createIntfRefs();',
  21694. ' try {',
  21695. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21696. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21697. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21698. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21699. ' } finally {',
  21700. ' $ir.free();',
  21701. ' rtl._Release(v);',
  21702. ' };',
  21703. '};',
  21704. 'this.v = null;',
  21705. 'this.i = 0;',
  21706. '']),
  21707. LinesToStr([ // $mod.$main
  21708. 'var $ir = rtl.createIntfRefs();',
  21709. 'try {',
  21710. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21711. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21712. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21713. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21714. '} finally {',
  21715. ' $ir.free();',
  21716. '};',
  21717. '']));
  21718. end;
  21719. procedure TTestModule.TestClassInterface_COM_Property;
  21720. begin
  21721. StartProgram(false);
  21722. Add([
  21723. '{$interfaces com}',
  21724. 'type',
  21725. ' IUnknown = interface',
  21726. ' function _AddRef: longint;',
  21727. ' function _Release: longint;',
  21728. ' end;',
  21729. ' TObject = class(IUnknown)',
  21730. ' FAnt: IUnknown;',
  21731. ' function _AddRef: longint; virtual; abstract;',
  21732. ' function _Release: longint; virtual; abstract;',
  21733. ' function GetBird: IUnknown; virtual; abstract;',
  21734. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21735. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21736. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21737. ' property Ant: IUnknown read FAnt write FAnt;',
  21738. ' property Bird: IUnknown read GetBird write SetBird;',
  21739. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21740. ' end;',
  21741. 'procedure DoIt;',
  21742. 'var',
  21743. ' o: TObject;',
  21744. ' v: IUnknown;',
  21745. 'begin',
  21746. ' v:=o.Ant;',
  21747. ' o.Ant:=v;',
  21748. ' o.Ant:=o.Ant;',
  21749. ' v:=o.Bird;',
  21750. ' o.Bird:=v;',
  21751. ' o.Bird:=o.Bird;',
  21752. ' v:=o.Items[1];',
  21753. ' o.Items[2]:=v;',
  21754. ' o.Items[3]:=o.Items[4];',
  21755. ' v:=o[5];',
  21756. ' o[6]:=v;',
  21757. ' o[7]:=o[8];',
  21758. 'end;',
  21759. 'begin',
  21760. '']);
  21761. ConvertProgram;
  21762. CheckSource('TestClassInterface_COM_Property',
  21763. LinesToStr([ // statements
  21764. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21765. 'rtl.createClass(this, "TObject", null, function () {',
  21766. ' this.$init = function () {',
  21767. ' this.FAnt = null;',
  21768. ' };',
  21769. ' this.$final = function () {',
  21770. ' rtl.setIntfP(this, "FAnt", null);',
  21771. ' };',
  21772. ' rtl.addIntf(this, $mod.IUnknown);',
  21773. '});',
  21774. 'this.DoIt = function () {',
  21775. ' var o = null;',
  21776. ' var v = null;',
  21777. ' var $ir = rtl.createIntfRefs();',
  21778. ' try {',
  21779. ' v = rtl.setIntfL(v, o.FAnt);',
  21780. ' rtl.setIntfP(o, "FAnt", v);',
  21781. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21782. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21783. ' o.SetBird(v);',
  21784. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21785. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21786. ' o.SetItems(2, v);',
  21787. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21788. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21789. ' o.SetItems(6, v);',
  21790. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21791. ' } finally {',
  21792. ' $ir.free();',
  21793. ' rtl._Release(v);',
  21794. ' };',
  21795. '};',
  21796. '']),
  21797. LinesToStr([ // $mod.$main
  21798. '']));
  21799. end;
  21800. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21801. begin
  21802. StartProgram(false);
  21803. Add([
  21804. '{$interfaces com}',
  21805. 'type',
  21806. ' IUnknown = interface',
  21807. ' function _AddRef: longint;',
  21808. ' function _Release: longint;',
  21809. ' function GetBird: IUnknown;',
  21810. ' procedure SetBird(Value: IUnknown);',
  21811. ' function GetItems(Index: longint): IUnknown;',
  21812. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21813. ' property Bird: IUnknown read GetBird write SetBird;',
  21814. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21815. ' end;',
  21816. ' TObject = class(IUnknown)',
  21817. ' function _AddRef: longint; virtual; abstract;',
  21818. ' function _Release: longint; virtual; abstract;',
  21819. ' function GetBird: IUnknown; virtual; abstract;',
  21820. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21821. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21822. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21823. ' end;',
  21824. 'procedure DoIt;',
  21825. 'var',
  21826. ' o: TObject;',
  21827. ' v: IUnknown;',
  21828. 'begin',
  21829. ' v:=v.Items[1];',
  21830. ' v.Items[2]:=v;',
  21831. ' v.Items[3]:=v.Items[4];',
  21832. ' v:=v[5];',
  21833. ' v[6]:=v;',
  21834. ' v[7]:=v[8];',
  21835. ' v[9].Bird.Bird:=v;',
  21836. ' v:=v.Bird[10].Bird',
  21837. 'end;',
  21838. 'begin',
  21839. '']);
  21840. ConvertProgram;
  21841. CheckSource('TestClassInterface_COM_IntfProperty',
  21842. LinesToStr([ // statements
  21843. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21844. ' "_AddRef",',
  21845. ' "_Release",',
  21846. ' "GetBird",',
  21847. ' "SetBird",',
  21848. ' "GetItems",',
  21849. ' "SetItems"',
  21850. '], null);',
  21851. 'rtl.createClass(this, "TObject", null, function () {',
  21852. ' this.$init = function () {',
  21853. ' };',
  21854. ' this.$final = function () {',
  21855. ' };',
  21856. ' rtl.addIntf(this, $mod.IUnknown);',
  21857. '});',
  21858. 'this.DoIt = function () {',
  21859. ' var o = null;',
  21860. ' var v = null;',
  21861. ' var $ir = rtl.createIntfRefs();',
  21862. ' try {',
  21863. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21864. ' v.SetItems(2, v);',
  21865. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21866. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21867. ' v.SetItems(6, v);',
  21868. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21869. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21870. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21871. ' } finally {',
  21872. ' $ir.free();',
  21873. ' rtl._Release(v);',
  21874. ' };',
  21875. '};',
  21876. '']),
  21877. LinesToStr([ // $mod.$main
  21878. '']));
  21879. end;
  21880. procedure TTestModule.TestClassInterface_COM_Delegation;
  21881. begin
  21882. StartProgram(false);
  21883. Add([
  21884. '{$interfaces com}',
  21885. 'type',
  21886. ' IUnknown = interface',
  21887. ' function _AddRef: longint;',
  21888. ' function _Release: longint;',
  21889. ' end;',
  21890. ' IBird = interface(IUnknown)',
  21891. ' procedure Fly(s: string);',
  21892. ' end;',
  21893. ' IEagle = interface(IBird) end;',
  21894. ' IDove = interface(IBird) end;',
  21895. ' ISwallow = interface(IBird) end;',
  21896. ' TObject = class',
  21897. ' end;',
  21898. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21899. ' function _AddRef: longint; virtual; abstract;',
  21900. ' function _Release: longint; virtual; abstract;',
  21901. ' procedure Fly(s: string); virtual; abstract;',
  21902. ' end;',
  21903. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21904. ' function _AddRef: longint; virtual; abstract;',
  21905. ' function _Release: longint; virtual; abstract;',
  21906. ' FBirdIntf: IBird;',
  21907. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21908. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21909. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21910. ' FDoveObj: TBird;',
  21911. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21912. ' function GetSwallowObj: TBird; virtual; abstract;',
  21913. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21914. ' end;',
  21915. 'begin',
  21916. '']);
  21917. ConvertProgram;
  21918. CheckSource('TestClassInterface_COM_Delegation',
  21919. LinesToStr([ // statements
  21920. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21921. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21922. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21923. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21924. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21925. 'rtl.createClass(this, "TObject", null, function () {',
  21926. ' this.$init = function () {',
  21927. ' };',
  21928. ' this.$final = function () {',
  21929. ' };',
  21930. '});',
  21931. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21932. ' rtl.addIntf(this, $mod.IBird);',
  21933. ' rtl.addIntf(this, $mod.IEagle);',
  21934. ' rtl.addIntf(this, $mod.IDove);',
  21935. ' rtl.addIntf(this, $mod.ISwallow);',
  21936. '});',
  21937. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21938. ' this.$init = function () {',
  21939. ' $mod.TObject.$init.call(this);',
  21940. ' this.FBirdIntf = null;',
  21941. ' this.FDoveObj = null;',
  21942. ' };',
  21943. ' this.$final = function () {',
  21944. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21945. ' this.FDoveObj = undefined;',
  21946. ' $mod.TObject.$final.call(this);',
  21947. ' };',
  21948. ' this.$intfmaps = {',
  21949. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21950. ' return rtl._AddRef(this.FBirdIntf);',
  21951. ' },',
  21952. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21953. ' return this.GetEagleIntf();',
  21954. ' },',
  21955. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21956. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21957. ' },',
  21958. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21959. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21960. ' }',
  21961. ' };',
  21962. '});',
  21963. '']),
  21964. LinesToStr([ // $mod.$main
  21965. '']));
  21966. end;
  21967. procedure TTestModule.TestClassInterface_COM_With;
  21968. begin
  21969. StartProgram(false);
  21970. Add([
  21971. '{$interfaces com}',
  21972. 'type',
  21973. ' IUnknown = interface',
  21974. ' function _AddRef: longint;',
  21975. ' function _Release: longint;',
  21976. ' function GetAnt: IUnknown;',
  21977. ' property Ant: IUnknown read GetAnt;',
  21978. ' end;',
  21979. ' TObject = class(IUnknown)',
  21980. ' function _AddRef: longint; virtual; abstract;',
  21981. ' function _Release: longint; virtual; abstract;',
  21982. ' function GetAnt: IUnknown; virtual; abstract;',
  21983. ' property Ant: IUnknown read GetAnt;',
  21984. ' end;',
  21985. 'procedure DoIt;',
  21986. 'var',
  21987. ' i: IUnknown;',
  21988. 'begin',
  21989. ' with i do ',
  21990. ' GetAnt;',
  21991. ' with i.Ant, Ant do ',
  21992. ' GetAnt;',
  21993. 'end;',
  21994. 'begin',
  21995. '']);
  21996. ConvertProgram;
  21997. CheckSource('TestClassInterface_COM_With',
  21998. LinesToStr([ // statements
  21999. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22000. 'rtl.createClass(this, "TObject", null, function () {',
  22001. ' this.$init = function () {',
  22002. ' };',
  22003. ' this.$final = function () {',
  22004. ' };',
  22005. ' rtl.addIntf(this, $mod.IUnknown);',
  22006. '});',
  22007. 'this.DoIt = function () {',
  22008. ' var i = null;',
  22009. ' var $ir = rtl.createIntfRefs();',
  22010. ' try {',
  22011. ' $ir.ref(1, i.GetAnt());',
  22012. ' var $with = $ir.ref(2, i.GetAnt());',
  22013. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22014. ' $ir.ref(4, $with1.GetAnt());',
  22015. ' } finally {',
  22016. ' $ir.free();',
  22017. ' };',
  22018. '};',
  22019. '']),
  22020. LinesToStr([ // $mod.$main
  22021. '']));
  22022. end;
  22023. procedure TTestModule.TestClassInterface_COM_ForIn;
  22024. begin
  22025. StartProgram(false);
  22026. Add([
  22027. '{$interfaces com}',
  22028. 'type',
  22029. ' IUnknown = interface end;',
  22030. ' TObject = class',
  22031. ' Id: longint;',
  22032. ' end;',
  22033. ' IEnumerator = interface(IUnknown)',
  22034. ' function GetCurrent: TObject;',
  22035. ' function MoveNext: Boolean;',
  22036. ' property Current: TObject read GetCurrent;',
  22037. ' end;',
  22038. ' IEnumerable = interface(IUnknown)',
  22039. ' function GetEnumerator: IEnumerator;',
  22040. ' end;',
  22041. 'var',
  22042. ' o: TObject;',
  22043. ' i: IEnumerable;',
  22044. 'begin',
  22045. ' for o in i do o.Id:=3;',
  22046. '']);
  22047. ConvertProgram;
  22048. CheckSource('TestClassInterface_COM_ForIn',
  22049. LinesToStr([ // statements
  22050. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22051. 'rtl.createClass(this, "TObject", null, function () {',
  22052. ' this.$init = function () {',
  22053. ' this.Id = 0;',
  22054. ' };',
  22055. ' this.$final = function () {',
  22056. ' };',
  22057. '});',
  22058. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22059. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22060. 'this.o = null;',
  22061. 'this.i = null;',
  22062. '']),
  22063. LinesToStr([ // $mod.$main
  22064. 'var $in = $mod.i.GetEnumerator();',
  22065. 'try {',
  22066. ' while ($in.MoveNext()) {',
  22067. ' $mod.o = $in.GetCurrent();',
  22068. ' $mod.o.Id = 3;',
  22069. ' }',
  22070. '} finally {',
  22071. ' rtl._Release($in)',
  22072. '};',
  22073. '']));
  22074. end;
  22075. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22076. begin
  22077. {$IFNDEF EnableCOMArrayOfIntf}
  22078. exit;
  22079. {$ENDIF}
  22080. StartProgram(false);
  22081. Add([
  22082. '{$interfaces com}',
  22083. 'type',
  22084. ' IUnknown = interface end;',
  22085. ' IBird = interface(IUnknown)',
  22086. ' function Fly(w: word): word;',
  22087. ' end;',
  22088. ' TBirdArray = array of IBird;',
  22089. 'procedure Run;',
  22090. 'var',
  22091. ' i: IBird;',
  22092. ' a,b: TBirdArray;',
  22093. 'begin',
  22094. //' SetLength(a,3);',
  22095. ' a:=b;',
  22096. ' i:=a[1];',
  22097. ' a[2]:=i;',
  22098. //' for i in a do i.fly(3);',
  22099. // a:=copy(b,1,2);
  22100. // a:=concat(b,a);
  22101. // insert(i,b,1);
  22102. // a:=[i,i];
  22103. 'end;',
  22104. // ToDo: pass TBirdArray as arg
  22105. 'begin',
  22106. '']);
  22107. ConvertProgram;
  22108. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22109. LinesToStr([ // statements
  22110. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22111. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22112. 'this.Run = function () {',
  22113. ' var i = null;',
  22114. ' var a = [];',
  22115. ' var b = [];',
  22116. ' try {',
  22117. ' a = rtl.arrayRef(b);',
  22118. ' i = rtl.setIntfL(i, a[1]);',
  22119. ' rtl.setIntfP(a, 2, i);',
  22120. ' } finally {',
  22121. ' rtl._Release(i);',
  22122. ' rtl._ReleaseArray(a,1);',
  22123. ' };',
  22124. '};',
  22125. '']),
  22126. LinesToStr([ // $mod.$main
  22127. '']));
  22128. end;
  22129. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22130. begin
  22131. StartProgram(false);
  22132. Add([
  22133. '{$interfaces com}',
  22134. 'type',
  22135. ' IUnknown = interface',
  22136. ' function _AddRef: longint;',
  22137. ' function _Release: longint;',
  22138. ' end;',
  22139. ' TObject = class',
  22140. ' end;',
  22141. ' TArrOfIntf = array of IUnknown;',
  22142. 'begin',
  22143. '']);
  22144. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22145. ConvertProgram;
  22146. end;
  22147. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22148. begin
  22149. StartProgram(false);
  22150. Add([
  22151. '{$interfaces com}',
  22152. 'type',
  22153. ' IUnknown = interface',
  22154. ' function _AddRef: longint;',
  22155. ' function _Release: longint;',
  22156. ' end;',
  22157. ' TRec = record',
  22158. ' i: IUnknown;',
  22159. ' end;',
  22160. 'begin',
  22161. '']);
  22162. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22163. ConvertProgram;
  22164. end;
  22165. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22166. begin
  22167. StartUnit(false);
  22168. Add([
  22169. '{$interfaces com}',
  22170. 'interface',
  22171. 'implementation',
  22172. 'type',
  22173. ' IUnknown = interface',
  22174. ' function _AddRef: longint;',
  22175. ' end;',
  22176. ' TObject = class(IUnknown)',
  22177. ' function _AddRef: longint;',
  22178. ' end;',
  22179. 'function TObject._AddRef: longint; begin end;',
  22180. 'var i: IUnknown;',
  22181. ' o: TObject;',
  22182. 'initialization',
  22183. ' i:=nil;',
  22184. ' i:=i;',
  22185. ' i:=o;',
  22186. ' if (o as IUnknown)=nil then ;',
  22187. '']);
  22188. ConvertUnit;
  22189. CheckSource('TestClassInterface_COM_UnitInitialization',
  22190. LinesToStr([ // statements
  22191. 'var $impl = $mod.$impl;',
  22192. '']),
  22193. LinesToStr([ // this.$init
  22194. 'var $ir = rtl.createIntfRefs();',
  22195. 'try {',
  22196. ' rtl.setIntfP($impl, "i", null);',
  22197. ' rtl.setIntfP($impl, "i", $impl.i);',
  22198. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22199. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22200. '} finally {',
  22201. ' $ir.free();',
  22202. '};',
  22203. '']),
  22204. LinesToStr([ // implementation
  22205. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22206. 'rtl.createClass($impl, "TObject", null, function () {',
  22207. ' this.$init = function () {',
  22208. ' };',
  22209. ' this.$final = function () {',
  22210. ' };',
  22211. ' this._AddRef = function () {',
  22212. ' var Result = 0;',
  22213. ' return Result;',
  22214. ' };',
  22215. ' rtl.addIntf(this, $impl.IUnknown);',
  22216. '});',
  22217. '$impl.i = null;',
  22218. '$impl.o = null;',
  22219. ''])
  22220. );
  22221. end;
  22222. procedure TTestModule.TestClassInterface_Corba_GUID;
  22223. begin
  22224. StartProgram(false);
  22225. Add([
  22226. '{$interfaces corba}',
  22227. 'type',
  22228. ' IUnknown = interface',
  22229. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22230. ' end;',
  22231. ' TObject = class end;',
  22232. ' TGUID = record D1, D2, D3, D4: word; end;',
  22233. ' TAliasGUID = TGUID;',
  22234. ' TGUIDString = type string;',
  22235. ' TAliasGUIDString = TGUIDString;',
  22236. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22237. 'begin end;',
  22238. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22239. 'begin end;',
  22240. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22241. 'begin end;',
  22242. 'var',
  22243. ' i: IUnknown;',
  22244. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22245. ' s: TAliasGUIDString;',
  22246. 'begin',
  22247. ' DoConstGUIDIt(IUnknown);',
  22248. ' DoDefGUID(IUnknown);',
  22249. ' DoStr(IUnknown);',
  22250. ' DoConstGUIDIt(i);',
  22251. ' DoDefGUID(i);',
  22252. ' DoStr(i);',
  22253. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22254. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22255. ' DoStr(g);',
  22256. ' g:=i;',
  22257. ' g:=IUnknown;',
  22258. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22259. ' s:=i;',
  22260. ' s:=IUnknown;',
  22261. ' s:=g;',
  22262. ' if g=i then ;',
  22263. ' if i=g then ;',
  22264. ' if g=IUnknown then ;',
  22265. ' if IUnknown=g then ;',
  22266. ' if s=i then ;',
  22267. ' if i=s then ;',
  22268. ' if s=IUnknown then ;',
  22269. ' if IUnknown=s then ;',
  22270. ' if s=g then ;',
  22271. ' if g=s then ;',
  22272. '']);
  22273. ConvertProgram;
  22274. CheckSource('TestClassInterface_Corba_GUID',
  22275. LinesToStr([ // statements
  22276. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22277. 'rtl.createClass(this, "TObject", null, function () {',
  22278. ' this.$init = function () {',
  22279. ' };',
  22280. ' this.$final = function () {',
  22281. ' };',
  22282. '});',
  22283. 'rtl.recNewT(this, "TGUID", function () {',
  22284. ' this.D1 = 0;',
  22285. ' this.D2 = 0;',
  22286. ' this.D3 = 0;',
  22287. ' this.D4 = 0;',
  22288. ' this.$eq = function (b) {',
  22289. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22290. ' };',
  22291. ' this.$assign = function (s) {',
  22292. ' this.D1 = s.D1;',
  22293. ' this.D2 = s.D2;',
  22294. ' this.D3 = s.D3;',
  22295. ' this.D4 = s.D4;',
  22296. ' return this;',
  22297. ' };',
  22298. '});',
  22299. 'this.DoConstGUIDIt = function (g) {',
  22300. '};',
  22301. 'this.DoDefGUID = function (g) {',
  22302. '};',
  22303. 'this.DoStr = function (s) {',
  22304. '};',
  22305. 'this.i = null;',
  22306. 'this.g = this.TGUID.$clone({',
  22307. ' D1: 0xD91C9AF4,',
  22308. ' D2: 0x3C93,',
  22309. ' D3: 0x420F,',
  22310. ' D4: [',
  22311. ' 0xA3,',
  22312. ' 0x03,',
  22313. ' 0xBF,',
  22314. ' 0x5B,',
  22315. ' 0xA8,',
  22316. ' 0x2B,',
  22317. ' 0xFD,',
  22318. ' 0x23',
  22319. ' ]',
  22320. '});',
  22321. 'this.s = "";',
  22322. '']),
  22323. LinesToStr([ // $mod.$main
  22324. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22325. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22326. '$mod.DoStr($mod.IUnknown.$guid);',
  22327. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22328. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22329. '$mod.DoStr($mod.i.$guid);',
  22330. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22331. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22332. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22333. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22334. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22335. '$mod.g.$assign({',
  22336. ' D1: 0xD91C9AF4,',
  22337. ' D2: 0x3C93,',
  22338. ' D3: 0x420F,',
  22339. ' D4: [',
  22340. ' 0xA3,',
  22341. ' 0x03,',
  22342. ' 0xBF,',
  22343. ' 0x5B,',
  22344. ' 0xA8,',
  22345. ' 0x2B,',
  22346. ' 0xFD,',
  22347. ' 0x23',
  22348. ' ]',
  22349. '});',
  22350. '$mod.s = $mod.i.$guid;',
  22351. '$mod.s = $mod.IUnknown.$guid;',
  22352. '$mod.s = rtl.guidrToStr($mod.g);',
  22353. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22354. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22355. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22356. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22357. 'if ($mod.s === $mod.i.$guid) ;',
  22358. 'if ($mod.i.$guid === $mod.s) ;',
  22359. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22360. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22361. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22362. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22363. '']));
  22364. end;
  22365. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22366. begin
  22367. StartProgram(false);
  22368. Add([
  22369. '{$interfaces corba}',
  22370. 'type',
  22371. ' IUnknown = interface',
  22372. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22373. ' end;',
  22374. ' TGUID = record D1, D2, D3, D4: word; end;',
  22375. ' TAliasGUID = TGUID;',
  22376. ' TGUIDString = type string;',
  22377. ' TAliasGUIDString = TGUIDString;',
  22378. ' TObject = class',
  22379. ' function GetG: TAliasGUID; virtual; abstract;',
  22380. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22381. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22382. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22383. ' property g: TAliasGUID read GetG write SetG;',
  22384. ' property s: TAliasGUIDString read GetS write SetS;',
  22385. ' end;',
  22386. 'var o: TObject;',
  22387. 'begin',
  22388. ' o.g:=IUnknown;',
  22389. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22390. ' o.s:=IUnknown;',
  22391. ' o.s:=o.g;',
  22392. '']);
  22393. ConvertProgram;
  22394. CheckSource('TestClassInterface_GUIDProperty',
  22395. LinesToStr([ // statements
  22396. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22397. 'rtl.recNewT(this, "TGUID", function () {',
  22398. ' this.D1 = 0;',
  22399. ' this.D2 = 0;',
  22400. ' this.D3 = 0;',
  22401. ' this.D4 = 0;',
  22402. ' this.$eq = function (b) {',
  22403. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22404. ' };',
  22405. ' this.$assign = function (s) {',
  22406. ' this.D1 = s.D1;',
  22407. ' this.D2 = s.D2;',
  22408. ' this.D3 = s.D3;',
  22409. ' this.D4 = s.D4;',
  22410. ' return this;',
  22411. ' };',
  22412. '});',
  22413. 'rtl.createClass(this, "TObject", null, function () {',
  22414. ' this.$init = function () {',
  22415. ' };',
  22416. ' this.$final = function () {',
  22417. ' };',
  22418. '});',
  22419. 'this.o = null;',
  22420. '']),
  22421. LinesToStr([ // $mod.$main
  22422. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22423. '$mod.o.SetG({',
  22424. ' D1: 0xD91C9AF4,',
  22425. ' D2: 0x3C93,',
  22426. ' D3: 0x420F,',
  22427. ' D4: [',
  22428. ' 0xA3,',
  22429. ' 0x03,',
  22430. ' 0xBF,',
  22431. ' 0x5B,',
  22432. ' 0xA8,',
  22433. ' 0x2B,',
  22434. ' 0xFD,',
  22435. ' 0x23',
  22436. ' ]',
  22437. '});',
  22438. '$mod.o.SetS($mod.IUnknown.$guid);',
  22439. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22440. '']));
  22441. end;
  22442. procedure TTestModule.TestClassHelper_ClassVar;
  22443. begin
  22444. StartProgram(false);
  22445. Add([
  22446. 'type',
  22447. ' TObject = class',
  22448. ' end;',
  22449. ' THelper = class helper for TObject',
  22450. ' const',
  22451. ' One = 1;',
  22452. ' Two: word = 2;',
  22453. ' class var',
  22454. ' Glob: word;',
  22455. ' function Foo(w: word): word;',
  22456. ' class function Bar(w: word): word;',
  22457. ' end;',
  22458. 'function THelper.foo(w: word): word;',
  22459. 'begin',
  22460. ' Result:=w;',
  22461. ' Two:=One+w;',
  22462. ' Glob:=Glob;',
  22463. ' Result:=Self.Glob;',
  22464. ' Self.Glob:=Self.Glob;',
  22465. ' with Self do Glob:=Glob;',
  22466. 'end;',
  22467. 'class function THelper.bar(w: word): word;',
  22468. 'begin',
  22469. ' Result:=w;',
  22470. ' Two:=One;',
  22471. ' Glob:=Glob;',
  22472. ' Self.Glob:=Self.Glob;',
  22473. ' with Self do Glob:=Glob;',
  22474. 'end;',
  22475. 'var o: TObject;',
  22476. 'begin',
  22477. ' tobject.two:=tobject.one;',
  22478. ' tobject.Glob:=tobject.Glob;',
  22479. ' with tobject do begin',
  22480. ' two:=one;',
  22481. ' Glob:=Glob;',
  22482. ' end;',
  22483. ' o.two:=o.one;',
  22484. ' o.Glob:=o.Glob;',
  22485. ' with o do begin',
  22486. ' two:=one;',
  22487. ' Glob:=Glob;',
  22488. ' end;',
  22489. '']);
  22490. ConvertProgram;
  22491. CheckSource('TestClassHelper_ClassVar',
  22492. LinesToStr([ // statements
  22493. 'rtl.createClass(this, "TObject", null, function () {',
  22494. ' this.$init = function () {',
  22495. ' };',
  22496. ' this.$final = function () {',
  22497. ' };',
  22498. '});',
  22499. 'rtl.createHelper(this, "THelper", null, function () {',
  22500. ' this.One = 1;',
  22501. ' this.Two = 2;',
  22502. ' this.Glob = 0;',
  22503. ' this.Foo = function (w) {',
  22504. ' var Result = 0;',
  22505. ' Result = w;',
  22506. ' $mod.THelper.Two = 1 + w;',
  22507. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22508. ' Result = $mod.THelper.Glob;',
  22509. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22510. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22511. ' return Result;',
  22512. ' };',
  22513. ' this.Bar = function (w) {',
  22514. ' var Result = 0;',
  22515. ' Result = w;',
  22516. ' $mod.THelper.Two = 1;',
  22517. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22518. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22519. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22520. ' return Result;',
  22521. ' };',
  22522. '});',
  22523. 'this.o = null;',
  22524. '']),
  22525. LinesToStr([ // $mod.$main
  22526. '$mod.THelper.Two = 1;',
  22527. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22528. 'var $with = $mod.TObject;',
  22529. '$mod.THelper.Two = 1;',
  22530. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22531. '$mod.THelper.Two = 1;',
  22532. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22533. 'var $with1 = $mod.o;',
  22534. '$mod.THelper.Two = 1;',
  22535. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22536. '']));
  22537. end;
  22538. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22539. begin
  22540. StartProgram(false);
  22541. Add([
  22542. 'type',
  22543. ' TObject = class',
  22544. ' FSize: word;',
  22545. ' property Size: word read FSize write FSize;',
  22546. ' end;',
  22547. ' THelper = class helper for TObject',
  22548. ' function Foo(w: word = 1): word;',
  22549. ' end;',
  22550. 'function THelper.foo(w: word): word;',
  22551. 'begin',
  22552. ' Result:=Size;',
  22553. ' Size:=Size+2;',
  22554. ' Self.Size:=Self.Size+3;',
  22555. ' FSize:=FSize+4;',
  22556. ' Self.FSize:=Self.FSize+5;',
  22557. ' with Self do begin',
  22558. ' Size:=Size+6;',
  22559. ' FSize:=FSize+7;',
  22560. ' FSize:=FSize+8;',
  22561. ' end;',
  22562. 'end;',
  22563. 'begin',
  22564. '']);
  22565. ConvertProgram;
  22566. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22567. LinesToStr([ // statements
  22568. 'rtl.createClass(this, "TObject", null, function () {',
  22569. ' this.$init = function () {',
  22570. ' this.FSize = 0;',
  22571. ' };',
  22572. ' this.$final = function () {',
  22573. ' };',
  22574. '});',
  22575. 'rtl.createHelper(this, "THelper", null, function () {',
  22576. ' this.Foo = function (w) {',
  22577. ' var Result = 0;',
  22578. ' Result = this.FSize;',
  22579. ' this.FSize = this.FSize + 2;',
  22580. ' this.FSize = this.FSize + 3;',
  22581. ' this.FSize = this.FSize + 4;',
  22582. ' this.FSize = this.FSize + 5;',
  22583. ' this.FSize = this.FSize + 6;',
  22584. ' this.FSize = this.FSize + 7;',
  22585. ' this.FSize = this.FSize + 8;',
  22586. ' return Result;',
  22587. ' };',
  22588. '});',
  22589. '']),
  22590. LinesToStr([ // $mod.$main
  22591. '']));
  22592. end;
  22593. procedure TTestModule.TestClassHelper_Method_Call;
  22594. begin
  22595. StartProgram(false);
  22596. Add([
  22597. 'type',
  22598. ' TObject = class',
  22599. ' procedure Run(w: word = 10);',
  22600. ' end;',
  22601. ' THelper = class helper for TObject',
  22602. ' function Foo(w: word = 1): word;',
  22603. ' end;',
  22604. 'procedure TObject.Run(w: word);',
  22605. 'var o: TObject;',
  22606. 'begin',
  22607. ' Foo;',
  22608. ' Foo();',
  22609. ' Foo(2);',
  22610. ' Self.Foo;',
  22611. ' Self.Foo();',
  22612. ' Self.Foo(3);',
  22613. ' with Self do begin',
  22614. ' Foo;',
  22615. ' Foo();',
  22616. ' Foo(4);',
  22617. ' end;',
  22618. ' with o do Foo(5);',
  22619. 'end;',
  22620. 'function THelper.foo(w: word): word;',
  22621. 'begin',
  22622. ' Run;',
  22623. ' Run();',
  22624. ' Run(11);',
  22625. ' Foo;',
  22626. ' Foo();',
  22627. ' Foo(12);',
  22628. ' Self.Foo;',
  22629. ' Self.Foo();',
  22630. ' Self.Foo(13);',
  22631. ' with Self do begin',
  22632. ' Foo;',
  22633. ' Foo();',
  22634. ' Foo(14);',
  22635. ' end;',
  22636. 'end;',
  22637. 'var Obj: TObject;',
  22638. 'begin',
  22639. ' obj.Foo;',
  22640. ' obj.Foo();',
  22641. ' obj.Foo(21);',
  22642. ' with obj do begin',
  22643. ' Foo;',
  22644. ' Foo();',
  22645. ' Foo(22);',
  22646. ' end;',
  22647. '']);
  22648. ConvertProgram;
  22649. CheckSource('TestClassHelper_Method_Call',
  22650. LinesToStr([ // statements
  22651. 'rtl.createClass(this, "TObject", null, function () {',
  22652. ' this.$init = function () {',
  22653. ' };',
  22654. ' this.$final = function () {',
  22655. ' };',
  22656. ' this.Run = function (w) {',
  22657. ' var o = null;',
  22658. ' $mod.THelper.Foo.call(this, 1);',
  22659. ' $mod.THelper.Foo.call(this, 1);',
  22660. ' $mod.THelper.Foo.call(this, 2);',
  22661. ' $mod.THelper.Foo.call(this, 1);',
  22662. ' $mod.THelper.Foo.call(this, 1);',
  22663. ' $mod.THelper.Foo.call(this, 3);',
  22664. ' $mod.THelper.Foo.call(this, 1);',
  22665. ' $mod.THelper.Foo.call(this, 1);',
  22666. ' $mod.THelper.Foo.call(this, 4);',
  22667. ' $mod.THelper.Foo.call(o, 5);',
  22668. ' };',
  22669. '});',
  22670. 'rtl.createHelper(this, "THelper", null, function () {',
  22671. ' this.Foo = function (w) {',
  22672. ' var Result = 0;',
  22673. ' this.Run(10);',
  22674. ' this.Run(10);',
  22675. ' this.Run(11);',
  22676. ' $mod.THelper.Foo.call(this, 1);',
  22677. ' $mod.THelper.Foo.call(this, 1);',
  22678. ' $mod.THelper.Foo.call(this, 12);',
  22679. ' $mod.THelper.Foo.call(this, 1);',
  22680. ' $mod.THelper.Foo.call(this, 1);',
  22681. ' $mod.THelper.Foo.call(this, 13);',
  22682. ' $mod.THelper.Foo.call(this, 1);',
  22683. ' $mod.THelper.Foo.call(this, 1);',
  22684. ' $mod.THelper.Foo.call(this, 14);',
  22685. ' return Result;',
  22686. ' };',
  22687. '});',
  22688. 'this.Obj = null;',
  22689. '']),
  22690. LinesToStr([ // $mod.$main
  22691. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22692. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22693. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22694. 'var $with = $mod.Obj;',
  22695. '$mod.THelper.Foo.call($with, 1);',
  22696. '$mod.THelper.Foo.call($with, 1);',
  22697. '$mod.THelper.Foo.call($with, 22);',
  22698. '']));
  22699. end;
  22700. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22701. begin
  22702. StartProgram(false);
  22703. Add([
  22704. 'type',
  22705. ' TObject = class',
  22706. ' procedure Run(w: word = 10);',
  22707. ' end;',
  22708. ' THelper = class helper for TObject',
  22709. ' function Foo(w: word = 1): word;',
  22710. ' end;',
  22711. 'procedure TObject.Run(w: word);',
  22712. ' procedure Sub(Self: TObject);',
  22713. ' begin',
  22714. ' Foo;',
  22715. ' Foo();',
  22716. ' Self.Foo;',
  22717. ' Self.Foo();',
  22718. ' with Self do begin',
  22719. ' Foo;',
  22720. ' Foo();',
  22721. ' end;',
  22722. ' end;',
  22723. 'begin',
  22724. 'end;',
  22725. 'function THelper.foo(w: word): word;',
  22726. ' procedure Sub(Self: TObject);',
  22727. ' begin',
  22728. ' Run;',
  22729. ' Run();',
  22730. ' Foo;',
  22731. ' Foo();',
  22732. ' Self.Foo;',
  22733. ' Self.Foo();',
  22734. ' with Self do begin',
  22735. ' Foo;',
  22736. ' Foo();',
  22737. ' end;',
  22738. ' end;',
  22739. 'begin',
  22740. 'end;',
  22741. 'begin',
  22742. '']);
  22743. ConvertProgram;
  22744. CheckSource('TestClassHelper_Method_Nested_Call',
  22745. LinesToStr([ // statements
  22746. 'rtl.createClass(this, "TObject", null, function () {',
  22747. ' this.$init = function () {',
  22748. ' };',
  22749. ' this.$final = function () {',
  22750. ' };',
  22751. ' this.Run = function (w) {',
  22752. ' var $Self = this;',
  22753. ' function Sub(Self) {',
  22754. ' $mod.THelper.Foo.call($Self, 1);',
  22755. ' $mod.THelper.Foo.call($Self, 1);',
  22756. ' $mod.THelper.Foo.call(Self, 1);',
  22757. ' $mod.THelper.Foo.call(Self, 1);',
  22758. ' $mod.THelper.Foo.call(Self, 1);',
  22759. ' $mod.THelper.Foo.call(Self, 1);',
  22760. ' };',
  22761. ' };',
  22762. '});',
  22763. 'rtl.createHelper(this, "THelper", null, function () {',
  22764. ' this.Foo = function (w) {',
  22765. ' var $Self = this;',
  22766. ' var Result = 0;',
  22767. ' function Sub(Self) {',
  22768. ' $Self.Run(10);',
  22769. ' $Self.Run(10);',
  22770. ' $mod.THelper.Foo.call($Self, 1);',
  22771. ' $mod.THelper.Foo.call($Self, 1);',
  22772. ' $mod.THelper.Foo.call(Self, 1);',
  22773. ' $mod.THelper.Foo.call(Self, 1);',
  22774. ' $mod.THelper.Foo.call(Self, 1);',
  22775. ' $mod.THelper.Foo.call(Self, 1);',
  22776. ' };',
  22777. ' return Result;',
  22778. ' };',
  22779. '});',
  22780. '']),
  22781. LinesToStr([ // $mod.$main
  22782. '']));
  22783. end;
  22784. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22785. begin
  22786. StartProgram(false);
  22787. Add([
  22788. 'type',
  22789. ' TObject = class',
  22790. ' class procedure Run(w: word = 10);',
  22791. ' end;',
  22792. ' THelper = class helper for TObject',
  22793. ' class function Foo(w: word = 1): word;',
  22794. ' end;',
  22795. 'class procedure TObject.Run(w: word);',
  22796. 'begin',
  22797. ' Foo;',
  22798. ' Foo();',
  22799. ' Self.Foo;',
  22800. ' Self.Foo();',
  22801. ' with Self do begin',
  22802. ' Foo;',
  22803. ' Foo();',
  22804. ' end;',
  22805. 'end;',
  22806. 'class function THelper.foo(w: word): word;',
  22807. 'begin',
  22808. ' Run;',
  22809. ' Run();',
  22810. ' Foo;',
  22811. ' Foo();',
  22812. ' Self.Foo;',
  22813. ' Self.Foo();',
  22814. ' with Self do begin',
  22815. ' Foo;',
  22816. ' Foo();',
  22817. ' end;',
  22818. 'end;',
  22819. 'var',
  22820. ' Obj: TObject;',
  22821. 'begin',
  22822. ' obj.Foo;',
  22823. ' obj.Foo();',
  22824. ' with obj do begin',
  22825. ' Foo;',
  22826. ' Foo();',
  22827. ' end;',
  22828. ' tobject.Foo;',
  22829. ' tobject.Foo();',
  22830. ' with tobject do begin',
  22831. ' Foo;',
  22832. ' Foo();',
  22833. ' end;',
  22834. '']);
  22835. ConvertProgram;
  22836. CheckSource('TestClassHelper_ClassMethod_Call',
  22837. LinesToStr([ // statements
  22838. 'rtl.createClass(this, "TObject", null, function () {',
  22839. ' this.$init = function () {',
  22840. ' };',
  22841. ' this.$final = function () {',
  22842. ' };',
  22843. ' this.Run = function (w) {',
  22844. ' $mod.THelper.Foo.call(this, 1);',
  22845. ' $mod.THelper.Foo.call(this, 1);',
  22846. ' $mod.THelper.Foo.call(this, 1);',
  22847. ' $mod.THelper.Foo.call(this, 1);',
  22848. ' $mod.THelper.Foo.call(this, 1);',
  22849. ' $mod.THelper.Foo.call(this, 1);',
  22850. ' };',
  22851. '});',
  22852. 'rtl.createHelper(this, "THelper", null, function () {',
  22853. ' this.Foo = function (w) {',
  22854. ' var Result = 0;',
  22855. ' this.Run(10);',
  22856. ' this.Run(10);',
  22857. ' $mod.THelper.Foo.call(this, 1);',
  22858. ' $mod.THelper.Foo.call(this, 1);',
  22859. ' $mod.THelper.Foo.call(this, 1);',
  22860. ' $mod.THelper.Foo.call(this, 1);',
  22861. ' $mod.THelper.Foo.call(this, 1);',
  22862. ' $mod.THelper.Foo.call(this, 1);',
  22863. ' return Result;',
  22864. ' };',
  22865. '});',
  22866. 'this.Obj = null;',
  22867. '']),
  22868. LinesToStr([ // $mod.$main
  22869. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22870. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22871. 'var $with = $mod.Obj;',
  22872. '$mod.THelper.Foo.call($with.$class, 1);',
  22873. '$mod.THelper.Foo.call($with.$class, 1);',
  22874. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22875. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22876. 'var $with1 = $mod.TObject;',
  22877. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22878. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22879. '']));
  22880. end;
  22881. procedure TTestModule.TestClassHelper_ClassOf;
  22882. begin
  22883. StartProgram(false);
  22884. Add([
  22885. 'type',
  22886. ' TObject = class',
  22887. ' end;',
  22888. ' TClass = class of TObject;',
  22889. ' THelper = class helper for TObject',
  22890. ' class function Foo(w: word = 1): word;',
  22891. ' end;',
  22892. 'class function THelper.foo(w: word): word;',
  22893. 'begin',
  22894. 'end;',
  22895. 'var',
  22896. ' c: TClass;',
  22897. 'begin',
  22898. ' c.Foo;',
  22899. ' c.Foo();',
  22900. ' with c do begin',
  22901. ' Foo;',
  22902. ' Foo();',
  22903. ' end;',
  22904. '']);
  22905. ConvertProgram;
  22906. CheckSource('TestClassHelper_ClassOf',
  22907. LinesToStr([ // statements
  22908. 'rtl.createClass(this, "TObject", null, function () {',
  22909. ' this.$init = function () {',
  22910. ' };',
  22911. ' this.$final = function () {',
  22912. ' };',
  22913. '});',
  22914. 'rtl.createHelper(this, "THelper", null, function () {',
  22915. ' this.Foo = function (w) {',
  22916. ' var Result = 0;',
  22917. ' return Result;',
  22918. ' };',
  22919. '});',
  22920. 'this.c = null;',
  22921. '']),
  22922. LinesToStr([ // $mod.$main
  22923. '$mod.THelper.Foo.call($mod.c, 1);',
  22924. '$mod.THelper.Foo.call($mod.c, 1);',
  22925. 'var $with = $mod.c;',
  22926. '$mod.THelper.Foo.call($with, 1);',
  22927. '$mod.THelper.Foo.call($with, 1);',
  22928. '']));
  22929. end;
  22930. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22931. begin
  22932. StartProgram(false);
  22933. Add([
  22934. '{$mode objfpc}',
  22935. 'type',
  22936. ' TObject = class',
  22937. ' procedure DoIt;',
  22938. ' end;',
  22939. ' THelper = class helper for TObject',
  22940. ' procedure Fly(w: word = 1);',
  22941. ' class procedure Glide(w: word = 1);',
  22942. ' class procedure Run(w: word = 1); static;',
  22943. ' end;',
  22944. ' TFly = procedure(w: word) of object;',
  22945. ' TGlide = TFly;',
  22946. ' TRun = procedure(w: word);',
  22947. 'var',
  22948. ' f: TFly;',
  22949. ' g: TGlide;',
  22950. ' r: TRun;',
  22951. 'procedure TObject.DoIt;',
  22952. 'begin',
  22953. ' f:=@fly;',
  22954. ' g:=@glide;',
  22955. ' r:=@run;',
  22956. ' f:[email protected];',
  22957. ' g:[email protected];',
  22958. ' r:[email protected];',
  22959. ' with self do begin',
  22960. ' f:=@fly;',
  22961. ' g:=@glide;',
  22962. ' r:=@run;',
  22963. ' end;',
  22964. 'end;',
  22965. 'procedure THelper.fly(w: word);',
  22966. 'begin',
  22967. ' f:=@fly;',
  22968. ' g:=@glide;',
  22969. ' r:=@run;',
  22970. 'end;',
  22971. 'class procedure THelper.glide(w: word);',
  22972. 'begin',
  22973. ' g:=@glide;',
  22974. ' r:=@run;',
  22975. 'end;',
  22976. 'class procedure THelper.run(w: word);',
  22977. 'begin',
  22978. ' g:=@glide;',
  22979. ' r:=@run;',
  22980. 'end;',
  22981. 'var',
  22982. ' Obj: TObject;',
  22983. 'begin',
  22984. ' f:[email protected];',
  22985. ' g:[email protected];',
  22986. ' r:[email protected];',
  22987. ' with obj do begin',
  22988. ' f:=@fly;',
  22989. ' g:=@glide;',
  22990. ' r:=@run;',
  22991. ' end;',
  22992. ' g:[email protected];',
  22993. ' r:[email protected];',
  22994. ' with tobject do begin',
  22995. ' g:=@glide;',
  22996. ' r:=@run;',
  22997. ' end;',
  22998. '']);
  22999. ConvertProgram;
  23000. CheckSource('TestClassHelper_MethodRefObjFPC',
  23001. LinesToStr([ // statements
  23002. 'rtl.createClass(this, "TObject", null, function () {',
  23003. ' this.$init = function () {',
  23004. ' };',
  23005. ' this.$final = function () {',
  23006. ' };',
  23007. ' this.DoIt = function () {',
  23008. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23009. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23010. ' $mod.r = $mod.THelper.Run;',
  23011. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23012. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23013. ' $mod.r = $mod.THelper.Run;',
  23014. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23015. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23016. ' $mod.r = $mod.THelper.Run;',
  23017. ' };',
  23018. '});',
  23019. 'rtl.createHelper(this, "THelper", null, function () {',
  23020. ' this.Fly = function (w) {',
  23021. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23022. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23023. ' $mod.r = $mod.THelper.Run;',
  23024. ' };',
  23025. ' this.Glide = function (w) {',
  23026. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23027. ' $mod.r = $mod.THelper.Run;',
  23028. ' };',
  23029. ' this.Run = function (w) {',
  23030. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23031. ' $mod.r = $mod.THelper.Run;',
  23032. ' };',
  23033. '});',
  23034. 'this.f = null;',
  23035. 'this.g = null;',
  23036. 'this.r = null;',
  23037. 'this.Obj = null;',
  23038. '']),
  23039. LinesToStr([ // $mod.$main
  23040. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23041. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23042. '$mod.r = $mod.THelper.Run;',
  23043. 'var $with = $mod.Obj;',
  23044. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23045. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23046. '$mod.r = $mod.THelper.Run;',
  23047. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23048. '$mod.r = $mod.THelper.Run;',
  23049. 'var $with1 = $mod.TObject;',
  23050. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23051. '$mod.r = $mod.THelper.Run;',
  23052. '']));
  23053. end;
  23054. procedure TTestModule.TestClassHelper_Constructor;
  23055. begin
  23056. StartProgram(false);
  23057. Add([
  23058. 'type',
  23059. ' TObject = class',
  23060. ' constructor Create;',
  23061. ' end;',
  23062. ' TClass = class of TObject;',
  23063. ' THelper = class helper for TObject',
  23064. ' constructor NewHlp(w: word);',
  23065. ' end;',
  23066. 'var',
  23067. ' obj: TObject;',
  23068. ' c: TClass;',
  23069. 'constructor TObject.Create;',
  23070. 'begin',
  23071. ' NewHlp(2);', // normal call
  23072. ' tobject.NewHlp(3);', // new instance
  23073. ' c.newhlp(4);', // new instance
  23074. 'end;',
  23075. 'constructor THelper.NewHlp(w: word);',
  23076. 'begin',
  23077. ' create;', // normal call
  23078. ' tobject.create;', // new instance
  23079. ' NewHlp(2);', // normal call
  23080. ' tobject.NewHlp(3);', // new instance
  23081. ' c.newhlp(4);', // new instance
  23082. 'end;',
  23083. 'begin',
  23084. ' obj.newhlp(2);', // normal call
  23085. ' with Obj do newhlp(12);', // normal call
  23086. ' tobject.newhlp(3);', // new instance
  23087. ' with tobject do newhlp(13);', // new instance
  23088. ' c.newhlp(4);', // new instance
  23089. ' with c do newhlp(14);', // new instance
  23090. '']);
  23091. ConvertProgram;
  23092. CheckSource('TestClassHelper_Constructor',
  23093. LinesToStr([ // statements
  23094. 'rtl.createClass(this, "TObject", null, function () {',
  23095. ' this.$init = function () {',
  23096. ' };',
  23097. ' this.$final = function () {',
  23098. ' };',
  23099. ' this.Create = function () {',
  23100. ' $mod.THelper.NewHlp.call(this, 2);',
  23101. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23102. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23103. ' return this;',
  23104. ' };',
  23105. '});',
  23106. 'rtl.createHelper(this, "THelper", null, function () {',
  23107. ' this.NewHlp = function (w) {',
  23108. ' this.Create();',
  23109. ' $mod.TObject.$create("Create");',
  23110. ' $mod.THelper.NewHlp.call(this, 2);',
  23111. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23112. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23113. ' return this;',
  23114. ' };',
  23115. '});',
  23116. 'this.obj = null;',
  23117. 'this.c = null;',
  23118. '']),
  23119. LinesToStr([ // $mod.$main
  23120. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23121. 'var $with = $mod.obj;',
  23122. '$mod.THelper.NewHlp.call($with, 12);',
  23123. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23124. 'var $with1 = $mod.TObject;',
  23125. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23126. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23127. 'var $with2 = $mod.c;',
  23128. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23129. '']));
  23130. end;
  23131. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23132. begin
  23133. StartProgram(false);
  23134. Add([
  23135. 'type',
  23136. ' TObject = class',
  23137. ' procedure Fly;',
  23138. ' end;',
  23139. ' TObjHelper = class helper for TObject',
  23140. ' procedure Fly;',
  23141. ' end;',
  23142. ' TBird = class',
  23143. ' procedure Fly;',
  23144. ' end;',
  23145. ' TBirdHelper = class helper for TBird',
  23146. ' procedure Fly;',
  23147. ' procedure Walk(w: word);',
  23148. ' end;',
  23149. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23150. ' procedure Fly;',
  23151. ' procedure Walk(w: word);',
  23152. ' end;',
  23153. 'procedure Tobject.fly;',
  23154. 'begin',
  23155. ' inherited;', // ignore
  23156. 'end;',
  23157. 'procedure Tobjhelper.fly;',
  23158. 'begin',
  23159. ' {@TObject_Fly}inherited;',
  23160. ' inherited {@TObject_Fly}Fly;',
  23161. 'end;',
  23162. 'procedure Tbird.fly;',
  23163. 'begin',
  23164. ' {@TObjHelper_Fly}inherited;',
  23165. ' inherited {@TObjHelper_Fly}Fly;',
  23166. 'end;',
  23167. 'procedure Tbirdhelper.fly;',
  23168. 'begin',
  23169. ' {@TBird_Fly}inherited;',
  23170. ' inherited {@TBird_Fly}Fly;',
  23171. 'end;',
  23172. 'procedure Tbirdhelper.walk(w: word);',
  23173. 'begin',
  23174. 'end;',
  23175. 'procedure teagleHelper.fly;',
  23176. 'begin',
  23177. ' {@TBird_Fly}inherited;',
  23178. ' inherited {@TBird_Fly}Fly;',
  23179. 'end;',
  23180. 'procedure teagleHelper.walk(w: word);',
  23181. 'begin',
  23182. ' {@TBirdHelper_Walk}inherited;',
  23183. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23184. 'end;',
  23185. 'begin',
  23186. '']);
  23187. ConvertProgram;
  23188. CheckSource('TestClassHelper_InheritedObjFPC',
  23189. LinesToStr([ // statements
  23190. 'rtl.createClass(this, "TObject", null, function () {',
  23191. ' this.$init = function () {',
  23192. ' };',
  23193. ' this.$final = function () {',
  23194. ' };',
  23195. ' this.Fly = function () {',
  23196. ' };',
  23197. '});',
  23198. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23199. ' this.Fly = function () {',
  23200. ' $mod.TObject.Fly.call(this);',
  23201. ' $mod.TObject.Fly.call(this);',
  23202. ' };',
  23203. '});',
  23204. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23205. ' this.Fly$1 = function () {',
  23206. ' $mod.TObjHelper.Fly.call(this);',
  23207. ' $mod.TObjHelper.Fly.call(this);',
  23208. ' };',
  23209. '});',
  23210. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23211. ' this.Fly = function () {',
  23212. ' $mod.TBird.Fly$1.call(this);',
  23213. ' $mod.TBird.Fly$1.call(this);',
  23214. ' };',
  23215. ' this.Walk = function (w) {',
  23216. ' };',
  23217. '});',
  23218. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23219. ' this.Fly$1 = function () {',
  23220. ' $mod.TBird.Fly$1.call(this);',
  23221. ' $mod.TBird.Fly$1.call(this);',
  23222. ' };',
  23223. ' this.Walk$1 = function (w) {',
  23224. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23225. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23226. ' };',
  23227. '});',
  23228. '']),
  23229. LinesToStr([ // $mod.$main
  23230. '']));
  23231. end;
  23232. procedure TTestModule.TestClassHelper_Property;
  23233. begin
  23234. StartProgram(false);
  23235. Add([
  23236. 'type',
  23237. ' TObject = class',
  23238. ' FSize: word;',
  23239. ' function GetSpeed: word;',
  23240. ' procedure SetSpeed(Value: word);',
  23241. ' end;',
  23242. ' TObjHelper = class helper for TObject',
  23243. ' function GetLeft: word;',
  23244. ' procedure SetLeft(Value: word);',
  23245. ' property Size: word read FSize write FSize;',
  23246. ' property Speed: word read GetSpeed write SetSpeed;',
  23247. ' property Left: word read GetLeft write SetLeft;',
  23248. ' end;',
  23249. ' TBird = class',
  23250. ' property NotRight: word read GetLeft write SetLeft;',
  23251. ' procedure DoIt;',
  23252. ' end;',
  23253. 'var',
  23254. ' b: TBird;',
  23255. 'function Tobject.GetSpeed: word;',
  23256. 'begin',
  23257. ' Size:=Size+11;',
  23258. ' Speed:=Speed+12;',
  23259. ' Result:=Left+13;',
  23260. ' Left:=13;',
  23261. ' Left:=Left+13;',
  23262. ' Self.Size:=Self.Size+21;',
  23263. ' Self.Speed:=Self.Speed+22;',
  23264. ' Self.Left:=Self.Left+23;',
  23265. ' with Self do begin',
  23266. ' Size:=Size+31;',
  23267. ' Speed:=Speed+32;',
  23268. ' Left:=Left+33;',
  23269. ' end;',
  23270. 'end;',
  23271. 'procedure Tobject.SetSpeed(Value: word);',
  23272. 'begin',
  23273. 'end;',
  23274. 'function TObjHelper.GetLeft: word;',
  23275. 'begin',
  23276. ' Size:=Size+11;',
  23277. ' Speed:=Speed+12;',
  23278. ' Left:=Left+13;',
  23279. ' Self.Size:=Self.Size+21;',
  23280. ' Self.Speed:=Self.Speed+22;',
  23281. ' Self.Left:=Self.Left+23;',
  23282. ' with Self do begin',
  23283. ' Size:=Size+31;',
  23284. ' Speed:=Speed+32;',
  23285. ' Left:=Left+33;',
  23286. ' end;',
  23287. 'end;',
  23288. 'procedure TObjHelper.SetLeft(Value: word);',
  23289. 'begin',
  23290. 'end;',
  23291. 'procedure TBird.DoIt;',
  23292. 'begin',
  23293. ' NotRight:=NotRight+11;',
  23294. ' Self.NotRight:=Self.NotRight+21;',
  23295. ' with Self do begin',
  23296. ' NotRight:=NotRight+31;',
  23297. ' end;',
  23298. 'end;',
  23299. 'begin',
  23300. ' b.Size:=b.Size+11;',
  23301. ' b.Speed:=b.Speed+12;',
  23302. ' b.Left:=b.Left+13;',
  23303. ' b.NotRight:=b.NotRight+14;',
  23304. ' with b do begin',
  23305. ' Size:=Size+31;',
  23306. ' Speed:=Speed+32;',
  23307. ' Left:=Left+33;',
  23308. ' NotRight:=NotRight+34;',
  23309. ' end;',
  23310. '']);
  23311. ConvertProgram;
  23312. CheckSource('TestClassHelper_Property',
  23313. LinesToStr([ // statements
  23314. 'rtl.createClass(this, "TObject", null, function () {',
  23315. ' this.$init = function () {',
  23316. ' this.FSize = 0;',
  23317. ' };',
  23318. ' this.$final = function () {',
  23319. ' };',
  23320. ' this.GetSpeed = function () {',
  23321. ' var Result = 0;',
  23322. ' this.FSize = this.FSize + 11;',
  23323. ' this.SetSpeed(this.GetSpeed() + 12);',
  23324. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23325. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23326. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23327. ' this.FSize = this.FSize + 21;',
  23328. ' this.SetSpeed(this.GetSpeed() + 22);',
  23329. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23330. ' this.FSize = this.FSize + 31;',
  23331. ' this.SetSpeed(this.GetSpeed() + 32);',
  23332. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23333. ' return Result;',
  23334. ' };',
  23335. ' this.SetSpeed = function (Value) {',
  23336. ' };',
  23337. '});',
  23338. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23339. ' this.GetLeft = function () {',
  23340. ' var Result = 0;',
  23341. ' this.FSize = this.FSize + 11;',
  23342. ' this.SetSpeed(this.GetSpeed() + 12);',
  23343. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23344. ' this.FSize = this.FSize + 21;',
  23345. ' this.SetSpeed(this.GetSpeed() + 22);',
  23346. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23347. ' this.FSize = this.FSize + 31;',
  23348. ' this.SetSpeed(this.GetSpeed() + 32);',
  23349. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23350. ' return Result;',
  23351. ' };',
  23352. ' this.SetLeft = function (Value) {',
  23353. ' };',
  23354. '});',
  23355. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23356. ' this.DoIt = function () {',
  23357. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23358. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23359. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23360. ' };',
  23361. '});',
  23362. 'this.b = null;',
  23363. '']),
  23364. LinesToStr([ // $mod.$main
  23365. '$mod.b.FSize = $mod.b.FSize + 11;',
  23366. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23367. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23368. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23369. 'var $with = $mod.b;',
  23370. '$with.FSize = $with.FSize + 31;',
  23371. '$with.SetSpeed($with.GetSpeed() + 32);',
  23372. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23373. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23374. '']));
  23375. end;
  23376. procedure TTestModule.TestClassHelper_Property_Array;
  23377. begin
  23378. StartProgram(false);
  23379. Add([
  23380. 'type',
  23381. ' TObject = class',
  23382. ' function GetSpeed(Index: boolean): word;',
  23383. ' procedure SetSpeed(Index: boolean; Value: word);',
  23384. ' end;',
  23385. ' TObjHelper = class helper for TObject',
  23386. ' function GetSize(Index: boolean): word;',
  23387. ' procedure SetSize(Index: boolean; Value: word);',
  23388. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23389. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23390. ' end;',
  23391. ' TBird = class',
  23392. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23393. ' procedure DoIt;',
  23394. ' end;',
  23395. 'var',
  23396. ' b: TBird;',
  23397. 'function Tobject.GetSpeed(Index: boolean): word;',
  23398. 'begin',
  23399. ' Result:=Size[false];',
  23400. ' Size[true]:=Size[false]+11;',
  23401. ' Speed[true]:=Speed[false]+12;',
  23402. ' Self.Size[true]:=Self.Size[false]+21;',
  23403. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23404. ' with Self do begin',
  23405. ' Size[true]:=Size[false]+31;',
  23406. ' Speed[true]:=Speed[false]+32;',
  23407. ' end;',
  23408. 'end;',
  23409. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23410. 'begin',
  23411. 'end;',
  23412. 'function TObjHelper.GetSize(Index: boolean): word;',
  23413. 'begin',
  23414. ' Size[true]:=Size[false]+11;',
  23415. ' Speed[true]:=Speed[false]+12;',
  23416. ' Self.Size[true]:=Self.Size[false]+21;',
  23417. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23418. ' with Self do begin',
  23419. ' Size[true]:=Size[false]+31;',
  23420. ' Speed[true]:=Speed[false]+32;',
  23421. ' end;',
  23422. 'end;',
  23423. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23424. 'begin',
  23425. 'end;',
  23426. 'procedure TBird.DoIt;',
  23427. 'begin',
  23428. ' Items[true]:=Items[false]+11;',
  23429. ' Self.Items[true]:=Self.Items[false]+21;',
  23430. ' with Self do Items[true]:=Items[false]+31;',
  23431. 'end;',
  23432. 'begin',
  23433. ' b.Size[true]:=b.Size[false]+11;',
  23434. ' b.Speed[true]:=b.Speed[false]+12;',
  23435. ' b.Items[true]:=b.Items[false]+13;',
  23436. ' with b do begin',
  23437. ' Size[true]:=Size[false]+21;',
  23438. ' Speed[true]:=Speed[false]+22;',
  23439. ' Items[true]:=Items[false]+23;',
  23440. ' end;',
  23441. '']);
  23442. ConvertProgram;
  23443. CheckSource('TestClassHelper_Property_Array',
  23444. LinesToStr([ // statements
  23445. 'rtl.createClass(this, "TObject", null, function () {',
  23446. ' this.$init = function () {',
  23447. ' };',
  23448. ' this.$final = function () {',
  23449. ' };',
  23450. ' this.GetSpeed = function (Index) {',
  23451. ' var Result = 0;',
  23452. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23453. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23454. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23455. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23456. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23457. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23458. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23459. ' return Result;',
  23460. ' };',
  23461. ' this.SetSpeed = function (Index, Value) {',
  23462. ' };',
  23463. '});',
  23464. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23465. ' this.GetSize = function (Index) {',
  23466. ' var Result = 0;',
  23467. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23468. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23469. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23470. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23471. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23472. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23473. ' return Result;',
  23474. ' };',
  23475. ' this.SetSize = function (Index, Value) {',
  23476. ' };',
  23477. '});',
  23478. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23479. ' this.DoIt = function () {',
  23480. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23481. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23482. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23483. ' };',
  23484. '});',
  23485. 'this.b = null;',
  23486. '']),
  23487. LinesToStr([ // $mod.$main
  23488. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23489. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23490. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23491. 'var $with = $mod.b;',
  23492. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23493. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23494. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23495. '']));
  23496. end;
  23497. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23498. begin
  23499. StartProgram(false);
  23500. Add([
  23501. 'type',
  23502. ' TObject = class',
  23503. ' function GetSpeed(Index: boolean): word;',
  23504. ' procedure SetSpeed(Index: boolean; Value: word);',
  23505. ' end;',
  23506. ' TObjHelper = class helper for TObject',
  23507. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23508. ' end;',
  23509. ' TBird = class',
  23510. ' end;',
  23511. ' TBirdHelper = class helper for TBird',
  23512. ' function GetSize(Index: word): boolean;',
  23513. ' procedure SetSize(Index: word; Value: boolean);',
  23514. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23515. ' end;',
  23516. 'function Tobject.GetSpeed(Index: boolean): word;',
  23517. 'begin',
  23518. ' Self[true]:=Self[false]+1;',
  23519. 'end;',
  23520. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23521. 'begin',
  23522. 'end;',
  23523. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23524. 'begin',
  23525. ' Self[1]:=not Self[2];',
  23526. 'end;',
  23527. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23528. 'begin',
  23529. 'end;',
  23530. 'var',
  23531. ' o: TObject;',
  23532. ' b: TBird;',
  23533. 'begin',
  23534. ' o[true]:=o[false]+1;',
  23535. ' b[3]:=not b[4];',
  23536. '']);
  23537. ConvertProgram;
  23538. CheckSource('TestClassHelper_Property_Array_Default',
  23539. LinesToStr([ // statements
  23540. 'rtl.createClass(this, "TObject", null, function () {',
  23541. ' this.$init = function () {',
  23542. ' };',
  23543. ' this.$final = function () {',
  23544. ' };',
  23545. ' this.GetSpeed = function (Index) {',
  23546. ' var Result = 0;',
  23547. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23548. ' return Result;',
  23549. ' };',
  23550. ' this.SetSpeed = function (Index, Value) {',
  23551. ' };',
  23552. '});',
  23553. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23554. '});',
  23555. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23556. '});',
  23557. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23558. ' this.GetSize = function (Index) {',
  23559. ' var Result = false;',
  23560. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23561. ' return Result;',
  23562. ' };',
  23563. ' this.SetSize = function (Index, Value) {',
  23564. ' };',
  23565. '});',
  23566. 'this.o = null;',
  23567. 'this.b = null;',
  23568. '']),
  23569. LinesToStr([ // $mod.$main
  23570. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23571. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23572. '']));
  23573. end;
  23574. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23575. begin
  23576. StartProgram(false);
  23577. Add([
  23578. 'type',
  23579. ' TObject = class',
  23580. ' end;',
  23581. ' TObjHelper = class helper for TObject',
  23582. ' function GetItems(Index: word): TObject;',
  23583. ' procedure SetItems(Index: word; Value: TObject);',
  23584. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23585. ' end;',
  23586. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23587. 'begin',
  23588. ' Self[1][2]:=Self[3][4];',
  23589. 'end;',
  23590. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23591. 'begin',
  23592. 'end;',
  23593. 'var',
  23594. ' o: TObject;',
  23595. 'begin',
  23596. ' o[1][2]:=o[3][4];',
  23597. '']);
  23598. ConvertProgram;
  23599. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23600. LinesToStr([ // statements
  23601. 'rtl.createClass(this, "TObject", null, function () {',
  23602. ' this.$init = function () {',
  23603. ' };',
  23604. ' this.$final = function () {',
  23605. ' };',
  23606. '});',
  23607. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23608. ' this.GetItems = function (Index) {',
  23609. ' var Result = null;',
  23610. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23611. ' return Result;',
  23612. ' };',
  23613. ' this.SetItems = function (Index, Value) {',
  23614. ' };',
  23615. '});',
  23616. 'this.o = null;',
  23617. '']),
  23618. LinesToStr([ // $mod.$main
  23619. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23620. '']));
  23621. end;
  23622. procedure TTestModule.TestClassHelper_ClassProperty;
  23623. begin
  23624. StartProgram(false);
  23625. Add([
  23626. 'type',
  23627. ' TObject = class',
  23628. ' class var FSize: word;',
  23629. ' class function GetSpeed: word;',
  23630. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23631. ' end;',
  23632. ' TObjHelper = class helper for TObject',
  23633. ' class function GetLeft: word;',
  23634. ' class procedure SetLeft(Value: word);',
  23635. ' class property Size: word read FSize write FSize;',
  23636. ' class property Speed: word read GetSpeed write SetSpeed;',
  23637. ' class property Left: word read GetLeft write SetLeft;',
  23638. ' end;',
  23639. ' TBird = class',
  23640. ' class property NotRight: word read GetLeft write SetLeft;',
  23641. ' class procedure DoIt;',
  23642. ' end;',
  23643. ' TBirdClass = class of TBird;',
  23644. 'class function Tobject.GetSpeed: word;',
  23645. 'begin',
  23646. ' Size:=Size+11;',
  23647. ' Speed:=Speed+12;',
  23648. ' Left:=Left+13;',
  23649. ' Self.Size:=Self.Size+21;',
  23650. ' Self.Speed:=Self.Speed+22;',
  23651. ' Self.Left:=Self.Left+23;',
  23652. ' with Self do begin',
  23653. ' Size:=Size+31;',
  23654. ' Speed:=Speed+32;',
  23655. ' Left:=Left+33;',
  23656. ' end;',
  23657. 'end;',
  23658. 'class function TObjHelper.GetLeft: word;',
  23659. 'begin',
  23660. ' Size:=Size+11;',
  23661. ' Speed:=Speed+12;',
  23662. ' Left:=Left+13;',
  23663. ' Self.Size:=Self.Size+21;',
  23664. ' Self.Speed:=Self.Speed+22;',
  23665. ' Self.Left:=Self.Left+23;',
  23666. ' with Self do begin',
  23667. ' Size:=Size+31;',
  23668. ' Speed:=Speed+32;',
  23669. ' Left:=Left+33;',
  23670. ' end;',
  23671. 'end;',
  23672. 'class procedure TObjHelper.SetLeft(Value: word);',
  23673. 'begin',
  23674. 'end;',
  23675. 'class procedure TBird.DoIt;',
  23676. 'begin',
  23677. ' NotRight:=NotRight+11;',
  23678. ' Self.NotRight:=Self.NotRight+21;',
  23679. ' with Self do NotRight:=NotRight+31;',
  23680. 'end;',
  23681. 'var',
  23682. ' b: TBird;',
  23683. ' c: TBirdClass;',
  23684. 'begin',
  23685. ' b.Size:=b.Size+11;',
  23686. ' b.Speed:=b.Speed+12;',
  23687. ' b.Left:=b.Left+13;',
  23688. ' b.NotRight:=b.NotRight+14;',
  23689. ' with b do begin',
  23690. ' Size:=Size+31;',
  23691. ' Speed:=Speed+32;',
  23692. ' Left:=Left+33;',
  23693. ' NotRight:=NotRight+34;',
  23694. ' end;',
  23695. ' c.Size:=c.Size+11;',
  23696. ' c.Speed:=c.Speed+12;',
  23697. ' c.Left:=c.Left+13;',
  23698. ' c.NotRight:=c.NotRight+14;',
  23699. ' with c do begin',
  23700. ' Size:=Size+31;',
  23701. ' Speed:=Speed+32;',
  23702. ' Left:=Left+33;',
  23703. ' NotRight:=NotRight+34;',
  23704. ' end;',
  23705. ' tbird.Size:=tbird.Size+11;',
  23706. ' tbird.Speed:=tbird.Speed+12;',
  23707. ' tbird.Left:=tbird.Left+13;',
  23708. ' tbird.NotRight:=tbird.NotRight+14;',
  23709. ' with tbird do begin',
  23710. ' Size:=Size+31;',
  23711. ' Speed:=Speed+32;',
  23712. ' Left:=Left+33;',
  23713. ' NotRight:=NotRight+34;',
  23714. ' end;',
  23715. '']);
  23716. ConvertProgram;
  23717. CheckSource('TestClassHelper_ClassProperty',
  23718. LinesToStr([ // statements
  23719. 'rtl.createClass(this, "TObject", null, function () {',
  23720. ' this.FSize = 0;',
  23721. ' this.$init = function () {',
  23722. ' };',
  23723. ' this.$final = function () {',
  23724. ' };',
  23725. ' this.GetSpeed = function () {',
  23726. ' var Result = 0;',
  23727. ' $mod.TObject.FSize = this.FSize + 11;',
  23728. ' this.SetSpeed(this.GetSpeed() + 12);',
  23729. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23730. ' $mod.TObject.FSize = this.FSize + 21;',
  23731. ' this.SetSpeed(this.GetSpeed() + 22);',
  23732. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23733. ' $mod.TObject.FSize = this.FSize + 31;',
  23734. ' this.SetSpeed(this.GetSpeed() + 32);',
  23735. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23736. ' return Result;',
  23737. ' };',
  23738. '});',
  23739. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23740. ' this.GetLeft = function () {',
  23741. ' var Result = 0;',
  23742. ' $mod.TObject.FSize = this.FSize + 11;',
  23743. ' this.SetSpeed(this.GetSpeed() + 12);',
  23744. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23745. ' $mod.TObject.FSize = this.FSize + 21;',
  23746. ' this.SetSpeed(this.GetSpeed() + 22);',
  23747. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23748. ' $mod.TObject.FSize = this.FSize + 31;',
  23749. ' this.SetSpeed(this.GetSpeed() + 32);',
  23750. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23751. ' return Result;',
  23752. ' };',
  23753. ' this.SetLeft = function (Value) {',
  23754. ' };',
  23755. '});',
  23756. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23757. ' this.DoIt = function () {',
  23758. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23759. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23760. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23761. ' };',
  23762. '});',
  23763. 'this.b = null;',
  23764. 'this.c = null;',
  23765. '']),
  23766. LinesToStr([ // $mod.$main
  23767. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23768. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23769. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23770. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23771. 'var $with = $mod.b;',
  23772. '$mod.TObject.FSize = $with.FSize + 31;',
  23773. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23774. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23775. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23776. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23777. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23778. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23779. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23780. 'var $with1 = $mod.c;',
  23781. '$mod.TObject.FSize = $with1.FSize + 31;',
  23782. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23783. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23784. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23785. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23786. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23787. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23788. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23789. 'var $with2 = $mod.TBird;',
  23790. '$mod.TObject.FSize = $with2.FSize + 31;',
  23791. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23792. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23793. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23794. '']));
  23795. end;
  23796. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23797. begin
  23798. StartProgram(false);
  23799. Add([
  23800. 'type',
  23801. ' TObject = class',
  23802. ' class function GetSpeed: word; static;',
  23803. ' class procedure SetSpeed(Value: word); static;',
  23804. ' end;',
  23805. ' TObjHelper = class helper for TObject',
  23806. ' class function GetLeft: word; static;',
  23807. ' class procedure SetLeft(Value: word); static;',
  23808. ' class property Speed: word read GetSpeed write SetSpeed;',
  23809. ' class property Left: word read GetLeft write SetLeft;',
  23810. ' end;',
  23811. ' TBird = class',
  23812. ' class property NotRight: word read GetLeft write SetLeft;',
  23813. ' class procedure DoIt; static;',
  23814. ' class procedure DoSome;',
  23815. ' end;',
  23816. ' TBirdClass = class of TBird;',
  23817. 'class function Tobject.GetSpeed: word;',
  23818. 'begin',
  23819. ' Speed:=Speed+12;',
  23820. ' Left:=Left+13;',
  23821. 'end;',
  23822. 'class procedure TObject.SetSpeed(Value: word);',
  23823. 'begin',
  23824. 'end;',
  23825. 'class function TObjHelper.GetLeft: word;',
  23826. 'begin',
  23827. ' Speed:=Speed+12;',
  23828. ' Left:=Left+13;',
  23829. 'end;',
  23830. 'class procedure TObjHelper.SetLeft(Value: word);',
  23831. 'begin',
  23832. 'end;',
  23833. 'class procedure TBird.DoIt;',
  23834. 'begin',
  23835. ' NotRight:=NotRight+11;',
  23836. 'end;',
  23837. 'class procedure TBird.DoSome;',
  23838. 'begin',
  23839. ' Speed:=Speed+12;',
  23840. ' Left:=Left+13;',
  23841. ' Self.Speed:=Self.Speed+22;',
  23842. ' Self.Left:=Self.Left+23;',
  23843. ' with Self do begin',
  23844. ' Speed:=Speed+32;',
  23845. ' Left:=Left+33;',
  23846. ' end;',
  23847. ' NotRight:=NotRight+11;',
  23848. ' Self.NotRight:=Self.NotRight+21;',
  23849. ' with Self do NotRight:=NotRight+31;',
  23850. 'end;',
  23851. 'var',
  23852. ' b: TBird;',
  23853. ' c: TBirdClass;',
  23854. 'begin',
  23855. ' b.Speed:=b.Speed+12;',
  23856. ' b.Left:=b.Left+13;',
  23857. ' b.NotRight:=b.NotRight+14;',
  23858. ' with b do begin',
  23859. ' Speed:=Speed+32;',
  23860. ' Left:=Left+33;',
  23861. ' NotRight:=NotRight+34;',
  23862. ' end;',
  23863. ' c.Speed:=c.Speed+12;',
  23864. ' c.Left:=c.Left+13;',
  23865. ' c.NotRight:=c.NotRight+14;',
  23866. ' with c do begin',
  23867. ' Speed:=Speed+32;',
  23868. ' Left:=Left+33;',
  23869. ' NotRight:=NotRight+34;',
  23870. ' end;',
  23871. ' tbird.Speed:=tbird.Speed+12;',
  23872. ' tbird.Left:=tbird.Left+13;',
  23873. ' tbird.NotRight:=tbird.NotRight+14;',
  23874. ' with tbird do begin',
  23875. ' Speed:=Speed+32;',
  23876. ' Left:=Left+33;',
  23877. ' NotRight:=NotRight+34;',
  23878. ' end;',
  23879. '']);
  23880. ConvertProgram;
  23881. CheckSource('TestClassHelper_ClassPropertyStatic',
  23882. LinesToStr([ // statements
  23883. 'rtl.createClass(this, "TObject", null, function () {',
  23884. ' this.$init = function () {',
  23885. ' };',
  23886. ' this.$final = function () {',
  23887. ' };',
  23888. ' this.GetSpeed = function () {',
  23889. ' var Result = 0;',
  23890. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23891. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23892. ' return Result;',
  23893. ' };',
  23894. ' this.SetSpeed = function (Value) {',
  23895. ' };',
  23896. '});',
  23897. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23898. ' this.GetLeft = function () {',
  23899. ' var Result = 0;',
  23900. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23901. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23902. ' return Result;',
  23903. ' };',
  23904. ' this.SetLeft = function (Value) {',
  23905. ' };',
  23906. '});',
  23907. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23908. ' this.DoIt = function () {',
  23909. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23910. ' };',
  23911. ' this.DoSome = function () {',
  23912. ' this.SetSpeed(this.GetSpeed() + 12);',
  23913. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23914. ' this.SetSpeed(this.GetSpeed() + 22);',
  23915. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23916. ' this.SetSpeed(this.GetSpeed() + 32);',
  23917. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23918. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23919. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23920. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23921. ' };',
  23922. '});',
  23923. 'this.b = null;',
  23924. 'this.c = null;',
  23925. '']),
  23926. LinesToStr([ // $mod.$main
  23927. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23928. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23929. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23930. 'var $with = $mod.b;',
  23931. '$with.SetSpeed($with.GetSpeed() + 32);',
  23932. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23933. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23934. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23935. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23936. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23937. 'var $with1 = $mod.c;',
  23938. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23939. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23940. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23941. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23942. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23943. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23944. 'var $with2 = $mod.TBird;',
  23945. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23946. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23947. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23948. '']));
  23949. end;
  23950. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23951. begin
  23952. StartProgram(false);
  23953. Add([
  23954. 'type',
  23955. ' TObject = class',
  23956. ' class function GetSpeed(Index: boolean): word;',
  23957. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23958. ' end;',
  23959. ' TObjHelper = class helper for TObject',
  23960. ' class function GetSize(Index: boolean): word;',
  23961. ' class procedure SetSize(Index: boolean; Value: word);',
  23962. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23963. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23964. ' end;',
  23965. ' TBird = class',
  23966. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23967. ' class procedure DoIt;',
  23968. ' end;',
  23969. ' TBirdClass = class of TBird;',
  23970. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23971. 'begin',
  23972. ' Size[true]:=Size[false]+11;',
  23973. ' Speed[true]:=Speed[false]+12;',
  23974. ' Self.Size[true]:=Self.Size[false]+21;',
  23975. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23976. ' with Self do begin',
  23977. ' Size[true]:=Size[false]+31;',
  23978. ' Speed[true]:=Speed[false]+32;',
  23979. ' end;',
  23980. 'end;',
  23981. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23982. 'begin',
  23983. ' Size[true]:=Size[false]+11;',
  23984. ' Speed[true]:=Speed[false]+12;',
  23985. ' Self.Size[true]:=Self.Size[false]+21;',
  23986. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23987. ' with Self do begin',
  23988. ' Size[true]:=Size[false]+31;',
  23989. ' Speed[true]:=Speed[false]+32;',
  23990. ' end;',
  23991. 'end;',
  23992. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23993. 'begin',
  23994. 'end;',
  23995. 'class procedure TBird.DoIt;',
  23996. 'begin',
  23997. ' Items[true]:=Items[false]+11;',
  23998. ' Self.Items[true]:=Self.Items[false]+21;',
  23999. ' with Self do Items[true]:=Items[false]+31;',
  24000. 'end;',
  24001. 'var',
  24002. ' b: TBird;',
  24003. ' c: TBirdClass;',
  24004. 'begin',
  24005. ' b.Size[true]:=b.Size[false]+11;',
  24006. ' b.Speed[true]:=b.Speed[false]+12;',
  24007. ' b.Items[true]:=b.Items[false]+13;',
  24008. ' with b do begin',
  24009. ' Size[true]:=Size[false]+21;',
  24010. ' Speed[true]:=Speed[false]+22;',
  24011. ' Items[true]:=Items[false]+23;',
  24012. ' end;',
  24013. ' c.Size[true]:=c.Size[false]+11;',
  24014. ' c.Speed[true]:=c.Speed[false]+12;',
  24015. ' c.Items[true]:=c.Items[false]+13;',
  24016. ' with c do begin',
  24017. ' Size[true]:=Size[false]+21;',
  24018. ' Speed[true]:=Speed[false]+22;',
  24019. ' Items[true]:=Items[false]+23;',
  24020. ' end;',
  24021. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24022. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24023. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24024. ' with TBird do begin',
  24025. ' Size[true]:=Size[false]+21;',
  24026. ' Speed[true]:=Speed[false]+22;',
  24027. ' Items[true]:=Items[false]+23;',
  24028. ' end;',
  24029. '']);
  24030. ConvertProgram;
  24031. CheckSource('TestClassHelper_ClassProperty_Array',
  24032. LinesToStr([ // statements
  24033. 'rtl.createClass(this, "TObject", null, function () {',
  24034. ' this.$init = function () {',
  24035. ' };',
  24036. ' this.$final = function () {',
  24037. ' };',
  24038. ' this.GetSpeed = function (Index) {',
  24039. ' var Result = 0;',
  24040. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24041. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24042. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24043. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24044. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24045. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24046. ' return Result;',
  24047. ' };',
  24048. '});',
  24049. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24050. ' this.GetSize = function (Index) {',
  24051. ' var Result = 0;',
  24052. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24053. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24054. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24055. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24056. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24057. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24058. ' return Result;',
  24059. ' };',
  24060. ' this.SetSize = function (Index, Value) {',
  24061. ' };',
  24062. '});',
  24063. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24064. ' this.DoIt = function () {',
  24065. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24066. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24067. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24068. ' };',
  24069. '});',
  24070. 'this.b = null;',
  24071. 'this.c = null;',
  24072. '']),
  24073. LinesToStr([ // $mod.$main
  24074. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24075. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24076. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24077. 'var $with = $mod.b;',
  24078. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24079. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24080. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24081. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24082. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24083. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24084. 'var $with1 = $mod.c;',
  24085. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24086. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24087. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24088. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24089. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24090. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24091. 'var $with2 = $mod.TBird;',
  24092. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24093. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24094. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24095. '']));
  24096. end;
  24097. procedure TTestModule.TestClassHelper_ForIn;
  24098. begin
  24099. StartProgram(false);
  24100. Add([
  24101. 'type',
  24102. ' TObject = class end;',
  24103. ' TItem = TObject;',
  24104. ' TEnumerator = class',
  24105. ' FCurrent: TItem;',
  24106. ' property Current: TItem read FCurrent;',
  24107. ' function MoveNext: boolean;',
  24108. ' end;',
  24109. ' TBird = class',
  24110. ' end;',
  24111. ' TBirdHelper = class helper for TBird',
  24112. ' function GetEnumerator: TEnumerator;',
  24113. ' end;',
  24114. 'function TEnumerator.MoveNext: boolean;',
  24115. 'begin',
  24116. 'end;',
  24117. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24118. 'begin',
  24119. 'end;',
  24120. 'var',
  24121. ' b: TBird;',
  24122. ' i, i2: TItem;',
  24123. 'begin',
  24124. ' for i in b do i2:=i;']);
  24125. ConvertProgram;
  24126. CheckSource('TestClassHelper_ForIn',
  24127. LinesToStr([ // statements
  24128. 'rtl.createClass(this, "TObject", null, function () {',
  24129. ' this.$init = function () {',
  24130. ' };',
  24131. ' this.$final = function () {',
  24132. ' };',
  24133. '});',
  24134. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24135. ' this.$init = function () {',
  24136. ' $mod.TObject.$init.call(this);',
  24137. ' this.FCurrent = null;',
  24138. ' };',
  24139. ' this.$final = function () {',
  24140. ' this.FCurrent = undefined;',
  24141. ' $mod.TObject.$final.call(this);',
  24142. ' };',
  24143. ' this.MoveNext = function () {',
  24144. ' var Result = false;',
  24145. ' return Result;',
  24146. ' };',
  24147. '});',
  24148. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24149. '});',
  24150. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24151. ' this.GetEnumerator = function () {',
  24152. ' var Result = null;',
  24153. ' return Result;',
  24154. ' };',
  24155. '});',
  24156. 'this.b = null;',
  24157. 'this.i = null;',
  24158. 'this.i2 = null;'
  24159. ]),
  24160. LinesToStr([ // $mod.$main
  24161. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24162. 'try {',
  24163. ' while ($in.MoveNext()){',
  24164. ' $mod.i = $in.FCurrent;',
  24165. ' $mod.i2 = $mod.i;',
  24166. ' }',
  24167. '} finally {',
  24168. ' $in = rtl.freeLoc($in)',
  24169. '};',
  24170. '']));
  24171. end;
  24172. procedure TTestModule.TestClassHelper_PassProperty;
  24173. begin
  24174. StartProgram(false);
  24175. Add([
  24176. 'type',
  24177. ' TObject = class',
  24178. ' FField: TObject;',
  24179. ' property Field: TObject read FField write FField;',
  24180. ' end;',
  24181. ' THelper = class helper for TObject',
  24182. ' procedure Fly;',
  24183. ' class procedure Run;',
  24184. ' class procedure Jump; static;',
  24185. ' end;',
  24186. 'procedure THelper.Fly;',
  24187. 'begin',
  24188. ' Field.Fly;',
  24189. ' Field.Run;',
  24190. ' Field.Jump;',
  24191. ' with Field do begin',
  24192. ' Fly;',
  24193. ' Run;',
  24194. ' Jump;',
  24195. ' end;',
  24196. 'end;',
  24197. 'class procedure THelper.Run;',
  24198. 'begin',
  24199. 'end;',
  24200. 'class procedure THelper.Jump;',
  24201. 'begin',
  24202. 'end;',
  24203. 'var',
  24204. ' b: TObject;',
  24205. 'begin',
  24206. ' b.Field.Fly;',
  24207. ' b.Field.Run;',
  24208. ' b.Field.Jump;',
  24209. ' with b do begin',
  24210. ' Field.Run;',
  24211. ' Field.Fly;',
  24212. ' Field.Jump;',
  24213. ' end;',
  24214. ' with b.Field do begin',
  24215. ' Run;',
  24216. ' Fly;',
  24217. ' Jump;',
  24218. ' end;',
  24219. '']);
  24220. ConvertProgram;
  24221. CheckSource('TestClassHelper_PassProperty',
  24222. LinesToStr([ // statements
  24223. 'rtl.createClass(this, "TObject", null, function () {',
  24224. ' this.$init = function () {',
  24225. ' this.FField = null;',
  24226. ' };',
  24227. ' this.$final = function () {',
  24228. ' this.FField = undefined;',
  24229. ' };',
  24230. '});',
  24231. 'rtl.createHelper(this, "THelper", null, function () {',
  24232. ' this.Fly = function () {',
  24233. ' $mod.THelper.Fly.call(this.FField);',
  24234. ' $mod.THelper.Run.call(this.FField.$class);',
  24235. ' $mod.THelper.Jump();',
  24236. ' var $with = this.FField;',
  24237. ' $mod.THelper.Fly.call($with);',
  24238. ' $mod.THelper.Run.call($with.$class);',
  24239. ' $mod.THelper.Jump();',
  24240. ' };',
  24241. ' this.Run = function () {',
  24242. ' };',
  24243. ' this.Jump = function () {',
  24244. ' };',
  24245. '});',
  24246. 'this.b = null;',
  24247. '']),
  24248. LinesToStr([ // $mod.$main
  24249. '$mod.THelper.Fly.call($mod.b.FField);',
  24250. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24251. '$mod.THelper.Jump();',
  24252. 'var $with = $mod.b;',
  24253. '$mod.THelper.Run.call($with.FField.$class);',
  24254. '$mod.THelper.Fly.call($with.FField);',
  24255. '$mod.THelper.Jump();',
  24256. 'var $with1 = $mod.b.FField;',
  24257. '$mod.THelper.Run.call($with1.$class);',
  24258. '$mod.THelper.Fly.call($with1);',
  24259. '$mod.THelper.Jump();',
  24260. '']));
  24261. end;
  24262. procedure TTestModule.TestExtClassHelper_ClassVar;
  24263. begin
  24264. StartProgram(false);
  24265. Add([
  24266. '{$modeswitch externalclass}',
  24267. 'type',
  24268. ' TExtA = class external name ''ExtObj''',
  24269. ' end;',
  24270. ' THelper = class helper for TExtA',
  24271. ' const',
  24272. ' One = 1;',
  24273. ' Two: word = 2;',
  24274. ' class var',
  24275. ' Glob: word;',
  24276. ' function Foo(w: word): word;',
  24277. ' class function Bar(w: word): word; static;',
  24278. ' end;',
  24279. 'function THelper.foo(w: word): word;',
  24280. 'begin',
  24281. ' Result:=w;',
  24282. ' Two:=One+w;',
  24283. ' Glob:=Glob;',
  24284. ' Result:=Self.Glob;',
  24285. ' Self.Glob:=Self.Glob;',
  24286. ' with Self do Glob:=Glob;',
  24287. 'end;',
  24288. 'class function THelper.bar(w: word): word;',
  24289. 'begin',
  24290. ' Result:=w;',
  24291. ' Two:=One;',
  24292. ' Glob:=Glob;',
  24293. 'end;',
  24294. 'var o: TExtA;',
  24295. 'begin',
  24296. ' texta.two:=texta.one;',
  24297. ' texta.Glob:=texta.Glob;',
  24298. ' with texta do begin',
  24299. ' two:=one;',
  24300. ' Glob:=Glob;',
  24301. ' end;',
  24302. ' o.two:=o.one;',
  24303. ' o.Glob:=o.Glob;',
  24304. ' with o do begin',
  24305. ' two:=one;',
  24306. ' Glob:=Glob;',
  24307. ' end;',
  24308. '']);
  24309. ConvertProgram;
  24310. CheckSource('TestExtClassHelper_ClassVar',
  24311. LinesToStr([ // statements
  24312. 'rtl.createHelper(this, "THelper", null, function () {',
  24313. ' this.One = 1;',
  24314. ' this.Two = 2;',
  24315. ' this.Glob = 0;',
  24316. ' this.Foo = function (w) {',
  24317. ' var Result = 0;',
  24318. ' Result = w;',
  24319. ' $mod.THelper.Two = 1 + w;',
  24320. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24321. ' Result = $mod.THelper.Glob;',
  24322. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24323. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24324. ' return Result;',
  24325. ' };',
  24326. ' this.Bar = function (w) {',
  24327. ' var Result = 0;',
  24328. ' Result = w;',
  24329. ' $mod.THelper.Two = 1;',
  24330. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24331. ' return Result;',
  24332. ' };',
  24333. '});',
  24334. 'this.o = null;',
  24335. '']),
  24336. LinesToStr([ // $mod.$main
  24337. '$mod.THelper.Two = 1;',
  24338. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24339. '$mod.THelper.Two = 1;',
  24340. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24341. '$mod.THelper.Two = 1;',
  24342. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24343. 'var $with = $mod.o;',
  24344. '$mod.THelper.Two = 1;',
  24345. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24346. '']));
  24347. end;
  24348. procedure TTestModule.TestExtClassHelper_Method_Call;
  24349. begin
  24350. StartProgram(false);
  24351. Add([
  24352. '{$modeswitch externalclass}',
  24353. 'type',
  24354. ' TFly = function(w: word): word of object;',
  24355. ' TExtA = class external name ''ExtObj''',
  24356. ' procedure Run(w: word = 10);',
  24357. ' end;',
  24358. ' THelper = class helper for TExtA',
  24359. ' function Foo(w: word = 1): word;',
  24360. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24361. ' end;',
  24362. 'var p: TFly;',
  24363. 'function THelper.foo(w: word): word;',
  24364. 'begin',
  24365. ' Run;',
  24366. ' Run();',
  24367. ' Run(11);',
  24368. ' Foo;',
  24369. ' Foo();',
  24370. ' Foo(12);',
  24371. ' Self.Foo;',
  24372. ' Self.Foo();',
  24373. ' Self.Foo(13);',
  24374. ' Fly;',
  24375. ' Fly();',
  24376. ' with Self do begin',
  24377. ' Foo;',
  24378. ' Foo();',
  24379. ' Foo(14);',
  24380. ' Fly;',
  24381. ' Fly();',
  24382. ' end;',
  24383. ' p:=@Fly;',
  24384. 'end;',
  24385. 'var Obj: TExtA;',
  24386. 'begin',
  24387. ' obj.Foo;',
  24388. ' obj.Foo();',
  24389. ' obj.Foo(21);',
  24390. ' obj.Fly;',
  24391. ' obj.Fly();',
  24392. ' with obj do begin',
  24393. ' Foo;',
  24394. ' Foo();',
  24395. ' Foo(22);',
  24396. ' Fly;',
  24397. ' Fly();',
  24398. ' end;',
  24399. ' p:[email protected];',
  24400. '']);
  24401. ConvertProgram;
  24402. CheckSource('TestExtClassHelper_Method_Call',
  24403. LinesToStr([ // statements
  24404. 'rtl.createHelper(this, "THelper", null, function () {',
  24405. ' this.Foo = function (w) {',
  24406. ' var Result = 0;',
  24407. ' this.Run(10);',
  24408. ' this.Run(10);',
  24409. ' this.Run(11);',
  24410. ' $mod.THelper.Foo.call(this, 1);',
  24411. ' $mod.THelper.Foo.call(this, 1);',
  24412. ' $mod.THelper.Foo.call(this, 12);',
  24413. ' $mod.THelper.Foo.call(this, 1);',
  24414. ' $mod.THelper.Foo.call(this, 1);',
  24415. ' $mod.THelper.Foo.call(this, 13);',
  24416. ' this.Fly(2);',
  24417. ' this.Fly(2);',
  24418. ' $mod.THelper.Foo.call(this, 1);',
  24419. ' $mod.THelper.Foo.call(this, 1);',
  24420. ' $mod.THelper.Foo.call(this, 14);',
  24421. ' this.Fly(2);',
  24422. ' this.Fly(2);',
  24423. ' $mod.p = rtl.createCallback(this, "Fly");',
  24424. ' return Result;',
  24425. ' };',
  24426. '});',
  24427. 'this.p = null;',
  24428. 'this.Obj = null;',
  24429. '']),
  24430. LinesToStr([ // $mod.$main
  24431. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24432. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24433. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24434. '$mod.Obj.Fly(2);',
  24435. '$mod.Obj.Fly(2);',
  24436. 'var $with = $mod.Obj;',
  24437. '$mod.THelper.Foo.call($with, 1);',
  24438. '$mod.THelper.Foo.call($with, 1);',
  24439. '$mod.THelper.Foo.call($with, 22);',
  24440. '$with.Fly(2);',
  24441. '$with.Fly(2);',
  24442. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24443. '']));
  24444. end;
  24445. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24446. begin
  24447. StartProgram(false);
  24448. Add([
  24449. '{$modeswitch externalclass}',
  24450. 'type',
  24451. ' TExtA = class external name ''ExtObj''',
  24452. ' procedure Run(w: word = 10);',
  24453. ' end;',
  24454. ' THelper = class helper for TExtA',
  24455. ' class procedure Fly;',
  24456. ' end;',
  24457. 'class procedure THelper.Fly;',
  24458. 'begin end;',
  24459. 'begin',
  24460. '']);
  24461. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24462. nHelperClassMethodForExtClassMustBeStatic);
  24463. ConvertProgram;
  24464. end;
  24465. procedure TTestModule.TestRecordHelper_ClassVar;
  24466. begin
  24467. StartProgram(false);
  24468. Add([
  24469. 'type',
  24470. ' TRec = record',
  24471. ' end;',
  24472. ' THelper = record helper for TRec',
  24473. ' const',
  24474. ' One = 1;',
  24475. ' Two: word = 2;',
  24476. ' class var',
  24477. ' Glob: word;',
  24478. ' function Foo(w: word): word;',
  24479. ' class function Bar(w: word): word; static;',
  24480. ' end;',
  24481. 'function THelper.foo(w: word): word;',
  24482. 'begin',
  24483. ' Result:=w;',
  24484. ' Two:=One+w;',
  24485. ' Glob:=Glob;',
  24486. ' Result:=Self.Glob;',
  24487. ' Self.Glob:=Self.Glob;',
  24488. ' with Self do Glob:=Glob;',
  24489. ' Self:=Self;',
  24490. 'end;',
  24491. 'class function THelper.bar(w: word): word;',
  24492. 'begin',
  24493. ' Result:=w;',
  24494. ' Two:=One;',
  24495. ' Glob:=Glob;',
  24496. 'end;',
  24497. 'var r: TRec;',
  24498. 'begin',
  24499. ' trec.two:=trec.one;',
  24500. ' trec.Glob:=trec.Glob;',
  24501. ' with trec do begin',
  24502. ' two:=one;',
  24503. ' Glob:=Glob;',
  24504. ' end;',
  24505. ' r.two:=r.one;',
  24506. ' r.Glob:=r.Glob;',
  24507. ' with r do begin',
  24508. ' two:=one;',
  24509. ' Glob:=Glob;',
  24510. ' end;',
  24511. '']);
  24512. ConvertProgram;
  24513. CheckSource('TestRecordHelper_ClassVar',
  24514. LinesToStr([ // statements
  24515. 'rtl.recNewT(this, "TRec", function () {',
  24516. ' this.$eq = function (b) {',
  24517. ' return true;',
  24518. ' };',
  24519. ' this.$assign = function (s) {',
  24520. ' return this;',
  24521. ' };',
  24522. '});',
  24523. 'rtl.createHelper(this, "THelper", null, function () {',
  24524. ' this.One = 1;',
  24525. ' this.Two = 2;',
  24526. ' this.Glob = 0;',
  24527. ' this.Foo = function (w) {',
  24528. ' var Result = 0;',
  24529. ' Result = w;',
  24530. ' $mod.THelper.Two = 1 + w;',
  24531. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24532. ' Result = $mod.THelper.Glob;',
  24533. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24534. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24535. ' this.$assign(this);',
  24536. ' return Result;',
  24537. ' };',
  24538. ' this.Bar = function (w) {',
  24539. ' var Result = 0;',
  24540. ' Result = w;',
  24541. ' $mod.THelper.Two = 1;',
  24542. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24543. ' return Result;',
  24544. ' };',
  24545. '});',
  24546. 'this.r = this.TRec.$new();',
  24547. '']),
  24548. LinesToStr([ // $mod.$main
  24549. '$mod.THelper.Two = 1;',
  24550. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24551. 'var $with = $mod.TRec;',
  24552. '$mod.THelper.Two = 1;',
  24553. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24554. '$mod.THelper.Two = 1;',
  24555. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24556. 'var $with1 = $mod.r;',
  24557. '$mod.THelper.Two = 1;',
  24558. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24559. '']));
  24560. end;
  24561. procedure TTestModule.TestRecordHelper_Method_Call;
  24562. begin
  24563. StartProgram(false);
  24564. Add([
  24565. '{$modeswitch AdvancedRecords}',
  24566. 'type',
  24567. ' TRec = record',
  24568. ' procedure Run(w: word = 10);',
  24569. ' end;',
  24570. ' THelper = record helper for TRec',
  24571. ' function Foo(w: word = 1): word;',
  24572. ' end;',
  24573. 'procedure TRec.Run(w: word);',
  24574. 'begin',
  24575. ' Foo;',
  24576. ' Foo();',
  24577. ' Foo(2);',
  24578. ' Self.Foo;',
  24579. ' Self.Foo();',
  24580. ' Self.Foo(3);',
  24581. ' with Self do begin',
  24582. ' Foo;',
  24583. ' Foo();',
  24584. ' Foo(4);',
  24585. ' end;',
  24586. 'end;',
  24587. 'function THelper.foo(w: word): word;',
  24588. 'begin',
  24589. ' Run;',
  24590. ' Run();',
  24591. ' Run(11);',
  24592. ' Foo;',
  24593. ' Foo();',
  24594. ' Foo(12);',
  24595. ' Self.Foo;',
  24596. ' Self.Foo();',
  24597. ' Self.Foo(13);',
  24598. ' with Self do begin',
  24599. ' Foo;',
  24600. ' Foo();',
  24601. ' Foo(14);',
  24602. ' end;',
  24603. 'end;',
  24604. 'var Rec: TRec;',
  24605. 'begin',
  24606. ' Rec.Foo;',
  24607. ' Rec.Foo();',
  24608. ' Rec.Foo(21);',
  24609. ' with Rec do begin',
  24610. ' Foo;',
  24611. ' Foo();',
  24612. ' Foo(22);',
  24613. ' end;',
  24614. '']);
  24615. ConvertProgram;
  24616. CheckSource('TestRecordHelper_Method_Call',
  24617. LinesToStr([ // statements
  24618. 'rtl.recNewT(this, "TRec", function () {',
  24619. ' this.$eq = function (b) {',
  24620. ' return true;',
  24621. ' };',
  24622. ' this.$assign = function (s) {',
  24623. ' return this;',
  24624. ' };',
  24625. ' this.Run = function (w) {',
  24626. ' $mod.THelper.Foo.call(this, 1);',
  24627. ' $mod.THelper.Foo.call(this, 1);',
  24628. ' $mod.THelper.Foo.call(this, 2);',
  24629. ' $mod.THelper.Foo.call(this, 1);',
  24630. ' $mod.THelper.Foo.call(this, 1);',
  24631. ' $mod.THelper.Foo.call(this, 3);',
  24632. ' $mod.THelper.Foo.call(this, 1);',
  24633. ' $mod.THelper.Foo.call(this, 1);',
  24634. ' $mod.THelper.Foo.call(this, 4);',
  24635. ' };',
  24636. '});',
  24637. 'rtl.createHelper(this, "THelper", null, function () {',
  24638. ' this.Foo = function (w) {',
  24639. ' var Result = 0;',
  24640. ' this.Run(10);',
  24641. ' this.Run(10);',
  24642. ' this.Run(11);',
  24643. ' $mod.THelper.Foo.call(this, 1);',
  24644. ' $mod.THelper.Foo.call(this, 1);',
  24645. ' $mod.THelper.Foo.call(this, 12);',
  24646. ' $mod.THelper.Foo.call(this, 1);',
  24647. ' $mod.THelper.Foo.call(this, 1);',
  24648. ' $mod.THelper.Foo.call(this, 13);',
  24649. ' $mod.THelper.Foo.call(this, 1);',
  24650. ' $mod.THelper.Foo.call(this, 1);',
  24651. ' $mod.THelper.Foo.call(this, 14);',
  24652. ' return Result;',
  24653. ' };',
  24654. '});',
  24655. 'this.Rec = this.TRec.$new();',
  24656. '']),
  24657. LinesToStr([ // $mod.$main
  24658. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24659. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24660. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24661. 'var $with = $mod.Rec;',
  24662. '$mod.THelper.Foo.call($with, 1);',
  24663. '$mod.THelper.Foo.call($with, 1);',
  24664. '$mod.THelper.Foo.call($with, 22);',
  24665. '']));
  24666. end;
  24667. procedure TTestModule.TestRecordHelper_Constructor;
  24668. begin
  24669. StartProgram(false);
  24670. Add([
  24671. '{$modeswitch AdvancedRecords}',
  24672. 'type',
  24673. ' TRec = record',
  24674. ' constructor Create(w: word);',
  24675. ' end;',
  24676. ' THelper = record helper for TRec',
  24677. ' constructor NewHlp(w: word);',
  24678. ' end;',
  24679. 'var',
  24680. ' Rec: TRec;',
  24681. 'constructor TRec.Create(w: word);',
  24682. 'begin',
  24683. ' NewHlp(2);', // normal call
  24684. ' trec.NewHlp(3);', // new instance
  24685. 'end;',
  24686. 'constructor THelper.NewHlp(w: word);',
  24687. 'begin',
  24688. ' create(2);', // normal call
  24689. ' trec.create(3);', // new instance
  24690. ' NewHlp(4);', // normal call
  24691. ' trec.NewHlp(5);', // new instance
  24692. 'end;',
  24693. 'begin',
  24694. ' rec.newhlp(2);', // normal call
  24695. ' with rec do newhlp(12);', // normal call
  24696. ' trec.newhlp(3);', // new instance
  24697. ' with trec do newhlp(13);', // new instance
  24698. '']);
  24699. ConvertProgram;
  24700. CheckSource('TestRecordHelper_Constructor',
  24701. LinesToStr([ // statements
  24702. 'rtl.recNewT(this, "TRec", function () {',
  24703. ' this.$eq = function (b) {',
  24704. ' return true;',
  24705. ' };',
  24706. ' this.$assign = function (s) {',
  24707. ' return this;',
  24708. ' };',
  24709. ' this.Create = function (w) {',
  24710. ' $mod.THelper.NewHlp.call(this, 2);',
  24711. ' $mod.THelper.$new("NewHlp", [3]);',
  24712. ' return this;',
  24713. ' };',
  24714. '});',
  24715. 'rtl.createHelper(this, "THelper", null, function () {',
  24716. ' this.NewHlp = function (w) {',
  24717. ' this.Create(2);',
  24718. ' $mod.TRec.$new().Create(3);',
  24719. ' $mod.THelper.NewHlp.call(this, 4);',
  24720. ' $mod.THelper.$new("NewHlp", [5]);',
  24721. ' return this;',
  24722. ' };',
  24723. ' this.$new = function (fn, args) {',
  24724. ' return this[fn].apply($mod.TRec.$new(), args);',
  24725. ' };',
  24726. '});',
  24727. 'this.Rec = this.TRec.$new();',
  24728. '']),
  24729. LinesToStr([ // $mod.$main
  24730. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24731. 'var $with = $mod.Rec;',
  24732. '$mod.THelper.NewHlp.call($with, 12);',
  24733. '$mod.THelper.$new("NewHlp", [3]);',
  24734. 'var $with1 = $mod.TRec;',
  24735. '$mod.THelper.$new("NewHlp", [13]);',
  24736. '']));
  24737. end;
  24738. procedure TTestModule.TestTypeHelper_ClassVar;
  24739. begin
  24740. StartProgram(false);
  24741. Add([
  24742. '{$modeswitch typehelpers}',
  24743. 'type',
  24744. ' THelper = type helper for byte',
  24745. ' const',
  24746. ' One = 1;',
  24747. ' Two: word = 2;',
  24748. ' class var',
  24749. ' Glob: word;',
  24750. ' function Foo(w: word): word;',
  24751. ' class function Bar(w: word): word; static;',
  24752. ' end;',
  24753. 'function THelper.foo(w: word): word;',
  24754. 'begin',
  24755. ' Result:=w;',
  24756. ' Two:=One+w;',
  24757. ' Glob:=Glob;',
  24758. ' Result:=Self.Glob;',
  24759. ' Self.Glob:=Self.Glob;',
  24760. ' with Self do Glob:=Glob;',
  24761. 'end;',
  24762. 'class function THelper.bar(w: word): word;',
  24763. 'begin',
  24764. ' Result:=w;',
  24765. ' Two:=One;',
  24766. ' Glob:=Glob;',
  24767. 'end;',
  24768. 'var b: byte;',
  24769. 'begin',
  24770. ' byte.two:=byte.one;',
  24771. ' byte.Glob:=byte.Glob;',
  24772. ' with byte do begin',
  24773. ' two:=one;',
  24774. ' Glob:=Glob;',
  24775. ' end;',
  24776. ' b.two:=b.one;',
  24777. ' b.Glob:=b.Glob;',
  24778. ' with b do begin',
  24779. ' two:=one;',
  24780. ' Glob:=Glob;',
  24781. ' end;',
  24782. '']);
  24783. ConvertProgram;
  24784. CheckSource('TestTypeHelper_ClassVar',
  24785. LinesToStr([ // statements
  24786. 'rtl.createHelper(this, "THelper", null, function () {',
  24787. ' this.One = 1;',
  24788. ' this.Two = 2;',
  24789. ' this.Glob = 0;',
  24790. ' this.Foo = function (w) {',
  24791. ' var Result = 0;',
  24792. ' Result = w;',
  24793. ' $mod.THelper.Two = 1 + w;',
  24794. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24795. ' Result = $mod.THelper.Glob;',
  24796. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24797. ' var $with = this.get();',
  24798. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24799. ' return Result;',
  24800. ' };',
  24801. ' this.Bar = function (w) {',
  24802. ' var Result = 0;',
  24803. ' Result = w;',
  24804. ' $mod.THelper.Two = 1;',
  24805. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24806. ' return Result;',
  24807. ' };',
  24808. '});',
  24809. 'this.b = 0;',
  24810. '']),
  24811. LinesToStr([ // $mod.$main
  24812. '$mod.THelper.Two = 1;',
  24813. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24814. '$mod.THelper.Two = 1;',
  24815. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24816. '$mod.THelper.Two = 1;',
  24817. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24818. 'var $with = $mod.b;',
  24819. '$mod.THelper.Two = 1;',
  24820. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24821. '']));
  24822. end;
  24823. procedure TTestModule.TestTypeHelper_PassResultElement;
  24824. begin
  24825. StartProgram(false);
  24826. Add([
  24827. '{$modeswitch typehelpers}',
  24828. 'type',
  24829. ' THelper = type helper for word',
  24830. ' procedure DoIt(e: byte = 123);',
  24831. ' class procedure DoSome(e: byte = 456); static;',
  24832. ' end;',
  24833. 'procedure THelper.DoIt(e: byte);',
  24834. 'begin',
  24835. 'end;',
  24836. 'class procedure THelper.DoSome(e: byte);',
  24837. 'begin',
  24838. 'end;',
  24839. 'function Foo(w: word): word;',
  24840. 'begin',
  24841. ' Result.DoIt;',
  24842. ' Result.DoIt();',
  24843. ' Result.DoSome;',
  24844. ' Result.DoSome();',
  24845. ' with Result do begin',
  24846. ' DoIt;',
  24847. ' DoIt();',
  24848. ' DoSome;',
  24849. ' DoSome();',
  24850. ' end;',
  24851. 'end;',
  24852. 'begin',
  24853. '']);
  24854. ConvertProgram;
  24855. CheckSource('TestTypeHelper_PassResultElement',
  24856. LinesToStr([ // statements
  24857. 'rtl.createHelper(this, "THelper", null, function () {',
  24858. ' this.DoIt = function (e) {',
  24859. ' };',
  24860. ' this.DoSome = function (e) {',
  24861. ' };',
  24862. '});',
  24863. 'this.Foo = function (w) {',
  24864. ' var Result = 0;',
  24865. ' $mod.THelper.DoIt.call({',
  24866. ' get: function () {',
  24867. ' return Result;',
  24868. ' },',
  24869. ' set: function (v) {',
  24870. ' Result = v;',
  24871. ' }',
  24872. ' }, 123);',
  24873. ' $mod.THelper.DoIt.call({',
  24874. ' get: function () {',
  24875. ' return Result;',
  24876. ' },',
  24877. ' set: function (v) {',
  24878. ' Result = v;',
  24879. ' }',
  24880. ' }, 123);',
  24881. ' $mod.THelper.DoSome(456);',
  24882. ' $mod.THelper.DoSome(456);',
  24883. ' $mod.THelper.DoIt.call({',
  24884. ' get: function () {',
  24885. ' return Result;',
  24886. ' },',
  24887. ' set: function (v) {',
  24888. ' Result = v;',
  24889. ' }',
  24890. ' }, 123);',
  24891. ' $mod.THelper.DoIt.call({',
  24892. ' get: function () {',
  24893. ' return Result;',
  24894. ' },',
  24895. ' set: function (v) {',
  24896. ' Result = v;',
  24897. ' }',
  24898. ' }, 123);',
  24899. ' $mod.THelper.DoSome(456);',
  24900. ' $mod.THelper.DoSome(456);',
  24901. ' return Result;',
  24902. '};',
  24903. '']),
  24904. LinesToStr([ // $mod.$main
  24905. '']));
  24906. end;
  24907. procedure TTestModule.TestTypeHelper_PassArgs;
  24908. begin
  24909. StartProgram(false);
  24910. Add([
  24911. '{$modeswitch typehelpers}',
  24912. 'type',
  24913. ' THelper = type helper for word',
  24914. ' procedure DoIt(e: byte = 123);',
  24915. ' end;',
  24916. 'procedure THelper.DoIt(e: byte);',
  24917. 'begin',
  24918. 'end;',
  24919. 'procedure FooDefault(a: word);',
  24920. 'begin',
  24921. ' a.DoIt;',
  24922. ' with a do DoIt;',
  24923. 'end;',
  24924. 'procedure FooConst(const a: word);',
  24925. 'begin',
  24926. ' a.DoIt;',
  24927. ' with a do DoIt;',
  24928. 'end;',
  24929. 'procedure FooVar(var a: word);',
  24930. 'begin',
  24931. ' a.DoIt;',
  24932. ' with a do DoIt;',
  24933. 'end;',
  24934. 'begin',
  24935. '']);
  24936. ConvertProgram;
  24937. CheckSource('TestTypeHelper_PassArgs',
  24938. LinesToStr([ // statements
  24939. 'rtl.createHelper(this, "THelper", null, function () {',
  24940. ' this.DoIt = function (e) {',
  24941. ' };',
  24942. '});',
  24943. 'this.FooDefault = function (a) {',
  24944. ' $mod.THelper.DoIt.call({',
  24945. ' get: function () {',
  24946. ' return a;',
  24947. ' },',
  24948. ' set: function (v) {',
  24949. ' a = v;',
  24950. ' }',
  24951. ' }, 123);',
  24952. ' $mod.THelper.DoIt.call({',
  24953. ' get: function () {',
  24954. ' return a;',
  24955. ' },',
  24956. ' set: function (v) {',
  24957. ' a = v;',
  24958. ' }',
  24959. ' }, 123);',
  24960. '};',
  24961. 'this.FooConst = function (a) {',
  24962. ' $mod.THelper.DoIt.call({',
  24963. ' get: function () {',
  24964. ' return a;',
  24965. ' },',
  24966. ' set: function (v) {',
  24967. ' rtl.raiseE("EPropReadOnly");',
  24968. ' }',
  24969. ' }, 123);',
  24970. ' $mod.THelper.DoIt.call({',
  24971. ' get: function () {',
  24972. ' return a;',
  24973. ' },',
  24974. ' set: function () {',
  24975. ' rtl.raiseE("EPropReadOnly");',
  24976. ' }',
  24977. ' }, 123);',
  24978. '};',
  24979. 'this.FooVar = function (a) {',
  24980. ' $mod.THelper.DoIt.call(a, 123);',
  24981. ' var $with = a.get();',
  24982. ' $mod.THelper.DoIt.call(a, 123);',
  24983. '};',
  24984. '']),
  24985. LinesToStr([ // $mod.$main
  24986. '']));
  24987. end;
  24988. procedure TTestModule.TestTypeHelper_PassVarConst;
  24989. begin
  24990. StartProgram(false);
  24991. Add([
  24992. '{$modeswitch typehelpers}',
  24993. 'type',
  24994. ' THelper = type helper for word',
  24995. ' procedure DoIt(e: byte = 123);',
  24996. ' end;',
  24997. 'procedure THelper.DoIt(e: byte);',
  24998. 'begin',
  24999. 'end;',
  25000. 'var a: word;',
  25001. 'const c: word = 2;',
  25002. '{$writeableconst off}',
  25003. 'const r: word = 3;',
  25004. 'begin',
  25005. ' a.DoIt;',
  25006. ' with a do DoIt;',
  25007. ' c.DoIt;',
  25008. ' with c do DoIt;',
  25009. ' r.DoIt;',
  25010. ' with r do DoIt;',
  25011. '']);
  25012. ConvertProgram;
  25013. CheckSource('TestTypeHelper_PassVarConst',
  25014. LinesToStr([ // statements
  25015. 'rtl.createHelper(this, "THelper", null, function () {',
  25016. ' this.DoIt = function (e) {',
  25017. ' };',
  25018. '});',
  25019. 'this.a = 0;',
  25020. 'this.c = 2;',
  25021. 'this.r = 3;',
  25022. '']),
  25023. LinesToStr([ // $mod.$main
  25024. '$mod.THelper.DoIt.call({',
  25025. ' p: $mod,',
  25026. ' get: function () {',
  25027. ' return this.p.a;',
  25028. ' },',
  25029. ' set: function (v) {',
  25030. ' this.p.a = v;',
  25031. ' }',
  25032. '}, 123);',
  25033. 'var $with = $mod.a;',
  25034. '$mod.THelper.DoIt.call({',
  25035. ' get: function () {',
  25036. ' return $with;',
  25037. ' },',
  25038. ' set: function (v) {',
  25039. ' $with = v;',
  25040. ' }',
  25041. '}, 123);',
  25042. '$mod.THelper.DoIt.call({',
  25043. ' p: $mod,',
  25044. ' get: function () {',
  25045. ' return this.p.c;',
  25046. ' },',
  25047. ' set: function (v) {',
  25048. ' this.p.c = v;',
  25049. ' }',
  25050. '}, 123);',
  25051. 'var $with1 = $mod.c;',
  25052. '$mod.THelper.DoIt.call({',
  25053. ' get: function () {',
  25054. ' return $with1;',
  25055. ' },',
  25056. ' set: function (v) {',
  25057. ' $with1 = v;',
  25058. ' }',
  25059. '}, 123);',
  25060. '$mod.THelper.DoIt.call({',
  25061. ' get: function () {',
  25062. ' return 3;',
  25063. ' },',
  25064. ' set: function (v) {',
  25065. ' rtl.raiseE("EPropReadOnly");',
  25066. ' }',
  25067. '}, 123);',
  25068. 'var $with2 = 3;',
  25069. ' $mod.THelper.DoIt.call({',
  25070. ' get: function () {',
  25071. ' return $with2;',
  25072. ' },',
  25073. ' set: function () {',
  25074. ' rtl.raiseE("EPropReadOnly");',
  25075. ' }',
  25076. ' }, 123);',
  25077. '']));
  25078. end;
  25079. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25080. begin
  25081. StartProgram(false);
  25082. Add([
  25083. '{$modeswitch typehelpers}',
  25084. 'type',
  25085. ' THelper = type helper for word',
  25086. ' procedure DoIt(e: byte = 123);',
  25087. ' end;',
  25088. 'procedure THelper.DoIt(e: byte);',
  25089. 'begin',
  25090. 'end;',
  25091. 'function Foo(b: byte = 1): word;',
  25092. 'begin',
  25093. 'end;',
  25094. 'begin',
  25095. ' Foo.DoIt;',
  25096. ' Foo().DoIt;',
  25097. ' with Foo do DoIt;',
  25098. ' with Foo() do DoIt;',
  25099. '']);
  25100. ConvertProgram;
  25101. CheckSource('TestTypeHelper_PassFuncResult',
  25102. LinesToStr([ // statements
  25103. 'rtl.createHelper(this, "THelper", null, function () {',
  25104. ' this.DoIt = function (e) {',
  25105. ' };',
  25106. '});',
  25107. 'this.Foo = function (b) {',
  25108. ' var Result = 0;',
  25109. ' return Result;',
  25110. '};',
  25111. '']),
  25112. LinesToStr([ // $mod.$main
  25113. '$mod.THelper.DoIt.call({',
  25114. ' a: $mod.Foo(1),',
  25115. ' get: function () {',
  25116. ' return this.a;',
  25117. ' },',
  25118. ' set: function (v) {',
  25119. ' this.a = v;',
  25120. ' }',
  25121. '}, 123);',
  25122. '$mod.THelper.DoIt.call({',
  25123. ' a: $mod.Foo(1),',
  25124. ' get: function () {',
  25125. ' return this.a;',
  25126. ' },',
  25127. ' set: function (v) {',
  25128. ' this.a = v;',
  25129. ' }',
  25130. '}, 123);',
  25131. 'var $with = $mod.Foo(1);',
  25132. '$mod.THelper.DoIt.call({',
  25133. ' get: function () {',
  25134. ' return $with;',
  25135. ' },',
  25136. ' set: function (v) {',
  25137. ' $with = v;',
  25138. ' }',
  25139. '}, 123);',
  25140. 'var $with1 = $mod.Foo(1);',
  25141. '$mod.THelper.DoIt.call({',
  25142. ' get: function () {',
  25143. ' return $with1;',
  25144. ' },',
  25145. ' set: function (v) {',
  25146. ' $with1 = v;',
  25147. ' }',
  25148. '}, 123);',
  25149. '']));
  25150. end;
  25151. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25152. begin
  25153. StartProgram(false);
  25154. Add([
  25155. '{$modeswitch typehelpers}',
  25156. 'type',
  25157. ' TObject = class',
  25158. ' FField: word;',
  25159. ' procedure SetField(Value: word);',
  25160. ' property Field: word read FField write SetField;',
  25161. ' end;',
  25162. ' THelper = type helper for word',
  25163. ' procedure Fly;',
  25164. ' class procedure Run; static;',
  25165. ' end;',
  25166. 'procedure TObject.SetField(Value: word);',
  25167. 'begin',
  25168. ' Field.Fly;',
  25169. ' Field.Run;',
  25170. ' Self.Field.Fly;',
  25171. ' Self.Field.Run;',
  25172. ' with Self do begin',
  25173. ' Field.Fly;',
  25174. ' Field.Run;',
  25175. ' end;',
  25176. ' with Self.Field do begin',
  25177. ' Fly;',
  25178. ' Run;',
  25179. ' end;',
  25180. 'end;',
  25181. 'procedure THelper.Fly;',
  25182. 'begin',
  25183. 'end;',
  25184. 'class procedure THelper.Run;',
  25185. 'begin',
  25186. 'end;',
  25187. 'var',
  25188. ' o: TObject;',
  25189. 'begin',
  25190. ' o.Field.Fly;',
  25191. ' o.Field.Run;',
  25192. ' with o do begin',
  25193. ' Field.Fly;',
  25194. ' Field.Run;',
  25195. ' end;',
  25196. ' with o.Field do begin',
  25197. ' Fly;',
  25198. ' Run;',
  25199. ' end;',
  25200. '']);
  25201. ConvertProgram;
  25202. CheckSource('TestTypeHelper_PassPropertyField',
  25203. LinesToStr([ // statements
  25204. 'rtl.createClass(this, "TObject", null, function () {',
  25205. ' this.$init = function () {',
  25206. ' this.FField = 0;',
  25207. ' };',
  25208. ' this.$final = function () {',
  25209. ' };',
  25210. ' this.SetField = function (Value) {',
  25211. ' $mod.THelper.Fly.call({',
  25212. ' p: this,',
  25213. ' get: function () {',
  25214. ' return this.p.FField;',
  25215. ' },',
  25216. ' set: function (v) {',
  25217. ' this.p.FField = v;',
  25218. ' }',
  25219. ' });',
  25220. ' $mod.THelper.Run();',
  25221. ' $mod.THelper.Fly.call({',
  25222. ' p: this,',
  25223. ' get: function () {',
  25224. ' return this.p.FField;',
  25225. ' },',
  25226. ' set: function (v) {',
  25227. ' this.p.FField = v;',
  25228. ' }',
  25229. ' });',
  25230. ' $mod.THelper.Run();',
  25231. ' $mod.THelper.Fly.call({',
  25232. ' p: this,',
  25233. ' get: function () {',
  25234. ' return this.p.FField;',
  25235. ' },',
  25236. ' set: function (v) {',
  25237. ' this.p.FField = v;',
  25238. ' }',
  25239. ' });',
  25240. ' $mod.THelper.Run();',
  25241. ' var $with = this.FField;',
  25242. ' $mod.THelper.Fly.call({',
  25243. ' get: function () {',
  25244. ' return $with;',
  25245. ' },',
  25246. ' set: function (v) {',
  25247. ' $with = v;',
  25248. ' }',
  25249. ' });',
  25250. ' $mod.THelper.Run();',
  25251. ' };',
  25252. '});',
  25253. 'rtl.createHelper(this, "THelper", null, function () {',
  25254. ' this.Fly = function () {',
  25255. ' };',
  25256. ' this.Run = function () {',
  25257. ' };',
  25258. '});',
  25259. 'this.o = null;',
  25260. '']),
  25261. LinesToStr([ // $mod.$main
  25262. '$mod.THelper.Fly.call({',
  25263. ' p: $mod.o,',
  25264. ' get: function () {',
  25265. ' return this.p.FField;',
  25266. ' },',
  25267. ' set: function (v) {',
  25268. ' this.p.FField = v;',
  25269. ' }',
  25270. '});',
  25271. '$mod.THelper.Run();',
  25272. 'var $with = $mod.o;',
  25273. '$mod.THelper.Fly.call({',
  25274. ' p: $with,',
  25275. ' get: function () {',
  25276. ' return this.p.FField;',
  25277. ' },',
  25278. ' set: function (v) {',
  25279. ' this.p.FField = v;',
  25280. ' }',
  25281. '});',
  25282. '$mod.THelper.Run();',
  25283. 'var $with1 = $mod.o.FField;',
  25284. '$mod.THelper.Fly.call({',
  25285. ' get: function () {',
  25286. ' return $with1;',
  25287. ' },',
  25288. ' set: function (v) {',
  25289. ' $with1 = v;',
  25290. ' }',
  25291. '});',
  25292. '$mod.THelper.Run();',
  25293. '']));
  25294. end;
  25295. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25296. begin
  25297. StartProgram(false);
  25298. Add([
  25299. '{$modeswitch typehelpers}',
  25300. 'type',
  25301. ' TObject = class',
  25302. ' FField: word;',
  25303. ' function GetField: word;',
  25304. ' property Field: word read GetField write FField;',
  25305. ' end;',
  25306. ' THelper = type helper for word',
  25307. ' procedure Fly;',
  25308. ' class procedure Run; static;',
  25309. ' end;',
  25310. 'function TObject.GetField: word;',
  25311. 'begin',
  25312. ' Field.Fly;',
  25313. ' Field.Run;',
  25314. ' Self.Field.Fly;',
  25315. ' Self.Field.Run;',
  25316. ' with Self do begin',
  25317. ' Field.Fly;',
  25318. ' Field.Run;',
  25319. ' end;',
  25320. ' with Self.Field do begin',
  25321. ' Fly;',
  25322. ' Run;',
  25323. ' end;',
  25324. 'end;',
  25325. 'procedure THelper.Fly;',
  25326. 'begin',
  25327. 'end;',
  25328. 'class procedure THelper.Run;',
  25329. 'begin',
  25330. 'end;',
  25331. 'var',
  25332. ' o: TObject;',
  25333. 'begin',
  25334. ' o.Field.Fly;',
  25335. ' o.Field.Run;',
  25336. ' with o do begin',
  25337. ' Field.Fly;',
  25338. ' Field.Run;',
  25339. ' end;',
  25340. ' with o.Field do begin',
  25341. ' Fly;',
  25342. ' Run;',
  25343. ' end;',
  25344. '']);
  25345. ConvertProgram;
  25346. CheckSource('TestTypeHelper_PassPropertyGetter',
  25347. LinesToStr([ // statements
  25348. 'rtl.createClass(this, "TObject", null, function () {',
  25349. ' this.$init = function () {',
  25350. ' this.FField = 0;',
  25351. ' };',
  25352. ' this.$final = function () {',
  25353. ' };',
  25354. ' this.GetField = function () {',
  25355. ' var Result = 0;',
  25356. ' $mod.THelper.Fly.call({',
  25357. ' p: this.GetField(),',
  25358. ' get: function () {',
  25359. ' return this.p;',
  25360. ' },',
  25361. ' set: function (v) {',
  25362. ' this.p = v;',
  25363. ' }',
  25364. ' });',
  25365. ' $mod.THelper.Run();',
  25366. ' $mod.THelper.Fly.call({',
  25367. ' p: this.GetField(),',
  25368. ' get: function () {',
  25369. ' return this.p;',
  25370. ' },',
  25371. ' set: function (v) {',
  25372. ' this.p = v;',
  25373. ' }',
  25374. ' });',
  25375. ' $mod.THelper.Run();',
  25376. ' $mod.THelper.Fly.call({',
  25377. ' p: this.GetField(),',
  25378. ' get: function () {',
  25379. ' return this.p;',
  25380. ' },',
  25381. ' set: function (v) {',
  25382. ' this.p = v;',
  25383. ' }',
  25384. ' });',
  25385. ' $mod.THelper.Run();',
  25386. ' var $with = this.GetField();',
  25387. ' $mod.THelper.Fly.call({',
  25388. ' get: function () {',
  25389. ' return $with;',
  25390. ' },',
  25391. ' set: function (v) {',
  25392. ' $with = v;',
  25393. ' }',
  25394. ' });',
  25395. ' $mod.THelper.Run();',
  25396. ' return Result;',
  25397. ' };',
  25398. '});',
  25399. 'rtl.createHelper(this, "THelper", null, function () {',
  25400. ' this.Fly = function () {',
  25401. ' };',
  25402. ' this.Run = function () {',
  25403. ' };',
  25404. '});',
  25405. 'this.o = null;',
  25406. '']),
  25407. LinesToStr([ // $mod.$main
  25408. '$mod.THelper.Fly.call({',
  25409. ' p: $mod.o.GetField(),',
  25410. ' get: function () {',
  25411. ' return this.p;',
  25412. ' },',
  25413. ' set: function (v) {',
  25414. ' this.p = v;',
  25415. ' }',
  25416. '});',
  25417. '$mod.THelper.Run();',
  25418. 'var $with = $mod.o;',
  25419. '$mod.THelper.Fly.call({',
  25420. ' p: $with.GetField(),',
  25421. ' get: function () {',
  25422. ' return this.p;',
  25423. ' },',
  25424. ' set: function (v) {',
  25425. ' this.p = v;',
  25426. ' }',
  25427. '});',
  25428. '$mod.THelper.Run();',
  25429. 'var $with1 = $mod.o.GetField();',
  25430. '$mod.THelper.Fly.call({',
  25431. ' get: function () {',
  25432. ' return $with1;',
  25433. ' },',
  25434. ' set: function (v) {',
  25435. ' $with1 = v;',
  25436. ' }',
  25437. '});',
  25438. '$mod.THelper.Run();',
  25439. '']));
  25440. end;
  25441. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25442. begin
  25443. StartProgram(false);
  25444. Add([
  25445. '{$modeswitch typehelpers}',
  25446. 'type',
  25447. ' TObject = class',
  25448. ' class var FField: word;',
  25449. ' class procedure SetField(Value: word);',
  25450. ' class property Field: word read FField write SetField;',
  25451. ' end;',
  25452. ' THelper = type helper for word',
  25453. ' procedure Fly(n: byte);',
  25454. ' end;',
  25455. 'class procedure TObject.SetField(Value: word);',
  25456. 'begin',
  25457. ' Field.Fly(1);',
  25458. ' Self.Field.Fly(2);',
  25459. ' with Self do Field.Fly(3);',
  25460. ' with Self.Field do Fly(4);',
  25461. ' TObject.Field.Fly(5);',
  25462. ' with TObject do Field.Fly(6);',
  25463. ' with TObject.Field do Fly(7);',
  25464. 'end;',
  25465. 'procedure THelper.Fly(n: byte);',
  25466. 'begin',
  25467. 'end;',
  25468. 'var',
  25469. ' o: TObject;',
  25470. 'begin',
  25471. ' o.Field.Fly(11);',
  25472. ' with o do Field.Fly(12);',
  25473. ' with o.Field do Fly(13);',
  25474. ' TObject.Field.Fly(14);',
  25475. ' with TObject do Field.Fly(15);',
  25476. ' with TObject.Field do Fly(16);',
  25477. '']);
  25478. ConvertProgram;
  25479. CheckSource('TestTypeHelper_PassClassPropertyField',
  25480. LinesToStr([ // statements
  25481. 'rtl.createClass(this, "TObject", null, function () {',
  25482. ' this.FField = 0;',
  25483. ' this.$init = function () {',
  25484. ' };',
  25485. ' this.$final = function () {',
  25486. ' };',
  25487. ' this.SetField = function (Value) {',
  25488. ' $mod.THelper.Fly.call({',
  25489. ' p: this,',
  25490. ' get: function () {',
  25491. ' return this.p.FField;',
  25492. ' },',
  25493. ' set: function (v) {',
  25494. ' $mod.TObject.FField = v;',
  25495. ' }',
  25496. ' }, 1);',
  25497. ' $mod.THelper.Fly.call({',
  25498. ' p: this,',
  25499. ' get: function () {',
  25500. ' return this.p.FField;',
  25501. ' },',
  25502. ' set: function (v) {',
  25503. ' $mod.TObject.FField = v;',
  25504. ' }',
  25505. ' }, 2);',
  25506. ' $mod.THelper.Fly.call({',
  25507. ' p: this,',
  25508. ' get: function () {',
  25509. ' return this.p.FField;',
  25510. ' },',
  25511. ' set: function (v) {',
  25512. ' $mod.TObject.FField = v;',
  25513. ' }',
  25514. ' }, 3);',
  25515. ' var $with = this.FField;',
  25516. ' $mod.THelper.Fly.call({',
  25517. ' get: function () {',
  25518. ' return $with;',
  25519. ' },',
  25520. ' set: function (v) {',
  25521. ' $with = v;',
  25522. ' }',
  25523. ' }, 4);',
  25524. ' $mod.THelper.Fly.call({',
  25525. ' p: $mod.TObject,',
  25526. ' get: function () {',
  25527. ' return this.p.FField;',
  25528. ' },',
  25529. ' set: function (v) {',
  25530. ' $mod.TObject.FField = v;',
  25531. ' }',
  25532. ' }, 5);',
  25533. ' var $with1 = $mod.TObject;',
  25534. ' $mod.THelper.Fly.call({',
  25535. ' p: $with1,',
  25536. ' get: function () {',
  25537. ' return this.p.FField;',
  25538. ' },',
  25539. ' set: function (v) {',
  25540. ' $mod.TObject.FField = v;',
  25541. ' }',
  25542. ' }, 6);',
  25543. ' var $with2 = $mod.TObject.FField;',
  25544. ' $mod.THelper.Fly.call({',
  25545. ' get: function () {',
  25546. ' return $with2;',
  25547. ' },',
  25548. ' set: function (v) {',
  25549. ' $with2 = v;',
  25550. ' }',
  25551. ' }, 7);',
  25552. ' };',
  25553. '});',
  25554. 'rtl.createHelper(this, "THelper", null, function () {',
  25555. ' this.Fly = function (n) {',
  25556. ' };',
  25557. '});',
  25558. 'this.o = null;',
  25559. '']),
  25560. LinesToStr([ // $mod.$main
  25561. '$mod.THelper.Fly.call({',
  25562. ' p: $mod.o,',
  25563. ' get: function () {',
  25564. ' return this.p.FField;',
  25565. ' },',
  25566. ' set: function (v) {',
  25567. ' $mod.TObject.FField = v;',
  25568. ' }',
  25569. '}, 11);',
  25570. 'var $with = $mod.o;',
  25571. '$mod.THelper.Fly.call({',
  25572. ' p: $with,',
  25573. ' get: function () {',
  25574. ' return this.p.FField;',
  25575. ' },',
  25576. ' set: function (v) {',
  25577. ' $mod.TObject.FField = v;',
  25578. ' }',
  25579. '}, 12);',
  25580. 'var $with1 = $mod.o.FField;',
  25581. '$mod.THelper.Fly.call({',
  25582. ' get: function () {',
  25583. ' return $with1;',
  25584. ' },',
  25585. ' set: function (v) {',
  25586. ' $with1 = v;',
  25587. ' }',
  25588. '}, 13);',
  25589. '$mod.THelper.Fly.call({',
  25590. ' p: $mod.TObject,',
  25591. ' get: function () {',
  25592. ' return this.p.FField;',
  25593. ' },',
  25594. ' set: function (v) {',
  25595. ' $mod.TObject.FField = v;',
  25596. ' }',
  25597. '}, 14);',
  25598. 'var $with2 = $mod.TObject;',
  25599. '$mod.THelper.Fly.call({',
  25600. ' p: $with2,',
  25601. ' get: function () {',
  25602. ' return this.p.FField;',
  25603. ' },',
  25604. ' set: function (v) {',
  25605. ' $mod.TObject.FField = v;',
  25606. ' }',
  25607. '}, 15);',
  25608. 'var $with3 = $mod.TObject.FField;',
  25609. '$mod.THelper.Fly.call({',
  25610. ' get: function () {',
  25611. ' return $with3;',
  25612. ' },',
  25613. ' set: function (v) {',
  25614. ' $with3 = v;',
  25615. ' }',
  25616. '}, 16);',
  25617. '']));
  25618. end;
  25619. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25620. begin
  25621. StartProgram(false);
  25622. Add([
  25623. '{$modeswitch typehelpers}',
  25624. 'type',
  25625. ' TObject = class',
  25626. ' class var FField: word;',
  25627. ' class function GetField: word; static;',
  25628. ' class property Field: word read GetField write FField;',
  25629. ' end;',
  25630. ' THelper = type helper for word',
  25631. ' procedure Fly(n: byte);',
  25632. ' end;',
  25633. 'class function TObject.GetField: word;',
  25634. 'begin',
  25635. ' Field.Fly(1);',
  25636. ' TObject.Field.Fly(5);',
  25637. ' with TObject do Field.Fly(6);',
  25638. ' with TObject.Field do Fly(7);',
  25639. 'end;',
  25640. 'procedure THelper.Fly(n: byte);',
  25641. 'begin',
  25642. 'end;',
  25643. 'var',
  25644. ' o: TObject;',
  25645. 'begin',
  25646. ' o.Field.Fly(11);',
  25647. ' with o do Field.Fly(12);',
  25648. ' with o.Field do Fly(13);',
  25649. '']);
  25650. ConvertProgram;
  25651. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25652. LinesToStr([ // statements
  25653. 'rtl.createClass(this, "TObject", null, function () {',
  25654. ' this.FField = 0;',
  25655. ' this.$init = function () {',
  25656. ' };',
  25657. ' this.$final = function () {',
  25658. ' };',
  25659. ' this.GetField = function () {',
  25660. ' var Result = 0;',
  25661. ' $mod.THelper.Fly.call({',
  25662. ' p: $mod.TObject.GetField(),',
  25663. ' get: function () {',
  25664. ' return this.p;',
  25665. ' },',
  25666. ' set: function (v) {',
  25667. ' this.p = v;',
  25668. ' }',
  25669. ' }, 1);',
  25670. ' $mod.THelper.Fly.call({',
  25671. ' p: $mod.TObject.GetField(),',
  25672. ' get: function () {',
  25673. ' return this.p;',
  25674. ' },',
  25675. ' set: function (v) {',
  25676. ' this.p = v;',
  25677. ' }',
  25678. ' }, 5);',
  25679. ' var $with = $mod.TObject;',
  25680. ' $mod.THelper.Fly.call({',
  25681. ' p: $with.GetField(),',
  25682. ' get: function () {',
  25683. ' return this.p;',
  25684. ' },',
  25685. ' set: function (v) {',
  25686. ' this.p = v;',
  25687. ' }',
  25688. ' }, 6);',
  25689. ' var $with1 = $mod.TObject.GetField();',
  25690. ' $mod.THelper.Fly.call({',
  25691. ' get: function () {',
  25692. ' return $with1;',
  25693. ' },',
  25694. ' set: function (v) {',
  25695. ' $with1 = v;',
  25696. ' }',
  25697. ' }, 7);',
  25698. ' return Result;',
  25699. ' };',
  25700. '});',
  25701. 'rtl.createHelper(this, "THelper", null, function () {',
  25702. ' this.Fly = function (n) {',
  25703. ' };',
  25704. '});',
  25705. 'this.o = null;',
  25706. '']),
  25707. LinesToStr([ // $mod.$main
  25708. '$mod.THelper.Fly.call({',
  25709. ' p: $mod.TObject.GetField(),',
  25710. ' get: function () {',
  25711. ' return this.p;',
  25712. ' },',
  25713. ' set: function (v) {',
  25714. ' this.p = v;',
  25715. ' }',
  25716. '}, 11);',
  25717. 'var $with = $mod.o;',
  25718. '$mod.THelper.Fly.call({',
  25719. ' p: $with.GetField(),',
  25720. ' get: function () {',
  25721. ' return this.p;',
  25722. ' },',
  25723. ' set: function (v) {',
  25724. ' this.p = v;',
  25725. ' }',
  25726. '}, 12);',
  25727. 'var $with1 = $mod.TObject.GetField();',
  25728. '$mod.THelper.Fly.call({',
  25729. ' get: function () {',
  25730. ' return $with1;',
  25731. ' },',
  25732. ' set: function (v) {',
  25733. ' $with1 = v;',
  25734. ' }',
  25735. '}, 13);',
  25736. '']));
  25737. end;
  25738. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25739. begin
  25740. StartProgram(false);
  25741. Add([
  25742. '{$modeswitch typehelpers}',
  25743. 'type',
  25744. ' TObject = class',
  25745. ' class var FField: word;',
  25746. ' class function GetField: word;',
  25747. ' class property Field: word read GetField write FField;',
  25748. ' end;',
  25749. ' TClass = class of TObject;',
  25750. ' THelper = type helper for word',
  25751. ' procedure Fly(n: byte);',
  25752. ' end;',
  25753. 'class function TObject.GetField: word;',
  25754. 'begin',
  25755. ' Field.Fly(1);',
  25756. ' Self.Field.Fly(5);',
  25757. ' with Self do Field.Fly(6);',
  25758. ' with Self.Field do Fly(7);',
  25759. 'end;',
  25760. 'procedure THelper.Fly(n: byte);',
  25761. 'begin',
  25762. 'end;',
  25763. 'var',
  25764. ' o: TObject;',
  25765. ' c: TClass;',
  25766. 'begin',
  25767. ' o.Field.Fly(11);',
  25768. ' with o do Field.Fly(12);',
  25769. ' with o.Field do Fly(13);',
  25770. ' c.Field.Fly(14);',
  25771. ' with c do Field.Fly(15);',
  25772. ' with c.Field do Fly(16);',
  25773. '']);
  25774. ConvertProgram;
  25775. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25776. LinesToStr([ // statements
  25777. 'rtl.createClass(this, "TObject", null, function () {',
  25778. ' this.FField = 0;',
  25779. ' this.$init = function () {',
  25780. ' };',
  25781. ' this.$final = function () {',
  25782. ' };',
  25783. ' this.GetField = function () {',
  25784. ' var Result = 0;',
  25785. ' $mod.THelper.Fly.call({',
  25786. ' p: this.GetField(),',
  25787. ' get: function () {',
  25788. ' return this.p;',
  25789. ' },',
  25790. ' set: function (v) {',
  25791. ' this.p = v;',
  25792. ' }',
  25793. ' }, 1);',
  25794. ' $mod.THelper.Fly.call({',
  25795. ' p: this.GetField(),',
  25796. ' get: function () {',
  25797. ' return this.p;',
  25798. ' },',
  25799. ' set: function (v) {',
  25800. ' this.p = v;',
  25801. ' }',
  25802. ' }, 5);',
  25803. ' $mod.THelper.Fly.call({',
  25804. ' p: this.GetField(),',
  25805. ' get: function () {',
  25806. ' return this.p;',
  25807. ' },',
  25808. ' set: function (v) {',
  25809. ' this.p = v;',
  25810. ' }',
  25811. ' }, 6);',
  25812. ' var $with = this.GetField();',
  25813. ' $mod.THelper.Fly.call({',
  25814. ' get: function () {',
  25815. ' return $with;',
  25816. ' },',
  25817. ' set: function (v) {',
  25818. ' $with = v;',
  25819. ' }',
  25820. ' }, 7);',
  25821. ' return Result;',
  25822. ' };',
  25823. '});',
  25824. 'rtl.createHelper(this, "THelper", null, function () {',
  25825. ' this.Fly = function (n) {',
  25826. ' };',
  25827. '});',
  25828. 'this.o = null;',
  25829. 'this.c = null;',
  25830. '']),
  25831. LinesToStr([ // $mod.$main
  25832. '$mod.THelper.Fly.call({',
  25833. ' p: $mod.o.$class.GetField(),',
  25834. ' get: function () {',
  25835. ' return this.p;',
  25836. ' },',
  25837. ' set: function (v) {',
  25838. ' this.p = v;',
  25839. ' }',
  25840. '}, 11);',
  25841. 'var $with = $mod.o;',
  25842. '$mod.THelper.Fly.call({',
  25843. ' p: $with.$class.GetField(),',
  25844. ' get: function () {',
  25845. ' return this.p;',
  25846. ' },',
  25847. ' set: function (v) {',
  25848. ' this.p = v;',
  25849. ' }',
  25850. '}, 12);',
  25851. 'var $with1 = $mod.o.$class.GetField();',
  25852. '$mod.THelper.Fly.call({',
  25853. ' get: function () {',
  25854. ' return $with1;',
  25855. ' },',
  25856. ' set: function (v) {',
  25857. ' $with1 = v;',
  25858. ' }',
  25859. '}, 13);',
  25860. '$mod.THelper.Fly.call({',
  25861. ' p: $mod.c.GetField(),',
  25862. ' get: function () {',
  25863. ' return this.p;',
  25864. ' },',
  25865. ' set: function (v) {',
  25866. ' this.p = v;',
  25867. ' }',
  25868. '}, 14);',
  25869. 'var $with2 = $mod.c;',
  25870. '$mod.THelper.Fly.call({',
  25871. ' p: $with2.GetField(),',
  25872. ' get: function () {',
  25873. ' return this.p;',
  25874. ' },',
  25875. ' set: function (v) {',
  25876. ' this.p = v;',
  25877. ' }',
  25878. '}, 15);',
  25879. 'var $with3 = $mod.c.GetField();',
  25880. '$mod.THelper.Fly.call({',
  25881. ' get: function () {',
  25882. ' return $with3;',
  25883. ' },',
  25884. ' set: function (v) {',
  25885. ' $with3 = v;',
  25886. ' }',
  25887. '}, 16);',
  25888. '']));
  25889. end;
  25890. procedure TTestModule.TestTypeHelper_Property;
  25891. begin
  25892. StartProgram(false);
  25893. Add([
  25894. '{$modeswitch typehelpers}',
  25895. 'type',
  25896. ' THelper = type helper for word',
  25897. ' function GetSize: longint;',
  25898. ' procedure SetSize(Value: longint);',
  25899. ' property Size: longint read GetSize write SetSize;',
  25900. ' end;',
  25901. 'function THelper.GetSize: longint;',
  25902. 'begin',
  25903. ' Result:=Size+1;',
  25904. ' Size:=2;',
  25905. ' Result:=Self.Size+3;',
  25906. ' Self.Size:=4;',
  25907. ' with Self do begin',
  25908. ' Result:=Size+5;',
  25909. ' Size:=6;',
  25910. ' end;',
  25911. 'end;',
  25912. 'procedure THelper.SetSize(Value: longint);',
  25913. 'begin',
  25914. 'end;',
  25915. 'var w: word;',
  25916. 'begin',
  25917. ' w:=w.Size+7;',
  25918. ' w.Size:=w+8;',
  25919. ' with w do begin',
  25920. ' w:=Size+9;',
  25921. ' Size:=w+10;',
  25922. ' end;',
  25923. '']);
  25924. ConvertProgram;
  25925. CheckSource('TestTypeHelper_Property',
  25926. LinesToStr([ // statements
  25927. 'rtl.createHelper(this, "THelper", null, function () {',
  25928. ' this.GetSize = function () {',
  25929. ' var Result = 0;',
  25930. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25931. ' $mod.THelper.SetSize.call(this, 2);',
  25932. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25933. ' $mod.THelper.SetSize.call(this, 4);',
  25934. ' var $with = this.get();',
  25935. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25936. ' $mod.THelper.SetSize.call(this, 6);',
  25937. ' return Result;',
  25938. ' };',
  25939. ' this.SetSize = function (Value) {',
  25940. ' };',
  25941. '});',
  25942. 'this.w = 0;',
  25943. '']),
  25944. LinesToStr([ // $mod.$main
  25945. '$mod.w = $mod.THelper.GetSize.call({',
  25946. ' p: $mod,',
  25947. ' get: function () {',
  25948. ' return this.p.w;',
  25949. ' },',
  25950. ' set: function (v) {',
  25951. ' this.p.w = v;',
  25952. ' }',
  25953. '}) + 7;',
  25954. '$mod.THelper.SetSize.call({',
  25955. ' p: $mod,',
  25956. ' get: function () {',
  25957. ' return this.p.w;',
  25958. ' },',
  25959. ' set: function (v) {',
  25960. ' this.p.w = v;',
  25961. ' }',
  25962. '}, $mod.w + 8);',
  25963. 'var $with = $mod.w;',
  25964. '$mod.w = $mod.THelper.GetSize.call({',
  25965. ' get: function () {',
  25966. ' return $with;',
  25967. ' },',
  25968. ' set: function (v) {',
  25969. ' $with = v;',
  25970. ' }',
  25971. '}) + 9;',
  25972. '$mod.THelper.SetSize.call({',
  25973. ' get: function () {',
  25974. ' return $with;',
  25975. ' },',
  25976. ' set: function (v) {',
  25977. ' $with = v;',
  25978. ' }',
  25979. '}, $mod.w + 10);',
  25980. '']));
  25981. end;
  25982. procedure TTestModule.TestTypeHelper_Property_Array;
  25983. begin
  25984. StartProgram(false);
  25985. Add([
  25986. '{$modeswitch typehelpers}',
  25987. 'type',
  25988. ' THelper = type helper for word',
  25989. ' function GetItems(Index: byte): boolean;',
  25990. ' procedure SetItems(Index: byte; Value: boolean);',
  25991. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25992. ' end;',
  25993. 'function THelper.GetItems(Index: byte): boolean;',
  25994. 'begin',
  25995. ' Result:=Items[1];',
  25996. ' Items[2]:=false;',
  25997. ' Result:=Self.Items[3];',
  25998. ' Self.Items[4]:=true;',
  25999. ' with Self do begin',
  26000. ' Result:=Items[5];',
  26001. ' Items[6]:=false;',
  26002. ' end;',
  26003. 'end;',
  26004. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26005. 'begin',
  26006. 'end;',
  26007. 'var',
  26008. ' w: word;',
  26009. ' b: boolean;',
  26010. 'begin',
  26011. ' b:=w.Items[1];',
  26012. ' w.Items[2]:=b;',
  26013. ' with w do begin',
  26014. ' b:=Items[3];',
  26015. ' Items[4]:=b;',
  26016. ' end;',
  26017. '']);
  26018. ConvertProgram;
  26019. CheckSource('TestTypeHelper_Property_Array',
  26020. LinesToStr([ // statements
  26021. 'rtl.createHelper(this, "THelper", null, function () {',
  26022. ' this.GetItems = function (Index) {',
  26023. ' var Result = false;',
  26024. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26025. ' $mod.THelper.SetItems.call(this, 2, false);',
  26026. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26027. ' $mod.THelper.SetItems.call(this, 4, true);',
  26028. ' var $with = this.get();',
  26029. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26030. ' $mod.THelper.SetItems.call(this, 6, false);',
  26031. ' return Result;',
  26032. ' };',
  26033. ' this.SetItems = function (Index, Value) {',
  26034. ' };',
  26035. '});',
  26036. 'this.w = 0;',
  26037. 'this.b = false;',
  26038. '']),
  26039. LinesToStr([ // $mod.$main
  26040. '$mod.b = $mod.THelper.GetItems.call({',
  26041. ' p: $mod,',
  26042. ' get: function () {',
  26043. ' return this.p.w;',
  26044. ' },',
  26045. ' set: function (v) {',
  26046. ' this.p.w = v;',
  26047. ' }',
  26048. '}, 1);',
  26049. '$mod.THelper.SetItems.call({',
  26050. ' p: $mod,',
  26051. ' get: function () {',
  26052. ' return this.p.w;',
  26053. ' },',
  26054. ' set: function (v) {',
  26055. ' this.p.w = v;',
  26056. ' }',
  26057. '}, 2, $mod.b);',
  26058. 'var $with = $mod.w;',
  26059. '$mod.b = $mod.THelper.GetItems.call({',
  26060. ' get: function () {',
  26061. ' return $with;',
  26062. ' },',
  26063. ' set: function (v) {',
  26064. ' $with = v;',
  26065. ' }',
  26066. '}, 3);',
  26067. '$mod.THelper.SetItems.call({',
  26068. ' get: function () {',
  26069. ' return $with;',
  26070. ' },',
  26071. ' set: function (v) {',
  26072. ' $with = v;',
  26073. ' }',
  26074. '}, 4, $mod.b);',
  26075. '']));
  26076. end;
  26077. procedure TTestModule.TestTypeHelper_ClassProperty;
  26078. begin
  26079. StartProgram(false);
  26080. Add([
  26081. '{$modeswitch typehelpers}',
  26082. 'type',
  26083. ' THelper = type helper for word',
  26084. ' class function GetSize: longint; static;',
  26085. ' class procedure SetSize(Value: longint); static;',
  26086. ' class property Size: longint read GetSize write SetSize;',
  26087. ' end;',
  26088. 'class function THelper.GetSize: longint;',
  26089. 'begin',
  26090. ' Result:=Size+1;',
  26091. ' Size:=2;',
  26092. 'end;',
  26093. 'class procedure THelper.SetSize(Value: longint);',
  26094. 'begin',
  26095. 'end;',
  26096. 'begin',
  26097. '']);
  26098. ConvertProgram;
  26099. CheckSource('TestTypeHelper_ClassProperty',
  26100. LinesToStr([ // statements
  26101. 'rtl.createHelper(this, "THelper", null, function () {',
  26102. ' this.GetSize = function () {',
  26103. ' var Result = 0;',
  26104. ' Result = $mod.THelper.GetSize() + 1;',
  26105. ' $mod.THelper.SetSize(2);',
  26106. ' return Result;',
  26107. ' };',
  26108. ' this.SetSize = function (Value) {',
  26109. ' };',
  26110. '});',
  26111. '']),
  26112. LinesToStr([ // $mod.$main
  26113. '']));
  26114. end;
  26115. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26116. begin
  26117. StartProgram(false);
  26118. Add([
  26119. '{$modeswitch typehelpers}',
  26120. 'type',
  26121. ' THelper = type helper for word',
  26122. ' class function GetItems(Index: byte): boolean; static;',
  26123. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26124. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26125. ' end;',
  26126. 'class function THelper.GetItems(Index: byte): boolean;',
  26127. 'begin',
  26128. ' Result:=Items[1];',
  26129. ' Items[2]:=false;',
  26130. 'end;',
  26131. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26132. 'begin',
  26133. 'end;',
  26134. 'var',
  26135. ' w: word;',
  26136. ' b: boolean;',
  26137. 'begin',
  26138. ' b:=w.Items[1];',
  26139. ' w.Items[2]:=b;',
  26140. ' with w do begin',
  26141. ' b:=Items[3];',
  26142. ' Items[4]:=b;',
  26143. ' end;',
  26144. '']);
  26145. ConvertProgram;
  26146. CheckSource('TestTypeHelper_ClassProperty_Array',
  26147. LinesToStr([ // statements
  26148. 'rtl.createHelper(this, "THelper", null, function () {',
  26149. ' this.GetItems = function (Index) {',
  26150. ' var Result = false;',
  26151. ' Result = $mod.THelper.GetItems(1);',
  26152. ' $mod.THelper.SetItems(2, false);',
  26153. ' return Result;',
  26154. ' };',
  26155. ' this.SetItems = function (Index, Value) {',
  26156. ' };',
  26157. '});',
  26158. 'this.w = 0;',
  26159. 'this.b = false;',
  26160. '']),
  26161. LinesToStr([ // $mod.$main
  26162. '$mod.b = $mod.THelper.GetItems(1);',
  26163. '$mod.THelper.SetItems(2, $mod.b);',
  26164. 'var $with = $mod.w;',
  26165. '$mod.b = $mod.THelper.GetItems(3);',
  26166. '$mod.THelper.SetItems(4, $mod.b);',
  26167. '']));
  26168. end;
  26169. procedure TTestModule.TestTypeHelper_ClassMethod;
  26170. begin
  26171. StartProgram(false);
  26172. Add([
  26173. '{$modeswitch typehelpers}',
  26174. 'type',
  26175. ' THelper = type helper for word',
  26176. ' class procedure DoStatic; static;',
  26177. ' end;',
  26178. 'class procedure THelper.DoStatic;',
  26179. 'begin',
  26180. ' DoStatic;',
  26181. ' DoStatic();',
  26182. 'end;',
  26183. 'var w: word;',
  26184. 'begin',
  26185. ' w.DoStatic;',
  26186. ' w.DoStatic();',
  26187. '']);
  26188. ConvertProgram;
  26189. CheckSource('TestTypeHelper_ClassMethod',
  26190. LinesToStr([ // statements
  26191. 'rtl.createHelper(this, "THelper", null, function () {',
  26192. ' this.DoStatic = function () {',
  26193. ' $mod.THelper.DoStatic();',
  26194. ' $mod.THelper.DoStatic();',
  26195. ' };',
  26196. '});',
  26197. 'this.w = 0;',
  26198. '']),
  26199. LinesToStr([ // $mod.$main
  26200. '$mod.THelper.DoStatic();',
  26201. '$mod.THelper.DoStatic();',
  26202. '']));
  26203. end;
  26204. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26205. begin
  26206. StartProgram(false);
  26207. Add([
  26208. '{$modeswitch typehelpers}',
  26209. 'type',
  26210. ' THelper = type helper for word',
  26211. ' procedure Run; external name ''Run'';',
  26212. ' end;',
  26213. 'var w: word;',
  26214. 'begin',
  26215. ' w.Run;',
  26216. '']);
  26217. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26218. ConvertProgram;
  26219. end;
  26220. procedure TTestModule.TestTypeHelper_Constructor;
  26221. begin
  26222. StartProgram(false);
  26223. Add([
  26224. '{$modeswitch typehelpers}',
  26225. 'type',
  26226. ' THelper = type helper for word',
  26227. ' constructor Init(e: longint);',
  26228. ' end;',
  26229. 'constructor THelper.Init(e: longint);',
  26230. 'begin',
  26231. ' Self:=e;',
  26232. ' Init(e+1);',
  26233. 'end;',
  26234. 'var w: word;',
  26235. 'begin',
  26236. ' w:=word.Init(2);',
  26237. ' w:=w.Init(3);',
  26238. ' with word do w:=Init(4);',
  26239. ' with w do w:=Init(5);',
  26240. '']);
  26241. ConvertProgram;
  26242. CheckSource('TestTypeHelper_Constructor',
  26243. LinesToStr([ // statements
  26244. 'rtl.createHelper(this, "THelper", null, function () {',
  26245. ' this.Init = function (e) {',
  26246. ' this.set(e);',
  26247. ' $mod.THelper.Init.call(this, e + 1);',
  26248. ' return this.get();',
  26249. ' };',
  26250. ' this.$new = function (fn, args) {',
  26251. ' return this[fn].apply({',
  26252. ' p: 0,',
  26253. ' get: function () {',
  26254. ' return this.p;',
  26255. ' },',
  26256. ' set: function (v) {',
  26257. ' this.p = v;',
  26258. ' }',
  26259. ' }, args);',
  26260. ' };',
  26261. '});',
  26262. 'this.w = 0;',
  26263. '']),
  26264. LinesToStr([ // $mod.$main
  26265. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26266. '$mod.w = $mod.THelper.Init.call({',
  26267. ' p: $mod,',
  26268. ' get: function () {',
  26269. ' return this.p.w;',
  26270. ' },',
  26271. ' set: function (v) {',
  26272. ' this.p.w = v;',
  26273. ' }',
  26274. '}, 3);',
  26275. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26276. 'var $with = $mod.w;',
  26277. '$mod.w = $mod.THelper.Init.call({',
  26278. ' get: function () {',
  26279. ' return $with;',
  26280. ' },',
  26281. ' set: function (v) {',
  26282. ' $with = v;',
  26283. ' }',
  26284. '}, 5);',
  26285. '']));
  26286. end;
  26287. procedure TTestModule.TestTypeHelper_Word;
  26288. begin
  26289. StartProgram(false);
  26290. Add([
  26291. '{$modeswitch typehelpers}',
  26292. 'type',
  26293. ' THelper = type helper for word',
  26294. ' procedure DoIt(e: byte = 123);',
  26295. ' end;',
  26296. 'procedure THelper.DoIt(e: byte);',
  26297. 'begin',
  26298. ' Self:=e;',
  26299. ' Self:=Self+1;',
  26300. ' with Self do Doit;',
  26301. 'end;',
  26302. 'begin',
  26303. ' word(3).DoIt;',
  26304. '']);
  26305. ConvertProgram;
  26306. CheckSource('TestTypeHelper_Word',
  26307. LinesToStr([ // statements
  26308. 'rtl.createHelper(this, "THelper", null, function () {',
  26309. ' this.DoIt = function (e) {',
  26310. ' this.set(e);',
  26311. ' this.set(this.get() + 1);',
  26312. ' var $with = this.get();',
  26313. ' $mod.THelper.DoIt.call(this, 123);',
  26314. ' };',
  26315. '});',
  26316. '']),
  26317. LinesToStr([ // $mod.$main
  26318. '$mod.THelper.DoIt.call({',
  26319. ' get: function () {',
  26320. ' return 3;',
  26321. ' },',
  26322. ' set: function (v) {',
  26323. ' rtl.raiseE("EPropReadOnly");',
  26324. ' }',
  26325. '}, 123);',
  26326. '']));
  26327. end;
  26328. procedure TTestModule.TestTypeHelper_Boolean;
  26329. begin
  26330. StartProgram(false);
  26331. Add([
  26332. '{$modeswitch typehelpers}',
  26333. 'type',
  26334. ' Integer = longint;',
  26335. ' THelper = type helper for boolean',
  26336. ' procedure Run(e: wordbool = true);',
  26337. ' end;',
  26338. 'procedure THelper.Run(e: wordbool);',
  26339. 'begin',
  26340. ' Self:=e;',
  26341. ' Self:=not Self;',
  26342. ' with Self do Run;',
  26343. ' if Integer(Self)=0 then ;',
  26344. 'end;',
  26345. 'begin',
  26346. ' boolean(3).Run;',
  26347. '']);
  26348. ConvertProgram;
  26349. CheckSource('TestTypeHelper_Boolean',
  26350. LinesToStr([ // statements
  26351. 'rtl.createHelper(this, "THelper", null, function () {',
  26352. ' this.Run = function (e) {',
  26353. ' this.set(e);',
  26354. ' this.set(!this.get());',
  26355. ' var $with = this.get();',
  26356. ' $mod.THelper.Run.call(this, true);',
  26357. ' if ((this.get() ? 1 : 0) === 0) ;',
  26358. ' };',
  26359. '});',
  26360. '']),
  26361. LinesToStr([ // $mod.$main
  26362. '$mod.THelper.Run.call({',
  26363. ' a: 3 != 0,',
  26364. ' get: function () {',
  26365. ' return this.a;',
  26366. ' },',
  26367. ' set: function (v) {',
  26368. ' rtl.raiseE("EPropReadOnly");',
  26369. ' }',
  26370. '}, true);',
  26371. '']));
  26372. end;
  26373. procedure TTestModule.TestTypeHelper_WordBool;
  26374. begin
  26375. StartProgram(false);
  26376. Add([
  26377. '{$modeswitch typehelpers}',
  26378. 'type',
  26379. ' Integer = longint;',
  26380. ' THelper = type helper for WordBool',
  26381. ' procedure Run(e: wordbool = true);',
  26382. ' end;',
  26383. 'procedure THelper.Run(e: wordbool);',
  26384. 'var i: integer;',
  26385. 'begin',
  26386. ' i:=Integer(Self);',
  26387. 'end;',
  26388. 'var w: wordbool;',
  26389. 'begin',
  26390. ' w.Run;',
  26391. ' wordbool(3).Run;',
  26392. '']);
  26393. ConvertProgram;
  26394. CheckSource('TestTypeHelper_WordBool',
  26395. LinesToStr([ // statements
  26396. 'rtl.createHelper(this, "THelper", null, function () {',
  26397. ' this.Run = function (e) {',
  26398. ' var i = 0;',
  26399. ' i = (this.get() ? 1 : 0);',
  26400. ' };',
  26401. '});',
  26402. 'this.w = false;',
  26403. '']),
  26404. LinesToStr([ // $mod.$main
  26405. '$mod.THelper.Run.call({',
  26406. ' p: $mod,',
  26407. ' get: function () {',
  26408. ' return this.p.w;',
  26409. ' },',
  26410. ' set: function (v) {',
  26411. ' this.p.w = v;',
  26412. ' }',
  26413. '}, true);',
  26414. '$mod.THelper.Run.call({',
  26415. ' a: 3 != 0,',
  26416. ' get: function () {',
  26417. ' return this.a;',
  26418. ' },',
  26419. ' set: function (v) {',
  26420. ' rtl.raiseE("EPropReadOnly");',
  26421. ' }',
  26422. '}, true);',
  26423. '']));
  26424. end;
  26425. procedure TTestModule.TestTypeHelper_Double;
  26426. begin
  26427. StartProgram(false);
  26428. Add([
  26429. '{$modeswitch typehelpers}',
  26430. 'type',
  26431. ' Float = type double;',
  26432. ' THelper = type helper for Float',
  26433. ' const NPI = 3.141592;',
  26434. ' function ToStr: String;',
  26435. ' end;',
  26436. 'function THelper.ToStr: String;',
  26437. 'begin',
  26438. 'end;',
  26439. 'procedure DoIt(s: string);',
  26440. 'begin',
  26441. 'end;',
  26442. 'var f: Float;',
  26443. 'begin',
  26444. ' DoIt(f.toStr);',
  26445. ' DoIt(f.toStr());',
  26446. ' (f*f).toStr;',
  26447. ' DoIt((f*f).toStr);',
  26448. '']);
  26449. ConvertProgram;
  26450. CheckSource('TestTypeHelper_Double',
  26451. LinesToStr([ // statements
  26452. 'rtl.createHelper(this, "THelper", null, function () {',
  26453. ' this.NPI = 3.141592;',
  26454. ' this.ToStr = function () {',
  26455. ' var Result = "";',
  26456. ' return Result;',
  26457. ' };',
  26458. '});',
  26459. 'this.DoIt = function (s) {',
  26460. '};',
  26461. 'this.f = 0.0;',
  26462. '']),
  26463. LinesToStr([ // $mod.$main
  26464. '$mod.DoIt($mod.THelper.ToStr.call({',
  26465. ' p: $mod,',
  26466. ' get: function () {',
  26467. ' return this.p.f;',
  26468. ' },',
  26469. ' set: function (v) {',
  26470. ' this.p.f = v;',
  26471. ' }',
  26472. '}));',
  26473. '$mod.DoIt($mod.THelper.ToStr.call({',
  26474. ' p: $mod,',
  26475. ' get: function () {',
  26476. ' return this.p.f;',
  26477. ' },',
  26478. ' set: function (v) {',
  26479. ' this.p.f = v;',
  26480. ' }',
  26481. '}));',
  26482. '$mod.THelper.ToStr.call({',
  26483. ' a: $mod.f * $mod.f,',
  26484. ' get: function () {',
  26485. ' return this.a;',
  26486. ' },',
  26487. ' set: function (v) {',
  26488. ' rtl.raiseE("EPropReadOnly");',
  26489. ' }',
  26490. '});',
  26491. '$mod.DoIt($mod.THelper.ToStr.call({',
  26492. ' a: $mod.f * $mod.f,',
  26493. ' get: function () {',
  26494. ' return this.a;',
  26495. ' },',
  26496. ' set: function (v) {',
  26497. ' rtl.raiseE("EPropReadOnly");',
  26498. ' }',
  26499. '}));',
  26500. '']));
  26501. end;
  26502. procedure TTestModule.TestTypeHelper_NativeInt;
  26503. begin
  26504. StartProgram(false);
  26505. Add([
  26506. '{$modeswitch typehelpers}',
  26507. 'type',
  26508. ' MaxInt = type nativeint;',
  26509. ' THelperI = type helper for MaxInt',
  26510. ' function ToStr: String;',
  26511. ' end;',
  26512. ' MaxUInt = type nativeuint;',
  26513. ' THelperU = type helper for MaxUInt',
  26514. ' function ToStr: String;',
  26515. ' end;',
  26516. 'function THelperI.ToStr: String;',
  26517. 'begin',
  26518. ' Result:=str(Self);',
  26519. 'end;',
  26520. 'function THelperU.ToStr: String;',
  26521. 'begin',
  26522. ' Result:=str(Self);',
  26523. 'end;',
  26524. 'procedure DoIt(s: string);',
  26525. 'begin',
  26526. 'end;',
  26527. 'var i: MaxInt;',
  26528. 'begin',
  26529. ' DoIt(i.toStr);',
  26530. ' DoIt(i.toStr());',
  26531. ' (i*i).toStr;',
  26532. ' DoIt((i*i).toStr);',
  26533. '']);
  26534. ConvertProgram;
  26535. CheckSource('TestTypeHelper_NativeInt',
  26536. LinesToStr([ // statements
  26537. 'rtl.createHelper(this, "THelperI", null, function () {',
  26538. ' this.ToStr = function () {',
  26539. ' var Result = "";',
  26540. ' Result = "" + this.get();',
  26541. ' return Result;',
  26542. ' };',
  26543. '});',
  26544. 'rtl.createHelper(this, "THelperU", null, function () {',
  26545. ' this.ToStr = function () {',
  26546. ' var Result = "";',
  26547. ' Result = "" + this.get();',
  26548. ' return Result;',
  26549. ' };',
  26550. '});',
  26551. 'this.DoIt = function (s) {',
  26552. '};',
  26553. 'this.i = 0;',
  26554. '']),
  26555. LinesToStr([ // $mod.$main
  26556. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26557. ' p: $mod,',
  26558. ' get: function () {',
  26559. ' return this.p.i;',
  26560. ' },',
  26561. ' set: function (v) {',
  26562. ' this.p.i = v;',
  26563. ' }',
  26564. '}));',
  26565. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26566. ' p: $mod,',
  26567. ' get: function () {',
  26568. ' return this.p.i;',
  26569. ' },',
  26570. ' set: function (v) {',
  26571. ' this.p.i = v;',
  26572. ' }',
  26573. '}));',
  26574. '$mod.THelperI.ToStr.call({',
  26575. ' a: $mod.i * $mod.i,',
  26576. ' get: function () {',
  26577. ' return this.a;',
  26578. ' },',
  26579. ' set: function (v) {',
  26580. ' rtl.raiseE("EPropReadOnly");',
  26581. ' }',
  26582. '});',
  26583. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26584. ' a: $mod.i * $mod.i,',
  26585. ' get: function () {',
  26586. ' return this.a;',
  26587. ' },',
  26588. ' set: function (v) {',
  26589. ' rtl.raiseE("EPropReadOnly");',
  26590. ' }',
  26591. '}));',
  26592. '']));
  26593. end;
  26594. procedure TTestModule.TestTypeHelper_StringChar;
  26595. begin
  26596. StartProgram(false);
  26597. Add([
  26598. '{$modeswitch typehelpers}',
  26599. 'type',
  26600. ' TStringHelper = type helper for string',
  26601. ' procedure DoIt(e: byte = 123);',
  26602. ' end;',
  26603. ' TCharHelper = type helper for char',
  26604. ' procedure Fly;',
  26605. ' end;',
  26606. 'procedure TStringHelper.DoIt(e: byte);',
  26607. 'begin',
  26608. ' Self[1]:=''c'';',
  26609. ' Self[2]:=Self[3];',
  26610. 'end;',
  26611. 'procedure TCharHelper.Fly;',
  26612. 'begin',
  26613. ' Self:=''c'';',
  26614. 'end;',
  26615. 'begin',
  26616. ' ''abc''.DoIt;',
  26617. ' ''xyz''.DoIt();',
  26618. ' ''c''.Fly();',
  26619. '']);
  26620. ConvertProgram;
  26621. CheckSource('TestTypeHelper_StringChar',
  26622. LinesToStr([ // statements
  26623. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26624. ' this.DoIt = function (e) {',
  26625. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26626. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26627. ' };',
  26628. '});',
  26629. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26630. ' this.Fly = function () {',
  26631. ' this.set("c");',
  26632. ' };',
  26633. '});',
  26634. '']),
  26635. LinesToStr([ // $mod.$main
  26636. '$mod.TStringHelper.DoIt.call({',
  26637. ' get: function () {',
  26638. ' return "abc";',
  26639. ' },',
  26640. ' set: function (v) {',
  26641. ' rtl.raiseE("EPropReadOnly");',
  26642. ' }',
  26643. '}, 123);',
  26644. '$mod.TStringHelper.DoIt.call({',
  26645. ' get: function () {',
  26646. ' return "xyz";',
  26647. ' },',
  26648. ' set: function (v) {',
  26649. ' rtl.raiseE("EPropReadOnly");',
  26650. ' }',
  26651. '}, 123);',
  26652. '$mod.TCharHelper.Fly.call({',
  26653. ' get: function () {',
  26654. ' return "c";',
  26655. ' },',
  26656. ' set: function (v) {',
  26657. ' rtl.raiseE("EPropReadOnly");',
  26658. ' }',
  26659. '});',
  26660. '']));
  26661. end;
  26662. procedure TTestModule.TestTypeHelper_JSValue;
  26663. begin
  26664. StartProgram(false);
  26665. Add([
  26666. '{$modeswitch typehelpers}',
  26667. 'type',
  26668. ' TExtValue = type jsvalue;',
  26669. ' THelper = type helper for TExtValue',
  26670. ' function ToStr: String;',
  26671. ' end;',
  26672. 'function THelper.ToStr: String;',
  26673. 'begin',
  26674. 'end;',
  26675. 'var',
  26676. ' s: string;',
  26677. ' v: TExtValue;',
  26678. 'begin',
  26679. ' s:=v.toStr;',
  26680. ' s:=v.toStr();',
  26681. ' TExtValue(s).toStr;',
  26682. '']);
  26683. ConvertProgram;
  26684. CheckSource('TestTypeHelper_JSValue',
  26685. LinesToStr([ // statements
  26686. 'rtl.createHelper(this, "THelper", null, function () {',
  26687. ' this.ToStr = function () {',
  26688. ' var Result = "";',
  26689. ' return Result;',
  26690. ' };',
  26691. '});',
  26692. 'this.s = "";',
  26693. 'this.v = undefined;',
  26694. '']),
  26695. LinesToStr([ // $mod.$main
  26696. '$mod.s = $mod.THelper.ToStr.call({',
  26697. ' p: $mod,',
  26698. ' get: function () {',
  26699. ' return this.p.v;',
  26700. ' },',
  26701. ' set: function (v) {',
  26702. ' this.p.v = v;',
  26703. ' }',
  26704. '});',
  26705. '$mod.s = $mod.THelper.ToStr.call({',
  26706. ' p: $mod,',
  26707. ' get: function () {',
  26708. ' return this.p.v;',
  26709. ' },',
  26710. ' set: function (v) {',
  26711. ' this.p.v = v;',
  26712. ' }',
  26713. '});',
  26714. '$mod.THelper.ToStr.call({',
  26715. ' p: $mod,',
  26716. ' get: function () {',
  26717. ' return this.p.s;',
  26718. ' },',
  26719. ' set: function (v) {',
  26720. ' rtl.raiseE("EPropReadOnly");',
  26721. ' }',
  26722. '});',
  26723. '']));
  26724. end;
  26725. procedure TTestModule.TestTypeHelper_Array;
  26726. begin
  26727. StartProgram(false);
  26728. Add([
  26729. '{$modeswitch typehelpers}',
  26730. 'type',
  26731. ' TArrOfBool = array of boolean;',
  26732. ' TArrOfJS = array of jsvalue;',
  26733. ' THelper = type helper for TArrOfBool',
  26734. ' procedure DoIt(e: byte = 123);',
  26735. ' end;',
  26736. 'procedure THelper.DoIt(e: byte);',
  26737. 'begin',
  26738. ' Self[1]:=true;',
  26739. ' Self[2]:=not Self[3];',
  26740. ' SetLength(Self,4);',
  26741. ' Self:=Concat(Self,[true]);',
  26742. 'end;',
  26743. 'var',
  26744. ' b: TArrOfBool;',
  26745. ' j: TArrOfJS;',
  26746. 'begin',
  26747. ' b.DoIt;',
  26748. ' TArrOfBool(j).DoIt();',
  26749. '']);
  26750. ConvertProgram;
  26751. CheckSource('TestTypeHelper_Array',
  26752. LinesToStr([ // statements
  26753. 'rtl.createHelper(this, "THelper", null, function () {',
  26754. ' this.DoIt = function (e) {',
  26755. ' this.get()[1] = true;',
  26756. ' this.get()[2] = !this.get()[3];',
  26757. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26758. ' this.set(rtl.arrayPushN(this.get(), true));',
  26759. ' };',
  26760. '});',
  26761. 'this.b = [];',
  26762. 'this.j = [];',
  26763. '']),
  26764. LinesToStr([ // $mod.$main
  26765. '$mod.THelper.DoIt.call({',
  26766. ' p: $mod,',
  26767. ' get: function () {',
  26768. ' return this.p.b;',
  26769. ' },',
  26770. ' set: function (v) {',
  26771. ' this.p.b = v;',
  26772. ' }',
  26773. '}, 123);',
  26774. '$mod.THelper.DoIt.call({',
  26775. ' p: $mod,',
  26776. ' get: function () {',
  26777. ' return this.p.j;',
  26778. ' },',
  26779. ' set: function (v) {',
  26780. ' this.p.j = v;',
  26781. ' }',
  26782. '}, 123);',
  26783. '']));
  26784. end;
  26785. procedure TTestModule.TestTypeHelper_EnumType;
  26786. begin
  26787. StartProgram(false);
  26788. Add([
  26789. '{$modeswitch typehelpers}',
  26790. 'type',
  26791. ' TEnum = (red,blue);',
  26792. ' THelper = type helper for TEnum',
  26793. ' procedure DoIt(e: byte = 123);',
  26794. ' class procedure Swing(w: word); static;',
  26795. ' end;',
  26796. 'procedure THelper.DoIt(e: byte);',
  26797. 'begin',
  26798. ' Self:=red;',
  26799. ' Self:=succ(Self);',
  26800. ' with Self do Doit;',
  26801. 'end;',
  26802. 'class procedure THelper.Swing(w: word);',
  26803. 'begin',
  26804. 'end;',
  26805. 'var e: TEnum;',
  26806. 'begin',
  26807. ' e.DoIt;',
  26808. ' red.DoIt;',
  26809. ' TEnum.blue.DoIt;',
  26810. ' TEnum(1).DoIt;',
  26811. ' TEnum.Swing(3);',
  26812. '']);
  26813. ConvertProgram;
  26814. CheckSource('TestTypeHelper_EnumType',
  26815. LinesToStr([ // statements
  26816. 'this.TEnum = {',
  26817. ' "0": "red",',
  26818. ' red: 0,',
  26819. ' "1": "blue",',
  26820. ' blue: 1',
  26821. '};',
  26822. 'rtl.createHelper(this, "THelper", null, function () {',
  26823. ' this.DoIt = function (e) {',
  26824. ' this.set($mod.TEnum.red);',
  26825. ' this.set(this.get() + 1);',
  26826. ' var $with = this.get();',
  26827. ' $mod.THelper.DoIt.call(this, 123);',
  26828. ' };',
  26829. ' this.Swing = function (w) {',
  26830. ' };',
  26831. '});',
  26832. 'this.e = 0;',
  26833. '']),
  26834. LinesToStr([ // $mod.$main
  26835. '$mod.THelper.DoIt.call({',
  26836. ' p: $mod,',
  26837. ' get: function () {',
  26838. ' return this.p.e;',
  26839. ' },',
  26840. ' set: function (v) {',
  26841. ' this.p.e = v;',
  26842. ' }',
  26843. '}, 123);',
  26844. '$mod.THelper.DoIt.call({',
  26845. ' p: $mod.TEnum,',
  26846. ' get: function () {',
  26847. ' return this.p.red;',
  26848. ' },',
  26849. ' set: function (v) {',
  26850. ' rtl.raiseE("EPropReadOnly");',
  26851. ' }',
  26852. '}, 123);',
  26853. '$mod.THelper.DoIt.call({',
  26854. ' p: $mod.TEnum,',
  26855. ' get: function () {',
  26856. ' return this.p.blue;',
  26857. ' },',
  26858. ' set: function (v) {',
  26859. ' rtl.raiseE("EPropReadOnly");',
  26860. ' }',
  26861. '}, 123);',
  26862. '$mod.THelper.DoIt.call({',
  26863. ' get: function () {',
  26864. ' return 1;',
  26865. ' },',
  26866. ' set: function (v) {',
  26867. ' rtl.raiseE("EPropReadOnly");',
  26868. ' }',
  26869. '}, 123);',
  26870. '$mod.THelper.Swing(3);',
  26871. '']));
  26872. end;
  26873. procedure TTestModule.TestTypeHelper_SetType;
  26874. begin
  26875. StartProgram(false);
  26876. Add([
  26877. '{$modeswitch typehelpers}',
  26878. 'type',
  26879. ' TEnum = (red,blue);',
  26880. ' TSetOfEnum = set of TEnum;',
  26881. ' THelper = type helper for TSetOfEnum',
  26882. ' procedure DoIt(e: byte = 123);',
  26883. ' constructor Init(e: TEnum);',
  26884. ' constructor InitEmpty;',
  26885. ' end;',
  26886. 'procedure THelper.DoIt(e: byte);',
  26887. 'begin',
  26888. ' Self:=[];',
  26889. ' Self:=[red];',
  26890. ' Include(Self,blue);',
  26891. 'end;',
  26892. 'constructor THelper.Init(e: TEnum);',
  26893. 'begin',
  26894. ' Self:=[];',
  26895. ' Self:=[e];',
  26896. ' Include(Self,blue);',
  26897. 'end;',
  26898. 'constructor THelper.InitEmpty;',
  26899. 'begin',
  26900. 'end;',
  26901. 'var s: TSetOfEnum;',
  26902. 'begin',
  26903. ' s.DoIt;',
  26904. //' [red].DoIt;',
  26905. //' with s do DoIt;',
  26906. //' with [red,blue] do DoIt;',
  26907. ' s:=TSetOfEnum.Init(blue);',
  26908. ' s:=s.Init(blue);',
  26909. '']);
  26910. ConvertProgram;
  26911. CheckSource('TestTypeHelper_SetType',
  26912. LinesToStr([ // statements
  26913. 'this.TEnum = {',
  26914. ' "0": "red",',
  26915. ' red: 0,',
  26916. ' "1": "blue",',
  26917. ' blue: 1',
  26918. '};',
  26919. 'rtl.createHelper(this, "THelper", null, function () {',
  26920. ' this.DoIt = function (e) {',
  26921. ' this.set({});',
  26922. ' this.set(rtl.createSet($mod.TEnum.red));',
  26923. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26924. ' };',
  26925. ' this.Init = function (e) {',
  26926. ' this.set({});',
  26927. ' this.set(rtl.createSet(e));',
  26928. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26929. ' return this.get();',
  26930. ' };',
  26931. ' this.InitEmpty = function () {',
  26932. ' return this.get();',
  26933. ' };',
  26934. ' this.$new = function (fn, args) {',
  26935. ' return this[fn].apply({',
  26936. ' p: {},',
  26937. ' get: function () {',
  26938. ' return this.p;',
  26939. ' },',
  26940. ' set: function (v) {',
  26941. ' this.p = v;',
  26942. ' }',
  26943. ' }, args);',
  26944. ' };',
  26945. '});',
  26946. 'this.s = {};',
  26947. '']),
  26948. LinesToStr([ // $mod.$main
  26949. '$mod.THelper.DoIt.call({',
  26950. ' p: $mod,',
  26951. ' get: function () {',
  26952. ' return this.p.s;',
  26953. ' },',
  26954. ' set: function (v) {',
  26955. ' this.p.s = v;',
  26956. ' }',
  26957. '}, 123);',
  26958. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26959. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26960. ' p: $mod,',
  26961. ' get: function () {',
  26962. ' return this.p.s;',
  26963. ' },',
  26964. ' set: function (v) {',
  26965. ' this.p.s = v;',
  26966. ' }',
  26967. '}, $mod.TEnum.blue));',
  26968. '']));
  26969. end;
  26970. procedure TTestModule.TestTypeHelper_InterfaceType;
  26971. begin
  26972. StartProgram(false);
  26973. Add([
  26974. '{$interfaces com}',
  26975. '{$modeswitch typehelpers}',
  26976. 'type',
  26977. ' IUnknown = interface',
  26978. ' function _AddRef: longint;',
  26979. ' function _Release: longint;',
  26980. ' end;',
  26981. ' TObject = class(IUnknown)',
  26982. ' function _AddRef: longint; virtual; abstract;',
  26983. ' function _Release: longint; virtual; abstract;',
  26984. ' end;',
  26985. ' THelper = type helper for IUnknown',
  26986. ' procedure Fly(e: byte = 123);',
  26987. ' class procedure Run; static;',
  26988. ' end;',
  26989. 'var',
  26990. ' i: IUnknown;',
  26991. ' o: TObject;',
  26992. 'procedure THelper.Fly(e: byte);',
  26993. 'begin',
  26994. ' i:=Self;',
  26995. ' o:=Self as TObject;',
  26996. ' Self:=nil;',
  26997. ' Self:=i;',
  26998. ' Self:=o;',
  26999. ' with Self do begin',
  27000. ' Fly;',
  27001. ' Fly();',
  27002. ' end;',
  27003. 'end;',
  27004. 'class procedure THelper.Run;',
  27005. 'var l: IUnknown;',
  27006. 'begin',
  27007. ' l.Fly;',
  27008. ' l.Fly();',
  27009. 'end;',
  27010. 'begin',
  27011. ' i.Fly;',
  27012. ' i.Fly();',
  27013. ' i.Run;',
  27014. ' i.Run();',
  27015. ' IUnknown.Run;',
  27016. ' IUnknown.Run();',
  27017. '']);
  27018. ConvertProgram;
  27019. CheckSource('TestTypeHelper_InterfaceType',
  27020. LinesToStr([ // statements
  27021. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27022. 'rtl.createClass(this, "TObject", null, function () {',
  27023. ' this.$init = function () {',
  27024. ' };',
  27025. ' this.$final = function () {',
  27026. ' };',
  27027. ' rtl.addIntf(this, $mod.IUnknown);',
  27028. '});',
  27029. 'rtl.createHelper(this, "THelper", null, function () {',
  27030. ' this.Fly = function (e) {',
  27031. ' var $ir = rtl.createIntfRefs();',
  27032. ' try {',
  27033. ' rtl.setIntfP($mod, "i", this.get());',
  27034. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27035. ' this.set(null);',
  27036. ' this.set($mod.i);',
  27037. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27038. ' var $with = this.get();',
  27039. ' $mod.THelper.Fly.call(this, 123);',
  27040. ' $mod.THelper.Fly.call(this, 123);',
  27041. ' } finally {',
  27042. ' $ir.free();',
  27043. ' };',
  27044. ' };',
  27045. ' this.Run = function () {',
  27046. ' var l = null;',
  27047. ' try {',
  27048. ' $mod.THelper.Fly.call({',
  27049. ' get: function () {',
  27050. ' return l;',
  27051. ' },',
  27052. ' set: function (v) {',
  27053. ' l = rtl.setIntfL(l, v);',
  27054. ' }',
  27055. ' }, 123);',
  27056. ' $mod.THelper.Fly.call({',
  27057. ' get: function () {',
  27058. ' return l;',
  27059. ' },',
  27060. ' set: function (v) {',
  27061. ' l = rtl.setIntfL(l, v);',
  27062. ' }',
  27063. ' }, 123);',
  27064. ' } finally {',
  27065. ' rtl._Release(l);',
  27066. ' };',
  27067. ' };',
  27068. '});',
  27069. 'this.i = null;',
  27070. 'this.o = null;',
  27071. '']),
  27072. LinesToStr([ // $mod.$main
  27073. '$mod.THelper.Fly.call({',
  27074. ' p: $mod,',
  27075. ' get: function () {',
  27076. ' return this.p.i;',
  27077. ' },',
  27078. ' set: function (v) {',
  27079. ' rtl.setIntfP(this.p, "i", v);',
  27080. ' }',
  27081. '}, 123);',
  27082. '$mod.THelper.Fly.call({',
  27083. ' p: $mod,',
  27084. ' get: function () {',
  27085. ' return this.p.i;',
  27086. ' },',
  27087. ' set: function (v) {',
  27088. ' rtl.setIntfP(this.p, "i", v);',
  27089. ' }',
  27090. '}, 123);',
  27091. '$mod.THelper.Run();',
  27092. '$mod.THelper.Run();',
  27093. '$mod.THelper.Run();',
  27094. '$mod.THelper.Run();',
  27095. '']));
  27096. end;
  27097. procedure TTestModule.TestTypeHelper_NestedSelf;
  27098. begin
  27099. StartProgram(false);
  27100. Add([
  27101. '{$modeswitch typehelpers}',
  27102. 'type',
  27103. ' THelper = type helper for string',
  27104. ' procedure Run(Value: string);',
  27105. ' end;',
  27106. 'procedure THelper.Run(Value: string);',
  27107. ' function Sub(i: nativeint): boolean;',
  27108. ' begin',
  27109. ' Result:=Self[i+1]=Value[i];',
  27110. ' end;',
  27111. 'begin',
  27112. ' if Self[3]=Value[4] then ;',
  27113. 'end;',
  27114. 'begin',
  27115. '']);
  27116. ConvertProgram;
  27117. CheckSource('TestTypeHelper_NestedSelf',
  27118. LinesToStr([ // statements
  27119. 'rtl.createHelper(this, "THelper", null, function () {',
  27120. ' this.Run = function (Value) {',
  27121. ' var $Self = this;',
  27122. ' function Sub(i) {',
  27123. ' var Result = false;',
  27124. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27125. ' return Result;',
  27126. ' };',
  27127. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27128. ' };',
  27129. '});',
  27130. '']),
  27131. LinesToStr([ // $mod.$main
  27132. '']));
  27133. end;
  27134. procedure TTestModule.TestProcType;
  27135. begin
  27136. StartProgram(false);
  27137. Add([
  27138. 'type',
  27139. ' TProcInt = procedure(vI: longint = 1);',
  27140. 'procedure DoIt(vJ: longint);',
  27141. 'begin end;',
  27142. 'var',
  27143. ' b: boolean;',
  27144. ' vP, vQ: tprocint;',
  27145. 'begin',
  27146. ' vp:=nil;',
  27147. ' vp:=vp;',
  27148. ' vp:=@doit;',
  27149. ' vp;',
  27150. ' vp();',
  27151. ' vp(2);',
  27152. ' b:=vp=nil;',
  27153. ' b:=nil=vp;',
  27154. ' b:=vp=vq;',
  27155. ' b:=vp=@doit;',
  27156. ' b:=@doit=vp;',
  27157. ' b:=vp<>nil;',
  27158. ' b:=nil<>vp;',
  27159. ' b:=vp<>vq;',
  27160. ' b:=vp<>@doit;',
  27161. ' b:=@doit<>vp;',
  27162. ' b:=Assigned(vp);',
  27163. ' if Assigned(vp) then ;']);
  27164. ConvertProgram;
  27165. CheckSource('TestProcType',
  27166. LinesToStr([ // statements
  27167. 'this.DoIt = function(vJ) {',
  27168. '};',
  27169. 'this.b = false;',
  27170. 'this.vP = null;',
  27171. 'this.vQ = null;'
  27172. ]),
  27173. LinesToStr([ // $mod.$main
  27174. '$mod.vP = null;',
  27175. '$mod.vP = $mod.vP;',
  27176. '$mod.vP = $mod.DoIt;',
  27177. '$mod.vP(1);',
  27178. '$mod.vP(1);',
  27179. '$mod.vP(2);',
  27180. '$mod.b = $mod.vP === null;',
  27181. '$mod.b = null === $mod.vP;',
  27182. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27183. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27184. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27185. '$mod.b = $mod.vP !== null;',
  27186. '$mod.b = null !== $mod.vP;',
  27187. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27188. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27189. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27190. '$mod.b = $mod.vP != null;',
  27191. 'if ($mod.vP != null) ;',
  27192. '']));
  27193. end;
  27194. procedure TTestModule.TestProcType_Arg;
  27195. begin
  27196. StartProgram(false);
  27197. Add([
  27198. 'type',
  27199. ' TProcInt = procedure(vI: longint = 1);',
  27200. 'procedure DoIt(vJ: longint); begin end;',
  27201. 'procedure DoSome(vP, vQ: TProcInt);',
  27202. 'var',
  27203. ' b: boolean;',
  27204. 'begin',
  27205. ' vp:=nil;',
  27206. ' vp:=vp;',
  27207. ' vp:=@doit;',
  27208. ' vp;',
  27209. ' vp();',
  27210. ' vp(2);',
  27211. ' b:=vp=nil;',
  27212. ' b:=nil=vp;',
  27213. ' b:=vp=vq;',
  27214. ' b:=vp=@doit;',
  27215. ' b:=@doit=vp;',
  27216. ' b:=vp<>nil;',
  27217. ' b:=nil<>vp;',
  27218. ' b:=vp<>vq;',
  27219. ' b:=vp<>@doit;',
  27220. ' b:=@doit<>vp;',
  27221. ' b:=Assigned(vp);',
  27222. ' if Assigned(vp) then ;',
  27223. 'end;',
  27224. 'begin',
  27225. ' DoSome(@DoIt,nil);']);
  27226. ConvertProgram;
  27227. CheckSource('TestProcType_Arg',
  27228. LinesToStr([ // statements
  27229. 'this.DoIt = function(vJ) {',
  27230. '};',
  27231. 'this.DoSome = function(vP, vQ) {',
  27232. ' var b = false;',
  27233. ' vP = null;',
  27234. ' vP = vP;',
  27235. ' vP = $mod.DoIt;',
  27236. ' vP(1);',
  27237. ' vP(1);',
  27238. ' vP(2);',
  27239. ' b = vP === null;',
  27240. ' b = null === vP;',
  27241. ' b = rtl.eqCallback(vP,vQ);',
  27242. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27243. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27244. ' b = vP !== null;',
  27245. ' b = null !== vP;',
  27246. ' b = !rtl.eqCallback(vP, vQ);',
  27247. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27248. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27249. ' b = vP != null;',
  27250. ' if (vP != null) ;',
  27251. '};',
  27252. '']),
  27253. LinesToStr([ // $mod.$main
  27254. '$mod.DoSome($mod.DoIt,null);',
  27255. '']));
  27256. end;
  27257. procedure TTestModule.TestProcType_FunctionFPC;
  27258. begin
  27259. StartProgram(false);
  27260. Add('type');
  27261. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27262. Add('function DoIt(vI: longint): longint;');
  27263. Add('begin end;');
  27264. Add('var');
  27265. Add(' b: boolean;');
  27266. Add(' vP, vQ: tfuncint;');
  27267. Add('begin');
  27268. Add(' vp:=nil;');
  27269. Add(' vp:=vp;');
  27270. Add(' vp:=@doit;'); // ok in fpc and delphi
  27271. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27272. Add(' vp;'); // ok in fpc and delphi
  27273. Add(' vp();');
  27274. Add(' vp(2);');
  27275. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27276. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27277. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27278. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27279. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27280. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27281. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27282. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27283. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27284. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27285. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27286. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27287. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27288. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27289. Add(' b:=Assigned(vp);');
  27290. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27291. Add(' doit(vp());'); // ok in fpc and delphi
  27292. Add(' doit(vp(2));'); // ok in fpc and delphi
  27293. ConvertProgram;
  27294. CheckSource('TestProcType_FunctionFPC',
  27295. LinesToStr([ // statements
  27296. 'this.DoIt = function(vI) {',
  27297. ' var Result = 0;',
  27298. ' return Result;',
  27299. '};',
  27300. 'this.b = false;',
  27301. 'this.vP = null;',
  27302. 'this.vQ = null;'
  27303. ]),
  27304. LinesToStr([ // $mod.$main
  27305. '$mod.vP = null;',
  27306. '$mod.vP = $mod.vP;',
  27307. '$mod.vP = $mod.DoIt;',
  27308. '$mod.vP(1);',
  27309. '$mod.vP(1);',
  27310. '$mod.vP(2);',
  27311. '$mod.b = $mod.vP === null;',
  27312. '$mod.b = null === $mod.vP;',
  27313. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27314. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27315. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27316. '$mod.b = 4 === $mod.vP(1);',
  27317. '$mod.b = $mod.vP !== null;',
  27318. '$mod.b = null !== $mod.vP;',
  27319. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27320. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27321. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27322. '$mod.b = 6 !== $mod.vP(1);',
  27323. '$mod.b = $mod.vP != null;',
  27324. '$mod.DoIt($mod.vP(1));',
  27325. '$mod.DoIt($mod.vP(2));',
  27326. '']));
  27327. end;
  27328. procedure TTestModule.TestProcType_FunctionDelphi;
  27329. begin
  27330. StartProgram(false);
  27331. Add('{$mode Delphi}');
  27332. Add('type');
  27333. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27334. Add('function DoIt(vI: longint): longint;');
  27335. Add('begin end;');
  27336. Add('var');
  27337. Add(' b: boolean;');
  27338. Add(' vP, vQ: tfuncint;');
  27339. Add('begin');
  27340. Add(' vp:=nil;');
  27341. Add(' vp:=vp;');
  27342. Add(' vp:=@doit;'); // ok in fpc and delphi
  27343. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27344. Add(' vp;'); // ok in fpc and delphi
  27345. Add(' vp();');
  27346. Add(' vp(2);');
  27347. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27348. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27349. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27350. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27351. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27352. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27353. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27354. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27355. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27356. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27357. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27358. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27359. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27360. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27361. Add(' b:=Assigned(vp);');
  27362. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27363. Add(' doit(vp());'); // ok in fpc and delphi
  27364. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27365. ConvertProgram;
  27366. CheckSource('TestProcType_FunctionDelphi',
  27367. LinesToStr([ // statements
  27368. 'this.DoIt = function(vI) {',
  27369. ' var Result = 0;',
  27370. ' return Result;',
  27371. '};',
  27372. 'this.b = false;',
  27373. 'this.vP = null;',
  27374. 'this.vQ = null;'
  27375. ]),
  27376. LinesToStr([ // $mod.$main
  27377. '$mod.vP = null;',
  27378. '$mod.vP = $mod.vP;',
  27379. '$mod.vP = $mod.DoIt;',
  27380. '$mod.vP = $mod.DoIt;',
  27381. '$mod.vP(1);',
  27382. '$mod.vP(1);',
  27383. '$mod.vP(2);',
  27384. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27385. '$mod.b = $mod.vP(1) === 3;',
  27386. '$mod.b = 4 === $mod.vP(1);',
  27387. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27388. '$mod.b = $mod.vP(1) !== 5;',
  27389. '$mod.b = 6 !== $mod.vP(1);',
  27390. '$mod.b = $mod.vP != null;',
  27391. '$mod.DoIt($mod.vP(1));',
  27392. '$mod.DoIt($mod.vP(1));',
  27393. '$mod.DoIt($mod.vP(2));',
  27394. '']));
  27395. end;
  27396. procedure TTestModule.TestProcType_ProcedureDelphi;
  27397. begin
  27398. StartProgram(false);
  27399. Add('{$mode Delphi}');
  27400. Add('type');
  27401. Add(' TProc = procedure;');
  27402. Add('procedure DoIt;');
  27403. Add('begin end;');
  27404. Add('var');
  27405. Add(' b: boolean;');
  27406. Add(' vP, vQ: tproc;');
  27407. Add('begin');
  27408. Add(' vp:=nil;');
  27409. Add(' vp:=vp;');
  27410. Add(' vp:=vq;');
  27411. 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
  27412. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27413. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27414. Add(' vp;'); // ok in fpc and delphi
  27415. Add(' vp();');
  27416. // equal
  27417. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27418. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27419. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27420. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27421. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27422. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27423. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27424. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27425. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27426. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27427. // unequal
  27428. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27429. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27430. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27431. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27432. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27433. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27434. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27435. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27436. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27437. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27438. Add(' b:=Assigned(vp);');
  27439. ConvertProgram;
  27440. CheckSource('TestProcType_ProcedureDelphi',
  27441. LinesToStr([ // statements
  27442. 'this.DoIt = function() {',
  27443. '};',
  27444. 'this.b = false;',
  27445. 'this.vP = null;',
  27446. 'this.vQ = null;'
  27447. ]),
  27448. LinesToStr([ // $mod.$main
  27449. '$mod.vP = null;',
  27450. '$mod.vP = $mod.vP;',
  27451. '$mod.vP = $mod.vQ;',
  27452. '$mod.vP = $mod.DoIt;',
  27453. '$mod.vP = $mod.DoIt;',
  27454. '$mod.vP();',
  27455. '$mod.vP();',
  27456. '$mod.b = $mod.vP === null;',
  27457. '$mod.b = null === $mod.vP;',
  27458. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27459. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27460. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27461. '$mod.b = $mod.vP !== null;',
  27462. '$mod.b = null !== $mod.vP;',
  27463. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27464. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27465. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27466. '$mod.b = $mod.vP != null;',
  27467. '']));
  27468. end;
  27469. procedure TTestModule.TestProcType_AsParam;
  27470. begin
  27471. StartProgram(false);
  27472. Add('type');
  27473. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27474. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27475. Add('var vJ: tfuncint;');
  27476. Add('begin');
  27477. Add(' vg:=vg;');
  27478. Add(' vj:=vh;');
  27479. Add(' vi:=vi;');
  27480. Add(' doit(vg,vg,vg);');
  27481. Add(' doit(vh,vh,vj);');
  27482. Add(' doit(vi,vi,vi);');
  27483. Add(' doit(vj,vj,vj);');
  27484. Add('end;');
  27485. Add('var i: tfuncint;');
  27486. Add('begin');
  27487. Add(' doit(i,i,i);');
  27488. ConvertProgram;
  27489. CheckSource('TestProcType_AsParam',
  27490. LinesToStr([ // statements
  27491. 'this.DoIt = function (vG,vH,vI) {',
  27492. ' var vJ = null;',
  27493. ' vG = vG;',
  27494. ' vJ = vH;',
  27495. ' vI.set(vI.get());',
  27496. ' $mod.DoIt(vG, vG, {',
  27497. ' get: function () {',
  27498. ' return vG;',
  27499. ' },',
  27500. ' set: function (v) {',
  27501. ' vG = v;',
  27502. ' }',
  27503. ' });',
  27504. ' $mod.DoIt(vH, vH, {',
  27505. ' get: function () {',
  27506. ' return vJ;',
  27507. ' },',
  27508. ' set: function (v) {',
  27509. ' vJ = v;',
  27510. ' }',
  27511. ' });',
  27512. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27513. ' $mod.DoIt(vJ, vJ, {',
  27514. ' get: function () {',
  27515. ' return vJ;',
  27516. ' },',
  27517. ' set: function (v) {',
  27518. ' vJ = v;',
  27519. ' }',
  27520. ' });',
  27521. '};',
  27522. 'this.i = null;'
  27523. ]),
  27524. LinesToStr([
  27525. '$mod.DoIt($mod.i,$mod.i,{',
  27526. ' p: $mod,',
  27527. ' get: function () {',
  27528. ' return this.p.i;',
  27529. ' },',
  27530. ' set: function (v) {',
  27531. ' this.p.i = v;',
  27532. ' }',
  27533. '});'
  27534. ]));
  27535. end;
  27536. procedure TTestModule.TestProcType_MethodFPC;
  27537. begin
  27538. StartProgram(false);
  27539. Add('type');
  27540. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27541. Add(' TObject = class');
  27542. Add(' function DoIt(vA: longint = 1): longint;');
  27543. Add(' end;');
  27544. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27545. Add('begin');
  27546. Add('end;');
  27547. Add('var');
  27548. Add(' Obj: TObject;');
  27549. Add(' vP: tfuncint;');
  27550. Add(' b: boolean;');
  27551. Add('begin');
  27552. Add(' vp:[email protected];'); // ok in fpc and delphi
  27553. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27554. Add(' vp;'); // ok in fpc and delphi
  27555. Add(' vp();');
  27556. Add(' vp(2);');
  27557. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27558. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27559. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27560. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27561. ConvertProgram;
  27562. CheckSource('TestProcType_MethodFPC',
  27563. LinesToStr([ // statements
  27564. 'rtl.createClass(this, "TObject", null, function () {',
  27565. ' this.$init = function () {',
  27566. ' };',
  27567. ' this.$final = function () {',
  27568. ' };',
  27569. ' this.DoIt = function (vA) {',
  27570. ' var Result = 0;',
  27571. ' return Result;',
  27572. ' };',
  27573. '});',
  27574. 'this.Obj = null;',
  27575. 'this.vP = null;',
  27576. 'this.b = false;'
  27577. ]),
  27578. LinesToStr([
  27579. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27580. '$mod.vP(1);',
  27581. '$mod.vP(1);',
  27582. '$mod.vP(2);',
  27583. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27584. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27585. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27586. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27587. '']));
  27588. end;
  27589. procedure TTestModule.TestProcType_MethodDelphi;
  27590. begin
  27591. StartProgram(false);
  27592. Add([
  27593. '{$mode delphi}',
  27594. 'type',
  27595. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27596. ' TObject = class',
  27597. ' function DoIt(vA: longint = 1): longint;',
  27598. ' end;',
  27599. 'function TObject.DoIt(vA: longint = 1): longint;',
  27600. 'begin',
  27601. 'end;',
  27602. 'var',
  27603. ' Obj: TObject;',
  27604. ' vP: tfuncint;',
  27605. ' b: boolean;',
  27606. 'begin',
  27607. ' vp:[email protected];', // ok in fpc and delphi
  27608. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27609. ' vp;', // ok in fpc and delphi
  27610. ' vp();',
  27611. ' vp(2);',
  27612. //' b:[email protected];', // ok in fpc, illegal in delphi
  27613. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27614. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27615. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27616. '']);
  27617. ConvertProgram;
  27618. CheckSource('TestProcType_MethodDelphi',
  27619. LinesToStr([ // statements
  27620. 'rtl.createClass(this, "TObject", null, function () {',
  27621. ' this.$init = function () {',
  27622. ' };',
  27623. ' this.$final = function () {',
  27624. ' };',
  27625. ' this.DoIt = function (vA) {',
  27626. ' var Result = 0;',
  27627. ' return Result;',
  27628. ' };',
  27629. '});',
  27630. 'this.Obj = null;',
  27631. 'this.vP = null;',
  27632. 'this.b = false;'
  27633. ]),
  27634. LinesToStr([
  27635. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27636. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27637. '$mod.vP(1);',
  27638. '$mod.vP(1);',
  27639. '$mod.vP(2);',
  27640. '']));
  27641. end;
  27642. procedure TTestModule.TestProcType_PropertyFPC;
  27643. begin
  27644. StartProgram(false);
  27645. Add('type');
  27646. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27647. Add(' TObject = class');
  27648. Add(' FOnFoo: TFuncInt;');
  27649. Add(' function DoIt(vA: longint = 1): longint;');
  27650. Add(' function GetFoo: TFuncInt;');
  27651. Add(' procedure SetFoo(const Value: TFuncInt);');
  27652. Add(' function GetEvents(Index: longint): TFuncInt;');
  27653. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27654. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27655. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27656. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27657. Add(' end;');
  27658. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27659. Add('function tobject.getfoo: tfuncint; begin end;');
  27660. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27661. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27662. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27663. Add('var');
  27664. Add(' Obj: TObject;');
  27665. Add(' vP: tfuncint;');
  27666. Add(' b: boolean;');
  27667. Add('begin');
  27668. Add(' obj.onfoo:=nil;');
  27669. Add(' obj.onbar:=nil;');
  27670. Add(' obj.events[1]:=nil;');
  27671. Add(' obj.onfoo:=obj.onfoo;');
  27672. Add(' obj.onbar:=obj.onbar;');
  27673. Add(' obj.events[2]:=obj.events[3];');
  27674. Add(' obj.onfoo:[email protected];');
  27675. Add(' obj.onbar:[email protected];');
  27676. Add(' obj.events[4]:[email protected];');
  27677. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27678. //Add(' obj.onbar:=obj.doit;'); // delphi
  27679. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27680. Add(' obj.onfoo;');
  27681. Add(' obj.onbar;');
  27682. //Add(' obj.events[5];'); ToDo in pasresolver
  27683. Add(' obj.onfoo();');
  27684. Add(' obj.onbar();');
  27685. Add(' obj.events[6]();');
  27686. Add(' b:=obj.onfoo=nil;');
  27687. Add(' b:=obj.onbar=nil;');
  27688. Add(' b:=obj.events[7]=nil;');
  27689. Add(' b:=obj.onfoo<>nil;');
  27690. Add(' b:=obj.onbar<>nil;');
  27691. Add(' b:=obj.events[8]<>nil;');
  27692. Add(' b:=obj.onfoo=vp;');
  27693. Add(' b:=obj.onbar=vp;');
  27694. Add(' b:=obj.events[9]=vp;');
  27695. Add(' b:=obj.onfoo=obj.onfoo;');
  27696. Add(' b:=obj.onbar=obj.onfoo;');
  27697. Add(' b:=obj.events[10]=obj.onfoo;');
  27698. Add(' b:=obj.onfoo<>obj.onfoo;');
  27699. Add(' b:=obj.onbar<>obj.onfoo;');
  27700. Add(' b:=obj.events[11]<>obj.onfoo;');
  27701. Add(' b:[email protected];');
  27702. Add(' b:[email protected];');
  27703. Add(' b:=obj.events[12][email protected];');
  27704. Add(' b:=obj.onfoo<>@obj.doit;');
  27705. Add(' b:=obj.onbar<>@obj.doit;');
  27706. Add(' b:=obj.events[12]<>@obj.doit;');
  27707. Add(' b:=Assigned(obj.onfoo);');
  27708. Add(' b:=Assigned(obj.onbar);');
  27709. Add(' b:=Assigned(obj.events[13]);');
  27710. ConvertProgram;
  27711. CheckSource('TestProcType_PropertyFPC',
  27712. LinesToStr([ // statements
  27713. 'rtl.createClass(this, "TObject", null, function () {',
  27714. ' this.$init = function () {',
  27715. ' this.FOnFoo = null;',
  27716. ' };',
  27717. ' this.$final = function () {',
  27718. ' this.FOnFoo = undefined;',
  27719. ' };',
  27720. ' this.DoIt = function (vA) {',
  27721. ' var Result = 0;',
  27722. ' return Result;',
  27723. ' };',
  27724. 'this.GetFoo = function () {',
  27725. ' var Result = null;',
  27726. ' return Result;',
  27727. '};',
  27728. 'this.SetFoo = function (Value) {',
  27729. '};',
  27730. 'this.GetEvents = function (Index) {',
  27731. ' var Result = null;',
  27732. ' return Result;',
  27733. '};',
  27734. 'this.SetEvents = function (Index, Value) {',
  27735. '};',
  27736. '});',
  27737. 'this.Obj = null;',
  27738. 'this.vP = null;',
  27739. 'this.b = false;'
  27740. ]),
  27741. LinesToStr([
  27742. '$mod.Obj.FOnFoo = null;',
  27743. '$mod.Obj.SetFoo(null);',
  27744. '$mod.Obj.SetEvents(1, null);',
  27745. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27746. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27747. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27748. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27749. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27750. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27751. '$mod.Obj.FOnFoo(1);',
  27752. '$mod.Obj.GetFoo();',
  27753. '$mod.Obj.FOnFoo(1);',
  27754. '$mod.Obj.GetFoo()(1);',
  27755. '$mod.Obj.GetEvents(6)(1);',
  27756. '$mod.b = $mod.Obj.FOnFoo === null;',
  27757. '$mod.b = $mod.Obj.GetFoo() === null;',
  27758. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27759. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27760. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27761. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27762. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27763. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27764. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27765. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27766. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27767. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27768. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27769. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27770. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27771. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27772. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27773. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27774. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27775. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27776. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27777. '$mod.b = $mod.Obj.FOnFoo != null;',
  27778. '$mod.b = $mod.Obj.GetFoo() != null;',
  27779. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27780. '']));
  27781. end;
  27782. procedure TTestModule.TestProcType_PropertyDelphi;
  27783. begin
  27784. StartProgram(false);
  27785. Add('{$mode delphi}');
  27786. Add('type');
  27787. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27788. Add(' TObject = class');
  27789. Add(' FOnFoo: TFuncInt;');
  27790. Add(' function DoIt(vA: longint = 1): longint;');
  27791. Add(' function GetFoo: TFuncInt;');
  27792. Add(' procedure SetFoo(const Value: TFuncInt);');
  27793. Add(' function GetEvents(Index: longint): TFuncInt;');
  27794. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27795. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27796. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27797. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27798. Add(' end;');
  27799. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27800. Add('function tobject.getfoo: tfuncint; begin end;');
  27801. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27802. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27803. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27804. Add('var');
  27805. Add(' Obj: TObject;');
  27806. Add(' vP: tfuncint;');
  27807. Add(' b: boolean;');
  27808. Add('begin');
  27809. Add(' obj.onfoo:=nil;');
  27810. Add(' obj.onbar:=nil;');
  27811. Add(' obj.events[1]:=nil;');
  27812. Add(' obj.onfoo:=obj.onfoo;');
  27813. Add(' obj.onbar:=obj.onbar;');
  27814. Add(' obj.events[2]:=obj.events[3];');
  27815. Add(' obj.onfoo:[email protected];');
  27816. Add(' obj.onbar:[email protected];');
  27817. Add(' obj.events[4]:[email protected];');
  27818. Add(' obj.onfoo:=obj.doit;'); // delphi
  27819. Add(' obj.onbar:=obj.doit;'); // delphi
  27820. Add(' obj.events[4]:=obj.doit;'); // delphi
  27821. Add(' obj.onfoo;');
  27822. Add(' obj.onbar;');
  27823. //Add(' obj.events[5];'); ToDo in pasresolver
  27824. Add(' obj.onfoo();');
  27825. Add(' obj.onbar();');
  27826. Add(' obj.events[6]();');
  27827. //Add(' b:=obj.onfoo=nil;'); // fpc
  27828. //Add(' b:=obj.onbar=nil;'); // fpc
  27829. //Add(' b:=obj.events[7]=nil;'); // fpc
  27830. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27831. //Add(' b:=obj.onbar<>nil;'); // fpc
  27832. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27833. Add(' b:=obj.onfoo=vp;');
  27834. Add(' b:=obj.onbar=vp;');
  27835. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27836. Add(' b:=obj.onfoo=obj.onfoo;');
  27837. Add(' b:=obj.onbar=obj.onfoo;');
  27838. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27839. Add(' b:=obj.onfoo<>obj.onfoo;');
  27840. Add(' b:=obj.onbar<>obj.onfoo;');
  27841. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27842. //Add(' b:[email protected];'); // fpc
  27843. //Add(' b:[email protected];'); // fpc
  27844. //Add(' b:=obj.events[12][email protected];'); // fpc
  27845. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27846. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27847. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27848. Add(' b:=Assigned(obj.onfoo);');
  27849. Add(' b:=Assigned(obj.onbar);');
  27850. Add(' b:=Assigned(obj.events[13]);');
  27851. ConvertProgram;
  27852. CheckSource('TestProcType_PropertyDelphi',
  27853. LinesToStr([ // statements
  27854. 'rtl.createClass(this, "TObject", null, function () {',
  27855. ' this.$init = function () {',
  27856. ' this.FOnFoo = null;',
  27857. ' };',
  27858. ' this.$final = function () {',
  27859. ' this.FOnFoo = undefined;',
  27860. ' };',
  27861. ' this.DoIt = function (vA) {',
  27862. ' var Result = 0;',
  27863. ' return Result;',
  27864. ' };',
  27865. 'this.GetFoo = function () {',
  27866. ' var Result = null;',
  27867. ' return Result;',
  27868. '};',
  27869. 'this.SetFoo = function (Value) {',
  27870. '};',
  27871. 'this.GetEvents = function (Index) {',
  27872. ' var Result = null;',
  27873. ' return Result;',
  27874. '};',
  27875. 'this.SetEvents = function (Index, Value) {',
  27876. '};',
  27877. '});',
  27878. 'this.Obj = null;',
  27879. 'this.vP = null;',
  27880. 'this.b = false;'
  27881. ]),
  27882. LinesToStr([
  27883. '$mod.Obj.FOnFoo = null;',
  27884. '$mod.Obj.SetFoo(null);',
  27885. '$mod.Obj.SetEvents(1, null);',
  27886. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27887. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27888. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27889. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27890. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27891. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27892. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27893. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27894. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27895. '$mod.Obj.FOnFoo(1);',
  27896. '$mod.Obj.GetFoo();',
  27897. '$mod.Obj.FOnFoo(1);',
  27898. '$mod.Obj.GetFoo()(1);',
  27899. '$mod.Obj.GetEvents(6)(1);',
  27900. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27901. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27902. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27903. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27904. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27905. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27906. '$mod.b = $mod.Obj.FOnFoo != null;',
  27907. '$mod.b = $mod.Obj.GetFoo() != null;',
  27908. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27909. '']));
  27910. end;
  27911. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27912. begin
  27913. StartProgram(false);
  27914. Add('type');
  27915. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27916. Add(' TObject = class');
  27917. Add(' FOnFoo: TFuncInt;');
  27918. Add(' function DoIt(vA: longint = 1): longint;');
  27919. Add(' function GetFoo: TFuncInt;');
  27920. Add(' procedure SetFoo(const Value: TFuncInt);');
  27921. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27922. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27923. Add(' end;');
  27924. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27925. Add('function tobject.getfoo: tfuncint; begin end;');
  27926. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27927. Add('var');
  27928. Add(' Obj: TObject;');
  27929. Add(' vP: tfuncint;');
  27930. Add(' b: boolean;');
  27931. Add('begin');
  27932. Add('with obj do begin');
  27933. Add(' fonfoo:=nil;');
  27934. Add(' onfoo:=nil;');
  27935. Add(' onbar:=nil;');
  27936. Add(' fonfoo:=fonfoo;');
  27937. Add(' onfoo:=onfoo;');
  27938. Add(' onbar:=onbar;');
  27939. Add(' fonfoo:=@doit;');
  27940. Add(' onfoo:=@doit;');
  27941. Add(' onbar:=@doit;');
  27942. //Add(' fonfoo:=doit;'); // delphi
  27943. //Add(' onfoo:=doit;'); // delphi
  27944. //Add(' onbar:=doit;'); // delphi
  27945. Add(' fonfoo;');
  27946. Add(' onfoo;');
  27947. Add(' onbar;');
  27948. Add(' fonfoo();');
  27949. Add(' onfoo();');
  27950. Add(' onbar();');
  27951. Add(' b:=fonfoo=nil;');
  27952. Add(' b:=onfoo=nil;');
  27953. Add(' b:=onbar=nil;');
  27954. Add(' b:=fonfoo<>nil;');
  27955. Add(' b:=onfoo<>nil;');
  27956. Add(' b:=onbar<>nil;');
  27957. Add(' b:=fonfoo=vp;');
  27958. Add(' b:=onfoo=vp;');
  27959. Add(' b:=onbar=vp;');
  27960. Add(' b:=fonfoo=fonfoo;');
  27961. Add(' b:=onfoo=onfoo;');
  27962. Add(' b:=onbar=onfoo;');
  27963. Add(' b:=fonfoo<>fonfoo;');
  27964. Add(' b:=onfoo<>onfoo;');
  27965. Add(' b:=onbar<>onfoo;');
  27966. Add(' b:=fonfoo=@doit;');
  27967. Add(' b:=onfoo=@doit;');
  27968. Add(' b:=onbar=@doit;');
  27969. Add(' b:=fonfoo<>@doit;');
  27970. Add(' b:=onfoo<>@doit;');
  27971. Add(' b:=onbar<>@doit;');
  27972. Add(' b:=Assigned(fonfoo);');
  27973. Add(' b:=Assigned(onfoo);');
  27974. Add(' b:=Assigned(onbar);');
  27975. Add('end;');
  27976. ConvertProgram;
  27977. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27978. LinesToStr([ // statements
  27979. 'rtl.createClass(this, "TObject", null, function () {',
  27980. ' this.$init = function () {',
  27981. ' this.FOnFoo = null;',
  27982. ' };',
  27983. ' this.$final = function () {',
  27984. ' this.FOnFoo = undefined;',
  27985. ' };',
  27986. ' this.DoIt = function (vA) {',
  27987. ' var Result = 0;',
  27988. ' return Result;',
  27989. ' };',
  27990. ' this.GetFoo = function () {',
  27991. ' var Result = null;',
  27992. ' return Result;',
  27993. ' };',
  27994. ' this.SetFoo = function (Value) {',
  27995. ' };',
  27996. '});',
  27997. 'this.Obj = null;',
  27998. 'this.vP = null;',
  27999. 'this.b = false;'
  28000. ]),
  28001. LinesToStr([
  28002. 'var $with = $mod.Obj;',
  28003. '$with.FOnFoo = null;',
  28004. '$with.FOnFoo = null;',
  28005. '$with.SetFoo(null);',
  28006. '$with.FOnFoo = $with.FOnFoo;',
  28007. '$with.FOnFoo = $with.FOnFoo;',
  28008. '$with.SetFoo($with.GetFoo());',
  28009. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28010. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28011. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28012. '$with.FOnFoo(1);',
  28013. '$with.FOnFoo(1);',
  28014. '$with.GetFoo();',
  28015. '$with.FOnFoo(1);',
  28016. '$with.FOnFoo(1);',
  28017. '$with.GetFoo()(1);',
  28018. '$mod.b = $with.FOnFoo === null;',
  28019. '$mod.b = $with.FOnFoo === null;',
  28020. '$mod.b = $with.GetFoo() === null;',
  28021. '$mod.b = $with.FOnFoo !== null;',
  28022. '$mod.b = $with.FOnFoo !== null;',
  28023. '$mod.b = $with.GetFoo() !== null;',
  28024. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28025. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28026. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28027. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28028. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28029. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28030. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28031. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28032. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28033. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28034. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28035. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28036. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28037. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28038. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28039. '$mod.b = $with.FOnFoo != null;',
  28040. '$mod.b = $with.FOnFoo != null;',
  28041. '$mod.b = $with.GetFoo() != null;',
  28042. '']));
  28043. end;
  28044. procedure TTestModule.TestProcType_Nested;
  28045. begin
  28046. StartProgram(false);
  28047. Add([
  28048. 'type',
  28049. ' TProcInt = procedure(vI: longint = 1);',
  28050. 'procedure DoIt(vJ: longint);',
  28051. 'var aProc: TProcInt;',
  28052. ' b: boolean;',
  28053. ' procedure Sub(vK: longint);',
  28054. ' var aSub: TProcInt;',
  28055. ' procedure SubSub(vK: longint);',
  28056. ' var aSubSub: TProcInt;',
  28057. ' begin;',
  28058. ' aProc:=@DoIt;',
  28059. ' aSub:=@DoIt;',
  28060. ' aSubSub:=@DoIt;',
  28061. ' aProc:=@Sub;',
  28062. ' aSub:=@Sub;',
  28063. ' aSubSub:=@Sub;',
  28064. ' aProc:=@SubSub;',
  28065. ' aSub:=@SubSub;',
  28066. ' aSubSub:=@SubSub;',
  28067. ' end;',
  28068. ' begin;',
  28069. ' end;',
  28070. 'begin;',
  28071. ' aProc:=@Sub;',
  28072. ' b:=aProc=@Sub;',
  28073. ' b:=@Sub=aProc;',
  28074. 'end;',
  28075. 'begin',
  28076. '']);
  28077. ConvertProgram;
  28078. CheckSource('TestProcType_Nested',
  28079. LinesToStr([ // statements
  28080. 'this.DoIt = function (vJ) {',
  28081. ' var aProc = null;',
  28082. ' var b = false;',
  28083. ' function Sub(vK) {',
  28084. ' var aSub = null;',
  28085. ' function SubSub(vK) {',
  28086. ' var aSubSub = null;',
  28087. ' aProc = $mod.DoIt;',
  28088. ' aSub = $mod.DoIt;',
  28089. ' aSubSub = $mod.DoIt;',
  28090. ' aProc = Sub;',
  28091. ' aSub = Sub;',
  28092. ' aSubSub = Sub;',
  28093. ' aProc = SubSub;',
  28094. ' aSub = SubSub;',
  28095. ' aSubSub = SubSub;',
  28096. ' };',
  28097. ' };',
  28098. ' aProc = Sub;',
  28099. ' b = rtl.eqCallback(aProc, Sub);',
  28100. ' b = rtl.eqCallback(Sub, aProc);',
  28101. '};',
  28102. '']),
  28103. LinesToStr([ // $mod.$main
  28104. '']));
  28105. end;
  28106. procedure TTestModule.TestProcType_NestedOfObject;
  28107. begin
  28108. StartProgram(false);
  28109. Add([
  28110. 'type',
  28111. ' TProcInt = procedure(vI: longint = 1) of object;',
  28112. ' TObject = class',
  28113. ' procedure DoIt(vJ: longint);',
  28114. ' end;',
  28115. 'procedure TObject.DoIt(vJ: longint);',
  28116. 'var aProc: TProcInt;',
  28117. ' b: boolean;',
  28118. ' procedure Sub(vK: longint);',
  28119. ' var aSub: TProcInt;',
  28120. ' procedure SubSub(vK: longint);',
  28121. ' var aSubSub: TProcInt;',
  28122. ' begin;',
  28123. ' aProc:=@DoIt;',
  28124. ' aSub:=@DoIt;',
  28125. ' aSubSub:=@DoIt;',
  28126. ' aProc:=@Sub;',
  28127. ' aSub:=@Sub;',
  28128. ' aSubSub:=@Sub;',
  28129. ' aProc:=@SubSub;',
  28130. ' aSub:=@SubSub;',
  28131. ' aSubSub:=@SubSub;',
  28132. ' end;',
  28133. ' begin;',
  28134. ' end;',
  28135. 'begin;',
  28136. ' aProc:=@Sub;',
  28137. ' b:=aProc=@Sub;',
  28138. ' b:=@Sub=aProc;',
  28139. 'end;',
  28140. 'begin',
  28141. '']);
  28142. ConvertProgram;
  28143. CheckSource('TestProcType_Nested',
  28144. LinesToStr([ // statements
  28145. 'rtl.createClass(this, "TObject", null, function () {',
  28146. ' this.$init = function () {',
  28147. ' };',
  28148. ' this.$final = function () {',
  28149. ' };',
  28150. ' this.DoIt = function (vJ) {',
  28151. ' var $Self = this;',
  28152. ' var aProc = null;',
  28153. ' var b = false;',
  28154. ' function Sub(vK) {',
  28155. ' var aSub = null;',
  28156. ' function SubSub(vK) {',
  28157. ' var aSubSub = null;',
  28158. ' aProc = rtl.createCallback($Self, "DoIt");',
  28159. ' aSub = rtl.createCallback($Self, "DoIt");',
  28160. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28161. ' aProc = Sub;',
  28162. ' aSub = Sub;',
  28163. ' aSubSub = Sub;',
  28164. ' aProc = SubSub;',
  28165. ' aSub = SubSub;',
  28166. ' aSubSub = SubSub;',
  28167. ' };',
  28168. ' };',
  28169. ' aProc = Sub;',
  28170. ' b = rtl.eqCallback(aProc, Sub);',
  28171. ' b = rtl.eqCallback(Sub, aProc);',
  28172. ' };',
  28173. '});',
  28174. '']),
  28175. LinesToStr([ // $mod.$main
  28176. '']));
  28177. end;
  28178. procedure TTestModule.TestProcType_ReferenceToProc;
  28179. begin
  28180. StartProgram(false);
  28181. Add([
  28182. 'type',
  28183. ' TProcRef = reference to procedure(i: longint = 0);',
  28184. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28185. 'var',
  28186. ' p: TProcRef;',
  28187. ' f: TFuncRef;',
  28188. 'procedure DoIt(i: longint);',
  28189. 'begin',
  28190. 'end;',
  28191. 'function GetIt(i: longint): longint;',
  28192. 'begin',
  28193. ' p:=@DoIt;',
  28194. ' f:=@GetIt;',
  28195. ' f;',
  28196. ' f();',
  28197. ' f(1);',
  28198. 'end;',
  28199. 'begin',
  28200. ' p:=@DoIt;',
  28201. ' f:=@GetIt;',
  28202. ' f;',
  28203. ' f();',
  28204. ' f(1);',
  28205. ' p:=TProcRef(f);',
  28206. '']);
  28207. ConvertProgram;
  28208. CheckSource('TestProcType_ReferenceToProc',
  28209. LinesToStr([ // statements
  28210. 'this.p = null;',
  28211. 'this.f = null;',
  28212. 'this.DoIt = function (i) {',
  28213. '};',
  28214. 'this.GetIt = function (i) {',
  28215. ' var Result = 0;',
  28216. ' $mod.p = $mod.DoIt;',
  28217. ' $mod.f = $mod.GetIt;',
  28218. ' $mod.f(0);',
  28219. ' $mod.f(0);',
  28220. ' $mod.f(1);',
  28221. ' return Result;',
  28222. '};',
  28223. '']),
  28224. LinesToStr([ // $mod.$main
  28225. '$mod.p = $mod.DoIt;',
  28226. '$mod.f = $mod.GetIt;',
  28227. '$mod.f(0);',
  28228. '$mod.f(0);',
  28229. '$mod.f(1);',
  28230. '$mod.p = $mod.f;',
  28231. '']));
  28232. end;
  28233. procedure TTestModule.TestProcType_ReferenceToMethod;
  28234. begin
  28235. StartProgram(false);
  28236. Add([
  28237. 'type',
  28238. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28239. ' TObject = class',
  28240. ' function Grow(s: longint): longint;',
  28241. ' end;',
  28242. 'var',
  28243. ' f: tfuncref;',
  28244. 'function tobject.grow(s: longint): longint;',
  28245. ' function GrowSub(i: longint): longint;',
  28246. ' begin',
  28247. ' f:=@grow;',
  28248. ' f:=@growsub;',
  28249. ' end;',
  28250. 'begin',
  28251. ' f:=@grow;',
  28252. ' f:=@growsub;',
  28253. 'end;',
  28254. 'begin',
  28255. '']);
  28256. ConvertProgram;
  28257. CheckSource('TestProcType_ReferenceToMethod',
  28258. LinesToStr([ // statements
  28259. 'rtl.createClass(this, "TObject", null, function () {',
  28260. ' this.$init = function () {',
  28261. ' };',
  28262. ' this.$final = function () {',
  28263. ' };',
  28264. ' this.Grow = function (s) {',
  28265. ' var $Self = this;',
  28266. ' var Result = 0;',
  28267. ' function GrowSub(i) {',
  28268. ' var Result = 0;',
  28269. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28270. ' $mod.f = GrowSub;',
  28271. ' return Result;',
  28272. ' };',
  28273. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28274. ' $mod.f = GrowSub;',
  28275. ' return Result;',
  28276. ' };',
  28277. '});',
  28278. 'this.f = null;',
  28279. '']),
  28280. LinesToStr([ // $mod.$main
  28281. '']));
  28282. end;
  28283. procedure TTestModule.TestProcType_Typecast;
  28284. begin
  28285. StartProgram(false);
  28286. Add([
  28287. 'type',
  28288. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28289. ' TEvent = procedure of object;',
  28290. ' TGetter = function:longint of object;',
  28291. ' TProcA = procedure(i: longint);',
  28292. ' TFuncB = function(i, j: longint): longint;',
  28293. 'procedure DoIt(); varargs; begin end;',
  28294. 'var',
  28295. ' Notify: tnotifyevent;',
  28296. ' Event: tevent;',
  28297. ' Getter: tgetter;',
  28298. ' ProcA: tproca;',
  28299. ' FuncB: tfuncb;',
  28300. ' p: pointer;',
  28301. 'begin',
  28302. ' notify:=tnotifyevent(event);',
  28303. ' event:=tevent(event);',
  28304. ' event:=tevent(notify);',
  28305. ' event:=tevent(getter);',
  28306. ' event:=tevent(proca);',
  28307. ' proca:=tproca(funcb);',
  28308. ' funcb:=tfuncb(funcb);',
  28309. ' funcb:=tfuncb(proca);',
  28310. ' funcb:=tfuncb(getter);',
  28311. ' proca:=tproca(p);',
  28312. ' funcb:=tfuncb(p);',
  28313. ' getter:=tgetter(p);',
  28314. ' p:=pointer(notify);',
  28315. ' p:=notify;',
  28316. ' p:=pointer(proca);',
  28317. ' p:=proca;',
  28318. ' p:=pointer(funcb);',
  28319. ' p:=funcb;',
  28320. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28321. '']);
  28322. ConvertProgram;
  28323. CheckSource('TestProcType_Typecast',
  28324. LinesToStr([ // statements
  28325. 'this.DoIt = function () {',
  28326. '};',
  28327. 'this.Notify = null;',
  28328. 'this.Event = null;',
  28329. 'this.Getter = null;',
  28330. 'this.ProcA = null;',
  28331. 'this.FuncB = null;',
  28332. 'this.p = null;',
  28333. '']),
  28334. LinesToStr([ // $mod.$main
  28335. '$mod.Notify = $mod.Event;',
  28336. '$mod.Event = $mod.Event;',
  28337. '$mod.Event = $mod.Notify;',
  28338. '$mod.Event = $mod.Getter;',
  28339. '$mod.Event = $mod.ProcA;',
  28340. '$mod.ProcA = $mod.FuncB;',
  28341. '$mod.FuncB = $mod.FuncB;',
  28342. '$mod.FuncB = $mod.ProcA;',
  28343. '$mod.FuncB = $mod.Getter;',
  28344. '$mod.ProcA = $mod.p;',
  28345. '$mod.FuncB = $mod.p;',
  28346. '$mod.Getter = $mod.p;',
  28347. '$mod.p = $mod.Notify;',
  28348. '$mod.p = $mod.Notify;',
  28349. '$mod.p = $mod.ProcA;',
  28350. '$mod.p = $mod.ProcA;',
  28351. '$mod.p = $mod.FuncB;',
  28352. '$mod.p = $mod.FuncB;',
  28353. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28354. '']));
  28355. end;
  28356. procedure TTestModule.TestProcType_PassProcToUntyped;
  28357. begin
  28358. StartProgram(false);
  28359. Add([
  28360. 'type',
  28361. ' TEvent = procedure of object;',
  28362. ' TFunc = function: longint;',
  28363. 'procedure DoIt(); varargs; begin end;',
  28364. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28365. 'var',
  28366. ' Event: tevent;',
  28367. ' Func: TFunc;',
  28368. 'begin',
  28369. ' doit(event,func);',
  28370. ' dosome(event,event,event);',
  28371. ' dosome(func,func,func);',
  28372. '']);
  28373. ConvertProgram;
  28374. CheckSource('TestProcType_PassProcToUntyped',
  28375. LinesToStr([ // statements
  28376. 'this.DoIt = function () {',
  28377. '};',
  28378. 'this.DoSome = function (a, b, p) {',
  28379. '};',
  28380. 'this.Event = null;',
  28381. 'this.Func = null;',
  28382. '']),
  28383. LinesToStr([ // $mod.$main
  28384. '$mod.DoIt($mod.Event, $mod.Func);',
  28385. '$mod.DoSome($mod.Event, {',
  28386. ' p: $mod,',
  28387. ' get: function () {',
  28388. ' return this.p.Event;',
  28389. ' },',
  28390. ' set: function (v) {',
  28391. ' this.p.Event = v;',
  28392. ' }',
  28393. '}, $mod.Event);',
  28394. '$mod.DoSome($mod.Func, {',
  28395. ' p: $mod,',
  28396. ' get: function () {',
  28397. ' return this.p.Func;',
  28398. ' },',
  28399. ' set: function (v) {',
  28400. ' this.p.Func = v;',
  28401. ' }',
  28402. '}, $mod.Func);',
  28403. '']));
  28404. end;
  28405. procedure TTestModule.TestProcType_PassProcToArray;
  28406. begin
  28407. StartProgram(false);
  28408. Add([
  28409. 'type',
  28410. ' TFunc = function: longint;',
  28411. ' TArrFunc = array of TFunc;',
  28412. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28413. 'function GetIt: longint; begin end;',
  28414. 'var',
  28415. ' Func: tfunc;',
  28416. 'begin',
  28417. ' doit([]);',
  28418. ' doit([@GetIt]);',
  28419. ' doit([Func]);',
  28420. '']);
  28421. ConvertProgram;
  28422. CheckSource('TestProcType_PassProcToArray',
  28423. LinesToStr([ // statements
  28424. 'this.DoIt = function (Arr) {',
  28425. '};',
  28426. 'this.GetIt = function () {',
  28427. ' var Result = 0;',
  28428. ' return Result;',
  28429. '};',
  28430. 'this.Func = null;',
  28431. '']),
  28432. LinesToStr([ // $mod.$main
  28433. '$mod.DoIt([]);',
  28434. '$mod.DoIt([$mod.GetIt]);',
  28435. '$mod.DoIt([$mod.Func]);',
  28436. '']));
  28437. end;
  28438. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28439. begin
  28440. StartProgram(false);
  28441. Add([
  28442. '{$modeswitch externalclass}',
  28443. 'type',
  28444. ' TProc = reference to procedure(i: longint); safecall;',
  28445. ' TEvent = procedure(i: longint) of object; safecall;',
  28446. ' TExtA = class external name ''ExtObj''',
  28447. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28448. ' procedure DoSome(Id: longint = 1);',
  28449. ' procedure SetOnClick(const e: TEvent);',
  28450. ' property OnClick: TEvent write SetOnClick;',
  28451. ' class procedure Fly(Id: longint = 1); static;',
  28452. ' procedure SetOnShow(const p: TProc);',
  28453. ' property OnShow: TProc write SetOnShow;',
  28454. ' end;',
  28455. 'procedure Run(i: longint = 1);',
  28456. 'begin',
  28457. 'end;',
  28458. 'var',
  28459. ' Obj: texta;',
  28460. ' e: TEvent;',
  28461. ' p: TProc;',
  28462. 'begin',
  28463. ' e:=e;',
  28464. ' e:[email protected];',
  28465. ' e:[email protected];',
  28466. ' e:=TEvent(@obj.dosome);', // no safecall
  28467. ' obj.OnClick:[email protected];',
  28468. ' obj.OnClick:[email protected];',
  28469. ' obj.setonclick(@obj.doit);',
  28470. ' obj.setonclick(@obj.dosome);',
  28471. ' p:=@Run;',
  28472. ' p:[email protected];',
  28473. ' obj.OnShow:=@Run;',
  28474. ' obj.OnShow:[email protected];',
  28475. ' obj.setOnShow(@Run);',
  28476. ' obj.setOnShow(@TExtA.Fly);',
  28477. ' with obj do begin',
  28478. ' e:=@doit;',
  28479. ' e:=@dosome;',
  28480. ' OnClick:=@doit;',
  28481. ' OnClick:=@dosome;',
  28482. ' setonclick(@doit);',
  28483. ' setonclick(@dosome);',
  28484. ' OnShow:=@Run;',
  28485. ' setOnShow(@Run);',
  28486. ' end;']);
  28487. ConvertProgram;
  28488. CheckSource('TestProcType_SafeCallObjFPC',
  28489. LinesToStr([ // statements
  28490. 'this.Run = function (i) {',
  28491. '};',
  28492. 'this.Obj = null;',
  28493. 'this.e = null;',
  28494. 'this.p = null;',
  28495. '']),
  28496. LinesToStr([ // $mod.$main
  28497. '$mod.e = $mod.e;',
  28498. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28499. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28500. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28501. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28502. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28503. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28504. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28505. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28506. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28507. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28508. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28509. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28510. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28511. 'var $with = $mod.Obj;',
  28512. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28513. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28514. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28515. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28516. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28517. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28518. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28519. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28520. '']));
  28521. end;
  28522. procedure TTestModule.TestProcType_SafeCallDelphi;
  28523. begin
  28524. StartProgram(false);
  28525. Add([
  28526. '{$mode delphi}',
  28527. '{$modeswitch externalclass}',
  28528. 'type',
  28529. ' TProc = reference to procedure(i: longint); safecall;',
  28530. ' TEvent = procedure(i: longint) of object; safecall;',
  28531. ' TExtA = class external name ''ExtObj''',
  28532. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28533. ' procedure DoSome(Id: longint = 1);',
  28534. ' procedure SetOnClick(const e: TEvent);',
  28535. ' property OnClick: TEvent write SetOnClick;',
  28536. ' class procedure Fly(Id: longint = 1); static;',
  28537. ' procedure SetOnShow(const p: TProc);',
  28538. ' property OnShow: TProc write SetOnShow;',
  28539. ' end;',
  28540. 'procedure Run(i: longint = 1);',
  28541. 'begin',
  28542. 'end;',
  28543. 'var',
  28544. ' Obj: texta;',
  28545. ' e: TEvent;',
  28546. ' p: TProc;',
  28547. 'begin',
  28548. ' e:=e;',
  28549. ' e:=obj.doit;',
  28550. ' e:=obj.dosome;',
  28551. ' e:=TEvent(@obj.dosome);', // no safecall
  28552. ' obj.OnClick:=obj.doit;',
  28553. ' obj.OnClick:=obj.dosome;',
  28554. ' obj.setonclick(obj.doit);',
  28555. ' obj.setonclick(obj.dosome);',
  28556. ' p:=Run;',
  28557. ' p:=TExtA.Fly;',
  28558. ' obj.OnShow:=Run;',
  28559. ' obj.OnShow:=TExtA.Fly;',
  28560. ' obj.setOnShow(Run);',
  28561. ' obj.setOnShow(TExtA.Fly);',
  28562. ' with obj do begin',
  28563. ' e:=doit;',
  28564. ' e:=dosome;',
  28565. ' OnClick:=doit;',
  28566. ' OnClick:=dosome;',
  28567. ' setonclick(doit);',
  28568. ' setonclick(dosome);',
  28569. ' OnShow:=@Run;',
  28570. ' setOnShow(@Run);',
  28571. ' end;']);
  28572. ConvertProgram;
  28573. CheckSource('TestProcType_SafeCallDelphi',
  28574. LinesToStr([ // statements
  28575. 'this.Run = function (i) {',
  28576. '};',
  28577. 'this.Obj = null;',
  28578. 'this.e = null;',
  28579. 'this.p = null;',
  28580. '']),
  28581. LinesToStr([ // $mod.$main
  28582. '$mod.e = $mod.e;',
  28583. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28584. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28585. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28586. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28587. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28588. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28589. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28590. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28591. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28592. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28593. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28594. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28595. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28596. 'var $with = $mod.Obj;',
  28597. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28598. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28599. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28600. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28601. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28602. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28603. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28604. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28605. '']));
  28606. end;
  28607. procedure TTestModule.TestProcType_SafeCall_Arg;
  28608. begin
  28609. StartProgram(false);
  28610. Add([
  28611. 'type',
  28612. ' TSafecallProc = reference to procedure; safecall;',
  28613. 'procedure Fly(const aHandler: TSafecallProc);',
  28614. 'var',
  28615. ' P: TSafecallProc;',
  28616. 'begin',
  28617. ' P := aHandler;',
  28618. ' Fly(P);',
  28619. ' Fly(aHandler);',
  28620. 'end;',
  28621. 'begin',
  28622. ' Fly(nil);',
  28623. '']);
  28624. ConvertProgram;
  28625. CheckSource('TestProcType_SafeCall_Arg',
  28626. LinesToStr([ // statements
  28627. 'this.Fly = function (aHandler) {',
  28628. ' var P = null;',
  28629. ' P = aHandler;',
  28630. ' $mod.Fly(P);',
  28631. ' $mod.Fly(aHandler);',
  28632. '};',
  28633. '']),
  28634. LinesToStr([ // $mod.$main
  28635. '$mod.Fly(null);',
  28636. '']));
  28637. end;
  28638. procedure TTestModule.TestPointer;
  28639. begin
  28640. StartProgram(false);
  28641. Add(['type',
  28642. ' TObject = class end;',
  28643. ' TClass = class of TObject;',
  28644. ' TArrInt = array of longint;',
  28645. 'const',
  28646. ' n = nil;',
  28647. 'var',
  28648. ' v: jsvalue;',
  28649. ' Obj: tobject;',
  28650. ' C: tclass;',
  28651. ' a: tarrint;',
  28652. ' p: Pointer = nil;',
  28653. ' s: string;',
  28654. 'begin',
  28655. ' p:=p;',
  28656. ' p:=nil;',
  28657. ' if p=nil then;',
  28658. ' if nil=p then;',
  28659. ' if Assigned(p) then;',
  28660. ' p:=Pointer(v);',
  28661. ' p:=obj;',
  28662. ' p:=c;',
  28663. ' p:=a;',
  28664. ' p:=tobject;',
  28665. ' obj:=TObject(p);',
  28666. ' c:=TClass(p);',
  28667. ' a:=TArrInt(p);',
  28668. ' p:=n;',
  28669. ' p:=Pointer(a);',
  28670. ' p:=pointer(s);',
  28671. ' s:=string(p);',
  28672. '']);
  28673. ConvertProgram;
  28674. CheckSource('TestPointer',
  28675. LinesToStr([ // statements
  28676. 'rtl.createClass(this, "TObject", null, function () {',
  28677. ' this.$init = function () {',
  28678. ' };',
  28679. ' this.$final = function () {',
  28680. ' };',
  28681. '});',
  28682. 'this.n = null;',
  28683. 'this.v = undefined;',
  28684. 'this.Obj = null;',
  28685. 'this.C = null;',
  28686. 'this.a = [];',
  28687. 'this.p = null;',
  28688. 'this.s = "";',
  28689. '']),
  28690. LinesToStr([ // $mod.$main
  28691. '$mod.p = $mod.p;',
  28692. '$mod.p = null;',
  28693. 'if ($mod.p === null) ;',
  28694. 'if (null === $mod.p) ;',
  28695. 'if ($mod.p != null) ;',
  28696. '$mod.p = $mod.v;',
  28697. '$mod.p = $mod.Obj;',
  28698. '$mod.p = $mod.C;',
  28699. '$mod.p = $mod.a;',
  28700. '$mod.p = $mod.TObject;',
  28701. '$mod.Obj = $mod.p;',
  28702. '$mod.C = $mod.p;',
  28703. '$mod.a = $mod.p;',
  28704. '$mod.p = null;',
  28705. '$mod.p = $mod.a;',
  28706. '$mod.p = $mod.s;',
  28707. '$mod.s = $mod.p;',
  28708. '']));
  28709. end;
  28710. procedure TTestModule.TestPointer_Proc;
  28711. begin
  28712. StartProgram(false);
  28713. Add('type');
  28714. Add(' TObject = class');
  28715. Add(' procedure DoIt; virtual; abstract;');
  28716. Add(' end;');
  28717. Add('procedure DoSome; begin end;');
  28718. Add('var');
  28719. Add(' o: TObject;');
  28720. Add(' p: Pointer;');
  28721. Add('begin');
  28722. Add(' p:=@DoSome;');
  28723. Add(' p:[email protected];');
  28724. ConvertProgram;
  28725. CheckSource('TestPointer_Proc',
  28726. LinesToStr([ // statements
  28727. 'rtl.createClass(this, "TObject", null, function () {',
  28728. ' this.$init = function () {',
  28729. ' };',
  28730. ' this.$final = function () {',
  28731. ' };',
  28732. '});',
  28733. 'this.DoSome = function () {',
  28734. '};',
  28735. 'this.o = null;',
  28736. 'this.p = null;',
  28737. '']),
  28738. LinesToStr([ // $mod.$main
  28739. '$mod.p = $mod.DoSome;',
  28740. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28741. '']));
  28742. end;
  28743. procedure TTestModule.TestPointer_AssignRecordFail;
  28744. begin
  28745. StartProgram(false);
  28746. Add('type');
  28747. Add(' TRec = record end;');
  28748. Add('var');
  28749. Add(' p: Pointer;');
  28750. Add(' r: TRec;');
  28751. Add('begin');
  28752. Add(' p:=r;');
  28753. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28754. nIncompatibleTypesGotExpected);
  28755. ConvertProgram;
  28756. end;
  28757. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28758. begin
  28759. StartProgram(false);
  28760. Add('type');
  28761. Add(' TArr = array[boolean] of longint;');
  28762. Add('var');
  28763. Add(' p: Pointer;');
  28764. Add(' a: TArr;');
  28765. Add('begin');
  28766. Add(' p:=a;');
  28767. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28768. nIncompatibleTypesGotExpected);
  28769. ConvertProgram;
  28770. end;
  28771. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28772. begin
  28773. StartProgram(false);
  28774. Add([
  28775. 'procedure DoIt(args: array of jsvalue); begin end;',
  28776. 'procedure DoAll; varargs; begin end;',
  28777. 'var',
  28778. ' v: jsvalue;',
  28779. 'begin',
  28780. ' DoIt([pointer(v)]);',
  28781. ' DoAll(pointer(v));',
  28782. '']);
  28783. ConvertProgram;
  28784. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28785. LinesToStr([ // statements
  28786. 'this.DoIt = function (args) {',
  28787. '};',
  28788. 'this.DoAll = function () {',
  28789. '};',
  28790. 'this.v = undefined;',
  28791. '']),
  28792. LinesToStr([ // $mod.$main
  28793. '$mod.DoIt([$mod.v]);',
  28794. '$mod.DoAll($mod.v);',
  28795. '']));
  28796. end;
  28797. procedure TTestModule.TestPointer_NonRecordFail;
  28798. begin
  28799. StartProgram(false);
  28800. Add([
  28801. 'type',
  28802. ' p = ^longint;',
  28803. 'begin',
  28804. '']);
  28805. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28806. ConvertProgram;
  28807. end;
  28808. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28809. begin
  28810. StartProgram(false);
  28811. Add([
  28812. 'procedure DoIt(p: ^longint); begin end;',
  28813. 'begin',
  28814. '']);
  28815. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28816. ConvertProgram;
  28817. end;
  28818. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28819. begin
  28820. StartProgram(false);
  28821. Add([
  28822. 'var p: ^longint;',
  28823. 'begin',
  28824. '']);
  28825. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28826. ConvertProgram;
  28827. end;
  28828. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28829. begin
  28830. StartProgram(false);
  28831. Add([
  28832. 'function DoIt: ^longint; begin end;',
  28833. 'begin',
  28834. '']);
  28835. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28836. ConvertProgram;
  28837. end;
  28838. procedure TTestModule.TestPointer_AddrOperatorFail;
  28839. begin
  28840. StartProgram(false);
  28841. Add([
  28842. 'var i: longint;',
  28843. 'begin',
  28844. ' if @i=nil then ;',
  28845. '']);
  28846. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28847. ConvertProgram;
  28848. end;
  28849. procedure TTestModule.TestPointer_ArrayParamsFail;
  28850. begin
  28851. StartProgram(false);
  28852. Add([
  28853. 'var',
  28854. ' p: Pointer;',
  28855. 'begin',
  28856. ' p:=p[1];',
  28857. '']);
  28858. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28859. ConvertProgram;
  28860. end;
  28861. procedure TTestModule.TestPointer_PointerAddFail;
  28862. begin
  28863. StartProgram(false);
  28864. Add([
  28865. 'var',
  28866. ' p: Pointer;',
  28867. 'begin',
  28868. ' p:=p+1;',
  28869. '']);
  28870. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28871. ConvertProgram;
  28872. end;
  28873. procedure TTestModule.TestPointer_IncPointerFail;
  28874. begin
  28875. StartProgram(false);
  28876. Add([
  28877. 'var',
  28878. ' p: Pointer;',
  28879. 'begin',
  28880. ' inc(p,1);',
  28881. '']);
  28882. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28883. nIncompatibleTypeArgNo);
  28884. ConvertProgram;
  28885. end;
  28886. procedure TTestModule.TestPointer_Record;
  28887. begin
  28888. StartProgram(false);
  28889. Add([
  28890. 'type',
  28891. ' TRec = record x: longint; end;',
  28892. ' PRec = ^TRec;',
  28893. 'var',
  28894. ' r: TRec;',
  28895. ' p: PRec;',
  28896. ' q: ^TRec;',
  28897. ' Ptr: pointer;',
  28898. 'begin',
  28899. ' new(p);',
  28900. ' p:=@r;',
  28901. ' r:=p^;',
  28902. ' r.x:=p^.x;',
  28903. ' p^.x:=r.x;',
  28904. ' if p^.x=3 then ;',
  28905. ' if 4=p^.x then ;',
  28906. ' dispose(p);',
  28907. ' new(q);',
  28908. ' dispose(q);',
  28909. ' Ptr:=p;',
  28910. ' p:=PRec(ptr);',
  28911. '']);
  28912. ConvertProgram;
  28913. CheckSource('TestPointer_Record',
  28914. LinesToStr([ // statements
  28915. 'rtl.recNewT(this, "TRec", function () {',
  28916. ' this.x = 0;',
  28917. ' this.$eq = function (b) {',
  28918. ' return this.x === b.x;',
  28919. ' };',
  28920. ' this.$assign = function (s) {',
  28921. ' this.x = s.x;',
  28922. ' return this;',
  28923. ' };',
  28924. '});',
  28925. 'this.r = this.TRec.$new();',
  28926. 'this.p = null;',
  28927. 'this.q = null;',
  28928. 'this.Ptr = null;',
  28929. '']),
  28930. LinesToStr([ // $mod.$main
  28931. '$mod.p = $mod.TRec.$new();',
  28932. '$mod.p = $mod.r;',
  28933. '$mod.r.$assign($mod.p);',
  28934. '$mod.r.x = $mod.p.x;',
  28935. '$mod.p.x = $mod.r.x;',
  28936. 'if ($mod.p.x === 3) ;',
  28937. 'if (4 === $mod.p.x) ;',
  28938. '$mod.p = null;',
  28939. '$mod.q = $mod.TRec.$new();',
  28940. '$mod.q = null;',
  28941. '$mod.Ptr = $mod.p;',
  28942. '$mod.p = $mod.Ptr;',
  28943. '']));
  28944. end;
  28945. procedure TTestModule.TestPointer_RecordArg;
  28946. begin
  28947. StartProgram(false);
  28948. Add([
  28949. '{$modeswitch autoderef}',
  28950. 'type',
  28951. ' TRec = record x: longint; end;',
  28952. ' PRec = ^TRec;',
  28953. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28954. 'begin',
  28955. ' a.x:=a.x;',
  28956. ' a^.x:=a^.x;',
  28957. ' with a^ do',
  28958. ' x:=x;',
  28959. 'end;',
  28960. 'function GetIt(p: PRec): PRec;',
  28961. 'begin',
  28962. ' p.x:=p.x;',
  28963. ' p^.x:=p^.x;',
  28964. ' with p^ do',
  28965. ' x:=x;',
  28966. 'end;',
  28967. 'var',
  28968. ' r: TRec;',
  28969. ' p: PRec;',
  28970. 'begin',
  28971. ' p:=GetIt(p);',
  28972. ' p^:=GetIt(@r)^;',
  28973. ' DoIt(p,p,p);',
  28974. ' DoIt(@r,p,p);',
  28975. '']);
  28976. ConvertProgram;
  28977. CheckSource('TestPointer_RecordArg',
  28978. LinesToStr([ // statements
  28979. 'rtl.recNewT(this, "TRec", function () {',
  28980. ' this.x = 0;',
  28981. ' this.$eq = function (b) {',
  28982. ' return this.x === b.x;',
  28983. ' };',
  28984. ' this.$assign = function (s) {',
  28985. ' this.x = s.x;',
  28986. ' return this;',
  28987. ' };',
  28988. '});',
  28989. 'this.DoIt = function (a, b, c) {',
  28990. ' var Result = $mod.TRec.$new();',
  28991. ' a.x = a.x;',
  28992. ' a.x = a.x;',
  28993. ' a.x = a.x;',
  28994. ' return Result;',
  28995. '};',
  28996. 'this.GetIt = function (p) {',
  28997. ' var Result = null;',
  28998. ' p.x = p.x;',
  28999. ' p.x = p.x;',
  29000. ' p.x = p.x;',
  29001. ' return Result;',
  29002. '};',
  29003. 'this.r = this.TRec.$new();',
  29004. 'this.p = null;',
  29005. '']),
  29006. LinesToStr([ // $mod.$main
  29007. '$mod.p = $mod.GetIt($mod.p);',
  29008. '$mod.p.$assign($mod.GetIt($mod.r));',
  29009. '$mod.DoIt($mod.p, {',
  29010. ' p: $mod,',
  29011. ' get: function () {',
  29012. ' return this.p.p;',
  29013. ' },',
  29014. ' set: function (v) {',
  29015. ' this.p.p = v;',
  29016. ' }',
  29017. '}, {',
  29018. ' p: $mod,',
  29019. ' get: function () {',
  29020. ' return this.p.p;',
  29021. ' },',
  29022. ' set: function (v) {',
  29023. ' this.p.p = v;',
  29024. ' }',
  29025. '});',
  29026. '$mod.DoIt($mod.r, {',
  29027. ' p: $mod,',
  29028. ' get: function () {',
  29029. ' return this.p.p;',
  29030. ' },',
  29031. ' set: function (v) {',
  29032. ' this.p.p = v;',
  29033. ' }',
  29034. '}, {',
  29035. ' p: $mod,',
  29036. ' get: function () {',
  29037. ' return this.p.p;',
  29038. ' },',
  29039. ' set: function (v) {',
  29040. ' this.p.p = v;',
  29041. ' }',
  29042. '});',
  29043. '']));
  29044. end;
  29045. procedure TTestModule.TestJSValue_AssignToJSValue;
  29046. begin
  29047. StartProgram(false);
  29048. Add('var');
  29049. Add(' v: jsvalue;');
  29050. Add(' i: longint;');
  29051. Add(' s: string;');
  29052. Add(' b: boolean;');
  29053. Add(' d: double;');
  29054. Add(' p: pointer;');
  29055. Add('begin');
  29056. Add(' v:=v;');
  29057. Add(' v:=1;');
  29058. Add(' v:=i;');
  29059. Add(' v:='''';');
  29060. Add(' v:=''c'';');
  29061. Add(' v:=''foo'';');
  29062. Add(' v:=s;');
  29063. Add(' v:=false;');
  29064. Add(' v:=true;');
  29065. Add(' v:=b;');
  29066. Add(' v:=0.1;');
  29067. Add(' v:=d;');
  29068. Add(' v:=nil;');
  29069. Add(' v:=p;');
  29070. ConvertProgram;
  29071. CheckSource('TestJSValue_AssignToJSValue',
  29072. LinesToStr([ // statements
  29073. 'this.v = undefined;',
  29074. 'this.i = 0;',
  29075. 'this.s = "";',
  29076. 'this.b = false;',
  29077. 'this.d = 0.0;',
  29078. 'this.p = null;',
  29079. '']),
  29080. LinesToStr([ // $mod.$main
  29081. '$mod.v = $mod.v;',
  29082. '$mod.v = 1;',
  29083. '$mod.v = $mod.i;',
  29084. '$mod.v = "";',
  29085. '$mod.v = "c";',
  29086. '$mod.v = "foo";',
  29087. '$mod.v = $mod.s;',
  29088. '$mod.v = false;',
  29089. '$mod.v = true;',
  29090. '$mod.v = $mod.b;',
  29091. '$mod.v = 0.1;',
  29092. '$mod.v = $mod.d;',
  29093. '$mod.v = null;',
  29094. '$mod.v = $mod.p;',
  29095. '']));
  29096. end;
  29097. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29098. begin
  29099. StartProgram(false);
  29100. Add('type');
  29101. Add(' integer = longint;');
  29102. Add(' TYesNo = boolean;');
  29103. Add(' TFloat = double;');
  29104. Add(' TCaption = string;');
  29105. Add(' TChar = char;');
  29106. Add('var');
  29107. Add(' v: jsvalue;');
  29108. Add(' i: integer;');
  29109. Add(' s: TCaption;');
  29110. Add(' b: TYesNo;');
  29111. Add(' d: TFloat;');
  29112. Add(' c: char;');
  29113. Add('begin');
  29114. Add(' i:=longint(v);');
  29115. Add(' i:=integer(v);');
  29116. Add(' s:=string(v);');
  29117. Add(' s:=TCaption(v);');
  29118. Add(' b:=boolean(v);');
  29119. Add(' b:=TYesNo(v);');
  29120. Add(' d:=double(v);');
  29121. Add(' d:=TFloat(v);');
  29122. Add(' c:=char(v);');
  29123. Add(' c:=TChar(v);');
  29124. ConvertProgram;
  29125. CheckSource('TestJSValue_TypeCastToBaseType',
  29126. LinesToStr([ // statements
  29127. 'this.v = undefined;',
  29128. 'this.i = 0;',
  29129. 'this.s = "";',
  29130. 'this.b = false;',
  29131. 'this.d = 0.0;',
  29132. 'this.c = "";',
  29133. '']),
  29134. LinesToStr([ // $mod.$main
  29135. '$mod.i = rtl.trunc($mod.v);',
  29136. '$mod.i = rtl.trunc($mod.v);',
  29137. '$mod.s = "" + $mod.v;',
  29138. '$mod.s = "" + $mod.v;',
  29139. '$mod.b = !($mod.v == false);',
  29140. '$mod.b = !($mod.v == false);',
  29141. '$mod.d = rtl.getNumber($mod.v);',
  29142. '$mod.d = rtl.getNumber($mod.v);',
  29143. '$mod.c = rtl.getChar($mod.v);',
  29144. '$mod.c = rtl.getChar($mod.v);',
  29145. '']));
  29146. end;
  29147. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29148. begin
  29149. StartProgram(false);
  29150. Add([
  29151. 'type',
  29152. ' TArr = array of word;',
  29153. ' TRec = record end;',
  29154. ' TSet = set of boolean;',
  29155. 'procedure Fly(v: jsvalue);',
  29156. 'begin',
  29157. 'end;',
  29158. 'var',
  29159. ' a: TArr;',
  29160. ' r: TRec;',
  29161. ' s: TSet;',
  29162. 'begin',
  29163. ' Fly(jsvalue(a));',
  29164. ' Fly(jsvalue(r));',
  29165. ' Fly(jsvalue(s));',
  29166. '']);
  29167. ConvertProgram;
  29168. CheckSource('TestJSValue_TypecastToJSValue',
  29169. LinesToStr([ // statements
  29170. 'rtl.recNewT(this, "TRec", function () {',
  29171. ' this.$eq = function (b) {',
  29172. ' return true;',
  29173. ' };',
  29174. ' this.$assign = function (s) {',
  29175. ' return this;',
  29176. ' };',
  29177. '});',
  29178. 'this.Fly = function (v) {',
  29179. '};',
  29180. 'this.a = [];',
  29181. 'this.r = this.TRec.$new();',
  29182. 'this.s = {};',
  29183. '']),
  29184. LinesToStr([ // $mod.$main
  29185. '$mod.Fly($mod.a);',
  29186. '$mod.Fly($mod.r);',
  29187. '$mod.Fly($mod.s);',
  29188. '']));
  29189. end;
  29190. procedure TTestModule.TestJSValue_Equal;
  29191. begin
  29192. StartProgram(false);
  29193. Add('type');
  29194. Add(' integer = longint;');
  29195. Add(' TYesNo = boolean;');
  29196. Add(' TFloat = double;');
  29197. Add(' TCaption = string;');
  29198. Add(' TChar = char;');
  29199. Add(' TMulti = JSValue;');
  29200. Add('var');
  29201. Add(' v: jsvalue;');
  29202. Add(' i: integer;');
  29203. Add(' s: TCaption;');
  29204. Add(' b: TYesNo;');
  29205. Add(' d: TFloat;');
  29206. Add(' c: char;');
  29207. Add(' m: TMulti;');
  29208. Add('begin');
  29209. Add(' b:=v=v;');
  29210. Add(' b:=v<>v;');
  29211. Add(' b:=v=1;');
  29212. Add(' b:=v<>1;');
  29213. Add(' b:=2=v;');
  29214. Add(' b:=2<>v;');
  29215. Add(' b:=v=i;');
  29216. Add(' b:=i=v;');
  29217. Add(' b:=v=nil;');
  29218. Add(' b:=nil=v;');
  29219. Add(' b:=v=false;');
  29220. Add(' b:=true=v;');
  29221. Add(' b:=v=b;');
  29222. Add(' b:=b=v;');
  29223. Add(' b:=v=s;');
  29224. Add(' b:=s=v;');
  29225. Add(' b:=v=''foo'';');
  29226. Add(' b:=''''=v;');
  29227. Add(' b:=v=d;');
  29228. Add(' b:=d=v;');
  29229. Add(' b:=v=3.4;');
  29230. Add(' b:=5.6=v;');
  29231. Add(' b:=v=c;');
  29232. Add(' b:=c=v;');
  29233. Add(' b:=m=m;');
  29234. Add(' b:=v=m;');
  29235. Add(' b:=m=v;');
  29236. ConvertProgram;
  29237. CheckSource('TestJSValue_Equal',
  29238. LinesToStr([ // statements
  29239. 'this.v = undefined;',
  29240. 'this.i = 0;',
  29241. 'this.s = "";',
  29242. 'this.b = false;',
  29243. 'this.d = 0.0;',
  29244. 'this.c = "";',
  29245. 'this.m = undefined;',
  29246. '']),
  29247. LinesToStr([ // $mod.$main
  29248. '$mod.b = $mod.v == $mod.v;',
  29249. '$mod.b = $mod.v != $mod.v;',
  29250. '$mod.b = $mod.v == 1;',
  29251. '$mod.b = $mod.v != 1;',
  29252. '$mod.b = 2 == $mod.v;',
  29253. '$mod.b = 2 != $mod.v;',
  29254. '$mod.b = $mod.v == $mod.i;',
  29255. '$mod.b = $mod.i == $mod.v;',
  29256. '$mod.b = $mod.v == null;',
  29257. '$mod.b = null == $mod.v;',
  29258. '$mod.b = $mod.v == false;',
  29259. '$mod.b = true == $mod.v;',
  29260. '$mod.b = $mod.v == $mod.b;',
  29261. '$mod.b = $mod.b == $mod.v;',
  29262. '$mod.b = $mod.v == $mod.s;',
  29263. '$mod.b = $mod.s == $mod.v;',
  29264. '$mod.b = $mod.v == "foo";',
  29265. '$mod.b = "" == $mod.v;',
  29266. '$mod.b = $mod.v == $mod.d;',
  29267. '$mod.b = $mod.d == $mod.v;',
  29268. '$mod.b = $mod.v == 3.4;',
  29269. '$mod.b = 5.6 == $mod.v;',
  29270. '$mod.b = $mod.v == $mod.c;',
  29271. '$mod.b = $mod.c == $mod.v;',
  29272. '$mod.b = $mod.m == $mod.m;',
  29273. '$mod.b = $mod.v == $mod.m;',
  29274. '$mod.b = $mod.m == $mod.v;',
  29275. '']));
  29276. end;
  29277. procedure TTestModule.TestJSValue_If;
  29278. begin
  29279. StartProgram(false);
  29280. Add([
  29281. 'procedure Fly(var u);',
  29282. 'begin',
  29283. ' if jsvalue(u) then ;',
  29284. 'end;',
  29285. 'var',
  29286. ' v: jsvalue;',
  29287. 'begin',
  29288. ' if v then ;',
  29289. ' while v do ;',
  29290. ' repeat until v;',
  29291. '']);
  29292. ConvertProgram;
  29293. CheckSource('TestJSValue_If',
  29294. LinesToStr([ // statements
  29295. 'this.Fly = function (u) {',
  29296. ' if (u.get()) ;',
  29297. '};',
  29298. 'this.v = undefined;',
  29299. '']),
  29300. LinesToStr([ // $mod.$main
  29301. 'if ($mod.v) ;',
  29302. 'while($mod.v){',
  29303. '};',
  29304. 'do{',
  29305. '} while(!$mod.v);',
  29306. '']));
  29307. end;
  29308. procedure TTestModule.TestJSValue_Not;
  29309. begin
  29310. StartProgram(false);
  29311. Add([
  29312. 'var',
  29313. ' v: jsvalue;',
  29314. ' b: boolean;',
  29315. 'begin',
  29316. ' b:=not v;',
  29317. ' if not v then ;',
  29318. ' while not v do ;',
  29319. ' repeat until not v;',
  29320. '']);
  29321. ConvertProgram;
  29322. CheckSource('TestJSValue_If',
  29323. LinesToStr([ // statements
  29324. 'this.v = undefined;',
  29325. 'this.b = false;',
  29326. '']),
  29327. LinesToStr([ // $mod.$main
  29328. '$mod.b=!$mod.v;',
  29329. 'if (!$mod.v) ;',
  29330. 'while(!$mod.v){',
  29331. '};',
  29332. 'do{',
  29333. '} while($mod.v);',
  29334. '']));
  29335. end;
  29336. procedure TTestModule.TestJSValue_Enum;
  29337. begin
  29338. StartProgram(false);
  29339. Add('type');
  29340. Add(' TColor = (red, blue);');
  29341. Add(' TRedBlue = TColor;');
  29342. Add('var');
  29343. Add(' v: jsvalue;');
  29344. Add(' e: TColor;');
  29345. Add('begin');
  29346. Add(' v:=e;');
  29347. Add(' v:=TColor(e);');
  29348. Add(' v:=TRedBlue(e);');
  29349. Add(' e:=TColor(v);');
  29350. Add(' e:=TRedBlue(v);');
  29351. ConvertProgram;
  29352. CheckSource('TestJSValue_Enum',
  29353. LinesToStr([ // statements
  29354. 'this.TColor = {',
  29355. ' "0": "red",',
  29356. ' red: 0,',
  29357. ' "1": "blue",',
  29358. ' blue: 1',
  29359. '};',
  29360. 'this.v = undefined;',
  29361. 'this.e = 0;',
  29362. '']),
  29363. LinesToStr([ // $mod.$main
  29364. '$mod.v = $mod.e;',
  29365. '$mod.v = $mod.e;',
  29366. '$mod.v = $mod.e;',
  29367. '$mod.e = $mod.v;',
  29368. '$mod.e = $mod.v;',
  29369. '']));
  29370. end;
  29371. procedure TTestModule.TestJSValue_ClassInstance;
  29372. begin
  29373. StartProgram(false);
  29374. Add([
  29375. 'type',
  29376. ' TObject = class',
  29377. ' end;',
  29378. ' TBirdObject = TObject;',
  29379. 'var',
  29380. ' v: jsvalue;',
  29381. ' o: TObject;',
  29382. 'begin',
  29383. ' v:=o;',
  29384. ' v:=TObject(o);',
  29385. ' v:=TBirdObject(o);',
  29386. ' o:=TObject(v);',
  29387. ' o:=TBirdObject(v);',
  29388. ' if v is TObject then ;',
  29389. '']);
  29390. ConvertProgram;
  29391. CheckSource('TestJSValue_ClassInstance',
  29392. LinesToStr([ // statements
  29393. 'rtl.createClass(this, "TObject", null, function () {',
  29394. ' this.$init = function () {',
  29395. ' };',
  29396. ' this.$final = function () {',
  29397. ' };',
  29398. '});',
  29399. 'this.v = undefined;',
  29400. 'this.o = null;',
  29401. '']),
  29402. LinesToStr([ // $mod.$main
  29403. '$mod.v = $mod.o;',
  29404. '$mod.v = $mod.o;',
  29405. '$mod.v = $mod.o;',
  29406. '$mod.o = rtl.getObject($mod.v);',
  29407. '$mod.o = rtl.getObject($mod.v);',
  29408. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29409. '']));
  29410. end;
  29411. procedure TTestModule.TestJSValue_ClassOf;
  29412. begin
  29413. StartProgram(false);
  29414. Add([
  29415. 'type',
  29416. ' TClass = class of TObject;',
  29417. ' TObject = class',
  29418. ' end;',
  29419. ' TBirds = class of TBird;',
  29420. ' TBird = class(TObject) end;',
  29421. 'var',
  29422. ' v: jsvalue;',
  29423. ' c: TClass;',
  29424. 'begin',
  29425. ' v:=c;',
  29426. ' v:=TObject;',
  29427. ' v:=TClass(c);',
  29428. ' v:=TBirds(c);',
  29429. ' c:=TClass(v);',
  29430. ' c:=TBirds(v);',
  29431. ' if v is TClass then ;',
  29432. '']);
  29433. ConvertProgram;
  29434. CheckSource('TestJSValue_ClassOf',
  29435. LinesToStr([ // statements
  29436. 'rtl.createClass(this, "TObject", null, function () {',
  29437. ' this.$init = function () {',
  29438. ' };',
  29439. ' this.$final = function () {',
  29440. ' };',
  29441. '});',
  29442. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29443. '});',
  29444. 'this.v = undefined;',
  29445. 'this.c = null;',
  29446. '']),
  29447. LinesToStr([ // $mod.$main
  29448. '$mod.v = $mod.c;',
  29449. '$mod.v = $mod.TObject;',
  29450. '$mod.v = $mod.c;',
  29451. '$mod.v = $mod.c;',
  29452. '$mod.c = rtl.getObject($mod.v);',
  29453. '$mod.c = rtl.getObject($mod.v);',
  29454. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29455. '']));
  29456. end;
  29457. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29458. begin
  29459. StartProgram(false);
  29460. Add([
  29461. 'type',
  29462. ' integer = longint;',
  29463. ' TArray = array of JSValue;',
  29464. ' TArrgh = tarray;',
  29465. ' TArrInt = array of integer;',
  29466. 'var',
  29467. ' v: jsvalue;',
  29468. ' TheArray: tarray = (1,''2'');',
  29469. ' Arr: tarrgh;',
  29470. ' i: integer;',
  29471. ' ArrInt: tarrint;',
  29472. 'begin',
  29473. ' arr:=thearray;',
  29474. ' thearray:=arr;',
  29475. ' setlength(arr,2);',
  29476. ' setlength(thearray,3);',
  29477. ' arr[4]:=v;',
  29478. ' arr[5]:=length(thearray);',
  29479. ' arr[6]:=nil;',
  29480. ' arr[7]:=thearray[8];',
  29481. ' arr[low(arr)]:=high(thearray);',
  29482. ' arr:=arrint;',
  29483. ' arrInt:=tarrint(arr);',
  29484. ' if TheArray = nil then ;',
  29485. ' if nil = TheArray then ;',
  29486. ' if TheArray <> nil then ;',
  29487. ' if nil <> TheArray then ;',
  29488. '']);
  29489. ConvertProgram;
  29490. CheckSource('TestJSValue_ArrayOfJSValue',
  29491. LinesToStr([ // statements
  29492. 'this.v = undefined;',
  29493. 'this.TheArray = [1, "2"];',
  29494. 'this.Arr = [];',
  29495. 'this.i = 0;',
  29496. 'this.ArrInt = [];',
  29497. '']),
  29498. LinesToStr([ // $mod.$main
  29499. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29500. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29501. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29502. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29503. '$mod.Arr[4] = $mod.v;',
  29504. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29505. '$mod.Arr[6] = null;',
  29506. '$mod.Arr[7] = $mod.TheArray[8];',
  29507. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29508. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29509. '$mod.ArrInt = $mod.Arr;',
  29510. 'if (rtl.length($mod.TheArray) === 0) ;',
  29511. 'if (rtl.length($mod.TheArray) === 0) ;',
  29512. 'if (rtl.length($mod.TheArray) > 0) ;',
  29513. 'if (rtl.length($mod.TheArray) > 0) ;',
  29514. '']));
  29515. end;
  29516. procedure TTestModule.TestJSValue_ArrayLit;
  29517. begin
  29518. StartProgram(false);
  29519. Add([
  29520. 'type',
  29521. ' TFlag = (big,small);',
  29522. ' TArray = array of JSValue;',
  29523. ' TObject = class end;',
  29524. ' TClass = class of TObject;',
  29525. 'var',
  29526. ' v: jsvalue;',
  29527. ' a: TArray;',
  29528. ' o: TObject;',
  29529. 'begin',
  29530. ' a:=[];',
  29531. ' a:=[1];',
  29532. ' a:=[1,2];',
  29533. ' a:=[big];',
  29534. ' a:=[1,big];',
  29535. ' a:=[o,nil];',
  29536. '']);
  29537. ConvertProgram;
  29538. CheckSource('TestJSValue_ArrayLit',
  29539. LinesToStr([ // statements
  29540. 'this.TFlag = {',
  29541. ' "0": "big",',
  29542. ' big: 0,',
  29543. ' "1": "small",',
  29544. ' small: 1',
  29545. '};',
  29546. 'rtl.createClass(this, "TObject", null, function () {',
  29547. ' this.$init = function () {',
  29548. ' };',
  29549. ' this.$final = function () {',
  29550. ' };',
  29551. '});',
  29552. 'this.v = undefined;',
  29553. 'this.a = [];',
  29554. 'this.o = null;',
  29555. '']),
  29556. LinesToStr([ // $mod.$main
  29557. '$mod.a = [];',
  29558. '$mod.a = [1];',
  29559. '$mod.a = [1, 2];',
  29560. '$mod.a = [$mod.TFlag.big];',
  29561. '$mod.a = [1, $mod.TFlag.big];',
  29562. '$mod.a = [$mod.o, null];',
  29563. '']));
  29564. end;
  29565. procedure TTestModule.TestJSValue_Params;
  29566. begin
  29567. StartProgram(false);
  29568. Add('type');
  29569. Add(' integer = longint;');
  29570. Add(' TYesNo = boolean;');
  29571. Add(' TFloat = double;');
  29572. Add(' TCaption = string;');
  29573. Add(' TChar = char;');
  29574. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29575. Add('var');
  29576. Add(' l: jsvalue;');
  29577. Add('begin');
  29578. Add(' a:=a;');
  29579. Add(' l:=b;');
  29580. Add(' c:=c;');
  29581. Add(' d:=d;');
  29582. Add(' Result:=l;');
  29583. Add('end;');
  29584. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29585. Add('var');
  29586. Add(' v: jsvalue;');
  29587. Add(' i: integer;');
  29588. Add(' b: TYesNo;');
  29589. Add(' d: TFloat;');
  29590. Add(' s: TCaption;');
  29591. Add(' c: TChar;');
  29592. Add('begin');
  29593. Add(' v:=doit(v,v,v,v);');
  29594. Add(' i:=integer(dosome(i,i));');
  29595. Add(' b:=TYesNo(dosome(b,b));');
  29596. Add(' d:=TFloat(dosome(d,d));');
  29597. Add(' s:=TCaption(dosome(s,s));');
  29598. Add(' c:=TChar(dosome(c,c));');
  29599. ConvertProgram;
  29600. CheckSource('TestJSValue_Params',
  29601. LinesToStr([ // statements
  29602. 'this.DoIt = function (a, b, c, d) {',
  29603. ' var Result = undefined;',
  29604. ' var l = undefined;',
  29605. ' a = a;',
  29606. ' l = b;',
  29607. ' c.set(c.get());',
  29608. ' d.set(d.get());',
  29609. ' Result = l;',
  29610. ' return Result;',
  29611. '};',
  29612. 'this.DoSome = function (a, b) {',
  29613. ' var Result = undefined;',
  29614. ' return Result;',
  29615. '};',
  29616. 'this.v = undefined;',
  29617. 'this.i = 0;',
  29618. 'this.b = false;',
  29619. 'this.d = 0.0;',
  29620. 'this.s = "";',
  29621. 'this.c = "";',
  29622. '']),
  29623. LinesToStr([ // $mod.$main
  29624. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29625. ' p: $mod,',
  29626. ' get: function () {',
  29627. ' return this.p.v;',
  29628. ' },',
  29629. ' set: function (v) {',
  29630. ' this.p.v = v;',
  29631. ' }',
  29632. '}, {',
  29633. ' p: $mod,',
  29634. ' get: function () {',
  29635. ' return this.p.v;',
  29636. ' },',
  29637. ' set: function (v) {',
  29638. ' this.p.v = v;',
  29639. ' }',
  29640. '});',
  29641. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29642. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29643. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29644. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29645. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29646. '']));
  29647. end;
  29648. procedure TTestModule.TestJSValue_UntypedParam;
  29649. begin
  29650. StartProgram(false);
  29651. Add('function DoIt(const a; var b; out c): jsvalue;');
  29652. Add('begin');
  29653. Add(' Result:=a;');
  29654. Add(' Result:=b;');
  29655. Add(' Result:=c;');
  29656. Add(' b:=Result;');
  29657. Add(' c:=Result;');
  29658. Add('end;');
  29659. Add('var i: longint;');
  29660. Add('begin');
  29661. Add(' doit(i,i,i);');
  29662. ConvertProgram;
  29663. CheckSource('TestJSValue_UntypedParam',
  29664. LinesToStr([ // statements
  29665. 'this.DoIt = function (a, b, c) {',
  29666. ' var Result = undefined;',
  29667. ' Result = a;',
  29668. ' Result = b.get();',
  29669. ' Result = c.get();',
  29670. ' b.set(Result);',
  29671. ' c.set(Result);',
  29672. ' return Result;',
  29673. '};',
  29674. 'this.i = 0;',
  29675. '']),
  29676. LinesToStr([ // $mod.$main
  29677. '$mod.DoIt($mod.i, {',
  29678. ' p: $mod,',
  29679. ' get: function () {',
  29680. ' return this.p.i;',
  29681. ' },',
  29682. ' set: function (v) {',
  29683. ' this.p.i = v;',
  29684. ' }',
  29685. '}, {',
  29686. ' p: $mod,',
  29687. ' get: function () {',
  29688. ' return this.p.i;',
  29689. ' },',
  29690. ' set: function (v) {',
  29691. ' this.p.i = v;',
  29692. ' }',
  29693. '});',
  29694. '']));
  29695. end;
  29696. procedure TTestModule.TestJSValue_FuncResultType;
  29697. begin
  29698. StartProgram(false);
  29699. Add('type');
  29700. Add(' integer = longint;');
  29701. Add(' TJSValueArray = array of JSValue;');
  29702. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29703. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29704. Add('begin');
  29705. Add(' while Compare(P,aList[0])>0 do ;');
  29706. Add('end;');
  29707. Add('var');
  29708. Add(' Compare: TListSortCompare;');
  29709. Add(' V: JSValue;');
  29710. Add(' i: integer;');
  29711. Add('begin');
  29712. Add(' if Compare(V,V)>0 then ;');
  29713. Add(' if Compare(i,i)>1 then ;');
  29714. Add(' if Compare(nil,false)>2 then ;');
  29715. Add(' if Compare(1,true)>3 then ;');
  29716. ConvertProgram;
  29717. CheckSource('TestJSValue_UntypedParam',
  29718. LinesToStr([ // statements
  29719. 'this.Sort = function (P, aList, Compare) {',
  29720. ' while (Compare(P, aList[0]) > 0) {',
  29721. ' };',
  29722. '};',
  29723. 'this.Compare = null;',
  29724. 'this.V = undefined;',
  29725. 'this.i = 0;',
  29726. '']),
  29727. LinesToStr([ // $mod.$main
  29728. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29729. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29730. 'if ($mod.Compare(null, false) > 2) ;',
  29731. 'if ($mod.Compare(1, true) > 3) ;',
  29732. '']));
  29733. end;
  29734. procedure TTestModule.TestJSValue_ProcType_Assign;
  29735. begin
  29736. StartProgram(false);
  29737. Add('type');
  29738. Add(' integer = longint;');
  29739. Add(' TObject = class');
  29740. Add(' class function GetGlob: integer;');
  29741. Add(' function Getter: integer;');
  29742. Add(' end;');
  29743. Add('class function TObject.GetGlob: integer;');
  29744. Add('var v1: jsvalue;');
  29745. Add('begin');
  29746. Add(' v1:=@GetGlob;');
  29747. Add(' v1:[email protected];');
  29748. Add('end;');
  29749. Add('function TObject.Getter: integer;');
  29750. Add('var v2: jsvalue;');
  29751. Add('begin');
  29752. Add(' v2:=@Getter;');
  29753. Add(' v2:[email protected];');
  29754. Add(' v2:=@GetGlob;');
  29755. Add(' v2:[email protected];');
  29756. Add('end;');
  29757. Add('function GetIt(i: integer): integer;');
  29758. Add('var v3: jsvalue;');
  29759. Add('begin');
  29760. Add(' v3:=@GetIt;');
  29761. Add('end;');
  29762. Add('var');
  29763. Add(' V: JSValue;');
  29764. Add(' o: TObject;');
  29765. Add('begin');
  29766. Add(' v:=@GetIt;');
  29767. Add(' v:[email protected];');
  29768. Add(' v:[email protected];');
  29769. ConvertProgram;
  29770. CheckSource('TestJSValue_ProcType_Assign',
  29771. LinesToStr([ // statements
  29772. 'rtl.createClass(this, "TObject", null, function () {',
  29773. ' this.$init = function () {',
  29774. ' };',
  29775. ' this.$final = function () {',
  29776. ' };',
  29777. ' this.GetGlob = function () {',
  29778. ' var Result = 0;',
  29779. ' var v1 = undefined;',
  29780. ' v1 = rtl.createCallback(this, "GetGlob");',
  29781. ' v1 = rtl.createCallback(this, "GetGlob");',
  29782. ' return Result;',
  29783. ' };',
  29784. ' this.Getter = function () {',
  29785. ' var Result = 0;',
  29786. ' var v2 = undefined;',
  29787. ' v2 = rtl.createCallback(this, "Getter");',
  29788. ' v2 = rtl.createCallback(this, "Getter");',
  29789. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29790. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29791. ' return Result;',
  29792. ' };',
  29793. '});',
  29794. 'this.GetIt = function (i) {',
  29795. ' var Result = 0;',
  29796. ' var v3 = undefined;',
  29797. ' v3 = $mod.GetIt;',
  29798. ' return Result;',
  29799. '};',
  29800. 'this.V = undefined;',
  29801. 'this.o = null;',
  29802. '']),
  29803. LinesToStr([ // $mod.$main
  29804. '$mod.V = $mod.GetIt;',
  29805. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29806. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29807. '']));
  29808. end;
  29809. procedure TTestModule.TestJSValue_ProcType_Equal;
  29810. begin
  29811. StartProgram(false);
  29812. Add('type');
  29813. Add(' integer = longint;');
  29814. Add(' TObject = class');
  29815. Add(' class function GetGlob: integer;');
  29816. Add(' function Getter: integer;');
  29817. Add(' end;');
  29818. Add('class function TObject.GetGlob: integer;');
  29819. Add('var v1: jsvalue;');
  29820. Add('begin');
  29821. Add(' if v1=@GetGlob then;');
  29822. Add(' if [email protected] then ;');
  29823. Add('end;');
  29824. Add('function TObject.Getter: integer;');
  29825. Add('var v2: jsvalue;');
  29826. Add('begin');
  29827. Add(' if v2=@Getter then;');
  29828. Add(' if [email protected] then ;');
  29829. Add(' if v2=@GetGlob then;');
  29830. Add(' if [email protected] then;');
  29831. Add('end;');
  29832. Add('function GetIt(i: integer): integer;');
  29833. Add('var v3: jsvalue;');
  29834. Add('begin');
  29835. Add(' if v3=@GetIt then;');
  29836. Add('end;');
  29837. Add('var');
  29838. Add(' V: JSValue;');
  29839. Add(' o: TObject;');
  29840. Add('begin');
  29841. Add(' if v=@GetIt then;');
  29842. Add(' if [email protected] then;');
  29843. Add(' if [email protected] then;');
  29844. Add(' if @GetIt=v then;');
  29845. Add(' if @o.Getter=v then;');
  29846. Add(' if @o.GetGlob=v then;');
  29847. ConvertProgram;
  29848. CheckSource('TestJSValue_ProcType_Equal',
  29849. LinesToStr([ // statements
  29850. 'rtl.createClass(this, "TObject", null, function () {',
  29851. ' this.$init = function () {',
  29852. ' };',
  29853. ' this.$final = function () {',
  29854. ' };',
  29855. ' this.GetGlob = function () {',
  29856. ' var Result = 0;',
  29857. ' var v1 = undefined;',
  29858. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29859. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29860. ' return Result;',
  29861. ' };',
  29862. ' this.Getter = function () {',
  29863. ' var Result = 0;',
  29864. ' var v2 = undefined;',
  29865. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29866. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29867. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29868. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29869. ' return Result;',
  29870. ' };',
  29871. '});',
  29872. 'this.GetIt = function (i) {',
  29873. ' var Result = 0;',
  29874. ' var v3 = undefined;',
  29875. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29876. ' return Result;',
  29877. '};',
  29878. 'this.V = undefined;',
  29879. 'this.o = null;',
  29880. '']),
  29881. LinesToStr([ // $mod.$main
  29882. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29883. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29884. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29885. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29886. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29887. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29888. '']));
  29889. end;
  29890. procedure TTestModule.TestJSValue_ProcType_Param;
  29891. begin
  29892. StartProgram(false);
  29893. Add([
  29894. 'type',
  29895. ' variant = jsvalue;',
  29896. ' TArrVariant = array of variant;',
  29897. ' TArrVar2 = TArrVariant;',
  29898. ' TFuncInt = function: longint;',
  29899. 'function GetIt: longint;',
  29900. 'begin',
  29901. 'end;',
  29902. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29903. 'var v: variant;',
  29904. 'begin',
  29905. ' v:=arr[1];',
  29906. 'end;',
  29907. 'var s: string;',
  29908. 'begin',
  29909. ' DoIt(GetIt,[]);',
  29910. ' DoIt(@GetIt,[]);',
  29911. ' DoIt(1,[s,GetIt]);',
  29912. ' DoIt(1,[s,@GetIt]);',
  29913. '']);
  29914. ConvertProgram;
  29915. CheckSource('TestJSValue_ProcType_Param',
  29916. LinesToStr([ // statements
  29917. 'this.GetIt = function () {',
  29918. ' var Result = 0;',
  29919. ' return Result;',
  29920. '};',
  29921. 'this.DoIt = function (p, Arr) {',
  29922. ' var v = undefined;',
  29923. ' v = Arr[1];',
  29924. '};',
  29925. 'this.s = "";',
  29926. '']),
  29927. LinesToStr([ // $mod.$main
  29928. '$mod.DoIt($mod.GetIt(), []);',
  29929. '$mod.DoIt($mod.GetIt, []);',
  29930. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29931. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29932. '']));
  29933. end;
  29934. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29935. begin
  29936. StartProgram(false);
  29937. Add([
  29938. 'var',
  29939. ' v: JSValue;',
  29940. ' p: Pointer;',
  29941. 'begin',
  29942. ' p:=v;',
  29943. '']);
  29944. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29945. nIncompatibleTypesGotExpected);
  29946. ConvertProgram;
  29947. end;
  29948. procedure TTestModule.TestJSValue_OverloadDouble;
  29949. begin
  29950. StartProgram(false);
  29951. Add([
  29952. 'type',
  29953. ' integer = longint;',
  29954. ' tdatetime = double;',
  29955. 'procedure DoIt(d: double); begin end;',
  29956. 'procedure DoIt(v: jsvalue); begin end;',
  29957. 'var',
  29958. ' d: double;',
  29959. ' dt: tdatetime;',
  29960. ' i: integer;',
  29961. ' b: byte;',
  29962. ' shi: shortint;',
  29963. ' w: word;',
  29964. ' smi: smallint;',
  29965. ' lw: longword;',
  29966. ' li: longint;',
  29967. ' ni: nativeint;',
  29968. ' nu: nativeuint;',
  29969. 'begin',
  29970. ' DoIt(d);',
  29971. ' DoIt(dt);',
  29972. ' DoIt(i);',
  29973. ' DoIt(b);',
  29974. ' DoIt(shi);',
  29975. ' DoIt(w);',
  29976. ' DoIt(smi);',
  29977. ' DoIt(lw);',
  29978. ' DoIt(li);',
  29979. ' DoIt(ni);',
  29980. ' DoIt(nu);',
  29981. '']);
  29982. ConvertProgram;
  29983. CheckSource('TestJSValue_OverloadDouble',
  29984. LinesToStr([ // statements
  29985. 'this.DoIt = function (d) {',
  29986. '};',
  29987. 'this.DoIt$1 = function (v) {',
  29988. '};',
  29989. 'this.d = 0.0;',
  29990. 'this.dt = 0.0;',
  29991. 'this.i = 0;',
  29992. 'this.b = 0;',
  29993. 'this.shi = 0;',
  29994. 'this.w = 0;',
  29995. 'this.smi = 0;',
  29996. 'this.lw = 0;',
  29997. 'this.li = 0;',
  29998. 'this.ni = 0;',
  29999. 'this.nu = 0;',
  30000. '']),
  30001. LinesToStr([ // $mod.$main
  30002. '$mod.DoIt($mod.d);',
  30003. '$mod.DoIt($mod.dt);',
  30004. '$mod.DoIt$1($mod.i);',
  30005. '$mod.DoIt$1($mod.b);',
  30006. '$mod.DoIt$1($mod.shi);',
  30007. '$mod.DoIt$1($mod.w);',
  30008. '$mod.DoIt$1($mod.smi);',
  30009. '$mod.DoIt$1($mod.lw);',
  30010. '$mod.DoIt$1($mod.li);',
  30011. '$mod.DoIt$1($mod.ni);',
  30012. '$mod.DoIt$1($mod.nu);',
  30013. '']));
  30014. end;
  30015. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30016. begin
  30017. StartProgram(false);
  30018. Add([
  30019. 'type',
  30020. ' integer = longint;',
  30021. ' int53 = nativeint;',
  30022. ' tdatetime = double;',
  30023. 'procedure DoIt(n: nativeint); begin end;',
  30024. 'procedure DoIt(v: jsvalue); begin end;',
  30025. 'var',
  30026. ' d: double;',
  30027. ' dt: tdatetime;',
  30028. ' i: integer;',
  30029. ' b: byte;',
  30030. ' shi: shortint;',
  30031. ' w: word;',
  30032. ' smi: smallint;',
  30033. ' lw: longword;',
  30034. ' li: longint;',
  30035. ' ni: nativeint;',
  30036. ' nu: nativeuint;',
  30037. 'begin',
  30038. ' DoIt(d);',
  30039. ' DoIt(dt);',
  30040. ' DoIt(i);',
  30041. ' DoIt(b);',
  30042. ' DoIt(shi);',
  30043. ' DoIt(w);',
  30044. ' DoIt(smi);',
  30045. ' DoIt(lw);',
  30046. ' DoIt(li);',
  30047. ' DoIt(ni);',
  30048. ' DoIt(nu);',
  30049. '']);
  30050. ConvertProgram;
  30051. CheckSource('TestJSValue_OverloadNativeInt',
  30052. LinesToStr([ // statements
  30053. 'this.DoIt = function (n) {',
  30054. '};',
  30055. 'this.DoIt$1 = function (v) {',
  30056. '};',
  30057. 'this.d = 0.0;',
  30058. 'this.dt = 0.0;',
  30059. 'this.i = 0;',
  30060. 'this.b = 0;',
  30061. 'this.shi = 0;',
  30062. 'this.w = 0;',
  30063. 'this.smi = 0;',
  30064. 'this.lw = 0;',
  30065. 'this.li = 0;',
  30066. 'this.ni = 0;',
  30067. 'this.nu = 0;',
  30068. '']),
  30069. LinesToStr([ // $mod.$main
  30070. '$mod.DoIt$1($mod.d);',
  30071. '$mod.DoIt$1($mod.dt);',
  30072. '$mod.DoIt($mod.i);',
  30073. '$mod.DoIt($mod.b);',
  30074. '$mod.DoIt($mod.shi);',
  30075. '$mod.DoIt($mod.w);',
  30076. '$mod.DoIt($mod.smi);',
  30077. '$mod.DoIt($mod.lw);',
  30078. '$mod.DoIt($mod.li);',
  30079. '$mod.DoIt($mod.ni);',
  30080. '$mod.DoIt($mod.nu);',
  30081. '']));
  30082. end;
  30083. procedure TTestModule.TestJSValue_OverloadWord;
  30084. begin
  30085. StartProgram(false);
  30086. Add([
  30087. 'type',
  30088. ' integer = longint;',
  30089. ' int53 = nativeint;',
  30090. ' tdatetime = double;',
  30091. 'procedure DoIt(w: word); begin end;',
  30092. 'procedure DoIt(v: jsvalue); begin end;',
  30093. 'var',
  30094. ' d: double;',
  30095. ' dt: tdatetime;',
  30096. ' i: integer;',
  30097. ' b: byte;',
  30098. ' shi: shortint;',
  30099. ' w: word;',
  30100. ' smi: smallint;',
  30101. ' lw: longword;',
  30102. ' li: longint;',
  30103. ' ni: nativeint;',
  30104. ' nu: nativeuint;',
  30105. 'begin',
  30106. ' DoIt(d);',
  30107. ' DoIt(dt);',
  30108. ' DoIt(i);',
  30109. ' DoIt(b);',
  30110. ' DoIt(shi);',
  30111. ' DoIt(w);',
  30112. ' DoIt(smi);',
  30113. ' DoIt(lw);',
  30114. ' DoIt(li);',
  30115. ' DoIt(ni);',
  30116. ' DoIt(nu);',
  30117. '']);
  30118. ConvertProgram;
  30119. CheckSource('TestJSValue_OverloadWord',
  30120. LinesToStr([ // statements
  30121. 'this.DoIt = function (w) {',
  30122. '};',
  30123. 'this.DoIt$1 = function (v) {',
  30124. '};',
  30125. 'this.d = 0.0;',
  30126. 'this.dt = 0.0;',
  30127. 'this.i = 0;',
  30128. 'this.b = 0;',
  30129. 'this.shi = 0;',
  30130. 'this.w = 0;',
  30131. 'this.smi = 0;',
  30132. 'this.lw = 0;',
  30133. 'this.li = 0;',
  30134. 'this.ni = 0;',
  30135. 'this.nu = 0;',
  30136. '']),
  30137. LinesToStr([ // $mod.$main
  30138. '$mod.DoIt$1($mod.d);',
  30139. '$mod.DoIt$1($mod.dt);',
  30140. '$mod.DoIt$1($mod.i);',
  30141. '$mod.DoIt($mod.b);',
  30142. '$mod.DoIt($mod.shi);',
  30143. '$mod.DoIt($mod.w);',
  30144. '$mod.DoIt$1($mod.smi);',
  30145. '$mod.DoIt$1($mod.lw);',
  30146. '$mod.DoIt$1($mod.li);',
  30147. '$mod.DoIt$1($mod.ni);',
  30148. '$mod.DoIt$1($mod.nu);',
  30149. '']));
  30150. end;
  30151. procedure TTestModule.TestJSValue_OverloadString;
  30152. begin
  30153. StartProgram(false);
  30154. Add([
  30155. 'type',
  30156. ' uni = string;',
  30157. ' WChar = char;',
  30158. 'procedure DoIt(s: string); begin end;',
  30159. 'procedure DoIt(v: jsvalue); begin end;',
  30160. 'var',
  30161. ' s: string;',
  30162. ' c: char;',
  30163. ' u: uni;',
  30164. 'begin',
  30165. ' DoIt(s);',
  30166. ' DoIt(c);',
  30167. ' DoIt(u);',
  30168. '']);
  30169. ConvertProgram;
  30170. CheckSource('TestJSValue_OverloadString',
  30171. LinesToStr([ // statements
  30172. 'this.DoIt = function (s) {',
  30173. '};',
  30174. 'this.DoIt$1 = function (v) {',
  30175. '};',
  30176. 'this.s = "";',
  30177. 'this.c = "";',
  30178. 'this.u = "";',
  30179. '']),
  30180. LinesToStr([ // $mod.$main
  30181. '$mod.DoIt($mod.s);',
  30182. '$mod.DoIt($mod.c);',
  30183. '$mod.DoIt($mod.u);',
  30184. '']));
  30185. end;
  30186. procedure TTestModule.TestJSValue_OverloadChar;
  30187. begin
  30188. StartProgram(false);
  30189. Add([
  30190. 'type',
  30191. ' uni = string;',
  30192. ' WChar = char;',
  30193. 'procedure DoIt(c: char); begin end;',
  30194. 'procedure DoIt(v: jsvalue); begin end;',
  30195. 'var',
  30196. ' s: string;',
  30197. ' c: char;',
  30198. ' u: uni;',
  30199. 'begin',
  30200. ' DoIt(s);',
  30201. ' DoIt(c);',
  30202. ' DoIt(u);',
  30203. '']);
  30204. ConvertProgram;
  30205. CheckSource('TestJSValue_OverloadChar',
  30206. LinesToStr([ // statements
  30207. 'this.DoIt = function (c) {',
  30208. '};',
  30209. 'this.DoIt$1 = function (v) {',
  30210. '};',
  30211. 'this.s = "";',
  30212. 'this.c = "";',
  30213. 'this.u = "";',
  30214. '']),
  30215. LinesToStr([ // $mod.$main
  30216. '$mod.DoIt$1($mod.s);',
  30217. '$mod.DoIt($mod.c);',
  30218. '$mod.DoIt$1($mod.u);',
  30219. '']));
  30220. end;
  30221. procedure TTestModule.TestJSValue_OverloadPointer;
  30222. begin
  30223. StartProgram(false);
  30224. Add([
  30225. 'type',
  30226. ' TObject = class end;',
  30227. 'procedure DoIt(p: pointer); begin end;',
  30228. 'procedure DoIt(v: jsvalue); begin end;',
  30229. 'var',
  30230. ' o: TObject;',
  30231. 'begin',
  30232. ' DoIt(o);',
  30233. '']);
  30234. ConvertProgram;
  30235. CheckSource('TestJSValue_OverloadPointer',
  30236. LinesToStr([ // statements
  30237. 'rtl.createClass(this, "TObject", null, function () {',
  30238. ' this.$init = function () {',
  30239. ' };',
  30240. ' this.$final = function () {',
  30241. ' };',
  30242. '});',
  30243. 'this.DoIt = function (p) {',
  30244. '};',
  30245. 'this.DoIt$1 = function (v) {',
  30246. '};',
  30247. 'this.o = null;',
  30248. '']),
  30249. LinesToStr([ // $mod.$main
  30250. '$mod.DoIt($mod.o);',
  30251. '']));
  30252. end;
  30253. procedure TTestModule.TestJSValue_ForIn;
  30254. begin
  30255. StartProgram(false);
  30256. Add([
  30257. 'var',
  30258. ' v: JSValue;',
  30259. ' key: string;',
  30260. 'begin',
  30261. ' for key in v do begin',
  30262. ' if key=''abc'' then ;',
  30263. ' end;',
  30264. '']);
  30265. ConvertProgram;
  30266. CheckSource('TestJSValue_ForIn',
  30267. LinesToStr([ // statements
  30268. 'this.v = undefined;',
  30269. 'this.key = "";',
  30270. '']),
  30271. LinesToStr([ // $mod.$main
  30272. 'for ($mod.key in $mod.v) {',
  30273. ' if ($mod.key === "abc") ;',
  30274. '};',
  30275. '']));
  30276. end;
  30277. procedure TTestModule.TestRTTI_IntRange;
  30278. begin
  30279. WithTypeInfo:=true;
  30280. StartProgram(true,[supTypeInfo]);
  30281. Add([
  30282. '{$modeswitch externalclass}',
  30283. 'type',
  30284. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30285. ' TColor = type TGraphicsColor;',
  30286. 'var',
  30287. ' p: TTypeInfo;',
  30288. ' k: TTypeKind;',
  30289. 'begin',
  30290. ' p:=typeinfo(TGraphicsColor);',
  30291. ' p:=typeinfo(TColor);',
  30292. ' k:=GetTypeKind(TGraphicsColor);',
  30293. ' k:=GetTypeKind(TColor);',
  30294. '']);
  30295. ConvertProgram;
  30296. CheckSource('TestRTTI_IntRange',
  30297. LinesToStr([ // statements
  30298. 'this.$rtti.$Int("TGraphicsColor", {',
  30299. ' minvalue: -2147483648,',
  30300. ' maxvalue: 2147483647,',
  30301. ' ordtype: 4',
  30302. '});',
  30303. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30304. 'this.p = null;',
  30305. 'this.k = 0;',
  30306. '']),
  30307. LinesToStr([ // $mod.$main
  30308. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30309. '$mod.p = $mod.$rtti["TColor"];',
  30310. '$mod.k = 1;',
  30311. '$mod.k = 1;',
  30312. '']));
  30313. end;
  30314. procedure TTestModule.TestRTTI_Double;
  30315. begin
  30316. WithTypeInfo:=true;
  30317. StartProgram(true,[supTypeInfo]);
  30318. Add([
  30319. '{$modeswitch externalclass}',
  30320. 'type',
  30321. ' TFloat = type double;',
  30322. 'var',
  30323. ' p: TTypeInfo;',
  30324. 'begin',
  30325. ' p:=typeinfo(double);',
  30326. ' p:=typeinfo(TFloat);',
  30327. '']);
  30328. ConvertProgram;
  30329. CheckSource('TestRTTI_Double',
  30330. LinesToStr([ // statements
  30331. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30332. 'this.p = null;',
  30333. '']),
  30334. LinesToStr([ // $mod.$main
  30335. '$mod.p = rtl.double;',
  30336. '$mod.p = $mod.$rtti["TFloat"];',
  30337. '']));
  30338. end;
  30339. procedure TTestModule.TestRTTI_ProcType;
  30340. begin
  30341. WithTypeInfo:=true;
  30342. StartProgram(false);
  30343. Add('type');
  30344. Add(' TProcA = procedure;');
  30345. Add(' TMethodB = procedure of object;');
  30346. Add(' TProcC = procedure; varargs;');
  30347. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30348. Add(' TProcE = function: nativeint;');
  30349. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30350. Add('var p: pointer;');
  30351. Add('begin');
  30352. Add(' p:=typeinfo(tproca);');
  30353. ConvertProgram;
  30354. CheckSource('TestRTTI_ProcType',
  30355. LinesToStr([ // statements
  30356. 'this.$rtti.$ProcVar("TProcA", {',
  30357. ' procsig: rtl.newTIProcSig([])',
  30358. '});',
  30359. 'this.$rtti.$MethodVar("TMethodB", {',
  30360. ' procsig: rtl.newTIProcSig([]),',
  30361. ' methodkind: 0',
  30362. '});',
  30363. 'this.$rtti.$ProcVar("TProcC", {',
  30364. ' procsig: rtl.newTIProcSig([], null, 2)',
  30365. '});',
  30366. 'this.$rtti.$ProcVar("TProcD", {',
  30367. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30368. '});',
  30369. 'this.$rtti.$ProcVar("TProcE", {',
  30370. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30371. '});',
  30372. 'this.$rtti.$ProcVar("TProcF", {',
  30373. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30374. '});',
  30375. 'this.p = null;',
  30376. '']),
  30377. LinesToStr([ // $mod.$main
  30378. '$mod.p = $mod.$rtti["TProcA"];',
  30379. '']));
  30380. end;
  30381. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30382. begin
  30383. WithTypeInfo:=true;
  30384. AddModuleWithIntfImplSrc('unit2.pas',
  30385. LinesToStr([
  30386. 'type',
  30387. ' TObject = class end;'
  30388. ]),
  30389. '');
  30390. StartUnit(true);
  30391. Add('interface');
  30392. Add('uses unit2;');
  30393. Add('type');
  30394. Add(' TProcA = function(o: tobject): tobject;');
  30395. Add('implementation');
  30396. Add('type');
  30397. Add(' TProcB = function(o: tobject): tobject;');
  30398. Add('var p: Pointer;');
  30399. Add('initialization');
  30400. Add(' p:=typeinfo(tproca);');
  30401. Add(' p:=typeinfo(tprocb);');
  30402. ConvertUnit;
  30403. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30404. LinesToStr([ // statements
  30405. 'var $impl = $mod.$impl;',
  30406. 'this.$rtti.$ProcVar("TProcA", {',
  30407. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30408. '});',
  30409. '']),
  30410. LinesToStr([ // this.$init
  30411. '$impl.p = $mod.$rtti["TProcA"];',
  30412. '$impl.p = $mod.$rtti["TProcB"];',
  30413. '']),
  30414. LinesToStr([ // implementation
  30415. '$mod.$rtti.$ProcVar("TProcB", {',
  30416. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30417. '});',
  30418. '$impl.p = null;',
  30419. '']) );
  30420. end;
  30421. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30422. begin
  30423. WithTypeInfo:=true;
  30424. StartProgram(false);
  30425. Add(['var',
  30426. ' ProcA: procedure;',
  30427. ' MethodB: procedure of object;',
  30428. ' ProcC: procedure; varargs;',
  30429. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30430. ' ProcE: function: nativeint;',
  30431. ' p: pointer;',
  30432. 'begin',
  30433. ' p:=typeinfo(proca);']);
  30434. ConvertProgram;
  30435. CheckSource('TestRTTI_ProcTypeAnonymous',
  30436. LinesToStr([ // statements
  30437. 'this.$rtti.$ProcVar("ProcA$a", {',
  30438. ' procsig: rtl.newTIProcSig([])',
  30439. '});',
  30440. 'this.ProcA = null;',
  30441. 'this.$rtti.$MethodVar("MethodB$a", {',
  30442. ' procsig: rtl.newTIProcSig([]),',
  30443. ' methodkind: 0',
  30444. '});',
  30445. 'this.MethodB = null;',
  30446. 'this.$rtti.$ProcVar("ProcC$a", {',
  30447. ' procsig: rtl.newTIProcSig([], null, 2)',
  30448. '});',
  30449. 'this.ProcC = null;',
  30450. 'this.$rtti.$ProcVar("ProcD$a", {',
  30451. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30452. '});',
  30453. 'this.ProcD = null;',
  30454. 'this.$rtti.$ProcVar("ProcE$a", {',
  30455. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30456. '});',
  30457. 'this.ProcE = null;',
  30458. 'this.p = null;',
  30459. '']),
  30460. LinesToStr([ // $mod.$main
  30461. '$mod.p = $mod.$rtti["ProcA$a"];',
  30462. '']));
  30463. end;
  30464. procedure TTestModule.TestRTTI_EnumAndSetType;
  30465. begin
  30466. WithTypeInfo:=true;
  30467. StartProgram(false);
  30468. Add('type');
  30469. Add(' TFlag = (light,dark);');
  30470. Add(' TFlags = set of TFlag;');
  30471. Add(' TProc = function(f: TFlags): TFlag;');
  30472. Add('var p: pointer;');
  30473. Add('begin');
  30474. Add(' p:=typeinfo(tflag);');
  30475. Add(' p:=typeinfo(tflags);');
  30476. ConvertProgram;
  30477. CheckSource('TestRTTI_EnumAndType',
  30478. LinesToStr([ // statements
  30479. 'this.TFlag = {',
  30480. ' "0": "light",',
  30481. ' light: 0,',
  30482. ' "1": "dark",',
  30483. ' dark: 1',
  30484. '};',
  30485. 'this.$rtti.$Enum("TFlag", {',
  30486. ' minvalue: 0,',
  30487. ' maxvalue: 1,',
  30488. ' ordtype: 1,',
  30489. ' enumtype: this.TFlag',
  30490. '});',
  30491. 'this.$rtti.$Set("TFlags", {',
  30492. ' comptype: this.$rtti["TFlag"]',
  30493. '});',
  30494. 'this.$rtti.$ProcVar("TProc", {',
  30495. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30496. '});',
  30497. 'this.p = null;',
  30498. '']),
  30499. LinesToStr([ // $mod.$main
  30500. '$mod.p = $mod.$rtti["TFlag"];',
  30501. '$mod.p = $mod.$rtti["TFlags"];',
  30502. '']));
  30503. end;
  30504. procedure TTestModule.TestRTTI_EnumRange;
  30505. begin
  30506. WithTypeInfo:=true;
  30507. StartProgram(false);
  30508. Add([
  30509. 'type',
  30510. ' TCol = (red,green,blue);',
  30511. ' TColRg = green..blue;',
  30512. ' TSetOfColRg = set of TColRg;',
  30513. 'var p: pointer;',
  30514. 'begin',
  30515. ' p:=typeinfo(tcolrg);',
  30516. ' p:=typeinfo(tsetofcolrg);',
  30517. '']);
  30518. ConvertProgram;
  30519. end;
  30520. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30521. begin
  30522. WithTypeInfo:=true;
  30523. StartProgram(false);
  30524. Add('type');
  30525. Add(' TFlags = set of (red, green);');
  30526. Add('var');
  30527. Add(' f: TFlags;');
  30528. Add('begin');
  30529. Add(' Include(f,red);');
  30530. ConvertProgram;
  30531. CheckSource('TestRTTI_AnonymousEnumType',
  30532. LinesToStr([ // statements
  30533. 'this.TFlags$a = {',
  30534. ' "0": "red",',
  30535. ' red: 0,',
  30536. ' "1": "green",',
  30537. ' green: 1',
  30538. '};',
  30539. 'this.$rtti.$Enum("TFlags$a", {',
  30540. ' minvalue: 0,',
  30541. ' maxvalue: 1,',
  30542. ' ordtype: 1,',
  30543. ' enumtype: this.TFlags$a',
  30544. '});',
  30545. 'this.$rtti.$Set("TFlags", {',
  30546. ' comptype: this.$rtti["TFlags$a"]',
  30547. '});',
  30548. 'this.f = {};',
  30549. '']),
  30550. LinesToStr([
  30551. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30552. '']));
  30553. end;
  30554. procedure TTestModule.TestRTTI_StaticArray;
  30555. begin
  30556. WithTypeInfo:=true;
  30557. StartProgram(false);
  30558. Add('type');
  30559. Add(' TFlag = (light,dark);');
  30560. Add(' TFlagNames = array[TFlag] of string;');
  30561. Add(' TBoolNames = array[boolean] of string;');
  30562. Add(' TByteArray = array[1..32768] of byte;');
  30563. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30564. Add('var p: pointer;');
  30565. Add('begin');
  30566. Add(' p:=typeinfo(TFlagNames);');
  30567. Add(' p:=typeinfo(TBoolNames);');
  30568. ConvertProgram;
  30569. CheckSource('TestRTTI_StaticArray',
  30570. LinesToStr([ // statements
  30571. 'this.TFlag = {',
  30572. ' "0": "light",',
  30573. ' light: 0,',
  30574. ' "1": "dark",',
  30575. ' dark: 1',
  30576. '};',
  30577. 'this.$rtti.$Enum("TFlag", {',
  30578. ' minvalue: 0,',
  30579. ' maxvalue: 1,',
  30580. ' ordtype: 1,',
  30581. ' enumtype: this.TFlag',
  30582. '});',
  30583. 'this.$rtti.$StaticArray("TFlagNames", {',
  30584. ' dims: [2],',
  30585. ' eltype: rtl.string',
  30586. '});',
  30587. 'this.$rtti.$StaticArray("TBoolNames", {',
  30588. ' dims: [2],',
  30589. ' eltype: rtl.string',
  30590. '});',
  30591. 'this.$rtti.$StaticArray("TByteArray", {',
  30592. ' dims: [32768],',
  30593. ' eltype: rtl.byte',
  30594. '});',
  30595. 'this.$rtti.$ProcVar("TProc", {',
  30596. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30597. '});',
  30598. 'this.p = null;',
  30599. '']),
  30600. LinesToStr([ // $mod.$main
  30601. '$mod.p = $mod.$rtti["TFlagNames"];',
  30602. '$mod.p = $mod.$rtti["TBoolNames"];',
  30603. '']));
  30604. end;
  30605. procedure TTestModule.TestRTTI_DynArray;
  30606. begin
  30607. WithTypeInfo:=true;
  30608. StartProgram(false);
  30609. Add('type');
  30610. Add(' TArrStr = array of string;');
  30611. Add(' TArr2Dim = array of tarrstr;');
  30612. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30613. Add('var p: pointer;');
  30614. Add('begin');
  30615. Add(' p:=typeinfo(tarrstr);');
  30616. Add(' p:=typeinfo(tarr2dim);');
  30617. ConvertProgram;
  30618. CheckSource('TestRTTI_DynArray',
  30619. LinesToStr([ // statements
  30620. 'this.$rtti.$DynArray("TArrStr", {',
  30621. ' eltype: rtl.string',
  30622. '});',
  30623. 'this.$rtti.$DynArray("TArr2Dim", {',
  30624. ' eltype: this.$rtti["TArrStr"]',
  30625. '});',
  30626. 'this.$rtti.$ProcVar("TProc", {',
  30627. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30628. '});',
  30629. 'this.p = null;',
  30630. '']),
  30631. LinesToStr([ // $mod.$main
  30632. '$mod.p = $mod.$rtti["TArrStr"];',
  30633. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30634. '']));
  30635. end;
  30636. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30637. begin
  30638. WithTypeInfo:=true;
  30639. StartProgram(false);
  30640. Add('type');
  30641. Add(' TArr = array of array of longint;');
  30642. Add('var a: TArr;');
  30643. Add('begin');
  30644. ConvertProgram;
  30645. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30646. LinesToStr([ // statements
  30647. 'this.$rtti.$DynArray("TArr$a", {',
  30648. ' eltype: rtl.longint',
  30649. '});',
  30650. 'this.$rtti.$DynArray("TArr", {',
  30651. ' eltype: this.$rtti["TArr$a"]',
  30652. '});',
  30653. 'this.a = [];',
  30654. '']),
  30655. LinesToStr([ // $mod.$main
  30656. ]));
  30657. end;
  30658. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30659. begin
  30660. WithTypeInfo:=true;
  30661. StartProgram(false);
  30662. Add('type');
  30663. Add(' TObject = class');
  30664. Add(' published');
  30665. Add(' procedure Proc; virtual; abstract;');
  30666. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30667. Add(' end;');
  30668. Add('begin');
  30669. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30670. nDuplicatePublishedMethodXAtY);
  30671. ConvertProgram;
  30672. end;
  30673. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30674. begin
  30675. WithTypeInfo:=true;
  30676. StartUnit(false);
  30677. Add([
  30678. 'interface',
  30679. 'type',
  30680. ' TObject = class',
  30681. ' end;',
  30682. ' {$M+}',
  30683. ' TBird = class',
  30684. ' procedure Fly;',
  30685. ' end;',
  30686. ' {$M-}',
  30687. 'type',
  30688. ' TEagle = class(TBird)',
  30689. ' procedure Fly;',
  30690. ' end;',
  30691. 'implementation',
  30692. 'procedure TBird.Fly;',
  30693. 'begin',
  30694. 'end;',
  30695. 'procedure TEagle.Fly;',
  30696. 'begin',
  30697. 'end;',
  30698. '']);
  30699. ConvertUnit;
  30700. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30701. LinesToStr([ // statements
  30702. 'rtl.createClass(this, "TObject", null, function () {',
  30703. ' this.$init = function () {',
  30704. ' };',
  30705. ' this.$final = function () {',
  30706. ' };',
  30707. '});',
  30708. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30709. ' this.Fly = function () {',
  30710. ' };',
  30711. ' var $r = this.$rtti;',
  30712. ' $r.addMethod("Fly", 0, []);',
  30713. '});',
  30714. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30715. ' this.Fly = function () {',
  30716. ' };',
  30717. ' var $r = this.$rtti;',
  30718. ' $r.addMethod("Fly", 0, []);',
  30719. '});',
  30720. '']),
  30721. LinesToStr([ // $mod.$main
  30722. ]));
  30723. CheckResolverUnexpectedHints(true);
  30724. end;
  30725. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30726. begin
  30727. WithTypeInfo:=true;
  30728. StartProgram(false);
  30729. Add('type');
  30730. Add(' TObject = class');
  30731. Add(' published');
  30732. Add(' procedure Proc; external name ''foo'';');
  30733. Add(' end;');
  30734. Add('begin');
  30735. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30736. nPublishedNameMustMatchExternal);
  30737. ConvertProgram;
  30738. end;
  30739. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30740. begin
  30741. WithTypeInfo:=true;
  30742. StartProgram(false);
  30743. Add('type');
  30744. Add(' TObject = class');
  30745. Add(' class var FA: longint;');
  30746. Add(' published');
  30747. Add(' class property A: longint read FA;');
  30748. Add(' end;');
  30749. Add('begin');
  30750. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30751. nInvalidXModifierY);
  30752. ConvertProgram;
  30753. end;
  30754. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30755. begin
  30756. WithTypeInfo:=true;
  30757. StartProgram(false);
  30758. Add('type');
  30759. Add(' TObject = class');
  30760. Add(' published');
  30761. Add(' class var FA: longint;');
  30762. Add(' end;');
  30763. Add('begin');
  30764. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30765. nSymbolCannotBePublished);
  30766. ConvertProgram;
  30767. end;
  30768. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30769. begin
  30770. WithTypeInfo:=true;
  30771. StartProgram(false);
  30772. Add('{$modeswitch externalclass}');
  30773. Add('type');
  30774. Add(' TObject = class');
  30775. Add(' published');
  30776. Add(' V: longint; external name ''foo'';');
  30777. Add(' end;');
  30778. Add('begin');
  30779. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30780. nPublishedNameMustMatchExternal);
  30781. ConvertProgram;
  30782. end;
  30783. procedure TTestModule.TestRTTI_Class_Field;
  30784. begin
  30785. WithTypeInfo:=true;
  30786. StartProgram(false);
  30787. Add('{$modeswitch externalclass}');
  30788. Add('type');
  30789. Add(' TObject = class');
  30790. Add(' private');
  30791. Add(' FPropA: string;');
  30792. Add(' published');
  30793. Add(' VarLI: longint;');
  30794. Add(' VarC: char;');
  30795. Add(' VarS: string;');
  30796. Add(' VarD: double;');
  30797. Add(' VarB: boolean;');
  30798. Add(' VarLW: longword;');
  30799. Add(' VarSmI: smallint;');
  30800. Add(' VarW: word;');
  30801. Add(' VarShI: shortint;');
  30802. Add(' VarBy: byte;');
  30803. Add(' VarExt: longint external name ''VarExt'';');
  30804. Add(' ArrA, ArrB: array of byte;');
  30805. Add(' end;');
  30806. Add('var p: pointer;');
  30807. Add(' Obj: tobject;');
  30808. Add('begin');
  30809. Add(' p:=typeinfo(tobject);');
  30810. Add(' p:=typeinfo(p);');
  30811. Add(' p:=typeinfo(obj);');
  30812. ConvertProgram;
  30813. CheckSource('TestRTTI_Class_Field',
  30814. LinesToStr([ // statements
  30815. 'rtl.createClass(this, "TObject", null, function () {',
  30816. ' this.$init = function () {',
  30817. ' this.FPropA = "";',
  30818. ' this.VarLI = 0;',
  30819. ' this.VarC = "";',
  30820. ' this.VarS = "";',
  30821. ' this.VarD = 0.0;',
  30822. ' this.VarB = false;',
  30823. ' this.VarLW = 0;',
  30824. ' this.VarSmI = 0;',
  30825. ' this.VarW = 0;',
  30826. ' this.VarShI = 0;',
  30827. ' this.VarBy = 0;',
  30828. ' this.ArrA = [];',
  30829. ' this.ArrB = [];',
  30830. ' };',
  30831. ' this.$final = function () {',
  30832. ' this.ArrA = undefined;',
  30833. ' this.ArrB = undefined;',
  30834. ' };',
  30835. ' var $r = this.$rtti;',
  30836. ' $r.addField("VarLI", rtl.longint);',
  30837. ' $r.addField("VarC", rtl.char);',
  30838. ' $r.addField("VarS", rtl.string);',
  30839. ' $r.addField("VarD", rtl.double);',
  30840. ' $r.addField("VarB", rtl.boolean);',
  30841. ' $r.addField("VarLW", rtl.longword);',
  30842. ' $r.addField("VarSmI", rtl.smallint);',
  30843. ' $r.addField("VarW", rtl.word);',
  30844. ' $r.addField("VarShI", rtl.shortint);',
  30845. ' $r.addField("VarBy", rtl.byte);',
  30846. ' $r.addField("VarExt", rtl.longint);',
  30847. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30848. ' eltype: rtl.byte',
  30849. ' });',
  30850. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30851. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30852. '});',
  30853. 'this.p = null;',
  30854. 'this.Obj = null;',
  30855. '']),
  30856. LinesToStr([ // $mod.$main
  30857. '$mod.p = $mod.$rtti["TObject"];',
  30858. '$mod.p = rtl.pointer;',
  30859. '$mod.p = $mod.Obj.$rtti;',
  30860. '']));
  30861. end;
  30862. procedure TTestModule.TestRTTI_Class_Method;
  30863. begin
  30864. WithTypeInfo:=true;
  30865. StartProgram(false);
  30866. Add([
  30867. 'type',
  30868. ' TObject = class',
  30869. ' private',
  30870. ' procedure Internal; external name ''$intern'';',
  30871. ' published',
  30872. ' procedure Click; virtual; abstract;',
  30873. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30874. ' function GetNotify: boolean; external name ''GetNotify'';',
  30875. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30876. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30877. ' end;',
  30878. 'begin']);
  30879. ConvertProgram;
  30880. CheckSource('TestRTTI_Class_Method',
  30881. LinesToStr([ // statements
  30882. 'rtl.createClass(this, "TObject", null, function () {',
  30883. ' this.$init = function () {',
  30884. ' };',
  30885. ' this.$final = function () {',
  30886. ' };',
  30887. ' var $r = this.$rtti;',
  30888. ' $r.addMethod("Click", 0, []);',
  30889. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30890. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30891. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30892. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30893. '});',
  30894. '']),
  30895. LinesToStr([ // $mod.$main
  30896. '']));
  30897. end;
  30898. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30899. begin
  30900. WithTypeInfo:=true;
  30901. StartProgram(false);
  30902. Add('type');
  30903. Add(' TObject = class');
  30904. Add(' published');
  30905. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30906. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30907. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30908. Add(' end;');
  30909. Add('begin');
  30910. ConvertProgram;
  30911. CheckSource('TestRTTI_Class_MethodOpenArray',
  30912. LinesToStr([ // statements
  30913. 'rtl.createClass(this, "TObject", null, function () {',
  30914. ' this.$init = function () {',
  30915. ' };',
  30916. ' this.$final = function () {',
  30917. ' };',
  30918. ' var $r = this.$rtti;',
  30919. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30920. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30921. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30922. '});',
  30923. '']),
  30924. LinesToStr([ // $mod.$main
  30925. '']));
  30926. end;
  30927. procedure TTestModule.TestRTTI_Class_Property;
  30928. begin
  30929. WithTypeInfo:=true;
  30930. StartProgram(false);
  30931. Add('{$modeswitch externalclass}');
  30932. Add('type');
  30933. Add(' TObject = class');
  30934. Add(' private');
  30935. Add(' FColor: longint;');
  30936. Add(' FColorStored: boolean;');
  30937. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30938. Add(' function GetColor: longint; virtual; abstract;');
  30939. Add(' function GetColorStored: boolean; virtual; abstract;');
  30940. Add(' FExtSize: longint external name ''$extSize'';');
  30941. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30942. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30943. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30944. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30945. Add(' published');
  30946. Add(' property ColorA: longint read FColor;');
  30947. Add(' property ColorB: longint write FColor;');
  30948. Add(' property ColorC: longint read GetColor write SetColor;');
  30949. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30950. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30951. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30952. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30953. Add(' end;');
  30954. Add('begin');
  30955. ConvertProgram;
  30956. CheckSource('TestRTTI_Class_Property',
  30957. LinesToStr([ // statements
  30958. 'rtl.createClass(this, "TObject", null, function () {',
  30959. ' this.$init = function () {',
  30960. ' this.FColor = 0;',
  30961. ' this.FColorStored = false;',
  30962. ' };',
  30963. ' this.$final = function () {',
  30964. ' };',
  30965. ' var $r = this.$rtti;',
  30966. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30967. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30968. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30969. ' $r.addProperty(',
  30970. ' "ColorD",',
  30971. ' 8,',
  30972. ' rtl.longint,',
  30973. ' "FColor",',
  30974. ' "FColor",',
  30975. ' {',
  30976. ' stored: "FColorStored"',
  30977. ' }',
  30978. ' );',
  30979. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30980. ' $r.addProperty(',
  30981. ' "ExtSizeB",',
  30982. ' 11,',
  30983. ' rtl.longint,',
  30984. ' "$getSize",',
  30985. ' "$setSize",',
  30986. ' {',
  30987. ' stored: "$extSizeStored"',
  30988. ' }',
  30989. ' );',
  30990. ' $r.addProperty(',
  30991. ' "ExtSizeC",',
  30992. ' 12,',
  30993. ' rtl.longint,',
  30994. ' "$extSize",',
  30995. ' "$extSize",',
  30996. ' {',
  30997. ' stored: "$getExtSizeStored"',
  30998. ' }',
  30999. ' );',
  31000. '});',
  31001. '']),
  31002. LinesToStr([ // $mod.$main
  31003. '']));
  31004. end;
  31005. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31006. begin
  31007. WithTypeInfo:=true;
  31008. StartProgram(false);
  31009. Add('{$modeswitch externalclass}');
  31010. Add('type');
  31011. Add(' integer = longint;');
  31012. Add(' TObject = class');
  31013. Add(' private');
  31014. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31015. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31016. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31017. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31018. Add(' published');
  31019. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31020. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31021. Add(' end;');
  31022. Add('begin');
  31023. ConvertProgram;
  31024. CheckSource('TestRTTI_Class_PropertyParams',
  31025. LinesToStr([ // statements
  31026. 'rtl.createClass(this, "TObject", null, function () {',
  31027. ' this.$init = function () {',
  31028. ' };',
  31029. ' this.$final = function () {',
  31030. ' };',
  31031. ' var $r = this.$rtti;',
  31032. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31033. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31034. '});',
  31035. '']),
  31036. LinesToStr([ // $mod.$main
  31037. '']));
  31038. end;
  31039. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31040. begin
  31041. WithTypeInfo:=true;
  31042. AddModuleWithIntfImplSrc('unit1.pas',
  31043. 'type TColor = -5..5;',
  31044. '');
  31045. StartProgram(true);
  31046. Add([
  31047. 'uses unit1;',
  31048. 'type',
  31049. ' TColorAlias = TColor;',
  31050. ' TColorTypeAlias = type TColor;',
  31051. ' TObject = class',
  31052. ' private',
  31053. ' fColor: TColor;',
  31054. ' fAlias: TColorAlias;',
  31055. ' fTypeAlias: TColorTypeAlias;',
  31056. ' published',
  31057. ' property Color: TColor read fcolor;',
  31058. ' property Alias: TColorAlias read falias;',
  31059. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31060. ' end;',
  31061. 'begin',
  31062. '']);
  31063. ConvertProgram;
  31064. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31065. LinesToStr([ // statements
  31066. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31067. 'rtl.createClass(this, "TObject", null, function () {',
  31068. ' this.$init = function () {',
  31069. ' this.fColor = 0;',
  31070. ' this.fAlias = 0;',
  31071. ' this.fTypeAlias = 0;',
  31072. ' };',
  31073. ' this.$final = function () {',
  31074. ' };',
  31075. ' var $r = this.$rtti;',
  31076. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31077. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31078. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31079. '});',
  31080. '']),
  31081. LinesToStr([ // $mod.$main
  31082. '']));
  31083. end;
  31084. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31085. begin
  31086. WithTypeInfo:=true;
  31087. StartProgram(false);
  31088. Add([
  31089. '{$modeswitch omitrtti}',
  31090. 'type',
  31091. ' TObject = class',
  31092. ' private',
  31093. ' FA: byte;',
  31094. ' published',
  31095. ' property A: byte read FA write FA;',
  31096. ' end;',
  31097. 'begin']);
  31098. ConvertProgram;
  31099. CheckSource('TestRTTI_Class_OmitRTTI',
  31100. LinesToStr([ // statements
  31101. 'rtl.createClass(this, "TObject", null, function () {',
  31102. ' this.$init = function () {',
  31103. ' this.FA = 0;',
  31104. ' };',
  31105. ' this.$final = function () {',
  31106. ' };',
  31107. '});',
  31108. '']),
  31109. LinesToStr([ // $mod.$main
  31110. '']));
  31111. end;
  31112. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31113. begin
  31114. WithTypeInfo:=true;
  31115. StartUnit(true,[supTObject]);
  31116. Add([
  31117. 'interface',
  31118. 'type',
  31119. ' {$M+}',
  31120. ' TBird = class',
  31121. ' published',
  31122. ' Swarm: array of TBird;',
  31123. ' end;',
  31124. 'implementation',
  31125. '']);
  31126. ConvertUnit;
  31127. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31128. LinesToStr([ // statements
  31129. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31130. ' this.$init = function () {',
  31131. ' pas.system.TObject.$init.call(this);',
  31132. ' this.Swarm = [];',
  31133. ' };',
  31134. ' this.$final = function () {',
  31135. ' this.Swarm = undefined;',
  31136. ' pas.system.TObject.$final.call(this);',
  31137. ' };',
  31138. ' var $r = this.$rtti;',
  31139. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31140. ' eltype: $r',
  31141. ' });',
  31142. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31143. '});',
  31144. '']),
  31145. LinesToStr([ // $mod.$main
  31146. '']));
  31147. end;
  31148. procedure TTestModule.TestRTTI_IndexModifier;
  31149. begin
  31150. WithTypeInfo:=true;
  31151. StartProgram(false);
  31152. Add([
  31153. 'type',
  31154. ' TEnum = (red, blue);',
  31155. ' TObject = class',
  31156. ' FB: boolean;',
  31157. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31158. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31159. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31160. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31161. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31162. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31163. ' published',
  31164. ' property B1: boolean index 1 read FB write SetIntBool;',
  31165. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31166. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31167. ' end;',
  31168. 'begin']);
  31169. ConvertProgram;
  31170. CheckSource('TestRTTI_IndexModifier',
  31171. LinesToStr([ // statements
  31172. 'this.TEnum = {',
  31173. ' "0": "red",',
  31174. ' red: 0,',
  31175. ' "1": "blue",',
  31176. ' blue: 1',
  31177. '};',
  31178. 'this.$rtti.$Enum("TEnum", {',
  31179. ' minvalue: 0,',
  31180. ' maxvalue: 1,',
  31181. ' ordtype: 1,',
  31182. ' enumtype: this.TEnum',
  31183. '});',
  31184. 'rtl.createClass(this, "TObject", null, function () {',
  31185. ' this.$init = function () {',
  31186. ' this.FB = false;',
  31187. ' };',
  31188. ' this.$final = function () {',
  31189. ' };',
  31190. ' var $r = this.$rtti;',
  31191. ' $r.addProperty(',
  31192. ' "B1",',
  31193. ' 18,',
  31194. ' rtl.boolean,',
  31195. ' "FB",',
  31196. ' "SetIntBool",',
  31197. ' {',
  31198. ' index: 1',
  31199. ' }',
  31200. ' );',
  31201. ' $r.addProperty(',
  31202. ' "B2",',
  31203. ' 17,',
  31204. ' rtl.boolean,',
  31205. ' "GetEnumBool",',
  31206. ' "FB",',
  31207. ' {',
  31208. ' index: $mod.TEnum.blue',
  31209. ' }',
  31210. ' );',
  31211. ' $r.addProperty(',
  31212. ' "I1",',
  31213. ' 19,',
  31214. ' rtl.boolean,',
  31215. ' "GetStrIntBool",',
  31216. ' "SetStrIntBool",',
  31217. ' {',
  31218. ' index: 2',
  31219. ' }',
  31220. ' );',
  31221. '});',
  31222. '']),
  31223. LinesToStr([ // $mod.$main
  31224. '']));
  31225. end;
  31226. procedure TTestModule.TestRTTI_StoredModifier;
  31227. begin
  31228. WithTypeInfo:=true;
  31229. StartProgram(false);
  31230. Add([
  31231. 'const',
  31232. ' ConstB = true;',
  31233. 'type',
  31234. ' TObject = class',
  31235. ' private',
  31236. ' FB: boolean;',
  31237. ' function IsBStored: boolean; virtual; abstract;',
  31238. ' published',
  31239. ' property BoolA: boolean read FB stored true;',
  31240. ' property BoolB: boolean read FB stored false;',
  31241. ' property BoolC: boolean read FB stored FB;',
  31242. ' property BoolD: boolean read FB stored ConstB;',
  31243. ' property BoolE: boolean read FB stored IsBStored;',
  31244. ' end;',
  31245. 'begin']);
  31246. ConvertProgram;
  31247. CheckSource('TestRTTI_StoredModifier',
  31248. LinesToStr([ // statements
  31249. 'this.ConstB = true;',
  31250. 'rtl.createClass(this, "TObject", null, function () {',
  31251. ' this.$init = function () {',
  31252. ' this.FB = false;',
  31253. ' };',
  31254. ' this.$final = function () {',
  31255. ' };',
  31256. ' var $r = this.$rtti;',
  31257. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31258. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31259. ' $r.addProperty(',
  31260. ' "BoolC",',
  31261. ' 8,',
  31262. ' rtl.boolean,',
  31263. ' "FB",',
  31264. ' "",',
  31265. ' {',
  31266. ' stored: "FB"',
  31267. ' }',
  31268. ' );',
  31269. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31270. ' $r.addProperty(',
  31271. ' "BoolE",',
  31272. ' 12,',
  31273. ' rtl.boolean,',
  31274. ' "FB",',
  31275. ' "",',
  31276. ' {',
  31277. ' stored: "IsBStored"',
  31278. ' }',
  31279. ' );',
  31280. '});',
  31281. '']),
  31282. LinesToStr([ // $mod.$main
  31283. '']));
  31284. end;
  31285. procedure TTestModule.TestRTTI_DefaultValue;
  31286. begin
  31287. WithTypeInfo:=true;
  31288. StartProgram(false);
  31289. Add([
  31290. 'type',
  31291. ' TEnum = (red, blue);',
  31292. 'const',
  31293. ' CB = true or false;',
  31294. ' CI = 1+2;',
  31295. 'type',
  31296. ' TObject = class',
  31297. ' FB: boolean;',
  31298. ' FI: longint;',
  31299. ' FE: TEnum;',
  31300. ' published',
  31301. ' property B1: boolean read FB default true;',
  31302. ' property B2: boolean read FB default CB;',
  31303. ' property B3: boolean read FB default test1.cb;',
  31304. ' property I1: longint read FI default 2;',
  31305. ' property I2: longint read FI default CI;',
  31306. ' property E1: TEnum read FE default red;',
  31307. ' property E2: TEnum read FE default TEnum.blue;',
  31308. ' end;',
  31309. 'begin']);
  31310. ConvertProgram;
  31311. CheckSource('TestRTTI_DefaultValue',
  31312. LinesToStr([ // statements
  31313. 'this.TEnum = {',
  31314. ' "0": "red",',
  31315. ' red: 0,',
  31316. ' "1": "blue",',
  31317. ' blue: 1',
  31318. '};',
  31319. 'this.$rtti.$Enum("TEnum", {',
  31320. ' minvalue: 0,',
  31321. ' maxvalue: 1,',
  31322. ' ordtype: 1,',
  31323. ' enumtype: this.TEnum',
  31324. '});',
  31325. 'this.CB = true || false;',
  31326. 'this.CI = 1 + 2;',
  31327. 'rtl.createClass(this, "TObject", null, function () {',
  31328. ' this.$init = function () {',
  31329. ' this.FB = false;',
  31330. ' this.FI = 0;',
  31331. ' this.FE = 0;',
  31332. ' };',
  31333. ' this.$final = function () {',
  31334. ' };',
  31335. ' var $r = this.$rtti;',
  31336. ' $r.addProperty(',
  31337. ' "B1",',
  31338. ' 0,',
  31339. ' rtl.boolean,',
  31340. ' "FB",',
  31341. ' "",',
  31342. ' {',
  31343. ' Default: true',
  31344. ' }',
  31345. ' );',
  31346. ' $r.addProperty(',
  31347. ' "B2",',
  31348. ' 0,',
  31349. ' rtl.boolean,',
  31350. ' "FB",',
  31351. ' "",',
  31352. ' {',
  31353. ' Default: true',
  31354. ' }',
  31355. ' );',
  31356. ' $r.addProperty(',
  31357. ' "B3",',
  31358. ' 0,',
  31359. ' rtl.boolean,',
  31360. ' "FB",',
  31361. ' "",',
  31362. ' {',
  31363. ' Default: true',
  31364. ' }',
  31365. ' );',
  31366. ' $r.addProperty(',
  31367. ' "I1",',
  31368. ' 0,',
  31369. ' rtl.longint,',
  31370. ' "FI",',
  31371. ' "",',
  31372. ' {',
  31373. ' Default: 2',
  31374. ' }',
  31375. ' );',
  31376. ' $r.addProperty(',
  31377. ' "I2",',
  31378. ' 0,',
  31379. ' rtl.longint,',
  31380. ' "FI",',
  31381. ' "",',
  31382. ' {',
  31383. ' Default: 3',
  31384. ' }',
  31385. ' );',
  31386. ' $r.addProperty(',
  31387. ' "E1",',
  31388. ' 0,',
  31389. ' $mod.$rtti["TEnum"],',
  31390. ' "FE",',
  31391. ' "",',
  31392. ' {',
  31393. ' Default: $mod.TEnum.red',
  31394. ' }',
  31395. ' );',
  31396. ' $r.addProperty(',
  31397. ' "E2",',
  31398. ' 0,',
  31399. ' $mod.$rtti["TEnum"],',
  31400. ' "FE",',
  31401. ' "",',
  31402. ' {',
  31403. ' Default: $mod.TEnum.blue',
  31404. ' }',
  31405. ' );',
  31406. '});',
  31407. '']),
  31408. LinesToStr([ // $mod.$main
  31409. '']));
  31410. end;
  31411. procedure TTestModule.TestRTTI_DefaultValueSet;
  31412. begin
  31413. WithTypeInfo:=true;
  31414. StartProgram(false);
  31415. Add([
  31416. 'type',
  31417. ' TEnum = (red, blue);',
  31418. ' TSet = set of TEnum;',
  31419. 'const',
  31420. ' CSet = [red,blue];',
  31421. 'type',
  31422. ' TObject = class',
  31423. ' FSet: TSet;',
  31424. ' published',
  31425. ' property Set1: TSet read FSet default [];',
  31426. ' property Set2: TSet read FSet default [red];',
  31427. ' property Set3: TSet read FSet default [red,blue];',
  31428. ' property Set4: TSet read FSet default CSet;',
  31429. ' end;',
  31430. 'begin']);
  31431. ConvertProgram;
  31432. CheckSource('TestRTTI_DefaultValueSet',
  31433. LinesToStr([ // statements
  31434. 'this.TEnum = {',
  31435. ' "0": "red",',
  31436. ' red: 0,',
  31437. ' "1": "blue",',
  31438. ' blue: 1',
  31439. '};',
  31440. 'this.$rtti.$Enum("TEnum", {',
  31441. ' minvalue: 0,',
  31442. ' maxvalue: 1,',
  31443. ' ordtype: 1,',
  31444. ' enumtype: this.TEnum',
  31445. '});',
  31446. 'this.$rtti.$Set("TSet", {',
  31447. ' comptype: this.$rtti["TEnum"]',
  31448. '});',
  31449. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31450. 'rtl.createClass(this, "TObject", null, function () {',
  31451. ' this.$init = function () {',
  31452. ' this.FSet = {};',
  31453. ' };',
  31454. ' this.$final = function () {',
  31455. ' this.FSet = undefined;',
  31456. ' };',
  31457. ' var $r = this.$rtti;',
  31458. ' $r.addProperty(',
  31459. ' "Set1",',
  31460. ' 0,',
  31461. ' $mod.$rtti["TSet"],',
  31462. ' "FSet",',
  31463. ' "",',
  31464. ' {',
  31465. ' Default: {}',
  31466. ' }',
  31467. ' );',
  31468. ' $r.addProperty(',
  31469. ' "Set2",',
  31470. ' 0,',
  31471. ' $mod.$rtti["TSet"],',
  31472. ' "FSet",',
  31473. ' "",',
  31474. ' {',
  31475. ' Default: rtl.createSet($mod.TEnum.red)',
  31476. ' }',
  31477. ' );',
  31478. ' $r.addProperty(',
  31479. ' "Set3",',
  31480. ' 0,',
  31481. ' $mod.$rtti["TSet"],',
  31482. ' "FSet",',
  31483. ' "",',
  31484. ' {',
  31485. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31486. ' }',
  31487. ' );',
  31488. ' $r.addProperty(',
  31489. ' "Set4",',
  31490. ' 0,',
  31491. ' $mod.$rtti["TSet"],',
  31492. ' "FSet",',
  31493. ' "",',
  31494. ' {',
  31495. ' Default: $mod.CSet',
  31496. ' }',
  31497. ' );',
  31498. '});',
  31499. '']),
  31500. LinesToStr([ // $mod.$main
  31501. '']));
  31502. end;
  31503. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31504. begin
  31505. WithTypeInfo:=true;
  31506. StartProgram(false);
  31507. Add([
  31508. 'type',
  31509. ' TRg = -1..1;',
  31510. 'const',
  31511. ' l = low(TRg);',
  31512. ' h = high(TRg);',
  31513. 'type',
  31514. ' TObject = class',
  31515. ' FV: TRg;',
  31516. ' published',
  31517. ' property V1: TRg read FV default -1;',
  31518. ' end;',
  31519. 'begin']);
  31520. ConvertProgram;
  31521. CheckSource('TestRTTI_DefaultValueRangeType',
  31522. LinesToStr([ // statements
  31523. 'this.$rtti.$Int("TRg", {',
  31524. ' minvalue: -1,',
  31525. ' maxvalue: 1,',
  31526. ' ordtype: 0',
  31527. '});',
  31528. 'this.l = -1;',
  31529. 'this.h = 1;',
  31530. 'rtl.createClass(this, "TObject", null, function () {',
  31531. ' this.$init = function () {',
  31532. ' this.FV = 0;',
  31533. ' };',
  31534. ' this.$final = function () {',
  31535. ' };',
  31536. ' var $r = this.$rtti;',
  31537. ' $r.addProperty(',
  31538. ' "V1",',
  31539. ' 0,',
  31540. ' $mod.$rtti["TRg"],',
  31541. ' "FV",',
  31542. ' "",',
  31543. ' {',
  31544. ' Default: -1',
  31545. ' }',
  31546. ' );',
  31547. '});',
  31548. '']),
  31549. LinesToStr([ // $mod.$main
  31550. '']));
  31551. end;
  31552. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31553. begin
  31554. WithTypeInfo:=true;
  31555. StartProgram(false);
  31556. Add([
  31557. 'type',
  31558. ' TObject = class',
  31559. ' FA, FB: byte;',
  31560. ' property A: byte read FA default 1;',
  31561. ' property B: byte read FB default 2;',
  31562. ' end;',
  31563. ' TBird = class',
  31564. ' published',
  31565. ' property A;',
  31566. ' property B nodefault;',
  31567. ' end;',
  31568. 'begin']);
  31569. ConvertProgram;
  31570. CheckSource('TestRTTI_DefaultValueInherit',
  31571. LinesToStr([ // statements
  31572. 'rtl.createClass(this, "TObject", null, function () {',
  31573. ' this.$init = function () {',
  31574. ' this.FA = 0;',
  31575. ' this.FB = 0;',
  31576. ' };',
  31577. ' this.$final = function () {',
  31578. ' };',
  31579. '});',
  31580. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31581. ' var $r = this.$rtti;',
  31582. ' $r.addProperty(',
  31583. ' "A",',
  31584. ' 0,',
  31585. ' rtl.byte,',
  31586. ' "FA",',
  31587. ' "",',
  31588. ' {',
  31589. ' Default: 1',
  31590. ' }',
  31591. ' );',
  31592. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31593. '});',
  31594. '']),
  31595. LinesToStr([ // $mod.$main
  31596. '']));
  31597. end;
  31598. procedure TTestModule.TestRTTI_OverrideMethod;
  31599. begin
  31600. WithTypeInfo:=true;
  31601. StartProgram(false);
  31602. Add('type');
  31603. Add(' TObject = class');
  31604. Add(' published');
  31605. Add(' procedure DoIt; virtual; abstract;');
  31606. Add(' end;');
  31607. Add(' TSky = class');
  31608. Add(' published');
  31609. Add(' procedure DoIt; override;');
  31610. Add(' end;');
  31611. Add('procedure TSky.DoIt; begin end;');
  31612. Add('begin');
  31613. ConvertProgram;
  31614. CheckSource('TestRTTI_OverrideMethod',
  31615. LinesToStr([ // statements
  31616. 'rtl.createClass(this, "TObject", null, function () {',
  31617. ' this.$init = function () {',
  31618. ' };',
  31619. ' this.$final = function () {',
  31620. ' };',
  31621. ' var $r = this.$rtti;',
  31622. ' $r.addMethod("DoIt", 0, []);',
  31623. '});',
  31624. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31625. ' this.DoIt = function () {',
  31626. ' };',
  31627. '});',
  31628. '']),
  31629. LinesToStr([ // $mod.$main
  31630. '']));
  31631. end;
  31632. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31633. begin
  31634. WithTypeInfo:=true;
  31635. StartProgram(false);
  31636. Add([
  31637. 'type',
  31638. ' TObject = class',
  31639. ' published',
  31640. ' procedure DoIt;',
  31641. ' end;',
  31642. ' TSky = class',
  31643. ' published',
  31644. ' procedure DoIt; reintroduce;',
  31645. ' end;',
  31646. 'procedure TObject.DoIt; begin end;',
  31647. 'procedure TSky.DoIt;',
  31648. 'begin',
  31649. ' inherited DoIt;',
  31650. 'end;',
  31651. 'begin']);
  31652. ConvertProgram;
  31653. CheckSource('TestRTTI_ReintroduceMethod',
  31654. LinesToStr([ // statements
  31655. 'rtl.createClass(this, "TObject", null, function () {',
  31656. ' this.$init = function () {',
  31657. ' };',
  31658. ' this.$final = function () {',
  31659. ' };',
  31660. ' this.DoIt = function () {',
  31661. ' };',
  31662. ' var $r = this.$rtti;',
  31663. ' $r.addMethod("DoIt", 0, []);',
  31664. '});',
  31665. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31666. ' this.DoIt = function () {',
  31667. ' $mod.TObject.DoIt.call(this);',
  31668. ' };',
  31669. ' var $r = this.$rtti;',
  31670. ' $r.addMethod("DoIt", 0, []);',
  31671. '});',
  31672. '']),
  31673. LinesToStr([ // $mod.$main
  31674. '']));
  31675. end;
  31676. procedure TTestModule.TestRTTI_OverloadProperty;
  31677. begin
  31678. WithTypeInfo:=true;
  31679. StartProgram(false);
  31680. Add('type');
  31681. Add(' TObject = class');
  31682. Add(' protected');
  31683. Add(' FFlag: longint;');
  31684. Add(' published');
  31685. Add(' property Flag: longint read fflag;');
  31686. Add(' end;');
  31687. Add(' TSky = class');
  31688. Add(' published');
  31689. Add(' property FLAG: longint write fflag;');
  31690. Add(' end;');
  31691. Add('begin');
  31692. ConvertProgram;
  31693. CheckSource('TestRTTI_OverrideMethod',
  31694. LinesToStr([ // statements
  31695. 'rtl.createClass(this, "TObject", null, function () {',
  31696. ' this.$init = function () {',
  31697. ' this.FFlag = 0;',
  31698. ' };',
  31699. ' this.$final = function () {',
  31700. ' };',
  31701. ' var $r = this.$rtti;',
  31702. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31703. '});',
  31704. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31705. ' var $r = this.$rtti;',
  31706. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31707. '});',
  31708. '']),
  31709. LinesToStr([ // $mod.$main
  31710. '']));
  31711. end;
  31712. procedure TTestModule.TestRTTI_ClassForward;
  31713. begin
  31714. WithTypeInfo:=true;
  31715. StartProgram(false);
  31716. Add('type');
  31717. Add(' TObject = class end;');
  31718. Add(' tbridge = class;');
  31719. Add(' TProc = function: tbridge;');
  31720. Add(' TOger = class');
  31721. Add(' published');
  31722. Add(' FBridge: tbridge;');
  31723. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31724. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31725. Add(' end;');
  31726. Add(' TBridge = class');
  31727. Add(' FOger: toger;');
  31728. Add(' end;');
  31729. Add('var p: Pointer;');
  31730. Add(' b: tbridge;');
  31731. Add('begin');
  31732. Add(' p:=typeinfo(tbridge);');
  31733. Add(' p:=typeinfo(b);');
  31734. ConvertProgram;
  31735. CheckSource('TestRTTI_ClassForward',
  31736. LinesToStr([ // statements
  31737. 'rtl.createClass(this, "TObject", null, function () {',
  31738. ' this.$init = function () {',
  31739. ' };',
  31740. ' this.$final = function () {',
  31741. ' };',
  31742. '});',
  31743. 'this.$rtti.$Class("TBridge");',
  31744. 'this.$rtti.$ProcVar("TProc", {',
  31745. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31746. '});',
  31747. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31748. ' this.$init = function () {',
  31749. ' $mod.TObject.$init.call(this);',
  31750. ' this.FBridge = null;',
  31751. ' };',
  31752. ' this.$final = function () {',
  31753. ' this.FBridge = undefined;',
  31754. ' $mod.TObject.$final.call(this);',
  31755. ' };',
  31756. ' var $r = this.$rtti;',
  31757. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31758. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31759. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31760. '});',
  31761. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31762. ' this.$init = function () {',
  31763. ' $mod.TObject.$init.call(this);',
  31764. ' this.FOger = null;',
  31765. ' };',
  31766. ' this.$final = function () {',
  31767. ' this.FOger = undefined;',
  31768. ' $mod.TObject.$final.call(this);',
  31769. ' };',
  31770. '});',
  31771. 'this.p = null;',
  31772. 'this.b = null;',
  31773. '']),
  31774. LinesToStr([ // $mod.$main
  31775. '$mod.p = $mod.$rtti["TBridge"];',
  31776. '$mod.p = $mod.b.$rtti;',
  31777. '']));
  31778. end;
  31779. procedure TTestModule.TestRTTI_ClassOf;
  31780. begin
  31781. WithTypeInfo:=true;
  31782. StartProgram(false);
  31783. Add('type');
  31784. Add(' TClass = class of tobject;');
  31785. Add(' TProcA = function: TClass;');
  31786. Add(' TObject = class');
  31787. Add(' published');
  31788. Add(' C: tclass;');
  31789. Add(' end;');
  31790. Add(' tfox = class;');
  31791. Add(' TBird = class end;');
  31792. Add(' TBirds = class of tbird;');
  31793. Add(' TFox = class end;');
  31794. Add(' TFoxes = class of tfox;');
  31795. Add(' TCows = class of TCow;');
  31796. Add(' TCow = class;');
  31797. Add(' TCow = class end;');
  31798. Add('begin');
  31799. ConvertProgram;
  31800. CheckSource('TestRTTI_ClassOf',
  31801. LinesToStr([ // statements
  31802. 'this.$rtti.$Class("TObject");',
  31803. 'this.$rtti.$ClassRef("TClass", {',
  31804. ' instancetype: this.$rtti["TObject"]',
  31805. '});',
  31806. 'this.$rtti.$ProcVar("TProcA", {',
  31807. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31808. '});',
  31809. 'rtl.createClass(this, "TObject", null, function () {',
  31810. ' this.$init = function () {',
  31811. ' this.C = null;',
  31812. ' };',
  31813. ' this.$final = function () {',
  31814. ' this.C = undefined;',
  31815. ' };',
  31816. ' var $r = this.$rtti;',
  31817. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31818. '});',
  31819. 'this.$rtti.$Class("TFox");',
  31820. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31821. '});',
  31822. 'this.$rtti.$ClassRef("TBirds", {',
  31823. ' instancetype: this.$rtti["TBird"]',
  31824. '});',
  31825. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31826. '});',
  31827. 'this.$rtti.$ClassRef("TFoxes", {',
  31828. ' instancetype: this.$rtti["TFox"]',
  31829. '});',
  31830. 'this.$rtti.$Class("TCow");',
  31831. 'this.$rtti.$ClassRef("TCows", {',
  31832. ' instancetype: this.$rtti["TCow"]',
  31833. '});',
  31834. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31835. '});',
  31836. '']),
  31837. LinesToStr([ // $mod.$main
  31838. '']));
  31839. end;
  31840. procedure TTestModule.TestRTTI_Record;
  31841. begin
  31842. WithTypeInfo:=true;
  31843. StartProgram(false);
  31844. Add('type');
  31845. Add(' integer = longint;');
  31846. Add(' TPoint = record');
  31847. Add(' x,y: integer;');
  31848. Add(' end;');
  31849. Add('var p: pointer;');
  31850. Add(' r: tpoint;');
  31851. Add('begin');
  31852. Add(' p:=typeinfo(tpoint);');
  31853. Add(' p:=typeinfo(r);');
  31854. Add(' p:=typeinfo(r.x);');
  31855. ConvertProgram;
  31856. CheckSource('TestRTTI_Record',
  31857. LinesToStr([ // statements
  31858. 'rtl.recNewT(this, "TPoint", function () {',
  31859. ' this.x = 0;',
  31860. ' this.y = 0;',
  31861. ' this.$eq = function (b) {',
  31862. ' return (this.x === b.x) && (this.y === b.y);',
  31863. ' };',
  31864. ' this.$assign = function (s) {',
  31865. ' this.x = s.x;',
  31866. ' this.y = s.y;',
  31867. ' return this;',
  31868. ' };',
  31869. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31870. ' $r.addField("x", rtl.longint);',
  31871. ' $r.addField("y", rtl.longint);',
  31872. '});',
  31873. 'this.p = null;',
  31874. 'this.r = this.TPoint.$new();',
  31875. '']),
  31876. LinesToStr([ // $mod.$main
  31877. '$mod.p = $mod.$rtti["TPoint"];',
  31878. '$mod.p = $mod.$rtti["TPoint"];',
  31879. '$mod.p = rtl.longint;',
  31880. '']));
  31881. end;
  31882. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31883. begin
  31884. WithTypeInfo:=true;
  31885. StartProgram(false);
  31886. Add('type');
  31887. Add(' TFloatRec = record');
  31888. Add(' c,d: array of char;');
  31889. // Add(' i: array of array of longint;');
  31890. Add(' end;');
  31891. Add('var p: pointer;');
  31892. Add(' r: tfloatrec;');
  31893. Add('begin');
  31894. Add(' p:=typeinfo(tfloatrec);');
  31895. Add(' p:=typeinfo(r);');
  31896. Add(' p:=typeinfo(r.d);');
  31897. ConvertProgram;
  31898. CheckSource('TestRTTI_Record',
  31899. LinesToStr([ // statements
  31900. 'rtl.recNewT(this, "TFloatRec", function () {',
  31901. ' this.$new = function () {',
  31902. ' var r = Object.create(this);',
  31903. ' r.c = [];',
  31904. ' r.d = [];',
  31905. ' return r;',
  31906. ' };',
  31907. ' this.$eq = function (b) {',
  31908. ' return (this.c === b.c) && (this.d === b.d);',
  31909. ' };',
  31910. ' this.$assign = function (s) {',
  31911. ' this.c = rtl.arrayRef(s.c);',
  31912. ' this.d = rtl.arrayRef(s.d);',
  31913. ' return this;',
  31914. ' };',
  31915. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31916. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31917. ' eltype: rtl.char',
  31918. ' });',
  31919. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31920. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31921. '});',
  31922. 'this.p = null;',
  31923. 'this.r = this.TFloatRec.$new();',
  31924. '']),
  31925. LinesToStr([ // $mod.$main
  31926. '$mod.p = $mod.$rtti["TFloatRec"];',
  31927. '$mod.p = $mod.$rtti["TFloatRec"];',
  31928. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31929. '']));
  31930. end;
  31931. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31932. begin
  31933. WithTypeInfo:=true;
  31934. StartProgram(false);
  31935. Add([
  31936. '{$modeswitch AdvancedRecords}',
  31937. 'type',
  31938. ' TPoint = record',
  31939. ' type TProc = procedure(w: word);',
  31940. ' class var p: TProc;',
  31941. ' end;',
  31942. 'begin',
  31943. '']);
  31944. ConvertProgram;
  31945. CheckSource('TestRTTI_Record_ClassVarType',
  31946. LinesToStr([ // statements
  31947. 'rtl.recNewT(this, "TPoint", function () {',
  31948. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31949. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31950. ' });',
  31951. ' this.p = null;',
  31952. ' this.$eq = function (b) {',
  31953. ' return true;',
  31954. ' };',
  31955. ' this.$assign = function (s) {',
  31956. ' return this;',
  31957. ' };',
  31958. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31959. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31960. '}, true);',
  31961. '']),
  31962. LinesToStr([ // $mod.$main
  31963. '']));
  31964. end;
  31965. procedure TTestModule.TestRTTI_LocalTypes;
  31966. begin
  31967. WithTypeInfo:=true;
  31968. StartProgram(false);
  31969. Add([
  31970. 'procedure DoIt;',
  31971. 'type',
  31972. ' integer = longint;',
  31973. ' TPoint = record',
  31974. ' x,y: integer;',
  31975. ' end;',
  31976. 'var p: TPoint;',
  31977. 'begin',
  31978. 'end;',
  31979. 'begin']);
  31980. ConvertProgram;
  31981. CheckSource('TestRTTI_LocalTypes',
  31982. LinesToStr([ // statements
  31983. 'var TPoint = rtl.recNewT(null, "", function () {',
  31984. ' this.x = 0;',
  31985. ' this.y = 0;',
  31986. ' this.$eq = function (b) {',
  31987. ' return (this.x === b.x) && (this.y === b.y);',
  31988. ' };',
  31989. ' this.$assign = function (s) {',
  31990. ' this.x = s.x;',
  31991. ' this.y = s.y;',
  31992. ' return this;',
  31993. ' };',
  31994. '});',
  31995. 'this.DoIt = function () {',
  31996. ' var p = TPoint.$new();',
  31997. '};',
  31998. '']),
  31999. LinesToStr([ // $mod.$main
  32000. '']));
  32001. end;
  32002. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32003. begin
  32004. WithTypeInfo:=true;
  32005. StartProgram(false);
  32006. Add([
  32007. 'type',
  32008. ' TCaption = string;',
  32009. ' TYesNo = boolean;',
  32010. ' TLetter = char;',
  32011. ' TFloat = double;',
  32012. ' TPtr = pointer;',
  32013. ' TShortInt = shortint;',
  32014. ' TByte = byte;',
  32015. ' TSmallInt = smallint;',
  32016. ' TWord = word;',
  32017. ' TInt32 = longint;',
  32018. ' TDWord = longword;',
  32019. ' TValue = jsvalue;',
  32020. 'var p: TPtr;',
  32021. 'begin',
  32022. ' p:=typeinfo(string);',
  32023. ' p:=typeinfo(tcaption);',
  32024. ' p:=typeinfo(boolean);',
  32025. ' p:=typeinfo(tyesno);',
  32026. ' p:=typeinfo(char);',
  32027. ' p:=typeinfo(tletter);',
  32028. ' p:=typeinfo(double);',
  32029. ' p:=typeinfo(tfloat);',
  32030. ' p:=typeinfo(pointer);',
  32031. ' p:=typeinfo(tptr);',
  32032. ' p:=typeinfo(shortint);',
  32033. ' p:=typeinfo(tshortint);',
  32034. ' p:=typeinfo(byte);',
  32035. ' p:=typeinfo(tbyte);',
  32036. ' p:=typeinfo(smallint);',
  32037. ' p:=typeinfo(tsmallint);',
  32038. ' p:=typeinfo(word);',
  32039. ' p:=typeinfo(tword);',
  32040. ' p:=typeinfo(longword);',
  32041. ' p:=typeinfo(tdword);',
  32042. ' p:=typeinfo(jsvalue);',
  32043. ' p:=typeinfo(tvalue);',
  32044. '']);
  32045. ConvertProgram;
  32046. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32047. LinesToStr([ // statements
  32048. 'this.p = null;',
  32049. '']),
  32050. LinesToStr([ // $mod.$main
  32051. '$mod.p = rtl.string;',
  32052. '$mod.p = rtl.string;',
  32053. '$mod.p = rtl.boolean;',
  32054. '$mod.p = rtl.boolean;',
  32055. '$mod.p = rtl.char;',
  32056. '$mod.p = rtl.char;',
  32057. '$mod.p = rtl.double;',
  32058. '$mod.p = rtl.double;',
  32059. '$mod.p = rtl.pointer;',
  32060. '$mod.p = rtl.pointer;',
  32061. '$mod.p = rtl.shortint;',
  32062. '$mod.p = rtl.shortint;',
  32063. '$mod.p = rtl.byte;',
  32064. '$mod.p = rtl.byte;',
  32065. '$mod.p = rtl.smallint;',
  32066. '$mod.p = rtl.smallint;',
  32067. '$mod.p = rtl.word;',
  32068. '$mod.p = rtl.word;',
  32069. '$mod.p = rtl.longword;',
  32070. '$mod.p = rtl.longword;',
  32071. '$mod.p = rtl.jsvalue;',
  32072. '$mod.p = rtl.jsvalue;',
  32073. '']));
  32074. end;
  32075. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32076. begin
  32077. WithTypeInfo:=true;
  32078. StartProgram(false);
  32079. Add([
  32080. 'type',
  32081. ' TCaption = type string;',
  32082. ' TYesNo = type boolean;',
  32083. ' TLetter = type char;',
  32084. ' TFloat = type double;',
  32085. ' TPtr = type pointer;',
  32086. ' TShortInt = type shortint;',
  32087. ' TByte = type byte;',
  32088. ' TSmallInt = type smallint;',
  32089. ' TWord = type word;',
  32090. ' TInt32 = type longint;',
  32091. ' TDWord = type longword;',
  32092. ' TValue = type jsvalue;',
  32093. ' TAliasValue = type TValue;',
  32094. 'var',
  32095. ' p: TPtr;',
  32096. ' a: TAliasValue;',
  32097. 'begin',
  32098. ' p:=typeinfo(tcaption);',
  32099. ' p:=typeinfo(tyesno);',
  32100. ' p:=typeinfo(tletter);',
  32101. ' p:=typeinfo(tfloat);',
  32102. ' p:=typeinfo(tptr);',
  32103. ' p:=typeinfo(tshortint);',
  32104. ' p:=typeinfo(tbyte);',
  32105. ' p:=typeinfo(tsmallint);',
  32106. ' p:=typeinfo(tword);',
  32107. ' p:=typeinfo(tdword);',
  32108. ' p:=typeinfo(tvalue);',
  32109. ' p:=typeinfo(taliasvalue);',
  32110. ' p:=typeinfo(a);',
  32111. '']);
  32112. ConvertProgram;
  32113. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32114. LinesToStr([ // statements
  32115. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32116. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32117. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32118. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32119. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32120. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32121. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32122. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32123. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32124. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32125. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32126. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32127. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32128. 'this.p = null;',
  32129. 'this.a = undefined;',
  32130. '']),
  32131. LinesToStr([ // $mod.$main
  32132. '$mod.p = $mod.$rtti["TCaption"];',
  32133. '$mod.p = $mod.$rtti["TYesNo"];',
  32134. '$mod.p = $mod.$rtti["TLetter"];',
  32135. '$mod.p = $mod.$rtti["TFloat"];',
  32136. '$mod.p = $mod.$rtti["TPtr"];',
  32137. '$mod.p = $mod.$rtti["TShortInt"];',
  32138. '$mod.p = $mod.$rtti["TByte"];',
  32139. '$mod.p = $mod.$rtti["TSmallInt"];',
  32140. '$mod.p = $mod.$rtti["TWord"];',
  32141. '$mod.p = $mod.$rtti["TDWord"];',
  32142. '$mod.p = $mod.$rtti["TValue"];',
  32143. '$mod.p = $mod.$rtti["TAliasValue"];',
  32144. '$mod.p = $mod.$rtti["TAliasValue"];',
  32145. '']));
  32146. end;
  32147. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32148. begin
  32149. WithTypeInfo:=true;
  32150. StartProgram(false);
  32151. Add('procedure DoIt;');
  32152. Add('type');
  32153. Add(' integer = longint;');
  32154. Add(' TPoint = record');
  32155. Add(' x,y: integer;');
  32156. Add(' end;');
  32157. Add('var p: pointer;');
  32158. Add('begin');
  32159. Add(' p:=typeinfo(tpoint);');
  32160. Add('end;');
  32161. Add('begin');
  32162. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32163. ConvertProgram;
  32164. end;
  32165. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32166. begin
  32167. WithTypeInfo:=true;
  32168. StartProgram(true,[supTypeInfo]);
  32169. Add([
  32170. '{$modeswitch externalclass}',
  32171. 'type',
  32172. ' TFlag = (up,down);',
  32173. ' TFlags = set of TFlag;',
  32174. 'var',
  32175. ' ti: TTypeInfo;',
  32176. ' tiInt: TTypeInfoInteger;',
  32177. ' tiEnum: TTypeInfoEnum;',
  32178. ' tiSet: TTypeInfoSet;',
  32179. 'begin',
  32180. ' ti:=typeinfo(string);',
  32181. ' ti:=typeinfo(boolean);',
  32182. ' ti:=typeinfo(char);',
  32183. ' ti:=typeinfo(double);',
  32184. ' tiInt:=typeinfo(shortint);',
  32185. ' tiInt:=typeinfo(byte);',
  32186. ' tiInt:=typeinfo(smallint);',
  32187. ' tiInt:=typeinfo(word);',
  32188. ' tiInt:=typeinfo(longint);',
  32189. ' tiInt:=typeinfo(longword);',
  32190. ' ti:=typeinfo(jsvalue);',
  32191. ' tiEnum:=typeinfo(tflag);',
  32192. ' tiSet:=typeinfo(tflags);']);
  32193. ConvertProgram;
  32194. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32195. LinesToStr([ // statements
  32196. 'this.TFlag = {',
  32197. ' "0": "up",',
  32198. ' up: 0,',
  32199. ' "1": "down",',
  32200. ' down: 1',
  32201. '};',
  32202. 'this.$rtti.$Enum("TFlag", {',
  32203. ' minvalue: 0,',
  32204. ' maxvalue: 1,',
  32205. ' ordtype: 1,',
  32206. ' enumtype: this.TFlag',
  32207. '});',
  32208. 'this.$rtti.$Set("TFlags", {',
  32209. ' comptype: this.$rtti["TFlag"]',
  32210. '});',
  32211. 'this.ti = null;',
  32212. 'this.tiInt = null;',
  32213. 'this.tiEnum = null;',
  32214. 'this.tiSet = null;',
  32215. '']),
  32216. LinesToStr([ // $mod.$main
  32217. '$mod.ti = rtl.string;',
  32218. '$mod.ti = rtl.boolean;',
  32219. '$mod.ti = rtl.char;',
  32220. '$mod.ti = rtl.double;',
  32221. '$mod.tiInt = rtl.shortint;',
  32222. '$mod.tiInt = rtl.byte;',
  32223. '$mod.tiInt = rtl.smallint;',
  32224. '$mod.tiInt = rtl.word;',
  32225. '$mod.tiInt = rtl.longint;',
  32226. '$mod.tiInt = rtl.longword;',
  32227. '$mod.ti = rtl.jsvalue;',
  32228. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32229. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32230. '']));
  32231. end;
  32232. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32233. begin
  32234. WithTypeInfo:=true;
  32235. StartProgram(true,[supTypeInfo]);
  32236. Add('{$modeswitch externalclass}');
  32237. Add('type');
  32238. Add(' TStaticArr = array[boolean] of string;');
  32239. Add(' TDynArr = array of string;');
  32240. Add(' TProc = procedure;');
  32241. Add(' TMethod = procedure of object;');
  32242. Add('var');
  32243. Add(' StaticArray: TStaticArr;');
  32244. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32245. Add(' DynArray: TDynArr;');
  32246. Add(' tiDynArray: TTypeInfoDynArray;');
  32247. Add(' ProcVar: TProc;');
  32248. Add(' tiProcVar: TTypeInfoProcVar;');
  32249. Add(' MethodVar: TMethod;');
  32250. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32251. Add('begin');
  32252. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32253. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32254. Add(' tiDynArray:=typeinfo(DynArray);');
  32255. Add(' tiDynArray:=typeinfo(TDynArr);');
  32256. Add(' tiProcVar:=typeinfo(ProcVar);');
  32257. Add(' tiProcVar:=typeinfo(TProc);');
  32258. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32259. Add(' tiMethodVar:=typeinfo(TMethod);');
  32260. ConvertProgram;
  32261. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32262. LinesToStr([ // statements
  32263. 'this.$rtti.$StaticArray("TStaticArr", {',
  32264. ' dims: [2],',
  32265. ' eltype: rtl.string',
  32266. '});',
  32267. 'this.$rtti.$DynArray("TDynArr", {',
  32268. ' eltype: rtl.string',
  32269. '});',
  32270. 'this.$rtti.$ProcVar("TProc", {',
  32271. ' procsig: rtl.newTIProcSig([])',
  32272. '});',
  32273. 'this.$rtti.$MethodVar("TMethod", {',
  32274. ' procsig: rtl.newTIProcSig([]),',
  32275. ' methodkind: 0',
  32276. '});',
  32277. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32278. 'this.tiStaticArray = null;',
  32279. 'this.DynArray = [];',
  32280. 'this.tiDynArray = null;',
  32281. 'this.ProcVar = null;',
  32282. 'this.tiProcVar = null;',
  32283. 'this.MethodVar = null;',
  32284. 'this.tiMethodVar = null;',
  32285. '']),
  32286. LinesToStr([ // $mod.$main
  32287. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32288. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32289. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32290. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32291. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32292. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32293. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32294. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32295. '']));
  32296. end;
  32297. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32298. begin
  32299. WithTypeInfo:=true;
  32300. StartProgram(true,[supTypeInfo]);
  32301. Add('{$modeswitch externalclass}');
  32302. Add('type');
  32303. Add(' TRec = record end;');
  32304. // ToDo: ^TRec
  32305. Add(' TObject = class end;');
  32306. Add(' TClass = class of tobject;');
  32307. Add('var');
  32308. Add(' Rec: trec;');
  32309. Add(' tiRecord: ttypeinforecord;');
  32310. Add(' Obj: tobject;');
  32311. Add(' tiClass: ttypeinfoclass;');
  32312. Add(' aClass: tclass;');
  32313. Add(' tiClassRef: ttypeinfoclassref;');
  32314. // ToDo: ^TRec
  32315. Add(' tiPointer: ttypeinfopointer;');
  32316. Add('begin');
  32317. Add(' tirecord:=typeinfo(trec);');
  32318. Add(' tirecord:=typeinfo(trec);');
  32319. Add(' ticlass:=typeinfo(obj);');
  32320. Add(' ticlass:=typeinfo(tobject);');
  32321. Add(' ticlass:=typeinfo(aclass);');
  32322. Add(' ticlassref:=typeinfo(tclass);');
  32323. ConvertProgram;
  32324. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32325. LinesToStr([ // statements
  32326. 'rtl.recNewT(this, "TRec", function () {',
  32327. ' this.$eq = function (b) {',
  32328. ' return true;',
  32329. ' };',
  32330. ' this.$assign = function (s) {',
  32331. ' return this;',
  32332. ' };',
  32333. ' $mod.$rtti.$Record("TRec", {});',
  32334. '});',
  32335. 'rtl.createClass(this, "TObject", null, function () {',
  32336. ' this.$init = function () {',
  32337. ' };',
  32338. ' this.$final = function () {',
  32339. ' };',
  32340. '});',
  32341. 'this.$rtti.$ClassRef("TClass", {',
  32342. ' instancetype: this.$rtti["TObject"]',
  32343. '});',
  32344. 'this.Rec = this.TRec.$new();',
  32345. 'this.tiRecord = null;',
  32346. 'this.Obj = null;',
  32347. 'this.tiClass = null;',
  32348. 'this.aClass = null;',
  32349. 'this.tiClassRef = null;',
  32350. 'this.tiPointer = null;',
  32351. '']),
  32352. LinesToStr([ // $mod.$main
  32353. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32354. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32355. '$mod.tiClass = $mod.Obj.$rtti;',
  32356. '$mod.tiClass = $mod.$rtti["TObject"];',
  32357. '$mod.tiClass = $mod.aClass.$rtti;',
  32358. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32359. '']));
  32360. end;
  32361. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32362. begin
  32363. WithTypeInfo:=true;
  32364. StartProgram(true,[supTypeInfo]);
  32365. Add([
  32366. '{$modeswitch externalclass}',
  32367. 'type',
  32368. ' TClass = class of tobject;',
  32369. ' TObject = class',
  32370. ' function MyClass: TClass;',
  32371. ' class function ClassType: TClass;',
  32372. ' end;',
  32373. 'function TObject.MyClass: TClass;',
  32374. 'var t: TTypeInfoClass;',
  32375. 'begin',
  32376. ' t:=TypeInfo(Self);',
  32377. ' t:=TypeInfo(Result);',
  32378. ' t:=TypeInfo(TObject);',
  32379. 'end;',
  32380. 'class function TObject.ClassType: TClass;',
  32381. 'var t: TTypeInfoClass;',
  32382. 'begin',
  32383. ' t:=TypeInfo(Self);',
  32384. ' t:=TypeInfo(Result);',
  32385. 'end;',
  32386. 'var',
  32387. ' Obj: TObject;',
  32388. ' t: TTypeInfoClass;',
  32389. 'begin',
  32390. ' t:=TypeInfo(TObject.ClassType);',
  32391. ' t:=TypeInfo(Obj.ClassType);',
  32392. ' t:=TypeInfo(Obj.MyClass);',
  32393. '']);
  32394. ConvertProgram;
  32395. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32396. LinesToStr([ // statements
  32397. 'this.$rtti.$Class("TObject");',
  32398. 'this.$rtti.$ClassRef("TClass", {',
  32399. ' instancetype: this.$rtti["TObject"]',
  32400. '});',
  32401. 'rtl.createClass(this, "TObject", null, function () {',
  32402. ' this.$init = function () {',
  32403. ' };',
  32404. ' this.$final = function () {',
  32405. ' };',
  32406. ' this.MyClass = function () {',
  32407. ' var Result = null;',
  32408. ' var t = null;',
  32409. ' t = this.$rtti;',
  32410. ' t = Result.$rtti;',
  32411. ' t = $mod.$rtti["TObject"];',
  32412. ' return Result;',
  32413. ' };',
  32414. ' this.ClassType = function () {',
  32415. ' var Result = null;',
  32416. ' var t = null;',
  32417. ' t = this.$rtti;',
  32418. ' t = Result.$rtti;',
  32419. ' return Result;',
  32420. ' };',
  32421. '});',
  32422. 'this.Obj = null;',
  32423. 'this.t = null;',
  32424. '']),
  32425. LinesToStr([ // $mod.$main
  32426. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32427. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32428. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32429. '']));
  32430. end;
  32431. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32432. begin
  32433. WithTypeInfo:=true;
  32434. AddModuleWithIntfImplSrc('typinfo.pas',
  32435. LinesToStr([
  32436. '{$modeswitch externalclass}',
  32437. 'type',
  32438. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32439. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32440. '']),
  32441. '');
  32442. AddModuleWithIntfImplSrc('unit2.pas',
  32443. LinesToStr([
  32444. 'uses typinfo;',
  32445. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32446. 'procedure DoPtr(p: PTypeInfo);',
  32447. 'procedure DoInfo(t: TTypeInfo);',
  32448. 'procedure DoInt(t: TTypeInfoInteger);',
  32449. '']),
  32450. LinesToStr([
  32451. 'procedure DoPtr(p: PTypeInfo);',
  32452. 'begin end;',
  32453. 'procedure DoInfo(t: TTypeInfo);',
  32454. 'begin end;',
  32455. 'procedure DoInt(t: TTypeInfoInteger);',
  32456. 'begin end;',
  32457. '']));
  32458. StartUnit(true);
  32459. Add([
  32460. 'interface',
  32461. 'uses unit2;', // does not use unit typinfo
  32462. 'implementation',
  32463. 'var',
  32464. ' i: byte;',
  32465. ' p: pointer;',
  32466. ' t: PTypeInfo;',
  32467. 'initialization',
  32468. ' p:=typeinfo(i);',
  32469. ' t:=typeinfo(i);',
  32470. ' if p=t then ;',
  32471. ' if p=typeinfo(i) then ;',
  32472. ' if typeinfo(i)=p then ;',
  32473. ' if t=typeinfo(i) then ;',
  32474. ' if typeinfo(i)=t then ;',
  32475. ' DoPtr(p);',
  32476. ' DoPtr(t);',
  32477. ' DoPtr(typeinfo(i));',
  32478. ' DoInfo(p);',
  32479. ' DoInfo(t);',
  32480. ' DoInfo(typeinfo(i));',
  32481. ' DoInt(typeinfo(i));',
  32482. '']);
  32483. ConvertUnit;
  32484. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32485. LinesToStr([ // statements
  32486. 'var $impl = $mod.$impl;',
  32487. '']),
  32488. LinesToStr([ // this.$init
  32489. '$impl.p = rtl.byte;',
  32490. '$impl.t = rtl.byte;',
  32491. 'if ($impl.p === $impl.t) ;',
  32492. 'if ($impl.p === rtl.byte) ;',
  32493. 'if (rtl.byte === $impl.p) ;',
  32494. 'if ($impl.t === rtl.byte) ;',
  32495. 'if (rtl.byte === $impl.t) ;',
  32496. 'pas.unit2.DoPtr($impl.p);',
  32497. 'pas.unit2.DoPtr($impl.t);',
  32498. 'pas.unit2.DoPtr(rtl.byte);',
  32499. 'pas.unit2.DoInfo($impl.p);',
  32500. 'pas.unit2.DoInfo($impl.t);',
  32501. 'pas.unit2.DoInfo(rtl.byte);',
  32502. 'pas.unit2.DoInt(rtl.byte);',
  32503. '']),
  32504. LinesToStr([ // implementation
  32505. '$impl.i = 0;',
  32506. '$impl.p = null;',
  32507. '$impl.t = null;',
  32508. '']) );
  32509. end;
  32510. procedure TTestModule.TestRTTI_Interface_Corba;
  32511. begin
  32512. WithTypeInfo:=true;
  32513. StartProgram(true,[supTypeInfo]);
  32514. Add([
  32515. '{$interfaces corba}',
  32516. '{$modeswitch externalclass}',
  32517. 'type',
  32518. ' IUnknown = interface',
  32519. ' end;',
  32520. ' IBird = interface',
  32521. ' function GetItem: longint;',
  32522. ' procedure SetItem(Value: longint);',
  32523. ' property Item: longint read GetItem write SetItem;',
  32524. ' end;',
  32525. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32526. 'var',
  32527. ' i: IBird;',
  32528. ' t: TTypeInfoInterface;',
  32529. 'begin',
  32530. ' t:=TypeInfo(IBird);',
  32531. ' t:=TypeInfo(i);',
  32532. ' DoIt(t);',
  32533. ' DoIt(TypeInfo(IBird));',
  32534. '']);
  32535. ConvertProgram;
  32536. CheckSource('TestRTTI_Interface_Corba',
  32537. LinesToStr([ // statements
  32538. 'rtl.createInterface(',
  32539. ' this,',
  32540. ' "IUnknown",',
  32541. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32542. ' [],',
  32543. ' null,',
  32544. ' function () {',
  32545. ' }',
  32546. ');',
  32547. 'rtl.createInterface(',
  32548. ' this,',
  32549. ' "IBird",',
  32550. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32551. ' ["GetItem", "SetItem"],',
  32552. ' null,',
  32553. ' function () {',
  32554. ' var $r = this.$rtti;',
  32555. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32556. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32557. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32558. ' }',
  32559. ');',
  32560. 'this.DoIt = function (t) {',
  32561. '}; ',
  32562. 'this.i = null;',
  32563. 'this.t = null;',
  32564. '']),
  32565. LinesToStr([ // $mod.$main
  32566. '$mod.t = $mod.$rtti["IBird"];',
  32567. '$mod.t = $mod.i.$rtti;',
  32568. '$mod.DoIt($mod.t);',
  32569. '$mod.DoIt($mod.$rtti["IBird"]);',
  32570. '']));
  32571. end;
  32572. procedure TTestModule.TestRTTI_Interface_COM;
  32573. begin
  32574. WithTypeInfo:=true;
  32575. StartProgram(true,[supTypeInfo]);
  32576. Add([
  32577. '{$interfaces com}',
  32578. '{$modeswitch externalclass}',
  32579. 'type',
  32580. ' TGuid = record end;',
  32581. ' integer = longint;',
  32582. ' IUnknown = interface',
  32583. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32584. ' function _AddRef: Integer;',
  32585. ' function _Release: Integer;',
  32586. ' end;',
  32587. ' IBird = interface',
  32588. ' function GetItem: longint;',
  32589. ' procedure SetItem(Value: longint);',
  32590. ' property Item: longint read GetItem write SetItem;',
  32591. ' end;',
  32592. 'var',
  32593. ' i: IBird;',
  32594. ' t: TTypeInfoInterface;',
  32595. 'begin',
  32596. ' t:=TypeInfo(IBird);',
  32597. ' t:=TypeInfo(i);',
  32598. '']);
  32599. ConvertProgram;
  32600. CheckSource('TestRTTI_Interface_COM',
  32601. LinesToStr([ // statements
  32602. 'rtl.recNewT(this, "TGuid", function () {',
  32603. ' this.$eq = function (b) {',
  32604. ' return true;',
  32605. ' };',
  32606. ' this.$assign = function (s) {',
  32607. ' return this;',
  32608. ' };',
  32609. ' $mod.$rtti.$Record("TGuid", {});',
  32610. '});',
  32611. 'rtl.createInterface(',
  32612. ' this,',
  32613. ' "IUnknown",',
  32614. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32615. ' ["QueryInterface", "_AddRef", "_Release"],',
  32616. ' null,',
  32617. ' function () {',
  32618. ' this.$kind = "com";',
  32619. ' var $r = this.$rtti;',
  32620. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32621. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32622. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32623. ' }',
  32624. ');',
  32625. 'rtl.createInterface(',
  32626. ' this,',
  32627. ' "IBird",',
  32628. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32629. ' ["GetItem", "SetItem"],',
  32630. ' this.IUnknown,',
  32631. ' function () {',
  32632. ' var $r = this.$rtti;',
  32633. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32634. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32635. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32636. ' }',
  32637. ');',
  32638. 'this.i = null;',
  32639. 'this.t = null;',
  32640. '']),
  32641. LinesToStr([ // $mod.$main
  32642. '$mod.t = $mod.$rtti["IBird"];',
  32643. '$mod.t = $mod.i.$rtti;',
  32644. '']));
  32645. end;
  32646. procedure TTestModule.TestRTTI_ClassHelper;
  32647. begin
  32648. WithTypeInfo:=true;
  32649. StartProgram(true,[supTypeInfo]);
  32650. Add([
  32651. '{$interfaces com}',
  32652. '{$modeswitch externalclass}',
  32653. 'type',
  32654. ' TObject = class',
  32655. ' end;',
  32656. ' THelper = class helper for TObject',
  32657. ' published',
  32658. ' function GetItem: longint;',
  32659. ' property Item: longint read GetItem;',
  32660. ' end;',
  32661. 'function THelper.GetItem: longint;',
  32662. 'begin',
  32663. 'end;',
  32664. 'var',
  32665. ' t: TTypeInfoHelper;',
  32666. 'begin',
  32667. ' t:=TypeInfo(THelper);',
  32668. '']);
  32669. ConvertProgram;
  32670. CheckSource('TestRTTI_ClassHelper',
  32671. LinesToStr([ // statements
  32672. 'rtl.createClass(this, "TObject", null, function () {',
  32673. ' this.$init = function () {',
  32674. ' };',
  32675. ' this.$final = function () {',
  32676. ' };',
  32677. '});',
  32678. 'rtl.createHelper(this, "THelper", null, function () {',
  32679. ' this.GetItem = function () {',
  32680. ' var Result = 0;',
  32681. ' return Result;',
  32682. ' };',
  32683. ' var $r = this.$rtti;',
  32684. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32685. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32686. '});',
  32687. 'this.t = null;',
  32688. '']),
  32689. LinesToStr([ // $mod.$main
  32690. '$mod.t = $mod.$rtti["THelper"];',
  32691. '']));
  32692. end;
  32693. procedure TTestModule.TestRTTI_ExternalClass;
  32694. begin
  32695. WithTypeInfo:=true;
  32696. StartProgram(true,[supTypeInfo]);
  32697. Add([
  32698. '{$modeswitch externalclass}',
  32699. 'type',
  32700. ' TJSObject = class external name ''Object''',
  32701. ' end;',
  32702. ' TJSArray = class external name ''Array'' (TJSObject)',
  32703. ' end;',
  32704. 'var',
  32705. ' p: Pointer;',
  32706. ' tc: TTypeInfoExtClass;',
  32707. 'begin',
  32708. ' p:=typeinfo(TJSArray);']);
  32709. ConvertProgram;
  32710. CheckSource('TestRTTI_ExternalClass',
  32711. LinesToStr([ // statements
  32712. 'this.$rtti.$ExtClass("TJSObject", {',
  32713. ' jsclass: "Object"',
  32714. '});',
  32715. 'this.$rtti.$ExtClass("TJSArray", {',
  32716. ' ancestor: this.$rtti["TJSObject"],',
  32717. ' jsclass: "Array"',
  32718. '});',
  32719. 'this.p = null;',
  32720. 'this.tc = null;',
  32721. '']),
  32722. LinesToStr([ // $mod.$main
  32723. '$mod.p = $mod.$rtti["TJSArray"];',
  32724. '']));
  32725. end;
  32726. procedure TTestModule.TestRTTI_Unit;
  32727. begin
  32728. WithTypeInfo:=true;
  32729. AddModuleWithIntfImplSrc('unit2.pas',
  32730. LinesToStr([
  32731. '{$mode delphi}',
  32732. 'type',
  32733. ' TWordArray = array of word;',
  32734. ' TArray<T> = array of T;',
  32735. '']),
  32736. '');
  32737. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32738. Add([
  32739. '{$mode delphi}',
  32740. 'interface',
  32741. 'uses unit2;',
  32742. 'type',
  32743. ' IBird = interface',
  32744. ' function Swoop: TWordArray;',
  32745. ' function Glide: TArray<word>;',
  32746. ' end;',
  32747. 'procedure Fly;',
  32748. 'implementation',
  32749. 'procedure Fly;',
  32750. 'var',
  32751. ' ta: tTypeInfoDynArray;',
  32752. ' ti: tTypeInfoInterface;',
  32753. 'begin',
  32754. ' ta:=typeinfo(TWordArray);',
  32755. ' ta:=typeinfo(TArray<word>);',
  32756. ' ti:=typeinfo(IBird);',
  32757. 'end;',
  32758. '']);
  32759. ConvertUnit;
  32760. CheckSource('TestRTTI_ExternalClass',
  32761. LinesToStr([ // statements
  32762. 'rtl.createInterface(',
  32763. ' this,',
  32764. ' "IBird",',
  32765. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32766. ' ["Swoop", "Glide"],',
  32767. ' pas.system.IUnknown,',
  32768. ' function () {',
  32769. ' var $r = this.$rtti;',
  32770. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32771. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32772. ' }',
  32773. ');',
  32774. 'this.Fly = function () {',
  32775. ' var ta = null;',
  32776. ' var ti = null;',
  32777. ' ta = pas.unit2.$rtti["TWordArray"];',
  32778. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32779. ' ti = $mod.$rtti["IBird"];',
  32780. '};',
  32781. '']),
  32782. LinesToStr([ // $mod.$main
  32783. '']));
  32784. end;
  32785. procedure TTestModule.TestResourcestringProgram;
  32786. begin
  32787. AddModuleWithIntfImplSrc('unit2.pas',
  32788. LinesToStr([
  32789. 'resourcestring Title = ''Nice'';',
  32790. '']),
  32791. '');
  32792. StartProgram(true);
  32793. Add([
  32794. 'uses unit2;',
  32795. 'const Bar = ''bar'';',
  32796. 'resourcestring',
  32797. ' Red = ''red'';',
  32798. ' Foobar = ''fOo''+bar;',
  32799. 'var s: string;',
  32800. ' c: char;',
  32801. 'begin',
  32802. ' s:=red;',
  32803. ' s:=test1.red;',
  32804. ' s:=Title;',
  32805. ' c:=red[1];',
  32806. ' c:=test1.red[2];',
  32807. ' if red=foobar then ;',
  32808. ' if red[3]=red[4] then ;']);
  32809. ConvertProgram;
  32810. CheckSource('TestResourcestringProgram',
  32811. LinesToStr([ // statements
  32812. 'this.Bar = "bar";',
  32813. 'this.s = "";',
  32814. 'this.c = "";',
  32815. '$mod.$resourcestrings = {',
  32816. ' Red: {',
  32817. ' org: "red"',
  32818. ' },',
  32819. ' Foobar: {',
  32820. ' org: "fOobar"',
  32821. ' }',
  32822. '};',
  32823. '']),
  32824. LinesToStr([ // $mod.$main
  32825. '$mod.s = rtl.getResStr($mod, "Red");',
  32826. '$mod.s = rtl.getResStr($mod, "Red");',
  32827. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32828. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32829. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32830. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32831. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32832. '']));
  32833. end;
  32834. procedure TTestModule.TestResourcestringUnit;
  32835. begin
  32836. AddModuleWithIntfImplSrc('unit2.pas',
  32837. LinesToStr([
  32838. 'resourcestring Title = ''Nice'';',
  32839. '']),
  32840. '');
  32841. StartUnit(true);
  32842. Add([
  32843. 'interface',
  32844. 'uses unit2;',
  32845. 'const Red = ''rEd'';',
  32846. 'resourcestring',
  32847. ' Blue = ''blue'';',
  32848. ' NotRed = ''not''+Red;',
  32849. 'var s: string;',
  32850. 'implementation',
  32851. 'resourcestring',
  32852. ' ImplGreen = ''green'';',
  32853. 'initialization',
  32854. ' s:=blue+ImplGreen;',
  32855. ' s:=test1.blue+test1.implgreen;',
  32856. ' s:=blue[1]+implgreen[2];',
  32857. ' s:=Title;',
  32858. '']);
  32859. ConvertUnit;
  32860. CheckSource('TestResourcestringUnit',
  32861. LinesToStr([ // statements
  32862. 'this.Red = "rEd";',
  32863. 'this.s = "";',
  32864. '$mod.$resourcestrings = {',
  32865. ' Blue: {',
  32866. ' org: "blue"',
  32867. ' },',
  32868. ' NotRed: {',
  32869. ' org: "notrEd"',
  32870. ' },',
  32871. ' ImplGreen: {',
  32872. ' org: "green"',
  32873. ' }',
  32874. '};',
  32875. '']),
  32876. LinesToStr([ // $mod.$main
  32877. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32878. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32879. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32880. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32881. '']));
  32882. end;
  32883. procedure TTestModule.TestResourcestringImplementation;
  32884. begin
  32885. StartUnit(false);
  32886. Add([
  32887. 'interface',
  32888. 'implementation',
  32889. 'resourcestring',
  32890. ' ImplRed = ''red'';']);
  32891. ConvertUnit;
  32892. CheckSource('TestResourcestringImplementation',
  32893. LinesToStr([ // intf statements
  32894. 'var $impl = $mod.$impl;']),
  32895. LinesToStr([ // $mod.$init
  32896. '']),
  32897. LinesToStr([ // impl statements
  32898. '$mod.$resourcestrings = {',
  32899. ' ImplRed: {',
  32900. ' org: "red"',
  32901. ' }',
  32902. '};',
  32903. '']));
  32904. end;
  32905. procedure TTestModule.TestAttributes_Members;
  32906. begin
  32907. WithTypeInfo:=true;
  32908. StartProgram(false);
  32909. Add([
  32910. '{$modeswitch PrefixedAttributes}',
  32911. 'type',
  32912. ' TObject = class',
  32913. ' constructor Create;',
  32914. ' end;',
  32915. ' TCustomAttribute = class',
  32916. ' constructor Create(Id: word);',
  32917. ' end;',
  32918. ' [Missing]',
  32919. ' TBird = class',
  32920. ' published',
  32921. ' [Tcustom]',
  32922. ' FField: word;',
  32923. ' [tcustom(14)]',
  32924. ' property Size: word read FField;',
  32925. ' [Tcustom(15)]',
  32926. ' procedure Fly; virtual; abstract;',
  32927. ' end;',
  32928. ' TRec = record',
  32929. ' [Tcustom,tcustom(14)]',
  32930. ' Size: word;',
  32931. ' end;',
  32932. 'constructor TObject.Create; begin end;',
  32933. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32934. 'begin',
  32935. '']);
  32936. ConvertProgram;
  32937. CheckSource('TestAttributes_Members',
  32938. LinesToStr([ // statements
  32939. 'rtl.createClass(this, "TObject", null, function () {',
  32940. ' this.$init = function () {',
  32941. ' };',
  32942. ' this.$final = function () {',
  32943. ' };',
  32944. ' this.Create = function () {',
  32945. ' return this;',
  32946. ' };',
  32947. '});',
  32948. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32949. ' this.Create$1 = function (Id) {',
  32950. ' return this;',
  32951. ' };',
  32952. '});',
  32953. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32954. ' this.$init = function () {',
  32955. ' $mod.TObject.$init.call(this);',
  32956. ' this.FField = 0;',
  32957. ' };',
  32958. ' var $r = this.$rtti;',
  32959. ' $r.addField("FField", rtl.word, {',
  32960. ' attr: [$mod.TCustomAttribute, "Create"]',
  32961. ' });',
  32962. ' $r.addProperty(',
  32963. ' "Size",',
  32964. ' 0,',
  32965. ' rtl.word,',
  32966. ' "FField",',
  32967. ' "",',
  32968. ' {',
  32969. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32970. ' }',
  32971. ' );',
  32972. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32973. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32974. ' });',
  32975. '});',
  32976. 'rtl.recNewT(this, "TRec", function () {',
  32977. ' this.Size = 0;',
  32978. ' this.$eq = function (b) {',
  32979. ' return this.Size === b.Size;',
  32980. ' };',
  32981. ' this.$assign = function (s) {',
  32982. ' this.Size = s.Size;',
  32983. ' return this;',
  32984. ' };',
  32985. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32986. ' $r.addField("Size", rtl.word, {',
  32987. ' attr: [',
  32988. ' $mod.TCustomAttribute,',
  32989. ' "Create",',
  32990. ' $mod.TCustomAttribute,',
  32991. ' "Create$1",',
  32992. ' [14]',
  32993. ' ]',
  32994. ' });',
  32995. '});',
  32996. '']),
  32997. LinesToStr([ // $mod.$main
  32998. '']));
  32999. end;
  33000. procedure TTestModule.TestAttributes_Types;
  33001. begin
  33002. WithTypeInfo:=true;
  33003. StartProgram(false);
  33004. Add([
  33005. '{$modeswitch PrefixedAttributes}',
  33006. 'type',
  33007. ' TObject = class',
  33008. ' constructor Create(Id: word);',
  33009. ' end;',
  33010. ' TCustomAttribute = class',
  33011. ' end;',
  33012. ' [TCustom(1)]',
  33013. ' TMyClass = class',
  33014. ' end;',
  33015. ' [TCustom(11)]',
  33016. ' TMyDescendant = class(TMyClass)',
  33017. ' end;',
  33018. ' [TCustom(2)]',
  33019. ' TRec = record',
  33020. ' end;',
  33021. ' [TCustom(3)]',
  33022. ' TInt = type word;',
  33023. 'constructor TObject.Create(Id: word);',
  33024. 'begin',
  33025. 'end;',
  33026. 'var p: pointer;',
  33027. 'begin',
  33028. ' p:=typeinfo(TMyClass);',
  33029. ' p:=typeinfo(TRec);',
  33030. ' p:=typeinfo(TInt);',
  33031. '']);
  33032. ConvertProgram;
  33033. CheckSource('TestAttributes_Types',
  33034. LinesToStr([ // statements
  33035. 'rtl.createClass(this, "TObject", null, function () {',
  33036. ' this.$init = function () {',
  33037. ' };',
  33038. ' this.$final = function () {',
  33039. ' };',
  33040. ' this.Create = function (Id) {',
  33041. ' return this;',
  33042. ' };',
  33043. '});',
  33044. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33045. '});',
  33046. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33047. ' var $r = this.$rtti;',
  33048. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33049. '});',
  33050. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33051. ' var $r = this.$rtti;',
  33052. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33053. '});',
  33054. 'rtl.recNewT(this, "TRec", function () {',
  33055. ' this.$eq = function (b) {',
  33056. ' return true;',
  33057. ' };',
  33058. ' this.$assign = function (s) {',
  33059. ' return this;',
  33060. ' };',
  33061. ' $mod.$rtti.$Record("TRec", {',
  33062. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33063. ' });',
  33064. '});',
  33065. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33066. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33067. '});',
  33068. 'this.p = null;',
  33069. '']),
  33070. LinesToStr([ // $mod.$main
  33071. '$mod.p = $mod.$rtti["TMyClass"];',
  33072. '$mod.p = $mod.$rtti["TRec"];',
  33073. '$mod.p = $mod.$rtti["TInt"];',
  33074. '']));
  33075. end;
  33076. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33077. begin
  33078. WithTypeInfo:=true;
  33079. StartProgram(false);
  33080. Add([
  33081. '{$modeswitch PrefixedAttributes}',
  33082. 'type',
  33083. ' TObject = class',
  33084. ' constructor Create;',
  33085. ' end;',
  33086. ' TCustomAttribute = class',
  33087. ' end;',
  33088. ' THelper = class helper for TCustomAttribute',
  33089. ' constructor Create(Id: word);',
  33090. ' end;',
  33091. ' [TCustom(3)]',
  33092. ' TMyInt = word;',
  33093. 'constructor TObject.Create; begin end;',
  33094. 'constructor THelper.Create(Id: word); begin end;',
  33095. 'begin',
  33096. ' if typeinfo(TMyInt)=nil then ;']);
  33097. ConvertProgram;
  33098. end;
  33099. procedure TTestModule.TestAttributes_InterfacesList;
  33100. begin
  33101. WithTypeInfo:=true;
  33102. StartProgram(false);
  33103. Add([
  33104. '{$mode Delphi}',
  33105. 'type',
  33106. ' TObject = class',
  33107. ' constructor Create;',
  33108. ' end;',
  33109. ' IInterface = interface end;',
  33110. ' TCustomAttribute = class',
  33111. ' end;',
  33112. ' Red = class(TCustomAttribute);',
  33113. ' Blue = class(TCustomAttribute);',
  33114. ' [Red]',
  33115. ' IBird<T> = interface',
  33116. ' procedure Fly;',
  33117. ' end;',
  33118. ' [Blue]',
  33119. ' IEagle = interface(IBird<Word>)',
  33120. ' procedure Dive;',
  33121. ' end;',
  33122. ' TAnt = class(TObject, IEagle)',
  33123. ' procedure Fly; virtual; abstract;',
  33124. ' procedure Dive; virtual; abstract;',
  33125. ' end;',
  33126. 'constructor TObject.Create;',
  33127. 'begin',
  33128. 'end;',
  33129. 'begin',
  33130. '']);
  33131. ConvertProgram;
  33132. CheckSource('TestAttributes_InterfacesList',
  33133. LinesToStr([ // statements
  33134. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33135. 'rtl.createClass(this, "TObject", null, function () {',
  33136. ' this.$init = function () {',
  33137. ' };',
  33138. ' this.$final = function () {',
  33139. ' };',
  33140. ' this.Create = function () {',
  33141. ' return this;',
  33142. ' };',
  33143. '});',
  33144. 'rtl.createInterface(',
  33145. ' this,',
  33146. ' "IInterface",',
  33147. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33148. ' [],',
  33149. ' null,',
  33150. ' function () {',
  33151. ' this.$kind = "com";',
  33152. ' }',
  33153. ');',
  33154. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33155. '});',
  33156. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33157. '});',
  33158. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33159. '});',
  33160. 'rtl.createInterface(',
  33161. ' this,',
  33162. ' "IBird$G1",',
  33163. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33164. ' ["Fly"],',
  33165. ' this.IInterface,',
  33166. ' function () {',
  33167. ' var $r = this.$rtti;',
  33168. ' $r.addMethod("Fly", 0, []);',
  33169. ' $r.attr = [$mod.Red, "Create"];',
  33170. ' },',
  33171. ' "IBird<System.Word>"',
  33172. ');',
  33173. 'rtl.createInterface(',
  33174. ' this,',
  33175. ' "IEagle",',
  33176. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33177. ' ["Dive"],',
  33178. ' this.IBird$G1,',
  33179. ' function () {',
  33180. ' var $r = this.$rtti;',
  33181. ' $r.addMethod("Dive", 0, []);',
  33182. ' $r.attr = [$mod.Blue, "Create"];',
  33183. ' }',
  33184. ');',
  33185. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33186. ' rtl.addIntf(this, $mod.IEagle);',
  33187. '});',
  33188. '']),
  33189. LinesToStr([ // $mod.$main
  33190. '']));
  33191. end;
  33192. procedure TTestModule.TestAssert;
  33193. begin
  33194. StartProgram(false);
  33195. Add([
  33196. 'procedure DoIt;',
  33197. 'var',
  33198. ' b: boolean;',
  33199. ' s: string;',
  33200. 'begin',
  33201. ' {$Assertions on}',
  33202. ' Assert(b);',
  33203. 'end;',
  33204. 'begin',
  33205. ' DoIt;',
  33206. '']);
  33207. ConvertProgram;
  33208. CheckSource('TestAssert',
  33209. LinesToStr([ // statements
  33210. 'this.DoIt = function () {',
  33211. ' var b = false;',
  33212. ' var s = "";',
  33213. ' if (!b) throw "assert failed";',
  33214. '};',
  33215. '']),
  33216. LinesToStr([ // $mod.$main
  33217. '$mod.DoIt();',
  33218. '']));
  33219. end;
  33220. procedure TTestModule.TestAssert_SysUtils;
  33221. begin
  33222. AddModuleWithIntfImplSrc('SysUtils.pas',
  33223. LinesToStr([
  33224. 'type',
  33225. ' TObject = class',
  33226. ' constructor Create;',
  33227. ' end;',
  33228. ' EAssertionFailed = class',
  33229. ' constructor Create(s: string);',
  33230. ' end;',
  33231. '']),
  33232. LinesToStr([
  33233. 'constructor TObject.Create;',
  33234. 'begin end;',
  33235. 'constructor EAssertionFailed.Create(s: string);',
  33236. 'begin end;',
  33237. '']) );
  33238. StartProgram(true);
  33239. Add([
  33240. 'uses sysutils;',
  33241. 'procedure DoIt;',
  33242. 'var',
  33243. ' b: boolean;',
  33244. ' s: string;',
  33245. 'begin',
  33246. ' {$Assertions on}',
  33247. ' Assert(b);',
  33248. ' Assert(b,''msg'');',
  33249. 'end;',
  33250. 'begin',
  33251. ' DoIt;',
  33252. '']);
  33253. ConvertProgram;
  33254. CheckSource('TestAssert_SysUtils',
  33255. LinesToStr([ // statements
  33256. 'this.DoIt = function () {',
  33257. ' var b = false;',
  33258. ' var s = "";',
  33259. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33260. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33261. '};',
  33262. '']),
  33263. LinesToStr([ // $mod.$main
  33264. '$mod.DoIt();',
  33265. '']));
  33266. end;
  33267. procedure TTestModule.TestObjectChecks;
  33268. begin
  33269. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33270. StartProgram(false);
  33271. Add([
  33272. 'type',
  33273. ' TObject = class',
  33274. ' procedure DoIt;',
  33275. ' end;',
  33276. ' TClass = class of tobject;',
  33277. ' TBird = class',
  33278. ' end;',
  33279. ' TBirdClass = class of TBird;',
  33280. 'var',
  33281. ' o : TObject;',
  33282. ' c: TClass;',
  33283. ' b: TBird;',
  33284. ' bc: TBirdClass;',
  33285. 'procedure TObject.DoIt;',
  33286. 'begin',
  33287. ' b:=TBird(o);',
  33288. 'end;',
  33289. 'begin',
  33290. ' o.DoIt;',
  33291. ' b:=TBird(o);',
  33292. ' bc:=TBirdClass(c);',
  33293. '']);
  33294. ConvertProgram;
  33295. CheckSource('TestCheckMethodCall',
  33296. LinesToStr([ // statements
  33297. 'rtl.createClass(this, "TObject", null, function () {',
  33298. ' this.$init = function () {',
  33299. ' };',
  33300. ' this.$final = function () {',
  33301. ' };',
  33302. ' this.DoIt = function () {',
  33303. ' rtl.checkMethodCall(this,$mod.TObject);',
  33304. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33305. ' };',
  33306. '});',
  33307. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33308. '});',
  33309. 'this.o = null;',
  33310. 'this.c = null;',
  33311. 'this.b = null;',
  33312. 'this.bc = null;',
  33313. '']),
  33314. LinesToStr([ // $mod.$main
  33315. '$mod.o.DoIt();',
  33316. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33317. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33318. '']));
  33319. end;
  33320. procedure TTestModule.TestOverflowChecks_Int;
  33321. begin
  33322. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33323. StartProgram(false);
  33324. Add([
  33325. 'procedure DoIt;',
  33326. 'var',
  33327. ' b: byte;',
  33328. ' n: nativeint;',
  33329. ' u: nativeuint;',
  33330. ' c: currency;',
  33331. 'begin',
  33332. ' n:=n+n;',
  33333. ' n:=n-n;',
  33334. ' n:=n+b;',
  33335. ' n:=b-n;',
  33336. ' n:=n*n;',
  33337. ' n:=n*u;',
  33338. ' c:=c+b;',
  33339. ' c:=b+c;',
  33340. ' c:=c*b;',
  33341. ' c:=b*c;',
  33342. 'end;',
  33343. 'begin',
  33344. '']);
  33345. ConvertProgram;
  33346. CheckSource('TestOverflowChecks_Int',
  33347. LinesToStr([ // statements
  33348. 'this.DoIt = function () {',
  33349. ' var b = 0;',
  33350. ' var n = 0;',
  33351. ' var u = 0;',
  33352. ' var c = 0;',
  33353. ' n = rtl.oc(n + n);',
  33354. ' n = rtl.oc(n - n);',
  33355. ' n = rtl.oc(n + b);',
  33356. ' n = rtl.oc(b - n);',
  33357. ' n = rtl.oc(n * n);',
  33358. ' n = rtl.oc(n * u);',
  33359. ' c = rtl.oc(c + (b * 10000));',
  33360. ' c = rtl.oc((b * 10000) + c);',
  33361. ' c = rtl.oc(c * b);',
  33362. ' c = rtl.oc(b * c);',
  33363. '};',
  33364. '']),
  33365. LinesToStr([ // $mod.$main
  33366. '']));
  33367. end;
  33368. procedure TTestModule.TestRangeChecks_AssignInt;
  33369. begin
  33370. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33371. StartProgram(false);
  33372. Add([
  33373. '{$R+}',
  33374. 'var',
  33375. ' b: byte = 2;',
  33376. ' w: word = 3;',
  33377. 'procedure DoIt(p: byte);',
  33378. 'begin',
  33379. ' b:=w;',
  33380. ' b+=w;',
  33381. ' b:=1;',
  33382. 'end;',
  33383. '{$R-}',
  33384. 'procedure DoSome;',
  33385. 'begin',
  33386. ' DoIt(w);',
  33387. ' b:=w;',
  33388. ' b:=2;',
  33389. 'end;',
  33390. 'begin',
  33391. '{$R+}',
  33392. '']);
  33393. ConvertProgram;
  33394. CheckSource('TestRangeChecks_AssignInt',
  33395. LinesToStr([ // statements
  33396. 'this.b = 2;',
  33397. 'this.w = 3;',
  33398. 'this.DoIt = function (p) {',
  33399. ' rtl.rc(p, 0, 255);',
  33400. ' $mod.b = rtl.rc($mod.w,0,255);',
  33401. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33402. ' $mod.b = 1;',
  33403. '};',
  33404. 'this.DoSome = function () {',
  33405. ' $mod.DoIt($mod.w);',
  33406. ' $mod.b = $mod.w;',
  33407. ' $mod.b = 2;',
  33408. '};',
  33409. '']),
  33410. LinesToStr([ // $mod.$main
  33411. '']));
  33412. end;
  33413. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33414. begin
  33415. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33416. StartProgram(false);
  33417. Add([
  33418. '{$R+}',
  33419. 'type Ten = 1..10;',
  33420. 'var',
  33421. ' b: Ten = 2;',
  33422. ' w: Ten = 3;',
  33423. 'procedure DoIt(p: Ten);',
  33424. 'begin',
  33425. ' b:=w;',
  33426. ' b+=w;',
  33427. ' b:=1;',
  33428. 'end;',
  33429. '{$R-}',
  33430. 'procedure DoSome;',
  33431. 'begin',
  33432. ' DoIt(w);',
  33433. ' b:=w;',
  33434. ' b:=2;',
  33435. 'end;',
  33436. 'begin',
  33437. '{$R+}',
  33438. '']);
  33439. ConvertProgram;
  33440. CheckSource('TestRangeChecks_AssignIntRange',
  33441. LinesToStr([ // statements
  33442. 'this.b = 2;',
  33443. 'this.w = 3;',
  33444. 'this.DoIt = function (p) {',
  33445. ' rtl.rc(p, 1, 10);',
  33446. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33447. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33448. ' $mod.b = 1;',
  33449. '};',
  33450. 'this.DoSome = function () {',
  33451. ' $mod.DoIt($mod.w);',
  33452. ' $mod.b = $mod.w;',
  33453. ' $mod.b = 2;',
  33454. '};',
  33455. '']),
  33456. LinesToStr([ // $mod.$main
  33457. '']));
  33458. end;
  33459. procedure TTestModule.TestRangeChecks_AssignEnum;
  33460. begin
  33461. StartProgram(false);
  33462. Add([
  33463. '{$R+}',
  33464. 'type TEnum = (red,green);',
  33465. 'var',
  33466. ' e: TEnum = red;',
  33467. 'procedure DoIt(p: TEnum);',
  33468. 'begin',
  33469. ' e:=p;',
  33470. ' p:=TEnum(0);',
  33471. ' p:=succ(e);',
  33472. 'end;',
  33473. '{$R-}',
  33474. 'procedure DoSome;',
  33475. 'begin',
  33476. ' DoIt(e);',
  33477. ' e:=TEnum(1);',
  33478. ' e:=pred(e);',
  33479. 'end;',
  33480. 'begin',
  33481. '{$R+}',
  33482. '']);
  33483. ConvertProgram;
  33484. CheckSource('TestRangeChecks_AssignEnum',
  33485. LinesToStr([ // statements
  33486. 'this.TEnum = {',
  33487. ' "0": "red",',
  33488. ' red: 0,',
  33489. ' "1": "green",',
  33490. ' green: 1',
  33491. '};',
  33492. 'this.e = this.TEnum.red;',
  33493. 'this.DoIt = function (p) {',
  33494. ' rtl.rc(p, 0, 1);',
  33495. ' $mod.e = rtl.rc(p, 0, 1);',
  33496. ' p = 0;',
  33497. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33498. '};',
  33499. 'this.DoSome = function () {',
  33500. ' $mod.DoIt($mod.e);',
  33501. ' $mod.e = 1;',
  33502. ' $mod.e = $mod.e - 1;',
  33503. '};',
  33504. '']),
  33505. LinesToStr([ // $mod.$main
  33506. '']));
  33507. end;
  33508. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33509. begin
  33510. StartProgram(false);
  33511. Add([
  33512. '{$R+}',
  33513. 'type',
  33514. ' TEnum = (red,green);',
  33515. ' TEnumRg = red..green;',
  33516. 'var',
  33517. ' e: TEnumRg = red;',
  33518. 'procedure DoIt(p: TEnumRg);',
  33519. 'begin',
  33520. ' e:=p;',
  33521. ' p:=TEnumRg(0);',
  33522. ' p:=succ(e);',
  33523. 'end;',
  33524. '{$R-}',
  33525. 'procedure DoSome;',
  33526. 'begin',
  33527. ' DoIt(e);',
  33528. ' e:=TEnum(1);',
  33529. ' e:=pred(e);',
  33530. 'end;',
  33531. 'begin',
  33532. '{$R+}',
  33533. '']);
  33534. ConvertProgram;
  33535. CheckSource('TestRangeChecks_AssignEnumRange',
  33536. LinesToStr([ // statements
  33537. 'this.TEnum = {',
  33538. ' "0": "red",',
  33539. ' red: 0,',
  33540. ' "1": "green",',
  33541. ' green: 1',
  33542. '};',
  33543. 'this.e = this.TEnum.red;',
  33544. 'this.DoIt = function (p) {',
  33545. ' rtl.rc(p, 0, 1);',
  33546. ' $mod.e = rtl.rc(p, 0, 1);',
  33547. ' p = 0;',
  33548. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33549. '};',
  33550. 'this.DoSome = function () {',
  33551. ' $mod.DoIt($mod.e);',
  33552. ' $mod.e = 1;',
  33553. ' $mod.e = $mod.e - 1;',
  33554. '};',
  33555. '']),
  33556. LinesToStr([ // $mod.$main
  33557. '']));
  33558. end;
  33559. procedure TTestModule.TestRangeChecks_AssignChar;
  33560. begin
  33561. StartProgram(false);
  33562. Add([
  33563. '{$R+}',
  33564. 'type',
  33565. ' TLetter = char;',
  33566. 'var',
  33567. ' b: TLetter = ''2'';',
  33568. ' w: TLetter = ''3'';',
  33569. 'procedure DoIt(p: TLetter);',
  33570. 'begin',
  33571. ' b:=w;',
  33572. ' b:=''1'';',
  33573. 'end;',
  33574. '{$R-}',
  33575. 'procedure DoSome;',
  33576. 'begin',
  33577. ' DoIt(w);',
  33578. ' b:=w;',
  33579. ' b:=''2'';',
  33580. 'end;',
  33581. 'begin',
  33582. '{$R+}',
  33583. '']);
  33584. ConvertProgram;
  33585. CheckSource('TestRangeChecks_AssignChar',
  33586. LinesToStr([ // statements
  33587. 'this.b = "2";',
  33588. 'this.w = "3";',
  33589. 'this.DoIt = function (p) {',
  33590. ' rtl.rcc(p, 0, 65535);',
  33591. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33592. ' $mod.b = "1";',
  33593. '};',
  33594. 'this.DoSome = function () {',
  33595. ' $mod.DoIt($mod.w);',
  33596. ' $mod.b = $mod.w;',
  33597. ' $mod.b = "2";',
  33598. '};',
  33599. '']),
  33600. LinesToStr([ // $mod.$main
  33601. '']));
  33602. end;
  33603. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33604. begin
  33605. StartProgram(false);
  33606. Add([
  33607. '{$R+}',
  33608. 'type TDigit = ''0''..''9'';',
  33609. 'var',
  33610. ' b: TDigit = ''2'';',
  33611. ' w: TDigit = ''3'';',
  33612. 'procedure DoIt(p: TDigit);',
  33613. 'begin',
  33614. ' b:=w;',
  33615. ' b:=''1'';',
  33616. 'end;',
  33617. '{$R-}',
  33618. 'procedure DoSome;',
  33619. 'begin',
  33620. ' DoIt(w);',
  33621. ' b:=w;',
  33622. ' b:=''2'';',
  33623. 'end;',
  33624. 'begin',
  33625. '{$R+}',
  33626. '']);
  33627. ConvertProgram;
  33628. CheckSource('TestRangeChecks_AssignCharRange',
  33629. LinesToStr([ // statements
  33630. 'this.b = "2";',
  33631. 'this.w = "3";',
  33632. 'this.DoIt = function (p) {',
  33633. ' rtl.rcc(p, 48, 57);',
  33634. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33635. ' $mod.b = "1";',
  33636. '};',
  33637. 'this.DoSome = function () {',
  33638. ' $mod.DoIt($mod.w);',
  33639. ' $mod.b = $mod.w;',
  33640. ' $mod.b = "2";',
  33641. '};',
  33642. '']),
  33643. LinesToStr([ // $mod.$main
  33644. '']));
  33645. end;
  33646. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33647. begin
  33648. StartProgram(false);
  33649. Add([
  33650. '{$R+}',
  33651. 'type',
  33652. ' Ten = 1..10;',
  33653. ' TArr = array of Ten;',
  33654. ' TArrArr = array of TArr;',
  33655. ' TArrByte = array[byte] of Ten;',
  33656. ' TArrChar = array[''0''..''9''] of Ten;',
  33657. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33658. ' TObject = class',
  33659. ' A: TArr;',
  33660. ' end;',
  33661. 'procedure DoIt;',
  33662. 'var',
  33663. ' Arr: TArr;',
  33664. ' ArrArr: TArrArr;',
  33665. ' ArrByte: TArrByte;',
  33666. ' ArrChar: TArrChar;',
  33667. ' ArrByteChar: TArrByteChar;',
  33668. ' i: Ten;',
  33669. ' c: char;',
  33670. ' o: tobject;',
  33671. 'begin',
  33672. ' i:=Arr[1];',
  33673. ' i:=ArrByteChar[1,''2''];',
  33674. ' Arr[1]:=Arr[1];',
  33675. ' Arr[i]:=Arr[i];',
  33676. ' ArrByte[3]:=ArrByte[3];',
  33677. ' ArrByte[i]:=ArrByte[i];',
  33678. ' ArrChar[''5'']:=ArrChar[''5''];',
  33679. ' ArrChar[c]:=ArrChar[c];',
  33680. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33681. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33682. ' o.a[i]:=o.a[i];',
  33683. 'end;',
  33684. 'begin',
  33685. '']);
  33686. ConvertProgram;
  33687. CheckSource('TestRangeChecks_ArrayIndex',
  33688. LinesToStr([ // statements
  33689. 'this.TArrByteChar$clone = function (a) {',
  33690. ' var b = [];',
  33691. ' b.length = 256;',
  33692. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33693. ' return b;',
  33694. '};',
  33695. 'rtl.createClass(this, "TObject", null, function () {',
  33696. ' this.$init = function () {',
  33697. ' this.A = [];',
  33698. ' };',
  33699. ' this.$final = function () {',
  33700. ' this.A = undefined;',
  33701. ' };',
  33702. '});',
  33703. 'this.DoIt = function () {',
  33704. ' var Arr = [];',
  33705. ' var ArrArr = [];',
  33706. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33707. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33708. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33709. ' var i = 0;',
  33710. ' var c = "";',
  33711. ' var o = null;',
  33712. ' i = rtl.rc(Arr[1], 1, 10);',
  33713. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33714. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33715. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33716. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33717. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33718. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33719. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33720. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33721. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33722. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33723. '};',
  33724. '']),
  33725. LinesToStr([ // $mod.$main
  33726. '']));
  33727. end;
  33728. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33729. begin
  33730. StartProgram(false);
  33731. Add([
  33732. '{$R+}',
  33733. 'type',
  33734. ' Ten = 1..10;',
  33735. ' TRec = record x: Ten end;',
  33736. ' TArr = array of TRec;',
  33737. ' TArrArr = array of TArr;',
  33738. ' TObject = class',
  33739. ' A: TArr;',
  33740. ' end;',
  33741. 'procedure DoIt;',
  33742. 'var',
  33743. ' Arr: TArr;',
  33744. ' ArrArr: TArrArr;',
  33745. ' i: Ten;',
  33746. ' o: tobject;',
  33747. 'begin',
  33748. ' Arr[1]:=Arr[1];',
  33749. ' Arr[i]:=Arr[i+1];',
  33750. ' o.a[i]:=o.a[i+2];',
  33751. 'end;',
  33752. 'begin',
  33753. '']);
  33754. ConvertProgram;
  33755. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33756. LinesToStr([ // statements
  33757. 'rtl.recNewT(this, "TRec", function () {',
  33758. ' this.x = 0;',
  33759. ' this.$eq = function (b) {',
  33760. ' return this.x === b.x;',
  33761. ' };',
  33762. ' this.$assign = function (s) {',
  33763. ' this.x = s.x;',
  33764. ' return this;',
  33765. ' };',
  33766. '});',
  33767. 'rtl.createClass(this, "TObject", null, function () {',
  33768. ' this.$init = function () {',
  33769. ' this.A = [];',
  33770. ' };',
  33771. ' this.$final = function () {',
  33772. ' this.A = undefined;',
  33773. ' };',
  33774. '});',
  33775. 'this.DoIt = function () {',
  33776. ' var Arr = [];',
  33777. ' var ArrArr = [];',
  33778. ' var i = 0;',
  33779. ' var o = null;',
  33780. ' Arr[1].$assign(Arr[1]);',
  33781. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33782. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33783. '};',
  33784. '']),
  33785. LinesToStr([ // $mod.$main
  33786. '']));
  33787. end;
  33788. procedure TTestModule.TestRangeChecks_StringIndex;
  33789. begin
  33790. StartProgram(false);
  33791. Add([
  33792. 'type',
  33793. ' TObject = class',
  33794. ' S: string;',
  33795. ' end;',
  33796. '{$R+}',
  33797. 'procedure DoIt(var h: string);',
  33798. 'var',
  33799. ' s: string;',
  33800. ' i: longint;',
  33801. ' c: char;',
  33802. ' o: tobject;',
  33803. 'begin',
  33804. ' c:=s[1];',
  33805. ' s[i]:=s[i];',
  33806. ' h[i]:=h[i];',
  33807. ' c:=o.s[i];',
  33808. ' o.s[i]:=c;',
  33809. 'end;',
  33810. 'begin',
  33811. '']);
  33812. ConvertProgram;
  33813. CheckSource('TestRangeChecks_StringIndex',
  33814. LinesToStr([ // statements
  33815. 'rtl.createClass(this, "TObject", null, function () {',
  33816. ' this.$init = function () {',
  33817. ' this.S = "";',
  33818. ' };',
  33819. ' this.$final = function () {',
  33820. ' };',
  33821. '});',
  33822. 'this.DoIt = function (h) {',
  33823. ' var s = "";',
  33824. ' var i = 0;',
  33825. ' var c = "";',
  33826. ' var o = null;',
  33827. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33828. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33829. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33830. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33831. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33832. '};',
  33833. '']),
  33834. LinesToStr([ // $mod.$main
  33835. '']));
  33836. end;
  33837. procedure TTestModule.TestRangeChecks_TypecastInt;
  33838. begin
  33839. StartProgram(false);
  33840. Add([
  33841. '{$R+}',
  33842. 'var',
  33843. ' i: nativeint;',
  33844. ' b: byte;',
  33845. ' sh: shortint;',
  33846. ' w: word;',
  33847. ' sm: smallint;',
  33848. ' lw: longword;',
  33849. ' li: longint;',
  33850. 'begin',
  33851. ' b:=12+byte(i);',
  33852. ' sh:=12+shortint(i);',
  33853. ' w:=12+word(i);',
  33854. ' sm:=12+smallint(i);',
  33855. ' lw:=12+longword(i);',
  33856. ' li:=12+longint(i);',
  33857. '']);
  33858. ConvertProgram;
  33859. CheckSource('TestRangeChecks_TypecastInt',
  33860. LinesToStr([
  33861. 'this.i = 0;',
  33862. 'this.b = 0;',
  33863. 'this.sh = 0;',
  33864. 'this.w = 0;',
  33865. 'this.sm = 0;',
  33866. 'this.lw = 0;',
  33867. 'this.li = 0;',
  33868. '']),
  33869. LinesToStr([
  33870. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33871. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33872. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33873. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33874. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33875. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33876. '']));
  33877. end;
  33878. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33879. begin
  33880. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33881. StartProgram(false);
  33882. Add([
  33883. '{$modeswitch typehelpers}',
  33884. '{$R+}',
  33885. 'type',
  33886. ' TObject = class',
  33887. ' FSize: byte;',
  33888. ' property Size: byte read FSize;',
  33889. ' end;',
  33890. ' THelper = type helper for byte',
  33891. ' procedure SetIt(w: word);',
  33892. ' end;',
  33893. 'procedure THelper.SetIt(w: word);',
  33894. 'begin',
  33895. ' Self:=w;',
  33896. 'end;',
  33897. 'function GetIt: byte;',
  33898. 'begin',
  33899. ' Result.SetIt(2);',
  33900. 'end;',
  33901. 'var',
  33902. ' b: byte = 3;',
  33903. ' o: TObject;',
  33904. 'begin',
  33905. ' b.SetIt(14);',
  33906. ' with b do SetIt(15);',
  33907. ' o.Size.SetIt(16);',
  33908. '']);
  33909. ConvertProgram;
  33910. CheckSource('TestRangeChecks_AssignInt',
  33911. LinesToStr([ // statements
  33912. 'rtl.createClass(this, "TObject", null, function () {',
  33913. ' this.$init = function () {',
  33914. ' this.FSize = 0;',
  33915. ' };',
  33916. ' this.$final = function () {',
  33917. ' };',
  33918. '});',
  33919. 'rtl.createHelper(this, "THelper", null, function () {',
  33920. ' this.SetIt = function (w) {',
  33921. ' rtl.rc(w, 0, 65535);',
  33922. ' this.set(w);',
  33923. ' };',
  33924. '});',
  33925. 'this.GetIt = function () {',
  33926. ' var Result = 0;',
  33927. ' $mod.THelper.SetIt.call({',
  33928. ' get: function () {',
  33929. ' return Result;',
  33930. ' },',
  33931. ' set: function (v) {',
  33932. ' rtl.rc(v, 0, 255);',
  33933. ' Result = v;',
  33934. ' }',
  33935. ' }, 2);',
  33936. ' return Result;',
  33937. '};',
  33938. 'this.b = 3;',
  33939. 'this.o = null;',
  33940. '']),
  33941. LinesToStr([ // $mod.$main
  33942. '$mod.THelper.SetIt.call({',
  33943. ' p: $mod,',
  33944. ' get: function () {',
  33945. ' return this.p.b;',
  33946. ' },',
  33947. ' set: function (v) {',
  33948. ' rtl.rc(v, 0, 255);',
  33949. ' this.p.b = v;',
  33950. ' }',
  33951. '}, 14);',
  33952. 'var $with = $mod.b;',
  33953. '$mod.THelper.SetIt.call({',
  33954. ' get: function () {',
  33955. ' return $with;',
  33956. ' },',
  33957. ' set: function (v) {',
  33958. ' rtl.rc(v, 0, 255);',
  33959. ' $with = v;',
  33960. ' }',
  33961. '}, 15);',
  33962. '$mod.THelper.SetIt.call({',
  33963. ' p: $mod.o,',
  33964. ' get: function () {',
  33965. ' return this.p.FSize;',
  33966. ' },',
  33967. ' set: function (v) {',
  33968. ' rtl.rc(v, 0, 255);',
  33969. ' this.p.FSize = v;',
  33970. ' }',
  33971. '}, 16);',
  33972. '']));
  33973. end;
  33974. procedure TTestModule.TestRangeChecks_AssignCurrency;
  33975. begin
  33976. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33977. StartProgram(false);
  33978. Add([
  33979. '{$R+}',
  33980. 'var',
  33981. ' c: currency = 2.34;',
  33982. ' i: double;',
  33983. 'procedure DoIt(p: currency);',
  33984. 'begin',
  33985. ' c:=i;',
  33986. ' c+=i;',
  33987. ' c:=1;',
  33988. 'end;',
  33989. '{$R-}',
  33990. 'procedure DoSome;',
  33991. 'begin',
  33992. ' DoIt(i);',
  33993. ' c:=i;',
  33994. ' c:=2;',
  33995. 'end;',
  33996. 'begin',
  33997. '{$R+}',
  33998. '']);
  33999. ConvertProgram;
  34000. CheckSource('TestRangeChecks_AssignCurrency',
  34001. LinesToStr([ // statements
  34002. 'this.c = 2.34;',
  34003. 'this.i = 0.0;',
  34004. 'this.DoIt = function (p) {',
  34005. ' rtl.rc(p, -922337203685477, 922337203685477);',
  34006. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34007. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34008. ' $mod.c = 10000;',
  34009. '};',
  34010. 'this.DoSome = function () {',
  34011. ' $mod.DoIt($mod.i * 10000);',
  34012. ' $mod.c = rtl.trunc($mod.i * 10000);',
  34013. ' $mod.c = 20000;',
  34014. '};',
  34015. '']),
  34016. LinesToStr([ // $mod.$main
  34017. '']));
  34018. end;
  34019. procedure TTestModule.TestAsync_Proc;
  34020. begin
  34021. StartProgram(false);
  34022. Add([
  34023. 'procedure Fly(w: word = 1); async; forward;',
  34024. 'procedure Run(w: word = 2); async;',
  34025. 'begin',
  34026. ' Fly(w);',
  34027. ' Fly;',
  34028. ' await(Fly(w));',
  34029. ' await(Fly);',
  34030. 'end;',
  34031. 'procedure Fly(w: word); ',
  34032. 'begin',
  34033. 'end;',
  34034. 'begin',
  34035. ' Run;',
  34036. ' Run(3);',
  34037. '']);
  34038. CheckResolverUnexpectedHints();
  34039. ConvertProgram;
  34040. CheckSource('TestAsync_Proc',
  34041. LinesToStr([ // statements
  34042. 'this.Run = async function (w) {',
  34043. ' $mod.Fly(w);',
  34044. ' $mod.Fly(1);',
  34045. ' await $mod.Fly(w);',
  34046. ' await $mod.Fly(1);',
  34047. '};',
  34048. 'this.Fly = async function (w) {',
  34049. '};',
  34050. '']),
  34051. LinesToStr([
  34052. '$mod.Run(2);',
  34053. '$mod.Run(3);',
  34054. '']));
  34055. end;
  34056. procedure TTestModule.TestAsync_CallResultIsPromise;
  34057. begin
  34058. StartProgram(false);
  34059. Add([
  34060. '{$modeswitch externalclass}',
  34061. 'type',
  34062. ' TObject = class',
  34063. ' end;',
  34064. ' TJSPromise = class external name ''Promise''',
  34065. ' end;',
  34066. ' TBird = class',
  34067. ' function Fly: word; async; ',
  34068. ' end;',
  34069. 'function TBird.Fly: word; async; ',
  34070. 'begin',
  34071. ' Result:=3;',
  34072. ' Fly:=4+Result;',
  34073. ' if Result=5 then ;',
  34074. ' exit(6);',
  34075. 'end;',
  34076. 'function Run: word; async;',
  34077. 'begin',
  34078. ' Result:=11+Result;',
  34079. ' inc(Result);',
  34080. 'end;',
  34081. 'var',
  34082. ' p: TJSPromise;',
  34083. ' o: TBird;',
  34084. 'begin',
  34085. ' p:=Run;',
  34086. ' p:=Run();',
  34087. ' if Run=p then ;',
  34088. ' if p=Run then ;',
  34089. ' if Run()=p then ;',
  34090. ' if p=Run() then ;',
  34091. ' p:=o.Fly;',
  34092. ' p:=o.Fly();',
  34093. ' if o.Fly=p then ;',
  34094. ' if o.Fly()=p then ;',
  34095. ' with o do begin',
  34096. ' p:=Fly;',
  34097. ' p:=Fly();',
  34098. ' if Fly=p then ;',
  34099. ' if Fly()=p then ;',
  34100. ' end;',
  34101. '']);
  34102. CheckResolverUnexpectedHints();
  34103. ConvertProgram;
  34104. CheckSource('TestAsync_CallResultIsPromise',
  34105. LinesToStr([ // statements
  34106. 'rtl.createClass(this, "TObject", null, function () {',
  34107. ' this.$init = function () {',
  34108. ' };',
  34109. ' this.$final = function () {',
  34110. ' };',
  34111. '});',
  34112. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34113. ' this.Fly = async function () {',
  34114. ' var Result = 0;',
  34115. ' Result = 3;',
  34116. ' Result = 4 + Result;',
  34117. ' if (Result === 5) ;',
  34118. ' return 6;',
  34119. ' return Result;',
  34120. ' };',
  34121. '});',
  34122. 'this.Run = async function () {',
  34123. ' var Result = 0;',
  34124. ' Result = 11 + Result;',
  34125. ' Result += 1;',
  34126. ' return Result;',
  34127. '};',
  34128. 'this.p = null;',
  34129. 'this.o = null;',
  34130. '']),
  34131. LinesToStr([
  34132. '$mod.p = $mod.Run();',
  34133. '$mod.p = $mod.Run();',
  34134. 'if ($mod.Run() === $mod.p) ;',
  34135. 'if ($mod.p === $mod.Run()) ;',
  34136. 'if ($mod.Run() === $mod.p) ;',
  34137. 'if ($mod.p === $mod.Run()) ;',
  34138. '$mod.p = $mod.o.Fly();',
  34139. '$mod.p = $mod.o.Fly();',
  34140. 'if ($mod.o.Fly() === $mod.p) ;',
  34141. 'if ($mod.o.Fly() === $mod.p) ;',
  34142. 'var $with = $mod.o;',
  34143. '$mod.p = $with.Fly();',
  34144. '$mod.p = $with.Fly();',
  34145. 'if ($with.Fly() === $mod.p) ;',
  34146. 'if ($with.Fly() === $mod.p) ;',
  34147. '']));
  34148. end;
  34149. procedure TTestModule.TestAsync_ConstructorFail;
  34150. begin
  34151. StartProgram(false);
  34152. Add([
  34153. 'type',
  34154. ' TObject = class',
  34155. ' end;',
  34156. ' TBird = class',
  34157. ' constructor Create; async;',
  34158. ' end;',
  34159. 'constructor TBird.Create; async;',
  34160. 'begin',
  34161. 'end;',
  34162. 'begin',
  34163. '']);
  34164. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34165. ConvertProgram;
  34166. end;
  34167. procedure TTestModule.TestAsync_PropertyGetterFail;
  34168. begin
  34169. StartProgram(false);
  34170. Add([
  34171. 'type',
  34172. ' TObject = class',
  34173. ' end;',
  34174. ' TBird = class',
  34175. ' function GetSize: word; async;',
  34176. ' property Size: word read GetSize;',
  34177. ' end;',
  34178. 'function TBird.GetSize: word; async;',
  34179. 'begin',
  34180. 'end;',
  34181. 'begin',
  34182. '']);
  34183. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34184. ConvertProgram;
  34185. end;
  34186. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34187. begin
  34188. StartProgram(false);
  34189. Add([
  34190. 'procedure Run; async;',
  34191. 'begin',
  34192. ' await(word,1);',
  34193. 'end;',
  34194. 'begin',
  34195. '']);
  34196. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34197. ConvertProgram;
  34198. end;
  34199. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34200. begin
  34201. StartProgram(false);
  34202. Add([
  34203. 'type',
  34204. ' TObject = class',
  34205. ' end;',
  34206. ' TBird = class',
  34207. ' end;',
  34208. 'function Fly: TObject; async;',
  34209. 'begin',
  34210. 'end;',
  34211. 'procedure Run; async;',
  34212. 'begin',
  34213. ' await(TBird,Fly);',
  34214. 'end;',
  34215. 'begin',
  34216. '']);
  34217. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34218. ConvertProgram;
  34219. end;
  34220. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34221. begin
  34222. StartProgram(false);
  34223. Add([
  34224. 'procedure Crawl(w: double); ',
  34225. 'begin',
  34226. 'end;',
  34227. 'procedure Run(w: double);',
  34228. 'begin',
  34229. ' await(Crawl(w));',
  34230. 'end;',
  34231. 'begin',
  34232. ' Run(1);']);
  34233. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34234. ConvertProgram;
  34235. end;
  34236. procedure TTestModule.TestAWait_IntegerFail;
  34237. begin
  34238. StartProgram(false);
  34239. Add([
  34240. 'function Run: word;',
  34241. 'begin',
  34242. 'end;',
  34243. 'procedure Fly(w: word); async;',
  34244. 'begin',
  34245. ' await(Run());',
  34246. 'end;',
  34247. 'begin',
  34248. ' Fly(1);']);
  34249. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34250. ConvertProgram;
  34251. end;
  34252. procedure TTestModule.TestAWait_ExternalClassPromise;
  34253. begin
  34254. StartProgram(false);
  34255. Add([
  34256. '{$modeswitch externalclass}',
  34257. 'type',
  34258. ' TJSPromise = class external name ''Promise''',
  34259. ' end;',
  34260. ' TJSThenable = class external name ''Thenable''',
  34261. ' end;',
  34262. 'function Fly(w: word): TJSPromise;',
  34263. 'begin',
  34264. 'end;',
  34265. 'function Jump(w: word): word; async;',
  34266. 'begin',
  34267. 'end;',
  34268. 'function Eat(w: word): TJSPromise; async;',
  34269. 'begin',
  34270. 'end;',
  34271. 'function Run(d: double): word; async;',
  34272. 'var',
  34273. ' p: TJSPromise;',
  34274. 'begin',
  34275. ' Result:=await(word,p);', // promise needs type
  34276. ' Result:=await(word,Fly(3));', // promise needs type
  34277. ' Result:=await(Jump(4));', // async non promise must omit the type
  34278. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34279. ' Result:=await(word,Eat(6));', // promise needs type
  34280. 'end;',
  34281. 'begin',
  34282. '']);
  34283. ConvertProgram;
  34284. CheckSource('TestAWait_ExternalClassPromise',
  34285. LinesToStr([ // statements
  34286. 'this.Fly = function (w) {',
  34287. ' var Result = null;',
  34288. ' return Result;',
  34289. '};',
  34290. 'this.Jump = async function (w) {',
  34291. ' var Result = 0;',
  34292. ' return Result;',
  34293. '};',
  34294. 'this.Eat = async function (w) {',
  34295. ' var Result = null;',
  34296. ' return Result;',
  34297. '};',
  34298. 'this.Run = async function (d) {',
  34299. ' var Result = 0;',
  34300. ' var p = null;',
  34301. ' Result = await p;',
  34302. ' Result = await $mod.Fly(3);',
  34303. ' Result = await $mod.Jump(4);',
  34304. ' Result = await $mod.Jump(5);',
  34305. ' Result = await $mod.Eat(6);',
  34306. ' return Result;',
  34307. '};',
  34308. '']),
  34309. LinesToStr([
  34310. ]));
  34311. CheckResolverUnexpectedHints();
  34312. end;
  34313. procedure TTestModule.TestAWait_JSValue;
  34314. begin
  34315. StartProgram(false);
  34316. Add([
  34317. '{$modeswitch externalclass}',
  34318. 'type',
  34319. ' TJSPromise = class external name ''Promise''',
  34320. ' end;',
  34321. 'function Fly(w: word): jsvalue; async;',
  34322. 'begin',
  34323. 'end;',
  34324. 'function Run(d: jsvalue; var e): word; async;',
  34325. 'begin',
  34326. ' Result:=await(word,d);', // promise needs type
  34327. ' d:=await(Fly(4));', // async non promise must omit the type
  34328. ' Result:=await(word,e);', // promise needs type
  34329. 'end;',
  34330. 'begin',
  34331. '']);
  34332. ConvertProgram;
  34333. CheckSource('TestAWait_JSValue',
  34334. LinesToStr([ // statements
  34335. 'this.Fly = async function (w) {',
  34336. ' var Result = undefined;',
  34337. ' return Result;',
  34338. '};',
  34339. 'this.Run = async function (d, e) {',
  34340. ' var Result = 0;',
  34341. ' Result = await d;',
  34342. ' d = await $mod.Fly(4);',
  34343. ' Result = await e.get();',
  34344. ' return Result;',
  34345. '};',
  34346. '']),
  34347. LinesToStr([
  34348. ]));
  34349. CheckResolverUnexpectedHints();
  34350. end;
  34351. procedure TTestModule.TestAWait_Result;
  34352. begin
  34353. StartProgram(false);
  34354. Add([
  34355. '{$modeswitch externalclass}',
  34356. 'type',
  34357. ' TJSPromise = class external name ''Promise''',
  34358. ' end;',
  34359. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34360. 'begin',
  34361. 'end;',
  34362. 'function Run(d: double = 1.6): word; async;',
  34363. 'begin',
  34364. ' Result:=await(word,Crawl);',
  34365. ' Result:=await(word,Crawl(4.5));',
  34366. ' Result:=await(Run);',
  34367. ' Result:=await(Run(6.7));',
  34368. 'end;',
  34369. 'begin',
  34370. ' Run(1);']);
  34371. ConvertProgram;
  34372. CheckSource('TestAWait_Result',
  34373. LinesToStr([ // statements
  34374. 'this.Crawl = function (d) {',
  34375. ' var Result = null;',
  34376. ' return Result;',
  34377. '};',
  34378. 'this.Run = async function (d) {',
  34379. ' var Result = 0;',
  34380. ' Result = await $mod.Crawl(1.3);',
  34381. ' Result = await $mod.Crawl(4.5);',
  34382. ' Result = await $mod.Run(1.6);',
  34383. ' Result = await $mod.Run(6.7);',
  34384. ' return Result;',
  34385. '};',
  34386. '']),
  34387. LinesToStr([
  34388. '$mod.Run(1);'
  34389. ]));
  34390. CheckResolverUnexpectedHints();
  34391. end;
  34392. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34393. begin
  34394. StartProgram(false);
  34395. Add([
  34396. '{$mode objfpc}',
  34397. '{$modeswitch externalclass}',
  34398. 'type',
  34399. ' TJSPromise = class external name ''Promise''',
  34400. ' end;',
  34401. 'function Run: TJSPromise; async;',
  34402. 'begin',
  34403. 'end;',
  34404. 'procedure Fly(w: word); async;',
  34405. 'begin',
  34406. ' await(Run());',
  34407. 'end;',
  34408. 'begin',
  34409. ' Fly(1);']);
  34410. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34411. nWrongNumberOfParametersForCallTo);
  34412. ConvertProgram;
  34413. end;
  34414. procedure TTestModule.TestAsync_AnonymousProc;
  34415. begin
  34416. StartProgram(false);
  34417. Add([
  34418. '{$mode objfpc}',
  34419. '{$modeswitch externalclass}',
  34420. 'type',
  34421. ' TJSPromise = class external name ''Promise''',
  34422. ' end;',
  34423. 'type',
  34424. ' TFunc = reference to function(x: double): word; async;',
  34425. 'function Crawl(d: double = 1.3): word; async;',
  34426. 'begin',
  34427. 'end;',
  34428. 'var Func: TFunc;',
  34429. 'begin',
  34430. ' Func:=function(c:double):word async begin',
  34431. ' Result:=await(Crawl(c));',
  34432. ' end;',
  34433. ' Func:=function(c:double):word async assembler asm',
  34434. ' end;',
  34435. '']);
  34436. ConvertProgram;
  34437. CheckSource('TestAsync_AnonymousProc',
  34438. LinesToStr([ // statements
  34439. 'this.Crawl = async function (d) {',
  34440. ' var Result = 0;',
  34441. ' return Result;',
  34442. '};',
  34443. 'this.Func = null;',
  34444. '']),
  34445. LinesToStr([
  34446. '$mod.Func = async function (c) {',
  34447. ' var Result = 0;',
  34448. ' Result = await $mod.Crawl(c);',
  34449. ' return Result;',
  34450. '};',
  34451. '$mod.Func = async function (c) {',
  34452. '};',
  34453. '']));
  34454. CheckResolverUnexpectedHints();
  34455. end;
  34456. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34457. begin
  34458. StartProgram(false);
  34459. Add([
  34460. '{$mode objfpc}',
  34461. '{$modeswitch externalclass}',
  34462. 'type',
  34463. ' TJSPromise = class external name ''Promise''',
  34464. ' end;',
  34465. ' TObject = class',
  34466. ' public',
  34467. ' procedure Fly(Prom: TJSPromise);',
  34468. ' end;',
  34469. ' TFunc = reference to procedure(Bird: TObject);',
  34470. 'procedure TObject.Fly(Prom: TJSPromise);',
  34471. 'begin',
  34472. 'end;',
  34473. 'function Crawl: jsvalue; async;',
  34474. 'begin',
  34475. 'end;',
  34476. 'procedure Add(Func: TFunc);',
  34477. 'begin',
  34478. 'end;',
  34479. 'begin',
  34480. ' Add(procedure(Bird: TObject)',
  34481. ' begin',
  34482. ' Bird.Fly(Crawl());',
  34483. ' end);',
  34484. '']);
  34485. ConvertProgram;
  34486. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34487. LinesToStr([ // statements
  34488. 'rtl.createClass(this, "TObject", null, function () {',
  34489. ' this.$init = function () {',
  34490. ' };',
  34491. ' this.$final = function () {',
  34492. ' };',
  34493. ' this.Fly = function (Prom) {',
  34494. ' };',
  34495. '});',
  34496. 'this.Crawl = async function () {',
  34497. ' var Result = undefined;',
  34498. ' return Result;',
  34499. '};',
  34500. 'this.Add = function (Func) {',
  34501. '};',
  34502. '']),
  34503. LinesToStr([
  34504. '$mod.Add(function (Bird) {',
  34505. ' Bird.Fly($mod.Crawl());',
  34506. '});',
  34507. '']));
  34508. end;
  34509. procedure TTestModule.TestAsync_ProcType;
  34510. begin
  34511. StartProgram(false);
  34512. Add([
  34513. '{$mode objfpc}',
  34514. 'type',
  34515. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34516. ' TFunc = function(x: double = 1.1): word; async;',
  34517. ' TProc = procedure(x: longint = 7); async;',
  34518. 'function Crawl(d: double): word; async;',
  34519. 'begin',
  34520. 'end;',
  34521. 'procedure Run(e:longint); async;',
  34522. 'begin',
  34523. 'end;',
  34524. 'procedure Fly(p: TProc); async;',
  34525. 'begin',
  34526. ' await(p);',
  34527. ' await(p());',
  34528. 'end;',
  34529. 'var',
  34530. ' RefFunc: TRefFunc;',
  34531. ' Func: TFunc;',
  34532. ' Proc, ProcB: TProc;',
  34533. 'begin',
  34534. ' Func:=@Crawl;',
  34535. ' RefFunc:=@Crawl;',
  34536. ' RefFunc:=function(c:double):word async begin',
  34537. ' Result:=await(RefFunc);',
  34538. ' Result:=await(RefFunc());',
  34539. ' Result:=await(Func);',
  34540. ' Result:=await(Func());',
  34541. ' await(Proc);',
  34542. ' await(Proc());',
  34543. ' await(Proc(13));',
  34544. ' end;',
  34545. ' Proc:=@Run;',
  34546. ' if Proc=ProcB then ;',
  34547. ' ']);
  34548. ConvertProgram;
  34549. CheckResolverUnexpectedHints();
  34550. CheckSource('TestAsync_ProcType',
  34551. LinesToStr([ // statements
  34552. 'this.Crawl = async function (d) {',
  34553. ' var Result = 0;',
  34554. ' return Result;',
  34555. '};',
  34556. 'this.Run = async function (e) {',
  34557. '};',
  34558. 'this.Fly = async function (p) {',
  34559. ' await p(7);',
  34560. ' await p(7);',
  34561. '};',
  34562. 'this.RefFunc = null;',
  34563. 'this.Func = null;',
  34564. 'this.Proc = null;',
  34565. 'this.ProcB = null;',
  34566. '']),
  34567. LinesToStr([
  34568. '$mod.Func = $mod.Crawl;',
  34569. '$mod.RefFunc = $mod.Crawl;',
  34570. '$mod.RefFunc = async function (c) {',
  34571. ' var Result = 0;',
  34572. ' Result = await $mod.RefFunc(1.3);',
  34573. ' Result = await $mod.RefFunc(1.3);',
  34574. ' Result = await $mod.Func(1.1);',
  34575. ' Result = await $mod.Func(1.1);',
  34576. ' await $mod.Proc(7);',
  34577. ' await $mod.Proc(7);',
  34578. ' await $mod.Proc(13);',
  34579. ' return Result;',
  34580. '};',
  34581. '$mod.Proc = $mod.Run;',
  34582. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34583. '']));
  34584. end;
  34585. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34586. begin
  34587. StartProgram(false);
  34588. Add([
  34589. '{$mode objfpc}',
  34590. 'type',
  34591. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34592. 'function Crawl(d: double): word; async;',
  34593. 'begin',
  34594. 'end;',
  34595. 'var',
  34596. ' RefFunc: TRefFunc;',
  34597. 'begin',
  34598. ' RefFunc:=@Crawl;',
  34599. ' ']);
  34600. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34601. ConvertProgram;
  34602. end;
  34603. procedure TTestModule.TestAsync_Inherited;
  34604. begin
  34605. StartProgram(false);
  34606. Add([
  34607. '{$mode objfpc}',
  34608. '{$modeswitch externalclass}',
  34609. 'type',
  34610. ' TJSPromise = class external name ''Promise''',
  34611. ' end;',
  34612. ' TObject = class',
  34613. ' function Run(w: word = 3): word; async; virtual;',
  34614. ' end;',
  34615. ' TBird = class',
  34616. ' function Run(w: word = 3): word; async; override;',
  34617. ' end;',
  34618. 'function TObject.Run(w: word = 3): word; async;',
  34619. 'begin',
  34620. 'end;',
  34621. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34622. 'var p: TJSPromise;',
  34623. 'begin',
  34624. ' p:=inherited;',
  34625. ' p:=inherited Run;',
  34626. ' p:=inherited Run();',
  34627. ' p:=inherited Run(4);',
  34628. ' exit(p);',
  34629. ' exit(inherited);',
  34630. ' exit(inherited Run);',
  34631. ' exit(inherited Run(5));',
  34632. ' exit(6);',
  34633. 'end;',
  34634. 'begin',
  34635. ' ']);
  34636. ConvertProgram;
  34637. CheckSource('TestAsync_Inherited',
  34638. LinesToStr([ // statements
  34639. 'rtl.createClass(this, "TObject", null, function () {',
  34640. ' this.$init = function () {',
  34641. ' };',
  34642. ' this.$final = function () {',
  34643. ' };',
  34644. ' this.Run = async function (w) {',
  34645. ' var Result = 0;',
  34646. ' return Result;',
  34647. ' };',
  34648. '});',
  34649. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34650. ' this.Run = async function (w) {',
  34651. ' var Result = 0;',
  34652. ' var p = null;',
  34653. ' p = $mod.TObject.Run.apply(this, arguments);',
  34654. ' p = $mod.TObject.Run.call(this, 3);',
  34655. ' p = $mod.TObject.Run.call(this, 3);',
  34656. ' p = $mod.TObject.Run.call(this, 4);',
  34657. ' return p;',
  34658. ' return $mod.TObject.Run.apply(this, arguments);',
  34659. ' return $mod.TObject.Run.call(this, 3);',
  34660. ' return $mod.TObject.Run.call(this, 5);',
  34661. ' return 6;',
  34662. ' return Result;',
  34663. ' };',
  34664. '});',
  34665. '']),
  34666. LinesToStr([
  34667. '']));
  34668. CheckResolverUnexpectedHints();
  34669. end;
  34670. procedure TTestModule.TestAsync_ClassInterface;
  34671. begin
  34672. StartProgram(false);
  34673. Add([
  34674. '{$mode objfpc}',
  34675. '{$modeswitch externalclass}',
  34676. 'type',
  34677. ' TJSPromise = class external name ''Promise''',
  34678. ' end;',
  34679. ' IUnknown = interface',
  34680. ' function _AddRef: longint;',
  34681. ' function _Release: longint;',
  34682. ' end;',
  34683. 'function Say(i: IUnknown): IUnknown; async;',
  34684. 'begin',
  34685. 'end;',
  34686. 'function Run: IUnknown; async;',
  34687. 'begin',
  34688. ' Result:=await(Run);',
  34689. ' Result:=await(Run());',
  34690. ' Result:=await(Run) as IUnknown;',
  34691. ' Result:=await(Say(nil));',
  34692. ' Result:=await(Say(await(Run())));',
  34693. ' Result:=await(Say(await(Run()) as IUnknown));',
  34694. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34695. 'end;',
  34696. 'procedure Fly;',
  34697. 'var p: TJSPromise;',
  34698. 'begin',
  34699. ' Run;',
  34700. ' Run();',
  34701. ' p:=Run;',
  34702. ' p:=Run();',
  34703. 'end;',
  34704. 'begin',
  34705. ' ']);
  34706. ConvertProgram;
  34707. CheckSource('TestAsync_ClassInterface',
  34708. LinesToStr([ // statements
  34709. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34710. 'this.Say = async function (i) {',
  34711. ' var Result = null;',
  34712. ' return Result;',
  34713. '};',
  34714. 'this.Run = async function () {',
  34715. ' var Result = null;',
  34716. ' var $ok = false;',
  34717. ' try {',
  34718. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34719. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34720. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34721. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34722. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34723. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34724. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34725. ' $ok = true;',
  34726. ' } finally {',
  34727. ' if (!$ok) rtl._Release(Result);',
  34728. ' };',
  34729. ' return Result;',
  34730. '};',
  34731. 'this.Fly = function () {',
  34732. ' var p = null;',
  34733. ' $mod.Run();',
  34734. ' $mod.Run();',
  34735. ' p = $mod.Run();',
  34736. ' p = $mod.Run();',
  34737. '};',
  34738. '']),
  34739. LinesToStr([
  34740. '']));
  34741. CheckResolverUnexpectedHints();
  34742. end;
  34743. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34744. begin
  34745. StartProgram(true,[supTInterfacedObject]);
  34746. Add([
  34747. '{$mode objfpc}',
  34748. '{$modeswitch externalclass}',
  34749. 'type',
  34750. ' TJSPromise = class external name ''Promise''',
  34751. ' end;',
  34752. ' IBird = interface',
  34753. ' procedure Run;',
  34754. ' end;',
  34755. ' TBird = class(TInterfacedObject,IBird)',
  34756. ' procedure Run; async;',
  34757. ' end;',
  34758. 'procedure TBird.Run;',
  34759. 'begin',
  34760. 'end;',
  34761. 'begin',
  34762. ' ']);
  34763. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34764. ConvertProgram;
  34765. end;
  34766. procedure TTestModule.TestAWait_ClassAs;
  34767. begin
  34768. StartProgram(false);
  34769. Add([
  34770. '{$mode objfpc}',
  34771. '{$modeswitch externalclass}',
  34772. 'type',
  34773. ' TJSPromise = class external name ''Promise''',
  34774. ' end;',
  34775. ' TObject = class',
  34776. ' function Run: TObject; async;',
  34777. ' end;',
  34778. ' TBird = class',
  34779. ' function Fly: TBird; async;',
  34780. ' end;',
  34781. 'function TObject.Run: TObject; async;',
  34782. 'begin',
  34783. 'end;',
  34784. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34785. 'var o: TObject;',
  34786. 'begin',
  34787. ' o:=await(TObject,Run);',
  34788. ' o:=await(TObject,Fly);',
  34789. ' o:=await(TBird,Fly);',
  34790. ' o:=await(TObject,inherited Run);',
  34791. ' o:=await(TObject,inherited Run) as TBird;',
  34792. 'end;',
  34793. 'begin',
  34794. ' ']);
  34795. ConvertProgram;
  34796. CheckSource('TestAWait_ClassAs',
  34797. LinesToStr([ // statements
  34798. 'rtl.createClass(this, "TObject", null, function () {',
  34799. ' this.$init = function () {',
  34800. ' };',
  34801. ' this.$final = function () {',
  34802. ' };',
  34803. ' this.Run = async function () {',
  34804. ' var Result = null;',
  34805. ' return Result;',
  34806. ' };',
  34807. '});',
  34808. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34809. ' this.Fly = async function () {',
  34810. ' var Result = null;',
  34811. ' var o = null;',
  34812. ' o = await this.Run();',
  34813. ' o = await this.Fly();',
  34814. ' o = await this.Fly();',
  34815. ' o = await $mod.TObject.Run.call(this);',
  34816. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34817. ' return Result;',
  34818. ' };',
  34819. '});',
  34820. '']),
  34821. LinesToStr([
  34822. '']));
  34823. CheckResolverUnexpectedHints();
  34824. end;
  34825. procedure TTestModule.TestLibrary_Empty;
  34826. begin
  34827. StartLibrary(false);
  34828. Add([
  34829. '']);
  34830. ConvertLibrary;
  34831. CheckFullSource('TestLibrary_Empty',
  34832. LinesToStr([ // statements
  34833. 'rtl.module("library", [], function () {',
  34834. ' var $mod = this;',
  34835. ' $mod.$main = function () {',
  34836. ' };',
  34837. '});',
  34838. 'rtl.run("library");',
  34839. '']));
  34840. CheckResolverUnexpectedHints();
  34841. end;
  34842. procedure TTestModule.TestLibrary_ExportFunc;
  34843. begin
  34844. StartLibrary(false);
  34845. Add([
  34846. 'procedure Run(w: word);',
  34847. 'begin',
  34848. 'end;',
  34849. 'exports',
  34850. ' Run;',
  34851. ' run name ''Foo'';',
  34852. ' test1.run name ''Test1Run'';',
  34853. '']);
  34854. ConvertLibrary;
  34855. CheckFullSource('TestLibrary_ExportFunc',
  34856. LinesToStr([ // statements
  34857. 'rtl.module("library", [], function () {',
  34858. ' var $mod = this;',
  34859. ' this.Run = function (w) {',
  34860. ' };',
  34861. ' $mod.$main = function () {',
  34862. ' };',
  34863. '});',
  34864. 'rtl.run("library");',
  34865. 'export const Run = pas.library.Run;',
  34866. 'export const Foo = pas.library.Run;',
  34867. 'export const Test1Run = pas.library.Run;',
  34868. '']));
  34869. CheckResolverUnexpectedHints();
  34870. end;
  34871. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34872. begin
  34873. StartLibrary(false);
  34874. Add([
  34875. 'procedure Run(w: word); overload;',
  34876. 'begin',
  34877. 'end;',
  34878. 'procedure Run(s: string); overload;',
  34879. 'begin',
  34880. 'end;',
  34881. 'exports',
  34882. ' Run;',
  34883. '']);
  34884. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34885. nCantDetermineWhichOverloadedFunctionToCall);
  34886. ConvertLibrary;
  34887. end;
  34888. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34889. begin
  34890. StartLibrary(false);
  34891. Add([
  34892. 'procedure Run(w: word);',
  34893. 'begin',
  34894. 'end;',
  34895. 'exports',
  34896. ' Run index 3;',
  34897. '']);
  34898. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34899. ConvertLibrary;
  34900. end;
  34901. procedure TTestModule.TestLibrary_ExportVar;
  34902. begin
  34903. StartLibrary(false);
  34904. Add([
  34905. 'var Wing: word;',
  34906. 'exports',
  34907. ' Wing, wing name ''BirdArm'';',
  34908. '']);
  34909. ConvertLibrary;
  34910. CheckFullSource('TestLibrary_ExportVar',
  34911. LinesToStr([ // statements
  34912. 'rtl.module("library", [], function () {',
  34913. ' var $mod = this;',
  34914. ' this.Wing = 0;',
  34915. ' $mod.$main = function () {',
  34916. ' };',
  34917. '});',
  34918. 'rtl.run("library");',
  34919. 'export const vars = {};',
  34920. 'Object.defineProperties(vars, {',
  34921. ' Wing: {',
  34922. ' enumerable: true,',
  34923. ' get: function () {',
  34924. ' return pas.library.Wing;',
  34925. ' },',
  34926. ' set: function (v) {',
  34927. ' pas.library.Wing = v;',
  34928. ' }',
  34929. ' },',
  34930. ' BirdArm: {',
  34931. ' enumerable: true,',
  34932. ' get: function () {',
  34933. ' return pas.library.Wing;',
  34934. ' },',
  34935. ' set: function (v) {',
  34936. ' pas.library.Wing = v;',
  34937. ' }',
  34938. ' }',
  34939. '});',
  34940. '']));
  34941. CheckResolverUnexpectedHints();
  34942. end;
  34943. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34944. begin
  34945. AddModuleWithIntfImplSrc('Unit1.pas',
  34946. LinesToStr([
  34947. 'type',
  34948. ' TAnt = class',
  34949. ' class function Crawl: word; static;',
  34950. ' end;',
  34951. 'function Fly: word;',
  34952. '']),
  34953. LinesToStr([
  34954. 'function Fly: word;',
  34955. 'begin',
  34956. 'end;',
  34957. 'class function TAnt.Crawl: word;',
  34958. 'begin',
  34959. 'end;',
  34960. '']));
  34961. StartLibrary(true,[supTObject]);
  34962. Add([
  34963. 'uses unit1;',
  34964. 'exports',
  34965. ' Fly;',
  34966. ' TAnt.Crawl;',
  34967. '']);
  34968. ConvertLibrary;
  34969. CheckFullSource('TestLibrary_ExportUnitFunc',
  34970. LinesToStr([ // statements
  34971. 'rtl.module("library", ["system", "Unit1"], function () {',
  34972. ' var $mod = this;',
  34973. ' $mod.$main = function () {',
  34974. ' };',
  34975. '});',
  34976. 'rtl.run("library");',
  34977. 'export const Fly = pas.Unit1.Fly;',
  34978. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34979. '']));
  34980. CheckResolverUnexpectedHints();
  34981. end;
  34982. Initialization
  34983. RegisterTests([TTestModule]);
  34984. end.