tcmodules.pas 945 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858
  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 = "\x00";',
  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 = "\x00";',
  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 = "\x00";',
  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 = "\x00";',
  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 = "\x00";',
  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 = "\x00";',
  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 = "\x00";',
  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. ' s:=''Hello ''#55357#56841', // #$D83D#$DE09
  8499. '']);
  8500. ConvertProgram;
  8501. CheckSource('TestStringConstSurrogate',
  8502. LinesToStr([
  8503. 'this.s="";'
  8504. ]),
  8505. LinesToStr([
  8506. '$mod.s="😊";',
  8507. '$mod.s="Hello 😉";'
  8508. ]));
  8509. end;
  8510. procedure TTestModule.TestStringConst_Multiline;
  8511. begin
  8512. StartProgram(false);
  8513. Add([
  8514. '{$modeswitch multilinestrings}',
  8515. 'const',
  8516. ' a = ``;',
  8517. ' b = `',
  8518. 'line`;',
  8519. ' c = `Single`;',
  8520. ' d = ````;',
  8521. ' e = `abc``xyz`;',
  8522. ' f = `first''line',
  8523. ' second''line`#10;',
  8524. 'begin',
  8525. '']);
  8526. ConvertProgram;
  8527. CheckSource('TestStringConst_Multiline',
  8528. LinesToStr([
  8529. 'this.a = "";',
  8530. 'this.b = "'+JSONNewLine+'line";',
  8531. 'this.c = "Single";',
  8532. 'this.d = "`";',
  8533. 'this.e = "abc`xyz";',
  8534. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8535. '']),
  8536. LinesToStr([
  8537. ]));
  8538. end;
  8539. procedure TTestModule.TestString_Length;
  8540. begin
  8541. StartProgram(false);
  8542. Add('const c = ''foo'';');
  8543. Add('var');
  8544. Add(' s: string;');
  8545. Add(' i: longint;');
  8546. Add('begin');
  8547. Add(' i:=length(s);');
  8548. Add(' i:=length(s+s);');
  8549. Add(' i:=length(''abc'');');
  8550. Add(' i:=length(c);');
  8551. ConvertProgram;
  8552. CheckSource('TestString_Length',
  8553. LinesToStr([
  8554. 'this.c = "foo";',
  8555. 'this.s = "";',
  8556. 'this.i = 0;',
  8557. '']),
  8558. LinesToStr([
  8559. '$mod.i = $mod.s.length;',
  8560. '$mod.i = ($mod.s+$mod.s).length;',
  8561. '$mod.i = "abc".length;',
  8562. '$mod.i = $mod.c.length;',
  8563. '']));
  8564. end;
  8565. procedure TTestModule.TestString_Compare;
  8566. begin
  8567. StartProgram(false);
  8568. Add('var');
  8569. Add(' s, t: string;');
  8570. Add(' b: boolean;');
  8571. Add('begin');
  8572. Add(' b:=s=t;');
  8573. Add(' b:=s<>t;');
  8574. Add(' b:=s>t;');
  8575. Add(' b:=s>=t;');
  8576. Add(' b:=s<t;');
  8577. Add(' b:=s<=t;');
  8578. ConvertProgram;
  8579. CheckSource('TestString_Compare',
  8580. LinesToStr([ // statements
  8581. 'this.s = "";',
  8582. 'this.t = "";',
  8583. 'this.b =false;'
  8584. ]),
  8585. LinesToStr([ // this.$main
  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. '$mod.b = $mod.s < $mod.t;',
  8591. '$mod.b = $mod.s <= $mod.t;',
  8592. '']));
  8593. end;
  8594. procedure TTestModule.TestString_SetLength;
  8595. begin
  8596. StartProgram(false);
  8597. Add([
  8598. 'procedure Fly(var s: string);',
  8599. 'begin',
  8600. ' SetLength(s,1);',
  8601. 'end;',
  8602. 'procedure Run(var s: unicodestring);',
  8603. 'begin',
  8604. ' SetLength(s,2);',
  8605. 'end;',
  8606. 'var s: string;',
  8607. ' u: unicodestring;',
  8608. 'begin',
  8609. ' SetLength(s,3);',
  8610. ' SetLength(u,4);',
  8611. '']);
  8612. ConvertProgram;
  8613. CheckSource('TestString_SetLength',
  8614. LinesToStr([ // statements
  8615. 'this.Fly = function (s) {',
  8616. ' s.set(rtl.strSetLength(s.get(), 1));',
  8617. '};',
  8618. 'this.Run = function (s) {',
  8619. ' s.set(rtl.strSetLength(s.get(), 2));',
  8620. '};',
  8621. 'this.s = "";',
  8622. 'this.u = "";',
  8623. '']),
  8624. LinesToStr([ // this.$main
  8625. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8626. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8627. ]));
  8628. end;
  8629. procedure TTestModule.TestString_CharAt;
  8630. begin
  8631. StartProgram(false);
  8632. Add([
  8633. 'var',
  8634. ' s: string;',
  8635. ' c: char;',
  8636. ' b: boolean;',
  8637. 'begin',
  8638. ' b:= s[1] = c;',
  8639. ' b:= c = s[1];',
  8640. ' b:= c <> s[1];',
  8641. ' b:= c > s[1];',
  8642. ' b:= c >= s[1];',
  8643. ' b:= c < s[2];',
  8644. ' b:= c <= s[1];',
  8645. ' s[1] := c;',
  8646. ' s[2+3] := c;']);
  8647. ConvertProgram;
  8648. CheckSource('TestString_CharAt',
  8649. LinesToStr([ // statements
  8650. 'this.s = "";',
  8651. 'this.c = "\x00";',
  8652. 'this.b = false;'
  8653. ]),
  8654. LinesToStr([ // this.$main
  8655. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  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(0);',
  8659. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8660. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8661. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8662. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8663. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8664. '']));
  8665. end;
  8666. procedure TTestModule.TestStringHMinusFail;
  8667. begin
  8668. StartProgram(false);
  8669. Add([
  8670. '{$H-}',
  8671. 'var s: string;',
  8672. 'begin']);
  8673. ConvertProgram;
  8674. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8675. end;
  8676. procedure TTestModule.TestStr;
  8677. begin
  8678. StartProgram(false);
  8679. Add('var');
  8680. Add(' b: boolean;');
  8681. Add(' i: longint;');
  8682. Add(' d: double;');
  8683. Add(' s: string;');
  8684. Add('begin');
  8685. Add(' str(b,s);');
  8686. Add(' str(i,s);');
  8687. Add(' str(d,s);');
  8688. Add(' str(i:3,s);');
  8689. Add(' str(d:3:2,s);');
  8690. Add(' Str(12.456:12:1,s);');
  8691. Add(' Str(12.456:12,s);');
  8692. Add(' s:=str(b);');
  8693. Add(' s:=str(i);');
  8694. Add(' s:=str(d);');
  8695. Add(' s:=str(i,i);');
  8696. Add(' s:=str(i:3);');
  8697. Add(' s:=str(d:3:2);');
  8698. Add(' s:=str(i:4,i);');
  8699. Add(' s:=str(i,i:5);');
  8700. Add(' s:=str(i:4,i:5);');
  8701. Add(' s:=str(s,s);');
  8702. Add(' s:=str(s,''foo'');');
  8703. ConvertProgram;
  8704. CheckSource('TestStr',
  8705. LinesToStr([ // statements
  8706. 'this.b = false;',
  8707. 'this.i = 0;',
  8708. 'this.d = 0.0;',
  8709. 'this.s = "";',
  8710. '']),
  8711. LinesToStr([ // this.$main
  8712. '$mod.s = ""+$mod.b;',
  8713. '$mod.s = ""+$mod.i;',
  8714. '$mod.s = rtl.floatToStr($mod.d);',
  8715. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8716. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8717. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8718. '$mod.s = rtl.floatToStr(12.456,12);',
  8719. '$mod.s = ""+$mod.b;',
  8720. '$mod.s = ""+$mod.i;',
  8721. '$mod.s = rtl.floatToStr($mod.d);',
  8722. '$mod.s = ""+$mod.i+$mod.i;',
  8723. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8724. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8725. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8726. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8727. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8728. '$mod.s = $mod.s + $mod.s;',
  8729. '$mod.s = $mod.s + "foo";',
  8730. '']));
  8731. end;
  8732. procedure TTestModule.TestBaseType_AnsiStringFail;
  8733. begin
  8734. StartProgram(false);
  8735. Add('var s: AnsiString');
  8736. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8737. ConvertProgram;
  8738. end;
  8739. procedure TTestModule.TestBaseType_WideStringFail;
  8740. begin
  8741. StartProgram(false);
  8742. Add('var s: WideString');
  8743. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8744. ConvertProgram;
  8745. end;
  8746. procedure TTestModule.TestBaseType_ShortStringFail;
  8747. begin
  8748. StartProgram(false);
  8749. Add('var s: ShortString');
  8750. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8751. ConvertProgram;
  8752. end;
  8753. procedure TTestModule.TestBaseType_RawByteStringFail;
  8754. begin
  8755. StartProgram(false);
  8756. Add('var s: RawByteString');
  8757. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8758. ConvertProgram;
  8759. end;
  8760. procedure TTestModule.TestTypeShortstring_Fail;
  8761. begin
  8762. StartProgram(false);
  8763. Add('type t = string[12];');
  8764. Add('var s: t;');
  8765. Add('begin');
  8766. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8767. ConvertProgram;
  8768. end;
  8769. procedure TTestModule.TestCharSet_Custom;
  8770. begin
  8771. StartProgram(false);
  8772. Add([
  8773. 'type',
  8774. ' TCharRg = ''a''..''z'';',
  8775. ' TSetOfCharRg = set of TCharRg;',
  8776. ' TCharRg2 = ''m''..''p'';',
  8777. 'const',
  8778. ' crg: TCharRg = ''b'';',
  8779. 'var',
  8780. ' c: char;',
  8781. ' crg2: TCharRg2;',
  8782. ' s: TSetOfCharRg;',
  8783. 'begin',
  8784. ' c:=crg;',
  8785. ' crg:=c;',
  8786. ' crg2:=crg;',
  8787. ' if c=crg then ;',
  8788. ' if crg=c then ;',
  8789. ' if crg=crg2 then ;',
  8790. ' if c in s then ;',
  8791. ' if crg2 in s then ;',
  8792. ' c:=default(TCharRg);',
  8793. '']);
  8794. ConvertProgram;
  8795. CheckSource('TestCharSet_Custom',
  8796. LinesToStr([ // statements
  8797. 'this.crg = "b";',
  8798. 'this.c = "\x00";',
  8799. 'this.crg2 = "m";',
  8800. 'this.s = {};',
  8801. '']),
  8802. LinesToStr([ // this.$main
  8803. '$mod.c = $mod.crg;',
  8804. '$mod.crg = $mod.c;',
  8805. '$mod.crg2 = $mod.crg;',
  8806. 'if ($mod.c === $mod.crg) ;',
  8807. 'if ($mod.crg === $mod.c) ;',
  8808. 'if ($mod.crg === $mod.crg2) ;',
  8809. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8810. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8811. '$mod.c = "a";',
  8812. '']));
  8813. end;
  8814. procedure TTestModule.TestWideChar;
  8815. begin
  8816. StartProgram(false);
  8817. Add([
  8818. 'procedure Fly(var c: char);',
  8819. 'begin',
  8820. 'end;',
  8821. 'procedure Run(var c: widechar);',
  8822. 'begin',
  8823. 'end;',
  8824. 'var',
  8825. ' c: char;',
  8826. ' wc: widechar;',
  8827. ' w: word;',
  8828. 'begin',
  8829. ' Fly(wc);',
  8830. ' Run(c);',
  8831. ' wc:=WideChar(w);',
  8832. ' w:=ord(wc);',
  8833. '']);
  8834. ConvertProgram;
  8835. CheckSource('TestWideChar_VarArg',
  8836. LinesToStr([ // statements
  8837. 'this.Fly = function (c) {',
  8838. '};',
  8839. 'this.Run = function (c) {',
  8840. '};',
  8841. 'this.c = "\x00";',
  8842. 'this.wc = "\x00";',
  8843. 'this.w = 0;',
  8844. '']),
  8845. LinesToStr([ // this.$main
  8846. '$mod.Fly({',
  8847. ' p: $mod,',
  8848. ' get: function () {',
  8849. ' return this.p.wc;',
  8850. ' },',
  8851. ' set: function (v) {',
  8852. ' this.p.wc = v;',
  8853. ' }',
  8854. '});',
  8855. '$mod.Run({',
  8856. ' p: $mod,',
  8857. ' get: function () {',
  8858. ' return this.p.c;',
  8859. ' },',
  8860. ' set: function (v) {',
  8861. ' this.p.c = v;',
  8862. ' }',
  8863. '});',
  8864. '$mod.wc = String.fromCharCode($mod.w);',
  8865. '$mod.w = $mod.wc.charCodeAt();',
  8866. '',
  8867. '']));
  8868. end;
  8869. procedure TTestModule.TestForCharDo;
  8870. begin
  8871. StartProgram(false);
  8872. Add([
  8873. 'var c: char;',
  8874. 'begin',
  8875. ' for c:=''a'' to ''c'' do ;',
  8876. ' for c:=c downto ''a'' do ;',
  8877. ' for c:=''Б'' to ''Я'' do ;',
  8878. '']);
  8879. ConvertProgram;
  8880. CheckSource('TestForCharDo',
  8881. LinesToStr([ // statements
  8882. 'this.c = "\x00";']),
  8883. LinesToStr([ // this.$main
  8884. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8885. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8886. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8887. '']));
  8888. end;
  8889. procedure TTestModule.TestForCharInDo;
  8890. begin
  8891. StartProgram(false);
  8892. Add([
  8893. 'type',
  8894. ' TSetOfChar = set of char;',
  8895. ' TCharRg = ''a''..''z'';',
  8896. ' TSetOfCharRg = set of TCharRg;',
  8897. 'const Foo = ''foo'';',
  8898. 'var',
  8899. ' c,c2: char;',
  8900. ' s: string;',
  8901. ' a1: array of char;',
  8902. ' a2: array[1..3] of char;',
  8903. ' soc: TSetOfChar;',
  8904. ' socr: TSetOfCharRg;',
  8905. ' cr: TCharRg;',
  8906. 'begin',
  8907. ' for c in foo do ;',
  8908. ' for c in s do ;',
  8909. ' for c in char do ;',
  8910. ' for c in a1 do ;',
  8911. ' for c in a2 do ;',
  8912. ' for c in [''1''..''3''] do ;',
  8913. ' for c in TSetOfChar do ;',
  8914. ' for c in TCharRg do ;',
  8915. ' for c in soc do c2:=c;',
  8916. ' for c in TSetOfCharRg do ;',
  8917. ' for c in socr do ;',
  8918. ' for cr in TCharRg do ;',
  8919. ' for cr in TSetOfCharRg do ;',
  8920. ' for cr in socr do ;',
  8921. '']);
  8922. ConvertProgram;
  8923. CheckSource('TestForCharInDo',
  8924. LinesToStr([ // statements
  8925. 'this.Foo = "foo";',
  8926. 'this.c = "\x00";',
  8927. 'this.c2 = "\x00";',
  8928. 'this.s = "";',
  8929. 'this.a1 = [];',
  8930. 'this.a2 = rtl.arraySetLength(null, "\x00", 3);',
  8931. 'this.soc = {};',
  8932. 'this.socr = {};',
  8933. 'this.cr = "a";',
  8934. '']),
  8935. LinesToStr([ // this.$main
  8936. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8937. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8938. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8939. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8940. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8941. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8942. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8943. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8944. 'for (var $l8 in $mod.soc) {',
  8945. ' $mod.c = String.fromCharCode($l8);',
  8946. ' $mod.c2 = $mod.c;',
  8947. '};',
  8948. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8949. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8950. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8951. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8952. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8953. '']));
  8954. end;
  8955. procedure TTestModule.TestProcTwoArgs;
  8956. begin
  8957. StartProgram(false);
  8958. Add('procedure Test(a,b: longint);');
  8959. Add('begin');
  8960. Add('end;');
  8961. Add('begin');
  8962. ConvertProgram;
  8963. CheckSource('TestProcTwoArgs',
  8964. LinesToStr([ // statements
  8965. 'this.Test = function (a,b) {',
  8966. '};'
  8967. ]),
  8968. LinesToStr([ // this.$main
  8969. ''
  8970. ]));
  8971. end;
  8972. procedure TTestModule.TestProc_DefaultValue;
  8973. begin
  8974. StartProgram(false);
  8975. Add('procedure p1(i: longint = 1);');
  8976. Add('begin');
  8977. Add('end;');
  8978. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8979. Add('begin');
  8980. Add('end;');
  8981. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8982. Add('begin');
  8983. Add('end;');
  8984. Add('begin');
  8985. Add(' p1;');
  8986. Add(' p1();');
  8987. Add(' p1(11);');
  8988. Add(' p2;');
  8989. Add(' p2();');
  8990. Add(' p2(12);');
  8991. Add(' p2(13,''b'');');
  8992. Add(' p3();');
  8993. ConvertProgram;
  8994. CheckSource('TestProc_DefaultValue',
  8995. LinesToStr([ // statements
  8996. 'this.p1 = function (i) {',
  8997. '};',
  8998. 'this.p2 = function (i,c) {',
  8999. '};',
  9000. 'this.p3 = function (d,b,s) {',
  9001. '};'
  9002. ]),
  9003. LinesToStr([ // this.$main
  9004. ' $mod.p1(1);',
  9005. ' $mod.p1(1);',
  9006. ' $mod.p1(11);',
  9007. ' $mod.p2(1,"a");',
  9008. ' $mod.p2(1,"a");',
  9009. ' $mod.p2(12,"a");',
  9010. ' $mod.p2(13,"b");',
  9011. ' $mod.p3(1.0,false,"abc");'
  9012. ]));
  9013. end;
  9014. procedure TTestModule.TestFunctionInt;
  9015. begin
  9016. StartProgram(false);
  9017. Add('function MyTest(Bar: longint): longint;');
  9018. Add('begin');
  9019. Add(' Result:=2*bar');
  9020. Add('end;');
  9021. Add('begin');
  9022. ConvertProgram;
  9023. CheckSource('TestFunctionInt',
  9024. LinesToStr([ // statements
  9025. 'this.MyTest = function (Bar) {',
  9026. ' var Result = 0;',
  9027. ' Result = 2*Bar;',
  9028. ' return Result;',
  9029. '};'
  9030. ]),
  9031. LinesToStr([ // this.$main
  9032. ''
  9033. ]));
  9034. end;
  9035. procedure TTestModule.TestFunctionString;
  9036. begin
  9037. StartProgram(false);
  9038. Add('function Test(Bar: string): string;');
  9039. Add('begin');
  9040. Add(' Result:=bar+BAR');
  9041. Add('end;');
  9042. Add('begin');
  9043. ConvertProgram;
  9044. CheckSource('TestFunctionString',
  9045. LinesToStr([ // statements
  9046. 'this.Test = function (Bar) {',
  9047. ' var Result = "";',
  9048. ' Result = Bar+Bar;',
  9049. ' return Result;',
  9050. '};'
  9051. ]),
  9052. LinesToStr([ // this.$main
  9053. ''
  9054. ]));
  9055. end;
  9056. procedure TTestModule.TestIfThen;
  9057. begin
  9058. StartProgram(false);
  9059. Add([
  9060. 'var b: boolean;',
  9061. 'begin',
  9062. ' if b then ;',
  9063. ' if b then else ;']);
  9064. ConvertProgram;
  9065. CheckSource('TestIfThen',
  9066. LinesToStr([ // statements
  9067. 'this.b = false;',
  9068. '']),
  9069. LinesToStr([ // this.$main
  9070. 'if ($mod.b) ;',
  9071. 'if ($mod.b) ;',
  9072. '']));
  9073. end;
  9074. procedure TTestModule.TestForLoop;
  9075. begin
  9076. StartProgram(false);
  9077. Add('var');
  9078. Add(' vI, vJ, vN: longint;');
  9079. Add('begin');
  9080. Add(' VJ:=0;');
  9081. Add(' VN:=3;');
  9082. Add(' for VI:=1 to VN do');
  9083. Add(' begin');
  9084. Add(' VJ:=VJ+VI;');
  9085. Add(' end;');
  9086. ConvertProgram;
  9087. CheckSource('TestForLoop',
  9088. LinesToStr([ // statements
  9089. 'this.vI = 0;',
  9090. 'this.vJ = 0;',
  9091. 'this.vN = 0;'
  9092. ]),
  9093. LinesToStr([ // this.$main
  9094. ' $mod.vJ = 0;',
  9095. ' $mod.vN = 3;',
  9096. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9097. ' $mod.vI = $l;',
  9098. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9099. ' };',
  9100. '']));
  9101. end;
  9102. procedure TTestModule.TestForLoopInsideFunction;
  9103. begin
  9104. StartProgram(false);
  9105. Add('function SumNumbers(Count: longint): longint;');
  9106. Add('var');
  9107. Add(' vI, vJ: longint;');
  9108. Add('begin');
  9109. Add(' vj:=0;');
  9110. Add(' for vi:=1 to count do');
  9111. Add(' begin');
  9112. Add(' vj:=vj+vi;');
  9113. Add(' end;');
  9114. Add('end;');
  9115. Add('begin');
  9116. Add(' sumnumbers(3);');
  9117. ConvertProgram;
  9118. CheckSource('TestForLoopInsideFunction',
  9119. LinesToStr([ // statements
  9120. 'this.SumNumbers = function (Count) {',
  9121. ' var Result = 0;',
  9122. ' var vI = 0;',
  9123. ' var vJ = 0;',
  9124. ' vJ = 0;',
  9125. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9126. ' vI = $l;',
  9127. ' vJ = vJ + vI;',
  9128. ' };',
  9129. ' return Result;',
  9130. '};'
  9131. ]),
  9132. LinesToStr([ // $mod.$main
  9133. ' $mod.SumNumbers(3);'
  9134. ]));
  9135. end;
  9136. procedure TTestModule.TestForLoop_ReadVarAfter;
  9137. begin
  9138. StartProgram(false);
  9139. Add('var');
  9140. Add(' vI: longint;');
  9141. Add('begin');
  9142. Add(' for vi:=1 to 2 do ;');
  9143. Add(' if vi=3 then ;');
  9144. ConvertProgram;
  9145. CheckSource('TestForLoop',
  9146. LinesToStr([ // statements
  9147. 'this.vI = 0;'
  9148. ]),
  9149. LinesToStr([ // this.$main
  9150. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9151. ' if ($mod.vI===3) ;'
  9152. ]));
  9153. end;
  9154. procedure TTestModule.TestForLoop_Nested;
  9155. begin
  9156. StartProgram(false);
  9157. Add('function SumNumbers(Count: longint): longint;');
  9158. Add('var');
  9159. Add(' vI, vJ, vK: longint;');
  9160. Add('begin');
  9161. Add(' VK:=0;');
  9162. Add(' for VI:=1 to count do');
  9163. Add(' begin');
  9164. Add(' for vj:=1 to vi do');
  9165. Add(' begin');
  9166. Add(' vk:=VK+VI;');
  9167. Add(' end;');
  9168. Add(' end;');
  9169. Add('end;');
  9170. Add('begin');
  9171. Add(' sumnumbers(3);');
  9172. ConvertProgram;
  9173. CheckSource('TestForLoopInFunction',
  9174. LinesToStr([ // statements
  9175. 'this.SumNumbers = function (Count) {',
  9176. ' var Result = 0;',
  9177. ' var vI = 0;',
  9178. ' var vJ = 0;',
  9179. ' var vK = 0;',
  9180. ' vK = 0;',
  9181. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9182. ' vI = $l;',
  9183. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9184. ' vJ = $l1;',
  9185. ' vK = vK + vI;',
  9186. ' };',
  9187. ' };',
  9188. ' return Result;',
  9189. '};'
  9190. ]),
  9191. LinesToStr([ // $mod.$main
  9192. ' $mod.SumNumbers(3);'
  9193. ]));
  9194. end;
  9195. procedure TTestModule.TestRepeatUntil;
  9196. begin
  9197. StartProgram(false);
  9198. Add('var');
  9199. Add(' vI, vJ, vN: longint;');
  9200. Add('begin');
  9201. Add(' vn:=3;');
  9202. Add(' vj:=0;');
  9203. Add(' VI:=0;');
  9204. Add(' repeat');
  9205. Add(' VI:=vi+1;');
  9206. Add(' vj:=VJ+vI;');
  9207. Add(' until vi>=vn');
  9208. ConvertProgram;
  9209. CheckSource('TestRepeatUntil',
  9210. LinesToStr([ // statements
  9211. 'this.vI = 0;',
  9212. 'this.vJ = 0;',
  9213. 'this.vN = 0;'
  9214. ]),
  9215. LinesToStr([ // $mod.$main
  9216. ' $mod.vN = 3;',
  9217. ' $mod.vJ = 0;',
  9218. ' $mod.vI = 0;',
  9219. ' do{',
  9220. ' $mod.vI = $mod.vI + 1;',
  9221. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9222. ' }while(!($mod.vI>=$mod.vN));'
  9223. ]));
  9224. end;
  9225. procedure TTestModule.TestAsmBlock;
  9226. begin
  9227. StartProgram(false);
  9228. Add([
  9229. 'var',
  9230. ' vI: longint;',
  9231. 'begin',
  9232. ' vi:=1;',
  9233. ' asm',
  9234. ' if (vI===1) {',
  9235. ' vI=2;',
  9236. //' console.log(''end;'');', ToDo
  9237. ' }',
  9238. ' if (vI===2){ vI=3; }',
  9239. ' end;',
  9240. ' VI:=4;']);
  9241. ConvertProgram;
  9242. CheckSource('TestAsmBlock',
  9243. LinesToStr([ // statements
  9244. 'this.vI = 0;'
  9245. ]),
  9246. LinesToStr([ // $mod.$main
  9247. '$mod.vI = 1;',
  9248. 'if (vI===1) {',
  9249. ' vI=2;',
  9250. '}',
  9251. 'if (vI===2){ vI=3; }',
  9252. ';',
  9253. '$mod.vI = 4;'
  9254. ]));
  9255. end;
  9256. procedure TTestModule.TestAsmPas_Impl;
  9257. begin
  9258. StartUnit(false);
  9259. Add('interface');
  9260. Add('const cIntf: longint = 1;');
  9261. Add('var vIntf: longint;');
  9262. Add('implementation');
  9263. Add('const cImpl: longint = 2;');
  9264. Add('var vImpl: longint;');
  9265. Add('procedure DoIt;');
  9266. Add('const cLoc: longint = 3;');
  9267. Add('var vLoc: longint;');
  9268. Add('begin;');
  9269. Add(' asm');
  9270. //Add(' pas(vIntf)=pas(cIntf);');
  9271. //Add(' pas(vImpl)=pas(cImpl);');
  9272. //Add(' pas(vLoc)=pas(cLoc);');
  9273. Add(' end;');
  9274. Add('end;');
  9275. ConvertUnit;
  9276. CheckSource('TestAsmPas_Impl',
  9277. LinesToStr([
  9278. 'var $impl = $mod.$impl;',
  9279. 'this.cIntf = 1;',
  9280. 'this.vIntf = 0;',
  9281. '']),
  9282. '', // this.$init
  9283. LinesToStr([ // implementation
  9284. '$impl.cImpl = 2;',
  9285. '$impl.vImpl = 0;',
  9286. 'var cLoc = 3;',
  9287. '$impl.DoIt = function () {',
  9288. ' var vLoc = 0;',
  9289. '};',
  9290. '']) );
  9291. end;
  9292. procedure TTestModule.TestTryFinally;
  9293. begin
  9294. StartProgram(false);
  9295. Add('var i: longint;');
  9296. Add('begin');
  9297. Add(' try');
  9298. Add(' i:=0; i:=2 div i;');
  9299. Add(' finally');
  9300. Add(' i:=3');
  9301. Add(' end;');
  9302. ConvertProgram;
  9303. CheckSource('TestTryFinally',
  9304. LinesToStr([ // statements
  9305. 'this.i = 0;'
  9306. ]),
  9307. LinesToStr([ // $mod.$main
  9308. 'try {',
  9309. ' $mod.i = 0;',
  9310. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9311. '} finally {',
  9312. ' $mod.i = 3;',
  9313. '};'
  9314. ]));
  9315. end;
  9316. procedure TTestModule.TestTryExcept;
  9317. begin
  9318. StartProgram(false);
  9319. Add([
  9320. 'type',
  9321. ' TObject = class end;',
  9322. ' Exception = class Msg: string; end;',
  9323. ' EInvalidCast = class(Exception) end;',
  9324. 'var vI: longint;',
  9325. 'begin',
  9326. ' try',
  9327. ' vi:=1;',
  9328. ' except',
  9329. ' vi:=2',
  9330. ' end;',
  9331. ' try',
  9332. ' vi:=3;',
  9333. ' except',
  9334. ' raise;',
  9335. ' end;',
  9336. ' try',
  9337. ' VI:=4;',
  9338. ' except',
  9339. ' on einvalidcast do',
  9340. ' raise;',
  9341. ' on E: exception do',
  9342. ' if e.msg='''' then',
  9343. ' raise e;',
  9344. ' else',
  9345. ' vi:=5',
  9346. ' end;',
  9347. ' try',
  9348. ' VI:=6;',
  9349. ' except',
  9350. ' on einvalidcast do ;',
  9351. ' end;',
  9352. '']);
  9353. ConvertProgram;
  9354. CheckSource('TestTryExcept',
  9355. LinesToStr([ // statements
  9356. 'rtl.createClass(this, "TObject", null, function () {',
  9357. ' this.$init = function () {',
  9358. ' };',
  9359. ' this.$final = function () {',
  9360. ' };',
  9361. '});',
  9362. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9363. ' this.$init = function () {',
  9364. ' $mod.TObject.$init.call(this);',
  9365. ' this.Msg = "";',
  9366. ' };',
  9367. '});',
  9368. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9369. '});',
  9370. 'this.vI = 0;'
  9371. ]),
  9372. LinesToStr([ // $mod.$main
  9373. 'try {',
  9374. ' $mod.vI = 1;',
  9375. '} catch ($e) {',
  9376. ' $mod.vI = 2;',
  9377. '};',
  9378. 'try {',
  9379. ' $mod.vI = 3;',
  9380. '} catch ($e) {',
  9381. ' throw $e;',
  9382. '};',
  9383. 'try {',
  9384. ' $mod.vI = 4;',
  9385. '} catch ($e) {',
  9386. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9387. ' throw $e',
  9388. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9389. ' var E = $e;',
  9390. ' if (E.Msg === "") throw E;',
  9391. ' } else {',
  9392. ' $mod.vI = 5;',
  9393. ' }',
  9394. '};',
  9395. 'try {',
  9396. ' $mod.vI = 6;',
  9397. '} catch ($e) {',
  9398. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9399. ' } else throw $e',
  9400. '};',
  9401. '']));
  9402. end;
  9403. procedure TTestModule.TestTryExcept_ReservedWords;
  9404. begin
  9405. StartProgram(false);
  9406. Add([
  9407. 'type',
  9408. ' TObject = class end;',
  9409. ' Exception = class',
  9410. ' Symbol: string;',
  9411. ' end;',
  9412. 'var &try: longint;',
  9413. 'begin',
  9414. ' try',
  9415. ' &try:=4;',
  9416. ' except',
  9417. ' on Error: exception do',
  9418. ' if errOR.symBol='''' then',
  9419. ' raise ERRor;',
  9420. ' end;',
  9421. '']);
  9422. ConvertProgram;
  9423. CheckSource('TestTryExcept_ReservedWords',
  9424. LinesToStr([ // statements
  9425. 'rtl.createClass(this, "TObject", null, function () {',
  9426. ' this.$init = function () {',
  9427. ' };',
  9428. ' this.$final = function () {',
  9429. ' };',
  9430. '});',
  9431. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9432. ' this.$init = function () {',
  9433. ' $mod.TObject.$init.call(this);',
  9434. ' this.Symbol = "";',
  9435. ' };',
  9436. '});',
  9437. 'this.Try = 0;',
  9438. '']),
  9439. LinesToStr([ // $mod.$main
  9440. 'try {',
  9441. ' $mod.Try = 4;',
  9442. '} catch ($e) {',
  9443. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9444. ' var error = $e;',
  9445. ' if (error.Symbol === "") throw error;',
  9446. ' } else throw $e',
  9447. '};',
  9448. '']));
  9449. end;
  9450. procedure TTestModule.TestIfThenRaiseElse;
  9451. begin
  9452. StartProgram(false);
  9453. Add([
  9454. 'type',
  9455. ' TObject = class',
  9456. ' constructor Create;',
  9457. ' end;',
  9458. 'constructor TObject.Create;',
  9459. 'begin',
  9460. 'end;',
  9461. 'var b: boolean;',
  9462. 'begin',
  9463. ' if b then',
  9464. ' raise TObject.Create',
  9465. ' else',
  9466. ' b:=false;',
  9467. '']);
  9468. ConvertProgram;
  9469. CheckSource('TestIfThenRaiseElse',
  9470. LinesToStr([ // statements
  9471. 'rtl.createClass(this, "TObject", null, function () {',
  9472. ' this.$init = function () {',
  9473. ' };',
  9474. ' this.$final = function () {',
  9475. ' };',
  9476. ' this.Create = function () {',
  9477. ' return this;',
  9478. ' };',
  9479. '});',
  9480. 'this.b = false;',
  9481. '']),
  9482. LinesToStr([ // $mod.$main
  9483. 'if ($mod.b) {',
  9484. ' throw $mod.TObject.$create("Create")}',
  9485. ' else $mod.b = false;',
  9486. '']));
  9487. end;
  9488. procedure TTestModule.TestCaseOf;
  9489. begin
  9490. StartProgram(false);
  9491. Add([
  9492. 'const e: longint; external name ''$e'';',
  9493. 'var vI: longint;',
  9494. 'begin',
  9495. ' case vi of',
  9496. ' 1: ;',
  9497. ' 2: vi:=3;',
  9498. ' e: ;',
  9499. ' else',
  9500. ' VI:=4',
  9501. ' end;']);
  9502. ConvertProgram;
  9503. CheckSource('TestCaseOf',
  9504. LinesToStr([ // statements
  9505. 'this.vI = 0;'
  9506. ]),
  9507. LinesToStr([ // $mod.$main
  9508. 'var $tmp = $mod.vI;',
  9509. 'if ($tmp === 1) {}',
  9510. 'else if ($tmp === 2) {',
  9511. ' $mod.vI = 3}',
  9512. ' else if ($tmp === $e) {}',
  9513. 'else {',
  9514. ' $mod.vI = 4;',
  9515. '};'
  9516. ]));
  9517. end;
  9518. procedure TTestModule.TestCaseOf_UseSwitch;
  9519. begin
  9520. StartProgram(false);
  9521. Converter.UseSwitchStatement:=true;
  9522. Add('var Vi: longint;');
  9523. Add('begin');
  9524. Add(' case vi of');
  9525. Add(' 1: ;');
  9526. Add(' 2: VI:=3;');
  9527. Add(' else');
  9528. Add(' vi:=4');
  9529. Add(' end;');
  9530. ConvertProgram;
  9531. CheckSource('TestCaseOf_UseSwitch',
  9532. LinesToStr([ // statements
  9533. 'this.Vi = 0;'
  9534. ]),
  9535. LinesToStr([ // $mod.$main
  9536. 'switch ($mod.Vi) {',
  9537. 'case 1:',
  9538. ' break;',
  9539. 'case 2:',
  9540. ' $mod.Vi = 3;',
  9541. ' break;',
  9542. 'default:',
  9543. ' $mod.Vi = 4;',
  9544. '};'
  9545. ]));
  9546. end;
  9547. procedure TTestModule.TestCaseOfNoElse;
  9548. begin
  9549. StartProgram(false);
  9550. Add('var Vi: longint;');
  9551. Add('begin');
  9552. Add(' case vi of');
  9553. Add(' 1: begin vi:=2; VI:=3; end;');
  9554. Add(' end;');
  9555. ConvertProgram;
  9556. CheckSource('TestCaseOfNoElse',
  9557. LinesToStr([ // statements
  9558. 'this.Vi = 0;'
  9559. ]),
  9560. LinesToStr([ // $mod.$main
  9561. 'var $tmp = $mod.Vi;',
  9562. 'if ($tmp === 1) {',
  9563. ' $mod.Vi = 2;',
  9564. ' $mod.Vi = 3;',
  9565. '};'
  9566. ]));
  9567. end;
  9568. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9569. begin
  9570. StartProgram(false);
  9571. Converter.UseSwitchStatement:=true;
  9572. Add('var vI: longint;');
  9573. Add('begin');
  9574. Add(' case vi of');
  9575. Add(' 1: begin VI:=2; vi:=3; end;');
  9576. Add(' end;');
  9577. ConvertProgram;
  9578. CheckSource('TestCaseOfNoElse_UseSwitch',
  9579. LinesToStr([ // statements
  9580. 'this.vI = 0;'
  9581. ]),
  9582. LinesToStr([ // $mod.$main
  9583. 'switch ($mod.vI) {',
  9584. 'case 1:',
  9585. ' $mod.vI = 2;',
  9586. ' $mod.vI = 3;',
  9587. ' break;',
  9588. '};'
  9589. ]));
  9590. end;
  9591. procedure TTestModule.TestCaseOfRange;
  9592. begin
  9593. StartProgram(false);
  9594. Add('var vI: longint;');
  9595. Add('begin');
  9596. Add(' case vi of');
  9597. Add(' 1..3: vi:=14;');
  9598. Add(' 4,5: vi:=16;');
  9599. Add(' 6..7,9..10: ;');
  9600. Add(' else ;');
  9601. Add(' end;');
  9602. ConvertProgram;
  9603. CheckSource('TestCaseOfRange',
  9604. LinesToStr([ // statements
  9605. 'this.vI = 0;'
  9606. ]),
  9607. LinesToStr([ // $mod.$main
  9608. 'var $tmp = $mod.vI;',
  9609. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9610. ' $mod.vI = 14',
  9611. '} else if (($tmp === 4) || ($tmp === 5)){',
  9612. ' $mod.vI = 16',
  9613. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9614. ]));
  9615. end;
  9616. procedure TTestModule.TestCaseOfString;
  9617. begin
  9618. StartProgram(false);
  9619. Add([
  9620. 'var s,h: string;',
  9621. 'begin',
  9622. ' case s of',
  9623. ' ''foo'': s:=h;',
  9624. ' ''a''..''z'': h:=s;',
  9625. ' ''ў'', ''ё'': ;',
  9626. ' ''Б''..''Я'': ;',
  9627. ' end;',
  9628. '']);
  9629. ConvertProgram;
  9630. CheckSource('TestCaseOfString',
  9631. LinesToStr([ // statements
  9632. 'this.s = "";',
  9633. 'this.h = "";',
  9634. '']),
  9635. LinesToStr([ // $mod.$main
  9636. 'var $tmp = $mod.s;',
  9637. 'if ($tmp === "foo") {',
  9638. ' $mod.s = $mod.h}',
  9639. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9640. ' $mod.h = $mod.s}',
  9641. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9642. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9643. '']));
  9644. end;
  9645. procedure TTestModule.TestCaseOfChar;
  9646. begin
  9647. StartProgram(false);
  9648. Add([
  9649. 'var s,h: char;',
  9650. 'begin',
  9651. ' case s of',
  9652. ' ''a''..''z'': h:=s;',
  9653. ' ''ä'': ;',
  9654. ' ''ў'', ''ё'': ;',
  9655. ' ''Б''..''Я'': ;',
  9656. ' end;',
  9657. '']);
  9658. ConvertProgram;
  9659. CheckSource('TestCaseOfString',
  9660. LinesToStr([ // statements
  9661. 'this.s = "\x00";',
  9662. 'this.h = "\x00";',
  9663. '']),
  9664. LinesToStr([ // $mod.$main
  9665. 'var $tmp = $mod.s;',
  9666. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9667. ' $mod.h = $mod.s}',
  9668. ' else if ($tmp === "ä") {}',
  9669. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9670. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9671. '']));
  9672. end;
  9673. procedure TTestModule.TestCaseOfExternalClassConst;
  9674. begin
  9675. StartProgram(false);
  9676. Add([
  9677. '{$modeswitch externalclass}',
  9678. 'type',
  9679. ' TBird = class external name ''Bird''',
  9680. ' const e: longint;',
  9681. ' end;',
  9682. 'var vI: longint;',
  9683. 'begin',
  9684. ' case vi of',
  9685. ' 1: vi:=3;',
  9686. ' TBird.e: ;',
  9687. ' end;']);
  9688. ConvertProgram;
  9689. CheckSource('TestCaseOfExternalClassConst',
  9690. LinesToStr([ // statements
  9691. 'this.vI = 0;'
  9692. ]),
  9693. LinesToStr([ // $mod.$main
  9694. 'var $tmp = $mod.vI;',
  9695. 'if ($tmp === 1) {',
  9696. ' $mod.vI = 3}',
  9697. ' else if ($tmp === Bird.e) ;'
  9698. ]));
  9699. end;
  9700. procedure TTestModule.TestDebugger;
  9701. begin
  9702. StartProgram(false);
  9703. Add([
  9704. 'procedure DoIt;',
  9705. 'begin',
  9706. ' deBugger;',
  9707. ' DeBugger();',
  9708. 'end;',
  9709. 'begin',
  9710. ' Debugger;']);
  9711. ConvertProgram;
  9712. CheckSource('TestDebugger',
  9713. LinesToStr([ // statements
  9714. 'this.DoIt = function () {',
  9715. ' debugger;',
  9716. ' debugger;',
  9717. '};',
  9718. '']),
  9719. LinesToStr([ // $mod.$main
  9720. 'debugger;',
  9721. '']));
  9722. end;
  9723. procedure TTestModule.TestArray_Dynamic;
  9724. begin
  9725. StartProgram(false);
  9726. Add([
  9727. 'type',
  9728. ' TArrayInt = array of longint;',
  9729. 'var',
  9730. ' Arr: TArrayInt;',
  9731. ' i: longint;',
  9732. ' b: boolean;',
  9733. 'begin',
  9734. ' SetLength(arr,3);',
  9735. ' arr[0]:=4;',
  9736. ' arr[1]:=length(arr)+arr[0];',
  9737. ' arr[i]:=5;',
  9738. ' arr[arr[i]]:=arr[6];',
  9739. ' i:=low(arr);',
  9740. ' i:=high(arr);',
  9741. ' b:=Assigned(arr);',
  9742. ' Arr:=default(TArrayInt);']);
  9743. ConvertProgram;
  9744. CheckSource('TestArray_Dynamic',
  9745. LinesToStr([ // statements
  9746. 'this.Arr = [];',
  9747. 'this.i = 0;',
  9748. 'this.b = false;'
  9749. ]),
  9750. LinesToStr([ // $mod.$main
  9751. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9752. '$mod.Arr[0] = 4;',
  9753. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9754. '$mod.Arr[$mod.i] = 5;',
  9755. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9756. '$mod.i = 0;',
  9757. '$mod.i = rtl.length($mod.Arr) - 1;',
  9758. '$mod.b = rtl.length($mod.Arr) > 0;',
  9759. '$mod.Arr = [];',
  9760. '']));
  9761. end;
  9762. procedure TTestModule.TestArray_Dynamic_Nil;
  9763. begin
  9764. StartProgram(false);
  9765. Add('type');
  9766. Add(' TArrayInt = array of longint;');
  9767. Add('var');
  9768. Add(' Arr: TArrayInt;');
  9769. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9770. Add('begin');
  9771. Add(' arr:=nil;');
  9772. Add(' if arr=nil then;');
  9773. Add(' if nil=arr then;');
  9774. Add(' if arr<>nil then;');
  9775. Add(' if nil<>arr then;');
  9776. Add(' DoIt(nil,nil);');
  9777. ConvertProgram;
  9778. CheckSource('TestArray_Dynamic',
  9779. LinesToStr([ // statements
  9780. 'this.Arr = [];',
  9781. 'this.DoIt = function(i,j){',
  9782. '};'
  9783. ]),
  9784. LinesToStr([ // $mod.$main
  9785. '$mod.Arr = [];',
  9786. 'if (rtl.length($mod.Arr) === 0) ;',
  9787. 'if (rtl.length($mod.Arr) === 0) ;',
  9788. 'if (rtl.length($mod.Arr) > 0) ;',
  9789. 'if (rtl.length($mod.Arr) > 0) ;',
  9790. '$mod.DoIt([],[]);',
  9791. '']));
  9792. end;
  9793. procedure TTestModule.TestArray_DynMultiDimensional;
  9794. begin
  9795. StartProgram(false);
  9796. Add([
  9797. 'type',
  9798. ' TArrayInt = array of longint;',
  9799. ' TArrayArrayInt = array of TArrayInt;',
  9800. 'var',
  9801. ' Arr: TArrayInt;',
  9802. ' Arr2: TArrayArrayInt;',
  9803. ' i: longint;',
  9804. 'begin',
  9805. ' arr2:=nil;',
  9806. ' if arr2=nil then;',
  9807. ' if nil=arr2 then;',
  9808. ' i:=low(arr2);',
  9809. ' i:=low(arr2[1]);',
  9810. ' i:=high(arr2);',
  9811. ' i:=high(arr2[2]);',
  9812. ' arr2[3]:=arr;',
  9813. ' arr2[4][5]:=i;',
  9814. ' i:=arr2[6][7];',
  9815. ' arr2[8,9]:=i;',
  9816. ' i:=arr2[10,11];',
  9817. ' SetLength(arr2,14);',
  9818. ' SetLength(arr2[15],16);']);
  9819. ConvertProgram;
  9820. CheckSource('TestArray_Dynamic',
  9821. LinesToStr([ // statements
  9822. 'this.Arr = [];',
  9823. 'this.Arr2 = [];',
  9824. 'this.i = 0;'
  9825. ]),
  9826. LinesToStr([ // $mod.$main
  9827. '$mod.Arr2 = [];',
  9828. 'if (rtl.length($mod.Arr2) === 0) ;',
  9829. 'if (rtl.length($mod.Arr2) === 0) ;',
  9830. '$mod.i = 0;',
  9831. '$mod.i = 0;',
  9832. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9833. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9834. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9835. '$mod.Arr2[4][5] = $mod.i;',
  9836. '$mod.i = $mod.Arr2[6][7];',
  9837. '$mod.Arr2[8][9] = $mod.i;',
  9838. '$mod.i = $mod.Arr2[10][11];',
  9839. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9840. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9841. '']));
  9842. end;
  9843. procedure TTestModule.TestArray_DynamicAssign;
  9844. begin
  9845. StartProgram(false);
  9846. Add([
  9847. 'type',
  9848. ' TArrayInt = array of longint;',
  9849. ' TArrayArrayInt = array of TArrayInt;',
  9850. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9851. 'begin',
  9852. 'end;',
  9853. 'procedure Fly(var a: TArrayInt);',
  9854. 'begin',
  9855. 'end;',
  9856. 'var',
  9857. ' Arr: TArrayInt;',
  9858. ' Arr2: TArrayArrayInt;',
  9859. 'begin',
  9860. ' arr:=nil;',
  9861. ' arr2:=nil;',
  9862. ' arr2[1]:=nil;',
  9863. ' arr2[2]:=arr;',
  9864. ' Run(arr,arr,arr);',
  9865. ' Fly(arr);',
  9866. ' Run(arr2[4],arr2[5],arr2[6]);',
  9867. ' Fly(arr2[7]);',
  9868. '']);
  9869. ConvertProgram;
  9870. CheckSource('TestArray_DynamicAssign',
  9871. LinesToStr([ // statements
  9872. 'this.Run = function (a, b, c) {',
  9873. '};',
  9874. 'this.Fly = function (a) {',
  9875. '};',
  9876. 'this.Arr = [];',
  9877. 'this.Arr2 = [];',
  9878. '']),
  9879. LinesToStr([ // $mod.$main
  9880. '$mod.Arr = [];',
  9881. '$mod.Arr2 = [];',
  9882. '$mod.Arr2[1] = [];',
  9883. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9884. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9885. '$mod.Fly({',
  9886. ' p: $mod,',
  9887. ' get: function () {',
  9888. ' return this.p.Arr;',
  9889. ' },',
  9890. ' set: function (v) {',
  9891. ' this.p.Arr = v;',
  9892. ' }',
  9893. '});',
  9894. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9895. '$mod.Fly({',
  9896. ' a: 7,',
  9897. ' p: $mod.Arr2,',
  9898. ' get: function () {',
  9899. ' return this.p[this.a];',
  9900. ' },',
  9901. ' set: function (v) {',
  9902. ' this.p[this.a] = v;',
  9903. ' }',
  9904. '});',
  9905. '']));
  9906. end;
  9907. procedure TTestModule.TestArray_StaticInt;
  9908. begin
  9909. StartProgram(false);
  9910. Add('type');
  9911. Add(' TArrayInt = array[2..4] of longint;');
  9912. Add('var');
  9913. Add(' Arr: TArrayInt;');
  9914. Add(' Arr2: TArrayInt = (5,6,7);');
  9915. Add(' i: longint;');
  9916. Add(' b: boolean;');
  9917. Add('begin');
  9918. Add(' arr[2]:=4;');
  9919. Add(' arr[3]:=arr[2]+arr[3];');
  9920. Add(' arr[i]:=5;');
  9921. Add(' arr[arr[i]]:=arr[high(arr)];');
  9922. Add(' i:=low(arr);');
  9923. Add(' i:=high(arr);');
  9924. Add(' b:=arr[2]=arr[3];');
  9925. Add(' arr:=default(TArrayInt);');
  9926. ConvertProgram;
  9927. CheckSource('TestArray_StaticInt',
  9928. LinesToStr([ // statements
  9929. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9930. 'this.Arr2 = [5, 6, 7];',
  9931. 'this.i = 0;',
  9932. 'this.b = false;'
  9933. ]),
  9934. LinesToStr([ // $mod.$main
  9935. '$mod.Arr[0] = 4;',
  9936. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9937. '$mod.Arr[$mod.i-2] = 5;',
  9938. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9939. '$mod.i = 2;',
  9940. '$mod.i = 4;',
  9941. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9942. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9943. '']));
  9944. end;
  9945. procedure TTestModule.TestArray_StaticBool;
  9946. begin
  9947. StartProgram(false);
  9948. Add('type');
  9949. Add(' TBools = array[boolean] of boolean;');
  9950. Add(' TBool2 = array[true..true] of boolean;');
  9951. Add('var');
  9952. Add(' Arr: TBools;');
  9953. Add(' Arr2: TBool2;');
  9954. Add(' Arr3: TBools = (true,false);');
  9955. Add(' b: boolean;');
  9956. Add('begin');
  9957. Add(' b:=low(arr);');
  9958. Add(' b:=high(arr);');
  9959. Add(' arr[true]:=false;');
  9960. Add(' arr[false]:=arr[b] or arr[true];');
  9961. Add(' arr[b]:=true;');
  9962. Add(' arr[arr[b]]:=arr[high(arr)];');
  9963. Add(' b:=arr[false]=arr[true];');
  9964. Add(' b:=low(arr2);');
  9965. Add(' b:=high(arr2);');
  9966. Add(' arr2[true]:=true;');
  9967. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9968. Add(' arr2[b]:=false;');
  9969. ConvertProgram;
  9970. CheckSource('TestArray_StaticBool',
  9971. LinesToStr([ // statements
  9972. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9973. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9974. 'this.Arr3 = [true, false];',
  9975. 'this.b = false;'
  9976. ]),
  9977. LinesToStr([ // $mod.$main
  9978. '$mod.b = false;',
  9979. '$mod.b = true;',
  9980. '$mod.Arr[1] = false;',
  9981. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9982. '$mod.Arr[+$mod.b] = true;',
  9983. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9984. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9985. '$mod.b = true;',
  9986. '$mod.b = true;',
  9987. '$mod.Arr2[0] = true;',
  9988. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9989. '$mod.Arr2[1-$mod.b] = false;',
  9990. '']));
  9991. end;
  9992. procedure TTestModule.TestArray_StaticChar;
  9993. begin
  9994. StartProgram(false);
  9995. Add([
  9996. 'type',
  9997. ' TChars = array[char] of char;',
  9998. ' TChars2 = array[''a''..''z''] of char;',
  9999. 'var',
  10000. ' Arr: TChars;',
  10001. ' Arr2: TChars2;',
  10002. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10003. ' Arr4: array[11..13] of char = ''pas'';',
  10004. ' Arr5: array[21..22] of char = ''äö'';',
  10005. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10006. ' c: char;',
  10007. ' b: boolean;',
  10008. 'begin',
  10009. ' c:=low(arr);',
  10010. ' c:=high(arr);',
  10011. ' arr[''B'']:=''a'';',
  10012. ' arr[''D'']:=arr[c];',
  10013. ' arr[c]:=arr[''d''];',
  10014. ' arr[arr[c]]:=arr[high(arr)];',
  10015. ' b:=arr[low(arr)]=arr[''e''];',
  10016. ' c:=low(arr2);',
  10017. ' c:=high(arr2);',
  10018. ' arr2[''b'']:=''f'';',
  10019. ' arr2[''a'']:=arr2[c];',
  10020. ' arr2[c]:=arr2[''g''];']);
  10021. ConvertProgram;
  10022. CheckSource('TestArray_StaticChar',
  10023. LinesToStr([ // statements
  10024. 'this.Arr = rtl.arraySetLength(null, "\x00", 65536);',
  10025. 'this.Arr2 = rtl.arraySetLength(null, "\x00", 26);',
  10026. 'this.Arr3 = ["p", "a", "s"];',
  10027. 'this.Arr4 = ["p", "a", "s"];',
  10028. 'this.Arr5 = ["ä", "ö"];',
  10029. 'this.Arr6 = ["ä", "ö"];',
  10030. 'this.c = "\x00";',
  10031. 'this.b = false;',
  10032. '']),
  10033. LinesToStr([ // $mod.$main
  10034. '$mod.c = "\x00";',
  10035. '$mod.c = "\uFFFF";',
  10036. '$mod.Arr[66] = "a";',
  10037. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10038. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10039. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10040. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10041. '$mod.c = "a";',
  10042. '$mod.c = "z";',
  10043. '$mod.Arr2[1] = "f";',
  10044. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10045. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10046. '']));
  10047. end;
  10048. procedure TTestModule.TestArray_StaticMultiDim;
  10049. begin
  10050. StartProgram(false);
  10051. Add([
  10052. 'type',
  10053. ' TArrayInt = array[1..3] of longint;',
  10054. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10055. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10056. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10057. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10058. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10059. 'var',
  10060. ' Arr: TArrayInt;',
  10061. ' Arr2: TArrayArrayInt;',
  10062. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10063. ' Arr4: TArrayArrayInt;',
  10064. ' ArrDim2: TArrayDim2Int;',
  10065. ' ArrDim3: TArrayDim3Int;',
  10066. ' ArrDim4: TArrayDim4Int;',
  10067. ' i: longint;',
  10068. 'begin',
  10069. ' i:=low(arr);',
  10070. ' i:=low(arr2);',
  10071. ' i:=low(arr2[5]);',
  10072. ' i:=high(arr);',
  10073. ' i:=high(arr2);',
  10074. ' i:=high(arr2[6]);',
  10075. ' arr2[5]:=arr;',
  10076. ' arr2[6][2]:=i;',
  10077. ' i:=arr2[6][3];',
  10078. ' arr2[6,3]:=i;',
  10079. ' i:=arr2[5,2];',
  10080. ' arr2:=arr2;',// clone multi dim static array
  10081. ' arr3:=arr3;',// clone anonymous multi dim static array
  10082. ' arr4:=arr4;',
  10083. ' Arr:=Arr;',
  10084. ' ArrDim2:=ArrDim2;',
  10085. ' ArrDim3:=ArrDim3;',
  10086. ' ArrDim4:=ArrDim4;',
  10087. '']);
  10088. ConvertProgram;
  10089. CheckSource('TestArray_StaticMultiDim',
  10090. LinesToStr([ // statements
  10091. 'this.TArrayArrayInt$clone = function (a) {',
  10092. ' var b = [];',
  10093. ' b.length = 2;',
  10094. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10095. ' return b;',
  10096. '};',
  10097. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10098. ' var b = [];',
  10099. ' b.length = 2;',
  10100. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10101. ' return b;',
  10102. '};',
  10103. 'this.TArrayDim2Int$clone = function (a) {',
  10104. ' var b = [];',
  10105. ' b.length = 2;',
  10106. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10107. ' return b;',
  10108. '};',
  10109. 'this.TArrayDim3Int$clone = function (a) {',
  10110. ' var b = [];',
  10111. ' b.length = 2;',
  10112. ' for (var c = 0; c < 2; c++) {',
  10113. ' var d = b[c] = [];',
  10114. ' d.length = 3;',
  10115. ' var e = a[c];',
  10116. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10117. ' };',
  10118. ' return b;',
  10119. '};',
  10120. 'this.TArrayDim4Int$clone = function (a) {',
  10121. ' var b = [];',
  10122. ' b.length = 2;',
  10123. ' for (var c = 0; c < 2; c++) {',
  10124. ' var d = b[c] = [];',
  10125. ' d.length = 3;',
  10126. ' var e = a[c];',
  10127. ' for (var f = 0; f < 3; f++) {',
  10128. ' var g = d[f] = [];',
  10129. ' g.length = 4;',
  10130. ' var h = e[f];',
  10131. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10132. ' };',
  10133. ' };',
  10134. ' return b;',
  10135. '};',
  10136. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10137. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10138. 'this.Arr3$a$clone = function (a) {',
  10139. ' var b = [];',
  10140. ' b.length = 2;',
  10141. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10142. ' return b;',
  10143. '};',
  10144. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10145. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10146. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10147. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10148. 'this.ArrDim4 = rtl.arraySetLength(',
  10149. ' null,',
  10150. ' 0,',
  10151. ' 2,',
  10152. ' 3,',
  10153. ' 4,',
  10154. ' 5',
  10155. ');',
  10156. 'this.i = 0;'
  10157. ]),
  10158. LinesToStr([ // $mod.$main
  10159. '$mod.i = 1;',
  10160. '$mod.i = 5;',
  10161. '$mod.i = 1;',
  10162. '$mod.i = 3;',
  10163. '$mod.i = 6;',
  10164. '$mod.i = 3;',
  10165. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10166. '$mod.Arr2[1][1] = $mod.i;',
  10167. '$mod.i = $mod.Arr2[1][2];',
  10168. '$mod.Arr2[1][2] = $mod.i;',
  10169. '$mod.i = $mod.Arr2[0][1];',
  10170. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10171. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10172. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10173. '$mod.Arr = $mod.Arr.slice(0);',
  10174. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10175. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10176. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10177. '']));
  10178. end;
  10179. procedure TTestModule.TestArray_StaticInFunction;
  10180. begin
  10181. StartProgram(false);
  10182. Add([
  10183. 'const TArrayInt = 3;',
  10184. 'const TArrayArrayInt = 4;',
  10185. 'procedure DoIt;',
  10186. 'type',
  10187. ' TArrayInt = array[1..3] of longint;',
  10188. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10189. 'var',
  10190. ' Arr: TArrayInt;',
  10191. ' Arr2: TArrayArrayInt;',
  10192. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10193. ' i: longint;',
  10194. 'begin',
  10195. ' arr2[5]:=arr;',
  10196. ' arr2:=arr2;',// clone multi dim static array
  10197. ' arr3:=arr3;',// clone multi dim anonymous static array
  10198. 'end;',
  10199. 'begin',
  10200. '']);
  10201. ConvertProgram;
  10202. CheckSource('TestArray_StaticInFunction',
  10203. LinesToStr([ // statements
  10204. 'this.TArrayInt = 3;',
  10205. 'this.TArrayArrayInt = 4;',
  10206. 'var TArrayArrayInt$1$clone = function (a) {',
  10207. ' var b = [];',
  10208. ' b.length = 2;',
  10209. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10210. ' return b;',
  10211. '};',
  10212. 'var Arr3$a$clone = function (a) {',
  10213. ' var b = [];',
  10214. ' b.length = 2;',
  10215. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10216. ' return b;',
  10217. '};',
  10218. 'this.DoIt = function () {',
  10219. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10220. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10221. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10222. ' var i = 0;',
  10223. ' Arr2[0] = Arr.slice(0);',
  10224. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10225. ' Arr3 = Arr3$a$clone(Arr3);',
  10226. '};',
  10227. '']),
  10228. LinesToStr([ // $mod.$main
  10229. '']));
  10230. end;
  10231. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10232. begin
  10233. StartProgram(false);
  10234. Add([
  10235. 'type',
  10236. ' TArrayInt = array[1..3,1..2] of longint;',
  10237. 'var',
  10238. ' a,b: TArrayInt;',
  10239. 'begin',
  10240. ' if a=b then ;',
  10241. '']);
  10242. SetExpectedPasResolverError('compare static array is not supported',
  10243. nXIsNotSupported);
  10244. ConvertProgram;
  10245. end;
  10246. procedure TTestModule.TestArrayOfRecord;
  10247. begin
  10248. StartProgram(false);
  10249. Add([
  10250. 'type',
  10251. ' TRec = record',
  10252. ' Int: longint;',
  10253. ' end;',
  10254. ' TArrayRec = array of TRec;',
  10255. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10256. 'begin',
  10257. 'end;',
  10258. 'var',
  10259. ' Arr: TArrayRec;',
  10260. ' r: TRec;',
  10261. ' i: longint;',
  10262. 'begin',
  10263. ' SetLength(arr,3);',
  10264. ' arr[0].int:=4;',
  10265. ' arr[1].int:=length(arr)+arr[2].int;',
  10266. ' arr[arr[i].int].int:=arr[5].int;',
  10267. ' arr[7]:=r;',
  10268. ' r:=arr[8];',
  10269. ' i:=low(arr);',
  10270. ' i:=high(arr);',
  10271. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10272. ConvertProgram;
  10273. CheckSource('TestArrayOfRecord',
  10274. LinesToStr([ // statements
  10275. 'rtl.recNewT(this, "TRec", function () {',
  10276. ' this.Int = 0;',
  10277. ' this.$eq = function (b) {',
  10278. ' return this.Int === b.Int;',
  10279. ' };',
  10280. ' this.$assign = function (s) {',
  10281. ' this.Int = s.Int;',
  10282. ' return this;',
  10283. ' };',
  10284. '});',
  10285. 'this.DoIt = function (vd, vc, vv) {',
  10286. '};',
  10287. 'this.Arr = [];',
  10288. 'this.r = this.TRec.$new();',
  10289. 'this.i = 0;'
  10290. ]),
  10291. LinesToStr([ // $mod.$main
  10292. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10293. '$mod.Arr[0].Int = 4;',
  10294. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10295. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10296. '$mod.Arr[7].$assign($mod.r);',
  10297. '$mod.r.$assign($mod.Arr[8]);',
  10298. '$mod.i = 0;',
  10299. '$mod.i = rtl.length($mod.Arr)-1;',
  10300. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10301. '']));
  10302. end;
  10303. procedure TTestModule.TestArray_StaticRecord;
  10304. begin
  10305. StartProgram(false);
  10306. Add([
  10307. 'type',
  10308. ' TRec = record',
  10309. ' Int: longint;',
  10310. ' end;',
  10311. ' TArrayRec = array[1..2] of TRec;',
  10312. 'var',
  10313. ' Arr: TArrayRec;',
  10314. 'begin',
  10315. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10316. '']);
  10317. ConvertProgram;
  10318. CheckSource('TestArray_StaticRecord',
  10319. LinesToStr([ // statements
  10320. 'rtl.recNewT(this, "TRec", function () {',
  10321. ' this.Int = 0;',
  10322. ' this.$eq = function (b) {',
  10323. ' return this.Int === b.Int;',
  10324. ' };',
  10325. ' this.$assign = function (s) {',
  10326. ' this.Int = s.Int;',
  10327. ' return this;',
  10328. ' };',
  10329. '});',
  10330. 'this.TArrayRec$clone = function (a) {',
  10331. ' var b = [];',
  10332. ' b.length = 2;',
  10333. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10334. ' return b;',
  10335. '};',
  10336. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10337. '']),
  10338. LinesToStr([ // $mod.$main
  10339. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10340. end;
  10341. procedure TTestModule.TestArrayOfSet;
  10342. begin
  10343. StartProgram(false);
  10344. Add([
  10345. 'type',
  10346. ' TFlag = (big,small);',
  10347. ' TSetOfFlag = set of tflag;',
  10348. ' TArrayFlag = array of TSetOfFlag;',
  10349. 'procedure DoIt(const a: Tarrayflag);',
  10350. 'begin',
  10351. 'end;',
  10352. 'var',
  10353. ' f: TFlag;',
  10354. ' s: TSetOfFlag;',
  10355. ' Arr: TArrayFlag;',
  10356. ' i: longint;',
  10357. 'begin',
  10358. ' SetLength(arr,3);',
  10359. ' arr[0]:=s;',
  10360. ' arr[1]:=[big];',
  10361. ' arr[2]:=[big]+s;',
  10362. ' arr[3]:=s+[big];',
  10363. ' arr[4]:=arr[5];',
  10364. ' s:=arr[6];',
  10365. ' i:=low(arr);',
  10366. ' i:=high(arr);',
  10367. ' DoIt(arr);',
  10368. ' DoIt([s]);',
  10369. ' DoIt([[],s]);',
  10370. ' DoIt([s,[]]);',
  10371. '']);
  10372. ConvertProgram;
  10373. CheckSource('TestArrayOfSet',
  10374. LinesToStr([ // statements
  10375. 'this.TFlag = {',
  10376. ' "0": "big",',
  10377. ' big: 0,',
  10378. ' "1": "small",',
  10379. ' small: 1',
  10380. '};',
  10381. 'this.DoIt = function (a) {',
  10382. '};',
  10383. 'this.f = 0;',
  10384. 'this.s = {};',
  10385. 'this.Arr = [];',
  10386. 'this.i = 0;',
  10387. '']),
  10388. LinesToStr([ // $mod.$main
  10389. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10390. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10391. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10392. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10393. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10394. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10395. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10396. '$mod.i = 0;',
  10397. '$mod.i = rtl.length($mod.Arr) - 1;',
  10398. '$mod.DoIt($mod.Arr);',
  10399. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10400. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10401. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10402. '']));
  10403. end;
  10404. procedure TTestModule.TestArray_DynAsParam;
  10405. begin
  10406. StartProgram(false);
  10407. Add([
  10408. 'type integer = longint;',
  10409. 'type TArrInt = array of integer;',
  10410. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10411. 'var vJ: TArrInt;',
  10412. 'begin',
  10413. ' vg:=vg;',
  10414. ' vj:=vh;',
  10415. ' vi:=vi;',
  10416. ' doit(vg,vg,vg);',
  10417. ' doit(vh,vh,vj);',
  10418. ' doit(vi,vi,vi);',
  10419. ' doit(vj,vj,vj);',
  10420. 'end;',
  10421. 'var i: TArrInt;',
  10422. 'begin',
  10423. ' doit(i,i,i);']);
  10424. ConvertProgram;
  10425. CheckSource('TestArray_DynAsParams',
  10426. LinesToStr([ // statements
  10427. 'this.DoIt = function (vG,vH,vI) {',
  10428. ' var vJ = [];',
  10429. ' vG = rtl.arrayRef(vG);',
  10430. ' vJ = rtl.arrayRef(vH);',
  10431. ' vI.set(rtl.arrayRef(vI.get()));',
  10432. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10433. ' get: function () {',
  10434. ' return vG;',
  10435. ' },',
  10436. ' set: function (v) {',
  10437. ' vG = v;',
  10438. ' }',
  10439. ' });',
  10440. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10441. ' get: function () {',
  10442. ' return vJ;',
  10443. ' },',
  10444. ' set: function (v) {',
  10445. ' vJ = v;',
  10446. ' }',
  10447. ' });',
  10448. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10449. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10450. ' get: function () {',
  10451. ' return vJ;',
  10452. ' },',
  10453. ' set: function (v) {',
  10454. ' vJ = v;',
  10455. ' }',
  10456. ' });',
  10457. '};',
  10458. 'this.i = [];'
  10459. ]),
  10460. LinesToStr([
  10461. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10462. ' p: $mod,',
  10463. ' get: function () {',
  10464. ' return this.p.i;',
  10465. ' },',
  10466. ' set: function (v) {',
  10467. ' this.p.i = v;',
  10468. ' }',
  10469. '});'
  10470. ]));
  10471. end;
  10472. procedure TTestModule.TestArray_StaticAsParam;
  10473. begin
  10474. StartProgram(false);
  10475. Add([
  10476. 'type integer = longint;',
  10477. 'type TArrInt = array[1..2] of integer;',
  10478. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10479. 'var vJ: TArrInt;',
  10480. 'begin',
  10481. ' vg:=vg;',
  10482. ' vj:=vh;',
  10483. ' vi:=vi;',
  10484. ' doit(vg,vg,vg);',
  10485. ' doit(vh,vh,vj);',
  10486. ' doit(vi,vi,vi);',
  10487. ' doit(vj,vj,vj);',
  10488. 'end;',
  10489. 'var i: TArrInt;',
  10490. 'begin',
  10491. ' doit(i,i,i);']);
  10492. ConvertProgram;
  10493. CheckSource('TestArray_StaticAsParams',
  10494. LinesToStr([ // statements
  10495. 'this.DoIt = function (vG,vH,vI) {',
  10496. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10497. ' vG = vG.slice(0);',
  10498. ' vJ = vH.slice(0);',
  10499. ' vI.set(vI.get().slice(0));',
  10500. ' $mod.DoIt(vG.slice(0), vG, {',
  10501. ' get: function () {',
  10502. ' return vG;',
  10503. ' },',
  10504. ' set: function (v) {',
  10505. ' vG = v;',
  10506. ' }',
  10507. ' });',
  10508. ' $mod.DoIt(vH.slice(0), vH, {',
  10509. ' get: function () {',
  10510. ' return vJ;',
  10511. ' },',
  10512. ' set: function (v) {',
  10513. ' vJ = v;',
  10514. ' }',
  10515. ' });',
  10516. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10517. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10518. ' get: function () {',
  10519. ' return vJ;',
  10520. ' },',
  10521. ' set: function (v) {',
  10522. ' vJ = v;',
  10523. ' }',
  10524. ' });',
  10525. '};',
  10526. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10527. ]),
  10528. LinesToStr([
  10529. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10530. ' p: $mod,',
  10531. ' get: function () {',
  10532. ' return this.p.i;',
  10533. ' },',
  10534. ' set: function (v) {',
  10535. ' this.p.i = v;',
  10536. ' }',
  10537. '});'
  10538. ]));
  10539. end;
  10540. procedure TTestModule.TestArrayElement_AsParams;
  10541. begin
  10542. StartProgram(false);
  10543. Add('type integer = longint;');
  10544. Add('type TArrayInt = array of integer;');
  10545. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10546. Add('var vJ: tarrayint;');
  10547. Add('begin');
  10548. Add(' vi:=vi;');
  10549. Add(' doit(vi,vi,vi);');
  10550. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10551. Add('end;');
  10552. Add('var a: TArrayInt;');
  10553. Add('begin');
  10554. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10555. ConvertProgram;
  10556. CheckSource('TestArrayElement_AsParams',
  10557. LinesToStr([ // statements
  10558. 'this.DoIt = function (vG,vH,vI) {',
  10559. ' var vJ = [];',
  10560. ' vI.set(vI.get());',
  10561. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10562. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10563. ' a:1+3,',
  10564. ' p:vJ,',
  10565. ' get: function () {',
  10566. ' return this.p[this.a];',
  10567. ' },',
  10568. ' set: function (v) {',
  10569. ' this.p[this.a] = v;',
  10570. ' }',
  10571. ' });',
  10572. '};',
  10573. 'this.a = [];'
  10574. ]),
  10575. LinesToStr([
  10576. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10577. ' a: 1+6,',
  10578. ' p: $mod.a,',
  10579. ' get: function () {',
  10580. ' return this.p[this.a];',
  10581. ' },',
  10582. ' set: function (v) {',
  10583. ' this.p[this.a] = v;',
  10584. ' }',
  10585. '});'
  10586. ]));
  10587. end;
  10588. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10589. begin
  10590. StartProgram(false);
  10591. Add('type Integer = longint;');
  10592. Add('type TArrayInt = array of integer;');
  10593. Add('function GetArr(vB: integer = 0): tarrayint;');
  10594. Add('begin');
  10595. Add('end;');
  10596. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10597. Add('begin');
  10598. Add('end;');
  10599. Add('begin');
  10600. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10601. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10602. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10603. ConvertProgram;
  10604. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10605. LinesToStr([ // statements
  10606. 'this.GetArr = function (vB) {',
  10607. ' var Result = [];',
  10608. ' return Result;',
  10609. '};',
  10610. 'this.DoIt = function (vG,vH,vI) {',
  10611. '};'
  10612. ]),
  10613. LinesToStr([
  10614. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10615. ' a: 1+3,',
  10616. ' p: $mod.GetArr(0),',
  10617. ' get: function () {',
  10618. ' return this.p[this.a];',
  10619. ' },',
  10620. ' set: function (v) {',
  10621. ' this.p[this.a] = v;',
  10622. ' }',
  10623. '});',
  10624. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10625. ' a: 2+3,',
  10626. ' p: $mod.GetArr(0),',
  10627. ' get: function () {',
  10628. ' return this.p[this.a];',
  10629. ' },',
  10630. ' set: function (v) {',
  10631. ' this.p[this.a] = v;',
  10632. ' }',
  10633. '});',
  10634. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10635. ' a: 3+3,',
  10636. ' p: $mod.GetArr(9),',
  10637. ' get: function () {',
  10638. ' return this.p[this.a];',
  10639. ' },',
  10640. ' set: function (v) {',
  10641. ' this.p[this.a] = v;',
  10642. ' }',
  10643. '});',
  10644. '']));
  10645. end;
  10646. procedure TTestModule.TestArrayEnumTypeRange;
  10647. begin
  10648. StartProgram(false);
  10649. Add([
  10650. 'type',
  10651. ' TEnum = (red,blue);',
  10652. ' TEnumArray = array[TEnum] of longint;',
  10653. 'var',
  10654. ' e: TEnum;',
  10655. ' i: longint;',
  10656. ' a: TEnumArray;',
  10657. ' numbers: TEnumArray = (1,2);',
  10658. ' names: array[TEnum] of string = (''red'',''blue'');',
  10659. 'begin',
  10660. ' e:=low(a);',
  10661. ' e:=high(a);',
  10662. ' i:=a[red];',
  10663. ' a[e]:=a[e];']);
  10664. ConvertProgram;
  10665. CheckSource('TestArrayEnumTypeRange',
  10666. LinesToStr([ // statements
  10667. ' this.TEnum = {',
  10668. ' "0": "red",',
  10669. ' red: 0,',
  10670. ' "1": "blue",',
  10671. ' blue: 1',
  10672. '};',
  10673. 'this.e = 0;',
  10674. 'this.i = 0;',
  10675. 'this.a = rtl.arraySetLength(null,0,2);',
  10676. 'this.numbers = [1, 2];',
  10677. 'this.names = ["red", "blue"];',
  10678. '']),
  10679. LinesToStr([ // $mod.$main
  10680. '$mod.e = $mod.TEnum.red;',
  10681. '$mod.e = $mod.TEnum.blue;',
  10682. '$mod.i = $mod.a[$mod.TEnum.red];',
  10683. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10684. '']));
  10685. end;
  10686. procedure TTestModule.TestArray_SetLengthOutArg;
  10687. begin
  10688. StartProgram(false);
  10689. Add([
  10690. 'type TArrInt = array of longint;',
  10691. 'procedure DoIt(out a: TArrInt);',
  10692. 'begin',
  10693. ' SetLength(a,2);',
  10694. 'end;',
  10695. 'begin',
  10696. '']);
  10697. ConvertProgram;
  10698. CheckSource('TestArray_SetLengthOutArg',
  10699. LinesToStr([ // statements
  10700. 'this.DoIt = function (a) {',
  10701. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10702. '};',
  10703. '']),
  10704. LinesToStr([
  10705. '']));
  10706. end;
  10707. procedure TTestModule.TestArray_SetLengthProperty;
  10708. begin
  10709. StartProgram(false);
  10710. Add('type');
  10711. Add(' TArrInt = array of longint;');
  10712. Add(' TObject = class');
  10713. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10714. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10715. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10716. Add(' end;');
  10717. Add('var Obj: TObject;');
  10718. Add('begin');
  10719. Add(' SetLength(Obj.Colors,2);');
  10720. ConvertProgram;
  10721. CheckSource('TestArray_SetLengthProperty',
  10722. LinesToStr([ // statements
  10723. 'rtl.createClass(this, "TObject", null, function () {',
  10724. ' this.$init = function () {',
  10725. ' };',
  10726. ' this.$final = function () {',
  10727. ' };',
  10728. '});',
  10729. 'this.Obj = null;',
  10730. '']),
  10731. LinesToStr([
  10732. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10733. '']));
  10734. end;
  10735. procedure TTestModule.TestArray_SetLengthMultiDim;
  10736. begin
  10737. StartProgram(false);
  10738. Add([
  10739. 'type',
  10740. ' TArrArrInt = array of array of longint;',
  10741. ' TArrStaInt = array of array[1..2] of longint;',
  10742. 'var',
  10743. ' a: TArrArrInt;',
  10744. ' b: TArrStaInt;',
  10745. 'begin',
  10746. ' SetLength(a,2);',
  10747. ' SetLength(a,3,4);',
  10748. ' SetLength(b,5);',
  10749. '']);
  10750. ConvertProgram;
  10751. CheckSource('TestArray_SetLengthMultiDim',
  10752. LinesToStr([ // statements
  10753. 'this.a = [];',
  10754. 'this.b = [];',
  10755. '']),
  10756. LinesToStr([
  10757. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10758. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10759. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10760. '']));
  10761. end;
  10762. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10763. begin
  10764. StartProgram(false);
  10765. Add([
  10766. 'type',
  10767. ' TStaArr1 = array[1..3] of boolean;',
  10768. //' TStaArr2 = array[5..6] of TStaArr1;',
  10769. ' TDynArr1StaArr1 = array of TStaArr1;',
  10770. //' TDynArr1StaArr2 = array of TStaArr2;',
  10771. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10772. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10773. 'var',
  10774. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10775. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10776. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10777. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10778. 'begin',
  10779. ' SetLength(DynArr1StaArr1,11);',
  10780. ' SetLength(DynArr2StaArr1,12);',
  10781. ' SetLength(DynArr2StaArr1[13],14);',
  10782. ' SetLength(DynArr2StaArr1,15,16);',
  10783. //' SetLength(DynArr1StaArr2,21);',
  10784. //' SetLength(DynArr2StaArr2,22);',
  10785. //' SetLength(DynArr2StaArr2[23],24);',
  10786. //' SetLength(DynArr2StaArr2,25,26);',
  10787. '']);
  10788. ConvertProgram;
  10789. CheckSource('TestArray_DynOfStatic',
  10790. LinesToStr([ // statements
  10791. 'this.DynArr1StaArr1 = [];',
  10792. 'this.DynArr2StaArr1 = [];',
  10793. '']),
  10794. LinesToStr([ // $mod.$main
  10795. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10796. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10797. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10798. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10799. ' $mod.DynArr2StaArr1,',
  10800. ' false,',
  10801. ' 15,',
  10802. ' 16,',
  10803. ' "s",',
  10804. ' 3',
  10805. ');',
  10806. '']));
  10807. end;
  10808. procedure TTestModule.TestArray_OpenArrayOfString;
  10809. begin
  10810. StartProgram(false);
  10811. Add('procedure DoIt(const a: array of String);');
  10812. Add('var');
  10813. Add(' i: longint;');
  10814. Add(' s: string;');
  10815. Add('begin');
  10816. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10817. Add('end;');
  10818. Add('var s: string;');
  10819. Add('begin');
  10820. Add(' DoIt([]);');
  10821. Add(' DoIt([s,''foo'','''',s+s]);');
  10822. ConvertProgram;
  10823. CheckSource('TestArray_OpenArrayOfString',
  10824. LinesToStr([ // statements
  10825. 'this.DoIt = function (a) {',
  10826. ' var i = 0;',
  10827. ' var s = "";',
  10828. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10829. ' i = $l;',
  10830. ' s = a[rtl.length(a) - i - 1];',
  10831. ' };',
  10832. '};',
  10833. 'this.s = "";',
  10834. '']),
  10835. LinesToStr([
  10836. '$mod.DoIt([]);',
  10837. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10838. '']));
  10839. end;
  10840. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10841. begin
  10842. StartProgram(false);
  10843. Add([
  10844. 'type TArr = array of char;',
  10845. 'var',
  10846. ' c: char;',
  10847. ' s: string;',
  10848. ' a: TArr;',
  10849. 'procedure Run(const a: array of char);',
  10850. 'begin',
  10851. ' Run(c);',
  10852. ' Run(s);',
  10853. 'end;',
  10854. 'begin',
  10855. ' a:=c;',
  10856. ' a:=s;',
  10857. ' a:=#13;',
  10858. ' a:=''Foo'';',
  10859. ' Run(c);',
  10860. ' Run(s);',
  10861. '']);
  10862. ConvertProgram;
  10863. CheckSource('TestArray_ArrayOfCharAssignString',
  10864. LinesToStr([ // statements
  10865. 'this.c = "\x00";',
  10866. 'this.s = "";',
  10867. 'this.a = [];',
  10868. 'this.Run = function (a) {',
  10869. ' $mod.Run($mod.c.split(""));',
  10870. ' $mod.Run($mod.s.split(""));',
  10871. '};',
  10872. '']),
  10873. LinesToStr([
  10874. '$mod.a = $mod.c.split("");',
  10875. '$mod.a = $mod.s.split("");',
  10876. '$mod.a = "\r".split("");',
  10877. '$mod.a = "Foo".split("");',
  10878. '$mod.Run($mod.c.split(""));',
  10879. '$mod.Run($mod.s.split(""));',
  10880. '']));
  10881. end;
  10882. procedure TTestModule.TestArray_ConstRef;
  10883. begin
  10884. StartProgram(false);
  10885. Add([
  10886. 'type TArr = array of word;',
  10887. 'procedure Run(constref a: TArr);',
  10888. 'begin',
  10889. 'end;',
  10890. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10891. 'var l: TArr;',
  10892. 'begin',
  10893. ' Run(l);',
  10894. ' Run(a);',
  10895. ' Run(b);',
  10896. ' Run(c);',
  10897. ' Run(d);',
  10898. ' Run(e);',
  10899. 'end;',
  10900. 'begin',
  10901. '']);
  10902. ConvertProgram;
  10903. CheckResolverUnexpectedHints();
  10904. CheckSource('TestArray_ConstRef',
  10905. LinesToStr([ // statements
  10906. 'this.Run = function (a) {',
  10907. '};',
  10908. 'this.Fly = function (a, b, c, d, e) {',
  10909. ' var l = [];',
  10910. ' $mod.Run(l);',
  10911. ' $mod.Run(a);',
  10912. ' $mod.Run(b.get());',
  10913. ' $mod.Run(c.get());',
  10914. ' $mod.Run(d);',
  10915. ' $mod.Run(e);',
  10916. '};',
  10917. '']),
  10918. LinesToStr([
  10919. '']));
  10920. end;
  10921. procedure TTestModule.TestArray_Concat;
  10922. begin
  10923. StartProgram(false);
  10924. Add([
  10925. 'type',
  10926. ' integer = longint;',
  10927. ' TFlag = (big,small);',
  10928. ' TFlags = set of TFlag;',
  10929. ' TRec = record',
  10930. ' i: integer;',
  10931. ' end;',
  10932. ' TArrInt = array of integer;',
  10933. ' TArrRec = array of TRec;',
  10934. ' TArrFlag = array of TFlag;',
  10935. ' TArrSet = array of TFlags;',
  10936. ' TArrJSValue = array of jsvalue;',
  10937. 'var',
  10938. ' ArrInt1, ArrInt2: tarrint;',
  10939. ' ArrRec1, ArrRec2: tarrrec;',
  10940. ' ArrFlag1, ArrFlag2: tarrflag;',
  10941. ' ArrSet1, ArrSet2: tarrset;',
  10942. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10943. 'begin',
  10944. ' arrint1:=concat(arrint2);',
  10945. ' arrint1:=concat(arrint2,arrint2);',
  10946. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10947. ' arrrec1:=concat(arrrec2);',
  10948. ' arrrec1:=concat(arrrec2,arrrec2);',
  10949. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10950. ' arrset1:=concat(arrset2);',
  10951. ' arrset1:=concat(arrset2,arrset2);',
  10952. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10953. ' arrjsvalue1:=concat(arrjsvalue2);',
  10954. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10955. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10956. ' arrint1:=concat([1],arrint2);',
  10957. ' arrflag1:=concat([big]);',
  10958. ' arrflag1:=concat([big],arrflag2);',
  10959. ' arrflag1:=concat(arrflag2,[small]);',
  10960. '']);
  10961. ConvertProgram;
  10962. CheckSource('TestArray_Concat',
  10963. LinesToStr([ // statements
  10964. 'this.TFlag = {',
  10965. ' "0": "big",',
  10966. ' big: 0,',
  10967. ' "1": "small",',
  10968. ' small: 1',
  10969. '};',
  10970. 'rtl.recNewT(this, "TRec", function () {',
  10971. ' this.i = 0;',
  10972. ' this.$eq = function (b) {',
  10973. ' return this.i === b.i;',
  10974. ' };',
  10975. ' this.$assign = function (s) {',
  10976. ' this.i = s.i;',
  10977. ' return this;',
  10978. ' };',
  10979. '});',
  10980. 'this.ArrInt1 = [];',
  10981. 'this.ArrInt2 = [];',
  10982. 'this.ArrRec1 = [];',
  10983. 'this.ArrRec2 = [];',
  10984. 'this.ArrFlag1 = [];',
  10985. 'this.ArrFlag2 = [];',
  10986. 'this.ArrSet1 = [];',
  10987. 'this.ArrSet2 = [];',
  10988. 'this.ArrJSValue1 = [];',
  10989. 'this.ArrJSValue2 = [];',
  10990. '']),
  10991. LinesToStr([ // $mod.$main
  10992. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10993. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10994. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10995. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10996. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10997. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10998. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10999. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11000. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11001. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11002. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11003. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11004. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11005. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11006. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11007. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11008. '']));
  11009. end;
  11010. procedure TTestModule.TestArray_Concat_Append;
  11011. begin
  11012. StartProgram(false);
  11013. Add([
  11014. 'type',
  11015. ' integer = longint;',
  11016. ' TFlag = (big,small);',
  11017. ' TFlags = set of TFlag;',
  11018. ' TRec = record',
  11019. ' i: integer;',
  11020. ' end;',
  11021. ' TArrInt = array of integer;',
  11022. ' TArrRec = array of TRec;',
  11023. ' TArrFlag = array of TFlag;',
  11024. ' TArrSet = array of TFlags;',
  11025. ' TArrJSValue = array of jsvalue;',
  11026. 'var',
  11027. ' ArrInt: tarrint;',
  11028. ' ArrRec: tarrrec;',
  11029. ' ArrFlag: tarrflag;',
  11030. ' ArrSet: tarrset;',
  11031. ' ArrJSValue: tarrjsvalue;',
  11032. ' r: TRec;',
  11033. ' f: TFlags;',
  11034. 'begin',
  11035. ' // append',
  11036. ' arrint:=concat(arrint);',
  11037. ' arrint:=concat(arrint,[2]);',
  11038. ' arrint:=concat(arrint,[3,4]);',
  11039. ' arrrec:=concat(arrrec);',
  11040. ' arrrec:=concat(arrrec,[r]);',
  11041. ' arrrec:=concat(arrrec,[r,r]);',
  11042. ' arrset:=concat(arrset);',
  11043. ' arrset:=concat(arrset,[f]);',
  11044. ' arrset:=concat(arrset,[f,f]);',
  11045. ' arrjsvalue:=concat(arrjsvalue);',
  11046. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11047. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11048. ' arrflag:=concat(arrflag);',
  11049. ' arrflag:=concat(arrflag,[small]);',
  11050. ' arrflag:=concat(arrflag,[small,big]);',
  11051. '']);
  11052. ConvertProgram;
  11053. CheckSource('TestArray_Concat_Append',
  11054. LinesToStr([ // statements
  11055. 'this.TFlag = {',
  11056. ' "0": "big",',
  11057. ' big: 0,',
  11058. ' "1": "small",',
  11059. ' small: 1',
  11060. '};',
  11061. 'rtl.recNewT(this, "TRec", function () {',
  11062. ' this.i = 0;',
  11063. ' this.$eq = function (b) {',
  11064. ' return this.i === b.i;',
  11065. ' };',
  11066. ' this.$assign = function (s) {',
  11067. ' this.i = s.i;',
  11068. ' return this;',
  11069. ' };',
  11070. '});',
  11071. 'this.ArrInt = [];',
  11072. 'this.ArrRec = [];',
  11073. 'this.ArrFlag = [];',
  11074. 'this.ArrSet = [];',
  11075. 'this.ArrJSValue = [];',
  11076. 'this.r = this.TRec.$new();',
  11077. 'this.f = {};',
  11078. '']),
  11079. LinesToStr([ // $mod.$main
  11080. '$mod.ArrInt = $mod.ArrInt;',
  11081. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11082. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11083. '$mod.ArrRec = $mod.ArrRec;',
  11084. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11085. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11086. '$mod.ArrSet = $mod.ArrSet;',
  11087. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11088. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11089. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11090. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11091. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11092. '$mod.ArrFlag = $mod.ArrFlag;',
  11093. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11094. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11095. '']));
  11096. end;
  11097. procedure TTestModule.TestArray_Concat_Append_Var;
  11098. begin
  11099. StartProgram(false);
  11100. Add([
  11101. 'type',
  11102. ' TArrInt = array of word;',
  11103. '',
  11104. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11105. 'begin',
  11106. ' a:=concat(a,[2]);',
  11107. ' b:=concat(b,[2]);',
  11108. 'end;',
  11109. 'var',
  11110. ' ArrInt: tarrint;',
  11111. 'begin',
  11112. ' Fly(ArrInt,ArrInt);',
  11113. '']);
  11114. ConvertProgram;
  11115. CheckSource('TestArray_Concat_Append_Var',
  11116. LinesToStr([ // statements
  11117. 'this.Fly = function (a, b) {',
  11118. ' a = rtl.arrayPushN(a, 2);',
  11119. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11120. '};',
  11121. 'this.ArrInt = [];',
  11122. '']),
  11123. LinesToStr([ // $mod.$main
  11124. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11125. ' p: $mod,',
  11126. ' get: function () {',
  11127. ' return this.p.ArrInt;',
  11128. ' },',
  11129. ' set: function (v) {',
  11130. ' this.p.ArrInt = v;',
  11131. ' }',
  11132. '});',
  11133. '']));
  11134. end;
  11135. procedure TTestModule.TestArray_Copy;
  11136. begin
  11137. StartProgram(false);
  11138. Add([
  11139. 'type',
  11140. ' integer = longint;',
  11141. ' TFlag = (big,small);',
  11142. ' TFlags = set of TFlag;',
  11143. ' TRec = record',
  11144. ' i: integer;',
  11145. ' end;',
  11146. ' TArrInt = array of integer;',
  11147. ' TArrRec = array of TRec;',
  11148. ' TArrSet = array of TFlags;',
  11149. ' TArrJSValue = array of jsvalue;',
  11150. 'var',
  11151. ' ArrInt: tarrint;',
  11152. ' ArrRec: tarrrec;',
  11153. ' ArrSet: tarrset;',
  11154. ' ArrJSValue: tarrjsvalue;',
  11155. 'begin',
  11156. ' arrint:=copy(arrint);',
  11157. ' arrint:=copy(arrint,2);',
  11158. ' arrint:=copy(arrint,3,4);',
  11159. ' arrint:=copy([1,1],1,2);',
  11160. ' arrrec:=copy(arrrec);',
  11161. ' arrrec:=copy(arrrec,5);',
  11162. ' arrrec:=copy(arrrec,6,7);',
  11163. ' arrset:=copy(arrset);',
  11164. ' arrset:=copy(arrset,8);',
  11165. ' arrset:=copy(arrset,9,10);',
  11166. ' arrjsvalue:=copy(arrjsvalue);',
  11167. ' arrjsvalue:=copy(arrjsvalue,11);',
  11168. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11169. ' ']);
  11170. ConvertProgram;
  11171. CheckSource('TestArray_Copy',
  11172. LinesToStr([ // statements
  11173. 'this.TFlag = {',
  11174. ' "0": "big",',
  11175. ' big: 0,',
  11176. ' "1": "small",',
  11177. ' small: 1',
  11178. '};',
  11179. 'rtl.recNewT(this, "TRec", function () {',
  11180. ' this.i = 0;',
  11181. ' this.$eq = function (b) {',
  11182. ' return this.i === b.i;',
  11183. ' };',
  11184. ' this.$assign = function (s) {',
  11185. ' this.i = s.i;',
  11186. ' return this;',
  11187. ' };',
  11188. '});',
  11189. 'this.ArrInt = [];',
  11190. 'this.ArrRec = [];',
  11191. 'this.ArrSet = [];',
  11192. 'this.ArrJSValue = [];',
  11193. '']),
  11194. LinesToStr([ // $mod.$main
  11195. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11196. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11197. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11198. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11199. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11200. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11201. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11202. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11203. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11204. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11205. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11206. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11207. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11208. '']));
  11209. end;
  11210. procedure TTestModule.TestArray_InsertDelete;
  11211. begin
  11212. StartProgram(false);
  11213. Add([
  11214. 'type',
  11215. ' integer = longint;',
  11216. ' TFlag = (big,small);',
  11217. ' TFlags = set of TFlag;',
  11218. ' TRec = record',
  11219. ' i: integer;',
  11220. ' end;',
  11221. ' TArrInt = array of integer;',
  11222. ' TArrRec = array of TRec;',
  11223. ' TArrSet = array of TFlags;',
  11224. ' TArrJSValue = array of jsvalue;',
  11225. ' TArrArrInt = array of TArrInt;',
  11226. 'var',
  11227. ' ArrInt: tarrint;',
  11228. ' ArrRec: tarrrec;',
  11229. ' ArrSet: tarrset;',
  11230. ' ArrJSValue: tarrjsvalue;',
  11231. ' ArrArrInt: TArrArrInt;',
  11232. 'begin',
  11233. ' Insert(1,arrint,2);',
  11234. ' Insert(arrint[3],arrint,4);',
  11235. ' Insert(arrrec[5],arrrec,6);',
  11236. ' Insert(arrset[7],arrset,7);',
  11237. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11238. ' Insert(10,arrjsvalue,11);',
  11239. ' Insert([23],arrarrint,22);',
  11240. ' Delete(arrint,12,13);',
  11241. ' Delete(arrrec,14,15);',
  11242. ' Delete(arrset,17,18);',
  11243. ' Delete(arrjsvalue,19,10);']);
  11244. ConvertProgram;
  11245. CheckSource('TestArray_InsertDelete',
  11246. LinesToStr([ // statements
  11247. 'this.TFlag = {',
  11248. ' "0": "big",',
  11249. ' big: 0,',
  11250. ' "1": "small",',
  11251. ' small: 1',
  11252. '};',
  11253. 'rtl.recNewT(this, "TRec", function () {',
  11254. ' this.i = 0;',
  11255. ' this.$eq = function (b) {',
  11256. ' return this.i === b.i;',
  11257. ' };',
  11258. ' this.$assign = function (s) {',
  11259. ' this.i = s.i;',
  11260. ' return this;',
  11261. ' };',
  11262. '});',
  11263. 'this.ArrInt = [];',
  11264. 'this.ArrRec = [];',
  11265. 'this.ArrSet = [];',
  11266. 'this.ArrJSValue = [];',
  11267. 'this.ArrArrInt = [];',
  11268. '']),
  11269. LinesToStr([ // $mod.$main
  11270. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11271. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11272. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11273. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11274. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11275. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11276. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11277. '$mod.ArrInt.splice(12, 13);',
  11278. '$mod.ArrRec.splice(14, 15);',
  11279. '$mod.ArrSet.splice(17, 18);',
  11280. '$mod.ArrJSValue.splice(19, 10);',
  11281. '']));
  11282. end;
  11283. procedure TTestModule.TestArray_Add_Append;
  11284. begin
  11285. StartProgram(false);
  11286. Add([
  11287. '{$modeswitch arrayoperators}',
  11288. 'type',
  11289. ' integer = longint;',
  11290. ' TFlag = (big,small);',
  11291. ' TFlags = set of TFlag;',
  11292. ' TRec = record',
  11293. ' i: integer;',
  11294. ' end;',
  11295. ' TArrInt = array of integer;',
  11296. ' TArrRec = array of TRec;',
  11297. ' TArrFlag = array of TFlag;',
  11298. ' TArrSet = array of TFlags;',
  11299. ' TArrJSValue = array of jsvalue;',
  11300. 'var',
  11301. ' ArrInt: tarrint;',
  11302. ' ArrRec: tarrrec;',
  11303. ' ArrFlag: tarrflag;',
  11304. ' ArrSet: tarrset;',
  11305. ' ArrJSValue: tarrjsvalue;',
  11306. ' r: TRec;',
  11307. ' f: TFlags;',
  11308. 'begin',
  11309. ' // append',
  11310. ' arrint:=arrint+[2];',
  11311. ' arrint:=arrint+[3,4];',
  11312. ' arrrec:=arrrec+[r];',
  11313. ' arrrec:=arrrec+[r,r];',
  11314. ' arrset:=arrset+[f];',
  11315. ' arrset:=arrset+[f,f];',
  11316. ' arrjsvalue:=arrjsvalue+[11];',
  11317. ' arrjsvalue:=arrjsvalue+[12,13];',
  11318. ' arrflag:=arrflag+[small];',
  11319. ' arrflag:=arrflag+[small,big];',
  11320. '']);
  11321. ConvertProgram;
  11322. CheckSource('TestArray_Add_Append',
  11323. LinesToStr([ // statements
  11324. 'this.TFlag = {',
  11325. ' "0": "big",',
  11326. ' big: 0,',
  11327. ' "1": "small",',
  11328. ' small: 1',
  11329. '};',
  11330. 'rtl.recNewT(this, "TRec", function () {',
  11331. ' this.i = 0;',
  11332. ' this.$eq = function (b) {',
  11333. ' return this.i === b.i;',
  11334. ' };',
  11335. ' this.$assign = function (s) {',
  11336. ' this.i = s.i;',
  11337. ' return this;',
  11338. ' };',
  11339. '});',
  11340. 'this.ArrInt = [];',
  11341. 'this.ArrRec = [];',
  11342. 'this.ArrFlag = [];',
  11343. 'this.ArrSet = [];',
  11344. 'this.ArrJSValue = [];',
  11345. 'this.r = this.TRec.$new();',
  11346. 'this.f = {};',
  11347. '']),
  11348. LinesToStr([ // $mod.$main
  11349. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11350. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11351. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11352. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11353. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11354. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11355. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11356. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11357. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11358. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11359. '']));
  11360. end;
  11361. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11362. begin
  11363. Parser.Options:=Parser.Options+[po_cassignments];
  11364. StartProgram(false);
  11365. Add([
  11366. '{$modeswitch arrayoperators}',
  11367. 'type',
  11368. ' integer = longint;',
  11369. ' TArrInt = array of integer;',
  11370. ' TArrStr = array of string;',
  11371. 'const',
  11372. ' Ints: TArrInt = (1,2,3);',
  11373. ' Aliases: TarrStr = (''foo'',''b'');',
  11374. ' OneInt: TArrInt = (7);',
  11375. ' OneStr: array of integer = (7);',
  11376. ' Chars: array of char = ''aoc'';',
  11377. ' Names: array of string = (''a'',''foo'');',
  11378. ' NameCount = low(Names)+high(Names)+length(Names);',
  11379. 'var i: integer;',
  11380. 'begin',
  11381. ' Ints:=[];',
  11382. ' Ints:=[1,1];',
  11383. ' Ints:=[1]+[2];',
  11384. ' Ints:=[2];',
  11385. ' Ints:=[]+ints;',
  11386. ' Ints:=Ints+[];',
  11387. ' Ints:=Ints+OneInt;',
  11388. ' Ints:=Ints+[1,1];',
  11389. ' Ints:=[i,i]+Ints;',
  11390. ' Ints:=[1]+[i]+[3];',
  11391. '']);
  11392. ConvertProgram;
  11393. CheckSource('TestArray_DynArrayConstObjFPC',
  11394. LinesToStr([ // statements
  11395. 'this.Ints = [1, 2, 3];',
  11396. 'this.Aliases = ["foo", "b"];',
  11397. 'this.OneInt = [7];',
  11398. 'this.OneStr = [7];',
  11399. 'this.Chars = ["a", "o", "c"];',
  11400. 'this.Names = ["a", "foo"];',
  11401. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11402. 'this.i = 0;',
  11403. '']),
  11404. LinesToStr([ // $mod.$main
  11405. '$mod.Ints = [];',
  11406. '$mod.Ints = [1, 1];',
  11407. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11408. '$mod.Ints = [2];',
  11409. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11410. '$mod.Ints = $mod.Ints;',
  11411. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11412. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11413. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11414. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11415. '']));
  11416. end;
  11417. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11418. begin
  11419. StartProgram(false);
  11420. // Note: const c = [1,1]; defines a set!
  11421. Add([
  11422. '{$mode delphi}',
  11423. 'type',
  11424. ' integer = longint;',
  11425. ' TArrInt = array of integer;',
  11426. ' TArrStr = array of string;',
  11427. 'const',
  11428. ' Ints: TArrInt = [1,1,2];',
  11429. ' Aliases: TarrStr = [''foo'',''b''];',
  11430. ' OneInt: TArrInt = [7];',
  11431. ' OneStr: array of integer = [7]+[8];',
  11432. ' Chars: array of char = ''aoc'';',
  11433. ' Names: array of string = [''a'',''a''];',
  11434. ' NameCount = low(Names)+high(Names)+length(Names);',
  11435. 'begin',
  11436. '']);
  11437. ConvertProgram;
  11438. CheckSource('TestArray_DynArrayConstDelphi',
  11439. LinesToStr([ // statements
  11440. 'this.Ints = [1, 1, 2];',
  11441. 'this.Aliases = ["foo", "b"];',
  11442. 'this.OneInt = [7];',
  11443. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11444. 'this.Chars = ["a", "o", "c"];',
  11445. 'this.Names = ["a", "a"];',
  11446. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11447. '']),
  11448. LinesToStr([ // $mod.$main
  11449. '']));
  11450. end;
  11451. procedure TTestModule.TestArray_ArrayLitAsParam;
  11452. begin
  11453. StartProgram(false);
  11454. Add([
  11455. '{$modeswitch arrayoperators}',
  11456. 'type',
  11457. ' integer = longint;',
  11458. ' TArrInt = array of integer;',
  11459. ' TArrSet = array of (red,green,blue);',
  11460. 'procedure DoOpenInt(const a: array of integer); forward;',
  11461. 'procedure DoInt(const a: TArrInt);',
  11462. 'begin',
  11463. ' DoInt(a+[1]);',
  11464. ' DoInt([1]+a);',
  11465. ' DoOpenInt(a);',
  11466. ' DoOpenInt(a+[1]);',
  11467. ' DoOpenInt([1]+a);',
  11468. 'end;',
  11469. 'procedure DoOpenInt(const a: array of integer);',
  11470. 'begin',
  11471. ' DoOpenInt(a+[1]);',
  11472. ' DoOpenInt([1]+a);',
  11473. ' DoInt(a);',
  11474. ' DoInt(a+[1]);',
  11475. ' DoInt([1]+a);',
  11476. 'end;',
  11477. 'procedure DoSet(const a: TArrSet);',
  11478. 'begin',
  11479. ' DoSet(a+[red]);',
  11480. ' DoSet([blue]+a);',
  11481. 'end;',
  11482. 'var',
  11483. ' i: TArrInt;',
  11484. ' s: TArrSet;',
  11485. 'begin',
  11486. ' DoInt([1]);',
  11487. ' DoInt([1]+[2]);',
  11488. ' DoInt(i+[1]);',
  11489. ' DoInt([1]+i);',
  11490. ' DoOpenInt([1]);',
  11491. ' DoOpenInt([1]+[2]);',
  11492. ' DoOpenInt(i+[1]);',
  11493. ' DoOpenInt([1]+i);',
  11494. ' DoSet([red]);',
  11495. ' DoSet([blue]+[green]);',
  11496. ' DoSet(s+[blue]);',
  11497. ' DoSet([red]+s);',
  11498. '']);
  11499. ConvertProgram;
  11500. CheckSource('TestArray_ArrayLitAsParam',
  11501. LinesToStr([ // statements
  11502. 'this.TArrSet$a = {',
  11503. ' "0": "red",',
  11504. ' red: 0,',
  11505. ' "1": "green",',
  11506. ' green: 1,',
  11507. ' "2": "blue",',
  11508. ' blue: 2',
  11509. '};',
  11510. 'this.DoInt = function (a) {',
  11511. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11512. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11513. ' $mod.DoOpenInt(a);',
  11514. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11515. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11516. '};',
  11517. 'this.DoOpenInt = function (a) {',
  11518. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11519. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11520. ' $mod.DoInt(a);',
  11521. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11522. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11523. '};',
  11524. 'this.DoSet = function (a) {',
  11525. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11526. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11527. '};',
  11528. 'this.i = [];',
  11529. 'this.s = [];',
  11530. '']),
  11531. LinesToStr([ // $mod.$main
  11532. '$mod.DoInt([1]);',
  11533. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11534. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11535. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11536. '$mod.DoOpenInt([1]);',
  11537. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11538. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11539. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11540. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11541. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11542. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11543. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11544. '']));
  11545. end;
  11546. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11547. begin
  11548. StartProgram(false);
  11549. Add([
  11550. '{$modeswitch arrayoperators}',
  11551. 'type',
  11552. ' integer = longint;',
  11553. ' TArrInt = array of integer;',
  11554. ' TArrArrInt = array of TArrInt;',
  11555. 'procedure DoInt(const a: TArrArrInt);',
  11556. 'begin',
  11557. ' DoInt(a+[[1]]);',
  11558. ' DoInt([[1]]+a);',
  11559. ' DoInt(a);',
  11560. 'end;',
  11561. 'var',
  11562. ' i: TArrInt;',
  11563. ' a: TArrArrInt;',
  11564. 'begin',
  11565. ' a:=[[1]];',
  11566. ' a:=[i];',
  11567. ' a:=a+[i];',
  11568. ' a:=[i]+a;',
  11569. ' a:=[[1]+i];',
  11570. ' a:=[[1]+[2]];',
  11571. ' a:=[i+[2]];',
  11572. ' DoInt([[1]]);',
  11573. ' DoInt([[1]+[2],[3,4],[5]]);',
  11574. ' DoInt([i+[1]]+a);',
  11575. ' DoInt([i]+a);',
  11576. '']);
  11577. ConvertProgram;
  11578. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11579. LinesToStr([ // statements
  11580. 'this.DoInt = function (a) {',
  11581. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11582. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11583. ' $mod.DoInt(a);',
  11584. '};',
  11585. 'this.i = [];',
  11586. 'this.a = [];',
  11587. '']),
  11588. LinesToStr([ // $mod.$main
  11589. '$mod.a = [[1]];',
  11590. '$mod.a = [$mod.i];',
  11591. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11592. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11593. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11594. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11595. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11596. '$mod.DoInt([[1]]);',
  11597. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11598. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11599. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11600. '']));
  11601. end;
  11602. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11603. begin
  11604. StartProgram(false);
  11605. Add([
  11606. '{$modeswitch arrayoperators}',
  11607. 'type',
  11608. ' integer = longint;',
  11609. ' TArrInt = array[1..2] of integer;',
  11610. ' TArrArrInt = array of TArrInt;',
  11611. 'procedure DoInt(const a: TArrArrInt);',
  11612. 'begin',
  11613. ' DoInt(a+[[1,2]]);',
  11614. ' DoInt([[1,2]]+a);',
  11615. ' DoInt(a);',
  11616. 'end;',
  11617. 'var',
  11618. ' i: TArrInt;',
  11619. ' a: TArrArrInt;',
  11620. 'begin',
  11621. ' a:=[[1,1]];',
  11622. ' a:=[i];',
  11623. ' a:=a+[i];',
  11624. ' a:=[i]+a;',
  11625. ' DoInt([[1,1]]);',
  11626. ' DoInt([[1,2],[3,4]]);',
  11627. '']);
  11628. ConvertProgram;
  11629. CheckSource('TestArray_ArrayLitStaticAsParam',
  11630. LinesToStr([ // statements
  11631. 'this.DoInt = function (a) {',
  11632. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11633. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11634. ' $mod.DoInt(a);',
  11635. '};',
  11636. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11637. 'this.a = [];',
  11638. '']),
  11639. LinesToStr([ // $mod.$main
  11640. '$mod.a = [[1, 1]];',
  11641. '$mod.a = [$mod.i.slice(0)];',
  11642. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11643. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11644. '$mod.DoInt([[1, 1]]);',
  11645. '$mod.DoInt([[1, 2], [3, 4]]);',
  11646. '']));
  11647. end;
  11648. procedure TTestModule.TestArray_ForInArrOfString;
  11649. begin
  11650. StartProgram(false);
  11651. Add([
  11652. 'type',
  11653. 'type',
  11654. ' TMonthNameArray = array [1..12] of string;',
  11655. ' TMonthNames = TMonthNameArray;',
  11656. ' TObject = class',
  11657. ' private',
  11658. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11659. ' public',
  11660. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11661. ' end;',
  11662. 'var',
  11663. ' f: TObject;',
  11664. ' Month: string;',
  11665. ' Names: array of string = (''a'',''foo'',''bar'');',
  11666. ' i: longint;',
  11667. 'begin',
  11668. ' for Month in f.LongMonthNames do ;',
  11669. ' for Month in Names do ;',
  11670. ' for i:=low(Names) to high(Names) do ;',
  11671. '']);
  11672. ConvertProgram;
  11673. CheckSource('TestArray_ForInArrOfString',
  11674. LinesToStr([ // statements
  11675. 'rtl.createClass(this, "TObject", null, function () {',
  11676. ' this.$init = function () {',
  11677. ' };',
  11678. ' this.$final = function () {',
  11679. ' };',
  11680. '});',
  11681. 'this.f = null;',
  11682. 'this.Month = "";',
  11683. 'this.Names = ["a", "foo", "bar"];',
  11684. 'this.i = 0;',
  11685. '']),
  11686. LinesToStr([ // $mod.$main
  11687. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11688. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11689. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11690. '']));
  11691. end;
  11692. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11693. begin
  11694. StartProgram(false);
  11695. Add([
  11696. '{$modeswitch externalclass}',
  11697. 'type',
  11698. ' TJSObject = class external name ''Object''',
  11699. ' end;',
  11700. ' TJSArray = class external name ''Array''',
  11701. ' class function isArray(Value: JSValue) : boolean;',
  11702. ' function concat() : TJSArray; varargs;',
  11703. ' end;',
  11704. 'var',
  11705. ' aObj: TJSArray;',
  11706. ' a: array of longint;',
  11707. ' o: TJSObject;',
  11708. 'begin',
  11709. ' if TJSArray.isArray(65) then ;',
  11710. ' aObj:=TJSArray(a).concat(a);',
  11711. ' o:=TJSObject(a);',
  11712. ' aObj:=TJSArray([''bird'',''ant'']);',
  11713. '']);
  11714. ConvertProgram;
  11715. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11716. LinesToStr([ // statements
  11717. 'this.aObj = null;',
  11718. 'this.a = [];',
  11719. 'this.o = null;',
  11720. '']),
  11721. LinesToStr([ // $mod.$main
  11722. 'if (Array.isArray(65)) ;',
  11723. '$mod.aObj = $mod.a.concat($mod.a);',
  11724. '$mod.o = $mod.a;',
  11725. '$mod.aObj = ["bird", "ant"];',
  11726. '']));
  11727. end;
  11728. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11729. begin
  11730. StartProgram(false);
  11731. Add([
  11732. '{$modeswitch externalclass}',
  11733. 'type',
  11734. ' TArrStr = array of string;',
  11735. ' TJSArray = class external name ''Array''',
  11736. ' end;',
  11737. ' TJSObject = class external name ''Object''',
  11738. ' end;',
  11739. 'var',
  11740. ' aObj: TJSArray;',
  11741. ' a: TArrStr;',
  11742. ' jo: TJSObject;',
  11743. 'begin',
  11744. ' a:=TArrStr(aObj);',
  11745. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11746. ' a:=TarrStr(jo);',
  11747. '']);
  11748. ConvertProgram;
  11749. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11750. LinesToStr([ // statements
  11751. 'this.aObj = null;',
  11752. 'this.a = [];',
  11753. 'this.jo = null;',
  11754. '']),
  11755. LinesToStr([ // $mod.$main
  11756. '$mod.a = $mod.aObj;',
  11757. '$mod.aObj[1] = $mod.aObj[2];',
  11758. '$mod.a = $mod.jo;',
  11759. '']));
  11760. end;
  11761. procedure TTestModule.TestArrayOfConst_TVarRec;
  11762. begin
  11763. StartProgram(true,[supTVarRec]);
  11764. Add([
  11765. 'procedure Say(args: array of const);',
  11766. 'var',
  11767. ' i: longint;',
  11768. ' v: TVarRec;',
  11769. 'begin',
  11770. ' for i:=low(args) to high(args) do begin',
  11771. ' v:=args[i];',
  11772. ' case v.vtype of',
  11773. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11774. ' end;',
  11775. ' end;',
  11776. ' for v in args do ;',
  11777. ' args:=nil;',
  11778. ' SetLength(args,2);',
  11779. 'end;',
  11780. 'begin']);
  11781. ConvertProgram;
  11782. CheckSource('TestArrayOfConst_TVarRec',
  11783. LinesToStr([ // statements
  11784. 'this.Say = function (args) {',
  11785. ' var i = 0;',
  11786. ' var v = pas.system.TVarRec.$new();',
  11787. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11788. ' i = $l;',
  11789. ' v.$assign(args[i]);',
  11790. ' var $tmp = v.VType;',
  11791. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11792. ' };',
  11793. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11794. ' args = [];',
  11795. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11796. '};',
  11797. '']),
  11798. LinesToStr([ // $mod.$main
  11799. ]));
  11800. end;
  11801. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11802. begin
  11803. StartProgram(true,[supTVarRec]);
  11804. Add([
  11805. 'procedure Say(args: array of const);',
  11806. 'begin',
  11807. ' Say(args);',
  11808. 'end;',
  11809. 'var',
  11810. ' p: Pointer;',
  11811. ' j: jsvalue;',
  11812. ' c: currency;',
  11813. 'begin',
  11814. ' Say([]);',
  11815. ' Say([1]);',
  11816. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11817. '']);
  11818. ConvertProgram;
  11819. CheckSource('TestArrayOfConst_PassBaseTypes',
  11820. LinesToStr([ // statements
  11821. 'this.Say = function (args) {',
  11822. ' $mod.Say(args);',
  11823. '};',
  11824. 'this.p = null;',
  11825. 'this.j = undefined;',
  11826. 'this.c = 0;',
  11827. '']),
  11828. LinesToStr([ // $mod.$main
  11829. '$mod.Say([]);',
  11830. '$mod.Say(pas.system.VarRecs(0, 1));',
  11831. '$mod.Say(pas.system.VarRecs(',
  11832. ' 9,',
  11833. ' "c",',
  11834. ' 18,',
  11835. ' "foo",',
  11836. ' 5,',
  11837. ' null,',
  11838. ' 1,',
  11839. ' true,',
  11840. ' 3,',
  11841. ' 1.3,',
  11842. ' 5,',
  11843. ' $mod.p,',
  11844. ' 20,',
  11845. ' $mod.j,',
  11846. ' 12,',
  11847. ' $mod.c',
  11848. ' ));',
  11849. '']));
  11850. end;
  11851. procedure TTestModule.TestArrayOfConst_PassObj;
  11852. begin
  11853. StartProgram(true,[supTVarRec]);
  11854. Add([
  11855. '{$interfaces corba}',
  11856. 'type',
  11857. ' TObject = class',
  11858. ' end;',
  11859. ' TClass = class of TObject;',
  11860. ' IUnknown = interface',
  11861. ' end;',
  11862. 'procedure Say(args: array of const);',
  11863. 'begin',
  11864. 'end;',
  11865. 'var',
  11866. ' o: TObject;',
  11867. ' c: TClass;',
  11868. ' i: IUnknown;',
  11869. 'begin',
  11870. ' Say([o,c,TObject]);',
  11871. ' Say([nil,i]);',
  11872. '']);
  11873. ConvertProgram;
  11874. CheckSource('TestArrayOfConst_PassObj',
  11875. LinesToStr([ // statements
  11876. 'rtl.createClass(this, "TObject", null, function () {',
  11877. ' this.$init = function () {',
  11878. ' };',
  11879. ' this.$final = function () {',
  11880. ' };',
  11881. '});',
  11882. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11883. 'this.Say = function (args) {',
  11884. '};',
  11885. 'this.o = null;',
  11886. 'this.c = null;',
  11887. 'this.i = null;',
  11888. '']),
  11889. LinesToStr([ // $mod.$main
  11890. '$mod.Say(pas.system.VarRecs(',
  11891. ' 7,',
  11892. ' $mod.o,',
  11893. ' 8,',
  11894. ' $mod.c,',
  11895. ' 8,',
  11896. ' $mod.TObject',
  11897. '));',
  11898. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11899. '']));
  11900. end;
  11901. procedure TTestModule.TestRecord_Empty;
  11902. begin
  11903. StartProgram(false);
  11904. Add([
  11905. 'type',
  11906. ' TRecA = record',
  11907. ' end;',
  11908. 'var a,b: TRecA;',
  11909. 'begin',
  11910. ' if a=b then ;']);
  11911. ConvertProgram;
  11912. CheckSource('TestRecord_Empty',
  11913. LinesToStr([ // statements
  11914. 'rtl.recNewT(this, "TRecA", function () {',
  11915. ' this.$eq = function (b) {',
  11916. ' return true;',
  11917. ' };',
  11918. ' this.$assign = function (s) {',
  11919. ' return this;',
  11920. ' };',
  11921. '});',
  11922. 'this.a = this.TRecA.$new();',
  11923. 'this.b = this.TRecA.$new();',
  11924. '']),
  11925. LinesToStr([ // $mod.$main
  11926. 'if ($mod.a.$eq($mod.b)) ;'
  11927. ]));
  11928. end;
  11929. procedure TTestModule.TestRecord_Var;
  11930. begin
  11931. StartProgram(false);
  11932. Add('type');
  11933. Add(' TRecA = record');
  11934. Add(' Bold: longint;');
  11935. Add(' end;');
  11936. Add('var Rec: TRecA;');
  11937. Add('begin');
  11938. Add(' rec.bold:=123');
  11939. ConvertProgram;
  11940. CheckSource('TestRecord_Var',
  11941. LinesToStr([ // statements
  11942. 'rtl.recNewT(this, "TRecA", function () {',
  11943. ' this.Bold = 0;',
  11944. ' this.$eq = function (b) {',
  11945. ' return this.Bold === b.Bold;',
  11946. ' };',
  11947. ' this.$assign = function (s) {',
  11948. ' this.Bold = s.Bold;',
  11949. ' return this;',
  11950. ' };',
  11951. '});',
  11952. 'this.Rec = this.TRecA.$new();',
  11953. '']),
  11954. LinesToStr([ // $mod.$main
  11955. '$mod.Rec.Bold = 123;'
  11956. ]));
  11957. end;
  11958. procedure TTestModule.TestRecord_VarExternal;
  11959. begin
  11960. StartProgram(false);
  11961. Add([
  11962. '{$modeswitch externalclass}',
  11963. 'type',
  11964. ' TRecA = record',
  11965. ' i: byte;',
  11966. ' length_: longint external name ''length'';',
  11967. ' end;',
  11968. 'var Rec: TRecA;',
  11969. 'begin',
  11970. ' rec.length_ := rec.length_',
  11971. '']);
  11972. ConvertProgram;
  11973. CheckSource('TestRecord_VarExternal',
  11974. LinesToStr([ // statements
  11975. 'rtl.recNewT(this, "TRecA", function () {',
  11976. ' this.i = 0;',
  11977. ' this.$eq = function (b) {',
  11978. ' return (this.i === b.i) && (this.length === b.length);',
  11979. ' };',
  11980. ' this.$assign = function (s) {',
  11981. ' this.i = s.i;',
  11982. ' this.length = s.length;',
  11983. ' return this;',
  11984. ' };',
  11985. '});',
  11986. 'this.Rec = this.TRecA.$new();',
  11987. '']),
  11988. LinesToStr([ // $mod.$main
  11989. '$mod.Rec.length = $mod.Rec.length;'
  11990. ]));
  11991. end;
  11992. procedure TTestModule.TestRecord_WithDo;
  11993. begin
  11994. StartProgram(false);
  11995. Add('type');
  11996. Add(' TRec = record');
  11997. Add(' vI: longint;');
  11998. Add(' end;');
  11999. Add('var');
  12000. Add(' Int: longint;');
  12001. Add(' r: TRec;');
  12002. Add('begin');
  12003. Add(' with r do');
  12004. Add(' int:=vi;');
  12005. Add(' with r do begin');
  12006. Add(' int:=vi;');
  12007. Add(' vi:=int;');
  12008. Add(' end;');
  12009. ConvertProgram;
  12010. CheckSource('TestWithRecordDo',
  12011. LinesToStr([ // statements
  12012. 'rtl.recNewT(this, "TRec", function () {',
  12013. ' this.vI = 0;',
  12014. ' this.$eq = function (b) {',
  12015. ' return this.vI === b.vI;',
  12016. ' };',
  12017. ' this.$assign = function (s) {',
  12018. ' this.vI = s.vI;',
  12019. ' return this;',
  12020. ' };',
  12021. '});',
  12022. 'this.Int = 0;',
  12023. 'this.r = this.TRec.$new();',
  12024. '']),
  12025. LinesToStr([ // $mod.$main
  12026. 'var $with = $mod.r;',
  12027. '$mod.Int = $with.vI;',
  12028. 'var $with1 = $mod.r;',
  12029. '$mod.Int = $with1.vI;',
  12030. '$with1.vI = $mod.Int;'
  12031. ]));
  12032. end;
  12033. procedure TTestModule.TestRecord_Assign;
  12034. begin
  12035. StartProgram(false);
  12036. Add([
  12037. 'type',
  12038. ' TEnum = (red,green);',
  12039. ' TEnums = set of TEnum;',
  12040. ' TSmallRec = record',
  12041. ' N: longint;',
  12042. ' end;',
  12043. ' TBigRec = record',
  12044. ' Int: longint;',
  12045. ' D: double;',
  12046. ' Arr: array of longint;',
  12047. ' Arr2: array[1..2] of longint;',
  12048. ' Small: TSmallRec;',
  12049. ' Enums: TEnums;',
  12050. ' end;',
  12051. 'var',
  12052. ' r, s: TBigRec;',
  12053. 'begin',
  12054. ' r:=s;',
  12055. ' r:=default(TBigRec);',
  12056. ' r:=default(s);',
  12057. '']);
  12058. ConvertProgram;
  12059. CheckSource('TestRecord_Assign',
  12060. LinesToStr([ // statements
  12061. 'this.TEnum = {',
  12062. ' "0": "red",',
  12063. ' red: 0,',
  12064. ' "1": "green",',
  12065. ' green: 1',
  12066. '};',
  12067. 'rtl.recNewT(this, "TSmallRec", function () {',
  12068. ' this.N = 0;',
  12069. ' this.$eq = function (b) {',
  12070. ' return this.N === b.N;',
  12071. ' };',
  12072. ' this.$assign = function (s) {',
  12073. ' this.N = s.N;',
  12074. ' return this;',
  12075. ' };',
  12076. '});',
  12077. 'rtl.recNewT(this, "TBigRec", function () {',
  12078. ' this.Int = 0;',
  12079. ' this.D = 0.0;',
  12080. ' this.$new = function () {',
  12081. ' var r = Object.create(this);',
  12082. ' r.Arr = [];',
  12083. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12084. ' r.Small = $mod.TSmallRec.$new();',
  12085. ' r.Enums = {};',
  12086. ' return r;',
  12087. ' };',
  12088. ' this.$eq = function (b) {',
  12089. ' 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);',
  12090. ' };',
  12091. ' this.$assign = function (s) {',
  12092. ' this.Int = s.Int;',
  12093. ' this.D = s.D;',
  12094. ' this.Arr = rtl.arrayRef(s.Arr);',
  12095. ' this.Arr2 = s.Arr2.slice(0);',
  12096. ' this.Small.$assign(s.Small);',
  12097. ' this.Enums = rtl.refSet(s.Enums);',
  12098. ' return this;',
  12099. ' };',
  12100. '});',
  12101. 'this.r = this.TBigRec.$new();',
  12102. 'this.s = this.TBigRec.$new();',
  12103. '']),
  12104. LinesToStr([ // $mod.$main
  12105. '$mod.r.$assign($mod.s);',
  12106. '$mod.r.$assign($mod.TBigRec.$new());',
  12107. '$mod.r.$assign($mod.TBigRec.$new());',
  12108. '']));
  12109. end;
  12110. procedure TTestModule.TestRecord_AsParams;
  12111. begin
  12112. StartProgram(false);
  12113. Add([
  12114. 'type',
  12115. ' integer = longint;',
  12116. ' TRecord = record',
  12117. ' i: integer;',
  12118. ' end;',
  12119. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12120. 'var vL: TRecord;',
  12121. 'begin',
  12122. ' vd:=vd;',
  12123. ' vd.i:=vd.i;',
  12124. ' vl:=vc;',
  12125. ' vv:=vv;',
  12126. ' vv.i:=vv.i;',
  12127. ' U:=vl;',
  12128. ' U:=vd;',
  12129. ' U:=vc;',
  12130. ' U:=vv;',
  12131. ' vl:=TRecord(U);',
  12132. ' vd:=TRecord(U);',
  12133. ' vv:=TRecord(U);',
  12134. ' doit(vd,vd,vd,vd);',
  12135. ' doit(vc,vc,vl,vl);',
  12136. ' doit(vv,vv,vv,vv);',
  12137. ' doit(vl,vl,vl,vl);',
  12138. ' TRecord(U).i:=3;',
  12139. 'end;',
  12140. 'var i: TRecord;',
  12141. 'begin',
  12142. ' doit(i,i,i,i);',
  12143. '']);
  12144. ConvertProgram;
  12145. CheckSource('TestRecord_AsParams',
  12146. LinesToStr([ // statements
  12147. 'rtl.recNewT(this, "TRecord", function () {',
  12148. ' this.i = 0;',
  12149. ' this.$eq = function (b) {',
  12150. ' return this.i === b.i;',
  12151. ' };',
  12152. ' this.$assign = function (s) {',
  12153. ' this.i = s.i;',
  12154. ' return this;',
  12155. ' };',
  12156. '});',
  12157. 'this.DoIt = function (vD, vC, vV, U) {',
  12158. ' var vL = $mod.TRecord.$new();',
  12159. ' vD.$assign(vD);',
  12160. ' vD.i = vD.i;',
  12161. ' vL.$assign(vC);',
  12162. ' vV.$assign(vV);',
  12163. ' vV.i = vV.i;',
  12164. ' U.$assign(vL);',
  12165. ' U.$assign(vD);',
  12166. ' U.$assign(vC);',
  12167. ' U.$assign(vV);',
  12168. ' vL.$assign(U);',
  12169. ' vD.$assign(U);',
  12170. ' vV.$assign(U);',
  12171. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12172. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12173. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12174. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12175. ' U.i = 3;',
  12176. '};',
  12177. 'this.i = this.TRecord.$new();'
  12178. ]),
  12179. LinesToStr([
  12180. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12181. '']));
  12182. end;
  12183. procedure TTestModule.TestRecord_ConstRef;
  12184. begin
  12185. StartProgram(false);
  12186. Add([
  12187. 'type TRec = record i: word; end;',
  12188. 'procedure Run(constref a: TRec);',
  12189. 'begin',
  12190. 'end;',
  12191. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12192. 'var l: TRec;',
  12193. 'begin',
  12194. ' Run(l);',
  12195. ' Run(a);',
  12196. ' Run(b);',
  12197. ' Run(c);',
  12198. ' Run(d);',
  12199. ' Run(e);',
  12200. 'end;',
  12201. 'begin',
  12202. '']);
  12203. ConvertProgram;
  12204. CheckResolverUnexpectedHints();
  12205. CheckSource('TestRecord_ConstRef',
  12206. LinesToStr([ // statements
  12207. 'rtl.recNewT(this, "TRec", function () {',
  12208. ' this.i = 0;',
  12209. ' this.$eq = function (b) {',
  12210. ' return this.i === b.i;',
  12211. ' };',
  12212. ' this.$assign = function (s) {',
  12213. ' this.i = s.i;',
  12214. ' return this;',
  12215. ' };',
  12216. '});',
  12217. 'this.Run = function (a) {',
  12218. '};',
  12219. 'this.Fly = function (a, b, c, d, e) {',
  12220. ' var l = $mod.TRec.$new();',
  12221. ' $mod.Run(l);',
  12222. ' $mod.Run(a);',
  12223. ' $mod.Run(b);',
  12224. ' $mod.Run(c);',
  12225. ' $mod.Run(d);',
  12226. ' $mod.Run(e);',
  12227. '};',
  12228. '']),
  12229. LinesToStr([
  12230. '']));
  12231. end;
  12232. procedure TTestModule.TestRecordElement_AsParams;
  12233. begin
  12234. StartProgram(false);
  12235. Add('type');
  12236. Add(' integer = longint;');
  12237. Add(' TRecord = record');
  12238. Add(' i: integer;');
  12239. Add(' end;');
  12240. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12241. Add('var vJ: TRecord;');
  12242. Add('begin');
  12243. Add(' doit(vj.i,vj.i,vj.i);');
  12244. Add('end;');
  12245. Add('var r: TRecord;');
  12246. Add('begin');
  12247. Add(' doit(r.i,r.i,r.i);');
  12248. ConvertProgram;
  12249. CheckSource('TestRecordElement_AsParams',
  12250. LinesToStr([ // statements
  12251. 'rtl.recNewT(this, "TRecord", function () {',
  12252. ' this.i = 0;',
  12253. ' this.$eq = function (b) {',
  12254. ' return this.i === b.i;',
  12255. ' };',
  12256. ' this.$assign = function (s) {',
  12257. ' this.i = s.i;',
  12258. ' return this;',
  12259. ' };',
  12260. '});',
  12261. 'this.DoIt = function (vG,vH,vI) {',
  12262. ' var vJ = $mod.TRecord.$new();',
  12263. ' $mod.DoIt(vJ.i, vJ.i, {',
  12264. ' p: vJ,',
  12265. ' get: function () {',
  12266. ' return this.p.i;',
  12267. ' },',
  12268. ' set: function (v) {',
  12269. ' this.p.i = v;',
  12270. ' }',
  12271. ' });',
  12272. '};',
  12273. 'this.r = this.TRecord.$new();'
  12274. ]),
  12275. LinesToStr([
  12276. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12277. ' p: $mod.r,',
  12278. ' get: function () {',
  12279. ' return this.p.i;',
  12280. ' },',
  12281. ' set: function (v) {',
  12282. ' this.p.i = v;',
  12283. ' }',
  12284. '});'
  12285. ]));
  12286. end;
  12287. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12288. begin
  12289. StartProgram(false);
  12290. Add('type');
  12291. Add(' integer = longint;');
  12292. Add(' TRecord = record');
  12293. Add(' i: integer;');
  12294. Add(' end;');
  12295. Add('function GetRec(vB: integer = 0): TRecord;');
  12296. Add('begin');
  12297. Add('end;');
  12298. Add('procedure DoIt(vG: integer; const vH: integer);');
  12299. Add('begin');
  12300. Add('end;');
  12301. Add('begin');
  12302. Add(' doit(getrec.i,getrec.i);');
  12303. Add(' doit(getrec().i,getrec().i);');
  12304. Add(' doit(getrec(1).i,getrec(2).i);');
  12305. ConvertProgram;
  12306. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12307. LinesToStr([ // statements
  12308. 'rtl.recNewT(this, "TRecord", function () {',
  12309. ' this.i = 0;',
  12310. ' this.$eq = function (b) {',
  12311. ' return this.i === b.i;',
  12312. ' };',
  12313. ' this.$assign = function (s) {',
  12314. ' this.i = s.i;',
  12315. ' return this;',
  12316. ' };',
  12317. '});',
  12318. 'this.GetRec = function (vB) {',
  12319. ' var Result = $mod.TRecord.$new();',
  12320. ' return Result;',
  12321. '};',
  12322. 'this.DoIt = function (vG, vH) {',
  12323. '};',
  12324. '']),
  12325. LinesToStr([
  12326. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12327. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12328. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12329. '']));
  12330. end;
  12331. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12332. begin
  12333. StartProgram(false);
  12334. Add('type');
  12335. Add(' integer = longint;');
  12336. Add(' TRecord = record');
  12337. Add(' i: integer;');
  12338. Add(' end;');
  12339. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12340. Add('begin');
  12341. Add('end;');
  12342. Add('var r: trecord;');
  12343. Add('begin');
  12344. Add(' with r do ');
  12345. Add(' doit(i,i,i);');
  12346. ConvertProgram;
  12347. CheckSource('TestRecordElementFromWith_AsParams',
  12348. LinesToStr([ // statements
  12349. 'rtl.recNewT(this, "TRecord", function () {',
  12350. ' this.i = 0;',
  12351. ' this.$eq = function (b) {',
  12352. ' return this.i === b.i;',
  12353. ' };',
  12354. ' this.$assign = function (s) {',
  12355. ' this.i = s.i;',
  12356. ' return this;',
  12357. ' };',
  12358. '});',
  12359. 'this.DoIt = function (vG,vH,vI) {',
  12360. '};',
  12361. 'this.r = this.TRecord.$new();'
  12362. ]),
  12363. LinesToStr([
  12364. 'var $with = $mod.r;',
  12365. '$mod.DoIt($with.i,$with.i,{',
  12366. ' p: $with,',
  12367. ' get: function () {',
  12368. ' return this.p.i;',
  12369. ' },',
  12370. ' set: function (v) {',
  12371. ' this.p.i = v;',
  12372. ' }',
  12373. '});',
  12374. '']));
  12375. end;
  12376. procedure TTestModule.TestRecord_Equal;
  12377. begin
  12378. StartProgram(false);
  12379. Add('type');
  12380. Add(' integer = longint;');
  12381. Add(' TFlag = (red,blue);');
  12382. Add(' TFlags = set of TFlag;');
  12383. Add(' TProc = procedure;');
  12384. Add(' TRecord = record');
  12385. Add(' i: integer;');
  12386. Add(' Event: TProc;');
  12387. Add(' f: TFlags;');
  12388. Add(' end;');
  12389. Add(' TNested = record');
  12390. Add(' r: TRecord;');
  12391. Add(' end;');
  12392. Add('var');
  12393. Add(' b: boolean;');
  12394. Add(' r,s: trecord;');
  12395. Add('begin');
  12396. Add(' b:=r=s;');
  12397. Add(' b:=r<>s;');
  12398. ConvertProgram;
  12399. CheckSource('TestRecord_Equal',
  12400. LinesToStr([ // statements
  12401. 'this.TFlag = {',
  12402. ' "0": "red",',
  12403. ' red: 0,',
  12404. ' "1": "blue",',
  12405. ' blue: 1',
  12406. '};',
  12407. 'rtl.recNewT(this, "TRecord", function () {',
  12408. ' this.i = 0;',
  12409. ' this.Event = null;',
  12410. ' this.$new = function () {',
  12411. ' var r = Object.create(this);',
  12412. ' r.f = {};',
  12413. ' return r;',
  12414. ' };',
  12415. ' this.$eq = function (b) {',
  12416. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12417. ' };',
  12418. ' this.$assign = function (s) {',
  12419. ' this.i = s.i;',
  12420. ' this.Event = s.Event;',
  12421. ' this.f = rtl.refSet(s.f);',
  12422. ' return this;',
  12423. ' };',
  12424. '});',
  12425. 'rtl.recNewT(this, "TNested", function () {',
  12426. ' this.$new = function () {',
  12427. ' var r = Object.create(this);',
  12428. ' r.r = $mod.TRecord.$new();',
  12429. ' return r;',
  12430. ' };',
  12431. ' this.$eq = function (b) {',
  12432. ' return this.r.$eq(b.r);',
  12433. ' };',
  12434. ' this.$assign = function (s) {',
  12435. ' this.r.$assign(s.r);',
  12436. ' return this;',
  12437. ' };',
  12438. '});',
  12439. 'this.b = false;',
  12440. 'this.r = this.TRecord.$new();',
  12441. 'this.s = this.TRecord.$new();',
  12442. '']),
  12443. LinesToStr([
  12444. '$mod.b = $mod.r.$eq($mod.s);',
  12445. '$mod.b = !$mod.r.$eq($mod.s);',
  12446. '']));
  12447. end;
  12448. procedure TTestModule.TestRecord_JSValue;
  12449. begin
  12450. StartProgram(false);
  12451. Add([
  12452. 'type',
  12453. ' TRecord = record',
  12454. ' i: longint;',
  12455. ' end;',
  12456. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12457. 'begin',
  12458. 'end;',
  12459. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12460. 'begin',
  12461. ' if jsvalue(d) then ;',
  12462. ' if jsvalue(c) then ;',
  12463. ' if jsvalue(v) then ;',
  12464. 'end;',
  12465. 'var',
  12466. ' Jv: jsvalue;',
  12467. ' Rec: trecord;',
  12468. 'begin',
  12469. ' rec:=trecord(jv);',
  12470. ' jv:=rec;',
  12471. ' Fly(rec,rec);',
  12472. ' Fly(@rec,@rec);',
  12473. ' if jsvalue(Rec) then ;',
  12474. ' Run(trecord(jv),trecord(jv),rec);',
  12475. '']);
  12476. ConvertProgram;
  12477. CheckSource('TestRecord_JSValue',
  12478. LinesToStr([ // statements
  12479. 'rtl.recNewT(this, "TRecord", function () {',
  12480. ' this.i = 0;',
  12481. ' this.$eq = function (b) {',
  12482. ' return this.i === b.i;',
  12483. ' };',
  12484. ' this.$assign = function (s) {',
  12485. ' this.i = s.i;',
  12486. ' return this;',
  12487. ' };',
  12488. '});',
  12489. 'this.Fly = function (d, c) {',
  12490. '};',
  12491. 'this.Run = function (d, c, v) {',
  12492. ' if (d) ;',
  12493. ' if (c) ;',
  12494. ' if (v) ;',
  12495. '};',
  12496. 'this.Jv = undefined;',
  12497. 'this.Rec = this.TRecord.$new();',
  12498. '']),
  12499. LinesToStr([
  12500. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12501. '$mod.Jv = $mod.Rec;',
  12502. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12503. '$mod.Fly($mod.Rec, $mod.Rec);',
  12504. 'if ($mod.Rec) ;',
  12505. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12506. '']));
  12507. end;
  12508. procedure TTestModule.TestRecord_VariantFail;
  12509. begin
  12510. StartProgram(false);
  12511. Add([
  12512. 'type',
  12513. ' TRec = record',
  12514. ' case word of',
  12515. ' 0: (b0, b1: Byte);',
  12516. ' 1: (i: word);',
  12517. ' end;',
  12518. 'begin']);
  12519. SetExpectedPasResolverError('Not supported: variant record',
  12520. nNotSupportedX);
  12521. ConvertProgram;
  12522. end;
  12523. procedure TTestModule.TestRecord_FieldArray;
  12524. begin
  12525. StartProgram(false);
  12526. Add([
  12527. 'type',
  12528. ' TArrInt = array[3..4] of longint;',
  12529. ' TArrArrInt = array[3..4] of longint;',
  12530. ' TRec = record',
  12531. ' a: array of longint;',
  12532. ' s: array[1..2] of longint;',
  12533. ' m: array[1..2,3..4] of longint;',
  12534. ' o: TArrArrInt;',
  12535. ' end;',
  12536. 'begin']);
  12537. ConvertProgram;
  12538. CheckSource('TestRecord_FieldArray',
  12539. LinesToStr([ // statements
  12540. 'rtl.recNewT(this, "TRec", function () {',
  12541. ' this.m$a$clone = function (a) {',
  12542. ' var b = [];',
  12543. ' b.length = 2;',
  12544. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12545. ' return b;',
  12546. ' };',
  12547. ' this.$new = function () {',
  12548. ' var r = Object.create(this);',
  12549. ' r.a = [];',
  12550. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12551. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12552. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12553. ' return r;',
  12554. ' };',
  12555. ' this.$eq = function (b) {',
  12556. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12557. ' };',
  12558. ' this.$assign = function (s) {',
  12559. ' this.a = rtl.arrayRef(s.a);',
  12560. ' this.s = s.s.slice(0);',
  12561. ' this.m = this.m$a$clone(s.m);',
  12562. ' this.o = s.o.slice(0);',
  12563. ' return this;',
  12564. ' };',
  12565. '});',
  12566. '']),
  12567. LinesToStr([ // $mod.$main
  12568. '']));
  12569. end;
  12570. procedure TTestModule.TestRecord_Const;
  12571. begin
  12572. StartProgram(false);
  12573. Add([
  12574. 'type',
  12575. ' TArrInt = array[3..4] of longint;',
  12576. ' TPoint = record x,y: longint; end;',
  12577. ' TRec = record',
  12578. ' i: longint;',
  12579. ' a: array of longint;',
  12580. ' s: array[1..2] of longint;',
  12581. ' m: array[1..2,3..4] of longint;',
  12582. ' p: TPoint;',
  12583. ' end;',
  12584. ' TPoints = array of TPoint;',
  12585. 'const',
  12586. ' r: TRec = (',
  12587. ' i:1;',
  12588. ' a:(2,3);',
  12589. ' s:(4,5);',
  12590. ' m:( (11,12), (13,14) );',
  12591. ' p: (x:21; y:22)',
  12592. ' );',
  12593. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12594. 'begin']);
  12595. ConvertProgram;
  12596. CheckSource('TestRecord_Const',
  12597. LinesToStr([ // statements
  12598. 'rtl.recNewT(this, "TPoint", function () {',
  12599. ' this.x = 0;',
  12600. ' this.y = 0;',
  12601. ' this.$eq = function (b) {',
  12602. ' return (this.x === b.x) && (this.y === b.y);',
  12603. ' };',
  12604. ' this.$assign = function (s) {',
  12605. ' this.x = s.x;',
  12606. ' this.y = s.y;',
  12607. ' return this;',
  12608. ' };',
  12609. '});',
  12610. 'rtl.recNewT(this, "TRec", function () {',
  12611. ' this.i = 0;',
  12612. ' this.m$a$clone = function (a) {',
  12613. ' var b = [];',
  12614. ' b.length = 2;',
  12615. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12616. ' return b;',
  12617. ' };',
  12618. ' this.$new = function () {',
  12619. ' var r = Object.create(this);',
  12620. ' r.a = [];',
  12621. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12622. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12623. ' r.p = $mod.TPoint.$new();',
  12624. ' return r;',
  12625. ' };',
  12626. ' this.$eq = function (b) {',
  12627. ' 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);',
  12628. ' };',
  12629. ' this.$assign = function (s) {',
  12630. ' this.i = s.i;',
  12631. ' this.a = rtl.arrayRef(s.a);',
  12632. ' this.s = s.s.slice(0);',
  12633. ' this.m = this.m$a$clone(s.m);',
  12634. ' this.p.$assign(s.p);',
  12635. ' return this;',
  12636. ' };',
  12637. '});',
  12638. 'this.r = this.TRec.$clone({',
  12639. ' i: 1,',
  12640. ' a: [2, 3],',
  12641. ' s: [4, 5],',
  12642. ' m: [[11, 12], [13, 14]],',
  12643. ' p: this.TPoint.$clone({',
  12644. ' x: 21,',
  12645. ' y: 22',
  12646. ' })',
  12647. '});',
  12648. 'this.p = [this.TPoint.$clone({',
  12649. ' x: 1,',
  12650. ' y: 2',
  12651. '}), this.TPoint.$clone({',
  12652. ' x: 3,',
  12653. ' y: 4',
  12654. '})];',
  12655. '']),
  12656. LinesToStr([ // $mod.$main
  12657. '']));
  12658. end;
  12659. procedure TTestModule.TestRecord_TypecastFail;
  12660. begin
  12661. StartProgram(false);
  12662. Add([
  12663. 'type',
  12664. ' TPoint = record x,y: longint; end;',
  12665. ' TRec = record l: longint end;',
  12666. 'var p: TPoint;',
  12667. 'begin',
  12668. ' if TRec(p).l=2 then ;']);
  12669. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12670. nIllegalTypeConversionTo);
  12671. ConvertProgram;
  12672. end;
  12673. procedure TTestModule.TestRecord_InFunction;
  12674. begin
  12675. StartProgram(false);
  12676. Add([
  12677. 'var TPoint: longint = 3;',
  12678. 'procedure DoIt;',
  12679. 'type',
  12680. ' TPoint = record x,y: longint; end;',
  12681. ' TPoints = array of TPoint;',
  12682. 'var',
  12683. ' r: TPoint;',
  12684. ' p: TPoints;',
  12685. 'begin',
  12686. ' SetLength(p,2);',
  12687. 'end;',
  12688. 'begin']);
  12689. ConvertProgram;
  12690. CheckSource('TestRecord_InFunction',
  12691. LinesToStr([ // statements
  12692. 'this.TPoint = 3;',
  12693. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12694. ' this.x = 0;',
  12695. ' this.y = 0;',
  12696. ' this.$eq = function (b) {',
  12697. ' return (this.x === b.x) && (this.y === b.y);',
  12698. ' };',
  12699. ' this.$assign = function (s) {',
  12700. ' this.x = s.x;',
  12701. ' this.y = s.y;',
  12702. ' return this;',
  12703. ' };',
  12704. '});',
  12705. 'this.DoIt = function () {',
  12706. ' var r = TPoint$1.$new();',
  12707. ' var p = [];',
  12708. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12709. '};',
  12710. '']),
  12711. LinesToStr([ // $mod.$main
  12712. '']));
  12713. end;
  12714. procedure TTestModule.TestRecordAnonym_Field;
  12715. begin
  12716. StartProgram(false);
  12717. Add(['',
  12718. 'var Rec: record',
  12719. ' Bold: longint;',
  12720. ' end;',
  12721. 'begin',
  12722. ' rec.bold:=123;',
  12723. ' rec.bold:=rec.bold+7;',
  12724. '']);
  12725. ConvertProgram;
  12726. CheckSource('TestRecordAnonym_Field',
  12727. LinesToStr([ // statements
  12728. 'rtl.recNewT(this, "Rec$a", function () {',
  12729. ' this.Bold = 0;',
  12730. ' this.$eq = function (b) {',
  12731. ' return this.Bold === b.Bold;',
  12732. ' };',
  12733. ' this.$assign = function (s) {',
  12734. ' this.Bold = s.Bold;',
  12735. ' return this;',
  12736. ' };',
  12737. '});',
  12738. 'this.Rec = this.Rec$a.$new();',
  12739. '']),
  12740. LinesToStr([ // $mod.$main
  12741. '$mod.Rec.Bold = 123;',
  12742. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12743. '']));
  12744. end;
  12745. procedure TTestModule.TestRecordAnonym_Assign;
  12746. begin
  12747. StartProgram(false);
  12748. Add(['',
  12749. 'var S,T: record',
  12750. ' Bold: longint;',
  12751. ' end;',
  12752. ' b: boolean;',
  12753. 'begin',
  12754. ' S:=T;',
  12755. ' b:=s=t;',
  12756. '']);
  12757. ConvertProgram;
  12758. CheckSource('TestRecordAnonym_Assign',
  12759. LinesToStr([ // statements
  12760. 'rtl.recNewT(this, "T$a", function () {',
  12761. ' this.Bold = 0;',
  12762. ' this.$eq = function (b) {',
  12763. ' return this.Bold === b.Bold;',
  12764. ' };',
  12765. ' this.$assign = function (s) {',
  12766. ' this.Bold = s.Bold;',
  12767. ' return this;',
  12768. ' };',
  12769. '});',
  12770. 'this.S = this.T$a.$new();',
  12771. 'this.T = this.T$a.$new();',
  12772. 'this.b = false;',
  12773. '']),
  12774. LinesToStr([ // $mod.$main
  12775. '$mod.S.$assign($mod.T);',
  12776. '$mod.b = $mod.S.$eq($mod.T);',
  12777. '']));
  12778. end;
  12779. procedure TTestModule.TestRecordAnonym_Nested;
  12780. begin
  12781. StartProgram(false);
  12782. Add(['',
  12783. 'var S,T: record',
  12784. ' Bold: longint;',
  12785. ' Sub: record',
  12786. ' Color: word;',
  12787. ' end;',
  12788. ' end;',
  12789. ' b: boolean;',
  12790. 'begin',
  12791. ' S:=T;',
  12792. ' S.Sub:=T.Sub;',
  12793. ' S.Sub.Color:=T.Sub.Color+3;',
  12794. ' b:=s=t;',
  12795. ' b:=s.Sub=t.Sub;',
  12796. '']);
  12797. ConvertProgram;
  12798. CheckSource('TestRecordAnonym_Nested',
  12799. LinesToStr([ // statements
  12800. 'rtl.recNewT(this, "T$a", function () {',
  12801. ' this.Bold = 0;',
  12802. ' rtl.recNewT(this, "Sub$a", function () {',
  12803. ' this.Color = 0;',
  12804. ' this.$eq = function (b) {',
  12805. ' return this.Color === b.Color;',
  12806. ' };',
  12807. ' this.$assign = function (s) {',
  12808. ' this.Color = s.Color;',
  12809. ' return this;',
  12810. ' };',
  12811. ' });',
  12812. ' this.$new = function () {',
  12813. ' var r = Object.create(this);',
  12814. ' r.Sub = this.Sub$a.$new();',
  12815. ' return r;',
  12816. ' };',
  12817. ' this.$eq = function (b) {',
  12818. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12819. ' };',
  12820. ' this.$assign = function (s) {',
  12821. ' this.Bold = s.Bold;',
  12822. ' this.Sub.$assign(s.Sub);',
  12823. ' return this;',
  12824. ' };',
  12825. '}, true);',
  12826. 'this.S = this.T$a.$new();',
  12827. 'this.T = this.T$a.$new();',
  12828. 'this.b = false;',
  12829. '']),
  12830. LinesToStr([ // $mod.$main
  12831. '$mod.S.$assign($mod.T);',
  12832. '$mod.S.Sub.$assign($mod.T.Sub);',
  12833. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12834. '$mod.b = $mod.S.$eq($mod.T);',
  12835. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12836. '']));
  12837. end;
  12838. procedure TTestModule.TestRecordAnonym_Const;
  12839. begin
  12840. StartProgram(false);
  12841. Add(['',
  12842. 'var T: record',
  12843. ' Bold: longint;',
  12844. ' Sub: record',
  12845. ' Color: word;',
  12846. ' end;',
  12847. ' end = (Bold: 2; Sub: (Color: 3));',
  12848. 'begin',
  12849. '']);
  12850. ConvertProgram;
  12851. CheckSource('TestRecordAnonym_Const',
  12852. LinesToStr([ // statements
  12853. 'rtl.recNewT(this, "T$a", function () {',
  12854. ' this.Bold = 0;',
  12855. ' rtl.recNewT(this, "Sub$a", function () {',
  12856. ' this.Color = 0;',
  12857. ' this.$eq = function (b) {',
  12858. ' return this.Color === b.Color;',
  12859. ' };',
  12860. ' this.$assign = function (s) {',
  12861. ' this.Color = s.Color;',
  12862. ' return this;',
  12863. ' };',
  12864. ' });',
  12865. ' this.$new = function () {',
  12866. ' var r = Object.create(this);',
  12867. ' r.Sub = this.Sub$a.$new();',
  12868. ' return r;',
  12869. ' };',
  12870. ' this.$eq = function (b) {',
  12871. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12872. ' };',
  12873. ' this.$assign = function (s) {',
  12874. ' this.Bold = s.Bold;',
  12875. ' this.Sub.$assign(s.Sub);',
  12876. ' return this;',
  12877. ' };',
  12878. '}, true);',
  12879. 'this.T = this.T$a.$clone({',
  12880. ' Bold: 2,',
  12881. ' Sub: this.T$a.Sub$a.$clone({',
  12882. ' Color: 3',
  12883. ' })',
  12884. '});',
  12885. '']),
  12886. LinesToStr([ // $mod.$main
  12887. '']));
  12888. end;
  12889. procedure TTestModule.TestRecordAnonym_InFunction;
  12890. begin
  12891. StartProgram(false);
  12892. Add(['',
  12893. 'procedure Fly;',
  12894. 'var T: record',
  12895. ' Bold: longint;',
  12896. ' Sub: record',
  12897. ' Color: word;',
  12898. ' end;',
  12899. ' end = (Bold: 2; Sub: (Color: 3));',
  12900. 'begin',
  12901. 'end;',
  12902. 'begin',
  12903. '']);
  12904. ConvertProgram;
  12905. CheckSource('TestRecordAnonym_InFunction',
  12906. LinesToStr([ // statements
  12907. 'var T$a = rtl.recNewT(null, "", function () {',
  12908. ' this.Bold = 0;',
  12909. ' rtl.recNewT(this, "Sub$a", function () {',
  12910. ' this.Color = 0;',
  12911. ' this.$eq = function (b) {',
  12912. ' return this.Color === b.Color;',
  12913. ' };',
  12914. ' this.$assign = function (s) {',
  12915. ' this.Color = s.Color;',
  12916. ' return this;',
  12917. ' };',
  12918. ' });',
  12919. ' this.$new = function () {',
  12920. ' var r = Object.create(this);',
  12921. ' r.Sub = this.Sub$a.$new();',
  12922. ' return r;',
  12923. ' };',
  12924. ' this.$eq = function (b) {',
  12925. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12926. ' };',
  12927. ' this.$assign = function (s) {',
  12928. ' this.Bold = s.Bold;',
  12929. ' this.Sub.$assign(s.Sub);',
  12930. ' return this;',
  12931. ' };',
  12932. '}, true);',
  12933. 'this.Fly = function () {',
  12934. ' var T = T$a.$clone({',
  12935. ' Bold: 2,',
  12936. ' Sub: T$a.Sub$a.$clone({',
  12937. ' Color: 3',
  12938. ' })',
  12939. ' });',
  12940. '};',
  12941. '']),
  12942. LinesToStr([ // $mod.$main
  12943. '']));
  12944. end;
  12945. procedure TTestModule.TestAdvRecord_Function;
  12946. begin
  12947. StartProgram(false);
  12948. Parser.Options:=Parser.Options+[po_cassignments];
  12949. Add([
  12950. '{$modeswitch AdvancedRecords}',
  12951. 'type',
  12952. ' TPoint = record',
  12953. ' x,y: word;',
  12954. ' function Add(const apt: TPoint): TPoint;',
  12955. ' end;',
  12956. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12957. 'begin',
  12958. ' Result:=Self;',
  12959. ' Result.x+=apt.x;',
  12960. ' Result.y:=Result.y+apt.y;',
  12961. ' Self:=apt;',
  12962. 'end;',
  12963. 'var p,q: TPoint;',
  12964. 'begin',
  12965. ' p.add(q);',
  12966. ' p:=default(TPoint);',
  12967. ' p:=q;',
  12968. '']);
  12969. ConvertProgram;
  12970. CheckSource('TestAdvRecord_Function',
  12971. LinesToStr([ // statements
  12972. 'rtl.recNewT(this, "TPoint", function () {',
  12973. ' this.x = 0;',
  12974. ' this.y = 0;',
  12975. ' this.$eq = function (b) {',
  12976. ' return (this.x === b.x) && (this.y === b.y);',
  12977. ' };',
  12978. ' this.$assign = function (s) {',
  12979. ' this.x = s.x;',
  12980. ' this.y = s.y;',
  12981. ' return this;',
  12982. ' };',
  12983. ' this.Add = function (apt) {',
  12984. ' var Result = $mod.TPoint.$new();',
  12985. ' Result.$assign(this);',
  12986. ' Result.x += apt.x;',
  12987. ' Result.y = Result.y + apt.y;',
  12988. ' this.$assign(apt);',
  12989. ' return Result;',
  12990. ' };',
  12991. '});',
  12992. 'this.p = this.TPoint.$new();',
  12993. 'this.q = this.TPoint.$new();',
  12994. '']),
  12995. LinesToStr([ // $mod.$main
  12996. '$mod.p.Add($mod.q);',
  12997. '$mod.p.$assign($mod.TPoint.$new());',
  12998. '$mod.p.$assign($mod.q);',
  12999. '']));
  13000. end;
  13001. procedure TTestModule.TestAdvRecord_Property;
  13002. begin
  13003. StartProgram(false);
  13004. Add([
  13005. '{$modeswitch AdvancedRecords}',
  13006. 'type',
  13007. ' TPoint = record',
  13008. ' x,y: word;',
  13009. ' strict private',
  13010. ' function GetSize: longword;',
  13011. ' procedure SetSize(Value: longword);',
  13012. ' public',
  13013. ' property Size: longword read GetSize write SetSize;',
  13014. ' property Left: word read x write y;',
  13015. ' end;',
  13016. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13017. 'function TPoint.GetSize: longword;',
  13018. 'begin',
  13019. ' x:=y;',
  13020. ' Size:=Size;',
  13021. ' Left:=Left;',
  13022. 'end;',
  13023. 'procedure TPoint.SetSize(Value: longword);',
  13024. 'begin',
  13025. 'end;',
  13026. 'var p,q: TPoint;',
  13027. 'begin',
  13028. ' p.Size:=q.Size;',
  13029. ' p.Left:=q.Left;',
  13030. '']);
  13031. ConvertProgram;
  13032. CheckSource('TestAdvRecord_Property',
  13033. LinesToStr([ // statements
  13034. 'rtl.recNewT(this, "TPoint", function () {',
  13035. ' this.x = 0;',
  13036. ' this.y = 0;',
  13037. ' this.$eq = function (b) {',
  13038. ' return (this.x === b.x) && (this.y === b.y);',
  13039. ' };',
  13040. ' this.$assign = function (s) {',
  13041. ' this.x = s.x;',
  13042. ' this.y = s.y;',
  13043. ' return this;',
  13044. ' };',
  13045. ' this.GetSize = function () {',
  13046. ' var Result = 0;',
  13047. ' this.x = this.y;',
  13048. ' this.SetSize(this.GetSize());',
  13049. ' this.y = this.x;',
  13050. ' return Result;',
  13051. ' };',
  13052. ' this.SetSize = function (Value) {',
  13053. ' };',
  13054. '});',
  13055. 'this.SetSize = function (Value) {',
  13056. '};',
  13057. 'this.p = this.TPoint.$new();',
  13058. 'this.q = this.TPoint.$new();',
  13059. '']),
  13060. LinesToStr([ // $mod.$main
  13061. '$mod.p.SetSize($mod.q.GetSize());',
  13062. '$mod.p.y = $mod.q.x;',
  13063. '']));
  13064. end;
  13065. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13066. begin
  13067. StartProgram(false);
  13068. Add([
  13069. '{$modeswitch AdvancedRecords}',
  13070. 'type',
  13071. ' TPoint = record',
  13072. ' strict private',
  13073. ' function GetItems(Index: word): word;',
  13074. ' procedure SetItems(Index: word; Value: word);',
  13075. ' public',
  13076. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13077. ' end;',
  13078. 'function TPoint.GetItems(Index: word): word;',
  13079. 'begin',
  13080. ' Items[index]:=Items[index];',
  13081. ' self.Items[index]:=self.Items[index];',
  13082. 'end;',
  13083. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13084. 'begin',
  13085. 'end;',
  13086. 'var p: TPoint;',
  13087. 'begin',
  13088. ' p[1]:=p[2];',
  13089. ' p.Items[3]:=p.Items[4];',
  13090. '']);
  13091. ConvertProgram;
  13092. CheckSource('TestAdvRecord_PropertyDefault',
  13093. LinesToStr([ // statements
  13094. 'rtl.recNewT(this, "TPoint", function () {',
  13095. ' this.$eq = function (b) {',
  13096. ' return true;',
  13097. ' };',
  13098. ' this.$assign = function (s) {',
  13099. ' return this;',
  13100. ' };',
  13101. ' this.GetItems = function (Index) {',
  13102. ' var Result = 0;',
  13103. ' this.SetItems(Index, this.GetItems(Index));',
  13104. ' this.SetItems(Index, this.GetItems(Index));',
  13105. ' return Result;',
  13106. ' };',
  13107. ' this.SetItems = function (Index, Value) {',
  13108. ' };',
  13109. '});',
  13110. 'this.p = this.TPoint.$new();',
  13111. '']),
  13112. LinesToStr([ // $mod.$main
  13113. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13114. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13115. '']));
  13116. end;
  13117. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13118. begin
  13119. StartProgram(false);
  13120. Add([
  13121. '{$modeswitch AdvancedRecords}',
  13122. 'type',
  13123. ' TRec = record',
  13124. ' class var',
  13125. ' Fx: longint;',
  13126. ' Fy: longint;',
  13127. ' class function GetInt: longint; static;',
  13128. ' class procedure SetInt(Value: longint); static;',
  13129. ' class procedure DoIt; static;',
  13130. ' class property IntA: longint read Fx write Fy;',
  13131. ' class property IntB: longint read GetInt write SetInt;',
  13132. ' end;',
  13133. 'class function trec.getint: longint;',
  13134. 'begin',
  13135. ' result:=fx;',
  13136. 'end;',
  13137. 'class procedure trec.setint(value: longint);',
  13138. 'begin',
  13139. 'end;',
  13140. 'class procedure trec.doit;',
  13141. 'begin',
  13142. ' IntA:=IntA+1;',
  13143. ' IntB:=IntB+1;',
  13144. 'end;',
  13145. 'var r: trec;',
  13146. 'begin',
  13147. ' trec.inta:=trec.inta+1;',
  13148. ' if trec.intb=2 then;',
  13149. ' trec.intb:=trec.intb+2;',
  13150. ' trec.setint(trec.inta);',
  13151. ' r.inta:=r.inta+1;',
  13152. ' if r.intb=2 then;',
  13153. ' r.intb:=r.intb+2;',
  13154. ' r.setint(r.inta);']);
  13155. ConvertProgram;
  13156. CheckSource('TestAdvRecord_Property_ClassMethod',
  13157. LinesToStr([ // statements
  13158. 'rtl.recNewT(this, "TRec", function () {',
  13159. ' this.Fx = 0;',
  13160. ' this.Fy = 0;',
  13161. ' this.$eq = function (b) {',
  13162. ' return true;',
  13163. ' };',
  13164. ' this.$assign = function (s) {',
  13165. ' return this;',
  13166. ' };',
  13167. ' this.GetInt = function () {',
  13168. ' var Result = 0;',
  13169. ' Result = $mod.TRec.Fx;',
  13170. ' return Result;',
  13171. ' };',
  13172. ' this.SetInt = function (Value) {',
  13173. ' };',
  13174. ' this.DoIt = function () {',
  13175. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13176. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13177. ' };',
  13178. '}, true);',
  13179. 'this.r = this.TRec.$new();',
  13180. '']),
  13181. LinesToStr([ // $mod.$main
  13182. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13183. 'if ($mod.TRec.GetInt() === 2) ;',
  13184. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13185. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13186. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13187. 'if ($mod.TRec.GetInt() === 2) ;',
  13188. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13189. '$mod.TRec.SetInt($mod.r.Fx);',
  13190. '']));
  13191. end;
  13192. procedure TTestModule.TestAdvRecord_Const;
  13193. begin
  13194. StartProgram(false);
  13195. Add([
  13196. '{$modeswitch AdvancedRecords}',
  13197. 'type',
  13198. ' TArrInt = array[3..4] of longint;',
  13199. ' TPoint = record',
  13200. ' x,y: longint;',
  13201. ' class var Count: nativeint;',
  13202. ' end;',
  13203. ' TRec = record',
  13204. ' i: longint;',
  13205. ' a: array of longint;',
  13206. ' s: array[1..2] of longint;',
  13207. ' m: array[1..2,3..4] of longint;',
  13208. ' p: TPoint;',
  13209. ' end;',
  13210. ' TPoints = array of TPoint;',
  13211. 'const',
  13212. ' r: TRec = (',
  13213. ' i:1;',
  13214. ' a:(2,3);',
  13215. ' s:(4,5);',
  13216. ' m:( (11,12), (13,14) );',
  13217. ' p: (x:21)',
  13218. ' );',
  13219. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13220. 'begin']);
  13221. ConvertProgram;
  13222. CheckSource('TestAdvRecord_Const',
  13223. LinesToStr([ // statements
  13224. 'rtl.recNewT(this, "TPoint", function () {',
  13225. ' this.x = 0;',
  13226. ' this.y = 0;',
  13227. ' this.Count = 0;',
  13228. ' this.$eq = function (b) {',
  13229. ' return (this.x === b.x) && (this.y === b.y);',
  13230. ' };',
  13231. ' this.$assign = function (s) {',
  13232. ' this.x = s.x;',
  13233. ' this.y = s.y;',
  13234. ' return this;',
  13235. ' };',
  13236. '}, true);',
  13237. 'rtl.recNewT(this, "TRec", function () {',
  13238. ' this.i = 0;',
  13239. ' this.m$a$clone = function (a) {',
  13240. ' var b = [];',
  13241. ' b.length = 2;',
  13242. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13243. ' return b;',
  13244. ' };',
  13245. ' this.$new = function () {',
  13246. ' var r = Object.create(this);',
  13247. ' r.a = [];',
  13248. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13249. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13250. ' r.p = $mod.TPoint.$new();',
  13251. ' return r;',
  13252. ' };',
  13253. ' this.$eq = function (b) {',
  13254. ' 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);',
  13255. ' };',
  13256. ' this.$assign = function (s) {',
  13257. ' this.i = s.i;',
  13258. ' this.a = rtl.arrayRef(s.a);',
  13259. ' this.s = s.s.slice(0);',
  13260. ' this.m = this.m$a$clone(s.m);',
  13261. ' this.p.$assign(s.p);',
  13262. ' return this;',
  13263. ' };',
  13264. '});',
  13265. 'this.r = this.TRec.$clone({',
  13266. ' i: 1,',
  13267. ' a: [2, 3],',
  13268. ' s: [4, 5],',
  13269. ' m: [[11, 12], [13, 14]],',
  13270. ' p: this.TPoint.$clone({',
  13271. ' x: 21,',
  13272. ' y: 0',
  13273. ' })',
  13274. '});',
  13275. 'this.p = [this.TPoint.$clone({',
  13276. ' x: 1,',
  13277. ' y: 2',
  13278. '}), this.TPoint.$clone({',
  13279. ' x: 3,',
  13280. ' y: 4',
  13281. '})];',
  13282. '']),
  13283. LinesToStr([ // $mod.$main
  13284. '']));
  13285. end;
  13286. procedure TTestModule.TestAdvRecord_ExternalField;
  13287. begin
  13288. StartProgram(false);
  13289. Add([
  13290. '{$modeswitch AdvancedRecords}',
  13291. '{$modeswitch externalclass}',
  13292. 'type',
  13293. ' TCar = record',
  13294. ' public',
  13295. ' Intern: longint external name ''$Intern'';',
  13296. ' Intern2: longint external name ''$Intern2'';',
  13297. ' Bracket: longint external name ''["A B"]'';',
  13298. ' procedure DoIt;',
  13299. ' end;',
  13300. 'procedure tcar.doit;',
  13301. 'begin',
  13302. ' Intern:=Intern+1;',
  13303. ' Intern2:=Intern2+2;',
  13304. ' Bracket:=Bracket+3;',
  13305. 'end;',
  13306. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13307. 'begin',
  13308. ' Rec.intern:=Rec.intern+1;',
  13309. ' Rec.intern2:=Rec.intern2+2;',
  13310. ' Rec.Bracket:=Rec.Bracket+3;',
  13311. ' with Rec do begin',
  13312. ' intern:=intern+1;',
  13313. ' intern2:=intern2+2;',
  13314. ' Bracket:=Bracket+3;',
  13315. ' end;']);
  13316. ConvertProgram;
  13317. CheckSource('TestAdvRecord_ExternalField',
  13318. LinesToStr([ // statements
  13319. 'rtl.recNewT(this, "TCar", function () {',
  13320. ' this.$eq = function (b) {',
  13321. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13322. ' };',
  13323. ' this.$assign = function (s) {',
  13324. ' this.$Intern = s.$Intern;',
  13325. ' this.$Intern2 = s.$Intern2;',
  13326. ' this["A B"] = s["A B"];',
  13327. ' return this;',
  13328. ' };',
  13329. ' this.DoIt = function () {',
  13330. ' this.$Intern = this.$Intern + 1;',
  13331. ' this.$Intern2 = this.$Intern2 + 2;',
  13332. ' this["A B"] = this["A B"] + 3;',
  13333. ' };',
  13334. '});',
  13335. 'this.Rec = this.TCar.$clone({',
  13336. ' $Intern: 11,',
  13337. ' $Intern2: 12,',
  13338. ' "A B": 13',
  13339. '});',
  13340. '']),
  13341. LinesToStr([ // $mod.$main
  13342. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13343. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13344. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13345. 'var $with = $mod.Rec;',
  13346. '$with.$Intern = $with.$Intern + 1;',
  13347. '$with.$Intern2 = $with.$Intern2 + 2;',
  13348. '$with["A B"] = $with["A B"] + 3;',
  13349. '']));
  13350. end;
  13351. procedure TTestModule.TestAdvRecord_SubRecord;
  13352. begin
  13353. StartProgram(false);
  13354. Add([
  13355. '{$modeswitch AdvancedRecords}',
  13356. 'type',
  13357. ' TRec = record',
  13358. ' type',
  13359. ' TPoint = record',
  13360. ' x,y: longint;',
  13361. ' class var Count: nativeint;',
  13362. ' procedure DoIt;',
  13363. ' class procedure DoThat; static;',
  13364. ' end;',
  13365. ' var',
  13366. ' i: longint;',
  13367. ' p: TPoint;',
  13368. ' procedure DoSome;',
  13369. ' end;',
  13370. 'const',
  13371. ' r: TRec = (',
  13372. ' i:1;',
  13373. ' p: (x:21;y:22)',
  13374. ' );',
  13375. 'procedure TRec.DoSome;',
  13376. 'begin',
  13377. ' p.x:=p.y+1;',
  13378. ' p.Count:=p.Count+2;',
  13379. 'end;',
  13380. 'procedure TRec.TPoint.DoIt;',
  13381. 'begin',
  13382. ' Count:=Count+3;',
  13383. 'end;',
  13384. 'class procedure TRec.TPoint.DoThat;',
  13385. 'begin',
  13386. ' Count:=Count+4;',
  13387. 'end;',
  13388. 'begin']);
  13389. ConvertProgram;
  13390. CheckSource('TestAdvRecord_SubRecord',
  13391. LinesToStr([ // statements
  13392. 'rtl.recNewT(this, "TRec", function () {',
  13393. ' rtl.recNewT(this, "TPoint", function () {',
  13394. ' this.x = 0;',
  13395. ' this.y = 0;',
  13396. ' this.Count = 0;',
  13397. ' this.$eq = function (b) {',
  13398. ' return (this.x === b.x) && (this.y === b.y);',
  13399. ' };',
  13400. ' this.$assign = function (s) {',
  13401. ' this.x = s.x;',
  13402. ' this.y = s.y;',
  13403. ' return this;',
  13404. ' };',
  13405. ' this.DoIt = function () {',
  13406. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13407. ' };',
  13408. ' this.DoThat = function () {',
  13409. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13410. ' };',
  13411. ' }, true);',
  13412. ' this.i = 0;',
  13413. ' this.$new = function () {',
  13414. ' var r = Object.create(this);',
  13415. ' r.p = this.TPoint.$new();',
  13416. ' return r;',
  13417. ' };',
  13418. ' this.$eq = function (b) {',
  13419. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13420. ' };',
  13421. ' this.$assign = function (s) {',
  13422. ' this.i = s.i;',
  13423. ' this.p.$assign(s.p);',
  13424. ' return this;',
  13425. ' };',
  13426. ' this.DoSome = function () {',
  13427. ' this.p.x = this.p.y + 1;',
  13428. ' this.TPoint.Count = this.p.Count + 2;',
  13429. ' };',
  13430. '}, true);',
  13431. 'this.r = this.TRec.$clone({',
  13432. ' i: 1,',
  13433. ' p: this.TRec.TPoint.$clone({',
  13434. ' x: 21,',
  13435. ' y: 22',
  13436. ' })',
  13437. '});',
  13438. '']),
  13439. LinesToStr([ // $mod.$main
  13440. '']));
  13441. end;
  13442. procedure TTestModule.TestAdvRecord_SubClass;
  13443. begin
  13444. StartProgram(false);
  13445. Add([
  13446. '{$modeswitch AdvancedRecords}',
  13447. 'type',
  13448. ' TObject = class end;',
  13449. ' TPoint = record',
  13450. ' type',
  13451. ' TBird = class',
  13452. ' procedure DoIt;',
  13453. ' class procedure Glob;',
  13454. ' end;',
  13455. ' procedure DoIt(b: TBird);',
  13456. ' end;',
  13457. 'procedure TPoint.TBird.DoIt;',
  13458. 'begin',
  13459. ' doit;',
  13460. ' self.doit;',
  13461. ' glob;',
  13462. ' self.glob;',
  13463. 'end;',
  13464. 'class procedure TPoint.TBird.Glob;',
  13465. 'begin',
  13466. ' glob;',
  13467. ' self.glob;',
  13468. 'end;',
  13469. 'procedure TPoint.DoIt(b: TBird);',
  13470. 'begin',
  13471. ' b.doit;',
  13472. ' b.glob;',
  13473. ' TBird.glob;',
  13474. 'end;',
  13475. 'begin',
  13476. '']);
  13477. ConvertProgram;
  13478. CheckSource('TestAdvRecord_SubClass',
  13479. LinesToStr([ // statements
  13480. 'rtl.createClass(this, "TObject", null, function () {',
  13481. ' this.$init = function () {',
  13482. ' };',
  13483. ' this.$final = function () {',
  13484. ' };',
  13485. '});',
  13486. 'rtl.recNewT(this, "TPoint", function () {',
  13487. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13488. ' this.DoIt = function () {',
  13489. ' this.DoIt();',
  13490. ' this.DoIt();',
  13491. ' this.$class.Glob();',
  13492. ' this.$class.Glob();',
  13493. ' };',
  13494. ' this.Glob = function () {',
  13495. ' this.Glob();',
  13496. ' this.Glob();',
  13497. ' };',
  13498. ' }, "TPoint.TBird");',
  13499. ' this.$eq = function (b) {',
  13500. ' return true;',
  13501. ' };',
  13502. ' this.$assign = function (s) {',
  13503. ' return this;',
  13504. ' };',
  13505. ' this.DoIt = function (b) {',
  13506. ' b.DoIt();',
  13507. ' b.$class.Glob();',
  13508. ' this.TBird.Glob();',
  13509. ' };',
  13510. '}, true);',
  13511. '']),
  13512. LinesToStr([ // $mod.$main
  13513. '']));
  13514. end;
  13515. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13516. begin
  13517. StartProgram(false);
  13518. Add([
  13519. '{$modeswitch AdvancedRecords}',
  13520. 'type',
  13521. ' IUnknown = interface end;',
  13522. ' TPoint = record',
  13523. ' type IBird = interface end;',
  13524. ' end;',
  13525. 'begin',
  13526. '']);
  13527. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13528. nNotYetImplemented);
  13529. ParseProgram;
  13530. end;
  13531. procedure TTestModule.TestAdvRecord_Constructor;
  13532. begin
  13533. StartProgram(false);
  13534. Add([
  13535. '{$modeswitch AdvancedRecords}',
  13536. 'type',
  13537. ' TPoint = record',
  13538. ' x,y: longint;',
  13539. ' class procedure Run(w: longint = 13); static;',
  13540. ' constructor Create(ax: longint; ay: longint = -1);',
  13541. ' end;',
  13542. 'class procedure tpoint.run(w: longint);',
  13543. 'begin',
  13544. ' run;',
  13545. ' run();',
  13546. 'end;',
  13547. 'constructor tpoint.create(ax,ay: longint);',
  13548. 'begin',
  13549. ' x:=ax;',
  13550. ' self.y:=ay;',
  13551. ' run;',
  13552. ' run(ax);',
  13553. 'end;',
  13554. 'var r: TPoint;',
  13555. 'begin',
  13556. ' r:=TPoint.Create(1,2);',
  13557. ' with TPoint do r:=Create(1,2);',
  13558. ' r.Create(3);',
  13559. ' r:=r.Create(4);',
  13560. '']);
  13561. ConvertProgram;
  13562. CheckSource('TestAdvRecord_Constructor',
  13563. LinesToStr([ // statements
  13564. 'rtl.recNewT(this, "TPoint", function () {',
  13565. ' this.x = 0;',
  13566. ' this.y = 0;',
  13567. ' this.$eq = function (b) {',
  13568. ' return (this.x === b.x) && (this.y === b.y);',
  13569. ' };',
  13570. ' this.$assign = function (s) {',
  13571. ' this.x = s.x;',
  13572. ' this.y = s.y;',
  13573. ' return this;',
  13574. ' };',
  13575. ' this.Run = function (w) {',
  13576. ' $mod.TPoint.Run(13);',
  13577. ' $mod.TPoint.Run(13);',
  13578. ' };',
  13579. ' this.Create = function (ax, ay) {',
  13580. ' this.x = ax;',
  13581. ' this.y = ay;',
  13582. ' this.Run(13);',
  13583. ' this.Run(ax);',
  13584. ' return this;',
  13585. ' };',
  13586. '});',
  13587. 'this.r = this.TPoint.$new();',
  13588. '']),
  13589. LinesToStr([ // $mod.$main
  13590. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13591. 'var $with = $mod.TPoint;',
  13592. '$mod.r.$assign($with.$new().Create(1, 2));',
  13593. '$mod.r.Create(3, -1);',
  13594. '$mod.r.$assign($mod.r.Create(4, -1));',
  13595. '']));
  13596. end;
  13597. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13598. begin
  13599. StartProgram(false);
  13600. Add([
  13601. '{$modeswitch AdvancedRecords}',
  13602. 'type',
  13603. ' TPoint = record',
  13604. ' class var x: longint;',
  13605. ' class procedure Fly; static;',
  13606. ' class constructor Init;',
  13607. ' end;',
  13608. 'var count: word;',
  13609. 'class procedure Tpoint.Fly;',
  13610. 'begin',
  13611. 'end;',
  13612. 'class constructor tpoint.init;',
  13613. 'begin',
  13614. ' count:=count+1;',
  13615. ' x:=x+3;',
  13616. ' tpoint.x:=tpoint.x+4;',
  13617. ' fly;',
  13618. ' tpoint.fly;',
  13619. 'end;',
  13620. 'var r: TPoint;',
  13621. 'begin',
  13622. ' r.x:=r.x+10;',
  13623. ' r.Fly;',
  13624. ' r.Fly();',
  13625. '']);
  13626. ConvertProgram;
  13627. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13628. LinesToStr([ // statements
  13629. 'rtl.recNewT(this, "TPoint", function () {',
  13630. ' this.x = 0;',
  13631. ' this.$eq = function (b) {',
  13632. ' return true;',
  13633. ' };',
  13634. ' this.$assign = function (s) {',
  13635. ' return this;',
  13636. ' };',
  13637. ' this.Fly = function () {',
  13638. ' };',
  13639. '}, true);',
  13640. 'this.count = 0;',
  13641. 'this.r = this.TPoint.$new();',
  13642. '']),
  13643. LinesToStr([ // $mod.$main
  13644. '(function () {',
  13645. ' $mod.count = $mod.count + 1;',
  13646. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13647. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13648. ' $mod.TPoint.Fly();',
  13649. ' $mod.TPoint.Fly();',
  13650. '})();',
  13651. '$mod.TPoint.x = $mod.r.x + 10;',
  13652. '$mod.TPoint.Fly();',
  13653. '$mod.TPoint.Fly();',
  13654. '']));
  13655. end;
  13656. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13657. begin
  13658. StartUnit(false);
  13659. Add([
  13660. 'interface',
  13661. '{$modeswitch AdvancedRecords}',
  13662. 'type',
  13663. ' TPoint = record',
  13664. ' class var x: longint;',
  13665. ' class procedure Fly; static;',
  13666. ' class constructor Init;',
  13667. ' end;',
  13668. 'implementation',
  13669. 'var count: word;',
  13670. 'class procedure Tpoint.Fly;',
  13671. 'begin',
  13672. 'end;',
  13673. 'class constructor tpoint.init;',
  13674. 'begin',
  13675. ' count:=count+1;',
  13676. ' x:=3;',
  13677. ' tpoint.x:=4;',
  13678. ' fly;',
  13679. ' tpoint.fly;',
  13680. 'end;',
  13681. '']);
  13682. ConvertUnit;
  13683. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13684. LinesToStr([ // statements
  13685. 'var $impl = $mod.$impl;',
  13686. 'rtl.recNewT(this, "TPoint", function () {',
  13687. ' this.x = 0;',
  13688. ' this.$eq = function (b) {',
  13689. ' return true;',
  13690. ' };',
  13691. ' this.$assign = function (s) {',
  13692. ' return this;',
  13693. ' };',
  13694. ' this.Fly = function () {',
  13695. ' };',
  13696. '}, true);',
  13697. '']),
  13698. LinesToStr([ // $mod.$init
  13699. '(function () {',
  13700. ' $impl.count = $impl.count + 1;',
  13701. ' $mod.TPoint.x = 3;',
  13702. ' $mod.TPoint.x = 4;',
  13703. ' $mod.TPoint.Fly();',
  13704. ' $mod.TPoint.Fly();',
  13705. '})();',
  13706. '']),
  13707. LinesToStr([ // $mod.$main
  13708. '$impl.count = 0;',
  13709. '']));
  13710. end;
  13711. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13712. begin
  13713. StartProgram(false);
  13714. Add(['type',
  13715. ' TObject = class',
  13716. ' public',
  13717. ' constructor Create;',
  13718. ' destructor Destroy;',
  13719. ' end;',
  13720. ' TBird = TObject;',
  13721. 'constructor tobject.create;',
  13722. 'begin end;',
  13723. 'destructor tobject.destroy;',
  13724. 'begin end;',
  13725. 'var Obj: tobject;',
  13726. 'begin',
  13727. ' obj:=tobject.create;',
  13728. ' obj:=tobject.create();',
  13729. ' obj:=tbird.create;',
  13730. ' obj:=tbird.create();',
  13731. ' obj:=obj.create();',
  13732. ' obj.destroy;',
  13733. '']);
  13734. ConvertProgram;
  13735. CheckSource('TestClass_TObjectDefaultConstructor',
  13736. LinesToStr([ // statements
  13737. 'rtl.createClass(this,"TObject",null,function(){',
  13738. ' this.$init = function () {',
  13739. ' };',
  13740. ' this.$final = function () {',
  13741. ' };',
  13742. ' this.Create = function(){',
  13743. ' return this;',
  13744. ' };',
  13745. ' this.Destroy = function(){',
  13746. ' };',
  13747. '});',
  13748. 'this.Obj = null;'
  13749. ]),
  13750. LinesToStr([ // $mod.$main
  13751. '$mod.Obj = $mod.TObject.$create("Create");',
  13752. '$mod.Obj = $mod.TObject.$create("Create");',
  13753. '$mod.Obj = $mod.TObject.$create("Create");',
  13754. '$mod.Obj = $mod.TObject.$create("Create");',
  13755. '$mod.Obj = $mod.Obj.Create();',
  13756. '$mod.Obj.$destroy("Destroy");',
  13757. '']));
  13758. end;
  13759. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13760. begin
  13761. StartProgram(false);
  13762. Add('type');
  13763. Add(' TObject = class');
  13764. Add(' public');
  13765. Add(' constructor Create(Par: longint);');
  13766. Add(' end;');
  13767. Add('constructor tobject.create(par: longint);');
  13768. Add('begin end;');
  13769. Add('var Obj: tobject;');
  13770. Add('begin');
  13771. Add(' obj:=tobject.create(3);');
  13772. ConvertProgram;
  13773. CheckSource('TestClass_TObjectConstructorWithParams',
  13774. LinesToStr([ // statements
  13775. 'rtl.createClass(this,"TObject",null,function(){',
  13776. ' this.$init = function () {',
  13777. ' };',
  13778. ' this.$final = function () {',
  13779. ' };',
  13780. ' this.Create = function(Par){',
  13781. ' return this;',
  13782. ' };',
  13783. '});',
  13784. 'this.Obj = null;'
  13785. ]),
  13786. LinesToStr([ // $mod.$main
  13787. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13788. ]));
  13789. end;
  13790. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13791. begin
  13792. StartProgram(false);
  13793. Add('type');
  13794. Add(' TObject = class');
  13795. Add(' public');
  13796. Add(' constructor Create;');
  13797. Add(' end;');
  13798. Add(' TTest = class(TObject)');
  13799. Add(' public');
  13800. Add(' constructor Create(const Par: longint = 1);');
  13801. Add(' end;');
  13802. Add('constructor tobject.create;');
  13803. Add('begin end;');
  13804. Add('constructor ttest.create(const par: longint);');
  13805. Add('begin end;');
  13806. Add('var t: ttest;');
  13807. Add('begin');
  13808. Add(' t:=ttest.create;');
  13809. Add(' t:=ttest.create(2);');
  13810. ConvertProgram;
  13811. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13812. LinesToStr([ // statements
  13813. 'rtl.createClass(this,"TObject",null,function(){',
  13814. ' this.$init = function () {',
  13815. ' };',
  13816. ' this.$final = function () {',
  13817. ' };',
  13818. ' this.Create = function(){',
  13819. ' return this;',
  13820. ' };',
  13821. '});',
  13822. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13823. ' this.Create$1 = function (Par) {',
  13824. ' return this;',
  13825. ' };',
  13826. '});',
  13827. 'this.t = null;'
  13828. ]),
  13829. LinesToStr([ // $mod.$main
  13830. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13831. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13832. ]));
  13833. end;
  13834. procedure TTestModule.TestClass_Var;
  13835. begin
  13836. StartProgram(false);
  13837. Add([
  13838. 'type',
  13839. ' TObject = class',
  13840. ' public',
  13841. ' vI: longint;',
  13842. ' constructor Create(Par: longint);',
  13843. ' end;',
  13844. 'constructor tobject.create(par: longint);',
  13845. 'begin',
  13846. ' vi:=par+3',
  13847. 'end;',
  13848. 'var Obj: tobject;',
  13849. 'begin',
  13850. ' obj:=tobject.create(4);',
  13851. ' obj.vi:=obj.VI+5;']);
  13852. ConvertProgram;
  13853. CheckSource('TestClass_Var',
  13854. LinesToStr([ // statements
  13855. 'rtl.createClass(this,"TObject",null,function(){',
  13856. ' this.$init = function () {',
  13857. ' this.vI = 0;',
  13858. ' };',
  13859. ' this.$final = function () {',
  13860. ' };',
  13861. ' this.Create = function(Par){',
  13862. ' this.vI = Par+3;',
  13863. ' return this;',
  13864. ' };',
  13865. '});',
  13866. 'this.Obj = null;'
  13867. ]),
  13868. LinesToStr([ // $mod.$main
  13869. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13870. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13871. ]));
  13872. end;
  13873. procedure TTestModule.TestClass_Method;
  13874. begin
  13875. StartProgram(false);
  13876. Add('type');
  13877. Add(' TObject = class');
  13878. Add(' public');
  13879. Add(' vI: longint;');
  13880. Add(' Sub: TObject;');
  13881. Add(' constructor Create;');
  13882. Add(' function GetIt(Par: longint): tobject;');
  13883. Add(' end;');
  13884. Add('constructor tobject.create; begin end;');
  13885. Add('function tobject.getit(par: longint): tobject;');
  13886. Add('begin');
  13887. Add(' Self.vi:=par+3;');
  13888. Add(' Result:=self.sub;');
  13889. Add('end;');
  13890. Add('var Obj: tobject;');
  13891. Add('begin');
  13892. Add(' obj:=tobject.create;');
  13893. Add(' obj.getit(4);');
  13894. Add(' obj.sub.sub:=nil;');
  13895. Add(' obj.sub.getit(5);');
  13896. Add(' obj.sub.getit(6).SUB:=nil;');
  13897. Add(' obj.sub.getit(7).GETIT(8);');
  13898. Add(' obj.sub.getit(9).SuB.getit(10);');
  13899. ConvertProgram;
  13900. CheckSource('TestClass_Method',
  13901. LinesToStr([ // statements
  13902. 'rtl.createClass(this,"TObject",null,function(){',
  13903. ' this.$init = function () {',
  13904. ' this.vI = 0;',
  13905. ' this.Sub = null;',
  13906. ' };',
  13907. ' this.$final = function () {',
  13908. ' this.Sub = undefined;',
  13909. ' };',
  13910. ' this.Create = function(){',
  13911. ' return this;',
  13912. ' };',
  13913. ' this.GetIt = function(Par){',
  13914. ' var Result = null;',
  13915. ' this.vI = Par + 3;',
  13916. ' Result = this.Sub;',
  13917. ' return Result;',
  13918. ' };',
  13919. '});',
  13920. 'this.Obj = null;'
  13921. ]),
  13922. LinesToStr([ // $mod.$main
  13923. '$mod.Obj = $mod.TObject.$create("Create");',
  13924. '$mod.Obj.GetIt(4);',
  13925. '$mod.Obj.Sub.Sub=null;',
  13926. '$mod.Obj.Sub.GetIt(5);',
  13927. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13928. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13929. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13930. ]));
  13931. end;
  13932. procedure TTestModule.TestClass_Implementation;
  13933. begin
  13934. StartUnit(false);
  13935. Add([
  13936. 'interface',
  13937. 'type',
  13938. ' TObject = class',
  13939. ' constructor Create;',
  13940. ' end;',
  13941. 'implementation',
  13942. 'type',
  13943. ' TIntClass = class',
  13944. ' constructor Create; reintroduce;',
  13945. ' class procedure DoGlob;',
  13946. ' end;',
  13947. 'constructor tintclass.create;',
  13948. 'begin',
  13949. ' inherited;',
  13950. ' inherited create;',
  13951. ' doglob;',
  13952. 'end;',
  13953. 'class procedure tintclass.doglob;',
  13954. 'begin',
  13955. 'end;',
  13956. 'constructor tobject.create;',
  13957. 'var',
  13958. ' iC: tintclass;',
  13959. 'begin',
  13960. ' ic:=tintclass.create;',
  13961. ' tintclass.doglob;',
  13962. ' ic.doglob;',
  13963. 'end;',
  13964. 'initialization',
  13965. ' tintclass.doglob;',
  13966. '']);
  13967. ConvertUnit;
  13968. CheckSource('TestClass_Implementation',
  13969. LinesToStr([ // statements
  13970. 'var $impl = $mod.$impl;',
  13971. 'rtl.createClass(this, "TObject", null, function () {',
  13972. ' this.$init = function () {',
  13973. ' };',
  13974. ' this.$final = function () {',
  13975. ' };',
  13976. ' this.Create = function () {',
  13977. ' var iC = null;',
  13978. ' iC = $impl.TIntClass.$create("Create$1");',
  13979. ' $impl.TIntClass.DoGlob();',
  13980. ' iC.$class.DoGlob();',
  13981. ' return this;',
  13982. ' };',
  13983. '});',
  13984. '']),
  13985. LinesToStr([ // $mod.$main
  13986. '$impl.TIntClass.DoGlob();',
  13987. '']),
  13988. LinesToStr([
  13989. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13990. ' this.Create$1 = function () {',
  13991. ' $mod.TObject.Create.call(this);',
  13992. ' $mod.TObject.Create.call(this);',
  13993. ' this.$class.DoGlob();',
  13994. ' return this;',
  13995. ' };',
  13996. ' this.DoGlob = function () {',
  13997. ' };',
  13998. '});',
  13999. '']));
  14000. end;
  14001. procedure TTestModule.TestClass_Inheritance;
  14002. begin
  14003. StartProgram(false);
  14004. Add('type');
  14005. Add(' TObject = class');
  14006. Add(' public');
  14007. Add(' constructor Create;');
  14008. Add(' end;');
  14009. Add(' TClassA = class');
  14010. Add(' end;');
  14011. Add(' TClassB = class(TObject)');
  14012. Add(' procedure ProcB;');
  14013. Add(' end;');
  14014. Add('constructor tobject.create; begin end;');
  14015. Add('procedure tclassb.procb; begin end;');
  14016. Add('var');
  14017. Add(' oO: TObject;');
  14018. Add(' oA: TClassA;');
  14019. Add(' oB: TClassB;');
  14020. Add('begin');
  14021. Add(' oO:=tobject.Create;');
  14022. Add(' oA:=tclassa.Create;');
  14023. Add(' ob:=tclassb.Create;');
  14024. Add(' if oo is tclassa then ;');
  14025. Add(' ob:=oo as tclassb;');
  14026. Add(' (oo as tclassb).procb;');
  14027. ConvertProgram;
  14028. CheckSource('TestClass_Inheritance',
  14029. LinesToStr([ // statements
  14030. 'rtl.createClass(this,"TObject",null,function(){',
  14031. ' this.$init = function () {',
  14032. ' };',
  14033. ' this.$final = function () {',
  14034. ' };',
  14035. ' this.Create = function () {',
  14036. ' return this;',
  14037. ' };',
  14038. '});',
  14039. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14040. '});',
  14041. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14042. ' this.ProcB = function () {',
  14043. ' };',
  14044. '});',
  14045. 'this.oO = null;',
  14046. 'this.oA = null;',
  14047. 'this.oB = null;'
  14048. ]),
  14049. LinesToStr([ // $mod.$main
  14050. '$mod.oO = $mod.TObject.$create("Create");',
  14051. '$mod.oA = $mod.TClassA.$create("Create");',
  14052. '$mod.oB = $mod.TClassB.$create("Create");',
  14053. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14054. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14055. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14056. ]));
  14057. end;
  14058. procedure TTestModule.TestClass_TypeAlias;
  14059. begin
  14060. StartProgram(false);
  14061. Add([
  14062. '{$interfaces corba}',
  14063. 'type',
  14064. ' IObject = interface',
  14065. ' end;',
  14066. ' IBird = type IObject;',
  14067. ' TObject = class',
  14068. ' end;',
  14069. ' TBird = type TObject;',
  14070. 'var',
  14071. ' oObj: TObject;',
  14072. ' oBird: TBird;',
  14073. ' IntfObj: IObject;',
  14074. ' IntfBird: IBird;',
  14075. 'begin',
  14076. ' oObj:=oBird;',
  14077. '']);
  14078. ConvertProgram;
  14079. CheckSource('TestClass_TypeAlias',
  14080. LinesToStr([ // statements
  14081. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14082. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14083. 'rtl.createClass(this, "TObject", null, function () {',
  14084. ' this.$init = function () {',
  14085. ' };',
  14086. ' this.$final = function () {',
  14087. ' };',
  14088. '});',
  14089. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14090. '});',
  14091. 'this.oObj = null;',
  14092. 'this.oBird = null;',
  14093. 'this.IntfObj = null;',
  14094. 'this.IntfBird = null;',
  14095. '']),
  14096. LinesToStr([ // $mod.$main
  14097. '$mod.oObj = $mod.oBird;',
  14098. '']));
  14099. end;
  14100. procedure TTestModule.TestClass_AbstractMethod;
  14101. begin
  14102. StartProgram(false);
  14103. Add('type');
  14104. Add(' TObject = class');
  14105. Add(' public');
  14106. Add(' procedure DoIt; virtual; abstract;');
  14107. Add(' end;');
  14108. Add('begin');
  14109. ConvertProgram;
  14110. CheckSource('TestClass_AbstractMethod',
  14111. LinesToStr([ // statements
  14112. 'rtl.createClass(this,"TObject",null,function(){',
  14113. ' this.$init = function () {',
  14114. ' };',
  14115. ' this.$final = function () {',
  14116. ' };',
  14117. '});'
  14118. ]),
  14119. LinesToStr([ // this.$main
  14120. ''
  14121. ]));
  14122. end;
  14123. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14124. begin
  14125. StartProgram(false);
  14126. Add([
  14127. 'type',
  14128. ' TObject = class',
  14129. ' procedure DoAbstract; virtual; abstract;',
  14130. ' procedure DoVirtual; virtual;',
  14131. ' procedure DoIt;',
  14132. ' end;',
  14133. ' TA = class',
  14134. ' procedure doabstract; override;',
  14135. ' procedure dovirtual; override;',
  14136. ' procedure DoSome;',
  14137. ' end;',
  14138. 'procedure tobject.dovirtual;',
  14139. 'begin',
  14140. ' inherited; // call non existing ancestor -> ignore silently',
  14141. 'end;',
  14142. 'procedure tobject.doit;',
  14143. 'begin',
  14144. 'end;',
  14145. 'procedure ta.doabstract;',
  14146. 'begin',
  14147. ' inherited dovirtual; // call TObject.DoVirtual',
  14148. 'end;',
  14149. 'procedure ta.dovirtual;',
  14150. 'begin',
  14151. ' inherited; // call TObject.DoVirtual',
  14152. ' inherited dovirtual; // call TObject.DoVirtual',
  14153. ' inherited dovirtual(); // call TObject.DoVirtual',
  14154. ' doit;',
  14155. ' doit();',
  14156. 'end;',
  14157. 'procedure ta.dosome;',
  14158. 'begin',
  14159. ' inherited; // call non existing ancestor method -> silently ignore',
  14160. 'end;',
  14161. 'begin']);
  14162. ConvertProgram;
  14163. CheckSource('TestClass_CallInherited_ProcNoParams',
  14164. LinesToStr([ // statements
  14165. 'rtl.createClass(this,"TObject",null,function(){',
  14166. ' this.$init = function () {',
  14167. ' };',
  14168. ' this.$final = function () {',
  14169. ' };',
  14170. ' this.DoVirtual = function () {',
  14171. ' };',
  14172. ' this.DoIt = function () {',
  14173. ' };',
  14174. '});',
  14175. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14176. ' this.DoAbstract = function () {',
  14177. ' $mod.TObject.DoVirtual.call(this);',
  14178. ' };',
  14179. ' this.DoVirtual = function () {',
  14180. ' $mod.TObject.DoVirtual.call(this);',
  14181. ' $mod.TObject.DoVirtual.call(this);',
  14182. ' $mod.TObject.DoVirtual.call(this);',
  14183. ' this.DoIt();',
  14184. ' this.DoIt();',
  14185. ' };',
  14186. ' this.DoSome = function () {',
  14187. ' };',
  14188. '});'
  14189. ]),
  14190. LinesToStr([ // this.$main
  14191. ''
  14192. ]));
  14193. end;
  14194. procedure TTestModule.TestClass_CallInherited_WithParams;
  14195. begin
  14196. StartProgram(false);
  14197. Add([
  14198. 'type',
  14199. ' TObject = class',
  14200. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14201. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14202. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14203. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14204. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14205. ' end;',
  14206. ' TClassA = class',
  14207. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14208. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14209. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14210. ' end;',
  14211. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14212. 'begin',
  14213. 'end;',
  14214. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14215. 'begin',
  14216. 'end;',
  14217. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14218. 'begin',
  14219. 'end;',
  14220. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14221. 'begin',
  14222. 'end;',
  14223. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14224. 'begin',
  14225. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14226. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14227. 'end;',
  14228. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14229. 'begin',
  14230. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14231. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14232. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14233. ' doit(pa,pb);',
  14234. ' doit(pa);',
  14235. ' doit2(pa);',
  14236. ' doit2;',
  14237. 'end;',
  14238. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14239. 'begin',
  14240. ' pa:=inherited;',
  14241. 'end;',
  14242. 'begin']);
  14243. ConvertProgram;
  14244. CheckSource('TestClass_CallInherited_WithParams',
  14245. LinesToStr([ // statements
  14246. 'rtl.createClass(this,"TObject",null,function(){',
  14247. ' this.$init = function () {',
  14248. ' };',
  14249. ' this.$final = function () {',
  14250. ' };',
  14251. ' this.DoVirtual = function (pA,pB) {',
  14252. ' };',
  14253. ' this.DoIt = function (pA,pB) {',
  14254. ' };',
  14255. ' this.DoIt2 = function (pA,pB) {',
  14256. ' };',
  14257. ' this.GetIt = function (pA, pB) {',
  14258. ' var Result = 0;',
  14259. ' return Result;',
  14260. ' };',
  14261. '});',
  14262. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14263. ' this.DoAbstract = function (pA,pB) {',
  14264. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14265. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14266. ' };',
  14267. ' this.DoVirtual = function (pA,pB) {',
  14268. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14269. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14270. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14271. ' this.DoIt(pA,pB);',
  14272. ' this.DoIt(pA,0);',
  14273. ' this.DoIt2(pA,2);',
  14274. ' this.DoIt2(1,2);',
  14275. ' };',
  14276. ' this.GetIt$1 = function (pA, pB) {',
  14277. ' var Result = 0;',
  14278. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14279. ' return Result;',
  14280. ' };',
  14281. '});'
  14282. ]),
  14283. LinesToStr([ // this.$main
  14284. ''
  14285. ]));
  14286. end;
  14287. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14288. begin
  14289. StartProgram(false);
  14290. Add('type');
  14291. Add(' TObject = class');
  14292. Add(' constructor Create; virtual;');
  14293. Add(' constructor CreateWithB(b: boolean);');
  14294. Add(' end;');
  14295. Add(' TA = class');
  14296. Add(' constructor Create; override;');
  14297. Add(' constructor CreateWithC(c: char);');
  14298. Add(' procedure DoIt;');
  14299. Add(' class function DoSome: TObject;');
  14300. Add(' end;');
  14301. Add('constructor tobject.create;');
  14302. Add('begin');
  14303. Add(' inherited; // call non existing ancestor -> ignore silently');
  14304. Add('end;');
  14305. Add('constructor tobject.createwithb(b: boolean);');
  14306. Add('begin');
  14307. Add(' inherited; // call non existing ancestor -> ignore silently');
  14308. Add(' create; // normal call');
  14309. Add('end;');
  14310. Add('constructor ta.create;');
  14311. Add('begin');
  14312. Add(' inherited; // normal call TObject.Create');
  14313. Add(' inherited create; // normal call TObject.Create');
  14314. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14315. Add('end;');
  14316. Add('constructor ta.createwithc(c: char);');
  14317. Add('begin');
  14318. Add(' inherited create; // call TObject.Create');
  14319. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14320. Add(' doit;');
  14321. Add(' doit();');
  14322. Add(' dosome;');
  14323. Add('end;');
  14324. Add('procedure ta.doit;');
  14325. Add('begin');
  14326. Add(' create; // normal call');
  14327. Add(' createwithb(false); // normal call');
  14328. Add(' createwithc(''c''); // normal call');
  14329. Add('end;');
  14330. Add('class function ta.dosome: TObject;');
  14331. Add('begin');
  14332. Add(' Result:=create; // constructor');
  14333. Add(' Result:=createwithb(true); // constructor');
  14334. Add(' Result:=createwithc(''c''); // constructor');
  14335. Add('end;');
  14336. Add('begin');
  14337. ConvertProgram;
  14338. CheckSource('TestClass_CallInheritedConstructor',
  14339. LinesToStr([ // statements
  14340. 'rtl.createClass(this,"TObject",null,function(){',
  14341. ' this.$init = function () {',
  14342. ' };',
  14343. ' this.$final = function () {',
  14344. ' };',
  14345. ' this.Create = function () {',
  14346. ' return this;',
  14347. ' };',
  14348. ' this.CreateWithB = function (b) {',
  14349. ' this.Create();',
  14350. ' return this;',
  14351. ' };',
  14352. '});',
  14353. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14354. ' this.Create = function () {',
  14355. ' $mod.TObject.Create.call(this);',
  14356. ' $mod.TObject.Create.call(this);',
  14357. ' $mod.TObject.CreateWithB.call(this, false);',
  14358. ' return this;',
  14359. ' };',
  14360. ' this.CreateWithC = function (c) {',
  14361. ' $mod.TObject.Create.call(this);',
  14362. ' $mod.TObject.CreateWithB.call(this, true);',
  14363. ' this.DoIt();',
  14364. ' this.DoIt();',
  14365. ' this.$class.DoSome();',
  14366. ' return this;',
  14367. ' };',
  14368. ' this.DoIt = function () {',
  14369. ' this.Create();',
  14370. ' this.CreateWithB(false);',
  14371. ' this.CreateWithC("c");',
  14372. ' };',
  14373. ' this.DoSome = function () {',
  14374. ' var Result = null;',
  14375. ' Result = this.$create("Create");',
  14376. ' Result = this.$create("CreateWithB", [true]);',
  14377. ' Result = this.$create("CreateWithC", ["c"]);',
  14378. ' return Result;',
  14379. ' };',
  14380. '});'
  14381. ]),
  14382. LinesToStr([ // this.$main
  14383. ''
  14384. ]));
  14385. end;
  14386. procedure TTestModule.TestClass_ClassVar_Assign;
  14387. begin
  14388. StartProgram(false);
  14389. Add([
  14390. 'type',
  14391. ' TObject = class',
  14392. ' public',
  14393. ' class var vI: longint;',
  14394. ' class var Sub: TObject;',
  14395. ' constructor Create;',
  14396. ' class function GetIt(var Par: longint): tobject;',
  14397. ' end;',
  14398. 'constructor tobject.create;',
  14399. 'begin',
  14400. ' vi:=vi+1;',
  14401. ' Self.vi:=Self.vi+1;',
  14402. ' inc(vi);',
  14403. 'end;',
  14404. 'class function tobject.getit(var par: longint): tobject;',
  14405. 'begin',
  14406. ' vi:=vi+3;',
  14407. ' Self.vi:=Self.vi+4;',
  14408. ' inc(vi);',
  14409. ' Result:=self.sub;',
  14410. ' GetIt(vi);',
  14411. 'end;',
  14412. 'var Obj: tobject;',
  14413. 'begin',
  14414. ' obj:=tobject.create;',
  14415. ' tobject.vi:=3;',
  14416. ' if tobject.vi=4 then ;',
  14417. ' tobject.sub:=nil;',
  14418. ' obj.sub:=nil;',
  14419. ' obj.sub.sub:=nil;']);
  14420. ConvertProgram;
  14421. CheckSource('TestClass_ClassVar_Assign',
  14422. LinesToStr([ // statements
  14423. 'rtl.createClass(this,"TObject",null,function(){',
  14424. ' this.vI = 0;',
  14425. ' this.Sub = null;',
  14426. ' this.$init = function () {',
  14427. ' };',
  14428. ' this.$final = function () {',
  14429. ' };',
  14430. ' this.Create = function(){',
  14431. ' $mod.TObject.vI = this.vI+1;',
  14432. ' $mod.TObject.vI = this.vI+1;',
  14433. ' $mod.TObject.vI += 1;',
  14434. ' return this;',
  14435. ' };',
  14436. ' this.GetIt = function(Par){',
  14437. ' var Result = null;',
  14438. ' $mod.TObject.vI = this.vI + 3;',
  14439. ' $mod.TObject.vI = this.vI + 4;',
  14440. ' $mod.TObject.vI += 1;',
  14441. ' Result = this.Sub;',
  14442. ' this.GetIt({',
  14443. ' p: $mod.TObject,',
  14444. ' get: function () {',
  14445. ' return this.p.vI;',
  14446. ' },',
  14447. ' set: function (v) {',
  14448. ' this.p.vI = v;',
  14449. ' }',
  14450. ' });',
  14451. ' return Result;',
  14452. ' };',
  14453. '});',
  14454. 'this.Obj = null;'
  14455. ]),
  14456. LinesToStr([ // $mod.$main
  14457. '$mod.Obj = $mod.TObject.$create("Create");',
  14458. '$mod.TObject.vI = 3;',
  14459. 'if ($mod.TObject.vI === 4);',
  14460. '$mod.TObject.Sub=null;',
  14461. '$mod.TObject.Sub=null;',
  14462. '$mod.TObject.Sub=null;',
  14463. '']));
  14464. end;
  14465. procedure TTestModule.TestClass_CallClassMethod;
  14466. begin
  14467. StartProgram(false);
  14468. Add('type');
  14469. Add(' TObject = class');
  14470. Add(' public');
  14471. Add(' class var vI: longint;');
  14472. Add(' class var Sub: TObject;');
  14473. Add(' constructor Create;');
  14474. Add(' function GetMore(Par: longint): longint;');
  14475. Add(' class function GetIt(Par: longint): tobject;');
  14476. Add(' end;');
  14477. Add('constructor tobject.create;');
  14478. Add('begin');
  14479. Add(' sub:=getit(3);');
  14480. Add(' vi:=getmore(4);');
  14481. Add(' sub:=Self.getit(5);');
  14482. Add(' vi:=Self.getmore(6);');
  14483. Add('end;');
  14484. Add('function tobject.getmore(par: longint): longint;');
  14485. Add('begin');
  14486. Add(' sub:=getit(11);');
  14487. Add(' vi:=getmore(12);');
  14488. Add(' sub:=self.getit(13);');
  14489. Add(' vi:=self.getmore(14);');
  14490. Add('end;');
  14491. Add('class function tobject.getit(par: longint): tobject;');
  14492. Add('begin');
  14493. Add(' sub:=getit(21);');
  14494. Add(' vi:=sub.getmore(22);');
  14495. Add(' sub:=self.getit(23);');
  14496. Add(' vi:=self.sub.getmore(24);');
  14497. Add('end;');
  14498. Add('var Obj: tobject;');
  14499. Add('begin');
  14500. Add(' obj:=tobject.create;');
  14501. Add(' tobject.getit(5);');
  14502. Add(' obj.getit(6);');
  14503. Add(' obj.sub.getit(7);');
  14504. Add(' obj.sub.getit(8).SUB:=nil;');
  14505. Add(' obj.sub.getit(9).GETIT(10);');
  14506. Add(' obj.sub.getit(11).SuB.getit(12);');
  14507. ConvertProgram;
  14508. CheckSource('TestClass_CallClassMethod',
  14509. LinesToStr([ // statements
  14510. 'rtl.createClass(this,"TObject",null,function(){',
  14511. ' this.vI = 0;',
  14512. ' this.Sub = null;',
  14513. ' this.$init = function () {',
  14514. ' };',
  14515. ' this.$final = function () {',
  14516. ' };',
  14517. ' this.Create = function(){',
  14518. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14519. ' $mod.TObject.vI = this.GetMore(4);',
  14520. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14521. ' $mod.TObject.vI = this.GetMore(6);',
  14522. ' return this;',
  14523. ' };',
  14524. ' this.GetMore = function(Par){',
  14525. ' var Result = 0;',
  14526. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14527. ' $mod.TObject.vI = this.GetMore(12);',
  14528. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14529. ' $mod.TObject.vI = this.GetMore(14);',
  14530. ' return Result;',
  14531. ' };',
  14532. ' this.GetIt = function(Par){',
  14533. ' var Result = null;',
  14534. ' $mod.TObject.Sub = this.GetIt(21);',
  14535. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14536. ' $mod.TObject.Sub = this.GetIt(23);',
  14537. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14538. ' return Result;',
  14539. ' };',
  14540. '});',
  14541. 'this.Obj = null;'
  14542. ]),
  14543. LinesToStr([ // $mod.$main
  14544. '$mod.Obj = $mod.TObject.$create("Create");',
  14545. '$mod.TObject.GetIt(5);',
  14546. '$mod.Obj.$class.GetIt(6);',
  14547. '$mod.Obj.Sub.$class.GetIt(7);',
  14548. '$mod.TObject.Sub=null;',
  14549. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14550. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14551. '']));
  14552. end;
  14553. procedure TTestModule.TestClass_CallClassMethodStatic;
  14554. begin
  14555. StartProgram(false);
  14556. Add([
  14557. 'type',
  14558. ' TObject = class',
  14559. ' public',
  14560. ' class function Fly: tobject; static;',
  14561. ' end;',
  14562. 'class function tobject.Fly: tobject;',
  14563. 'begin',
  14564. ' Result.Fly;',
  14565. ' Result.Fly();',
  14566. ' Fly;',
  14567. ' Fly();',
  14568. ' Fly.Fly;',
  14569. ' Fly.Fly();',
  14570. 'end;',
  14571. 'var Obj: tobject;',
  14572. 'begin',
  14573. ' obj.Fly;',
  14574. ' obj.Fly();',
  14575. ' with obj do begin',
  14576. ' Fly;',
  14577. ' Fly();',
  14578. ' end;',
  14579. '']);
  14580. ConvertProgram;
  14581. CheckSource('TestClass_CallClassMethodStatic',
  14582. LinesToStr([ // statements
  14583. 'rtl.createClass(this, "TObject", null, function () {',
  14584. ' this.$init = function () {',
  14585. ' };',
  14586. ' this.$final = function () {',
  14587. ' };',
  14588. ' this.Fly = function () {',
  14589. ' var Result = null;',
  14590. ' $mod.TObject.Fly();',
  14591. ' $mod.TObject.Fly();',
  14592. ' $mod.TObject.Fly();',
  14593. ' $mod.TObject.Fly();',
  14594. ' $mod.TObject.Fly();',
  14595. ' $mod.TObject.Fly();',
  14596. ' return Result;',
  14597. ' };',
  14598. '});',
  14599. 'this.Obj = null;'
  14600. ]),
  14601. LinesToStr([ // $mod.$main
  14602. '$mod.TObject.Fly();',
  14603. '$mod.TObject.Fly();',
  14604. 'var $with = $mod.Obj;',
  14605. '$with.Fly();',
  14606. '$with.Fly();',
  14607. '']));
  14608. end;
  14609. procedure TTestModule.TestClass_Property;
  14610. begin
  14611. StartProgram(false);
  14612. Add('type');
  14613. Add(' TObject = class');
  14614. Add(' Fx: longint;');
  14615. Add(' Fy: longint;');
  14616. Add(' function GetInt: longint;');
  14617. Add(' procedure SetInt(Value: longint);');
  14618. Add(' procedure DoIt;');
  14619. Add(' property IntA: longint read Fx write Fy;');
  14620. Add(' property IntB: longint read GetInt write SetInt;');
  14621. Add(' end;');
  14622. Add('function tobject.getint: longint;');
  14623. Add('begin');
  14624. Add(' result:=fx;');
  14625. Add('end;');
  14626. Add('procedure tobject.setint(value: longint);');
  14627. Add('begin');
  14628. Add(' if value=fy then exit;');
  14629. Add(' fy:=value;');
  14630. Add('end;');
  14631. Add('procedure tobject.doit;');
  14632. Add('begin');
  14633. Add(' IntA:=IntA+1;');
  14634. Add(' Self.IntA:=Self.IntA+1;');
  14635. Add(' IntB:=IntB+1;');
  14636. Add(' Self.IntB:=Self.IntB+1;');
  14637. Add('end;');
  14638. Add('var Obj: tobject;');
  14639. Add('begin');
  14640. Add(' obj.inta:=obj.inta+1;');
  14641. Add(' if obj.intb=2 then;');
  14642. Add(' obj.intb:=obj.intb+2;');
  14643. Add(' obj.setint(obj.inta);');
  14644. ConvertProgram;
  14645. CheckSource('TestClass_Property',
  14646. LinesToStr([ // statements
  14647. 'rtl.createClass(this, "TObject", null, function () {',
  14648. ' this.$init = function () {',
  14649. ' this.Fx = 0;',
  14650. ' this.Fy = 0;',
  14651. ' };',
  14652. ' this.$final = function () {',
  14653. ' };',
  14654. ' this.GetInt = function () {',
  14655. ' var Result = 0;',
  14656. ' Result = this.Fx;',
  14657. ' return Result;',
  14658. ' };',
  14659. ' this.SetInt = function (Value) {',
  14660. ' if (Value === this.Fy) return;',
  14661. ' this.Fy = Value;',
  14662. ' };',
  14663. ' this.DoIt = function () {',
  14664. ' this.Fy = this.Fx + 1;',
  14665. ' this.Fy = this.Fx + 1;',
  14666. ' this.SetInt(this.GetInt() + 1);',
  14667. ' this.SetInt(this.GetInt() + 1);',
  14668. ' };',
  14669. '});',
  14670. 'this.Obj = null;'
  14671. ]),
  14672. LinesToStr([ // $mod.$main
  14673. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14674. 'if ($mod.Obj.GetInt() === 2);',
  14675. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14676. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14677. ]));
  14678. end;
  14679. procedure TTestModule.TestClass_Property_ClassMethod;
  14680. begin
  14681. StartProgram(false);
  14682. Add([
  14683. 'type',
  14684. ' TObject = class',
  14685. ' class var Fx: longint;',
  14686. ' class var Fy: longint;',
  14687. ' class function GetInt: longint;',
  14688. ' class procedure SetInt(Value: longint);',
  14689. ' end;',
  14690. ' TBird = class',
  14691. ' class procedure DoIt;',
  14692. ' class property IntA: longint read Fx write Fy;',
  14693. ' class property IntB: longint read GetInt write SetInt;',
  14694. ' end;',
  14695. 'class function tobject.getint: longint;',
  14696. 'begin',
  14697. ' result:=fx;',
  14698. 'end;',
  14699. 'class procedure tobject.setint(value: longint);',
  14700. 'begin',
  14701. 'end;',
  14702. 'class procedure tbird.doit;',
  14703. 'begin',
  14704. ' FX:=3;',
  14705. ' IntA:=IntA+1;',
  14706. ' Self.IntA:=Self.IntA+1;',
  14707. ' IntB:=IntB+1;',
  14708. ' Self.IntB:=Self.IntB+1;',
  14709. ' with Self do begin',
  14710. ' FX:=11;',
  14711. ' IntA:=IntA+12;',
  14712. ' IntB:=IntB+13;',
  14713. ' end;',
  14714. 'end;',
  14715. 'var Obj: tbird;',
  14716. 'begin',
  14717. ' tbird.fx:=tbird.fx+1;',
  14718. ' tbird.inta:=tbird.inta+1;',
  14719. ' if tbird.intb=2 then;',
  14720. ' tbird.intb:=tbird.intb+2;',
  14721. ' tbird.setint(tbird.inta);',
  14722. ' obj.inta:=obj.inta+1;',
  14723. ' if obj.intb=2 then;',
  14724. ' obj.intb:=obj.intb+2;',
  14725. ' obj.setint(obj.inta);',
  14726. ' with Tbird do begin',
  14727. ' FX:=FY+1;',
  14728. ' inta:=inta+2;',
  14729. ' intb:=intb+3;',
  14730. ' end;',
  14731. ' with Obj do begin',
  14732. ' FX:=FY+1;',
  14733. ' inta:=inta+2;',
  14734. ' intb:=intb+3;',
  14735. ' end;',
  14736. '']);
  14737. ConvertProgram;
  14738. CheckSource('TestClass_Property_ClassMethod',
  14739. LinesToStr([ // statements
  14740. 'rtl.createClass(this, "TObject", null, function () {',
  14741. ' this.Fx = 0;',
  14742. ' this.Fy = 0;',
  14743. ' this.$init = function () {',
  14744. ' };',
  14745. ' this.$final = function () {',
  14746. ' };',
  14747. ' this.GetInt = function () {',
  14748. ' var Result = 0;',
  14749. ' Result = this.Fx;',
  14750. ' return Result;',
  14751. ' };',
  14752. ' this.SetInt = function (Value) {',
  14753. ' };',
  14754. '});',
  14755. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14756. ' this.DoIt = function () {',
  14757. ' $mod.TObject.Fx = 3;',
  14758. ' $mod.TObject.Fy = this.Fx + 1;',
  14759. ' $mod.TObject.Fy = this.Fx + 1;',
  14760. ' this.SetInt(this.GetInt() + 1);',
  14761. ' this.SetInt(this.GetInt() + 1);',
  14762. ' $mod.TObject.Fx = 11;',
  14763. ' $mod.TObject.Fy = this.Fx + 12;',
  14764. ' this.SetInt(this.GetInt() + 13);',
  14765. ' };',
  14766. '});',
  14767. 'this.Obj = null;'
  14768. ]),
  14769. LinesToStr([ // $mod.$main
  14770. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14771. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14772. 'if ($mod.TBird.GetInt() === 2);',
  14773. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14774. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14775. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14776. 'if ($mod.Obj.$class.GetInt() === 2);',
  14777. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14778. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14779. 'var $with = $mod.TBird;',
  14780. '$mod.TObject.Fx = $with.Fy + 1;',
  14781. '$mod.TObject.Fy = $with.Fx + 2;',
  14782. '$with.SetInt($with.GetInt() + 3);',
  14783. 'var $with1 = $mod.Obj;',
  14784. '$mod.TObject.Fx = $with1.Fy + 1;',
  14785. '$mod.TObject.Fy = $with1.Fx + 2;',
  14786. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14787. '']));
  14788. end;
  14789. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14790. begin
  14791. StartProgram(false);
  14792. Add([
  14793. 'type',
  14794. ' TObject = class',
  14795. ' class function GetInt: longint; static;',
  14796. ' class procedure SetInt(Value: longint); static;',
  14797. ' class function GetItems(Index: word): longint; static;',
  14798. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14799. ' end;',
  14800. ' TBird = class',
  14801. ' class procedure Fly;',
  14802. ' class property IntA: longint read GetInt write SetInt;',
  14803. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14804. ' end;',
  14805. 'class function tobject.getint: longint;',
  14806. 'begin',
  14807. 'end;',
  14808. 'class procedure tobject.setint(value: longint);',
  14809. 'begin',
  14810. 'end;',
  14811. 'class function tobject.GetItems(Index: word): longint;',
  14812. 'begin',
  14813. 'end;',
  14814. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14815. 'begin',
  14816. 'end;',
  14817. 'class procedure tbird.fly;',
  14818. 'var w: longint;',
  14819. 'begin',
  14820. ' inta:=inta+51;',
  14821. ' w:=items[52];',
  14822. ' items[53]:=54;',
  14823. 'end;',
  14824. 'var Obj: tbird;',
  14825. ' i: longint;',
  14826. 'begin',
  14827. ' tbird.inta:=tbird.inta+1;',
  14828. ' i:=tbird.items[2];',
  14829. ' tbird.items[3]:=4;',
  14830. ' obj.inta:=obj.inta+11;',
  14831. ' i:=obj.items[12];',
  14832. ' obj.items[13]:=14;',
  14833. ' with Tbird do begin',
  14834. ' inta:=inta+21;',
  14835. ' i:=items[22];',
  14836. ' items[23]:=24;',
  14837. ' end;',
  14838. ' with Obj do begin',
  14839. ' inta:=inta+31;',
  14840. ' i:=items[32];',
  14841. ' items[33]:=34;',
  14842. ' end;',
  14843. '']);
  14844. ConvertProgram;
  14845. CheckSource('TestClass_Property_ClassMethod',
  14846. LinesToStr([ // statements
  14847. 'rtl.createClass(this, "TObject", null, function () {',
  14848. ' this.$init = function () {',
  14849. ' };',
  14850. ' this.$final = function () {',
  14851. ' };',
  14852. ' this.GetInt = function () {',
  14853. ' var Result = 0;',
  14854. ' return Result;',
  14855. ' };',
  14856. ' this.SetInt = function (Value) {',
  14857. ' };',
  14858. ' this.GetItems = function (Index) {',
  14859. ' var Result = 0;',
  14860. ' return Result;',
  14861. ' };',
  14862. ' this.SetItems = function (Index, Value) {',
  14863. ' };',
  14864. '});',
  14865. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14866. ' this.Fly = function () {',
  14867. ' var w = 0;',
  14868. ' this.SetInt(this.GetInt() + 51);',
  14869. ' w = this.GetItems(52);',
  14870. ' this.SetItems(53, 54);',
  14871. ' };',
  14872. '});',
  14873. 'this.Obj = null;',
  14874. 'this.i = 0;',
  14875. '']),
  14876. LinesToStr([ // $mod.$main
  14877. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14878. '$mod.i = $mod.TObject.GetItems(2);',
  14879. '$mod.TObject.SetItems(3, 4);',
  14880. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14881. '$mod.i = $mod.TObject.GetItems(12);',
  14882. '$mod.TObject.SetItems(13, 14);',
  14883. 'var $with = $mod.TBird;',
  14884. '$with.SetInt($with.GetInt() + 21);',
  14885. '$mod.i = $with.GetItems(22);',
  14886. '$with.SetItems(23, 24);',
  14887. 'var $with1 = $mod.Obj;',
  14888. '$with1.SetInt($with1.GetInt() + 31);',
  14889. '$mod.i = $with1.GetItems(32);',
  14890. '$with1.SetItems(33, 34);',
  14891. '']));
  14892. end;
  14893. procedure TTestModule.TestClass_Property_Indexed;
  14894. begin
  14895. StartProgram(false);
  14896. Add([
  14897. 'type',
  14898. ' TObject = class',
  14899. ' FItems: array of longint;',
  14900. ' function GetItems(Index: longint): longint;',
  14901. ' procedure SetItems(Index: longint; Value: longint);',
  14902. ' procedure DoIt;',
  14903. ' property Items[Index: longint]: longint read getitems write setitems;',
  14904. ' end;',
  14905. 'function tobject.getitems(index: longint): longint;',
  14906. 'begin',
  14907. ' Result:=fitems[index];',
  14908. 'end;',
  14909. 'procedure tobject.setitems(index: longint; value: longint);',
  14910. 'begin',
  14911. ' fitems[index]:=value;',
  14912. 'end;',
  14913. 'procedure tobject.doit;',
  14914. 'begin',
  14915. ' items[1]:=2;',
  14916. ' items[3]:=items[4];',
  14917. ' self.items[5]:=self.items[6];',
  14918. ' items[items[7]]:=items[items[8]];',
  14919. 'end;',
  14920. 'var Obj: tobject;',
  14921. 'begin',
  14922. ' obj.Items[11]:=obj.Items[12];',
  14923. '']);
  14924. ConvertProgram;
  14925. CheckSource('TestClass_Property_Indexed',
  14926. LinesToStr([ // statements
  14927. 'rtl.createClass(this, "TObject", null, function () {',
  14928. ' this.$init = function () {',
  14929. ' this.FItems = [];',
  14930. ' };',
  14931. ' this.$final = function () {',
  14932. ' this.FItems = undefined;',
  14933. ' };',
  14934. ' this.GetItems = function (Index) {',
  14935. ' var Result = 0;',
  14936. ' Result = this.FItems[Index];',
  14937. ' return Result;',
  14938. ' };',
  14939. ' this.SetItems = function (Index, Value) {',
  14940. ' this.FItems[Index] = Value;',
  14941. ' };',
  14942. ' this.DoIt = function () {',
  14943. ' this.SetItems(1, 2);',
  14944. ' this.SetItems(3,this.GetItems(4));',
  14945. ' this.SetItems(5,this.GetItems(6));',
  14946. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14947. ' };',
  14948. '});',
  14949. 'this.Obj = null;'
  14950. ]),
  14951. LinesToStr([ // $mod.$main
  14952. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14953. ]));
  14954. end;
  14955. procedure TTestModule.TestClass_Property_IndexSpec;
  14956. begin
  14957. StartProgram(false);
  14958. Add([
  14959. 'type',
  14960. ' TEnum = (red, blue);',
  14961. ' TObject = class',
  14962. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14963. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14964. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14965. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14966. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14967. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14968. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14969. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14970. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14971. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14972. ' end;',
  14973. 'procedure DoIt(b: boolean); begin end;',
  14974. 'var',
  14975. ' o: TObject;',
  14976. 'begin',
  14977. ' o.B1:=o.B1;',
  14978. ' o.B2:=o.B2;',
  14979. ' o.B3:=o.B3;',
  14980. ' o.I1[''a'']:=o.I1[''b''];',
  14981. ' doit(o.b1);',
  14982. ' doit(o.b2);',
  14983. ' doit(o.i1[''c'']);',
  14984. '']);
  14985. ConvertProgram;
  14986. CheckSource('TestClass_Property_IndexSpec',
  14987. LinesToStr([ // statements
  14988. 'this.TEnum = {',
  14989. ' "0": "red",',
  14990. ' red: 0,',
  14991. ' "1": "blue",',
  14992. ' blue: 1',
  14993. '};',
  14994. 'rtl.createClass(this, "TObject", null, function () {',
  14995. ' this.$init = function () {',
  14996. ' };',
  14997. ' this.$final = function () {',
  14998. ' };',
  14999. '});',
  15000. 'this.DoIt = function (b) {',
  15001. '};',
  15002. 'this.o = null;',
  15003. '']),
  15004. LinesToStr([ // $mod.$main
  15005. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15006. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15007. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15008. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15009. '$mod.DoIt($mod.o.GetIntBool(1));',
  15010. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15011. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15012. '']));
  15013. end;
  15014. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15015. begin
  15016. StartProgram(false);
  15017. Add('type');
  15018. Add(' TArray = array of longint;');
  15019. Add(' TObject = class');
  15020. Add(' FItems: TArray;');
  15021. Add(' function GetItems: tarray;');
  15022. Add(' procedure SetItems(Value: tarray);');
  15023. Add(' property Items: tarray read getitems write setitems;');
  15024. Add(' procedure SetNumbers(const Value: tarray);');
  15025. Add(' property Numbers: tarray write setnumbers;');
  15026. Add(' end;');
  15027. Add('function tobject.getitems: tarray;');
  15028. Add('begin');
  15029. Add(' Result:=fitems;');
  15030. Add('end;');
  15031. Add('procedure tobject.setitems(value: tarray);');
  15032. Add('begin');
  15033. Add(' fitems:=value;');
  15034. Add(' fitems:=nil;');
  15035. Add(' Items:=nil;');
  15036. Add(' Items:=Items;');
  15037. Add(' Items[1]:=2;');
  15038. Add(' fitems[3]:=Items[4];');
  15039. Add(' Items[5]:=Items[6];');
  15040. Add(' Self.Items[7]:=8;');
  15041. Add(' Self.Items[9]:=Self.Items[10];');
  15042. Add(' Items[Items[11]]:=Items[Items[12]];');
  15043. Add('end;');
  15044. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15045. Add('begin;');
  15046. Add(' Numbers:=nil;');
  15047. Add(' Numbers:=Value;');
  15048. Add(' Self.Numbers:=Value;');
  15049. Add('end;');
  15050. Add('var Obj: tobject;');
  15051. Add('begin');
  15052. Add(' obj.items:=nil;');
  15053. Add(' obj.items:=obj.items;');
  15054. Add(' obj.items[11]:=obj.items[12];');
  15055. ConvertProgram;
  15056. CheckSource('TestClass_PropertyOfTypeArray',
  15057. LinesToStr([ // statements
  15058. 'rtl.createClass(this, "TObject", null, function () {',
  15059. ' this.$init = function () {',
  15060. ' this.FItems = [];',
  15061. ' };',
  15062. ' this.$final = function () {',
  15063. ' this.FItems = undefined;',
  15064. ' };',
  15065. ' this.GetItems = function () {',
  15066. ' var Result = [];',
  15067. ' Result = rtl.arrayRef(this.FItems);',
  15068. ' return Result;',
  15069. ' };',
  15070. ' this.SetItems = function (Value) {',
  15071. ' this.FItems = rtl.arrayRef(Value);',
  15072. ' this.FItems = [];',
  15073. ' this.SetItems([]);',
  15074. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15075. ' this.GetItems()[1] = 2;',
  15076. ' this.FItems[3] = this.GetItems()[4];',
  15077. ' this.GetItems()[5] = this.GetItems()[6];',
  15078. ' this.GetItems()[7] = 8;',
  15079. ' this.GetItems()[9] = this.GetItems()[10];',
  15080. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15081. ' };',
  15082. ' this.SetNumbers = function (Value) {',
  15083. ' this.SetNumbers([]);',
  15084. ' this.SetNumbers(Value);',
  15085. ' this.SetNumbers(Value);',
  15086. ' };',
  15087. '});',
  15088. 'this.Obj = null;'
  15089. ]),
  15090. LinesToStr([ // $mod.$main
  15091. '$mod.Obj.SetItems([]);',
  15092. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15093. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15094. ]));
  15095. end;
  15096. procedure TTestModule.TestClass_PropertyDefault;
  15097. begin
  15098. StartProgram(false);
  15099. Add([
  15100. 'type',
  15101. ' TArray = array of longint;',
  15102. ' TObject = class',
  15103. ' end;',
  15104. ' TBird = class',
  15105. ' FItems: TArray;',
  15106. ' function GetItems(Index: longint): longint;',
  15107. ' procedure SetItems(Index, Value: longint);',
  15108. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15109. ' end;',
  15110. 'function TBird.getitems(index: longint): longint;',
  15111. 'begin',
  15112. 'end;',
  15113. 'procedure TBird.setitems(index, value: longint);',
  15114. 'begin',
  15115. ' Self[1]:=2;',
  15116. ' Self[3]:=Self[index];',
  15117. ' Self[index]:=Self[Self[value]];',
  15118. ' Self[Self[4]]:=value;',
  15119. 'end;',
  15120. 'var',
  15121. ' Bird: TBird;',
  15122. ' Obj: TObject;',
  15123. 'begin',
  15124. ' bird[11]:=12;',
  15125. ' bird[13]:=bird[14];',
  15126. ' bird[Bird[15]]:=bird[Bird[15]];',
  15127. ' TBird(obj)[16]:=TBird(obj)[17];',
  15128. ' (obj as tbird)[18]:=19;',
  15129. '']);
  15130. ConvertProgram;
  15131. CheckSource('TestClass_PropertyDefault',
  15132. LinesToStr([ // statements
  15133. 'rtl.createClass(this, "TObject", null, function () {',
  15134. ' this.$init = function () {',
  15135. ' };',
  15136. ' this.$final = function () {',
  15137. ' };',
  15138. '});',
  15139. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15140. ' this.$init = function () {',
  15141. ' $mod.TObject.$init.call(this);',
  15142. ' this.FItems = [];',
  15143. ' };',
  15144. ' this.$final = function () {',
  15145. ' this.FItems = undefined;',
  15146. ' $mod.TObject.$final.call(this);',
  15147. ' };',
  15148. ' this.GetItems = function (Index) {',
  15149. ' var Result = 0;',
  15150. ' return Result;',
  15151. ' };',
  15152. ' this.SetItems = function (Index, Value) {',
  15153. ' this.SetItems(1, 2);',
  15154. ' this.SetItems(3, this.GetItems(Index));',
  15155. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15156. ' this.SetItems(this.GetItems(4), Value);',
  15157. ' };',
  15158. '});',
  15159. 'this.Bird = null;',
  15160. 'this.Obj = null;',
  15161. '']),
  15162. LinesToStr([ // $mod.$main
  15163. '$mod.Bird.SetItems(11, 12);',
  15164. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15165. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15166. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15167. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15168. '']));
  15169. end;
  15170. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15171. begin
  15172. StartProgram(false);
  15173. Add([
  15174. 'type',
  15175. ' TObject = class end;',
  15176. ' TAlphaList = class',
  15177. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15178. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15179. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15180. ' end;',
  15181. ' TBetaList = class',
  15182. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15183. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15184. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15185. ' end;',
  15186. ' TBird = class',
  15187. ' procedure DoIt;',
  15188. ' end;',
  15189. 'procedure TBird.DoIt;',
  15190. 'var',
  15191. ' List: TAlphaList;',
  15192. 'begin',
  15193. ' if TBetaList(List[true])[3]=nil then ;',
  15194. ' TBetaList(List[false])[5]:=nil;',
  15195. 'end;',
  15196. 'var',
  15197. ' List: TAlphaList;',
  15198. 'begin',
  15199. ' if TBetaList(List[true])[3]=nil then ;',
  15200. ' TBetaList(List[false])[5]:=nil;',
  15201. '']);
  15202. ConvertProgram;
  15203. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15204. LinesToStr([ // statements
  15205. 'rtl.createClass(this, "TObject", null, function () {',
  15206. ' this.$init = function () {',
  15207. ' };',
  15208. ' this.$final = function () {',
  15209. ' };',
  15210. '});',
  15211. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15212. '});',
  15213. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15214. '});',
  15215. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15216. ' this.DoIt = function () {',
  15217. ' var List = null;',
  15218. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15219. ' List.GetAlphas(false).SetBetas(5, null);',
  15220. ' };',
  15221. '});',
  15222. 'this.List = null;',
  15223. '']),
  15224. LinesToStr([ // $mod.$main
  15225. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15226. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15227. '']));
  15228. end;
  15229. procedure TTestModule.TestClass_PropertyOverride;
  15230. begin
  15231. StartProgram(false);
  15232. Add('type');
  15233. Add(' integer = longint;');
  15234. Add(' TObject = class');
  15235. Add(' FItem: integer;');
  15236. Add(' function GetItem: integer; external name ''GetItem'';');
  15237. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15238. Add(' property Item: integer read getitem write setitem;');
  15239. Add(' end;');
  15240. Add(' TCar = class');
  15241. Add(' FBag: integer;');
  15242. Add(' function GetBag: integer; external name ''GetBag'';');
  15243. Add(' property Item read getbag;');
  15244. Add(' end;');
  15245. Add('var');
  15246. Add(' Obj: tobject;');
  15247. Add(' Car: tcar;');
  15248. Add('begin');
  15249. Add(' Obj.Item:=Obj.Item;');
  15250. Add(' Car.Item:=Car.Item;');
  15251. ConvertProgram;
  15252. CheckSource('TestClass_PropertyOverride',
  15253. LinesToStr([ // statements
  15254. 'rtl.createClass(this, "TObject", null, function () {',
  15255. ' this.$init = function () {',
  15256. ' this.FItem = 0;',
  15257. ' };',
  15258. ' this.$final = function () {',
  15259. ' };',
  15260. '});',
  15261. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15262. ' this.$init = function () {',
  15263. ' $mod.TObject.$init.call(this);',
  15264. ' this.FBag = 0;',
  15265. ' };',
  15266. '});',
  15267. 'this.Obj = null;',
  15268. 'this.Car = null;',
  15269. '']),
  15270. LinesToStr([ // $mod.$main
  15271. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15272. '$mod.Car.SetItem($mod.Car.GetBag());',
  15273. '']));
  15274. end;
  15275. procedure TTestModule.TestClass_PropertyIncVisibility;
  15276. begin
  15277. AddModuleWithIntfImplSrc('unit1.pp',
  15278. LinesToStr([
  15279. 'type',
  15280. ' TNumber = longint;',
  15281. ' TInteger = longint;',
  15282. ' TObject = class',
  15283. ' private',
  15284. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15285. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15286. ' protected',
  15287. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15288. ' end;']),
  15289. LinesToStr([
  15290. '']));
  15291. StartProgram(true);
  15292. Add([
  15293. 'uses unit1;',
  15294. 'type',
  15295. ' TBird = class',
  15296. ' public',
  15297. ' property Items;',
  15298. ' end;',
  15299. 'procedure DoIt(i: TInteger);',
  15300. 'begin',
  15301. 'end;',
  15302. 'var b: TBird;',
  15303. 'begin',
  15304. ' b.Items[1]:=2;',
  15305. ' b.Items[3]:=b.Items[4];',
  15306. ' DoIt(b.Items[5]);',
  15307. '']);
  15308. ConvertProgram;
  15309. CheckSource('TestClass_PropertyIncVisibility',
  15310. LinesToStr([ // statements
  15311. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15312. '});',
  15313. 'this.DoIt = function (i) {',
  15314. '};',
  15315. 'this.b = null;'
  15316. ]),
  15317. LinesToStr([ // $mod.$main
  15318. '$mod.b.SetItems(1, 2);',
  15319. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15320. '$mod.DoIt($mod.b.GetItems(5));'
  15321. ]));
  15322. end;
  15323. procedure TTestModule.TestClass_Assigned;
  15324. begin
  15325. StartProgram(false);
  15326. Add('type');
  15327. Add(' TObject = class');
  15328. Add(' end;');
  15329. Add('var');
  15330. Add(' Obj: tobject;');
  15331. Add(' b: boolean;');
  15332. Add('begin');
  15333. Add(' if Assigned(obj) then ;');
  15334. Add(' b:=Assigned(obj) or false;');
  15335. ConvertProgram;
  15336. CheckSource('TestClass_Assigned',
  15337. LinesToStr([ // statements
  15338. 'rtl.createClass(this, "TObject", null, function () {',
  15339. ' this.$init = function () {',
  15340. ' };',
  15341. ' this.$final = function () {',
  15342. ' };',
  15343. '});',
  15344. 'this.Obj = null;',
  15345. 'this.b = false;'
  15346. ]),
  15347. LinesToStr([ // $mod.$main
  15348. 'if ($mod.Obj != null);',
  15349. '$mod.b = ($mod.Obj != null) || false;'
  15350. ]));
  15351. end;
  15352. procedure TTestModule.TestClass_WithClassDoCreate;
  15353. begin
  15354. StartProgram(false);
  15355. Add('type');
  15356. Add(' TObject = class');
  15357. Add(' aBool: boolean;');
  15358. Add(' Arr: array of boolean;');
  15359. Add(' constructor Create;');
  15360. Add(' end;');
  15361. Add('constructor TObject.Create; begin end;');
  15362. Add('var');
  15363. Add(' Obj: tobject;');
  15364. Add(' b: boolean;');
  15365. Add('begin');
  15366. Add(' with tobject.create do begin');
  15367. Add(' b:=abool;');
  15368. Add(' abool:=b;');
  15369. Add(' b:=arr[1];');
  15370. Add(' arr[2]:=b;');
  15371. Add(' end;');
  15372. Add(' with tobject do');
  15373. Add(' obj:=create;');
  15374. Add(' with obj do begin');
  15375. Add(' create;');
  15376. Add(' b:=abool;');
  15377. Add(' abool:=b;');
  15378. Add(' b:=arr[3];');
  15379. Add(' arr[4]:=b;');
  15380. Add(' end;');
  15381. ConvertProgram;
  15382. CheckSource('TestClass_WithClassDoCreate',
  15383. LinesToStr([ // statements
  15384. 'rtl.createClass(this, "TObject", null, function () {',
  15385. ' this.$init = function () {',
  15386. ' this.aBool = false;',
  15387. ' this.Arr = [];',
  15388. ' };',
  15389. ' this.$final = function () {',
  15390. ' this.Arr = undefined;',
  15391. ' };',
  15392. ' this.Create = function () {',
  15393. ' return this;',
  15394. ' };',
  15395. '});',
  15396. 'this.Obj = null;',
  15397. 'this.b = false;'
  15398. ]),
  15399. LinesToStr([ // $mod.$main
  15400. 'var $with = $mod.TObject.$create("Create");',
  15401. '$mod.b = $with.aBool;',
  15402. '$with.aBool = $mod.b;',
  15403. '$mod.b = $with.Arr[1];',
  15404. '$with.Arr[2] = $mod.b;',
  15405. 'var $with1 = $mod.TObject;',
  15406. '$mod.Obj = $with1.$create("Create");',
  15407. 'var $with2 = $mod.Obj;',
  15408. '$with2.Create();',
  15409. '$mod.b = $with2.aBool;',
  15410. '$with2.aBool = $mod.b;',
  15411. '$mod.b = $with2.Arr[3];',
  15412. '$with2.Arr[4] = $mod.b;',
  15413. '']));
  15414. end;
  15415. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15416. begin
  15417. StartProgram(false);
  15418. Add('type');
  15419. Add(' TObject = class');
  15420. Add(' FInt: longint;');
  15421. Add(' constructor Create;');
  15422. Add(' function GetSize: longint;');
  15423. Add(' procedure SetSize(Value: longint);');
  15424. Add(' property Int: longint read FInt write FInt;');
  15425. Add(' property Size: longint read GetSize write SetSize;');
  15426. Add(' end;');
  15427. Add('constructor TObject.Create; begin end;');
  15428. Add('function TObject.GetSize: longint; begin; end;');
  15429. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15430. Add('var');
  15431. Add(' Obj: tobject;');
  15432. Add(' i: longint;');
  15433. Add('begin');
  15434. Add(' with TObject.Create do begin');
  15435. Add(' i:=int;');
  15436. Add(' int:=i;');
  15437. Add(' i:=size;');
  15438. Add(' size:=i;');
  15439. Add(' end;');
  15440. Add(' with obj do begin');
  15441. Add(' i:=int;');
  15442. Add(' int:=i;');
  15443. Add(' i:=size;');
  15444. Add(' size:=i;');
  15445. Add(' end;');
  15446. ConvertProgram;
  15447. CheckSource('TestClass_WithClassInstDoProperty',
  15448. LinesToStr([ // statements
  15449. 'rtl.createClass(this, "TObject", null, function () {',
  15450. ' this.$init = function () {',
  15451. ' this.FInt = 0;',
  15452. ' };',
  15453. ' this.$final = function () {',
  15454. ' };',
  15455. ' this.Create = function () {',
  15456. ' return this;',
  15457. ' };',
  15458. ' this.GetSize = function () {',
  15459. ' var Result = 0;',
  15460. ' return Result;',
  15461. ' };',
  15462. ' this.SetSize = function (Value) {',
  15463. ' };',
  15464. '});',
  15465. 'this.Obj = null;',
  15466. 'this.i = 0;'
  15467. ]),
  15468. LinesToStr([ // $mod.$main
  15469. 'var $with = $mod.TObject.$create("Create");',
  15470. '$mod.i = $with.FInt;',
  15471. '$with.FInt = $mod.i;',
  15472. '$mod.i = $with.GetSize();',
  15473. '$with.SetSize($mod.i);',
  15474. 'var $with1 = $mod.Obj;',
  15475. '$mod.i = $with1.FInt;',
  15476. '$with1.FInt = $mod.i;',
  15477. '$mod.i = $with1.GetSize();',
  15478. '$with1.SetSize($mod.i);',
  15479. '']));
  15480. end;
  15481. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15482. begin
  15483. StartProgram(false);
  15484. Add('type');
  15485. Add(' TObject = class');
  15486. Add(' constructor Create;');
  15487. Add(' function GetItems(Index: longint): longint;');
  15488. Add(' procedure SetItems(Index, Value: longint);');
  15489. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15490. Add(' end;');
  15491. Add('constructor TObject.Create; begin end;');
  15492. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15493. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15494. Add('var');
  15495. Add(' Obj: tobject;');
  15496. Add(' i: longint;');
  15497. Add('begin');
  15498. Add(' with TObject.Create do begin');
  15499. Add(' i:=Items[1];');
  15500. Add(' Items[2]:=i;');
  15501. Add(' end;');
  15502. Add(' with obj do begin');
  15503. Add(' i:=Items[3];');
  15504. Add(' Items[4]:=i;');
  15505. Add(' end;');
  15506. ConvertProgram;
  15507. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15508. LinesToStr([ // statements
  15509. 'rtl.createClass(this, "TObject", null, function () {',
  15510. ' this.$init = function () {',
  15511. ' };',
  15512. ' this.$final = function () {',
  15513. ' };',
  15514. ' this.Create = function () {',
  15515. ' return this;',
  15516. ' };',
  15517. ' this.GetItems = function (Index) {',
  15518. ' var Result = 0;',
  15519. ' return Result;',
  15520. ' };',
  15521. ' this.SetItems = function (Index, Value) {',
  15522. ' };',
  15523. '});',
  15524. 'this.Obj = null;',
  15525. 'this.i = 0;'
  15526. ]),
  15527. LinesToStr([ // $mod.$main
  15528. 'var $with = $mod.TObject.$create("Create");',
  15529. '$mod.i = $with.GetItems(1);',
  15530. '$with.SetItems(2, $mod.i);',
  15531. 'var $with1 = $mod.Obj;',
  15532. '$mod.i = $with1.GetItems(3);',
  15533. '$with1.SetItems(4, $mod.i);',
  15534. '']));
  15535. end;
  15536. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15537. begin
  15538. StartProgram(false);
  15539. Add('type');
  15540. Add(' TObject = class');
  15541. Add(' constructor Create;');
  15542. Add(' function GetSize: longint;');
  15543. Add(' procedure SetSize(Value: longint);');
  15544. Add(' end;');
  15545. Add('constructor TObject.Create; begin end;');
  15546. Add('function TObject.GetSize: longint; begin; end;');
  15547. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15548. Add('var');
  15549. Add(' Obj: tobject;');
  15550. Add(' i: longint;');
  15551. Add('begin');
  15552. Add(' with TObject.Create do begin');
  15553. Add(' i:=GetSize;');
  15554. Add(' i:=GetSize();');
  15555. Add(' SetSize(i);');
  15556. Add(' end;');
  15557. Add(' with obj do begin');
  15558. Add(' i:=GetSize;');
  15559. Add(' i:=GetSize();');
  15560. Add(' SetSize(i);');
  15561. Add(' end;');
  15562. ConvertProgram;
  15563. CheckSource('TestClass_WithClassInstDoFunc',
  15564. LinesToStr([ // statements
  15565. 'rtl.createClass(this, "TObject", null, function () {',
  15566. ' this.$init = function () {',
  15567. ' };',
  15568. ' this.$final = function () {',
  15569. ' };',
  15570. ' this.Create = function () {',
  15571. ' return this;',
  15572. ' };',
  15573. ' this.GetSize = function () {',
  15574. ' var Result = 0;',
  15575. ' return Result;',
  15576. ' };',
  15577. ' this.SetSize = function (Value) {',
  15578. ' };',
  15579. '});',
  15580. 'this.Obj = null;',
  15581. 'this.i = 0;'
  15582. ]),
  15583. LinesToStr([ // $mod.$main
  15584. 'var $with = $mod.TObject.$create("Create");',
  15585. '$mod.i = $with.GetSize();',
  15586. '$mod.i = $with.GetSize();',
  15587. '$with.SetSize($mod.i);',
  15588. 'var $with1 = $mod.Obj;',
  15589. '$mod.i = $with1.GetSize();',
  15590. '$mod.i = $with1.GetSize();',
  15591. '$with1.SetSize($mod.i);',
  15592. '']));
  15593. end;
  15594. procedure TTestModule.TestClass_TypeCast;
  15595. begin
  15596. StartProgram(false);
  15597. Add('type');
  15598. Add(' TObject = class');
  15599. Add(' Next: TObject;');
  15600. Add(' constructor Create;');
  15601. Add(' end;');
  15602. Add(' TControl = class(TObject)');
  15603. Add(' Arr: array of TObject;');
  15604. Add(' function GetIt(vI: longint = 0): TObject;');
  15605. Add(' end;');
  15606. Add('constructor tobject.create; begin end;');
  15607. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15608. Add('var');
  15609. Add(' Obj: tobject;');
  15610. Add('begin');
  15611. Add(' obj:=tcontrol(obj).next;');
  15612. Add(' tcontrol(obj):=nil;');
  15613. Add(' obj:=tcontrol(obj);');
  15614. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15615. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15616. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15617. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15618. Add(' obj:=tcontrol(nil);');
  15619. ConvertProgram;
  15620. CheckSource('TestClass_TypeCast',
  15621. LinesToStr([ // statements
  15622. 'rtl.createClass(this, "TObject", null, function () {',
  15623. ' this.$init = function () {',
  15624. ' this.Next = null;',
  15625. ' };',
  15626. ' this.$final = function () {',
  15627. ' this.Next = undefined;',
  15628. ' };',
  15629. ' this.Create = function () {',
  15630. ' return this;',
  15631. ' };',
  15632. '});',
  15633. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15634. ' this.$init = function () {',
  15635. ' $mod.TObject.$init.call(this);',
  15636. ' this.Arr = [];',
  15637. ' };',
  15638. ' this.$final = function () {',
  15639. ' this.Arr = undefined;',
  15640. ' $mod.TObject.$final.call(this);',
  15641. ' };',
  15642. ' this.GetIt = function (vI) {',
  15643. ' var Result = null;',
  15644. ' return Result;',
  15645. ' };',
  15646. '});',
  15647. 'this.Obj = null;'
  15648. ]),
  15649. LinesToStr([ // $mod.$main
  15650. '$mod.Obj = $mod.Obj.Next;',
  15651. '$mod.Obj = null;',
  15652. '$mod.Obj = $mod.Obj;',
  15653. '$mod.Obj = $mod.Obj.GetIt(0);',
  15654. '$mod.Obj = $mod.Obj.GetIt(0);',
  15655. '$mod.Obj = $mod.Obj.GetIt(1);',
  15656. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15657. '$mod.Obj = null;',
  15658. '']));
  15659. end;
  15660. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15661. begin
  15662. StartProgram(false);
  15663. Add('type');
  15664. Add(' TObject = class end;');
  15665. Add('procedure ProcA(var A);');
  15666. Add('begin');
  15667. Add(' TObject(A):=nil;');
  15668. Add(' TObject(A):=TObject(A);');
  15669. Add(' if TObject(A)=nil then ;');
  15670. Add(' if nil=TObject(A) then ;');
  15671. Add('end;');
  15672. Add('procedure ProcB(out A);');
  15673. Add('begin');
  15674. Add(' TObject(A):=nil;');
  15675. Add(' TObject(A):=TObject(A);');
  15676. Add(' if TObject(A)=nil then ;');
  15677. Add(' if nil=TObject(A) then ;');
  15678. Add('end;');
  15679. Add('procedure ProcC(const A);');
  15680. Add('begin');
  15681. Add(' if TObject(A)=nil then ;');
  15682. Add(' if nil=TObject(A) then ;');
  15683. Add('end;');
  15684. Add('var o: TObject;');
  15685. Add('begin');
  15686. Add(' ProcA(o);');
  15687. Add(' ProcB(o);');
  15688. Add(' ProcC(o);');
  15689. ConvertProgram;
  15690. CheckSource('TestClass_TypeCastUntypedParam',
  15691. LinesToStr([ // statements
  15692. 'rtl.createClass(this, "TObject", null, function () {',
  15693. ' this.$init = function () {',
  15694. ' };',
  15695. ' this.$final = function () {',
  15696. ' };',
  15697. '});',
  15698. 'this.ProcA = function (A) {',
  15699. ' A.set(null);',
  15700. ' A.set(A.get());',
  15701. ' if (A.get() === null);',
  15702. ' if (null === A.get());',
  15703. '};',
  15704. 'this.ProcB = function (A) {',
  15705. ' A.set(null);',
  15706. ' A.set(A.get());',
  15707. ' if (A.get() === null);',
  15708. ' if (null === A.get());',
  15709. '};',
  15710. 'this.ProcC = function (A) {',
  15711. ' if (A === null);',
  15712. ' if (null === A);',
  15713. '};',
  15714. 'this.o = null;',
  15715. '']),
  15716. LinesToStr([ // $mod.$main
  15717. '$mod.ProcA({',
  15718. ' p: $mod,',
  15719. ' get: function () {',
  15720. ' return this.p.o;',
  15721. ' },',
  15722. ' set: function (v) {',
  15723. ' this.p.o = v;',
  15724. ' }',
  15725. '});',
  15726. '$mod.ProcB({',
  15727. ' p: $mod,',
  15728. ' get: function () {',
  15729. ' return this.p.o;',
  15730. ' },',
  15731. ' set: function (v) {',
  15732. ' this.p.o = v;',
  15733. ' }',
  15734. '});',
  15735. '$mod.ProcC($mod.o);',
  15736. '']));
  15737. end;
  15738. procedure TTestModule.TestClass_Overloads;
  15739. begin
  15740. StartProgram(false);
  15741. Add('type');
  15742. Add(' TObject = class');
  15743. Add(' procedure DoIt;');
  15744. Add(' procedure DoIt(vI: longint);');
  15745. Add(' end;');
  15746. Add('procedure TObject.DoIt;');
  15747. Add('begin');
  15748. Add(' DoIt;');
  15749. Add(' DoIt(1);');
  15750. Add('end;');
  15751. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15752. Add('begin');
  15753. ConvertProgram;
  15754. CheckSource('TestClass_Overloads',
  15755. LinesToStr([ // statements
  15756. 'rtl.createClass(this, "TObject", null, function () {',
  15757. ' this.$init = function () {',
  15758. ' };',
  15759. ' this.$final = function () {',
  15760. ' };',
  15761. ' this.DoIt = function () {',
  15762. ' this.DoIt();',
  15763. ' this.DoIt$1(1);',
  15764. ' };',
  15765. ' this.DoIt$1 = function (vI) {',
  15766. ' };',
  15767. '});',
  15768. '']),
  15769. LinesToStr([ // $mod.$main
  15770. '']));
  15771. end;
  15772. procedure TTestModule.TestClass_OverloadsAncestor;
  15773. begin
  15774. StartProgram(false);
  15775. Add('type');
  15776. Add(' TObject = class;');
  15777. Add(' TObject = class');
  15778. Add(' procedure DoIt(vA: longint);');
  15779. Add(' procedure DoIt(vA, vB: longint);');
  15780. Add(' end;');
  15781. Add(' TCar = class;');
  15782. Add(' TCar = class');
  15783. Add(' procedure DoIt(vA: longint);');
  15784. Add(' procedure DoIt(vA, vB: longint);');
  15785. Add(' end;');
  15786. Add('procedure tobject.doit(va: longint);');
  15787. Add('begin');
  15788. Add(' doit(1);');
  15789. Add(' doit(1,2);');
  15790. Add('end;');
  15791. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15792. Add('procedure tcar.doit(va: longint);');
  15793. Add('begin');
  15794. Add(' doit(1);');
  15795. Add(' doit(1,2);');
  15796. Add(' inherited doit(1);');
  15797. Add(' inherited doit(1,2);');
  15798. Add('end;');
  15799. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15800. Add('begin');
  15801. ConvertProgram;
  15802. CheckSource('TestClass_OverloadsAncestor',
  15803. LinesToStr([ // statements
  15804. 'rtl.createClass(this, "TObject", null, function () {',
  15805. ' this.$init = function () {',
  15806. ' };',
  15807. ' this.$final = function () {',
  15808. ' };',
  15809. ' this.DoIt = function (vA) {',
  15810. ' this.DoIt(1);',
  15811. ' this.DoIt$1(1,2);',
  15812. ' };',
  15813. ' this.DoIt$1 = function (vA, vB) {',
  15814. ' };',
  15815. '});',
  15816. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15817. ' this.DoIt$2 = function (vA) {',
  15818. ' this.DoIt$2(1);',
  15819. ' this.DoIt$3(1, 2);',
  15820. ' $mod.TObject.DoIt.call(this, 1);',
  15821. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15822. ' };',
  15823. ' this.DoIt$3 = function (vA, vB) {',
  15824. ' };',
  15825. '});',
  15826. '']),
  15827. LinesToStr([ // $mod.$main
  15828. '']));
  15829. end;
  15830. procedure TTestModule.TestClass_OverloadConstructor;
  15831. begin
  15832. StartProgram(false);
  15833. Add('type');
  15834. Add(' TObject = class');
  15835. Add(' constructor Create(vA: longint);');
  15836. Add(' constructor Create(vA, vB: longint);');
  15837. Add(' end;');
  15838. Add(' TCar = class');
  15839. Add(' constructor Create(vA: longint);');
  15840. Add(' constructor Create(vA, vB: longint);');
  15841. Add(' end;');
  15842. Add('constructor tobject.create(va: longint);');
  15843. Add('begin');
  15844. Add(' create(1);');
  15845. Add(' create(1,2);');
  15846. Add('end;');
  15847. Add('constructor tobject.create(va, vb: longint); begin end;');
  15848. Add('constructor tcar.create(va: longint);');
  15849. Add('begin');
  15850. Add(' create(1);');
  15851. Add(' create(1,2);');
  15852. Add(' inherited create(1);');
  15853. Add(' inherited create(1,2);');
  15854. Add('end;');
  15855. Add('constructor tcar.create(va, vb: longint); begin end;');
  15856. Add('begin');
  15857. Add(' tobject.create(1);');
  15858. Add(' tobject.create(1,2);');
  15859. Add(' tcar.create(1);');
  15860. Add(' tcar.create(1,2);');
  15861. ConvertProgram;
  15862. CheckSource('TestClass_OverloadConstructor',
  15863. LinesToStr([ // statements
  15864. 'rtl.createClass(this, "TObject", null, function () {',
  15865. ' this.$init = function () {',
  15866. ' };',
  15867. ' this.$final = function () {',
  15868. ' };',
  15869. ' this.Create = function (vA) {',
  15870. ' this.Create(1);',
  15871. ' this.Create$1(1,2);',
  15872. ' return this;',
  15873. ' };',
  15874. ' this.Create$1 = function (vA, vB) {',
  15875. ' return this;',
  15876. ' };',
  15877. '});',
  15878. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15879. ' this.Create$2 = function (vA) {',
  15880. ' this.Create$2(1);',
  15881. ' this.Create$3(1, 2);',
  15882. ' $mod.TObject.Create.call(this, 1);',
  15883. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15884. ' return this;',
  15885. ' };',
  15886. ' this.Create$3 = function (vA, vB) {',
  15887. ' return this;',
  15888. ' };',
  15889. '});',
  15890. '']),
  15891. LinesToStr([ // $mod.$main
  15892. '$mod.TObject.$create("Create", [1]);',
  15893. '$mod.TObject.$create("Create$1", [1, 2]);',
  15894. '$mod.TCar.$create("Create$2", [1]);',
  15895. '$mod.TCar.$create("Create$3", [1, 2]);',
  15896. '']));
  15897. end;
  15898. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15899. begin
  15900. StartProgram(false);
  15901. Add([
  15902. '{$mode delphi}',
  15903. 'type',
  15904. ' TObject = class end;',
  15905. ' TBird = class',
  15906. ' function {#a}GetValue: longint; overload; virtual;',
  15907. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15908. ' end;',
  15909. ' TEagle = class(TBird)',
  15910. ' function {#c}GetValue: longint; overload; override;',
  15911. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15912. ' end;',
  15913. 'function TBird.GetValue: longint;',
  15914. 'begin',
  15915. ' if 3={@a}GetValue then ;',
  15916. ' if 4={@b}GetValue(5) then ;',
  15917. 'end;',
  15918. 'function TBird.GetValue(AValue: longint): longint;',
  15919. 'begin',
  15920. 'end;',
  15921. 'function TEagle.GetValue: longint;',
  15922. 'begin',
  15923. ' if 13={@c}GetValue then ;',
  15924. ' if 14={@d}GetValue(15) then ;',
  15925. ' if 15=inherited {@a}GetValue then ;',
  15926. ' if 16=inherited {@b}GetValue(17) then ;',
  15927. 'end;',
  15928. 'function TEagle.GetValue(AValue: longint): longint;',
  15929. 'begin',
  15930. 'end;',
  15931. 'var',
  15932. ' e: TEagle;',
  15933. 'begin',
  15934. ' if 23=e.{@c}GetValue then ;',
  15935. ' if 24=e.{@d}GetValue(25) then ;']);
  15936. ConvertProgram;
  15937. CheckSource('TestClass_OverloadDelphiOverride',
  15938. LinesToStr([ // statements
  15939. 'rtl.createClass(this, "TObject", null, function () {',
  15940. ' this.$init = function () {',
  15941. ' };',
  15942. ' this.$final = function () {',
  15943. ' };',
  15944. '});',
  15945. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15946. ' this.GetValue = function () {',
  15947. ' var Result = 0;',
  15948. ' if (3 === this.GetValue()) ;',
  15949. ' if (4 === this.GetValue$1(5)) ;',
  15950. ' return Result;',
  15951. ' };',
  15952. ' this.GetValue$1 = function (AValue) {',
  15953. ' var Result = 0;',
  15954. ' return Result;',
  15955. ' };',
  15956. '});',
  15957. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15958. ' this.GetValue = function () {',
  15959. ' var Result = 0;',
  15960. ' if (13 === this.GetValue()) ;',
  15961. ' if (14 === this.GetValue$1(15)) ;',
  15962. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15963. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15964. ' return Result;',
  15965. ' };',
  15966. ' this.GetValue$1 = function (AValue) {',
  15967. ' var Result = 0;',
  15968. ' return Result;',
  15969. ' };',
  15970. '});',
  15971. 'this.e = null;',
  15972. '']),
  15973. LinesToStr([ // $mod.$main
  15974. 'if (23 === $mod.e.GetValue()) ;',
  15975. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15976. '']));
  15977. end;
  15978. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15979. begin
  15980. StartProgram(false);
  15981. Add([
  15982. '{$mode delphi}',
  15983. 'type',
  15984. ' TObject = class end;',
  15985. ' TAnimal = class',
  15986. ' public',
  15987. ' {#animal_a}A: longint;',
  15988. ' function {#animal_b}B: longint;',
  15989. ' end;',
  15990. ' TBird = class(TAnimal)',
  15991. ' public',
  15992. ' {#bird_a}A: double;',
  15993. ' {#bird_b}B: boolean;',
  15994. ' end;',
  15995. ' TEagle = class(TBird)',
  15996. ' public',
  15997. ' function {#eagle_a}A: boolean;',
  15998. ' {#eagle_b}B: double;',
  15999. ' end;',
  16000. 'function TAnimal.B: longint;',
  16001. 'begin',
  16002. 'end;',
  16003. 'function TEagle.A: boolean;',
  16004. 'begin',
  16005. ' {@eagle_b}B:=3.3;',
  16006. ' {@eagle_a}A();',
  16007. ' TBird(Self).{@bird_b}B:=true;',
  16008. ' TAnimal(Self).{@animal_a}A:=17;',
  16009. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16010. 'end;',
  16011. 'var',
  16012. ' e: TEagle;',
  16013. 'begin',
  16014. ' e.{@eagle_b}B:=5.3;',
  16015. ' if e.{@eagle_a}A then ;',
  16016. '']);
  16017. ConvertProgram;
  16018. CheckSource('TestClass_ReintroduceVarDelphi',
  16019. LinesToStr([ // statements
  16020. 'rtl.createClass(this, "TObject", null, function () {',
  16021. ' this.$init = function () {',
  16022. ' };',
  16023. ' this.$final = function () {',
  16024. ' };',
  16025. '});',
  16026. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16027. ' this.$init = function () {',
  16028. ' $mod.TObject.$init.call(this);',
  16029. ' this.A = 0;',
  16030. ' };',
  16031. ' this.B = function () {',
  16032. ' var Result = 0;',
  16033. ' return Result;',
  16034. ' };',
  16035. '});',
  16036. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16037. ' this.$init = function () {',
  16038. ' $mod.TAnimal.$init.call(this);',
  16039. ' this.A$1 = 0.0;',
  16040. ' this.B$1 = false;',
  16041. ' };',
  16042. '});',
  16043. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16044. ' this.$init = function () {',
  16045. ' $mod.TBird.$init.call(this);',
  16046. ' this.B$2 = 0.0;',
  16047. ' };',
  16048. ' this.A$2 = function () {',
  16049. ' var Result = false;',
  16050. ' this.B$2 = 3.3;',
  16051. ' this.A$2();',
  16052. ' this.B$1 = true;',
  16053. ' this.A = 17;',
  16054. ' this.B$1 = this.A$1 > 1;',
  16055. ' return Result;',
  16056. ' };',
  16057. '});',
  16058. 'this.e = null;',
  16059. '']),
  16060. LinesToStr([ // $mod.$main
  16061. '$mod.e.B$2 = 5.3;',
  16062. 'if ($mod.e.A$2()) ;',
  16063. '']));
  16064. end;
  16065. procedure TTestModule.TestClass_ReintroducedVar;
  16066. begin
  16067. StartProgram(false);
  16068. Add('type');
  16069. Add(' TObject = class');
  16070. Add(' strict private');
  16071. Add(' Some: longint;');
  16072. Add(' end;');
  16073. Add(' TMobile = class');
  16074. Add(' strict private');
  16075. Add(' Some: string;');
  16076. Add(' end;');
  16077. Add(' TCar = class(tmobile)');
  16078. Add(' procedure Some;');
  16079. Add(' procedure Some(vA: longint);');
  16080. Add(' end;');
  16081. Add('procedure tcar.some;');
  16082. Add('begin');
  16083. Add(' Some;');
  16084. Add(' Some(1);');
  16085. Add('end;');
  16086. Add('procedure tcar.some(va: longint); begin end;');
  16087. Add('begin');
  16088. ConvertProgram;
  16089. CheckSource('TestClass_ReintroducedVar',
  16090. LinesToStr([ // statements
  16091. 'rtl.createClass(this, "TObject", null, function () {',
  16092. ' this.$init = function () {',
  16093. ' this.Some = 0;',
  16094. ' };',
  16095. ' this.$final = function () {',
  16096. ' };',
  16097. '});',
  16098. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16099. ' this.$init = function () {',
  16100. ' $mod.TObject.$init.call(this);',
  16101. ' this.Some$1 = "";',
  16102. ' };',
  16103. '});',
  16104. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16105. ' this.Some$2 = function () {',
  16106. ' this.Some$2();',
  16107. ' this.Some$3(1);',
  16108. ' };',
  16109. ' this.Some$3 = function (vA) {',
  16110. ' };',
  16111. '});',
  16112. '']),
  16113. LinesToStr([ // $mod.$main
  16114. '']));
  16115. end;
  16116. procedure TTestModule.TestClass_RaiseDescendant;
  16117. begin
  16118. StartProgram(false);
  16119. Add([
  16120. 'type',
  16121. ' TObject = class',
  16122. ' constructor Create(Msg: string);',
  16123. ' end;',
  16124. ' Exception = class',
  16125. ' end;',
  16126. ' EConvertError = class(Exception)',
  16127. ' end;',
  16128. 'constructor TObject.Create(Msg: string); begin end;',
  16129. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16130. 'begin',
  16131. ' raise Exception.Create(''Bar1'');',
  16132. ' raise EConvertError.Create(''Bar2'');',
  16133. ' raise AssertConv(''Bar2'');',
  16134. ' raise AssertConv;',
  16135. '']);
  16136. ConvertProgram;
  16137. CheckSource('TestClass_RaiseDescendant',
  16138. LinesToStr([ // statements
  16139. 'rtl.createClass(this, "TObject", null, function () {',
  16140. ' this.$init = function () {',
  16141. ' };',
  16142. ' this.$final = function () {',
  16143. ' };',
  16144. ' this.Create = function (Msg) {',
  16145. ' return this;',
  16146. ' };',
  16147. '});',
  16148. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16149. '});',
  16150. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16151. '});',
  16152. 'this.AssertConv = function (Msg) {',
  16153. ' var Result = null;',
  16154. ' return Result;',
  16155. '};',
  16156. '']),
  16157. LinesToStr([ // $mod.$main
  16158. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16159. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16160. 'throw $mod.AssertConv("Bar2");',
  16161. 'throw $mod.AssertConv("def");',
  16162. '']));
  16163. end;
  16164. procedure TTestModule.TestClass_ExternalMethod;
  16165. begin
  16166. AddModuleWithIntfImplSrc('unit2.pas',
  16167. LinesToStr([
  16168. 'type',
  16169. ' TObject = class',
  16170. ' public',
  16171. ' procedure Intern; external name ''$DoIntern'';',
  16172. ' end;',
  16173. '']),
  16174. LinesToStr([
  16175. '']));
  16176. StartUnit(true);
  16177. Add('interface');
  16178. Add('uses unit2;');
  16179. Add('type');
  16180. Add(' TCar = class(TObject)');
  16181. Add(' public');
  16182. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16183. Add(' procedure DoIt;');
  16184. Add(' end;');
  16185. Add('implementation');
  16186. Add('procedure tcar.doit;');
  16187. Add('begin');
  16188. Add(' Intern;');
  16189. Add(' Intern();');
  16190. Add(' Intern2;');
  16191. Add(' Intern2();');
  16192. Add('end;');
  16193. Add('var Obj: TCar;');
  16194. Add('begin');
  16195. Add(' obj.intern;');
  16196. Add(' obj.intern();');
  16197. Add(' obj.intern2;');
  16198. Add(' obj.intern2();');
  16199. Add(' obj.doit;');
  16200. Add(' obj.doit();');
  16201. Add(' with obj do begin');
  16202. Add(' Intern;');
  16203. Add(' Intern();');
  16204. Add(' Intern2;');
  16205. Add(' Intern2();');
  16206. Add(' end;');
  16207. ConvertUnit;
  16208. CheckSource('TestClass_ExternalMethod',
  16209. LinesToStr([
  16210. 'var $impl = $mod.$impl;',
  16211. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16212. ' this.DoIt = function () {',
  16213. ' this.$DoIntern();',
  16214. ' this.$DoIntern();',
  16215. ' this.$DoIntern2();',
  16216. ' this.$DoIntern2();',
  16217. ' };',
  16218. ' });',
  16219. '']),
  16220. LinesToStr([ // this.$init
  16221. '$impl.Obj.$DoIntern();',
  16222. '$impl.Obj.$DoIntern();',
  16223. '$impl.Obj.$DoIntern2();',
  16224. '$impl.Obj.$DoIntern2();',
  16225. '$impl.Obj.DoIt();',
  16226. '$impl.Obj.DoIt();',
  16227. 'var $with = $impl.Obj;',
  16228. '$with.$DoIntern();',
  16229. '$with.$DoIntern();',
  16230. '$with.$DoIntern2();',
  16231. '$with.$DoIntern2();',
  16232. '']),
  16233. LinesToStr([ // implementation
  16234. '$impl.Obj = null;',
  16235. '']) );
  16236. end;
  16237. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16238. begin
  16239. StartProgram(false);
  16240. Add('type');
  16241. Add(' TObject = class');
  16242. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16243. Add(' end;');
  16244. Add('begin');
  16245. SetExpectedPasResolverError('Virtual method name must match external',
  16246. nVirtualMethodNameMustMatchExternal);
  16247. ConvertProgram;
  16248. end;
  16249. procedure TTestModule.TestClass_ExternalOverrideFail;
  16250. begin
  16251. StartProgram(false);
  16252. Add('type');
  16253. Add(' TObject = class');
  16254. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16255. Add(' end;');
  16256. Add(' TCar = class');
  16257. Add(' procedure DoIt; override; external name ''DoIt'';');
  16258. Add(' end;');
  16259. Add('begin');
  16260. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16261. nInvalidXModifierY);
  16262. ConvertProgram;
  16263. end;
  16264. procedure TTestModule.TestClass_ExternalVar;
  16265. begin
  16266. AddModuleWithIntfImplSrc('unit2.pas',
  16267. LinesToStr([
  16268. '{$modeswitch externalclass}',
  16269. 'type',
  16270. ' TObject = class',
  16271. ' public',
  16272. ' Intern: longint external name ''$Intern'';',
  16273. ' Bracket: longint external name ''["A B"]'';',
  16274. ' end;',
  16275. '']),
  16276. LinesToStr([
  16277. '']));
  16278. StartUnit(true);
  16279. Add([
  16280. 'interface',
  16281. 'uses unit2;',
  16282. '{$modeswitch externalclass}',
  16283. 'type',
  16284. ' TCar = class(tobject)',
  16285. ' public',
  16286. ' Intern2: longint external name ''$Intern2'';',
  16287. ' procedure DoIt;',
  16288. ' end;',
  16289. 'implementation',
  16290. 'procedure tcar.doit;',
  16291. 'begin',
  16292. ' Intern:=Intern+1;',
  16293. ' Intern2:=Intern2+2;',
  16294. ' Bracket:=Bracket+3;',
  16295. 'end;',
  16296. 'var Obj: TCar;',
  16297. 'begin',
  16298. ' obj.intern:=obj.intern+1;',
  16299. ' obj.intern2:=obj.intern2+2;',
  16300. ' obj.Bracket:=obj.Bracket+3;',
  16301. ' with obj do begin',
  16302. ' intern:=intern+1;',
  16303. ' intern2:=intern2+2;',
  16304. ' Bracket:=Bracket+3;',
  16305. ' end;']);
  16306. ConvertUnit;
  16307. CheckSource('TestClass_ExternalVar',
  16308. LinesToStr([
  16309. 'var $impl = $mod.$impl;',
  16310. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16311. ' this.DoIt = function () {',
  16312. ' this.$Intern = this.$Intern + 1;',
  16313. ' this.$Intern2 = this.$Intern2 + 2;',
  16314. ' this["A B"] = this["A B"] + 3;',
  16315. ' };',
  16316. ' });',
  16317. '']),
  16318. LinesToStr([
  16319. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16320. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16321. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16322. 'var $with = $impl.Obj;',
  16323. '$with.$Intern = $with.$Intern + 1;',
  16324. '$with.$Intern2 = $with.$Intern2 + 2;',
  16325. '$with["A B"] = $with["A B"] + 3;',
  16326. '']),
  16327. LinesToStr([ // implementation
  16328. '$impl.Obj = null;',
  16329. '']));
  16330. end;
  16331. procedure TTestModule.TestClass_Const;
  16332. begin
  16333. StartProgram(false);
  16334. Add([
  16335. 'type',
  16336. ' integer = longint;',
  16337. ' TClass = class of TObject;',
  16338. ' TObject = class',
  16339. ' public',
  16340. ' const cI: integer = 3;',
  16341. ' procedure DoIt;',
  16342. ' class procedure DoMore;',
  16343. ' end;',
  16344. 'procedure tobject.doit;',
  16345. 'begin',
  16346. ' if cI=4 then;',
  16347. ' if 5=cI then;',
  16348. ' if Self.cI=6 then;',
  16349. ' if 7=Self.cI then;',
  16350. ' with Self do begin',
  16351. ' if cI=11 then;',
  16352. ' if 12=cI then;',
  16353. ' end;',
  16354. 'end;',
  16355. 'class procedure tobject.domore;',
  16356. 'begin',
  16357. ' if cI=8 then;',
  16358. ' if Self.cI=9 then;',
  16359. ' if 10=cI then;',
  16360. ' if 11=Self.cI then;',
  16361. ' with Self do begin',
  16362. ' if cI=13 then;',
  16363. ' if 14=cI then;',
  16364. ' end;',
  16365. 'end;',
  16366. 'var',
  16367. ' Obj: TObject;',
  16368. ' Cla: TClass;',
  16369. 'begin',
  16370. ' if TObject.cI=21 then ;',
  16371. ' if Obj.cI=22 then ;',
  16372. ' if Cla.cI=23 then ;',
  16373. ' with obj do if ci=24 then;',
  16374. ' with TObject do if ci=25 then;',
  16375. ' with Cla do if ci=26 then;']);
  16376. ConvertProgram;
  16377. CheckSource('TestClass_Const',
  16378. LinesToStr([
  16379. 'rtl.createClass(this, "TObject", null, function () {',
  16380. ' this.cI = 3;',
  16381. ' this.$init = function () {',
  16382. ' };',
  16383. ' this.$final = function () {',
  16384. ' };',
  16385. ' this.DoIt = function () {',
  16386. ' if (this.cI === 4) ;',
  16387. ' if (5 === this.cI) ;',
  16388. ' if (this.cI === 6) ;',
  16389. ' if (7 === this.cI) ;',
  16390. ' if (this.cI === 11) ;',
  16391. ' if (12 === this.cI) ;',
  16392. ' };',
  16393. ' this.DoMore = function () {',
  16394. ' if (this.cI === 8) ;',
  16395. ' if (this.cI === 9) ;',
  16396. ' if (10 === this.cI) ;',
  16397. ' if (11 === this.cI) ;',
  16398. ' if (this.cI === 13) ;',
  16399. ' if (14 === this.cI) ;',
  16400. ' };',
  16401. '});',
  16402. 'this.Obj = null;',
  16403. 'this.Cla = null;',
  16404. '']),
  16405. LinesToStr([
  16406. 'if ($mod.TObject.cI === 21) ;',
  16407. 'if ($mod.Obj.cI === 22) ;',
  16408. 'if ($mod.Cla.cI === 23) ;',
  16409. 'var $with = $mod.Obj;',
  16410. 'if ($with.cI === 24) ;',
  16411. 'var $with1 = $mod.TObject;',
  16412. 'if ($with1.cI === 25) ;',
  16413. 'var $with2 = $mod.Cla;',
  16414. 'if ($with2.cI === 26) ;',
  16415. '']));
  16416. end;
  16417. procedure TTestModule.TestClass_ConstEnum;
  16418. begin
  16419. StartProgram(false);
  16420. Add([
  16421. 'type',
  16422. ' TEnum = (red,blue);',
  16423. ' TObject = class',
  16424. ' end;',
  16425. ' TAnimal = class',
  16426. ' public',
  16427. ' type TSubEnum = (light,dark);',
  16428. ' const a = high(TEnum);',
  16429. ' const b = high(TSubEnum);',
  16430. ' end;',
  16431. ' TBird = class(TAnimal)',
  16432. ' public',
  16433. ' const c = high(TEnum);',
  16434. ' const d = high(TSubEnum);',
  16435. ' end;',
  16436. ' TAnt = class',
  16437. ' public',
  16438. ' const e = high(TEnum);',
  16439. ' const f = high(TBird.TSubEnum);',
  16440. ' end;',
  16441. 'begin',
  16442. '']);
  16443. ConvertProgram;
  16444. CheckSource('TestClass_ConstEnum',
  16445. LinesToStr([
  16446. 'this.TEnum = {',
  16447. ' "0": "red",',
  16448. ' red: 0,',
  16449. ' "1": "blue",',
  16450. ' blue: 1',
  16451. '};',
  16452. 'rtl.createClass(this, "TObject", null, function () {',
  16453. ' this.$init = function () {',
  16454. ' };',
  16455. ' this.$final = function () {',
  16456. ' };',
  16457. '});',
  16458. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16459. ' this.TSubEnum = {',
  16460. ' "0": "light",',
  16461. ' light: 0,',
  16462. ' "1": "dark",',
  16463. ' dark: 1',
  16464. ' };',
  16465. ' this.a = $mod.TEnum.blue;',
  16466. ' this.b = this.TSubEnum.dark;',
  16467. '});',
  16468. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16469. ' this.c = $mod.TEnum.blue;',
  16470. ' this.d = this.TSubEnum.dark;',
  16471. '});',
  16472. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16473. ' this.e = $mod.TEnum.blue;',
  16474. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16475. '});',
  16476. '']),
  16477. LinesToStr([
  16478. '']));
  16479. end;
  16480. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16481. begin
  16482. StartProgram(false);
  16483. Add([
  16484. 'type',
  16485. ' TObject = class',
  16486. ' const cI: longint = 3;',
  16487. ' procedure Fly;',
  16488. ' procedure Run;',
  16489. ' end;',
  16490. ' TBird = class',
  16491. ' procedure Go;',
  16492. ' end;',
  16493. 'procedure tobject.fly;',
  16494. 'const cI: word = 4;',
  16495. 'begin',
  16496. ' if cI=Self.cI then ;',
  16497. 'end;',
  16498. 'procedure tobject.run;',
  16499. 'const cI: word = 5;',
  16500. 'begin',
  16501. ' if cI=Self.cI then ;',
  16502. 'end;',
  16503. 'procedure tbird.go;',
  16504. 'const cI: word = 6;',
  16505. 'begin',
  16506. ' if cI=Self.cI then ;',
  16507. 'end;',
  16508. 'begin',
  16509. '']);
  16510. ConvertProgram;
  16511. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16512. LinesToStr([
  16513. 'rtl.createClass(this, "TObject", null, function () {',
  16514. ' this.cI = 3;',
  16515. ' this.$init = function () {',
  16516. ' };',
  16517. ' this.$final = function () {',
  16518. ' };',
  16519. ' var cI$1 = 4;',
  16520. ' this.Fly = function () {',
  16521. ' if (cI$1 === this.cI) ;',
  16522. ' };',
  16523. ' var cI$2 = 5;',
  16524. ' this.Run = function () {',
  16525. ' if (cI$2 === this.cI) ;',
  16526. ' };',
  16527. '});',
  16528. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16529. ' var cI$3 = 6;',
  16530. ' this.Go = function () {',
  16531. ' if (cI$3 === this.cI) ;',
  16532. ' };',
  16533. '});',
  16534. '']),
  16535. LinesToStr([
  16536. '']));
  16537. end;
  16538. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16539. begin
  16540. StartUnit(false);
  16541. Add([
  16542. 'interface',
  16543. 'type',
  16544. ' TObject = class',
  16545. ' const cI: longint = 3;',
  16546. ' procedure Fly;',
  16547. ' procedure Run;',
  16548. ' end;',
  16549. ' TBird = class',
  16550. ' procedure Go;',
  16551. ' end;',
  16552. 'implementation',
  16553. 'procedure tobject.fly;',
  16554. 'const cI: word = 4;',
  16555. 'begin',
  16556. ' if cI=Self.cI then ;',
  16557. 'end;',
  16558. 'procedure tobject.run;',
  16559. 'const cI: word = 5;',
  16560. 'begin',
  16561. ' if cI=Self.cI then ;',
  16562. 'end;',
  16563. 'procedure tbird.go;',
  16564. 'const cI: word = 6;',
  16565. 'begin',
  16566. ' if cI=Self.cI then ;',
  16567. 'end;',
  16568. '']);
  16569. ConvertUnit;
  16570. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16571. LinesToStr([
  16572. 'rtl.createClass(this, "TObject", null, function () {',
  16573. ' this.cI = 3;',
  16574. ' this.$init = function () {',
  16575. ' };',
  16576. ' this.$final = function () {',
  16577. ' };',
  16578. ' var cI$1 = 4;',
  16579. ' this.Fly = function () {',
  16580. ' if (cI$1 === this.cI) ;',
  16581. ' };',
  16582. ' var cI$2 = 5;',
  16583. ' this.Run = function () {',
  16584. ' if (cI$2 === this.cI) ;',
  16585. ' };',
  16586. '});',
  16587. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16588. ' var cI$3 = 6;',
  16589. ' this.Go = function () {',
  16590. ' if (cI$3 === this.cI) ;',
  16591. ' };',
  16592. '});',
  16593. '']),
  16594. '',
  16595. '');
  16596. end;
  16597. procedure TTestModule.TestClass_LocalVarSelfFail;
  16598. begin
  16599. StartProgram(false);
  16600. Add([
  16601. 'type',
  16602. ' TObject = class',
  16603. ' constructor Create;',
  16604. ' end;',
  16605. 'constructor tobject.create;',
  16606. 'var self: longint;',
  16607. 'begin',
  16608. 'end',
  16609. 'begin',
  16610. '']);
  16611. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16612. ConvertProgram;
  16613. end;
  16614. procedure TTestModule.TestClass_ArgSelfFail;
  16615. begin
  16616. StartProgram(false);
  16617. Add([
  16618. 'type',
  16619. ' TObject = class',
  16620. ' procedure DoIt(Self: longint);',
  16621. ' end;',
  16622. 'procedure tobject.doit(self: longint);',
  16623. 'begin',
  16624. 'end',
  16625. 'begin',
  16626. '']);
  16627. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16628. ConvertProgram;
  16629. end;
  16630. procedure TTestModule.TestClass_NestedProcSelf;
  16631. begin
  16632. StartProgram(false);
  16633. Add([
  16634. 'type',
  16635. ' TObject = class',
  16636. ' Key: longint;',
  16637. ' class var State: longint;',
  16638. ' procedure DoIt;',
  16639. ' function GetSize: longint; virtual; abstract;',
  16640. ' procedure SetSize(Value: longint); virtual; abstract;',
  16641. ' property Size: longint read GetSize write SetSize;',
  16642. ' end;',
  16643. 'procedure tobject.doit;',
  16644. ' procedure Sub;',
  16645. ' begin',
  16646. ' key:=key+2;',
  16647. ' self.key:=self.key+3;',
  16648. ' state:=state+4;',
  16649. ' self.state:=self.state+5;',
  16650. ' tobject.state:=tobject.state+6;',
  16651. ' size:=size+7;',
  16652. ' self.size:=self.size+8;',
  16653. ' end;',
  16654. 'begin',
  16655. ' sub;',
  16656. ' key:=key+12;',
  16657. ' self.key:=self.key+13;',
  16658. ' state:=state+14;',
  16659. ' self.state:=self.state+15;',
  16660. ' tobject.state:=tobject.state+16;',
  16661. ' size:=size+17;',
  16662. ' self.size:=self.size+18;',
  16663. 'end;',
  16664. 'begin',
  16665. '']);
  16666. ConvertProgram;
  16667. CheckSource('TestClass_NestedProcSelf',
  16668. LinesToStr([ // statements
  16669. 'rtl.createClass(this, "TObject", null, function () {',
  16670. ' this.State = 0;',
  16671. ' this.$init = function () {',
  16672. ' this.Key = 0;',
  16673. ' };',
  16674. ' this.$final = function () {',
  16675. ' };',
  16676. ' this.DoIt = function () {',
  16677. ' var $Self = this;',
  16678. ' function Sub() {',
  16679. ' $Self.Key = $Self.Key + 2;',
  16680. ' $Self.Key = $Self.Key + 3;',
  16681. ' $mod.TObject.State = $Self.State + 4;',
  16682. ' $mod.TObject.State = $Self.State + 5;',
  16683. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16684. ' $Self.SetSize($Self.GetSize() + 7);',
  16685. ' $Self.SetSize($Self.GetSize() + 8);',
  16686. ' };',
  16687. ' Sub();',
  16688. ' this.Key = this.Key + 12;',
  16689. ' $Self.Key = $Self.Key + 13;',
  16690. ' $mod.TObject.State = this.State + 14;',
  16691. ' $mod.TObject.State = $Self.State + 15;',
  16692. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16693. ' this.SetSize(this.GetSize() + 17);',
  16694. ' $Self.SetSize($Self.GetSize() + 18);',
  16695. ' };',
  16696. '});',
  16697. '']),
  16698. LinesToStr([ // $mod.$main
  16699. '']));
  16700. end;
  16701. procedure TTestModule.TestClass_NestedProcSelf2;
  16702. begin
  16703. StartProgram(false);
  16704. Add([
  16705. 'type',
  16706. ' TObject = class',
  16707. ' Key: longint;',
  16708. ' class var State: longint;',
  16709. ' function GetSize: longint; virtual; abstract;',
  16710. ' procedure SetSize(Value: longint); virtual; abstract;',
  16711. ' property Size: longint read GetSize write SetSize;',
  16712. ' end;',
  16713. ' TBird = class',
  16714. ' procedure DoIt;',
  16715. ' end;',
  16716. 'procedure tbird.doit;',
  16717. ' procedure Sub;',
  16718. ' begin',
  16719. ' key:=key+2;',
  16720. ' self.key:=self.key+3;',
  16721. ' state:=state+4;',
  16722. ' self.state:=self.state+5;',
  16723. ' tobject.state:=tobject.state+6;',
  16724. ' size:=size+7;',
  16725. ' self.size:=self.size+8;',
  16726. ' end;',
  16727. 'begin',
  16728. ' sub;',
  16729. ' key:=key+12;',
  16730. ' self.key:=self.key+13;',
  16731. ' state:=state+14;',
  16732. ' self.state:=self.state+15;',
  16733. ' tobject.state:=tobject.state+16;',
  16734. ' size:=size+17;',
  16735. ' self.size:=self.size+18;',
  16736. 'end;',
  16737. 'begin',
  16738. '']);
  16739. ConvertProgram;
  16740. CheckSource('TestClass_NestedProcSelf2',
  16741. LinesToStr([ // statements
  16742. 'rtl.createClass(this, "TObject", null, function () {',
  16743. ' this.State = 0;',
  16744. ' this.$init = function () {',
  16745. ' this.Key = 0;',
  16746. ' };',
  16747. ' this.$final = function () {',
  16748. ' };',
  16749. '});',
  16750. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16751. ' this.DoIt = function () {',
  16752. ' var $Self = this;',
  16753. ' function Sub() {',
  16754. ' $Self.Key = $Self.Key + 2;',
  16755. ' $Self.Key = $Self.Key + 3;',
  16756. ' $mod.TObject.State = $Self.State + 4;',
  16757. ' $mod.TObject.State = $Self.State + 5;',
  16758. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16759. ' $Self.SetSize($Self.GetSize() + 7);',
  16760. ' $Self.SetSize($Self.GetSize() + 8);',
  16761. ' };',
  16762. ' Sub();',
  16763. ' this.Key = this.Key + 12;',
  16764. ' $Self.Key = $Self.Key + 13;',
  16765. ' $mod.TObject.State = this.State + 14;',
  16766. ' $mod.TObject.State = $Self.State + 15;',
  16767. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16768. ' this.SetSize(this.GetSize() + 17);',
  16769. ' $Self.SetSize($Self.GetSize() + 18);',
  16770. ' };',
  16771. '});',
  16772. '']),
  16773. LinesToStr([ // $mod.$main
  16774. '']));
  16775. end;
  16776. procedure TTestModule.TestClass_NestedProcClassSelf;
  16777. begin
  16778. StartProgram(false);
  16779. Add([
  16780. 'type',
  16781. ' TObject = class',
  16782. ' class var State: longint;',
  16783. ' class procedure DoIt;',
  16784. ' class function GetSize: longint; virtual; abstract;',
  16785. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16786. ' class property Size: longint read GetSize write SetSize;',
  16787. ' end;',
  16788. 'class procedure tobject.doit;',
  16789. ' procedure Sub;',
  16790. ' begin',
  16791. ' state:=state+2;',
  16792. ' self.state:=self.state+3;',
  16793. ' tobject.state:=tobject.state+4;',
  16794. ' size:=size+5;',
  16795. ' self.size:=self.size+6;',
  16796. ' tobject.size:=tobject.size+7;',
  16797. ' end;',
  16798. 'begin',
  16799. ' sub;',
  16800. ' state:=state+12;',
  16801. ' self.state:=self.state+13;',
  16802. ' tobject.state:=tobject.state+14;',
  16803. ' size:=size+15;',
  16804. ' self.size:=self.size+16;',
  16805. ' tobject.size:=tobject.size+17;',
  16806. 'end;',
  16807. 'begin',
  16808. '']);
  16809. ConvertProgram;
  16810. CheckSource('TestClass_NestedProcClassSelf',
  16811. LinesToStr([ // statements
  16812. 'rtl.createClass(this, "TObject", null, function () {',
  16813. ' this.State = 0;',
  16814. ' this.$init = function () {',
  16815. ' };',
  16816. ' this.$final = function () {',
  16817. ' };',
  16818. ' this.DoIt = function () {',
  16819. ' var $Self = this;',
  16820. ' function Sub() {',
  16821. ' $mod.TObject.State = $Self.State + 2;',
  16822. ' $mod.TObject.State = $Self.State + 3;',
  16823. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16824. ' $Self.SetSize($Self.GetSize() + 5);',
  16825. ' $Self.SetSize($Self.GetSize() + 6);',
  16826. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16827. ' };',
  16828. ' Sub();',
  16829. ' $mod.TObject.State = this.State + 12;',
  16830. ' $mod.TObject.State = $Self.State + 13;',
  16831. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16832. ' this.SetSize(this.GetSize() + 15);',
  16833. ' $Self.SetSize($Self.GetSize() + 16);',
  16834. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16835. ' };',
  16836. '});',
  16837. '']),
  16838. LinesToStr([ // $mod.$main
  16839. '']));
  16840. end;
  16841. procedure TTestModule.TestClass_NestedProcCallInherited;
  16842. begin
  16843. StartProgram(false);
  16844. Add([
  16845. 'type',
  16846. ' TObject = class',
  16847. ' function DoIt(k: boolean): longint; virtual;',
  16848. ' end;',
  16849. ' TBird = class',
  16850. ' function DoIt(k: boolean): longint; override;',
  16851. ' end;',
  16852. 'function tobject.doit(k: boolean): longint;',
  16853. 'begin',
  16854. 'end;',
  16855. 'function tbird.doit(k: boolean): longint;',
  16856. ' procedure Sub;',
  16857. ' begin',
  16858. ' inherited DoIt(true);',
  16859. //' if inherited DoIt(false)=4 then ;',
  16860. ' end;',
  16861. 'begin',
  16862. ' Sub;',
  16863. ' inherited;',
  16864. ' inherited DoIt(true);',
  16865. //' if inherited DoIt(false)=14 then ;',
  16866. 'end;',
  16867. 'begin',
  16868. '']);
  16869. ConvertProgram;
  16870. CheckSource('TestClass_NestedProcCallInherited',
  16871. LinesToStr([ // statements
  16872. 'rtl.createClass(this, "TObject", null, function () {',
  16873. ' this.$init = function () {',
  16874. ' };',
  16875. ' this.$final = function () {',
  16876. ' };',
  16877. ' this.DoIt = function (k) {',
  16878. ' var Result = 0;',
  16879. ' return Result;',
  16880. ' };',
  16881. '});',
  16882. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16883. ' this.DoIt = function (k) {',
  16884. ' var $Self = this;',
  16885. ' var Result = 0;',
  16886. ' function Sub() {',
  16887. ' $mod.TObject.DoIt.call($Self, true);',
  16888. ' };',
  16889. ' Sub();',
  16890. ' $mod.TObject.DoIt.apply(this, arguments);',
  16891. ' $mod.TObject.DoIt.call(this, true);',
  16892. ' return Result;',
  16893. ' };',
  16894. '});',
  16895. '']),
  16896. LinesToStr([ // $mod.$main
  16897. '']));
  16898. end;
  16899. procedure TTestModule.TestClass_TObjectFree;
  16900. begin
  16901. StartProgram(false);
  16902. Add([
  16903. 'type',
  16904. ' TObject = class',
  16905. ' Obj: tobject;',
  16906. ' procedure Free;',
  16907. ' procedure Release;',
  16908. ' end;',
  16909. 'procedure tobject.free;',
  16910. 'begin',
  16911. 'end;',
  16912. 'procedure tobject.release;',
  16913. 'begin',
  16914. ' free;',
  16915. ' if true then free;',
  16916. 'end;',
  16917. 'function DoIt(o: tobject): tobject;',
  16918. 'var l: tobject;',
  16919. 'begin',
  16920. ' o.free;',
  16921. ' o.free();',
  16922. ' l.free;',
  16923. ' l.free();',
  16924. ' o.obj.free;',
  16925. ' o.obj.free();',
  16926. ' with o do obj.free;',
  16927. ' with o do obj.free();',
  16928. ' result.Free;',
  16929. ' result.Free();',
  16930. 'end;',
  16931. 'var o: tobject;',
  16932. ' a: array of tobject;',
  16933. 'begin',
  16934. ' o.free;',
  16935. ' o.obj.free;',
  16936. ' a[1+2].free;',
  16937. '']);
  16938. ConvertProgram;
  16939. CheckSource('TestClass_TObjectFree',
  16940. LinesToStr([ // statements
  16941. 'rtl.createClass(this, "TObject", null, function () {',
  16942. ' this.$init = function () {',
  16943. ' this.Obj = null;',
  16944. ' };',
  16945. ' this.$final = function () {',
  16946. ' this.Obj = undefined;',
  16947. ' };',
  16948. ' this.Free = function () {',
  16949. ' };',
  16950. ' this.Release = function () {',
  16951. ' this.Free();',
  16952. ' if (true) this.Free();',
  16953. ' };',
  16954. '});',
  16955. 'this.DoIt = function (o) {',
  16956. ' var Result = null;',
  16957. ' var l = null;',
  16958. ' o = rtl.freeLoc(o);',
  16959. ' o = rtl.freeLoc(o);',
  16960. ' l = rtl.freeLoc(l);',
  16961. ' l = rtl.freeLoc(l);',
  16962. ' rtl.free(o, "Obj");',
  16963. ' rtl.free(o, "Obj");',
  16964. ' rtl.free(o, "Obj");',
  16965. ' rtl.free(o, "Obj");',
  16966. ' Result = rtl.freeLoc(Result);',
  16967. ' Result = rtl.freeLoc(Result);',
  16968. ' return Result;',
  16969. '};',
  16970. 'this.o = null;',
  16971. 'this.a = [];',
  16972. '']),
  16973. LinesToStr([ // $mod.$main
  16974. 'rtl.free($mod, "o");',
  16975. 'rtl.free($mod.o, "Obj");',
  16976. 'rtl.free($mod.a, 1 + 2);',
  16977. '']));
  16978. end;
  16979. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16980. begin
  16981. StartProgram(false);
  16982. Add([
  16983. 'type',
  16984. ' TObject = class',
  16985. ' Obj: tobject;',
  16986. ' procedure Free;',
  16987. ' end;',
  16988. 'procedure tobject.free;',
  16989. 'begin',
  16990. 'end;',
  16991. 'procedure DoIt(var o: tobject);',
  16992. 'begin',
  16993. ' o.free;',
  16994. ' o.free();',
  16995. 'end;',
  16996. 'begin',
  16997. '']);
  16998. ConvertProgram;
  16999. CheckSource('TestClass_TObjectFree_VarArg',
  17000. LinesToStr([ // statements
  17001. 'rtl.createClass(this, "TObject", null, function () {',
  17002. ' this.$init = function () {',
  17003. ' this.Obj = null;',
  17004. ' };',
  17005. ' this.$final = function () {',
  17006. ' this.Obj = undefined;',
  17007. ' };',
  17008. ' this.Free = function () {',
  17009. ' };',
  17010. '});',
  17011. 'this.DoIt = function (o) {',
  17012. ' o.set(rtl.freeLoc(o.get()));',
  17013. ' o.set(rtl.freeLoc(o.get()));',
  17014. '};',
  17015. '']),
  17016. LinesToStr([ // $mod.$main
  17017. '']));
  17018. end;
  17019. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17020. begin
  17021. StartProgram(false);
  17022. Add([
  17023. 'type',
  17024. ' TObject = class',
  17025. ' constructor Create;',
  17026. ' procedure Free;',
  17027. ' end;',
  17028. 'constructor TObject.Create; begin end;',
  17029. 'procedure tobject.free; begin end;',
  17030. 'begin',
  17031. ' with tobject.create do free;',
  17032. '']);
  17033. ConvertProgram;
  17034. CheckSource('TestClass_TObjectFreeNewInstance',
  17035. LinesToStr([ // statements
  17036. 'rtl.createClass(this, "TObject", null, function () {',
  17037. ' this.$init = function () {',
  17038. ' };',
  17039. ' this.$final = function () {',
  17040. ' };',
  17041. ' this.Create = function () {',
  17042. ' return this;',
  17043. ' };',
  17044. ' this.Free = function () {',
  17045. ' };',
  17046. '});',
  17047. '']),
  17048. LinesToStr([ // $mod.$main
  17049. 'var $with = $mod.TObject.$create("Create");',
  17050. '$with=rtl.freeLoc($with);',
  17051. '']));
  17052. end;
  17053. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17054. begin
  17055. StartProgram(false);
  17056. Add([
  17057. 'type',
  17058. ' TObject = class',
  17059. ' destructor Destroy;',
  17060. ' procedure Free;',
  17061. ' end;',
  17062. 'destructor TObject.Destroy; begin end;',
  17063. 'procedure tobject.free; begin end;',
  17064. 'var o: tobject;',
  17065. 'begin',
  17066. ' o.free;',
  17067. '']);
  17068. Converter.UseLowerCase:=true;
  17069. ConvertProgram;
  17070. CheckSource('TestClass_TObjectFreeLowerCase',
  17071. LinesToStr([ // statements
  17072. 'rtl.createClass(this, "tobject", null, function () {',
  17073. ' this.$init = function () {',
  17074. ' };',
  17075. ' this.$final = function () {',
  17076. ' };',
  17077. ' rtl.tObjectDestroy = "destroy";',
  17078. ' this.destroy = function () {',
  17079. ' };',
  17080. ' this.free = function () {',
  17081. ' };',
  17082. '});',
  17083. 'this.o = null;',
  17084. '']),
  17085. LinesToStr([ // $mod.$main
  17086. 'rtl.free($mod, "o");',
  17087. '']));
  17088. end;
  17089. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17090. begin
  17091. StartProgram(false);
  17092. Add([
  17093. 'type',
  17094. ' TObject = class',
  17095. ' procedure Free;',
  17096. ' function GetObj: tobject; virtual; abstract;',
  17097. ' end;',
  17098. 'procedure tobject.free;',
  17099. 'begin',
  17100. 'end;',
  17101. 'var o: tobject;',
  17102. 'begin',
  17103. ' o.getobj.free;',
  17104. '']);
  17105. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17106. ConvertProgram;
  17107. end;
  17108. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17109. begin
  17110. StartProgram(false);
  17111. Add([
  17112. 'type',
  17113. ' TObject = class',
  17114. ' procedure Free;',
  17115. ' FObj: TObject;',
  17116. ' property Obj: tobject read FObj write FObj;',
  17117. ' end;',
  17118. 'procedure tobject.free;',
  17119. 'begin',
  17120. 'end;',
  17121. 'var o: tobject;',
  17122. 'begin',
  17123. ' o.obj.free;',
  17124. '']);
  17125. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17126. ConvertProgram;
  17127. end;
  17128. procedure TTestModule.TestClass_ForIn;
  17129. begin
  17130. StartProgram(false);
  17131. Add([
  17132. 'type',
  17133. ' TObject = class end;',
  17134. ' TItem = TObject;',
  17135. ' TEnumerator = class',
  17136. ' FCurrent: TItem;',
  17137. ' property Current: TItem read FCurrent;',
  17138. ' function MoveNext: boolean;',
  17139. ' end;',
  17140. ' TBird = class',
  17141. ' function GetEnumerator: TEnumerator;',
  17142. ' end;',
  17143. 'function TEnumerator.MoveNext: boolean;',
  17144. 'begin',
  17145. 'end;',
  17146. 'function TBird.GetEnumerator: TEnumerator;',
  17147. 'begin',
  17148. 'end;',
  17149. 'var',
  17150. ' b: TBird;',
  17151. ' i, i2: TItem;',
  17152. 'begin',
  17153. ' for i in b do i2:=i;']);
  17154. ConvertProgram;
  17155. CheckSource('TestClass_ForIn',
  17156. LinesToStr([ // statements
  17157. 'rtl.createClass(this, "TObject", null, function () {',
  17158. ' this.$init = function () {',
  17159. ' };',
  17160. ' this.$final = function () {',
  17161. ' };',
  17162. '});',
  17163. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17164. ' this.$init = function () {',
  17165. ' $mod.TObject.$init.call(this);',
  17166. ' this.FCurrent = null;',
  17167. ' };',
  17168. ' this.$final = function () {',
  17169. ' this.FCurrent = undefined;',
  17170. ' $mod.TObject.$final.call(this);',
  17171. ' };',
  17172. ' this.MoveNext = function () {',
  17173. ' var Result = false;',
  17174. ' return Result;',
  17175. ' };',
  17176. '});',
  17177. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17178. ' this.GetEnumerator = function () {',
  17179. ' var Result = null;',
  17180. ' return Result;',
  17181. ' };',
  17182. '});',
  17183. 'this.b = null;',
  17184. 'this.i = null;',
  17185. 'this.i2 = null;'
  17186. ]),
  17187. LinesToStr([ // $mod.$main
  17188. 'var $in = $mod.b.GetEnumerator();',
  17189. 'try {',
  17190. ' while ($in.MoveNext()){',
  17191. ' $mod.i = $in.FCurrent;',
  17192. ' $mod.i2 = $mod.i;',
  17193. ' }',
  17194. '} finally {',
  17195. ' $in = rtl.freeLoc($in)',
  17196. '};',
  17197. '']));
  17198. end;
  17199. procedure TTestModule.TestClass_DispatchMessage;
  17200. begin
  17201. StartProgram(false);
  17202. Add([
  17203. 'type',
  17204. ' {$DispatchField DispInt}',
  17205. ' {$DispatchStrField DispStr}',
  17206. ' TObject = class',
  17207. ' procedure Dispatch(var Msg); virtual; abstract;',
  17208. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17209. ' end;',
  17210. ' THopMsg = record',
  17211. ' DispInt: longint;',
  17212. ' end;',
  17213. ' TPutMsg = record',
  17214. ' DispStr: string;',
  17215. ' end;',
  17216. ' TBird = class',
  17217. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17218. ' procedure Run; overload; virtual; abstract;',
  17219. ' procedure Run(var Msg); overload; message ''Fast'';',
  17220. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17221. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17222. ' end;',
  17223. 'procedure TBird.Run(var Msg);',
  17224. 'begin',
  17225. 'end;',
  17226. 'begin',
  17227. '']);
  17228. ConvertProgram;
  17229. CheckResolverUnexpectedHints(true);
  17230. CheckSource('TestClass_Message',
  17231. LinesToStr([ // statements
  17232. 'rtl.createClass(this, "TObject", null, function () {',
  17233. ' this.$init = function () {',
  17234. ' };',
  17235. ' this.$final = function () {',
  17236. ' };',
  17237. '});',
  17238. 'rtl.recNewT(this, "THopMsg", function () {',
  17239. ' this.DispInt = 0;',
  17240. ' this.$eq = function (b) {',
  17241. ' return this.DispInt === b.DispInt;',
  17242. ' };',
  17243. ' this.$assign = function (s) {',
  17244. ' this.DispInt = s.DispInt;',
  17245. ' return this;',
  17246. ' };',
  17247. '});',
  17248. 'rtl.recNewT(this, "TPutMsg", function () {',
  17249. ' this.DispStr = "";',
  17250. ' this.$eq = function (b) {',
  17251. ' return this.DispStr === b.DispStr;',
  17252. ' };',
  17253. ' this.$assign = function (s) {',
  17254. ' this.DispStr = s.DispStr;',
  17255. ' return this;',
  17256. ' };',
  17257. '});',
  17258. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17259. ' this.Run$1 = function (Msg) {',
  17260. ' };',
  17261. ' this.$msgint = {',
  17262. ' "2": "Fly",',
  17263. ' "3": "Hop"',
  17264. ' };',
  17265. ' this.$msgstr = {',
  17266. ' Fast: "Run$1",',
  17267. ' foo: "Put"',
  17268. ' };',
  17269. '});',
  17270. '']),
  17271. LinesToStr([ // $mod.$main
  17272. '']));
  17273. end;
  17274. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17275. begin
  17276. StartProgram(false);
  17277. Add([
  17278. 'type',
  17279. ' TObject = class',
  17280. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17281. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17282. ' end;',
  17283. 'begin',
  17284. '']);
  17285. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17286. ConvertProgram;
  17287. end;
  17288. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17289. begin
  17290. StartProgram(false);
  17291. Add([
  17292. 'type',
  17293. ' TObject = class',
  17294. ' procedure Dispatch(var Msg); virtual; abstract;',
  17295. ' end;',
  17296. ' TFlyMsg = record',
  17297. ' FlyId: longint;',
  17298. ' end;',
  17299. ' TBird = class',
  17300. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17301. ' end;',
  17302. 'begin',
  17303. '']);
  17304. ConvertProgram;
  17305. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17306. end;
  17307. procedure TTestModule.TestClassOf_Create;
  17308. begin
  17309. StartProgram(false);
  17310. Add('type');
  17311. Add(' TObject = class');
  17312. Add(' constructor Create;');
  17313. Add(' end;');
  17314. Add(' TClass = class of TObject;');
  17315. Add('constructor tobject.create; begin end;');
  17316. Add('var');
  17317. Add(' Obj: tobject;');
  17318. Add(' C: tclass;');
  17319. Add('begin');
  17320. Add(' obj:=C.create;');
  17321. Add(' with c do obj:=create;');
  17322. ConvertProgram;
  17323. CheckSource('TestClassOf_Create',
  17324. LinesToStr([ // statements
  17325. 'rtl.createClass(this, "TObject", null, function () {',
  17326. ' this.$init = function () {',
  17327. ' };',
  17328. ' this.$final = function () {',
  17329. ' };',
  17330. ' this.Create = function () {',
  17331. ' return this;',
  17332. ' };',
  17333. '});',
  17334. 'this.Obj = null;',
  17335. 'this.C = null;'
  17336. ]),
  17337. LinesToStr([ // $mod.$main
  17338. '$mod.Obj = $mod.C.$create("Create");',
  17339. 'var $with = $mod.C;',
  17340. '$mod.Obj = $with.$create("Create");',
  17341. '']));
  17342. end;
  17343. procedure TTestModule.TestClassOf_Call;
  17344. begin
  17345. StartProgram(false);
  17346. Add('type');
  17347. Add(' TObject = class');
  17348. Add(' class procedure DoIt;');
  17349. Add(' end;');
  17350. Add(' TClass = class of TObject;');
  17351. Add('class procedure tobject.doit; begin end;');
  17352. Add('var');
  17353. Add(' C: tclass;');
  17354. Add('begin');
  17355. Add(' c.doit;');
  17356. Add(' with c do doit;');
  17357. ConvertProgram;
  17358. CheckSource('TestClassOf_Call',
  17359. LinesToStr([ // statements
  17360. 'rtl.createClass(this, "TObject", null, function () {',
  17361. ' this.$init = function () {',
  17362. ' };',
  17363. ' this.$final = function () {',
  17364. ' };',
  17365. ' this.DoIt = function () {',
  17366. ' };',
  17367. '});',
  17368. 'this.C = null;'
  17369. ]),
  17370. LinesToStr([ // $mod.$main
  17371. '$mod.C.DoIt();',
  17372. 'var $with = $mod.C;',
  17373. '$with.DoIt();',
  17374. '']));
  17375. end;
  17376. procedure TTestModule.TestClassOf_Assign;
  17377. begin
  17378. StartProgram(false);
  17379. Add('type');
  17380. Add(' TClass = class of TObject;');
  17381. Add(' TObject = class');
  17382. Add(' ClassType: TClass; ');
  17383. Add(' end;');
  17384. Add('var');
  17385. Add(' Obj: tobject;');
  17386. Add(' C: tclass;');
  17387. Add('begin');
  17388. Add(' c:=nil;');
  17389. Add(' c:=obj.classtype;');
  17390. ConvertProgram;
  17391. CheckSource('TestClassOf_Assign',
  17392. LinesToStr([ // statements
  17393. 'rtl.createClass(this, "TObject", null, function () {',
  17394. ' this.$init = function () {',
  17395. ' this.ClassType = null;',
  17396. ' };',
  17397. ' this.$final = function () {',
  17398. ' this.ClassType = undefined;',
  17399. ' };',
  17400. '});',
  17401. 'this.Obj = null;',
  17402. 'this.C = null;'
  17403. ]),
  17404. LinesToStr([ // $mod.$main
  17405. '$mod.C = null;',
  17406. '$mod.C = $mod.Obj.ClassType;',
  17407. '']));
  17408. end;
  17409. procedure TTestModule.TestClassOf_Is;
  17410. begin
  17411. StartProgram(false);
  17412. Add('type');
  17413. Add(' TClass = class of TObject;');
  17414. Add(' TObject = class');
  17415. Add(' end;');
  17416. Add(' TCar = class');
  17417. Add(' end;');
  17418. Add(' TCars = class of TCar;');
  17419. Add('var');
  17420. Add(' Obj: tobject;');
  17421. Add(' C: tclass;');
  17422. Add(' Cars: tcars;');
  17423. Add('begin');
  17424. Add(' if c is tcar then ;');
  17425. Add(' if c is tcars then ;');
  17426. ConvertProgram;
  17427. CheckSource('TestClassOf_Is',
  17428. LinesToStr([ // statements
  17429. 'rtl.createClass(this, "TObject", null, function () {',
  17430. ' this.$init = function () {',
  17431. ' };',
  17432. ' this.$final = function () {',
  17433. ' };',
  17434. '});',
  17435. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17436. '});',
  17437. 'this.Obj = null;',
  17438. 'this.C = null;',
  17439. 'this.Cars = null;'
  17440. ]),
  17441. LinesToStr([ // $mod.$main
  17442. 'if(rtl.is($mod.C,$mod.TCar));',
  17443. 'if(rtl.is($mod.C,$mod.TCar));',
  17444. '']));
  17445. end;
  17446. procedure TTestModule.TestClassOf_Compare;
  17447. begin
  17448. StartProgram(false);
  17449. Add('type');
  17450. Add(' TClass = class of TObject;');
  17451. Add(' TObject = class');
  17452. Add(' ClassType: TClass; ');
  17453. Add(' end;');
  17454. Add('var');
  17455. Add(' b: boolean;');
  17456. Add(' Obj: tobject;');
  17457. Add(' C: tclass;');
  17458. Add('begin');
  17459. Add(' b:=c=nil;');
  17460. Add(' b:=nil=c;');
  17461. Add(' b:=c=obj.classtype;');
  17462. Add(' b:=obj.classtype=c;');
  17463. Add(' b:=c=TObject;');
  17464. Add(' b:=TObject=c;');
  17465. Add(' b:=c<>nil;');
  17466. Add(' b:=nil<>c;');
  17467. Add(' b:=c<>obj.classtype;');
  17468. Add(' b:=obj.classtype<>c;');
  17469. Add(' b:=c<>TObject;');
  17470. Add(' b:=TObject<>c;');
  17471. ConvertProgram;
  17472. CheckSource('TestClassOf_Compare',
  17473. LinesToStr([ // statements
  17474. 'rtl.createClass(this, "TObject", null, function () {',
  17475. ' this.$init = function () {',
  17476. ' this.ClassType = null;',
  17477. ' };',
  17478. ' this.$final = function () {',
  17479. ' this.ClassType = undefined;',
  17480. ' };',
  17481. '});',
  17482. 'this.b = false;',
  17483. 'this.Obj = null;',
  17484. 'this.C = null;'
  17485. ]),
  17486. LinesToStr([ // $mod.$main
  17487. '$mod.b = $mod.C === null;',
  17488. '$mod.b = null === $mod.C;',
  17489. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17490. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17491. '$mod.b = $mod.C === $mod.TObject;',
  17492. '$mod.b = $mod.TObject === $mod.C;',
  17493. '$mod.b = $mod.C !== null;',
  17494. '$mod.b = null !== $mod.C;',
  17495. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17496. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17497. '$mod.b = $mod.C !== $mod.TObject;',
  17498. '$mod.b = $mod.TObject !== $mod.C;',
  17499. '']));
  17500. end;
  17501. procedure TTestModule.TestClassOf_ClassVar;
  17502. begin
  17503. StartProgram(false);
  17504. Add('type');
  17505. Add(' TObject = class');
  17506. Add(' class var id: longint;');
  17507. Add(' end;');
  17508. Add(' TClass = class of TObject;');
  17509. Add('var');
  17510. Add(' C: tclass;');
  17511. Add('begin');
  17512. Add(' C.id:=C.id;');
  17513. ConvertProgram;
  17514. CheckSource('TestClassOf_ClassVar',
  17515. LinesToStr([ // statements
  17516. 'rtl.createClass(this, "TObject", null, function () {',
  17517. ' this.id = 0;',
  17518. ' this.$init = function () {',
  17519. ' };',
  17520. ' this.$final = function () {',
  17521. ' };',
  17522. '});',
  17523. 'this.C = null;'
  17524. ]),
  17525. LinesToStr([ // $mod.$main
  17526. '$mod.TObject.id = $mod.C.id;',
  17527. '']));
  17528. end;
  17529. procedure TTestModule.TestClassOf_ClassMethod;
  17530. begin
  17531. StartProgram(false);
  17532. Add('type');
  17533. Add(' TObject = class');
  17534. Add(' class function DoIt(i: longint = 0): longint;');
  17535. Add(' end;');
  17536. Add(' TClass = class of TObject;');
  17537. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17538. Add('var');
  17539. Add(' i: longint;');
  17540. Add(' C: tclass;');
  17541. Add('begin');
  17542. Add(' C.DoIt;');
  17543. Add(' C.DoIt();');
  17544. Add(' i:=C.DoIt;');
  17545. Add(' i:=C.DoIt();');
  17546. ConvertProgram;
  17547. CheckSource('TestClassOf_ClassMethod',
  17548. LinesToStr([ // statements
  17549. 'rtl.createClass(this, "TObject", null, function () {',
  17550. ' this.$init = function () {',
  17551. ' };',
  17552. ' this.$final = function () {',
  17553. ' };',
  17554. ' this.DoIt = function (i) {',
  17555. ' var Result = 0;',
  17556. ' return Result;',
  17557. ' };',
  17558. '});',
  17559. 'this.i = 0;',
  17560. 'this.C = null;'
  17561. ]),
  17562. LinesToStr([ // $mod.$main
  17563. '$mod.C.DoIt(0);',
  17564. '$mod.C.DoIt(0);',
  17565. '$mod.i = $mod.C.DoIt(0);',
  17566. '$mod.i = $mod.C.DoIt(0);',
  17567. '']));
  17568. end;
  17569. procedure TTestModule.TestClassOf_ClassProperty;
  17570. begin
  17571. StartProgram(false);
  17572. Add([
  17573. 'type',
  17574. ' TObject = class',
  17575. ' class var FA: longint;',
  17576. ' class function GetA: longint;',
  17577. ' class procedure SetA(Value: longint);',
  17578. ' class property pA: longint read fa write fa;',
  17579. ' class property pB: longint read geta write seta;',
  17580. ' end;',
  17581. ' TObjectClass = class of tobject;',
  17582. 'class function tobject.geta: longint; begin end;',
  17583. 'class procedure tobject.seta(value: longint); begin end;',
  17584. 'var',
  17585. ' b: boolean;',
  17586. ' Obj: tobject;',
  17587. ' Cla: tobjectclass;',
  17588. 'begin',
  17589. ' obj.pa:=obj.pa;',
  17590. ' obj.pb:=obj.pb;',
  17591. ' b:=obj.pa=4;',
  17592. ' b:=obj.pb=obj.pb;',
  17593. ' b:=5=obj.pa;',
  17594. ' cla.pa:=6;',
  17595. ' cla.pa:=cla.pa;',
  17596. ' cla.pb:=cla.pb;',
  17597. ' b:=cla.pa=7;',
  17598. ' b:=cla.pb=cla.pb;',
  17599. ' b:=8=cla.pa;',
  17600. ' tobject.pa:=9;',
  17601. ' tobject.pb:=tobject.pb;',
  17602. ' b:=tobject.pa=10;',
  17603. ' b:=11=tobject.pa;',
  17604. '']);
  17605. ConvertProgram;
  17606. CheckSource('TestClassOf_ClassProperty',
  17607. LinesToStr([ // statements
  17608. 'rtl.createClass(this, "TObject", null, function () {',
  17609. ' this.FA = 0;',
  17610. ' this.$init = function () {',
  17611. ' };',
  17612. ' this.$final = function () {',
  17613. ' };',
  17614. ' this.GetA = function () {',
  17615. ' var Result = 0;',
  17616. ' return Result;',
  17617. ' };',
  17618. ' this.SetA = function (Value) {',
  17619. ' };',
  17620. '});',
  17621. 'this.b = false;',
  17622. 'this.Obj = null;',
  17623. 'this.Cla = null;'
  17624. ]),
  17625. LinesToStr([ // $mod.$main
  17626. '$mod.TObject.FA = $mod.Obj.FA;',
  17627. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17628. '$mod.b = $mod.Obj.FA === 4;',
  17629. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17630. '$mod.b = 5 === $mod.Obj.FA;',
  17631. '$mod.TObject.FA = 6;',
  17632. '$mod.TObject.FA = $mod.Cla.FA;',
  17633. '$mod.Cla.SetA($mod.Cla.GetA());',
  17634. '$mod.b = $mod.Cla.FA === 7;',
  17635. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17636. '$mod.b = 8 === $mod.Cla.FA;',
  17637. '$mod.TObject.FA = 9;',
  17638. '$mod.TObject.SetA($mod.TObject.GetA());',
  17639. '$mod.b = $mod.TObject.FA === 10;',
  17640. '$mod.b = 11 === $mod.TObject.FA;',
  17641. '']));
  17642. end;
  17643. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17644. begin
  17645. StartProgram(false);
  17646. Add('type');
  17647. Add(' TObject = class');
  17648. Add(' class var GlobalId: longint;');
  17649. Add(' class procedure ProcA;');
  17650. Add(' end;');
  17651. Add('class procedure tobject.proca;');
  17652. Add('var b: boolean;');
  17653. Add('begin');
  17654. Add(' b:=self=nil;');
  17655. Add(' b:=self.globalid=3;');
  17656. Add(' b:=4=self.globalid;');
  17657. Add(' self.globalid:=5;');
  17658. Add(' self.proca;');
  17659. Add('end;');
  17660. Add('begin');
  17661. ConvertProgram;
  17662. CheckSource('TestClassOf_ClassMethodSelf',
  17663. LinesToStr([ // statements
  17664. 'rtl.createClass(this, "TObject", null, function () {',
  17665. ' this.GlobalId = 0;',
  17666. ' this.$init = function () {',
  17667. ' };',
  17668. ' this.$final = function () {',
  17669. ' };',
  17670. ' this.ProcA = function () {',
  17671. ' var b = false;',
  17672. ' b = this === null;',
  17673. ' b = this.GlobalId === 3;',
  17674. ' b = 4 === this.GlobalId;',
  17675. ' $mod.TObject.GlobalId = 5;',
  17676. ' this.ProcA();',
  17677. ' };',
  17678. '});'
  17679. ]),
  17680. LinesToStr([ // $mod.$main
  17681. '']));
  17682. end;
  17683. procedure TTestModule.TestClassOf_TypeCast;
  17684. begin
  17685. StartProgram(false);
  17686. Add('type');
  17687. Add(' TObject = class');
  17688. Add(' class procedure {#TObject_DoIt}DoIt;');
  17689. Add(' end;');
  17690. Add(' TClass = class of TObject;');
  17691. Add(' TMobile = class');
  17692. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17693. Add(' end;');
  17694. Add(' TMobileClass = class of TMobile;');
  17695. Add(' TCar = class(TMobile)');
  17696. Add(' class procedure {#TCar_DoIt}DoIt;');
  17697. Add(' end;');
  17698. Add(' TCarClass = class of TCar;');
  17699. Add('class procedure TObject.DoIt;');
  17700. Add('begin');
  17701. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17702. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17703. Add('end;');
  17704. Add('class procedure TMobile.DoIt;');
  17705. Add('begin');
  17706. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17707. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17708. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17709. Add('end;');
  17710. Add('class procedure TCar.DoIt; begin end;');
  17711. Add('var');
  17712. Add(' ObjC: TClass;');
  17713. Add(' MobileC: TMobileClass;');
  17714. Add(' CarC: TCarClass;');
  17715. Add('begin');
  17716. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17717. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17718. Add(' CarC.{@TCar_DoIt}DoIt;');
  17719. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17720. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17721. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17722. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17723. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17724. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17725. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17726. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17727. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17728. ConvertProgram;
  17729. CheckSource('TestClassOf_TypeCast',
  17730. LinesToStr([ // statements
  17731. 'rtl.createClass(this, "TObject", null, function () {',
  17732. ' this.$init = function () {',
  17733. ' };',
  17734. ' this.$final = function () {',
  17735. ' };',
  17736. ' this.DoIt = function () {',
  17737. ' this.DoIt();',
  17738. ' this.DoIt$1();',
  17739. ' };',
  17740. '});',
  17741. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17742. ' this.DoIt$1 = function () {',
  17743. ' this.DoIt();',
  17744. ' this.DoIt$1();',
  17745. ' this.DoIt$2();',
  17746. ' };',
  17747. '});',
  17748. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17749. ' this.DoIt$2 = function () {',
  17750. ' };',
  17751. '});',
  17752. 'this.ObjC = null;',
  17753. 'this.MobileC = null;',
  17754. 'this.CarC = null;',
  17755. '']),
  17756. LinesToStr([ // $mod.$main
  17757. '$mod.ObjC.DoIt();',
  17758. '$mod.MobileC.DoIt$1();',
  17759. '$mod.CarC.DoIt$2();',
  17760. '$mod.ObjC.DoIt();',
  17761. '$mod.ObjC.DoIt$1();',
  17762. '$mod.ObjC.DoIt$2();',
  17763. '$mod.MobileC.DoIt();',
  17764. '$mod.MobileC.DoIt$1();',
  17765. '$mod.MobileC.DoIt$2();',
  17766. '$mod.CarC.DoIt();',
  17767. '$mod.CarC.DoIt$1();',
  17768. '$mod.CarC.DoIt$2();',
  17769. '']));
  17770. end;
  17771. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17772. begin
  17773. StartProgram(false);
  17774. Add('type');
  17775. Add(' TObject = class');
  17776. Add(' function CurNow: longint; ');
  17777. Add(' class function Now: longint; ');
  17778. Add(' end;');
  17779. Add('function TObject.CurNow: longint; begin end;');
  17780. Add('class function TObject.Now: longint; begin end;');
  17781. Add('var');
  17782. Add(' Obj: tobject;');
  17783. Add(' vI: longint;');
  17784. Add('begin');
  17785. Add(' obj.curnow;');
  17786. Add(' vi:=obj.curnow;');
  17787. Add(' tobject.now;');
  17788. Add(' vi:=tobject.now;');
  17789. ConvertProgram;
  17790. CheckSource('TestClassOf_ImplicitFunctionCall',
  17791. LinesToStr([ // statements
  17792. 'rtl.createClass(this, "TObject", null, function () {',
  17793. ' this.$init = function () {',
  17794. ' };',
  17795. ' this.$final = function () {',
  17796. ' };',
  17797. ' this.CurNow = function () {',
  17798. ' var Result = 0;',
  17799. ' return Result;',
  17800. ' };',
  17801. ' this.Now = function () {',
  17802. ' var Result = 0;',
  17803. ' return Result;',
  17804. ' };',
  17805. '});',
  17806. 'this.Obj = null;',
  17807. 'this.vI = 0;',
  17808. '']),
  17809. LinesToStr([ // $mod.$main
  17810. '$mod.Obj.CurNow();',
  17811. '$mod.vI = $mod.Obj.CurNow();',
  17812. '$mod.TObject.Now();',
  17813. '$mod.vI = $mod.TObject.Now();',
  17814. '']));
  17815. end;
  17816. procedure TTestModule.TestClassOf_Const;
  17817. begin
  17818. StartProgram(false);
  17819. Add([
  17820. 'type',
  17821. ' TObject = class',
  17822. ' end;',
  17823. ' TBird = TObject;',
  17824. ' TBirds = class of TBird;',
  17825. ' TEagles = TBirds;',
  17826. ' THawk = class(TBird);',
  17827. 'const',
  17828. ' Hawk: TEagles = THawk;',
  17829. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17830. ' TBird,',
  17831. ' THawk',
  17832. ' );',
  17833. 'begin']);
  17834. ConvertProgram;
  17835. CheckSource('TestClassOf_Const',
  17836. LinesToStr([ // statements
  17837. 'rtl.createClass(this, "TObject", null, function () {',
  17838. ' this.$init = function () {',
  17839. ' };',
  17840. ' this.$final = function () {',
  17841. ' };',
  17842. '});',
  17843. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17844. '});',
  17845. 'this.Hawk = this.THawk;',
  17846. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17847. '']),
  17848. LinesToStr([ // $mod.$main
  17849. '']));
  17850. end;
  17851. procedure TTestModule.TestNestedClass_Alias;
  17852. begin
  17853. WithTypeInfo:=true;
  17854. StartProgram(false);
  17855. Add([
  17856. 'type',
  17857. ' TObject = class',
  17858. ' type TNested = type longint;',
  17859. ' end;',
  17860. 'type TAlias = type tobject.tnested;',
  17861. 'var i: tobject.tnested = 3;',
  17862. 'var j: TAlias = 4;',
  17863. 'begin',
  17864. ' if typeinfo(TAlias)=nil then ;',
  17865. ' if typeinfo(tobject.tnested)=nil then ;',
  17866. '']);
  17867. ConvertProgram;
  17868. CheckSource('TestNestedClass_Alias',
  17869. LinesToStr([ // statements
  17870. 'rtl.createClass(this, "TObject", null, function () {',
  17871. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17872. ' this.$init = function () {',
  17873. ' };',
  17874. ' this.$final = function () {',
  17875. ' };',
  17876. '});',
  17877. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17878. 'this.i = 3;',
  17879. 'this.j = 4;',
  17880. '']),
  17881. LinesToStr([ // $mod.$main
  17882. 'if ($mod.$rtti["TAlias"] === null) ;',
  17883. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17884. '']));
  17885. end;
  17886. procedure TTestModule.TestNestedClass_Record;
  17887. begin
  17888. WithTypeInfo:=true;
  17889. StartProgram(false);
  17890. Add([
  17891. 'type',
  17892. ' TObject = class',
  17893. ' type TPoint = record',
  17894. ' x,y: byte;',
  17895. ' end;',
  17896. ' procedure DoIt(t: TPoint);',
  17897. ' end;',
  17898. 'procedure tobject.DoIt(t: TPoint);',
  17899. 'var p: TPoint;',
  17900. 'begin',
  17901. ' t.x:=t.y;',
  17902. ' p:=t;',
  17903. 'end;',
  17904. 'var',
  17905. ' p: tobject.tpoint = (x:2; y:4);',
  17906. ' o: TObject;',
  17907. 'begin',
  17908. ' p:=p;',
  17909. ' o.doit(p);',
  17910. '']);
  17911. ConvertProgram;
  17912. CheckSource('TestNestedClass_Record',
  17913. LinesToStr([ // statements
  17914. 'rtl.createClass(this, "TObject", null, function () {',
  17915. ' rtl.recNewT(this, "TPoint", function () {',
  17916. ' this.x = 0;',
  17917. ' this.y = 0;',
  17918. ' this.$eq = function (b) {',
  17919. ' return (this.x === b.x) && (this.y === b.y);',
  17920. ' };',
  17921. ' this.$assign = function (s) {',
  17922. ' this.x = s.x;',
  17923. ' this.y = s.y;',
  17924. ' return this;',
  17925. ' };',
  17926. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17927. ' $r.addField("x", rtl.byte);',
  17928. ' $r.addField("y", rtl.byte);',
  17929. ' });',
  17930. ' this.$init = function () {',
  17931. ' };',
  17932. ' this.$final = function () {',
  17933. ' };',
  17934. ' this.DoIt = function (t) {',
  17935. ' var p = this.TPoint.$new();',
  17936. ' t.x = t.y;',
  17937. ' p.$assign(t);',
  17938. ' };',
  17939. '});',
  17940. 'this.p = this.TObject.TPoint.$clone({',
  17941. ' x: 2,',
  17942. ' y: 4',
  17943. '});',
  17944. 'this.o = null;',
  17945. '']),
  17946. LinesToStr([ // $mod.$main
  17947. '$mod.p.$assign($mod.p);',
  17948. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17949. '']));
  17950. end;
  17951. procedure TTestModule.TestNestedClass_Class;
  17952. begin
  17953. StartProgram(false);
  17954. Add([
  17955. 'type',
  17956. ' TObject = class end;',
  17957. ' TBird = class',
  17958. ' type TLeg = class',
  17959. ' FId: longint;',
  17960. ' constructor Create;',
  17961. ' function Create(i: longint): TLeg;',
  17962. ' end;',
  17963. ' function DoIt(b: TBird): Tleg;',
  17964. ' end;',
  17965. 'constructor tbird.tleg.create;',
  17966. 'begin',
  17967. ' FId:=3;',
  17968. 'end;',
  17969. 'function tbird.tleg.Create(i: longint): TLeg;',
  17970. 'begin',
  17971. ' Create;',
  17972. ' Result:=TLeg.Create;',
  17973. ' Result:=TBird.TLeg.Create;',
  17974. ' Result:=Create(3);',
  17975. ' FId:=i;',
  17976. 'end;',
  17977. 'function tbird.DoIt(b: tbird): tleg;',
  17978. 'begin',
  17979. ' Result.Create;',
  17980. ' Result:=TLeg.Create;',
  17981. ' Result:=TBird.TLeg.Create;',
  17982. ' Result:=Result.Create(3);',
  17983. 'end;',
  17984. 'var',
  17985. ' b: Tbird.tleg;',
  17986. 'begin',
  17987. ' b.Create;',
  17988. ' b:=TBird.TLeg.Create;',
  17989. ' b:=b.Create(3);',
  17990. '']);
  17991. ConvertProgram;
  17992. CheckSource('TestNestedClass_Class',
  17993. LinesToStr([ // statements
  17994. 'rtl.createClass(this, "TObject", null, function () {',
  17995. ' this.$init = function () {',
  17996. ' };',
  17997. ' this.$final = function () {',
  17998. ' };',
  17999. '});',
  18000. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18001. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18002. ' this.$init = function () {',
  18003. ' $mod.TObject.$init.call(this);',
  18004. ' this.FId = 0;',
  18005. ' };',
  18006. ' this.Create = function () {',
  18007. ' this.FId = 3;',
  18008. ' return this;',
  18009. ' };',
  18010. ' this.Create$1 = function (i) {',
  18011. ' var Result = null;',
  18012. ' this.Create();',
  18013. ' Result = $mod.TBird.TLeg.$create("Create");',
  18014. ' Result = $mod.TBird.TLeg.$create("Create");',
  18015. ' Result = this.Create$1(3);',
  18016. ' this.FId = i;',
  18017. ' return Result;',
  18018. ' };',
  18019. ' }, "TBird.TLeg");',
  18020. ' this.DoIt = function (b) {',
  18021. ' var Result = null;',
  18022. ' Result.Create();',
  18023. ' Result = this.TLeg.$create("Create");',
  18024. ' Result = $mod.TBird.TLeg.$create("Create");',
  18025. ' Result = Result.Create$1(3);',
  18026. ' return Result;',
  18027. ' };',
  18028. '});',
  18029. 'this.b = null;',
  18030. '']),
  18031. LinesToStr([ // $mod.$main
  18032. '$mod.b.Create();',
  18033. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18034. '$mod.b = $mod.b.Create$1(3);',
  18035. '']));
  18036. end;
  18037. procedure TTestModule.TestNestedClass_CallInherited;
  18038. begin
  18039. StartProgram(false);
  18040. Add([
  18041. 'type',
  18042. ' TObject = class end;',
  18043. ' TBird = class',
  18044. ' type',
  18045. ' TWing = class',
  18046. ' function Fly(w: word = 17): word; virtual;',
  18047. ' end;',
  18048. ' end;',
  18049. ' TEagle = class(TBird)',
  18050. ' type',
  18051. ' TEagleWing = class(TWing)',
  18052. ' function Fly(w: word): word; override;',
  18053. ' end;',
  18054. ' end;',
  18055. 'function TBird.TWing.Fly(w: word): word;',
  18056. 'begin',
  18057. 'end;',
  18058. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18059. 'begin',
  18060. ' inherited;',
  18061. ' inherited Fly;',
  18062. ' inherited Fly(3);',
  18063. ' Result:=inherited Fly;',
  18064. ' Result:=inherited Fly(4);',
  18065. 'end;',
  18066. 'begin',
  18067. '']);
  18068. ConvertProgram;
  18069. CheckSource('TestNestedClass_CallInherited',
  18070. LinesToStr([ // statements
  18071. 'rtl.createClass(this, "TObject", null, function () {',
  18072. ' this.$init = function () {',
  18073. ' };',
  18074. ' this.$final = function () {',
  18075. ' };',
  18076. '});',
  18077. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18078. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18079. ' this.Fly = function (w) {',
  18080. ' var Result = 0;',
  18081. ' return Result;',
  18082. ' };',
  18083. ' }, "TBird.TWing");',
  18084. '});',
  18085. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18086. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18087. ' this.Fly = function (w) {',
  18088. ' var Result = 0;',
  18089. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18090. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18091. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18092. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18093. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18094. ' return Result;',
  18095. ' };',
  18096. ' }, "TEagle.TEagleWing");',
  18097. '});',
  18098. '']),
  18099. LinesToStr([ // $mod.$main
  18100. '']));
  18101. end;
  18102. procedure TTestModule.TestExternalClass_Var;
  18103. begin
  18104. StartProgram(false);
  18105. Add([
  18106. '{$modeswitch externalclass}',
  18107. 'type',
  18108. ' TExtA = class external name ''ExtObj''',
  18109. ' Id: longint external name ''$Id'';',
  18110. ' B: longint;',
  18111. ' end;',
  18112. 'var Obj: TExtA;',
  18113. 'begin',
  18114. ' obj.id:=obj.id+1;',
  18115. ' obj.B:=obj.B+1;']);
  18116. ConvertProgram;
  18117. CheckSource('TestExternalClass_Var',
  18118. LinesToStr([ // statements
  18119. 'this.Obj = null;',
  18120. '']),
  18121. LinesToStr([ // $mod.$main
  18122. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18123. '$mod.Obj.B = $mod.Obj.B + 1;',
  18124. '']));
  18125. end;
  18126. procedure TTestModule.TestExternalClass_Const;
  18127. begin
  18128. StartProgram(false);
  18129. Add([
  18130. '{$modeswitch externalclass}',
  18131. 'type',
  18132. ' TExtA = class external name ''ExtObj''',
  18133. ' const Two: longint = 2;',
  18134. ' const Three = 3;',
  18135. ' const Id: longint;',
  18136. ' end;',
  18137. ' TExtB = class external name ''ExtB''',
  18138. ' A: TExtA;',
  18139. ' end;',
  18140. 'var',
  18141. ' A: texta;',
  18142. ' B: textb;',
  18143. ' i: longint;',
  18144. 'begin',
  18145. ' i:=a.two;',
  18146. ' i:=texta.two;',
  18147. ' i:=a.three;',
  18148. ' i:=texta.three;',
  18149. ' i:=a.id;',
  18150. ' i:=texta.id;',
  18151. '']);
  18152. ConvertProgram;
  18153. CheckSource('TestExternalClass_Const',
  18154. LinesToStr([ // statements
  18155. 'this.A = null;',
  18156. 'this.B = null;',
  18157. 'this.i = 0;',
  18158. '']),
  18159. LinesToStr([ // $mod.$main
  18160. '$mod.i = 2;',
  18161. '$mod.i = 2;',
  18162. '$mod.i = 3;',
  18163. '$mod.i = 3;',
  18164. '$mod.i = $mod.A.Id;',
  18165. '$mod.i = ExtObj.Id;',
  18166. '']));
  18167. end;
  18168. procedure TTestModule.TestExternalClass_Dollar;
  18169. begin
  18170. StartProgram(false);
  18171. Add([
  18172. '{$modeswitch externalclass}',
  18173. 'type',
  18174. ' TExtA = class external name ''$''',
  18175. ' Id: longint external name ''$'';',
  18176. ' function Bla(i: longint): longint; external name ''$'';',
  18177. ' end;',
  18178. 'function dollar(k: longint): longint; external name ''$'';',
  18179. 'var Obj: TExtA;',
  18180. 'begin',
  18181. ' dollar(1);',
  18182. ' obj.id:=obj.id+2;',
  18183. ' obj.Bla(3);',
  18184. '']);
  18185. ConvertProgram;
  18186. CheckSource('TestExternalClass_Dollar',
  18187. LinesToStr([ // statements
  18188. 'this.Obj = null;',
  18189. '']),
  18190. LinesToStr([ // $mod.$main
  18191. '$(1);',
  18192. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18193. '$mod.Obj.$(3);',
  18194. '']));
  18195. end;
  18196. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18197. begin
  18198. StartProgram(false);
  18199. Add('{$modeswitch externalclass}');
  18200. Add('type');
  18201. Add(' TExtA = class external name ''ExtA''');
  18202. Add(' Id: longint external name ''$Id'';');
  18203. Add(' end;');
  18204. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18205. Add(' Id: longint;');
  18206. Add(' end;');
  18207. Add('begin');
  18208. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18209. ConvertProgram;
  18210. end;
  18211. procedure TTestModule.TestExternalClass_Method;
  18212. begin
  18213. StartProgram(false);
  18214. Add(['{$modeswitch externalclass}',
  18215. 'type',
  18216. ' TExtA = class external name ''ExtObj''',
  18217. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18218. ' procedure DoSome(Id: longint = 1);',
  18219. ' end;',
  18220. 'var Obj: texta;',
  18221. 'begin',
  18222. ' obj.doit;',
  18223. ' obj.doit();',
  18224. ' obj.doit(2);',
  18225. ' with obj do begin',
  18226. ' doit;',
  18227. ' doit();',
  18228. ' doit(3);',
  18229. ' end;']);
  18230. ConvertProgram;
  18231. CheckSource('TestExternalClass_Method',
  18232. LinesToStr([ // statements
  18233. 'this.Obj = null;',
  18234. '']),
  18235. LinesToStr([ // $mod.$main
  18236. '$mod.Obj.$Execute(1);',
  18237. '$mod.Obj.$Execute(1);',
  18238. '$mod.Obj.$Execute(2);',
  18239. 'var $with = $mod.Obj;',
  18240. '$with.$Execute(1);',
  18241. '$with.$Execute(1);',
  18242. '$with.$Execute(3);',
  18243. '']));
  18244. end;
  18245. procedure TTestModule.TestExternalClass_ClassMethod;
  18246. begin
  18247. StartProgram(false);
  18248. Add([
  18249. '{$modeswitch externalclass}',
  18250. 'type',
  18251. ' TExtA = class external name ''ExtObj''',
  18252. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18253. ' end;',
  18254. ' TExtB = TExtA;',
  18255. 'var p: Pointer;',
  18256. 'begin',
  18257. ' texta.doit;',
  18258. ' texta.doit();',
  18259. ' texta.doit(2);',
  18260. ' p:[email protected];',
  18261. ' with texta do begin',
  18262. ' doit;',
  18263. ' doit();',
  18264. ' doit(3);',
  18265. ' p:=@DoIt;',
  18266. ' end;',
  18267. ' textb.doit;',
  18268. ' textb.doit();',
  18269. ' textb.doit(4);',
  18270. ' with textb do begin',
  18271. ' doit;',
  18272. ' doit();',
  18273. ' doit(5);',
  18274. ' end;',
  18275. '']);
  18276. ConvertProgram;
  18277. CheckSource('TestExternalClass_ClassMethod',
  18278. LinesToStr([ // statements
  18279. 'this.p = null;',
  18280. '']),
  18281. LinesToStr([ // $mod.$main
  18282. 'ExtObj.$Execute(1);',
  18283. 'ExtObj.$Execute(1);',
  18284. 'ExtObj.$Execute(2);',
  18285. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18286. 'ExtObj.$Execute(1);',
  18287. 'ExtObj.$Execute(1);',
  18288. 'ExtObj.$Execute(3);',
  18289. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18290. 'ExtObj.$Execute(1);',
  18291. 'ExtObj.$Execute(1);',
  18292. 'ExtObj.$Execute(4);',
  18293. 'ExtObj.$Execute(1);',
  18294. 'ExtObj.$Execute(1);',
  18295. 'ExtObj.$Execute(5);',
  18296. '']));
  18297. end;
  18298. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18299. begin
  18300. StartProgram(false);
  18301. Add([
  18302. '{$modeswitch externalclass}',
  18303. 'type',
  18304. ' TExtA = class external name ''ExtObj''',
  18305. ' class procedure DoIt(Id: longint = 1); static;',
  18306. ' end;',
  18307. 'var p: Pointer;',
  18308. 'begin',
  18309. ' texta.doit;',
  18310. ' texta.doit();',
  18311. ' texta.doit(2);',
  18312. ' p:[email protected];',
  18313. ' with texta do begin',
  18314. ' doit;',
  18315. ' doit();',
  18316. ' doit(3);',
  18317. ' p:=@DoIt;',
  18318. ' end;',
  18319. '']);
  18320. ConvertProgram;
  18321. CheckSource('TestExternalClass_ClassMethodStatic',
  18322. LinesToStr([ // statements
  18323. 'this.p = null;',
  18324. '']),
  18325. LinesToStr([ // $mod.$main
  18326. 'ExtObj.DoIt(1);',
  18327. 'ExtObj.DoIt(1);',
  18328. 'ExtObj.DoIt(2);',
  18329. '$mod.p = ExtObj.DoIt;',
  18330. 'ExtObj.DoIt(1);',
  18331. 'ExtObj.DoIt(1);',
  18332. 'ExtObj.DoIt(3);',
  18333. '$mod.p = ExtObj.DoIt;',
  18334. '']));
  18335. end;
  18336. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18337. begin
  18338. StartProgram(false);
  18339. Add([
  18340. '{$modeswitch externalclass}',
  18341. 'type',
  18342. ' TBird = class external name ''Array''',
  18343. ' end;',
  18344. 'function GetPtr: Pointer;',
  18345. 'begin',
  18346. 'end;',
  18347. 'procedure Write(const p);',
  18348. 'begin',
  18349. 'end;',
  18350. 'procedure WriteLn; varargs;',
  18351. 'begin',
  18352. 'end;',
  18353. 'begin',
  18354. ' if TBird(GetPtr)=nil then ;',
  18355. ' Write(GetPtr);',
  18356. ' WriteLn(GetPtr);',
  18357. ' Write(TBird(GetPtr));',
  18358. ' WriteLn(TBird(GetPtr));',
  18359. '']);
  18360. ConvertProgram;
  18361. CheckSource('TestFunctionResultInTypeCast',
  18362. LinesToStr([ // statements
  18363. 'this.GetPtr = function () {',
  18364. ' var Result = null;',
  18365. ' return Result;',
  18366. '};',
  18367. 'this.Write = function (p) {',
  18368. '};',
  18369. 'this.WriteLn = function () {',
  18370. '};',
  18371. '']),
  18372. LinesToStr([
  18373. 'if ($mod.GetPtr() === null) ;',
  18374. '$mod.Write($mod.GetPtr());',
  18375. '$mod.WriteLn($mod.GetPtr());',
  18376. '$mod.Write($mod.GetPtr());',
  18377. '$mod.WriteLn($mod.GetPtr());',
  18378. '']));
  18379. end;
  18380. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18381. begin
  18382. StartProgram(false);
  18383. Add([
  18384. '{$modeswitch externalclass}',
  18385. 'type',
  18386. ' TExtA = class external name ''ExtObjA''',
  18387. ' procedure ProcA; virtual;',
  18388. ' procedure ProcB; virtual;',
  18389. ' end;',
  18390. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18391. ' end;',
  18392. ' TExtC = class (TExtB)',
  18393. ' procedure ProcA; override;',
  18394. ' end;',
  18395. 'procedure TExtC.ProcA;',
  18396. 'begin',
  18397. ' ProcA;',
  18398. ' Self.ProcA;',
  18399. ' ProcB;',
  18400. ' Self.ProcB;',
  18401. 'end;',
  18402. 'var',
  18403. ' A: texta;',
  18404. ' B: textb;',
  18405. ' C: textc;',
  18406. 'begin',
  18407. ' a.proca;',
  18408. ' b.proca;',
  18409. ' c.proca;']);
  18410. ConvertProgram;
  18411. CheckSource('TestExternalClass_NonExternalOverride',
  18412. LinesToStr([ // statements
  18413. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18414. ' this.$init = function () {',
  18415. ' };',
  18416. ' this.$final = function () {',
  18417. ' };',
  18418. ' this.ProcA = function () {',
  18419. ' this.ProcA();',
  18420. ' this.ProcA();',
  18421. ' this.ProcB();',
  18422. ' this.ProcB();',
  18423. ' };',
  18424. '});',
  18425. 'this.A = null;',
  18426. 'this.B = null;',
  18427. 'this.C = null;',
  18428. '']),
  18429. LinesToStr([ // $mod.$main
  18430. '$mod.A.ProcA();',
  18431. '$mod.B.ProcA();',
  18432. '$mod.C.ProcA();',
  18433. '']));
  18434. end;
  18435. procedure TTestModule.TestExternalClass_OverloadHint;
  18436. begin
  18437. StartProgram(false);
  18438. Add([
  18439. '{$modeswitch externalclass}',
  18440. 'type',
  18441. ' TExtA = class external name ''ExtObjA''',
  18442. ' procedure DoIt;',
  18443. ' procedure DoIt(i: longint);',
  18444. ' end;',
  18445. 'begin',
  18446. '']);
  18447. ConvertProgram;
  18448. CheckResolverUnexpectedHints(true);
  18449. CheckSource('TestExternalClass_OverloadHint',
  18450. LinesToStr([ // statements
  18451. '']),
  18452. LinesToStr([ // $mod.$main
  18453. '']));
  18454. end;
  18455. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18456. begin
  18457. WithTypeInfo:=true;
  18458. StartProgram(false);
  18459. Add([
  18460. '{$modeswitch externalclass}',
  18461. 'type',
  18462. ' JSwiper = class external name ''Swiper''',
  18463. ' constructor New;',
  18464. ' end;',
  18465. ' TObject = class',
  18466. ' private',
  18467. ' FSwiper: JSwiper;',
  18468. ' published',
  18469. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18470. ' end;',
  18471. 'begin',
  18472. ' JSwiper.new;',
  18473. '']);
  18474. ConvertProgram;
  18475. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18476. LinesToStr([ // statements
  18477. 'this.$rtti.$ExtClass("JSwiper", {',
  18478. ' jsclass: "Swiper"',
  18479. '});',
  18480. 'rtl.createClass(this, "TObject", null, function () {',
  18481. ' this.$init = function () {',
  18482. ' this.FSwiper = null;',
  18483. ' };',
  18484. ' this.$final = function () {',
  18485. ' this.FSwiper = undefined;',
  18486. ' };',
  18487. ' var $r = this.$rtti;',
  18488. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18489. '});',
  18490. '']),
  18491. LinesToStr([ // $mod.$main
  18492. 'new Swiper();',
  18493. '']));
  18494. end;
  18495. procedure TTestModule.TestExternalClass_Property;
  18496. begin
  18497. StartProgram(false);
  18498. Add([
  18499. '{$modeswitch externalclass}',
  18500. 'type',
  18501. ' TExtA = class external name ''ExtA''',
  18502. ' function getYear: longint;',
  18503. ' procedure setYear(Value: longint);',
  18504. ' property Year: longint read getyear write setyear;',
  18505. ' end;',
  18506. ' TExtB = class (TExtA)',
  18507. ' procedure OtherSetYear(Value: longint);',
  18508. ' property year write othersetyear;',
  18509. ' end;',
  18510. 'procedure textb.othersetyear(value: longint);',
  18511. 'begin',
  18512. ' setYear(Value+4);',
  18513. 'end;',
  18514. 'var',
  18515. ' A: texta;',
  18516. ' B: textb;',
  18517. 'begin',
  18518. ' a.year:=a.year+1;',
  18519. ' b.year:=b.year+2;']);
  18520. ConvertProgram;
  18521. CheckSource('TestExternalClass_NonExternalOverride',
  18522. LinesToStr([ // statements
  18523. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18524. ' this.$init = function () {',
  18525. ' };',
  18526. ' this.$final = function () {',
  18527. ' };',
  18528. ' this.OtherSetYear = function (Value) {',
  18529. ' this.setYear(Value+4);',
  18530. ' };',
  18531. '});',
  18532. 'this.A = null;',
  18533. 'this.B = null;',
  18534. '']),
  18535. LinesToStr([ // $mod.$main
  18536. '$mod.A.setYear($mod.A.getYear()+1);',
  18537. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18538. '']));
  18539. end;
  18540. procedure TTestModule.TestExternalClass_PropertyDate;
  18541. begin
  18542. StartProgram(false);
  18543. Add([
  18544. '{$modeswitch externalclass}',
  18545. 'type',
  18546. ' TExtA = class external name ''ExtA''',
  18547. ' end;',
  18548. ' TExtB = class (TExtA)',
  18549. ' FDate: string;',
  18550. ' property Date: string read FDate write FDate;',
  18551. ' property ExtA: string read FDate write FDate;',
  18552. ' end;',
  18553. ' {$M+}',
  18554. ' TObject = class',
  18555. ' FDate: string;',
  18556. ' published',
  18557. ' property Date: string read FDate write FDate;',
  18558. ' property ExtA: string read FDate write FDate;',
  18559. ' end;',
  18560. 'var',
  18561. ' B: textb;',
  18562. ' o: TObject;',
  18563. 'begin',
  18564. ' b.date:=b.exta;',
  18565. ' o.date:=o.exta;']);
  18566. ConvertProgram;
  18567. CheckSource('TestExternalClass_PropertyDate',
  18568. LinesToStr([ // statements
  18569. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18570. ' this.$init = function () {',
  18571. ' this.FDate = "";',
  18572. ' };',
  18573. ' this.$final = function () {',
  18574. ' };',
  18575. '});',
  18576. 'rtl.createClass(this, "TObject", null, function () {',
  18577. ' this.$init = function () {',
  18578. ' this.FDate = "";',
  18579. ' };',
  18580. ' this.$final = function () {',
  18581. ' };',
  18582. ' var $r = this.$rtti;',
  18583. ' $r.addField("FDate", rtl.string);',
  18584. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18585. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18586. '});',
  18587. 'this.B = null;',
  18588. 'this.o = null;',
  18589. '']),
  18590. LinesToStr([ // $mod.$main
  18591. '$mod.B.FDate = $mod.B.FDate;',
  18592. '$mod.o.FDate = $mod.o.FDate;',
  18593. '']));
  18594. end;
  18595. procedure TTestModule.TestExternalClass_ClassProperty;
  18596. begin
  18597. StartProgram(false);
  18598. Add('{$modeswitch externalclass}');
  18599. Add('type');
  18600. Add(' TExtA = class external name ''ExtA''');
  18601. Add(' class function getYear: longint;');
  18602. Add(' class procedure setYear(Value: longint);');
  18603. Add(' class property Year: longint read getyear write setyear;');
  18604. Add(' end;');
  18605. Add(' TExtB = class (TExtA)');
  18606. Add(' class function GetCentury: longint;');
  18607. Add(' class procedure SetCentury(Value: longint);');
  18608. Add(' class property Century: longint read getcentury write setcentury;');
  18609. Add(' end;');
  18610. Add('class function textb.getcentury: longint;');
  18611. Add('begin');
  18612. Add('end;');
  18613. Add('class procedure textb.setcentury(value: longint);');
  18614. Add('begin');
  18615. Add(' setyear(value+11);');
  18616. Add(' texta.year:=texta.year+12;');
  18617. Add(' year:=year+13;');
  18618. Add(' textb.century:=textb.century+14;');
  18619. Add(' century:=century+15;');
  18620. Add('end;');
  18621. Add('var');
  18622. Add(' A: texta;');
  18623. Add(' B: textb;');
  18624. Add('begin');
  18625. Add(' texta.year:=texta.year+1;');
  18626. Add(' textb.year:=textb.year+2;');
  18627. Add(' TextA.year:=TextA.year+3;');
  18628. Add(' b.year:=b.year+4;');
  18629. Add(' textb.century:=textb.century+5;');
  18630. Add(' b.century:=b.century+6;');
  18631. ConvertProgram;
  18632. CheckSource('TestExternalClass_ClassProperty',
  18633. LinesToStr([ // statements
  18634. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18635. ' this.$init = function () {',
  18636. ' };',
  18637. ' this.$final = function () {',
  18638. ' };',
  18639. ' this.GetCentury = function () {',
  18640. ' var Result = 0;',
  18641. ' return Result;',
  18642. ' };',
  18643. ' this.SetCentury = function (Value) {',
  18644. ' this.setYear(Value + 11);',
  18645. ' ExtA.setYear(ExtA.getYear() + 12);',
  18646. ' this.setYear(this.getYear() + 13);',
  18647. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18648. ' this.SetCentury(this.GetCentury() + 15);',
  18649. ' };',
  18650. '});',
  18651. 'this.A = null;',
  18652. 'this.B = null;',
  18653. '']),
  18654. LinesToStr([ // $mod.$main
  18655. 'ExtA.setYear(ExtA.getYear() + 1);',
  18656. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18657. 'ExtA.setYear(ExtA.getYear() + 3);',
  18658. '$mod.B.setYear($mod.B.getYear() + 4);',
  18659. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18660. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18661. '']));
  18662. end;
  18663. procedure TTestModule.TestExternalClass_ClassOf;
  18664. begin
  18665. StartProgram(false);
  18666. Add('{$modeswitch externalclass}');
  18667. Add('type');
  18668. Add(' TExtA = class external name ''ExtA''');
  18669. Add(' procedure ProcA; virtual;');
  18670. Add(' procedure ProcB; virtual;');
  18671. Add(' end;');
  18672. Add(' TExtAClass = class of TExtA;');
  18673. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18674. Add(' end;');
  18675. Add(' TExtBClass = class of TExtB;');
  18676. Add(' TExtC = class (TExtB)');
  18677. Add(' procedure ProcA; override;');
  18678. Add(' end;');
  18679. Add(' TExtCClass = class of TExtC;');
  18680. Add('procedure TExtC.ProcA; begin end;');
  18681. Add('var');
  18682. Add(' A: texta; ClA: TExtAClass;');
  18683. Add(' B: textb; ClB: TExtBClass;');
  18684. Add(' C: textc; ClC: TExtCClass;');
  18685. Add('begin');
  18686. Add(' ClA:=texta;');
  18687. Add(' ClA:=textb;');
  18688. Add(' ClA:=textc;');
  18689. Add(' ClB:=textb;');
  18690. Add(' ClB:=textc;');
  18691. Add(' ClC:=textc;');
  18692. ConvertProgram;
  18693. CheckSource('TestExternalClass_ClassOf',
  18694. LinesToStr([ // statements
  18695. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18696. ' this.$init = function () {',
  18697. ' };',
  18698. ' this.$final = function () {',
  18699. ' };',
  18700. ' this.ProcA = function () {',
  18701. ' };',
  18702. '});',
  18703. 'this.A = null;',
  18704. 'this.ClA = null;',
  18705. 'this.B = null;',
  18706. 'this.ClB = null;',
  18707. 'this.C = null;',
  18708. 'this.ClC = null;',
  18709. '']),
  18710. LinesToStr([ // $mod.$main
  18711. '$mod.ClA = ExtA;',
  18712. '$mod.ClA = ExtB;',
  18713. '$mod.ClA = $mod.TExtC;',
  18714. '$mod.ClB = ExtB;',
  18715. '$mod.ClB = $mod.TExtC;',
  18716. '$mod.ClC = $mod.TExtC;',
  18717. '']));
  18718. end;
  18719. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18720. begin
  18721. AddModuleWithIntfImplSrc('unit2.pas',
  18722. LinesToStr([
  18723. '{$modeswitch externalclass}',
  18724. 'type',
  18725. ' TExtA = class external name ''ExtA''',
  18726. ' class var Id: longint;',
  18727. ' end;',
  18728. '']),
  18729. '');
  18730. StartUnit(true);
  18731. Add('interface');
  18732. Add('uses unit2;');
  18733. Add('implementation');
  18734. Add('begin');
  18735. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18736. ConvertUnit;
  18737. CheckSource('TestExternalClass_ClassOtherUnit',
  18738. LinesToStr([
  18739. '']),
  18740. LinesToStr([
  18741. 'ExtA.Id = ExtA.Id + 1;',
  18742. '']));
  18743. end;
  18744. procedure TTestModule.TestExternalClass_Is;
  18745. begin
  18746. StartProgram(false);
  18747. Add([
  18748. '{$modeswitch externalclass}',
  18749. 'type',
  18750. ' TExtA = class external name ''ExtA''',
  18751. ' end;',
  18752. ' TExtAClass = class of TExtA;',
  18753. ' TExtB = class external name ''ExtB'' (TExtA)',
  18754. ' end;',
  18755. ' TExtBClass = class of TExtB;',
  18756. ' TExtC = class (TExtB)',
  18757. ' end;',
  18758. ' TExtCClass = class of TExtC;',
  18759. 'var',
  18760. ' A: texta; ClA: TExtAClass;',
  18761. ' B: textb; ClB: TExtBClass;',
  18762. ' C: textc; ClC: TExtCClass;',
  18763. 'begin',
  18764. ' if a is textb then ;',
  18765. ' if a is textc then ;',
  18766. ' if b is textc then ;',
  18767. ' if cla is textb then ;',
  18768. ' if cla is textc then ;',
  18769. ' if clb is textc then ;',
  18770. ' try',
  18771. ' except',
  18772. ' on TExtA do ;',
  18773. ' on e: TExtB do ;',
  18774. ' end;',
  18775. '']);
  18776. ConvertProgram;
  18777. CheckSource('TestExternalClass_Is',
  18778. LinesToStr([ // statements
  18779. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18780. ' this.$init = function () {',
  18781. ' };',
  18782. ' this.$final = function () {',
  18783. ' };',
  18784. '});',
  18785. 'this.A = null;',
  18786. 'this.ClA = null;',
  18787. 'this.B = null;',
  18788. 'this.ClB = null;',
  18789. 'this.C = null;',
  18790. 'this.ClC = null;',
  18791. '']),
  18792. LinesToStr([ // $mod.$main
  18793. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18794. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18795. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18796. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18797. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18798. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18799. 'try {} catch ($e) {',
  18800. ' if (rtl.isExt($e,ExtA)) {}',
  18801. ' else if (rtl.isExt($e,ExtB)) {',
  18802. ' var e = $e;',
  18803. ' } else throw $e',
  18804. '};',
  18805. '']));
  18806. end;
  18807. procedure TTestModule.TestExternalClass_As;
  18808. begin
  18809. StartProgram(false);
  18810. Add('{$modeswitch externalclass}');
  18811. Add('type');
  18812. Add(' TExtA = class external name ''ExtA''');
  18813. Add(' end;');
  18814. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18815. Add(' end;');
  18816. Add(' TExtC = class (TExtB)');
  18817. Add(' end;');
  18818. Add('var');
  18819. Add(' A: texta;');
  18820. Add(' B: textb;');
  18821. Add(' C: textc;');
  18822. Add('begin');
  18823. Add(' b:=a as textb;');
  18824. Add(' c:=a as textc;');
  18825. Add(' c:=b as textc;');
  18826. ConvertProgram;
  18827. CheckSource('TestExternalClass_Is',
  18828. LinesToStr([ // statements
  18829. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18830. ' this.$init = function () {',
  18831. ' };',
  18832. ' this.$final = function () {',
  18833. ' };',
  18834. '});',
  18835. 'this.A = null;',
  18836. 'this.B = null;',
  18837. 'this.C = null;',
  18838. '']),
  18839. LinesToStr([ // $mod.$main
  18840. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18841. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18842. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18843. '']));
  18844. end;
  18845. procedure TTestModule.TestExternalClass_DestructorFail;
  18846. begin
  18847. StartProgram(false);
  18848. Add('{$modeswitch externalclass}');
  18849. Add('type');
  18850. Add(' TExtA = class external name ''ExtA''');
  18851. Add(' destructor Free;');
  18852. Add(' end;');
  18853. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18854. nPasElementNotSupported);
  18855. ConvertProgram;
  18856. end;
  18857. procedure TTestModule.TestExternalClass_New;
  18858. begin
  18859. StartProgram(false);
  18860. Add([
  18861. '{$modeswitch externalclass}',
  18862. 'type',
  18863. ' TExtA = class external name ''ExtA''',
  18864. ' constructor New;',
  18865. ' constructor New(i: longint; j: longint = 2);',
  18866. ' end;',
  18867. 'var',
  18868. ' A: texta;',
  18869. 'begin',
  18870. ' a:=texta.new;',
  18871. ' a:=texta(texta.new);',
  18872. ' a:=texta.new();',
  18873. ' a:=texta.new(1);',
  18874. ' with texta do begin',
  18875. ' a:=new;',
  18876. ' a:=new();',
  18877. ' a:=new(2);',
  18878. ' end;',
  18879. ' a:=test1.texta.new;',
  18880. ' a:=test1.texta.new();',
  18881. ' a:=test1.texta.new(3);',
  18882. '']);
  18883. ConvertProgram;
  18884. CheckSource('TestExternalClass_New',
  18885. LinesToStr([ // statements
  18886. 'this.A = null;',
  18887. '']),
  18888. LinesToStr([ // $mod.$main
  18889. '$mod.A = new ExtA();',
  18890. '$mod.A = new ExtA();',
  18891. '$mod.A = new ExtA();',
  18892. '$mod.A = new ExtA(1,2);',
  18893. '$mod.A = new ExtA();',
  18894. '$mod.A = new ExtA();',
  18895. '$mod.A = new ExtA(2,2);',
  18896. '$mod.A = new ExtA();',
  18897. '$mod.A = new ExtA();',
  18898. '$mod.A = new ExtA(3,2);',
  18899. '']));
  18900. end;
  18901. procedure TTestModule.TestExternalClass_ClassOf_New;
  18902. begin
  18903. StartProgram(false);
  18904. Add('{$modeswitch externalclass}');
  18905. Add('type');
  18906. Add(' TExtAClass = class of TExtA;');
  18907. Add(' TExtA = class external name ''ExtA''');
  18908. Add(' C: TExtAClass;');
  18909. Add(' constructor New;');
  18910. Add(' end;');
  18911. Add('var');
  18912. Add(' A: texta;');
  18913. Add(' C: textaclass;');
  18914. Add('begin');
  18915. Add(' a:=c.new;');
  18916. Add(' a:=c.new();');
  18917. Add(' with C do begin');
  18918. Add(' a:=new;');
  18919. Add(' a:=new();');
  18920. Add(' end;');
  18921. Add(' a:=test1.c.new;');
  18922. Add(' a:=test1.c.new();');
  18923. Add(' a:=A.c.new();');
  18924. ConvertProgram;
  18925. CheckSource('TestExternalClass_ClassOf_New',
  18926. LinesToStr([ // statements
  18927. 'this.A = null;',
  18928. 'this.C = null;',
  18929. '']),
  18930. LinesToStr([ // $mod.$main
  18931. '$mod.A = new $mod.C();',
  18932. '$mod.A = new $mod.C();',
  18933. 'var $with = $mod.C;',
  18934. '$mod.A = new $with();',
  18935. '$mod.A = new $with();',
  18936. '$mod.A = new $mod.C();',
  18937. '$mod.A = new $mod.C();',
  18938. '$mod.A = new $mod.A.C();',
  18939. '']));
  18940. end;
  18941. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18942. begin
  18943. StartProgram(false);
  18944. Add([
  18945. '{$modeswitch externalclass}',
  18946. 'type',
  18947. ' TExtAClass = class of TExtA;',
  18948. ' TExtA = class external name ''ExtA''',
  18949. ' constructor New;',
  18950. ' end;',
  18951. 'function GetCreator: TExtAClass;',
  18952. 'begin',
  18953. ' Result:=TExtA;',
  18954. 'end;',
  18955. 'var',
  18956. ' A: texta;',
  18957. 'begin',
  18958. ' a:=getcreator.new;',
  18959. ' a:=getcreator().new;',
  18960. ' a:=getcreator().new();',
  18961. ' a:=getcreator.new();',
  18962. ' with getcreator do begin',
  18963. ' a:=new;',
  18964. ' a:=new();',
  18965. ' end;']);
  18966. ConvertProgram;
  18967. CheckSource('TestExternalClass_FuncClassOf_New',
  18968. LinesToStr([ // statements
  18969. 'this.GetCreator = function () {',
  18970. ' var Result = null;',
  18971. ' Result = ExtA;',
  18972. ' return Result;',
  18973. '};',
  18974. 'this.A = null;',
  18975. '']),
  18976. LinesToStr([ // $mod.$main
  18977. '$mod.A = new ($mod.GetCreator())();',
  18978. '$mod.A = new ($mod.GetCreator())();',
  18979. '$mod.A = new ($mod.GetCreator())();',
  18980. '$mod.A = new ($mod.GetCreator())();',
  18981. 'var $with = $mod.GetCreator();',
  18982. '$mod.A = new $with();',
  18983. '$mod.A = new $with();',
  18984. '']));
  18985. end;
  18986. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18987. begin
  18988. StartProgram(false);
  18989. Add([
  18990. '{$modeswitch externalclass}',
  18991. 'type',
  18992. ' TExtA = class external name ''ExtA''',
  18993. ' constructor New;',
  18994. ' end;',
  18995. ' TBird = class(TExtA)',
  18996. ' end;',
  18997. 'begin',
  18998. ' TBird.new;',
  18999. '']);
  19000. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19001. ConvertProgram;
  19002. end;
  19003. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19004. begin
  19005. StartProgram(false);
  19006. Add([
  19007. '{$modeswitch externalclass}',
  19008. 'type',
  19009. ' TExtA = class external name ''ExtA''',
  19010. ' constructor New;',
  19011. ' end;',
  19012. ' TBird = class(TExtA)',
  19013. ' end;',
  19014. 'begin',
  19015. ' TBird.new();',
  19016. '']);
  19017. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19018. ConvertProgram;
  19019. end;
  19020. procedure TTestModule.TestExternalClass_NewExtName;
  19021. begin
  19022. StartProgram(false);
  19023. Add([
  19024. '{$modeswitch externalclass}',
  19025. 'type',
  19026. ' TExtA = class external name ''ExtA''',
  19027. ' constructor New; external name ''Other'';',
  19028. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19029. ' end;',
  19030. 'var',
  19031. ' A: texta;',
  19032. 'begin',
  19033. ' a:=texta.new;',
  19034. ' a:=texta(texta.new);',
  19035. ' a:=texta.new();',
  19036. ' a:=texta.new(1);',
  19037. ' with texta do begin',
  19038. ' a:=new;',
  19039. ' a:=new();',
  19040. ' a:=new(2);',
  19041. ' end;',
  19042. ' a:=test1.texta.new;',
  19043. ' a:=test1.texta.new();',
  19044. ' a:=test1.texta.new(3);',
  19045. '']);
  19046. ConvertProgram;
  19047. CheckSource('TestExternalClass_NewExtName',
  19048. LinesToStr([ // statements
  19049. 'this.A = null;',
  19050. '']),
  19051. LinesToStr([ // $mod.$main
  19052. '$mod.A = new Other();',
  19053. '$mod.A = new Other();',
  19054. '$mod.A = new Other();',
  19055. '$mod.A = new A.B(1,2);',
  19056. '$mod.A = new Other();',
  19057. '$mod.A = new Other();',
  19058. '$mod.A = new A.B(2,2);',
  19059. '$mod.A = new Other();',
  19060. '$mod.A = new Other();',
  19061. '$mod.A = new A.B(3,2);',
  19062. '']));
  19063. end;
  19064. procedure TTestModule.TestExternalClass_Constructor;
  19065. begin
  19066. StartProgram(false);
  19067. Add([
  19068. '{$modeswitch externalclass}',
  19069. 'type',
  19070. ' TExtA = class external name ''ExtA''',
  19071. ' public type',
  19072. ' TExtB = class external name ''ExtB''',
  19073. ' public type',
  19074. ' TExtC = class external name ''ExtC''',
  19075. ' constructor New;',
  19076. ' constructor New(i: word);',
  19077. ' end;',
  19078. ' end;',
  19079. ' constructor Create;',
  19080. ' constructor Create(i: longint; j: longint = 2);',
  19081. ' end;',
  19082. 'var',
  19083. ' A: texta;',
  19084. ' C: texta.textb.textc;',
  19085. 'begin',
  19086. ' a:=texta.create;',
  19087. ' a:=texta(texta.create);',
  19088. ' a:=texta.create();',
  19089. ' a:=texta.create(1);',
  19090. ' with texta do begin',
  19091. ' a:=create;',
  19092. ' a:=create();',
  19093. ' a:=create(2);',
  19094. ' end;',
  19095. ' a:=test1.texta.create;',
  19096. ' a:=test1.texta.create();',
  19097. ' a:=test1.texta.create(3);',
  19098. ' c:=texta.textb.textc.new;',
  19099. ' c:=texta.textb.textc.new();',
  19100. ' c:=texta.textb.textc.new(4);',
  19101. '']);
  19102. ConvertProgram;
  19103. CheckSource('TestExternalClass_Constructor',
  19104. LinesToStr([ // statements
  19105. 'this.A = null;',
  19106. 'this.C = null;',
  19107. '']),
  19108. LinesToStr([ // $mod.$main
  19109. '$mod.A = new ExtA.Create();',
  19110. '$mod.A = new ExtA.Create();',
  19111. '$mod.A = new ExtA.Create();',
  19112. '$mod.A = new ExtA.Create(1,2);',
  19113. '$mod.A = new ExtA.Create();',
  19114. '$mod.A = new ExtA.Create();',
  19115. '$mod.A = new ExtA.Create(2,2);',
  19116. '$mod.A = new ExtA.Create();',
  19117. '$mod.A = new ExtA.Create();',
  19118. '$mod.A = new ExtA.Create(3,2);',
  19119. '$mod.C = new ExtA.ExtB.ExtC();',
  19120. '$mod.C = new ExtA.ExtB.ExtC();',
  19121. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19122. '']));
  19123. end;
  19124. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19125. begin
  19126. StartProgram(false);
  19127. Add([
  19128. '{$modeswitch externalclass}',
  19129. 'type',
  19130. ' TExtA = class external name ''ExtA''',
  19131. ' constructor Create; external name ''{}'';',
  19132. ' end;',
  19133. 'var',
  19134. ' A: texta;',
  19135. 'begin',
  19136. ' a:=texta.create;',
  19137. ' a:=texta(texta.create);',
  19138. ' a:=texta.create();',
  19139. ' with texta do begin',
  19140. ' a:=create;',
  19141. ' a:=create();',
  19142. ' end;',
  19143. ' a:=test1.texta.create;',
  19144. ' a:=test1.texta.create();',
  19145. '']);
  19146. ConvertProgram;
  19147. CheckSource('TestExternalClass_ConstructorBrackets',
  19148. LinesToStr([ // statements
  19149. 'this.A = null;',
  19150. '']),
  19151. LinesToStr([ // $mod.$main
  19152. '$mod.A = {};',
  19153. '$mod.A = {};',
  19154. '$mod.A = {};',
  19155. '$mod.A = {};',
  19156. '$mod.A = {};',
  19157. '$mod.A = {};',
  19158. '$mod.A = {};',
  19159. '']));
  19160. end;
  19161. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19162. begin
  19163. StartProgram(false);
  19164. Add('{$modeswitch externalclass}');
  19165. Add('type');
  19166. Add(' TExtA = class external name ''ExtA''');
  19167. Add(' constructor New;');
  19168. Add(' end;');
  19169. Add('function DoIt: longint;');
  19170. Add('const ExtA: longint = 3;');
  19171. Add('begin');
  19172. Add(' Result:=ExtA;');
  19173. Add('end;');
  19174. Add('var');
  19175. Add(' A: texta;');
  19176. Add('begin');
  19177. Add(' a:=texta.new;');
  19178. ConvertProgram;
  19179. CheckSource('TestExternalClass_LocalConstSameName',
  19180. LinesToStr([ // statements
  19181. 'var ExtA$1 = 3;',
  19182. 'this.DoIt = function () {',
  19183. ' var Result = 0;',
  19184. ' Result = ExtA$1;',
  19185. ' return Result;',
  19186. '};',
  19187. 'this.A = null;',
  19188. '']),
  19189. LinesToStr([ // $mod.$main
  19190. '$mod.A = new ExtA();',
  19191. '']));
  19192. end;
  19193. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19194. begin
  19195. StartProgram(false);
  19196. Add('{$modeswitch externalclass}');
  19197. Add('type');
  19198. Add(' TExtA = class external name ''ExtA''');
  19199. Add(' procedure DoIt;');
  19200. Add(' end;');
  19201. Add(' TMyA = class(TExtA)');
  19202. Add(' procedure DoIt;');
  19203. Add(' end;');
  19204. Add('procedure TMyA.DoIt; begin end;');
  19205. Add('begin');
  19206. ConvertProgram;
  19207. CheckSource('TestExternalClass_ReintroduceOverload',
  19208. LinesToStr([ // statements
  19209. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19210. ' this.$init = function () {',
  19211. ' };',
  19212. ' this.$final = function () {',
  19213. ' };',
  19214. ' this.DoIt$1 = function () {',
  19215. ' };',
  19216. '});',
  19217. '']),
  19218. LinesToStr([ // $mod.$main
  19219. '']));
  19220. end;
  19221. procedure TTestModule.TestExternalClass_Inherited;
  19222. begin
  19223. StartProgram(false);
  19224. Add('{$modeswitch externalclass}');
  19225. Add('type');
  19226. Add(' TExtA = class external name ''ExtA''');
  19227. Add(' procedure DoIt(i: longint = 1); virtual;');
  19228. Add(' procedure DoSome(j: longint = 2);');
  19229. Add(' end;');
  19230. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19231. Add(' end;');
  19232. Add(' TMyC = class(TExtB)');
  19233. Add(' procedure DoIt(i: longint = 1); override;');
  19234. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19235. Add(' end;');
  19236. Add('procedure TMyC.DoIt(i: longint);');
  19237. Add('begin');
  19238. Add(' inherited;');
  19239. Add(' inherited DoIt;');
  19240. Add(' inherited DoIt();');
  19241. Add(' inherited DoIt(3);');
  19242. Add(' inherited DoSome;');
  19243. Add(' inherited DoSome();');
  19244. Add(' inherited DoSome(4);');
  19245. Add('end;');
  19246. Add('procedure TMyC.DoSome(j: longint);');
  19247. Add('begin');
  19248. Add(' inherited;');
  19249. Add('end;');
  19250. Add('begin');
  19251. ConvertProgram;
  19252. CheckSource('TestExternalClass_ReintroduceOverload',
  19253. LinesToStr([ // statements
  19254. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19255. ' this.$init = function () {',
  19256. ' };',
  19257. ' this.$final = function () {',
  19258. ' };',
  19259. ' this.DoIt = function (i) {',
  19260. ' ExtB.DoIt.apply(this, arguments);',
  19261. ' ExtB.DoIt.call(this, 1);',
  19262. ' ExtB.DoIt.call(this, 1);',
  19263. ' ExtB.DoIt.call(this, 3);',
  19264. ' ExtB.DoSome.call(this, 2);',
  19265. ' ExtB.DoSome.call(this, 2);',
  19266. ' ExtB.DoSome.call(this, 4);',
  19267. ' };',
  19268. ' this.DoSome$1 = function (j) {',
  19269. ' ExtB.DoSome.apply(this, arguments);',
  19270. ' };',
  19271. '});',
  19272. '']),
  19273. LinesToStr([ // $mod.$main
  19274. '']));
  19275. end;
  19276. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19277. begin
  19278. StartProgram(false);
  19279. Add('{$modeswitch externalclass}');
  19280. Add('type');
  19281. Add(' TObject = class');
  19282. Add(' end;');
  19283. Add(' TExtA = class external name ''ExtA''(TObject)');
  19284. Add(' end;');
  19285. Add('begin');
  19286. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19287. ConvertProgram;
  19288. end;
  19289. procedure TTestModule.TestExternalClass_NewInstance;
  19290. begin
  19291. StartProgram(false);
  19292. Add('{$modeswitch externalclass}');
  19293. Add('type');
  19294. Add(' TExtA = class external name ''ExtA''');
  19295. Add(' end;');
  19296. Add(' TMyB = class(TExtA)');
  19297. Add(' protected');
  19298. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19299. Add(' end;');
  19300. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19301. Add('begin end;');
  19302. Add('begin');
  19303. ConvertProgram;
  19304. CheckSource('TestExternalClass_NewInstance',
  19305. LinesToStr([ // statements
  19306. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19307. ' this.$init = function () {',
  19308. ' };',
  19309. ' this.$final = function () {',
  19310. ' };',
  19311. ' this.NewInstance = function (fnname, paramarray) {',
  19312. ' var Result = null;',
  19313. ' return Result;',
  19314. ' };',
  19315. '});',
  19316. '']),
  19317. LinesToStr([ // $mod.$main
  19318. '']));
  19319. end;
  19320. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19321. begin
  19322. StartProgram(false);
  19323. Add('{$modeswitch externalclass}');
  19324. Add('type');
  19325. Add(' TExtA = class external name ''ExtA''');
  19326. Add(' end;');
  19327. Add(' TMyB = class(TExtA)');
  19328. Add(' protected');
  19329. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19330. Add(' end;');
  19331. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19332. Add('begin end;');
  19333. Add('begin');
  19334. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19335. ConvertProgram;
  19336. end;
  19337. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19338. begin
  19339. StartProgram(false);
  19340. Add('{$modeswitch externalclass}');
  19341. Add('type');
  19342. Add(' TExtA = class external name ''ExtA''');
  19343. Add(' end;');
  19344. Add(' TMyB = class(TExtA)');
  19345. Add(' protected');
  19346. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19347. Add(' end;');
  19348. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19349. Add('begin end;');
  19350. Add('begin');
  19351. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19352. nIncompatibleTypeArgNo);
  19353. ConvertProgram;
  19354. end;
  19355. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19356. begin
  19357. StartProgram(false);
  19358. Add('{$modeswitch externalclass}');
  19359. Add('type');
  19360. Add(' TExtA = class external name ''ExtA''');
  19361. Add(' end;');
  19362. Add(' TMyB = class(TExtA)');
  19363. Add(' protected');
  19364. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19365. Add(' end;');
  19366. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19367. Add('begin end;');
  19368. Add('begin');
  19369. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19370. nIncompatibleTypeArgNo);
  19371. ConvertProgram;
  19372. end;
  19373. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19374. begin
  19375. StartProgram(false);
  19376. Add([
  19377. '{$modeswitch externalclass}',
  19378. 'type',
  19379. ' TJSFunction = class external name ''Function''',
  19380. ' end;',
  19381. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19382. ' constructor New(w: word);',
  19383. ' end;',
  19384. ' TBird = class (TExtA)',
  19385. ' public',
  19386. ' Size: word;',
  19387. ' class var Legs: word;',
  19388. ' constructor Create(a: word);',
  19389. ' end;',
  19390. ' TEagle = class (TBird)',
  19391. ' public',
  19392. ' constructor Create(b: word); reintroduce;',
  19393. ' end;',
  19394. 'constructor TBird.Create(a: word);',
  19395. 'begin',
  19396. ' inherited;', // silently ignored
  19397. ' inherited New(a);', // this.$func(a)
  19398. 'end;',
  19399. 'constructor TEagle.Create(b: word);',
  19400. 'begin',
  19401. ' inherited Create(b);',
  19402. 'end;',
  19403. 'var',
  19404. ' Bird: TBird;',
  19405. ' Eagle: TEagle;',
  19406. 'begin',
  19407. ' Bird:=TBird.Create(3);',
  19408. ' Eagle:=TEagle.Create(4);',
  19409. ' Bird.Size:=Bird.Size+5;',
  19410. ' Bird.Legs:=Bird.Legs+6;',
  19411. ' Eagle.Size:=Eagle.Size+5;',
  19412. ' Eagle.Legs:=Eagle.Legs+6;',
  19413. '']);
  19414. ConvertProgram;
  19415. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19416. LinesToStr([ // statements
  19417. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19418. ' this.Legs = 0;',
  19419. ' this.$init = function () {',
  19420. ' this.Size = 0;',
  19421. ' };',
  19422. ' this.$final = function () {',
  19423. ' };',
  19424. ' this.Create = function (a) {',
  19425. ' this.$ancestorfunc(a);',
  19426. ' return this;',
  19427. ' };',
  19428. '});',
  19429. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19430. ' this.Create$1 = function (b) {',
  19431. ' $mod.TBird.Create.call(this, b);',
  19432. ' return this;',
  19433. ' };',
  19434. '});',
  19435. 'this.Bird = null;',
  19436. 'this.Eagle = null;',
  19437. '']),
  19438. LinesToStr([ // $mod.$main
  19439. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19440. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19441. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19442. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19443. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19444. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19445. '']));
  19446. end;
  19447. procedure TTestModule.TestExternalClass_PascalProperty;
  19448. begin
  19449. StartProgram(false);
  19450. Add('{$modeswitch externalclass}');
  19451. Add('type');
  19452. Add(' TJSElement = class;');
  19453. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19454. Add(' TJSElement = class external name ''ExtA''');
  19455. Add(' end;');
  19456. Add(' TControl = class(TJSElement)');
  19457. Add(' private');
  19458. Add(' FOnClick: TJSNotifyEvent;');
  19459. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19460. Add(' procedure Click(Sender: TJSElement);');
  19461. Add(' end;');
  19462. Add('procedure TControl.Click(Sender: TJSElement);');
  19463. Add('begin');
  19464. Add(' OnClick(Self);');
  19465. Add('end;');
  19466. Add('var');
  19467. Add(' Ctrl: TControl;');
  19468. Add('begin');
  19469. Add(' Ctrl.OnClick:[email protected];');
  19470. Add(' Ctrl.OnClick(Ctrl);');
  19471. ConvertProgram;
  19472. CheckSource('TestExternalClass_PascalProperty',
  19473. LinesToStr([ // statements
  19474. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19475. ' this.$init = function () {',
  19476. ' this.FOnClick = null;',
  19477. ' };',
  19478. ' this.$final = function () {',
  19479. ' this.FOnClick = undefined;',
  19480. ' };',
  19481. ' this.Click = function (Sender) {',
  19482. ' this.FOnClick(this);',
  19483. ' };',
  19484. '});',
  19485. 'this.Ctrl = null;',
  19486. '']),
  19487. LinesToStr([ // $mod.$main
  19488. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19489. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19490. '']));
  19491. end;
  19492. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19493. begin
  19494. StartProgram(false);
  19495. Add([
  19496. '{$modeswitch externalclass}',
  19497. 'type',
  19498. ' IUnknown = interface end;',
  19499. ' TObject = class',
  19500. ' end;',
  19501. ' TChild = class',
  19502. ' end;',
  19503. ' TExtRootA = class external name ''ExtRootA''',
  19504. ' end;',
  19505. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19506. ' end;',
  19507. ' TExtRootB = class external name ''ExtRootB''',
  19508. ' end;',
  19509. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19510. ' end;',
  19511. ' TExtString = class external name ''String''',
  19512. ' function charAt(aIndex : NativeInt) : string;',
  19513. ' end;',
  19514. 'var',
  19515. ' Obj: TObject;',
  19516. ' Child: TChild;',
  19517. ' RootA: TExtRootA;',
  19518. ' ChildA: TExtChildA;',
  19519. ' RootB: TExtRootB;',
  19520. ' ChildB: TExtChildB;',
  19521. ' i: IUnknown;',
  19522. ' s: string;',
  19523. ' v: jsvalue;',
  19524. 'begin',
  19525. ' obj:=tobject(roota);',
  19526. ' obj:=tobject(childa);',
  19527. ' child:=tchild(tobject(roota));',
  19528. ' roota:=textroota(obj);',
  19529. ' roota:=textroota(child);',
  19530. ' roota:=textroota(rootb);',
  19531. ' roota:=textroota(childb);',
  19532. ' childa:=textchilda(textroota(obj));',
  19533. ' roota:=TExtRootA(i);',
  19534. ' s:=TExtString(s).charAt(7);',
  19535. ' s:=TExtString(v).charAt(8);',
  19536. '']);
  19537. ConvertProgram;
  19538. CheckSource('TestExternalClass_TypeCastToRootClass',
  19539. LinesToStr([ // statements
  19540. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19541. 'rtl.createClass(this, "TObject", null, function () {',
  19542. ' this.$init = function () {',
  19543. ' };',
  19544. ' this.$final = function () {',
  19545. ' };',
  19546. '});',
  19547. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19548. '});',
  19549. 'this.Obj = null;',
  19550. 'this.Child = null;',
  19551. 'this.RootA = null;',
  19552. 'this.ChildA = null;',
  19553. 'this.RootB = null;',
  19554. 'this.ChildB = null;',
  19555. 'this.i = null;',
  19556. 'this.s = "";',
  19557. 'this.v = undefined;',
  19558. '']),
  19559. LinesToStr([ // $mod.$main
  19560. '$mod.Obj = $mod.RootA;',
  19561. '$mod.Obj = $mod.ChildA;',
  19562. '$mod.Child = $mod.RootA;',
  19563. '$mod.RootA = $mod.Obj;',
  19564. '$mod.RootA = $mod.Child;',
  19565. '$mod.RootA = $mod.RootB;',
  19566. '$mod.RootA = $mod.ChildB;',
  19567. '$mod.ChildA = $mod.Obj;',
  19568. '$mod.RootA = $mod.i;',
  19569. '$mod.s = $mod.s.charAt(7);',
  19570. '$mod.s = $mod.v.charAt(8);',
  19571. '']));
  19572. end;
  19573. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19574. begin
  19575. StartProgram(false);
  19576. Add([
  19577. '{$modeswitch externalclass}',
  19578. 'type',
  19579. ' IUnknown = interface end;',
  19580. ' IBird = interface(IUnknown) end;',
  19581. ' TClass = class of TObject;',
  19582. ' TObject = class',
  19583. ' end;',
  19584. ' TChild = class',
  19585. ' end;',
  19586. ' TJSObject = class external name ''Object''',
  19587. ' end;',
  19588. ' TRec = record end;',
  19589. 'var',
  19590. ' Obj: TObject;',
  19591. ' Child: TChild;',
  19592. ' i: IUnknown;',
  19593. ' Bird: IBird;',
  19594. ' j: TJSObject;',
  19595. ' r: TRec;',
  19596. ' c: TClass;',
  19597. 'begin',
  19598. ' j:=tjsobject(IUnknown);',
  19599. ' j:=tjsobject(IBird);',
  19600. ' j:=tjsobject(TObject);',
  19601. ' j:=tjsobject(TChild);',
  19602. ' j:=tjsobject(TRec);',
  19603. ' j:=tjsobject(Obj);',
  19604. ' j:=tjsobject(Child);',
  19605. ' j:=tjsobject(i);',
  19606. ' j:=tjsobject(Bird);',
  19607. ' j:=tjsobject(r);',
  19608. ' j:=tjsobject(c);',
  19609. '']);
  19610. ConvertProgram;
  19611. CheckSource('TestExternalClass_TypeCastToJSObject',
  19612. LinesToStr([ // statements
  19613. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19614. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19615. 'rtl.createClass(this, "TObject", null, function () {',
  19616. ' this.$init = function () {',
  19617. ' };',
  19618. ' this.$final = function () {',
  19619. ' };',
  19620. '});',
  19621. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19622. '});',
  19623. 'rtl.recNewT(this, "TRec", function () {',
  19624. ' this.$eq = function (b) {',
  19625. ' return true;',
  19626. ' };',
  19627. ' this.$assign = function (s) {',
  19628. ' return this;',
  19629. ' };',
  19630. '});',
  19631. 'this.Obj = null;',
  19632. 'this.Child = null;',
  19633. 'this.i = null;',
  19634. 'this.Bird = null;',
  19635. 'this.j = null;',
  19636. 'this.r = this.TRec.$new();',
  19637. 'this.c = null;',
  19638. '']),
  19639. LinesToStr([ // $mod.$main
  19640. '$mod.j = $mod.IUnknown;',
  19641. '$mod.j = $mod.IBird;',
  19642. '$mod.j = $mod.TObject;',
  19643. '$mod.j = $mod.TChild;',
  19644. '$mod.j = $mod.TRec;',
  19645. '$mod.j = $mod.Obj;',
  19646. '$mod.j = $mod.Child;',
  19647. '$mod.j = $mod.i;',
  19648. '$mod.j = $mod.Bird;',
  19649. '$mod.j = $mod.r;',
  19650. '$mod.j = $mod.c;',
  19651. '']));
  19652. end;
  19653. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19654. begin
  19655. StartProgram(false);
  19656. Add('{$modeswitch externalclass}');
  19657. Add('type');
  19658. Add(' TJSString = class external name ''String''');
  19659. Add(' class function fromCharCode() : string; varargs;');
  19660. Add(' function anchor(const aName : string) : string;');
  19661. Add(' end;');
  19662. Add('var');
  19663. Add(' s: string;');
  19664. Add('begin');
  19665. Add(' s:=TJSString.fromCharCode(65,66);');
  19666. Add(' s:=TJSString(s).anchor(s);');
  19667. Add(' s:=TJSString(''foo'').anchor(s);');
  19668. ConvertProgram;
  19669. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19670. LinesToStr([ // statements
  19671. 'this.s = "";',
  19672. '']),
  19673. LinesToStr([ // $mod.$main
  19674. '$mod.s = String.fromCharCode(65, 66);',
  19675. '$mod.s = $mod.s.anchor($mod.s);',
  19676. '$mod.s = "foo".anchor($mod.s);',
  19677. '']));
  19678. end;
  19679. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19680. begin
  19681. StartProgram(false);
  19682. Add([
  19683. '{$modeswitch externalclass}',
  19684. 'type',
  19685. ' TJSObject = class external name ''Object'' end;',
  19686. ' TJSFunction = class external name ''Function''',
  19687. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19688. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19689. ' end;',
  19690. ' TObject = class',
  19691. ' procedure DoIt(i: longint);',
  19692. ' end;',
  19693. ' TFuncInt = function(o: TObject): longint;',
  19694. 'function GetIt(o: TObject): longint;',
  19695. ' procedure Sub; begin end;',
  19696. 'var',
  19697. ' f: TJSFunction;',
  19698. ' fi: TFuncInt;',
  19699. 'begin',
  19700. ' fi:=TFuncInt(f);',
  19701. ' f:=TJSFunction(fi);',
  19702. ' f:=TJSFunction(@GetIt);',
  19703. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19704. ' f:=TJSFunction(@Sub);',
  19705. ' f:=TJSFunction(@o.doit);',
  19706. ' f:=TJSFunction(fi).bind(nil,4)',
  19707. 'end;',
  19708. 'procedure TObject.DoIt(i: longint);',
  19709. ' procedure Sub; begin end;',
  19710. 'var f: TJSFunction;',
  19711. 'begin',
  19712. ' f:=TJSFunction(@DoIt);',
  19713. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19714. ' f:=TJSFunction(@Sub);',
  19715. ' f:=TJSFunction(@GetIt);',
  19716. 'end;',
  19717. 'begin']);
  19718. ConvertProgram;
  19719. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19720. LinesToStr([ // statements
  19721. 'rtl.createClass(this, "TObject", null, function () {',
  19722. ' this.$init = function () {',
  19723. ' };',
  19724. ' this.$final = function () {',
  19725. ' };',
  19726. ' this.DoIt = function (i) {',
  19727. ' var $Self = this;',
  19728. ' function Sub() {',
  19729. ' };',
  19730. ' var f = null;',
  19731. ' f = this.DoIt;',
  19732. ' f = this.DoIt.bind(null, 13);',
  19733. ' f = Sub;',
  19734. ' f = $mod.GetIt;',
  19735. ' };',
  19736. '});',
  19737. 'this.GetIt = function (o) {',
  19738. ' var Result = 0;',
  19739. ' function Sub() {',
  19740. ' };',
  19741. ' var f = null;',
  19742. ' var fi = null;',
  19743. ' fi = f;',
  19744. ' f = fi;',
  19745. ' f = $mod.GetIt;',
  19746. ' f = $mod.GetIt.bind(null, 3);',
  19747. ' f = Sub;',
  19748. ' f = $mod.TObject.DoIt;',
  19749. ' f = fi.bind(null, 4);',
  19750. ' return Result;',
  19751. '};',
  19752. '']),
  19753. LinesToStr([ // $mod.$main
  19754. '']));
  19755. end;
  19756. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19757. begin
  19758. StartProgram(false);
  19759. Add([
  19760. '{$mode delphi}',
  19761. '{$modeswitch externalclass}',
  19762. 'type',
  19763. ' TJSObject = class external name ''Object'' end;',
  19764. ' TJSWindow = class external name ''Window''(TJSObject)',
  19765. ' procedure Open;',
  19766. ' end;',
  19767. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19768. ' procedure Execute;',
  19769. ' end;',
  19770. 'procedure Fly;',
  19771. 'var',
  19772. ' w: TJSWindow;',
  19773. ' e: TJSEventTarget;',
  19774. 'begin',
  19775. ' w:=TJSWindow(e);',
  19776. ' e:=TJSEventTarget(w);',
  19777. 'end;',
  19778. 'begin']);
  19779. ConvertProgram;
  19780. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19781. LinesToStr([ // statements
  19782. 'this.Fly = function () {',
  19783. ' var w = null;',
  19784. ' var e = null;',
  19785. ' w = e;',
  19786. ' e = w;',
  19787. '};',
  19788. '']),
  19789. LinesToStr([ // $mod.$main
  19790. '']));
  19791. end;
  19792. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19793. begin
  19794. StartProgram(false);
  19795. Add('{$modeswitch externalclass}');
  19796. Add('type');
  19797. Add(' TJSString = class external name ''String''');
  19798. Add(' class function fromCharCode() : string; varargs;');
  19799. Add(' end;');
  19800. Add('var');
  19801. Add(' s: string;');
  19802. Add(' sObj: TJSString;');
  19803. Add('begin');
  19804. Add(' s:=sObj.fromCharCode(65,66);');
  19805. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19806. nExternalClassInstanceCannotAccessStaticX);
  19807. ConvertProgram;
  19808. end;
  19809. procedure TTestModule.TestExternalClass_BracketAccessor;
  19810. begin
  19811. StartProgram(false);
  19812. Add([
  19813. '{$modeswitch externalclass}',
  19814. 'type',
  19815. ' TJSArray = class external name ''Array2''',
  19816. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19817. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19818. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19819. ' end;',
  19820. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19821. 'begin end;',
  19822. 'var',
  19823. ' Arr: tjsarray;',
  19824. ' s: string;',
  19825. ' i: longint;',
  19826. ' v: jsvalue;',
  19827. 'begin',
  19828. ' v:=arr[0];',
  19829. ' v:=arr.items[1];',
  19830. ' arr[2]:=s;',
  19831. ' arr.items[3]:=s;',
  19832. ' arr[4]:=i;',
  19833. ' arr[5]:=arr[6];',
  19834. ' arr.items[7]:=arr.items[8];',
  19835. ' with arr do items[9]:=items[10];',
  19836. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19837. ' with arr do begin',
  19838. ' v:=GetItems(14);',
  19839. ' setitems(15,16);',
  19840. ' end;',
  19841. ' v:=test1.arr.items[17];',
  19842. ' test1.arr.items[18]:=v;',
  19843. '']);
  19844. ConvertProgram;
  19845. CheckSource('TestExternalClass_BracketAccessor',
  19846. LinesToStr([ // statements
  19847. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19848. '};',
  19849. 'this.Arr = null;',
  19850. 'this.s = "";',
  19851. 'this.i = 0;',
  19852. 'this.v = undefined;',
  19853. '']),
  19854. LinesToStr([ // $mod.$main
  19855. '$mod.v = $mod.Arr[0];',
  19856. '$mod.v = $mod.Arr[1];',
  19857. '$mod.Arr[2] = $mod.s;',
  19858. '$mod.Arr[3] = $mod.s;',
  19859. '$mod.Arr[4] = $mod.i;',
  19860. '$mod.Arr[5] = $mod.Arr[6];',
  19861. '$mod.Arr[7] = $mod.Arr[8];',
  19862. 'var $with = $mod.Arr;',
  19863. '$with[9] = $with[10];',
  19864. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19865. ' a: 9,',
  19866. ' p: $mod.Arr,',
  19867. ' get: function () {',
  19868. ' return this.p[this.a];',
  19869. ' },',
  19870. ' set: function (v) {',
  19871. ' this.p[this.a] = v;',
  19872. ' }',
  19873. '}, {',
  19874. ' a: 10,',
  19875. ' p: $mod.Arr,',
  19876. ' get: function () {',
  19877. ' return this.p[this.a];',
  19878. ' },',
  19879. ' set: function (v) {',
  19880. ' this.p[this.a] = v;',
  19881. ' }',
  19882. '});',
  19883. 'var $with1 = $mod.Arr;',
  19884. '$mod.v = $with1[14];',
  19885. '$with1[15] = 16;',
  19886. '$mod.v = $mod.Arr[17];',
  19887. '$mod.Arr[18] = $mod.v;',
  19888. '']));
  19889. end;
  19890. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19891. begin
  19892. StartProgram(false);
  19893. Add([
  19894. '{$modeswitch externalclass}',
  19895. 'type',
  19896. ' TJSArray = class external name ''Array2''',
  19897. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19898. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19899. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19900. ' end;',
  19901. ' TMyArr = class(TJSArray)',
  19902. ' procedure DoIt;',
  19903. ' end;',
  19904. 'procedure tmyarr.DoIt;',
  19905. 'begin',
  19906. ' Items[1]:=Items[2];',
  19907. ' SetItems(3,getItems(4));',
  19908. 'end;',
  19909. 'var',
  19910. ' Arr: tmyarr;',
  19911. ' s: string;',
  19912. ' i: longint;',
  19913. ' v: jsvalue;',
  19914. 'begin',
  19915. ' v:=arr[0];',
  19916. ' v:=arr.items[1];',
  19917. ' arr[2]:=s;',
  19918. ' arr.items[3]:=s;',
  19919. ' arr[4]:=i;',
  19920. ' arr[5]:=arr[6];',
  19921. ' arr.items[7]:=arr.items[8];',
  19922. ' with arr do items[9]:=items[10];',
  19923. ' with arr do begin',
  19924. ' v:=GetItems(14);',
  19925. ' setitems(15,16);',
  19926. ' end;',
  19927. '']);
  19928. ConvertProgram;
  19929. CheckSource('TestExternalClass_BracketAccessor_Call',
  19930. LinesToStr([ // statements
  19931. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19932. ' this.$init = function () {',
  19933. ' };',
  19934. ' this.$final = function () {',
  19935. ' };',
  19936. ' this.DoIt = function () {',
  19937. ' this[1] = this[2];',
  19938. ' this[3] = this[4];',
  19939. ' };',
  19940. '});',
  19941. 'this.Arr = null;',
  19942. 'this.s = "";',
  19943. 'this.i = 0;',
  19944. 'this.v = undefined;',
  19945. '']),
  19946. LinesToStr([ // $mod.$main
  19947. '$mod.v = $mod.Arr[0];',
  19948. '$mod.v = $mod.Arr[1];',
  19949. '$mod.Arr[2] = $mod.s;',
  19950. '$mod.Arr[3] = $mod.s;',
  19951. '$mod.Arr[4] = $mod.i;',
  19952. '$mod.Arr[5] = $mod.Arr[6];',
  19953. '$mod.Arr[7] = $mod.Arr[8];',
  19954. 'var $with = $mod.Arr;',
  19955. '$with[9] = $with[10];',
  19956. 'var $with1 = $mod.Arr;',
  19957. '$mod.v = $with1[14];',
  19958. '$with1[15] = 16;',
  19959. '']));
  19960. end;
  19961. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19962. begin
  19963. StartProgram(false);
  19964. Add('{$modeswitch externalclass}');
  19965. Add('type');
  19966. Add(' TJSArray = class external name ''Array2''');
  19967. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19968. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19969. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19970. Add(' end;');
  19971. Add('begin');
  19972. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19973. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19974. ConvertProgram;
  19975. end;
  19976. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19977. begin
  19978. StartProgram(false);
  19979. Add('{$modeswitch externalclass}');
  19980. Add('type');
  19981. Add(' TJSArray = class external name ''Array2''');
  19982. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19983. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19984. Add(' end;');
  19985. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19986. Add('begin end;');
  19987. Add('var');
  19988. Add(' Arr: tjsarray;');
  19989. Add(' v: jsvalue;');
  19990. Add('begin');
  19991. Add(' v:=arr[0];');
  19992. Add(' v:=arr.items[1];');
  19993. Add(' with arr do v:=items[2];');
  19994. Add(' doit(arr[3],arr[4]);');
  19995. ConvertProgram;
  19996. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19997. LinesToStr([ // statements
  19998. 'this.DoIt = function (vI, vJ) {',
  19999. '};',
  20000. 'this.Arr = null;',
  20001. 'this.v = undefined;',
  20002. '']),
  20003. LinesToStr([ // $mod.$main
  20004. '$mod.v = $mod.Arr[0];',
  20005. '$mod.v = $mod.Arr[1];',
  20006. 'var $with = $mod.Arr;',
  20007. '$mod.v = $with[2];',
  20008. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20009. '']));
  20010. end;
  20011. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20012. begin
  20013. StartProgram(false);
  20014. Add('{$modeswitch externalclass}');
  20015. Add('type');
  20016. Add(' TJSArray = class external name ''Array2''');
  20017. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20018. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20019. Add(' end;');
  20020. Add('var');
  20021. Add(' Arr: tjsarray;');
  20022. Add(' s: string;');
  20023. Add(' i: longint;');
  20024. Add(' v: jsvalue;');
  20025. Add('begin');
  20026. Add(' arr[2]:=s;');
  20027. Add(' arr.items[3]:=s;');
  20028. Add(' arr[4]:=i;');
  20029. Add(' with arr do items[5]:=i;');
  20030. ConvertProgram;
  20031. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20032. LinesToStr([ // statements
  20033. 'this.Arr = null;',
  20034. 'this.s = "";',
  20035. 'this.i = 0;',
  20036. 'this.v = undefined;',
  20037. '']),
  20038. LinesToStr([ // $mod.$main
  20039. '$mod.Arr[2] = $mod.s;',
  20040. '$mod.Arr[3] = $mod.s;',
  20041. '$mod.Arr[4] = $mod.i;',
  20042. 'var $with = $mod.Arr;',
  20043. '$with[5] = $mod.i;',
  20044. '']));
  20045. end;
  20046. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20047. begin
  20048. StartProgram(false);
  20049. Add('{$modeswitch externalclass}');
  20050. Add('type');
  20051. Add(' TJSArray = class external name ''Array2''');
  20052. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20053. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20054. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20055. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20056. Add(' end;');
  20057. Add('var');
  20058. Add(' Arr: tjsarray;');
  20059. Add(' s: string;');
  20060. Add(' i: longint;');
  20061. Add(' v: jsvalue;');
  20062. Add('begin');
  20063. Add(' arr[2]:=s;');
  20064. Add(' arr.items[3]:=s;');
  20065. Add(' arr.numbers[4]:=i;');
  20066. Add(' with arr do items[5]:=i;');
  20067. Add(' with arr do numbers[6]:=i;');
  20068. ConvertProgram;
  20069. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20070. LinesToStr([ // statements
  20071. 'this.Arr = null;',
  20072. 'this.s = "";',
  20073. 'this.i = 0;',
  20074. 'this.v = undefined;',
  20075. '']),
  20076. LinesToStr([ // $mod.$main
  20077. '$mod.Arr[2] = $mod.s;',
  20078. '$mod.Arr[3] = $mod.s;',
  20079. '$mod.Arr[4] = $mod.i;',
  20080. 'var $with = $mod.Arr;',
  20081. '$with[5] = $mod.i;',
  20082. 'var $with1 = $mod.Arr;',
  20083. '$with1[6] = $mod.i;',
  20084. '']));
  20085. end;
  20086. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20087. begin
  20088. StartProgram(false);
  20089. Add('{$modeswitch externalclass}');
  20090. Add('type');
  20091. Add(' TJSArray = class external name ''Array2''');
  20092. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20093. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20094. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20095. Add(' end;');
  20096. Add('var');
  20097. Add(' Arr: tjsarray;');
  20098. Add(' i: longint;');
  20099. Add(' IntArr: array of longint;');
  20100. Add(' v: jsvalue;');
  20101. Add('begin');
  20102. Add(' v:=arr.items[i];');
  20103. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20104. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20105. ConvertProgram;
  20106. CheckSource('TestExternalClass_BracketAccessor_Index',
  20107. LinesToStr([ // statements
  20108. 'this.Arr = null;',
  20109. 'this.i = 0;',
  20110. 'this.IntArr = [];',
  20111. 'this.v = undefined;',
  20112. '']),
  20113. LinesToStr([ // $mod.$main
  20114. '$mod.v = $mod.Arr[$mod.i];',
  20115. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20116. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20117. '']));
  20118. end;
  20119. procedure TTestModule.TestExternalClass_ForInJSObject;
  20120. begin
  20121. StartProgram(false);
  20122. Add([
  20123. '{$modeswitch externalclass}',
  20124. 'type',
  20125. ' TJSObject = class external name ''Object''',
  20126. ' end;',
  20127. 'var',
  20128. ' o: TJSObject;',
  20129. ' key: string;',
  20130. 'begin',
  20131. ' for key in o do',
  20132. ' if key=''abc'' then ;',
  20133. '']);
  20134. ConvertProgram;
  20135. CheckSource('TestExternalClass_ForInJSObject',
  20136. LinesToStr([ // statements
  20137. 'this.o = null;',
  20138. 'this.key = "";',
  20139. '']),
  20140. LinesToStr([ // $mod.$main
  20141. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20142. '']));
  20143. end;
  20144. procedure TTestModule.TestExternalClass_ForInJSArray;
  20145. begin
  20146. StartProgram(false);
  20147. Add([
  20148. '{$modeswitch externalclass}',
  20149. 'type',
  20150. ' TJSInt8Array = class external name ''Int8Array''',
  20151. ' private',
  20152. ' flength: NativeInt external name ''length'';',
  20153. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20154. ' public',
  20155. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20156. ' property Length: NativeInt read flength;',
  20157. ' end;',
  20158. 'var',
  20159. ' a: TJSInt8Array;',
  20160. ' value: shortint;',
  20161. 'begin',
  20162. ' for value in a do',
  20163. ' if value=3 then ;',
  20164. '']);
  20165. ConvertProgram;
  20166. CheckSource('TestExternalClass_ForInJSArray',
  20167. LinesToStr([ // statements
  20168. 'this.a = null;',
  20169. 'this.value = 0;',
  20170. '']),
  20171. LinesToStr([ // $mod.$main
  20172. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20173. ' $mod.value = $in[$l];',
  20174. ' if ($mod.value === 3) ;',
  20175. '};',
  20176. '']));
  20177. end;
  20178. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20179. begin
  20180. AddModuleWithIntfImplSrc('unit2.pas',
  20181. LinesToStr([
  20182. '{$modeswitch externalclass}',
  20183. 'type',
  20184. ' TJSBufferSource = class external name ''BufferSource''',
  20185. ' end;',
  20186. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20187. '']),
  20188. '');
  20189. AddModuleWithIntfImplSrc('unit3.pas',
  20190. LinesToStr([
  20191. '{$modeswitch externalclass}',
  20192. 'type',
  20193. ' TJSBufferSource = class external name ''BufferSource''',
  20194. ' end;',
  20195. '']),
  20196. '');
  20197. StartUnit(true);
  20198. Add([
  20199. 'interface',
  20200. 'uses unit2, unit3;',
  20201. 'procedure DoSome(s: TJSBufferSource);',
  20202. 'implementation',
  20203. 'procedure DoSome(s: TJSBufferSource);',
  20204. 'begin',
  20205. ' DoIt(s);',
  20206. 'end;',
  20207. '']);
  20208. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20209. nIncompatibleTypeArgNo);
  20210. ConvertUnit;
  20211. end;
  20212. procedure TTestModule.TestExternalClass_NestedConstructor;
  20213. begin
  20214. StartProgram(false);
  20215. Add([
  20216. '{$modeswitch externalclass}',
  20217. 'type',
  20218. ' TJSObject = class external name ''Object''',
  20219. ' type TBird = class external name ''Bird''',
  20220. ' type TWing = class external name ''Wing''',
  20221. ' constructor New;',
  20222. ' constructor Create(w: word = 3);',
  20223. ' end;',
  20224. ' end;',
  20225. ' end;',
  20226. 'var',
  20227. ' w: TJSObject.TBird.TWing;',
  20228. 'begin',
  20229. ' w:=tjsobject.tbird.twing.new;',
  20230. ' w:=tjsobject.tbird.twing.new();',
  20231. ' w:=tjsobject.tbird.twing.create;',
  20232. ' w:=tjsobject.tbird.twing.create(4);',
  20233. ' with tjsobject do begin',
  20234. ' w:=tbird.twing.new;',
  20235. ' w:=tbird.twing.new();',
  20236. ' w:=tbird.twing.create;',
  20237. ' w:=tbird.twing.create(11);',
  20238. ' end;',
  20239. ' with tjsobject.tbird do begin',
  20240. ' w:=twing.new;',
  20241. ' w:=twing.new();',
  20242. ' w:=twing.create;',
  20243. ' w:=twing.create(21);',
  20244. ' end;',
  20245. ' with tjsobject.tbird.twing do begin',
  20246. ' w:=new;',
  20247. ' w:=new();',
  20248. ' w:=create;',
  20249. ' w:=create(31);',
  20250. ' end;',
  20251. '']);
  20252. ConvertProgram;
  20253. CheckSource('TestExternalClass_NestedConstructor',
  20254. LinesToStr([ // statements
  20255. 'this.w = null;',
  20256. '']),
  20257. LinesToStr([ // $mod.$main
  20258. '$mod.w = new Object.Bird.Wing();',
  20259. '$mod.w = new Object.Bird.Wing();',
  20260. '$mod.w = new Object.Bird.Wing.Create();',
  20261. '$mod.w = new Object.Bird.Wing.Create(4);',
  20262. '$mod.w = new Object.Bird.Wing();',
  20263. '$mod.w = new Object.Bird.Wing();',
  20264. '$mod.w = new Object.Bird.Wing.Create();',
  20265. '$mod.w = new Object.Bird.Wing.Create(11);',
  20266. 'var $with = Object.Bird;',
  20267. '$mod.w = new Object.Bird.Wing();',
  20268. '$mod.w = new Object.Bird.Wing();',
  20269. '$mod.w = new Object.Bird.Wing.Create();',
  20270. '$mod.w = new Object.Bird.Wing.Create(21);',
  20271. 'var $with1 = Object.Bird.Wing;',
  20272. '$mod.w = new $with1();',
  20273. '$mod.w = new $with1();',
  20274. '$mod.w = new Object.Bird.Wing.Create();',
  20275. '$mod.w = new Object.Bird.Wing.Create(31);',
  20276. '']));
  20277. end;
  20278. procedure TTestModule.TestClassInterface_Corba;
  20279. begin
  20280. StartProgram(false);
  20281. Add([
  20282. '{$interfaces corba}',
  20283. 'type',
  20284. ' IUnknown = interface;',
  20285. ' IUnknown = interface',
  20286. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20287. ' end;',
  20288. ' IInterface = IUnknown;',
  20289. ' IBird = interface(IInterface)',
  20290. ' function GetSize: longint;',
  20291. ' procedure SetSize(i: longint);',
  20292. ' property Size: longint read GetSize write SetSize;',
  20293. ' procedure DoIt(i: longint);',
  20294. ' end;',
  20295. ' TObject = class',
  20296. ' end;',
  20297. ' TBird = class(TObject,IBird)',
  20298. ' function GetSize: longint; virtual; abstract;',
  20299. ' procedure SetSize(i: longint); virtual; abstract;',
  20300. ' procedure DoIt(i: longint); virtual; abstract;',
  20301. ' end;',
  20302. 'var',
  20303. ' BirdIntf: IBird;',
  20304. 'begin',
  20305. ' BirdIntf.Size:=BirdIntf.Size;',
  20306. '']);
  20307. ConvertProgram;
  20308. CheckSource('TestClassInterface_Corba',
  20309. LinesToStr([ // statements
  20310. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20311. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20312. 'rtl.createClass(this, "TObject", null, function () {',
  20313. ' this.$init = function () {',
  20314. ' };',
  20315. ' this.$final = function () {',
  20316. ' };',
  20317. '});',
  20318. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20319. ' rtl.addIntf(this, $mod.IBird);',
  20320. '});',
  20321. 'this.BirdIntf = null;',
  20322. '']),
  20323. LinesToStr([ // $mod.$main
  20324. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20325. '']));
  20326. end;
  20327. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20328. begin
  20329. StartProgram(false);
  20330. Add([
  20331. '{$interfaces corba}',
  20332. 'type',
  20333. ' IUnknown = interface',
  20334. ' procedure DoIt; external name ''foo'';',
  20335. ' end;',
  20336. 'begin']);
  20337. SetExpectedParserError(
  20338. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20339. nParserNoFieldsAllowed);
  20340. ConvertProgram;
  20341. end;
  20342. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20343. begin
  20344. StartProgram(false);
  20345. Add([
  20346. '{$interfaces corba}',
  20347. 'type',
  20348. ' integer = longint;',
  20349. ' IUnknown = interface',
  20350. ' procedure DoIt(i: integer);',
  20351. ' procedure DoIt(s: string);',
  20352. ' end;',
  20353. ' IBird = interface(IUnknown)',
  20354. ' procedure DoIt(b: boolean); overload;',
  20355. ' end;',
  20356. ' TObject = class',
  20357. ' end;',
  20358. ' TBird = class(TObject,IBird)',
  20359. ' procedure DoIt(o: TObject);',
  20360. ' procedure DoIt(s: string);',
  20361. ' procedure DoIt(i: integer);',
  20362. ' procedure DoIt(b: boolean);',
  20363. ' end;',
  20364. 'procedure TBird.DoIt(o: TObject); begin end;',
  20365. 'procedure TBird.DoIt(s: string); begin end;',
  20366. 'procedure TBird.DoIt(i: integer); begin end;',
  20367. 'procedure TBird.DoIt(b: boolean); begin end;',
  20368. 'var',
  20369. ' BirdIntf: IBird;',
  20370. 'begin',
  20371. ' BirdIntf.DoIt(3);',
  20372. ' BirdIntf.DoIt(''abc'');',
  20373. ' BirdIntf.DoIt(true);',
  20374. '']);
  20375. ConvertProgram;
  20376. CheckSource('TestClassInterface_Overloads',
  20377. LinesToStr([ // statements
  20378. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20379. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20380. 'rtl.createClass(this, "TObject", null, function () {',
  20381. ' this.$init = function () {',
  20382. ' };',
  20383. ' this.$final = function () {',
  20384. ' };',
  20385. '});',
  20386. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20387. ' this.DoIt = function (o) {',
  20388. ' };',
  20389. ' this.DoIt$1 = function (s) {',
  20390. ' };',
  20391. ' this.DoIt$2 = function (i) {',
  20392. ' };',
  20393. ' this.DoIt$3 = function (b) {',
  20394. ' };',
  20395. ' rtl.addIntf(this, $mod.IBird, {',
  20396. ' DoIt$2: "DoIt$3",',
  20397. ' DoIt: "DoIt$2"',
  20398. ' });',
  20399. '});',
  20400. 'this.BirdIntf = null;',
  20401. '']),
  20402. LinesToStr([ // $mod.$main
  20403. '$mod.BirdIntf.DoIt(3);',
  20404. '$mod.BirdIntf.DoIt$1("abc");',
  20405. '$mod.BirdIntf.DoIt$2(true);',
  20406. '']));
  20407. end;
  20408. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20409. begin
  20410. StartProgram(false);
  20411. Add([
  20412. '{$interfaces corba}',
  20413. 'type',
  20414. ' IBird = interface',
  20415. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20416. ' end;',
  20417. ' IDog = interface',
  20418. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20419. ' end;',
  20420. ' TObject = class(IBird,IDog)',
  20421. ' end;',
  20422. 'begin']);
  20423. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20424. nDuplicateGUIDXInYZ);
  20425. ConvertProgram;
  20426. end;
  20427. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20428. begin
  20429. StartProgram(false);
  20430. Add([
  20431. '{$interfaces corba}',
  20432. 'type',
  20433. ' IAnimal = interface',
  20434. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20435. ' end;',
  20436. ' IBird = interface(IAnimal)',
  20437. ' end;',
  20438. ' IHawk = interface(IBird)',
  20439. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20440. ' end;',
  20441. 'begin']);
  20442. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20443. nDuplicateGUIDXInYZ);
  20444. ConvertProgram;
  20445. end;
  20446. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20447. begin
  20448. StartProgram(false);
  20449. Add([
  20450. '{$interfaces corba}',
  20451. 'type',
  20452. ' integer = longint;',
  20453. ' IUnknown = interface',
  20454. ' procedure DoIt(i: integer);',
  20455. ' end;',
  20456. ' IBird = interface',
  20457. ' procedure Fly(i: integer);',
  20458. ' end;',
  20459. ' TObject = class(IUnknown)',
  20460. ' procedure DoIt(i: integer);',
  20461. ' end;',
  20462. ' TBird = class(IBird)',
  20463. ' procedure Fly(i: integer);',
  20464. ' end;',
  20465. 'procedure TObject.DoIt(i: integer); begin end;',
  20466. 'procedure TBird.Fly(i: integer); begin end;',
  20467. 'begin',
  20468. '']);
  20469. ConvertProgram;
  20470. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20471. LinesToStr([ // statements
  20472. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20473. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20474. 'rtl.createClass(this, "TObject", null, function () {',
  20475. ' this.$init = function () {',
  20476. ' };',
  20477. ' this.$final = function () {',
  20478. ' };',
  20479. ' this.DoIt = function (i) {',
  20480. ' };',
  20481. ' rtl.addIntf(this, $mod.IUnknown);',
  20482. '});',
  20483. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20484. ' this.Fly = function (i) {',
  20485. ' };',
  20486. ' rtl.addIntf(this, $mod.IBird);',
  20487. ' rtl.addIntf(this, $mod.IUnknown);',
  20488. '});',
  20489. '']),
  20490. LinesToStr([ // $mod.$main
  20491. '']));
  20492. end;
  20493. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20494. begin
  20495. StartProgram(false);
  20496. Add([
  20497. '{$interfaces corba}',
  20498. 'type',
  20499. ' integer = longint;',
  20500. ' IBird = interface',
  20501. ' procedure DoIt(i: integer);',
  20502. ' end;',
  20503. ' TObject = class',
  20504. ' procedure DoIt(i: integer);',
  20505. ' end;',
  20506. ' TBird = class(IBird)',
  20507. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20508. ' end;',
  20509. 'procedure TObject.DoIt(i: integer); begin end;',
  20510. 'procedure TBird.DoIt(i: integer); begin end;',
  20511. 'begin',
  20512. '']);
  20513. ConvertProgram;
  20514. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20515. LinesToStr([ // statements
  20516. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20517. 'rtl.createClass(this, "TObject", null, function () {',
  20518. ' this.$init = function () {',
  20519. ' };',
  20520. ' this.$final = function () {',
  20521. ' };',
  20522. ' this.DoIt = function (i) {',
  20523. ' };',
  20524. '});',
  20525. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20526. ' this.DoIt$1 = function (i) {',
  20527. ' };',
  20528. ' rtl.addIntf(this, $mod.IBird, {',
  20529. ' DoIt: "DoIt$1"',
  20530. ' });',
  20531. '});',
  20532. '']),
  20533. LinesToStr([ // $mod.$main
  20534. '']));
  20535. end;
  20536. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20537. begin
  20538. StartProgram(false);
  20539. Add([
  20540. '{$interfaces corba}',
  20541. 'type',
  20542. ' IUnknown = interface',
  20543. ' procedure Walk(i: longint);',
  20544. ' end;',
  20545. ' IBird = interface(IUnknown)',
  20546. ' procedure Walk(b: boolean); overload;',
  20547. ' procedure Fly(s: string);',
  20548. ' end;',
  20549. ' TObject = class',
  20550. ' end;',
  20551. ' TBird = class(TObject,IBird)',
  20552. ' procedure IBird.Fly = Move;',
  20553. ' procedure IBird.Walk = Hop;',
  20554. ' procedure Hop(i: longint);',
  20555. ' procedure Move(s: string);',
  20556. ' procedure Hop(b: boolean);',
  20557. ' end;',
  20558. 'procedure TBird.Move(s: string); begin end;',
  20559. 'procedure TBird.Hop(i: longint); begin end;',
  20560. 'procedure TBird.Hop(b: boolean); begin end;',
  20561. 'var',
  20562. ' BirdIntf: IBird;',
  20563. 'begin',
  20564. ' BirdIntf.Walk(3);',
  20565. ' BirdIntf.Walk(true);',
  20566. ' BirdIntf.Fly(''abc'');',
  20567. '']);
  20568. ConvertProgram;
  20569. CheckSource('TestClassInterface_Corba_MethodResolution',
  20570. LinesToStr([ // statements
  20571. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20572. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20573. 'rtl.createClass(this, "TObject", null, function () {',
  20574. ' this.$init = function () {',
  20575. ' };',
  20576. ' this.$final = function () {',
  20577. ' };',
  20578. '});',
  20579. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20580. ' this.Hop = function (i) {',
  20581. ' };',
  20582. ' this.Move = function (s) {',
  20583. ' };',
  20584. ' this.Hop$1 = function (b) {',
  20585. ' };',
  20586. ' rtl.addIntf(this, $mod.IBird, {',
  20587. ' Walk$1: "Hop$1",',
  20588. ' Fly: "Move",',
  20589. ' Walk: "Hop"',
  20590. ' });',
  20591. '});',
  20592. 'this.BirdIntf = null;',
  20593. '']),
  20594. LinesToStr([ // $mod.$main
  20595. '$mod.BirdIntf.Walk(3);',
  20596. '$mod.BirdIntf.Walk$1(true);',
  20597. '$mod.BirdIntf.Fly("abc");',
  20598. '']));
  20599. end;
  20600. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20601. begin
  20602. StartProgram(false);
  20603. Add([
  20604. '{$interfaces com}',
  20605. 'type',
  20606. ' IUnknown = interface',
  20607. ' function _AddRef: longint;',
  20608. ' procedure Walk;',
  20609. ' end;',
  20610. ' IBird = interface end;',
  20611. ' IDog = interface end;',
  20612. ' TObject = class(IBird,IDog)',
  20613. ' function _AddRef: longint; virtual; abstract;',
  20614. ' procedure Walk; virtual; abstract;',
  20615. ' end;',
  20616. ' TBird = class(IUnknown)',
  20617. ' end;',
  20618. 'begin',
  20619. '']);
  20620. ConvertProgram;
  20621. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20622. LinesToStr([ // statements
  20623. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20624. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20625. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20626. 'rtl.createClass(this, "TObject", null, function () {',
  20627. ' this.$init = function () {',
  20628. ' };',
  20629. ' this.$final = function () {',
  20630. ' };',
  20631. ' rtl.addIntf(this, $mod.IBird);',
  20632. ' rtl.addIntf(this, $mod.IDog);',
  20633. '});',
  20634. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20635. ' rtl.addIntf(this, $mod.IUnknown);',
  20636. ' rtl.addIntf(this, $mod.IBird);',
  20637. ' rtl.addIntf(this, $mod.IDog);',
  20638. '});',
  20639. '']),
  20640. LinesToStr([ // $mod.$main
  20641. '']));
  20642. end;
  20643. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20644. begin
  20645. StartProgram(false);
  20646. Add([
  20647. '{$interfaces corba}',
  20648. 'type',
  20649. ' IUnknown = interface',
  20650. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20651. ' procedure Go;',
  20652. ' end;',
  20653. ' TObject = class(IUnknown)',
  20654. ' procedure Go; virtual; abstract;',
  20655. ' end;',
  20656. ' TBird = class',
  20657. ' procedure Go; override;',
  20658. ' end;',
  20659. ' TCat = class(TObject)',
  20660. ' procedure Go; override;',
  20661. ' end;',
  20662. ' TDog = class(TObject, IUnknown)',
  20663. ' procedure Go; override;',
  20664. ' end;',
  20665. 'procedure TBird.Go; begin end;',
  20666. 'procedure TCat.Go; begin end;',
  20667. 'procedure TDog.Go; begin end;',
  20668. 'begin',
  20669. '']);
  20670. ConvertProgram;
  20671. CheckSource('TestClassInterface_Corba_MethodOverride',
  20672. LinesToStr([ // statements
  20673. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20674. 'rtl.createClass(this, "TObject", null, function () {',
  20675. ' this.$init = function () {',
  20676. ' };',
  20677. ' this.$final = function () {',
  20678. ' };',
  20679. ' rtl.addIntf(this, $mod.IUnknown);',
  20680. '});',
  20681. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20682. ' this.Go = function () {',
  20683. ' };',
  20684. ' rtl.addIntf(this, $mod.IUnknown);',
  20685. '});',
  20686. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20687. ' this.Go = function () {',
  20688. ' };',
  20689. ' rtl.addIntf(this, $mod.IUnknown);',
  20690. '});',
  20691. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20692. ' this.Go = function () {',
  20693. ' };',
  20694. ' rtl.addIntf(this, $mod.IUnknown);',
  20695. '});',
  20696. '']),
  20697. LinesToStr([ // $mod.$main
  20698. '']));
  20699. end;
  20700. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20701. begin
  20702. StartProgram(false);
  20703. Add([
  20704. '{$interfaces corba}',
  20705. 'type',
  20706. ' IUnknown = interface',
  20707. ' end;',
  20708. ' IBird = interface(IUnknown)',
  20709. ' procedure Fly(s: string);',
  20710. ' end;',
  20711. ' IEagle = interface(IBird)',
  20712. ' end;',
  20713. ' IDove = interface(IBird)',
  20714. ' end;',
  20715. ' ISwallow = interface(IBird)',
  20716. ' end;',
  20717. ' TObject = class',
  20718. ' end;',
  20719. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20720. ' procedure Fly(s: string); virtual; abstract;',
  20721. ' end;',
  20722. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20723. ' FBirdIntf: IBird;',
  20724. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20725. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20726. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20727. ' FDoveObj: TBird;',
  20728. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20729. ' function GetSwallowObj: TBird; virtual; abstract;',
  20730. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20731. ' end;',
  20732. 'begin',
  20733. '']);
  20734. ConvertProgram;
  20735. CheckSource('TestClassInterface_Corba_Delegation',
  20736. LinesToStr([ // statements
  20737. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20738. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20739. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20740. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20741. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20742. 'rtl.createClass(this, "TObject", null, function () {',
  20743. ' this.$init = function () {',
  20744. ' };',
  20745. ' this.$final = function () {',
  20746. ' };',
  20747. '});',
  20748. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20749. ' rtl.addIntf(this, $mod.IBird);',
  20750. ' rtl.addIntf(this, $mod.IEagle);',
  20751. ' rtl.addIntf(this, $mod.IDove);',
  20752. ' rtl.addIntf(this, $mod.ISwallow);',
  20753. '});',
  20754. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20755. ' this.$init = function () {',
  20756. ' $mod.TObject.$init.call(this);',
  20757. ' this.FBirdIntf = null;',
  20758. ' this.FDoveObj = null;',
  20759. ' };',
  20760. ' this.$final = function () {',
  20761. ' this.FBirdIntf = undefined;',
  20762. ' this.FDoveObj = undefined;',
  20763. ' $mod.TObject.$final.call(this);',
  20764. ' };',
  20765. ' this.$intfmaps = {',
  20766. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20767. ' return this.FBirdIntf;',
  20768. ' },',
  20769. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20770. ' return this.GetEagleIntf();',
  20771. ' },',
  20772. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20773. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20774. ' },',
  20775. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20776. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20777. ' }',
  20778. ' };',
  20779. '});',
  20780. '']),
  20781. LinesToStr([ // $mod.$main
  20782. '']));
  20783. end;
  20784. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20785. begin
  20786. StartProgram(false);
  20787. Add([
  20788. '{$interfaces corba}',
  20789. 'type',
  20790. ' IUnknown = interface',
  20791. ' end;',
  20792. ' IBird = interface(IUnknown)',
  20793. ' procedure Fly(s: string);',
  20794. ' end;',
  20795. ' IEagle = interface(IBird)',
  20796. ' end;',
  20797. ' IDove = interface(IBird)',
  20798. ' end;',
  20799. ' ISwallow = interface(IBird)',
  20800. ' end;',
  20801. ' TObject = class',
  20802. ' end;',
  20803. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20804. ' procedure Fly(s: string); virtual; abstract;',
  20805. ' end;',
  20806. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20807. ' private',
  20808. ' class var FBirdIntf: IBird;',
  20809. ' class var FDoveObj: TBird;',
  20810. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20811. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20812. ' protected',
  20813. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20814. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20815. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20816. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20817. ' end;',
  20818. 'begin',
  20819. '']);
  20820. ConvertProgram;
  20821. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20822. LinesToStr([ // statements
  20823. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20824. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20825. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20826. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20827. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20828. 'rtl.createClass(this, "TObject", null, function () {',
  20829. ' this.$init = function () {',
  20830. ' };',
  20831. ' this.$final = function () {',
  20832. ' };',
  20833. '});',
  20834. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20835. ' rtl.addIntf(this, $mod.IBird);',
  20836. ' rtl.addIntf(this, $mod.IEagle);',
  20837. ' rtl.addIntf(this, $mod.IDove);',
  20838. ' rtl.addIntf(this, $mod.ISwallow);',
  20839. '});',
  20840. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20841. ' this.FBirdIntf = null;',
  20842. ' this.FDoveObj = null;',
  20843. ' this.$intfmaps = {',
  20844. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20845. ' return this.FBirdIntf;',
  20846. ' },',
  20847. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20848. ' return this.GetEagleIntf();',
  20849. ' },',
  20850. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20851. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20852. ' },',
  20853. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20854. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20855. ' }',
  20856. ' };',
  20857. '});',
  20858. '']),
  20859. LinesToStr([ // $mod.$main
  20860. '']));
  20861. end;
  20862. procedure TTestModule.TestClassInterface_Corba_Operators;
  20863. begin
  20864. StartProgram(false);
  20865. Add([
  20866. '{$interfaces corba}',
  20867. 'type',
  20868. ' IUnknown = interface',
  20869. ' end;',
  20870. ' IBird = interface(IUnknown)',
  20871. ' function GetItems(Index: longint): longint;',
  20872. ' procedure SetItems(Index: longint; Value: longint);',
  20873. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20874. ' end;',
  20875. ' TObject = class',
  20876. ' end;',
  20877. ' TBird = class(TObject,IBird)',
  20878. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20879. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20880. ' end;',
  20881. 'var',
  20882. ' IntfVar: IBird = nil;',
  20883. ' IntfVar2: IBird;',
  20884. ' ObjVar: TBird;',
  20885. ' v: JSValue;',
  20886. 'begin',
  20887. ' IntfVar:=nil;',
  20888. ' IntfVar[3]:=IntfVar[4];',
  20889. ' if Assigned(IntfVar) then ;',
  20890. ' IntfVar:=IntfVar2;',
  20891. ' IntfVar:=ObjVar;',
  20892. ' if IntfVar=IntfVar2 then ;',
  20893. ' if IntfVar<>IntfVar2 then ;',
  20894. ' if IntfVar is IBird then ;',
  20895. ' if IntfVar is TBird then ;',
  20896. ' if ObjVar is IBird then ;',
  20897. ' IntfVar:=IntfVar2 as IBird;',
  20898. ' ObjVar:=IntfVar2 as TBird;',
  20899. ' IntfVar:=ObjVar as IBird;',
  20900. ' IntfVar:=IBird(IntfVar2);',
  20901. ' ObjVar:=TBird(IntfVar);',
  20902. ' IntfVar:=IBird(ObjVar);',
  20903. ' v:=IntfVar;',
  20904. ' IntfVar:=IBird(v);',
  20905. ' if v is IBird then ;',
  20906. ' v:=JSValue(IntfVar);',
  20907. ' v:=IBird;',
  20908. '']);
  20909. ConvertProgram;
  20910. CheckSource('TestClassInterface_Corba_Operators',
  20911. LinesToStr([ // statements
  20912. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20913. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20914. 'rtl.createClass(this, "TObject", null, function () {',
  20915. ' this.$init = function () {',
  20916. ' };',
  20917. ' this.$final = function () {',
  20918. ' };',
  20919. '});',
  20920. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20921. ' rtl.addIntf(this, $mod.IBird);',
  20922. '});',
  20923. 'this.IntfVar = null;',
  20924. 'this.IntfVar2 = null;',
  20925. 'this.ObjVar = null;',
  20926. 'this.v = undefined;',
  20927. '']),
  20928. LinesToStr([ // $mod.$main
  20929. '$mod.IntfVar = null;',
  20930. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20931. 'if ($mod.IntfVar != null) ;',
  20932. '$mod.IntfVar = $mod.IntfVar2;',
  20933. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20934. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20935. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20936. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20937. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20938. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20939. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20940. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20941. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20942. '$mod.IntfVar = $mod.IntfVar2;',
  20943. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20944. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20945. '$mod.v = $mod.IntfVar;',
  20946. '$mod.IntfVar = rtl.getObject($mod.v);',
  20947. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20948. '$mod.v = $mod.IntfVar;',
  20949. '$mod.v = $mod.IBird;',
  20950. '']));
  20951. end;
  20952. procedure TTestModule.TestClassInterface_Corba_Args;
  20953. begin
  20954. StartProgram(false);
  20955. Add([
  20956. '{$interfaces corba}',
  20957. 'type',
  20958. ' IUnknown = interface',
  20959. ' end;',
  20960. ' IBird = interface(IUnknown)',
  20961. ' end;',
  20962. ' TObject = class',
  20963. ' end;',
  20964. ' TBird = class(TObject,IBird)',
  20965. ' end;',
  20966. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20967. 'begin',
  20968. ' DoIt(i,i,i);',
  20969. 'end;',
  20970. 'procedure Change(var i: IBird; out j: IBird);',
  20971. 'begin',
  20972. ' DoIt(i,i,i);',
  20973. ' Change(i,i);',
  20974. 'end;',
  20975. 'var',
  20976. ' i: IBird;',
  20977. ' o: TBird;',
  20978. 'begin',
  20979. ' DoIt(i,i,i);',
  20980. ' Change(i,i);',
  20981. ' DoIt(o,o,o);',
  20982. '']);
  20983. ConvertProgram;
  20984. CheckSource('TestClassInterface_Corba_Args',
  20985. LinesToStr([ // statements
  20986. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20987. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20988. 'rtl.createClass(this, "TObject", null, function () {',
  20989. ' this.$init = function () {',
  20990. ' };',
  20991. ' this.$final = function () {',
  20992. ' };',
  20993. '});',
  20994. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20995. ' rtl.addIntf(this, $mod.IBird);',
  20996. '});',
  20997. 'this.DoIt = function (u, i, j) {',
  20998. ' $mod.DoIt({',
  20999. ' get: function () {',
  21000. ' return i;',
  21001. ' },',
  21002. ' set: function (v) {',
  21003. ' i = v;',
  21004. ' }',
  21005. ' }, i, i);',
  21006. '};',
  21007. 'this.Change = function (i, j) {',
  21008. ' $mod.DoIt(i, i.get(), i.get());',
  21009. ' $mod.Change(i, i);',
  21010. '};',
  21011. 'this.i = null;',
  21012. 'this.o = null;',
  21013. '']),
  21014. LinesToStr([ // $mod.$main
  21015. '$mod.DoIt({',
  21016. ' p: $mod,',
  21017. ' get: function () {',
  21018. ' return this.p.i;',
  21019. ' },',
  21020. ' set: function (v) {',
  21021. ' this.p.i = v;',
  21022. ' }',
  21023. '}, $mod.i, $mod.i);',
  21024. '$mod.Change({',
  21025. ' p: $mod,',
  21026. ' get: function () {',
  21027. ' return this.p.i;',
  21028. ' },',
  21029. ' set: function (v) {',
  21030. ' this.p.i = v;',
  21031. ' }',
  21032. '}, {',
  21033. ' p: $mod,',
  21034. ' get: function () {',
  21035. ' return this.p.i;',
  21036. ' },',
  21037. ' set: function (v) {',
  21038. ' this.p.i = v;',
  21039. ' }',
  21040. '});',
  21041. '$mod.DoIt({',
  21042. ' p: $mod,',
  21043. ' get: function () {',
  21044. ' return this.p.o;',
  21045. ' },',
  21046. ' set: function (v) {',
  21047. ' this.p.o = v;',
  21048. ' }',
  21049. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21050. '']));
  21051. end;
  21052. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21053. begin
  21054. StartProgram(false);
  21055. Add([
  21056. '{$interfaces corba}',
  21057. 'type',
  21058. ' IUnknown = interface end;',
  21059. ' TObject = class',
  21060. ' Id: longint;',
  21061. ' end;',
  21062. ' IEnumerator = interface(IUnknown)',
  21063. ' function GetCurrent: TObject;',
  21064. ' function MoveNext: Boolean;',
  21065. ' property Current: TObject read GetCurrent;',
  21066. ' end;',
  21067. ' IEnumerable = interface(IUnknown)',
  21068. ' function GetEnumerator: IEnumerator;',
  21069. ' end;',
  21070. 'var',
  21071. ' o: TObject;',
  21072. ' i: IEnumerable;',
  21073. 'begin',
  21074. ' for o in i do o.Id:=3;',
  21075. '']);
  21076. ConvertProgram;
  21077. CheckSource('TestClassInterface_Corba_ForIn',
  21078. LinesToStr([ // statements
  21079. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21080. 'rtl.createClass(this, "TObject", null, function () {',
  21081. ' this.$init = function () {',
  21082. ' this.Id = 0;',
  21083. ' };',
  21084. ' this.$final = function () {',
  21085. ' };',
  21086. '});',
  21087. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21088. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21089. 'this.o = null;',
  21090. 'this.i = null;',
  21091. '']),
  21092. LinesToStr([ // $mod.$main
  21093. 'var $in = $mod.i.GetEnumerator();',
  21094. 'while ($in.MoveNext()) {',
  21095. ' $mod.o = $in.GetCurrent();',
  21096. ' $mod.o.Id = 3;',
  21097. '};',
  21098. '']));
  21099. end;
  21100. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21101. begin
  21102. StartProgram(false);
  21103. Add([
  21104. '{$interfaces corba}',
  21105. 'type',
  21106. ' IUnknown = interface end;',
  21107. ' IBird = interface(IUnknown)',
  21108. ' function Fly(w: word): word;',
  21109. ' end;',
  21110. ' TBirdArray = array of IBird;',
  21111. 'var',
  21112. ' i: IBird;',
  21113. ' a: TBirdArray;',
  21114. 'begin',
  21115. ' SetLength(a,3);',
  21116. ' i:=a[1];',
  21117. ' a[2]:=i;',
  21118. ' for i in a do i.fly(3);',
  21119. '']);
  21120. ConvertProgram;
  21121. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21122. LinesToStr([ // statements
  21123. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21124. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21125. 'this.i = null;',
  21126. 'this.a = [];',
  21127. '']),
  21128. LinesToStr([ // $mod.$main
  21129. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21130. '$mod.i = $mod.a[1];',
  21131. '$mod.a[2] = $mod.i;',
  21132. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21133. ' $mod.i = $in[$l];',
  21134. ' $mod.i.Fly(3);',
  21135. '};',
  21136. '']));
  21137. end;
  21138. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21139. begin
  21140. StartProgram(false);
  21141. Add([
  21142. '{$interfaces com}',
  21143. 'type',
  21144. ' IUnknown = interface',
  21145. ' function _AddRef: longint;',
  21146. ' function _Release: longint;',
  21147. ' end;',
  21148. ' TObject = class(IUnknown)',
  21149. ' function _AddRef: longint; virtual; abstract;',
  21150. ' function _Release: longint; virtual; abstract;',
  21151. ' end;',
  21152. 'var',
  21153. ' i: IUnknown;',
  21154. 'procedure DoGlobal(o: TObject);',
  21155. 'begin',
  21156. ' i:=nil;',
  21157. ' i:=o;',
  21158. ' i:=i;',
  21159. 'end;',
  21160. 'procedure DoLocal(o: TObject);',
  21161. 'const k: IUnknown = nil;',
  21162. 'var j: IUnknown;',
  21163. 'begin',
  21164. ' k:=o;',
  21165. ' k:=i;',
  21166. ' j:=o;',
  21167. ' j:=i;',
  21168. 'end;',
  21169. 'var o: TObject;',
  21170. 'begin',
  21171. ' i:=nil;',
  21172. ' i:=o;',
  21173. '']);
  21174. ConvertProgram;
  21175. CheckSource('TestClassInterface_COM_AssignVar',
  21176. LinesToStr([ // statements
  21177. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21178. 'rtl.createClass(this, "TObject", null, function () {',
  21179. ' this.$init = function () {',
  21180. ' };',
  21181. ' this.$final = function () {',
  21182. ' };',
  21183. ' rtl.addIntf(this, $mod.IUnknown);',
  21184. '});',
  21185. 'this.i = null;',
  21186. 'this.DoGlobal = function (o) {',
  21187. ' rtl.setIntfP($mod, "i", null);',
  21188. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21189. ' rtl.setIntfP($mod, "i", $mod.i);',
  21190. '};',
  21191. 'var k = null;',
  21192. 'this.DoLocal = function (o) {',
  21193. ' var j = null;',
  21194. ' try{',
  21195. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21196. ' k = rtl.setIntfL(k, $mod.i);',
  21197. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21198. ' j = rtl.setIntfL(j, $mod.i);',
  21199. ' }finally{',
  21200. ' rtl._Release(j);',
  21201. ' };',
  21202. '};',
  21203. 'this.o = null;',
  21204. '']),
  21205. LinesToStr([ // $mod.$main
  21206. 'rtl.setIntfP($mod, "i", null);',
  21207. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21208. '']));
  21209. end;
  21210. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21211. begin
  21212. StartProgram(false);
  21213. Add([
  21214. '{$interfaces com}',
  21215. 'type',
  21216. ' IUnknown = interface',
  21217. ' function _AddRef: longint;',
  21218. ' function _Release: longint;',
  21219. ' end;',
  21220. ' TObject = class(IUnknown)',
  21221. ' function _AddRef: longint; virtual; abstract;',
  21222. ' function _Release: longint; virtual; abstract;',
  21223. ' end;',
  21224. 'procedure DoDefault(i, j: IUnknown);',
  21225. 'begin',
  21226. ' i:=nil;',
  21227. ' i:=j;',
  21228. 'end;',
  21229. 'begin',
  21230. '']);
  21231. ConvertProgram;
  21232. CheckSource('TestClassInterface_COM_AssignArg',
  21233. LinesToStr([ // statements
  21234. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21235. 'rtl.createClass(this, "TObject", null, function () {',
  21236. ' this.$init = function () {',
  21237. ' };',
  21238. ' this.$final = function () {',
  21239. ' };',
  21240. ' rtl.addIntf(this, $mod.IUnknown);',
  21241. '});',
  21242. 'this.DoDefault = function (i, j) {',
  21243. ' rtl._AddRef(i);',
  21244. ' try {',
  21245. ' i = rtl.setIntfL(i, null);',
  21246. ' i = rtl.setIntfL(i, j);',
  21247. ' } finally {',
  21248. ' rtl._Release(i);',
  21249. ' };',
  21250. '};',
  21251. '']),
  21252. LinesToStr([ // $mod.$main
  21253. '']));
  21254. end;
  21255. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21256. begin
  21257. StartProgram(false);
  21258. Add([
  21259. '{$interfaces com}',
  21260. 'type',
  21261. ' IUnknown = interface',
  21262. ' function _AddRef: longint;',
  21263. ' function _Release: longint;',
  21264. ' end;',
  21265. ' TObject = class(IUnknown)',
  21266. ' function _AddRef: longint; virtual; abstract;',
  21267. ' function _Release: longint; virtual; abstract;',
  21268. ' end;',
  21269. 'function DoDefault(i: IUnknown): IUnknown;',
  21270. 'begin',
  21271. ' Result:=i;',
  21272. ' if Result<>nil then exit;',
  21273. 'end;',
  21274. 'begin',
  21275. '']);
  21276. ConvertProgram;
  21277. CheckSource('TestClassInterface_COM_FunctionResult',
  21278. LinesToStr([ // statements
  21279. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21280. 'rtl.createClass(this, "TObject", null, function () {',
  21281. ' this.$init = function () {',
  21282. ' };',
  21283. ' this.$final = function () {',
  21284. ' };',
  21285. ' rtl.addIntf(this, $mod.IUnknown);',
  21286. '});',
  21287. 'this.DoDefault = function (i) {',
  21288. ' var Result = null;',
  21289. ' var $ok = false;',
  21290. ' try {',
  21291. ' Result = rtl.setIntfL(Result, i);',
  21292. ' if(Result !== null){',
  21293. ' $ok = true;',
  21294. ' return Result;',
  21295. ' };',
  21296. ' $ok = true;',
  21297. ' } finally {',
  21298. ' if(!$ok) rtl._Release(Result);',
  21299. ' };',
  21300. ' return Result;',
  21301. '};',
  21302. '']),
  21303. LinesToStr([ // $mod.$main
  21304. '']));
  21305. end;
  21306. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21307. begin
  21308. StartProgram(false);
  21309. Add([
  21310. '{$interfaces com}',
  21311. 'type',
  21312. ' IUnknown = interface',
  21313. ' function _AddRef: longint;',
  21314. ' function _Release: longint;',
  21315. ' end;',
  21316. ' TObject = class(IUnknown)',
  21317. ' function _AddRef: longint; virtual; abstract;',
  21318. ' function _Release: longint; virtual; abstract;',
  21319. ' function GetIntf: IUnknown; virtual;',
  21320. ' end;',
  21321. ' TMouse = class',
  21322. ' function GetIntf: IUnknown; override;',
  21323. ' end;',
  21324. 'function TObject.GetIntf: IUnknown; begin end;',
  21325. 'function TMouse.GetIntf: IUnknown;',
  21326. 'var i: IUnknown;',
  21327. 'begin',
  21328. ' inherited;',
  21329. ' inherited GetIntf;',
  21330. ' inherited GetIntf();',
  21331. ' Result:=inherited GetIntf;',
  21332. ' Result:=inherited GetIntf();',
  21333. ' i:=inherited GetIntf;',
  21334. ' i:=inherited GetIntf();',
  21335. 'end;',
  21336. 'begin',
  21337. '']);
  21338. ConvertProgram;
  21339. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21340. LinesToStr([ // statements
  21341. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21342. 'rtl.createClass(this, "TObject", null, function () {',
  21343. ' this.$init = function () {',
  21344. ' };',
  21345. ' this.$final = function () {',
  21346. ' };',
  21347. ' this.GetIntf = function () {',
  21348. ' var Result = null;',
  21349. ' return Result;',
  21350. ' };',
  21351. ' rtl.addIntf(this, $mod.IUnknown);',
  21352. '});',
  21353. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21354. ' this.GetIntf = function () {',
  21355. ' var Result = null;',
  21356. ' var i = null;',
  21357. ' var $ir = rtl.createIntfRefs();',
  21358. ' var $ok = false;',
  21359. ' try {',
  21360. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21361. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21362. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21363. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21364. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21365. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21366. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21367. ' $ok = true;',
  21368. ' } finally {',
  21369. ' $ir.free();',
  21370. ' rtl._Release(i);',
  21371. ' if (!$ok) rtl._Release(Result);',
  21372. ' };',
  21373. ' return Result;',
  21374. ' };',
  21375. ' rtl.addIntf(this, $mod.IUnknown);',
  21376. '});',
  21377. '']),
  21378. LinesToStr([ // $mod.$main
  21379. '']));
  21380. end;
  21381. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21382. begin
  21383. StartProgram(false);
  21384. Add([
  21385. '{$interfaces com}',
  21386. 'type',
  21387. ' IUnknown = interface',
  21388. ' function _AddRef: longint;',
  21389. ' function _Release: longint;',
  21390. ' end;',
  21391. ' TObject = class(IUnknown)',
  21392. ' function _AddRef: longint; virtual; abstract;',
  21393. ' function _Release: longint; virtual; abstract;',
  21394. ' end;',
  21395. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21396. 'begin',
  21397. ' if i is IUnknown then ;',
  21398. ' if o is IUnknown then ;',
  21399. ' if i is TObject then ;',
  21400. ' i:=j as IUnknown;',
  21401. ' i:=o as IUnknown;',
  21402. ' o:=j as TObject;',
  21403. ' i:=IUnknown(j);',
  21404. ' i:=IUnknown(o);',
  21405. ' o:=TObject(i);',
  21406. 'end;',
  21407. 'begin',
  21408. '']);
  21409. ConvertProgram;
  21410. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21411. LinesToStr([ // statements
  21412. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21413. 'rtl.createClass(this, "TObject", null, function () {',
  21414. ' this.$init = function () {',
  21415. ' };',
  21416. ' this.$final = function () {',
  21417. ' };',
  21418. ' rtl.addIntf(this, $mod.IUnknown);',
  21419. '});',
  21420. 'this.DoDefault = function (i, j, o) {',
  21421. ' rtl._AddRef(i);',
  21422. ' try {',
  21423. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21424. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21425. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21426. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21427. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21428. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21429. ' i = rtl.setIntfL(i, j);',
  21430. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21431. ' o = rtl.intfToClass(i, $mod.TObject);',
  21432. ' } finally {',
  21433. ' rtl._Release(i);',
  21434. ' };',
  21435. '};',
  21436. '']),
  21437. LinesToStr([ // $mod.$main
  21438. '']));
  21439. end;
  21440. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21441. begin
  21442. StartProgram(false);
  21443. Add([
  21444. '{$interfaces com}',
  21445. 'type',
  21446. ' IUnknown = interface',
  21447. ' function _AddRef: longint;',
  21448. ' function _Release: longint;',
  21449. ' end;',
  21450. ' TObject = class(IUnknown)',
  21451. ' function _AddRef: longint; virtual; abstract;',
  21452. ' function _Release: longint; virtual; abstract;',
  21453. ' end;',
  21454. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21455. 'var o: TObject;',
  21456. 'begin',
  21457. ' DoIt(v,v,v,v);',
  21458. ' DoIt(o,o,k,k);',
  21459. 'end;',
  21460. 'procedure DoSome;',
  21461. 'var v: IUnknown;',
  21462. 'begin',
  21463. ' DoIt(v,v,v,v);',
  21464. 'end;',
  21465. 'var i: IUnknown;',
  21466. 'begin',
  21467. ' DoIt(i,i,i,i);',
  21468. '']);
  21469. ConvertProgram;
  21470. CheckSource('TestClassInterface_COM_PassAsArg',
  21471. LinesToStr([ // statements
  21472. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21473. 'rtl.createClass(this, "TObject", null, function () {',
  21474. ' this.$init = function () {',
  21475. ' };',
  21476. ' this.$final = function () {',
  21477. ' };',
  21478. ' rtl.addIntf(this, $mod.IUnknown);',
  21479. '});',
  21480. 'this.DoIt = function (v, j, k, l) {',
  21481. ' var o = null;',
  21482. ' var $ir = rtl.createIntfRefs();',
  21483. ' rtl._AddRef(v);',
  21484. ' try {',
  21485. ' $mod.DoIt(v, v, {',
  21486. ' get: function () {',
  21487. ' return v;',
  21488. ' },',
  21489. ' set: function (w) {',
  21490. ' v = rtl.setIntfL(v, w);',
  21491. ' }',
  21492. ' }, {',
  21493. ' get: function () {',
  21494. ' return v;',
  21495. ' },',
  21496. ' set: function (w) {',
  21497. ' v = rtl.setIntfL(v, w);',
  21498. ' }',
  21499. ' });',
  21500. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21501. ' } finally {',
  21502. ' $ir.free();',
  21503. ' rtl._Release(v);',
  21504. ' };',
  21505. '};',
  21506. 'this.DoSome = function () {',
  21507. ' var v = null;',
  21508. ' try {',
  21509. ' $mod.DoIt(v, v, {',
  21510. ' get: function () {',
  21511. ' return v;',
  21512. ' },',
  21513. ' set: function (w) {',
  21514. ' v = rtl.setIntfL(v, w);',
  21515. ' }',
  21516. ' }, {',
  21517. ' get: function () {',
  21518. ' return v;',
  21519. ' },',
  21520. ' set: function (w) {',
  21521. ' v = rtl.setIntfL(v, w);',
  21522. ' }',
  21523. ' });',
  21524. ' } finally {',
  21525. ' rtl._Release(v);',
  21526. ' };',
  21527. '};',
  21528. 'this.i = null;',
  21529. '']),
  21530. LinesToStr([ // $mod.$main
  21531. '$mod.DoIt($mod.i, $mod.i, {',
  21532. ' p: $mod,',
  21533. ' get: function () {',
  21534. ' return this.p.i;',
  21535. ' },',
  21536. ' set: function (v) {',
  21537. ' rtl.setIntfP(this.p, "i", v);',
  21538. ' }',
  21539. '}, {',
  21540. ' p: $mod,',
  21541. ' get: function () {',
  21542. ' return this.p.i;',
  21543. ' },',
  21544. ' set: function (v) {',
  21545. ' rtl.setIntfP(this.p, "i", v);',
  21546. ' }',
  21547. '});',
  21548. '']));
  21549. end;
  21550. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21551. begin
  21552. StartProgram(false);
  21553. Add([
  21554. '{$interfaces com}',
  21555. 'type',
  21556. ' IUnknown = interface',
  21557. ' function _AddRef: longint;',
  21558. ' function _Release: longint;',
  21559. ' end;',
  21560. ' TObject = class(IUnknown)',
  21561. ' function _AddRef: longint; virtual; abstract;',
  21562. ' function _Release: longint; virtual; abstract;',
  21563. ' end;',
  21564. 'procedure DoIt(out i);',
  21565. 'begin end;',
  21566. 'procedure DoSome;',
  21567. 'var v: IUnknown;',
  21568. 'begin',
  21569. ' DoIt(v);',
  21570. 'end;',
  21571. 'function GetIt: IUnknown;',
  21572. 'begin',
  21573. ' DoIt(Result);',
  21574. 'end;',
  21575. 'var i: IUnknown;',
  21576. 'begin',
  21577. ' DoIt(i);',
  21578. '']);
  21579. ConvertProgram;
  21580. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21581. LinesToStr([ // statements
  21582. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21583. 'rtl.createClass(this, "TObject", null, function () {',
  21584. ' this.$init = function () {',
  21585. ' };',
  21586. ' this.$final = function () {',
  21587. ' };',
  21588. ' rtl.addIntf(this, $mod.IUnknown);',
  21589. '});',
  21590. 'this.DoIt = function (i) {',
  21591. '};',
  21592. 'this.DoSome = function () {',
  21593. ' var v = null;',
  21594. ' try {',
  21595. ' $mod.DoIt({',
  21596. ' get: function () {',
  21597. ' return v;',
  21598. ' },',
  21599. ' set: function (w) {',
  21600. ' v = w;',
  21601. ' }',
  21602. ' });',
  21603. ' } finally {',
  21604. ' rtl._Release(v);',
  21605. ' };',
  21606. '};',
  21607. 'this.GetIt = function () {',
  21608. ' var Result = null;',
  21609. ' var $ok = false;',
  21610. ' try {',
  21611. ' $mod.DoIt({',
  21612. ' get: function () {',
  21613. ' return Result;',
  21614. ' },',
  21615. ' set: function (v) {',
  21616. ' Result = v;',
  21617. ' }',
  21618. ' });',
  21619. ' $ok = true;',
  21620. ' } finally {',
  21621. ' if (!$ok) rtl._Release(Result);',
  21622. ' };',
  21623. ' return Result;',
  21624. '};',
  21625. 'this.i = null;',
  21626. '']),
  21627. LinesToStr([ // $mod.$main
  21628. 'try {',
  21629. ' $mod.DoIt({',
  21630. ' p: $mod,',
  21631. ' get: function () {',
  21632. ' return this.p.i;',
  21633. ' },',
  21634. ' set: function (v) {',
  21635. ' this.p.i = v;',
  21636. ' }',
  21637. ' });',
  21638. '} finally {',
  21639. ' rtl._Release($mod.i);',
  21640. '};',
  21641. '']));
  21642. end;
  21643. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21644. begin
  21645. StartProgram(false);
  21646. Add([
  21647. '{$interfaces com}',
  21648. 'type',
  21649. ' IUnknown = interface',
  21650. ' function _AddRef: longint;',
  21651. ' function _Release: longint;',
  21652. ' end;',
  21653. ' TObject = class(IUnknown)',
  21654. ' function _AddRef: longint; virtual; abstract;',
  21655. ' function _Release: longint; virtual; abstract;',
  21656. ' end;',
  21657. 'function GetIt: IUnknown;',
  21658. 'begin',
  21659. 'end;',
  21660. 'procedure DoSome;',
  21661. 'var v: IUnknown;',
  21662. ' i: longint;',
  21663. 'begin',
  21664. ' v:=GetIt;',
  21665. ' v:=GetIt();',
  21666. ' GetIt()._AddRef;',
  21667. ' i:=GetIt()._AddRef;',
  21668. 'end;',
  21669. 'var v: IUnknown;',
  21670. ' i: longint;',
  21671. 'begin',
  21672. ' v:=GetIt;',
  21673. ' v:=GetIt();',
  21674. ' GetIt()._AddRef;',
  21675. ' i:=GetIt()._AddRef;',
  21676. '']);
  21677. ConvertProgram;
  21678. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21679. LinesToStr([ // statements
  21680. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21681. 'rtl.createClass(this, "TObject", null, function () {',
  21682. ' this.$init = function () {',
  21683. ' };',
  21684. ' this.$final = function () {',
  21685. ' };',
  21686. ' rtl.addIntf(this, $mod.IUnknown);',
  21687. '});',
  21688. 'this.GetIt = function () {',
  21689. ' var Result = null;',
  21690. ' return Result;',
  21691. '};',
  21692. 'this.DoSome = function () {',
  21693. ' var v = null;',
  21694. ' var i = 0;',
  21695. ' var $ir = rtl.createIntfRefs();',
  21696. ' try {',
  21697. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21698. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21699. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21700. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21701. ' } finally {',
  21702. ' $ir.free();',
  21703. ' rtl._Release(v);',
  21704. ' };',
  21705. '};',
  21706. 'this.v = null;',
  21707. 'this.i = 0;',
  21708. '']),
  21709. LinesToStr([ // $mod.$main
  21710. 'var $ir = rtl.createIntfRefs();',
  21711. 'try {',
  21712. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21713. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21714. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21715. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21716. '} finally {',
  21717. ' $ir.free();',
  21718. '};',
  21719. '']));
  21720. end;
  21721. procedure TTestModule.TestClassInterface_COM_Property;
  21722. begin
  21723. StartProgram(false);
  21724. Add([
  21725. '{$interfaces com}',
  21726. 'type',
  21727. ' IUnknown = interface',
  21728. ' function _AddRef: longint;',
  21729. ' function _Release: longint;',
  21730. ' end;',
  21731. ' TObject = class(IUnknown)',
  21732. ' FAnt: IUnknown;',
  21733. ' function _AddRef: longint; virtual; abstract;',
  21734. ' function _Release: longint; virtual; abstract;',
  21735. ' function GetBird: IUnknown; virtual; abstract;',
  21736. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21737. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21738. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21739. ' property Ant: IUnknown read FAnt write FAnt;',
  21740. ' property Bird: IUnknown read GetBird write SetBird;',
  21741. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21742. ' end;',
  21743. 'procedure DoIt;',
  21744. 'var',
  21745. ' o: TObject;',
  21746. ' v: IUnknown;',
  21747. 'begin',
  21748. ' v:=o.Ant;',
  21749. ' o.Ant:=v;',
  21750. ' o.Ant:=o.Ant;',
  21751. ' v:=o.Bird;',
  21752. ' o.Bird:=v;',
  21753. ' o.Bird:=o.Bird;',
  21754. ' v:=o.Items[1];',
  21755. ' o.Items[2]:=v;',
  21756. ' o.Items[3]:=o.Items[4];',
  21757. ' v:=o[5];',
  21758. ' o[6]:=v;',
  21759. ' o[7]:=o[8];',
  21760. 'end;',
  21761. 'begin',
  21762. '']);
  21763. ConvertProgram;
  21764. CheckSource('TestClassInterface_COM_Property',
  21765. LinesToStr([ // statements
  21766. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21767. 'rtl.createClass(this, "TObject", null, function () {',
  21768. ' this.$init = function () {',
  21769. ' this.FAnt = null;',
  21770. ' };',
  21771. ' this.$final = function () {',
  21772. ' rtl.setIntfP(this, "FAnt", null);',
  21773. ' };',
  21774. ' rtl.addIntf(this, $mod.IUnknown);',
  21775. '});',
  21776. 'this.DoIt = function () {',
  21777. ' var o = null;',
  21778. ' var v = null;',
  21779. ' var $ir = rtl.createIntfRefs();',
  21780. ' try {',
  21781. ' v = rtl.setIntfL(v, o.FAnt);',
  21782. ' rtl.setIntfP(o, "FAnt", v);',
  21783. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21784. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21785. ' o.SetBird(v);',
  21786. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21787. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21788. ' o.SetItems(2, v);',
  21789. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21790. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21791. ' o.SetItems(6, v);',
  21792. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21793. ' } finally {',
  21794. ' $ir.free();',
  21795. ' rtl._Release(v);',
  21796. ' };',
  21797. '};',
  21798. '']),
  21799. LinesToStr([ // $mod.$main
  21800. '']));
  21801. end;
  21802. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21803. begin
  21804. StartProgram(false);
  21805. Add([
  21806. '{$interfaces com}',
  21807. 'type',
  21808. ' IUnknown = interface',
  21809. ' function _AddRef: longint;',
  21810. ' function _Release: longint;',
  21811. ' function GetBird: IUnknown;',
  21812. ' procedure SetBird(Value: IUnknown);',
  21813. ' function GetItems(Index: longint): IUnknown;',
  21814. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21815. ' property Bird: IUnknown read GetBird write SetBird;',
  21816. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21817. ' end;',
  21818. ' TObject = class(IUnknown)',
  21819. ' function _AddRef: longint; virtual; abstract;',
  21820. ' function _Release: longint; virtual; abstract;',
  21821. ' function GetBird: IUnknown; virtual; abstract;',
  21822. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21823. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21824. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21825. ' end;',
  21826. 'procedure DoIt;',
  21827. 'var',
  21828. ' o: TObject;',
  21829. ' v: IUnknown;',
  21830. 'begin',
  21831. ' v:=v.Items[1];',
  21832. ' v.Items[2]:=v;',
  21833. ' v.Items[3]:=v.Items[4];',
  21834. ' v:=v[5];',
  21835. ' v[6]:=v;',
  21836. ' v[7]:=v[8];',
  21837. ' v[9].Bird.Bird:=v;',
  21838. ' v:=v.Bird[10].Bird',
  21839. 'end;',
  21840. 'begin',
  21841. '']);
  21842. ConvertProgram;
  21843. CheckSource('TestClassInterface_COM_IntfProperty',
  21844. LinesToStr([ // statements
  21845. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21846. ' "_AddRef",',
  21847. ' "_Release",',
  21848. ' "GetBird",',
  21849. ' "SetBird",',
  21850. ' "GetItems",',
  21851. ' "SetItems"',
  21852. '], null);',
  21853. 'rtl.createClass(this, "TObject", null, function () {',
  21854. ' this.$init = function () {',
  21855. ' };',
  21856. ' this.$final = function () {',
  21857. ' };',
  21858. ' rtl.addIntf(this, $mod.IUnknown);',
  21859. '});',
  21860. 'this.DoIt = function () {',
  21861. ' var o = null;',
  21862. ' var v = null;',
  21863. ' var $ir = rtl.createIntfRefs();',
  21864. ' try {',
  21865. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21866. ' v.SetItems(2, v);',
  21867. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21868. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21869. ' v.SetItems(6, v);',
  21870. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21871. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21872. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21873. ' } finally {',
  21874. ' $ir.free();',
  21875. ' rtl._Release(v);',
  21876. ' };',
  21877. '};',
  21878. '']),
  21879. LinesToStr([ // $mod.$main
  21880. '']));
  21881. end;
  21882. procedure TTestModule.TestClassInterface_COM_Delegation;
  21883. begin
  21884. StartProgram(false);
  21885. Add([
  21886. '{$interfaces com}',
  21887. 'type',
  21888. ' IUnknown = interface',
  21889. ' function _AddRef: longint;',
  21890. ' function _Release: longint;',
  21891. ' end;',
  21892. ' IBird = interface(IUnknown)',
  21893. ' procedure Fly(s: string);',
  21894. ' end;',
  21895. ' IEagle = interface(IBird) end;',
  21896. ' IDove = interface(IBird) end;',
  21897. ' ISwallow = interface(IBird) end;',
  21898. ' TObject = class',
  21899. ' end;',
  21900. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21901. ' function _AddRef: longint; virtual; abstract;',
  21902. ' function _Release: longint; virtual; abstract;',
  21903. ' procedure Fly(s: string); virtual; abstract;',
  21904. ' end;',
  21905. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21906. ' function _AddRef: longint; virtual; abstract;',
  21907. ' function _Release: longint; virtual; abstract;',
  21908. ' FBirdIntf: IBird;',
  21909. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21910. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21911. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21912. ' FDoveObj: TBird;',
  21913. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21914. ' function GetSwallowObj: TBird; virtual; abstract;',
  21915. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21916. ' end;',
  21917. 'begin',
  21918. '']);
  21919. ConvertProgram;
  21920. CheckSource('TestClassInterface_COM_Delegation',
  21921. LinesToStr([ // statements
  21922. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21923. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21924. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21925. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21926. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21927. 'rtl.createClass(this, "TObject", null, function () {',
  21928. ' this.$init = function () {',
  21929. ' };',
  21930. ' this.$final = function () {',
  21931. ' };',
  21932. '});',
  21933. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21934. ' rtl.addIntf(this, $mod.IBird);',
  21935. ' rtl.addIntf(this, $mod.IEagle);',
  21936. ' rtl.addIntf(this, $mod.IDove);',
  21937. ' rtl.addIntf(this, $mod.ISwallow);',
  21938. '});',
  21939. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21940. ' this.$init = function () {',
  21941. ' $mod.TObject.$init.call(this);',
  21942. ' this.FBirdIntf = null;',
  21943. ' this.FDoveObj = null;',
  21944. ' };',
  21945. ' this.$final = function () {',
  21946. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21947. ' this.FDoveObj = undefined;',
  21948. ' $mod.TObject.$final.call(this);',
  21949. ' };',
  21950. ' this.$intfmaps = {',
  21951. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21952. ' return rtl._AddRef(this.FBirdIntf);',
  21953. ' },',
  21954. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21955. ' return this.GetEagleIntf();',
  21956. ' },',
  21957. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21958. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21959. ' },',
  21960. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21961. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21962. ' }',
  21963. ' };',
  21964. '});',
  21965. '']),
  21966. LinesToStr([ // $mod.$main
  21967. '']));
  21968. end;
  21969. procedure TTestModule.TestClassInterface_COM_With;
  21970. begin
  21971. StartProgram(false);
  21972. Add([
  21973. '{$interfaces com}',
  21974. 'type',
  21975. ' IUnknown = interface',
  21976. ' function _AddRef: longint;',
  21977. ' function _Release: longint;',
  21978. ' function GetAnt: IUnknown;',
  21979. ' property Ant: IUnknown read GetAnt;',
  21980. ' end;',
  21981. ' TObject = class(IUnknown)',
  21982. ' function _AddRef: longint; virtual; abstract;',
  21983. ' function _Release: longint; virtual; abstract;',
  21984. ' function GetAnt: IUnknown; virtual; abstract;',
  21985. ' property Ant: IUnknown read GetAnt;',
  21986. ' end;',
  21987. 'procedure DoIt;',
  21988. 'var',
  21989. ' i: IUnknown;',
  21990. 'begin',
  21991. ' with i do ',
  21992. ' GetAnt;',
  21993. ' with i.Ant, Ant do ',
  21994. ' GetAnt;',
  21995. 'end;',
  21996. 'begin',
  21997. '']);
  21998. ConvertProgram;
  21999. CheckSource('TestClassInterface_COM_With',
  22000. LinesToStr([ // statements
  22001. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22002. 'rtl.createClass(this, "TObject", null, function () {',
  22003. ' this.$init = function () {',
  22004. ' };',
  22005. ' this.$final = function () {',
  22006. ' };',
  22007. ' rtl.addIntf(this, $mod.IUnknown);',
  22008. '});',
  22009. 'this.DoIt = function () {',
  22010. ' var i = null;',
  22011. ' var $ir = rtl.createIntfRefs();',
  22012. ' try {',
  22013. ' $ir.ref(1, i.GetAnt());',
  22014. ' var $with = $ir.ref(2, i.GetAnt());',
  22015. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22016. ' $ir.ref(4, $with1.GetAnt());',
  22017. ' } finally {',
  22018. ' $ir.free();',
  22019. ' };',
  22020. '};',
  22021. '']),
  22022. LinesToStr([ // $mod.$main
  22023. '']));
  22024. end;
  22025. procedure TTestModule.TestClassInterface_COM_ForIn;
  22026. begin
  22027. StartProgram(false);
  22028. Add([
  22029. '{$interfaces com}',
  22030. 'type',
  22031. ' IUnknown = interface end;',
  22032. ' TObject = class',
  22033. ' Id: longint;',
  22034. ' end;',
  22035. ' IEnumerator = interface(IUnknown)',
  22036. ' function GetCurrent: TObject;',
  22037. ' function MoveNext: Boolean;',
  22038. ' property Current: TObject read GetCurrent;',
  22039. ' end;',
  22040. ' IEnumerable = interface(IUnknown)',
  22041. ' function GetEnumerator: IEnumerator;',
  22042. ' end;',
  22043. 'var',
  22044. ' o: TObject;',
  22045. ' i: IEnumerable;',
  22046. 'begin',
  22047. ' for o in i do o.Id:=3;',
  22048. '']);
  22049. ConvertProgram;
  22050. CheckSource('TestClassInterface_COM_ForIn',
  22051. LinesToStr([ // statements
  22052. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22053. 'rtl.createClass(this, "TObject", null, function () {',
  22054. ' this.$init = function () {',
  22055. ' this.Id = 0;',
  22056. ' };',
  22057. ' this.$final = function () {',
  22058. ' };',
  22059. '});',
  22060. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22061. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22062. 'this.o = null;',
  22063. 'this.i = null;',
  22064. '']),
  22065. LinesToStr([ // $mod.$main
  22066. 'var $in = $mod.i.GetEnumerator();',
  22067. 'try {',
  22068. ' while ($in.MoveNext()) {',
  22069. ' $mod.o = $in.GetCurrent();',
  22070. ' $mod.o.Id = 3;',
  22071. ' }',
  22072. '} finally {',
  22073. ' rtl._Release($in)',
  22074. '};',
  22075. '']));
  22076. end;
  22077. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22078. begin
  22079. {$IFNDEF EnableCOMArrayOfIntf}
  22080. exit;
  22081. {$ENDIF}
  22082. StartProgram(false);
  22083. Add([
  22084. '{$interfaces com}',
  22085. 'type',
  22086. ' IUnknown = interface end;',
  22087. ' IBird = interface(IUnknown)',
  22088. ' function Fly(w: word): word;',
  22089. ' end;',
  22090. ' TBirdArray = array of IBird;',
  22091. 'procedure Run;',
  22092. 'var',
  22093. ' i: IBird;',
  22094. ' a,b: TBirdArray;',
  22095. 'begin',
  22096. //' SetLength(a,3);',
  22097. ' a:=b;',
  22098. ' i:=a[1];',
  22099. ' a[2]:=i;',
  22100. //' for i in a do i.fly(3);',
  22101. // a:=copy(b,1,2);
  22102. // a:=concat(b,a);
  22103. // insert(i,b,1);
  22104. // a:=[i,i];
  22105. 'end;',
  22106. // ToDo: pass TBirdArray as arg
  22107. 'begin',
  22108. '']);
  22109. ConvertProgram;
  22110. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22111. LinesToStr([ // statements
  22112. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22113. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22114. 'this.Run = function () {',
  22115. ' var i = null;',
  22116. ' var a = [];',
  22117. ' var b = [];',
  22118. ' try {',
  22119. ' a = rtl.arrayRef(b);',
  22120. ' i = rtl.setIntfL(i, a[1]);',
  22121. ' rtl.setIntfP(a, 2, i);',
  22122. ' } finally {',
  22123. ' rtl._Release(i);',
  22124. ' rtl._ReleaseArray(a,1);',
  22125. ' };',
  22126. '};',
  22127. '']),
  22128. LinesToStr([ // $mod.$main
  22129. '']));
  22130. end;
  22131. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22132. begin
  22133. StartProgram(false);
  22134. Add([
  22135. '{$interfaces com}',
  22136. 'type',
  22137. ' IUnknown = interface',
  22138. ' function _AddRef: longint;',
  22139. ' function _Release: longint;',
  22140. ' end;',
  22141. ' TObject = class',
  22142. ' end;',
  22143. ' TArrOfIntf = array of IUnknown;',
  22144. 'begin',
  22145. '']);
  22146. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22147. ConvertProgram;
  22148. end;
  22149. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22150. begin
  22151. StartProgram(false);
  22152. Add([
  22153. '{$interfaces com}',
  22154. 'type',
  22155. ' IUnknown = interface',
  22156. ' function _AddRef: longint;',
  22157. ' function _Release: longint;',
  22158. ' end;',
  22159. ' TRec = record',
  22160. ' i: IUnknown;',
  22161. ' end;',
  22162. 'begin',
  22163. '']);
  22164. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22165. ConvertProgram;
  22166. end;
  22167. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22168. begin
  22169. StartUnit(false);
  22170. Add([
  22171. '{$interfaces com}',
  22172. 'interface',
  22173. 'implementation',
  22174. 'type',
  22175. ' IUnknown = interface',
  22176. ' function _AddRef: longint;',
  22177. ' end;',
  22178. ' TObject = class(IUnknown)',
  22179. ' function _AddRef: longint;',
  22180. ' end;',
  22181. 'function TObject._AddRef: longint; begin end;',
  22182. 'var i: IUnknown;',
  22183. ' o: TObject;',
  22184. 'initialization',
  22185. ' i:=nil;',
  22186. ' i:=i;',
  22187. ' i:=o;',
  22188. ' if (o as IUnknown)=nil then ;',
  22189. '']);
  22190. ConvertUnit;
  22191. CheckSource('TestClassInterface_COM_UnitInitialization',
  22192. LinesToStr([ // statements
  22193. 'var $impl = $mod.$impl;',
  22194. '']),
  22195. LinesToStr([ // this.$init
  22196. 'var $ir = rtl.createIntfRefs();',
  22197. 'try {',
  22198. ' rtl.setIntfP($impl, "i", null);',
  22199. ' rtl.setIntfP($impl, "i", $impl.i);',
  22200. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22201. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22202. '} finally {',
  22203. ' $ir.free();',
  22204. '};',
  22205. '']),
  22206. LinesToStr([ // implementation
  22207. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22208. 'rtl.createClass($impl, "TObject", null, function () {',
  22209. ' this.$init = function () {',
  22210. ' };',
  22211. ' this.$final = function () {',
  22212. ' };',
  22213. ' this._AddRef = function () {',
  22214. ' var Result = 0;',
  22215. ' return Result;',
  22216. ' };',
  22217. ' rtl.addIntf(this, $impl.IUnknown);',
  22218. '});',
  22219. '$impl.i = null;',
  22220. '$impl.o = null;',
  22221. ''])
  22222. );
  22223. end;
  22224. procedure TTestModule.TestClassInterface_Corba_GUID;
  22225. begin
  22226. StartProgram(false);
  22227. Add([
  22228. '{$interfaces corba}',
  22229. 'type',
  22230. ' IUnknown = interface',
  22231. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22232. ' end;',
  22233. ' TObject = class end;',
  22234. ' TGUID = record D1, D2, D3, D4: word; end;',
  22235. ' TAliasGUID = TGUID;',
  22236. ' TGUIDString = type string;',
  22237. ' TAliasGUIDString = TGUIDString;',
  22238. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22239. 'begin end;',
  22240. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22241. 'begin end;',
  22242. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22243. 'begin end;',
  22244. 'var',
  22245. ' i: IUnknown;',
  22246. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22247. ' s: TAliasGUIDString;',
  22248. 'begin',
  22249. ' DoConstGUIDIt(IUnknown);',
  22250. ' DoDefGUID(IUnknown);',
  22251. ' DoStr(IUnknown);',
  22252. ' DoConstGUIDIt(i);',
  22253. ' DoDefGUID(i);',
  22254. ' DoStr(i);',
  22255. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22256. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22257. ' DoStr(g);',
  22258. ' g:=i;',
  22259. ' g:=IUnknown;',
  22260. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22261. ' s:=i;',
  22262. ' s:=IUnknown;',
  22263. ' s:=g;',
  22264. ' if g=i then ;',
  22265. ' if i=g then ;',
  22266. ' if g=IUnknown then ;',
  22267. ' if IUnknown=g then ;',
  22268. ' if s=i then ;',
  22269. ' if i=s then ;',
  22270. ' if s=IUnknown then ;',
  22271. ' if IUnknown=s then ;',
  22272. ' if s=g then ;',
  22273. ' if g=s then ;',
  22274. '']);
  22275. ConvertProgram;
  22276. CheckSource('TestClassInterface_Corba_GUID',
  22277. LinesToStr([ // statements
  22278. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22279. 'rtl.createClass(this, "TObject", null, function () {',
  22280. ' this.$init = function () {',
  22281. ' };',
  22282. ' this.$final = function () {',
  22283. ' };',
  22284. '});',
  22285. 'rtl.recNewT(this, "TGUID", function () {',
  22286. ' this.D1 = 0;',
  22287. ' this.D2 = 0;',
  22288. ' this.D3 = 0;',
  22289. ' this.D4 = 0;',
  22290. ' this.$eq = function (b) {',
  22291. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22292. ' };',
  22293. ' this.$assign = function (s) {',
  22294. ' this.D1 = s.D1;',
  22295. ' this.D2 = s.D2;',
  22296. ' this.D3 = s.D3;',
  22297. ' this.D4 = s.D4;',
  22298. ' return this;',
  22299. ' };',
  22300. '});',
  22301. 'this.DoConstGUIDIt = function (g) {',
  22302. '};',
  22303. 'this.DoDefGUID = function (g) {',
  22304. '};',
  22305. 'this.DoStr = function (s) {',
  22306. '};',
  22307. 'this.i = null;',
  22308. 'this.g = this.TGUID.$clone({',
  22309. ' D1: 0xD91C9AF4,',
  22310. ' D2: 0x3C93,',
  22311. ' D3: 0x420F,',
  22312. ' D4: [',
  22313. ' 0xA3,',
  22314. ' 0x03,',
  22315. ' 0xBF,',
  22316. ' 0x5B,',
  22317. ' 0xA8,',
  22318. ' 0x2B,',
  22319. ' 0xFD,',
  22320. ' 0x23',
  22321. ' ]',
  22322. '});',
  22323. 'this.s = "";',
  22324. '']),
  22325. LinesToStr([ // $mod.$main
  22326. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22327. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22328. '$mod.DoStr($mod.IUnknown.$guid);',
  22329. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22330. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22331. '$mod.DoStr($mod.i.$guid);',
  22332. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22333. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22334. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22335. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22336. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22337. '$mod.g.$assign({',
  22338. ' D1: 0xD91C9AF4,',
  22339. ' D2: 0x3C93,',
  22340. ' D3: 0x420F,',
  22341. ' D4: [',
  22342. ' 0xA3,',
  22343. ' 0x03,',
  22344. ' 0xBF,',
  22345. ' 0x5B,',
  22346. ' 0xA8,',
  22347. ' 0x2B,',
  22348. ' 0xFD,',
  22349. ' 0x23',
  22350. ' ]',
  22351. '});',
  22352. '$mod.s = $mod.i.$guid;',
  22353. '$mod.s = $mod.IUnknown.$guid;',
  22354. '$mod.s = rtl.guidrToStr($mod.g);',
  22355. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22356. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22357. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22358. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22359. 'if ($mod.s === $mod.i.$guid) ;',
  22360. 'if ($mod.i.$guid === $mod.s) ;',
  22361. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22362. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22363. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22364. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22365. '']));
  22366. end;
  22367. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22368. begin
  22369. StartProgram(false);
  22370. Add([
  22371. '{$interfaces corba}',
  22372. 'type',
  22373. ' IUnknown = interface',
  22374. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22375. ' end;',
  22376. ' TGUID = record D1, D2, D3, D4: word; end;',
  22377. ' TAliasGUID = TGUID;',
  22378. ' TGUIDString = type string;',
  22379. ' TAliasGUIDString = TGUIDString;',
  22380. ' TObject = class',
  22381. ' function GetG: TAliasGUID; virtual; abstract;',
  22382. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22383. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22384. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22385. ' property g: TAliasGUID read GetG write SetG;',
  22386. ' property s: TAliasGUIDString read GetS write SetS;',
  22387. ' end;',
  22388. 'var o: TObject;',
  22389. 'begin',
  22390. ' o.g:=IUnknown;',
  22391. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22392. ' o.s:=IUnknown;',
  22393. ' o.s:=o.g;',
  22394. '']);
  22395. ConvertProgram;
  22396. CheckSource('TestClassInterface_GUIDProperty',
  22397. LinesToStr([ // statements
  22398. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22399. 'rtl.recNewT(this, "TGUID", function () {',
  22400. ' this.D1 = 0;',
  22401. ' this.D2 = 0;',
  22402. ' this.D3 = 0;',
  22403. ' this.D4 = 0;',
  22404. ' this.$eq = function (b) {',
  22405. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22406. ' };',
  22407. ' this.$assign = function (s) {',
  22408. ' this.D1 = s.D1;',
  22409. ' this.D2 = s.D2;',
  22410. ' this.D3 = s.D3;',
  22411. ' this.D4 = s.D4;',
  22412. ' return this;',
  22413. ' };',
  22414. '});',
  22415. 'rtl.createClass(this, "TObject", null, function () {',
  22416. ' this.$init = function () {',
  22417. ' };',
  22418. ' this.$final = function () {',
  22419. ' };',
  22420. '});',
  22421. 'this.o = null;',
  22422. '']),
  22423. LinesToStr([ // $mod.$main
  22424. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22425. '$mod.o.SetG({',
  22426. ' D1: 0xD91C9AF4,',
  22427. ' D2: 0x3C93,',
  22428. ' D3: 0x420F,',
  22429. ' D4: [',
  22430. ' 0xA3,',
  22431. ' 0x03,',
  22432. ' 0xBF,',
  22433. ' 0x5B,',
  22434. ' 0xA8,',
  22435. ' 0x2B,',
  22436. ' 0xFD,',
  22437. ' 0x23',
  22438. ' ]',
  22439. '});',
  22440. '$mod.o.SetS($mod.IUnknown.$guid);',
  22441. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22442. '']));
  22443. end;
  22444. procedure TTestModule.TestClassHelper_ClassVar;
  22445. begin
  22446. StartProgram(false);
  22447. Add([
  22448. 'type',
  22449. ' TObject = class',
  22450. ' end;',
  22451. ' THelper = class helper for TObject',
  22452. ' const',
  22453. ' One = 1;',
  22454. ' Two: word = 2;',
  22455. ' class var',
  22456. ' Glob: word;',
  22457. ' function Foo(w: word): word;',
  22458. ' class function Bar(w: word): word;',
  22459. ' end;',
  22460. 'function THelper.foo(w: word): word;',
  22461. 'begin',
  22462. ' Result:=w;',
  22463. ' Two:=One+w;',
  22464. ' Glob:=Glob;',
  22465. ' Result:=Self.Glob;',
  22466. ' Self.Glob:=Self.Glob;',
  22467. ' with Self do Glob:=Glob;',
  22468. 'end;',
  22469. 'class function THelper.bar(w: word): word;',
  22470. 'begin',
  22471. ' Result:=w;',
  22472. ' Two:=One;',
  22473. ' Glob:=Glob;',
  22474. ' Self.Glob:=Self.Glob;',
  22475. ' with Self do Glob:=Glob;',
  22476. 'end;',
  22477. 'var o: TObject;',
  22478. 'begin',
  22479. ' tobject.two:=tobject.one;',
  22480. ' tobject.Glob:=tobject.Glob;',
  22481. ' with tobject do begin',
  22482. ' two:=one;',
  22483. ' Glob:=Glob;',
  22484. ' end;',
  22485. ' o.two:=o.one;',
  22486. ' o.Glob:=o.Glob;',
  22487. ' with o do begin',
  22488. ' two:=one;',
  22489. ' Glob:=Glob;',
  22490. ' end;',
  22491. '']);
  22492. ConvertProgram;
  22493. CheckSource('TestClassHelper_ClassVar',
  22494. LinesToStr([ // statements
  22495. 'rtl.createClass(this, "TObject", null, function () {',
  22496. ' this.$init = function () {',
  22497. ' };',
  22498. ' this.$final = function () {',
  22499. ' };',
  22500. '});',
  22501. 'rtl.createHelper(this, "THelper", null, function () {',
  22502. ' this.One = 1;',
  22503. ' this.Two = 2;',
  22504. ' this.Glob = 0;',
  22505. ' this.Foo = function (w) {',
  22506. ' var Result = 0;',
  22507. ' Result = w;',
  22508. ' $mod.THelper.Two = 1 + w;',
  22509. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22510. ' Result = $mod.THelper.Glob;',
  22511. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22512. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22513. ' return Result;',
  22514. ' };',
  22515. ' this.Bar = function (w) {',
  22516. ' var Result = 0;',
  22517. ' Result = w;',
  22518. ' $mod.THelper.Two = 1;',
  22519. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22520. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22521. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22522. ' return Result;',
  22523. ' };',
  22524. '});',
  22525. 'this.o = null;',
  22526. '']),
  22527. LinesToStr([ // $mod.$main
  22528. '$mod.THelper.Two = 1;',
  22529. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22530. 'var $with = $mod.TObject;',
  22531. '$mod.THelper.Two = 1;',
  22532. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22533. '$mod.THelper.Two = 1;',
  22534. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22535. 'var $with1 = $mod.o;',
  22536. '$mod.THelper.Two = 1;',
  22537. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22538. '']));
  22539. end;
  22540. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22541. begin
  22542. StartProgram(false);
  22543. Add([
  22544. 'type',
  22545. ' TObject = class',
  22546. ' FSize: word;',
  22547. ' property Size: word read FSize write FSize;',
  22548. ' end;',
  22549. ' THelper = class helper for TObject',
  22550. ' function Foo(w: word = 1): word;',
  22551. ' end;',
  22552. 'function THelper.foo(w: word): word;',
  22553. 'begin',
  22554. ' Result:=Size;',
  22555. ' Size:=Size+2;',
  22556. ' Self.Size:=Self.Size+3;',
  22557. ' FSize:=FSize+4;',
  22558. ' Self.FSize:=Self.FSize+5;',
  22559. ' with Self do begin',
  22560. ' Size:=Size+6;',
  22561. ' FSize:=FSize+7;',
  22562. ' FSize:=FSize+8;',
  22563. ' end;',
  22564. 'end;',
  22565. 'begin',
  22566. '']);
  22567. ConvertProgram;
  22568. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22569. LinesToStr([ // statements
  22570. 'rtl.createClass(this, "TObject", null, function () {',
  22571. ' this.$init = function () {',
  22572. ' this.FSize = 0;',
  22573. ' };',
  22574. ' this.$final = function () {',
  22575. ' };',
  22576. '});',
  22577. 'rtl.createHelper(this, "THelper", null, function () {',
  22578. ' this.Foo = function (w) {',
  22579. ' var Result = 0;',
  22580. ' Result = this.FSize;',
  22581. ' this.FSize = this.FSize + 2;',
  22582. ' this.FSize = this.FSize + 3;',
  22583. ' this.FSize = this.FSize + 4;',
  22584. ' this.FSize = this.FSize + 5;',
  22585. ' this.FSize = this.FSize + 6;',
  22586. ' this.FSize = this.FSize + 7;',
  22587. ' this.FSize = this.FSize + 8;',
  22588. ' return Result;',
  22589. ' };',
  22590. '});',
  22591. '']),
  22592. LinesToStr([ // $mod.$main
  22593. '']));
  22594. end;
  22595. procedure TTestModule.TestClassHelper_Method_Call;
  22596. begin
  22597. StartProgram(false);
  22598. Add([
  22599. 'type',
  22600. ' TObject = class',
  22601. ' procedure Run(w: word = 10);',
  22602. ' end;',
  22603. ' THelper = class helper for TObject',
  22604. ' function Foo(w: word = 1): word;',
  22605. ' end;',
  22606. 'procedure TObject.Run(w: word);',
  22607. 'var o: TObject;',
  22608. 'begin',
  22609. ' Foo;',
  22610. ' Foo();',
  22611. ' Foo(2);',
  22612. ' Self.Foo;',
  22613. ' Self.Foo();',
  22614. ' Self.Foo(3);',
  22615. ' with Self do begin',
  22616. ' Foo;',
  22617. ' Foo();',
  22618. ' Foo(4);',
  22619. ' end;',
  22620. ' with o do Foo(5);',
  22621. 'end;',
  22622. 'function THelper.foo(w: word): word;',
  22623. 'begin',
  22624. ' Run;',
  22625. ' Run();',
  22626. ' Run(11);',
  22627. ' Foo;',
  22628. ' Foo();',
  22629. ' Foo(12);',
  22630. ' Self.Foo;',
  22631. ' Self.Foo();',
  22632. ' Self.Foo(13);',
  22633. ' with Self do begin',
  22634. ' Foo;',
  22635. ' Foo();',
  22636. ' Foo(14);',
  22637. ' end;',
  22638. 'end;',
  22639. 'var Obj: TObject;',
  22640. 'begin',
  22641. ' obj.Foo;',
  22642. ' obj.Foo();',
  22643. ' obj.Foo(21);',
  22644. ' with obj do begin',
  22645. ' Foo;',
  22646. ' Foo();',
  22647. ' Foo(22);',
  22648. ' end;',
  22649. '']);
  22650. ConvertProgram;
  22651. CheckSource('TestClassHelper_Method_Call',
  22652. LinesToStr([ // statements
  22653. 'rtl.createClass(this, "TObject", null, function () {',
  22654. ' this.$init = function () {',
  22655. ' };',
  22656. ' this.$final = function () {',
  22657. ' };',
  22658. ' this.Run = function (w) {',
  22659. ' var o = null;',
  22660. ' $mod.THelper.Foo.call(this, 1);',
  22661. ' $mod.THelper.Foo.call(this, 1);',
  22662. ' $mod.THelper.Foo.call(this, 2);',
  22663. ' $mod.THelper.Foo.call(this, 1);',
  22664. ' $mod.THelper.Foo.call(this, 1);',
  22665. ' $mod.THelper.Foo.call(this, 3);',
  22666. ' $mod.THelper.Foo.call(this, 1);',
  22667. ' $mod.THelper.Foo.call(this, 1);',
  22668. ' $mod.THelper.Foo.call(this, 4);',
  22669. ' $mod.THelper.Foo.call(o, 5);',
  22670. ' };',
  22671. '});',
  22672. 'rtl.createHelper(this, "THelper", null, function () {',
  22673. ' this.Foo = function (w) {',
  22674. ' var Result = 0;',
  22675. ' this.Run(10);',
  22676. ' this.Run(10);',
  22677. ' this.Run(11);',
  22678. ' $mod.THelper.Foo.call(this, 1);',
  22679. ' $mod.THelper.Foo.call(this, 1);',
  22680. ' $mod.THelper.Foo.call(this, 12);',
  22681. ' $mod.THelper.Foo.call(this, 1);',
  22682. ' $mod.THelper.Foo.call(this, 1);',
  22683. ' $mod.THelper.Foo.call(this, 13);',
  22684. ' $mod.THelper.Foo.call(this, 1);',
  22685. ' $mod.THelper.Foo.call(this, 1);',
  22686. ' $mod.THelper.Foo.call(this, 14);',
  22687. ' return Result;',
  22688. ' };',
  22689. '});',
  22690. 'this.Obj = null;',
  22691. '']),
  22692. LinesToStr([ // $mod.$main
  22693. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22694. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22695. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22696. 'var $with = $mod.Obj;',
  22697. '$mod.THelper.Foo.call($with, 1);',
  22698. '$mod.THelper.Foo.call($with, 1);',
  22699. '$mod.THelper.Foo.call($with, 22);',
  22700. '']));
  22701. end;
  22702. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22703. begin
  22704. StartProgram(false);
  22705. Add([
  22706. 'type',
  22707. ' TObject = class',
  22708. ' procedure Run(w: word = 10);',
  22709. ' end;',
  22710. ' THelper = class helper for TObject',
  22711. ' function Foo(w: word = 1): word;',
  22712. ' end;',
  22713. 'procedure TObject.Run(w: word);',
  22714. ' procedure Sub(Self: TObject);',
  22715. ' begin',
  22716. ' Foo;',
  22717. ' Foo();',
  22718. ' Self.Foo;',
  22719. ' Self.Foo();',
  22720. ' with Self do begin',
  22721. ' Foo;',
  22722. ' Foo();',
  22723. ' end;',
  22724. ' end;',
  22725. 'begin',
  22726. 'end;',
  22727. 'function THelper.foo(w: word): word;',
  22728. ' procedure Sub(Self: TObject);',
  22729. ' begin',
  22730. ' Run;',
  22731. ' Run();',
  22732. ' Foo;',
  22733. ' Foo();',
  22734. ' Self.Foo;',
  22735. ' Self.Foo();',
  22736. ' with Self do begin',
  22737. ' Foo;',
  22738. ' Foo();',
  22739. ' end;',
  22740. ' end;',
  22741. 'begin',
  22742. 'end;',
  22743. 'begin',
  22744. '']);
  22745. ConvertProgram;
  22746. CheckSource('TestClassHelper_Method_Nested_Call',
  22747. LinesToStr([ // statements
  22748. 'rtl.createClass(this, "TObject", null, function () {',
  22749. ' this.$init = function () {',
  22750. ' };',
  22751. ' this.$final = function () {',
  22752. ' };',
  22753. ' this.Run = function (w) {',
  22754. ' var $Self = this;',
  22755. ' function Sub(Self) {',
  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. ' $mod.THelper.Foo.call(Self, 1);',
  22761. ' $mod.THelper.Foo.call(Self, 1);',
  22762. ' };',
  22763. ' };',
  22764. '});',
  22765. 'rtl.createHelper(this, "THelper", null, function () {',
  22766. ' this.Foo = function (w) {',
  22767. ' var $Self = this;',
  22768. ' var Result = 0;',
  22769. ' function Sub(Self) {',
  22770. ' $Self.Run(10);',
  22771. ' $Self.Run(10);',
  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. ' $mod.THelper.Foo.call(Self, 1);',
  22777. ' $mod.THelper.Foo.call(Self, 1);',
  22778. ' };',
  22779. ' return Result;',
  22780. ' };',
  22781. '});',
  22782. '']),
  22783. LinesToStr([ // $mod.$main
  22784. '']));
  22785. end;
  22786. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22787. begin
  22788. StartProgram(false);
  22789. Add([
  22790. 'type',
  22791. ' TObject = class',
  22792. ' class procedure Run(w: word = 10);',
  22793. ' end;',
  22794. ' THelper = class helper for TObject',
  22795. ' class function Foo(w: word = 1): word;',
  22796. ' end;',
  22797. 'class procedure TObject.Run(w: word);',
  22798. 'begin',
  22799. ' Foo;',
  22800. ' Foo();',
  22801. ' Self.Foo;',
  22802. ' Self.Foo();',
  22803. ' with Self do begin',
  22804. ' Foo;',
  22805. ' Foo();',
  22806. ' end;',
  22807. 'end;',
  22808. 'class function THelper.foo(w: word): word;',
  22809. 'begin',
  22810. ' Run;',
  22811. ' Run();',
  22812. ' Foo;',
  22813. ' Foo();',
  22814. ' Self.Foo;',
  22815. ' Self.Foo();',
  22816. ' with Self do begin',
  22817. ' Foo;',
  22818. ' Foo();',
  22819. ' end;',
  22820. 'end;',
  22821. 'var',
  22822. ' Obj: TObject;',
  22823. 'begin',
  22824. ' obj.Foo;',
  22825. ' obj.Foo();',
  22826. ' with obj do begin',
  22827. ' Foo;',
  22828. ' Foo();',
  22829. ' end;',
  22830. ' tobject.Foo;',
  22831. ' tobject.Foo();',
  22832. ' with tobject do begin',
  22833. ' Foo;',
  22834. ' Foo();',
  22835. ' end;',
  22836. '']);
  22837. ConvertProgram;
  22838. CheckSource('TestClassHelper_ClassMethod_Call',
  22839. LinesToStr([ // statements
  22840. 'rtl.createClass(this, "TObject", null, function () {',
  22841. ' this.$init = function () {',
  22842. ' };',
  22843. ' this.$final = function () {',
  22844. ' };',
  22845. ' this.Run = function (w) {',
  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. ' $mod.THelper.Foo.call(this, 1);',
  22851. ' $mod.THelper.Foo.call(this, 1);',
  22852. ' };',
  22853. '});',
  22854. 'rtl.createHelper(this, "THelper", null, function () {',
  22855. ' this.Foo = function (w) {',
  22856. ' var Result = 0;',
  22857. ' this.Run(10);',
  22858. ' this.Run(10);',
  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. ' $mod.THelper.Foo.call(this, 1);',
  22864. ' $mod.THelper.Foo.call(this, 1);',
  22865. ' return Result;',
  22866. ' };',
  22867. '});',
  22868. 'this.Obj = null;',
  22869. '']),
  22870. LinesToStr([ // $mod.$main
  22871. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22872. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22873. 'var $with = $mod.Obj;',
  22874. '$mod.THelper.Foo.call($with.$class, 1);',
  22875. '$mod.THelper.Foo.call($with.$class, 1);',
  22876. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22877. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22878. 'var $with1 = $mod.TObject;',
  22879. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22880. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22881. '']));
  22882. end;
  22883. procedure TTestModule.TestClassHelper_ClassOf;
  22884. begin
  22885. StartProgram(false);
  22886. Add([
  22887. 'type',
  22888. ' TObject = class',
  22889. ' end;',
  22890. ' TClass = class of TObject;',
  22891. ' THelper = class helper for TObject',
  22892. ' class function Foo(w: word = 1): word;',
  22893. ' end;',
  22894. 'class function THelper.foo(w: word): word;',
  22895. 'begin',
  22896. 'end;',
  22897. 'var',
  22898. ' c: TClass;',
  22899. 'begin',
  22900. ' c.Foo;',
  22901. ' c.Foo();',
  22902. ' with c do begin',
  22903. ' Foo;',
  22904. ' Foo();',
  22905. ' end;',
  22906. '']);
  22907. ConvertProgram;
  22908. CheckSource('TestClassHelper_ClassOf',
  22909. LinesToStr([ // statements
  22910. 'rtl.createClass(this, "TObject", null, function () {',
  22911. ' this.$init = function () {',
  22912. ' };',
  22913. ' this.$final = function () {',
  22914. ' };',
  22915. '});',
  22916. 'rtl.createHelper(this, "THelper", null, function () {',
  22917. ' this.Foo = function (w) {',
  22918. ' var Result = 0;',
  22919. ' return Result;',
  22920. ' };',
  22921. '});',
  22922. 'this.c = null;',
  22923. '']),
  22924. LinesToStr([ // $mod.$main
  22925. '$mod.THelper.Foo.call($mod.c, 1);',
  22926. '$mod.THelper.Foo.call($mod.c, 1);',
  22927. 'var $with = $mod.c;',
  22928. '$mod.THelper.Foo.call($with, 1);',
  22929. '$mod.THelper.Foo.call($with, 1);',
  22930. '']));
  22931. end;
  22932. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22933. begin
  22934. StartProgram(false);
  22935. Add([
  22936. '{$mode objfpc}',
  22937. 'type',
  22938. ' TObject = class',
  22939. ' procedure DoIt;',
  22940. ' end;',
  22941. ' THelper = class helper for TObject',
  22942. ' procedure Fly(w: word = 1);',
  22943. ' class procedure Glide(w: word = 1);',
  22944. ' class procedure Run(w: word = 1); static;',
  22945. ' end;',
  22946. ' TFly = procedure(w: word) of object;',
  22947. ' TGlide = TFly;',
  22948. ' TRun = procedure(w: word);',
  22949. 'var',
  22950. ' f: TFly;',
  22951. ' g: TGlide;',
  22952. ' r: TRun;',
  22953. 'procedure TObject.DoIt;',
  22954. 'begin',
  22955. ' f:=@fly;',
  22956. ' g:=@glide;',
  22957. ' r:=@run;',
  22958. ' f:[email protected];',
  22959. ' g:[email protected];',
  22960. ' r:[email protected];',
  22961. ' with self do begin',
  22962. ' f:=@fly;',
  22963. ' g:=@glide;',
  22964. ' r:=@run;',
  22965. ' end;',
  22966. 'end;',
  22967. 'procedure THelper.fly(w: word);',
  22968. 'begin',
  22969. ' f:=@fly;',
  22970. ' g:=@glide;',
  22971. ' r:=@run;',
  22972. 'end;',
  22973. 'class procedure THelper.glide(w: word);',
  22974. 'begin',
  22975. ' g:=@glide;',
  22976. ' r:=@run;',
  22977. 'end;',
  22978. 'class procedure THelper.run(w: word);',
  22979. 'begin',
  22980. ' g:=@glide;',
  22981. ' r:=@run;',
  22982. 'end;',
  22983. 'var',
  22984. ' Obj: TObject;',
  22985. 'begin',
  22986. ' f:[email protected];',
  22987. ' g:[email protected];',
  22988. ' r:[email protected];',
  22989. ' with obj do begin',
  22990. ' f:=@fly;',
  22991. ' g:=@glide;',
  22992. ' r:=@run;',
  22993. ' end;',
  22994. ' g:[email protected];',
  22995. ' r:[email protected];',
  22996. ' with tobject do begin',
  22997. ' g:=@glide;',
  22998. ' r:=@run;',
  22999. ' end;',
  23000. '']);
  23001. ConvertProgram;
  23002. CheckSource('TestClassHelper_MethodRefObjFPC',
  23003. LinesToStr([ // statements
  23004. 'rtl.createClass(this, "TObject", null, function () {',
  23005. ' this.$init = function () {',
  23006. ' };',
  23007. ' this.$final = function () {',
  23008. ' };',
  23009. ' this.DoIt = function () {',
  23010. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23011. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23012. ' $mod.r = $mod.THelper.Run;',
  23013. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23014. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23015. ' $mod.r = $mod.THelper.Run;',
  23016. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23017. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23018. ' $mod.r = $mod.THelper.Run;',
  23019. ' };',
  23020. '});',
  23021. 'rtl.createHelper(this, "THelper", null, function () {',
  23022. ' this.Fly = function (w) {',
  23023. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23024. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23025. ' $mod.r = $mod.THelper.Run;',
  23026. ' };',
  23027. ' this.Glide = function (w) {',
  23028. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23029. ' $mod.r = $mod.THelper.Run;',
  23030. ' };',
  23031. ' this.Run = function (w) {',
  23032. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23033. ' $mod.r = $mod.THelper.Run;',
  23034. ' };',
  23035. '});',
  23036. 'this.f = null;',
  23037. 'this.g = null;',
  23038. 'this.r = null;',
  23039. 'this.Obj = null;',
  23040. '']),
  23041. LinesToStr([ // $mod.$main
  23042. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23043. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23044. '$mod.r = $mod.THelper.Run;',
  23045. 'var $with = $mod.Obj;',
  23046. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23047. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23048. '$mod.r = $mod.THelper.Run;',
  23049. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23050. '$mod.r = $mod.THelper.Run;',
  23051. 'var $with1 = $mod.TObject;',
  23052. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23053. '$mod.r = $mod.THelper.Run;',
  23054. '']));
  23055. end;
  23056. procedure TTestModule.TestClassHelper_Constructor;
  23057. begin
  23058. StartProgram(false);
  23059. Add([
  23060. 'type',
  23061. ' TObject = class',
  23062. ' constructor Create;',
  23063. ' end;',
  23064. ' TClass = class of TObject;',
  23065. ' THelper = class helper for TObject',
  23066. ' constructor NewHlp(w: word);',
  23067. ' end;',
  23068. 'var',
  23069. ' obj: TObject;',
  23070. ' c: TClass;',
  23071. 'constructor TObject.Create;',
  23072. 'begin',
  23073. ' NewHlp(2);', // normal call
  23074. ' tobject.NewHlp(3);', // new instance
  23075. ' c.newhlp(4);', // new instance
  23076. 'end;',
  23077. 'constructor THelper.NewHlp(w: word);',
  23078. 'begin',
  23079. ' create;', // normal call
  23080. ' tobject.create;', // new instance
  23081. ' NewHlp(2);', // normal call
  23082. ' tobject.NewHlp(3);', // new instance
  23083. ' c.newhlp(4);', // new instance
  23084. 'end;',
  23085. 'begin',
  23086. ' obj.newhlp(2);', // normal call
  23087. ' with Obj do newhlp(12);', // normal call
  23088. ' tobject.newhlp(3);', // new instance
  23089. ' with tobject do newhlp(13);', // new instance
  23090. ' c.newhlp(4);', // new instance
  23091. ' with c do newhlp(14);', // new instance
  23092. '']);
  23093. ConvertProgram;
  23094. CheckSource('TestClassHelper_Constructor',
  23095. LinesToStr([ // statements
  23096. 'rtl.createClass(this, "TObject", null, function () {',
  23097. ' this.$init = function () {',
  23098. ' };',
  23099. ' this.$final = function () {',
  23100. ' };',
  23101. ' this.Create = function () {',
  23102. ' $mod.THelper.NewHlp.call(this, 2);',
  23103. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23104. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23105. ' return this;',
  23106. ' };',
  23107. '});',
  23108. 'rtl.createHelper(this, "THelper", null, function () {',
  23109. ' this.NewHlp = function (w) {',
  23110. ' this.Create();',
  23111. ' $mod.TObject.$create("Create");',
  23112. ' $mod.THelper.NewHlp.call(this, 2);',
  23113. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23114. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23115. ' return this;',
  23116. ' };',
  23117. '});',
  23118. 'this.obj = null;',
  23119. 'this.c = null;',
  23120. '']),
  23121. LinesToStr([ // $mod.$main
  23122. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23123. 'var $with = $mod.obj;',
  23124. '$mod.THelper.NewHlp.call($with, 12);',
  23125. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23126. 'var $with1 = $mod.TObject;',
  23127. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23128. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23129. 'var $with2 = $mod.c;',
  23130. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23131. '']));
  23132. end;
  23133. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23134. begin
  23135. StartProgram(false);
  23136. Add([
  23137. 'type',
  23138. ' TObject = class',
  23139. ' procedure Fly;',
  23140. ' end;',
  23141. ' TObjHelper = class helper for TObject',
  23142. ' procedure Fly;',
  23143. ' end;',
  23144. ' TBird = class',
  23145. ' procedure Fly;',
  23146. ' end;',
  23147. ' TBirdHelper = class helper for TBird',
  23148. ' procedure Fly;',
  23149. ' procedure Walk(w: word);',
  23150. ' end;',
  23151. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23152. ' procedure Fly;',
  23153. ' procedure Walk(w: word);',
  23154. ' end;',
  23155. 'procedure Tobject.fly;',
  23156. 'begin',
  23157. ' inherited;', // ignore
  23158. 'end;',
  23159. 'procedure Tobjhelper.fly;',
  23160. 'begin',
  23161. ' {@TObject_Fly}inherited;',
  23162. ' inherited {@TObject_Fly}Fly;',
  23163. 'end;',
  23164. 'procedure Tbird.fly;',
  23165. 'begin',
  23166. ' {@TObjHelper_Fly}inherited;',
  23167. ' inherited {@TObjHelper_Fly}Fly;',
  23168. 'end;',
  23169. 'procedure Tbirdhelper.fly;',
  23170. 'begin',
  23171. ' {@TBird_Fly}inherited;',
  23172. ' inherited {@TBird_Fly}Fly;',
  23173. 'end;',
  23174. 'procedure Tbirdhelper.walk(w: word);',
  23175. 'begin',
  23176. 'end;',
  23177. 'procedure teagleHelper.fly;',
  23178. 'begin',
  23179. ' {@TBird_Fly}inherited;',
  23180. ' inherited {@TBird_Fly}Fly;',
  23181. 'end;',
  23182. 'procedure teagleHelper.walk(w: word);',
  23183. 'begin',
  23184. ' {@TBirdHelper_Walk}inherited;',
  23185. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23186. 'end;',
  23187. 'begin',
  23188. '']);
  23189. ConvertProgram;
  23190. CheckSource('TestClassHelper_InheritedObjFPC',
  23191. LinesToStr([ // statements
  23192. 'rtl.createClass(this, "TObject", null, function () {',
  23193. ' this.$init = function () {',
  23194. ' };',
  23195. ' this.$final = function () {',
  23196. ' };',
  23197. ' this.Fly = function () {',
  23198. ' };',
  23199. '});',
  23200. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23201. ' this.Fly = function () {',
  23202. ' $mod.TObject.Fly.call(this);',
  23203. ' $mod.TObject.Fly.call(this);',
  23204. ' };',
  23205. '});',
  23206. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23207. ' this.Fly$1 = function () {',
  23208. ' $mod.TObjHelper.Fly.call(this);',
  23209. ' $mod.TObjHelper.Fly.call(this);',
  23210. ' };',
  23211. '});',
  23212. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23213. ' this.Fly = function () {',
  23214. ' $mod.TBird.Fly$1.call(this);',
  23215. ' $mod.TBird.Fly$1.call(this);',
  23216. ' };',
  23217. ' this.Walk = function (w) {',
  23218. ' };',
  23219. '});',
  23220. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23221. ' this.Fly$1 = function () {',
  23222. ' $mod.TBird.Fly$1.call(this);',
  23223. ' $mod.TBird.Fly$1.call(this);',
  23224. ' };',
  23225. ' this.Walk$1 = function (w) {',
  23226. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23227. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23228. ' };',
  23229. '});',
  23230. '']),
  23231. LinesToStr([ // $mod.$main
  23232. '']));
  23233. end;
  23234. procedure TTestModule.TestClassHelper_Property;
  23235. begin
  23236. StartProgram(false);
  23237. Add([
  23238. 'type',
  23239. ' TObject = class',
  23240. ' FSize: word;',
  23241. ' function GetSpeed: word;',
  23242. ' procedure SetSpeed(Value: word);',
  23243. ' end;',
  23244. ' TObjHelper = class helper for TObject',
  23245. ' function GetLeft: word;',
  23246. ' procedure SetLeft(Value: word);',
  23247. ' property Size: word read FSize write FSize;',
  23248. ' property Speed: word read GetSpeed write SetSpeed;',
  23249. ' property Left: word read GetLeft write SetLeft;',
  23250. ' end;',
  23251. ' TBird = class',
  23252. ' property NotRight: word read GetLeft write SetLeft;',
  23253. ' procedure DoIt;',
  23254. ' end;',
  23255. 'var',
  23256. ' b: TBird;',
  23257. 'function Tobject.GetSpeed: word;',
  23258. 'begin',
  23259. ' Size:=Size+11;',
  23260. ' Speed:=Speed+12;',
  23261. ' Result:=Left+13;',
  23262. ' Left:=13;',
  23263. ' Left:=Left+13;',
  23264. ' Self.Size:=Self.Size+21;',
  23265. ' Self.Speed:=Self.Speed+22;',
  23266. ' Self.Left:=Self.Left+23;',
  23267. ' with Self do begin',
  23268. ' Size:=Size+31;',
  23269. ' Speed:=Speed+32;',
  23270. ' Left:=Left+33;',
  23271. ' end;',
  23272. 'end;',
  23273. 'procedure Tobject.SetSpeed(Value: word);',
  23274. 'begin',
  23275. 'end;',
  23276. 'function TObjHelper.GetLeft: word;',
  23277. 'begin',
  23278. ' Size:=Size+11;',
  23279. ' Speed:=Speed+12;',
  23280. ' Left:=Left+13;',
  23281. ' Self.Size:=Self.Size+21;',
  23282. ' Self.Speed:=Self.Speed+22;',
  23283. ' Self.Left:=Self.Left+23;',
  23284. ' with Self do begin',
  23285. ' Size:=Size+31;',
  23286. ' Speed:=Speed+32;',
  23287. ' Left:=Left+33;',
  23288. ' end;',
  23289. 'end;',
  23290. 'procedure TObjHelper.SetLeft(Value: word);',
  23291. 'begin',
  23292. 'end;',
  23293. 'procedure TBird.DoIt;',
  23294. 'begin',
  23295. ' NotRight:=NotRight+11;',
  23296. ' Self.NotRight:=Self.NotRight+21;',
  23297. ' with Self do begin',
  23298. ' NotRight:=NotRight+31;',
  23299. ' end;',
  23300. 'end;',
  23301. 'begin',
  23302. ' b.Size:=b.Size+11;',
  23303. ' b.Speed:=b.Speed+12;',
  23304. ' b.Left:=b.Left+13;',
  23305. ' b.NotRight:=b.NotRight+14;',
  23306. ' with b do begin',
  23307. ' Size:=Size+31;',
  23308. ' Speed:=Speed+32;',
  23309. ' Left:=Left+33;',
  23310. ' NotRight:=NotRight+34;',
  23311. ' end;',
  23312. '']);
  23313. ConvertProgram;
  23314. CheckSource('TestClassHelper_Property',
  23315. LinesToStr([ // statements
  23316. 'rtl.createClass(this, "TObject", null, function () {',
  23317. ' this.$init = function () {',
  23318. ' this.FSize = 0;',
  23319. ' };',
  23320. ' this.$final = function () {',
  23321. ' };',
  23322. ' this.GetSpeed = function () {',
  23323. ' var Result = 0;',
  23324. ' this.FSize = this.FSize + 11;',
  23325. ' this.SetSpeed(this.GetSpeed() + 12);',
  23326. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23327. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23328. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23329. ' this.FSize = this.FSize + 21;',
  23330. ' this.SetSpeed(this.GetSpeed() + 22);',
  23331. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23332. ' this.FSize = this.FSize + 31;',
  23333. ' this.SetSpeed(this.GetSpeed() + 32);',
  23334. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23335. ' return Result;',
  23336. ' };',
  23337. ' this.SetSpeed = function (Value) {',
  23338. ' };',
  23339. '});',
  23340. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23341. ' this.GetLeft = function () {',
  23342. ' var Result = 0;',
  23343. ' this.FSize = this.FSize + 11;',
  23344. ' this.SetSpeed(this.GetSpeed() + 12);',
  23345. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23346. ' this.FSize = this.FSize + 21;',
  23347. ' this.SetSpeed(this.GetSpeed() + 22);',
  23348. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23349. ' this.FSize = this.FSize + 31;',
  23350. ' this.SetSpeed(this.GetSpeed() + 32);',
  23351. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23352. ' return Result;',
  23353. ' };',
  23354. ' this.SetLeft = function (Value) {',
  23355. ' };',
  23356. '});',
  23357. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23358. ' this.DoIt = function () {',
  23359. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23360. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23361. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23362. ' };',
  23363. '});',
  23364. 'this.b = null;',
  23365. '']),
  23366. LinesToStr([ // $mod.$main
  23367. '$mod.b.FSize = $mod.b.FSize + 11;',
  23368. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23369. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23370. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23371. 'var $with = $mod.b;',
  23372. '$with.FSize = $with.FSize + 31;',
  23373. '$with.SetSpeed($with.GetSpeed() + 32);',
  23374. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23375. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23376. '']));
  23377. end;
  23378. procedure TTestModule.TestClassHelper_Property_Array;
  23379. begin
  23380. StartProgram(false);
  23381. Add([
  23382. 'type',
  23383. ' TObject = class',
  23384. ' function GetSpeed(Index: boolean): word;',
  23385. ' procedure SetSpeed(Index: boolean; Value: word);',
  23386. ' end;',
  23387. ' TObjHelper = class helper for TObject',
  23388. ' function GetSize(Index: boolean): word;',
  23389. ' procedure SetSize(Index: boolean; Value: word);',
  23390. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23391. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23392. ' end;',
  23393. ' TBird = class',
  23394. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23395. ' procedure DoIt;',
  23396. ' end;',
  23397. 'var',
  23398. ' b: TBird;',
  23399. 'function Tobject.GetSpeed(Index: boolean): word;',
  23400. 'begin',
  23401. ' Result:=Size[false];',
  23402. ' Size[true]:=Size[false]+11;',
  23403. ' Speed[true]:=Speed[false]+12;',
  23404. ' Self.Size[true]:=Self.Size[false]+21;',
  23405. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23406. ' with Self do begin',
  23407. ' Size[true]:=Size[false]+31;',
  23408. ' Speed[true]:=Speed[false]+32;',
  23409. ' end;',
  23410. 'end;',
  23411. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23412. 'begin',
  23413. 'end;',
  23414. 'function TObjHelper.GetSize(Index: boolean): word;',
  23415. 'begin',
  23416. ' Size[true]:=Size[false]+11;',
  23417. ' Speed[true]:=Speed[false]+12;',
  23418. ' Self.Size[true]:=Self.Size[false]+21;',
  23419. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23420. ' with Self do begin',
  23421. ' Size[true]:=Size[false]+31;',
  23422. ' Speed[true]:=Speed[false]+32;',
  23423. ' end;',
  23424. 'end;',
  23425. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23426. 'begin',
  23427. 'end;',
  23428. 'procedure TBird.DoIt;',
  23429. 'begin',
  23430. ' Items[true]:=Items[false]+11;',
  23431. ' Self.Items[true]:=Self.Items[false]+21;',
  23432. ' with Self do Items[true]:=Items[false]+31;',
  23433. 'end;',
  23434. 'begin',
  23435. ' b.Size[true]:=b.Size[false]+11;',
  23436. ' b.Speed[true]:=b.Speed[false]+12;',
  23437. ' b.Items[true]:=b.Items[false]+13;',
  23438. ' with b do begin',
  23439. ' Size[true]:=Size[false]+21;',
  23440. ' Speed[true]:=Speed[false]+22;',
  23441. ' Items[true]:=Items[false]+23;',
  23442. ' end;',
  23443. '']);
  23444. ConvertProgram;
  23445. CheckSource('TestClassHelper_Property_Array',
  23446. LinesToStr([ // statements
  23447. 'rtl.createClass(this, "TObject", null, function () {',
  23448. ' this.$init = function () {',
  23449. ' };',
  23450. ' this.$final = function () {',
  23451. ' };',
  23452. ' this.GetSpeed = function (Index) {',
  23453. ' var Result = 0;',
  23454. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23455. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23456. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23457. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23458. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23459. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23460. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23461. ' return Result;',
  23462. ' };',
  23463. ' this.SetSpeed = function (Index, Value) {',
  23464. ' };',
  23465. '});',
  23466. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23467. ' this.GetSize = function (Index) {',
  23468. ' var Result = 0;',
  23469. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23470. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23471. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23472. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23473. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23474. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23475. ' return Result;',
  23476. ' };',
  23477. ' this.SetSize = function (Index, Value) {',
  23478. ' };',
  23479. '});',
  23480. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23481. ' this.DoIt = function () {',
  23482. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23483. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23484. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23485. ' };',
  23486. '});',
  23487. 'this.b = null;',
  23488. '']),
  23489. LinesToStr([ // $mod.$main
  23490. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23491. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23492. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23493. 'var $with = $mod.b;',
  23494. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23495. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23496. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23497. '']));
  23498. end;
  23499. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23500. begin
  23501. StartProgram(false);
  23502. Add([
  23503. 'type',
  23504. ' TObject = class',
  23505. ' function GetSpeed(Index: boolean): word;',
  23506. ' procedure SetSpeed(Index: boolean; Value: word);',
  23507. ' end;',
  23508. ' TObjHelper = class helper for TObject',
  23509. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23510. ' end;',
  23511. ' TBird = class',
  23512. ' end;',
  23513. ' TBirdHelper = class helper for TBird',
  23514. ' function GetSize(Index: word): boolean;',
  23515. ' procedure SetSize(Index: word; Value: boolean);',
  23516. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23517. ' end;',
  23518. 'function Tobject.GetSpeed(Index: boolean): word;',
  23519. 'begin',
  23520. ' Self[true]:=Self[false]+1;',
  23521. 'end;',
  23522. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23523. 'begin',
  23524. 'end;',
  23525. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23526. 'begin',
  23527. ' Self[1]:=not Self[2];',
  23528. 'end;',
  23529. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23530. 'begin',
  23531. 'end;',
  23532. 'var',
  23533. ' o: TObject;',
  23534. ' b: TBird;',
  23535. 'begin',
  23536. ' o[true]:=o[false]+1;',
  23537. ' b[3]:=not b[4];',
  23538. '']);
  23539. ConvertProgram;
  23540. CheckSource('TestClassHelper_Property_Array_Default',
  23541. LinesToStr([ // statements
  23542. 'rtl.createClass(this, "TObject", null, function () {',
  23543. ' this.$init = function () {',
  23544. ' };',
  23545. ' this.$final = function () {',
  23546. ' };',
  23547. ' this.GetSpeed = function (Index) {',
  23548. ' var Result = 0;',
  23549. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23550. ' return Result;',
  23551. ' };',
  23552. ' this.SetSpeed = function (Index, Value) {',
  23553. ' };',
  23554. '});',
  23555. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23556. '});',
  23557. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23558. '});',
  23559. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23560. ' this.GetSize = function (Index) {',
  23561. ' var Result = false;',
  23562. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23563. ' return Result;',
  23564. ' };',
  23565. ' this.SetSize = function (Index, Value) {',
  23566. ' };',
  23567. '});',
  23568. 'this.o = null;',
  23569. 'this.b = null;',
  23570. '']),
  23571. LinesToStr([ // $mod.$main
  23572. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23573. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23574. '']));
  23575. end;
  23576. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23577. begin
  23578. StartProgram(false);
  23579. Add([
  23580. 'type',
  23581. ' TObject = class',
  23582. ' end;',
  23583. ' TObjHelper = class helper for TObject',
  23584. ' function GetItems(Index: word): TObject;',
  23585. ' procedure SetItems(Index: word; Value: TObject);',
  23586. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23587. ' end;',
  23588. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23589. 'begin',
  23590. ' Self[1][2]:=Self[3][4];',
  23591. 'end;',
  23592. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23593. 'begin',
  23594. 'end;',
  23595. 'var',
  23596. ' o: TObject;',
  23597. 'begin',
  23598. ' o[1][2]:=o[3][4];',
  23599. '']);
  23600. ConvertProgram;
  23601. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23602. LinesToStr([ // statements
  23603. 'rtl.createClass(this, "TObject", null, function () {',
  23604. ' this.$init = function () {',
  23605. ' };',
  23606. ' this.$final = function () {',
  23607. ' };',
  23608. '});',
  23609. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23610. ' this.GetItems = function (Index) {',
  23611. ' var Result = null;',
  23612. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23613. ' return Result;',
  23614. ' };',
  23615. ' this.SetItems = function (Index, Value) {',
  23616. ' };',
  23617. '});',
  23618. 'this.o = null;',
  23619. '']),
  23620. LinesToStr([ // $mod.$main
  23621. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23622. '']));
  23623. end;
  23624. procedure TTestModule.TestClassHelper_ClassProperty;
  23625. begin
  23626. StartProgram(false);
  23627. Add([
  23628. 'type',
  23629. ' TObject = class',
  23630. ' class var FSize: word;',
  23631. ' class function GetSpeed: word;',
  23632. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23633. ' end;',
  23634. ' TObjHelper = class helper for TObject',
  23635. ' class function GetLeft: word;',
  23636. ' class procedure SetLeft(Value: word);',
  23637. ' class property Size: word read FSize write FSize;',
  23638. ' class property Speed: word read GetSpeed write SetSpeed;',
  23639. ' class property Left: word read GetLeft write SetLeft;',
  23640. ' end;',
  23641. ' TBird = class',
  23642. ' class property NotRight: word read GetLeft write SetLeft;',
  23643. ' class procedure DoIt;',
  23644. ' end;',
  23645. ' TBirdClass = class of TBird;',
  23646. 'class function Tobject.GetSpeed: word;',
  23647. 'begin',
  23648. ' Size:=Size+11;',
  23649. ' Speed:=Speed+12;',
  23650. ' Left:=Left+13;',
  23651. ' Self.Size:=Self.Size+21;',
  23652. ' Self.Speed:=Self.Speed+22;',
  23653. ' Self.Left:=Self.Left+23;',
  23654. ' with Self do begin',
  23655. ' Size:=Size+31;',
  23656. ' Speed:=Speed+32;',
  23657. ' Left:=Left+33;',
  23658. ' end;',
  23659. 'end;',
  23660. 'class function TObjHelper.GetLeft: word;',
  23661. 'begin',
  23662. ' Size:=Size+11;',
  23663. ' Speed:=Speed+12;',
  23664. ' Left:=Left+13;',
  23665. ' Self.Size:=Self.Size+21;',
  23666. ' Self.Speed:=Self.Speed+22;',
  23667. ' Self.Left:=Self.Left+23;',
  23668. ' with Self do begin',
  23669. ' Size:=Size+31;',
  23670. ' Speed:=Speed+32;',
  23671. ' Left:=Left+33;',
  23672. ' end;',
  23673. 'end;',
  23674. 'class procedure TObjHelper.SetLeft(Value: word);',
  23675. 'begin',
  23676. 'end;',
  23677. 'class procedure TBird.DoIt;',
  23678. 'begin',
  23679. ' NotRight:=NotRight+11;',
  23680. ' Self.NotRight:=Self.NotRight+21;',
  23681. ' with Self do NotRight:=NotRight+31;',
  23682. 'end;',
  23683. 'var',
  23684. ' b: TBird;',
  23685. ' c: TBirdClass;',
  23686. 'begin',
  23687. ' b.Size:=b.Size+11;',
  23688. ' b.Speed:=b.Speed+12;',
  23689. ' b.Left:=b.Left+13;',
  23690. ' b.NotRight:=b.NotRight+14;',
  23691. ' with b do begin',
  23692. ' Size:=Size+31;',
  23693. ' Speed:=Speed+32;',
  23694. ' Left:=Left+33;',
  23695. ' NotRight:=NotRight+34;',
  23696. ' end;',
  23697. ' c.Size:=c.Size+11;',
  23698. ' c.Speed:=c.Speed+12;',
  23699. ' c.Left:=c.Left+13;',
  23700. ' c.NotRight:=c.NotRight+14;',
  23701. ' with c do begin',
  23702. ' Size:=Size+31;',
  23703. ' Speed:=Speed+32;',
  23704. ' Left:=Left+33;',
  23705. ' NotRight:=NotRight+34;',
  23706. ' end;',
  23707. ' tbird.Size:=tbird.Size+11;',
  23708. ' tbird.Speed:=tbird.Speed+12;',
  23709. ' tbird.Left:=tbird.Left+13;',
  23710. ' tbird.NotRight:=tbird.NotRight+14;',
  23711. ' with tbird do begin',
  23712. ' Size:=Size+31;',
  23713. ' Speed:=Speed+32;',
  23714. ' Left:=Left+33;',
  23715. ' NotRight:=NotRight+34;',
  23716. ' end;',
  23717. '']);
  23718. ConvertProgram;
  23719. CheckSource('TestClassHelper_ClassProperty',
  23720. LinesToStr([ // statements
  23721. 'rtl.createClass(this, "TObject", null, function () {',
  23722. ' this.FSize = 0;',
  23723. ' this.$init = function () {',
  23724. ' };',
  23725. ' this.$final = function () {',
  23726. ' };',
  23727. ' this.GetSpeed = function () {',
  23728. ' var Result = 0;',
  23729. ' $mod.TObject.FSize = this.FSize + 11;',
  23730. ' this.SetSpeed(this.GetSpeed() + 12);',
  23731. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23732. ' $mod.TObject.FSize = this.FSize + 21;',
  23733. ' this.SetSpeed(this.GetSpeed() + 22);',
  23734. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23735. ' $mod.TObject.FSize = this.FSize + 31;',
  23736. ' this.SetSpeed(this.GetSpeed() + 32);',
  23737. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23738. ' return Result;',
  23739. ' };',
  23740. '});',
  23741. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23742. ' this.GetLeft = function () {',
  23743. ' var Result = 0;',
  23744. ' $mod.TObject.FSize = this.FSize + 11;',
  23745. ' this.SetSpeed(this.GetSpeed() + 12);',
  23746. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23747. ' $mod.TObject.FSize = this.FSize + 21;',
  23748. ' this.SetSpeed(this.GetSpeed() + 22);',
  23749. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23750. ' $mod.TObject.FSize = this.FSize + 31;',
  23751. ' this.SetSpeed(this.GetSpeed() + 32);',
  23752. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23753. ' return Result;',
  23754. ' };',
  23755. ' this.SetLeft = function (Value) {',
  23756. ' };',
  23757. '});',
  23758. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23759. ' this.DoIt = function () {',
  23760. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23761. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23762. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23763. ' };',
  23764. '});',
  23765. 'this.b = null;',
  23766. 'this.c = null;',
  23767. '']),
  23768. LinesToStr([ // $mod.$main
  23769. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23770. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23771. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23772. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23773. 'var $with = $mod.b;',
  23774. '$mod.TObject.FSize = $with.FSize + 31;',
  23775. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23776. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23777. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23778. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23779. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23780. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23781. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23782. 'var $with1 = $mod.c;',
  23783. '$mod.TObject.FSize = $with1.FSize + 31;',
  23784. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23785. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23786. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23787. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23788. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23789. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23790. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23791. 'var $with2 = $mod.TBird;',
  23792. '$mod.TObject.FSize = $with2.FSize + 31;',
  23793. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23794. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23795. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23796. '']));
  23797. end;
  23798. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23799. begin
  23800. StartProgram(false);
  23801. Add([
  23802. 'type',
  23803. ' TObject = class',
  23804. ' class function GetSpeed: word; static;',
  23805. ' class procedure SetSpeed(Value: word); static;',
  23806. ' end;',
  23807. ' TObjHelper = class helper for TObject',
  23808. ' class function GetLeft: word; static;',
  23809. ' class procedure SetLeft(Value: word); static;',
  23810. ' class property Speed: word read GetSpeed write SetSpeed;',
  23811. ' class property Left: word read GetLeft write SetLeft;',
  23812. ' end;',
  23813. ' TBird = class',
  23814. ' class property NotRight: word read GetLeft write SetLeft;',
  23815. ' class procedure DoIt; static;',
  23816. ' class procedure DoSome;',
  23817. ' end;',
  23818. ' TBirdClass = class of TBird;',
  23819. 'class function Tobject.GetSpeed: word;',
  23820. 'begin',
  23821. ' Speed:=Speed+12;',
  23822. ' Left:=Left+13;',
  23823. 'end;',
  23824. 'class procedure TObject.SetSpeed(Value: word);',
  23825. 'begin',
  23826. 'end;',
  23827. 'class function TObjHelper.GetLeft: word;',
  23828. 'begin',
  23829. ' Speed:=Speed+12;',
  23830. ' Left:=Left+13;',
  23831. 'end;',
  23832. 'class procedure TObjHelper.SetLeft(Value: word);',
  23833. 'begin',
  23834. 'end;',
  23835. 'class procedure TBird.DoIt;',
  23836. 'begin',
  23837. ' NotRight:=NotRight+11;',
  23838. 'end;',
  23839. 'class procedure TBird.DoSome;',
  23840. 'begin',
  23841. ' Speed:=Speed+12;',
  23842. ' Left:=Left+13;',
  23843. ' Self.Speed:=Self.Speed+22;',
  23844. ' Self.Left:=Self.Left+23;',
  23845. ' with Self do begin',
  23846. ' Speed:=Speed+32;',
  23847. ' Left:=Left+33;',
  23848. ' end;',
  23849. ' NotRight:=NotRight+11;',
  23850. ' Self.NotRight:=Self.NotRight+21;',
  23851. ' with Self do NotRight:=NotRight+31;',
  23852. 'end;',
  23853. 'var',
  23854. ' b: TBird;',
  23855. ' c: TBirdClass;',
  23856. 'begin',
  23857. ' b.Speed:=b.Speed+12;',
  23858. ' b.Left:=b.Left+13;',
  23859. ' b.NotRight:=b.NotRight+14;',
  23860. ' with b do begin',
  23861. ' Speed:=Speed+32;',
  23862. ' Left:=Left+33;',
  23863. ' NotRight:=NotRight+34;',
  23864. ' end;',
  23865. ' c.Speed:=c.Speed+12;',
  23866. ' c.Left:=c.Left+13;',
  23867. ' c.NotRight:=c.NotRight+14;',
  23868. ' with c do begin',
  23869. ' Speed:=Speed+32;',
  23870. ' Left:=Left+33;',
  23871. ' NotRight:=NotRight+34;',
  23872. ' end;',
  23873. ' tbird.Speed:=tbird.Speed+12;',
  23874. ' tbird.Left:=tbird.Left+13;',
  23875. ' tbird.NotRight:=tbird.NotRight+14;',
  23876. ' with tbird do begin',
  23877. ' Speed:=Speed+32;',
  23878. ' Left:=Left+33;',
  23879. ' NotRight:=NotRight+34;',
  23880. ' end;',
  23881. '']);
  23882. ConvertProgram;
  23883. CheckSource('TestClassHelper_ClassPropertyStatic',
  23884. LinesToStr([ // statements
  23885. 'rtl.createClass(this, "TObject", null, function () {',
  23886. ' this.$init = function () {',
  23887. ' };',
  23888. ' this.$final = function () {',
  23889. ' };',
  23890. ' this.GetSpeed = function () {',
  23891. ' var Result = 0;',
  23892. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23893. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23894. ' return Result;',
  23895. ' };',
  23896. ' this.SetSpeed = function (Value) {',
  23897. ' };',
  23898. '});',
  23899. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23900. ' this.GetLeft = function () {',
  23901. ' var Result = 0;',
  23902. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23903. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23904. ' return Result;',
  23905. ' };',
  23906. ' this.SetLeft = function (Value) {',
  23907. ' };',
  23908. '});',
  23909. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23910. ' this.DoIt = function () {',
  23911. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23912. ' };',
  23913. ' this.DoSome = function () {',
  23914. ' this.SetSpeed(this.GetSpeed() + 12);',
  23915. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23916. ' this.SetSpeed(this.GetSpeed() + 22);',
  23917. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23918. ' this.SetSpeed(this.GetSpeed() + 32);',
  23919. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23920. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23921. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23922. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23923. ' };',
  23924. '});',
  23925. 'this.b = null;',
  23926. 'this.c = null;',
  23927. '']),
  23928. LinesToStr([ // $mod.$main
  23929. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23930. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23931. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23932. 'var $with = $mod.b;',
  23933. '$with.SetSpeed($with.GetSpeed() + 32);',
  23934. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23935. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23936. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23937. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23938. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23939. 'var $with1 = $mod.c;',
  23940. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23941. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23942. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23943. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23944. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23945. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23946. 'var $with2 = $mod.TBird;',
  23947. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23948. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23949. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23950. '']));
  23951. end;
  23952. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23953. begin
  23954. StartProgram(false);
  23955. Add([
  23956. 'type',
  23957. ' TObject = class',
  23958. ' class function GetSpeed(Index: boolean): word;',
  23959. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23960. ' end;',
  23961. ' TObjHelper = class helper for TObject',
  23962. ' class function GetSize(Index: boolean): word;',
  23963. ' class procedure SetSize(Index: boolean; Value: word);',
  23964. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23965. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23966. ' end;',
  23967. ' TBird = class',
  23968. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23969. ' class procedure DoIt;',
  23970. ' end;',
  23971. ' TBirdClass = class of TBird;',
  23972. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23973. 'begin',
  23974. ' Size[true]:=Size[false]+11;',
  23975. ' Speed[true]:=Speed[false]+12;',
  23976. ' Self.Size[true]:=Self.Size[false]+21;',
  23977. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23978. ' with Self do begin',
  23979. ' Size[true]:=Size[false]+31;',
  23980. ' Speed[true]:=Speed[false]+32;',
  23981. ' end;',
  23982. 'end;',
  23983. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23984. 'begin',
  23985. ' Size[true]:=Size[false]+11;',
  23986. ' Speed[true]:=Speed[false]+12;',
  23987. ' Self.Size[true]:=Self.Size[false]+21;',
  23988. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23989. ' with Self do begin',
  23990. ' Size[true]:=Size[false]+31;',
  23991. ' Speed[true]:=Speed[false]+32;',
  23992. ' end;',
  23993. 'end;',
  23994. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23995. 'begin',
  23996. 'end;',
  23997. 'class procedure TBird.DoIt;',
  23998. 'begin',
  23999. ' Items[true]:=Items[false]+11;',
  24000. ' Self.Items[true]:=Self.Items[false]+21;',
  24001. ' with Self do Items[true]:=Items[false]+31;',
  24002. 'end;',
  24003. 'var',
  24004. ' b: TBird;',
  24005. ' c: TBirdClass;',
  24006. 'begin',
  24007. ' b.Size[true]:=b.Size[false]+11;',
  24008. ' b.Speed[true]:=b.Speed[false]+12;',
  24009. ' b.Items[true]:=b.Items[false]+13;',
  24010. ' with b do begin',
  24011. ' Size[true]:=Size[false]+21;',
  24012. ' Speed[true]:=Speed[false]+22;',
  24013. ' Items[true]:=Items[false]+23;',
  24014. ' end;',
  24015. ' c.Size[true]:=c.Size[false]+11;',
  24016. ' c.Speed[true]:=c.Speed[false]+12;',
  24017. ' c.Items[true]:=c.Items[false]+13;',
  24018. ' with c do begin',
  24019. ' Size[true]:=Size[false]+21;',
  24020. ' Speed[true]:=Speed[false]+22;',
  24021. ' Items[true]:=Items[false]+23;',
  24022. ' end;',
  24023. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24024. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24025. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24026. ' with TBird do begin',
  24027. ' Size[true]:=Size[false]+21;',
  24028. ' Speed[true]:=Speed[false]+22;',
  24029. ' Items[true]:=Items[false]+23;',
  24030. ' end;',
  24031. '']);
  24032. ConvertProgram;
  24033. CheckSource('TestClassHelper_ClassProperty_Array',
  24034. LinesToStr([ // statements
  24035. 'rtl.createClass(this, "TObject", null, function () {',
  24036. ' this.$init = function () {',
  24037. ' };',
  24038. ' this.$final = function () {',
  24039. ' };',
  24040. ' this.GetSpeed = function (Index) {',
  24041. ' var Result = 0;',
  24042. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24043. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24044. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24045. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24046. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24047. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24048. ' return Result;',
  24049. ' };',
  24050. '});',
  24051. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24052. ' this.GetSize = function (Index) {',
  24053. ' var Result = 0;',
  24054. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24055. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24056. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24057. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24058. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24059. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24060. ' return Result;',
  24061. ' };',
  24062. ' this.SetSize = function (Index, Value) {',
  24063. ' };',
  24064. '});',
  24065. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24066. ' this.DoIt = function () {',
  24067. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24068. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24069. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24070. ' };',
  24071. '});',
  24072. 'this.b = null;',
  24073. 'this.c = null;',
  24074. '']),
  24075. LinesToStr([ // $mod.$main
  24076. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24077. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24078. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24079. 'var $with = $mod.b;',
  24080. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24081. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24082. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24083. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24084. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24085. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24086. 'var $with1 = $mod.c;',
  24087. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24088. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24089. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24090. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24091. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24092. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24093. 'var $with2 = $mod.TBird;',
  24094. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24095. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24096. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24097. '']));
  24098. end;
  24099. procedure TTestModule.TestClassHelper_ForIn;
  24100. begin
  24101. StartProgram(false);
  24102. Add([
  24103. 'type',
  24104. ' TObject = class end;',
  24105. ' TItem = TObject;',
  24106. ' TEnumerator = class',
  24107. ' FCurrent: TItem;',
  24108. ' property Current: TItem read FCurrent;',
  24109. ' function MoveNext: boolean;',
  24110. ' end;',
  24111. ' TBird = class',
  24112. ' end;',
  24113. ' TBirdHelper = class helper for TBird',
  24114. ' function GetEnumerator: TEnumerator;',
  24115. ' end;',
  24116. 'function TEnumerator.MoveNext: boolean;',
  24117. 'begin',
  24118. 'end;',
  24119. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24120. 'begin',
  24121. 'end;',
  24122. 'var',
  24123. ' b: TBird;',
  24124. ' i, i2: TItem;',
  24125. 'begin',
  24126. ' for i in b do i2:=i;']);
  24127. ConvertProgram;
  24128. CheckSource('TestClassHelper_ForIn',
  24129. LinesToStr([ // statements
  24130. 'rtl.createClass(this, "TObject", null, function () {',
  24131. ' this.$init = function () {',
  24132. ' };',
  24133. ' this.$final = function () {',
  24134. ' };',
  24135. '});',
  24136. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24137. ' this.$init = function () {',
  24138. ' $mod.TObject.$init.call(this);',
  24139. ' this.FCurrent = null;',
  24140. ' };',
  24141. ' this.$final = function () {',
  24142. ' this.FCurrent = undefined;',
  24143. ' $mod.TObject.$final.call(this);',
  24144. ' };',
  24145. ' this.MoveNext = function () {',
  24146. ' var Result = false;',
  24147. ' return Result;',
  24148. ' };',
  24149. '});',
  24150. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24151. '});',
  24152. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24153. ' this.GetEnumerator = function () {',
  24154. ' var Result = null;',
  24155. ' return Result;',
  24156. ' };',
  24157. '});',
  24158. 'this.b = null;',
  24159. 'this.i = null;',
  24160. 'this.i2 = null;'
  24161. ]),
  24162. LinesToStr([ // $mod.$main
  24163. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24164. 'try {',
  24165. ' while ($in.MoveNext()){',
  24166. ' $mod.i = $in.FCurrent;',
  24167. ' $mod.i2 = $mod.i;',
  24168. ' }',
  24169. '} finally {',
  24170. ' $in = rtl.freeLoc($in)',
  24171. '};',
  24172. '']));
  24173. end;
  24174. procedure TTestModule.TestClassHelper_PassProperty;
  24175. begin
  24176. StartProgram(false);
  24177. Add([
  24178. 'type',
  24179. ' TObject = class',
  24180. ' FField: TObject;',
  24181. ' property Field: TObject read FField write FField;',
  24182. ' end;',
  24183. ' THelper = class helper for TObject',
  24184. ' procedure Fly;',
  24185. ' class procedure Run;',
  24186. ' class procedure Jump; static;',
  24187. ' end;',
  24188. 'procedure THelper.Fly;',
  24189. 'begin',
  24190. ' Field.Fly;',
  24191. ' Field.Run;',
  24192. ' Field.Jump;',
  24193. ' with Field do begin',
  24194. ' Fly;',
  24195. ' Run;',
  24196. ' Jump;',
  24197. ' end;',
  24198. 'end;',
  24199. 'class procedure THelper.Run;',
  24200. 'begin',
  24201. 'end;',
  24202. 'class procedure THelper.Jump;',
  24203. 'begin',
  24204. 'end;',
  24205. 'var',
  24206. ' b: TObject;',
  24207. 'begin',
  24208. ' b.Field.Fly;',
  24209. ' b.Field.Run;',
  24210. ' b.Field.Jump;',
  24211. ' with b do begin',
  24212. ' Field.Run;',
  24213. ' Field.Fly;',
  24214. ' Field.Jump;',
  24215. ' end;',
  24216. ' with b.Field do begin',
  24217. ' Run;',
  24218. ' Fly;',
  24219. ' Jump;',
  24220. ' end;',
  24221. '']);
  24222. ConvertProgram;
  24223. CheckSource('TestClassHelper_PassProperty',
  24224. LinesToStr([ // statements
  24225. 'rtl.createClass(this, "TObject", null, function () {',
  24226. ' this.$init = function () {',
  24227. ' this.FField = null;',
  24228. ' };',
  24229. ' this.$final = function () {',
  24230. ' this.FField = undefined;',
  24231. ' };',
  24232. '});',
  24233. 'rtl.createHelper(this, "THelper", null, function () {',
  24234. ' this.Fly = function () {',
  24235. ' $mod.THelper.Fly.call(this.FField);',
  24236. ' $mod.THelper.Run.call(this.FField.$class);',
  24237. ' $mod.THelper.Jump();',
  24238. ' var $with = this.FField;',
  24239. ' $mod.THelper.Fly.call($with);',
  24240. ' $mod.THelper.Run.call($with.$class);',
  24241. ' $mod.THelper.Jump();',
  24242. ' };',
  24243. ' this.Run = function () {',
  24244. ' };',
  24245. ' this.Jump = function () {',
  24246. ' };',
  24247. '});',
  24248. 'this.b = null;',
  24249. '']),
  24250. LinesToStr([ // $mod.$main
  24251. '$mod.THelper.Fly.call($mod.b.FField);',
  24252. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24253. '$mod.THelper.Jump();',
  24254. 'var $with = $mod.b;',
  24255. '$mod.THelper.Run.call($with.FField.$class);',
  24256. '$mod.THelper.Fly.call($with.FField);',
  24257. '$mod.THelper.Jump();',
  24258. 'var $with1 = $mod.b.FField;',
  24259. '$mod.THelper.Run.call($with1.$class);',
  24260. '$mod.THelper.Fly.call($with1);',
  24261. '$mod.THelper.Jump();',
  24262. '']));
  24263. end;
  24264. procedure TTestModule.TestExtClassHelper_ClassVar;
  24265. begin
  24266. StartProgram(false);
  24267. Add([
  24268. '{$modeswitch externalclass}',
  24269. 'type',
  24270. ' TExtA = class external name ''ExtObj''',
  24271. ' end;',
  24272. ' THelper = class helper for TExtA',
  24273. ' const',
  24274. ' One = 1;',
  24275. ' Two: word = 2;',
  24276. ' class var',
  24277. ' Glob: word;',
  24278. ' function Foo(w: word): word;',
  24279. ' class function Bar(w: word): word; static;',
  24280. ' end;',
  24281. 'function THelper.foo(w: word): word;',
  24282. 'begin',
  24283. ' Result:=w;',
  24284. ' Two:=One+w;',
  24285. ' Glob:=Glob;',
  24286. ' Result:=Self.Glob;',
  24287. ' Self.Glob:=Self.Glob;',
  24288. ' with Self do Glob:=Glob;',
  24289. 'end;',
  24290. 'class function THelper.bar(w: word): word;',
  24291. 'begin',
  24292. ' Result:=w;',
  24293. ' Two:=One;',
  24294. ' Glob:=Glob;',
  24295. 'end;',
  24296. 'var o: TExtA;',
  24297. 'begin',
  24298. ' texta.two:=texta.one;',
  24299. ' texta.Glob:=texta.Glob;',
  24300. ' with texta do begin',
  24301. ' two:=one;',
  24302. ' Glob:=Glob;',
  24303. ' end;',
  24304. ' o.two:=o.one;',
  24305. ' o.Glob:=o.Glob;',
  24306. ' with o do begin',
  24307. ' two:=one;',
  24308. ' Glob:=Glob;',
  24309. ' end;',
  24310. '']);
  24311. ConvertProgram;
  24312. CheckSource('TestExtClassHelper_ClassVar',
  24313. LinesToStr([ // statements
  24314. 'rtl.createHelper(this, "THelper", null, function () {',
  24315. ' this.One = 1;',
  24316. ' this.Two = 2;',
  24317. ' this.Glob = 0;',
  24318. ' this.Foo = function (w) {',
  24319. ' var Result = 0;',
  24320. ' Result = w;',
  24321. ' $mod.THelper.Two = 1 + w;',
  24322. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24323. ' Result = $mod.THelper.Glob;',
  24324. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24325. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24326. ' return Result;',
  24327. ' };',
  24328. ' this.Bar = function (w) {',
  24329. ' var Result = 0;',
  24330. ' Result = w;',
  24331. ' $mod.THelper.Two = 1;',
  24332. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24333. ' return Result;',
  24334. ' };',
  24335. '});',
  24336. 'this.o = null;',
  24337. '']),
  24338. LinesToStr([ // $mod.$main
  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. '$mod.THelper.Two = 1;',
  24344. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24345. 'var $with = $mod.o;',
  24346. '$mod.THelper.Two = 1;',
  24347. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24348. '']));
  24349. end;
  24350. procedure TTestModule.TestExtClassHelper_Method_Call;
  24351. begin
  24352. StartProgram(false);
  24353. Add([
  24354. '{$modeswitch externalclass}',
  24355. 'type',
  24356. ' TFly = function(w: word): word of object;',
  24357. ' TExtA = class external name ''ExtObj''',
  24358. ' procedure Run(w: word = 10);',
  24359. ' end;',
  24360. ' THelper = class helper for TExtA',
  24361. ' function Foo(w: word = 1): word;',
  24362. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24363. ' end;',
  24364. 'var p: TFly;',
  24365. 'function THelper.foo(w: word): word;',
  24366. 'begin',
  24367. ' Run;',
  24368. ' Run();',
  24369. ' Run(11);',
  24370. ' Foo;',
  24371. ' Foo();',
  24372. ' Foo(12);',
  24373. ' Self.Foo;',
  24374. ' Self.Foo();',
  24375. ' Self.Foo(13);',
  24376. ' Fly;',
  24377. ' Fly();',
  24378. ' with Self do begin',
  24379. ' Foo;',
  24380. ' Foo();',
  24381. ' Foo(14);',
  24382. ' Fly;',
  24383. ' Fly();',
  24384. ' end;',
  24385. ' p:=@Fly;',
  24386. 'end;',
  24387. 'var Obj: TExtA;',
  24388. 'begin',
  24389. ' obj.Foo;',
  24390. ' obj.Foo();',
  24391. ' obj.Foo(21);',
  24392. ' obj.Fly;',
  24393. ' obj.Fly();',
  24394. ' with obj do begin',
  24395. ' Foo;',
  24396. ' Foo();',
  24397. ' Foo(22);',
  24398. ' Fly;',
  24399. ' Fly();',
  24400. ' end;',
  24401. ' p:[email protected];',
  24402. '']);
  24403. ConvertProgram;
  24404. CheckSource('TestExtClassHelper_Method_Call',
  24405. LinesToStr([ // statements
  24406. 'rtl.createHelper(this, "THelper", null, function () {',
  24407. ' this.Foo = function (w) {',
  24408. ' var Result = 0;',
  24409. ' this.Run(10);',
  24410. ' this.Run(10);',
  24411. ' this.Run(11);',
  24412. ' $mod.THelper.Foo.call(this, 1);',
  24413. ' $mod.THelper.Foo.call(this, 1);',
  24414. ' $mod.THelper.Foo.call(this, 12);',
  24415. ' $mod.THelper.Foo.call(this, 1);',
  24416. ' $mod.THelper.Foo.call(this, 1);',
  24417. ' $mod.THelper.Foo.call(this, 13);',
  24418. ' this.Fly(2);',
  24419. ' this.Fly(2);',
  24420. ' $mod.THelper.Foo.call(this, 1);',
  24421. ' $mod.THelper.Foo.call(this, 1);',
  24422. ' $mod.THelper.Foo.call(this, 14);',
  24423. ' this.Fly(2);',
  24424. ' this.Fly(2);',
  24425. ' $mod.p = rtl.createCallback(this, "Fly");',
  24426. ' return Result;',
  24427. ' };',
  24428. '});',
  24429. 'this.p = null;',
  24430. 'this.Obj = null;',
  24431. '']),
  24432. LinesToStr([ // $mod.$main
  24433. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24434. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24435. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24436. '$mod.Obj.Fly(2);',
  24437. '$mod.Obj.Fly(2);',
  24438. 'var $with = $mod.Obj;',
  24439. '$mod.THelper.Foo.call($with, 1);',
  24440. '$mod.THelper.Foo.call($with, 1);',
  24441. '$mod.THelper.Foo.call($with, 22);',
  24442. '$with.Fly(2);',
  24443. '$with.Fly(2);',
  24444. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24445. '']));
  24446. end;
  24447. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24448. begin
  24449. StartProgram(false);
  24450. Add([
  24451. '{$modeswitch externalclass}',
  24452. 'type',
  24453. ' TExtA = class external name ''ExtObj''',
  24454. ' procedure Run(w: word = 10);',
  24455. ' end;',
  24456. ' THelper = class helper for TExtA',
  24457. ' class procedure Fly;',
  24458. ' end;',
  24459. 'class procedure THelper.Fly;',
  24460. 'begin end;',
  24461. 'begin',
  24462. '']);
  24463. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24464. nHelperClassMethodForExtClassMustBeStatic);
  24465. ConvertProgram;
  24466. end;
  24467. procedure TTestModule.TestRecordHelper_ClassVar;
  24468. begin
  24469. StartProgram(false);
  24470. Add([
  24471. 'type',
  24472. ' TRec = record',
  24473. ' end;',
  24474. ' THelper = record helper for TRec',
  24475. ' const',
  24476. ' One = 1;',
  24477. ' Two: word = 2;',
  24478. ' class var',
  24479. ' Glob: word;',
  24480. ' function Foo(w: word): word;',
  24481. ' class function Bar(w: word): word; static;',
  24482. ' end;',
  24483. 'function THelper.foo(w: word): word;',
  24484. 'begin',
  24485. ' Result:=w;',
  24486. ' Two:=One+w;',
  24487. ' Glob:=Glob;',
  24488. ' Result:=Self.Glob;',
  24489. ' Self.Glob:=Self.Glob;',
  24490. ' with Self do Glob:=Glob;',
  24491. ' Self:=Self;',
  24492. 'end;',
  24493. 'class function THelper.bar(w: word): word;',
  24494. 'begin',
  24495. ' Result:=w;',
  24496. ' Two:=One;',
  24497. ' Glob:=Glob;',
  24498. 'end;',
  24499. 'var r: TRec;',
  24500. 'begin',
  24501. ' trec.two:=trec.one;',
  24502. ' trec.Glob:=trec.Glob;',
  24503. ' with trec do begin',
  24504. ' two:=one;',
  24505. ' Glob:=Glob;',
  24506. ' end;',
  24507. ' r.two:=r.one;',
  24508. ' r.Glob:=r.Glob;',
  24509. ' with r do begin',
  24510. ' two:=one;',
  24511. ' Glob:=Glob;',
  24512. ' end;',
  24513. '']);
  24514. ConvertProgram;
  24515. CheckSource('TestRecordHelper_ClassVar',
  24516. LinesToStr([ // statements
  24517. 'rtl.recNewT(this, "TRec", function () {',
  24518. ' this.$eq = function (b) {',
  24519. ' return true;',
  24520. ' };',
  24521. ' this.$assign = function (s) {',
  24522. ' return this;',
  24523. ' };',
  24524. '});',
  24525. 'rtl.createHelper(this, "THelper", null, function () {',
  24526. ' this.One = 1;',
  24527. ' this.Two = 2;',
  24528. ' this.Glob = 0;',
  24529. ' this.Foo = function (w) {',
  24530. ' var Result = 0;',
  24531. ' Result = w;',
  24532. ' $mod.THelper.Two = 1 + w;',
  24533. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24534. ' Result = $mod.THelper.Glob;',
  24535. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24536. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24537. ' this.$assign(this);',
  24538. ' return Result;',
  24539. ' };',
  24540. ' this.Bar = function (w) {',
  24541. ' var Result = 0;',
  24542. ' Result = w;',
  24543. ' $mod.THelper.Two = 1;',
  24544. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24545. ' return Result;',
  24546. ' };',
  24547. '});',
  24548. 'this.r = this.TRec.$new();',
  24549. '']),
  24550. LinesToStr([ // $mod.$main
  24551. '$mod.THelper.Two = 1;',
  24552. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24553. 'var $with = $mod.TRec;',
  24554. '$mod.THelper.Two = 1;',
  24555. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24556. '$mod.THelper.Two = 1;',
  24557. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24558. 'var $with1 = $mod.r;',
  24559. '$mod.THelper.Two = 1;',
  24560. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24561. '']));
  24562. end;
  24563. procedure TTestModule.TestRecordHelper_Method_Call;
  24564. begin
  24565. StartProgram(false);
  24566. Add([
  24567. '{$modeswitch AdvancedRecords}',
  24568. 'type',
  24569. ' TRec = record',
  24570. ' procedure Run(w: word = 10);',
  24571. ' end;',
  24572. ' THelper = record helper for TRec',
  24573. ' function Foo(w: word = 1): word;',
  24574. ' end;',
  24575. 'procedure TRec.Run(w: word);',
  24576. 'begin',
  24577. ' Foo;',
  24578. ' Foo();',
  24579. ' Foo(2);',
  24580. ' Self.Foo;',
  24581. ' Self.Foo();',
  24582. ' Self.Foo(3);',
  24583. ' with Self do begin',
  24584. ' Foo;',
  24585. ' Foo();',
  24586. ' Foo(4);',
  24587. ' end;',
  24588. 'end;',
  24589. 'function THelper.foo(w: word): word;',
  24590. 'begin',
  24591. ' Run;',
  24592. ' Run();',
  24593. ' Run(11);',
  24594. ' Foo;',
  24595. ' Foo();',
  24596. ' Foo(12);',
  24597. ' Self.Foo;',
  24598. ' Self.Foo();',
  24599. ' Self.Foo(13);',
  24600. ' with Self do begin',
  24601. ' Foo;',
  24602. ' Foo();',
  24603. ' Foo(14);',
  24604. ' end;',
  24605. 'end;',
  24606. 'var Rec: TRec;',
  24607. 'begin',
  24608. ' Rec.Foo;',
  24609. ' Rec.Foo();',
  24610. ' Rec.Foo(21);',
  24611. ' with Rec do begin',
  24612. ' Foo;',
  24613. ' Foo();',
  24614. ' Foo(22);',
  24615. ' end;',
  24616. '']);
  24617. ConvertProgram;
  24618. CheckSource('TestRecordHelper_Method_Call',
  24619. LinesToStr([ // statements
  24620. 'rtl.recNewT(this, "TRec", function () {',
  24621. ' this.$eq = function (b) {',
  24622. ' return true;',
  24623. ' };',
  24624. ' this.$assign = function (s) {',
  24625. ' return this;',
  24626. ' };',
  24627. ' this.Run = function (w) {',
  24628. ' $mod.THelper.Foo.call(this, 1);',
  24629. ' $mod.THelper.Foo.call(this, 1);',
  24630. ' $mod.THelper.Foo.call(this, 2);',
  24631. ' $mod.THelper.Foo.call(this, 1);',
  24632. ' $mod.THelper.Foo.call(this, 1);',
  24633. ' $mod.THelper.Foo.call(this, 3);',
  24634. ' $mod.THelper.Foo.call(this, 1);',
  24635. ' $mod.THelper.Foo.call(this, 1);',
  24636. ' $mod.THelper.Foo.call(this, 4);',
  24637. ' };',
  24638. '});',
  24639. 'rtl.createHelper(this, "THelper", null, function () {',
  24640. ' this.Foo = function (w) {',
  24641. ' var Result = 0;',
  24642. ' this.Run(10);',
  24643. ' this.Run(10);',
  24644. ' this.Run(11);',
  24645. ' $mod.THelper.Foo.call(this, 1);',
  24646. ' $mod.THelper.Foo.call(this, 1);',
  24647. ' $mod.THelper.Foo.call(this, 12);',
  24648. ' $mod.THelper.Foo.call(this, 1);',
  24649. ' $mod.THelper.Foo.call(this, 1);',
  24650. ' $mod.THelper.Foo.call(this, 13);',
  24651. ' $mod.THelper.Foo.call(this, 1);',
  24652. ' $mod.THelper.Foo.call(this, 1);',
  24653. ' $mod.THelper.Foo.call(this, 14);',
  24654. ' return Result;',
  24655. ' };',
  24656. '});',
  24657. 'this.Rec = this.TRec.$new();',
  24658. '']),
  24659. LinesToStr([ // $mod.$main
  24660. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24661. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24662. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24663. 'var $with = $mod.Rec;',
  24664. '$mod.THelper.Foo.call($with, 1);',
  24665. '$mod.THelper.Foo.call($with, 1);',
  24666. '$mod.THelper.Foo.call($with, 22);',
  24667. '']));
  24668. end;
  24669. procedure TTestModule.TestRecordHelper_Constructor;
  24670. begin
  24671. StartProgram(false);
  24672. Add([
  24673. '{$modeswitch AdvancedRecords}',
  24674. 'type',
  24675. ' TRec = record',
  24676. ' constructor Create(w: word);',
  24677. ' end;',
  24678. ' THelper = record helper for TRec',
  24679. ' constructor NewHlp(w: word);',
  24680. ' end;',
  24681. 'var',
  24682. ' Rec: TRec;',
  24683. 'constructor TRec.Create(w: word);',
  24684. 'begin',
  24685. ' NewHlp(2);', // normal call
  24686. ' trec.NewHlp(3);', // new instance
  24687. 'end;',
  24688. 'constructor THelper.NewHlp(w: word);',
  24689. 'begin',
  24690. ' create(2);', // normal call
  24691. ' trec.create(3);', // new instance
  24692. ' NewHlp(4);', // normal call
  24693. ' trec.NewHlp(5);', // new instance
  24694. 'end;',
  24695. 'begin',
  24696. ' rec.newhlp(2);', // normal call
  24697. ' with rec do newhlp(12);', // normal call
  24698. ' trec.newhlp(3);', // new instance
  24699. ' with trec do newhlp(13);', // new instance
  24700. '']);
  24701. ConvertProgram;
  24702. CheckSource('TestRecordHelper_Constructor',
  24703. LinesToStr([ // statements
  24704. 'rtl.recNewT(this, "TRec", function () {',
  24705. ' this.$eq = function (b) {',
  24706. ' return true;',
  24707. ' };',
  24708. ' this.$assign = function (s) {',
  24709. ' return this;',
  24710. ' };',
  24711. ' this.Create = function (w) {',
  24712. ' $mod.THelper.NewHlp.call(this, 2);',
  24713. ' $mod.THelper.$new("NewHlp", [3]);',
  24714. ' return this;',
  24715. ' };',
  24716. '});',
  24717. 'rtl.createHelper(this, "THelper", null, function () {',
  24718. ' this.NewHlp = function (w) {',
  24719. ' this.Create(2);',
  24720. ' $mod.TRec.$new().Create(3);',
  24721. ' $mod.THelper.NewHlp.call(this, 4);',
  24722. ' $mod.THelper.$new("NewHlp", [5]);',
  24723. ' return this;',
  24724. ' };',
  24725. ' this.$new = function (fn, args) {',
  24726. ' return this[fn].apply($mod.TRec.$new(), args);',
  24727. ' };',
  24728. '});',
  24729. 'this.Rec = this.TRec.$new();',
  24730. '']),
  24731. LinesToStr([ // $mod.$main
  24732. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24733. 'var $with = $mod.Rec;',
  24734. '$mod.THelper.NewHlp.call($with, 12);',
  24735. '$mod.THelper.$new("NewHlp", [3]);',
  24736. 'var $with1 = $mod.TRec;',
  24737. '$mod.THelper.$new("NewHlp", [13]);',
  24738. '']));
  24739. end;
  24740. procedure TTestModule.TestTypeHelper_ClassVar;
  24741. begin
  24742. StartProgram(false);
  24743. Add([
  24744. '{$modeswitch typehelpers}',
  24745. 'type',
  24746. ' THelper = type helper for byte',
  24747. ' const',
  24748. ' One = 1;',
  24749. ' Two: word = 2;',
  24750. ' class var',
  24751. ' Glob: word;',
  24752. ' function Foo(w: word): word;',
  24753. ' class function Bar(w: word): word; static;',
  24754. ' end;',
  24755. 'function THelper.foo(w: word): word;',
  24756. 'begin',
  24757. ' Result:=w;',
  24758. ' Two:=One+w;',
  24759. ' Glob:=Glob;',
  24760. ' Result:=Self.Glob;',
  24761. ' Self.Glob:=Self.Glob;',
  24762. ' with Self do Glob:=Glob;',
  24763. 'end;',
  24764. 'class function THelper.bar(w: word): word;',
  24765. 'begin',
  24766. ' Result:=w;',
  24767. ' Two:=One;',
  24768. ' Glob:=Glob;',
  24769. 'end;',
  24770. 'var b: byte;',
  24771. 'begin',
  24772. ' byte.two:=byte.one;',
  24773. ' byte.Glob:=byte.Glob;',
  24774. ' with byte do begin',
  24775. ' two:=one;',
  24776. ' Glob:=Glob;',
  24777. ' end;',
  24778. ' b.two:=b.one;',
  24779. ' b.Glob:=b.Glob;',
  24780. ' with b do begin',
  24781. ' two:=one;',
  24782. ' Glob:=Glob;',
  24783. ' end;',
  24784. '']);
  24785. ConvertProgram;
  24786. CheckSource('TestTypeHelper_ClassVar',
  24787. LinesToStr([ // statements
  24788. 'rtl.createHelper(this, "THelper", null, function () {',
  24789. ' this.One = 1;',
  24790. ' this.Two = 2;',
  24791. ' this.Glob = 0;',
  24792. ' this.Foo = function (w) {',
  24793. ' var Result = 0;',
  24794. ' Result = w;',
  24795. ' $mod.THelper.Two = 1 + w;',
  24796. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24797. ' Result = $mod.THelper.Glob;',
  24798. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24799. ' var $with = this.get();',
  24800. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24801. ' return Result;',
  24802. ' };',
  24803. ' this.Bar = function (w) {',
  24804. ' var Result = 0;',
  24805. ' Result = w;',
  24806. ' $mod.THelper.Two = 1;',
  24807. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24808. ' return Result;',
  24809. ' };',
  24810. '});',
  24811. 'this.b = 0;',
  24812. '']),
  24813. LinesToStr([ // $mod.$main
  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. '$mod.THelper.Two = 1;',
  24819. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24820. 'var $with = $mod.b;',
  24821. '$mod.THelper.Two = 1;',
  24822. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24823. '']));
  24824. end;
  24825. procedure TTestModule.TestTypeHelper_PassResultElement;
  24826. begin
  24827. StartProgram(false);
  24828. Add([
  24829. '{$modeswitch typehelpers}',
  24830. 'type',
  24831. ' THelper = type helper for word',
  24832. ' procedure DoIt(e: byte = 123);',
  24833. ' class procedure DoSome(e: byte = 456); static;',
  24834. ' end;',
  24835. 'procedure THelper.DoIt(e: byte);',
  24836. 'begin',
  24837. 'end;',
  24838. 'class procedure THelper.DoSome(e: byte);',
  24839. 'begin',
  24840. 'end;',
  24841. 'function Foo(w: word): word;',
  24842. 'begin',
  24843. ' Result.DoIt;',
  24844. ' Result.DoIt();',
  24845. ' Result.DoSome;',
  24846. ' Result.DoSome();',
  24847. ' with Result do begin',
  24848. ' DoIt;',
  24849. ' DoIt();',
  24850. ' DoSome;',
  24851. ' DoSome();',
  24852. ' end;',
  24853. 'end;',
  24854. 'begin',
  24855. '']);
  24856. ConvertProgram;
  24857. CheckSource('TestTypeHelper_PassResultElement',
  24858. LinesToStr([ // statements
  24859. 'rtl.createHelper(this, "THelper", null, function () {',
  24860. ' this.DoIt = function (e) {',
  24861. ' };',
  24862. ' this.DoSome = function (e) {',
  24863. ' };',
  24864. '});',
  24865. 'this.Foo = function (w) {',
  24866. ' var Result = 0;',
  24867. ' $mod.THelper.DoIt.call({',
  24868. ' get: function () {',
  24869. ' return Result;',
  24870. ' },',
  24871. ' set: function (v) {',
  24872. ' Result = v;',
  24873. ' }',
  24874. ' }, 123);',
  24875. ' $mod.THelper.DoIt.call({',
  24876. ' get: function () {',
  24877. ' return Result;',
  24878. ' },',
  24879. ' set: function (v) {',
  24880. ' Result = v;',
  24881. ' }',
  24882. ' }, 123);',
  24883. ' $mod.THelper.DoSome(456);',
  24884. ' $mod.THelper.DoSome(456);',
  24885. ' $mod.THelper.DoIt.call({',
  24886. ' get: function () {',
  24887. ' return Result;',
  24888. ' },',
  24889. ' set: function (v) {',
  24890. ' Result = v;',
  24891. ' }',
  24892. ' }, 123);',
  24893. ' $mod.THelper.DoIt.call({',
  24894. ' get: function () {',
  24895. ' return Result;',
  24896. ' },',
  24897. ' set: function (v) {',
  24898. ' Result = v;',
  24899. ' }',
  24900. ' }, 123);',
  24901. ' $mod.THelper.DoSome(456);',
  24902. ' $mod.THelper.DoSome(456);',
  24903. ' return Result;',
  24904. '};',
  24905. '']),
  24906. LinesToStr([ // $mod.$main
  24907. '']));
  24908. end;
  24909. procedure TTestModule.TestTypeHelper_PassArgs;
  24910. begin
  24911. StartProgram(false);
  24912. Add([
  24913. '{$modeswitch typehelpers}',
  24914. 'type',
  24915. ' THelper = type helper for word',
  24916. ' procedure DoIt(e: byte = 123);',
  24917. ' end;',
  24918. 'procedure THelper.DoIt(e: byte);',
  24919. 'begin',
  24920. 'end;',
  24921. 'procedure FooDefault(a: word);',
  24922. 'begin',
  24923. ' a.DoIt;',
  24924. ' with a do DoIt;',
  24925. 'end;',
  24926. 'procedure FooConst(const a: word);',
  24927. 'begin',
  24928. ' a.DoIt;',
  24929. ' with a do DoIt;',
  24930. 'end;',
  24931. 'procedure FooVar(var a: word);',
  24932. 'begin',
  24933. ' a.DoIt;',
  24934. ' with a do DoIt;',
  24935. 'end;',
  24936. 'begin',
  24937. '']);
  24938. ConvertProgram;
  24939. CheckSource('TestTypeHelper_PassArgs',
  24940. LinesToStr([ // statements
  24941. 'rtl.createHelper(this, "THelper", null, function () {',
  24942. ' this.DoIt = function (e) {',
  24943. ' };',
  24944. '});',
  24945. 'this.FooDefault = function (a) {',
  24946. ' $mod.THelper.DoIt.call({',
  24947. ' get: function () {',
  24948. ' return a;',
  24949. ' },',
  24950. ' set: function (v) {',
  24951. ' a = v;',
  24952. ' }',
  24953. ' }, 123);',
  24954. ' $mod.THelper.DoIt.call({',
  24955. ' get: function () {',
  24956. ' return a;',
  24957. ' },',
  24958. ' set: function (v) {',
  24959. ' a = v;',
  24960. ' }',
  24961. ' }, 123);',
  24962. '};',
  24963. 'this.FooConst = function (a) {',
  24964. ' $mod.THelper.DoIt.call({',
  24965. ' get: function () {',
  24966. ' return a;',
  24967. ' },',
  24968. ' set: function (v) {',
  24969. ' rtl.raiseE("EPropReadOnly");',
  24970. ' }',
  24971. ' }, 123);',
  24972. ' $mod.THelper.DoIt.call({',
  24973. ' get: function () {',
  24974. ' return a;',
  24975. ' },',
  24976. ' set: function () {',
  24977. ' rtl.raiseE("EPropReadOnly");',
  24978. ' }',
  24979. ' }, 123);',
  24980. '};',
  24981. 'this.FooVar = function (a) {',
  24982. ' $mod.THelper.DoIt.call(a, 123);',
  24983. ' var $with = a.get();',
  24984. ' $mod.THelper.DoIt.call(a, 123);',
  24985. '};',
  24986. '']),
  24987. LinesToStr([ // $mod.$main
  24988. '']));
  24989. end;
  24990. procedure TTestModule.TestTypeHelper_PassVarConst;
  24991. begin
  24992. StartProgram(false);
  24993. Add([
  24994. '{$modeswitch typehelpers}',
  24995. 'type',
  24996. ' THelper = type helper for word',
  24997. ' procedure DoIt(e: byte = 123);',
  24998. ' end;',
  24999. 'procedure THelper.DoIt(e: byte);',
  25000. 'begin',
  25001. 'end;',
  25002. 'var a: word;',
  25003. 'const c: word = 2;',
  25004. '{$writeableconst off}',
  25005. 'const r: word = 3;',
  25006. 'begin',
  25007. ' a.DoIt;',
  25008. ' with a do DoIt;',
  25009. ' c.DoIt;',
  25010. ' with c do DoIt;',
  25011. ' r.DoIt;',
  25012. ' with r do DoIt;',
  25013. '']);
  25014. ConvertProgram;
  25015. CheckSource('TestTypeHelper_PassVarConst',
  25016. LinesToStr([ // statements
  25017. 'rtl.createHelper(this, "THelper", null, function () {',
  25018. ' this.DoIt = function (e) {',
  25019. ' };',
  25020. '});',
  25021. 'this.a = 0;',
  25022. 'this.c = 2;',
  25023. 'this.r = 3;',
  25024. '']),
  25025. LinesToStr([ // $mod.$main
  25026. '$mod.THelper.DoIt.call({',
  25027. ' p: $mod,',
  25028. ' get: function () {',
  25029. ' return this.p.a;',
  25030. ' },',
  25031. ' set: function (v) {',
  25032. ' this.p.a = v;',
  25033. ' }',
  25034. '}, 123);',
  25035. 'var $with = $mod.a;',
  25036. '$mod.THelper.DoIt.call({',
  25037. ' get: function () {',
  25038. ' return $with;',
  25039. ' },',
  25040. ' set: function (v) {',
  25041. ' $with = v;',
  25042. ' }',
  25043. '}, 123);',
  25044. '$mod.THelper.DoIt.call({',
  25045. ' p: $mod,',
  25046. ' get: function () {',
  25047. ' return this.p.c;',
  25048. ' },',
  25049. ' set: function (v) {',
  25050. ' this.p.c = v;',
  25051. ' }',
  25052. '}, 123);',
  25053. 'var $with1 = $mod.c;',
  25054. '$mod.THelper.DoIt.call({',
  25055. ' get: function () {',
  25056. ' return $with1;',
  25057. ' },',
  25058. ' set: function (v) {',
  25059. ' $with1 = v;',
  25060. ' }',
  25061. '}, 123);',
  25062. '$mod.THelper.DoIt.call({',
  25063. ' get: function () {',
  25064. ' return 3;',
  25065. ' },',
  25066. ' set: function (v) {',
  25067. ' rtl.raiseE("EPropReadOnly");',
  25068. ' }',
  25069. '}, 123);',
  25070. 'var $with2 = 3;',
  25071. ' $mod.THelper.DoIt.call({',
  25072. ' get: function () {',
  25073. ' return $with2;',
  25074. ' },',
  25075. ' set: function () {',
  25076. ' rtl.raiseE("EPropReadOnly");',
  25077. ' }',
  25078. ' }, 123);',
  25079. '']));
  25080. end;
  25081. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25082. begin
  25083. StartProgram(false);
  25084. Add([
  25085. '{$modeswitch typehelpers}',
  25086. 'type',
  25087. ' THelper = type helper for word',
  25088. ' procedure DoIt(e: byte = 123);',
  25089. ' end;',
  25090. 'procedure THelper.DoIt(e: byte);',
  25091. 'begin',
  25092. 'end;',
  25093. 'function Foo(b: byte = 1): word;',
  25094. 'begin',
  25095. 'end;',
  25096. 'begin',
  25097. ' Foo.DoIt;',
  25098. ' Foo().DoIt;',
  25099. ' with Foo do DoIt;',
  25100. ' with Foo() do DoIt;',
  25101. '']);
  25102. ConvertProgram;
  25103. CheckSource('TestTypeHelper_PassFuncResult',
  25104. LinesToStr([ // statements
  25105. 'rtl.createHelper(this, "THelper", null, function () {',
  25106. ' this.DoIt = function (e) {',
  25107. ' };',
  25108. '});',
  25109. 'this.Foo = function (b) {',
  25110. ' var Result = 0;',
  25111. ' return Result;',
  25112. '};',
  25113. '']),
  25114. LinesToStr([ // $mod.$main
  25115. '$mod.THelper.DoIt.call({',
  25116. ' a: $mod.Foo(1),',
  25117. ' get: function () {',
  25118. ' return this.a;',
  25119. ' },',
  25120. ' set: function (v) {',
  25121. ' this.a = v;',
  25122. ' }',
  25123. '}, 123);',
  25124. '$mod.THelper.DoIt.call({',
  25125. ' a: $mod.Foo(1),',
  25126. ' get: function () {',
  25127. ' return this.a;',
  25128. ' },',
  25129. ' set: function (v) {',
  25130. ' this.a = v;',
  25131. ' }',
  25132. '}, 123);',
  25133. 'var $with = $mod.Foo(1);',
  25134. '$mod.THelper.DoIt.call({',
  25135. ' get: function () {',
  25136. ' return $with;',
  25137. ' },',
  25138. ' set: function (v) {',
  25139. ' $with = v;',
  25140. ' }',
  25141. '}, 123);',
  25142. 'var $with1 = $mod.Foo(1);',
  25143. '$mod.THelper.DoIt.call({',
  25144. ' get: function () {',
  25145. ' return $with1;',
  25146. ' },',
  25147. ' set: function (v) {',
  25148. ' $with1 = v;',
  25149. ' }',
  25150. '}, 123);',
  25151. '']));
  25152. end;
  25153. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25154. begin
  25155. StartProgram(false);
  25156. Add([
  25157. '{$modeswitch typehelpers}',
  25158. 'type',
  25159. ' TObject = class',
  25160. ' FField: word;',
  25161. ' procedure SetField(Value: word);',
  25162. ' property Field: word read FField write SetField;',
  25163. ' end;',
  25164. ' THelper = type helper for word',
  25165. ' procedure Fly;',
  25166. ' class procedure Run; static;',
  25167. ' end;',
  25168. 'procedure TObject.SetField(Value: word);',
  25169. 'begin',
  25170. ' Field.Fly;',
  25171. ' Field.Run;',
  25172. ' Self.Field.Fly;',
  25173. ' Self.Field.Run;',
  25174. ' with Self do begin',
  25175. ' Field.Fly;',
  25176. ' Field.Run;',
  25177. ' end;',
  25178. ' with Self.Field do begin',
  25179. ' Fly;',
  25180. ' Run;',
  25181. ' end;',
  25182. 'end;',
  25183. 'procedure THelper.Fly;',
  25184. 'begin',
  25185. 'end;',
  25186. 'class procedure THelper.Run;',
  25187. 'begin',
  25188. 'end;',
  25189. 'var',
  25190. ' o: TObject;',
  25191. 'begin',
  25192. ' o.Field.Fly;',
  25193. ' o.Field.Run;',
  25194. ' with o do begin',
  25195. ' Field.Fly;',
  25196. ' Field.Run;',
  25197. ' end;',
  25198. ' with o.Field do begin',
  25199. ' Fly;',
  25200. ' Run;',
  25201. ' end;',
  25202. '']);
  25203. ConvertProgram;
  25204. CheckSource('TestTypeHelper_PassPropertyField',
  25205. LinesToStr([ // statements
  25206. 'rtl.createClass(this, "TObject", null, function () {',
  25207. ' this.$init = function () {',
  25208. ' this.FField = 0;',
  25209. ' };',
  25210. ' this.$final = function () {',
  25211. ' };',
  25212. ' this.SetField = function (Value) {',
  25213. ' $mod.THelper.Fly.call({',
  25214. ' p: this,',
  25215. ' get: function () {',
  25216. ' return this.p.FField;',
  25217. ' },',
  25218. ' set: function (v) {',
  25219. ' this.p.FField = v;',
  25220. ' }',
  25221. ' });',
  25222. ' $mod.THelper.Run();',
  25223. ' $mod.THelper.Fly.call({',
  25224. ' p: this,',
  25225. ' get: function () {',
  25226. ' return this.p.FField;',
  25227. ' },',
  25228. ' set: function (v) {',
  25229. ' this.p.FField = v;',
  25230. ' }',
  25231. ' });',
  25232. ' $mod.THelper.Run();',
  25233. ' $mod.THelper.Fly.call({',
  25234. ' p: this,',
  25235. ' get: function () {',
  25236. ' return this.p.FField;',
  25237. ' },',
  25238. ' set: function (v) {',
  25239. ' this.p.FField = v;',
  25240. ' }',
  25241. ' });',
  25242. ' $mod.THelper.Run();',
  25243. ' var $with = this.FField;',
  25244. ' $mod.THelper.Fly.call({',
  25245. ' get: function () {',
  25246. ' return $with;',
  25247. ' },',
  25248. ' set: function (v) {',
  25249. ' $with = v;',
  25250. ' }',
  25251. ' });',
  25252. ' $mod.THelper.Run();',
  25253. ' };',
  25254. '});',
  25255. 'rtl.createHelper(this, "THelper", null, function () {',
  25256. ' this.Fly = function () {',
  25257. ' };',
  25258. ' this.Run = function () {',
  25259. ' };',
  25260. '});',
  25261. 'this.o = null;',
  25262. '']),
  25263. LinesToStr([ // $mod.$main
  25264. '$mod.THelper.Fly.call({',
  25265. ' p: $mod.o,',
  25266. ' get: function () {',
  25267. ' return this.p.FField;',
  25268. ' },',
  25269. ' set: function (v) {',
  25270. ' this.p.FField = v;',
  25271. ' }',
  25272. '});',
  25273. '$mod.THelper.Run();',
  25274. 'var $with = $mod.o;',
  25275. '$mod.THelper.Fly.call({',
  25276. ' p: $with,',
  25277. ' get: function () {',
  25278. ' return this.p.FField;',
  25279. ' },',
  25280. ' set: function (v) {',
  25281. ' this.p.FField = v;',
  25282. ' }',
  25283. '});',
  25284. '$mod.THelper.Run();',
  25285. 'var $with1 = $mod.o.FField;',
  25286. '$mod.THelper.Fly.call({',
  25287. ' get: function () {',
  25288. ' return $with1;',
  25289. ' },',
  25290. ' set: function (v) {',
  25291. ' $with1 = v;',
  25292. ' }',
  25293. '});',
  25294. '$mod.THelper.Run();',
  25295. '']));
  25296. end;
  25297. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25298. begin
  25299. StartProgram(false);
  25300. Add([
  25301. '{$modeswitch typehelpers}',
  25302. 'type',
  25303. ' TObject = class',
  25304. ' FField: word;',
  25305. ' function GetField: word;',
  25306. ' property Field: word read GetField write FField;',
  25307. ' end;',
  25308. ' THelper = type helper for word',
  25309. ' procedure Fly;',
  25310. ' class procedure Run; static;',
  25311. ' end;',
  25312. 'function TObject.GetField: word;',
  25313. 'begin',
  25314. ' Field.Fly;',
  25315. ' Field.Run;',
  25316. ' Self.Field.Fly;',
  25317. ' Self.Field.Run;',
  25318. ' with Self do begin',
  25319. ' Field.Fly;',
  25320. ' Field.Run;',
  25321. ' end;',
  25322. ' with Self.Field do begin',
  25323. ' Fly;',
  25324. ' Run;',
  25325. ' end;',
  25326. 'end;',
  25327. 'procedure THelper.Fly;',
  25328. 'begin',
  25329. 'end;',
  25330. 'class procedure THelper.Run;',
  25331. 'begin',
  25332. 'end;',
  25333. 'var',
  25334. ' o: TObject;',
  25335. 'begin',
  25336. ' o.Field.Fly;',
  25337. ' o.Field.Run;',
  25338. ' with o do begin',
  25339. ' Field.Fly;',
  25340. ' Field.Run;',
  25341. ' end;',
  25342. ' with o.Field do begin',
  25343. ' Fly;',
  25344. ' Run;',
  25345. ' end;',
  25346. '']);
  25347. ConvertProgram;
  25348. CheckSource('TestTypeHelper_PassPropertyGetter',
  25349. LinesToStr([ // statements
  25350. 'rtl.createClass(this, "TObject", null, function () {',
  25351. ' this.$init = function () {',
  25352. ' this.FField = 0;',
  25353. ' };',
  25354. ' this.$final = function () {',
  25355. ' };',
  25356. ' this.GetField = function () {',
  25357. ' var Result = 0;',
  25358. ' $mod.THelper.Fly.call({',
  25359. ' p: this.GetField(),',
  25360. ' get: function () {',
  25361. ' return this.p;',
  25362. ' },',
  25363. ' set: function (v) {',
  25364. ' this.p = v;',
  25365. ' }',
  25366. ' });',
  25367. ' $mod.THelper.Run();',
  25368. ' $mod.THelper.Fly.call({',
  25369. ' p: this.GetField(),',
  25370. ' get: function () {',
  25371. ' return this.p;',
  25372. ' },',
  25373. ' set: function (v) {',
  25374. ' this.p = v;',
  25375. ' }',
  25376. ' });',
  25377. ' $mod.THelper.Run();',
  25378. ' $mod.THelper.Fly.call({',
  25379. ' p: this.GetField(),',
  25380. ' get: function () {',
  25381. ' return this.p;',
  25382. ' },',
  25383. ' set: function (v) {',
  25384. ' this.p = v;',
  25385. ' }',
  25386. ' });',
  25387. ' $mod.THelper.Run();',
  25388. ' var $with = this.GetField();',
  25389. ' $mod.THelper.Fly.call({',
  25390. ' get: function () {',
  25391. ' return $with;',
  25392. ' },',
  25393. ' set: function (v) {',
  25394. ' $with = v;',
  25395. ' }',
  25396. ' });',
  25397. ' $mod.THelper.Run();',
  25398. ' return Result;',
  25399. ' };',
  25400. '});',
  25401. 'rtl.createHelper(this, "THelper", null, function () {',
  25402. ' this.Fly = function () {',
  25403. ' };',
  25404. ' this.Run = function () {',
  25405. ' };',
  25406. '});',
  25407. 'this.o = null;',
  25408. '']),
  25409. LinesToStr([ // $mod.$main
  25410. '$mod.THelper.Fly.call({',
  25411. ' p: $mod.o.GetField(),',
  25412. ' get: function () {',
  25413. ' return this.p;',
  25414. ' },',
  25415. ' set: function (v) {',
  25416. ' this.p = v;',
  25417. ' }',
  25418. '});',
  25419. '$mod.THelper.Run();',
  25420. 'var $with = $mod.o;',
  25421. '$mod.THelper.Fly.call({',
  25422. ' p: $with.GetField(),',
  25423. ' get: function () {',
  25424. ' return this.p;',
  25425. ' },',
  25426. ' set: function (v) {',
  25427. ' this.p = v;',
  25428. ' }',
  25429. '});',
  25430. '$mod.THelper.Run();',
  25431. 'var $with1 = $mod.o.GetField();',
  25432. '$mod.THelper.Fly.call({',
  25433. ' get: function () {',
  25434. ' return $with1;',
  25435. ' },',
  25436. ' set: function (v) {',
  25437. ' $with1 = v;',
  25438. ' }',
  25439. '});',
  25440. '$mod.THelper.Run();',
  25441. '']));
  25442. end;
  25443. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25444. begin
  25445. StartProgram(false);
  25446. Add([
  25447. '{$modeswitch typehelpers}',
  25448. 'type',
  25449. ' TObject = class',
  25450. ' class var FField: word;',
  25451. ' class procedure SetField(Value: word);',
  25452. ' class property Field: word read FField write SetField;',
  25453. ' end;',
  25454. ' THelper = type helper for word',
  25455. ' procedure Fly(n: byte);',
  25456. ' end;',
  25457. 'class procedure TObject.SetField(Value: word);',
  25458. 'begin',
  25459. ' Field.Fly(1);',
  25460. ' Self.Field.Fly(2);',
  25461. ' with Self do Field.Fly(3);',
  25462. ' with Self.Field do Fly(4);',
  25463. ' TObject.Field.Fly(5);',
  25464. ' with TObject do Field.Fly(6);',
  25465. ' with TObject.Field do Fly(7);',
  25466. 'end;',
  25467. 'procedure THelper.Fly(n: byte);',
  25468. 'begin',
  25469. 'end;',
  25470. 'var',
  25471. ' o: TObject;',
  25472. 'begin',
  25473. ' o.Field.Fly(11);',
  25474. ' with o do Field.Fly(12);',
  25475. ' with o.Field do Fly(13);',
  25476. ' TObject.Field.Fly(14);',
  25477. ' with TObject do Field.Fly(15);',
  25478. ' with TObject.Field do Fly(16);',
  25479. '']);
  25480. ConvertProgram;
  25481. CheckSource('TestTypeHelper_PassClassPropertyField',
  25482. LinesToStr([ // statements
  25483. 'rtl.createClass(this, "TObject", null, function () {',
  25484. ' this.FField = 0;',
  25485. ' this.$init = function () {',
  25486. ' };',
  25487. ' this.$final = function () {',
  25488. ' };',
  25489. ' this.SetField = function (Value) {',
  25490. ' $mod.THelper.Fly.call({',
  25491. ' p: this,',
  25492. ' get: function () {',
  25493. ' return this.p.FField;',
  25494. ' },',
  25495. ' set: function (v) {',
  25496. ' $mod.TObject.FField = v;',
  25497. ' }',
  25498. ' }, 1);',
  25499. ' $mod.THelper.Fly.call({',
  25500. ' p: this,',
  25501. ' get: function () {',
  25502. ' return this.p.FField;',
  25503. ' },',
  25504. ' set: function (v) {',
  25505. ' $mod.TObject.FField = v;',
  25506. ' }',
  25507. ' }, 2);',
  25508. ' $mod.THelper.Fly.call({',
  25509. ' p: this,',
  25510. ' get: function () {',
  25511. ' return this.p.FField;',
  25512. ' },',
  25513. ' set: function (v) {',
  25514. ' $mod.TObject.FField = v;',
  25515. ' }',
  25516. ' }, 3);',
  25517. ' var $with = this.FField;',
  25518. ' $mod.THelper.Fly.call({',
  25519. ' get: function () {',
  25520. ' return $with;',
  25521. ' },',
  25522. ' set: function (v) {',
  25523. ' $with = v;',
  25524. ' }',
  25525. ' }, 4);',
  25526. ' $mod.THelper.Fly.call({',
  25527. ' p: $mod.TObject,',
  25528. ' get: function () {',
  25529. ' return this.p.FField;',
  25530. ' },',
  25531. ' set: function (v) {',
  25532. ' $mod.TObject.FField = v;',
  25533. ' }',
  25534. ' }, 5);',
  25535. ' var $with1 = $mod.TObject;',
  25536. ' $mod.THelper.Fly.call({',
  25537. ' p: $with1,',
  25538. ' get: function () {',
  25539. ' return this.p.FField;',
  25540. ' },',
  25541. ' set: function (v) {',
  25542. ' $mod.TObject.FField = v;',
  25543. ' }',
  25544. ' }, 6);',
  25545. ' var $with2 = $mod.TObject.FField;',
  25546. ' $mod.THelper.Fly.call({',
  25547. ' get: function () {',
  25548. ' return $with2;',
  25549. ' },',
  25550. ' set: function (v) {',
  25551. ' $with2 = v;',
  25552. ' }',
  25553. ' }, 7);',
  25554. ' };',
  25555. '});',
  25556. 'rtl.createHelper(this, "THelper", null, function () {',
  25557. ' this.Fly = function (n) {',
  25558. ' };',
  25559. '});',
  25560. 'this.o = null;',
  25561. '']),
  25562. LinesToStr([ // $mod.$main
  25563. '$mod.THelper.Fly.call({',
  25564. ' p: $mod.o,',
  25565. ' get: function () {',
  25566. ' return this.p.FField;',
  25567. ' },',
  25568. ' set: function (v) {',
  25569. ' $mod.TObject.FField = v;',
  25570. ' }',
  25571. '}, 11);',
  25572. 'var $with = $mod.o;',
  25573. '$mod.THelper.Fly.call({',
  25574. ' p: $with,',
  25575. ' get: function () {',
  25576. ' return this.p.FField;',
  25577. ' },',
  25578. ' set: function (v) {',
  25579. ' $mod.TObject.FField = v;',
  25580. ' }',
  25581. '}, 12);',
  25582. 'var $with1 = $mod.o.FField;',
  25583. '$mod.THelper.Fly.call({',
  25584. ' get: function () {',
  25585. ' return $with1;',
  25586. ' },',
  25587. ' set: function (v) {',
  25588. ' $with1 = v;',
  25589. ' }',
  25590. '}, 13);',
  25591. '$mod.THelper.Fly.call({',
  25592. ' p: $mod.TObject,',
  25593. ' get: function () {',
  25594. ' return this.p.FField;',
  25595. ' },',
  25596. ' set: function (v) {',
  25597. ' $mod.TObject.FField = v;',
  25598. ' }',
  25599. '}, 14);',
  25600. 'var $with2 = $mod.TObject;',
  25601. '$mod.THelper.Fly.call({',
  25602. ' p: $with2,',
  25603. ' get: function () {',
  25604. ' return this.p.FField;',
  25605. ' },',
  25606. ' set: function (v) {',
  25607. ' $mod.TObject.FField = v;',
  25608. ' }',
  25609. '}, 15);',
  25610. 'var $with3 = $mod.TObject.FField;',
  25611. '$mod.THelper.Fly.call({',
  25612. ' get: function () {',
  25613. ' return $with3;',
  25614. ' },',
  25615. ' set: function (v) {',
  25616. ' $with3 = v;',
  25617. ' }',
  25618. '}, 16);',
  25619. '']));
  25620. end;
  25621. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25622. begin
  25623. StartProgram(false);
  25624. Add([
  25625. '{$modeswitch typehelpers}',
  25626. 'type',
  25627. ' TObject = class',
  25628. ' class var FField: word;',
  25629. ' class function GetField: word; static;',
  25630. ' class property Field: word read GetField write FField;',
  25631. ' end;',
  25632. ' THelper = type helper for word',
  25633. ' procedure Fly(n: byte);',
  25634. ' end;',
  25635. 'class function TObject.GetField: word;',
  25636. 'begin',
  25637. ' Field.Fly(1);',
  25638. ' TObject.Field.Fly(5);',
  25639. ' with TObject do Field.Fly(6);',
  25640. ' with TObject.Field do Fly(7);',
  25641. 'end;',
  25642. 'procedure THelper.Fly(n: byte);',
  25643. 'begin',
  25644. 'end;',
  25645. 'var',
  25646. ' o: TObject;',
  25647. 'begin',
  25648. ' o.Field.Fly(11);',
  25649. ' with o do Field.Fly(12);',
  25650. ' with o.Field do Fly(13);',
  25651. '']);
  25652. ConvertProgram;
  25653. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25654. LinesToStr([ // statements
  25655. 'rtl.createClass(this, "TObject", null, function () {',
  25656. ' this.FField = 0;',
  25657. ' this.$init = function () {',
  25658. ' };',
  25659. ' this.$final = function () {',
  25660. ' };',
  25661. ' this.GetField = function () {',
  25662. ' var Result = 0;',
  25663. ' $mod.THelper.Fly.call({',
  25664. ' p: $mod.TObject.GetField(),',
  25665. ' get: function () {',
  25666. ' return this.p;',
  25667. ' },',
  25668. ' set: function (v) {',
  25669. ' this.p = v;',
  25670. ' }',
  25671. ' }, 1);',
  25672. ' $mod.THelper.Fly.call({',
  25673. ' p: $mod.TObject.GetField(),',
  25674. ' get: function () {',
  25675. ' return this.p;',
  25676. ' },',
  25677. ' set: function (v) {',
  25678. ' this.p = v;',
  25679. ' }',
  25680. ' }, 5);',
  25681. ' var $with = $mod.TObject;',
  25682. ' $mod.THelper.Fly.call({',
  25683. ' p: $with.GetField(),',
  25684. ' get: function () {',
  25685. ' return this.p;',
  25686. ' },',
  25687. ' set: function (v) {',
  25688. ' this.p = v;',
  25689. ' }',
  25690. ' }, 6);',
  25691. ' var $with1 = $mod.TObject.GetField();',
  25692. ' $mod.THelper.Fly.call({',
  25693. ' get: function () {',
  25694. ' return $with1;',
  25695. ' },',
  25696. ' set: function (v) {',
  25697. ' $with1 = v;',
  25698. ' }',
  25699. ' }, 7);',
  25700. ' return Result;',
  25701. ' };',
  25702. '});',
  25703. 'rtl.createHelper(this, "THelper", null, function () {',
  25704. ' this.Fly = function (n) {',
  25705. ' };',
  25706. '});',
  25707. 'this.o = null;',
  25708. '']),
  25709. LinesToStr([ // $mod.$main
  25710. '$mod.THelper.Fly.call({',
  25711. ' p: $mod.TObject.GetField(),',
  25712. ' get: function () {',
  25713. ' return this.p;',
  25714. ' },',
  25715. ' set: function (v) {',
  25716. ' this.p = v;',
  25717. ' }',
  25718. '}, 11);',
  25719. 'var $with = $mod.o;',
  25720. '$mod.THelper.Fly.call({',
  25721. ' p: $with.GetField(),',
  25722. ' get: function () {',
  25723. ' return this.p;',
  25724. ' },',
  25725. ' set: function (v) {',
  25726. ' this.p = v;',
  25727. ' }',
  25728. '}, 12);',
  25729. 'var $with1 = $mod.TObject.GetField();',
  25730. '$mod.THelper.Fly.call({',
  25731. ' get: function () {',
  25732. ' return $with1;',
  25733. ' },',
  25734. ' set: function (v) {',
  25735. ' $with1 = v;',
  25736. ' }',
  25737. '}, 13);',
  25738. '']));
  25739. end;
  25740. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25741. begin
  25742. StartProgram(false);
  25743. Add([
  25744. '{$modeswitch typehelpers}',
  25745. 'type',
  25746. ' TObject = class',
  25747. ' class var FField: word;',
  25748. ' class function GetField: word;',
  25749. ' class property Field: word read GetField write FField;',
  25750. ' end;',
  25751. ' TClass = class of TObject;',
  25752. ' THelper = type helper for word',
  25753. ' procedure Fly(n: byte);',
  25754. ' end;',
  25755. 'class function TObject.GetField: word;',
  25756. 'begin',
  25757. ' Field.Fly(1);',
  25758. ' Self.Field.Fly(5);',
  25759. ' with Self do Field.Fly(6);',
  25760. ' with Self.Field do Fly(7);',
  25761. 'end;',
  25762. 'procedure THelper.Fly(n: byte);',
  25763. 'begin',
  25764. 'end;',
  25765. 'var',
  25766. ' o: TObject;',
  25767. ' c: TClass;',
  25768. 'begin',
  25769. ' o.Field.Fly(11);',
  25770. ' with o do Field.Fly(12);',
  25771. ' with o.Field do Fly(13);',
  25772. ' c.Field.Fly(14);',
  25773. ' with c do Field.Fly(15);',
  25774. ' with c.Field do Fly(16);',
  25775. '']);
  25776. ConvertProgram;
  25777. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25778. LinesToStr([ // statements
  25779. 'rtl.createClass(this, "TObject", null, function () {',
  25780. ' this.FField = 0;',
  25781. ' this.$init = function () {',
  25782. ' };',
  25783. ' this.$final = function () {',
  25784. ' };',
  25785. ' this.GetField = function () {',
  25786. ' var Result = 0;',
  25787. ' $mod.THelper.Fly.call({',
  25788. ' p: this.GetField(),',
  25789. ' get: function () {',
  25790. ' return this.p;',
  25791. ' },',
  25792. ' set: function (v) {',
  25793. ' this.p = v;',
  25794. ' }',
  25795. ' }, 1);',
  25796. ' $mod.THelper.Fly.call({',
  25797. ' p: this.GetField(),',
  25798. ' get: function () {',
  25799. ' return this.p;',
  25800. ' },',
  25801. ' set: function (v) {',
  25802. ' this.p = v;',
  25803. ' }',
  25804. ' }, 5);',
  25805. ' $mod.THelper.Fly.call({',
  25806. ' p: this.GetField(),',
  25807. ' get: function () {',
  25808. ' return this.p;',
  25809. ' },',
  25810. ' set: function (v) {',
  25811. ' this.p = v;',
  25812. ' }',
  25813. ' }, 6);',
  25814. ' var $with = this.GetField();',
  25815. ' $mod.THelper.Fly.call({',
  25816. ' get: function () {',
  25817. ' return $with;',
  25818. ' },',
  25819. ' set: function (v) {',
  25820. ' $with = v;',
  25821. ' }',
  25822. ' }, 7);',
  25823. ' return Result;',
  25824. ' };',
  25825. '});',
  25826. 'rtl.createHelper(this, "THelper", null, function () {',
  25827. ' this.Fly = function (n) {',
  25828. ' };',
  25829. '});',
  25830. 'this.o = null;',
  25831. 'this.c = null;',
  25832. '']),
  25833. LinesToStr([ // $mod.$main
  25834. '$mod.THelper.Fly.call({',
  25835. ' p: $mod.o.$class.GetField(),',
  25836. ' get: function () {',
  25837. ' return this.p;',
  25838. ' },',
  25839. ' set: function (v) {',
  25840. ' this.p = v;',
  25841. ' }',
  25842. '}, 11);',
  25843. 'var $with = $mod.o;',
  25844. '$mod.THelper.Fly.call({',
  25845. ' p: $with.$class.GetField(),',
  25846. ' get: function () {',
  25847. ' return this.p;',
  25848. ' },',
  25849. ' set: function (v) {',
  25850. ' this.p = v;',
  25851. ' }',
  25852. '}, 12);',
  25853. 'var $with1 = $mod.o.$class.GetField();',
  25854. '$mod.THelper.Fly.call({',
  25855. ' get: function () {',
  25856. ' return $with1;',
  25857. ' },',
  25858. ' set: function (v) {',
  25859. ' $with1 = v;',
  25860. ' }',
  25861. '}, 13);',
  25862. '$mod.THelper.Fly.call({',
  25863. ' p: $mod.c.GetField(),',
  25864. ' get: function () {',
  25865. ' return this.p;',
  25866. ' },',
  25867. ' set: function (v) {',
  25868. ' this.p = v;',
  25869. ' }',
  25870. '}, 14);',
  25871. 'var $with2 = $mod.c;',
  25872. '$mod.THelper.Fly.call({',
  25873. ' p: $with2.GetField(),',
  25874. ' get: function () {',
  25875. ' return this.p;',
  25876. ' },',
  25877. ' set: function (v) {',
  25878. ' this.p = v;',
  25879. ' }',
  25880. '}, 15);',
  25881. 'var $with3 = $mod.c.GetField();',
  25882. '$mod.THelper.Fly.call({',
  25883. ' get: function () {',
  25884. ' return $with3;',
  25885. ' },',
  25886. ' set: function (v) {',
  25887. ' $with3 = v;',
  25888. ' }',
  25889. '}, 16);',
  25890. '']));
  25891. end;
  25892. procedure TTestModule.TestTypeHelper_Property;
  25893. begin
  25894. StartProgram(false);
  25895. Add([
  25896. '{$modeswitch typehelpers}',
  25897. 'type',
  25898. ' THelper = type helper for word',
  25899. ' function GetSize: longint;',
  25900. ' procedure SetSize(Value: longint);',
  25901. ' property Size: longint read GetSize write SetSize;',
  25902. ' end;',
  25903. 'function THelper.GetSize: longint;',
  25904. 'begin',
  25905. ' Result:=Size+1;',
  25906. ' Size:=2;',
  25907. ' Result:=Self.Size+3;',
  25908. ' Self.Size:=4;',
  25909. ' with Self do begin',
  25910. ' Result:=Size+5;',
  25911. ' Size:=6;',
  25912. ' end;',
  25913. 'end;',
  25914. 'procedure THelper.SetSize(Value: longint);',
  25915. 'begin',
  25916. 'end;',
  25917. 'var w: word;',
  25918. 'begin',
  25919. ' w:=w.Size+7;',
  25920. ' w.Size:=w+8;',
  25921. ' with w do begin',
  25922. ' w:=Size+9;',
  25923. ' Size:=w+10;',
  25924. ' end;',
  25925. '']);
  25926. ConvertProgram;
  25927. CheckSource('TestTypeHelper_Property',
  25928. LinesToStr([ // statements
  25929. 'rtl.createHelper(this, "THelper", null, function () {',
  25930. ' this.GetSize = function () {',
  25931. ' var Result = 0;',
  25932. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25933. ' $mod.THelper.SetSize.call(this, 2);',
  25934. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25935. ' $mod.THelper.SetSize.call(this, 4);',
  25936. ' var $with = this.get();',
  25937. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25938. ' $mod.THelper.SetSize.call(this, 6);',
  25939. ' return Result;',
  25940. ' };',
  25941. ' this.SetSize = function (Value) {',
  25942. ' };',
  25943. '});',
  25944. 'this.w = 0;',
  25945. '']),
  25946. LinesToStr([ // $mod.$main
  25947. '$mod.w = $mod.THelper.GetSize.call({',
  25948. ' p: $mod,',
  25949. ' get: function () {',
  25950. ' return this.p.w;',
  25951. ' },',
  25952. ' set: function (v) {',
  25953. ' this.p.w = v;',
  25954. ' }',
  25955. '}) + 7;',
  25956. '$mod.THelper.SetSize.call({',
  25957. ' p: $mod,',
  25958. ' get: function () {',
  25959. ' return this.p.w;',
  25960. ' },',
  25961. ' set: function (v) {',
  25962. ' this.p.w = v;',
  25963. ' }',
  25964. '}, $mod.w + 8);',
  25965. 'var $with = $mod.w;',
  25966. '$mod.w = $mod.THelper.GetSize.call({',
  25967. ' get: function () {',
  25968. ' return $with;',
  25969. ' },',
  25970. ' set: function (v) {',
  25971. ' $with = v;',
  25972. ' }',
  25973. '}) + 9;',
  25974. '$mod.THelper.SetSize.call({',
  25975. ' get: function () {',
  25976. ' return $with;',
  25977. ' },',
  25978. ' set: function (v) {',
  25979. ' $with = v;',
  25980. ' }',
  25981. '}, $mod.w + 10);',
  25982. '']));
  25983. end;
  25984. procedure TTestModule.TestTypeHelper_Property_Array;
  25985. begin
  25986. StartProgram(false);
  25987. Add([
  25988. '{$modeswitch typehelpers}',
  25989. 'type',
  25990. ' THelper = type helper for word',
  25991. ' function GetItems(Index: byte): boolean;',
  25992. ' procedure SetItems(Index: byte; Value: boolean);',
  25993. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25994. ' end;',
  25995. 'function THelper.GetItems(Index: byte): boolean;',
  25996. 'begin',
  25997. ' Result:=Items[1];',
  25998. ' Items[2]:=false;',
  25999. ' Result:=Self.Items[3];',
  26000. ' Self.Items[4]:=true;',
  26001. ' with Self do begin',
  26002. ' Result:=Items[5];',
  26003. ' Items[6]:=false;',
  26004. ' end;',
  26005. 'end;',
  26006. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26007. 'begin',
  26008. 'end;',
  26009. 'var',
  26010. ' w: word;',
  26011. ' b: boolean;',
  26012. 'begin',
  26013. ' b:=w.Items[1];',
  26014. ' w.Items[2]:=b;',
  26015. ' with w do begin',
  26016. ' b:=Items[3];',
  26017. ' Items[4]:=b;',
  26018. ' end;',
  26019. '']);
  26020. ConvertProgram;
  26021. CheckSource('TestTypeHelper_Property_Array',
  26022. LinesToStr([ // statements
  26023. 'rtl.createHelper(this, "THelper", null, function () {',
  26024. ' this.GetItems = function (Index) {',
  26025. ' var Result = false;',
  26026. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26027. ' $mod.THelper.SetItems.call(this, 2, false);',
  26028. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26029. ' $mod.THelper.SetItems.call(this, 4, true);',
  26030. ' var $with = this.get();',
  26031. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26032. ' $mod.THelper.SetItems.call(this, 6, false);',
  26033. ' return Result;',
  26034. ' };',
  26035. ' this.SetItems = function (Index, Value) {',
  26036. ' };',
  26037. '});',
  26038. 'this.w = 0;',
  26039. 'this.b = false;',
  26040. '']),
  26041. LinesToStr([ // $mod.$main
  26042. '$mod.b = $mod.THelper.GetItems.call({',
  26043. ' p: $mod,',
  26044. ' get: function () {',
  26045. ' return this.p.w;',
  26046. ' },',
  26047. ' set: function (v) {',
  26048. ' this.p.w = v;',
  26049. ' }',
  26050. '}, 1);',
  26051. '$mod.THelper.SetItems.call({',
  26052. ' p: $mod,',
  26053. ' get: function () {',
  26054. ' return this.p.w;',
  26055. ' },',
  26056. ' set: function (v) {',
  26057. ' this.p.w = v;',
  26058. ' }',
  26059. '}, 2, $mod.b);',
  26060. 'var $with = $mod.w;',
  26061. '$mod.b = $mod.THelper.GetItems.call({',
  26062. ' get: function () {',
  26063. ' return $with;',
  26064. ' },',
  26065. ' set: function (v) {',
  26066. ' $with = v;',
  26067. ' }',
  26068. '}, 3);',
  26069. '$mod.THelper.SetItems.call({',
  26070. ' get: function () {',
  26071. ' return $with;',
  26072. ' },',
  26073. ' set: function (v) {',
  26074. ' $with = v;',
  26075. ' }',
  26076. '}, 4, $mod.b);',
  26077. '']));
  26078. end;
  26079. procedure TTestModule.TestTypeHelper_ClassProperty;
  26080. begin
  26081. StartProgram(false);
  26082. Add([
  26083. '{$modeswitch typehelpers}',
  26084. 'type',
  26085. ' THelper = type helper for word',
  26086. ' class function GetSize: longint; static;',
  26087. ' class procedure SetSize(Value: longint); static;',
  26088. ' class property Size: longint read GetSize write SetSize;',
  26089. ' end;',
  26090. 'class function THelper.GetSize: longint;',
  26091. 'begin',
  26092. ' Result:=Size+1;',
  26093. ' Size:=2;',
  26094. 'end;',
  26095. 'class procedure THelper.SetSize(Value: longint);',
  26096. 'begin',
  26097. 'end;',
  26098. 'begin',
  26099. '']);
  26100. ConvertProgram;
  26101. CheckSource('TestTypeHelper_ClassProperty',
  26102. LinesToStr([ // statements
  26103. 'rtl.createHelper(this, "THelper", null, function () {',
  26104. ' this.GetSize = function () {',
  26105. ' var Result = 0;',
  26106. ' Result = $mod.THelper.GetSize() + 1;',
  26107. ' $mod.THelper.SetSize(2);',
  26108. ' return Result;',
  26109. ' };',
  26110. ' this.SetSize = function (Value) {',
  26111. ' };',
  26112. '});',
  26113. '']),
  26114. LinesToStr([ // $mod.$main
  26115. '']));
  26116. end;
  26117. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26118. begin
  26119. StartProgram(false);
  26120. Add([
  26121. '{$modeswitch typehelpers}',
  26122. 'type',
  26123. ' THelper = type helper for word',
  26124. ' class function GetItems(Index: byte): boolean; static;',
  26125. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26126. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26127. ' end;',
  26128. 'class function THelper.GetItems(Index: byte): boolean;',
  26129. 'begin',
  26130. ' Result:=Items[1];',
  26131. ' Items[2]:=false;',
  26132. 'end;',
  26133. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26134. 'begin',
  26135. 'end;',
  26136. 'var',
  26137. ' w: word;',
  26138. ' b: boolean;',
  26139. 'begin',
  26140. ' b:=w.Items[1];',
  26141. ' w.Items[2]:=b;',
  26142. ' with w do begin',
  26143. ' b:=Items[3];',
  26144. ' Items[4]:=b;',
  26145. ' end;',
  26146. '']);
  26147. ConvertProgram;
  26148. CheckSource('TestTypeHelper_ClassProperty_Array',
  26149. LinesToStr([ // statements
  26150. 'rtl.createHelper(this, "THelper", null, function () {',
  26151. ' this.GetItems = function (Index) {',
  26152. ' var Result = false;',
  26153. ' Result = $mod.THelper.GetItems(1);',
  26154. ' $mod.THelper.SetItems(2, false);',
  26155. ' return Result;',
  26156. ' };',
  26157. ' this.SetItems = function (Index, Value) {',
  26158. ' };',
  26159. '});',
  26160. 'this.w = 0;',
  26161. 'this.b = false;',
  26162. '']),
  26163. LinesToStr([ // $mod.$main
  26164. '$mod.b = $mod.THelper.GetItems(1);',
  26165. '$mod.THelper.SetItems(2, $mod.b);',
  26166. 'var $with = $mod.w;',
  26167. '$mod.b = $mod.THelper.GetItems(3);',
  26168. '$mod.THelper.SetItems(4, $mod.b);',
  26169. '']));
  26170. end;
  26171. procedure TTestModule.TestTypeHelper_ClassMethod;
  26172. begin
  26173. StartProgram(false);
  26174. Add([
  26175. '{$modeswitch typehelpers}',
  26176. 'type',
  26177. ' THelper = type helper for word',
  26178. ' class procedure DoStatic; static;',
  26179. ' end;',
  26180. 'class procedure THelper.DoStatic;',
  26181. 'begin',
  26182. ' DoStatic;',
  26183. ' DoStatic();',
  26184. 'end;',
  26185. 'var w: word;',
  26186. 'begin',
  26187. ' w.DoStatic;',
  26188. ' w.DoStatic();',
  26189. '']);
  26190. ConvertProgram;
  26191. CheckSource('TestTypeHelper_ClassMethod',
  26192. LinesToStr([ // statements
  26193. 'rtl.createHelper(this, "THelper", null, function () {',
  26194. ' this.DoStatic = function () {',
  26195. ' $mod.THelper.DoStatic();',
  26196. ' $mod.THelper.DoStatic();',
  26197. ' };',
  26198. '});',
  26199. 'this.w = 0;',
  26200. '']),
  26201. LinesToStr([ // $mod.$main
  26202. '$mod.THelper.DoStatic();',
  26203. '$mod.THelper.DoStatic();',
  26204. '']));
  26205. end;
  26206. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26207. begin
  26208. StartProgram(false);
  26209. Add([
  26210. '{$modeswitch typehelpers}',
  26211. 'type',
  26212. ' THelper = type helper for word',
  26213. ' procedure Run; external name ''Run'';',
  26214. ' end;',
  26215. 'var w: word;',
  26216. 'begin',
  26217. ' w.Run;',
  26218. '']);
  26219. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26220. ConvertProgram;
  26221. end;
  26222. procedure TTestModule.TestTypeHelper_Constructor;
  26223. begin
  26224. StartProgram(false);
  26225. Add([
  26226. '{$modeswitch typehelpers}',
  26227. 'type',
  26228. ' THelper = type helper for word',
  26229. ' constructor Init(e: longint);',
  26230. ' end;',
  26231. 'constructor THelper.Init(e: longint);',
  26232. 'begin',
  26233. ' Self:=e;',
  26234. ' Init(e+1);',
  26235. 'end;',
  26236. 'var w: word;',
  26237. 'begin',
  26238. ' w:=word.Init(2);',
  26239. ' w:=w.Init(3);',
  26240. ' with word do w:=Init(4);',
  26241. ' with w do w:=Init(5);',
  26242. '']);
  26243. ConvertProgram;
  26244. CheckSource('TestTypeHelper_Constructor',
  26245. LinesToStr([ // statements
  26246. 'rtl.createHelper(this, "THelper", null, function () {',
  26247. ' this.Init = function (e) {',
  26248. ' this.set(e);',
  26249. ' $mod.THelper.Init.call(this, e + 1);',
  26250. ' return this.get();',
  26251. ' };',
  26252. ' this.$new = function (fn, args) {',
  26253. ' return this[fn].apply({',
  26254. ' p: 0,',
  26255. ' get: function () {',
  26256. ' return this.p;',
  26257. ' },',
  26258. ' set: function (v) {',
  26259. ' this.p = v;',
  26260. ' }',
  26261. ' }, args);',
  26262. ' };',
  26263. '});',
  26264. 'this.w = 0;',
  26265. '']),
  26266. LinesToStr([ // $mod.$main
  26267. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26268. '$mod.w = $mod.THelper.Init.call({',
  26269. ' p: $mod,',
  26270. ' get: function () {',
  26271. ' return this.p.w;',
  26272. ' },',
  26273. ' set: function (v) {',
  26274. ' this.p.w = v;',
  26275. ' }',
  26276. '}, 3);',
  26277. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26278. 'var $with = $mod.w;',
  26279. '$mod.w = $mod.THelper.Init.call({',
  26280. ' get: function () {',
  26281. ' return $with;',
  26282. ' },',
  26283. ' set: function (v) {',
  26284. ' $with = v;',
  26285. ' }',
  26286. '}, 5);',
  26287. '']));
  26288. end;
  26289. procedure TTestModule.TestTypeHelper_Word;
  26290. begin
  26291. StartProgram(false);
  26292. Add([
  26293. '{$modeswitch typehelpers}',
  26294. 'type',
  26295. ' THelper = type helper for word',
  26296. ' procedure DoIt(e: byte = 123);',
  26297. ' end;',
  26298. 'procedure THelper.DoIt(e: byte);',
  26299. 'begin',
  26300. ' Self:=e;',
  26301. ' Self:=Self+1;',
  26302. ' with Self do Doit;',
  26303. 'end;',
  26304. 'begin',
  26305. ' word(3).DoIt;',
  26306. '']);
  26307. ConvertProgram;
  26308. CheckSource('TestTypeHelper_Word',
  26309. LinesToStr([ // statements
  26310. 'rtl.createHelper(this, "THelper", null, function () {',
  26311. ' this.DoIt = function (e) {',
  26312. ' this.set(e);',
  26313. ' this.set(this.get() + 1);',
  26314. ' var $with = this.get();',
  26315. ' $mod.THelper.DoIt.call(this, 123);',
  26316. ' };',
  26317. '});',
  26318. '']),
  26319. LinesToStr([ // $mod.$main
  26320. '$mod.THelper.DoIt.call({',
  26321. ' get: function () {',
  26322. ' return 3;',
  26323. ' },',
  26324. ' set: function (v) {',
  26325. ' rtl.raiseE("EPropReadOnly");',
  26326. ' }',
  26327. '}, 123);',
  26328. '']));
  26329. end;
  26330. procedure TTestModule.TestTypeHelper_Boolean;
  26331. begin
  26332. StartProgram(false);
  26333. Add([
  26334. '{$modeswitch typehelpers}',
  26335. 'type',
  26336. ' Integer = longint;',
  26337. ' THelper = type helper for boolean',
  26338. ' procedure Run(e: wordbool = true);',
  26339. ' end;',
  26340. 'procedure THelper.Run(e: wordbool);',
  26341. 'begin',
  26342. ' Self:=e;',
  26343. ' Self:=not Self;',
  26344. ' with Self do Run;',
  26345. ' if Integer(Self)=0 then ;',
  26346. 'end;',
  26347. 'begin',
  26348. ' boolean(3).Run;',
  26349. '']);
  26350. ConvertProgram;
  26351. CheckSource('TestTypeHelper_Boolean',
  26352. LinesToStr([ // statements
  26353. 'rtl.createHelper(this, "THelper", null, function () {',
  26354. ' this.Run = function (e) {',
  26355. ' this.set(e);',
  26356. ' this.set(!this.get());',
  26357. ' var $with = this.get();',
  26358. ' $mod.THelper.Run.call(this, true);',
  26359. ' if ((this.get() ? 1 : 0) === 0) ;',
  26360. ' };',
  26361. '});',
  26362. '']),
  26363. LinesToStr([ // $mod.$main
  26364. '$mod.THelper.Run.call({',
  26365. ' a: 3 != 0,',
  26366. ' get: function () {',
  26367. ' return this.a;',
  26368. ' },',
  26369. ' set: function (v) {',
  26370. ' rtl.raiseE("EPropReadOnly");',
  26371. ' }',
  26372. '}, true);',
  26373. '']));
  26374. end;
  26375. procedure TTestModule.TestTypeHelper_WordBool;
  26376. begin
  26377. StartProgram(false);
  26378. Add([
  26379. '{$modeswitch typehelpers}',
  26380. 'type',
  26381. ' Integer = longint;',
  26382. ' THelper = type helper for WordBool',
  26383. ' procedure Run(e: wordbool = true);',
  26384. ' end;',
  26385. 'procedure THelper.Run(e: wordbool);',
  26386. 'var i: integer;',
  26387. 'begin',
  26388. ' i:=Integer(Self);',
  26389. 'end;',
  26390. 'var w: wordbool;',
  26391. 'begin',
  26392. ' w.Run;',
  26393. ' wordbool(3).Run;',
  26394. '']);
  26395. ConvertProgram;
  26396. CheckSource('TestTypeHelper_WordBool',
  26397. LinesToStr([ // statements
  26398. 'rtl.createHelper(this, "THelper", null, function () {',
  26399. ' this.Run = function (e) {',
  26400. ' var i = 0;',
  26401. ' i = (this.get() ? 1 : 0);',
  26402. ' };',
  26403. '});',
  26404. 'this.w = false;',
  26405. '']),
  26406. LinesToStr([ // $mod.$main
  26407. '$mod.THelper.Run.call({',
  26408. ' p: $mod,',
  26409. ' get: function () {',
  26410. ' return this.p.w;',
  26411. ' },',
  26412. ' set: function (v) {',
  26413. ' this.p.w = v;',
  26414. ' }',
  26415. '}, true);',
  26416. '$mod.THelper.Run.call({',
  26417. ' a: 3 != 0,',
  26418. ' get: function () {',
  26419. ' return this.a;',
  26420. ' },',
  26421. ' set: function (v) {',
  26422. ' rtl.raiseE("EPropReadOnly");',
  26423. ' }',
  26424. '}, true);',
  26425. '']));
  26426. end;
  26427. procedure TTestModule.TestTypeHelper_Double;
  26428. begin
  26429. StartProgram(false);
  26430. Add([
  26431. '{$modeswitch typehelpers}',
  26432. 'type',
  26433. ' Float = type double;',
  26434. ' THelper = type helper for Float',
  26435. ' const NPI = 3.141592;',
  26436. ' function ToStr: String;',
  26437. ' end;',
  26438. 'function THelper.ToStr: String;',
  26439. 'begin',
  26440. 'end;',
  26441. 'procedure DoIt(s: string);',
  26442. 'begin',
  26443. 'end;',
  26444. 'var f: Float;',
  26445. 'begin',
  26446. ' DoIt(f.toStr);',
  26447. ' DoIt(f.toStr());',
  26448. ' (f*f).toStr;',
  26449. ' DoIt((f*f).toStr);',
  26450. '']);
  26451. ConvertProgram;
  26452. CheckSource('TestTypeHelper_Double',
  26453. LinesToStr([ // statements
  26454. 'rtl.createHelper(this, "THelper", null, function () {',
  26455. ' this.NPI = 3.141592;',
  26456. ' this.ToStr = function () {',
  26457. ' var Result = "";',
  26458. ' return Result;',
  26459. ' };',
  26460. '});',
  26461. 'this.DoIt = function (s) {',
  26462. '};',
  26463. 'this.f = 0.0;',
  26464. '']),
  26465. LinesToStr([ // $mod.$main
  26466. '$mod.DoIt($mod.THelper.ToStr.call({',
  26467. ' p: $mod,',
  26468. ' get: function () {',
  26469. ' return this.p.f;',
  26470. ' },',
  26471. ' set: function (v) {',
  26472. ' this.p.f = v;',
  26473. ' }',
  26474. '}));',
  26475. '$mod.DoIt($mod.THelper.ToStr.call({',
  26476. ' p: $mod,',
  26477. ' get: function () {',
  26478. ' return this.p.f;',
  26479. ' },',
  26480. ' set: function (v) {',
  26481. ' this.p.f = v;',
  26482. ' }',
  26483. '}));',
  26484. '$mod.THelper.ToStr.call({',
  26485. ' a: $mod.f * $mod.f,',
  26486. ' get: function () {',
  26487. ' return this.a;',
  26488. ' },',
  26489. ' set: function (v) {',
  26490. ' rtl.raiseE("EPropReadOnly");',
  26491. ' }',
  26492. '});',
  26493. '$mod.DoIt($mod.THelper.ToStr.call({',
  26494. ' a: $mod.f * $mod.f,',
  26495. ' get: function () {',
  26496. ' return this.a;',
  26497. ' },',
  26498. ' set: function (v) {',
  26499. ' rtl.raiseE("EPropReadOnly");',
  26500. ' }',
  26501. '}));',
  26502. '']));
  26503. end;
  26504. procedure TTestModule.TestTypeHelper_NativeInt;
  26505. begin
  26506. StartProgram(false);
  26507. Add([
  26508. '{$modeswitch typehelpers}',
  26509. 'type',
  26510. ' MaxInt = type nativeint;',
  26511. ' THelperI = type helper for MaxInt',
  26512. ' function ToStr: String;',
  26513. ' end;',
  26514. ' MaxUInt = type nativeuint;',
  26515. ' THelperU = type helper for MaxUInt',
  26516. ' function ToStr: String;',
  26517. ' end;',
  26518. 'function THelperI.ToStr: String;',
  26519. 'begin',
  26520. ' Result:=str(Self);',
  26521. 'end;',
  26522. 'function THelperU.ToStr: String;',
  26523. 'begin',
  26524. ' Result:=str(Self);',
  26525. 'end;',
  26526. 'procedure DoIt(s: string);',
  26527. 'begin',
  26528. 'end;',
  26529. 'var i: MaxInt;',
  26530. 'begin',
  26531. ' DoIt(i.toStr);',
  26532. ' DoIt(i.toStr());',
  26533. ' (i*i).toStr;',
  26534. ' DoIt((i*i).toStr);',
  26535. '']);
  26536. ConvertProgram;
  26537. CheckSource('TestTypeHelper_NativeInt',
  26538. LinesToStr([ // statements
  26539. 'rtl.createHelper(this, "THelperI", null, function () {',
  26540. ' this.ToStr = function () {',
  26541. ' var Result = "";',
  26542. ' Result = "" + this.get();',
  26543. ' return Result;',
  26544. ' };',
  26545. '});',
  26546. 'rtl.createHelper(this, "THelperU", null, function () {',
  26547. ' this.ToStr = function () {',
  26548. ' var Result = "";',
  26549. ' Result = "" + this.get();',
  26550. ' return Result;',
  26551. ' };',
  26552. '});',
  26553. 'this.DoIt = function (s) {',
  26554. '};',
  26555. 'this.i = 0;',
  26556. '']),
  26557. LinesToStr([ // $mod.$main
  26558. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26559. ' p: $mod,',
  26560. ' get: function () {',
  26561. ' return this.p.i;',
  26562. ' },',
  26563. ' set: function (v) {',
  26564. ' this.p.i = v;',
  26565. ' }',
  26566. '}));',
  26567. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26568. ' p: $mod,',
  26569. ' get: function () {',
  26570. ' return this.p.i;',
  26571. ' },',
  26572. ' set: function (v) {',
  26573. ' this.p.i = v;',
  26574. ' }',
  26575. '}));',
  26576. '$mod.THelperI.ToStr.call({',
  26577. ' a: $mod.i * $mod.i,',
  26578. ' get: function () {',
  26579. ' return this.a;',
  26580. ' },',
  26581. ' set: function (v) {',
  26582. ' rtl.raiseE("EPropReadOnly");',
  26583. ' }',
  26584. '});',
  26585. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26586. ' a: $mod.i * $mod.i,',
  26587. ' get: function () {',
  26588. ' return this.a;',
  26589. ' },',
  26590. ' set: function (v) {',
  26591. ' rtl.raiseE("EPropReadOnly");',
  26592. ' }',
  26593. '}));',
  26594. '']));
  26595. end;
  26596. procedure TTestModule.TestTypeHelper_StringChar;
  26597. begin
  26598. StartProgram(false);
  26599. Add([
  26600. '{$modeswitch typehelpers}',
  26601. 'type',
  26602. ' TStringHelper = type helper for string',
  26603. ' procedure DoIt(e: byte = 123);',
  26604. ' end;',
  26605. ' TCharHelper = type helper for char',
  26606. ' procedure Fly;',
  26607. ' end;',
  26608. 'procedure TStringHelper.DoIt(e: byte);',
  26609. 'begin',
  26610. ' Self[1]:=''c'';',
  26611. ' Self[2]:=Self[3];',
  26612. 'end;',
  26613. 'procedure TCharHelper.Fly;',
  26614. 'begin',
  26615. ' Self:=''c'';',
  26616. 'end;',
  26617. 'begin',
  26618. ' ''abc''.DoIt;',
  26619. ' ''xyz''.DoIt();',
  26620. ' ''c''.Fly();',
  26621. '']);
  26622. ConvertProgram;
  26623. CheckSource('TestTypeHelper_StringChar',
  26624. LinesToStr([ // statements
  26625. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26626. ' this.DoIt = function (e) {',
  26627. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26628. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26629. ' };',
  26630. '});',
  26631. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26632. ' this.Fly = function () {',
  26633. ' this.set("c");',
  26634. ' };',
  26635. '});',
  26636. '']),
  26637. LinesToStr([ // $mod.$main
  26638. '$mod.TStringHelper.DoIt.call({',
  26639. ' get: function () {',
  26640. ' return "abc";',
  26641. ' },',
  26642. ' set: function (v) {',
  26643. ' rtl.raiseE("EPropReadOnly");',
  26644. ' }',
  26645. '}, 123);',
  26646. '$mod.TStringHelper.DoIt.call({',
  26647. ' get: function () {',
  26648. ' return "xyz";',
  26649. ' },',
  26650. ' set: function (v) {',
  26651. ' rtl.raiseE("EPropReadOnly");',
  26652. ' }',
  26653. '}, 123);',
  26654. '$mod.TCharHelper.Fly.call({',
  26655. ' get: function () {',
  26656. ' return "c";',
  26657. ' },',
  26658. ' set: function (v) {',
  26659. ' rtl.raiseE("EPropReadOnly");',
  26660. ' }',
  26661. '});',
  26662. '']));
  26663. end;
  26664. procedure TTestModule.TestTypeHelper_JSValue;
  26665. begin
  26666. StartProgram(false);
  26667. Add([
  26668. '{$modeswitch typehelpers}',
  26669. 'type',
  26670. ' TExtValue = type jsvalue;',
  26671. ' THelper = type helper for TExtValue',
  26672. ' function ToStr: String;',
  26673. ' end;',
  26674. 'function THelper.ToStr: String;',
  26675. 'begin',
  26676. 'end;',
  26677. 'var',
  26678. ' s: string;',
  26679. ' v: TExtValue;',
  26680. 'begin',
  26681. ' s:=v.toStr;',
  26682. ' s:=v.toStr();',
  26683. ' TExtValue(s).toStr;',
  26684. '']);
  26685. ConvertProgram;
  26686. CheckSource('TestTypeHelper_JSValue',
  26687. LinesToStr([ // statements
  26688. 'rtl.createHelper(this, "THelper", null, function () {',
  26689. ' this.ToStr = function () {',
  26690. ' var Result = "";',
  26691. ' return Result;',
  26692. ' };',
  26693. '});',
  26694. 'this.s = "";',
  26695. 'this.v = undefined;',
  26696. '']),
  26697. LinesToStr([ // $mod.$main
  26698. '$mod.s = $mod.THelper.ToStr.call({',
  26699. ' p: $mod,',
  26700. ' get: function () {',
  26701. ' return this.p.v;',
  26702. ' },',
  26703. ' set: function (v) {',
  26704. ' this.p.v = v;',
  26705. ' }',
  26706. '});',
  26707. '$mod.s = $mod.THelper.ToStr.call({',
  26708. ' p: $mod,',
  26709. ' get: function () {',
  26710. ' return this.p.v;',
  26711. ' },',
  26712. ' set: function (v) {',
  26713. ' this.p.v = v;',
  26714. ' }',
  26715. '});',
  26716. '$mod.THelper.ToStr.call({',
  26717. ' p: $mod,',
  26718. ' get: function () {',
  26719. ' return this.p.s;',
  26720. ' },',
  26721. ' set: function (v) {',
  26722. ' rtl.raiseE("EPropReadOnly");',
  26723. ' }',
  26724. '});',
  26725. '']));
  26726. end;
  26727. procedure TTestModule.TestTypeHelper_Array;
  26728. begin
  26729. StartProgram(false);
  26730. Add([
  26731. '{$modeswitch typehelpers}',
  26732. 'type',
  26733. ' TArrOfBool = array of boolean;',
  26734. ' TArrOfJS = array of jsvalue;',
  26735. ' THelper = type helper for TArrOfBool',
  26736. ' procedure DoIt(e: byte = 123);',
  26737. ' end;',
  26738. 'procedure THelper.DoIt(e: byte);',
  26739. 'begin',
  26740. ' Self[1]:=true;',
  26741. ' Self[2]:=not Self[3];',
  26742. ' SetLength(Self,4);',
  26743. ' Self:=Concat(Self,[true]);',
  26744. 'end;',
  26745. 'var',
  26746. ' b: TArrOfBool;',
  26747. ' j: TArrOfJS;',
  26748. 'begin',
  26749. ' b.DoIt;',
  26750. ' TArrOfBool(j).DoIt();',
  26751. '']);
  26752. ConvertProgram;
  26753. CheckSource('TestTypeHelper_Array',
  26754. LinesToStr([ // statements
  26755. 'rtl.createHelper(this, "THelper", null, function () {',
  26756. ' this.DoIt = function (e) {',
  26757. ' this.get()[1] = true;',
  26758. ' this.get()[2] = !this.get()[3];',
  26759. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26760. ' this.set(rtl.arrayPushN(this.get(), true));',
  26761. ' };',
  26762. '});',
  26763. 'this.b = [];',
  26764. 'this.j = [];',
  26765. '']),
  26766. LinesToStr([ // $mod.$main
  26767. '$mod.THelper.DoIt.call({',
  26768. ' p: $mod,',
  26769. ' get: function () {',
  26770. ' return this.p.b;',
  26771. ' },',
  26772. ' set: function (v) {',
  26773. ' this.p.b = v;',
  26774. ' }',
  26775. '}, 123);',
  26776. '$mod.THelper.DoIt.call({',
  26777. ' p: $mod,',
  26778. ' get: function () {',
  26779. ' return this.p.j;',
  26780. ' },',
  26781. ' set: function (v) {',
  26782. ' this.p.j = v;',
  26783. ' }',
  26784. '}, 123);',
  26785. '']));
  26786. end;
  26787. procedure TTestModule.TestTypeHelper_EnumType;
  26788. begin
  26789. StartProgram(false);
  26790. Add([
  26791. '{$modeswitch typehelpers}',
  26792. 'type',
  26793. ' TEnum = (red,blue);',
  26794. ' THelper = type helper for TEnum',
  26795. ' procedure DoIt(e: byte = 123);',
  26796. ' class procedure Swing(w: word); static;',
  26797. ' end;',
  26798. 'procedure THelper.DoIt(e: byte);',
  26799. 'begin',
  26800. ' Self:=red;',
  26801. ' Self:=succ(Self);',
  26802. ' with Self do Doit;',
  26803. 'end;',
  26804. 'class procedure THelper.Swing(w: word);',
  26805. 'begin',
  26806. 'end;',
  26807. 'var e: TEnum;',
  26808. 'begin',
  26809. ' e.DoIt;',
  26810. ' red.DoIt;',
  26811. ' TEnum.blue.DoIt;',
  26812. ' TEnum(1).DoIt;',
  26813. ' TEnum.Swing(3);',
  26814. '']);
  26815. ConvertProgram;
  26816. CheckSource('TestTypeHelper_EnumType',
  26817. LinesToStr([ // statements
  26818. 'this.TEnum = {',
  26819. ' "0": "red",',
  26820. ' red: 0,',
  26821. ' "1": "blue",',
  26822. ' blue: 1',
  26823. '};',
  26824. 'rtl.createHelper(this, "THelper", null, function () {',
  26825. ' this.DoIt = function (e) {',
  26826. ' this.set($mod.TEnum.red);',
  26827. ' this.set(this.get() + 1);',
  26828. ' var $with = this.get();',
  26829. ' $mod.THelper.DoIt.call(this, 123);',
  26830. ' };',
  26831. ' this.Swing = function (w) {',
  26832. ' };',
  26833. '});',
  26834. 'this.e = 0;',
  26835. '']),
  26836. LinesToStr([ // $mod.$main
  26837. '$mod.THelper.DoIt.call({',
  26838. ' p: $mod,',
  26839. ' get: function () {',
  26840. ' return this.p.e;',
  26841. ' },',
  26842. ' set: function (v) {',
  26843. ' this.p.e = v;',
  26844. ' }',
  26845. '}, 123);',
  26846. '$mod.THelper.DoIt.call({',
  26847. ' p: $mod.TEnum,',
  26848. ' get: function () {',
  26849. ' return this.p.red;',
  26850. ' },',
  26851. ' set: function (v) {',
  26852. ' rtl.raiseE("EPropReadOnly");',
  26853. ' }',
  26854. '}, 123);',
  26855. '$mod.THelper.DoIt.call({',
  26856. ' p: $mod.TEnum,',
  26857. ' get: function () {',
  26858. ' return this.p.blue;',
  26859. ' },',
  26860. ' set: function (v) {',
  26861. ' rtl.raiseE("EPropReadOnly");',
  26862. ' }',
  26863. '}, 123);',
  26864. '$mod.THelper.DoIt.call({',
  26865. ' get: function () {',
  26866. ' return 1;',
  26867. ' },',
  26868. ' set: function (v) {',
  26869. ' rtl.raiseE("EPropReadOnly");',
  26870. ' }',
  26871. '}, 123);',
  26872. '$mod.THelper.Swing(3);',
  26873. '']));
  26874. end;
  26875. procedure TTestModule.TestTypeHelper_SetType;
  26876. begin
  26877. StartProgram(false);
  26878. Add([
  26879. '{$modeswitch typehelpers}',
  26880. 'type',
  26881. ' TEnum = (red,blue);',
  26882. ' TSetOfEnum = set of TEnum;',
  26883. ' THelper = type helper for TSetOfEnum',
  26884. ' procedure DoIt(e: byte = 123);',
  26885. ' constructor Init(e: TEnum);',
  26886. ' constructor InitEmpty;',
  26887. ' end;',
  26888. 'procedure THelper.DoIt(e: byte);',
  26889. 'begin',
  26890. ' Self:=[];',
  26891. ' Self:=[red];',
  26892. ' Include(Self,blue);',
  26893. 'end;',
  26894. 'constructor THelper.Init(e: TEnum);',
  26895. 'begin',
  26896. ' Self:=[];',
  26897. ' Self:=[e];',
  26898. ' Include(Self,blue);',
  26899. 'end;',
  26900. 'constructor THelper.InitEmpty;',
  26901. 'begin',
  26902. 'end;',
  26903. 'var s: TSetOfEnum;',
  26904. 'begin',
  26905. ' s.DoIt;',
  26906. //' [red].DoIt;',
  26907. //' with s do DoIt;',
  26908. //' with [red,blue] do DoIt;',
  26909. ' s:=TSetOfEnum.Init(blue);',
  26910. ' s:=s.Init(blue);',
  26911. '']);
  26912. ConvertProgram;
  26913. CheckSource('TestTypeHelper_SetType',
  26914. LinesToStr([ // statements
  26915. 'this.TEnum = {',
  26916. ' "0": "red",',
  26917. ' red: 0,',
  26918. ' "1": "blue",',
  26919. ' blue: 1',
  26920. '};',
  26921. 'rtl.createHelper(this, "THelper", null, function () {',
  26922. ' this.DoIt = function (e) {',
  26923. ' this.set({});',
  26924. ' this.set(rtl.createSet($mod.TEnum.red));',
  26925. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26926. ' };',
  26927. ' this.Init = function (e) {',
  26928. ' this.set({});',
  26929. ' this.set(rtl.createSet(e));',
  26930. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26931. ' return this.get();',
  26932. ' };',
  26933. ' this.InitEmpty = function () {',
  26934. ' return this.get();',
  26935. ' };',
  26936. ' this.$new = function (fn, args) {',
  26937. ' return this[fn].apply({',
  26938. ' p: {},',
  26939. ' get: function () {',
  26940. ' return this.p;',
  26941. ' },',
  26942. ' set: function (v) {',
  26943. ' this.p = v;',
  26944. ' }',
  26945. ' }, args);',
  26946. ' };',
  26947. '});',
  26948. 'this.s = {};',
  26949. '']),
  26950. LinesToStr([ // $mod.$main
  26951. '$mod.THelper.DoIt.call({',
  26952. ' p: $mod,',
  26953. ' get: function () {',
  26954. ' return this.p.s;',
  26955. ' },',
  26956. ' set: function (v) {',
  26957. ' this.p.s = v;',
  26958. ' }',
  26959. '}, 123);',
  26960. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26961. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26962. ' p: $mod,',
  26963. ' get: function () {',
  26964. ' return this.p.s;',
  26965. ' },',
  26966. ' set: function (v) {',
  26967. ' this.p.s = v;',
  26968. ' }',
  26969. '}, $mod.TEnum.blue));',
  26970. '']));
  26971. end;
  26972. procedure TTestModule.TestTypeHelper_InterfaceType;
  26973. begin
  26974. StartProgram(false);
  26975. Add([
  26976. '{$interfaces com}',
  26977. '{$modeswitch typehelpers}',
  26978. 'type',
  26979. ' IUnknown = interface',
  26980. ' function _AddRef: longint;',
  26981. ' function _Release: longint;',
  26982. ' end;',
  26983. ' TObject = class(IUnknown)',
  26984. ' function _AddRef: longint; virtual; abstract;',
  26985. ' function _Release: longint; virtual; abstract;',
  26986. ' end;',
  26987. ' THelper = type helper for IUnknown',
  26988. ' procedure Fly(e: byte = 123);',
  26989. ' class procedure Run; static;',
  26990. ' end;',
  26991. 'var',
  26992. ' i: IUnknown;',
  26993. ' o: TObject;',
  26994. 'procedure THelper.Fly(e: byte);',
  26995. 'begin',
  26996. ' i:=Self;',
  26997. ' o:=Self as TObject;',
  26998. ' Self:=nil;',
  26999. ' Self:=i;',
  27000. ' Self:=o;',
  27001. ' with Self do begin',
  27002. ' Fly;',
  27003. ' Fly();',
  27004. ' end;',
  27005. 'end;',
  27006. 'class procedure THelper.Run;',
  27007. 'var l: IUnknown;',
  27008. 'begin',
  27009. ' l.Fly;',
  27010. ' l.Fly();',
  27011. 'end;',
  27012. 'begin',
  27013. ' i.Fly;',
  27014. ' i.Fly();',
  27015. ' i.Run;',
  27016. ' i.Run();',
  27017. ' IUnknown.Run;',
  27018. ' IUnknown.Run();',
  27019. '']);
  27020. ConvertProgram;
  27021. CheckSource('TestTypeHelper_InterfaceType',
  27022. LinesToStr([ // statements
  27023. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27024. 'rtl.createClass(this, "TObject", null, function () {',
  27025. ' this.$init = function () {',
  27026. ' };',
  27027. ' this.$final = function () {',
  27028. ' };',
  27029. ' rtl.addIntf(this, $mod.IUnknown);',
  27030. '});',
  27031. 'rtl.createHelper(this, "THelper", null, function () {',
  27032. ' this.Fly = function (e) {',
  27033. ' var $ir = rtl.createIntfRefs();',
  27034. ' try {',
  27035. ' rtl.setIntfP($mod, "i", this.get());',
  27036. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27037. ' this.set(null);',
  27038. ' this.set($mod.i);',
  27039. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27040. ' var $with = this.get();',
  27041. ' $mod.THelper.Fly.call(this, 123);',
  27042. ' $mod.THelper.Fly.call(this, 123);',
  27043. ' } finally {',
  27044. ' $ir.free();',
  27045. ' };',
  27046. ' };',
  27047. ' this.Run = function () {',
  27048. ' var l = null;',
  27049. ' try {',
  27050. ' $mod.THelper.Fly.call({',
  27051. ' get: function () {',
  27052. ' return l;',
  27053. ' },',
  27054. ' set: function (v) {',
  27055. ' l = rtl.setIntfL(l, v);',
  27056. ' }',
  27057. ' }, 123);',
  27058. ' $mod.THelper.Fly.call({',
  27059. ' get: function () {',
  27060. ' return l;',
  27061. ' },',
  27062. ' set: function (v) {',
  27063. ' l = rtl.setIntfL(l, v);',
  27064. ' }',
  27065. ' }, 123);',
  27066. ' } finally {',
  27067. ' rtl._Release(l);',
  27068. ' };',
  27069. ' };',
  27070. '});',
  27071. 'this.i = null;',
  27072. 'this.o = null;',
  27073. '']),
  27074. LinesToStr([ // $mod.$main
  27075. '$mod.THelper.Fly.call({',
  27076. ' p: $mod,',
  27077. ' get: function () {',
  27078. ' return this.p.i;',
  27079. ' },',
  27080. ' set: function (v) {',
  27081. ' rtl.setIntfP(this.p, "i", v);',
  27082. ' }',
  27083. '}, 123);',
  27084. '$mod.THelper.Fly.call({',
  27085. ' p: $mod,',
  27086. ' get: function () {',
  27087. ' return this.p.i;',
  27088. ' },',
  27089. ' set: function (v) {',
  27090. ' rtl.setIntfP(this.p, "i", v);',
  27091. ' }',
  27092. '}, 123);',
  27093. '$mod.THelper.Run();',
  27094. '$mod.THelper.Run();',
  27095. '$mod.THelper.Run();',
  27096. '$mod.THelper.Run();',
  27097. '']));
  27098. end;
  27099. procedure TTestModule.TestTypeHelper_NestedSelf;
  27100. begin
  27101. StartProgram(false);
  27102. Add([
  27103. '{$modeswitch typehelpers}',
  27104. 'type',
  27105. ' THelper = type helper for string',
  27106. ' procedure Run(Value: string);',
  27107. ' end;',
  27108. 'procedure THelper.Run(Value: string);',
  27109. ' function Sub(i: nativeint): boolean;',
  27110. ' begin',
  27111. ' Result:=Self[i+1]=Value[i];',
  27112. ' end;',
  27113. 'begin',
  27114. ' if Self[3]=Value[4] then ;',
  27115. 'end;',
  27116. 'begin',
  27117. '']);
  27118. ConvertProgram;
  27119. CheckSource('TestTypeHelper_NestedSelf',
  27120. LinesToStr([ // statements
  27121. 'rtl.createHelper(this, "THelper", null, function () {',
  27122. ' this.Run = function (Value) {',
  27123. ' var $Self = this;',
  27124. ' function Sub(i) {',
  27125. ' var Result = false;',
  27126. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27127. ' return Result;',
  27128. ' };',
  27129. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27130. ' };',
  27131. '});',
  27132. '']),
  27133. LinesToStr([ // $mod.$main
  27134. '']));
  27135. end;
  27136. procedure TTestModule.TestProcType;
  27137. begin
  27138. StartProgram(false);
  27139. Add([
  27140. 'type',
  27141. ' TProcInt = procedure(vI: longint = 1);',
  27142. 'procedure DoIt(vJ: longint);',
  27143. 'begin end;',
  27144. 'var',
  27145. ' b: boolean;',
  27146. ' vP, vQ: tprocint;',
  27147. 'begin',
  27148. ' vp:=nil;',
  27149. ' vp:=vp;',
  27150. ' vp:=@doit;',
  27151. ' vp;',
  27152. ' vp();',
  27153. ' vp(2);',
  27154. ' b:=vp=nil;',
  27155. ' b:=nil=vp;',
  27156. ' b:=vp=vq;',
  27157. ' b:=vp=@doit;',
  27158. ' b:=@doit=vp;',
  27159. ' b:=vp<>nil;',
  27160. ' b:=nil<>vp;',
  27161. ' b:=vp<>vq;',
  27162. ' b:=vp<>@doit;',
  27163. ' b:=@doit<>vp;',
  27164. ' b:=Assigned(vp);',
  27165. ' if Assigned(vp) then ;']);
  27166. ConvertProgram;
  27167. CheckSource('TestProcType',
  27168. LinesToStr([ // statements
  27169. 'this.DoIt = function(vJ) {',
  27170. '};',
  27171. 'this.b = false;',
  27172. 'this.vP = null;',
  27173. 'this.vQ = null;'
  27174. ]),
  27175. LinesToStr([ // $mod.$main
  27176. '$mod.vP = null;',
  27177. '$mod.vP = $mod.vP;',
  27178. '$mod.vP = $mod.DoIt;',
  27179. '$mod.vP(1);',
  27180. '$mod.vP(1);',
  27181. '$mod.vP(2);',
  27182. '$mod.b = $mod.vP === null;',
  27183. '$mod.b = null === $mod.vP;',
  27184. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27185. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27186. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27187. '$mod.b = $mod.vP !== null;',
  27188. '$mod.b = null !== $mod.vP;',
  27189. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27190. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27191. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27192. '$mod.b = $mod.vP != null;',
  27193. 'if ($mod.vP != null) ;',
  27194. '']));
  27195. end;
  27196. procedure TTestModule.TestProcType_Arg;
  27197. begin
  27198. StartProgram(false);
  27199. Add([
  27200. 'type',
  27201. ' TProcInt = procedure(vI: longint = 1);',
  27202. 'procedure DoIt(vJ: longint); begin end;',
  27203. 'procedure DoSome(vP, vQ: TProcInt);',
  27204. 'var',
  27205. ' b: boolean;',
  27206. 'begin',
  27207. ' vp:=nil;',
  27208. ' vp:=vp;',
  27209. ' vp:=@doit;',
  27210. ' vp;',
  27211. ' vp();',
  27212. ' vp(2);',
  27213. ' b:=vp=nil;',
  27214. ' b:=nil=vp;',
  27215. ' b:=vp=vq;',
  27216. ' b:=vp=@doit;',
  27217. ' b:=@doit=vp;',
  27218. ' b:=vp<>nil;',
  27219. ' b:=nil<>vp;',
  27220. ' b:=vp<>vq;',
  27221. ' b:=vp<>@doit;',
  27222. ' b:=@doit<>vp;',
  27223. ' b:=Assigned(vp);',
  27224. ' if Assigned(vp) then ;',
  27225. 'end;',
  27226. 'begin',
  27227. ' DoSome(@DoIt,nil);']);
  27228. ConvertProgram;
  27229. CheckSource('TestProcType_Arg',
  27230. LinesToStr([ // statements
  27231. 'this.DoIt = function(vJ) {',
  27232. '};',
  27233. 'this.DoSome = function(vP, vQ) {',
  27234. ' var b = false;',
  27235. ' vP = null;',
  27236. ' vP = vP;',
  27237. ' vP = $mod.DoIt;',
  27238. ' vP(1);',
  27239. ' vP(1);',
  27240. ' vP(2);',
  27241. ' b = vP === null;',
  27242. ' b = null === vP;',
  27243. ' b = rtl.eqCallback(vP,vQ);',
  27244. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27245. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27246. ' b = vP !== null;',
  27247. ' b = null !== vP;',
  27248. ' b = !rtl.eqCallback(vP, vQ);',
  27249. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27250. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27251. ' b = vP != null;',
  27252. ' if (vP != null) ;',
  27253. '};',
  27254. '']),
  27255. LinesToStr([ // $mod.$main
  27256. '$mod.DoSome($mod.DoIt,null);',
  27257. '']));
  27258. end;
  27259. procedure TTestModule.TestProcType_FunctionFPC;
  27260. begin
  27261. StartProgram(false);
  27262. Add('type');
  27263. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27264. Add('function DoIt(vI: longint): longint;');
  27265. Add('begin end;');
  27266. Add('var');
  27267. Add(' b: boolean;');
  27268. Add(' vP, vQ: tfuncint;');
  27269. Add('begin');
  27270. Add(' vp:=nil;');
  27271. Add(' vp:=vp;');
  27272. Add(' vp:=@doit;'); // ok in fpc and delphi
  27273. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27274. Add(' vp;'); // ok in fpc and delphi
  27275. Add(' vp();');
  27276. Add(' vp(2);');
  27277. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27278. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27279. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27280. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27281. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27282. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27283. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27284. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27285. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27286. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27287. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27288. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27289. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27290. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27291. Add(' b:=Assigned(vp);');
  27292. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27293. Add(' doit(vp());'); // ok in fpc and delphi
  27294. Add(' doit(vp(2));'); // ok in fpc and delphi
  27295. ConvertProgram;
  27296. CheckSource('TestProcType_FunctionFPC',
  27297. LinesToStr([ // statements
  27298. 'this.DoIt = function(vI) {',
  27299. ' var Result = 0;',
  27300. ' return Result;',
  27301. '};',
  27302. 'this.b = false;',
  27303. 'this.vP = null;',
  27304. 'this.vQ = null;'
  27305. ]),
  27306. LinesToStr([ // $mod.$main
  27307. '$mod.vP = null;',
  27308. '$mod.vP = $mod.vP;',
  27309. '$mod.vP = $mod.DoIt;',
  27310. '$mod.vP(1);',
  27311. '$mod.vP(1);',
  27312. '$mod.vP(2);',
  27313. '$mod.b = $mod.vP === null;',
  27314. '$mod.b = null === $mod.vP;',
  27315. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27316. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27317. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27318. '$mod.b = 4 === $mod.vP(1);',
  27319. '$mod.b = $mod.vP !== null;',
  27320. '$mod.b = null !== $mod.vP;',
  27321. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27322. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27323. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27324. '$mod.b = 6 !== $mod.vP(1);',
  27325. '$mod.b = $mod.vP != null;',
  27326. '$mod.DoIt($mod.vP(1));',
  27327. '$mod.DoIt($mod.vP(2));',
  27328. '']));
  27329. end;
  27330. procedure TTestModule.TestProcType_FunctionDelphi;
  27331. begin
  27332. StartProgram(false);
  27333. Add('{$mode Delphi}');
  27334. Add('type');
  27335. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27336. Add('function DoIt(vI: longint): longint;');
  27337. Add('begin end;');
  27338. Add('var');
  27339. Add(' b: boolean;');
  27340. Add(' vP, vQ: tfuncint;');
  27341. Add('begin');
  27342. Add(' vp:=nil;');
  27343. Add(' vp:=vp;');
  27344. Add(' vp:=@doit;'); // ok in fpc and delphi
  27345. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27346. Add(' vp;'); // ok in fpc and delphi
  27347. Add(' vp();');
  27348. Add(' vp(2);');
  27349. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27350. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27351. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27352. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27353. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27354. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27355. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27356. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27357. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27358. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27359. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27360. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27361. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27362. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27363. Add(' b:=Assigned(vp);');
  27364. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27365. Add(' doit(vp());'); // ok in fpc and delphi
  27366. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27367. ConvertProgram;
  27368. CheckSource('TestProcType_FunctionDelphi',
  27369. LinesToStr([ // statements
  27370. 'this.DoIt = function(vI) {',
  27371. ' var Result = 0;',
  27372. ' return Result;',
  27373. '};',
  27374. 'this.b = false;',
  27375. 'this.vP = null;',
  27376. 'this.vQ = null;'
  27377. ]),
  27378. LinesToStr([ // $mod.$main
  27379. '$mod.vP = null;',
  27380. '$mod.vP = $mod.vP;',
  27381. '$mod.vP = $mod.DoIt;',
  27382. '$mod.vP = $mod.DoIt;',
  27383. '$mod.vP(1);',
  27384. '$mod.vP(1);',
  27385. '$mod.vP(2);',
  27386. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27387. '$mod.b = $mod.vP(1) === 3;',
  27388. '$mod.b = 4 === $mod.vP(1);',
  27389. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27390. '$mod.b = $mod.vP(1) !== 5;',
  27391. '$mod.b = 6 !== $mod.vP(1);',
  27392. '$mod.b = $mod.vP != null;',
  27393. '$mod.DoIt($mod.vP(1));',
  27394. '$mod.DoIt($mod.vP(1));',
  27395. '$mod.DoIt($mod.vP(2));',
  27396. '']));
  27397. end;
  27398. procedure TTestModule.TestProcType_ProcedureDelphi;
  27399. begin
  27400. StartProgram(false);
  27401. Add('{$mode Delphi}');
  27402. Add('type');
  27403. Add(' TProc = procedure;');
  27404. Add('procedure DoIt;');
  27405. Add('begin end;');
  27406. Add('var');
  27407. Add(' b: boolean;');
  27408. Add(' vP, vQ: tproc;');
  27409. Add('begin');
  27410. Add(' vp:=nil;');
  27411. Add(' vp:=vp;');
  27412. Add(' vp:=vq;');
  27413. 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
  27414. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27415. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27416. Add(' vp;'); // ok in fpc and delphi
  27417. Add(' vp();');
  27418. // equal
  27419. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27420. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27421. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27422. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27423. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27424. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27425. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27426. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27427. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27428. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27429. // unequal
  27430. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27431. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27432. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27433. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27434. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27435. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27436. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27437. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27438. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27439. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27440. Add(' b:=Assigned(vp);');
  27441. ConvertProgram;
  27442. CheckSource('TestProcType_ProcedureDelphi',
  27443. LinesToStr([ // statements
  27444. 'this.DoIt = function() {',
  27445. '};',
  27446. 'this.b = false;',
  27447. 'this.vP = null;',
  27448. 'this.vQ = null;'
  27449. ]),
  27450. LinesToStr([ // $mod.$main
  27451. '$mod.vP = null;',
  27452. '$mod.vP = $mod.vP;',
  27453. '$mod.vP = $mod.vQ;',
  27454. '$mod.vP = $mod.DoIt;',
  27455. '$mod.vP = $mod.DoIt;',
  27456. '$mod.vP();',
  27457. '$mod.vP();',
  27458. '$mod.b = $mod.vP === null;',
  27459. '$mod.b = null === $mod.vP;',
  27460. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27461. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27462. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27463. '$mod.b = $mod.vP !== null;',
  27464. '$mod.b = null !== $mod.vP;',
  27465. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27466. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27467. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27468. '$mod.b = $mod.vP != null;',
  27469. '']));
  27470. end;
  27471. procedure TTestModule.TestProcType_AsParam;
  27472. begin
  27473. StartProgram(false);
  27474. Add('type');
  27475. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27476. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27477. Add('var vJ: tfuncint;');
  27478. Add('begin');
  27479. Add(' vg:=vg;');
  27480. Add(' vj:=vh;');
  27481. Add(' vi:=vi;');
  27482. Add(' doit(vg,vg,vg);');
  27483. Add(' doit(vh,vh,vj);');
  27484. Add(' doit(vi,vi,vi);');
  27485. Add(' doit(vj,vj,vj);');
  27486. Add('end;');
  27487. Add('var i: tfuncint;');
  27488. Add('begin');
  27489. Add(' doit(i,i,i);');
  27490. ConvertProgram;
  27491. CheckSource('TestProcType_AsParam',
  27492. LinesToStr([ // statements
  27493. 'this.DoIt = function (vG,vH,vI) {',
  27494. ' var vJ = null;',
  27495. ' vG = vG;',
  27496. ' vJ = vH;',
  27497. ' vI.set(vI.get());',
  27498. ' $mod.DoIt(vG, vG, {',
  27499. ' get: function () {',
  27500. ' return vG;',
  27501. ' },',
  27502. ' set: function (v) {',
  27503. ' vG = v;',
  27504. ' }',
  27505. ' });',
  27506. ' $mod.DoIt(vH, vH, {',
  27507. ' get: function () {',
  27508. ' return vJ;',
  27509. ' },',
  27510. ' set: function (v) {',
  27511. ' vJ = v;',
  27512. ' }',
  27513. ' });',
  27514. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27515. ' $mod.DoIt(vJ, vJ, {',
  27516. ' get: function () {',
  27517. ' return vJ;',
  27518. ' },',
  27519. ' set: function (v) {',
  27520. ' vJ = v;',
  27521. ' }',
  27522. ' });',
  27523. '};',
  27524. 'this.i = null;'
  27525. ]),
  27526. LinesToStr([
  27527. '$mod.DoIt($mod.i,$mod.i,{',
  27528. ' p: $mod,',
  27529. ' get: function () {',
  27530. ' return this.p.i;',
  27531. ' },',
  27532. ' set: function (v) {',
  27533. ' this.p.i = v;',
  27534. ' }',
  27535. '});'
  27536. ]));
  27537. end;
  27538. procedure TTestModule.TestProcType_MethodFPC;
  27539. begin
  27540. StartProgram(false);
  27541. Add('type');
  27542. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27543. Add(' TObject = class');
  27544. Add(' function DoIt(vA: longint = 1): longint;');
  27545. Add(' end;');
  27546. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27547. Add('begin');
  27548. Add('end;');
  27549. Add('var');
  27550. Add(' Obj: TObject;');
  27551. Add(' vP: tfuncint;');
  27552. Add(' b: boolean;');
  27553. Add('begin');
  27554. Add(' vp:[email protected];'); // ok in fpc and delphi
  27555. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27556. Add(' vp;'); // ok in fpc and delphi
  27557. Add(' vp();');
  27558. Add(' vp(2);');
  27559. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27560. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27561. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27562. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27563. ConvertProgram;
  27564. CheckSource('TestProcType_MethodFPC',
  27565. LinesToStr([ // statements
  27566. 'rtl.createClass(this, "TObject", null, function () {',
  27567. ' this.$init = function () {',
  27568. ' };',
  27569. ' this.$final = function () {',
  27570. ' };',
  27571. ' this.DoIt = function (vA) {',
  27572. ' var Result = 0;',
  27573. ' return Result;',
  27574. ' };',
  27575. '});',
  27576. 'this.Obj = null;',
  27577. 'this.vP = null;',
  27578. 'this.b = false;'
  27579. ]),
  27580. LinesToStr([
  27581. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27582. '$mod.vP(1);',
  27583. '$mod.vP(1);',
  27584. '$mod.vP(2);',
  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. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27588. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27589. '']));
  27590. end;
  27591. procedure TTestModule.TestProcType_MethodDelphi;
  27592. begin
  27593. StartProgram(false);
  27594. Add([
  27595. '{$mode delphi}',
  27596. 'type',
  27597. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27598. ' TObject = class',
  27599. ' function DoIt(vA: longint = 1): longint;',
  27600. ' end;',
  27601. 'function TObject.DoIt(vA: longint = 1): longint;',
  27602. 'begin',
  27603. 'end;',
  27604. 'var',
  27605. ' Obj: TObject;',
  27606. ' vP: tfuncint;',
  27607. ' b: boolean;',
  27608. 'begin',
  27609. ' vp:[email protected];', // ok in fpc and delphi
  27610. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27611. ' vp;', // ok in fpc and delphi
  27612. ' vp();',
  27613. ' vp(2);',
  27614. //' b:[email protected];', // ok in fpc, illegal in delphi
  27615. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27616. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27617. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27618. '']);
  27619. ConvertProgram;
  27620. CheckSource('TestProcType_MethodDelphi',
  27621. LinesToStr([ // statements
  27622. 'rtl.createClass(this, "TObject", null, function () {',
  27623. ' this.$init = function () {',
  27624. ' };',
  27625. ' this.$final = function () {',
  27626. ' };',
  27627. ' this.DoIt = function (vA) {',
  27628. ' var Result = 0;',
  27629. ' return Result;',
  27630. ' };',
  27631. '});',
  27632. 'this.Obj = null;',
  27633. 'this.vP = null;',
  27634. 'this.b = false;'
  27635. ]),
  27636. LinesToStr([
  27637. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27638. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27639. '$mod.vP(1);',
  27640. '$mod.vP(1);',
  27641. '$mod.vP(2);',
  27642. '']));
  27643. end;
  27644. procedure TTestModule.TestProcType_PropertyFPC;
  27645. begin
  27646. StartProgram(false);
  27647. Add('type');
  27648. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27649. Add(' TObject = class');
  27650. Add(' FOnFoo: TFuncInt;');
  27651. Add(' function DoIt(vA: longint = 1): longint;');
  27652. Add(' function GetFoo: TFuncInt;');
  27653. Add(' procedure SetFoo(const Value: TFuncInt);');
  27654. Add(' function GetEvents(Index: longint): TFuncInt;');
  27655. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27656. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27657. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27658. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27659. Add(' end;');
  27660. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27661. Add('function tobject.getfoo: tfuncint; begin end;');
  27662. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27663. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27664. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27665. Add('var');
  27666. Add(' Obj: TObject;');
  27667. Add(' vP: tfuncint;');
  27668. Add(' b: boolean;');
  27669. Add('begin');
  27670. Add(' obj.onfoo:=nil;');
  27671. Add(' obj.onbar:=nil;');
  27672. Add(' obj.events[1]:=nil;');
  27673. Add(' obj.onfoo:=obj.onfoo;');
  27674. Add(' obj.onbar:=obj.onbar;');
  27675. Add(' obj.events[2]:=obj.events[3];');
  27676. Add(' obj.onfoo:[email protected];');
  27677. Add(' obj.onbar:[email protected];');
  27678. Add(' obj.events[4]:[email protected];');
  27679. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27680. //Add(' obj.onbar:=obj.doit;'); // delphi
  27681. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27682. Add(' obj.onfoo;');
  27683. Add(' obj.onbar;');
  27684. //Add(' obj.events[5];'); ToDo in pasresolver
  27685. Add(' obj.onfoo();');
  27686. Add(' obj.onbar();');
  27687. Add(' obj.events[6]();');
  27688. Add(' b:=obj.onfoo=nil;');
  27689. Add(' b:=obj.onbar=nil;');
  27690. Add(' b:=obj.events[7]=nil;');
  27691. Add(' b:=obj.onfoo<>nil;');
  27692. Add(' b:=obj.onbar<>nil;');
  27693. Add(' b:=obj.events[8]<>nil;');
  27694. Add(' b:=obj.onfoo=vp;');
  27695. Add(' b:=obj.onbar=vp;');
  27696. Add(' b:=obj.events[9]=vp;');
  27697. Add(' b:=obj.onfoo=obj.onfoo;');
  27698. Add(' b:=obj.onbar=obj.onfoo;');
  27699. Add(' b:=obj.events[10]=obj.onfoo;');
  27700. Add(' b:=obj.onfoo<>obj.onfoo;');
  27701. Add(' b:=obj.onbar<>obj.onfoo;');
  27702. Add(' b:=obj.events[11]<>obj.onfoo;');
  27703. Add(' b:[email protected];');
  27704. Add(' b:[email protected];');
  27705. Add(' b:=obj.events[12][email protected];');
  27706. Add(' b:=obj.onfoo<>@obj.doit;');
  27707. Add(' b:=obj.onbar<>@obj.doit;');
  27708. Add(' b:=obj.events[12]<>@obj.doit;');
  27709. Add(' b:=Assigned(obj.onfoo);');
  27710. Add(' b:=Assigned(obj.onbar);');
  27711. Add(' b:=Assigned(obj.events[13]);');
  27712. ConvertProgram;
  27713. CheckSource('TestProcType_PropertyFPC',
  27714. LinesToStr([ // statements
  27715. 'rtl.createClass(this, "TObject", null, function () {',
  27716. ' this.$init = function () {',
  27717. ' this.FOnFoo = null;',
  27718. ' };',
  27719. ' this.$final = function () {',
  27720. ' this.FOnFoo = undefined;',
  27721. ' };',
  27722. ' this.DoIt = function (vA) {',
  27723. ' var Result = 0;',
  27724. ' return Result;',
  27725. ' };',
  27726. 'this.GetFoo = function () {',
  27727. ' var Result = null;',
  27728. ' return Result;',
  27729. '};',
  27730. 'this.SetFoo = function (Value) {',
  27731. '};',
  27732. 'this.GetEvents = function (Index) {',
  27733. ' var Result = null;',
  27734. ' return Result;',
  27735. '};',
  27736. 'this.SetEvents = function (Index, Value) {',
  27737. '};',
  27738. '});',
  27739. 'this.Obj = null;',
  27740. 'this.vP = null;',
  27741. 'this.b = false;'
  27742. ]),
  27743. LinesToStr([
  27744. '$mod.Obj.FOnFoo = null;',
  27745. '$mod.Obj.SetFoo(null);',
  27746. '$mod.Obj.SetEvents(1, null);',
  27747. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27748. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27749. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27750. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27751. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27752. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27753. '$mod.Obj.FOnFoo(1);',
  27754. '$mod.Obj.GetFoo();',
  27755. '$mod.Obj.FOnFoo(1);',
  27756. '$mod.Obj.GetFoo()(1);',
  27757. '$mod.Obj.GetEvents(6)(1);',
  27758. '$mod.b = $mod.Obj.FOnFoo === null;',
  27759. '$mod.b = $mod.Obj.GetFoo() === null;',
  27760. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27761. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27762. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27763. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27764. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27765. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27766. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27767. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27768. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27769. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27770. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27771. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27772. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27773. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27774. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27775. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27776. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27777. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27778. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27779. '$mod.b = $mod.Obj.FOnFoo != null;',
  27780. '$mod.b = $mod.Obj.GetFoo() != null;',
  27781. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27782. '']));
  27783. end;
  27784. procedure TTestModule.TestProcType_PropertyDelphi;
  27785. begin
  27786. StartProgram(false);
  27787. Add('{$mode delphi}');
  27788. Add('type');
  27789. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27790. Add(' TObject = class');
  27791. Add(' FOnFoo: TFuncInt;');
  27792. Add(' function DoIt(vA: longint = 1): longint;');
  27793. Add(' function GetFoo: TFuncInt;');
  27794. Add(' procedure SetFoo(const Value: TFuncInt);');
  27795. Add(' function GetEvents(Index: longint): TFuncInt;');
  27796. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27797. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27798. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27799. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27800. Add(' end;');
  27801. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27802. Add('function tobject.getfoo: tfuncint; begin end;');
  27803. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27804. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27805. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27806. Add('var');
  27807. Add(' Obj: TObject;');
  27808. Add(' vP: tfuncint;');
  27809. Add(' b: boolean;');
  27810. Add('begin');
  27811. Add(' obj.onfoo:=nil;');
  27812. Add(' obj.onbar:=nil;');
  27813. Add(' obj.events[1]:=nil;');
  27814. Add(' obj.onfoo:=obj.onfoo;');
  27815. Add(' obj.onbar:=obj.onbar;');
  27816. Add(' obj.events[2]:=obj.events[3];');
  27817. Add(' obj.onfoo:[email protected];');
  27818. Add(' obj.onbar:[email protected];');
  27819. Add(' obj.events[4]:[email protected];');
  27820. Add(' obj.onfoo:=obj.doit;'); // delphi
  27821. Add(' obj.onbar:=obj.doit;'); // delphi
  27822. Add(' obj.events[4]:=obj.doit;'); // delphi
  27823. Add(' obj.onfoo;');
  27824. Add(' obj.onbar;');
  27825. //Add(' obj.events[5];'); ToDo in pasresolver
  27826. Add(' obj.onfoo();');
  27827. Add(' obj.onbar();');
  27828. Add(' obj.events[6]();');
  27829. //Add(' b:=obj.onfoo=nil;'); // fpc
  27830. //Add(' b:=obj.onbar=nil;'); // fpc
  27831. //Add(' b:=obj.events[7]=nil;'); // fpc
  27832. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27833. //Add(' b:=obj.onbar<>nil;'); // fpc
  27834. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27835. Add(' b:=obj.onfoo=vp;');
  27836. Add(' b:=obj.onbar=vp;');
  27837. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27838. Add(' b:=obj.onfoo=obj.onfoo;');
  27839. Add(' b:=obj.onbar=obj.onfoo;');
  27840. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27841. Add(' b:=obj.onfoo<>obj.onfoo;');
  27842. Add(' b:=obj.onbar<>obj.onfoo;');
  27843. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27844. //Add(' b:[email protected];'); // fpc
  27845. //Add(' b:[email protected];'); // fpc
  27846. //Add(' b:=obj.events[12][email protected];'); // fpc
  27847. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27848. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27849. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27850. Add(' b:=Assigned(obj.onfoo);');
  27851. Add(' b:=Assigned(obj.onbar);');
  27852. Add(' b:=Assigned(obj.events[13]);');
  27853. ConvertProgram;
  27854. CheckSource('TestProcType_PropertyDelphi',
  27855. LinesToStr([ // statements
  27856. 'rtl.createClass(this, "TObject", null, function () {',
  27857. ' this.$init = function () {',
  27858. ' this.FOnFoo = null;',
  27859. ' };',
  27860. ' this.$final = function () {',
  27861. ' this.FOnFoo = undefined;',
  27862. ' };',
  27863. ' this.DoIt = function (vA) {',
  27864. ' var Result = 0;',
  27865. ' return Result;',
  27866. ' };',
  27867. 'this.GetFoo = function () {',
  27868. ' var Result = null;',
  27869. ' return Result;',
  27870. '};',
  27871. 'this.SetFoo = function (Value) {',
  27872. '};',
  27873. 'this.GetEvents = function (Index) {',
  27874. ' var Result = null;',
  27875. ' return Result;',
  27876. '};',
  27877. 'this.SetEvents = function (Index, Value) {',
  27878. '};',
  27879. '});',
  27880. 'this.Obj = null;',
  27881. 'this.vP = null;',
  27882. 'this.b = false;'
  27883. ]),
  27884. LinesToStr([
  27885. '$mod.Obj.FOnFoo = null;',
  27886. '$mod.Obj.SetFoo(null);',
  27887. '$mod.Obj.SetEvents(1, null);',
  27888. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27889. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27890. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27891. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27892. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27893. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27894. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27895. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27896. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27897. '$mod.Obj.FOnFoo(1);',
  27898. '$mod.Obj.GetFoo();',
  27899. '$mod.Obj.FOnFoo(1);',
  27900. '$mod.Obj.GetFoo()(1);',
  27901. '$mod.Obj.GetEvents(6)(1);',
  27902. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27903. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(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(1) !== $mod.Obj.FOnFoo(1);',
  27907. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27908. '$mod.b = $mod.Obj.FOnFoo != null;',
  27909. '$mod.b = $mod.Obj.GetFoo() != null;',
  27910. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27911. '']));
  27912. end;
  27913. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27914. begin
  27915. StartProgram(false);
  27916. Add('type');
  27917. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27918. Add(' TObject = class');
  27919. Add(' FOnFoo: TFuncInt;');
  27920. Add(' function DoIt(vA: longint = 1): longint;');
  27921. Add(' function GetFoo: TFuncInt;');
  27922. Add(' procedure SetFoo(const Value: TFuncInt);');
  27923. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27924. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27925. Add(' end;');
  27926. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27927. Add('function tobject.getfoo: tfuncint; begin end;');
  27928. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27929. Add('var');
  27930. Add(' Obj: TObject;');
  27931. Add(' vP: tfuncint;');
  27932. Add(' b: boolean;');
  27933. Add('begin');
  27934. Add('with obj do begin');
  27935. Add(' fonfoo:=nil;');
  27936. Add(' onfoo:=nil;');
  27937. Add(' onbar:=nil;');
  27938. Add(' fonfoo:=fonfoo;');
  27939. Add(' onfoo:=onfoo;');
  27940. Add(' onbar:=onbar;');
  27941. Add(' fonfoo:=@doit;');
  27942. Add(' onfoo:=@doit;');
  27943. Add(' onbar:=@doit;');
  27944. //Add(' fonfoo:=doit;'); // delphi
  27945. //Add(' onfoo:=doit;'); // delphi
  27946. //Add(' onbar:=doit;'); // delphi
  27947. Add(' fonfoo;');
  27948. Add(' onfoo;');
  27949. Add(' onbar;');
  27950. Add(' fonfoo();');
  27951. Add(' onfoo();');
  27952. Add(' onbar();');
  27953. Add(' b:=fonfoo=nil;');
  27954. Add(' b:=onfoo=nil;');
  27955. Add(' b:=onbar=nil;');
  27956. Add(' b:=fonfoo<>nil;');
  27957. Add(' b:=onfoo<>nil;');
  27958. Add(' b:=onbar<>nil;');
  27959. Add(' b:=fonfoo=vp;');
  27960. Add(' b:=onfoo=vp;');
  27961. Add(' b:=onbar=vp;');
  27962. Add(' b:=fonfoo=fonfoo;');
  27963. Add(' b:=onfoo=onfoo;');
  27964. Add(' b:=onbar=onfoo;');
  27965. Add(' b:=fonfoo<>fonfoo;');
  27966. Add(' b:=onfoo<>onfoo;');
  27967. Add(' b:=onbar<>onfoo;');
  27968. Add(' b:=fonfoo=@doit;');
  27969. Add(' b:=onfoo=@doit;');
  27970. Add(' b:=onbar=@doit;');
  27971. Add(' b:=fonfoo<>@doit;');
  27972. Add(' b:=onfoo<>@doit;');
  27973. Add(' b:=onbar<>@doit;');
  27974. Add(' b:=Assigned(fonfoo);');
  27975. Add(' b:=Assigned(onfoo);');
  27976. Add(' b:=Assigned(onbar);');
  27977. Add('end;');
  27978. ConvertProgram;
  27979. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27980. LinesToStr([ // statements
  27981. 'rtl.createClass(this, "TObject", null, function () {',
  27982. ' this.$init = function () {',
  27983. ' this.FOnFoo = null;',
  27984. ' };',
  27985. ' this.$final = function () {',
  27986. ' this.FOnFoo = undefined;',
  27987. ' };',
  27988. ' this.DoIt = function (vA) {',
  27989. ' var Result = 0;',
  27990. ' return Result;',
  27991. ' };',
  27992. ' this.GetFoo = function () {',
  27993. ' var Result = null;',
  27994. ' return Result;',
  27995. ' };',
  27996. ' this.SetFoo = function (Value) {',
  27997. ' };',
  27998. '});',
  27999. 'this.Obj = null;',
  28000. 'this.vP = null;',
  28001. 'this.b = false;'
  28002. ]),
  28003. LinesToStr([
  28004. 'var $with = $mod.Obj;',
  28005. '$with.FOnFoo = null;',
  28006. '$with.FOnFoo = null;',
  28007. '$with.SetFoo(null);',
  28008. '$with.FOnFoo = $with.FOnFoo;',
  28009. '$with.FOnFoo = $with.FOnFoo;',
  28010. '$with.SetFoo($with.GetFoo());',
  28011. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28012. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28013. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28014. '$with.FOnFoo(1);',
  28015. '$with.FOnFoo(1);',
  28016. '$with.GetFoo();',
  28017. '$with.FOnFoo(1);',
  28018. '$with.FOnFoo(1);',
  28019. '$with.GetFoo()(1);',
  28020. '$mod.b = $with.FOnFoo === null;',
  28021. '$mod.b = $with.FOnFoo === null;',
  28022. '$mod.b = $with.GetFoo() === null;',
  28023. '$mod.b = $with.FOnFoo !== null;',
  28024. '$mod.b = $with.FOnFoo !== null;',
  28025. '$mod.b = $with.GetFoo() !== null;',
  28026. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28027. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28028. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28029. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28030. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28031. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28032. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28033. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28034. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28035. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28036. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28037. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28038. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28039. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28040. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28041. '$mod.b = $with.FOnFoo != null;',
  28042. '$mod.b = $with.FOnFoo != null;',
  28043. '$mod.b = $with.GetFoo() != null;',
  28044. '']));
  28045. end;
  28046. procedure TTestModule.TestProcType_Nested;
  28047. begin
  28048. StartProgram(false);
  28049. Add([
  28050. 'type',
  28051. ' TProcInt = procedure(vI: longint = 1);',
  28052. 'procedure DoIt(vJ: longint);',
  28053. 'var aProc: TProcInt;',
  28054. ' b: boolean;',
  28055. ' procedure Sub(vK: longint);',
  28056. ' var aSub: TProcInt;',
  28057. ' procedure SubSub(vK: longint);',
  28058. ' var aSubSub: TProcInt;',
  28059. ' begin;',
  28060. ' aProc:=@DoIt;',
  28061. ' aSub:=@DoIt;',
  28062. ' aSubSub:=@DoIt;',
  28063. ' aProc:=@Sub;',
  28064. ' aSub:=@Sub;',
  28065. ' aSubSub:=@Sub;',
  28066. ' aProc:=@SubSub;',
  28067. ' aSub:=@SubSub;',
  28068. ' aSubSub:=@SubSub;',
  28069. ' end;',
  28070. ' begin;',
  28071. ' end;',
  28072. 'begin;',
  28073. ' aProc:=@Sub;',
  28074. ' b:=aProc=@Sub;',
  28075. ' b:=@Sub=aProc;',
  28076. 'end;',
  28077. 'begin',
  28078. '']);
  28079. ConvertProgram;
  28080. CheckSource('TestProcType_Nested',
  28081. LinesToStr([ // statements
  28082. 'this.DoIt = function (vJ) {',
  28083. ' var aProc = null;',
  28084. ' var b = false;',
  28085. ' function Sub(vK) {',
  28086. ' var aSub = null;',
  28087. ' function SubSub(vK) {',
  28088. ' var aSubSub = null;',
  28089. ' aProc = $mod.DoIt;',
  28090. ' aSub = $mod.DoIt;',
  28091. ' aSubSub = $mod.DoIt;',
  28092. ' aProc = Sub;',
  28093. ' aSub = Sub;',
  28094. ' aSubSub = Sub;',
  28095. ' aProc = SubSub;',
  28096. ' aSub = SubSub;',
  28097. ' aSubSub = SubSub;',
  28098. ' };',
  28099. ' };',
  28100. ' aProc = Sub;',
  28101. ' b = rtl.eqCallback(aProc, Sub);',
  28102. ' b = rtl.eqCallback(Sub, aProc);',
  28103. '};',
  28104. '']),
  28105. LinesToStr([ // $mod.$main
  28106. '']));
  28107. end;
  28108. procedure TTestModule.TestProcType_NestedOfObject;
  28109. begin
  28110. StartProgram(false);
  28111. Add([
  28112. 'type',
  28113. ' TProcInt = procedure(vI: longint = 1) of object;',
  28114. ' TObject = class',
  28115. ' procedure DoIt(vJ: longint);',
  28116. ' end;',
  28117. 'procedure TObject.DoIt(vJ: longint);',
  28118. 'var aProc: TProcInt;',
  28119. ' b: boolean;',
  28120. ' procedure Sub(vK: longint);',
  28121. ' var aSub: TProcInt;',
  28122. ' procedure SubSub(vK: longint);',
  28123. ' var aSubSub: TProcInt;',
  28124. ' begin;',
  28125. ' aProc:=@DoIt;',
  28126. ' aSub:=@DoIt;',
  28127. ' aSubSub:=@DoIt;',
  28128. ' aProc:=@Sub;',
  28129. ' aSub:=@Sub;',
  28130. ' aSubSub:=@Sub;',
  28131. ' aProc:=@SubSub;',
  28132. ' aSub:=@SubSub;',
  28133. ' aSubSub:=@SubSub;',
  28134. ' end;',
  28135. ' begin;',
  28136. ' end;',
  28137. 'begin;',
  28138. ' aProc:=@Sub;',
  28139. ' b:=aProc=@Sub;',
  28140. ' b:=@Sub=aProc;',
  28141. 'end;',
  28142. 'begin',
  28143. '']);
  28144. ConvertProgram;
  28145. CheckSource('TestProcType_Nested',
  28146. LinesToStr([ // statements
  28147. 'rtl.createClass(this, "TObject", null, function () {',
  28148. ' this.$init = function () {',
  28149. ' };',
  28150. ' this.$final = function () {',
  28151. ' };',
  28152. ' this.DoIt = function (vJ) {',
  28153. ' var $Self = this;',
  28154. ' var aProc = null;',
  28155. ' var b = false;',
  28156. ' function Sub(vK) {',
  28157. ' var aSub = null;',
  28158. ' function SubSub(vK) {',
  28159. ' var aSubSub = null;',
  28160. ' aProc = rtl.createCallback($Self, "DoIt");',
  28161. ' aSub = rtl.createCallback($Self, "DoIt");',
  28162. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28163. ' aProc = Sub;',
  28164. ' aSub = Sub;',
  28165. ' aSubSub = Sub;',
  28166. ' aProc = SubSub;',
  28167. ' aSub = SubSub;',
  28168. ' aSubSub = SubSub;',
  28169. ' };',
  28170. ' };',
  28171. ' aProc = Sub;',
  28172. ' b = rtl.eqCallback(aProc, Sub);',
  28173. ' b = rtl.eqCallback(Sub, aProc);',
  28174. ' };',
  28175. '});',
  28176. '']),
  28177. LinesToStr([ // $mod.$main
  28178. '']));
  28179. end;
  28180. procedure TTestModule.TestProcType_ReferenceToProc;
  28181. begin
  28182. StartProgram(false);
  28183. Add([
  28184. 'type',
  28185. ' TProcRef = reference to procedure(i: longint = 0);',
  28186. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28187. 'var',
  28188. ' p: TProcRef;',
  28189. ' f: TFuncRef;',
  28190. 'procedure DoIt(i: longint);',
  28191. 'begin',
  28192. 'end;',
  28193. 'function GetIt(i: longint): longint;',
  28194. 'begin',
  28195. ' p:=@DoIt;',
  28196. ' f:=@GetIt;',
  28197. ' f;',
  28198. ' f();',
  28199. ' f(1);',
  28200. 'end;',
  28201. 'begin',
  28202. ' p:=@DoIt;',
  28203. ' f:=@GetIt;',
  28204. ' f;',
  28205. ' f();',
  28206. ' f(1);',
  28207. ' p:=TProcRef(f);',
  28208. '']);
  28209. ConvertProgram;
  28210. CheckSource('TestProcType_ReferenceToProc',
  28211. LinesToStr([ // statements
  28212. 'this.p = null;',
  28213. 'this.f = null;',
  28214. 'this.DoIt = function (i) {',
  28215. '};',
  28216. 'this.GetIt = function (i) {',
  28217. ' var Result = 0;',
  28218. ' $mod.p = $mod.DoIt;',
  28219. ' $mod.f = $mod.GetIt;',
  28220. ' $mod.f(0);',
  28221. ' $mod.f(0);',
  28222. ' $mod.f(1);',
  28223. ' return Result;',
  28224. '};',
  28225. '']),
  28226. LinesToStr([ // $mod.$main
  28227. '$mod.p = $mod.DoIt;',
  28228. '$mod.f = $mod.GetIt;',
  28229. '$mod.f(0);',
  28230. '$mod.f(0);',
  28231. '$mod.f(1);',
  28232. '$mod.p = $mod.f;',
  28233. '']));
  28234. end;
  28235. procedure TTestModule.TestProcType_ReferenceToMethod;
  28236. begin
  28237. StartProgram(false);
  28238. Add([
  28239. 'type',
  28240. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28241. ' TObject = class',
  28242. ' function Grow(s: longint): longint;',
  28243. ' end;',
  28244. 'var',
  28245. ' f: tfuncref;',
  28246. 'function tobject.grow(s: longint): longint;',
  28247. ' function GrowSub(i: longint): longint;',
  28248. ' begin',
  28249. ' f:=@grow;',
  28250. ' f:=@growsub;',
  28251. ' end;',
  28252. 'begin',
  28253. ' f:=@grow;',
  28254. ' f:=@growsub;',
  28255. 'end;',
  28256. 'begin',
  28257. '']);
  28258. ConvertProgram;
  28259. CheckSource('TestProcType_ReferenceToMethod',
  28260. LinesToStr([ // statements
  28261. 'rtl.createClass(this, "TObject", null, function () {',
  28262. ' this.$init = function () {',
  28263. ' };',
  28264. ' this.$final = function () {',
  28265. ' };',
  28266. ' this.Grow = function (s) {',
  28267. ' var $Self = this;',
  28268. ' var Result = 0;',
  28269. ' function GrowSub(i) {',
  28270. ' var Result = 0;',
  28271. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28272. ' $mod.f = GrowSub;',
  28273. ' return Result;',
  28274. ' };',
  28275. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28276. ' $mod.f = GrowSub;',
  28277. ' return Result;',
  28278. ' };',
  28279. '});',
  28280. 'this.f = null;',
  28281. '']),
  28282. LinesToStr([ // $mod.$main
  28283. '']));
  28284. end;
  28285. procedure TTestModule.TestProcType_Typecast;
  28286. begin
  28287. StartProgram(false);
  28288. Add([
  28289. 'type',
  28290. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28291. ' TEvent = procedure of object;',
  28292. ' TGetter = function:longint of object;',
  28293. ' TProcA = procedure(i: longint);',
  28294. ' TFuncB = function(i, j: longint): longint;',
  28295. 'procedure DoIt(); varargs; begin end;',
  28296. 'var',
  28297. ' Notify: tnotifyevent;',
  28298. ' Event: tevent;',
  28299. ' Getter: tgetter;',
  28300. ' ProcA: tproca;',
  28301. ' FuncB: tfuncb;',
  28302. ' p: pointer;',
  28303. 'begin',
  28304. ' notify:=tnotifyevent(event);',
  28305. ' event:=tevent(event);',
  28306. ' event:=tevent(notify);',
  28307. ' event:=tevent(getter);',
  28308. ' event:=tevent(proca);',
  28309. ' proca:=tproca(funcb);',
  28310. ' funcb:=tfuncb(funcb);',
  28311. ' funcb:=tfuncb(proca);',
  28312. ' funcb:=tfuncb(getter);',
  28313. ' proca:=tproca(p);',
  28314. ' funcb:=tfuncb(p);',
  28315. ' getter:=tgetter(p);',
  28316. ' p:=pointer(notify);',
  28317. ' p:=notify;',
  28318. ' p:=pointer(proca);',
  28319. ' p:=proca;',
  28320. ' p:=pointer(funcb);',
  28321. ' p:=funcb;',
  28322. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28323. '']);
  28324. ConvertProgram;
  28325. CheckSource('TestProcType_Typecast',
  28326. LinesToStr([ // statements
  28327. 'this.DoIt = function () {',
  28328. '};',
  28329. 'this.Notify = null;',
  28330. 'this.Event = null;',
  28331. 'this.Getter = null;',
  28332. 'this.ProcA = null;',
  28333. 'this.FuncB = null;',
  28334. 'this.p = null;',
  28335. '']),
  28336. LinesToStr([ // $mod.$main
  28337. '$mod.Notify = $mod.Event;',
  28338. '$mod.Event = $mod.Event;',
  28339. '$mod.Event = $mod.Notify;',
  28340. '$mod.Event = $mod.Getter;',
  28341. '$mod.Event = $mod.ProcA;',
  28342. '$mod.ProcA = $mod.FuncB;',
  28343. '$mod.FuncB = $mod.FuncB;',
  28344. '$mod.FuncB = $mod.ProcA;',
  28345. '$mod.FuncB = $mod.Getter;',
  28346. '$mod.ProcA = $mod.p;',
  28347. '$mod.FuncB = $mod.p;',
  28348. '$mod.Getter = $mod.p;',
  28349. '$mod.p = $mod.Notify;',
  28350. '$mod.p = $mod.Notify;',
  28351. '$mod.p = $mod.ProcA;',
  28352. '$mod.p = $mod.ProcA;',
  28353. '$mod.p = $mod.FuncB;',
  28354. '$mod.p = $mod.FuncB;',
  28355. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28356. '']));
  28357. end;
  28358. procedure TTestModule.TestProcType_PassProcToUntyped;
  28359. begin
  28360. StartProgram(false);
  28361. Add([
  28362. 'type',
  28363. ' TEvent = procedure of object;',
  28364. ' TFunc = function: longint;',
  28365. 'procedure DoIt(); varargs; begin end;',
  28366. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28367. 'var',
  28368. ' Event: tevent;',
  28369. ' Func: TFunc;',
  28370. 'begin',
  28371. ' doit(event,func);',
  28372. ' dosome(event,event,event);',
  28373. ' dosome(func,func,func);',
  28374. '']);
  28375. ConvertProgram;
  28376. CheckSource('TestProcType_PassProcToUntyped',
  28377. LinesToStr([ // statements
  28378. 'this.DoIt = function () {',
  28379. '};',
  28380. 'this.DoSome = function (a, b, p) {',
  28381. '};',
  28382. 'this.Event = null;',
  28383. 'this.Func = null;',
  28384. '']),
  28385. LinesToStr([ // $mod.$main
  28386. '$mod.DoIt($mod.Event, $mod.Func);',
  28387. '$mod.DoSome($mod.Event, {',
  28388. ' p: $mod,',
  28389. ' get: function () {',
  28390. ' return this.p.Event;',
  28391. ' },',
  28392. ' set: function (v) {',
  28393. ' this.p.Event = v;',
  28394. ' }',
  28395. '}, $mod.Event);',
  28396. '$mod.DoSome($mod.Func, {',
  28397. ' p: $mod,',
  28398. ' get: function () {',
  28399. ' return this.p.Func;',
  28400. ' },',
  28401. ' set: function (v) {',
  28402. ' this.p.Func = v;',
  28403. ' }',
  28404. '}, $mod.Func);',
  28405. '']));
  28406. end;
  28407. procedure TTestModule.TestProcType_PassProcToArray;
  28408. begin
  28409. StartProgram(false);
  28410. Add([
  28411. 'type',
  28412. ' TFunc = function: longint;',
  28413. ' TArrFunc = array of TFunc;',
  28414. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28415. 'function GetIt: longint; begin end;',
  28416. 'var',
  28417. ' Func: tfunc;',
  28418. 'begin',
  28419. ' doit([]);',
  28420. ' doit([@GetIt]);',
  28421. ' doit([Func]);',
  28422. '']);
  28423. ConvertProgram;
  28424. CheckSource('TestProcType_PassProcToArray',
  28425. LinesToStr([ // statements
  28426. 'this.DoIt = function (Arr) {',
  28427. '};',
  28428. 'this.GetIt = function () {',
  28429. ' var Result = 0;',
  28430. ' return Result;',
  28431. '};',
  28432. 'this.Func = null;',
  28433. '']),
  28434. LinesToStr([ // $mod.$main
  28435. '$mod.DoIt([]);',
  28436. '$mod.DoIt([$mod.GetIt]);',
  28437. '$mod.DoIt([$mod.Func]);',
  28438. '']));
  28439. end;
  28440. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28441. begin
  28442. StartProgram(false);
  28443. Add([
  28444. '{$modeswitch externalclass}',
  28445. 'type',
  28446. ' TProc = reference to procedure(i: longint); safecall;',
  28447. ' TEvent = procedure(i: longint) of object; safecall;',
  28448. ' TExtA = class external name ''ExtObj''',
  28449. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28450. ' procedure DoSome(Id: longint = 1);',
  28451. ' procedure SetOnClick(const e: TEvent);',
  28452. ' property OnClick: TEvent write SetOnClick;',
  28453. ' class procedure Fly(Id: longint = 1); static;',
  28454. ' procedure SetOnShow(const p: TProc);',
  28455. ' property OnShow: TProc write SetOnShow;',
  28456. ' end;',
  28457. 'procedure Run(i: longint = 1);',
  28458. 'begin',
  28459. 'end;',
  28460. 'var',
  28461. ' Obj: texta;',
  28462. ' e: TEvent;',
  28463. ' p: TProc;',
  28464. 'begin',
  28465. ' e:=e;',
  28466. ' e:[email protected];',
  28467. ' e:[email protected];',
  28468. ' e:=TEvent(@obj.dosome);', // no safecall
  28469. ' obj.OnClick:[email protected];',
  28470. ' obj.OnClick:[email protected];',
  28471. ' obj.setonclick(@obj.doit);',
  28472. ' obj.setonclick(@obj.dosome);',
  28473. ' p:=@Run;',
  28474. ' p:[email protected];',
  28475. ' obj.OnShow:=@Run;',
  28476. ' obj.OnShow:[email protected];',
  28477. ' obj.setOnShow(@Run);',
  28478. ' obj.setOnShow(@TExtA.Fly);',
  28479. ' with obj do begin',
  28480. ' e:=@doit;',
  28481. ' e:=@dosome;',
  28482. ' OnClick:=@doit;',
  28483. ' OnClick:=@dosome;',
  28484. ' setonclick(@doit);',
  28485. ' setonclick(@dosome);',
  28486. ' OnShow:=@Run;',
  28487. ' setOnShow(@Run);',
  28488. ' end;']);
  28489. ConvertProgram;
  28490. CheckSource('TestProcType_SafeCallObjFPC',
  28491. LinesToStr([ // statements
  28492. 'this.Run = function (i) {',
  28493. '};',
  28494. 'this.Obj = null;',
  28495. 'this.e = null;',
  28496. 'this.p = null;',
  28497. '']),
  28498. LinesToStr([ // $mod.$main
  28499. '$mod.e = $mod.e;',
  28500. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28501. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28502. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28503. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28504. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28505. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28506. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28507. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28508. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28509. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28510. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28511. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28512. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28513. 'var $with = $mod.Obj;',
  28514. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28515. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28516. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28517. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28518. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28519. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28520. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28521. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28522. '']));
  28523. end;
  28524. procedure TTestModule.TestProcType_SafeCallDelphi;
  28525. begin
  28526. StartProgram(false);
  28527. Add([
  28528. '{$mode delphi}',
  28529. '{$modeswitch externalclass}',
  28530. 'type',
  28531. ' TProc = reference to procedure(i: longint); safecall;',
  28532. ' TEvent = procedure(i: longint) of object; safecall;',
  28533. ' TExtA = class external name ''ExtObj''',
  28534. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28535. ' procedure DoSome(Id: longint = 1);',
  28536. ' procedure SetOnClick(const e: TEvent);',
  28537. ' property OnClick: TEvent write SetOnClick;',
  28538. ' class procedure Fly(Id: longint = 1); static;',
  28539. ' procedure SetOnShow(const p: TProc);',
  28540. ' property OnShow: TProc write SetOnShow;',
  28541. ' end;',
  28542. 'procedure Run(i: longint = 1);',
  28543. 'begin',
  28544. 'end;',
  28545. 'var',
  28546. ' Obj: texta;',
  28547. ' e: TEvent;',
  28548. ' p: TProc;',
  28549. 'begin',
  28550. ' e:=e;',
  28551. ' e:=obj.doit;',
  28552. ' e:=obj.dosome;',
  28553. ' e:=TEvent(@obj.dosome);', // no safecall
  28554. ' obj.OnClick:=obj.doit;',
  28555. ' obj.OnClick:=obj.dosome;',
  28556. ' obj.setonclick(obj.doit);',
  28557. ' obj.setonclick(obj.dosome);',
  28558. ' p:=Run;',
  28559. ' p:=TExtA.Fly;',
  28560. ' obj.OnShow:=Run;',
  28561. ' obj.OnShow:=TExtA.Fly;',
  28562. ' obj.setOnShow(Run);',
  28563. ' obj.setOnShow(TExtA.Fly);',
  28564. ' with obj do begin',
  28565. ' e:=doit;',
  28566. ' e:=dosome;',
  28567. ' OnClick:=doit;',
  28568. ' OnClick:=dosome;',
  28569. ' setonclick(doit);',
  28570. ' setonclick(dosome);',
  28571. ' OnShow:=@Run;',
  28572. ' setOnShow(@Run);',
  28573. ' end;']);
  28574. ConvertProgram;
  28575. CheckSource('TestProcType_SafeCallDelphi',
  28576. LinesToStr([ // statements
  28577. 'this.Run = function (i) {',
  28578. '};',
  28579. 'this.Obj = null;',
  28580. 'this.e = null;',
  28581. 'this.p = null;',
  28582. '']),
  28583. LinesToStr([ // $mod.$main
  28584. '$mod.e = $mod.e;',
  28585. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28586. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28587. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28588. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28589. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28590. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28591. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28592. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28593. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28594. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28595. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28596. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28597. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28598. 'var $with = $mod.Obj;',
  28599. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28600. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28601. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28602. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28603. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28604. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28605. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28606. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28607. '']));
  28608. end;
  28609. procedure TTestModule.TestProcType_SafeCall_Arg;
  28610. begin
  28611. StartProgram(false);
  28612. Add([
  28613. 'type',
  28614. ' TSafecallProc = reference to procedure; safecall;',
  28615. 'procedure Fly(const aHandler: TSafecallProc);',
  28616. 'var',
  28617. ' P: TSafecallProc;',
  28618. 'begin',
  28619. ' P := aHandler;',
  28620. ' Fly(P);',
  28621. ' Fly(aHandler);',
  28622. 'end;',
  28623. 'begin',
  28624. ' Fly(nil);',
  28625. '']);
  28626. ConvertProgram;
  28627. CheckSource('TestProcType_SafeCall_Arg',
  28628. LinesToStr([ // statements
  28629. 'this.Fly = function (aHandler) {',
  28630. ' var P = null;',
  28631. ' P = aHandler;',
  28632. ' $mod.Fly(P);',
  28633. ' $mod.Fly(aHandler);',
  28634. '};',
  28635. '']),
  28636. LinesToStr([ // $mod.$main
  28637. '$mod.Fly(null);',
  28638. '']));
  28639. end;
  28640. procedure TTestModule.TestPointer;
  28641. begin
  28642. StartProgram(false);
  28643. Add(['type',
  28644. ' TObject = class end;',
  28645. ' TClass = class of TObject;',
  28646. ' TArrInt = array of longint;',
  28647. 'const',
  28648. ' n = nil;',
  28649. 'var',
  28650. ' v: jsvalue;',
  28651. ' Obj: tobject;',
  28652. ' C: tclass;',
  28653. ' a: tarrint;',
  28654. ' p: Pointer = nil;',
  28655. ' s: string;',
  28656. 'begin',
  28657. ' p:=p;',
  28658. ' p:=nil;',
  28659. ' if p=nil then;',
  28660. ' if nil=p then;',
  28661. ' if Assigned(p) then;',
  28662. ' p:=Pointer(v);',
  28663. ' p:=obj;',
  28664. ' p:=c;',
  28665. ' p:=a;',
  28666. ' p:=tobject;',
  28667. ' obj:=TObject(p);',
  28668. ' c:=TClass(p);',
  28669. ' a:=TArrInt(p);',
  28670. ' p:=n;',
  28671. ' p:=Pointer(a);',
  28672. ' p:=pointer(s);',
  28673. ' s:=string(p);',
  28674. '']);
  28675. ConvertProgram;
  28676. CheckSource('TestPointer',
  28677. LinesToStr([ // statements
  28678. 'rtl.createClass(this, "TObject", null, function () {',
  28679. ' this.$init = function () {',
  28680. ' };',
  28681. ' this.$final = function () {',
  28682. ' };',
  28683. '});',
  28684. 'this.n = null;',
  28685. 'this.v = undefined;',
  28686. 'this.Obj = null;',
  28687. 'this.C = null;',
  28688. 'this.a = [];',
  28689. 'this.p = null;',
  28690. 'this.s = "";',
  28691. '']),
  28692. LinesToStr([ // $mod.$main
  28693. '$mod.p = $mod.p;',
  28694. '$mod.p = null;',
  28695. 'if ($mod.p === null) ;',
  28696. 'if (null === $mod.p) ;',
  28697. 'if ($mod.p != null) ;',
  28698. '$mod.p = $mod.v;',
  28699. '$mod.p = $mod.Obj;',
  28700. '$mod.p = $mod.C;',
  28701. '$mod.p = $mod.a;',
  28702. '$mod.p = $mod.TObject;',
  28703. '$mod.Obj = $mod.p;',
  28704. '$mod.C = $mod.p;',
  28705. '$mod.a = $mod.p;',
  28706. '$mod.p = null;',
  28707. '$mod.p = $mod.a;',
  28708. '$mod.p = $mod.s;',
  28709. '$mod.s = $mod.p;',
  28710. '']));
  28711. end;
  28712. procedure TTestModule.TestPointer_Proc;
  28713. begin
  28714. StartProgram(false);
  28715. Add('type');
  28716. Add(' TObject = class');
  28717. Add(' procedure DoIt; virtual; abstract;');
  28718. Add(' end;');
  28719. Add('procedure DoSome; begin end;');
  28720. Add('var');
  28721. Add(' o: TObject;');
  28722. Add(' p: Pointer;');
  28723. Add('begin');
  28724. Add(' p:=@DoSome;');
  28725. Add(' p:[email protected];');
  28726. ConvertProgram;
  28727. CheckSource('TestPointer_Proc',
  28728. LinesToStr([ // statements
  28729. 'rtl.createClass(this, "TObject", null, function () {',
  28730. ' this.$init = function () {',
  28731. ' };',
  28732. ' this.$final = function () {',
  28733. ' };',
  28734. '});',
  28735. 'this.DoSome = function () {',
  28736. '};',
  28737. 'this.o = null;',
  28738. 'this.p = null;',
  28739. '']),
  28740. LinesToStr([ // $mod.$main
  28741. '$mod.p = $mod.DoSome;',
  28742. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28743. '']));
  28744. end;
  28745. procedure TTestModule.TestPointer_AssignRecordFail;
  28746. begin
  28747. StartProgram(false);
  28748. Add('type');
  28749. Add(' TRec = record end;');
  28750. Add('var');
  28751. Add(' p: Pointer;');
  28752. Add(' r: TRec;');
  28753. Add('begin');
  28754. Add(' p:=r;');
  28755. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28756. nIncompatibleTypesGotExpected);
  28757. ConvertProgram;
  28758. end;
  28759. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28760. begin
  28761. StartProgram(false);
  28762. Add('type');
  28763. Add(' TArr = array[boolean] of longint;');
  28764. Add('var');
  28765. Add(' p: Pointer;');
  28766. Add(' a: TArr;');
  28767. Add('begin');
  28768. Add(' p:=a;');
  28769. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28770. nIncompatibleTypesGotExpected);
  28771. ConvertProgram;
  28772. end;
  28773. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28774. begin
  28775. StartProgram(false);
  28776. Add([
  28777. 'procedure DoIt(args: array of jsvalue); begin end;',
  28778. 'procedure DoAll; varargs; begin end;',
  28779. 'var',
  28780. ' v: jsvalue;',
  28781. 'begin',
  28782. ' DoIt([pointer(v)]);',
  28783. ' DoAll(pointer(v));',
  28784. '']);
  28785. ConvertProgram;
  28786. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28787. LinesToStr([ // statements
  28788. 'this.DoIt = function (args) {',
  28789. '};',
  28790. 'this.DoAll = function () {',
  28791. '};',
  28792. 'this.v = undefined;',
  28793. '']),
  28794. LinesToStr([ // $mod.$main
  28795. '$mod.DoIt([$mod.v]);',
  28796. '$mod.DoAll($mod.v);',
  28797. '']));
  28798. end;
  28799. procedure TTestModule.TestPointer_NonRecordFail;
  28800. begin
  28801. StartProgram(false);
  28802. Add([
  28803. 'type',
  28804. ' p = ^longint;',
  28805. 'begin',
  28806. '']);
  28807. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28808. ConvertProgram;
  28809. end;
  28810. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28811. begin
  28812. StartProgram(false);
  28813. Add([
  28814. 'procedure DoIt(p: ^longint); begin end;',
  28815. 'begin',
  28816. '']);
  28817. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28818. ConvertProgram;
  28819. end;
  28820. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28821. begin
  28822. StartProgram(false);
  28823. Add([
  28824. 'var p: ^longint;',
  28825. 'begin',
  28826. '']);
  28827. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28828. ConvertProgram;
  28829. end;
  28830. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28831. begin
  28832. StartProgram(false);
  28833. Add([
  28834. 'function DoIt: ^longint; begin end;',
  28835. 'begin',
  28836. '']);
  28837. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28838. ConvertProgram;
  28839. end;
  28840. procedure TTestModule.TestPointer_AddrOperatorFail;
  28841. begin
  28842. StartProgram(false);
  28843. Add([
  28844. 'var i: longint;',
  28845. 'begin',
  28846. ' if @i=nil then ;',
  28847. '']);
  28848. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28849. ConvertProgram;
  28850. end;
  28851. procedure TTestModule.TestPointer_ArrayParamsFail;
  28852. begin
  28853. StartProgram(false);
  28854. Add([
  28855. 'var',
  28856. ' p: Pointer;',
  28857. 'begin',
  28858. ' p:=p[1];',
  28859. '']);
  28860. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28861. ConvertProgram;
  28862. end;
  28863. procedure TTestModule.TestPointer_PointerAddFail;
  28864. begin
  28865. StartProgram(false);
  28866. Add([
  28867. 'var',
  28868. ' p: Pointer;',
  28869. 'begin',
  28870. ' p:=p+1;',
  28871. '']);
  28872. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28873. ConvertProgram;
  28874. end;
  28875. procedure TTestModule.TestPointer_IncPointerFail;
  28876. begin
  28877. StartProgram(false);
  28878. Add([
  28879. 'var',
  28880. ' p: Pointer;',
  28881. 'begin',
  28882. ' inc(p,1);',
  28883. '']);
  28884. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28885. nIncompatibleTypeArgNo);
  28886. ConvertProgram;
  28887. end;
  28888. procedure TTestModule.TestPointer_Record;
  28889. begin
  28890. StartProgram(false);
  28891. Add([
  28892. 'type',
  28893. ' TRec = record x: longint; end;',
  28894. ' PRec = ^TRec;',
  28895. 'var',
  28896. ' r: TRec;',
  28897. ' p: PRec;',
  28898. ' q: ^TRec;',
  28899. ' Ptr: pointer;',
  28900. 'begin',
  28901. ' new(p);',
  28902. ' p:=@r;',
  28903. ' r:=p^;',
  28904. ' r.x:=p^.x;',
  28905. ' p^.x:=r.x;',
  28906. ' if p^.x=3 then ;',
  28907. ' if 4=p^.x then ;',
  28908. ' dispose(p);',
  28909. ' new(q);',
  28910. ' dispose(q);',
  28911. ' Ptr:=p;',
  28912. ' p:=PRec(ptr);',
  28913. '']);
  28914. ConvertProgram;
  28915. CheckSource('TestPointer_Record',
  28916. LinesToStr([ // statements
  28917. 'rtl.recNewT(this, "TRec", function () {',
  28918. ' this.x = 0;',
  28919. ' this.$eq = function (b) {',
  28920. ' return this.x === b.x;',
  28921. ' };',
  28922. ' this.$assign = function (s) {',
  28923. ' this.x = s.x;',
  28924. ' return this;',
  28925. ' };',
  28926. '});',
  28927. 'this.r = this.TRec.$new();',
  28928. 'this.p = null;',
  28929. 'this.q = null;',
  28930. 'this.Ptr = null;',
  28931. '']),
  28932. LinesToStr([ // $mod.$main
  28933. '$mod.p = $mod.TRec.$new();',
  28934. '$mod.p = $mod.r;',
  28935. '$mod.r.$assign($mod.p);',
  28936. '$mod.r.x = $mod.p.x;',
  28937. '$mod.p.x = $mod.r.x;',
  28938. 'if ($mod.p.x === 3) ;',
  28939. 'if (4 === $mod.p.x) ;',
  28940. '$mod.p = null;',
  28941. '$mod.q = $mod.TRec.$new();',
  28942. '$mod.q = null;',
  28943. '$mod.Ptr = $mod.p;',
  28944. '$mod.p = $mod.Ptr;',
  28945. '']));
  28946. end;
  28947. procedure TTestModule.TestPointer_RecordArg;
  28948. begin
  28949. StartProgram(false);
  28950. Add([
  28951. '{$modeswitch autoderef}',
  28952. 'type',
  28953. ' TRec = record x: longint; end;',
  28954. ' PRec = ^TRec;',
  28955. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28956. 'begin',
  28957. ' a.x:=a.x;',
  28958. ' a^.x:=a^.x;',
  28959. ' with a^ do',
  28960. ' x:=x;',
  28961. 'end;',
  28962. 'function GetIt(p: PRec): PRec;',
  28963. 'begin',
  28964. ' p.x:=p.x;',
  28965. ' p^.x:=p^.x;',
  28966. ' with p^ do',
  28967. ' x:=x;',
  28968. 'end;',
  28969. 'var',
  28970. ' r: TRec;',
  28971. ' p: PRec;',
  28972. 'begin',
  28973. ' p:=GetIt(p);',
  28974. ' p^:=GetIt(@r)^;',
  28975. ' DoIt(p,p,p);',
  28976. ' DoIt(@r,p,p);',
  28977. '']);
  28978. ConvertProgram;
  28979. CheckSource('TestPointer_RecordArg',
  28980. LinesToStr([ // statements
  28981. 'rtl.recNewT(this, "TRec", function () {',
  28982. ' this.x = 0;',
  28983. ' this.$eq = function (b) {',
  28984. ' return this.x === b.x;',
  28985. ' };',
  28986. ' this.$assign = function (s) {',
  28987. ' this.x = s.x;',
  28988. ' return this;',
  28989. ' };',
  28990. '});',
  28991. 'this.DoIt = function (a, b, c) {',
  28992. ' var Result = $mod.TRec.$new();',
  28993. ' a.x = a.x;',
  28994. ' a.x = a.x;',
  28995. ' a.x = a.x;',
  28996. ' return Result;',
  28997. '};',
  28998. 'this.GetIt = function (p) {',
  28999. ' var Result = null;',
  29000. ' p.x = p.x;',
  29001. ' p.x = p.x;',
  29002. ' p.x = p.x;',
  29003. ' return Result;',
  29004. '};',
  29005. 'this.r = this.TRec.$new();',
  29006. 'this.p = null;',
  29007. '']),
  29008. LinesToStr([ // $mod.$main
  29009. '$mod.p = $mod.GetIt($mod.p);',
  29010. '$mod.p.$assign($mod.GetIt($mod.r));',
  29011. '$mod.DoIt($mod.p, {',
  29012. ' p: $mod,',
  29013. ' get: function () {',
  29014. ' return this.p.p;',
  29015. ' },',
  29016. ' set: function (v) {',
  29017. ' this.p.p = v;',
  29018. ' }',
  29019. '}, {',
  29020. ' p: $mod,',
  29021. ' get: function () {',
  29022. ' return this.p.p;',
  29023. ' },',
  29024. ' set: function (v) {',
  29025. ' this.p.p = v;',
  29026. ' }',
  29027. '});',
  29028. '$mod.DoIt($mod.r, {',
  29029. ' p: $mod,',
  29030. ' get: function () {',
  29031. ' return this.p.p;',
  29032. ' },',
  29033. ' set: function (v) {',
  29034. ' this.p.p = v;',
  29035. ' }',
  29036. '}, {',
  29037. ' p: $mod,',
  29038. ' get: function () {',
  29039. ' return this.p.p;',
  29040. ' },',
  29041. ' set: function (v) {',
  29042. ' this.p.p = v;',
  29043. ' }',
  29044. '});',
  29045. '']));
  29046. end;
  29047. procedure TTestModule.TestJSValue_AssignToJSValue;
  29048. begin
  29049. StartProgram(false);
  29050. Add('var');
  29051. Add(' v: jsvalue;');
  29052. Add(' i: longint;');
  29053. Add(' s: string;');
  29054. Add(' b: boolean;');
  29055. Add(' d: double;');
  29056. Add(' p: pointer;');
  29057. Add('begin');
  29058. Add(' v:=v;');
  29059. Add(' v:=1;');
  29060. Add(' v:=i;');
  29061. Add(' v:='''';');
  29062. Add(' v:=''c'';');
  29063. Add(' v:=''foo'';');
  29064. Add(' v:=s;');
  29065. Add(' v:=false;');
  29066. Add(' v:=true;');
  29067. Add(' v:=b;');
  29068. Add(' v:=0.1;');
  29069. Add(' v:=d;');
  29070. Add(' v:=nil;');
  29071. Add(' v:=p;');
  29072. ConvertProgram;
  29073. CheckSource('TestJSValue_AssignToJSValue',
  29074. LinesToStr([ // statements
  29075. 'this.v = undefined;',
  29076. 'this.i = 0;',
  29077. 'this.s = "";',
  29078. 'this.b = false;',
  29079. 'this.d = 0.0;',
  29080. 'this.p = null;',
  29081. '']),
  29082. LinesToStr([ // $mod.$main
  29083. '$mod.v = $mod.v;',
  29084. '$mod.v = 1;',
  29085. '$mod.v = $mod.i;',
  29086. '$mod.v = "";',
  29087. '$mod.v = "c";',
  29088. '$mod.v = "foo";',
  29089. '$mod.v = $mod.s;',
  29090. '$mod.v = false;',
  29091. '$mod.v = true;',
  29092. '$mod.v = $mod.b;',
  29093. '$mod.v = 0.1;',
  29094. '$mod.v = $mod.d;',
  29095. '$mod.v = null;',
  29096. '$mod.v = $mod.p;',
  29097. '']));
  29098. end;
  29099. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29100. begin
  29101. StartProgram(false);
  29102. Add('type');
  29103. Add(' integer = longint;');
  29104. Add(' TYesNo = boolean;');
  29105. Add(' TFloat = double;');
  29106. Add(' TCaption = string;');
  29107. Add(' TChar = char;');
  29108. Add('var');
  29109. Add(' v: jsvalue;');
  29110. Add(' i: integer;');
  29111. Add(' s: TCaption;');
  29112. Add(' b: TYesNo;');
  29113. Add(' d: TFloat;');
  29114. Add(' c: char;');
  29115. Add('begin');
  29116. Add(' i:=longint(v);');
  29117. Add(' i:=integer(v);');
  29118. Add(' s:=string(v);');
  29119. Add(' s:=TCaption(v);');
  29120. Add(' b:=boolean(v);');
  29121. Add(' b:=TYesNo(v);');
  29122. Add(' d:=double(v);');
  29123. Add(' d:=TFloat(v);');
  29124. Add(' c:=char(v);');
  29125. Add(' c:=TChar(v);');
  29126. ConvertProgram;
  29127. CheckSource('TestJSValue_TypeCastToBaseType',
  29128. LinesToStr([ // statements
  29129. 'this.v = undefined;',
  29130. 'this.i = 0;',
  29131. 'this.s = "";',
  29132. 'this.b = false;',
  29133. 'this.d = 0.0;',
  29134. 'this.c = "\x00";',
  29135. '']),
  29136. LinesToStr([ // $mod.$main
  29137. '$mod.i = rtl.trunc($mod.v);',
  29138. '$mod.i = rtl.trunc($mod.v);',
  29139. '$mod.s = "" + $mod.v;',
  29140. '$mod.s = "" + $mod.v;',
  29141. '$mod.b = !($mod.v == false);',
  29142. '$mod.b = !($mod.v == false);',
  29143. '$mod.d = rtl.getNumber($mod.v);',
  29144. '$mod.d = rtl.getNumber($mod.v);',
  29145. '$mod.c = rtl.getChar($mod.v);',
  29146. '$mod.c = rtl.getChar($mod.v);',
  29147. '']));
  29148. end;
  29149. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29150. begin
  29151. StartProgram(false);
  29152. Add([
  29153. 'type',
  29154. ' TArr = array of word;',
  29155. ' TRec = record end;',
  29156. ' TSet = set of boolean;',
  29157. 'procedure Fly(v: jsvalue);',
  29158. 'begin',
  29159. 'end;',
  29160. 'var',
  29161. ' a: TArr;',
  29162. ' r: TRec;',
  29163. ' s: TSet;',
  29164. 'begin',
  29165. ' Fly(jsvalue(a));',
  29166. ' Fly(jsvalue(r));',
  29167. ' Fly(jsvalue(s));',
  29168. '']);
  29169. ConvertProgram;
  29170. CheckSource('TestJSValue_TypecastToJSValue',
  29171. LinesToStr([ // statements
  29172. 'rtl.recNewT(this, "TRec", function () {',
  29173. ' this.$eq = function (b) {',
  29174. ' return true;',
  29175. ' };',
  29176. ' this.$assign = function (s) {',
  29177. ' return this;',
  29178. ' };',
  29179. '});',
  29180. 'this.Fly = function (v) {',
  29181. '};',
  29182. 'this.a = [];',
  29183. 'this.r = this.TRec.$new();',
  29184. 'this.s = {};',
  29185. '']),
  29186. LinesToStr([ // $mod.$main
  29187. '$mod.Fly($mod.a);',
  29188. '$mod.Fly($mod.r);',
  29189. '$mod.Fly($mod.s);',
  29190. '']));
  29191. end;
  29192. procedure TTestModule.TestJSValue_Equal;
  29193. begin
  29194. StartProgram(false);
  29195. Add('type');
  29196. Add(' integer = longint;');
  29197. Add(' TYesNo = boolean;');
  29198. Add(' TFloat = double;');
  29199. Add(' TCaption = string;');
  29200. Add(' TChar = char;');
  29201. Add(' TMulti = JSValue;');
  29202. Add('var');
  29203. Add(' v: jsvalue;');
  29204. Add(' i: integer;');
  29205. Add(' s: TCaption;');
  29206. Add(' b: TYesNo;');
  29207. Add(' d: TFloat;');
  29208. Add(' c: char;');
  29209. Add(' m: TMulti;');
  29210. Add('begin');
  29211. Add(' b:=v=v;');
  29212. Add(' b:=v<>v;');
  29213. Add(' b:=v=1;');
  29214. Add(' b:=v<>1;');
  29215. Add(' b:=2=v;');
  29216. Add(' b:=2<>v;');
  29217. Add(' b:=v=i;');
  29218. Add(' b:=i=v;');
  29219. Add(' b:=v=nil;');
  29220. Add(' b:=nil=v;');
  29221. Add(' b:=v=false;');
  29222. Add(' b:=true=v;');
  29223. Add(' b:=v=b;');
  29224. Add(' b:=b=v;');
  29225. Add(' b:=v=s;');
  29226. Add(' b:=s=v;');
  29227. Add(' b:=v=''foo'';');
  29228. Add(' b:=''''=v;');
  29229. Add(' b:=v=d;');
  29230. Add(' b:=d=v;');
  29231. Add(' b:=v=3.4;');
  29232. Add(' b:=5.6=v;');
  29233. Add(' b:=v=c;');
  29234. Add(' b:=c=v;');
  29235. Add(' b:=m=m;');
  29236. Add(' b:=v=m;');
  29237. Add(' b:=m=v;');
  29238. ConvertProgram;
  29239. CheckSource('TestJSValue_Equal',
  29240. LinesToStr([ // statements
  29241. 'this.v = undefined;',
  29242. 'this.i = 0;',
  29243. 'this.s = "";',
  29244. 'this.b = false;',
  29245. 'this.d = 0.0;',
  29246. 'this.c = "\x00";',
  29247. 'this.m = undefined;',
  29248. '']),
  29249. LinesToStr([ // $mod.$main
  29250. '$mod.b = $mod.v == $mod.v;',
  29251. '$mod.b = $mod.v != $mod.v;',
  29252. '$mod.b = $mod.v == 1;',
  29253. '$mod.b = $mod.v != 1;',
  29254. '$mod.b = 2 == $mod.v;',
  29255. '$mod.b = 2 != $mod.v;',
  29256. '$mod.b = $mod.v == $mod.i;',
  29257. '$mod.b = $mod.i == $mod.v;',
  29258. '$mod.b = $mod.v == null;',
  29259. '$mod.b = null == $mod.v;',
  29260. '$mod.b = $mod.v == false;',
  29261. '$mod.b = true == $mod.v;',
  29262. '$mod.b = $mod.v == $mod.b;',
  29263. '$mod.b = $mod.b == $mod.v;',
  29264. '$mod.b = $mod.v == $mod.s;',
  29265. '$mod.b = $mod.s == $mod.v;',
  29266. '$mod.b = $mod.v == "foo";',
  29267. '$mod.b = "" == $mod.v;',
  29268. '$mod.b = $mod.v == $mod.d;',
  29269. '$mod.b = $mod.d == $mod.v;',
  29270. '$mod.b = $mod.v == 3.4;',
  29271. '$mod.b = 5.6 == $mod.v;',
  29272. '$mod.b = $mod.v == $mod.c;',
  29273. '$mod.b = $mod.c == $mod.v;',
  29274. '$mod.b = $mod.m == $mod.m;',
  29275. '$mod.b = $mod.v == $mod.m;',
  29276. '$mod.b = $mod.m == $mod.v;',
  29277. '']));
  29278. end;
  29279. procedure TTestModule.TestJSValue_If;
  29280. begin
  29281. StartProgram(false);
  29282. Add([
  29283. 'procedure Fly(var u);',
  29284. 'begin',
  29285. ' if jsvalue(u) then ;',
  29286. 'end;',
  29287. 'var',
  29288. ' v: jsvalue;',
  29289. 'begin',
  29290. ' if v then ;',
  29291. ' while v do ;',
  29292. ' repeat until v;',
  29293. '']);
  29294. ConvertProgram;
  29295. CheckSource('TestJSValue_If',
  29296. LinesToStr([ // statements
  29297. 'this.Fly = function (u) {',
  29298. ' if (u.get()) ;',
  29299. '};',
  29300. 'this.v = undefined;',
  29301. '']),
  29302. LinesToStr([ // $mod.$main
  29303. 'if ($mod.v) ;',
  29304. 'while($mod.v){',
  29305. '};',
  29306. 'do{',
  29307. '} while(!$mod.v);',
  29308. '']));
  29309. end;
  29310. procedure TTestModule.TestJSValue_Not;
  29311. begin
  29312. StartProgram(false);
  29313. Add([
  29314. 'var',
  29315. ' v: jsvalue;',
  29316. ' b: boolean;',
  29317. 'begin',
  29318. ' b:=not v;',
  29319. ' if not v then ;',
  29320. ' while not v do ;',
  29321. ' repeat until not v;',
  29322. '']);
  29323. ConvertProgram;
  29324. CheckSource('TestJSValue_If',
  29325. LinesToStr([ // statements
  29326. 'this.v = undefined;',
  29327. 'this.b = false;',
  29328. '']),
  29329. LinesToStr([ // $mod.$main
  29330. '$mod.b=!$mod.v;',
  29331. 'if (!$mod.v) ;',
  29332. 'while(!$mod.v){',
  29333. '};',
  29334. 'do{',
  29335. '} while($mod.v);',
  29336. '']));
  29337. end;
  29338. procedure TTestModule.TestJSValue_Enum;
  29339. begin
  29340. StartProgram(false);
  29341. Add('type');
  29342. Add(' TColor = (red, blue);');
  29343. Add(' TRedBlue = TColor;');
  29344. Add('var');
  29345. Add(' v: jsvalue;');
  29346. Add(' e: TColor;');
  29347. Add('begin');
  29348. Add(' v:=e;');
  29349. Add(' v:=TColor(e);');
  29350. Add(' v:=TRedBlue(e);');
  29351. Add(' e:=TColor(v);');
  29352. Add(' e:=TRedBlue(v);');
  29353. ConvertProgram;
  29354. CheckSource('TestJSValue_Enum',
  29355. LinesToStr([ // statements
  29356. 'this.TColor = {',
  29357. ' "0": "red",',
  29358. ' red: 0,',
  29359. ' "1": "blue",',
  29360. ' blue: 1',
  29361. '};',
  29362. 'this.v = undefined;',
  29363. 'this.e = 0;',
  29364. '']),
  29365. LinesToStr([ // $mod.$main
  29366. '$mod.v = $mod.e;',
  29367. '$mod.v = $mod.e;',
  29368. '$mod.v = $mod.e;',
  29369. '$mod.e = $mod.v;',
  29370. '$mod.e = $mod.v;',
  29371. '']));
  29372. end;
  29373. procedure TTestModule.TestJSValue_ClassInstance;
  29374. begin
  29375. StartProgram(false);
  29376. Add([
  29377. 'type',
  29378. ' TObject = class',
  29379. ' end;',
  29380. ' TBirdObject = TObject;',
  29381. 'var',
  29382. ' v: jsvalue;',
  29383. ' o: TObject;',
  29384. 'begin',
  29385. ' v:=o;',
  29386. ' v:=TObject(o);',
  29387. ' v:=TBirdObject(o);',
  29388. ' o:=TObject(v);',
  29389. ' o:=TBirdObject(v);',
  29390. ' if v is TObject then ;',
  29391. '']);
  29392. ConvertProgram;
  29393. CheckSource('TestJSValue_ClassInstance',
  29394. LinesToStr([ // statements
  29395. 'rtl.createClass(this, "TObject", null, function () {',
  29396. ' this.$init = function () {',
  29397. ' };',
  29398. ' this.$final = function () {',
  29399. ' };',
  29400. '});',
  29401. 'this.v = undefined;',
  29402. 'this.o = null;',
  29403. '']),
  29404. LinesToStr([ // $mod.$main
  29405. '$mod.v = $mod.o;',
  29406. '$mod.v = $mod.o;',
  29407. '$mod.v = $mod.o;',
  29408. '$mod.o = rtl.getObject($mod.v);',
  29409. '$mod.o = rtl.getObject($mod.v);',
  29410. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29411. '']));
  29412. end;
  29413. procedure TTestModule.TestJSValue_ClassOf;
  29414. begin
  29415. StartProgram(false);
  29416. Add([
  29417. 'type',
  29418. ' TClass = class of TObject;',
  29419. ' TObject = class',
  29420. ' end;',
  29421. ' TBirds = class of TBird;',
  29422. ' TBird = class(TObject) end;',
  29423. 'var',
  29424. ' v: jsvalue;',
  29425. ' c: TClass;',
  29426. 'begin',
  29427. ' v:=c;',
  29428. ' v:=TObject;',
  29429. ' v:=TClass(c);',
  29430. ' v:=TBirds(c);',
  29431. ' c:=TClass(v);',
  29432. ' c:=TBirds(v);',
  29433. ' if v is TClass then ;',
  29434. '']);
  29435. ConvertProgram;
  29436. CheckSource('TestJSValue_ClassOf',
  29437. LinesToStr([ // statements
  29438. 'rtl.createClass(this, "TObject", null, function () {',
  29439. ' this.$init = function () {',
  29440. ' };',
  29441. ' this.$final = function () {',
  29442. ' };',
  29443. '});',
  29444. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29445. '});',
  29446. 'this.v = undefined;',
  29447. 'this.c = null;',
  29448. '']),
  29449. LinesToStr([ // $mod.$main
  29450. '$mod.v = $mod.c;',
  29451. '$mod.v = $mod.TObject;',
  29452. '$mod.v = $mod.c;',
  29453. '$mod.v = $mod.c;',
  29454. '$mod.c = rtl.getObject($mod.v);',
  29455. '$mod.c = rtl.getObject($mod.v);',
  29456. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29457. '']));
  29458. end;
  29459. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29460. begin
  29461. StartProgram(false);
  29462. Add([
  29463. 'type',
  29464. ' integer = longint;',
  29465. ' TArray = array of JSValue;',
  29466. ' TArrgh = tarray;',
  29467. ' TArrInt = array of integer;',
  29468. 'var',
  29469. ' v: jsvalue;',
  29470. ' TheArray: tarray = (1,''2'');',
  29471. ' Arr: tarrgh;',
  29472. ' i: integer;',
  29473. ' ArrInt: tarrint;',
  29474. 'begin',
  29475. ' arr:=thearray;',
  29476. ' thearray:=arr;',
  29477. ' setlength(arr,2);',
  29478. ' setlength(thearray,3);',
  29479. ' arr[4]:=v;',
  29480. ' arr[5]:=length(thearray);',
  29481. ' arr[6]:=nil;',
  29482. ' arr[7]:=thearray[8];',
  29483. ' arr[low(arr)]:=high(thearray);',
  29484. ' arr:=arrint;',
  29485. ' arrInt:=tarrint(arr);',
  29486. ' if TheArray = nil then ;',
  29487. ' if nil = TheArray then ;',
  29488. ' if TheArray <> nil then ;',
  29489. ' if nil <> TheArray then ;',
  29490. '']);
  29491. ConvertProgram;
  29492. CheckSource('TestJSValue_ArrayOfJSValue',
  29493. LinesToStr([ // statements
  29494. 'this.v = undefined;',
  29495. 'this.TheArray = [1, "2"];',
  29496. 'this.Arr = [];',
  29497. 'this.i = 0;',
  29498. 'this.ArrInt = [];',
  29499. '']),
  29500. LinesToStr([ // $mod.$main
  29501. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29502. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29503. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29504. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29505. '$mod.Arr[4] = $mod.v;',
  29506. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29507. '$mod.Arr[6] = null;',
  29508. '$mod.Arr[7] = $mod.TheArray[8];',
  29509. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29510. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29511. '$mod.ArrInt = $mod.Arr;',
  29512. 'if (rtl.length($mod.TheArray) === 0) ;',
  29513. 'if (rtl.length($mod.TheArray) === 0) ;',
  29514. 'if (rtl.length($mod.TheArray) > 0) ;',
  29515. 'if (rtl.length($mod.TheArray) > 0) ;',
  29516. '']));
  29517. end;
  29518. procedure TTestModule.TestJSValue_ArrayLit;
  29519. begin
  29520. StartProgram(false);
  29521. Add([
  29522. 'type',
  29523. ' TFlag = (big,small);',
  29524. ' TArray = array of JSValue;',
  29525. ' TObject = class end;',
  29526. ' TClass = class of TObject;',
  29527. 'var',
  29528. ' v: jsvalue;',
  29529. ' a: TArray;',
  29530. ' o: TObject;',
  29531. 'begin',
  29532. ' a:=[];',
  29533. ' a:=[1];',
  29534. ' a:=[1,2];',
  29535. ' a:=[big];',
  29536. ' a:=[1,big];',
  29537. ' a:=[o,nil];',
  29538. '']);
  29539. ConvertProgram;
  29540. CheckSource('TestJSValue_ArrayLit',
  29541. LinesToStr([ // statements
  29542. 'this.TFlag = {',
  29543. ' "0": "big",',
  29544. ' big: 0,',
  29545. ' "1": "small",',
  29546. ' small: 1',
  29547. '};',
  29548. 'rtl.createClass(this, "TObject", null, function () {',
  29549. ' this.$init = function () {',
  29550. ' };',
  29551. ' this.$final = function () {',
  29552. ' };',
  29553. '});',
  29554. 'this.v = undefined;',
  29555. 'this.a = [];',
  29556. 'this.o = null;',
  29557. '']),
  29558. LinesToStr([ // $mod.$main
  29559. '$mod.a = [];',
  29560. '$mod.a = [1];',
  29561. '$mod.a = [1, 2];',
  29562. '$mod.a = [$mod.TFlag.big];',
  29563. '$mod.a = [1, $mod.TFlag.big];',
  29564. '$mod.a = [$mod.o, null];',
  29565. '']));
  29566. end;
  29567. procedure TTestModule.TestJSValue_Params;
  29568. begin
  29569. StartProgram(false);
  29570. Add('type');
  29571. Add(' integer = longint;');
  29572. Add(' TYesNo = boolean;');
  29573. Add(' TFloat = double;');
  29574. Add(' TCaption = string;');
  29575. Add(' TChar = char;');
  29576. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29577. Add('var');
  29578. Add(' l: jsvalue;');
  29579. Add('begin');
  29580. Add(' a:=a;');
  29581. Add(' l:=b;');
  29582. Add(' c:=c;');
  29583. Add(' d:=d;');
  29584. Add(' Result:=l;');
  29585. Add('end;');
  29586. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29587. Add('var');
  29588. Add(' v: jsvalue;');
  29589. Add(' i: integer;');
  29590. Add(' b: TYesNo;');
  29591. Add(' d: TFloat;');
  29592. Add(' s: TCaption;');
  29593. Add(' c: TChar;');
  29594. Add('begin');
  29595. Add(' v:=doit(v,v,v,v);');
  29596. Add(' i:=integer(dosome(i,i));');
  29597. Add(' b:=TYesNo(dosome(b,b));');
  29598. Add(' d:=TFloat(dosome(d,d));');
  29599. Add(' s:=TCaption(dosome(s,s));');
  29600. Add(' c:=TChar(dosome(c,c));');
  29601. ConvertProgram;
  29602. CheckSource('TestJSValue_Params',
  29603. LinesToStr([ // statements
  29604. 'this.DoIt = function (a, b, c, d) {',
  29605. ' var Result = undefined;',
  29606. ' var l = undefined;',
  29607. ' a = a;',
  29608. ' l = b;',
  29609. ' c.set(c.get());',
  29610. ' d.set(d.get());',
  29611. ' Result = l;',
  29612. ' return Result;',
  29613. '};',
  29614. 'this.DoSome = function (a, b) {',
  29615. ' var Result = undefined;',
  29616. ' return Result;',
  29617. '};',
  29618. 'this.v = undefined;',
  29619. 'this.i = 0;',
  29620. 'this.b = false;',
  29621. 'this.d = 0.0;',
  29622. 'this.s = "";',
  29623. 'this.c = "\x00";',
  29624. '']),
  29625. LinesToStr([ // $mod.$main
  29626. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29627. ' p: $mod,',
  29628. ' get: function () {',
  29629. ' return this.p.v;',
  29630. ' },',
  29631. ' set: function (v) {',
  29632. ' this.p.v = v;',
  29633. ' }',
  29634. '}, {',
  29635. ' p: $mod,',
  29636. ' get: function () {',
  29637. ' return this.p.v;',
  29638. ' },',
  29639. ' set: function (v) {',
  29640. ' this.p.v = v;',
  29641. ' }',
  29642. '});',
  29643. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29644. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29645. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29646. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29647. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29648. '']));
  29649. end;
  29650. procedure TTestModule.TestJSValue_UntypedParam;
  29651. begin
  29652. StartProgram(false);
  29653. Add('function DoIt(const a; var b; out c): jsvalue;');
  29654. Add('begin');
  29655. Add(' Result:=a;');
  29656. Add(' Result:=b;');
  29657. Add(' Result:=c;');
  29658. Add(' b:=Result;');
  29659. Add(' c:=Result;');
  29660. Add('end;');
  29661. Add('var i: longint;');
  29662. Add('begin');
  29663. Add(' doit(i,i,i);');
  29664. ConvertProgram;
  29665. CheckSource('TestJSValue_UntypedParam',
  29666. LinesToStr([ // statements
  29667. 'this.DoIt = function (a, b, c) {',
  29668. ' var Result = undefined;',
  29669. ' Result = a;',
  29670. ' Result = b.get();',
  29671. ' Result = c.get();',
  29672. ' b.set(Result);',
  29673. ' c.set(Result);',
  29674. ' return Result;',
  29675. '};',
  29676. 'this.i = 0;',
  29677. '']),
  29678. LinesToStr([ // $mod.$main
  29679. '$mod.DoIt($mod.i, {',
  29680. ' p: $mod,',
  29681. ' get: function () {',
  29682. ' return this.p.i;',
  29683. ' },',
  29684. ' set: function (v) {',
  29685. ' this.p.i = v;',
  29686. ' }',
  29687. '}, {',
  29688. ' p: $mod,',
  29689. ' get: function () {',
  29690. ' return this.p.i;',
  29691. ' },',
  29692. ' set: function (v) {',
  29693. ' this.p.i = v;',
  29694. ' }',
  29695. '});',
  29696. '']));
  29697. end;
  29698. procedure TTestModule.TestJSValue_FuncResultType;
  29699. begin
  29700. StartProgram(false);
  29701. Add('type');
  29702. Add(' integer = longint;');
  29703. Add(' TJSValueArray = array of JSValue;');
  29704. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29705. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29706. Add('begin');
  29707. Add(' while Compare(P,aList[0])>0 do ;');
  29708. Add('end;');
  29709. Add('var');
  29710. Add(' Compare: TListSortCompare;');
  29711. Add(' V: JSValue;');
  29712. Add(' i: integer;');
  29713. Add('begin');
  29714. Add(' if Compare(V,V)>0 then ;');
  29715. Add(' if Compare(i,i)>1 then ;');
  29716. Add(' if Compare(nil,false)>2 then ;');
  29717. Add(' if Compare(1,true)>3 then ;');
  29718. ConvertProgram;
  29719. CheckSource('TestJSValue_UntypedParam',
  29720. LinesToStr([ // statements
  29721. 'this.Sort = function (P, aList, Compare) {',
  29722. ' while (Compare(P, aList[0]) > 0) {',
  29723. ' };',
  29724. '};',
  29725. 'this.Compare = null;',
  29726. 'this.V = undefined;',
  29727. 'this.i = 0;',
  29728. '']),
  29729. LinesToStr([ // $mod.$main
  29730. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29731. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29732. 'if ($mod.Compare(null, false) > 2) ;',
  29733. 'if ($mod.Compare(1, true) > 3) ;',
  29734. '']));
  29735. end;
  29736. procedure TTestModule.TestJSValue_ProcType_Assign;
  29737. begin
  29738. StartProgram(false);
  29739. Add('type');
  29740. Add(' integer = longint;');
  29741. Add(' TObject = class');
  29742. Add(' class function GetGlob: integer;');
  29743. Add(' function Getter: integer;');
  29744. Add(' end;');
  29745. Add('class function TObject.GetGlob: integer;');
  29746. Add('var v1: jsvalue;');
  29747. Add('begin');
  29748. Add(' v1:=@GetGlob;');
  29749. Add(' v1:[email protected];');
  29750. Add('end;');
  29751. Add('function TObject.Getter: integer;');
  29752. Add('var v2: jsvalue;');
  29753. Add('begin');
  29754. Add(' v2:=@Getter;');
  29755. Add(' v2:[email protected];');
  29756. Add(' v2:=@GetGlob;');
  29757. Add(' v2:[email protected];');
  29758. Add('end;');
  29759. Add('function GetIt(i: integer): integer;');
  29760. Add('var v3: jsvalue;');
  29761. Add('begin');
  29762. Add(' v3:=@GetIt;');
  29763. Add('end;');
  29764. Add('var');
  29765. Add(' V: JSValue;');
  29766. Add(' o: TObject;');
  29767. Add('begin');
  29768. Add(' v:=@GetIt;');
  29769. Add(' v:[email protected];');
  29770. Add(' v:[email protected];');
  29771. ConvertProgram;
  29772. CheckSource('TestJSValue_ProcType_Assign',
  29773. LinesToStr([ // statements
  29774. 'rtl.createClass(this, "TObject", null, function () {',
  29775. ' this.$init = function () {',
  29776. ' };',
  29777. ' this.$final = function () {',
  29778. ' };',
  29779. ' this.GetGlob = function () {',
  29780. ' var Result = 0;',
  29781. ' var v1 = undefined;',
  29782. ' v1 = rtl.createCallback(this, "GetGlob");',
  29783. ' v1 = rtl.createCallback(this, "GetGlob");',
  29784. ' return Result;',
  29785. ' };',
  29786. ' this.Getter = function () {',
  29787. ' var Result = 0;',
  29788. ' var v2 = undefined;',
  29789. ' v2 = rtl.createCallback(this, "Getter");',
  29790. ' v2 = rtl.createCallback(this, "Getter");',
  29791. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29792. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29793. ' return Result;',
  29794. ' };',
  29795. '});',
  29796. 'this.GetIt = function (i) {',
  29797. ' var Result = 0;',
  29798. ' var v3 = undefined;',
  29799. ' v3 = $mod.GetIt;',
  29800. ' return Result;',
  29801. '};',
  29802. 'this.V = undefined;',
  29803. 'this.o = null;',
  29804. '']),
  29805. LinesToStr([ // $mod.$main
  29806. '$mod.V = $mod.GetIt;',
  29807. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29808. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29809. '']));
  29810. end;
  29811. procedure TTestModule.TestJSValue_ProcType_Equal;
  29812. begin
  29813. StartProgram(false);
  29814. Add('type');
  29815. Add(' integer = longint;');
  29816. Add(' TObject = class');
  29817. Add(' class function GetGlob: integer;');
  29818. Add(' function Getter: integer;');
  29819. Add(' end;');
  29820. Add('class function TObject.GetGlob: integer;');
  29821. Add('var v1: jsvalue;');
  29822. Add('begin');
  29823. Add(' if v1=@GetGlob then;');
  29824. Add(' if [email protected] then ;');
  29825. Add('end;');
  29826. Add('function TObject.Getter: integer;');
  29827. Add('var v2: jsvalue;');
  29828. Add('begin');
  29829. Add(' if v2=@Getter then;');
  29830. Add(' if [email protected] then ;');
  29831. Add(' if v2=@GetGlob then;');
  29832. Add(' if [email protected] then;');
  29833. Add('end;');
  29834. Add('function GetIt(i: integer): integer;');
  29835. Add('var v3: jsvalue;');
  29836. Add('begin');
  29837. Add(' if v3=@GetIt then;');
  29838. Add('end;');
  29839. Add('var');
  29840. Add(' V: JSValue;');
  29841. Add(' o: TObject;');
  29842. Add('begin');
  29843. Add(' if v=@GetIt then;');
  29844. Add(' if [email protected] then;');
  29845. Add(' if [email protected] then;');
  29846. Add(' if @GetIt=v then;');
  29847. Add(' if @o.Getter=v then;');
  29848. Add(' if @o.GetGlob=v then;');
  29849. ConvertProgram;
  29850. CheckSource('TestJSValue_ProcType_Equal',
  29851. LinesToStr([ // statements
  29852. 'rtl.createClass(this, "TObject", null, function () {',
  29853. ' this.$init = function () {',
  29854. ' };',
  29855. ' this.$final = function () {',
  29856. ' };',
  29857. ' this.GetGlob = function () {',
  29858. ' var Result = 0;',
  29859. ' var v1 = undefined;',
  29860. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29861. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29862. ' return Result;',
  29863. ' };',
  29864. ' this.Getter = function () {',
  29865. ' var Result = 0;',
  29866. ' var v2 = undefined;',
  29867. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29868. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29869. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29870. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29871. ' return Result;',
  29872. ' };',
  29873. '});',
  29874. 'this.GetIt = function (i) {',
  29875. ' var Result = 0;',
  29876. ' var v3 = undefined;',
  29877. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29878. ' return Result;',
  29879. '};',
  29880. 'this.V = undefined;',
  29881. 'this.o = null;',
  29882. '']),
  29883. LinesToStr([ // $mod.$main
  29884. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29885. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29886. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29887. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29888. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29889. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29890. '']));
  29891. end;
  29892. procedure TTestModule.TestJSValue_ProcType_Param;
  29893. begin
  29894. StartProgram(false);
  29895. Add([
  29896. 'type',
  29897. ' variant = jsvalue;',
  29898. ' TArrVariant = array of variant;',
  29899. ' TArrVar2 = TArrVariant;',
  29900. ' TFuncInt = function: longint;',
  29901. 'function GetIt: longint;',
  29902. 'begin',
  29903. 'end;',
  29904. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29905. 'var v: variant;',
  29906. 'begin',
  29907. ' v:=arr[1];',
  29908. 'end;',
  29909. 'var s: string;',
  29910. 'begin',
  29911. ' DoIt(GetIt,[]);',
  29912. ' DoIt(@GetIt,[]);',
  29913. ' DoIt(1,[s,GetIt]);',
  29914. ' DoIt(1,[s,@GetIt]);',
  29915. '']);
  29916. ConvertProgram;
  29917. CheckSource('TestJSValue_ProcType_Param',
  29918. LinesToStr([ // statements
  29919. 'this.GetIt = function () {',
  29920. ' var Result = 0;',
  29921. ' return Result;',
  29922. '};',
  29923. 'this.DoIt = function (p, Arr) {',
  29924. ' var v = undefined;',
  29925. ' v = Arr[1];',
  29926. '};',
  29927. 'this.s = "";',
  29928. '']),
  29929. LinesToStr([ // $mod.$main
  29930. '$mod.DoIt($mod.GetIt(), []);',
  29931. '$mod.DoIt($mod.GetIt, []);',
  29932. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29933. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29934. '']));
  29935. end;
  29936. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29937. begin
  29938. StartProgram(false);
  29939. Add([
  29940. 'var',
  29941. ' v: JSValue;',
  29942. ' p: Pointer;',
  29943. 'begin',
  29944. ' p:=v;',
  29945. '']);
  29946. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29947. nIncompatibleTypesGotExpected);
  29948. ConvertProgram;
  29949. end;
  29950. procedure TTestModule.TestJSValue_OverloadDouble;
  29951. begin
  29952. StartProgram(false);
  29953. Add([
  29954. 'type',
  29955. ' integer = longint;',
  29956. ' tdatetime = double;',
  29957. 'procedure DoIt(d: double); begin end;',
  29958. 'procedure DoIt(v: jsvalue); begin end;',
  29959. 'var',
  29960. ' d: double;',
  29961. ' dt: tdatetime;',
  29962. ' i: integer;',
  29963. ' b: byte;',
  29964. ' shi: shortint;',
  29965. ' w: word;',
  29966. ' smi: smallint;',
  29967. ' lw: longword;',
  29968. ' li: longint;',
  29969. ' ni: nativeint;',
  29970. ' nu: nativeuint;',
  29971. 'begin',
  29972. ' DoIt(d);',
  29973. ' DoIt(dt);',
  29974. ' DoIt(i);',
  29975. ' DoIt(b);',
  29976. ' DoIt(shi);',
  29977. ' DoIt(w);',
  29978. ' DoIt(smi);',
  29979. ' DoIt(lw);',
  29980. ' DoIt(li);',
  29981. ' DoIt(ni);',
  29982. ' DoIt(nu);',
  29983. '']);
  29984. ConvertProgram;
  29985. CheckSource('TestJSValue_OverloadDouble',
  29986. LinesToStr([ // statements
  29987. 'this.DoIt = function (d) {',
  29988. '};',
  29989. 'this.DoIt$1 = function (v) {',
  29990. '};',
  29991. 'this.d = 0.0;',
  29992. 'this.dt = 0.0;',
  29993. 'this.i = 0;',
  29994. 'this.b = 0;',
  29995. 'this.shi = 0;',
  29996. 'this.w = 0;',
  29997. 'this.smi = 0;',
  29998. 'this.lw = 0;',
  29999. 'this.li = 0;',
  30000. 'this.ni = 0;',
  30001. 'this.nu = 0;',
  30002. '']),
  30003. LinesToStr([ // $mod.$main
  30004. '$mod.DoIt($mod.d);',
  30005. '$mod.DoIt($mod.dt);',
  30006. '$mod.DoIt$1($mod.i);',
  30007. '$mod.DoIt$1($mod.b);',
  30008. '$mod.DoIt$1($mod.shi);',
  30009. '$mod.DoIt$1($mod.w);',
  30010. '$mod.DoIt$1($mod.smi);',
  30011. '$mod.DoIt$1($mod.lw);',
  30012. '$mod.DoIt$1($mod.li);',
  30013. '$mod.DoIt$1($mod.ni);',
  30014. '$mod.DoIt$1($mod.nu);',
  30015. '']));
  30016. end;
  30017. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30018. begin
  30019. StartProgram(false);
  30020. Add([
  30021. 'type',
  30022. ' integer = longint;',
  30023. ' int53 = nativeint;',
  30024. ' tdatetime = double;',
  30025. 'procedure DoIt(n: nativeint); begin end;',
  30026. 'procedure DoIt(v: jsvalue); begin end;',
  30027. 'var',
  30028. ' d: double;',
  30029. ' dt: tdatetime;',
  30030. ' i: integer;',
  30031. ' b: byte;',
  30032. ' shi: shortint;',
  30033. ' w: word;',
  30034. ' smi: smallint;',
  30035. ' lw: longword;',
  30036. ' li: longint;',
  30037. ' ni: nativeint;',
  30038. ' nu: nativeuint;',
  30039. 'begin',
  30040. ' DoIt(d);',
  30041. ' DoIt(dt);',
  30042. ' DoIt(i);',
  30043. ' DoIt(b);',
  30044. ' DoIt(shi);',
  30045. ' DoIt(w);',
  30046. ' DoIt(smi);',
  30047. ' DoIt(lw);',
  30048. ' DoIt(li);',
  30049. ' DoIt(ni);',
  30050. ' DoIt(nu);',
  30051. '']);
  30052. ConvertProgram;
  30053. CheckSource('TestJSValue_OverloadNativeInt',
  30054. LinesToStr([ // statements
  30055. 'this.DoIt = function (n) {',
  30056. '};',
  30057. 'this.DoIt$1 = function (v) {',
  30058. '};',
  30059. 'this.d = 0.0;',
  30060. 'this.dt = 0.0;',
  30061. 'this.i = 0;',
  30062. 'this.b = 0;',
  30063. 'this.shi = 0;',
  30064. 'this.w = 0;',
  30065. 'this.smi = 0;',
  30066. 'this.lw = 0;',
  30067. 'this.li = 0;',
  30068. 'this.ni = 0;',
  30069. 'this.nu = 0;',
  30070. '']),
  30071. LinesToStr([ // $mod.$main
  30072. '$mod.DoIt$1($mod.d);',
  30073. '$mod.DoIt$1($mod.dt);',
  30074. '$mod.DoIt($mod.i);',
  30075. '$mod.DoIt($mod.b);',
  30076. '$mod.DoIt($mod.shi);',
  30077. '$mod.DoIt($mod.w);',
  30078. '$mod.DoIt($mod.smi);',
  30079. '$mod.DoIt($mod.lw);',
  30080. '$mod.DoIt($mod.li);',
  30081. '$mod.DoIt($mod.ni);',
  30082. '$mod.DoIt($mod.nu);',
  30083. '']));
  30084. end;
  30085. procedure TTestModule.TestJSValue_OverloadWord;
  30086. begin
  30087. StartProgram(false);
  30088. Add([
  30089. 'type',
  30090. ' integer = longint;',
  30091. ' int53 = nativeint;',
  30092. ' tdatetime = double;',
  30093. 'procedure DoIt(w: word); begin end;',
  30094. 'procedure DoIt(v: jsvalue); begin end;',
  30095. 'var',
  30096. ' d: double;',
  30097. ' dt: tdatetime;',
  30098. ' i: integer;',
  30099. ' b: byte;',
  30100. ' shi: shortint;',
  30101. ' w: word;',
  30102. ' smi: smallint;',
  30103. ' lw: longword;',
  30104. ' li: longint;',
  30105. ' ni: nativeint;',
  30106. ' nu: nativeuint;',
  30107. 'begin',
  30108. ' DoIt(d);',
  30109. ' DoIt(dt);',
  30110. ' DoIt(i);',
  30111. ' DoIt(b);',
  30112. ' DoIt(shi);',
  30113. ' DoIt(w);',
  30114. ' DoIt(smi);',
  30115. ' DoIt(lw);',
  30116. ' DoIt(li);',
  30117. ' DoIt(ni);',
  30118. ' DoIt(nu);',
  30119. '']);
  30120. ConvertProgram;
  30121. CheckSource('TestJSValue_OverloadWord',
  30122. LinesToStr([ // statements
  30123. 'this.DoIt = function (w) {',
  30124. '};',
  30125. 'this.DoIt$1 = function (v) {',
  30126. '};',
  30127. 'this.d = 0.0;',
  30128. 'this.dt = 0.0;',
  30129. 'this.i = 0;',
  30130. 'this.b = 0;',
  30131. 'this.shi = 0;',
  30132. 'this.w = 0;',
  30133. 'this.smi = 0;',
  30134. 'this.lw = 0;',
  30135. 'this.li = 0;',
  30136. 'this.ni = 0;',
  30137. 'this.nu = 0;',
  30138. '']),
  30139. LinesToStr([ // $mod.$main
  30140. '$mod.DoIt$1($mod.d);',
  30141. '$mod.DoIt$1($mod.dt);',
  30142. '$mod.DoIt$1($mod.i);',
  30143. '$mod.DoIt($mod.b);',
  30144. '$mod.DoIt($mod.shi);',
  30145. '$mod.DoIt($mod.w);',
  30146. '$mod.DoIt$1($mod.smi);',
  30147. '$mod.DoIt$1($mod.lw);',
  30148. '$mod.DoIt$1($mod.li);',
  30149. '$mod.DoIt$1($mod.ni);',
  30150. '$mod.DoIt$1($mod.nu);',
  30151. '']));
  30152. end;
  30153. procedure TTestModule.TestJSValue_OverloadString;
  30154. begin
  30155. StartProgram(false);
  30156. Add([
  30157. 'type',
  30158. ' uni = string;',
  30159. ' WChar = char;',
  30160. 'procedure DoIt(s: string); begin end;',
  30161. 'procedure DoIt(v: jsvalue); begin end;',
  30162. 'var',
  30163. ' s: string;',
  30164. ' c: char;',
  30165. ' u: uni;',
  30166. 'begin',
  30167. ' DoIt(s);',
  30168. ' DoIt(c);',
  30169. ' DoIt(u);',
  30170. '']);
  30171. ConvertProgram;
  30172. CheckSource('TestJSValue_OverloadString',
  30173. LinesToStr([ // statements
  30174. 'this.DoIt = function (s) {',
  30175. '};',
  30176. 'this.DoIt$1 = function (v) {',
  30177. '};',
  30178. 'this.s = "";',
  30179. 'this.c = "\x00";',
  30180. 'this.u = "";',
  30181. '']),
  30182. LinesToStr([ // $mod.$main
  30183. '$mod.DoIt($mod.s);',
  30184. '$mod.DoIt($mod.c);',
  30185. '$mod.DoIt($mod.u);',
  30186. '']));
  30187. end;
  30188. procedure TTestModule.TestJSValue_OverloadChar;
  30189. begin
  30190. StartProgram(false);
  30191. Add([
  30192. 'type',
  30193. ' uni = string;',
  30194. ' WChar = char;',
  30195. 'procedure DoIt(c: char); begin end;',
  30196. 'procedure DoIt(v: jsvalue); begin end;',
  30197. 'var',
  30198. ' s: string;',
  30199. ' c: char;',
  30200. ' u: uni;',
  30201. 'begin',
  30202. ' DoIt(s);',
  30203. ' DoIt(c);',
  30204. ' DoIt(u);',
  30205. '']);
  30206. ConvertProgram;
  30207. CheckSource('TestJSValue_OverloadChar',
  30208. LinesToStr([ // statements
  30209. 'this.DoIt = function (c) {',
  30210. '};',
  30211. 'this.DoIt$1 = function (v) {',
  30212. '};',
  30213. 'this.s = "";',
  30214. 'this.c = "\x00";',
  30215. 'this.u = "";',
  30216. '']),
  30217. LinesToStr([ // $mod.$main
  30218. '$mod.DoIt$1($mod.s);',
  30219. '$mod.DoIt($mod.c);',
  30220. '$mod.DoIt$1($mod.u);',
  30221. '']));
  30222. end;
  30223. procedure TTestModule.TestJSValue_OverloadPointer;
  30224. begin
  30225. StartProgram(false);
  30226. Add([
  30227. 'type',
  30228. ' TObject = class end;',
  30229. 'procedure DoIt(p: pointer); begin end;',
  30230. 'procedure DoIt(v: jsvalue); begin end;',
  30231. 'var',
  30232. ' o: TObject;',
  30233. 'begin',
  30234. ' DoIt(o);',
  30235. '']);
  30236. ConvertProgram;
  30237. CheckSource('TestJSValue_OverloadPointer',
  30238. LinesToStr([ // statements
  30239. 'rtl.createClass(this, "TObject", null, function () {',
  30240. ' this.$init = function () {',
  30241. ' };',
  30242. ' this.$final = function () {',
  30243. ' };',
  30244. '});',
  30245. 'this.DoIt = function (p) {',
  30246. '};',
  30247. 'this.DoIt$1 = function (v) {',
  30248. '};',
  30249. 'this.o = null;',
  30250. '']),
  30251. LinesToStr([ // $mod.$main
  30252. '$mod.DoIt($mod.o);',
  30253. '']));
  30254. end;
  30255. procedure TTestModule.TestJSValue_ForIn;
  30256. begin
  30257. StartProgram(false);
  30258. Add([
  30259. 'var',
  30260. ' v: JSValue;',
  30261. ' key: string;',
  30262. 'begin',
  30263. ' for key in v do begin',
  30264. ' if key=''abc'' then ;',
  30265. ' end;',
  30266. '']);
  30267. ConvertProgram;
  30268. CheckSource('TestJSValue_ForIn',
  30269. LinesToStr([ // statements
  30270. 'this.v = undefined;',
  30271. 'this.key = "";',
  30272. '']),
  30273. LinesToStr([ // $mod.$main
  30274. 'for ($mod.key in $mod.v) {',
  30275. ' if ($mod.key === "abc") ;',
  30276. '};',
  30277. '']));
  30278. end;
  30279. procedure TTestModule.TestRTTI_IntRange;
  30280. begin
  30281. WithTypeInfo:=true;
  30282. StartProgram(true,[supTypeInfo]);
  30283. Add([
  30284. '{$modeswitch externalclass}',
  30285. 'type',
  30286. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30287. ' TColor = type TGraphicsColor;',
  30288. 'var',
  30289. ' p: TTypeInfo;',
  30290. ' k: TTypeKind;',
  30291. 'begin',
  30292. ' p:=typeinfo(TGraphicsColor);',
  30293. ' p:=typeinfo(TColor);',
  30294. ' k:=GetTypeKind(TGraphicsColor);',
  30295. ' k:=GetTypeKind(TColor);',
  30296. '']);
  30297. ConvertProgram;
  30298. CheckSource('TestRTTI_IntRange',
  30299. LinesToStr([ // statements
  30300. 'this.$rtti.$Int("TGraphicsColor", {',
  30301. ' minvalue: -2147483648,',
  30302. ' maxvalue: 2147483647,',
  30303. ' ordtype: 4',
  30304. '});',
  30305. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30306. 'this.p = null;',
  30307. 'this.k = 0;',
  30308. '']),
  30309. LinesToStr([ // $mod.$main
  30310. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30311. '$mod.p = $mod.$rtti["TColor"];',
  30312. '$mod.k = 1;',
  30313. '$mod.k = 1;',
  30314. '']));
  30315. end;
  30316. procedure TTestModule.TestRTTI_Double;
  30317. begin
  30318. WithTypeInfo:=true;
  30319. StartProgram(true,[supTypeInfo]);
  30320. Add([
  30321. '{$modeswitch externalclass}',
  30322. 'type',
  30323. ' TFloat = type double;',
  30324. 'var',
  30325. ' p: TTypeInfo;',
  30326. 'begin',
  30327. ' p:=typeinfo(double);',
  30328. ' p:=typeinfo(TFloat);',
  30329. '']);
  30330. ConvertProgram;
  30331. CheckSource('TestRTTI_Double',
  30332. LinesToStr([ // statements
  30333. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30334. 'this.p = null;',
  30335. '']),
  30336. LinesToStr([ // $mod.$main
  30337. '$mod.p = rtl.double;',
  30338. '$mod.p = $mod.$rtti["TFloat"];',
  30339. '']));
  30340. end;
  30341. procedure TTestModule.TestRTTI_ProcType;
  30342. begin
  30343. WithTypeInfo:=true;
  30344. StartProgram(false);
  30345. Add('type');
  30346. Add(' TProcA = procedure;');
  30347. Add(' TMethodB = procedure of object;');
  30348. Add(' TProcC = procedure; varargs;');
  30349. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30350. Add(' TProcE = function: nativeint;');
  30351. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30352. Add('var p: pointer;');
  30353. Add('begin');
  30354. Add(' p:=typeinfo(tproca);');
  30355. ConvertProgram;
  30356. CheckSource('TestRTTI_ProcType',
  30357. LinesToStr([ // statements
  30358. 'this.$rtti.$ProcVar("TProcA", {',
  30359. ' procsig: rtl.newTIProcSig([])',
  30360. '});',
  30361. 'this.$rtti.$MethodVar("TMethodB", {',
  30362. ' procsig: rtl.newTIProcSig([]),',
  30363. ' methodkind: 0',
  30364. '});',
  30365. 'this.$rtti.$ProcVar("TProcC", {',
  30366. ' procsig: rtl.newTIProcSig([], null, 2)',
  30367. '});',
  30368. 'this.$rtti.$ProcVar("TProcD", {',
  30369. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30370. '});',
  30371. 'this.$rtti.$ProcVar("TProcE", {',
  30372. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30373. '});',
  30374. 'this.$rtti.$ProcVar("TProcF", {',
  30375. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30376. '});',
  30377. 'this.p = null;',
  30378. '']),
  30379. LinesToStr([ // $mod.$main
  30380. '$mod.p = $mod.$rtti["TProcA"];',
  30381. '']));
  30382. end;
  30383. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30384. begin
  30385. WithTypeInfo:=true;
  30386. AddModuleWithIntfImplSrc('unit2.pas',
  30387. LinesToStr([
  30388. 'type',
  30389. ' TObject = class end;'
  30390. ]),
  30391. '');
  30392. StartUnit(true);
  30393. Add('interface');
  30394. Add('uses unit2;');
  30395. Add('type');
  30396. Add(' TProcA = function(o: tobject): tobject;');
  30397. Add('implementation');
  30398. Add('type');
  30399. Add(' TProcB = function(o: tobject): tobject;');
  30400. Add('var p: Pointer;');
  30401. Add('initialization');
  30402. Add(' p:=typeinfo(tproca);');
  30403. Add(' p:=typeinfo(tprocb);');
  30404. ConvertUnit;
  30405. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30406. LinesToStr([ // statements
  30407. 'var $impl = $mod.$impl;',
  30408. 'this.$rtti.$ProcVar("TProcA", {',
  30409. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30410. '});',
  30411. '']),
  30412. LinesToStr([ // this.$init
  30413. '$impl.p = $mod.$rtti["TProcA"];',
  30414. '$impl.p = $mod.$rtti["TProcB"];',
  30415. '']),
  30416. LinesToStr([ // implementation
  30417. '$mod.$rtti.$ProcVar("TProcB", {',
  30418. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30419. '});',
  30420. '$impl.p = null;',
  30421. '']) );
  30422. end;
  30423. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30424. begin
  30425. WithTypeInfo:=true;
  30426. StartProgram(false);
  30427. Add(['var',
  30428. ' ProcA: procedure;',
  30429. ' MethodB: procedure of object;',
  30430. ' ProcC: procedure; varargs;',
  30431. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30432. ' ProcE: function: nativeint;',
  30433. ' p: pointer;',
  30434. 'begin',
  30435. ' p:=typeinfo(proca);']);
  30436. ConvertProgram;
  30437. CheckSource('TestRTTI_ProcTypeAnonymous',
  30438. LinesToStr([ // statements
  30439. 'this.$rtti.$ProcVar("ProcA$a", {',
  30440. ' procsig: rtl.newTIProcSig([])',
  30441. '});',
  30442. 'this.ProcA = null;',
  30443. 'this.$rtti.$MethodVar("MethodB$a", {',
  30444. ' procsig: rtl.newTIProcSig([]),',
  30445. ' methodkind: 0',
  30446. '});',
  30447. 'this.MethodB = null;',
  30448. 'this.$rtti.$ProcVar("ProcC$a", {',
  30449. ' procsig: rtl.newTIProcSig([], null, 2)',
  30450. '});',
  30451. 'this.ProcC = null;',
  30452. 'this.$rtti.$ProcVar("ProcD$a", {',
  30453. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30454. '});',
  30455. 'this.ProcD = null;',
  30456. 'this.$rtti.$ProcVar("ProcE$a", {',
  30457. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30458. '});',
  30459. 'this.ProcE = null;',
  30460. 'this.p = null;',
  30461. '']),
  30462. LinesToStr([ // $mod.$main
  30463. '$mod.p = $mod.$rtti["ProcA$a"];',
  30464. '']));
  30465. end;
  30466. procedure TTestModule.TestRTTI_EnumAndSetType;
  30467. begin
  30468. WithTypeInfo:=true;
  30469. StartProgram(false);
  30470. Add('type');
  30471. Add(' TFlag = (light,dark);');
  30472. Add(' TFlags = set of TFlag;');
  30473. Add(' TProc = function(f: TFlags): TFlag;');
  30474. Add('var p: pointer;');
  30475. Add('begin');
  30476. Add(' p:=typeinfo(tflag);');
  30477. Add(' p:=typeinfo(tflags);');
  30478. ConvertProgram;
  30479. CheckSource('TestRTTI_EnumAndType',
  30480. LinesToStr([ // statements
  30481. 'this.TFlag = {',
  30482. ' "0": "light",',
  30483. ' light: 0,',
  30484. ' "1": "dark",',
  30485. ' dark: 1',
  30486. '};',
  30487. 'this.$rtti.$Enum("TFlag", {',
  30488. ' minvalue: 0,',
  30489. ' maxvalue: 1,',
  30490. ' ordtype: 1,',
  30491. ' enumtype: this.TFlag',
  30492. '});',
  30493. 'this.$rtti.$Set("TFlags", {',
  30494. ' comptype: this.$rtti["TFlag"]',
  30495. '});',
  30496. 'this.$rtti.$ProcVar("TProc", {',
  30497. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30498. '});',
  30499. 'this.p = null;',
  30500. '']),
  30501. LinesToStr([ // $mod.$main
  30502. '$mod.p = $mod.$rtti["TFlag"];',
  30503. '$mod.p = $mod.$rtti["TFlags"];',
  30504. '']));
  30505. end;
  30506. procedure TTestModule.TestRTTI_EnumRange;
  30507. begin
  30508. WithTypeInfo:=true;
  30509. StartProgram(false);
  30510. Add([
  30511. 'type',
  30512. ' TCol = (red,green,blue);',
  30513. ' TColRg = green..blue;',
  30514. ' TSetOfColRg = set of TColRg;',
  30515. 'var p: pointer;',
  30516. 'begin',
  30517. ' p:=typeinfo(tcolrg);',
  30518. ' p:=typeinfo(tsetofcolrg);',
  30519. '']);
  30520. ConvertProgram;
  30521. end;
  30522. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30523. begin
  30524. WithTypeInfo:=true;
  30525. StartProgram(false);
  30526. Add('type');
  30527. Add(' TFlags = set of (red, green);');
  30528. Add('var');
  30529. Add(' f: TFlags;');
  30530. Add('begin');
  30531. Add(' Include(f,red);');
  30532. ConvertProgram;
  30533. CheckSource('TestRTTI_AnonymousEnumType',
  30534. LinesToStr([ // statements
  30535. 'this.TFlags$a = {',
  30536. ' "0": "red",',
  30537. ' red: 0,',
  30538. ' "1": "green",',
  30539. ' green: 1',
  30540. '};',
  30541. 'this.$rtti.$Enum("TFlags$a", {',
  30542. ' minvalue: 0,',
  30543. ' maxvalue: 1,',
  30544. ' ordtype: 1,',
  30545. ' enumtype: this.TFlags$a',
  30546. '});',
  30547. 'this.$rtti.$Set("TFlags", {',
  30548. ' comptype: this.$rtti["TFlags$a"]',
  30549. '});',
  30550. 'this.f = {};',
  30551. '']),
  30552. LinesToStr([
  30553. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30554. '']));
  30555. end;
  30556. procedure TTestModule.TestRTTI_StaticArray;
  30557. begin
  30558. WithTypeInfo:=true;
  30559. StartProgram(false);
  30560. Add('type');
  30561. Add(' TFlag = (light,dark);');
  30562. Add(' TFlagNames = array[TFlag] of string;');
  30563. Add(' TBoolNames = array[boolean] of string;');
  30564. Add(' TByteArray = array[1..32768] of byte;');
  30565. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30566. Add('var p: pointer;');
  30567. Add('begin');
  30568. Add(' p:=typeinfo(TFlagNames);');
  30569. Add(' p:=typeinfo(TBoolNames);');
  30570. ConvertProgram;
  30571. CheckSource('TestRTTI_StaticArray',
  30572. LinesToStr([ // statements
  30573. 'this.TFlag = {',
  30574. ' "0": "light",',
  30575. ' light: 0,',
  30576. ' "1": "dark",',
  30577. ' dark: 1',
  30578. '};',
  30579. 'this.$rtti.$Enum("TFlag", {',
  30580. ' minvalue: 0,',
  30581. ' maxvalue: 1,',
  30582. ' ordtype: 1,',
  30583. ' enumtype: this.TFlag',
  30584. '});',
  30585. 'this.$rtti.$StaticArray("TFlagNames", {',
  30586. ' dims: [2],',
  30587. ' eltype: rtl.string',
  30588. '});',
  30589. 'this.$rtti.$StaticArray("TBoolNames", {',
  30590. ' dims: [2],',
  30591. ' eltype: rtl.string',
  30592. '});',
  30593. 'this.$rtti.$StaticArray("TByteArray", {',
  30594. ' dims: [32768],',
  30595. ' eltype: rtl.byte',
  30596. '});',
  30597. 'this.$rtti.$ProcVar("TProc", {',
  30598. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30599. '});',
  30600. 'this.p = null;',
  30601. '']),
  30602. LinesToStr([ // $mod.$main
  30603. '$mod.p = $mod.$rtti["TFlagNames"];',
  30604. '$mod.p = $mod.$rtti["TBoolNames"];',
  30605. '']));
  30606. end;
  30607. procedure TTestModule.TestRTTI_DynArray;
  30608. begin
  30609. WithTypeInfo:=true;
  30610. StartProgram(false);
  30611. Add('type');
  30612. Add(' TArrStr = array of string;');
  30613. Add(' TArr2Dim = array of tarrstr;');
  30614. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30615. Add('var p: pointer;');
  30616. Add('begin');
  30617. Add(' p:=typeinfo(tarrstr);');
  30618. Add(' p:=typeinfo(tarr2dim);');
  30619. ConvertProgram;
  30620. CheckSource('TestRTTI_DynArray',
  30621. LinesToStr([ // statements
  30622. 'this.$rtti.$DynArray("TArrStr", {',
  30623. ' eltype: rtl.string',
  30624. '});',
  30625. 'this.$rtti.$DynArray("TArr2Dim", {',
  30626. ' eltype: this.$rtti["TArrStr"]',
  30627. '});',
  30628. 'this.$rtti.$ProcVar("TProc", {',
  30629. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30630. '});',
  30631. 'this.p = null;',
  30632. '']),
  30633. LinesToStr([ // $mod.$main
  30634. '$mod.p = $mod.$rtti["TArrStr"];',
  30635. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30636. '']));
  30637. end;
  30638. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30639. begin
  30640. WithTypeInfo:=true;
  30641. StartProgram(false);
  30642. Add('type');
  30643. Add(' TArr = array of array of longint;');
  30644. Add('var a: TArr;');
  30645. Add('begin');
  30646. ConvertProgram;
  30647. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30648. LinesToStr([ // statements
  30649. 'this.$rtti.$DynArray("TArr$a", {',
  30650. ' eltype: rtl.longint',
  30651. '});',
  30652. 'this.$rtti.$DynArray("TArr", {',
  30653. ' eltype: this.$rtti["TArr$a"]',
  30654. '});',
  30655. 'this.a = [];',
  30656. '']),
  30657. LinesToStr([ // $mod.$main
  30658. ]));
  30659. end;
  30660. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30661. begin
  30662. WithTypeInfo:=true;
  30663. StartProgram(false);
  30664. Add('type');
  30665. Add(' TObject = class');
  30666. Add(' published');
  30667. Add(' procedure Proc; virtual; abstract;');
  30668. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30669. Add(' end;');
  30670. Add('begin');
  30671. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30672. nDuplicatePublishedMethodXAtY);
  30673. ConvertProgram;
  30674. end;
  30675. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30676. begin
  30677. WithTypeInfo:=true;
  30678. StartUnit(false);
  30679. Add([
  30680. 'interface',
  30681. 'type',
  30682. ' TObject = class',
  30683. ' end;',
  30684. ' {$M+}',
  30685. ' TBird = class',
  30686. ' procedure Fly;',
  30687. ' end;',
  30688. ' {$M-}',
  30689. 'type',
  30690. ' TEagle = class(TBird)',
  30691. ' procedure Fly;',
  30692. ' end;',
  30693. 'implementation',
  30694. 'procedure TBird.Fly;',
  30695. 'begin',
  30696. 'end;',
  30697. 'procedure TEagle.Fly;',
  30698. 'begin',
  30699. 'end;',
  30700. '']);
  30701. ConvertUnit;
  30702. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30703. LinesToStr([ // statements
  30704. 'rtl.createClass(this, "TObject", null, function () {',
  30705. ' this.$init = function () {',
  30706. ' };',
  30707. ' this.$final = function () {',
  30708. ' };',
  30709. '});',
  30710. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30711. ' this.Fly = function () {',
  30712. ' };',
  30713. ' var $r = this.$rtti;',
  30714. ' $r.addMethod("Fly", 0, []);',
  30715. '});',
  30716. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30717. ' this.Fly = function () {',
  30718. ' };',
  30719. ' var $r = this.$rtti;',
  30720. ' $r.addMethod("Fly", 0, []);',
  30721. '});',
  30722. '']),
  30723. LinesToStr([ // $mod.$main
  30724. ]));
  30725. CheckResolverUnexpectedHints(true);
  30726. end;
  30727. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30728. begin
  30729. WithTypeInfo:=true;
  30730. StartProgram(false);
  30731. Add('type');
  30732. Add(' TObject = class');
  30733. Add(' published');
  30734. Add(' procedure Proc; external name ''foo'';');
  30735. Add(' end;');
  30736. Add('begin');
  30737. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30738. nPublishedNameMustMatchExternal);
  30739. ConvertProgram;
  30740. end;
  30741. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30742. begin
  30743. WithTypeInfo:=true;
  30744. StartProgram(false);
  30745. Add('type');
  30746. Add(' TObject = class');
  30747. Add(' class var FA: longint;');
  30748. Add(' published');
  30749. Add(' class property A: longint read FA;');
  30750. Add(' end;');
  30751. Add('begin');
  30752. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30753. nInvalidXModifierY);
  30754. ConvertProgram;
  30755. end;
  30756. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30757. begin
  30758. WithTypeInfo:=true;
  30759. StartProgram(false);
  30760. Add('type');
  30761. Add(' TObject = class');
  30762. Add(' published');
  30763. Add(' class var FA: longint;');
  30764. Add(' end;');
  30765. Add('begin');
  30766. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30767. nSymbolCannotBePublished);
  30768. ConvertProgram;
  30769. end;
  30770. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30771. begin
  30772. WithTypeInfo:=true;
  30773. StartProgram(false);
  30774. Add('{$modeswitch externalclass}');
  30775. Add('type');
  30776. Add(' TObject = class');
  30777. Add(' published');
  30778. Add(' V: longint; external name ''foo'';');
  30779. Add(' end;');
  30780. Add('begin');
  30781. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30782. nPublishedNameMustMatchExternal);
  30783. ConvertProgram;
  30784. end;
  30785. procedure TTestModule.TestRTTI_Class_Field;
  30786. begin
  30787. WithTypeInfo:=true;
  30788. StartProgram(false);
  30789. Add('{$modeswitch externalclass}');
  30790. Add('type');
  30791. Add(' TObject = class');
  30792. Add(' private');
  30793. Add(' FPropA: string;');
  30794. Add(' published');
  30795. Add(' VarLI: longint;');
  30796. Add(' VarC: char;');
  30797. Add(' VarS: string;');
  30798. Add(' VarD: double;');
  30799. Add(' VarB: boolean;');
  30800. Add(' VarLW: longword;');
  30801. Add(' VarSmI: smallint;');
  30802. Add(' VarW: word;');
  30803. Add(' VarShI: shortint;');
  30804. Add(' VarBy: byte;');
  30805. Add(' VarExt: longint external name ''VarExt'';');
  30806. Add(' ArrA, ArrB: array of byte;');
  30807. Add(' end;');
  30808. Add('var p: pointer;');
  30809. Add(' Obj: tobject;');
  30810. Add('begin');
  30811. Add(' p:=typeinfo(tobject);');
  30812. Add(' p:=typeinfo(p);');
  30813. Add(' p:=typeinfo(obj);');
  30814. ConvertProgram;
  30815. CheckSource('TestRTTI_Class_Field',
  30816. LinesToStr([ // statements
  30817. 'rtl.createClass(this, "TObject", null, function () {',
  30818. ' this.$init = function () {',
  30819. ' this.FPropA = "";',
  30820. ' this.VarLI = 0;',
  30821. ' this.VarC = "\x00";',
  30822. ' this.VarS = "";',
  30823. ' this.VarD = 0.0;',
  30824. ' this.VarB = false;',
  30825. ' this.VarLW = 0;',
  30826. ' this.VarSmI = 0;',
  30827. ' this.VarW = 0;',
  30828. ' this.VarShI = 0;',
  30829. ' this.VarBy = 0;',
  30830. ' this.ArrA = [];',
  30831. ' this.ArrB = [];',
  30832. ' };',
  30833. ' this.$final = function () {',
  30834. ' this.ArrA = undefined;',
  30835. ' this.ArrB = undefined;',
  30836. ' };',
  30837. ' var $r = this.$rtti;',
  30838. ' $r.addField("VarLI", rtl.longint);',
  30839. ' $r.addField("VarC", rtl.char);',
  30840. ' $r.addField("VarS", rtl.string);',
  30841. ' $r.addField("VarD", rtl.double);',
  30842. ' $r.addField("VarB", rtl.boolean);',
  30843. ' $r.addField("VarLW", rtl.longword);',
  30844. ' $r.addField("VarSmI", rtl.smallint);',
  30845. ' $r.addField("VarW", rtl.word);',
  30846. ' $r.addField("VarShI", rtl.shortint);',
  30847. ' $r.addField("VarBy", rtl.byte);',
  30848. ' $r.addField("VarExt", rtl.longint);',
  30849. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30850. ' eltype: rtl.byte',
  30851. ' });',
  30852. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30853. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30854. '});',
  30855. 'this.p = null;',
  30856. 'this.Obj = null;',
  30857. '']),
  30858. LinesToStr([ // $mod.$main
  30859. '$mod.p = $mod.$rtti["TObject"];',
  30860. '$mod.p = rtl.pointer;',
  30861. '$mod.p = $mod.Obj.$rtti;',
  30862. '']));
  30863. end;
  30864. procedure TTestModule.TestRTTI_Class_Method;
  30865. begin
  30866. WithTypeInfo:=true;
  30867. StartProgram(false);
  30868. Add([
  30869. 'type',
  30870. ' TObject = class',
  30871. ' private',
  30872. ' procedure Internal; external name ''$intern'';',
  30873. ' published',
  30874. ' procedure Click; virtual; abstract;',
  30875. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30876. ' function GetNotify: boolean; external name ''GetNotify'';',
  30877. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30878. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30879. ' end;',
  30880. 'begin']);
  30881. ConvertProgram;
  30882. CheckSource('TestRTTI_Class_Method',
  30883. LinesToStr([ // statements
  30884. 'rtl.createClass(this, "TObject", null, function () {',
  30885. ' this.$init = function () {',
  30886. ' };',
  30887. ' this.$final = function () {',
  30888. ' };',
  30889. ' var $r = this.$rtti;',
  30890. ' $r.addMethod("Click", 0, []);',
  30891. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30892. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30893. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30894. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30895. '});',
  30896. '']),
  30897. LinesToStr([ // $mod.$main
  30898. '']));
  30899. end;
  30900. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30901. begin
  30902. WithTypeInfo:=true;
  30903. StartProgram(false);
  30904. Add('type');
  30905. Add(' TObject = class');
  30906. Add(' published');
  30907. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30908. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30909. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30910. Add(' end;');
  30911. Add('begin');
  30912. ConvertProgram;
  30913. CheckSource('TestRTTI_Class_MethodOpenArray',
  30914. LinesToStr([ // statements
  30915. 'rtl.createClass(this, "TObject", null, function () {',
  30916. ' this.$init = function () {',
  30917. ' };',
  30918. ' this.$final = function () {',
  30919. ' };',
  30920. ' var $r = this.$rtti;',
  30921. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30922. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30923. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30924. '});',
  30925. '']),
  30926. LinesToStr([ // $mod.$main
  30927. '']));
  30928. end;
  30929. procedure TTestModule.TestRTTI_Class_Property;
  30930. begin
  30931. WithTypeInfo:=true;
  30932. StartProgram(false);
  30933. Add('{$modeswitch externalclass}');
  30934. Add('type');
  30935. Add(' TObject = class');
  30936. Add(' private');
  30937. Add(' FColor: longint;');
  30938. Add(' FColorStored: boolean;');
  30939. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30940. Add(' function GetColor: longint; virtual; abstract;');
  30941. Add(' function GetColorStored: boolean; virtual; abstract;');
  30942. Add(' FExtSize: longint external name ''$extSize'';');
  30943. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30944. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30945. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30946. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30947. Add(' published');
  30948. Add(' property ColorA: longint read FColor;');
  30949. Add(' property ColorB: longint write FColor;');
  30950. Add(' property ColorC: longint read GetColor write SetColor;');
  30951. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30952. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30953. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30954. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30955. Add(' end;');
  30956. Add('begin');
  30957. ConvertProgram;
  30958. CheckSource('TestRTTI_Class_Property',
  30959. LinesToStr([ // statements
  30960. 'rtl.createClass(this, "TObject", null, function () {',
  30961. ' this.$init = function () {',
  30962. ' this.FColor = 0;',
  30963. ' this.FColorStored = false;',
  30964. ' };',
  30965. ' this.$final = function () {',
  30966. ' };',
  30967. ' var $r = this.$rtti;',
  30968. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30969. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30970. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30971. ' $r.addProperty(',
  30972. ' "ColorD",',
  30973. ' 8,',
  30974. ' rtl.longint,',
  30975. ' "FColor",',
  30976. ' "FColor",',
  30977. ' {',
  30978. ' stored: "FColorStored"',
  30979. ' }',
  30980. ' );',
  30981. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30982. ' $r.addProperty(',
  30983. ' "ExtSizeB",',
  30984. ' 11,',
  30985. ' rtl.longint,',
  30986. ' "$getSize",',
  30987. ' "$setSize",',
  30988. ' {',
  30989. ' stored: "$extSizeStored"',
  30990. ' }',
  30991. ' );',
  30992. ' $r.addProperty(',
  30993. ' "ExtSizeC",',
  30994. ' 12,',
  30995. ' rtl.longint,',
  30996. ' "$extSize",',
  30997. ' "$extSize",',
  30998. ' {',
  30999. ' stored: "$getExtSizeStored"',
  31000. ' }',
  31001. ' );',
  31002. '});',
  31003. '']),
  31004. LinesToStr([ // $mod.$main
  31005. '']));
  31006. end;
  31007. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31008. begin
  31009. WithTypeInfo:=true;
  31010. StartProgram(false);
  31011. Add('{$modeswitch externalclass}');
  31012. Add('type');
  31013. Add(' integer = longint;');
  31014. Add(' TObject = class');
  31015. Add(' private');
  31016. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31017. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31018. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31019. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31020. Add(' published');
  31021. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31022. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31023. Add(' end;');
  31024. Add('begin');
  31025. ConvertProgram;
  31026. CheckSource('TestRTTI_Class_PropertyParams',
  31027. LinesToStr([ // statements
  31028. 'rtl.createClass(this, "TObject", null, function () {',
  31029. ' this.$init = function () {',
  31030. ' };',
  31031. ' this.$final = function () {',
  31032. ' };',
  31033. ' var $r = this.$rtti;',
  31034. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31035. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31036. '});',
  31037. '']),
  31038. LinesToStr([ // $mod.$main
  31039. '']));
  31040. end;
  31041. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31042. begin
  31043. WithTypeInfo:=true;
  31044. AddModuleWithIntfImplSrc('unit1.pas',
  31045. 'type TColor = -5..5;',
  31046. '');
  31047. StartProgram(true);
  31048. Add([
  31049. 'uses unit1;',
  31050. 'type',
  31051. ' TColorAlias = TColor;',
  31052. ' TColorTypeAlias = type TColor;',
  31053. ' TObject = class',
  31054. ' private',
  31055. ' fColor: TColor;',
  31056. ' fAlias: TColorAlias;',
  31057. ' fTypeAlias: TColorTypeAlias;',
  31058. ' published',
  31059. ' property Color: TColor read fcolor;',
  31060. ' property Alias: TColorAlias read falias;',
  31061. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31062. ' end;',
  31063. 'begin',
  31064. '']);
  31065. ConvertProgram;
  31066. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31067. LinesToStr([ // statements
  31068. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31069. 'rtl.createClass(this, "TObject", null, function () {',
  31070. ' this.$init = function () {',
  31071. ' this.fColor = 0;',
  31072. ' this.fAlias = 0;',
  31073. ' this.fTypeAlias = 0;',
  31074. ' };',
  31075. ' this.$final = function () {',
  31076. ' };',
  31077. ' var $r = this.$rtti;',
  31078. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31079. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31080. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31081. '});',
  31082. '']),
  31083. LinesToStr([ // $mod.$main
  31084. '']));
  31085. end;
  31086. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31087. begin
  31088. WithTypeInfo:=true;
  31089. StartProgram(false);
  31090. Add([
  31091. '{$modeswitch omitrtti}',
  31092. 'type',
  31093. ' TObject = class',
  31094. ' private',
  31095. ' FA: byte;',
  31096. ' published',
  31097. ' property A: byte read FA write FA;',
  31098. ' end;',
  31099. 'begin']);
  31100. ConvertProgram;
  31101. CheckSource('TestRTTI_Class_OmitRTTI',
  31102. LinesToStr([ // statements
  31103. 'rtl.createClass(this, "TObject", null, function () {',
  31104. ' this.$init = function () {',
  31105. ' this.FA = 0;',
  31106. ' };',
  31107. ' this.$final = function () {',
  31108. ' };',
  31109. '});',
  31110. '']),
  31111. LinesToStr([ // $mod.$main
  31112. '']));
  31113. end;
  31114. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31115. begin
  31116. WithTypeInfo:=true;
  31117. StartUnit(true,[supTObject]);
  31118. Add([
  31119. 'interface',
  31120. 'type',
  31121. ' {$M+}',
  31122. ' TBird = class',
  31123. ' published',
  31124. ' Swarm: array of TBird;',
  31125. ' end;',
  31126. 'implementation',
  31127. '']);
  31128. ConvertUnit;
  31129. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31130. LinesToStr([ // statements
  31131. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31132. ' this.$init = function () {',
  31133. ' pas.system.TObject.$init.call(this);',
  31134. ' this.Swarm = [];',
  31135. ' };',
  31136. ' this.$final = function () {',
  31137. ' this.Swarm = undefined;',
  31138. ' pas.system.TObject.$final.call(this);',
  31139. ' };',
  31140. ' var $r = this.$rtti;',
  31141. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31142. ' eltype: $r',
  31143. ' });',
  31144. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31145. '});',
  31146. '']),
  31147. LinesToStr([ // $mod.$main
  31148. '']));
  31149. end;
  31150. procedure TTestModule.TestRTTI_IndexModifier;
  31151. begin
  31152. WithTypeInfo:=true;
  31153. StartProgram(false);
  31154. Add([
  31155. 'type',
  31156. ' TEnum = (red, blue);',
  31157. ' TObject = class',
  31158. ' FB: boolean;',
  31159. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31160. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31161. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31162. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31163. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31164. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31165. ' published',
  31166. ' property B1: boolean index 1 read FB write SetIntBool;',
  31167. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31168. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31169. ' end;',
  31170. 'begin']);
  31171. ConvertProgram;
  31172. CheckSource('TestRTTI_IndexModifier',
  31173. LinesToStr([ // statements
  31174. 'this.TEnum = {',
  31175. ' "0": "red",',
  31176. ' red: 0,',
  31177. ' "1": "blue",',
  31178. ' blue: 1',
  31179. '};',
  31180. 'this.$rtti.$Enum("TEnum", {',
  31181. ' minvalue: 0,',
  31182. ' maxvalue: 1,',
  31183. ' ordtype: 1,',
  31184. ' enumtype: this.TEnum',
  31185. '});',
  31186. 'rtl.createClass(this, "TObject", null, function () {',
  31187. ' this.$init = function () {',
  31188. ' this.FB = false;',
  31189. ' };',
  31190. ' this.$final = function () {',
  31191. ' };',
  31192. ' var $r = this.$rtti;',
  31193. ' $r.addProperty(',
  31194. ' "B1",',
  31195. ' 18,',
  31196. ' rtl.boolean,',
  31197. ' "FB",',
  31198. ' "SetIntBool",',
  31199. ' {',
  31200. ' index: 1',
  31201. ' }',
  31202. ' );',
  31203. ' $r.addProperty(',
  31204. ' "B2",',
  31205. ' 17,',
  31206. ' rtl.boolean,',
  31207. ' "GetEnumBool",',
  31208. ' "FB",',
  31209. ' {',
  31210. ' index: $mod.TEnum.blue',
  31211. ' }',
  31212. ' );',
  31213. ' $r.addProperty(',
  31214. ' "I1",',
  31215. ' 19,',
  31216. ' rtl.boolean,',
  31217. ' "GetStrIntBool",',
  31218. ' "SetStrIntBool",',
  31219. ' {',
  31220. ' index: 2',
  31221. ' }',
  31222. ' );',
  31223. '});',
  31224. '']),
  31225. LinesToStr([ // $mod.$main
  31226. '']));
  31227. end;
  31228. procedure TTestModule.TestRTTI_StoredModifier;
  31229. begin
  31230. WithTypeInfo:=true;
  31231. StartProgram(false);
  31232. Add([
  31233. 'const',
  31234. ' ConstB = true;',
  31235. 'type',
  31236. ' TObject = class',
  31237. ' private',
  31238. ' FB: boolean;',
  31239. ' function IsBStored: boolean; virtual; abstract;',
  31240. ' published',
  31241. ' property BoolA: boolean read FB stored true;',
  31242. ' property BoolB: boolean read FB stored false;',
  31243. ' property BoolC: boolean read FB stored FB;',
  31244. ' property BoolD: boolean read FB stored ConstB;',
  31245. ' property BoolE: boolean read FB stored IsBStored;',
  31246. ' end;',
  31247. 'begin']);
  31248. ConvertProgram;
  31249. CheckSource('TestRTTI_StoredModifier',
  31250. LinesToStr([ // statements
  31251. 'this.ConstB = true;',
  31252. 'rtl.createClass(this, "TObject", null, function () {',
  31253. ' this.$init = function () {',
  31254. ' this.FB = false;',
  31255. ' };',
  31256. ' this.$final = function () {',
  31257. ' };',
  31258. ' var $r = this.$rtti;',
  31259. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31260. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31261. ' $r.addProperty(',
  31262. ' "BoolC",',
  31263. ' 8,',
  31264. ' rtl.boolean,',
  31265. ' "FB",',
  31266. ' "",',
  31267. ' {',
  31268. ' stored: "FB"',
  31269. ' }',
  31270. ' );',
  31271. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31272. ' $r.addProperty(',
  31273. ' "BoolE",',
  31274. ' 12,',
  31275. ' rtl.boolean,',
  31276. ' "FB",',
  31277. ' "",',
  31278. ' {',
  31279. ' stored: "IsBStored"',
  31280. ' }',
  31281. ' );',
  31282. '});',
  31283. '']),
  31284. LinesToStr([ // $mod.$main
  31285. '']));
  31286. end;
  31287. procedure TTestModule.TestRTTI_DefaultValue;
  31288. begin
  31289. WithTypeInfo:=true;
  31290. StartProgram(false);
  31291. Add([
  31292. 'type',
  31293. ' TEnum = (red, blue);',
  31294. 'const',
  31295. ' CB = true or false;',
  31296. ' CI = 1+2;',
  31297. 'type',
  31298. ' TObject = class',
  31299. ' FB: boolean;',
  31300. ' FI: longint;',
  31301. ' FE: TEnum;',
  31302. ' published',
  31303. ' property B1: boolean read FB default true;',
  31304. ' property B2: boolean read FB default CB;',
  31305. ' property B3: boolean read FB default test1.cb;',
  31306. ' property I1: longint read FI default 2;',
  31307. ' property I2: longint read FI default CI;',
  31308. ' property E1: TEnum read FE default red;',
  31309. ' property E2: TEnum read FE default TEnum.blue;',
  31310. ' end;',
  31311. 'begin']);
  31312. ConvertProgram;
  31313. CheckSource('TestRTTI_DefaultValue',
  31314. LinesToStr([ // statements
  31315. 'this.TEnum = {',
  31316. ' "0": "red",',
  31317. ' red: 0,',
  31318. ' "1": "blue",',
  31319. ' blue: 1',
  31320. '};',
  31321. 'this.$rtti.$Enum("TEnum", {',
  31322. ' minvalue: 0,',
  31323. ' maxvalue: 1,',
  31324. ' ordtype: 1,',
  31325. ' enumtype: this.TEnum',
  31326. '});',
  31327. 'this.CB = true || false;',
  31328. 'this.CI = 1 + 2;',
  31329. 'rtl.createClass(this, "TObject", null, function () {',
  31330. ' this.$init = function () {',
  31331. ' this.FB = false;',
  31332. ' this.FI = 0;',
  31333. ' this.FE = 0;',
  31334. ' };',
  31335. ' this.$final = function () {',
  31336. ' };',
  31337. ' var $r = this.$rtti;',
  31338. ' $r.addProperty(',
  31339. ' "B1",',
  31340. ' 0,',
  31341. ' rtl.boolean,',
  31342. ' "FB",',
  31343. ' "",',
  31344. ' {',
  31345. ' Default: true',
  31346. ' }',
  31347. ' );',
  31348. ' $r.addProperty(',
  31349. ' "B2",',
  31350. ' 0,',
  31351. ' rtl.boolean,',
  31352. ' "FB",',
  31353. ' "",',
  31354. ' {',
  31355. ' Default: true',
  31356. ' }',
  31357. ' );',
  31358. ' $r.addProperty(',
  31359. ' "B3",',
  31360. ' 0,',
  31361. ' rtl.boolean,',
  31362. ' "FB",',
  31363. ' "",',
  31364. ' {',
  31365. ' Default: true',
  31366. ' }',
  31367. ' );',
  31368. ' $r.addProperty(',
  31369. ' "I1",',
  31370. ' 0,',
  31371. ' rtl.longint,',
  31372. ' "FI",',
  31373. ' "",',
  31374. ' {',
  31375. ' Default: 2',
  31376. ' }',
  31377. ' );',
  31378. ' $r.addProperty(',
  31379. ' "I2",',
  31380. ' 0,',
  31381. ' rtl.longint,',
  31382. ' "FI",',
  31383. ' "",',
  31384. ' {',
  31385. ' Default: 3',
  31386. ' }',
  31387. ' );',
  31388. ' $r.addProperty(',
  31389. ' "E1",',
  31390. ' 0,',
  31391. ' $mod.$rtti["TEnum"],',
  31392. ' "FE",',
  31393. ' "",',
  31394. ' {',
  31395. ' Default: $mod.TEnum.red',
  31396. ' }',
  31397. ' );',
  31398. ' $r.addProperty(',
  31399. ' "E2",',
  31400. ' 0,',
  31401. ' $mod.$rtti["TEnum"],',
  31402. ' "FE",',
  31403. ' "",',
  31404. ' {',
  31405. ' Default: $mod.TEnum.blue',
  31406. ' }',
  31407. ' );',
  31408. '});',
  31409. '']),
  31410. LinesToStr([ // $mod.$main
  31411. '']));
  31412. end;
  31413. procedure TTestModule.TestRTTI_DefaultValueSet;
  31414. begin
  31415. WithTypeInfo:=true;
  31416. StartProgram(false);
  31417. Add([
  31418. 'type',
  31419. ' TEnum = (red, blue);',
  31420. ' TSet = set of TEnum;',
  31421. 'const',
  31422. ' CSet = [red,blue];',
  31423. 'type',
  31424. ' TObject = class',
  31425. ' FSet: TSet;',
  31426. ' published',
  31427. ' property Set1: TSet read FSet default [];',
  31428. ' property Set2: TSet read FSet default [red];',
  31429. ' property Set3: TSet read FSet default [red,blue];',
  31430. ' property Set4: TSet read FSet default CSet;',
  31431. ' end;',
  31432. 'begin']);
  31433. ConvertProgram;
  31434. CheckSource('TestRTTI_DefaultValueSet',
  31435. LinesToStr([ // statements
  31436. 'this.TEnum = {',
  31437. ' "0": "red",',
  31438. ' red: 0,',
  31439. ' "1": "blue",',
  31440. ' blue: 1',
  31441. '};',
  31442. 'this.$rtti.$Enum("TEnum", {',
  31443. ' minvalue: 0,',
  31444. ' maxvalue: 1,',
  31445. ' ordtype: 1,',
  31446. ' enumtype: this.TEnum',
  31447. '});',
  31448. 'this.$rtti.$Set("TSet", {',
  31449. ' comptype: this.$rtti["TEnum"]',
  31450. '});',
  31451. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31452. 'rtl.createClass(this, "TObject", null, function () {',
  31453. ' this.$init = function () {',
  31454. ' this.FSet = {};',
  31455. ' };',
  31456. ' this.$final = function () {',
  31457. ' this.FSet = undefined;',
  31458. ' };',
  31459. ' var $r = this.$rtti;',
  31460. ' $r.addProperty(',
  31461. ' "Set1",',
  31462. ' 0,',
  31463. ' $mod.$rtti["TSet"],',
  31464. ' "FSet",',
  31465. ' "",',
  31466. ' {',
  31467. ' Default: {}',
  31468. ' }',
  31469. ' );',
  31470. ' $r.addProperty(',
  31471. ' "Set2",',
  31472. ' 0,',
  31473. ' $mod.$rtti["TSet"],',
  31474. ' "FSet",',
  31475. ' "",',
  31476. ' {',
  31477. ' Default: rtl.createSet($mod.TEnum.red)',
  31478. ' }',
  31479. ' );',
  31480. ' $r.addProperty(',
  31481. ' "Set3",',
  31482. ' 0,',
  31483. ' $mod.$rtti["TSet"],',
  31484. ' "FSet",',
  31485. ' "",',
  31486. ' {',
  31487. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31488. ' }',
  31489. ' );',
  31490. ' $r.addProperty(',
  31491. ' "Set4",',
  31492. ' 0,',
  31493. ' $mod.$rtti["TSet"],',
  31494. ' "FSet",',
  31495. ' "",',
  31496. ' {',
  31497. ' Default: $mod.CSet',
  31498. ' }',
  31499. ' );',
  31500. '});',
  31501. '']),
  31502. LinesToStr([ // $mod.$main
  31503. '']));
  31504. end;
  31505. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31506. begin
  31507. WithTypeInfo:=true;
  31508. StartProgram(false);
  31509. Add([
  31510. 'type',
  31511. ' TRg = -1..1;',
  31512. 'const',
  31513. ' l = low(TRg);',
  31514. ' h = high(TRg);',
  31515. 'type',
  31516. ' TObject = class',
  31517. ' FV: TRg;',
  31518. ' published',
  31519. ' property V1: TRg read FV default -1;',
  31520. ' end;',
  31521. 'begin']);
  31522. ConvertProgram;
  31523. CheckSource('TestRTTI_DefaultValueRangeType',
  31524. LinesToStr([ // statements
  31525. 'this.$rtti.$Int("TRg", {',
  31526. ' minvalue: -1,',
  31527. ' maxvalue: 1,',
  31528. ' ordtype: 0',
  31529. '});',
  31530. 'this.l = -1;',
  31531. 'this.h = 1;',
  31532. 'rtl.createClass(this, "TObject", null, function () {',
  31533. ' this.$init = function () {',
  31534. ' this.FV = 0;',
  31535. ' };',
  31536. ' this.$final = function () {',
  31537. ' };',
  31538. ' var $r = this.$rtti;',
  31539. ' $r.addProperty(',
  31540. ' "V1",',
  31541. ' 0,',
  31542. ' $mod.$rtti["TRg"],',
  31543. ' "FV",',
  31544. ' "",',
  31545. ' {',
  31546. ' Default: -1',
  31547. ' }',
  31548. ' );',
  31549. '});',
  31550. '']),
  31551. LinesToStr([ // $mod.$main
  31552. '']));
  31553. end;
  31554. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31555. begin
  31556. WithTypeInfo:=true;
  31557. StartProgram(false);
  31558. Add([
  31559. 'type',
  31560. ' TObject = class',
  31561. ' FA, FB: byte;',
  31562. ' property A: byte read FA default 1;',
  31563. ' property B: byte read FB default 2;',
  31564. ' end;',
  31565. ' TBird = class',
  31566. ' published',
  31567. ' property A;',
  31568. ' property B nodefault;',
  31569. ' end;',
  31570. 'begin']);
  31571. ConvertProgram;
  31572. CheckSource('TestRTTI_DefaultValueInherit',
  31573. LinesToStr([ // statements
  31574. 'rtl.createClass(this, "TObject", null, function () {',
  31575. ' this.$init = function () {',
  31576. ' this.FA = 0;',
  31577. ' this.FB = 0;',
  31578. ' };',
  31579. ' this.$final = function () {',
  31580. ' };',
  31581. '});',
  31582. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31583. ' var $r = this.$rtti;',
  31584. ' $r.addProperty(',
  31585. ' "A",',
  31586. ' 0,',
  31587. ' rtl.byte,',
  31588. ' "FA",',
  31589. ' "",',
  31590. ' {',
  31591. ' Default: 1',
  31592. ' }',
  31593. ' );',
  31594. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31595. '});',
  31596. '']),
  31597. LinesToStr([ // $mod.$main
  31598. '']));
  31599. end;
  31600. procedure TTestModule.TestRTTI_OverrideMethod;
  31601. begin
  31602. WithTypeInfo:=true;
  31603. StartProgram(false);
  31604. Add('type');
  31605. Add(' TObject = class');
  31606. Add(' published');
  31607. Add(' procedure DoIt; virtual; abstract;');
  31608. Add(' end;');
  31609. Add(' TSky = class');
  31610. Add(' published');
  31611. Add(' procedure DoIt; override;');
  31612. Add(' end;');
  31613. Add('procedure TSky.DoIt; begin end;');
  31614. Add('begin');
  31615. ConvertProgram;
  31616. CheckSource('TestRTTI_OverrideMethod',
  31617. LinesToStr([ // statements
  31618. 'rtl.createClass(this, "TObject", null, function () {',
  31619. ' this.$init = function () {',
  31620. ' };',
  31621. ' this.$final = function () {',
  31622. ' };',
  31623. ' var $r = this.$rtti;',
  31624. ' $r.addMethod("DoIt", 0, []);',
  31625. '});',
  31626. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31627. ' this.DoIt = function () {',
  31628. ' };',
  31629. '});',
  31630. '']),
  31631. LinesToStr([ // $mod.$main
  31632. '']));
  31633. end;
  31634. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31635. begin
  31636. WithTypeInfo:=true;
  31637. StartProgram(false);
  31638. Add([
  31639. 'type',
  31640. ' TObject = class',
  31641. ' published',
  31642. ' procedure DoIt;',
  31643. ' end;',
  31644. ' TSky = class',
  31645. ' published',
  31646. ' procedure DoIt; reintroduce;',
  31647. ' end;',
  31648. 'procedure TObject.DoIt; begin end;',
  31649. 'procedure TSky.DoIt;',
  31650. 'begin',
  31651. ' inherited DoIt;',
  31652. 'end;',
  31653. 'begin']);
  31654. ConvertProgram;
  31655. CheckSource('TestRTTI_ReintroduceMethod',
  31656. LinesToStr([ // statements
  31657. 'rtl.createClass(this, "TObject", null, function () {',
  31658. ' this.$init = function () {',
  31659. ' };',
  31660. ' this.$final = function () {',
  31661. ' };',
  31662. ' this.DoIt = function () {',
  31663. ' };',
  31664. ' var $r = this.$rtti;',
  31665. ' $r.addMethod("DoIt", 0, []);',
  31666. '});',
  31667. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31668. ' this.DoIt = function () {',
  31669. ' $mod.TObject.DoIt.call(this);',
  31670. ' };',
  31671. ' var $r = this.$rtti;',
  31672. ' $r.addMethod("DoIt", 0, []);',
  31673. '});',
  31674. '']),
  31675. LinesToStr([ // $mod.$main
  31676. '']));
  31677. end;
  31678. procedure TTestModule.TestRTTI_OverloadProperty;
  31679. begin
  31680. WithTypeInfo:=true;
  31681. StartProgram(false);
  31682. Add('type');
  31683. Add(' TObject = class');
  31684. Add(' protected');
  31685. Add(' FFlag: longint;');
  31686. Add(' published');
  31687. Add(' property Flag: longint read fflag;');
  31688. Add(' end;');
  31689. Add(' TSky = class');
  31690. Add(' published');
  31691. Add(' property FLAG: longint write fflag;');
  31692. Add(' end;');
  31693. Add('begin');
  31694. ConvertProgram;
  31695. CheckSource('TestRTTI_OverrideMethod',
  31696. LinesToStr([ // statements
  31697. 'rtl.createClass(this, "TObject", null, function () {',
  31698. ' this.$init = function () {',
  31699. ' this.FFlag = 0;',
  31700. ' };',
  31701. ' this.$final = function () {',
  31702. ' };',
  31703. ' var $r = this.$rtti;',
  31704. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31705. '});',
  31706. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31707. ' var $r = this.$rtti;',
  31708. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31709. '});',
  31710. '']),
  31711. LinesToStr([ // $mod.$main
  31712. '']));
  31713. end;
  31714. procedure TTestModule.TestRTTI_ClassForward;
  31715. begin
  31716. WithTypeInfo:=true;
  31717. StartProgram(false);
  31718. Add('type');
  31719. Add(' TObject = class end;');
  31720. Add(' tbridge = class;');
  31721. Add(' TProc = function: tbridge;');
  31722. Add(' TOger = class');
  31723. Add(' published');
  31724. Add(' FBridge: tbridge;');
  31725. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31726. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31727. Add(' end;');
  31728. Add(' TBridge = class');
  31729. Add(' FOger: toger;');
  31730. Add(' end;');
  31731. Add('var p: Pointer;');
  31732. Add(' b: tbridge;');
  31733. Add('begin');
  31734. Add(' p:=typeinfo(tbridge);');
  31735. Add(' p:=typeinfo(b);');
  31736. ConvertProgram;
  31737. CheckSource('TestRTTI_ClassForward',
  31738. LinesToStr([ // statements
  31739. 'rtl.createClass(this, "TObject", null, function () {',
  31740. ' this.$init = function () {',
  31741. ' };',
  31742. ' this.$final = function () {',
  31743. ' };',
  31744. '});',
  31745. 'this.$rtti.$Class("TBridge");',
  31746. 'this.$rtti.$ProcVar("TProc", {',
  31747. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31748. '});',
  31749. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31750. ' this.$init = function () {',
  31751. ' $mod.TObject.$init.call(this);',
  31752. ' this.FBridge = null;',
  31753. ' };',
  31754. ' this.$final = function () {',
  31755. ' this.FBridge = undefined;',
  31756. ' $mod.TObject.$final.call(this);',
  31757. ' };',
  31758. ' var $r = this.$rtti;',
  31759. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31760. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31761. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31762. '});',
  31763. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31764. ' this.$init = function () {',
  31765. ' $mod.TObject.$init.call(this);',
  31766. ' this.FOger = null;',
  31767. ' };',
  31768. ' this.$final = function () {',
  31769. ' this.FOger = undefined;',
  31770. ' $mod.TObject.$final.call(this);',
  31771. ' };',
  31772. '});',
  31773. 'this.p = null;',
  31774. 'this.b = null;',
  31775. '']),
  31776. LinesToStr([ // $mod.$main
  31777. '$mod.p = $mod.$rtti["TBridge"];',
  31778. '$mod.p = $mod.b.$rtti;',
  31779. '']));
  31780. end;
  31781. procedure TTestModule.TestRTTI_ClassOf;
  31782. begin
  31783. WithTypeInfo:=true;
  31784. StartProgram(false);
  31785. Add('type');
  31786. Add(' TClass = class of tobject;');
  31787. Add(' TProcA = function: TClass;');
  31788. Add(' TObject = class');
  31789. Add(' published');
  31790. Add(' C: tclass;');
  31791. Add(' end;');
  31792. Add(' tfox = class;');
  31793. Add(' TBird = class end;');
  31794. Add(' TBirds = class of tbird;');
  31795. Add(' TFox = class end;');
  31796. Add(' TFoxes = class of tfox;');
  31797. Add(' TCows = class of TCow;');
  31798. Add(' TCow = class;');
  31799. Add(' TCow = class end;');
  31800. Add('begin');
  31801. ConvertProgram;
  31802. CheckSource('TestRTTI_ClassOf',
  31803. LinesToStr([ // statements
  31804. 'this.$rtti.$Class("TObject");',
  31805. 'this.$rtti.$ClassRef("TClass", {',
  31806. ' instancetype: this.$rtti["TObject"]',
  31807. '});',
  31808. 'this.$rtti.$ProcVar("TProcA", {',
  31809. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31810. '});',
  31811. 'rtl.createClass(this, "TObject", null, function () {',
  31812. ' this.$init = function () {',
  31813. ' this.C = null;',
  31814. ' };',
  31815. ' this.$final = function () {',
  31816. ' this.C = undefined;',
  31817. ' };',
  31818. ' var $r = this.$rtti;',
  31819. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31820. '});',
  31821. 'this.$rtti.$Class("TFox");',
  31822. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31823. '});',
  31824. 'this.$rtti.$ClassRef("TBirds", {',
  31825. ' instancetype: this.$rtti["TBird"]',
  31826. '});',
  31827. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31828. '});',
  31829. 'this.$rtti.$ClassRef("TFoxes", {',
  31830. ' instancetype: this.$rtti["TFox"]',
  31831. '});',
  31832. 'this.$rtti.$Class("TCow");',
  31833. 'this.$rtti.$ClassRef("TCows", {',
  31834. ' instancetype: this.$rtti["TCow"]',
  31835. '});',
  31836. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31837. '});',
  31838. '']),
  31839. LinesToStr([ // $mod.$main
  31840. '']));
  31841. end;
  31842. procedure TTestModule.TestRTTI_Record;
  31843. begin
  31844. WithTypeInfo:=true;
  31845. StartProgram(false);
  31846. Add('type');
  31847. Add(' integer = longint;');
  31848. Add(' TPoint = record');
  31849. Add(' x,y: integer;');
  31850. Add(' end;');
  31851. Add('var p: pointer;');
  31852. Add(' r: tpoint;');
  31853. Add('begin');
  31854. Add(' p:=typeinfo(tpoint);');
  31855. Add(' p:=typeinfo(r);');
  31856. Add(' p:=typeinfo(r.x);');
  31857. ConvertProgram;
  31858. CheckSource('TestRTTI_Record',
  31859. LinesToStr([ // statements
  31860. 'rtl.recNewT(this, "TPoint", function () {',
  31861. ' this.x = 0;',
  31862. ' this.y = 0;',
  31863. ' this.$eq = function (b) {',
  31864. ' return (this.x === b.x) && (this.y === b.y);',
  31865. ' };',
  31866. ' this.$assign = function (s) {',
  31867. ' this.x = s.x;',
  31868. ' this.y = s.y;',
  31869. ' return this;',
  31870. ' };',
  31871. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31872. ' $r.addField("x", rtl.longint);',
  31873. ' $r.addField("y", rtl.longint);',
  31874. '});',
  31875. 'this.p = null;',
  31876. 'this.r = this.TPoint.$new();',
  31877. '']),
  31878. LinesToStr([ // $mod.$main
  31879. '$mod.p = $mod.$rtti["TPoint"];',
  31880. '$mod.p = $mod.$rtti["TPoint"];',
  31881. '$mod.p = rtl.longint;',
  31882. '']));
  31883. end;
  31884. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31885. begin
  31886. WithTypeInfo:=true;
  31887. StartProgram(false);
  31888. Add('type');
  31889. Add(' TFloatRec = record');
  31890. Add(' c,d: array of char;');
  31891. // Add(' i: array of array of longint;');
  31892. Add(' end;');
  31893. Add('var p: pointer;');
  31894. Add(' r: tfloatrec;');
  31895. Add('begin');
  31896. Add(' p:=typeinfo(tfloatrec);');
  31897. Add(' p:=typeinfo(r);');
  31898. Add(' p:=typeinfo(r.d);');
  31899. ConvertProgram;
  31900. CheckSource('TestRTTI_Record',
  31901. LinesToStr([ // statements
  31902. 'rtl.recNewT(this, "TFloatRec", function () {',
  31903. ' this.$new = function () {',
  31904. ' var r = Object.create(this);',
  31905. ' r.c = [];',
  31906. ' r.d = [];',
  31907. ' return r;',
  31908. ' };',
  31909. ' this.$eq = function (b) {',
  31910. ' return (this.c === b.c) && (this.d === b.d);',
  31911. ' };',
  31912. ' this.$assign = function (s) {',
  31913. ' this.c = rtl.arrayRef(s.c);',
  31914. ' this.d = rtl.arrayRef(s.d);',
  31915. ' return this;',
  31916. ' };',
  31917. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31918. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31919. ' eltype: rtl.char',
  31920. ' });',
  31921. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31922. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31923. '});',
  31924. 'this.p = null;',
  31925. 'this.r = this.TFloatRec.$new();',
  31926. '']),
  31927. LinesToStr([ // $mod.$main
  31928. '$mod.p = $mod.$rtti["TFloatRec"];',
  31929. '$mod.p = $mod.$rtti["TFloatRec"];',
  31930. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31931. '']));
  31932. end;
  31933. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31934. begin
  31935. WithTypeInfo:=true;
  31936. StartProgram(false);
  31937. Add([
  31938. '{$modeswitch AdvancedRecords}',
  31939. 'type',
  31940. ' TPoint = record',
  31941. ' type TProc = procedure(w: word);',
  31942. ' class var p: TProc;',
  31943. ' end;',
  31944. 'begin',
  31945. '']);
  31946. ConvertProgram;
  31947. CheckSource('TestRTTI_Record_ClassVarType',
  31948. LinesToStr([ // statements
  31949. 'rtl.recNewT(this, "TPoint", function () {',
  31950. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31951. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31952. ' });',
  31953. ' this.p = null;',
  31954. ' this.$eq = function (b) {',
  31955. ' return true;',
  31956. ' };',
  31957. ' this.$assign = function (s) {',
  31958. ' return this;',
  31959. ' };',
  31960. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31961. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31962. '}, true);',
  31963. '']),
  31964. LinesToStr([ // $mod.$main
  31965. '']));
  31966. end;
  31967. procedure TTestModule.TestRTTI_LocalTypes;
  31968. begin
  31969. WithTypeInfo:=true;
  31970. StartProgram(false);
  31971. Add([
  31972. 'procedure DoIt;',
  31973. 'type',
  31974. ' integer = longint;',
  31975. ' TPoint = record',
  31976. ' x,y: integer;',
  31977. ' end;',
  31978. 'var p: TPoint;',
  31979. 'begin',
  31980. 'end;',
  31981. 'begin']);
  31982. ConvertProgram;
  31983. CheckSource('TestRTTI_LocalTypes',
  31984. LinesToStr([ // statements
  31985. 'var TPoint = rtl.recNewT(null, "", function () {',
  31986. ' this.x = 0;',
  31987. ' this.y = 0;',
  31988. ' this.$eq = function (b) {',
  31989. ' return (this.x === b.x) && (this.y === b.y);',
  31990. ' };',
  31991. ' this.$assign = function (s) {',
  31992. ' this.x = s.x;',
  31993. ' this.y = s.y;',
  31994. ' return this;',
  31995. ' };',
  31996. '});',
  31997. 'this.DoIt = function () {',
  31998. ' var p = TPoint.$new();',
  31999. '};',
  32000. '']),
  32001. LinesToStr([ // $mod.$main
  32002. '']));
  32003. end;
  32004. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32005. begin
  32006. WithTypeInfo:=true;
  32007. StartProgram(false);
  32008. Add([
  32009. 'type',
  32010. ' TCaption = string;',
  32011. ' TYesNo = boolean;',
  32012. ' TLetter = char;',
  32013. ' TFloat = double;',
  32014. ' TPtr = pointer;',
  32015. ' TShortInt = shortint;',
  32016. ' TByte = byte;',
  32017. ' TSmallInt = smallint;',
  32018. ' TWord = word;',
  32019. ' TInt32 = longint;',
  32020. ' TDWord = longword;',
  32021. ' TValue = jsvalue;',
  32022. 'var p: TPtr;',
  32023. 'begin',
  32024. ' p:=typeinfo(string);',
  32025. ' p:=typeinfo(tcaption);',
  32026. ' p:=typeinfo(boolean);',
  32027. ' p:=typeinfo(tyesno);',
  32028. ' p:=typeinfo(char);',
  32029. ' p:=typeinfo(tletter);',
  32030. ' p:=typeinfo(double);',
  32031. ' p:=typeinfo(tfloat);',
  32032. ' p:=typeinfo(pointer);',
  32033. ' p:=typeinfo(tptr);',
  32034. ' p:=typeinfo(shortint);',
  32035. ' p:=typeinfo(tshortint);',
  32036. ' p:=typeinfo(byte);',
  32037. ' p:=typeinfo(tbyte);',
  32038. ' p:=typeinfo(smallint);',
  32039. ' p:=typeinfo(tsmallint);',
  32040. ' p:=typeinfo(word);',
  32041. ' p:=typeinfo(tword);',
  32042. ' p:=typeinfo(longword);',
  32043. ' p:=typeinfo(tdword);',
  32044. ' p:=typeinfo(jsvalue);',
  32045. ' p:=typeinfo(tvalue);',
  32046. '']);
  32047. ConvertProgram;
  32048. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32049. LinesToStr([ // statements
  32050. 'this.p = null;',
  32051. '']),
  32052. LinesToStr([ // $mod.$main
  32053. '$mod.p = rtl.string;',
  32054. '$mod.p = rtl.string;',
  32055. '$mod.p = rtl.boolean;',
  32056. '$mod.p = rtl.boolean;',
  32057. '$mod.p = rtl.char;',
  32058. '$mod.p = rtl.char;',
  32059. '$mod.p = rtl.double;',
  32060. '$mod.p = rtl.double;',
  32061. '$mod.p = rtl.pointer;',
  32062. '$mod.p = rtl.pointer;',
  32063. '$mod.p = rtl.shortint;',
  32064. '$mod.p = rtl.shortint;',
  32065. '$mod.p = rtl.byte;',
  32066. '$mod.p = rtl.byte;',
  32067. '$mod.p = rtl.smallint;',
  32068. '$mod.p = rtl.smallint;',
  32069. '$mod.p = rtl.word;',
  32070. '$mod.p = rtl.word;',
  32071. '$mod.p = rtl.longword;',
  32072. '$mod.p = rtl.longword;',
  32073. '$mod.p = rtl.jsvalue;',
  32074. '$mod.p = rtl.jsvalue;',
  32075. '']));
  32076. end;
  32077. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32078. begin
  32079. WithTypeInfo:=true;
  32080. StartProgram(false);
  32081. Add([
  32082. 'type',
  32083. ' TCaption = type string;',
  32084. ' TYesNo = type boolean;',
  32085. ' TLetter = type char;',
  32086. ' TFloat = type double;',
  32087. ' TPtr = type pointer;',
  32088. ' TShortInt = type shortint;',
  32089. ' TByte = type byte;',
  32090. ' TSmallInt = type smallint;',
  32091. ' TWord = type word;',
  32092. ' TInt32 = type longint;',
  32093. ' TDWord = type longword;',
  32094. ' TValue = type jsvalue;',
  32095. ' TAliasValue = type TValue;',
  32096. 'var',
  32097. ' p: TPtr;',
  32098. ' a: TAliasValue;',
  32099. 'begin',
  32100. ' p:=typeinfo(tcaption);',
  32101. ' p:=typeinfo(tyesno);',
  32102. ' p:=typeinfo(tletter);',
  32103. ' p:=typeinfo(tfloat);',
  32104. ' p:=typeinfo(tptr);',
  32105. ' p:=typeinfo(tshortint);',
  32106. ' p:=typeinfo(tbyte);',
  32107. ' p:=typeinfo(tsmallint);',
  32108. ' p:=typeinfo(tword);',
  32109. ' p:=typeinfo(tdword);',
  32110. ' p:=typeinfo(tvalue);',
  32111. ' p:=typeinfo(taliasvalue);',
  32112. ' p:=typeinfo(a);',
  32113. '']);
  32114. ConvertProgram;
  32115. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32116. LinesToStr([ // statements
  32117. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32118. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32119. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32120. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32121. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32122. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32123. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32124. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32125. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32126. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32127. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32128. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32129. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32130. 'this.p = null;',
  32131. 'this.a = undefined;',
  32132. '']),
  32133. LinesToStr([ // $mod.$main
  32134. '$mod.p = $mod.$rtti["TCaption"];',
  32135. '$mod.p = $mod.$rtti["TYesNo"];',
  32136. '$mod.p = $mod.$rtti["TLetter"];',
  32137. '$mod.p = $mod.$rtti["TFloat"];',
  32138. '$mod.p = $mod.$rtti["TPtr"];',
  32139. '$mod.p = $mod.$rtti["TShortInt"];',
  32140. '$mod.p = $mod.$rtti["TByte"];',
  32141. '$mod.p = $mod.$rtti["TSmallInt"];',
  32142. '$mod.p = $mod.$rtti["TWord"];',
  32143. '$mod.p = $mod.$rtti["TDWord"];',
  32144. '$mod.p = $mod.$rtti["TValue"];',
  32145. '$mod.p = $mod.$rtti["TAliasValue"];',
  32146. '$mod.p = $mod.$rtti["TAliasValue"];',
  32147. '']));
  32148. end;
  32149. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32150. begin
  32151. WithTypeInfo:=true;
  32152. StartProgram(false);
  32153. Add('procedure DoIt;');
  32154. Add('type');
  32155. Add(' integer = longint;');
  32156. Add(' TPoint = record');
  32157. Add(' x,y: integer;');
  32158. Add(' end;');
  32159. Add('var p: pointer;');
  32160. Add('begin');
  32161. Add(' p:=typeinfo(tpoint);');
  32162. Add('end;');
  32163. Add('begin');
  32164. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32165. ConvertProgram;
  32166. end;
  32167. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32168. begin
  32169. WithTypeInfo:=true;
  32170. StartProgram(true,[supTypeInfo]);
  32171. Add([
  32172. '{$modeswitch externalclass}',
  32173. 'type',
  32174. ' TFlag = (up,down);',
  32175. ' TFlags = set of TFlag;',
  32176. 'var',
  32177. ' ti: TTypeInfo;',
  32178. ' tiInt: TTypeInfoInteger;',
  32179. ' tiEnum: TTypeInfoEnum;',
  32180. ' tiSet: TTypeInfoSet;',
  32181. 'begin',
  32182. ' ti:=typeinfo(string);',
  32183. ' ti:=typeinfo(boolean);',
  32184. ' ti:=typeinfo(char);',
  32185. ' ti:=typeinfo(double);',
  32186. ' tiInt:=typeinfo(shortint);',
  32187. ' tiInt:=typeinfo(byte);',
  32188. ' tiInt:=typeinfo(smallint);',
  32189. ' tiInt:=typeinfo(word);',
  32190. ' tiInt:=typeinfo(longint);',
  32191. ' tiInt:=typeinfo(longword);',
  32192. ' ti:=typeinfo(jsvalue);',
  32193. ' tiEnum:=typeinfo(tflag);',
  32194. ' tiSet:=typeinfo(tflags);']);
  32195. ConvertProgram;
  32196. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32197. LinesToStr([ // statements
  32198. 'this.TFlag = {',
  32199. ' "0": "up",',
  32200. ' up: 0,',
  32201. ' "1": "down",',
  32202. ' down: 1',
  32203. '};',
  32204. 'this.$rtti.$Enum("TFlag", {',
  32205. ' minvalue: 0,',
  32206. ' maxvalue: 1,',
  32207. ' ordtype: 1,',
  32208. ' enumtype: this.TFlag',
  32209. '});',
  32210. 'this.$rtti.$Set("TFlags", {',
  32211. ' comptype: this.$rtti["TFlag"]',
  32212. '});',
  32213. 'this.ti = null;',
  32214. 'this.tiInt = null;',
  32215. 'this.tiEnum = null;',
  32216. 'this.tiSet = null;',
  32217. '']),
  32218. LinesToStr([ // $mod.$main
  32219. '$mod.ti = rtl.string;',
  32220. '$mod.ti = rtl.boolean;',
  32221. '$mod.ti = rtl.char;',
  32222. '$mod.ti = rtl.double;',
  32223. '$mod.tiInt = rtl.shortint;',
  32224. '$mod.tiInt = rtl.byte;',
  32225. '$mod.tiInt = rtl.smallint;',
  32226. '$mod.tiInt = rtl.word;',
  32227. '$mod.tiInt = rtl.longint;',
  32228. '$mod.tiInt = rtl.longword;',
  32229. '$mod.ti = rtl.jsvalue;',
  32230. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32231. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32232. '']));
  32233. end;
  32234. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32235. begin
  32236. WithTypeInfo:=true;
  32237. StartProgram(true,[supTypeInfo]);
  32238. Add('{$modeswitch externalclass}');
  32239. Add('type');
  32240. Add(' TStaticArr = array[boolean] of string;');
  32241. Add(' TDynArr = array of string;');
  32242. Add(' TProc = procedure;');
  32243. Add(' TMethod = procedure of object;');
  32244. Add('var');
  32245. Add(' StaticArray: TStaticArr;');
  32246. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32247. Add(' DynArray: TDynArr;');
  32248. Add(' tiDynArray: TTypeInfoDynArray;');
  32249. Add(' ProcVar: TProc;');
  32250. Add(' tiProcVar: TTypeInfoProcVar;');
  32251. Add(' MethodVar: TMethod;');
  32252. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32253. Add('begin');
  32254. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32255. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32256. Add(' tiDynArray:=typeinfo(DynArray);');
  32257. Add(' tiDynArray:=typeinfo(TDynArr);');
  32258. Add(' tiProcVar:=typeinfo(ProcVar);');
  32259. Add(' tiProcVar:=typeinfo(TProc);');
  32260. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32261. Add(' tiMethodVar:=typeinfo(TMethod);');
  32262. ConvertProgram;
  32263. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32264. LinesToStr([ // statements
  32265. 'this.$rtti.$StaticArray("TStaticArr", {',
  32266. ' dims: [2],',
  32267. ' eltype: rtl.string',
  32268. '});',
  32269. 'this.$rtti.$DynArray("TDynArr", {',
  32270. ' eltype: rtl.string',
  32271. '});',
  32272. 'this.$rtti.$ProcVar("TProc", {',
  32273. ' procsig: rtl.newTIProcSig([])',
  32274. '});',
  32275. 'this.$rtti.$MethodVar("TMethod", {',
  32276. ' procsig: rtl.newTIProcSig([]),',
  32277. ' methodkind: 0',
  32278. '});',
  32279. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32280. 'this.tiStaticArray = null;',
  32281. 'this.DynArray = [];',
  32282. 'this.tiDynArray = null;',
  32283. 'this.ProcVar = null;',
  32284. 'this.tiProcVar = null;',
  32285. 'this.MethodVar = null;',
  32286. 'this.tiMethodVar = null;',
  32287. '']),
  32288. LinesToStr([ // $mod.$main
  32289. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32290. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32291. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32292. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32293. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32294. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32295. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32296. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32297. '']));
  32298. end;
  32299. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32300. begin
  32301. WithTypeInfo:=true;
  32302. StartProgram(true,[supTypeInfo]);
  32303. Add('{$modeswitch externalclass}');
  32304. Add('type');
  32305. Add(' TRec = record end;');
  32306. // ToDo: ^TRec
  32307. Add(' TObject = class end;');
  32308. Add(' TClass = class of tobject;');
  32309. Add('var');
  32310. Add(' Rec: trec;');
  32311. Add(' tiRecord: ttypeinforecord;');
  32312. Add(' Obj: tobject;');
  32313. Add(' tiClass: ttypeinfoclass;');
  32314. Add(' aClass: tclass;');
  32315. Add(' tiClassRef: ttypeinfoclassref;');
  32316. // ToDo: ^TRec
  32317. Add(' tiPointer: ttypeinfopointer;');
  32318. Add('begin');
  32319. Add(' tirecord:=typeinfo(trec);');
  32320. Add(' tirecord:=typeinfo(trec);');
  32321. Add(' ticlass:=typeinfo(obj);');
  32322. Add(' ticlass:=typeinfo(tobject);');
  32323. Add(' ticlass:=typeinfo(aclass);');
  32324. Add(' ticlassref:=typeinfo(tclass);');
  32325. ConvertProgram;
  32326. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32327. LinesToStr([ // statements
  32328. 'rtl.recNewT(this, "TRec", function () {',
  32329. ' this.$eq = function (b) {',
  32330. ' return true;',
  32331. ' };',
  32332. ' this.$assign = function (s) {',
  32333. ' return this;',
  32334. ' };',
  32335. ' $mod.$rtti.$Record("TRec", {});',
  32336. '});',
  32337. 'rtl.createClass(this, "TObject", null, function () {',
  32338. ' this.$init = function () {',
  32339. ' };',
  32340. ' this.$final = function () {',
  32341. ' };',
  32342. '});',
  32343. 'this.$rtti.$ClassRef("TClass", {',
  32344. ' instancetype: this.$rtti["TObject"]',
  32345. '});',
  32346. 'this.Rec = this.TRec.$new();',
  32347. 'this.tiRecord = null;',
  32348. 'this.Obj = null;',
  32349. 'this.tiClass = null;',
  32350. 'this.aClass = null;',
  32351. 'this.tiClassRef = null;',
  32352. 'this.tiPointer = null;',
  32353. '']),
  32354. LinesToStr([ // $mod.$main
  32355. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32356. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32357. '$mod.tiClass = $mod.Obj.$rtti;',
  32358. '$mod.tiClass = $mod.$rtti["TObject"];',
  32359. '$mod.tiClass = $mod.aClass.$rtti;',
  32360. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32361. '']));
  32362. end;
  32363. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32364. begin
  32365. WithTypeInfo:=true;
  32366. StartProgram(true,[supTypeInfo]);
  32367. Add([
  32368. '{$modeswitch externalclass}',
  32369. 'type',
  32370. ' TClass = class of tobject;',
  32371. ' TObject = class',
  32372. ' function MyClass: TClass;',
  32373. ' class function ClassType: TClass;',
  32374. ' end;',
  32375. 'function TObject.MyClass: TClass;',
  32376. 'var t: TTypeInfoClass;',
  32377. 'begin',
  32378. ' t:=TypeInfo(Self);',
  32379. ' t:=TypeInfo(Result);',
  32380. ' t:=TypeInfo(TObject);',
  32381. 'end;',
  32382. 'class function TObject.ClassType: TClass;',
  32383. 'var t: TTypeInfoClass;',
  32384. 'begin',
  32385. ' t:=TypeInfo(Self);',
  32386. ' t:=TypeInfo(Result);',
  32387. 'end;',
  32388. 'var',
  32389. ' Obj: TObject;',
  32390. ' t: TTypeInfoClass;',
  32391. 'begin',
  32392. ' t:=TypeInfo(TObject.ClassType);',
  32393. ' t:=TypeInfo(Obj.ClassType);',
  32394. ' t:=TypeInfo(Obj.MyClass);',
  32395. '']);
  32396. ConvertProgram;
  32397. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32398. LinesToStr([ // statements
  32399. 'this.$rtti.$Class("TObject");',
  32400. 'this.$rtti.$ClassRef("TClass", {',
  32401. ' instancetype: this.$rtti["TObject"]',
  32402. '});',
  32403. 'rtl.createClass(this, "TObject", null, function () {',
  32404. ' this.$init = function () {',
  32405. ' };',
  32406. ' this.$final = function () {',
  32407. ' };',
  32408. ' this.MyClass = function () {',
  32409. ' var Result = null;',
  32410. ' var t = null;',
  32411. ' t = this.$rtti;',
  32412. ' t = Result.$rtti;',
  32413. ' t = $mod.$rtti["TObject"];',
  32414. ' return Result;',
  32415. ' };',
  32416. ' this.ClassType = function () {',
  32417. ' var Result = null;',
  32418. ' var t = null;',
  32419. ' t = this.$rtti;',
  32420. ' t = Result.$rtti;',
  32421. ' return Result;',
  32422. ' };',
  32423. '});',
  32424. 'this.Obj = null;',
  32425. 'this.t = null;',
  32426. '']),
  32427. LinesToStr([ // $mod.$main
  32428. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32429. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32430. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32431. '']));
  32432. end;
  32433. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32434. begin
  32435. WithTypeInfo:=true;
  32436. AddModuleWithIntfImplSrc('typinfo.pas',
  32437. LinesToStr([
  32438. '{$modeswitch externalclass}',
  32439. 'type',
  32440. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32441. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32442. '']),
  32443. '');
  32444. AddModuleWithIntfImplSrc('unit2.pas',
  32445. LinesToStr([
  32446. 'uses typinfo;',
  32447. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32448. 'procedure DoPtr(p: PTypeInfo);',
  32449. 'procedure DoInfo(t: TTypeInfo);',
  32450. 'procedure DoInt(t: TTypeInfoInteger);',
  32451. '']),
  32452. LinesToStr([
  32453. 'procedure DoPtr(p: PTypeInfo);',
  32454. 'begin end;',
  32455. 'procedure DoInfo(t: TTypeInfo);',
  32456. 'begin end;',
  32457. 'procedure DoInt(t: TTypeInfoInteger);',
  32458. 'begin end;',
  32459. '']));
  32460. StartUnit(true);
  32461. Add([
  32462. 'interface',
  32463. 'uses unit2;', // does not use unit typinfo
  32464. 'implementation',
  32465. 'var',
  32466. ' i: byte;',
  32467. ' p: pointer;',
  32468. ' t: PTypeInfo;',
  32469. 'initialization',
  32470. ' p:=typeinfo(i);',
  32471. ' t:=typeinfo(i);',
  32472. ' if p=t then ;',
  32473. ' if p=typeinfo(i) then ;',
  32474. ' if typeinfo(i)=p then ;',
  32475. ' if t=typeinfo(i) then ;',
  32476. ' if typeinfo(i)=t then ;',
  32477. ' DoPtr(p);',
  32478. ' DoPtr(t);',
  32479. ' DoPtr(typeinfo(i));',
  32480. ' DoInfo(p);',
  32481. ' DoInfo(t);',
  32482. ' DoInfo(typeinfo(i));',
  32483. ' DoInt(typeinfo(i));',
  32484. '']);
  32485. ConvertUnit;
  32486. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32487. LinesToStr([ // statements
  32488. 'var $impl = $mod.$impl;',
  32489. '']),
  32490. LinesToStr([ // this.$init
  32491. '$impl.p = rtl.byte;',
  32492. '$impl.t = rtl.byte;',
  32493. 'if ($impl.p === $impl.t) ;',
  32494. 'if ($impl.p === rtl.byte) ;',
  32495. 'if (rtl.byte === $impl.p) ;',
  32496. 'if ($impl.t === rtl.byte) ;',
  32497. 'if (rtl.byte === $impl.t) ;',
  32498. 'pas.unit2.DoPtr($impl.p);',
  32499. 'pas.unit2.DoPtr($impl.t);',
  32500. 'pas.unit2.DoPtr(rtl.byte);',
  32501. 'pas.unit2.DoInfo($impl.p);',
  32502. 'pas.unit2.DoInfo($impl.t);',
  32503. 'pas.unit2.DoInfo(rtl.byte);',
  32504. 'pas.unit2.DoInt(rtl.byte);',
  32505. '']),
  32506. LinesToStr([ // implementation
  32507. '$impl.i = 0;',
  32508. '$impl.p = null;',
  32509. '$impl.t = null;',
  32510. '']) );
  32511. end;
  32512. procedure TTestModule.TestRTTI_Interface_Corba;
  32513. begin
  32514. WithTypeInfo:=true;
  32515. StartProgram(true,[supTypeInfo]);
  32516. Add([
  32517. '{$interfaces corba}',
  32518. '{$modeswitch externalclass}',
  32519. 'type',
  32520. ' IUnknown = interface',
  32521. ' end;',
  32522. ' IBird = interface',
  32523. ' function GetItem: longint;',
  32524. ' procedure SetItem(Value: longint);',
  32525. ' property Item: longint read GetItem write SetItem;',
  32526. ' end;',
  32527. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32528. 'var',
  32529. ' i: IBird;',
  32530. ' t: TTypeInfoInterface;',
  32531. 'begin',
  32532. ' t:=TypeInfo(IBird);',
  32533. ' t:=TypeInfo(i);',
  32534. ' DoIt(t);',
  32535. ' DoIt(TypeInfo(IBird));',
  32536. '']);
  32537. ConvertProgram;
  32538. CheckSource('TestRTTI_Interface_Corba',
  32539. LinesToStr([ // statements
  32540. 'rtl.createInterface(',
  32541. ' this,',
  32542. ' "IUnknown",',
  32543. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32544. ' [],',
  32545. ' null,',
  32546. ' function () {',
  32547. ' }',
  32548. ');',
  32549. 'rtl.createInterface(',
  32550. ' this,',
  32551. ' "IBird",',
  32552. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32553. ' ["GetItem", "SetItem"],',
  32554. ' null,',
  32555. ' function () {',
  32556. ' var $r = this.$rtti;',
  32557. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32558. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32559. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32560. ' }',
  32561. ');',
  32562. 'this.DoIt = function (t) {',
  32563. '}; ',
  32564. 'this.i = null;',
  32565. 'this.t = null;',
  32566. '']),
  32567. LinesToStr([ // $mod.$main
  32568. '$mod.t = $mod.$rtti["IBird"];',
  32569. '$mod.t = $mod.i.$rtti;',
  32570. '$mod.DoIt($mod.t);',
  32571. '$mod.DoIt($mod.$rtti["IBird"]);',
  32572. '']));
  32573. end;
  32574. procedure TTestModule.TestRTTI_Interface_COM;
  32575. begin
  32576. WithTypeInfo:=true;
  32577. StartProgram(true,[supTypeInfo]);
  32578. Add([
  32579. '{$interfaces com}',
  32580. '{$modeswitch externalclass}',
  32581. 'type',
  32582. ' TGuid = record end;',
  32583. ' integer = longint;',
  32584. ' IUnknown = interface',
  32585. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32586. ' function _AddRef: Integer;',
  32587. ' function _Release: Integer;',
  32588. ' end;',
  32589. ' IBird = interface',
  32590. ' function GetItem: longint;',
  32591. ' procedure SetItem(Value: longint);',
  32592. ' property Item: longint read GetItem write SetItem;',
  32593. ' end;',
  32594. 'var',
  32595. ' i: IBird;',
  32596. ' t: TTypeInfoInterface;',
  32597. 'begin',
  32598. ' t:=TypeInfo(IBird);',
  32599. ' t:=TypeInfo(i);',
  32600. '']);
  32601. ConvertProgram;
  32602. CheckSource('TestRTTI_Interface_COM',
  32603. LinesToStr([ // statements
  32604. 'rtl.recNewT(this, "TGuid", function () {',
  32605. ' this.$eq = function (b) {',
  32606. ' return true;',
  32607. ' };',
  32608. ' this.$assign = function (s) {',
  32609. ' return this;',
  32610. ' };',
  32611. ' $mod.$rtti.$Record("TGuid", {});',
  32612. '});',
  32613. 'rtl.createInterface(',
  32614. ' this,',
  32615. ' "IUnknown",',
  32616. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32617. ' ["QueryInterface", "_AddRef", "_Release"],',
  32618. ' null,',
  32619. ' function () {',
  32620. ' this.$kind = "com";',
  32621. ' var $r = this.$rtti;',
  32622. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32623. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32624. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32625. ' }',
  32626. ');',
  32627. 'rtl.createInterface(',
  32628. ' this,',
  32629. ' "IBird",',
  32630. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32631. ' ["GetItem", "SetItem"],',
  32632. ' this.IUnknown,',
  32633. ' function () {',
  32634. ' var $r = this.$rtti;',
  32635. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32636. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32637. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32638. ' }',
  32639. ');',
  32640. 'this.i = null;',
  32641. 'this.t = null;',
  32642. '']),
  32643. LinesToStr([ // $mod.$main
  32644. '$mod.t = $mod.$rtti["IBird"];',
  32645. '$mod.t = $mod.i.$rtti;',
  32646. '']));
  32647. end;
  32648. procedure TTestModule.TestRTTI_ClassHelper;
  32649. begin
  32650. WithTypeInfo:=true;
  32651. StartProgram(true,[supTypeInfo]);
  32652. Add([
  32653. '{$interfaces com}',
  32654. '{$modeswitch externalclass}',
  32655. 'type',
  32656. ' TObject = class',
  32657. ' end;',
  32658. ' THelper = class helper for TObject',
  32659. ' published',
  32660. ' function GetItem: longint;',
  32661. ' property Item: longint read GetItem;',
  32662. ' end;',
  32663. 'function THelper.GetItem: longint;',
  32664. 'begin',
  32665. 'end;',
  32666. 'var',
  32667. ' t: TTypeInfoHelper;',
  32668. 'begin',
  32669. ' t:=TypeInfo(THelper);',
  32670. '']);
  32671. ConvertProgram;
  32672. CheckSource('TestRTTI_ClassHelper',
  32673. LinesToStr([ // statements
  32674. 'rtl.createClass(this, "TObject", null, function () {',
  32675. ' this.$init = function () {',
  32676. ' };',
  32677. ' this.$final = function () {',
  32678. ' };',
  32679. '});',
  32680. 'rtl.createHelper(this, "THelper", null, function () {',
  32681. ' this.GetItem = function () {',
  32682. ' var Result = 0;',
  32683. ' return Result;',
  32684. ' };',
  32685. ' var $r = this.$rtti;',
  32686. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32687. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32688. '});',
  32689. 'this.t = null;',
  32690. '']),
  32691. LinesToStr([ // $mod.$main
  32692. '$mod.t = $mod.$rtti["THelper"];',
  32693. '']));
  32694. end;
  32695. procedure TTestModule.TestRTTI_ExternalClass;
  32696. begin
  32697. WithTypeInfo:=true;
  32698. StartProgram(true,[supTypeInfo]);
  32699. Add([
  32700. '{$modeswitch externalclass}',
  32701. 'type',
  32702. ' TJSObject = class external name ''Object''',
  32703. ' end;',
  32704. ' TJSArray = class external name ''Array'' (TJSObject)',
  32705. ' end;',
  32706. 'var',
  32707. ' p: Pointer;',
  32708. ' tc: TTypeInfoExtClass;',
  32709. 'begin',
  32710. ' p:=typeinfo(TJSArray);']);
  32711. ConvertProgram;
  32712. CheckSource('TestRTTI_ExternalClass',
  32713. LinesToStr([ // statements
  32714. 'this.$rtti.$ExtClass("TJSObject", {',
  32715. ' jsclass: "Object"',
  32716. '});',
  32717. 'this.$rtti.$ExtClass("TJSArray", {',
  32718. ' ancestor: this.$rtti["TJSObject"],',
  32719. ' jsclass: "Array"',
  32720. '});',
  32721. 'this.p = null;',
  32722. 'this.tc = null;',
  32723. '']),
  32724. LinesToStr([ // $mod.$main
  32725. '$mod.p = $mod.$rtti["TJSArray"];',
  32726. '']));
  32727. end;
  32728. procedure TTestModule.TestRTTI_Unit;
  32729. begin
  32730. WithTypeInfo:=true;
  32731. AddModuleWithIntfImplSrc('unit2.pas',
  32732. LinesToStr([
  32733. '{$mode delphi}',
  32734. 'type',
  32735. ' TWordArray = array of word;',
  32736. ' TArray<T> = array of T;',
  32737. '']),
  32738. '');
  32739. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32740. Add([
  32741. '{$mode delphi}',
  32742. 'interface',
  32743. 'uses unit2;',
  32744. 'type',
  32745. ' IBird = interface',
  32746. ' function Swoop: TWordArray;',
  32747. ' function Glide: TArray<word>;',
  32748. ' end;',
  32749. 'procedure Fly;',
  32750. 'implementation',
  32751. 'procedure Fly;',
  32752. 'var',
  32753. ' ta: tTypeInfoDynArray;',
  32754. ' ti: tTypeInfoInterface;',
  32755. 'begin',
  32756. ' ta:=typeinfo(TWordArray);',
  32757. ' ta:=typeinfo(TArray<word>);',
  32758. ' ti:=typeinfo(IBird);',
  32759. 'end;',
  32760. '']);
  32761. ConvertUnit;
  32762. CheckSource('TestRTTI_ExternalClass',
  32763. LinesToStr([ // statements
  32764. 'rtl.createInterface(',
  32765. ' this,',
  32766. ' "IBird",',
  32767. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32768. ' ["Swoop", "Glide"],',
  32769. ' pas.system.IUnknown,',
  32770. ' function () {',
  32771. ' var $r = this.$rtti;',
  32772. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32773. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32774. ' }',
  32775. ');',
  32776. 'this.Fly = function () {',
  32777. ' var ta = null;',
  32778. ' var ti = null;',
  32779. ' ta = pas.unit2.$rtti["TWordArray"];',
  32780. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32781. ' ti = $mod.$rtti["IBird"];',
  32782. '};',
  32783. '']),
  32784. LinesToStr([ // $mod.$main
  32785. '']));
  32786. end;
  32787. procedure TTestModule.TestResourcestringProgram;
  32788. begin
  32789. AddModuleWithIntfImplSrc('unit2.pas',
  32790. LinesToStr([
  32791. 'resourcestring Title = ''Nice'';',
  32792. '']),
  32793. '');
  32794. StartProgram(true);
  32795. Add([
  32796. 'uses unit2;',
  32797. 'const Bar = ''bar'';',
  32798. 'resourcestring',
  32799. ' Red = ''red'';',
  32800. ' Foobar = ''fOo''+bar;',
  32801. 'var s: string;',
  32802. ' c: char;',
  32803. 'begin',
  32804. ' s:=red;',
  32805. ' s:=test1.red;',
  32806. ' s:=Title;',
  32807. ' c:=red[1];',
  32808. ' c:=test1.red[2];',
  32809. ' if red=foobar then ;',
  32810. ' if red[3]=red[4] then ;']);
  32811. ConvertProgram;
  32812. CheckSource('TestResourcestringProgram',
  32813. LinesToStr([ // statements
  32814. 'this.Bar = "bar";',
  32815. 'this.s = "";',
  32816. 'this.c = "\x00";',
  32817. '$mod.$resourcestrings = {',
  32818. ' Red: {',
  32819. ' org: "red"',
  32820. ' },',
  32821. ' Foobar: {',
  32822. ' org: "fOobar"',
  32823. ' }',
  32824. '};',
  32825. '']),
  32826. LinesToStr([ // $mod.$main
  32827. '$mod.s = rtl.getResStr($mod, "Red");',
  32828. '$mod.s = rtl.getResStr($mod, "Red");',
  32829. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32830. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32831. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32832. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32833. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32834. '']));
  32835. end;
  32836. procedure TTestModule.TestResourcestringUnit;
  32837. begin
  32838. AddModuleWithIntfImplSrc('unit2.pas',
  32839. LinesToStr([
  32840. 'resourcestring Title = ''Nice'';',
  32841. '']),
  32842. '');
  32843. StartUnit(true);
  32844. Add([
  32845. 'interface',
  32846. 'uses unit2;',
  32847. 'const Red = ''rEd'';',
  32848. 'resourcestring',
  32849. ' Blue = ''blue'';',
  32850. ' NotRed = ''not''+Red;',
  32851. 'var s: string;',
  32852. 'implementation',
  32853. 'resourcestring',
  32854. ' ImplGreen = ''green'';',
  32855. 'initialization',
  32856. ' s:=blue+ImplGreen;',
  32857. ' s:=test1.blue+test1.implgreen;',
  32858. ' s:=blue[1]+implgreen[2];',
  32859. ' s:=Title;',
  32860. '']);
  32861. ConvertUnit;
  32862. CheckSource('TestResourcestringUnit',
  32863. LinesToStr([ // statements
  32864. 'this.Red = "rEd";',
  32865. 'this.s = "";',
  32866. '$mod.$resourcestrings = {',
  32867. ' Blue: {',
  32868. ' org: "blue"',
  32869. ' },',
  32870. ' NotRed: {',
  32871. ' org: "notrEd"',
  32872. ' },',
  32873. ' ImplGreen: {',
  32874. ' org: "green"',
  32875. ' }',
  32876. '};',
  32877. '']),
  32878. LinesToStr([ // $mod.$main
  32879. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32880. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32881. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32882. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32883. '']));
  32884. end;
  32885. procedure TTestModule.TestResourcestringImplementation;
  32886. begin
  32887. StartUnit(false);
  32888. Add([
  32889. 'interface',
  32890. 'implementation',
  32891. 'resourcestring',
  32892. ' ImplRed = ''red'';']);
  32893. ConvertUnit;
  32894. CheckSource('TestResourcestringImplementation',
  32895. LinesToStr([ // intf statements
  32896. 'var $impl = $mod.$impl;']),
  32897. LinesToStr([ // $mod.$init
  32898. '']),
  32899. LinesToStr([ // impl statements
  32900. '$mod.$resourcestrings = {',
  32901. ' ImplRed: {',
  32902. ' org: "red"',
  32903. ' }',
  32904. '};',
  32905. '']));
  32906. end;
  32907. procedure TTestModule.TestAttributes_Members;
  32908. begin
  32909. WithTypeInfo:=true;
  32910. StartProgram(false);
  32911. Add([
  32912. '{$modeswitch PrefixedAttributes}',
  32913. 'type',
  32914. ' TObject = class',
  32915. ' constructor Create;',
  32916. ' end;',
  32917. ' TCustomAttribute = class',
  32918. ' constructor Create(Id: word);',
  32919. ' end;',
  32920. ' [Missing]',
  32921. ' TBird = class',
  32922. ' published',
  32923. ' [Tcustom]',
  32924. ' FField: word;',
  32925. ' [tcustom(14)]',
  32926. ' property Size: word read FField;',
  32927. ' [Tcustom(15)]',
  32928. ' procedure Fly; virtual; abstract;',
  32929. ' end;',
  32930. ' TRec = record',
  32931. ' [Tcustom,tcustom(14)]',
  32932. ' Size: word;',
  32933. ' end;',
  32934. 'constructor TObject.Create; begin end;',
  32935. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32936. 'begin',
  32937. '']);
  32938. ConvertProgram;
  32939. CheckSource('TestAttributes_Members',
  32940. LinesToStr([ // statements
  32941. 'rtl.createClass(this, "TObject", null, function () {',
  32942. ' this.$init = function () {',
  32943. ' };',
  32944. ' this.$final = function () {',
  32945. ' };',
  32946. ' this.Create = function () {',
  32947. ' return this;',
  32948. ' };',
  32949. '});',
  32950. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32951. ' this.Create$1 = function (Id) {',
  32952. ' return this;',
  32953. ' };',
  32954. '});',
  32955. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32956. ' this.$init = function () {',
  32957. ' $mod.TObject.$init.call(this);',
  32958. ' this.FField = 0;',
  32959. ' };',
  32960. ' var $r = this.$rtti;',
  32961. ' $r.addField("FField", rtl.word, {',
  32962. ' attr: [$mod.TCustomAttribute, "Create"]',
  32963. ' });',
  32964. ' $r.addProperty(',
  32965. ' "Size",',
  32966. ' 0,',
  32967. ' rtl.word,',
  32968. ' "FField",',
  32969. ' "",',
  32970. ' {',
  32971. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32972. ' }',
  32973. ' );',
  32974. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32975. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32976. ' });',
  32977. '});',
  32978. 'rtl.recNewT(this, "TRec", function () {',
  32979. ' this.Size = 0;',
  32980. ' this.$eq = function (b) {',
  32981. ' return this.Size === b.Size;',
  32982. ' };',
  32983. ' this.$assign = function (s) {',
  32984. ' this.Size = s.Size;',
  32985. ' return this;',
  32986. ' };',
  32987. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32988. ' $r.addField("Size", rtl.word, {',
  32989. ' attr: [',
  32990. ' $mod.TCustomAttribute,',
  32991. ' "Create",',
  32992. ' $mod.TCustomAttribute,',
  32993. ' "Create$1",',
  32994. ' [14]',
  32995. ' ]',
  32996. ' });',
  32997. '});',
  32998. '']),
  32999. LinesToStr([ // $mod.$main
  33000. '']));
  33001. end;
  33002. procedure TTestModule.TestAttributes_Types;
  33003. begin
  33004. WithTypeInfo:=true;
  33005. StartProgram(false);
  33006. Add([
  33007. '{$modeswitch PrefixedAttributes}',
  33008. 'type',
  33009. ' TObject = class',
  33010. ' constructor Create(Id: word);',
  33011. ' end;',
  33012. ' TCustomAttribute = class',
  33013. ' end;',
  33014. ' [TCustom(1)]',
  33015. ' TMyClass = class',
  33016. ' end;',
  33017. ' [TCustom(11)]',
  33018. ' TMyDescendant = class(TMyClass)',
  33019. ' end;',
  33020. ' [TCustom(2)]',
  33021. ' TRec = record',
  33022. ' end;',
  33023. ' [TCustom(3)]',
  33024. ' TInt = type word;',
  33025. 'constructor TObject.Create(Id: word);',
  33026. 'begin',
  33027. 'end;',
  33028. 'var p: pointer;',
  33029. 'begin',
  33030. ' p:=typeinfo(TMyClass);',
  33031. ' p:=typeinfo(TRec);',
  33032. ' p:=typeinfo(TInt);',
  33033. '']);
  33034. ConvertProgram;
  33035. CheckSource('TestAttributes_Types',
  33036. LinesToStr([ // statements
  33037. 'rtl.createClass(this, "TObject", null, function () {',
  33038. ' this.$init = function () {',
  33039. ' };',
  33040. ' this.$final = function () {',
  33041. ' };',
  33042. ' this.Create = function (Id) {',
  33043. ' return this;',
  33044. ' };',
  33045. '});',
  33046. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33047. '});',
  33048. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33049. ' var $r = this.$rtti;',
  33050. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33051. '});',
  33052. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33053. ' var $r = this.$rtti;',
  33054. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33055. '});',
  33056. 'rtl.recNewT(this, "TRec", function () {',
  33057. ' this.$eq = function (b) {',
  33058. ' return true;',
  33059. ' };',
  33060. ' this.$assign = function (s) {',
  33061. ' return this;',
  33062. ' };',
  33063. ' $mod.$rtti.$Record("TRec", {',
  33064. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33065. ' });',
  33066. '});',
  33067. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33068. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33069. '});',
  33070. 'this.p = null;',
  33071. '']),
  33072. LinesToStr([ // $mod.$main
  33073. '$mod.p = $mod.$rtti["TMyClass"];',
  33074. '$mod.p = $mod.$rtti["TRec"];',
  33075. '$mod.p = $mod.$rtti["TInt"];',
  33076. '']));
  33077. end;
  33078. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33079. begin
  33080. WithTypeInfo:=true;
  33081. StartProgram(false);
  33082. Add([
  33083. '{$modeswitch PrefixedAttributes}',
  33084. 'type',
  33085. ' TObject = class',
  33086. ' constructor Create;',
  33087. ' end;',
  33088. ' TCustomAttribute = class',
  33089. ' end;',
  33090. ' THelper = class helper for TCustomAttribute',
  33091. ' constructor Create(Id: word);',
  33092. ' end;',
  33093. ' [TCustom(3)]',
  33094. ' TMyInt = word;',
  33095. 'constructor TObject.Create; begin end;',
  33096. 'constructor THelper.Create(Id: word); begin end;',
  33097. 'begin',
  33098. ' if typeinfo(TMyInt)=nil then ;']);
  33099. ConvertProgram;
  33100. end;
  33101. procedure TTestModule.TestAttributes_InterfacesList;
  33102. begin
  33103. WithTypeInfo:=true;
  33104. StartProgram(false);
  33105. Add([
  33106. '{$mode Delphi}',
  33107. 'type',
  33108. ' TObject = class',
  33109. ' constructor Create;',
  33110. ' end;',
  33111. ' IInterface = interface end;',
  33112. ' TCustomAttribute = class',
  33113. ' end;',
  33114. ' Red = class(TCustomAttribute);',
  33115. ' Blue = class(TCustomAttribute);',
  33116. ' [Red]',
  33117. ' IBird<T> = interface',
  33118. ' procedure Fly;',
  33119. ' end;',
  33120. ' [Blue]',
  33121. ' IEagle = interface(IBird<Word>)',
  33122. ' procedure Dive;',
  33123. ' end;',
  33124. ' TAnt = class(TObject, IEagle)',
  33125. ' procedure Fly; virtual; abstract;',
  33126. ' procedure Dive; virtual; abstract;',
  33127. ' end;',
  33128. 'constructor TObject.Create;',
  33129. 'begin',
  33130. 'end;',
  33131. 'begin',
  33132. '']);
  33133. ConvertProgram;
  33134. CheckSource('TestAttributes_InterfacesList',
  33135. LinesToStr([ // statements
  33136. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33137. 'rtl.createClass(this, "TObject", null, function () {',
  33138. ' this.$init = function () {',
  33139. ' };',
  33140. ' this.$final = function () {',
  33141. ' };',
  33142. ' this.Create = function () {',
  33143. ' return this;',
  33144. ' };',
  33145. '});',
  33146. 'rtl.createInterface(',
  33147. ' this,',
  33148. ' "IInterface",',
  33149. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33150. ' [],',
  33151. ' null,',
  33152. ' function () {',
  33153. ' this.$kind = "com";',
  33154. ' }',
  33155. ');',
  33156. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33157. '});',
  33158. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33159. '});',
  33160. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33161. '});',
  33162. 'rtl.createInterface(',
  33163. ' this,',
  33164. ' "IBird$G1",',
  33165. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33166. ' ["Fly"],',
  33167. ' this.IInterface,',
  33168. ' function () {',
  33169. ' var $r = this.$rtti;',
  33170. ' $r.addMethod("Fly", 0, []);',
  33171. ' $r.attr = [$mod.Red, "Create"];',
  33172. ' },',
  33173. ' "IBird<System.Word>"',
  33174. ');',
  33175. 'rtl.createInterface(',
  33176. ' this,',
  33177. ' "IEagle",',
  33178. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33179. ' ["Dive"],',
  33180. ' this.IBird$G1,',
  33181. ' function () {',
  33182. ' var $r = this.$rtti;',
  33183. ' $r.addMethod("Dive", 0, []);',
  33184. ' $r.attr = [$mod.Blue, "Create"];',
  33185. ' }',
  33186. ');',
  33187. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33188. ' rtl.addIntf(this, $mod.IEagle);',
  33189. '});',
  33190. '']),
  33191. LinesToStr([ // $mod.$main
  33192. '']));
  33193. end;
  33194. procedure TTestModule.TestAssert;
  33195. begin
  33196. StartProgram(false);
  33197. Add([
  33198. 'procedure DoIt;',
  33199. 'var',
  33200. ' b: boolean;',
  33201. ' s: string;',
  33202. 'begin',
  33203. ' {$Assertions on}',
  33204. ' Assert(b);',
  33205. 'end;',
  33206. 'begin',
  33207. ' DoIt;',
  33208. '']);
  33209. ConvertProgram;
  33210. CheckSource('TestAssert',
  33211. LinesToStr([ // statements
  33212. 'this.DoIt = function () {',
  33213. ' var b = false;',
  33214. ' var s = "";',
  33215. ' if (!b) throw "assert failed";',
  33216. '};',
  33217. '']),
  33218. LinesToStr([ // $mod.$main
  33219. '$mod.DoIt();',
  33220. '']));
  33221. end;
  33222. procedure TTestModule.TestAssert_SysUtils;
  33223. begin
  33224. AddModuleWithIntfImplSrc('SysUtils.pas',
  33225. LinesToStr([
  33226. 'type',
  33227. ' TObject = class',
  33228. ' constructor Create;',
  33229. ' end;',
  33230. ' EAssertionFailed = class',
  33231. ' constructor Create(s: string);',
  33232. ' end;',
  33233. '']),
  33234. LinesToStr([
  33235. 'constructor TObject.Create;',
  33236. 'begin end;',
  33237. 'constructor EAssertionFailed.Create(s: string);',
  33238. 'begin end;',
  33239. '']) );
  33240. StartProgram(true);
  33241. Add([
  33242. 'uses sysutils;',
  33243. 'procedure DoIt;',
  33244. 'var',
  33245. ' b: boolean;',
  33246. ' s: string;',
  33247. 'begin',
  33248. ' {$Assertions on}',
  33249. ' Assert(b);',
  33250. ' Assert(b,''msg'');',
  33251. 'end;',
  33252. 'begin',
  33253. ' DoIt;',
  33254. '']);
  33255. ConvertProgram;
  33256. CheckSource('TestAssert_SysUtils',
  33257. LinesToStr([ // statements
  33258. 'this.DoIt = function () {',
  33259. ' var b = false;',
  33260. ' var s = "";',
  33261. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33262. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33263. '};',
  33264. '']),
  33265. LinesToStr([ // $mod.$main
  33266. '$mod.DoIt();',
  33267. '']));
  33268. end;
  33269. procedure TTestModule.TestObjectChecks;
  33270. begin
  33271. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33272. StartProgram(false);
  33273. Add([
  33274. 'type',
  33275. ' TObject = class',
  33276. ' procedure DoIt;',
  33277. ' end;',
  33278. ' TClass = class of tobject;',
  33279. ' TBird = class',
  33280. ' end;',
  33281. ' TBirdClass = class of TBird;',
  33282. 'var',
  33283. ' o : TObject;',
  33284. ' c: TClass;',
  33285. ' b: TBird;',
  33286. ' bc: TBirdClass;',
  33287. 'procedure TObject.DoIt;',
  33288. 'begin',
  33289. ' b:=TBird(o);',
  33290. 'end;',
  33291. 'begin',
  33292. ' o.DoIt;',
  33293. ' b:=TBird(o);',
  33294. ' bc:=TBirdClass(c);',
  33295. '']);
  33296. ConvertProgram;
  33297. CheckSource('TestCheckMethodCall',
  33298. LinesToStr([ // statements
  33299. 'rtl.createClass(this, "TObject", null, function () {',
  33300. ' this.$init = function () {',
  33301. ' };',
  33302. ' this.$final = function () {',
  33303. ' };',
  33304. ' this.DoIt = function () {',
  33305. ' rtl.checkMethodCall(this,$mod.TObject);',
  33306. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33307. ' };',
  33308. '});',
  33309. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33310. '});',
  33311. 'this.o = null;',
  33312. 'this.c = null;',
  33313. 'this.b = null;',
  33314. 'this.bc = null;',
  33315. '']),
  33316. LinesToStr([ // $mod.$main
  33317. '$mod.o.DoIt();',
  33318. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33319. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33320. '']));
  33321. end;
  33322. procedure TTestModule.TestOverflowChecks_Int;
  33323. begin
  33324. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33325. StartProgram(false);
  33326. Add([
  33327. 'procedure DoIt;',
  33328. 'var',
  33329. ' b: byte;',
  33330. ' n: nativeint;',
  33331. ' u: nativeuint;',
  33332. ' c: currency;',
  33333. 'begin',
  33334. ' n:=n+n;',
  33335. ' n:=n-n;',
  33336. ' n:=n+b;',
  33337. ' n:=b-n;',
  33338. ' n:=n*n;',
  33339. ' n:=n*u;',
  33340. ' c:=c+b;',
  33341. ' c:=b+c;',
  33342. ' c:=c*b;',
  33343. ' c:=b*c;',
  33344. 'end;',
  33345. 'begin',
  33346. '']);
  33347. ConvertProgram;
  33348. CheckSource('TestOverflowChecks_Int',
  33349. LinesToStr([ // statements
  33350. 'this.DoIt = function () {',
  33351. ' var b = 0;',
  33352. ' var n = 0;',
  33353. ' var u = 0;',
  33354. ' var c = 0;',
  33355. ' n = rtl.oc(n + n);',
  33356. ' n = rtl.oc(n - n);',
  33357. ' n = rtl.oc(n + b);',
  33358. ' n = rtl.oc(b - n);',
  33359. ' n = rtl.oc(n * n);',
  33360. ' n = rtl.oc(n * u);',
  33361. ' c = rtl.oc(c + (b * 10000));',
  33362. ' c = rtl.oc((b * 10000) + c);',
  33363. ' c = rtl.oc(c * b);',
  33364. ' c = rtl.oc(b * c);',
  33365. '};',
  33366. '']),
  33367. LinesToStr([ // $mod.$main
  33368. '']));
  33369. end;
  33370. procedure TTestModule.TestRangeChecks_AssignInt;
  33371. begin
  33372. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33373. StartProgram(false);
  33374. Add([
  33375. '{$R+}',
  33376. 'var',
  33377. ' b: byte = 2;',
  33378. ' w: word = 3;',
  33379. 'procedure DoIt(p: byte);',
  33380. 'begin',
  33381. ' b:=w;',
  33382. ' b+=w;',
  33383. ' b:=1;',
  33384. 'end;',
  33385. '{$R-}',
  33386. 'procedure DoSome;',
  33387. 'begin',
  33388. ' DoIt(w);',
  33389. ' b:=w;',
  33390. ' b:=2;',
  33391. 'end;',
  33392. 'begin',
  33393. '{$R+}',
  33394. '']);
  33395. ConvertProgram;
  33396. CheckSource('TestRangeChecks_AssignInt',
  33397. LinesToStr([ // statements
  33398. 'this.b = 2;',
  33399. 'this.w = 3;',
  33400. 'this.DoIt = function (p) {',
  33401. ' rtl.rc(p, 0, 255);',
  33402. ' $mod.b = rtl.rc($mod.w,0,255);',
  33403. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33404. ' $mod.b = 1;',
  33405. '};',
  33406. 'this.DoSome = function () {',
  33407. ' $mod.DoIt($mod.w);',
  33408. ' $mod.b = $mod.w;',
  33409. ' $mod.b = 2;',
  33410. '};',
  33411. '']),
  33412. LinesToStr([ // $mod.$main
  33413. '']));
  33414. end;
  33415. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33416. begin
  33417. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33418. StartProgram(false);
  33419. Add([
  33420. '{$R+}',
  33421. 'type Ten = 1..10;',
  33422. 'var',
  33423. ' b: Ten = 2;',
  33424. ' w: Ten = 3;',
  33425. 'procedure DoIt(p: Ten);',
  33426. 'begin',
  33427. ' b:=w;',
  33428. ' b+=w;',
  33429. ' b:=1;',
  33430. 'end;',
  33431. '{$R-}',
  33432. 'procedure DoSome;',
  33433. 'begin',
  33434. ' DoIt(w);',
  33435. ' b:=w;',
  33436. ' b:=2;',
  33437. 'end;',
  33438. 'begin',
  33439. '{$R+}',
  33440. '']);
  33441. ConvertProgram;
  33442. CheckSource('TestRangeChecks_AssignIntRange',
  33443. LinesToStr([ // statements
  33444. 'this.b = 2;',
  33445. 'this.w = 3;',
  33446. 'this.DoIt = function (p) {',
  33447. ' rtl.rc(p, 1, 10);',
  33448. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33449. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33450. ' $mod.b = 1;',
  33451. '};',
  33452. 'this.DoSome = function () {',
  33453. ' $mod.DoIt($mod.w);',
  33454. ' $mod.b = $mod.w;',
  33455. ' $mod.b = 2;',
  33456. '};',
  33457. '']),
  33458. LinesToStr([ // $mod.$main
  33459. '']));
  33460. end;
  33461. procedure TTestModule.TestRangeChecks_AssignEnum;
  33462. begin
  33463. StartProgram(false);
  33464. Add([
  33465. '{$R+}',
  33466. 'type TEnum = (red,green);',
  33467. 'var',
  33468. ' e: TEnum = red;',
  33469. 'procedure DoIt(p: TEnum);',
  33470. 'begin',
  33471. ' e:=p;',
  33472. ' p:=TEnum(0);',
  33473. ' p:=succ(e);',
  33474. 'end;',
  33475. '{$R-}',
  33476. 'procedure DoSome;',
  33477. 'begin',
  33478. ' DoIt(e);',
  33479. ' e:=TEnum(1);',
  33480. ' e:=pred(e);',
  33481. 'end;',
  33482. 'begin',
  33483. '{$R+}',
  33484. '']);
  33485. ConvertProgram;
  33486. CheckSource('TestRangeChecks_AssignEnum',
  33487. LinesToStr([ // statements
  33488. 'this.TEnum = {',
  33489. ' "0": "red",',
  33490. ' red: 0,',
  33491. ' "1": "green",',
  33492. ' green: 1',
  33493. '};',
  33494. 'this.e = this.TEnum.red;',
  33495. 'this.DoIt = function (p) {',
  33496. ' rtl.rc(p, 0, 1);',
  33497. ' $mod.e = rtl.rc(p, 0, 1);',
  33498. ' p = 0;',
  33499. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33500. '};',
  33501. 'this.DoSome = function () {',
  33502. ' $mod.DoIt($mod.e);',
  33503. ' $mod.e = 1;',
  33504. ' $mod.e = $mod.e - 1;',
  33505. '};',
  33506. '']),
  33507. LinesToStr([ // $mod.$main
  33508. '']));
  33509. end;
  33510. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33511. begin
  33512. StartProgram(false);
  33513. Add([
  33514. '{$R+}',
  33515. 'type',
  33516. ' TEnum = (red,green);',
  33517. ' TEnumRg = red..green;',
  33518. 'var',
  33519. ' e: TEnumRg = red;',
  33520. 'procedure DoIt(p: TEnumRg);',
  33521. 'begin',
  33522. ' e:=p;',
  33523. ' p:=TEnumRg(0);',
  33524. ' p:=succ(e);',
  33525. 'end;',
  33526. '{$R-}',
  33527. 'procedure DoSome;',
  33528. 'begin',
  33529. ' DoIt(e);',
  33530. ' e:=TEnum(1);',
  33531. ' e:=pred(e);',
  33532. 'end;',
  33533. 'begin',
  33534. '{$R+}',
  33535. '']);
  33536. ConvertProgram;
  33537. CheckSource('TestRangeChecks_AssignEnumRange',
  33538. LinesToStr([ // statements
  33539. 'this.TEnum = {',
  33540. ' "0": "red",',
  33541. ' red: 0,',
  33542. ' "1": "green",',
  33543. ' green: 1',
  33544. '};',
  33545. 'this.e = this.TEnum.red;',
  33546. 'this.DoIt = function (p) {',
  33547. ' rtl.rc(p, 0, 1);',
  33548. ' $mod.e = rtl.rc(p, 0, 1);',
  33549. ' p = 0;',
  33550. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33551. '};',
  33552. 'this.DoSome = function () {',
  33553. ' $mod.DoIt($mod.e);',
  33554. ' $mod.e = 1;',
  33555. ' $mod.e = $mod.e - 1;',
  33556. '};',
  33557. '']),
  33558. LinesToStr([ // $mod.$main
  33559. '']));
  33560. end;
  33561. procedure TTestModule.TestRangeChecks_AssignChar;
  33562. begin
  33563. StartProgram(false);
  33564. Add([
  33565. '{$R+}',
  33566. 'type',
  33567. ' TLetter = char;',
  33568. 'var',
  33569. ' b: TLetter = ''2'';',
  33570. ' w: TLetter = ''3'';',
  33571. 'procedure DoIt(p: TLetter);',
  33572. 'begin',
  33573. ' b:=w;',
  33574. ' b:=''1'';',
  33575. 'end;',
  33576. '{$R-}',
  33577. 'procedure DoSome;',
  33578. 'begin',
  33579. ' DoIt(w);',
  33580. ' b:=w;',
  33581. ' b:=''2'';',
  33582. 'end;',
  33583. 'begin',
  33584. '{$R+}',
  33585. '']);
  33586. ConvertProgram;
  33587. CheckSource('TestRangeChecks_AssignChar',
  33588. LinesToStr([ // statements
  33589. 'this.b = "2";',
  33590. 'this.w = "3";',
  33591. 'this.DoIt = function (p) {',
  33592. ' rtl.rcc(p, 0, 65535);',
  33593. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33594. ' $mod.b = "1";',
  33595. '};',
  33596. 'this.DoSome = function () {',
  33597. ' $mod.DoIt($mod.w);',
  33598. ' $mod.b = $mod.w;',
  33599. ' $mod.b = "2";',
  33600. '};',
  33601. '']),
  33602. LinesToStr([ // $mod.$main
  33603. '']));
  33604. end;
  33605. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33606. begin
  33607. StartProgram(false);
  33608. Add([
  33609. '{$R+}',
  33610. 'type TDigit = ''0''..''9'';',
  33611. 'var',
  33612. ' b: TDigit = ''2'';',
  33613. ' w: TDigit = ''3'';',
  33614. 'procedure DoIt(p: TDigit);',
  33615. 'begin',
  33616. ' b:=w;',
  33617. ' b:=''1'';',
  33618. 'end;',
  33619. '{$R-}',
  33620. 'procedure DoSome;',
  33621. 'begin',
  33622. ' DoIt(w);',
  33623. ' b:=w;',
  33624. ' b:=''2'';',
  33625. 'end;',
  33626. 'begin',
  33627. '{$R+}',
  33628. '']);
  33629. ConvertProgram;
  33630. CheckSource('TestRangeChecks_AssignCharRange',
  33631. LinesToStr([ // statements
  33632. 'this.b = "2";',
  33633. 'this.w = "3";',
  33634. 'this.DoIt = function (p) {',
  33635. ' rtl.rcc(p, 48, 57);',
  33636. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33637. ' $mod.b = "1";',
  33638. '};',
  33639. 'this.DoSome = function () {',
  33640. ' $mod.DoIt($mod.w);',
  33641. ' $mod.b = $mod.w;',
  33642. ' $mod.b = "2";',
  33643. '};',
  33644. '']),
  33645. LinesToStr([ // $mod.$main
  33646. '']));
  33647. end;
  33648. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33649. begin
  33650. StartProgram(false);
  33651. Add([
  33652. '{$R+}',
  33653. 'type',
  33654. ' Ten = 1..10;',
  33655. ' TArr = array of Ten;',
  33656. ' TArrArr = array of TArr;',
  33657. ' TArrByte = array[byte] of Ten;',
  33658. ' TArrChar = array[''0''..''9''] of Ten;',
  33659. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33660. ' TObject = class',
  33661. ' A: TArr;',
  33662. ' end;',
  33663. 'procedure DoIt;',
  33664. 'var',
  33665. ' Arr: TArr;',
  33666. ' ArrArr: TArrArr;',
  33667. ' ArrByte: TArrByte;',
  33668. ' ArrChar: TArrChar;',
  33669. ' ArrByteChar: TArrByteChar;',
  33670. ' i: Ten;',
  33671. ' c: char;',
  33672. ' o: tobject;',
  33673. 'begin',
  33674. ' i:=Arr[1];',
  33675. ' i:=ArrByteChar[1,''2''];',
  33676. ' Arr[1]:=Arr[1];',
  33677. ' Arr[i]:=Arr[i];',
  33678. ' ArrByte[3]:=ArrByte[3];',
  33679. ' ArrByte[i]:=ArrByte[i];',
  33680. ' ArrChar[''5'']:=ArrChar[''5''];',
  33681. ' ArrChar[c]:=ArrChar[c];',
  33682. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33683. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33684. ' o.a[i]:=o.a[i];',
  33685. 'end;',
  33686. 'begin',
  33687. '']);
  33688. ConvertProgram;
  33689. CheckSource('TestRangeChecks_ArrayIndex',
  33690. LinesToStr([ // statements
  33691. 'this.TArrByteChar$clone = function (a) {',
  33692. ' var b = [];',
  33693. ' b.length = 256;',
  33694. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33695. ' return b;',
  33696. '};',
  33697. 'rtl.createClass(this, "TObject", null, function () {',
  33698. ' this.$init = function () {',
  33699. ' this.A = [];',
  33700. ' };',
  33701. ' this.$final = function () {',
  33702. ' this.A = undefined;',
  33703. ' };',
  33704. '});',
  33705. 'this.DoIt = function () {',
  33706. ' var Arr = [];',
  33707. ' var ArrArr = [];',
  33708. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33709. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33710. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33711. ' var i = 0;',
  33712. ' var c = "\x00";',
  33713. ' var o = null;',
  33714. ' i = rtl.rc(Arr[1], 1, 10);',
  33715. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33716. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33717. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33718. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33719. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33720. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33721. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33722. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33723. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33724. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33725. '};',
  33726. '']),
  33727. LinesToStr([ // $mod.$main
  33728. '']));
  33729. end;
  33730. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33731. begin
  33732. StartProgram(false);
  33733. Add([
  33734. '{$R+}',
  33735. 'type',
  33736. ' Ten = 1..10;',
  33737. ' TRec = record x: Ten end;',
  33738. ' TArr = array of TRec;',
  33739. ' TArrArr = array of TArr;',
  33740. ' TObject = class',
  33741. ' A: TArr;',
  33742. ' end;',
  33743. 'procedure DoIt;',
  33744. 'var',
  33745. ' Arr: TArr;',
  33746. ' ArrArr: TArrArr;',
  33747. ' i: Ten;',
  33748. ' o: tobject;',
  33749. 'begin',
  33750. ' Arr[1]:=Arr[1];',
  33751. ' Arr[i]:=Arr[i+1];',
  33752. ' o.a[i]:=o.a[i+2];',
  33753. 'end;',
  33754. 'begin',
  33755. '']);
  33756. ConvertProgram;
  33757. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33758. LinesToStr([ // statements
  33759. 'rtl.recNewT(this, "TRec", function () {',
  33760. ' this.x = 0;',
  33761. ' this.$eq = function (b) {',
  33762. ' return this.x === b.x;',
  33763. ' };',
  33764. ' this.$assign = function (s) {',
  33765. ' this.x = s.x;',
  33766. ' return this;',
  33767. ' };',
  33768. '});',
  33769. 'rtl.createClass(this, "TObject", null, function () {',
  33770. ' this.$init = function () {',
  33771. ' this.A = [];',
  33772. ' };',
  33773. ' this.$final = function () {',
  33774. ' this.A = undefined;',
  33775. ' };',
  33776. '});',
  33777. 'this.DoIt = function () {',
  33778. ' var Arr = [];',
  33779. ' var ArrArr = [];',
  33780. ' var i = 0;',
  33781. ' var o = null;',
  33782. ' Arr[1].$assign(Arr[1]);',
  33783. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33784. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33785. '};',
  33786. '']),
  33787. LinesToStr([ // $mod.$main
  33788. '']));
  33789. end;
  33790. procedure TTestModule.TestRangeChecks_StringIndex;
  33791. begin
  33792. StartProgram(false);
  33793. Add([
  33794. 'type',
  33795. ' TObject = class',
  33796. ' S: string;',
  33797. ' end;',
  33798. '{$R+}',
  33799. 'procedure DoIt(var h: string);',
  33800. 'var',
  33801. ' s: string;',
  33802. ' i: longint;',
  33803. ' c: char;',
  33804. ' o: tobject;',
  33805. 'begin',
  33806. ' c:=s[1];',
  33807. ' s[i]:=s[i];',
  33808. ' h[i]:=h[i];',
  33809. ' c:=o.s[i];',
  33810. ' o.s[i]:=c;',
  33811. 'end;',
  33812. 'begin',
  33813. '']);
  33814. ConvertProgram;
  33815. CheckSource('TestRangeChecks_StringIndex',
  33816. LinesToStr([ // statements
  33817. 'rtl.createClass(this, "TObject", null, function () {',
  33818. ' this.$init = function () {',
  33819. ' this.S = "";',
  33820. ' };',
  33821. ' this.$final = function () {',
  33822. ' };',
  33823. '});',
  33824. 'this.DoIt = function (h) {',
  33825. ' var s = "";',
  33826. ' var i = 0;',
  33827. ' var c = "\x00";',
  33828. ' var o = null;',
  33829. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33830. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33831. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33832. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33833. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33834. '};',
  33835. '']),
  33836. LinesToStr([ // $mod.$main
  33837. '']));
  33838. end;
  33839. procedure TTestModule.TestRangeChecks_TypecastInt;
  33840. begin
  33841. StartProgram(false);
  33842. Add([
  33843. '{$R+}',
  33844. 'var',
  33845. ' i: nativeint;',
  33846. ' b: byte;',
  33847. ' sh: shortint;',
  33848. ' w: word;',
  33849. ' sm: smallint;',
  33850. ' lw: longword;',
  33851. ' li: longint;',
  33852. 'begin',
  33853. ' b:=12+byte(i);',
  33854. ' sh:=12+shortint(i);',
  33855. ' w:=12+word(i);',
  33856. ' sm:=12+smallint(i);',
  33857. ' lw:=12+longword(i);',
  33858. ' li:=12+longint(i);',
  33859. '']);
  33860. ConvertProgram;
  33861. CheckSource('TestRangeChecks_TypecastInt',
  33862. LinesToStr([
  33863. 'this.i = 0;',
  33864. 'this.b = 0;',
  33865. 'this.sh = 0;',
  33866. 'this.w = 0;',
  33867. 'this.sm = 0;',
  33868. 'this.lw = 0;',
  33869. 'this.li = 0;',
  33870. '']),
  33871. LinesToStr([
  33872. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33873. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33874. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33875. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33876. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33877. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33878. '']));
  33879. end;
  33880. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33881. begin
  33882. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33883. StartProgram(false);
  33884. Add([
  33885. '{$modeswitch typehelpers}',
  33886. '{$R+}',
  33887. 'type',
  33888. ' TObject = class',
  33889. ' FSize: byte;',
  33890. ' property Size: byte read FSize;',
  33891. ' end;',
  33892. ' THelper = type helper for byte',
  33893. ' procedure SetIt(w: word);',
  33894. ' end;',
  33895. 'procedure THelper.SetIt(w: word);',
  33896. 'begin',
  33897. ' Self:=w;',
  33898. 'end;',
  33899. 'function GetIt: byte;',
  33900. 'begin',
  33901. ' Result.SetIt(2);',
  33902. 'end;',
  33903. 'var',
  33904. ' b: byte = 3;',
  33905. ' o: TObject;',
  33906. 'begin',
  33907. ' b.SetIt(14);',
  33908. ' with b do SetIt(15);',
  33909. ' o.Size.SetIt(16);',
  33910. '']);
  33911. ConvertProgram;
  33912. CheckSource('TestRangeChecks_AssignInt',
  33913. LinesToStr([ // statements
  33914. 'rtl.createClass(this, "TObject", null, function () {',
  33915. ' this.$init = function () {',
  33916. ' this.FSize = 0;',
  33917. ' };',
  33918. ' this.$final = function () {',
  33919. ' };',
  33920. '});',
  33921. 'rtl.createHelper(this, "THelper", null, function () {',
  33922. ' this.SetIt = function (w) {',
  33923. ' rtl.rc(w, 0, 65535);',
  33924. ' this.set(w);',
  33925. ' };',
  33926. '});',
  33927. 'this.GetIt = function () {',
  33928. ' var Result = 0;',
  33929. ' $mod.THelper.SetIt.call({',
  33930. ' get: function () {',
  33931. ' return Result;',
  33932. ' },',
  33933. ' set: function (v) {',
  33934. ' rtl.rc(v, 0, 255);',
  33935. ' Result = v;',
  33936. ' }',
  33937. ' }, 2);',
  33938. ' return Result;',
  33939. '};',
  33940. 'this.b = 3;',
  33941. 'this.o = null;',
  33942. '']),
  33943. LinesToStr([ // $mod.$main
  33944. '$mod.THelper.SetIt.call({',
  33945. ' p: $mod,',
  33946. ' get: function () {',
  33947. ' return this.p.b;',
  33948. ' },',
  33949. ' set: function (v) {',
  33950. ' rtl.rc(v, 0, 255);',
  33951. ' this.p.b = v;',
  33952. ' }',
  33953. '}, 14);',
  33954. 'var $with = $mod.b;',
  33955. '$mod.THelper.SetIt.call({',
  33956. ' get: function () {',
  33957. ' return $with;',
  33958. ' },',
  33959. ' set: function (v) {',
  33960. ' rtl.rc(v, 0, 255);',
  33961. ' $with = v;',
  33962. ' }',
  33963. '}, 15);',
  33964. '$mod.THelper.SetIt.call({',
  33965. ' p: $mod.o,',
  33966. ' get: function () {',
  33967. ' return this.p.FSize;',
  33968. ' },',
  33969. ' set: function (v) {',
  33970. ' rtl.rc(v, 0, 255);',
  33971. ' this.p.FSize = v;',
  33972. ' }',
  33973. '}, 16);',
  33974. '']));
  33975. end;
  33976. procedure TTestModule.TestRangeChecks_AssignCurrency;
  33977. begin
  33978. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33979. StartProgram(false);
  33980. Add([
  33981. '{$R+}',
  33982. 'var',
  33983. ' c: currency = 2.34;',
  33984. ' i: double;',
  33985. 'procedure DoIt(p: currency);',
  33986. 'begin',
  33987. ' c:=i;',
  33988. ' c+=i;',
  33989. ' c:=1;',
  33990. 'end;',
  33991. '{$R-}',
  33992. 'procedure DoSome;',
  33993. 'begin',
  33994. ' DoIt(i);',
  33995. ' c:=i;',
  33996. ' c:=2;',
  33997. 'end;',
  33998. 'begin',
  33999. '{$R+}',
  34000. '']);
  34001. ConvertProgram;
  34002. CheckSource('TestRangeChecks_AssignCurrency',
  34003. LinesToStr([ // statements
  34004. 'this.c = 2.34;',
  34005. 'this.i = 0.0;',
  34006. 'this.DoIt = function (p) {',
  34007. ' rtl.rc(p, -922337203685477, 922337203685477);',
  34008. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34009. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34010. ' $mod.c = 10000;',
  34011. '};',
  34012. 'this.DoSome = function () {',
  34013. ' $mod.DoIt($mod.i * 10000);',
  34014. ' $mod.c = rtl.trunc($mod.i * 10000);',
  34015. ' $mod.c = 20000;',
  34016. '};',
  34017. '']),
  34018. LinesToStr([ // $mod.$main
  34019. '']));
  34020. end;
  34021. procedure TTestModule.TestAsync_Proc;
  34022. begin
  34023. StartProgram(false);
  34024. Add([
  34025. 'procedure Fly(w: word = 1); async; forward;',
  34026. 'procedure Run(w: word = 2); async;',
  34027. 'begin',
  34028. ' Fly(w);',
  34029. ' Fly;',
  34030. ' await(Fly(w));',
  34031. ' await(Fly);',
  34032. 'end;',
  34033. 'procedure Fly(w: word); ',
  34034. 'begin',
  34035. 'end;',
  34036. 'begin',
  34037. ' Run;',
  34038. ' Run(3);',
  34039. '']);
  34040. CheckResolverUnexpectedHints();
  34041. ConvertProgram;
  34042. CheckSource('TestAsync_Proc',
  34043. LinesToStr([ // statements
  34044. 'this.Run = async function (w) {',
  34045. ' $mod.Fly(w);',
  34046. ' $mod.Fly(1);',
  34047. ' await $mod.Fly(w);',
  34048. ' await $mod.Fly(1);',
  34049. '};',
  34050. 'this.Fly = async function (w) {',
  34051. '};',
  34052. '']),
  34053. LinesToStr([
  34054. '$mod.Run(2);',
  34055. '$mod.Run(3);',
  34056. '']));
  34057. end;
  34058. procedure TTestModule.TestAsync_CallResultIsPromise;
  34059. begin
  34060. StartProgram(false);
  34061. Add([
  34062. '{$modeswitch externalclass}',
  34063. 'type',
  34064. ' TObject = class',
  34065. ' end;',
  34066. ' TJSPromise = class external name ''Promise''',
  34067. ' end;',
  34068. ' TBird = class',
  34069. ' function Fly: word; async; ',
  34070. ' end;',
  34071. 'function TBird.Fly: word; async; ',
  34072. 'begin',
  34073. ' Result:=3;',
  34074. ' Fly:=4+Result;',
  34075. ' if Result=5 then ;',
  34076. ' exit(6);',
  34077. 'end;',
  34078. 'function Run: word; async;',
  34079. 'begin',
  34080. ' Result:=11+Result;',
  34081. ' inc(Result);',
  34082. 'end;',
  34083. 'var',
  34084. ' p: TJSPromise;',
  34085. ' o: TBird;',
  34086. 'begin',
  34087. ' p:=Run;',
  34088. ' p:=Run();',
  34089. ' if Run=p then ;',
  34090. ' if p=Run then ;',
  34091. ' if Run()=p then ;',
  34092. ' if p=Run() then ;',
  34093. ' p:=o.Fly;',
  34094. ' p:=o.Fly();',
  34095. ' if o.Fly=p then ;',
  34096. ' if o.Fly()=p then ;',
  34097. ' with o do begin',
  34098. ' p:=Fly;',
  34099. ' p:=Fly();',
  34100. ' if Fly=p then ;',
  34101. ' if Fly()=p then ;',
  34102. ' end;',
  34103. '']);
  34104. CheckResolverUnexpectedHints();
  34105. ConvertProgram;
  34106. CheckSource('TestAsync_CallResultIsPromise',
  34107. LinesToStr([ // statements
  34108. 'rtl.createClass(this, "TObject", null, function () {',
  34109. ' this.$init = function () {',
  34110. ' };',
  34111. ' this.$final = function () {',
  34112. ' };',
  34113. '});',
  34114. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34115. ' this.Fly = async function () {',
  34116. ' var Result = 0;',
  34117. ' Result = 3;',
  34118. ' Result = 4 + Result;',
  34119. ' if (Result === 5) ;',
  34120. ' return 6;',
  34121. ' return Result;',
  34122. ' };',
  34123. '});',
  34124. 'this.Run = async function () {',
  34125. ' var Result = 0;',
  34126. ' Result = 11 + Result;',
  34127. ' Result += 1;',
  34128. ' return Result;',
  34129. '};',
  34130. 'this.p = null;',
  34131. 'this.o = null;',
  34132. '']),
  34133. LinesToStr([
  34134. '$mod.p = $mod.Run();',
  34135. '$mod.p = $mod.Run();',
  34136. 'if ($mod.Run() === $mod.p) ;',
  34137. 'if ($mod.p === $mod.Run()) ;',
  34138. 'if ($mod.Run() === $mod.p) ;',
  34139. 'if ($mod.p === $mod.Run()) ;',
  34140. '$mod.p = $mod.o.Fly();',
  34141. '$mod.p = $mod.o.Fly();',
  34142. 'if ($mod.o.Fly() === $mod.p) ;',
  34143. 'if ($mod.o.Fly() === $mod.p) ;',
  34144. 'var $with = $mod.o;',
  34145. '$mod.p = $with.Fly();',
  34146. '$mod.p = $with.Fly();',
  34147. 'if ($with.Fly() === $mod.p) ;',
  34148. 'if ($with.Fly() === $mod.p) ;',
  34149. '']));
  34150. end;
  34151. procedure TTestModule.TestAsync_ConstructorFail;
  34152. begin
  34153. StartProgram(false);
  34154. Add([
  34155. 'type',
  34156. ' TObject = class',
  34157. ' end;',
  34158. ' TBird = class',
  34159. ' constructor Create; async;',
  34160. ' end;',
  34161. 'constructor TBird.Create; async;',
  34162. 'begin',
  34163. 'end;',
  34164. 'begin',
  34165. '']);
  34166. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34167. ConvertProgram;
  34168. end;
  34169. procedure TTestModule.TestAsync_PropertyGetterFail;
  34170. begin
  34171. StartProgram(false);
  34172. Add([
  34173. 'type',
  34174. ' TObject = class',
  34175. ' end;',
  34176. ' TBird = class',
  34177. ' function GetSize: word; async;',
  34178. ' property Size: word read GetSize;',
  34179. ' end;',
  34180. 'function TBird.GetSize: word; async;',
  34181. 'begin',
  34182. 'end;',
  34183. 'begin',
  34184. '']);
  34185. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34186. ConvertProgram;
  34187. end;
  34188. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34189. begin
  34190. StartProgram(false);
  34191. Add([
  34192. 'procedure Run; async;',
  34193. 'begin',
  34194. ' await(word,1);',
  34195. 'end;',
  34196. 'begin',
  34197. '']);
  34198. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34199. ConvertProgram;
  34200. end;
  34201. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34202. begin
  34203. StartProgram(false);
  34204. Add([
  34205. 'type',
  34206. ' TObject = class',
  34207. ' end;',
  34208. ' TBird = class',
  34209. ' end;',
  34210. 'function Fly: TObject; async;',
  34211. 'begin',
  34212. 'end;',
  34213. 'procedure Run; async;',
  34214. 'begin',
  34215. ' await(TBird,Fly);',
  34216. 'end;',
  34217. 'begin',
  34218. '']);
  34219. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34220. ConvertProgram;
  34221. end;
  34222. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34223. begin
  34224. StartProgram(false);
  34225. Add([
  34226. 'procedure Crawl(w: double); ',
  34227. 'begin',
  34228. 'end;',
  34229. 'procedure Run(w: double);',
  34230. 'begin',
  34231. ' await(Crawl(w));',
  34232. 'end;',
  34233. 'begin',
  34234. ' Run(1);']);
  34235. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34236. ConvertProgram;
  34237. end;
  34238. procedure TTestModule.TestAWait_IntegerFail;
  34239. begin
  34240. StartProgram(false);
  34241. Add([
  34242. 'function Run: word;',
  34243. 'begin',
  34244. 'end;',
  34245. 'procedure Fly(w: word); async;',
  34246. 'begin',
  34247. ' await(Run());',
  34248. 'end;',
  34249. 'begin',
  34250. ' Fly(1);']);
  34251. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34252. ConvertProgram;
  34253. end;
  34254. procedure TTestModule.TestAWait_ExternalClassPromise;
  34255. begin
  34256. StartProgram(false);
  34257. Add([
  34258. '{$modeswitch externalclass}',
  34259. 'type',
  34260. ' TJSPromise = class external name ''Promise''',
  34261. ' end;',
  34262. ' TJSThenable = class external name ''Thenable''',
  34263. ' end;',
  34264. 'function Fly(w: word): TJSPromise;',
  34265. 'begin',
  34266. 'end;',
  34267. 'function Jump(w: word): word; async;',
  34268. 'begin',
  34269. 'end;',
  34270. 'function Eat(w: word): TJSPromise; async;',
  34271. 'begin',
  34272. 'end;',
  34273. 'function Run(d: double): word; async;',
  34274. 'var',
  34275. ' p: TJSPromise;',
  34276. 'begin',
  34277. ' Result:=await(word,p);', // promise needs type
  34278. ' Result:=await(word,Fly(3));', // promise needs type
  34279. ' Result:=await(Jump(4));', // async non promise must omit the type
  34280. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34281. ' Result:=await(word,Eat(6));', // promise needs type
  34282. 'end;',
  34283. 'begin',
  34284. '']);
  34285. ConvertProgram;
  34286. CheckSource('TestAWait_ExternalClassPromise',
  34287. LinesToStr([ // statements
  34288. 'this.Fly = function (w) {',
  34289. ' var Result = null;',
  34290. ' return Result;',
  34291. '};',
  34292. 'this.Jump = async function (w) {',
  34293. ' var Result = 0;',
  34294. ' return Result;',
  34295. '};',
  34296. 'this.Eat = async function (w) {',
  34297. ' var Result = null;',
  34298. ' return Result;',
  34299. '};',
  34300. 'this.Run = async function (d) {',
  34301. ' var Result = 0;',
  34302. ' var p = null;',
  34303. ' Result = await p;',
  34304. ' Result = await $mod.Fly(3);',
  34305. ' Result = await $mod.Jump(4);',
  34306. ' Result = await $mod.Jump(5);',
  34307. ' Result = await $mod.Eat(6);',
  34308. ' return Result;',
  34309. '};',
  34310. '']),
  34311. LinesToStr([
  34312. ]));
  34313. CheckResolverUnexpectedHints();
  34314. end;
  34315. procedure TTestModule.TestAWait_JSValue;
  34316. begin
  34317. StartProgram(false);
  34318. Add([
  34319. '{$modeswitch externalclass}',
  34320. 'type',
  34321. ' TJSPromise = class external name ''Promise''',
  34322. ' end;',
  34323. 'function Fly(w: word): jsvalue; async;',
  34324. 'begin',
  34325. 'end;',
  34326. 'function Run(d: jsvalue; var e): word; async;',
  34327. 'begin',
  34328. ' Result:=await(word,d);', // promise needs type
  34329. ' d:=await(Fly(4));', // async non promise must omit the type
  34330. ' Result:=await(word,e);', // promise needs type
  34331. 'end;',
  34332. 'begin',
  34333. '']);
  34334. ConvertProgram;
  34335. CheckSource('TestAWait_JSValue',
  34336. LinesToStr([ // statements
  34337. 'this.Fly = async function (w) {',
  34338. ' var Result = undefined;',
  34339. ' return Result;',
  34340. '};',
  34341. 'this.Run = async function (d, e) {',
  34342. ' var Result = 0;',
  34343. ' Result = await d;',
  34344. ' d = await $mod.Fly(4);',
  34345. ' Result = await e.get();',
  34346. ' return Result;',
  34347. '};',
  34348. '']),
  34349. LinesToStr([
  34350. ]));
  34351. CheckResolverUnexpectedHints();
  34352. end;
  34353. procedure TTestModule.TestAWait_Result;
  34354. begin
  34355. StartProgram(false);
  34356. Add([
  34357. '{$modeswitch externalclass}',
  34358. 'type',
  34359. ' TJSPromise = class external name ''Promise''',
  34360. ' end;',
  34361. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34362. 'begin',
  34363. 'end;',
  34364. 'function Run(d: double = 1.6): word; async;',
  34365. 'begin',
  34366. ' Result:=await(word,Crawl);',
  34367. ' Result:=await(word,Crawl(4.5));',
  34368. ' Result:=await(Run);',
  34369. ' Result:=await(Run(6.7));',
  34370. 'end;',
  34371. 'begin',
  34372. ' Run(1);']);
  34373. ConvertProgram;
  34374. CheckSource('TestAWait_Result',
  34375. LinesToStr([ // statements
  34376. 'this.Crawl = function (d) {',
  34377. ' var Result = null;',
  34378. ' return Result;',
  34379. '};',
  34380. 'this.Run = async function (d) {',
  34381. ' var Result = 0;',
  34382. ' Result = await $mod.Crawl(1.3);',
  34383. ' Result = await $mod.Crawl(4.5);',
  34384. ' Result = await $mod.Run(1.6);',
  34385. ' Result = await $mod.Run(6.7);',
  34386. ' return Result;',
  34387. '};',
  34388. '']),
  34389. LinesToStr([
  34390. '$mod.Run(1);'
  34391. ]));
  34392. CheckResolverUnexpectedHints();
  34393. end;
  34394. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34395. begin
  34396. StartProgram(false);
  34397. Add([
  34398. '{$mode objfpc}',
  34399. '{$modeswitch externalclass}',
  34400. 'type',
  34401. ' TJSPromise = class external name ''Promise''',
  34402. ' end;',
  34403. 'function Run: TJSPromise; async;',
  34404. 'begin',
  34405. 'end;',
  34406. 'procedure Fly(w: word); async;',
  34407. 'begin',
  34408. ' await(Run());',
  34409. 'end;',
  34410. 'begin',
  34411. ' Fly(1);']);
  34412. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34413. nWrongNumberOfParametersForCallTo);
  34414. ConvertProgram;
  34415. end;
  34416. procedure TTestModule.TestAsync_AnonymousProc;
  34417. begin
  34418. StartProgram(false);
  34419. Add([
  34420. '{$mode objfpc}',
  34421. '{$modeswitch externalclass}',
  34422. 'type',
  34423. ' TJSPromise = class external name ''Promise''',
  34424. ' end;',
  34425. 'type',
  34426. ' TFunc = reference to function(x: double): word; async;',
  34427. 'function Crawl(d: double = 1.3): word; async;',
  34428. 'begin',
  34429. 'end;',
  34430. 'var Func: TFunc;',
  34431. 'begin',
  34432. ' Func:=function(c:double):word async begin',
  34433. ' Result:=await(Crawl(c));',
  34434. ' end;',
  34435. ' Func:=function(c:double):word async assembler asm',
  34436. ' end;',
  34437. '']);
  34438. ConvertProgram;
  34439. CheckSource('TestAsync_AnonymousProc',
  34440. LinesToStr([ // statements
  34441. 'this.Crawl = async function (d) {',
  34442. ' var Result = 0;',
  34443. ' return Result;',
  34444. '};',
  34445. 'this.Func = null;',
  34446. '']),
  34447. LinesToStr([
  34448. '$mod.Func = async function (c) {',
  34449. ' var Result = 0;',
  34450. ' Result = await $mod.Crawl(c);',
  34451. ' return Result;',
  34452. '};',
  34453. '$mod.Func = async function (c) {',
  34454. '};',
  34455. '']));
  34456. CheckResolverUnexpectedHints();
  34457. end;
  34458. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34459. begin
  34460. StartProgram(false);
  34461. Add([
  34462. '{$mode objfpc}',
  34463. '{$modeswitch externalclass}',
  34464. 'type',
  34465. ' TJSPromise = class external name ''Promise''',
  34466. ' end;',
  34467. ' TObject = class',
  34468. ' public',
  34469. ' procedure Fly(Prom: TJSPromise);',
  34470. ' end;',
  34471. ' TFunc = reference to procedure(Bird: TObject);',
  34472. 'procedure TObject.Fly(Prom: TJSPromise);',
  34473. 'begin',
  34474. 'end;',
  34475. 'function Crawl: jsvalue; async;',
  34476. 'begin',
  34477. 'end;',
  34478. 'procedure Add(Func: TFunc);',
  34479. 'begin',
  34480. 'end;',
  34481. 'begin',
  34482. ' Add(procedure(Bird: TObject)',
  34483. ' begin',
  34484. ' Bird.Fly(Crawl());',
  34485. ' end);',
  34486. '']);
  34487. ConvertProgram;
  34488. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34489. LinesToStr([ // statements
  34490. 'rtl.createClass(this, "TObject", null, function () {',
  34491. ' this.$init = function () {',
  34492. ' };',
  34493. ' this.$final = function () {',
  34494. ' };',
  34495. ' this.Fly = function (Prom) {',
  34496. ' };',
  34497. '});',
  34498. 'this.Crawl = async function () {',
  34499. ' var Result = undefined;',
  34500. ' return Result;',
  34501. '};',
  34502. 'this.Add = function (Func) {',
  34503. '};',
  34504. '']),
  34505. LinesToStr([
  34506. '$mod.Add(function (Bird) {',
  34507. ' Bird.Fly($mod.Crawl());',
  34508. '});',
  34509. '']));
  34510. end;
  34511. procedure TTestModule.TestAsync_ProcType;
  34512. begin
  34513. StartProgram(false);
  34514. Add([
  34515. '{$mode objfpc}',
  34516. 'type',
  34517. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34518. ' TFunc = function(x: double = 1.1): word; async;',
  34519. ' TProc = procedure(x: longint = 7); async;',
  34520. 'function Crawl(d: double): word; async;',
  34521. 'begin',
  34522. 'end;',
  34523. 'procedure Run(e:longint); async;',
  34524. 'begin',
  34525. 'end;',
  34526. 'procedure Fly(p: TProc); async;',
  34527. 'begin',
  34528. ' await(p);',
  34529. ' await(p());',
  34530. 'end;',
  34531. 'var',
  34532. ' RefFunc: TRefFunc;',
  34533. ' Func: TFunc;',
  34534. ' Proc, ProcB: TProc;',
  34535. 'begin',
  34536. ' Func:=@Crawl;',
  34537. ' RefFunc:=@Crawl;',
  34538. ' RefFunc:=function(c:double):word async begin',
  34539. ' Result:=await(RefFunc);',
  34540. ' Result:=await(RefFunc());',
  34541. ' Result:=await(Func);',
  34542. ' Result:=await(Func());',
  34543. ' await(Proc);',
  34544. ' await(Proc());',
  34545. ' await(Proc(13));',
  34546. ' end;',
  34547. ' Proc:=@Run;',
  34548. ' if Proc=ProcB then ;',
  34549. ' ']);
  34550. ConvertProgram;
  34551. CheckResolverUnexpectedHints();
  34552. CheckSource('TestAsync_ProcType',
  34553. LinesToStr([ // statements
  34554. 'this.Crawl = async function (d) {',
  34555. ' var Result = 0;',
  34556. ' return Result;',
  34557. '};',
  34558. 'this.Run = async function (e) {',
  34559. '};',
  34560. 'this.Fly = async function (p) {',
  34561. ' await p(7);',
  34562. ' await p(7);',
  34563. '};',
  34564. 'this.RefFunc = null;',
  34565. 'this.Func = null;',
  34566. 'this.Proc = null;',
  34567. 'this.ProcB = null;',
  34568. '']),
  34569. LinesToStr([
  34570. '$mod.Func = $mod.Crawl;',
  34571. '$mod.RefFunc = $mod.Crawl;',
  34572. '$mod.RefFunc = async function (c) {',
  34573. ' var Result = 0;',
  34574. ' Result = await $mod.RefFunc(1.3);',
  34575. ' Result = await $mod.RefFunc(1.3);',
  34576. ' Result = await $mod.Func(1.1);',
  34577. ' Result = await $mod.Func(1.1);',
  34578. ' await $mod.Proc(7);',
  34579. ' await $mod.Proc(7);',
  34580. ' await $mod.Proc(13);',
  34581. ' return Result;',
  34582. '};',
  34583. '$mod.Proc = $mod.Run;',
  34584. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34585. '']));
  34586. end;
  34587. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34588. begin
  34589. StartProgram(false);
  34590. Add([
  34591. '{$mode objfpc}',
  34592. 'type',
  34593. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34594. 'function Crawl(d: double): word; async;',
  34595. 'begin',
  34596. 'end;',
  34597. 'var',
  34598. ' RefFunc: TRefFunc;',
  34599. 'begin',
  34600. ' RefFunc:=@Crawl;',
  34601. ' ']);
  34602. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34603. ConvertProgram;
  34604. end;
  34605. procedure TTestModule.TestAsync_Inherited;
  34606. begin
  34607. StartProgram(false);
  34608. Add([
  34609. '{$mode objfpc}',
  34610. '{$modeswitch externalclass}',
  34611. 'type',
  34612. ' TJSPromise = class external name ''Promise''',
  34613. ' end;',
  34614. ' TObject = class',
  34615. ' function Run(w: word = 3): word; async; virtual;',
  34616. ' end;',
  34617. ' TBird = class',
  34618. ' function Run(w: word = 3): word; async; override;',
  34619. ' end;',
  34620. 'function TObject.Run(w: word = 3): word; async;',
  34621. 'begin',
  34622. 'end;',
  34623. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34624. 'var p: TJSPromise;',
  34625. 'begin',
  34626. ' p:=inherited;',
  34627. ' p:=inherited Run;',
  34628. ' p:=inherited Run();',
  34629. ' p:=inherited Run(4);',
  34630. ' exit(p);',
  34631. ' exit(inherited);',
  34632. ' exit(inherited Run);',
  34633. ' exit(inherited Run(5));',
  34634. ' exit(6);',
  34635. 'end;',
  34636. 'begin',
  34637. ' ']);
  34638. ConvertProgram;
  34639. CheckSource('TestAsync_Inherited',
  34640. LinesToStr([ // statements
  34641. 'rtl.createClass(this, "TObject", null, function () {',
  34642. ' this.$init = function () {',
  34643. ' };',
  34644. ' this.$final = function () {',
  34645. ' };',
  34646. ' this.Run = async function (w) {',
  34647. ' var Result = 0;',
  34648. ' return Result;',
  34649. ' };',
  34650. '});',
  34651. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34652. ' this.Run = async function (w) {',
  34653. ' var Result = 0;',
  34654. ' var p = null;',
  34655. ' p = $mod.TObject.Run.apply(this, arguments);',
  34656. ' p = $mod.TObject.Run.call(this, 3);',
  34657. ' p = $mod.TObject.Run.call(this, 3);',
  34658. ' p = $mod.TObject.Run.call(this, 4);',
  34659. ' return p;',
  34660. ' return $mod.TObject.Run.apply(this, arguments);',
  34661. ' return $mod.TObject.Run.call(this, 3);',
  34662. ' return $mod.TObject.Run.call(this, 5);',
  34663. ' return 6;',
  34664. ' return Result;',
  34665. ' };',
  34666. '});',
  34667. '']),
  34668. LinesToStr([
  34669. '']));
  34670. CheckResolverUnexpectedHints();
  34671. end;
  34672. procedure TTestModule.TestAsync_ClassInterface;
  34673. begin
  34674. StartProgram(false);
  34675. Add([
  34676. '{$mode objfpc}',
  34677. '{$modeswitch externalclass}',
  34678. 'type',
  34679. ' TJSPromise = class external name ''Promise''',
  34680. ' end;',
  34681. ' IUnknown = interface',
  34682. ' function _AddRef: longint;',
  34683. ' function _Release: longint;',
  34684. ' end;',
  34685. 'function Say(i: IUnknown): IUnknown; async;',
  34686. 'begin',
  34687. 'end;',
  34688. 'function Run: IUnknown; async;',
  34689. 'begin',
  34690. ' Result:=await(Run);',
  34691. ' Result:=await(Run());',
  34692. ' Result:=await(Run) as IUnknown;',
  34693. ' Result:=await(Say(nil));',
  34694. ' Result:=await(Say(await(Run())));',
  34695. ' Result:=await(Say(await(Run()) as IUnknown));',
  34696. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34697. 'end;',
  34698. 'procedure Fly;',
  34699. 'var p: TJSPromise;',
  34700. 'begin',
  34701. ' Run;',
  34702. ' Run();',
  34703. ' p:=Run;',
  34704. ' p:=Run();',
  34705. 'end;',
  34706. 'begin',
  34707. ' ']);
  34708. ConvertProgram;
  34709. CheckSource('TestAsync_ClassInterface',
  34710. LinesToStr([ // statements
  34711. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34712. 'this.Say = async function (i) {',
  34713. ' var Result = null;',
  34714. ' return Result;',
  34715. '};',
  34716. 'this.Run = async function () {',
  34717. ' var Result = null;',
  34718. ' var $ok = false;',
  34719. ' try {',
  34720. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34721. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34722. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34723. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34724. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34725. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34726. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34727. ' $ok = true;',
  34728. ' } finally {',
  34729. ' if (!$ok) rtl._Release(Result);',
  34730. ' };',
  34731. ' return Result;',
  34732. '};',
  34733. 'this.Fly = function () {',
  34734. ' var p = null;',
  34735. ' $mod.Run();',
  34736. ' $mod.Run();',
  34737. ' p = $mod.Run();',
  34738. ' p = $mod.Run();',
  34739. '};',
  34740. '']),
  34741. LinesToStr([
  34742. '']));
  34743. CheckResolverUnexpectedHints();
  34744. end;
  34745. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34746. begin
  34747. StartProgram(true,[supTInterfacedObject]);
  34748. Add([
  34749. '{$mode objfpc}',
  34750. '{$modeswitch externalclass}',
  34751. 'type',
  34752. ' TJSPromise = class external name ''Promise''',
  34753. ' end;',
  34754. ' IBird = interface',
  34755. ' procedure Run;',
  34756. ' end;',
  34757. ' TBird = class(TInterfacedObject,IBird)',
  34758. ' procedure Run; async;',
  34759. ' end;',
  34760. 'procedure TBird.Run;',
  34761. 'begin',
  34762. 'end;',
  34763. 'begin',
  34764. ' ']);
  34765. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34766. ConvertProgram;
  34767. end;
  34768. procedure TTestModule.TestAWait_ClassAs;
  34769. begin
  34770. StartProgram(false);
  34771. Add([
  34772. '{$mode objfpc}',
  34773. '{$modeswitch externalclass}',
  34774. 'type',
  34775. ' TJSPromise = class external name ''Promise''',
  34776. ' end;',
  34777. ' TObject = class',
  34778. ' function Run: TObject; async;',
  34779. ' end;',
  34780. ' TBird = class',
  34781. ' function Fly: TBird; async;',
  34782. ' end;',
  34783. 'function TObject.Run: TObject; async;',
  34784. 'begin',
  34785. 'end;',
  34786. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34787. 'var o: TObject;',
  34788. 'begin',
  34789. ' o:=await(TObject,Run);',
  34790. ' o:=await(TObject,Fly);',
  34791. ' o:=await(TBird,Fly);',
  34792. ' o:=await(TObject,inherited Run);',
  34793. ' o:=await(TObject,inherited Run) as TBird;',
  34794. 'end;',
  34795. 'begin',
  34796. ' ']);
  34797. ConvertProgram;
  34798. CheckSource('TestAWait_ClassAs',
  34799. LinesToStr([ // statements
  34800. 'rtl.createClass(this, "TObject", null, function () {',
  34801. ' this.$init = function () {',
  34802. ' };',
  34803. ' this.$final = function () {',
  34804. ' };',
  34805. ' this.Run = async function () {',
  34806. ' var Result = null;',
  34807. ' return Result;',
  34808. ' };',
  34809. '});',
  34810. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34811. ' this.Fly = async function () {',
  34812. ' var Result = null;',
  34813. ' var o = null;',
  34814. ' o = await this.Run();',
  34815. ' o = await this.Fly();',
  34816. ' o = await this.Fly();',
  34817. ' o = await $mod.TObject.Run.call(this);',
  34818. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34819. ' return Result;',
  34820. ' };',
  34821. '});',
  34822. '']),
  34823. LinesToStr([
  34824. '']));
  34825. CheckResolverUnexpectedHints();
  34826. end;
  34827. procedure TTestModule.TestLibrary_Empty;
  34828. begin
  34829. StartLibrary(false);
  34830. Add([
  34831. '']);
  34832. ConvertLibrary;
  34833. CheckFullSource('TestLibrary_Empty',
  34834. LinesToStr([ // statements
  34835. 'rtl.module("library", [], function () {',
  34836. ' var $mod = this;',
  34837. ' $mod.$main = function () {',
  34838. ' };',
  34839. '});',
  34840. 'rtl.run("library");',
  34841. '']));
  34842. CheckResolverUnexpectedHints();
  34843. end;
  34844. procedure TTestModule.TestLibrary_ExportFunc;
  34845. begin
  34846. StartLibrary(false);
  34847. Add([
  34848. 'procedure Run(w: word);',
  34849. 'begin',
  34850. 'end;',
  34851. 'exports',
  34852. ' Run;',
  34853. ' run name ''Foo'';',
  34854. ' test1.run name ''Test1Run'';',
  34855. '']);
  34856. ConvertLibrary;
  34857. CheckFullSource('TestLibrary_ExportFunc',
  34858. LinesToStr([ // statements
  34859. 'rtl.module("library", [], function () {',
  34860. ' var $mod = this;',
  34861. ' this.Run = function (w) {',
  34862. ' };',
  34863. ' $mod.$main = function () {',
  34864. ' };',
  34865. '});',
  34866. 'rtl.run("library");',
  34867. 'export const Run = pas.library.Run;',
  34868. 'export const Foo = pas.library.Run;',
  34869. 'export const Test1Run = pas.library.Run;',
  34870. '']));
  34871. CheckResolverUnexpectedHints();
  34872. end;
  34873. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34874. begin
  34875. StartLibrary(false);
  34876. Add([
  34877. 'procedure Run(w: word); overload;',
  34878. 'begin',
  34879. 'end;',
  34880. 'procedure Run(s: string); overload;',
  34881. 'begin',
  34882. 'end;',
  34883. 'exports',
  34884. ' Run;',
  34885. '']);
  34886. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34887. nCantDetermineWhichOverloadedFunctionToCall);
  34888. ConvertLibrary;
  34889. end;
  34890. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34891. begin
  34892. StartLibrary(false);
  34893. Add([
  34894. 'procedure Run(w: word);',
  34895. 'begin',
  34896. 'end;',
  34897. 'exports',
  34898. ' Run index 3;',
  34899. '']);
  34900. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34901. ConvertLibrary;
  34902. end;
  34903. procedure TTestModule.TestLibrary_ExportVar;
  34904. begin
  34905. StartLibrary(false);
  34906. Add([
  34907. 'var Wing: word;',
  34908. 'exports',
  34909. ' Wing, wing name ''BirdArm'';',
  34910. '']);
  34911. ConvertLibrary;
  34912. CheckFullSource('TestLibrary_ExportVar',
  34913. LinesToStr([ // statements
  34914. 'rtl.module("library", [], function () {',
  34915. ' var $mod = this;',
  34916. ' this.Wing = 0;',
  34917. ' $mod.$main = function () {',
  34918. ' };',
  34919. '});',
  34920. 'rtl.run("library");',
  34921. 'export const vars = {};',
  34922. 'Object.defineProperties(vars, {',
  34923. ' Wing: {',
  34924. ' enumerable: true,',
  34925. ' get: function () {',
  34926. ' return pas.library.Wing;',
  34927. ' },',
  34928. ' set: function (v) {',
  34929. ' pas.library.Wing = v;',
  34930. ' }',
  34931. ' },',
  34932. ' BirdArm: {',
  34933. ' enumerable: true,',
  34934. ' get: function () {',
  34935. ' return pas.library.Wing;',
  34936. ' },',
  34937. ' set: function (v) {',
  34938. ' pas.library.Wing = v;',
  34939. ' }',
  34940. ' }',
  34941. '});',
  34942. '']));
  34943. CheckResolverUnexpectedHints();
  34944. end;
  34945. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34946. begin
  34947. AddModuleWithIntfImplSrc('Unit1.pas',
  34948. LinesToStr([
  34949. 'type',
  34950. ' TAnt = class',
  34951. ' class function Crawl: word; static;',
  34952. ' end;',
  34953. 'function Fly: word;',
  34954. '']),
  34955. LinesToStr([
  34956. 'function Fly: word;',
  34957. 'begin',
  34958. 'end;',
  34959. 'class function TAnt.Crawl: word;',
  34960. 'begin',
  34961. 'end;',
  34962. '']));
  34963. StartLibrary(true,[supTObject]);
  34964. Add([
  34965. 'uses unit1;',
  34966. 'exports',
  34967. ' Fly;',
  34968. ' TAnt.Crawl;',
  34969. '']);
  34970. ConvertLibrary;
  34971. CheckFullSource('TestLibrary_ExportUnitFunc',
  34972. LinesToStr([ // statements
  34973. 'rtl.module("library", ["system", "Unit1"], function () {',
  34974. ' var $mod = this;',
  34975. ' $mod.$main = function () {',
  34976. ' };',
  34977. '});',
  34978. 'rtl.run("library");',
  34979. 'export const Fly = pas.Unit1.Fly;',
  34980. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34981. '']));
  34982. CheckResolverUnexpectedHints();
  34983. end;
  34984. Initialization
  34985. RegisterTests([TTestModule]);
  34986. end.