tcmodules.pas 880 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FPasLibrary: TPasLibrary;
  110. FHintMsgs: TObjectList; // list of TTestHintMessage
  111. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  112. FJSRegModuleCall: TJSCallExpression;
  113. FScanner: TPas2jsPasScanner;
  114. FSkipTests: boolean;
  115. FSource: TStringList;
  116. FFirstPasStatement: TPasImplBlock;
  117. FWithTypeInfo: boolean;
  118. {$IFDEF EnablePasTreeGlobalRefCount}
  119. FElementRefCountAtSetup: int64;
  120. {$ENDIF}
  121. function GetMsgCount: integer;
  122. function GetMsgs(Index: integer): TTestHintMessage;
  123. function GetResolverCount: integer;
  124. function GetResolvers(Index: integer): TTestEnginePasResolver;
  125. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  126. procedure OnParserLog(Sender: TObject; const Msg: String);
  127. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  128. procedure OnScannerLog(Sender: TObject; const Msg: String);
  129. procedure SetWithTypeInfo(const AValue: boolean);
  130. protected
  131. procedure SetUp; override;
  132. function CreateConverter: TPasToJSConverter; virtual;
  133. function LoadUnit(const aUnitName: String): TPasModule;
  134. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  135. procedure TearDown; override;
  136. Procedure Add(Line: string); virtual;
  137. Procedure Add(const Lines: array of string);
  138. Procedure StartParsing; virtual;
  139. procedure ParseModuleQueue; virtual;
  140. procedure ParseModule; virtual;
  141. procedure ParseProgram; virtual;
  142. procedure ParseLibrary; virtual;
  143. procedure ParseUnit; virtual;
  144. protected
  145. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  146. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  149. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  150. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  151. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  152. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure ConvertModule; virtual;
  155. procedure ConvertProgram; virtual;
  156. procedure ConvertLibrary; virtual;
  157. procedure ConvertUnit; virtual;
  158. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  159. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  160. function GetDottedIdentifier(El: TJSElement): string;
  161. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  162. ImplStatements: string = ''); virtual;
  163. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  164. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  165. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  166. Msg: string; Marker: PSrcMarker = nil); virtual;
  167. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  168. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  169. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  172. function IsErrorExpected(E: Exception): boolean;
  173. procedure HandleScannerError(E: EScannerError);
  174. procedure HandleParserError(E: EParserError);
  175. procedure HandlePasResolveError(E: EPasResolve);
  176. procedure HandlePas2JSError(E: EPas2JS);
  177. procedure HandleException(E: Exception);
  178. procedure FailException(E: Exception);
  179. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  180. function IndexOfResolver(const Filename: string): integer;
  181. function GetResolver(const Filename: string): TTestEnginePasResolver;
  182. function GetDefaultNamespace: string;
  183. property PasProgram: TPasProgram Read FPasProgram;
  184. property PasLibrary: TPasLibrary Read FPasLibrary;
  185. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  186. property ResolverCount: integer read GetResolverCount;
  187. property Engine: TTestEnginePasResolver read FEngine;
  188. property Filename: string read FFilename;
  189. Property Module: TPasModule Read FModule;
  190. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  191. property Converter: TPasToJSConverter read FConverter;
  192. property JSSource: TStringList read FJSSource;
  193. property JSModule: TJSSourceElements read FJSModule;
  194. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  195. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  196. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  197. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  198. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  199. property JSInitBody: TJSFunctionBody read FJSInitBody;
  200. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  201. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  202. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  203. property SkipTests: boolean read FSkipTests write FSkipTests;
  204. public
  205. constructor Create; override;
  206. destructor Destroy; override;
  207. property Hub: TPas2JSResolverHub read FHub;
  208. property Source: TStringList read FSource;
  209. property FileResolver: TStreamResolver read FFileResolver;
  210. property Scanner: TPas2jsPasScanner read FScanner;
  211. property Parser: TTestPasParser read FParser;
  212. property MsgCount: integer read GetMsgCount;
  213. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  214. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  215. end;
  216. { TTestModule }
  217. TTestModule = class(TCustomTestModule)
  218. Published
  219. Procedure TestReservedWords;
  220. // program, units, includes
  221. Procedure TestEmptyProgram;
  222. Procedure TestEmptyProgramUseStrict;
  223. Procedure TestEmptyUnit;
  224. Procedure TestEmptyUnitUseStrict;
  225. Procedure TestDottedUnitNames;
  226. Procedure TestDottedUnitNameImpl;
  227. Procedure TestDottedUnitExpr;
  228. Procedure Test_ModeFPCFail;
  229. Procedure Test_ModeSwitchCBlocksFail;
  230. Procedure TestUnit_UseSystem;
  231. Procedure TestUnit_Intf1Impl2Intf1;
  232. Procedure TestIncludeVersion;
  233. // vars/const
  234. Procedure TestVarInt;
  235. Procedure TestVarBaseTypes;
  236. Procedure TestBaseTypeSingleFail;
  237. Procedure TestBaseTypeExtendedFail;
  238. Procedure TestConstBaseTypes;
  239. Procedure TestUnitImplVars;
  240. Procedure TestUnitImplConsts;
  241. Procedure TestUnitImplRecord;
  242. Procedure TestRenameJSNameConflict;
  243. Procedure TestLocalConst;
  244. Procedure TestVarExternal;
  245. Procedure TestVarExternalOtherUnit;
  246. Procedure TestVarAbsoluteFail;
  247. Procedure TestConstExternal;
  248. // numbers
  249. Procedure TestDouble;
  250. Procedure TestInteger;
  251. Procedure TestIntegerRange;
  252. Procedure TestIntegerTypecasts;
  253. Procedure TestInteger_BitwiseShrNativeInt;
  254. Procedure TestInteger_BitwiseShlNativeInt;
  255. Procedure TestInteger_SystemFunc;
  256. Procedure TestCurrency;
  257. Procedure TestForBoolDo;
  258. Procedure TestForIntDo;
  259. Procedure TestForIntInDo;
  260. // strings
  261. Procedure TestCharConst;
  262. Procedure TestChar_Compare;
  263. Procedure TestChar_BuiltInProcs;
  264. Procedure TestStringConst;
  265. Procedure TestStringConst_InvalidUTF16;
  266. Procedure TestStringConstSurrogate;
  267. Procedure TestString_Length;
  268. Procedure TestString_Compare;
  269. Procedure TestString_SetLength;
  270. Procedure TestString_CharAt;
  271. Procedure TestStringHMinusFail;
  272. Procedure TestStr;
  273. Procedure TestBaseType_AnsiStringFail;
  274. Procedure TestBaseType_WideStringFail;
  275. Procedure TestBaseType_ShortStringFail;
  276. Procedure TestBaseType_RawByteStringFail;
  277. Procedure TestTypeShortstring_Fail;
  278. Procedure TestCharSet_Custom;
  279. Procedure TestWideChar;
  280. Procedure TestForCharDo;
  281. Procedure TestForCharInDo;
  282. // alias types
  283. Procedure TestAliasTypeRef;
  284. Procedure TestTypeCast_BaseTypes;
  285. Procedure TestTypeCast_AliasBaseTypes;
  286. // functions
  287. Procedure TestEmptyProc;
  288. Procedure TestProcOneParam;
  289. Procedure TestFunctionWithoutParams;
  290. Procedure TestProcedureWithoutParams;
  291. Procedure TestPrgProcVar;
  292. Procedure TestProcTwoArgs;
  293. Procedure TestProc_DefaultValue;
  294. Procedure TestUnitProcVar;
  295. Procedure TestImplProc;
  296. Procedure TestFunctionResult;
  297. Procedure TestNestedProc;
  298. Procedure TestNestedProc_ResultString;
  299. Procedure TestForwardProc;
  300. Procedure TestNestedForwardProc;
  301. Procedure TestAssignFunctionResult;
  302. Procedure TestFunctionResultInCondition;
  303. Procedure TestFunctionResultInForLoop;
  304. Procedure TestFunctionResultInTypeCast;
  305. Procedure TestExit;
  306. Procedure TestExit_ResultInFinally;
  307. Procedure TestBreak;
  308. Procedure TestBreakAsVar;
  309. Procedure TestContinue;
  310. Procedure TestProc_External;
  311. Procedure TestProc_ExternalOtherUnit;
  312. Procedure TestProc_Asm;
  313. Procedure TestProc_AsmSubBlock;
  314. Procedure TestProc_Assembler;
  315. Procedure TestProc_VarParam;
  316. Procedure TestProc_VarParamString;
  317. Procedure TestProc_VarParamV;
  318. Procedure TestProc_Overload;
  319. Procedure TestProc_OverloadForward;
  320. Procedure TestProc_OverloadIntfImpl;
  321. Procedure TestProc_OverloadNested;
  322. Procedure TestProc_OverloadNestedForward;
  323. Procedure TestProc_OverloadUnitCycle;
  324. Procedure TestProc_Varargs;
  325. Procedure TestProc_ConstOrder;
  326. Procedure TestProc_DuplicateConst;
  327. Procedure TestProc_LocalVarAbsolute;
  328. Procedure TestProc_LocalVarInit;
  329. Procedure TestProc_ReservedWords;
  330. Procedure TestProc_ConstRefWord;
  331. // anonymous functions
  332. Procedure TestAnonymousProc_Assign_ObjFPC;
  333. Procedure TestAnonymousProc_Assign_Delphi;
  334. Procedure TestAnonymousProc_Arg;
  335. Procedure TestAnonymousProc_Typecast;
  336. Procedure TestAnonymousProc_With;
  337. Procedure TestAnonymousProc_ExceptOn;
  338. Procedure TestAnonymousProc_Nested;
  339. Procedure TestAnonymousProc_NestedAssignResult;
  340. Procedure TestAnonymousProc_Class;
  341. Procedure TestAnonymousProc_ForLoop;
  342. Procedure TestAnonymousProc_AsmDelphi;
  343. // enums, sets
  344. Procedure TestEnum_Name;
  345. Procedure TestEnum_Number;
  346. Procedure TestEnum_ConstFail;
  347. Procedure TestEnum_Functions;
  348. Procedure TestEnumRg_Functions;
  349. Procedure TestEnum_AsParams;
  350. Procedure TestEnumRange_Array;
  351. Procedure TestEnum_ForIn;
  352. Procedure TestEnum_ScopedNumber;
  353. Procedure TestEnum_InFunction;
  354. Procedure TestEnum_Name_Anonymous_Unit;
  355. Procedure TestSet_Enum;
  356. Procedure TestSet_Operators;
  357. Procedure TestSet_Operator_In;
  358. Procedure TestSet_Functions;
  359. Procedure TestSet_PassAsArgClone;
  360. Procedure TestSet_AsParams;
  361. Procedure TestSet_Property;
  362. Procedure TestSet_EnumConst;
  363. Procedure TestSet_IntConst;
  364. Procedure TestSet_IntRange;
  365. Procedure TestSet_AnonymousEnumType;
  366. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  367. Procedure TestSet_ConstEnum;
  368. Procedure TestSet_ConstChar;
  369. Procedure TestSet_ConstInt;
  370. Procedure TestSet_InFunction;
  371. Procedure TestSet_ForIn;
  372. // statements
  373. Procedure TestNestBegin;
  374. Procedure TestIncDec;
  375. Procedure TestLoHiFpcMode;
  376. Procedure TestLoHiDelphiMode;
  377. Procedure TestAssignments;
  378. Procedure TestArithmeticOperators1;
  379. Procedure TestMultiAdd;
  380. Procedure TestLogicalOperators;
  381. Procedure TestBitwiseOperators;
  382. Procedure TestBitwiseOperatorsLongword;
  383. Procedure TestFunctionInt;
  384. Procedure TestFunctionString;
  385. Procedure TestIfThen;
  386. Procedure TestForLoop;
  387. Procedure TestForLoopInsideFunction;
  388. Procedure TestForLoop_ReadVarAfter;
  389. Procedure TestForLoop_Nested;
  390. Procedure TestRepeatUntil;
  391. Procedure TestAsmBlock;
  392. Procedure TestAsmPas_Impl; // ToDo
  393. Procedure TestTryFinally;
  394. Procedure TestTryExcept;
  395. Procedure TestTryExcept_ReservedWords;
  396. Procedure TestIfThenRaiseElse;
  397. Procedure TestCaseOf;
  398. Procedure TestCaseOf_UseSwitch;
  399. Procedure TestCaseOfNoElse;
  400. Procedure TestCaseOfNoElse_UseSwitch;
  401. Procedure TestCaseOfRange;
  402. Procedure TestCaseOfString;
  403. Procedure TestCaseOfChar;
  404. Procedure TestCaseOfExternalClassConst;
  405. Procedure TestDebugger;
  406. // arrays
  407. Procedure TestArray_Dynamic;
  408. Procedure TestArray_Dynamic_Nil;
  409. Procedure TestArray_DynMultiDimensional;
  410. Procedure TestArray_DynamicAssign;
  411. Procedure TestArray_StaticInt;
  412. Procedure TestArray_StaticBool;
  413. Procedure TestArray_StaticChar;
  414. Procedure TestArray_StaticMultiDim;
  415. Procedure TestArray_StaticInFunction;
  416. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  417. Procedure TestArrayOfRecord;
  418. Procedure TestArray_StaticRecord;
  419. Procedure TestArrayOfSet;
  420. Procedure TestArray_DynAsParam;
  421. Procedure TestArray_StaticAsParam;
  422. Procedure TestArrayElement_AsParams;
  423. Procedure TestArrayElementFromFuncResult_AsParams;
  424. Procedure TestArrayEnumTypeRange;
  425. Procedure TestArray_SetLengthOutArg;
  426. Procedure TestArray_SetLengthProperty;
  427. Procedure TestArray_SetLengthMultiDim;
  428. Procedure TestArray_SetLengthDynOfStatic;
  429. Procedure TestArray_OpenArrayOfString;
  430. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  431. Procedure TestArray_ConstRef;
  432. Procedure TestArray_Concat;
  433. Procedure TestArray_Copy;
  434. Procedure TestArray_InsertDelete;
  435. Procedure TestArray_DynArrayConstObjFPC;
  436. Procedure TestArray_DynArrayConstDelphi;
  437. Procedure TestArray_ArrayLitAsParam;
  438. Procedure TestArray_ArrayLitMultiDimAsParam;
  439. Procedure TestArray_ArrayLitStaticAsParam;
  440. Procedure TestArray_ForInArrOfString;
  441. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  442. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  443. Procedure TestArrayOfConst_TVarRec;
  444. Procedure TestArrayOfConst_PassBaseTypes;
  445. Procedure TestArrayOfConst_PassObj;
  446. // record
  447. Procedure TestRecord_Empty;
  448. Procedure TestRecord_Var;
  449. Procedure TestRecord_VarExternal;
  450. Procedure TestRecord_WithDo;
  451. Procedure TestRecord_Assign;
  452. Procedure TestRecord_AsParams;
  453. Procedure TestRecord_ConstRef;
  454. Procedure TestRecordElement_AsParams;
  455. Procedure TestRecordElementFromFuncResult_AsParams;
  456. Procedure TestRecordElementFromWith_AsParams;
  457. Procedure TestRecord_Equal;
  458. Procedure TestRecord_JSValue;
  459. Procedure TestRecord_VariantFail;
  460. Procedure TestRecord_FieldArray;
  461. Procedure TestRecord_Const;
  462. Procedure TestRecord_TypecastFail;
  463. Procedure TestRecord_InFunction;
  464. Procedure TestRecord_AnonymousFail;
  465. // advanced record
  466. Procedure TestAdvRecord_Function;
  467. Procedure TestAdvRecord_Property;
  468. Procedure TestAdvRecord_PropertyDefault;
  469. Procedure TestAdvRecord_Property_ClassMethod;
  470. Procedure TestAdvRecord_Const;
  471. Procedure TestAdvRecord_ExternalField;
  472. Procedure TestAdvRecord_SubRecord;
  473. Procedure TestAdvRecord_SubClass;
  474. Procedure TestAdvRecord_SubInterfaceFail;
  475. Procedure TestAdvRecord_Constructor;
  476. Procedure TestAdvRecord_ClassConstructor_Program;
  477. Procedure TestAdvRecord_ClassConstructor_Unit;
  478. // classes
  479. Procedure TestClass_TObjectDefaultConstructor;
  480. Procedure TestClass_TObjectConstructorWithParams;
  481. Procedure TestClass_TObjectConstructorWithDefaultParam;
  482. Procedure TestClass_Var;
  483. Procedure TestClass_Method;
  484. Procedure TestClass_Implementation;
  485. Procedure TestClass_Inheritance;
  486. Procedure TestClass_TypeAlias;
  487. Procedure TestClass_AbstractMethod;
  488. Procedure TestClass_CallInherited_ProcNoParams;
  489. Procedure TestClass_CallInherited_WithParams;
  490. Procedure TestClasS_CallInheritedConstructor;
  491. Procedure TestClass_ClassVar_Assign;
  492. Procedure TestClass_CallClassMethod;
  493. Procedure TestClass_CallClassMethodStatic; // ToDo
  494. Procedure TestClass_Property;
  495. Procedure TestClass_Property_ClassMethod;
  496. Procedure TestClass_Property_Indexed;
  497. Procedure TestClass_Property_IndexSpec;
  498. Procedure TestClass_PropertyOfTypeArray;
  499. Procedure TestClass_PropertyDefault;
  500. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  501. //Procedure TestClass_PropertyDefault;
  502. Procedure TestClass_PropertyOverride;
  503. Procedure TestClass_PropertyIncVisibility;
  504. Procedure TestClass_Assigned;
  505. Procedure TestClass_WithClassDoCreate;
  506. Procedure TestClass_WithClassInstDoProperty;
  507. Procedure TestClass_WithClassInstDoPropertyWithParams;
  508. Procedure TestClass_WithClassInstDoFunc;
  509. Procedure TestClass_TypeCast;
  510. Procedure TestClass_TypeCastUntypedParam;
  511. Procedure TestClass_Overloads;
  512. Procedure TestClass_OverloadsAncestor;
  513. Procedure TestClass_OverloadConstructor;
  514. Procedure TestClass_OverloadDelphiOverride;
  515. Procedure TestClass_ReintroduceVarDelphi;
  516. Procedure TestClass_ReintroducedVar;
  517. Procedure TestClass_RaiseDescendant;
  518. Procedure TestClass_ExternalMethod;
  519. Procedure TestClass_ExternalVirtualNameMismatchFail;
  520. Procedure TestClass_ExternalOverrideFail;
  521. Procedure TestClass_ExternalVar;
  522. Procedure TestClass_Const;
  523. Procedure TestClass_ConstEnum;
  524. Procedure TestClass_LocalConstDuplicate_Prg;
  525. Procedure TestClass_LocalConstDuplicate_Unit;
  526. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  527. Procedure TestClass_LocalVarSelfFail;
  528. Procedure TestClass_ArgSelfFail;
  529. Procedure TestClass_NestedProcSelf;
  530. Procedure TestClass_NestedProcSelf2;
  531. Procedure TestClass_NestedProcClassSelf;
  532. Procedure TestClass_NestedProcCallInherited;
  533. Procedure TestClass_TObjectFree;
  534. Procedure TestClass_TObjectFree_VarArg;
  535. Procedure TestClass_TObjectFreeNewInstance;
  536. Procedure TestClass_TObjectFreeLowerCase;
  537. Procedure TestClass_TObjectFreeFunctionFail;
  538. Procedure TestClass_TObjectFreePropertyFail;
  539. Procedure TestClass_ForIn;
  540. Procedure TestClass_DispatchMessage;
  541. Procedure TestClass_Message_DuplicateIntFail;
  542. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  543. // class of
  544. Procedure TestClassOf_Create;
  545. Procedure TestClassOf_Call;
  546. Procedure TestClassOf_Assign;
  547. Procedure TestClassOf_Is;
  548. Procedure TestClassOf_Compare;
  549. Procedure TestClassOf_ClassVar;
  550. Procedure TestClassOf_ClassMethod;
  551. Procedure TestClassOf_ClassProperty;
  552. Procedure TestClassOf_ClassMethodSelf;
  553. Procedure TestClassOf_TypeCast;
  554. Procedure TestClassOf_ImplicitFunctionCall;
  555. Procedure TestClassOf_Const;
  556. // nested class
  557. Procedure TestNestedClass_Alias;
  558. Procedure TestNestedClass_Record;
  559. Procedure TestNestedClass_Class;
  560. // external class
  561. Procedure TestExternalClass_Var;
  562. Procedure TestExternalClass_Const;
  563. Procedure TestExternalClass_Dollar;
  564. Procedure TestExternalClass_DuplicateVarFail;
  565. Procedure TestExternalClass_Method;
  566. Procedure TestExternalClass_ClassMethod;
  567. Procedure TestExternalClass_ClassMethodStatic;
  568. Procedure TestExternalClass_FunctionResultInTypeCast;
  569. Procedure TestExternalClass_NonExternalOverride;
  570. Procedure TestExternalClass_OverloadHint;
  571. Procedure TestExternalClass_SameNamePublishedProperty;
  572. Procedure TestExternalClass_Property;
  573. Procedure TestExternalClass_PropertyDate;
  574. Procedure TestExternalClass_ClassProperty;
  575. Procedure TestExternalClass_ClassOf;
  576. Procedure TestExternalClass_ClassOtherUnit;
  577. Procedure TestExternalClass_Is;
  578. Procedure TestExternalClass_As;
  579. Procedure TestExternalClass_DestructorFail;
  580. Procedure TestExternalClass_New;
  581. Procedure TestExternalClass_ClassOf_New;
  582. Procedure TestExternalClass_FuncClassOf_New;
  583. Procedure TestExternalClass_New_PasClassFail;
  584. Procedure TestExternalClass_New_PasClassBracketsFail;
  585. Procedure TestExternalClass_NewExtName;
  586. Procedure TestExternalClass_Constructor;
  587. Procedure TestExternalClass_ConstructorBrackets;
  588. Procedure TestExternalClass_LocalConstSameName;
  589. Procedure TestExternalClass_ReintroduceOverload;
  590. Procedure TestExternalClass_Inherited;
  591. Procedure TestExternalClass_PascalAncestorFail;
  592. Procedure TestExternalClass_NewInstance;
  593. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  594. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  595. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  596. Procedure TestExternalClass_JSFunctionPasDescendant;
  597. Procedure TestExternalClass_PascalProperty;
  598. Procedure TestExternalClass_TypeCastToRootClass;
  599. Procedure TestExternalClass_TypeCastToJSObject;
  600. Procedure TestExternalClass_TypeCastStringToExternalString;
  601. Procedure TestExternalClass_TypeCastToJSFunction;
  602. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  603. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  604. Procedure TestExternalClass_BracketAccessor;
  605. Procedure TestExternalClass_BracketAccessor_Call;
  606. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  607. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  608. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  609. Procedure TestExternalClass_BracketAccessor_MultiType;
  610. Procedure TestExternalClass_BracketAccessor_Index;
  611. Procedure TestExternalClass_ForInJSObject;
  612. Procedure TestExternalClass_ForInJSArray;
  613. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  614. // class interfaces
  615. Procedure TestClassInterface_Corba;
  616. Procedure TestClassInterface_ProcExternalFail;
  617. Procedure TestClassInterface_Overloads;
  618. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  619. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  620. Procedure TestClassInterface_AncestorImpl;
  621. Procedure TestClassInterface_ImplReintroduce;
  622. Procedure TestClassInterface_MethodResolution;
  623. Procedure TestClassInterface_AncestorMoreInterfaces;
  624. Procedure TestClassInterface_MethodOverride;
  625. Procedure TestClassInterface_Corba_Delegation;
  626. Procedure TestClassInterface_Corba_DelegationStatic;
  627. Procedure TestClassInterface_Corba_Operators;
  628. Procedure TestClassInterface_Corba_Args;
  629. Procedure TestClassInterface_Corba_ForIn;
  630. Procedure TestClassInterface_COM_AssignVar;
  631. Procedure TestClassInterface_COM_AssignArg;
  632. Procedure TestClassInterface_COM_FunctionResult;
  633. Procedure TestClassInterface_COM_InheritedFuncResult;
  634. Procedure TestClassInterface_COM_IsAsTypeCasts;
  635. Procedure TestClassInterface_COM_PassAsArg;
  636. Procedure TestClassInterface_COM_PassToUntypedParam;
  637. Procedure TestClassInterface_COM_FunctionInExpr;
  638. Procedure TestClassInterface_COM_Property;
  639. Procedure TestClassInterface_COM_IntfProperty;
  640. Procedure TestClassInterface_COM_Delegation;
  641. Procedure TestClassInterface_COM_With;
  642. Procedure TestClassInterface_COM_ForIn;
  643. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  644. Procedure TestClassInterface_COM_RecordIntfFail;
  645. Procedure TestClassInterface_COM_UnitInitialization;
  646. Procedure TestClassInterface_GUID;
  647. Procedure TestClassInterface_GUIDProperty;
  648. // helpers
  649. Procedure TestClassHelper_ClassVar;
  650. Procedure TestClassHelper_Method_AccessInstanceFields;
  651. Procedure TestClassHelper_Method_Call;
  652. Procedure TestClassHelper_Method_Nested_Call;
  653. Procedure TestClassHelper_ClassMethod_Call;
  654. Procedure TestClassHelper_ClassOf;
  655. Procedure TestClassHelper_MethodRefObjFPC;
  656. Procedure TestClassHelper_Constructor;
  657. Procedure TestClassHelper_InheritedObjFPC;
  658. Procedure TestClassHelper_Property;
  659. Procedure TestClassHelper_Property_Array;
  660. Procedure TestClassHelper_Property_Array_Default;
  661. Procedure TestClassHelper_Property_Array_DefaultDefault;
  662. Procedure TestClassHelper_ClassProperty;
  663. Procedure TestClassHelper_ClassPropertyStatic;
  664. Procedure TestClassHelper_ClassProperty_Array;
  665. Procedure TestClassHelper_ForIn;
  666. Procedure TestClassHelper_PassProperty;
  667. Procedure TestExtClassHelper_ClassVar;
  668. Procedure TestExtClassHelper_Method_Call;
  669. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  670. Procedure TestRecordHelper_ClassVar;
  671. Procedure TestRecordHelper_Method_Call;
  672. Procedure TestRecordHelper_Constructor;
  673. Procedure TestTypeHelper_ClassVar;
  674. Procedure TestTypeHelper_PassResultElement;
  675. Procedure TestTypeHelper_PassArgs;
  676. Procedure TestTypeHelper_PassVarConst;
  677. Procedure TestTypeHelper_PassFuncResult;
  678. Procedure TestTypeHelper_PassPropertyField;
  679. Procedure TestTypeHelper_PassPropertyGetter;
  680. Procedure TestTypeHelper_PassClassPropertyField;
  681. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  682. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  683. Procedure TestTypeHelper_Property;
  684. Procedure TestTypeHelper_Property_Array;
  685. Procedure TestTypeHelper_ClassProperty;
  686. Procedure TestTypeHelper_ClassProperty_Array;
  687. Procedure TestTypeHelper_ClassMethod;
  688. Procedure TestTypeHelper_ExtClassMethodFail;
  689. Procedure TestTypeHelper_Constructor;
  690. Procedure TestTypeHelper_Word;
  691. Procedure TestTypeHelper_Boolean;
  692. Procedure TestTypeHelper_WordBool;
  693. Procedure TestTypeHelper_Double;
  694. Procedure TestTypeHelper_NativeInt;
  695. Procedure TestTypeHelper_StringChar;
  696. Procedure TestTypeHelper_JSValue;
  697. Procedure TestTypeHelper_Array;
  698. Procedure TestTypeHelper_EnumType;
  699. Procedure TestTypeHelper_SetType;
  700. Procedure TestTypeHelper_InterfaceType;
  701. Procedure TestTypeHelper_NestedSelf;
  702. // proc types
  703. Procedure TestProcType;
  704. Procedure TestProcType_Arg;
  705. Procedure TestProcType_FunctionFPC;
  706. Procedure TestProcType_FunctionDelphi;
  707. Procedure TestProcType_ProcedureDelphi;
  708. Procedure TestProcType_AsParam;
  709. Procedure TestProcType_MethodFPC;
  710. Procedure TestProcType_MethodDelphi;
  711. Procedure TestProcType_PropertyFPC;
  712. Procedure TestProcType_PropertyDelphi;
  713. Procedure TestProcType_WithClassInstDoPropertyFPC;
  714. Procedure TestProcType_Nested;
  715. Procedure TestProcType_NestedOfObject;
  716. Procedure TestProcType_ReferenceToProc;
  717. Procedure TestProcType_ReferenceToMethod;
  718. Procedure TestProcType_Typecast;
  719. Procedure TestProcType_PassProcToUntyped;
  720. Procedure TestProcType_PassProcToArray;
  721. Procedure TestProcType_SafeCallObjFPC;
  722. Procedure TestProcType_SafeCallDelphi;
  723. // pointer
  724. Procedure TestPointer;
  725. Procedure TestPointer_Proc;
  726. Procedure TestPointer_AssignRecordFail;
  727. Procedure TestPointer_AssignStaticArrayFail;
  728. Procedure TestPointer_TypeCastJSValueToPointer;
  729. Procedure TestPointer_NonRecordFail;
  730. Procedure TestPointer_AnonymousArgTypeFail;
  731. Procedure TestPointer_AnonymousVarTypeFail;
  732. Procedure TestPointer_AnonymousResultTypeFail;
  733. Procedure TestPointer_AddrOperatorFail;
  734. Procedure TestPointer_ArrayParamsFail;
  735. Procedure TestPointer_PointerAddFail;
  736. Procedure TestPointer_IncPointerFail;
  737. Procedure TestPointer_Record;
  738. Procedure TestPointer_RecordArg;
  739. // jsvalue
  740. Procedure TestJSValue_AssignToJSValue;
  741. Procedure TestJSValue_TypeCastToBaseType;
  742. Procedure TestJSValue_TypecastToJSValue;
  743. Procedure TestJSValue_Equal;
  744. Procedure TestJSValue_If;
  745. Procedure TestJSValue_Not;
  746. Procedure TestJSValue_Enum;
  747. Procedure TestJSValue_ClassInstance;
  748. Procedure TestJSValue_ClassOf;
  749. Procedure TestJSValue_ArrayOfJSValue;
  750. Procedure TestJSValue_ArrayLit;
  751. Procedure TestJSValue_Params;
  752. Procedure TestJSValue_UntypedParam;
  753. Procedure TestJSValue_FuncResultType;
  754. Procedure TestJSValue_ProcType_Assign;
  755. Procedure TestJSValue_ProcType_Equal;
  756. Procedure TestJSValue_ProcType_Param;
  757. Procedure TestJSValue_AssignToPointerFail;
  758. Procedure TestJSValue_OverloadDouble;
  759. Procedure TestJSValue_OverloadNativeInt;
  760. Procedure TestJSValue_OverloadWord;
  761. Procedure TestJSValue_OverloadString;
  762. Procedure TestJSValue_OverloadChar;
  763. Procedure TestJSValue_OverloadPointer;
  764. Procedure TestJSValue_ForIn;
  765. // RTTI
  766. Procedure TestRTTI_IntRange;
  767. Procedure TestRTTI_Double;
  768. Procedure TestRTTI_ProcType;
  769. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  770. Procedure TestRTTI_EnumAndSetType;
  771. Procedure TestRTTI_EnumRange;
  772. Procedure TestRTTI_AnonymousEnumType;
  773. Procedure TestRTTI_StaticArray;
  774. Procedure TestRTTI_DynArray;
  775. Procedure TestRTTI_ArrayNestedAnonymous;
  776. Procedure TestRTTI_PublishedMethodOverloadFail;
  777. Procedure TestRTTI_PublishedMethodHideNoHint;
  778. Procedure TestRTTI_PublishedMethodExternalFail;
  779. Procedure TestRTTI_PublishedClassPropertyFail;
  780. Procedure TestRTTI_PublishedClassFieldFail;
  781. Procedure TestRTTI_PublishedFieldExternalFail;
  782. Procedure TestRTTI_Class_Field;
  783. Procedure TestRTTI_Class_Method;
  784. Procedure TestRTTI_Class_MethodArgFlags;
  785. Procedure TestRTTI_Class_Property;
  786. Procedure TestRTTI_Class_PropertyParams;
  787. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  788. Procedure TestRTTI_Class_OmitRTTI;
  789. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  790. Procedure TestRTTI_IndexModifier;
  791. Procedure TestRTTI_StoredModifier;
  792. Procedure TestRTTI_DefaultValue;
  793. Procedure TestRTTI_DefaultValueSet;
  794. Procedure TestRTTI_DefaultValueRangeType;
  795. Procedure TestRTTI_DefaultValueInherit;
  796. Procedure TestRTTI_OverrideMethod;
  797. Procedure TestRTTI_ReintroduceMethod;
  798. Procedure TestRTTI_OverloadProperty;
  799. // ToDo: array argument
  800. Procedure TestRTTI_ClassForward;
  801. Procedure TestRTTI_ClassOf;
  802. Procedure TestRTTI_Record;
  803. Procedure TestRTTI_RecordAnonymousArray;
  804. Procedure TestRTTI_Record_ClassVarType;
  805. Procedure TestRTTI_LocalTypes;
  806. Procedure TestRTTI_TypeInfo_BaseTypes;
  807. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  808. Procedure TestRTTI_TypeInfo_LocalFail;
  809. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  810. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  811. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  812. Procedure TestRTTI_TypeInfo_FunctionClassType;
  813. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  814. Procedure TestRTTI_Interface_Corba;
  815. Procedure TestRTTI_Interface_COM;
  816. Procedure TestRTTI_ClassHelper;
  817. Procedure TestRTTI_ExternalClass;
  818. Procedure TestRTTI_Unit;
  819. // Resourcestring
  820. Procedure TestResourcestringProgram;
  821. Procedure TestResourcestringUnit;
  822. Procedure TestResourcestringImplementation;
  823. // Attributes
  824. Procedure TestAttributes_Members;
  825. Procedure TestAttributes_Types;
  826. Procedure TestAttributes_HelperConstructor_Fail;
  827. // Assertions, checks
  828. procedure TestAssert;
  829. procedure TestAssert_SysUtils;
  830. procedure TestObjectChecks;
  831. procedure TestOverflowChecks_Int;
  832. procedure TestRangeChecks_AssignInt;
  833. procedure TestRangeChecks_AssignIntRange;
  834. procedure TestRangeChecks_AssignEnum;
  835. procedure TestRangeChecks_AssignEnumRange;
  836. procedure TestRangeChecks_AssignChar;
  837. procedure TestRangeChecks_AssignCharRange;
  838. procedure TestRangeChecks_ArrayIndex;
  839. procedure TestRangeChecks_ArrayOfRecIndex;
  840. procedure TestRangeChecks_StringIndex;
  841. procedure TestRangeChecks_TypecastInt;
  842. procedure TestRangeChecks_TypeHelperInt;
  843. // Async/AWait
  844. Procedure TestAsync_Proc;
  845. Procedure TestAsync_CallResultIsPromise;
  846. Procedure TestAsync_ConstructorFail;
  847. Procedure TestAsync_PropertyGetterFail;
  848. Procedure TestAwait_NonPromiseWithTypeFail;
  849. Procedure TestAwait_AsyncCallTypeMismatch;
  850. Procedure TestAWait_OutsideAsyncFail;
  851. Procedure TestAWait_IntegerFail;
  852. Procedure TestAWait_ExternalClassPromise;
  853. Procedure TestAWait_JSValue;
  854. Procedure TestAWait_Result;
  855. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  856. Procedure TestAsync_AnonymousProc;
  857. Procedure TestAsync_ProcType;
  858. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  859. Procedure TestAsync_Inherited;
  860. Procedure TestAsync_ClassInterface;
  861. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  862. // Library
  863. Procedure TestLibrary_Empty;
  864. Procedure TestLibrary_ExportFunc; // ToDo
  865. // ToDo: test delayed specialization init
  866. // ToDO: analyzer
  867. end;
  868. function LinesToStr(Args: array of const): string;
  869. function ExtractFileUnitName(aFilename: string): string;
  870. function JSToStr(El: TJSElement): string;
  871. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  872. implementation
  873. function LinesToStr(Args: array of const): string;
  874. var
  875. s: String;
  876. i: Integer;
  877. begin
  878. s:='';
  879. for i:=Low(Args) to High(Args) do
  880. case Args[i].VType of
  881. vtChar: s += Args[i].VChar+LineEnding;
  882. vtString: s += Args[i].VString^+LineEnding;
  883. vtPChar: s += Args[i].VPChar+LineEnding;
  884. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  885. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  886. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  887. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  888. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  889. end;
  890. Result:=s;
  891. end;
  892. function ExtractFileUnitName(aFilename: string): string;
  893. var
  894. p: Integer;
  895. begin
  896. Result:=ExtractFileName(aFilename);
  897. if Result='' then exit;
  898. for p:=length(Result) downto 1 do
  899. case Result[p] of
  900. '/','\': exit;
  901. '.':
  902. begin
  903. Delete(Result,p,length(Result));
  904. exit;
  905. end;
  906. end;
  907. end;
  908. function JSToStr(El: TJSElement): string;
  909. var
  910. aWriter: TBufferWriter;
  911. aJSWriter: TJSWriter;
  912. begin
  913. aJSWriter:=nil;
  914. aWriter:=TBufferWriter.Create(1000);
  915. try
  916. aJSWriter:=TJSWriter.Create(aWriter);
  917. aJSWriter.IndentSize:=2;
  918. aJSWriter.WriteJS(El);
  919. Result:=aWriter.AsString;
  920. finally
  921. aJSWriter.Free;
  922. aWriter.Free;
  923. end;
  924. end;
  925. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  926. // search diff, ignore changes in spaces
  927. const
  928. SpaceChars = [#9,#10,#13,' '];
  929. var
  930. ExpectedP, ActualP: PChar;
  931. function FindLineEnd(p: PChar): PChar;
  932. begin
  933. Result:=p;
  934. while not (Result^ in [#0,#10,#13]) do inc(Result);
  935. end;
  936. function FindLineStart(p, MinP: PChar): PChar;
  937. begin
  938. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  939. Result:=p;
  940. end;
  941. procedure SkipLineEnd(var p: PChar);
  942. begin
  943. if p^ in [#10,#13] then
  944. begin
  945. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  946. inc(p,2)
  947. else
  948. inc(p);
  949. end;
  950. end;
  951. function HasSpecialChar(s: string): boolean;
  952. var
  953. i: Integer;
  954. begin
  955. for i:=1 to length(s) do
  956. if s[i] in [#0..#31,#127..#255] then
  957. exit(true);
  958. Result:=false;
  959. end;
  960. function HashSpecialChars(s: string): string;
  961. var
  962. i: Integer;
  963. begin
  964. Result:='';
  965. for i:=1 to length(s) do
  966. if s[i] in [#0..#31,#127..#255] then
  967. Result:=Result+'#'+hexstr(ord(s[i]),2)
  968. else
  969. Result:=Result+s[i];
  970. end;
  971. procedure DiffFound;
  972. var
  973. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  974. ExpLine, ActLine: String;
  975. i, LineNo, DiffLineNo: Integer;
  976. begin
  977. writeln('Diff found "',Msg,'". Lines:');
  978. // write correct lines
  979. p:=PChar(Expected);
  980. LineNo:=0;
  981. DiffLineNo:=0;
  982. repeat
  983. StartPos:=p;
  984. while not (p^ in [#0,#10,#13]) do inc(p);
  985. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  986. SkipLineEnd(p);
  987. inc(LineNo);
  988. if (p<=ExpectedP) and (p^<>#0) then
  989. begin
  990. writeln('= ',ExpLine);
  991. end else begin
  992. // diff line
  993. if DiffLineNo=0 then DiffLineNo:=LineNo;
  994. // write actual line
  995. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  996. ActLineEndP:=FindLineEnd(ActualP);
  997. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  998. writeln('- ',ActLine);
  999. if HasSpecialChar(ActLine) then
  1000. writeln('- ',HashSpecialChars(ActLine));
  1001. // write expected line
  1002. writeln('+ ',ExpLine);
  1003. if HasSpecialChar(ExpLine) then
  1004. writeln('- ',HashSpecialChars(ExpLine));
  1005. // write empty line with pointer ^
  1006. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1007. writeln('^');
  1008. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1009. CheckSrcDiff:=false;
  1010. // write up to three following actual lines to get some context
  1011. for i:=1 to 3 do begin
  1012. ActLineStartP:=ActLineEndP;
  1013. SkipLineEnd(ActLineStartP);
  1014. if ActLineStartP^=#0 then break;
  1015. ActLineEndP:=FindLineEnd(ActLineStartP);
  1016. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1017. writeln('~ ',ActLine);
  1018. end;
  1019. exit;
  1020. end;
  1021. until p^=#0;
  1022. writeln('DiffFound Actual:-----------------------');
  1023. writeln(Actual);
  1024. writeln('DiffFound Expected:---------------------');
  1025. writeln(Expected);
  1026. writeln('DiffFound ------------------------------');
  1027. Msg:='diff found, but lines are the same, internal error';
  1028. CheckSrcDiff:=false;
  1029. end;
  1030. var
  1031. IsSpaceNeeded: Boolean;
  1032. LastChar, Quote: Char;
  1033. begin
  1034. Result:=true;
  1035. Msg:='';
  1036. if Expected='' then Expected:=' ';
  1037. if Actual='' then Actual:=' ';
  1038. ExpectedP:=PChar(Expected);
  1039. ActualP:=PChar(Actual);
  1040. repeat
  1041. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1042. case ExpectedP^ of
  1043. #0:
  1044. begin
  1045. // check that rest of Actual has only spaces
  1046. while ActualP^ in SpaceChars do inc(ActualP);
  1047. if ActualP^<>#0 then
  1048. begin
  1049. DiffFound;
  1050. exit;
  1051. end;
  1052. exit(true);
  1053. end;
  1054. ' ',#9,#10,#13:
  1055. begin
  1056. // skip space in Expected
  1057. IsSpaceNeeded:=false;
  1058. if ExpectedP>PChar(Expected) then
  1059. LastChar:=ExpectedP[-1]
  1060. else
  1061. LastChar:=#0;
  1062. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1063. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1064. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1065. IsSpaceNeeded:=true;
  1066. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1067. begin
  1068. DiffFound;
  1069. exit;
  1070. end;
  1071. while ActualP^ in SpaceChars do inc(ActualP);
  1072. end;
  1073. '''','"':
  1074. begin
  1075. while ActualP^ in SpaceChars do inc(ActualP);
  1076. if ExpectedP^<>ActualP^ then
  1077. begin
  1078. DiffFound;
  1079. exit;
  1080. end;
  1081. Quote:=ExpectedP^;
  1082. repeat
  1083. inc(ExpectedP);
  1084. inc(ActualP);
  1085. if ExpectedP^<>ActualP^ then
  1086. begin
  1087. DiffFound;
  1088. exit;
  1089. end;
  1090. if (ExpectedP^ in [#0,#10,#13]) then
  1091. break
  1092. else if (ExpectedP^=Quote) then
  1093. begin
  1094. inc(ExpectedP);
  1095. inc(ActualP);
  1096. break;
  1097. end;
  1098. until false;
  1099. end;
  1100. else
  1101. while ActualP^ in SpaceChars do inc(ActualP);
  1102. if ExpectedP^<>ActualP^ then
  1103. begin
  1104. DiffFound;
  1105. exit;
  1106. end;
  1107. inc(ExpectedP);
  1108. inc(ActualP);
  1109. end;
  1110. until false;
  1111. end;
  1112. { TTestEnginePasResolver }
  1113. destructor TTestEnginePasResolver.Destroy;
  1114. begin
  1115. FreeAndNil(FStreamResolver);
  1116. FreeAndNil(FParser);
  1117. FreeAndNil(FScanner);
  1118. FreeAndNil(FStreamResolver);
  1119. if Module<>nil then
  1120. begin
  1121. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1122. FModule:=nil;
  1123. end;
  1124. inherited Destroy;
  1125. end;
  1126. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1127. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1128. begin
  1129. Result:=nil;
  1130. if InFilename<>'' then
  1131. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1132. if Assigned(OnFindUnit) then
  1133. Result:=OnFindUnit(AName);
  1134. if NameExpr=nil then ;
  1135. end;
  1136. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1137. begin
  1138. // do not parse recursively
  1139. // parse via the queue
  1140. if Section=nil then ;
  1141. end;
  1142. { TCustomTestModule }
  1143. function TCustomTestModule.GetMsgCount: integer;
  1144. begin
  1145. Result:=FHintMsgs.Count;
  1146. end;
  1147. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1148. begin
  1149. Result:=TTestHintMessage(FHintMsgs[Index]);
  1150. end;
  1151. function TCustomTestModule.GetResolverCount: integer;
  1152. begin
  1153. Result:=FModules.Count;
  1154. end;
  1155. function TCustomTestModule.GetResolvers(Index: integer
  1156. ): TTestEnginePasResolver;
  1157. begin
  1158. Result:=TTestEnginePasResolver(FModules[Index]);
  1159. end;
  1160. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1161. ): TPasModule;
  1162. var
  1163. DefNamespace: String;
  1164. begin
  1165. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1166. if (Pos('.',aUnitName)<1) then
  1167. begin
  1168. DefNamespace:=GetDefaultNamespace;
  1169. if DefNamespace<>'' then
  1170. begin
  1171. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1172. if Result<>nil then exit;
  1173. end;
  1174. end;
  1175. Result:=LoadUnit(aUnitName);
  1176. if Result<>nil then exit;
  1177. {$IFDEF VerbosePas2JS}
  1178. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1179. {$ENDIF}
  1180. Fail('can''t find unit "'+aUnitName+'"');
  1181. end;
  1182. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1183. var
  1184. aParser: TPasParser;
  1185. Item: TTestHintMessage;
  1186. begin
  1187. aParser:=Sender as TPasParser;
  1188. Item:=TTestHintMessage.Create;
  1189. Item.Id:=aParser.LastMsgNumber;
  1190. Item.MsgType:=aParser.LastMsgType;
  1191. Item.MsgNumber:=aParser.LastMsgNumber;
  1192. Item.Msg:=Msg;
  1193. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1194. {$IFDEF VerbosePas2JS}
  1195. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1196. {$ENDIF}
  1197. FHintMsgs.Add(Item);
  1198. end;
  1199. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1200. );
  1201. var
  1202. aResolver: TTestEnginePasResolver;
  1203. Item: TTestHintMessage;
  1204. begin
  1205. aResolver:=Sender as TTestEnginePasResolver;
  1206. Item:=TTestHintMessage.Create;
  1207. Item.Id:=aResolver.LastMsgId;
  1208. Item.MsgType:=aResolver.LastMsgType;
  1209. Item.MsgNumber:=aResolver.LastMsgNumber;
  1210. Item.Msg:=Msg;
  1211. Item.SourcePos:=aResolver.LastSourcePos;
  1212. {$IFDEF VerbosePas2JS}
  1213. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1214. {$ENDIF}
  1215. FHintMsgs.Add(Item);
  1216. end;
  1217. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1218. var
  1219. Item: TTestHintMessage;
  1220. aScanner: TPas2jsPasScanner;
  1221. begin
  1222. aScanner:=Sender as TPas2jsPasScanner;
  1223. Item:=TTestHintMessage.Create;
  1224. Item.Id:=aScanner.LastMsgNumber;
  1225. Item.MsgType:=aScanner.LastMsgType;
  1226. Item.MsgNumber:=aScanner.LastMsgNumber;
  1227. Item.Msg:=Msg;
  1228. Item.SourcePos:=aScanner.CurSourcePos;
  1229. {$IFDEF VerbosePas2JS}
  1230. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1231. {$ENDIF}
  1232. FHintMsgs.Add(Item);
  1233. end;
  1234. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1235. begin
  1236. if FWithTypeInfo=AValue then Exit;
  1237. FWithTypeInfo:=AValue;
  1238. if AValue then
  1239. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1240. else
  1241. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1242. end;
  1243. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1244. var
  1245. i: Integer;
  1246. CurEngine: TTestEnginePasResolver;
  1247. CurUnitName: String;
  1248. begin
  1249. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1250. Result:=nil;
  1251. if (Module.ClassType=TPasModule)
  1252. and (CompareText(Module.Name,aUnitName)=0) then
  1253. exit(Module);
  1254. for i:=0 to ResolverCount-1 do
  1255. begin
  1256. CurEngine:=Resolvers[i];
  1257. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1258. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1259. if CompareText(aUnitName,CurUnitName)=0 then
  1260. begin
  1261. Result:=CurEngine.Module;
  1262. if Result<>nil then exit;
  1263. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1264. FileResolver.FindSourceFile(aUnitName);
  1265. CurEngine.StreamResolver:=TStreamResolver.Create;
  1266. CurEngine.StreamResolver.OwnsStreams:=True;
  1267. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1268. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1269. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1270. InitScanner(CurEngine.Scanner);
  1271. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1272. CurEngine.Parser.Options:=po_tcmodules;
  1273. if CompareText(CurUnitName,'System')=0 then
  1274. CurEngine.Parser.ImplicitUses.Clear;
  1275. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1276. try
  1277. CurEngine.Parser.NextToken;
  1278. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1279. except
  1280. on E: Exception do
  1281. HandleException(E);
  1282. end;
  1283. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1284. Result:=CurEngine.Module;
  1285. exit;
  1286. end;
  1287. end;
  1288. end;
  1289. procedure TCustomTestModule.SetUp;
  1290. begin
  1291. {$IFDEF EnablePasTreeGlobalRefCount}
  1292. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1293. {$ENDIF}
  1294. if FModules<>nil then
  1295. begin
  1296. writeln('TCustomTestModule.SetUp FModules<>nil');
  1297. Halt;
  1298. end;
  1299. inherited SetUp;
  1300. FSkipTests:=false;
  1301. FWithTypeInfo:=false;
  1302. FSource:=TStringList.Create;
  1303. FHub:=TPas2JSResolverHub.Create(Self);
  1304. FModules:=TObjectList.Create(true);
  1305. FFilename:='test1.pp';
  1306. FFileResolver:=TStreamResolver.Create;
  1307. FFileResolver.OwnsStreams:=True;
  1308. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1309. InitScanner(FScanner);
  1310. FEngine:=AddModule(Filename);
  1311. FEngine.Scanner:=FScanner;
  1312. FScanner.Resolver:=FEngine;
  1313. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1314. FParser.OnLog:=@OnParserLog;
  1315. FEngine.Parser:=FParser;
  1316. Parser.Options:=po_tcmodules;
  1317. FModule:=Nil;
  1318. FConverter:=CreateConverter;
  1319. FExpectedErrorClass:=nil;
  1320. end;
  1321. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1322. var
  1323. Options: TPasToJsConverterOptions;
  1324. begin
  1325. Result:=TPasToJSConverter.Create;
  1326. Options:=co_tcmodules;
  1327. if WithTypeInfo then
  1328. Exclude(Options,coNoTypeInfo)
  1329. else
  1330. Include(Options,coNoTypeInfo);
  1331. Result.Options:=Options;
  1332. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1333. end;
  1334. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1335. begin
  1336. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1337. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1338. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1339. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1340. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1341. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1342. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1343. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1344. aScanner.OnLog:=@OnScannerLog;
  1345. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1346. end;
  1347. procedure TCustomTestModule.TearDown;
  1348. {$IFDEF CheckPasTreeRefCount}
  1349. var
  1350. El: TPasElement;
  1351. {$ENDIF}
  1352. var
  1353. i: Integer;
  1354. CurModule: TPasModule;
  1355. begin
  1356. FHintMsgs.Clear;
  1357. FHintMsgsGood.Clear;
  1358. FSkipTests:=false;
  1359. FWithTypeInfo:=false;
  1360. FJSRegModuleCall:=nil;
  1361. FJSModuleCallArgs:=nil;
  1362. FJSImplentationUses:=nil;
  1363. FJSInterfaceUses:=nil;
  1364. FJSModuleSrc:=nil;
  1365. FJSInitBody:=nil;
  1366. FreeAndNil(FJSSource);
  1367. FreeAndNil(FJSModule);
  1368. FreeAndNil(FConverter);
  1369. Engine.Clear;
  1370. FreeAndNil(FSource);
  1371. FreeAndNil(FFileResolver);
  1372. if FModules<>nil then
  1373. begin
  1374. for i:=0 to FModules.Count-1 do
  1375. begin
  1376. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1377. if CurModule=nil then continue;
  1378. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1379. CurModule.ReleaseUsedUnits;
  1380. end;
  1381. if FModule<>nil then
  1382. FModule.ReleaseUsedUnits;
  1383. for i:=0 to FModules.Count-1 do
  1384. begin
  1385. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1386. if CurModule=nil then continue;
  1387. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1388. end;
  1389. FreeAndNil(FModules);
  1390. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1391. FEngine:=nil;
  1392. end;
  1393. FreeAndNil(FHub);
  1394. inherited TearDown;
  1395. {$IFDEF EnablePasTreeGlobalRefCount}
  1396. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1397. begin
  1398. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1399. {$IFDEF CheckPasTreeRefCount}
  1400. El:=TPasElement.FirstRefEl;
  1401. while El<>nil do
  1402. begin
  1403. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1404. for i:=0 to El.RefIds.Count-1 do
  1405. writeln(' ',El.RefIds[i]);
  1406. El:=El.NextRefEl;
  1407. end;
  1408. {$ENDIF}
  1409. Halt;
  1410. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1411. end;
  1412. {$ENDIF}
  1413. end;
  1414. procedure TCustomTestModule.Add(Line: string);
  1415. begin
  1416. Source.Add(Line);
  1417. end;
  1418. procedure TCustomTestModule.Add(const Lines: array of string);
  1419. var
  1420. i: Integer;
  1421. begin
  1422. for i:=low(Lines) to high(Lines) do
  1423. Add(Lines[i]);
  1424. end;
  1425. procedure TCustomTestModule.StartParsing;
  1426. var
  1427. Src: String;
  1428. begin
  1429. Src:=Source.Text;
  1430. FEngine.Source:=Src;
  1431. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1432. Scanner.OpenFile(FileName);
  1433. Writeln('// Test : ',Self.TestName);
  1434. Writeln(Src);
  1435. end;
  1436. procedure TCustomTestModule.ParseModuleQueue;
  1437. var
  1438. i: Integer;
  1439. CurResolver: TTestEnginePasResolver;
  1440. Found: Boolean;
  1441. Section: TPasSection;
  1442. begin
  1443. // parse til exception or all modules finished
  1444. while not SkipTests do
  1445. begin
  1446. Found:=false;
  1447. for i:=0 to ResolverCount-1 do
  1448. begin
  1449. CurResolver:=Resolvers[i];
  1450. if CurResolver.CurrentParser=nil then continue;
  1451. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1452. continue;
  1453. CurResolver.Parser.ParseContinue;
  1454. Found:=true;
  1455. break;
  1456. end;
  1457. if not Found then break;
  1458. end;
  1459. for i:=0 to ResolverCount-1 do
  1460. begin
  1461. CurResolver:=Resolvers[i];
  1462. if CurResolver.Parser=nil then
  1463. begin
  1464. if CurResolver.CurrentParser<>nil then
  1465. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1466. continue;
  1467. end;
  1468. if CurResolver.Parser.CurModule<>nil then
  1469. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1470. end;
  1471. end;
  1472. procedure TCustomTestModule.ParseModule;
  1473. begin
  1474. if SkipTests then exit;
  1475. FFirstPasStatement:=nil;
  1476. try
  1477. StartParsing;
  1478. Parser.ParseMain(FModule);
  1479. ParseModuleQueue;
  1480. except
  1481. on E: Exception do
  1482. HandleException(E);
  1483. end;
  1484. if SkipTests then exit;
  1485. AssertNotNull('Module resulted in Module',Module);
  1486. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1487. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1488. end;
  1489. procedure TCustomTestModule.ParseProgram;
  1490. begin
  1491. if SkipTests then exit;
  1492. ParseModule;
  1493. if SkipTests then exit;
  1494. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1495. FPasProgram:=TPasProgram(Module);
  1496. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1497. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1498. if (PasProgram.InitializationSection.Elements.Count>0) then
  1499. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1500. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1501. end;
  1502. procedure TCustomTestModule.ParseLibrary;
  1503. var
  1504. Init: TInitializationSection;
  1505. begin
  1506. if SkipTests then exit;
  1507. ParseModule;
  1508. if SkipTests then exit;
  1509. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1510. FPasLibrary:=TPasLibrary(Module);
  1511. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1512. Init:=PasLibrary.InitializationSection;
  1513. if (Init<>nil) and (Init.Elements.Count>0) then
  1514. if TObject(Init.Elements[0]) is TPasImplBlock then
  1515. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1516. end;
  1517. procedure TCustomTestModule.ParseUnit;
  1518. begin
  1519. if SkipTests then exit;
  1520. ParseModule;
  1521. if SkipTests then exit;
  1522. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1523. AssertNotNull('Has interface section',Module.InterfaceSection);
  1524. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1525. if (Module.InitializationSection<>nil)
  1526. and (Module.InitializationSection.Elements.Count>0)
  1527. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1528. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1529. end;
  1530. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1531. ): TTestEnginePasResolver;
  1532. var
  1533. i: Integer;
  1534. begin
  1535. for i:=0 to ResolverCount-1 do
  1536. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1537. exit(Resolvers[i]);
  1538. Result:=nil;
  1539. end;
  1540. function TCustomTestModule.AddModule(aFilename: string
  1541. ): TTestEnginePasResolver;
  1542. begin
  1543. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1544. if FindModuleWithFilename(aFilename)<>nil then
  1545. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1546. Result:=TTestEnginePasResolver.Create;
  1547. Result.Filename:=aFilename;
  1548. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1549. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1550. Result.OnLog:=@OnPasResolverLog;
  1551. Result.Hub:=Hub;
  1552. FModules.Add(Result);
  1553. end;
  1554. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1555. ): TTestEnginePasResolver;
  1556. begin
  1557. Result:=AddModule(aFilename);
  1558. Result.Source:=Src;
  1559. end;
  1560. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1561. ImplementationSrc: string): TTestEnginePasResolver;
  1562. var
  1563. Src: String;
  1564. begin
  1565. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1566. Src+=LineEnding;
  1567. Src+='interface'+LineEnding;
  1568. Src+=LineEnding;
  1569. Src+=InterfaceSrc;
  1570. Src+='implementation'+LineEnding;
  1571. Src+=LineEnding;
  1572. Src+=ImplementationSrc;
  1573. Src+='end.'+LineEnding;
  1574. Result:=AddModuleWithSrc(aFilename,Src);
  1575. end;
  1576. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1577. var
  1578. Intf, Impl: TStringList;
  1579. begin
  1580. Intf:=TStringList.Create;
  1581. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1582. // unit interface
  1583. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1584. Intf.Add('{$modeswitch externalclass}');
  1585. Intf.Add('type');
  1586. Intf.Add(' integer=longint;');
  1587. Intf.Add(' sizeint=nativeint;');
  1588. //'const',
  1589. //' LineEnding = #10;',
  1590. //' DirectorySeparator = ''/'';',
  1591. //' DriveSeparator = '''';',
  1592. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1593. //' AllowDriveSeparators : set of char = [];',
  1594. if supTObject in Parts then
  1595. Intf.AddStrings([
  1596. 'type',
  1597. ' TClass = class of TObject;',
  1598. ' TObject = class',
  1599. ' constructor Create;',
  1600. ' destructor Destroy; virtual;',
  1601. ' class function ClassType: TClass; assembler;',
  1602. ' class function ClassName: String; assembler;',
  1603. ' class function ClassNameIs(const Name: string): boolean;',
  1604. ' class function ClassParent: TClass; assembler;',
  1605. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1606. ' class function UnitName: String; assembler;',
  1607. ' procedure AfterConstruction; virtual;',
  1608. ' procedure BeforeDestruction;virtual;',
  1609. ' function Equals(Obj: TObject): boolean; virtual;',
  1610. ' function ToString: String; virtual;',
  1611. ' end;']);
  1612. if supTInterfacedObject in Parts then
  1613. Intf.AddStrings([
  1614. ' {$Interfaces COM}',
  1615. ' IUnknown = interface',
  1616. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1617. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1618. ' function _AddRef: Integer;',
  1619. ' function _Release: Integer;',
  1620. ' end;',
  1621. ' IInterface = IUnknown;',
  1622. ' TInterfacedObject = class(TObject,IUnknown)',
  1623. ' protected',
  1624. ' fRefCount: Integer;',
  1625. ' { implement methods of IUnknown }',
  1626. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1627. ' function _AddRef: Integer; virtual;',
  1628. ' function _Release: Integer; virtual;',
  1629. ' end;',
  1630. ' TInterfacedClass = class of TInterfacedObject;',
  1631. '',
  1632. '']);
  1633. if supTVarRec in Parts then
  1634. Intf.AddStrings([
  1635. 'const',
  1636. ' vtInteger = 0;',
  1637. ' vtBoolean = 1;',
  1638. ' vtJSValue = 19;',
  1639. 'type',
  1640. ' PVarRec = ^TVarRec;',
  1641. ' TVarRec = record',
  1642. ' VType : byte;',
  1643. ' VJSValue: JSValue;',
  1644. ' vInteger: longint external name ''VJSValue'';',
  1645. ' vBoolean: boolean external name ''VJSValue'';',
  1646. ' end;',
  1647. ' TVarRecArray = array of TVarRec;',
  1648. 'function VarRecs: TVarRecArray; varargs;',
  1649. '']);
  1650. if supTypeInfo in Parts then
  1651. begin
  1652. Intf.AddStrings([
  1653. 'type',
  1654. ' TTypeKind = (',
  1655. ' tkUnknown, // 0',
  1656. ' tkInteger, // 1',
  1657. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1658. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1659. ' tkEnumeration, // 4',
  1660. ' tkSet, // 5',
  1661. ' tkDouble, // 6',
  1662. ' tkBool, // 7',
  1663. ' tkProcVar, // 8 function or procedure',
  1664. ' tkMethod, // 9 proc var of object',
  1665. ' tkArray, // 10 static array',
  1666. ' tkDynArray, // 11',
  1667. ' tkRecord, // 12',
  1668. ' tkClass, // 13',
  1669. ' tkClassRef, // 14',
  1670. ' tkPointer, // 15',
  1671. ' tkJSValue, // 16',
  1672. ' tkRefToProcVar, // 17 variable of procedure type',
  1673. ' tkInterface, // 18',
  1674. ' //tkObject,',
  1675. ' //tkSString,tkLString,tkAString,tkWString,',
  1676. ' //tkVariant,',
  1677. ' //tkWChar,',
  1678. ' //tkInt64,',
  1679. ' //tkQWord,',
  1680. ' //tkInterfaceRaw,',
  1681. ' //tkUString,tkUChar,',
  1682. ' tkHelper, // 19',
  1683. ' //tkFile,',
  1684. ' tkExtClass // 20',
  1685. ' );',
  1686. ' TTypeKinds = set of TTypeKind;',
  1687. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1688. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1689. ' end;',
  1690. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1691. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1692. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1693. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1694. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1695. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1696. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1697. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1698. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1699. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1700. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1701. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1702. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1703. '']);
  1704. end;
  1705. if supWriteln in Parts then
  1706. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1707. Intf.Add('var');
  1708. Intf.Add(' ExitCode: Longint = 0;');
  1709. // unit implementation
  1710. Impl:=TStringList.Create;
  1711. if supTObject in Parts then
  1712. Impl.AddStrings([
  1713. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1714. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1715. 'asm',
  1716. 'end;',
  1717. 'constructor TObject.Create; begin end;',
  1718. 'destructor TObject.Destroy; begin end;',
  1719. 'class function TObject.ClassType: TClass; assembler;',
  1720. 'asm',
  1721. 'end;',
  1722. 'class function TObject.ClassName: String; assembler;',
  1723. 'asm',
  1724. 'end;',
  1725. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1726. 'begin',
  1727. ' Result:=SameText(Name,ClassName);',
  1728. 'end;',
  1729. 'class function TObject.ClassParent: TClass; assembler;',
  1730. 'asm',
  1731. 'end;',
  1732. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1733. 'asm',
  1734. 'end;',
  1735. 'class function TObject.UnitName: String; assembler;',
  1736. 'asm',
  1737. 'end;',
  1738. 'procedure TObject.AfterConstruction; begin end;',
  1739. 'procedure TObject.BeforeDestruction; begin end;',
  1740. 'function TObject.Equals(Obj: TObject): boolean;',
  1741. 'begin',
  1742. ' Result:=Obj=Self;',
  1743. 'end;',
  1744. 'function TObject.ToString: String;',
  1745. 'begin',
  1746. ' Result:=ClassName;',
  1747. 'end;'
  1748. ]);
  1749. if supTInterfacedObject in Parts then
  1750. Impl.AddStrings([
  1751. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1752. //'begin',
  1753. //'end;',
  1754. 'function TInterfacedObject._AddRef: Integer;',
  1755. 'begin',
  1756. 'end;',
  1757. 'function TInterfacedObject._Release: Integer;',
  1758. 'begin',
  1759. 'end;',
  1760. '']);
  1761. if supTVarRec in Parts then
  1762. Impl.AddStrings([
  1763. 'function VarRecs: TVarRecArray; varargs;',
  1764. 'var',
  1765. ' v: PVarRec;',
  1766. 'begin',
  1767. ' v^.VType:=1;',
  1768. ' v^.VJSValue:=2;',
  1769. 'end;',
  1770. '']);
  1771. try
  1772. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1773. finally
  1774. Intf.Free;
  1775. Impl.Free;
  1776. end;
  1777. end;
  1778. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1779. SystemUnitParts: TSystemUnitParts);
  1780. begin
  1781. if NeedSystemUnit then
  1782. AddSystemUnit(SystemUnitParts)
  1783. else
  1784. Parser.ImplicitUses.Clear;
  1785. Add('program '+ExtractFileUnitName(Filename)+';');
  1786. Add('');
  1787. end;
  1788. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1789. SystemUnitParts: TSystemUnitParts);
  1790. begin
  1791. if NeedSystemUnit then
  1792. AddSystemUnit(SystemUnitParts)
  1793. else
  1794. Parser.ImplicitUses.Clear;
  1795. Add('library '+ExtractFileUnitName(Filename)+';');
  1796. Add('');
  1797. end;
  1798. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1799. SystemUnitParts: TSystemUnitParts);
  1800. begin
  1801. if NeedSystemUnit then
  1802. AddSystemUnit(SystemUnitParts)
  1803. else
  1804. Parser.ImplicitUses.Clear;
  1805. Add('unit Test1;');
  1806. Add('');
  1807. end;
  1808. procedure TCustomTestModule.ConvertModule;
  1809. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1810. out UsesLit: TJSArrayLiteral);
  1811. var
  1812. i: Integer;
  1813. Item: TJSElement;
  1814. Lit: TJSLiteral;
  1815. begin
  1816. UsesLit:=nil;
  1817. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1818. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1819. exit; // null is ok
  1820. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1821. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1822. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1823. begin
  1824. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1825. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1826. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1827. Lit:=TJSLiteral(Item);
  1828. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1829. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1830. end;
  1831. end;
  1832. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1833. out Src: TJSSourceElements);
  1834. var
  1835. FunDecl: TJSFunctionDeclarationStatement;
  1836. FunDef: TJSFuncDef;
  1837. FunBody: TJSFunctionBody;
  1838. begin
  1839. Src:=nil;
  1840. AssertNotNull(ParamName,Arg.Expr);
  1841. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1842. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1843. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1844. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1845. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1846. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1847. AssertNotNull(ParamName+' body',FunDef.Body);
  1848. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1849. FunBody:=FunDef.Body as TJSFunctionBody;
  1850. AssertNotNull(ParamName+' body.A',FunBody.A);
  1851. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1852. Src:=FunBody.A as TJSSourceElements;
  1853. end;
  1854. var
  1855. ModuleNameExpr: TJSLiteral;
  1856. InitFunction: TJSFunctionDeclarationStatement;
  1857. InitAssign: TJSSimpleAssignStatement;
  1858. InitName: String;
  1859. LastNode: TJSElement;
  1860. Arg: TJSArrayLiteralElement;
  1861. begin
  1862. if SkipTests then exit;
  1863. try
  1864. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1865. except
  1866. on E: Exception do
  1867. HandleException(E);
  1868. end;
  1869. if SkipTests then exit;
  1870. if ExpectedErrorClass<>nil then
  1871. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1872. FJSSource:=TStringList.Create;
  1873. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1874. {$IFDEF VerbosePas2JS}
  1875. writeln('TTestModule.ConvertModule JS:');
  1876. write(FJSSource.Text);
  1877. {$ENDIF}
  1878. // rtl.module(...
  1879. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1880. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1881. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1882. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1883. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1884. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1885. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1886. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1887. // parameter 'unitname'
  1888. if JSModuleCallArgs.Elements.Count<1 then
  1889. Fail('rtl.module first param unit missing');
  1890. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1891. AssertNotNull('module name param',Arg.Expr);
  1892. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1893. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1894. if Module is TPasProgram then
  1895. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1896. else if Module is TPasLibrary then
  1897. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1898. else
  1899. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1900. // main uses section
  1901. if JSModuleCallArgs.Elements.Count<2 then
  1902. Fail('rtl.module second param main uses missing');
  1903. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1904. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1905. // program/library/interface function()
  1906. if JSModuleCallArgs.Elements.Count<3 then
  1907. Fail('rtl.module third param intf-function missing');
  1908. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1909. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1910. // search for $mod.$init or $mod.$main - the last statement
  1911. if (Module is TPasProgram) or (Module is TPasLibrary) then
  1912. begin
  1913. InitName:='$main';
  1914. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1915. end
  1916. else
  1917. InitName:='$init';
  1918. FJSInitBody:=nil;
  1919. if JSModuleSrc.Statements.Count>0 then
  1920. begin
  1921. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1922. if LastNode is TJSSimpleAssignStatement then
  1923. begin
  1924. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1925. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1926. begin
  1927. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1928. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1929. end
  1930. else if (Module is TPasProgram) or (Module is TPasLibrary) then
  1931. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1932. end;
  1933. end;
  1934. // optional: implementation uses section
  1935. if JSModuleCallArgs.Elements.Count<4 then
  1936. exit;
  1937. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1938. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1939. end;
  1940. procedure TCustomTestModule.ConvertProgram;
  1941. begin
  1942. Add('end.');
  1943. ParseProgram;
  1944. ConvertModule;
  1945. end;
  1946. procedure TCustomTestModule.ConvertLibrary;
  1947. begin
  1948. Add('end.');
  1949. ParseLibrary;
  1950. ConvertModule;
  1951. end;
  1952. procedure TCustomTestModule.ConvertUnit;
  1953. begin
  1954. Add('end.');
  1955. ParseUnit;
  1956. ConvertModule;
  1957. end;
  1958. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1959. begin
  1960. Result:=tcmodules.JSToStr(El);
  1961. end;
  1962. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1963. DottedName: string);
  1964. begin
  1965. if DottedName='' then
  1966. begin
  1967. AssertNull(Msg,El);
  1968. end
  1969. else
  1970. begin
  1971. AssertNotNull(Msg,El);
  1972. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1973. end;
  1974. end;
  1975. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1976. begin
  1977. if El=nil then
  1978. Result:=''
  1979. else if El is TJSPrimaryExpressionIdent then
  1980. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1981. else if El is TJSDotMemberExpression then
  1982. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1983. else
  1984. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1985. end;
  1986. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1987. InitStatements: string; ImplStatements: string);
  1988. var
  1989. ActualSrc, ExpectedSrc, InitName: String;
  1990. begin
  1991. ActualSrc:=JSToStr(JSModuleSrc);
  1992. if coUseStrict in Converter.Options then
  1993. ExpectedSrc:='"use strict";'+LineEnding
  1994. else
  1995. ExpectedSrc:='';
  1996. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1997. ExpectedSrc:=ExpectedSrc+Statements;
  1998. // unit implementation
  1999. if (Trim(ImplStatements)<>'') then
  2000. ExpectedSrc:=ExpectedSrc+LineEnding
  2001. +'$mod.$implcode = function () {'+LineEnding
  2002. +ImplStatements
  2003. +'};'+LineEnding;
  2004. // program main or unit initialization
  2005. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  2006. begin
  2007. if (Module is TPasProgram) or (Module is TPasLibrary) then
  2008. InitName:='$main'
  2009. else
  2010. InitName:='$init';
  2011. ExpectedSrc:=ExpectedSrc+LineEnding
  2012. +'$mod.'+InitName+' = function () {'+LineEnding
  2013. +InitStatements
  2014. +'};'+LineEnding;
  2015. end;
  2016. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2017. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2018. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2019. end;
  2020. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2021. // search diff, ignore changes in spaces
  2022. var
  2023. s: string;
  2024. begin
  2025. if CheckSrcDiff(Expected,Actual,s) then exit;
  2026. Fail(Msg+': '+s);
  2027. end;
  2028. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2029. var
  2030. aResolver: TTestEnginePasResolver;
  2031. aConverter: TPasToJSConverter;
  2032. aJSModule: TJSSourceElements;
  2033. ActualSrc: String;
  2034. begin
  2035. aResolver:=GetResolver(Filename);
  2036. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2037. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2038. {$IFDEF VerbosePas2JS}
  2039. writeln('CheckUnit '+Filename+' converting ...');
  2040. {$ENDIF}
  2041. aConverter:=CreateConverter;
  2042. aJSModule:=nil;
  2043. try
  2044. try
  2045. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2046. except
  2047. on E: Exception do
  2048. HandleException(E);
  2049. end;
  2050. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2051. {$IFDEF VerbosePas2JS}
  2052. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2053. write(aResolver.Source);
  2054. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2055. write(ActualSrc);
  2056. {$ENDIF}
  2057. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2058. finally
  2059. aJSModule.Free;
  2060. aConverter.Free;
  2061. end;
  2062. end;
  2063. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2064. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2065. var
  2066. i: Integer;
  2067. Item: TTestHintMessage;
  2068. Expected,Actual: string;
  2069. begin
  2070. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2071. for i:=0 to MsgCount-1 do
  2072. begin
  2073. Item:=Msgs[i];
  2074. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2075. if (Marker<>nil) then
  2076. begin
  2077. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2078. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2079. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2080. end;
  2081. // found
  2082. FHintMsgsGood.Add(Item);
  2083. str(Item.MsgType,Actual);
  2084. str(MsgType,Expected);
  2085. AssertEquals('MsgType',Expected,Actual);
  2086. exit;
  2087. end;
  2088. // needed message missing -> show emitted messages
  2089. WriteSources('',0,0);
  2090. for i:=0 to MsgCount-1 do
  2091. begin
  2092. Item:=Msgs[i];
  2093. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2094. ' ('+IntToStr(Item.MsgNumber),')');
  2095. if Marker<>nil then
  2096. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2097. writeln(' {',Item.Msg,'}');
  2098. end;
  2099. str(MsgType,Expected);
  2100. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2101. if Marker<>nil then
  2102. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2103. Actual:=Actual+' '+Msg;
  2104. Fail(Actual);
  2105. end;
  2106. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2107. );
  2108. var
  2109. i: Integer;
  2110. s, Txt: String;
  2111. Msg: TTestHintMessage;
  2112. begin
  2113. for i:=0 to MsgCount-1 do
  2114. begin
  2115. Msg:=Msgs[i];
  2116. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2117. s:='';
  2118. str(Msg.MsgType,s);
  2119. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2120. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2121. if WithSourcePos then
  2122. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2123. Txt:=Txt+' {'+Msg.Msg+'}';
  2124. Fail(Txt);
  2125. end;
  2126. end;
  2127. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2128. MsgNumber: integer);
  2129. begin
  2130. ExpectedErrorClass:=EScannerError;
  2131. ExpectedErrorMsg:=Msg;
  2132. ExpectedErrorNumber:=MsgNumber;
  2133. end;
  2134. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2135. MsgNumber: integer);
  2136. begin
  2137. ExpectedErrorClass:=EParserError;
  2138. ExpectedErrorMsg:=Msg;
  2139. ExpectedErrorNumber:=MsgNumber;
  2140. end;
  2141. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2142. MsgNumber: integer);
  2143. begin
  2144. ExpectedErrorClass:=EPasResolve;
  2145. ExpectedErrorMsg:=Msg;
  2146. ExpectedErrorNumber:=MsgNumber;
  2147. end;
  2148. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2149. MsgNumber: integer);
  2150. begin
  2151. ExpectedErrorClass:=EPas2JS;
  2152. ExpectedErrorMsg:=Msg;
  2153. ExpectedErrorNumber:=MsgNumber;
  2154. end;
  2155. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2156. var
  2157. MsgNumber: Integer;
  2158. Msg: String;
  2159. begin
  2160. Result:=false;
  2161. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2162. Msg:=E.Message;
  2163. if E is EPas2JS then
  2164. MsgNumber:=EPas2JS(E).MsgNumber
  2165. else if E is EPasResolve then
  2166. MsgNumber:=EPasResolve(E).MsgNumber
  2167. else if E is EParserError then
  2168. MsgNumber:=Parser.LastMsgNumber
  2169. else if E is EScannerError then
  2170. begin
  2171. MsgNumber:=Scanner.LastMsgNumber;
  2172. Msg:=Scanner.LastMsg;
  2173. end
  2174. else
  2175. MsgNumber:=0;
  2176. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2177. if Result then
  2178. SkipTests:=true;
  2179. end;
  2180. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2181. begin
  2182. if IsErrorExpected(E) then exit;
  2183. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2184. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2185. +' '+Scanner.CurFilename
  2186. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2187. FailException(E);
  2188. end;
  2189. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2190. begin
  2191. if IsErrorExpected(E) then exit;
  2192. WriteSources(E.Filename,E.Row,E.Column);
  2193. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2194. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2195. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2196. );
  2197. FailException(E);
  2198. end;
  2199. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2200. var
  2201. P: TPasSourcePos;
  2202. begin
  2203. if IsErrorExpected(E) then exit;
  2204. P:=E.SourcePos;
  2205. WriteSources(P.FileName,P.Row,P.Column);
  2206. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2207. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2208. FailException(E);
  2209. end;
  2210. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2211. var
  2212. Row, Col: integer;
  2213. begin
  2214. if IsErrorExpected(E) then exit;
  2215. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2216. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2217. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2218. +' '+E.PasElement.SourceFilename
  2219. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2220. FailException(E);
  2221. end;
  2222. procedure TCustomTestModule.HandleException(E: Exception);
  2223. begin
  2224. if E is EScannerError then
  2225. HandleScannerError(EScannerError(E))
  2226. else if E is EParserError then
  2227. HandleParserError(EParserError(E))
  2228. else if E is EPasResolve then
  2229. HandlePasResolveError(EPasResolve(E))
  2230. else if E is EPas2JS then
  2231. HandlePas2JSError(EPas2JS(E))
  2232. else
  2233. begin
  2234. if IsErrorExpected(E) then exit;
  2235. if not (E is EAssertionFailedError) then
  2236. begin
  2237. WriteSources('',0,0);
  2238. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2239. end;
  2240. FailException(E);
  2241. end;
  2242. end;
  2243. procedure TCustomTestModule.FailException(E: Exception);
  2244. var
  2245. MsgNumber: Integer;
  2246. begin
  2247. if ExpectedErrorClass<>nil then
  2248. begin
  2249. if FExpectedErrorClass=E.ClassType then
  2250. begin
  2251. if E is EPas2JS then
  2252. MsgNumber:=EPas2JS(E).MsgNumber
  2253. else if E is EPasResolve then
  2254. MsgNumber:=EPasResolve(E).MsgNumber
  2255. else if E is EParserError then
  2256. MsgNumber:=Parser.LastMsgNumber
  2257. else if E is EScannerError then
  2258. MsgNumber:=Scanner.LastMsgNumber
  2259. else
  2260. MsgNumber:=0;
  2261. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2262. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2263. ExpectedErrorNumber,MsgNumber);
  2264. end else begin
  2265. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2266. end;
  2267. end;
  2268. Fail(E.Message);
  2269. end;
  2270. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2271. aCol: integer);
  2272. var
  2273. IsSrc: Boolean;
  2274. i, j: Integer;
  2275. SrcLines: TStringList;
  2276. Line: string;
  2277. aModule: TTestEnginePasResolver;
  2278. begin
  2279. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2280. for i:=0 to ResolverCount-1 do
  2281. begin
  2282. aModule:=Resolvers[i];
  2283. SrcLines:=TStringList.Create;
  2284. try
  2285. SrcLines.Text:=aModule.Source;
  2286. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2287. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2288. for j:=1 to SrcLines.Count do
  2289. begin
  2290. Line:=SrcLines[j-1];
  2291. if IsSrc and (j=aRow) then
  2292. begin
  2293. write('*');
  2294. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2295. end;
  2296. writeln(Format('%:4d: ',[j]),Line);
  2297. end;
  2298. finally
  2299. SrcLines.Free;
  2300. end;
  2301. end;
  2302. end;
  2303. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2304. var
  2305. i: Integer;
  2306. begin
  2307. for i:=0 to ResolverCount-1 do
  2308. if Filename=Resolvers[i].Filename then exit(i);
  2309. Result:=-1;
  2310. end;
  2311. function TCustomTestModule.GetResolver(const Filename: string
  2312. ): TTestEnginePasResolver;
  2313. var
  2314. i: Integer;
  2315. begin
  2316. i:=IndexOfResolver(Filename);
  2317. if i<0 then exit(nil);
  2318. Result:=Resolvers[i];
  2319. end;
  2320. function TCustomTestModule.GetDefaultNamespace: string;
  2321. var
  2322. C: TClass;
  2323. begin
  2324. Result:='';
  2325. if FModule=nil then exit;
  2326. C:=FModule.ClassType;
  2327. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2328. Result:=Engine.DefaultNameSpace;
  2329. end;
  2330. constructor TCustomTestModule.Create;
  2331. begin
  2332. inherited Create;
  2333. FHintMsgs:=TObjectList.Create(true);
  2334. FHintMsgsGood:=TFPList.Create;
  2335. end;
  2336. destructor TCustomTestModule.Destroy;
  2337. begin
  2338. FreeAndNil(FHintMsgs);
  2339. FreeAndNil(FHintMsgsGood);
  2340. inherited Destroy;
  2341. end;
  2342. { TTestModule }
  2343. procedure TTestModule.TestReservedWords;
  2344. var
  2345. i: integer;
  2346. begin
  2347. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2348. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2349. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2350. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2351. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2352. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2353. end;
  2354. procedure TTestModule.TestEmptyProgram;
  2355. begin
  2356. StartProgram(false);
  2357. Add('begin');
  2358. ConvertProgram;
  2359. CheckSource('TestEmptyProgram','','');
  2360. end;
  2361. procedure TTestModule.TestEmptyProgramUseStrict;
  2362. begin
  2363. Converter.Options:=Converter.Options+[coUseStrict];
  2364. StartProgram(false);
  2365. Add('begin');
  2366. ConvertProgram;
  2367. CheckSource('TestEmptyProgramUseStrict','','');
  2368. end;
  2369. procedure TTestModule.TestEmptyUnit;
  2370. begin
  2371. StartUnit(false);
  2372. Add('interface');
  2373. Add('implementation');
  2374. ConvertUnit;
  2375. CheckSource('TestEmptyUnit',
  2376. LinesToStr([
  2377. ]),
  2378. '');
  2379. end;
  2380. procedure TTestModule.TestEmptyUnitUseStrict;
  2381. begin
  2382. Converter.Options:=Converter.Options+[coUseStrict];
  2383. StartUnit(false);
  2384. Add('interface');
  2385. Add('implementation');
  2386. ConvertUnit;
  2387. CheckSource('TestEmptyUnitUseStrict',
  2388. LinesToStr([
  2389. ''
  2390. ]),
  2391. '');
  2392. end;
  2393. procedure TTestModule.TestDottedUnitNames;
  2394. begin
  2395. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2396. LinesToStr([
  2397. 'var iV: longint;'
  2398. ]),
  2399. '');
  2400. FFilename:='ns1.test1.pp';
  2401. StartProgram(true);
  2402. Add('uses unIt2;');
  2403. Add('var');
  2404. Add(' i: longint;');
  2405. Add('begin');
  2406. Add(' i:=iv;');
  2407. Add(' i:=uNit2.iv;');
  2408. Add(' i:=Ns1.TEst1.i;');
  2409. ConvertProgram;
  2410. CheckSource('TestDottedUnitNames',
  2411. LinesToStr([
  2412. 'this.i = 0;',
  2413. '']),
  2414. LinesToStr([ // this.$init
  2415. '$mod.i = pas["NS1.Unit2"].iV;',
  2416. '$mod.i = pas["NS1.Unit2"].iV;',
  2417. '$mod.i = $mod.i;',
  2418. '']) );
  2419. end;
  2420. procedure TTestModule.TestDottedUnitNameImpl;
  2421. begin
  2422. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2423. LinesToStr([
  2424. 'type',
  2425. ' TObject = class end;',
  2426. ' TTestA = class',
  2427. ' end;'
  2428. ]),
  2429. LinesToStr(['uses TEST.UnitB;'])
  2430. );
  2431. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2432. LinesToStr([
  2433. 'uses TEST.UnitA;',
  2434. 'type TTestB = class(TTestA);'
  2435. ]),
  2436. ''
  2437. );
  2438. StartProgram(true);
  2439. Add('uses TEST.UnitA;');
  2440. Add('begin');
  2441. ConvertProgram;
  2442. CheckSource('TestDottedUnitNameImpl',
  2443. LinesToStr([
  2444. '']),
  2445. LinesToStr([ // this.$init
  2446. '']) );
  2447. CheckUnit('TEST.UnitA.pas',
  2448. LinesToStr([
  2449. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2450. ' var $mod = this;',
  2451. ' rtl.createClass(this, "TObject", null, function () {',
  2452. ' this.$init = function () {',
  2453. ' };',
  2454. ' this.$final = function () {',
  2455. ' };',
  2456. ' });',
  2457. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2458. ' });',
  2459. '}, ["TEST.UnitB"]);'
  2460. ]));
  2461. CheckUnit('TEST.UnitB.pas',
  2462. LinesToStr([
  2463. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2464. ' var $mod = this;',
  2465. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2466. ' });',
  2467. '});'
  2468. ]));
  2469. end;
  2470. procedure TTestModule.TestDottedUnitExpr;
  2471. begin
  2472. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2473. LinesToStr([
  2474. 'procedure DoIt;'
  2475. ]),
  2476. 'procedure DoIt; begin end;');
  2477. FFilename:='Ns1.SubNs1.Test1.pp';
  2478. StartProgram(true);
  2479. Add('uses Ns2.sUbnS2.unIt2;');
  2480. Add('var');
  2481. Add(' i: longint;');
  2482. Add('begin');
  2483. Add(' ns2.subns2.unit2.doit;');
  2484. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2485. ConvertProgram;
  2486. CheckSource('TestDottedUnitExpr',
  2487. LinesToStr([
  2488. 'this.i = 0;',
  2489. '']),
  2490. LinesToStr([ // this.$init
  2491. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2492. '$mod.i = $mod.i;',
  2493. '']) );
  2494. end;
  2495. procedure TTestModule.Test_ModeFPCFail;
  2496. begin
  2497. StartProgram(false);
  2498. Add('{$mode FPC}');
  2499. Add('begin');
  2500. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2501. ConvertProgram;
  2502. end;
  2503. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2504. begin
  2505. StartProgram(false);
  2506. Add('{$modeswitch cblocks-}');
  2507. Add('begin');
  2508. ConvertProgram;
  2509. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2510. CheckResolverUnexpectedHints();
  2511. end;
  2512. procedure TTestModule.TestUnit_UseSystem;
  2513. begin
  2514. StartUnit(true);
  2515. Add([
  2516. 'interface',
  2517. 'var i: integer;',
  2518. 'implementation']);
  2519. ConvertUnit;
  2520. CheckSource('TestUnit_UseSystem',
  2521. LinesToStr([
  2522. 'this.i = 0;',
  2523. '']),
  2524. LinesToStr([
  2525. '']) );
  2526. end;
  2527. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2528. begin
  2529. AddModuleWithIntfImplSrc('unit1.pp',
  2530. LinesToStr([
  2531. 'type number = longint;']),
  2532. LinesToStr([
  2533. 'uses test1;',
  2534. 'procedure DoIt;',
  2535. 'begin',
  2536. ' i:=3;',
  2537. 'end;']));
  2538. StartUnit(true);
  2539. Add([
  2540. 'interface',
  2541. 'uses unit1;',
  2542. 'var i: number;',
  2543. 'implementation']);
  2544. ConvertUnit;
  2545. CheckSource('TestUnit_Intf1Impl2Intf1',
  2546. LinesToStr([
  2547. 'this.i = 0;',
  2548. '']),
  2549. LinesToStr([
  2550. '']) );
  2551. end;
  2552. procedure TTestModule.TestIncludeVersion;
  2553. begin
  2554. StartProgram(false);
  2555. Add([
  2556. 'var',
  2557. ' s: string;',
  2558. ' i: word;',
  2559. 'begin',
  2560. ' s:={$I %line%};',
  2561. ' i:={$I %linenum%};',
  2562. ' s:={$I %currentroutine%};',
  2563. ' s:={$I %pas2jsversion%};',
  2564. ' s:={$I %pas2jstarget%};',
  2565. ' s:={$I %pas2jstargetos%};',
  2566. ' s:={$I %pas2jstargetcpu%};',
  2567. ' s:={$I %file%};',
  2568. '']);
  2569. ConvertProgram;
  2570. CheckSource('TestIncludeVersion',
  2571. LinesToStr([
  2572. 'this.s="";',
  2573. 'this.i = 0;']),
  2574. LinesToStr([
  2575. '$mod.s = "7";',
  2576. '$mod.i = 8;',
  2577. '$mod.s = "<anonymous>";',
  2578. '$mod.s = "Comp.Ver.tcmodules";',
  2579. '$mod.s = "Browser";',
  2580. '$mod.s = "Browser";',
  2581. '$mod.s = "ECMAScript5";',
  2582. '$mod.s = "test1.pp";',
  2583. '']));
  2584. end;
  2585. procedure TTestModule.TestVarInt;
  2586. begin
  2587. StartProgram(false);
  2588. Add('var MyI: longint;');
  2589. Add('begin');
  2590. ConvertProgram;
  2591. CheckSource('TestVarInt','this.MyI=0;','');
  2592. end;
  2593. procedure TTestModule.TestVarBaseTypes;
  2594. begin
  2595. StartProgram(false);
  2596. Add('var');
  2597. Add(' i: longint;');
  2598. Add(' s: string;');
  2599. Add(' c: char;');
  2600. Add(' b: boolean;');
  2601. Add(' d: double;');
  2602. Add(' i2: longint = 3;');
  2603. Add(' s2: string = ''foo'';');
  2604. Add(' c2: char = ''4'';');
  2605. Add(' b2: boolean = true;');
  2606. Add(' d2: double = 5.6;');
  2607. Add(' i3: longint = $707;');
  2608. Add(' i4: nativeint = 9007199254740991;');
  2609. Add(' i5: nativeint = -9007199254740991-1;');
  2610. Add(' i6: nativeint = $fffffffffffff;');
  2611. Add(' i7: nativeint = -$fffffffffffff-1;');
  2612. Add(' i8: byte = 00;');
  2613. Add(' u8: nativeuint = $fffffffffffff;');
  2614. Add(' u9: nativeuint = $0000000000000;');
  2615. Add(' u10: nativeuint = $00ff00;');
  2616. Add('begin');
  2617. ConvertProgram;
  2618. CheckSource('TestVarBaseTypes',
  2619. LinesToStr([
  2620. 'this.i = 0;',
  2621. 'this.s = "";',
  2622. 'this.c = "";',
  2623. 'this.b = false;',
  2624. 'this.d = 0.0;',
  2625. 'this.i2 = 3;',
  2626. 'this.s2 = "foo";',
  2627. 'this.c2 = "4";',
  2628. 'this.b2 = true;',
  2629. 'this.d2 = 5.6;',
  2630. 'this.i3 = 0x707;',
  2631. 'this.i4 = 9007199254740991;',
  2632. 'this.i5 = -9007199254740991-1;',
  2633. 'this.i6 = 0xfffffffffffff;',
  2634. 'this.i7 =-0xfffffffffffff-1;',
  2635. 'this.i8 = 0;',
  2636. 'this.u8 = 0xfffffffffffff;',
  2637. 'this.u9 = 0x0;',
  2638. 'this.u10 = 0xff00;'
  2639. ]),
  2640. '');
  2641. end;
  2642. procedure TTestModule.TestBaseTypeSingleFail;
  2643. begin
  2644. StartProgram(false);
  2645. Add('var s: single;');
  2646. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2647. ConvertProgram;
  2648. end;
  2649. procedure TTestModule.TestBaseTypeExtendedFail;
  2650. begin
  2651. StartProgram(false);
  2652. Add('var e: extended;');
  2653. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2654. ConvertProgram;
  2655. end;
  2656. procedure TTestModule.TestConstBaseTypes;
  2657. begin
  2658. StartProgram(false);
  2659. Add('const');
  2660. Add(' i: longint = 3;');
  2661. Add(' s: string = ''foo'';');
  2662. Add(' c: char = ''4'';');
  2663. Add(' b: boolean = true;');
  2664. Add(' d: double = 5.6;');
  2665. Add(' e = low(word);');
  2666. Add(' f = high(word);');
  2667. Add('begin');
  2668. ConvertProgram;
  2669. CheckSource('TestVarBaseTypes',
  2670. LinesToStr([
  2671. 'this.i=3;',
  2672. 'this.s="foo";',
  2673. 'this.c="4";',
  2674. 'this.b=true;',
  2675. 'this.d=5.6;',
  2676. 'this.e = 0;',
  2677. 'this.f = 65535;'
  2678. ]),
  2679. '');
  2680. end;
  2681. procedure TTestModule.TestAliasTypeRef;
  2682. begin
  2683. StartProgram(false);
  2684. Add('type');
  2685. Add(' a=longint;');
  2686. Add(' b=a;');
  2687. Add('var');
  2688. Add(' c: A;');
  2689. Add(' d: B;');
  2690. Add('begin');
  2691. ConvertProgram;
  2692. CheckSource('TestAliasTypeRef',
  2693. LinesToStr([ // statements
  2694. 'this.c = 0;',
  2695. 'this.d = 0;'
  2696. ]),
  2697. LinesToStr([ // this.$main
  2698. ''
  2699. ]));
  2700. end;
  2701. procedure TTestModule.TestTypeCast_BaseTypes;
  2702. begin
  2703. StartProgram(false);
  2704. Add([
  2705. 'var',
  2706. ' i: longint;',
  2707. ' b: boolean;',
  2708. ' d: double;',
  2709. ' s: string;',
  2710. ' c: char;',
  2711. 'begin',
  2712. ' i:=longint(i);',
  2713. ' i:=longint(b);',
  2714. ' b:=boolean(b);',
  2715. ' b:=boolean(i);',
  2716. ' d:=double(d);',
  2717. ' d:=double(i);',
  2718. ' s:=string(s);',
  2719. ' s:=string(c);',
  2720. ' c:=char(c);',
  2721. ' c:=char(i);',
  2722. ' c:=char(65);',
  2723. ' c:=char(#10);',
  2724. ' c:=char(#$E000);',
  2725. '']);
  2726. ConvertProgram;
  2727. CheckSource('TestAliasTypeRef',
  2728. LinesToStr([ // statements
  2729. 'this.i = 0;',
  2730. 'this.b = false;',
  2731. 'this.d = 0.0;',
  2732. 'this.s = "";',
  2733. 'this.c = "";',
  2734. '']),
  2735. LinesToStr([ // this.$main
  2736. '$mod.i = $mod.i;',
  2737. '$mod.i = ($mod.b ? 1 : 0);',
  2738. '$mod.b = $mod.b;',
  2739. '$mod.b = $mod.i != 0;',
  2740. '$mod.d = $mod.d;',
  2741. '$mod.d = $mod.i;',
  2742. '$mod.s = $mod.s;',
  2743. '$mod.s = $mod.c;',
  2744. '$mod.c = $mod.c;',
  2745. '$mod.c = String.fromCharCode($mod.i);',
  2746. '$mod.c = "A";',
  2747. '$mod.c = "\n";',
  2748. '$mod.c = "";',
  2749. '']));
  2750. end;
  2751. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2752. begin
  2753. StartProgram(false);
  2754. Add('type');
  2755. Add(' integer = longint;');
  2756. Add(' TYesNo = boolean;');
  2757. Add(' TFloat = double;');
  2758. Add(' TCaption = string;');
  2759. Add(' TChar = char;');
  2760. Add('var');
  2761. Add(' i: integer;');
  2762. Add(' b: TYesNo;');
  2763. Add(' d: TFloat;');
  2764. Add(' s: TCaption;');
  2765. Add(' c: TChar;');
  2766. Add('begin');
  2767. Add(' i:=integer(i);');
  2768. Add(' i:=integer(b);');
  2769. Add(' b:=TYesNo(b);');
  2770. Add(' b:=TYesNo(i);');
  2771. Add(' d:=TFloat(d);');
  2772. Add(' d:=TFloat(i);');
  2773. Add(' s:=TCaption(s);');
  2774. Add(' s:=TCaption(c);');
  2775. Add(' c:=TChar(c);');
  2776. ConvertProgram;
  2777. CheckSource('TestAliasTypeRef',
  2778. LinesToStr([ // statements
  2779. 'this.i = 0;',
  2780. 'this.b = false;',
  2781. 'this.d = 0.0;',
  2782. 'this.s = "";',
  2783. 'this.c = "";',
  2784. '']),
  2785. LinesToStr([ // this.$main
  2786. '$mod.i = $mod.i;',
  2787. '$mod.i = ($mod.b ? 1 : 0);',
  2788. '$mod.b = $mod.b;',
  2789. '$mod.b = $mod.i != 0;',
  2790. '$mod.d = $mod.d;',
  2791. '$mod.d = $mod.i;',
  2792. '$mod.s = $mod.s;',
  2793. '$mod.s = $mod.c;',
  2794. '$mod.c = $mod.c;',
  2795. '']));
  2796. end;
  2797. procedure TTestModule.TestEmptyProc;
  2798. begin
  2799. StartProgram(false);
  2800. Add('procedure Test;');
  2801. Add('begin');
  2802. Add('end;');
  2803. Add('begin');
  2804. ConvertProgram;
  2805. CheckSource('TestEmptyProc',
  2806. LinesToStr([ // statements
  2807. 'this.Test = function () {',
  2808. '};'
  2809. ]),
  2810. LinesToStr([ // this.$main
  2811. ''
  2812. ]));
  2813. end;
  2814. procedure TTestModule.TestProcOneParam;
  2815. begin
  2816. StartProgram(false);
  2817. Add('procedure ProcA(i: longint);');
  2818. Add('begin');
  2819. Add('end;');
  2820. Add('begin');
  2821. Add(' PROCA(3);');
  2822. ConvertProgram;
  2823. CheckSource('TestProcOneParam',
  2824. LinesToStr([ // statements
  2825. 'this.ProcA = function (i) {',
  2826. '};'
  2827. ]),
  2828. LinesToStr([ // this.$main
  2829. '$mod.ProcA(3);'
  2830. ]));
  2831. end;
  2832. procedure TTestModule.TestFunctionWithoutParams;
  2833. begin
  2834. StartProgram(false);
  2835. Add('function FuncA: longint;');
  2836. Add('begin');
  2837. Add('end;');
  2838. Add('var i: longint;');
  2839. Add('begin');
  2840. Add(' I:=FUNCA();');
  2841. Add(' I:=FUNCA;');
  2842. Add(' FUNCA();');
  2843. Add(' FUNCA;');
  2844. ConvertProgram;
  2845. CheckSource('TestProcWithoutParams',
  2846. LinesToStr([ // statements
  2847. 'this.FuncA = function () {',
  2848. ' var Result = 0;',
  2849. ' return Result;',
  2850. '};',
  2851. 'this.i=0;'
  2852. ]),
  2853. LinesToStr([ // this.$main
  2854. '$mod.i=$mod.FuncA();',
  2855. '$mod.i=$mod.FuncA();',
  2856. '$mod.FuncA();',
  2857. '$mod.FuncA();'
  2858. ]));
  2859. end;
  2860. procedure TTestModule.TestProcedureWithoutParams;
  2861. begin
  2862. StartProgram(false);
  2863. Add('procedure ProcA;');
  2864. Add('begin');
  2865. Add('end;');
  2866. Add('begin');
  2867. Add(' PROCA();');
  2868. Add(' PROCA;');
  2869. ConvertProgram;
  2870. CheckSource('TestProcWithoutParams',
  2871. LinesToStr([ // statements
  2872. 'this.ProcA = function () {',
  2873. '};'
  2874. ]),
  2875. LinesToStr([ // this.$main
  2876. '$mod.ProcA();',
  2877. '$mod.ProcA();'
  2878. ]));
  2879. end;
  2880. procedure TTestModule.TestIncDec;
  2881. begin
  2882. StartProgram(false);
  2883. Add([
  2884. 'procedure DoIt(var i: longint);',
  2885. 'begin',
  2886. ' inc(i);',
  2887. ' inc(i,2);',
  2888. 'end;',
  2889. 'var',
  2890. ' Bar: longint;',
  2891. 'begin',
  2892. ' inc(bar);',
  2893. ' inc(bar,2);',
  2894. ' dec(bar);',
  2895. ' dec(bar,3);',
  2896. '']);
  2897. ConvertProgram;
  2898. CheckSource('TestIncDec',
  2899. LinesToStr([ // statements
  2900. 'this.DoIt = function (i) {',
  2901. ' i.set(i.get()+1);',
  2902. ' i.set(i.get()+2);',
  2903. '};',
  2904. 'this.Bar = 0;'
  2905. ]),
  2906. LinesToStr([ // this.$main
  2907. '$mod.Bar+=1;',
  2908. '$mod.Bar+=2;',
  2909. '$mod.Bar-=1;',
  2910. '$mod.Bar-=3;'
  2911. ]));
  2912. end;
  2913. procedure TTestModule.TestLoHiFpcMode;
  2914. begin
  2915. StartProgram(false);
  2916. Add([
  2917. '{$mode objfpc}',
  2918. 'const',
  2919. ' LoByte1 = Lo(Word($1234));',
  2920. ' HiByte1 = Hi(Word($1234));',
  2921. ' LoByte2 = Lo(SmallInt($1234));',
  2922. ' HiByte2 = Hi(SmallInt($1234));',
  2923. ' LoWord1 = Lo($1234CDEF);',
  2924. ' HiWord1 = Hi($1234CDEF);',
  2925. ' LoWord2 = Lo(-$1234CDEF);',
  2926. ' HiWord2 = Hi(-$1234CDEF);',
  2927. ' lo4:byte=lo(byte($34));',
  2928. ' hi4:byte=hi(byte($34));',
  2929. ' lo5:byte=lo(shortint(-$34));',
  2930. ' hi5:byte=hi(shortint(-$34));',
  2931. ' lo6:longword=lo($123456789ABCD);',
  2932. ' hi6:longword=hi($123456789ABCD);',
  2933. ' lo7:longword=lo(-$123456789ABCD);',
  2934. ' hi7:longword=hi(-$123456789ABCD);',
  2935. 'var',
  2936. ' b: Byte;',
  2937. ' ss: shortint;',
  2938. ' w: Word;',
  2939. ' si: SmallInt;',
  2940. ' lw: LongWord;',
  2941. ' li: LongInt;',
  2942. ' b2: Byte;',
  2943. ' ni: nativeint;',
  2944. 'begin',
  2945. ' w := $1234;',
  2946. ' ss := -$12;',
  2947. ' b := lo(ss);',
  2948. ' b := HI(ss);',
  2949. ' b := lo(w);',
  2950. ' b := HI(w);',
  2951. ' b2 := lo(b);',
  2952. ' b2 := hi(b);',
  2953. ' lw := $1234CDEF;',
  2954. ' w := lo(lw);',
  2955. ' w := hi(lw);',
  2956. ' ni := $123456789ABCD;',
  2957. ' lw := lo(ni);',
  2958. ' lw := hi(ni);',
  2959. '']);
  2960. ConvertProgram;
  2961. CheckSource('TestLoHiFpcMode',
  2962. LinesToStr([ // statements
  2963. 'this.LoByte1 = 0x1234 & 0xFF;',
  2964. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2965. 'this.LoByte2 = 0x1234 & 0xFF;',
  2966. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2967. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2968. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2969. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2970. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2971. 'this.lo4 = 0x34 & 0xF;',
  2972. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2973. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2974. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2975. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2976. 'this.hi6 = 74565 >>> 0;',
  2977. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2978. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2979. 'this.b = 0;',
  2980. 'this.ss = 0;',
  2981. 'this.w = 0;',
  2982. 'this.si = 0;',
  2983. 'this.lw = 0;',
  2984. 'this.li = 0;',
  2985. 'this.b2 = 0;',
  2986. 'this.ni = 0;',
  2987. '']),
  2988. LinesToStr([ // this.$main
  2989. '$mod.w = 0x1234;',
  2990. '$mod.ss = -0x12;',
  2991. '$mod.b = $mod.ss & 0xFF;',
  2992. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2993. '$mod.b = $mod.w & 0xFF;',
  2994. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2995. '$mod.b2 = $mod.b & 0xF;',
  2996. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2997. '$mod.lw = 0x1234CDEF;',
  2998. '$mod.w = $mod.lw & 0xFFFF;',
  2999. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3000. '$mod.ni = 0x123456789ABCD;',
  3001. '$mod.lw = $mod.ni >>> 0;',
  3002. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3003. '']));
  3004. end;
  3005. procedure TTestModule.TestLoHiDelphiMode;
  3006. begin
  3007. StartProgram(false);
  3008. Add([
  3009. '{$mode delphi}',
  3010. 'const',
  3011. ' LoByte1 = Lo(Word($1234));',
  3012. ' HiByte1 = Hi(Word($1234));',
  3013. ' LoByte2 = Lo(SmallInt($1234));',
  3014. ' HiByte2 = Hi(SmallInt($1234));',
  3015. ' LoByte3 = Lo($1234CDEF);',
  3016. ' HiByte3 = Hi($1234CDEF);',
  3017. ' LoByte4 = Lo(-$1234CDEF);',
  3018. ' HiByte4 = Hi(-$1234CDEF);',
  3019. 'var',
  3020. ' b: Byte;',
  3021. ' w: Word;',
  3022. ' si: SmallInt;',
  3023. ' lw: LongWord;',
  3024. ' li: LongInt;',
  3025. 'begin',
  3026. ' w := $1234;',
  3027. ' b := lo(w);',
  3028. ' b := HI(w);',
  3029. ' lw := $1234CDEF;',
  3030. ' b := lo(lw);',
  3031. ' b := hi(lw);',
  3032. '']);
  3033. ConvertProgram;
  3034. CheckSource('TestLoHiDelphiMode',
  3035. LinesToStr([ // statements
  3036. 'this.LoByte1 = 0x1234 & 0xFF;',
  3037. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3038. 'this.LoByte2 = 0x1234 & 0xFF;',
  3039. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3040. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3041. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3042. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3043. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3044. 'this.b = 0;',
  3045. 'this.w = 0;',
  3046. 'this.si = 0;',
  3047. 'this.lw = 0;',
  3048. 'this.li = 0;'
  3049. ]),
  3050. LinesToStr([ // this.$main
  3051. '$mod.w = 0x1234;',
  3052. '$mod.b = $mod.w & 0xFF;',
  3053. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3054. '$mod.lw = 0x1234CDEF;',
  3055. '$mod.b = $mod.lw & 0xFF;',
  3056. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3057. ]));
  3058. end;
  3059. procedure TTestModule.TestAssignments;
  3060. begin
  3061. StartProgram(false);
  3062. Parser.Options:=Parser.Options+[po_cassignments];
  3063. Add('var');
  3064. Add(' Bar:longint;');
  3065. Add('begin');
  3066. Add(' bar:=3;');
  3067. Add(' bar+=4;');
  3068. Add(' bar-=5;');
  3069. Add(' bar*=6;');
  3070. ConvertProgram;
  3071. CheckSource('TestAssignments',
  3072. LinesToStr([ // statements
  3073. 'this.Bar = 0;'
  3074. ]),
  3075. LinesToStr([ // this.$main
  3076. '$mod.Bar=3;',
  3077. '$mod.Bar+=4;',
  3078. '$mod.Bar-=5;',
  3079. '$mod.Bar*=6;'
  3080. ]));
  3081. end;
  3082. procedure TTestModule.TestArithmeticOperators1;
  3083. begin
  3084. StartProgram(false);
  3085. Add('var');
  3086. Add(' vA,vB,vC:longint;');
  3087. Add('begin');
  3088. Add(' va:=1;');
  3089. Add(' vb:=va+va;');
  3090. Add(' vb:=va div vb;');
  3091. Add(' vb:=va mod vb;');
  3092. Add(' vb:=va+va*vb+va div vb;');
  3093. Add(' vc:=-va;');
  3094. Add(' va:=va-vb;');
  3095. Add(' vb:=va;');
  3096. Add(' if va<vb then vc:=va else vc:=vb;');
  3097. ConvertProgram;
  3098. CheckSource('TestArithmeticOperators1',
  3099. LinesToStr([ // statements
  3100. 'this.vA = 0;',
  3101. 'this.vB = 0;',
  3102. 'this.vC = 0;'
  3103. ]),
  3104. LinesToStr([ // this.$main
  3105. '$mod.vA = 1;',
  3106. '$mod.vB = $mod.vA + $mod.vA;',
  3107. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3108. '$mod.vB = $mod.vA % $mod.vB;',
  3109. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3110. '$mod.vC = -$mod.vA;',
  3111. '$mod.vA = $mod.vA - $mod.vB;',
  3112. '$mod.vB = $mod.vA;',
  3113. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3114. ]));
  3115. end;
  3116. procedure TTestModule.TestMultiAdd;
  3117. begin
  3118. StartProgram(false);
  3119. Add([
  3120. 'function Fly: string; external name ''fly'';',
  3121. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3122. 'var',
  3123. ' Date: double;',
  3124. 'begin',
  3125. ' Result:=(Year>0) and (Year<10000) and',
  3126. ' (Month >= 1) and (Month<=12) and',
  3127. ' (Day>0) and (Day<=31);',
  3128. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3129. 'end;',
  3130. 'var s: string;',
  3131. 'begin',
  3132. ' s:=''a''+''b''+''c''+''d'';',
  3133. ' s:=s+Fly+''e'';',
  3134. ' s:=Fly+Fly+Fly;',
  3135. '']);
  3136. ConvertProgram;
  3137. CheckSource('TestMultiAdd',
  3138. LinesToStr([ // statements
  3139. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3140. ' var Result = false;',
  3141. ' var date = 0.0;',
  3142. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3143. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3144. ' return Result;',
  3145. '};',
  3146. 'this.s = "";',
  3147. '']),
  3148. LinesToStr([ // this.$main
  3149. '$mod.s = "a" + "b" + "c" + "d";',
  3150. '$mod.s = $mod.s + fly() + "e";',
  3151. '$mod.s = fly() + fly() + fly();',
  3152. '']));
  3153. end;
  3154. procedure TTestModule.TestLogicalOperators;
  3155. begin
  3156. StartProgram(false);
  3157. Add('var');
  3158. Add(' vA,vB,vC:boolean;');
  3159. Add('begin');
  3160. Add(' va:=vb and vc;');
  3161. Add(' va:=vb or vc;');
  3162. Add(' va:=vb xor vc;');
  3163. Add(' va:=true and vc;');
  3164. Add(' va:=(vb and vc) or (va and vb);');
  3165. Add(' va:=not vb;');
  3166. ConvertProgram;
  3167. CheckSource('TestLogicalOperators',
  3168. LinesToStr([ // statements
  3169. 'this.vA = false;',
  3170. 'this.vB = false;',
  3171. 'this.vC = false;'
  3172. ]),
  3173. LinesToStr([ // this.$main
  3174. '$mod.vA = $mod.vB && $mod.vC;',
  3175. '$mod.vA = $mod.vB || $mod.vC;',
  3176. '$mod.vA = $mod.vB ^ $mod.vC;',
  3177. '$mod.vA = true && $mod.vC;',
  3178. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3179. '$mod.vA = !$mod.vB;'
  3180. ]));
  3181. end;
  3182. procedure TTestModule.TestBitwiseOperators;
  3183. begin
  3184. StartProgram(false);
  3185. Add([
  3186. 'var',
  3187. ' vA,vB,vC:longint;',
  3188. ' X,Y,Z: nativeint;',
  3189. 'begin',
  3190. ' va:=vb and vc;',
  3191. ' va:=vb or vc;',
  3192. ' va:=vb xor vc;',
  3193. ' va:=vb shl vc;',
  3194. ' va:=vb shr vc;',
  3195. ' va:=3 and vc;',
  3196. ' va:=(vb and vc) or (va and vb);',
  3197. ' va:=not vb;',
  3198. ' X:=Y and Z;',
  3199. ' X:=Y and va;',
  3200. ' X:=Y or Z;',
  3201. ' X:=Y or va;',
  3202. ' X:=Y xor Z;',
  3203. ' X:=Y xor va;',
  3204. '']);
  3205. ConvertProgram;
  3206. CheckSource('TestBitwiseOperators',
  3207. LinesToStr([ // statements
  3208. 'this.vA = 0;',
  3209. 'this.vB = 0;',
  3210. 'this.vC = 0;',
  3211. 'this.X = 0;',
  3212. 'this.Y = 0;',
  3213. 'this.Z = 0;',
  3214. '']),
  3215. LinesToStr([ // this.$main
  3216. '$mod.vA = $mod.vB & $mod.vC;',
  3217. '$mod.vA = $mod.vB | $mod.vC;',
  3218. '$mod.vA = $mod.vB ^ $mod.vC;',
  3219. '$mod.vA = $mod.vB << $mod.vC;',
  3220. '$mod.vA = $mod.vB >>> $mod.vC;',
  3221. '$mod.vA = 3 & $mod.vC;',
  3222. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3223. '$mod.vA = ~$mod.vB;',
  3224. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3225. '$mod.X = $mod.Y & $mod.vA;',
  3226. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3227. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3228. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3229. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3230. '']));
  3231. end;
  3232. procedure TTestModule.TestBitwiseOperatorsLongword;
  3233. begin
  3234. StartProgram(false);
  3235. Add([
  3236. 'var',
  3237. ' a,b,c:longword;',
  3238. ' i: longint;',
  3239. 'begin',
  3240. ' a:=$12345678;',
  3241. ' b:=$EDCBA987;',
  3242. ' c:=not a;',
  3243. ' c:=a and b;',
  3244. ' c:=a and $ffff0000;',
  3245. ' c:=a or b;',
  3246. ' c:=a or $ff00ff00;',
  3247. ' c:=a xor b;',
  3248. ' c:=a xor $f0f0f0f0;',
  3249. ' c:=a shl 1;',
  3250. ' c:=a shl 16;',
  3251. ' c:=a shl 24;',
  3252. ' c:=a shl b;',
  3253. ' c:=a shr 1;',
  3254. ' c:=a shr 16;',
  3255. ' c:=a shr 24;',
  3256. ' c:=a shr b;',
  3257. ' c:=(b and c) or (a and b);',
  3258. ' c:=i and a;',
  3259. ' c:=i or a;',
  3260. ' c:=i xor a;',
  3261. '']);
  3262. ConvertProgram;
  3263. CheckSource('TestBitwiseOperatorsLongword',
  3264. LinesToStr([ // statements
  3265. 'this.a = 0;',
  3266. 'this.b = 0;',
  3267. 'this.c = 0;',
  3268. 'this.i = 0;',
  3269. '']),
  3270. LinesToStr([ // this.$main
  3271. '$mod.a = 0x12345678;',
  3272. '$mod.b = 0xEDCBA987;',
  3273. '$mod.c = rtl.lw(~$mod.a);',
  3274. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3275. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3276. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3277. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3278. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3279. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3280. '$mod.c = rtl.lw($mod.a << 1);',
  3281. '$mod.c = rtl.lw($mod.a << 16);',
  3282. '$mod.c = rtl.lw($mod.a << 24);',
  3283. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3284. '$mod.c = rtl.lw($mod.a >>> 1);',
  3285. '$mod.c = rtl.lw($mod.a >>> 16);',
  3286. '$mod.c = rtl.lw($mod.a >>> 24);',
  3287. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3288. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3289. '$mod.c = $mod.i & $mod.a;',
  3290. '$mod.c = $mod.i | $mod.a;',
  3291. '$mod.c = $mod.i ^ $mod.a;',
  3292. '']));
  3293. end;
  3294. procedure TTestModule.TestPrgProcVar;
  3295. begin
  3296. StartProgram(false);
  3297. Add('procedure Proc1;');
  3298. Add('type');
  3299. Add(' t1=longint;');
  3300. Add('var');
  3301. Add(' vA:t1;');
  3302. Add('begin');
  3303. Add('end;');
  3304. Add('begin');
  3305. ConvertProgram;
  3306. CheckSource('TestPrgProcVar',
  3307. LinesToStr([ // statements
  3308. 'this.Proc1 = function () {',
  3309. ' var vA=0;',
  3310. '};'
  3311. ]),
  3312. LinesToStr([ // this.$main
  3313. ''
  3314. ]));
  3315. end;
  3316. procedure TTestModule.TestUnitProcVar;
  3317. begin
  3318. StartUnit(false);
  3319. Add('interface');
  3320. Add('');
  3321. Add('type tA=string; // unit scope');
  3322. Add('procedure Proc1;');
  3323. Add('');
  3324. Add('implementation');
  3325. Add('');
  3326. Add('procedure Proc1;');
  3327. Add('type tA=longint; // local proc scope');
  3328. Add('var v1:tA; // using local tA');
  3329. Add('begin');
  3330. Add('end;');
  3331. Add('var v2:tA; // using interface tA');
  3332. ConvertUnit;
  3333. CheckSource('TestUnitProcVar',
  3334. LinesToStr([ // statements
  3335. 'var $impl = $mod.$impl;',
  3336. 'this.Proc1 = function () {',
  3337. ' var v1 = 0;',
  3338. '};',
  3339. '']),
  3340. // this.$init
  3341. '',
  3342. // implementation
  3343. LinesToStr([
  3344. '$impl.v2 = "";',
  3345. '']));
  3346. end;
  3347. procedure TTestModule.TestImplProc;
  3348. begin
  3349. StartUnit(false);
  3350. Add('interface');
  3351. Add('');
  3352. Add('procedure Proc1;');
  3353. Add('');
  3354. Add('implementation');
  3355. Add('');
  3356. Add('procedure Proc1; begin end;');
  3357. Add('procedure Proc2; begin end;');
  3358. Add('initialization');
  3359. Add(' Proc1;');
  3360. Add(' Proc2;');
  3361. ConvertUnit;
  3362. CheckSource('TestImplProc',
  3363. LinesToStr([ // statements
  3364. 'var $impl = $mod.$impl;',
  3365. 'this.Proc1 = function () {',
  3366. '};',
  3367. '']),
  3368. LinesToStr([ // this.$init
  3369. '$mod.Proc1();',
  3370. '$impl.Proc2();',
  3371. '']),
  3372. LinesToStr([ // implementation
  3373. '$impl.Proc2 = function () {',
  3374. '};',
  3375. ''])
  3376. );
  3377. end;
  3378. procedure TTestModule.TestFunctionResult;
  3379. begin
  3380. StartProgram(false);
  3381. Add('function Func1: longint;');
  3382. Add('begin');
  3383. Add(' Result:=3;');
  3384. Add(' Func1:=4;');
  3385. Add('end;');
  3386. Add('begin');
  3387. ConvertProgram;
  3388. CheckSource('TestFunctionResult',
  3389. LinesToStr([ // statements
  3390. 'this.Func1 = function () {',
  3391. ' var Result = 0;',
  3392. ' Result = 3;',
  3393. ' Result = 4;',
  3394. ' return Result;',
  3395. '};'
  3396. ]),
  3397. '');
  3398. end;
  3399. procedure TTestModule.TestNestedProc;
  3400. begin
  3401. StartProgram(false);
  3402. Add([
  3403. 'var vInUnit: longint;',
  3404. 'function DoIt(pA,pD: longint): longint;',
  3405. 'var',
  3406. ' vB: longint;',
  3407. ' vC: longint;',
  3408. ' function Nesty(pA: longint): longint; ',
  3409. ' var vB: longint;',
  3410. ' begin',
  3411. ' Result:=pa+vb+vc+pd+vInUnit;',
  3412. ' nesty:=3;',
  3413. ' doit:=4;',
  3414. ' exit;',
  3415. ' end;',
  3416. 'begin',
  3417. ' Result:=pa+vb+vc;',
  3418. ' doit:=6;',
  3419. ' exit;',
  3420. 'end;',
  3421. 'begin']);
  3422. ConvertProgram;
  3423. CheckSource('TestNestedProc',
  3424. LinesToStr([ // statements
  3425. 'this.vInUnit = 0;',
  3426. 'this.DoIt = function (pA, pD) {',
  3427. ' var Result = 0;',
  3428. ' var vB = 0;',
  3429. ' var vC = 0;',
  3430. ' function Nesty(pA) {',
  3431. ' var Result$1 = 0;',
  3432. ' var vB = 0;',
  3433. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3434. ' Result$1 = 3;',
  3435. ' Result = 4;',
  3436. ' return Result$1;',
  3437. ' return Result$1;',
  3438. ' };',
  3439. ' Result = pA + vB + vC;',
  3440. ' Result = 6;',
  3441. ' return Result;',
  3442. ' return Result;',
  3443. '};'
  3444. ]),
  3445. '');
  3446. end;
  3447. procedure TTestModule.TestNestedProc_ResultString;
  3448. begin
  3449. StartProgram(false);
  3450. Add([
  3451. 'function DoIt: string;',
  3452. ' function Nesty: string; ',
  3453. ' begin',
  3454. ' nesty:=#65#66;',
  3455. ' nesty[1]:=#67;',
  3456. ' doit:=#68;',
  3457. ' doit[2]:=#69;',
  3458. ' end;',
  3459. 'begin',
  3460. ' doit:=#70;',
  3461. ' doit[3]:=#71;',
  3462. 'end;',
  3463. 'begin']);
  3464. ConvertProgram;
  3465. CheckSource('TestNestedProc_ResultString',
  3466. LinesToStr([ // statements
  3467. 'this.DoIt = function () {',
  3468. ' var Result = "";',
  3469. ' function Nesty() {',
  3470. ' var Result$1 = "";',
  3471. ' Result$1 = "AB";',
  3472. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3473. ' Result = "D";',
  3474. ' Result = rtl.setCharAt(Result, 1, "E");',
  3475. ' return Result$1;',
  3476. ' };',
  3477. ' Result = "F";',
  3478. ' Result = rtl.setCharAt(Result, 2, "G");',
  3479. ' return Result;',
  3480. '};'
  3481. ]),
  3482. '');
  3483. end;
  3484. procedure TTestModule.TestForwardProc;
  3485. begin
  3486. StartProgram(false);
  3487. Add('procedure FuncA(Bar: longint); forward;');
  3488. Add('procedure FuncB(Bar: longint);');
  3489. Add('begin');
  3490. Add(' funca(bar);');
  3491. Add('end;');
  3492. Add('procedure funca(bar: longint);');
  3493. Add('begin');
  3494. Add(' if bar=3 then ;');
  3495. Add('end;');
  3496. Add('begin');
  3497. Add(' funca(4);');
  3498. Add(' funcb(5);');
  3499. ConvertProgram;
  3500. CheckSource('TestForwardProc',
  3501. LinesToStr([ // statements'
  3502. 'this.FuncB = function (Bar) {',
  3503. ' $mod.FuncA(Bar);',
  3504. '};',
  3505. 'this.FuncA = function (Bar) {',
  3506. ' if (Bar === 3);',
  3507. '};'
  3508. ]),
  3509. LinesToStr([
  3510. '$mod.FuncA(4);',
  3511. '$mod.FuncB(5);'
  3512. ])
  3513. );
  3514. end;
  3515. procedure TTestModule.TestNestedForwardProc;
  3516. begin
  3517. StartProgram(false);
  3518. Add('procedure FuncA;');
  3519. Add(' procedure FuncB(i: longint); forward;');
  3520. Add(' procedure FuncC(i: longint);');
  3521. Add(' begin');
  3522. Add(' funcb(i);');
  3523. Add(' end;');
  3524. Add(' procedure FuncB(i: longint);');
  3525. Add(' begin');
  3526. Add(' if i=3 then ;');
  3527. Add(' end;');
  3528. Add('begin');
  3529. Add(' funcc(4)');
  3530. Add('end;');
  3531. Add('begin');
  3532. Add(' funca;');
  3533. ConvertProgram;
  3534. CheckSource('TestNestedForwardProc',
  3535. LinesToStr([ // statements'
  3536. 'this.FuncA = function () {',
  3537. ' function FuncC(i) {',
  3538. ' FuncB(i);',
  3539. ' };',
  3540. ' function FuncB(i) {',
  3541. ' if (i === 3);',
  3542. ' };',
  3543. ' FuncC(4);',
  3544. '};'
  3545. ]),
  3546. LinesToStr([
  3547. '$mod.FuncA();'
  3548. ])
  3549. );
  3550. end;
  3551. procedure TTestModule.TestAssignFunctionResult;
  3552. begin
  3553. StartProgram(false);
  3554. Add('function Func1: longint;');
  3555. Add('begin');
  3556. Add('end;');
  3557. Add('var i: longint;');
  3558. Add('begin');
  3559. Add(' i:=func1();');
  3560. Add(' i:=func1()+func1();');
  3561. ConvertProgram;
  3562. CheckSource('TestAssignFunctionResult',
  3563. LinesToStr([ // statements
  3564. 'this.Func1 = function () {',
  3565. ' var Result = 0;',
  3566. ' return Result;',
  3567. '};',
  3568. 'this.i = 0;'
  3569. ]),
  3570. LinesToStr([
  3571. '$mod.i = $mod.Func1();',
  3572. '$mod.i = $mod.Func1() + $mod.Func1();'
  3573. ]));
  3574. end;
  3575. procedure TTestModule.TestFunctionResultInCondition;
  3576. begin
  3577. StartProgram(false);
  3578. Add('function Func1: longint;');
  3579. Add('begin');
  3580. Add('end;');
  3581. Add('function Func2: boolean;');
  3582. Add('begin');
  3583. Add('end;');
  3584. Add('var i: longint;');
  3585. Add('begin');
  3586. Add(' if func2 then ;');
  3587. Add(' if i=func1() then ;');
  3588. Add(' if i=func1 then ;');
  3589. ConvertProgram;
  3590. CheckSource('TestFunctionResultInCondition',
  3591. LinesToStr([ // statements
  3592. 'this.Func1 = function () {',
  3593. ' var Result = 0;',
  3594. ' return Result;',
  3595. '};',
  3596. 'this.Func2 = function () {',
  3597. ' var Result = false;',
  3598. ' return Result;',
  3599. '};',
  3600. 'this.i = 0;'
  3601. ]),
  3602. LinesToStr([
  3603. 'if ($mod.Func2());',
  3604. 'if ($mod.i === $mod.Func1());',
  3605. 'if ($mod.i === $mod.Func1());'
  3606. ]));
  3607. end;
  3608. procedure TTestModule.TestFunctionResultInForLoop;
  3609. begin
  3610. StartProgram(false);
  3611. Add([
  3612. 'function Func1(a: array of longint): longint;',
  3613. 'begin',
  3614. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3615. ' for Result in a do if a[Result]=0 then exit;',
  3616. 'end;',
  3617. 'begin',
  3618. ' Func1([1,2,3])']);
  3619. ConvertProgram;
  3620. CheckSource('TestFunctionResultInForLoop',
  3621. LinesToStr([ // statements
  3622. 'this.Func1 = function (a) {',
  3623. ' var Result = 0;',
  3624. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3625. ' Result = $l;',
  3626. ' if (a[Result] === 0) return Result;',
  3627. ' };',
  3628. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3629. ' Result = $in[$l1];',
  3630. ' if (a[Result] === 0) return Result;',
  3631. ' };',
  3632. ' return Result;',
  3633. '};',
  3634. '']),
  3635. LinesToStr([
  3636. '$mod.Func1([1, 2, 3]);'
  3637. ]));
  3638. end;
  3639. procedure TTestModule.TestFunctionResultInTypeCast;
  3640. begin
  3641. StartProgram(false);
  3642. Add([
  3643. 'function GetInt: longint;',
  3644. 'begin',
  3645. 'end;',
  3646. 'begin',
  3647. ' if Byte(GetInt)=0 then ;',
  3648. '']);
  3649. ConvertProgram;
  3650. CheckSource('TestFunctionResultInTypeCast',
  3651. LinesToStr([ // statements
  3652. 'this.GetInt = function () {',
  3653. ' var Result = 0;',
  3654. ' return Result;',
  3655. '};',
  3656. '']),
  3657. LinesToStr([
  3658. 'if (($mod.GetInt() & 255) === 0) ;'
  3659. ]));
  3660. end;
  3661. procedure TTestModule.TestExit;
  3662. begin
  3663. StartProgram(false);
  3664. Add('procedure ProcA;');
  3665. Add('begin');
  3666. Add(' exit;');
  3667. Add('end;');
  3668. Add('function FuncB: longint;');
  3669. Add('begin');
  3670. Add(' exit;');
  3671. Add(' exit(3);');
  3672. Add('end;');
  3673. Add('function FuncC: string;');
  3674. Add('begin');
  3675. Add(' exit;');
  3676. Add(' exit(''a'');');
  3677. Add(' exit(''abc'');');
  3678. Add('end;');
  3679. Add('begin');
  3680. Add(' exit;');
  3681. Add(' exit(1);');
  3682. ConvertProgram;
  3683. CheckSource('TestExit',
  3684. LinesToStr([ // statements
  3685. 'this.ProcA = function () {',
  3686. ' return;',
  3687. '};',
  3688. 'this.FuncB = function () {',
  3689. ' var Result = 0;',
  3690. ' return Result;',
  3691. ' return 3;',
  3692. ' return Result;',
  3693. '};',
  3694. 'this.FuncC = function () {',
  3695. ' var Result = "";',
  3696. ' return Result;',
  3697. ' return "a";',
  3698. ' return "abc";',
  3699. ' return Result;',
  3700. '};'
  3701. ]),
  3702. LinesToStr([
  3703. 'return;',
  3704. 'return 1;',
  3705. '']));
  3706. end;
  3707. procedure TTestModule.TestExit_ResultInFinally;
  3708. begin
  3709. StartProgram(false);
  3710. Add([
  3711. 'function Run: word;',
  3712. 'begin',
  3713. ' try',
  3714. ' exit(3);', // no Result in finally -> use return 3
  3715. ' finally',
  3716. ' end;',
  3717. 'end;',
  3718. 'function Fly: word;',
  3719. 'begin',
  3720. ' try',
  3721. ' exit(3);',
  3722. ' finally',
  3723. ' if Result>0 then ;',
  3724. ' end;',
  3725. 'end;',
  3726. 'function Jump: word;',
  3727. 'begin',
  3728. ' try',
  3729. ' try',
  3730. ' exit(4);',
  3731. ' finally',
  3732. ' end;',
  3733. ' finally',
  3734. ' if Result>0 then ;',
  3735. ' end;',
  3736. 'end;',
  3737. 'begin',
  3738. '']);
  3739. ConvertProgram;
  3740. CheckSource('TestExit_ResultInFinally',
  3741. LinesToStr([ // statements
  3742. 'this.Run = function () {',
  3743. ' var Result = 0;',
  3744. ' try {',
  3745. ' return 3;',
  3746. ' } finally {',
  3747. ' };',
  3748. ' return Result;',
  3749. '};',
  3750. 'this.Fly = function () {',
  3751. ' var Result = 0;',
  3752. ' try {',
  3753. ' Result = 3;',
  3754. ' return Result;',
  3755. ' } finally {',
  3756. ' if (Result > 0) ;',
  3757. ' };',
  3758. ' return Result;',
  3759. '};',
  3760. 'this.Jump = function () {',
  3761. ' var Result = 0;',
  3762. ' try {',
  3763. ' try {',
  3764. ' Result = 4;',
  3765. ' return Result;',
  3766. ' } finally {',
  3767. ' };',
  3768. ' } finally {',
  3769. ' if (Result > 0) ;',
  3770. ' };',
  3771. ' return Result;',
  3772. '};',
  3773. '']),
  3774. LinesToStr([
  3775. '']));
  3776. end;
  3777. procedure TTestModule.TestBreak;
  3778. begin
  3779. StartProgram(false);
  3780. Add([
  3781. 'var',
  3782. ' i: longint;',
  3783. 'begin',
  3784. ' repeat',
  3785. ' break;',
  3786. ' until true;',
  3787. ' while true do',
  3788. ' break;',
  3789. ' for i:=1 to 2 do',
  3790. ' break;']);
  3791. ConvertProgram;
  3792. CheckSource('TestBreak',
  3793. LinesToStr([ // statements
  3794. 'this.i = 0;'
  3795. ]),
  3796. LinesToStr([
  3797. 'do {',
  3798. ' break;',
  3799. '} while (!true);',
  3800. 'while (true) break;',
  3801. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3802. '']));
  3803. end;
  3804. procedure TTestModule.TestBreakAsVar;
  3805. begin
  3806. StartProgram(false);
  3807. Add([
  3808. 'procedure DoIt(break: boolean);',
  3809. 'begin',
  3810. ' if break then ;',
  3811. 'end;',
  3812. 'var',
  3813. ' break: boolean;',
  3814. 'begin',
  3815. ' if break then ;']);
  3816. ConvertProgram;
  3817. CheckSource('TestBreakAsVar',
  3818. LinesToStr([ // statements
  3819. 'this.DoIt = function (Break) {',
  3820. ' if (Break) ;',
  3821. '};',
  3822. 'this.Break = false;',
  3823. '']),
  3824. LinesToStr([
  3825. 'if($mod.Break) ;',
  3826. '']));
  3827. end;
  3828. procedure TTestModule.TestContinue;
  3829. begin
  3830. StartProgram(false);
  3831. Add('var i: longint;');
  3832. Add('begin');
  3833. Add(' repeat');
  3834. Add(' continue;');
  3835. Add(' until true;');
  3836. Add(' while true do');
  3837. Add(' continue;');
  3838. Add(' for i:=1 to 2 do');
  3839. Add(' continue;');
  3840. ConvertProgram;
  3841. CheckSource('TestContinue',
  3842. LinesToStr([ // statements
  3843. 'this.i = 0;'
  3844. ]),
  3845. LinesToStr([
  3846. 'do {',
  3847. ' continue;',
  3848. '} while (!true);',
  3849. 'while (true) continue;',
  3850. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3851. '']));
  3852. end;
  3853. procedure TTestModule.TestProc_External;
  3854. begin
  3855. StartProgram(false);
  3856. Add('procedure Foo; external name ''console.log'';');
  3857. Add('function Bar: longint; external name ''get.item'';');
  3858. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3859. Add('var');
  3860. Add(' i: longint;');
  3861. Add('begin');
  3862. Add(' Foo;');
  3863. Add(' i:=Bar;');
  3864. Add(' i:=Bla(''abc'');');
  3865. ConvertProgram;
  3866. CheckSource('TestProc_External',
  3867. LinesToStr([ // statements
  3868. 'this.i = 0;'
  3869. ]),
  3870. LinesToStr([
  3871. 'console.log();',
  3872. '$mod.i = get.item();',
  3873. '$mod.i = apply.something("abc");'
  3874. ]));
  3875. end;
  3876. procedure TTestModule.TestProc_ExternalOtherUnit;
  3877. begin
  3878. AddModuleWithIntfImplSrc('unit2.pas',
  3879. LinesToStr([
  3880. 'procedure Now; external name ''Date.now'';',
  3881. 'procedure DoIt;'
  3882. ]),
  3883. 'procedure doit; begin end;');
  3884. StartUnit(true);
  3885. Add('interface');
  3886. Add('uses unit2;');
  3887. Add('implementation');
  3888. Add('begin');
  3889. Add(' now;');
  3890. Add(' now();');
  3891. Add(' uNit2.now;');
  3892. Add(' uNit2.now();');
  3893. Add(' doit;');
  3894. Add(' uNit2.doit;');
  3895. ConvertUnit;
  3896. CheckSource('TestProc_ExternalOtherUnit',
  3897. LinesToStr([
  3898. '']),
  3899. LinesToStr([
  3900. 'Date.now();',
  3901. 'Date.now();',
  3902. 'Date.now();',
  3903. 'Date.now();',
  3904. 'pas.unit2.DoIt();',
  3905. 'pas.unit2.DoIt();',
  3906. '']));
  3907. end;
  3908. procedure TTestModule.TestProc_Asm;
  3909. begin
  3910. StartProgram(false);
  3911. Add([
  3912. '{$mode delphi}',
  3913. 'function DoIt: longint;',
  3914. 'begin;',
  3915. ' asm',
  3916. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3917. ' end;',
  3918. ' asm console.log(); end;',
  3919. ' asm',
  3920. ' s = "'' ";',
  3921. ' s = ''" '';',
  3922. ' s = s + "world" + "''";',
  3923. ' // end',
  3924. ' s = ''end'';',
  3925. ' s = "end";',
  3926. ' s = "foo\"bar";',
  3927. ' s = ''a\''b'';',
  3928. ' s = `${expr}\`-"-''-`;',
  3929. ' s = `multi',
  3930. 'line`;',
  3931. ' end;',
  3932. 'end;',
  3933. 'procedure Fly;',
  3934. 'asm',
  3935. ' return;',
  3936. 'end;',
  3937. 'begin']);
  3938. ConvertProgram;
  3939. CheckSource('TestProc_Asm',
  3940. LinesToStr([ // statements
  3941. 'this.DoIt = function () {',
  3942. ' var Result = 0;',
  3943. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3944. ' console.log();',
  3945. ' s = "'' ";',
  3946. ' s = ''" '';',
  3947. ' s = s + "world" + "''";',
  3948. ' // end',
  3949. ' s = ''end'';',
  3950. ' s = "end";',
  3951. ' s = "foo\"bar";',
  3952. ' s = ''a\''b'';',
  3953. ' s = `${expr}\`-"-''-`;',
  3954. ' s = `multi',
  3955. 'line`;',
  3956. ' return Result;',
  3957. '};',
  3958. 'this.Fly = function () {',
  3959. ' return;',
  3960. '};',
  3961. '']),
  3962. LinesToStr([
  3963. ''
  3964. ]));
  3965. end;
  3966. procedure TTestModule.TestProc_AsmSubBlock;
  3967. begin
  3968. StartProgram(true,[supTObject]);
  3969. Add([
  3970. '{$mode delphi}',
  3971. 'type',
  3972. ' TBird = class end;',
  3973. 'procedure Run(w: word);',
  3974. 'begin;',
  3975. ' if true then asm console.log(); end;',
  3976. ' if w>3 then asm',
  3977. ' var a = w+1;',
  3978. ' w = a+3;',
  3979. ' end;',
  3980. ' while (w>7) do asm',
  3981. ' w+=3; w*=2;',
  3982. ' end;',
  3983. ' try',
  3984. ' except',
  3985. ' on E: TBird do',
  3986. ' asm console.log(E); end;',
  3987. ' on E: TObject do',
  3988. ' asm var i=3; i--; end;',
  3989. ' else asm Fly; High; end;',
  3990. ' end;',
  3991. 'end;',
  3992. 'begin']);
  3993. ConvertProgram;
  3994. CheckSource('TestProc_AsmSubBlock',
  3995. LinesToStr([ // statements
  3996. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3997. '});',
  3998. 'this.Run = function (w) {',
  3999. ' if (true) console.log();',
  4000. ' if (w > 3) {',
  4001. ' var a = w+1;',
  4002. ' w = a+3;',
  4003. ' };',
  4004. ' while (w > 7) {',
  4005. ' w+=3; w*=2;',
  4006. ' };',
  4007. ' try {} catch ($e) {',
  4008. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4009. ' var E = $e;',
  4010. ' console.log(E);',
  4011. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4012. ' var E = $e;',
  4013. ' var i=3; i--;',
  4014. ' } else {',
  4015. ' Fly; High;',
  4016. ' }',
  4017. ' };',
  4018. '};',
  4019. '']),
  4020. LinesToStr([
  4021. ''
  4022. ]));
  4023. end;
  4024. procedure TTestModule.TestProc_Assembler;
  4025. begin
  4026. StartProgram(false);
  4027. Add('function DoIt: longint; assembler;');
  4028. Add('asm');
  4029. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4030. Add('end;');
  4031. Add('begin');
  4032. ConvertProgram;
  4033. CheckSource('TestProc_Assembler',
  4034. LinesToStr([ // statements
  4035. 'this.DoIt = function () {',
  4036. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4037. '};'
  4038. ]),
  4039. LinesToStr([
  4040. ''
  4041. ]));
  4042. end;
  4043. procedure TTestModule.TestProc_VarParam;
  4044. begin
  4045. StartProgram(false);
  4046. Add('type integer = longint;');
  4047. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4048. Add('var vJ: integer;');
  4049. Add('begin');
  4050. Add(' vg:=vg+1;');
  4051. Add(' vj:=vh+2;');
  4052. Add(' vi:=vi+3;');
  4053. Add(' doit(vg,vg,vg);');
  4054. Add(' doit(vh,vh,vj);');
  4055. Add(' doit(vi,vi,vi);');
  4056. Add(' doit(vj,vj,vj);');
  4057. Add('end;');
  4058. Add('var i: integer;');
  4059. Add('begin');
  4060. Add(' doit(i,i,i);');
  4061. ConvertProgram;
  4062. CheckSource('TestProc_VarParam',
  4063. LinesToStr([ // statements
  4064. 'this.DoIt = function (vG,vH,vI) {',
  4065. ' var vJ = 0;',
  4066. ' vG = vG + 1;',
  4067. ' vJ = vH + 2;',
  4068. ' vI.set(vI.get()+3);',
  4069. ' $mod.DoIt(vG, vG, {',
  4070. ' get: function () {',
  4071. ' return vG;',
  4072. ' },',
  4073. ' set: function (v) {',
  4074. ' vG = v;',
  4075. ' }',
  4076. ' });',
  4077. ' $mod.DoIt(vH, vH, {',
  4078. ' get: function () {',
  4079. ' return vJ;',
  4080. ' },',
  4081. ' set: function (v) {',
  4082. ' vJ = v;',
  4083. ' }',
  4084. ' });',
  4085. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4086. ' $mod.DoIt(vJ, vJ, {',
  4087. ' get: function () {',
  4088. ' return vJ;',
  4089. ' },',
  4090. ' set: function (v) {',
  4091. ' vJ = v;',
  4092. ' }',
  4093. ' });',
  4094. '};',
  4095. 'this.i = 0;'
  4096. ]),
  4097. LinesToStr([
  4098. '$mod.DoIt($mod.i,$mod.i,{',
  4099. ' p: $mod,',
  4100. ' get: function () {',
  4101. ' return this.p.i;',
  4102. ' },',
  4103. ' set: function (v) {',
  4104. ' this.p.i = v;',
  4105. ' }',
  4106. '});'
  4107. ]));
  4108. end;
  4109. procedure TTestModule.TestProc_VarParamString;
  4110. begin
  4111. StartProgram(false);
  4112. Add(['type TCaption = string;',
  4113. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4114. 'var c: char;',
  4115. 'begin',
  4116. ' va[1]:=c;',
  4117. ' vb[2]:=c;',
  4118. ' vc[3]:=c;',
  4119. 'end;',
  4120. 'begin']);
  4121. ConvertProgram;
  4122. CheckSource('TestProc_VarParamString',
  4123. LinesToStr([ // statements
  4124. 'this.DoIt = function (vA,vB,vC) {',
  4125. ' var c = "";',
  4126. ' vA = rtl.setCharAt(vA, 0, c);',
  4127. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4128. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4129. '};',
  4130. '']),
  4131. LinesToStr([
  4132. ]));
  4133. end;
  4134. procedure TTestModule.TestProc_VarParamV;
  4135. begin
  4136. StartProgram(false);
  4137. Add([
  4138. 'procedure Inc2(var i: longint);',
  4139. 'begin',
  4140. ' i:=i+2;',
  4141. 'end;',
  4142. 'procedure DoIt(v: longint);',
  4143. 'var p: array of longint;',
  4144. 'begin',
  4145. ' Inc2(v);',
  4146. ' Inc2(p[v]);',
  4147. 'end;',
  4148. 'begin']);
  4149. ConvertProgram;
  4150. CheckSource('TestProc_VarParamV',
  4151. LinesToStr([ // statements
  4152. 'this.Inc2 = function (i) {',
  4153. ' i.set(i.get()+2);',
  4154. '};',
  4155. 'this.DoIt = function (v) {',
  4156. ' var p = [];',
  4157. ' $mod.Inc2({get: function () {',
  4158. ' return v;',
  4159. ' }, set: function (w) {',
  4160. ' v = w;',
  4161. ' }});',
  4162. ' $mod.Inc2({',
  4163. ' a: v,',
  4164. ' p: p,',
  4165. ' get: function () {',
  4166. ' return this.p[this.a];',
  4167. ' },',
  4168. ' set: function (v) {',
  4169. ' this.p[this.a] = v;',
  4170. ' }',
  4171. ' });',
  4172. '};',
  4173. '']),
  4174. LinesToStr([
  4175. '']));
  4176. end;
  4177. procedure TTestModule.TestProc_Overload;
  4178. begin
  4179. StartProgram(false);
  4180. Add('procedure DoIt(vI: longint); begin end;');
  4181. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4182. Add('procedure DoIt(vD: double); begin end;');
  4183. Add('begin');
  4184. Add(' DoIt(1);');
  4185. Add(' DoIt(2,3);');
  4186. Add(' DoIt(4.5);');
  4187. ConvertProgram;
  4188. CheckSource('TestProcedureOverload',
  4189. LinesToStr([ // statements
  4190. 'this.DoIt = function (vI) {',
  4191. '};',
  4192. 'this.DoIt$1 = function (vI, vJ) {',
  4193. '};',
  4194. 'this.DoIt$2 = function (vD) {',
  4195. '};',
  4196. '']),
  4197. LinesToStr([
  4198. '$mod.DoIt(1);',
  4199. '$mod.DoIt$1(2, 3);',
  4200. '$mod.DoIt$2(4.5);',
  4201. '']));
  4202. end;
  4203. procedure TTestModule.TestProc_OverloadForward;
  4204. begin
  4205. StartProgram(false);
  4206. Add('procedure DoIt(vI: longint); forward;');
  4207. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4208. Add('procedure doit(vi: longint); begin end;');
  4209. Add('begin');
  4210. Add(' doit(1);');
  4211. Add(' doit(2,3);');
  4212. ConvertProgram;
  4213. CheckSource('TestProcedureOverloadForward',
  4214. LinesToStr([ // statements
  4215. 'this.DoIt$1 = function (vI, vJ) {',
  4216. '};',
  4217. 'this.DoIt = function (vI) {',
  4218. '};',
  4219. '']),
  4220. LinesToStr([
  4221. '$mod.DoIt(1);',
  4222. '$mod.DoIt$1(2, 3);',
  4223. '']));
  4224. end;
  4225. procedure TTestModule.TestProc_OverloadIntfImpl;
  4226. begin
  4227. StartUnit(false);
  4228. Add('interface');
  4229. Add('procedure DoIt(vI: longint);');
  4230. Add('procedure DoIt(vI, vJ: longint);');
  4231. Add('implementation');
  4232. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4233. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4234. Add('procedure DoIt(vi: longint); begin end;');
  4235. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4236. Add('procedure DoIt(vi, vj: longint); begin end;');
  4237. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4238. Add('begin');
  4239. Add(' doit(1);');
  4240. Add(' doit(2,3);');
  4241. Add(' doit(4,5,6);');
  4242. Add(' doit(7,8,9,10);');
  4243. Add(' doit(11,12,13,14,15);');
  4244. ConvertUnit;
  4245. CheckSource('TestProcedureOverloadUnit',
  4246. LinesToStr([ // statements
  4247. 'var $impl = $mod.$impl;',
  4248. 'this.DoIt = function (vI) {',
  4249. '};',
  4250. 'this.DoIt$1 = function (vI, vJ) {',
  4251. '};',
  4252. '']),
  4253. LinesToStr([ // this.$init
  4254. '$mod.DoIt(1);',
  4255. '$mod.DoIt$1(2, 3);',
  4256. '$impl.DoIt$3(4,5,6);',
  4257. '$impl.DoIt$4(7,8,9,10);',
  4258. '$impl.DoIt$2(11,12,13,14,15);',
  4259. '']),
  4260. LinesToStr([ // implementation
  4261. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4262. '};',
  4263. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4264. '};',
  4265. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4266. '};',
  4267. '']));
  4268. end;
  4269. procedure TTestModule.TestProc_OverloadNested;
  4270. begin
  4271. StartProgram(false);
  4272. Add([
  4273. 'procedure doit(vA: longint);',
  4274. ' procedure DoIt(vA, vB: longint); overload;',
  4275. ' begin',
  4276. ' doit(1);',
  4277. ' doit(1,2);',
  4278. ' end;',
  4279. ' procedure doit(vA, vB, vC: longint);',
  4280. ' begin',
  4281. ' doit(1);',
  4282. ' doit(1,2);',
  4283. ' doit(1,2,3);',
  4284. ' end;',
  4285. 'begin',
  4286. ' doit(1);',
  4287. ' doit(1,2);',
  4288. ' doit(1,2,3);',
  4289. 'end;',
  4290. 'begin // main',
  4291. ' doit(1);']);
  4292. ConvertProgram;
  4293. CheckSource('TestProcedureOverloadNested',
  4294. LinesToStr([ // statements
  4295. 'this.doit = function (vA) {',
  4296. ' function DoIt$1(vA, vB) {',
  4297. ' $mod.doit(1);',
  4298. ' DoIt$1(1, 2);',
  4299. ' };',
  4300. ' function doit$2(vA, vB, vC) {',
  4301. ' $mod.doit(1);',
  4302. ' DoIt$1(1, 2);',
  4303. ' doit$2(1, 2, 3);',
  4304. ' };',
  4305. ' $mod.doit(1);',
  4306. ' DoIt$1(1, 2);',
  4307. ' doit$2(1, 2, 3);',
  4308. '};',
  4309. '']),
  4310. LinesToStr([
  4311. '$mod.doit(1);',
  4312. '']));
  4313. end;
  4314. procedure TTestModule.TestProc_OverloadNestedForward;
  4315. begin
  4316. StartProgram(false);
  4317. Add([
  4318. 'procedure DoIt(vA: longint); overload; forward;',
  4319. 'procedure DoIt(vB, vC: longint); overload;',
  4320. 'begin // 2 param overload',
  4321. ' doit(1);',
  4322. ' doit(1,2);',
  4323. 'end;',
  4324. 'procedure doit(vA: longint);',
  4325. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4326. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4327. ' begin // 4 param overload',
  4328. ' doit(1);',
  4329. ' doit(1,2);',
  4330. ' doit(1,2,3);',
  4331. ' doit(1,2,3,4);',
  4332. ' end;',
  4333. ' procedure doit(vA, vB, vC: longint);',
  4334. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4335. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4336. ' begin // 6 param overload',
  4337. ' doit(1);',
  4338. ' doit(1,2);',
  4339. ' doit(1,2,3);',
  4340. ' doit(1,2,3,4);',
  4341. ' doit(1,2,3,4,5);',
  4342. ' doit(1,2,3,4,5,6);',
  4343. ' end;',
  4344. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4345. ' begin // 5 param overload',
  4346. ' doit(1);',
  4347. ' doit(1,2);',
  4348. ' doit(1,2,3);',
  4349. ' doit(1,2,3,4);',
  4350. ' doit(1,2,3,4,5);',
  4351. ' doit(1,2,3,4,5,6);',
  4352. ' end;',
  4353. ' begin // 3 param overload',
  4354. ' doit(1);',
  4355. ' doit(1,2);',
  4356. ' doit(1,2,3);',
  4357. ' doit(1,2,3,4);',
  4358. ' doit(1,2,3,4,5);',
  4359. ' doit(1,2,3,4,5,6);',
  4360. ' end;',
  4361. 'begin // 1 param overload',
  4362. ' doit(1);',
  4363. ' doit(1,2);',
  4364. ' doit(1,2,3);',
  4365. ' doit(1,2,3,4);',
  4366. 'end;',
  4367. 'begin // main',
  4368. ' doit(1);',
  4369. ' doit(1,2);']);
  4370. ConvertProgram;
  4371. CheckSource('TestProc_OverloadNestedForward',
  4372. LinesToStr([ // statements
  4373. 'this.DoIt$1 = function (vB, vC) {',
  4374. ' $mod.DoIt(1);',
  4375. ' $mod.DoIt$1(1, 2);',
  4376. '};',
  4377. 'this.DoIt = function (vA) {',
  4378. ' function DoIt$3(vA, vB, vC, vD) {',
  4379. ' $mod.DoIt(1);',
  4380. ' $mod.DoIt$1(1, 2);',
  4381. ' DoIt$2(1, 2, 3);',
  4382. ' DoIt$3(1, 2, 3, 4);',
  4383. ' };',
  4384. ' function DoIt$2(vA, vB, vC) {',
  4385. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4386. ' $mod.DoIt(1);',
  4387. ' $mod.DoIt$1(1, 2);',
  4388. ' DoIt$2(1, 2, 3);',
  4389. ' DoIt$3(1, 2, 3, 4);',
  4390. ' DoIt$4(1, 2, 3, 4, 5);',
  4391. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4392. ' };',
  4393. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4394. ' $mod.DoIt(1);',
  4395. ' $mod.DoIt$1(1, 2);',
  4396. ' DoIt$2(1, 2, 3);',
  4397. ' DoIt$3(1, 2, 3, 4);',
  4398. ' DoIt$4(1, 2, 3, 4, 5);',
  4399. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4400. ' };',
  4401. ' $mod.DoIt(1);',
  4402. ' $mod.DoIt$1(1, 2);',
  4403. ' DoIt$2(1, 2, 3);',
  4404. ' DoIt$3(1, 2, 3, 4);',
  4405. ' DoIt$4(1, 2, 3, 4, 5);',
  4406. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4407. ' };',
  4408. ' $mod.DoIt(1);',
  4409. ' $mod.DoIt$1(1, 2);',
  4410. ' DoIt$2(1, 2, 3);',
  4411. ' DoIt$3(1, 2, 3, 4);',
  4412. '};',
  4413. '']),
  4414. LinesToStr([
  4415. '$mod.DoIt(1);',
  4416. '$mod.DoIt$1(1, 2);',
  4417. '']));
  4418. end;
  4419. procedure TTestModule.TestProc_OverloadUnitCycle;
  4420. begin
  4421. AddModuleWithIntfImplSrc('Unit2.pas',
  4422. LinesToStr([
  4423. 'type',
  4424. ' TObject = class',
  4425. ' procedure DoIt(b: boolean); virtual; abstract;',
  4426. ' procedure DoIt(i: longint); virtual; abstract;',
  4427. ' end;',
  4428. '']),
  4429. 'uses test1;');
  4430. StartUnit(true);
  4431. Add([
  4432. 'interface',
  4433. 'uses unit2;',
  4434. 'type',
  4435. ' TEagle = class(TObject)',
  4436. ' procedure DoIt(b: boolean); override;',
  4437. ' procedure DoIt(i: longint); override;',
  4438. ' end;',
  4439. 'implementation',
  4440. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4441. 'procedure TEagle.DoIt(i: longint); begin end;',
  4442. '']);
  4443. ConvertUnit;
  4444. CheckSource('TestProc_OverloadUnitCycle',
  4445. LinesToStr([ // statements
  4446. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4447. ' this.DoIt = function (b) {',
  4448. ' };',
  4449. ' this.DoIt$1 = function (i) {',
  4450. ' };',
  4451. '});',
  4452. '']),
  4453. '',
  4454. LinesToStr([
  4455. '']));
  4456. end;
  4457. procedure TTestModule.TestProc_Varargs;
  4458. begin
  4459. StartProgram(false);
  4460. Add([
  4461. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4462. 'procedure ProcB; varargs; external name ''ProcB'';',
  4463. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4464. 'function GetIt: longint; begin end;',
  4465. 'begin',
  4466. ' ProcA(1);',
  4467. ' ProcA(1,2);',
  4468. ' ProcA(1,2.0);',
  4469. ' ProcA(1,2,3);',
  4470. ' ProcA(1,''2'');',
  4471. ' ProcA(2,'''');',
  4472. ' ProcA(3,false);',
  4473. ' ProcB;',
  4474. ' ProcB();',
  4475. ' ProcB(4);',
  4476. ' ProcB(''foo'');',
  4477. ' ProcC;',
  4478. ' ProcC();',
  4479. ' ProcC(4);',
  4480. ' ProcC(5,''foo'');',
  4481. ' ProcB(GetIt);',
  4482. ' ProcB(GetIt());',
  4483. ' ProcB(GetIt,GetIt());']);
  4484. ConvertProgram;
  4485. CheckSource('TestProc_Varargs',
  4486. LinesToStr([ // statements
  4487. 'this.GetIt = function () {',
  4488. ' var Result = 0;',
  4489. ' return Result;',
  4490. '};',
  4491. '']),
  4492. LinesToStr([
  4493. 'ProcA(1);',
  4494. 'ProcA(1, 2);',
  4495. 'ProcA(1, 2.0);',
  4496. 'ProcA(1, 2, 3);',
  4497. 'ProcA(1, "2");',
  4498. 'ProcA(2, "");',
  4499. 'ProcA(3, false);',
  4500. 'ProcB();',
  4501. 'ProcB();',
  4502. 'ProcB(4);',
  4503. 'ProcB("foo");',
  4504. 'ProcC(17);',
  4505. 'ProcC(17);',
  4506. 'ProcC(4);',
  4507. 'ProcC(5, "foo");',
  4508. 'ProcB($mod.GetIt());',
  4509. 'ProcB($mod.GetIt());',
  4510. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4511. '']));
  4512. end;
  4513. procedure TTestModule.TestProc_ConstOrder;
  4514. begin
  4515. StartProgram(false);
  4516. Add([
  4517. 'const A = 3;',
  4518. 'const B = A+1;',
  4519. 'procedure DoIt;',
  4520. 'const C = A+1;',
  4521. 'const D = B+1;',
  4522. 'const E = D+C+B+A;',
  4523. 'begin',
  4524. 'end;',
  4525. 'begin'
  4526. ]);
  4527. ConvertProgram;
  4528. CheckSource('TestProc_ConstOrder',
  4529. LinesToStr([ // statements
  4530. 'this.A = 3;',
  4531. 'this.B = 3 + 1;',
  4532. 'var C = 3 + 1;',
  4533. 'var D = 4 + 1;',
  4534. 'var E = 5 + 4 + 4 + 3;',
  4535. 'this.DoIt = function () {',
  4536. '};',
  4537. '']),
  4538. LinesToStr([
  4539. ''
  4540. ]));
  4541. end;
  4542. procedure TTestModule.TestProc_DuplicateConst;
  4543. begin
  4544. StartProgram(false);
  4545. Add([
  4546. 'const A = 1;',
  4547. 'procedure DoIt;',
  4548. 'const A = 2;',
  4549. ' procedure SubIt;',
  4550. ' const A = 21;',
  4551. ' begin',
  4552. ' end;',
  4553. 'begin',
  4554. 'end;',
  4555. 'procedure DoSome;',
  4556. 'const A = 3;',
  4557. 'begin',
  4558. 'end;',
  4559. 'begin'
  4560. ]);
  4561. ConvertProgram;
  4562. CheckSource('TestProc_DuplicateConst',
  4563. LinesToStr([ // statements
  4564. 'this.A = 1;',
  4565. 'var A$1 = 2;',
  4566. 'var A$2 = 21;',
  4567. 'this.DoIt = function () {',
  4568. ' function SubIt() {',
  4569. ' };',
  4570. '};',
  4571. 'var A$3 = 3;',
  4572. 'this.DoSome = function () {',
  4573. '};',
  4574. '']),
  4575. LinesToStr([
  4576. ''
  4577. ]));
  4578. end;
  4579. procedure TTestModule.TestProc_LocalVarAbsolute;
  4580. begin
  4581. StartProgram(false);
  4582. Add([
  4583. 'type',
  4584. ' TObject = class',
  4585. ' Index: longint;',
  4586. ' procedure DoAbs(Item: pointer);',
  4587. ' end;',
  4588. 'procedure TObject.DoAbs(Item: pointer);',
  4589. 'var',
  4590. ' o: TObject absolute Item;',
  4591. 'begin',
  4592. ' if o.Index<o.Index then o.Index:=o.Index;',
  4593. 'end;',
  4594. 'procedure DoIt(i: longint; p: pointer);',
  4595. 'var',
  4596. ' d: double absolute i;',
  4597. ' s: string absolute d;',
  4598. ' oi: TObject absolute i;',
  4599. ' op: TObject absolute p;',
  4600. 'begin',
  4601. ' if d=d then d:=d;',
  4602. ' if s=s then s:=s;',
  4603. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4604. ' if op.Index=op.Index then op.Index:=op.Index;',
  4605. 'end;',
  4606. 'begin']);
  4607. ConvertProgram;
  4608. CheckSource('TestProc_LocalVarAbsolute',
  4609. LinesToStr([ // statements
  4610. 'rtl.createClass(this, "TObject", null, function () {',
  4611. ' this.$init = function () {',
  4612. ' this.Index = 0;',
  4613. ' };',
  4614. ' this.$final = function () {',
  4615. ' };',
  4616. ' this.DoAbs = function (Item) {',
  4617. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4618. ' };',
  4619. '});',
  4620. 'this.DoIt = function (i, p) {',
  4621. ' if (i === i) i = i;',
  4622. ' if (i === i) i = i;',
  4623. ' if (i.Index < i.Index) i.Index = i.Index;',
  4624. ' if (p.Index === p.Index) p.Index = p.Index;',
  4625. '};'
  4626. ]),
  4627. LinesToStr([
  4628. ]));
  4629. end;
  4630. procedure TTestModule.TestProc_LocalVarInit;
  4631. begin
  4632. StartProgram(false);
  4633. Add([
  4634. 'type TBytes = array of byte;',
  4635. 'procedure DoIt;',
  4636. 'const c = 4;',
  4637. 'var',
  4638. ' b: byte = 1;',
  4639. ' w: word = 2+c;',
  4640. ' p: pointer = nil;',
  4641. ' Buffer: TBytes = nil;',
  4642. 'begin',
  4643. 'end;',
  4644. 'begin']);
  4645. ConvertProgram;
  4646. CheckSource('TestProc_LocalVarInit',
  4647. LinesToStr([ // statements
  4648. 'var c = 4;',
  4649. 'this.DoIt = function () {',
  4650. ' var b = 1;',
  4651. ' var w = 2 + 4;',
  4652. ' var p = null;',
  4653. ' var Buffer = [];',
  4654. '};',
  4655. '']),
  4656. LinesToStr([
  4657. ]));
  4658. end;
  4659. procedure TTestModule.TestProc_ReservedWords;
  4660. begin
  4661. StartProgram(false);
  4662. Add([
  4663. 'procedure Date(ArrayBuffer: longint);',
  4664. 'const',
  4665. ' NaN: longint = 3;',
  4666. 'var',
  4667. ' &Boolean: longint;',
  4668. ' procedure Error(ArrayBuffer: longint);',
  4669. ' begin',
  4670. ' end;',
  4671. 'begin',
  4672. ' Nan:=&bOolean;',
  4673. 'end;',
  4674. 'begin',
  4675. ' Date(1);']);
  4676. ConvertProgram;
  4677. CheckSource('TestProc_ReservedWords',
  4678. LinesToStr([ // statements
  4679. 'var naN = 3;',
  4680. 'this.Date = function (arrayBuffer) {',
  4681. ' var boolean = 0;',
  4682. ' function error(arrayBuffer) {',
  4683. ' };',
  4684. ' naN = boolean;',
  4685. '};',
  4686. '']),
  4687. LinesToStr([
  4688. ' $mod.Date(1);'
  4689. ]));
  4690. end;
  4691. procedure TTestModule.TestProc_ConstRefWord;
  4692. begin
  4693. StartProgram(false);
  4694. Add([
  4695. 'procedure Run(constref w: word);',
  4696. 'var l: word;',
  4697. 'begin',
  4698. ' l:=w;',
  4699. ' Run(w);',
  4700. ' Run(l);',
  4701. 'end;',
  4702. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4703. 'begin',
  4704. ' Run(a);',
  4705. ' Run(b);',
  4706. ' Run(c);',
  4707. ' Run(d);',
  4708. ' Run(e);',
  4709. 'end;',
  4710. 'begin',
  4711. ' Run(1);']);
  4712. ConvertProgram;
  4713. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4714. CheckSource('TestProc_ConstRefWord',
  4715. LinesToStr([ // statements
  4716. 'this.Run = function (w) {',
  4717. ' var l = 0;',
  4718. ' l = w;',
  4719. ' $mod.Run(w);',
  4720. ' $mod.Run(l);',
  4721. '};',
  4722. 'this.Fly = function (a, b, c, d, e) {',
  4723. ' $mod.Run(a);',
  4724. ' $mod.Run(b.get());',
  4725. ' $mod.Run(c.get());',
  4726. ' $mod.Run(d);',
  4727. ' $mod.Run(e);',
  4728. '};',
  4729. '']),
  4730. LinesToStr([
  4731. '$mod.Run(1);'
  4732. ]));
  4733. end;
  4734. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4735. begin
  4736. StartProgram(false);
  4737. Add([
  4738. '{$mode objfpc}',
  4739. 'type',
  4740. ' TFunc = reference to function(x: word): word;',
  4741. 'var Func: TFunc;',
  4742. 'procedure DoIt(a: word);',
  4743. 'begin',
  4744. ' Func:=function(b:word): word',
  4745. ' begin',
  4746. ' Result:=a+b;',
  4747. ' exit(b);',
  4748. ' exit(Result);',
  4749. ' end;',// test semicolon
  4750. ' a:=3;',
  4751. 'end;',
  4752. 'begin',
  4753. ' Func:=function(c:word):word begin',
  4754. ' Result:=3+c;',
  4755. ' exit(c);',
  4756. ' exit(Result);',
  4757. ' end;']);
  4758. ConvertProgram;
  4759. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4760. LinesToStr([ // statements
  4761. 'this.Func = null;',
  4762. 'this.DoIt = function (a) {',
  4763. ' $mod.Func = function (b) {',
  4764. ' var Result = 0;',
  4765. ' Result = a + b;',
  4766. ' return b;',
  4767. ' return Result;',
  4768. ' return Result;',
  4769. ' };',
  4770. ' a = 3;',
  4771. '};',
  4772. '']),
  4773. LinesToStr([
  4774. '$mod.Func = function (c) {',
  4775. ' var Result = 0;',
  4776. ' Result = 3 + c;',
  4777. ' return c;',
  4778. ' return Result;',
  4779. ' return Result;',
  4780. '};',
  4781. '']));
  4782. end;
  4783. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4784. begin
  4785. StartProgram(false);
  4786. Add([
  4787. '{$mode delphi}',
  4788. 'type',
  4789. ' TProc = reference to procedure(x: word);',
  4790. 'procedure DoIt(a: word);',
  4791. 'var Proc: TProc;',
  4792. 'begin',
  4793. ' Proc:=procedure(b:word) begin end;',
  4794. 'end;',
  4795. 'var Proc: TProc;',
  4796. 'begin',
  4797. ' Proc:=procedure(c:word) begin end;',
  4798. '']);
  4799. ConvertProgram;
  4800. CheckSource('TestAnonymousProc_Assign_Delphi',
  4801. LinesToStr([ // statements
  4802. 'this.DoIt = function (a) {',
  4803. ' var Proc = null;',
  4804. ' Proc = function (b) {',
  4805. ' };',
  4806. '};',
  4807. 'this.Proc = null;',
  4808. '']),
  4809. LinesToStr([
  4810. '$mod.Proc = function (c) {',
  4811. '};',
  4812. '']));
  4813. end;
  4814. procedure TTestModule.TestAnonymousProc_Arg;
  4815. begin
  4816. StartProgram(false);
  4817. Add([
  4818. 'type',
  4819. ' TProc = reference to procedure;',
  4820. ' TFunc = reference to function(x: word): word;',
  4821. 'procedure DoMore(f,g: TProc);',
  4822. 'begin',
  4823. 'end;',
  4824. 'procedure DoOdd(v: jsvalue);',
  4825. 'begin',
  4826. 'end;',
  4827. 'procedure DoIt(f: TFunc);',
  4828. 'begin',
  4829. ' DoIt(function(b:word): word',
  4830. ' begin',
  4831. ' Result:=1+b;',
  4832. ' end);',
  4833. ' DoMore(procedure begin end, procedure begin end);',
  4834. ' DoOdd(procedure begin end);',
  4835. 'end;',
  4836. 'begin',
  4837. ' DoMore(procedure begin end,',
  4838. ' procedure assembler asm',
  4839. ' console.log("c");',
  4840. ' end);',
  4841. '']);
  4842. ConvertProgram;
  4843. CheckSource('TestAnonymousProc_Arg',
  4844. LinesToStr([ // statements
  4845. 'this.DoMore = function (f, g) {',
  4846. '};',
  4847. 'this.DoOdd = function (v) {',
  4848. '};',
  4849. 'this.DoIt = function (f) {',
  4850. ' $mod.DoIt(function (b) {',
  4851. ' var Result = 0;',
  4852. ' Result = 1 + b;',
  4853. ' return Result;',
  4854. ' });',
  4855. ' $mod.DoMore(function () {',
  4856. ' }, function () {',
  4857. ' });',
  4858. ' $mod.DoOdd(function () {',
  4859. ' });',
  4860. '};',
  4861. '']),
  4862. LinesToStr([
  4863. '$mod.DoMore(function () {',
  4864. '}, function () {',
  4865. ' console.log("c");',
  4866. '});',
  4867. '']));
  4868. end;
  4869. procedure TTestModule.TestAnonymousProc_Typecast;
  4870. begin
  4871. StartProgram(false);
  4872. Add([
  4873. 'type',
  4874. ' TProc = reference to procedure(w: word);',
  4875. ' TArr = array of word;',
  4876. ' TFuncArr = reference to function: TArr;',
  4877. 'procedure DoIt(p: TProc);',
  4878. 'var',
  4879. ' w: word;',
  4880. ' a: TArr;',
  4881. 'begin',
  4882. ' p:=TProc(procedure(b: smallint) begin end);',
  4883. ' a:=TFuncArr(function: TArr begin end)();',
  4884. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4885. 'end;',
  4886. 'begin']);
  4887. ConvertProgram;
  4888. CheckSource('TestAnonymousProc_Typecast',
  4889. LinesToStr([ // statements
  4890. 'this.DoIt = function (p) {',
  4891. ' var w = 0;',
  4892. ' var a = [];',
  4893. ' p = function (b) {',
  4894. ' };',
  4895. ' a = function () {',
  4896. ' var Result = [];',
  4897. ' return Result;',
  4898. ' }();',
  4899. ' w = function () {',
  4900. ' var Result = [];',
  4901. ' return Result;',
  4902. ' }()[3];',
  4903. '};',
  4904. '']),
  4905. LinesToStr([
  4906. '']));
  4907. end;
  4908. procedure TTestModule.TestAnonymousProc_With;
  4909. begin
  4910. StartProgram(false);
  4911. Add([
  4912. 'type',
  4913. ' TProc = reference to procedure(w: word);',
  4914. ' TObject = class',
  4915. ' b: boolean;',
  4916. ' end;',
  4917. 'var',
  4918. ' p: TProc;',
  4919. ' bird: TObject;',
  4920. 'begin',
  4921. ' with bird do',
  4922. ' p:=procedure(w: word)',
  4923. ' begin',
  4924. ' b:=w>2;',
  4925. ' end;',
  4926. '']);
  4927. ConvertProgram;
  4928. CheckSource('TestAnonymousProc_With',
  4929. LinesToStr([ // statements
  4930. 'rtl.createClass(this, "TObject", null, function () {',
  4931. ' this.$init = function () {',
  4932. ' this.b = false;',
  4933. ' };',
  4934. ' this.$final = function () {',
  4935. ' };',
  4936. '});',
  4937. 'this.p = null;',
  4938. 'this.bird = null;',
  4939. '']),
  4940. LinesToStr([
  4941. 'var $with = $mod.bird;',
  4942. '$mod.p = function (w) {',
  4943. ' $with.b = w > 2;',
  4944. '};',
  4945. '']));
  4946. end;
  4947. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4948. begin
  4949. StartProgram(false);
  4950. Add([
  4951. 'type',
  4952. ' TProc = reference to procedure;',
  4953. ' TObject = class',
  4954. ' b: boolean;',
  4955. ' end;',
  4956. 'procedure DoIt;',
  4957. 'var',
  4958. ' p: TProc;',
  4959. 'begin',
  4960. ' try',
  4961. ' except',
  4962. ' on E: TObject do',
  4963. ' p:=procedure',
  4964. ' begin',
  4965. ' E.b:=true;',
  4966. ' end;',
  4967. ' end;',
  4968. 'end;',
  4969. 'begin']);
  4970. ConvertProgram;
  4971. CheckSource('TestAnonymousProc_ExceptOn',
  4972. LinesToStr([ // statements
  4973. 'rtl.createClass(this, "TObject", null, function () {',
  4974. ' this.$init = function () {',
  4975. ' this.b = false;',
  4976. ' };',
  4977. ' this.$final = function () {',
  4978. ' };',
  4979. '});',
  4980. 'this.DoIt = function () {',
  4981. ' var p = null;',
  4982. ' try {} catch ($e) {',
  4983. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4984. ' var E = $e;',
  4985. ' p = function () {',
  4986. ' E.b = true;',
  4987. ' };',
  4988. ' } else throw $e',
  4989. ' };',
  4990. '};',
  4991. '']),
  4992. LinesToStr([
  4993. '']));
  4994. end;
  4995. procedure TTestModule.TestAnonymousProc_Nested;
  4996. begin
  4997. StartProgram(false);
  4998. Add([
  4999. 'type',
  5000. ' TProc = reference to procedure;',
  5001. ' TObject = class',
  5002. ' i: byte;',
  5003. ' procedure DoIt;',
  5004. ' end;',
  5005. 'procedure TObject.DoIt;',
  5006. 'var',
  5007. ' p: TProc;',
  5008. ' procedure Sub;',
  5009. ' begin',
  5010. ' p:=procedure',
  5011. ' begin',
  5012. ' i:=3;',
  5013. ' Self.i:=4;',
  5014. ' p:=procedure',
  5015. ' procedure SubSub;',
  5016. ' begin',
  5017. ' i:=13;',
  5018. ' Self.i:=14;',
  5019. ' end;',
  5020. ' begin',
  5021. ' i:=13;',
  5022. ' Self.i:=14;',
  5023. ' end;',
  5024. ' end;',
  5025. ' end;',
  5026. 'begin',
  5027. 'end;',
  5028. 'begin']);
  5029. ConvertProgram;
  5030. CheckSource('TestAnonymousProc_Nested',
  5031. LinesToStr([ // statements
  5032. 'rtl.createClass(this, "TObject", null, function () {',
  5033. ' this.$init = function () {',
  5034. ' this.i = 0;',
  5035. ' };',
  5036. ' this.$final = function () {',
  5037. ' };',
  5038. ' this.DoIt = function () {',
  5039. ' var $Self = this;',
  5040. ' var p = null;',
  5041. ' function Sub() {',
  5042. ' p = function () {',
  5043. ' $Self.i = 3;',
  5044. ' $Self.i = 4;',
  5045. ' p = function () {',
  5046. ' function SubSub() {',
  5047. ' $Self.i = 13;',
  5048. ' $Self.i = 14;',
  5049. ' };',
  5050. ' $Self.i = 13;',
  5051. ' $Self.i = 14;',
  5052. ' };',
  5053. ' };',
  5054. ' };',
  5055. ' };',
  5056. '});',
  5057. '']),
  5058. LinesToStr([
  5059. '']));
  5060. end;
  5061. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5062. begin
  5063. StartProgram(false);
  5064. Add([
  5065. 'type',
  5066. ' TProc = reference to procedure;',
  5067. 'function DoIt: TProc;',
  5068. ' function Sub: TProc;',
  5069. ' begin',
  5070. ' Result:=procedure',
  5071. ' begin',
  5072. ' Sub:=procedure',
  5073. ' procedure SubSub;',
  5074. ' begin',
  5075. ' Result:=nil;',
  5076. ' Sub:=nil;',
  5077. ' DoIt:=nil;',
  5078. ' end;',
  5079. ' begin',
  5080. ' Result:=nil;',
  5081. ' Sub:=nil;',
  5082. ' DoIt:=nil;',
  5083. ' end;',
  5084. ' end;',
  5085. ' end;',
  5086. 'begin',
  5087. 'end;',
  5088. 'begin']);
  5089. ConvertProgram;
  5090. CheckSource('TestAnonymousProc_NestedAssignResult',
  5091. LinesToStr([ // statements
  5092. 'this.DoIt = function () {',
  5093. ' var Result = null;',
  5094. ' function Sub() {',
  5095. ' var Result$1 = null;',
  5096. ' Result$1 = function () {',
  5097. ' Result$1 = function () {',
  5098. ' function SubSub() {',
  5099. ' Result$1 = null;',
  5100. ' Result$1 = null;',
  5101. ' Result = null;',
  5102. ' };',
  5103. ' Result$1 = null;',
  5104. ' Result$1 = null;',
  5105. ' Result = null;',
  5106. ' };',
  5107. ' };',
  5108. ' return Result$1;',
  5109. ' };',
  5110. ' return Result;',
  5111. '};',
  5112. '']),
  5113. LinesToStr([
  5114. '']));
  5115. end;
  5116. procedure TTestModule.TestAnonymousProc_Class;
  5117. begin
  5118. StartProgram(false);
  5119. Add([
  5120. 'type',
  5121. ' TProc = reference to procedure;',
  5122. ' TEvent = procedure of object;',
  5123. ' TObject = class',
  5124. ' Size: word;',
  5125. ' function GetIt: TProc;',
  5126. ' procedure DoIt; virtual; abstract;',
  5127. ' end;',
  5128. 'function TObject.GetIt: TProc;',
  5129. 'begin',
  5130. ' Result:=procedure',
  5131. ' var p: TEvent;',
  5132. ' begin',
  5133. ' Size:=Size;',
  5134. ' Size:=Self.Size;',
  5135. ' p:=@DoIt;',
  5136. ' p:[email protected];',
  5137. ' end;',
  5138. 'end;',
  5139. 'begin']);
  5140. ConvertProgram;
  5141. CheckSource('TestAnonymousProc_Class',
  5142. LinesToStr([ // statements
  5143. 'rtl.createClass(this, "TObject", null, function () {',
  5144. ' this.$init = function () {',
  5145. ' this.Size = 0;',
  5146. ' };',
  5147. ' this.$final = function () {',
  5148. ' };',
  5149. ' this.GetIt = function () {',
  5150. ' var $Self = this;',
  5151. ' var Result = null;',
  5152. ' Result = function () {',
  5153. ' var p = null;',
  5154. ' $Self.Size = $Self.Size;',
  5155. ' $Self.Size = $Self.Size;',
  5156. ' p = rtl.createCallback($Self, "DoIt");',
  5157. ' p = rtl.createCallback($Self, "DoIt");',
  5158. ' };',
  5159. ' return Result;',
  5160. ' };',
  5161. '});',
  5162. '']),
  5163. LinesToStr([
  5164. '']));
  5165. end;
  5166. procedure TTestModule.TestAnonymousProc_ForLoop;
  5167. begin
  5168. StartProgram(false);
  5169. Add([
  5170. 'type TProc = reference to procedure;',
  5171. 'procedure Foo(p: TProc);',
  5172. 'begin',
  5173. 'end;',
  5174. 'procedure DoIt;',
  5175. 'var i: word;',
  5176. ' a: word;',
  5177. 'begin',
  5178. ' for i:=1 to 10 do begin',
  5179. ' Foo(procedure begin a:=3; end);',
  5180. ' end;',
  5181. 'end;',
  5182. 'begin',
  5183. ' DoIt;']);
  5184. ConvertProgram;
  5185. CheckSource('TestAnonymousProc_ForLoop',
  5186. LinesToStr([ // statements
  5187. 'this.Foo = function (p) {',
  5188. '};',
  5189. 'this.DoIt = function () {',
  5190. ' var i = 0;',
  5191. ' var a = 0;',
  5192. ' for (i = 1; i <= 10; i++) {',
  5193. ' $mod.Foo(function () {',
  5194. ' a = 3;',
  5195. ' });',
  5196. ' };',
  5197. '};',
  5198. '']),
  5199. LinesToStr([
  5200. '$mod.DoIt();'
  5201. ]));
  5202. end;
  5203. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5204. begin
  5205. StartProgram(false);
  5206. Add([
  5207. '{$mode delphi}',
  5208. 'type',
  5209. ' TProc = reference to procedure;',
  5210. ' TFunc = reference to function(x: word): word;',
  5211. 'procedure Run;',
  5212. 'asm',
  5213. 'end;',
  5214. 'procedure Walk(p: TProc; f: TFunc);',
  5215. 'begin',
  5216. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5217. 'end;',
  5218. 'begin',
  5219. ' Walk(procedure',
  5220. ' asm',
  5221. ' console.log("a");',
  5222. ' end,',
  5223. ' function(x: word): word asm',
  5224. ' console.log("c");',
  5225. ' end);',
  5226. '']);
  5227. ConvertProgram;
  5228. CheckSource('TestAnonymousProc_AsmDelphi',
  5229. LinesToStr([ // statements
  5230. 'this.Run = function () {',
  5231. '};',
  5232. 'this.Walk = function (p, f) {',
  5233. ' $mod.Walk(function () {',
  5234. ' }, function (b) {',
  5235. ' return 1+b;',
  5236. ' });',
  5237. '};',
  5238. '']),
  5239. LinesToStr([
  5240. '$mod.Walk(function () {',
  5241. ' console.log("a");',
  5242. '}, function (x) {',
  5243. ' console.log("c");',
  5244. '});',
  5245. '']));
  5246. end;
  5247. procedure TTestModule.TestEnum_Name;
  5248. begin
  5249. StartProgram(false);
  5250. Add('type TMyEnum = (Red, Green, Blue);');
  5251. Add('var e: TMyEnum;');
  5252. Add('var f: TMyEnum = Blue;');
  5253. Add('begin');
  5254. Add(' e:=green;');
  5255. Add(' e:=default(TMyEnum);');
  5256. ConvertProgram;
  5257. CheckSource('TestEnum_Name',
  5258. LinesToStr([ // statements
  5259. 'this.TMyEnum = {',
  5260. ' "0":"Red",',
  5261. ' Red:0,',
  5262. ' "1":"Green",',
  5263. ' Green:1,',
  5264. ' "2":"Blue",',
  5265. ' Blue:2',
  5266. ' };',
  5267. 'this.e = 0;',
  5268. 'this.f = this.TMyEnum.Blue;'
  5269. ]),
  5270. LinesToStr([
  5271. '$mod.e=$mod.TMyEnum.Green;',
  5272. '$mod.e=$mod.TMyEnum.Red;'
  5273. ]));
  5274. end;
  5275. procedure TTestModule.TestEnum_Number;
  5276. begin
  5277. Converter.Options:=Converter.Options+[coEnumNumbers];
  5278. StartProgram(false);
  5279. Add('type TMyEnum = (Red, Green);');
  5280. Add('var');
  5281. Add(' e: TMyEnum;');
  5282. Add(' f: TMyEnum = Green;');
  5283. Add(' i: longint;');
  5284. Add('begin');
  5285. Add(' e:=green;');
  5286. Add(' i:=longint(e);');
  5287. ConvertProgram;
  5288. CheckSource('TestEnumNumber',
  5289. LinesToStr([ // statements
  5290. 'this.TMyEnum = {',
  5291. ' "0":"Red",',
  5292. ' Red:0,',
  5293. ' "1":"Green",',
  5294. ' Green:1',
  5295. ' };',
  5296. 'this.e = 0;',
  5297. 'this.f = 1;',
  5298. 'this.i = 0;'
  5299. ]),
  5300. LinesToStr([
  5301. '$mod.e=1;',
  5302. '$mod.i=$mod.e;'
  5303. ]));
  5304. end;
  5305. procedure TTestModule.TestEnum_ConstFail;
  5306. begin
  5307. StartProgram(false);
  5308. Add([
  5309. 'type TMyEnum = (Red = 100, Green = 101);',
  5310. 'var',
  5311. ' e: TMyEnum;',
  5312. ' f: TMyEnum = Green;',
  5313. 'begin',
  5314. ' e:=green;']);
  5315. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5316. ConvertProgram;
  5317. end;
  5318. procedure TTestModule.TestEnum_Functions;
  5319. begin
  5320. StartProgram(false);
  5321. Add([
  5322. 'type TMyEnum = (Red, Green);',
  5323. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5324. 'var',
  5325. ' v: longint;',
  5326. ' s: string;',
  5327. 'begin',
  5328. ' val(s,e,v);',
  5329. ' val(s,e,i);',
  5330. 'end;',
  5331. 'var',
  5332. ' e: TMyEnum;',
  5333. ' i: longint;',
  5334. ' s: string;',
  5335. ' b: boolean;',
  5336. 'begin',
  5337. ' i:=ord(red);',
  5338. ' i:=ord(green);',
  5339. ' i:=ord(e);',
  5340. ' i:=ord(b);',
  5341. ' e:=low(tmyenum);',
  5342. ' e:=low(e);',
  5343. ' b:=low(boolean);',
  5344. ' e:=high(tmyenum);',
  5345. ' e:=high(e);',
  5346. ' b:=high(boolean);',
  5347. ' e:=pred(green);',
  5348. ' e:=pred(e);',
  5349. ' b:=pred(b);',
  5350. ' e:=succ(red);',
  5351. ' e:=succ(e);',
  5352. ' b:=succ(b);',
  5353. ' e:=tmyenum(1);',
  5354. ' e:=tmyenum(i);',
  5355. ' s:=str(e);',
  5356. ' str(e,s);',
  5357. ' str(red,s);',
  5358. ' s:=str(e:3);',
  5359. ' writestr(s,e:3,red);',
  5360. ' val(s,e,i);',
  5361. ' i:=longint(e);']);
  5362. ConvertProgram;
  5363. CheckSource('TestEnum_Functions',
  5364. LinesToStr([ // statements
  5365. 'this.TMyEnum = {',
  5366. ' "0":"Red",',
  5367. ' Red:0,',
  5368. ' "1":"Green",',
  5369. ' Green:1',
  5370. ' };',
  5371. 'this.DoIt = function (e, i) {',
  5372. ' var v = 0;',
  5373. ' var s = "";',
  5374. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5375. ' v = w;',
  5376. ' }));',
  5377. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5378. '};',
  5379. 'this.e = 0;',
  5380. 'this.i = 0;',
  5381. 'this.s = "";',
  5382. 'this.b = false;',
  5383. '']),
  5384. LinesToStr([
  5385. '$mod.i=$mod.TMyEnum.Red;',
  5386. '$mod.i=$mod.TMyEnum.Green;',
  5387. '$mod.i=$mod.e;',
  5388. '$mod.i=$mod.b+0;',
  5389. '$mod.e=$mod.TMyEnum.Red;',
  5390. '$mod.e=$mod.TMyEnum.Red;',
  5391. '$mod.b=false;',
  5392. '$mod.e=$mod.TMyEnum.Green;',
  5393. '$mod.e=$mod.TMyEnum.Green;',
  5394. '$mod.b=true;',
  5395. '$mod.e=$mod.TMyEnum.Green-1;',
  5396. '$mod.e=$mod.e-1;',
  5397. '$mod.b=false;',
  5398. '$mod.e=$mod.TMyEnum.Red+1;',
  5399. '$mod.e=$mod.e+1;',
  5400. '$mod.b=true;',
  5401. '$mod.e=1;',
  5402. '$mod.e=$mod.i;',
  5403. '$mod.s = $mod.TMyEnum[$mod.e];',
  5404. '$mod.s = $mod.TMyEnum[$mod.e];',
  5405. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5406. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5407. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5408. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5409. ' $mod.i = v;',
  5410. '});',
  5411. '$mod.i=$mod.e;',
  5412. '']));
  5413. end;
  5414. procedure TTestModule.TestEnumRg_Functions;
  5415. begin
  5416. StartProgram(false);
  5417. Add([
  5418. 'type',
  5419. ' TEnum = (Red, Green, Blue);',
  5420. ' TEnumRg = Green..Blue;',
  5421. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5422. 'var',
  5423. ' v: longint;',
  5424. ' s: string;',
  5425. 'begin',
  5426. ' val(s,e,v);',
  5427. ' val(s,e,i);',
  5428. 'end;',
  5429. 'var',
  5430. ' e: TEnumRg;',
  5431. ' i: longint;',
  5432. ' s: string;',
  5433. 'begin',
  5434. ' i:=ord(green);',
  5435. ' i:=ord(e);',
  5436. ' e:=low(tenumrg);',
  5437. ' e:=low(e);',
  5438. ' e:=high(tenumrg);',
  5439. ' e:=high(e);',
  5440. ' e:=pred(blue);',
  5441. ' e:=pred(e);',
  5442. ' e:=succ(green);',
  5443. ' e:=succ(e);',
  5444. ' e:=tenumrg(1);',
  5445. ' e:=tenumrg(i);',
  5446. ' s:=str(e);',
  5447. ' str(e,s);',
  5448. ' str(red,s);',
  5449. ' s:=str(e:3);',
  5450. ' writestr(s,e:3,blue);',
  5451. ' val(s,e,i);',
  5452. ' i:=longint(e);']);
  5453. ConvertProgram;
  5454. CheckSource('TestEnumRg_Functions',
  5455. LinesToStr([ // statements
  5456. 'this.TEnum = {',
  5457. ' "0":"Red",',
  5458. ' Red:0,',
  5459. ' "1":"Green",',
  5460. ' Green:1,',
  5461. ' "2":"Blue",',
  5462. ' Blue:2',
  5463. ' };',
  5464. 'this.DoIt = function (e, i) {',
  5465. ' var v = 0;',
  5466. ' var s = "";',
  5467. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5468. ' v = w;',
  5469. ' }));',
  5470. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5471. '};',
  5472. 'this.e = this.TEnum.Green;',
  5473. 'this.i = 0;',
  5474. 'this.s = "";',
  5475. '']),
  5476. LinesToStr([
  5477. '$mod.i=$mod.TEnum.Green;',
  5478. '$mod.i=$mod.e;',
  5479. '$mod.e=$mod.TEnum.Green;',
  5480. '$mod.e=$mod.TEnum.Green;',
  5481. '$mod.e=$mod.TEnum.Blue;',
  5482. '$mod.e=$mod.TEnum.Blue;',
  5483. '$mod.e=$mod.TEnum.Blue-1;',
  5484. '$mod.e=$mod.e-1;',
  5485. '$mod.e=$mod.TEnum.Green+1;',
  5486. '$mod.e=$mod.e+1;',
  5487. '$mod.e=1;',
  5488. '$mod.e=$mod.i;',
  5489. '$mod.s = $mod.TEnum[$mod.e];',
  5490. '$mod.s = $mod.TEnum[$mod.e];',
  5491. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5492. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5493. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5494. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5495. ' $mod.i = v;',
  5496. '});',
  5497. '$mod.i=$mod.e;',
  5498. '']));
  5499. end;
  5500. procedure TTestModule.TestEnum_AsParams;
  5501. begin
  5502. StartProgram(false);
  5503. Add('type TEnum = (Red,Blue);');
  5504. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5505. Add('var vJ: TEnum;');
  5506. Add('begin');
  5507. Add(' vg:=vg;');
  5508. Add(' vj:=vh;');
  5509. Add(' vi:=vi;');
  5510. Add(' doit(vg,vg,vg);');
  5511. Add(' doit(vh,vh,vj);');
  5512. Add(' doit(vi,vi,vi);');
  5513. Add(' doit(vj,vj,vj);');
  5514. Add('end;');
  5515. Add('var i: TEnum;');
  5516. Add('begin');
  5517. Add(' doit(i,i,i);');
  5518. ConvertProgram;
  5519. CheckSource('TestEnum_AsParams',
  5520. LinesToStr([ // statements
  5521. 'this.TEnum = {',
  5522. ' "0": "Red",',
  5523. ' Red: 0,',
  5524. ' "1": "Blue",',
  5525. ' Blue: 1',
  5526. '};',
  5527. 'this.DoIt = function (vG,vH,vI) {',
  5528. ' var vJ = 0;',
  5529. ' vG = vG;',
  5530. ' vJ = vH;',
  5531. ' vI.set(vI.get());',
  5532. ' $mod.DoIt(vG, vG, {',
  5533. ' get: function () {',
  5534. ' return vG;',
  5535. ' },',
  5536. ' set: function (v) {',
  5537. ' vG = v;',
  5538. ' }',
  5539. ' });',
  5540. ' $mod.DoIt(vH, vH, {',
  5541. ' get: function () {',
  5542. ' return vJ;',
  5543. ' },',
  5544. ' set: function (v) {',
  5545. ' vJ = v;',
  5546. ' }',
  5547. ' });',
  5548. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5549. ' $mod.DoIt(vJ, vJ, {',
  5550. ' get: function () {',
  5551. ' return vJ;',
  5552. ' },',
  5553. ' set: function (v) {',
  5554. ' vJ = v;',
  5555. ' }',
  5556. ' });',
  5557. '};',
  5558. 'this.i = 0;'
  5559. ]),
  5560. LinesToStr([
  5561. '$mod.DoIt($mod.i,$mod.i,{',
  5562. ' p: $mod,',
  5563. ' get: function () {',
  5564. ' return this.p.i;',
  5565. ' },',
  5566. ' set: function (v) {',
  5567. ' this.p.i = v;',
  5568. ' }',
  5569. '});'
  5570. ]));
  5571. end;
  5572. procedure TTestModule.TestEnumRange_Array;
  5573. begin
  5574. StartProgram(false);
  5575. Add([
  5576. 'type',
  5577. ' TEnum = (Red, Green, Blue);',
  5578. ' TEnumRg = green..blue;',
  5579. ' TArr = array[TEnumRg] of byte;',
  5580. ' TArr2 = array[green..blue] of byte;',
  5581. 'var',
  5582. ' a: TArr;',
  5583. ' b: TArr = (3,4);',
  5584. ' c: TArr2 = (5,6);',
  5585. 'begin',
  5586. ' a[green] := b[blue];',
  5587. ' c[green] := c[blue];',
  5588. '']);
  5589. ConvertProgram;
  5590. CheckSource('TestEnumRange_Array',
  5591. LinesToStr([ // statements
  5592. 'this.TEnum = {',
  5593. ' "0": "Red",',
  5594. ' Red: 0,',
  5595. ' "1": "Green",',
  5596. ' Green: 1,',
  5597. ' "2": "Blue",',
  5598. ' Blue: 2',
  5599. '};',
  5600. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5601. 'this.b = [3, 4];',
  5602. 'this.c = [5, 6];',
  5603. '']),
  5604. LinesToStr([
  5605. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5606. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestEnum_ForIn;
  5610. begin
  5611. StartProgram(false);
  5612. Add([
  5613. 'type',
  5614. ' TEnum = (Red, Green, Blue);',
  5615. ' TEnumRg = green..blue;',
  5616. ' TArr = array[TEnum] of byte;',
  5617. ' TArrRg = array[TEnumRg] of byte;',
  5618. 'var',
  5619. ' e: TEnum;',
  5620. ' a1: TArr = (3,4,5);',
  5621. ' a2: TArrRg = (11,12);',
  5622. ' b: byte;',
  5623. 'begin',
  5624. ' for e in TEnum do ;',
  5625. ' for e in TEnumRg do ;',
  5626. ' for e in TArr do ;',
  5627. ' for e in TArrRg do ;',
  5628. ' for b in a1 do ;',
  5629. ' for b in a2 do ;',
  5630. '']);
  5631. ConvertProgram;
  5632. CheckSource('TestEnum_ForIn',
  5633. LinesToStr([ // statements
  5634. 'this.TEnum = {',
  5635. ' "0": "Red",',
  5636. ' Red: 0,',
  5637. ' "1": "Green",',
  5638. ' Green: 1,',
  5639. ' "2": "Blue",',
  5640. ' Blue: 2',
  5641. '};',
  5642. 'this.e = 0;',
  5643. 'this.a1 = [3, 4, 5];',
  5644. 'this.a2 = [11, 12];',
  5645. 'this.b = 0;',
  5646. '']),
  5647. LinesToStr([
  5648. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5649. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5650. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5651. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5652. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5653. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5654. '']));
  5655. end;
  5656. procedure TTestModule.TestEnum_ScopedNumber;
  5657. begin
  5658. Converter.Options:=Converter.Options+[coEnumNumbers];
  5659. StartProgram(false);
  5660. Add([
  5661. 'type',
  5662. ' TEnum = (Red, Green);',
  5663. 'var',
  5664. ' e: TEnum;',
  5665. 'begin',
  5666. ' e:=TEnum.Green;',
  5667. '']);
  5668. ConvertProgram;
  5669. CheckSource('TestEnum_ScopedNumber',
  5670. LinesToStr([ // statements
  5671. 'this.TEnum = {',
  5672. ' "0": "Red",',
  5673. ' Red: 0,',
  5674. ' "1": "Green",',
  5675. ' Green: 1',
  5676. '};',
  5677. 'this.e = 0;',
  5678. '']),
  5679. LinesToStr([
  5680. '$mod.e = 1;']));
  5681. end;
  5682. procedure TTestModule.TestEnum_InFunction;
  5683. begin
  5684. StartProgram(false);
  5685. Add([
  5686. 'const TEnum = 3;',
  5687. 'procedure DoIt;',
  5688. 'type',
  5689. ' TEnum = (Red, Green, Blue);',
  5690. ' procedure Sub;',
  5691. ' type',
  5692. ' TEnumSub = (Left, Right);',
  5693. ' var',
  5694. ' es: TEnumSub;',
  5695. ' begin',
  5696. ' es:=Left;',
  5697. ' end;',
  5698. 'var',
  5699. ' e, e2: TEnum;',
  5700. 'begin',
  5701. ' if e in [red,blue] then e2:=e;',
  5702. 'end;',
  5703. 'begin']);
  5704. ConvertProgram;
  5705. CheckSource('TestEnum_InFunction',
  5706. LinesToStr([ // statements
  5707. 'this.TEnum = 3;',
  5708. 'var TEnum$1 = {',
  5709. ' "0":"Red",',
  5710. ' Red:0,',
  5711. ' "1":"Green",',
  5712. ' Green:1,',
  5713. ' "2":"Blue",',
  5714. ' Blue:2',
  5715. ' };',
  5716. 'var TEnumSub = {',
  5717. ' "0": "Left",',
  5718. ' Left: 0,',
  5719. ' "1": "Right",',
  5720. ' Right: 1',
  5721. '};',
  5722. 'this.DoIt = function () {',
  5723. ' function Sub() {',
  5724. ' var es = 0;',
  5725. ' es = TEnumSub.Left;',
  5726. ' };',
  5727. ' var e = 0;',
  5728. ' var e2 = 0;',
  5729. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5730. '};',
  5731. '']),
  5732. LinesToStr([
  5733. '']));
  5734. end;
  5735. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5736. begin
  5737. StartUnit(true);
  5738. Add([
  5739. 'interface',
  5740. 'var color: (red, green);',
  5741. 'implementation',
  5742. 'initialization',
  5743. ' color:=green;',
  5744. '']);
  5745. ConvertUnit;
  5746. CheckSource('TestEnum_Name_Anonymous_Unit',
  5747. LinesToStr([
  5748. 'this.color$a = {',
  5749. ' "0": "red",',
  5750. ' red: 0,',
  5751. ' "1": "green",',
  5752. ' green: 1',
  5753. '};',
  5754. 'this.color = 0;',
  5755. '']),
  5756. LinesToStr([ // this.$init
  5757. '$mod.color = $mod.color$a.green;',
  5758. '']),
  5759. LinesToStr([ // implementation
  5760. '']) );
  5761. end;
  5762. procedure TTestModule.TestSet_Enum;
  5763. begin
  5764. StartProgram(false);
  5765. Add([
  5766. 'type',
  5767. ' TColor = (Red, Green, Blue);',
  5768. ' TColors = set of TColor;',
  5769. 'var',
  5770. ' c: TColor;',
  5771. ' s: TColors;',
  5772. ' t: TColors = [];',
  5773. ' u: TColors = [Red];',
  5774. 'begin',
  5775. ' s:=[];',
  5776. ' s:=[Green];',
  5777. ' s:=[Green,Blue];',
  5778. ' s:=[Red..Blue];',
  5779. ' s:=[Red,Green..Blue];',
  5780. ' s:=[Red,c];',
  5781. ' s:=t;',
  5782. ' s:=default(TColors);',
  5783. '']);
  5784. ConvertProgram;
  5785. CheckSource('TestSet',
  5786. LinesToStr([ // statements
  5787. 'this.TColor = {',
  5788. ' "0":"Red",',
  5789. ' Red:0,',
  5790. ' "1":"Green",',
  5791. ' Green:1,',
  5792. ' "2":"Blue",',
  5793. ' Blue:2',
  5794. ' };',
  5795. 'this.c = 0;',
  5796. 'this.s = {};',
  5797. 'this.t = {};',
  5798. 'this.u = rtl.createSet(this.TColor.Red);'
  5799. ]),
  5800. LinesToStr([
  5801. '$mod.s={};',
  5802. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5803. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5804. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5805. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5806. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5807. '$mod.s=rtl.refSet($mod.t);',
  5808. '$mod.s={};',
  5809. '']));
  5810. end;
  5811. procedure TTestModule.TestSet_Operators;
  5812. begin
  5813. StartProgram(false);
  5814. Add('type');
  5815. Add(' TColor = (Red, Green, Blue);');
  5816. Add(' TColors = set of tcolor;');
  5817. Add('var');
  5818. Add(' vC: TColor;');
  5819. Add(' vS: TColors;');
  5820. Add(' vT: TColors;');
  5821. Add(' vU: TColors;');
  5822. Add(' B: boolean;');
  5823. Add('begin');
  5824. Add(' include(vs,green);');
  5825. Add(' exclude(vs,vc);');
  5826. Add(' vs:=vt+vu;');
  5827. Add(' vs:=vt+[red];');
  5828. Add(' vs:=[red]+vt;');
  5829. Add(' vs:=[red]+[green];');
  5830. Add(' vs:=vt-vu;');
  5831. Add(' vs:=vt-[red];');
  5832. Add(' vs:=[red]-vt;');
  5833. Add(' vs:=[red]-[green];');
  5834. Add(' vs:=vt*vu;');
  5835. Add(' vs:=vt*[red];');
  5836. Add(' vs:=[red]*vt;');
  5837. Add(' vs:=[red]*[green];');
  5838. Add(' vs:=vt><vu;');
  5839. Add(' vs:=vt><[red];');
  5840. Add(' vs:=[red]><vt;');
  5841. Add(' vs:=[red]><[green];');
  5842. Add(' b:=vt=vu;');
  5843. Add(' b:=vt=[red];');
  5844. Add(' b:=[red]=vt;');
  5845. Add(' b:=[red]=[green];');
  5846. Add(' b:=vt<>vu;');
  5847. Add(' b:=vt<>[red];');
  5848. Add(' b:=[red]<>vt;');
  5849. Add(' b:=[red]<>[green];');
  5850. Add(' b:=vt<=vu;');
  5851. Add(' b:=vt<=[red];');
  5852. Add(' b:=[red]<=vt;');
  5853. Add(' b:=[red]<=[green];');
  5854. Add(' b:=vt>=vu;');
  5855. Add(' b:=vt>=[red];');
  5856. Add(' b:=[red]>=vt;');
  5857. Add(' b:=[red]>=[green];');
  5858. ConvertProgram;
  5859. CheckSource('TestSet_Operators',
  5860. LinesToStr([ // statements
  5861. 'this.TColor = {',
  5862. ' "0":"Red",',
  5863. ' Red:0,',
  5864. ' "1":"Green",',
  5865. ' Green:1,',
  5866. ' "2":"Blue",',
  5867. ' Blue:2',
  5868. ' };',
  5869. 'this.vC = 0;',
  5870. 'this.vS = {};',
  5871. 'this.vT = {};',
  5872. 'this.vU = {};',
  5873. 'this.B = false;'
  5874. ]),
  5875. LinesToStr([
  5876. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5877. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5878. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5879. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5880. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5881. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5882. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5883. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5884. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5885. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5886. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5887. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5888. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5889. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5890. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5891. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5892. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5893. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5894. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5895. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5896. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5897. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5898. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5899. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5900. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5901. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5902. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5903. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5904. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5905. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5906. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5907. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5908. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5909. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5910. '']));
  5911. end;
  5912. procedure TTestModule.TestSet_Operator_In;
  5913. begin
  5914. StartProgram(false);
  5915. Add([
  5916. 'type',
  5917. ' TColor = (Red, Green, Blue);',
  5918. ' TColors = set of tcolor;',
  5919. ' TColorRg = green..blue;',
  5920. 'var',
  5921. ' vC: tcolor;',
  5922. ' vT: tcolors;',
  5923. ' B: boolean;',
  5924. ' rg: TColorRg;',
  5925. 'begin',
  5926. ' b:=red in vt;',
  5927. ' b:=vc in vt;',
  5928. ' b:=green in [red..blue];',
  5929. ' b:=vc in [red..blue];',
  5930. ' ',
  5931. ' if red in vt then ;',
  5932. ' while vC in vt do ;',
  5933. ' repeat',
  5934. ' until vC in vt;',
  5935. ' if rg in [green..blue] then ;',
  5936. '']);
  5937. ConvertProgram;
  5938. CheckSource('TestSet_Operator_In',
  5939. LinesToStr([ // statements
  5940. 'this.TColor = {',
  5941. ' "0":"Red",',
  5942. ' Red:0,',
  5943. ' "1":"Green",',
  5944. ' Green:1,',
  5945. ' "2":"Blue",',
  5946. ' Blue:2',
  5947. ' };',
  5948. 'this.vC = 0;',
  5949. 'this.vT = {};',
  5950. 'this.B = false;',
  5951. 'this.rg = this.TColor.Green;',
  5952. '']),
  5953. LinesToStr([
  5954. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5955. '$mod.B = $mod.vC in $mod.vT;',
  5956. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5957. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5958. 'if ($mod.TColor.Red in $mod.vT) ;',
  5959. 'while ($mod.vC in $mod.vT) {',
  5960. '};',
  5961. 'do {',
  5962. '} while (!($mod.vC in $mod.vT));',
  5963. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5964. '']));
  5965. end;
  5966. procedure TTestModule.TestSet_Functions;
  5967. begin
  5968. StartProgram(false);
  5969. Add('type');
  5970. Add(' TMyEnum = (Red, Green);');
  5971. Add(' TMyEnums = set of TMyEnum;');
  5972. Add('var');
  5973. Add(' e: TMyEnum;');
  5974. Add(' s: TMyEnums;');
  5975. Add('begin');
  5976. Add(' e:=Low(TMyEnums);');
  5977. Add(' e:=Low(s);');
  5978. Add(' e:=High(TMyEnums);');
  5979. Add(' e:=High(s);');
  5980. ConvertProgram;
  5981. CheckSource('TestSetFunctions',
  5982. LinesToStr([ // statements
  5983. 'this.TMyEnum = {',
  5984. ' "0":"Red",',
  5985. ' Red:0,',
  5986. ' "1":"Green",',
  5987. ' Green:1',
  5988. ' };',
  5989. 'this.e = 0;',
  5990. 'this.s = {};'
  5991. ]),
  5992. LinesToStr([
  5993. '$mod.e=$mod.TMyEnum.Red;',
  5994. '$mod.e=$mod.TMyEnum.Red;',
  5995. '$mod.e=$mod.TMyEnum.Green;',
  5996. '$mod.e=$mod.TMyEnum.Green;',
  5997. '']));
  5998. end;
  5999. procedure TTestModule.TestSet_PassAsArgClone;
  6000. begin
  6001. StartProgram(false);
  6002. Add('type');
  6003. Add(' TMyEnum = (Red, Green);');
  6004. Add(' TMyEnums = set of TMyEnum;');
  6005. Add('procedure DoDefault(s: tmyenums); begin end;');
  6006. Add('procedure DoConst(const s: tmyenums); begin end;');
  6007. Add('var');
  6008. Add(' aSet: tmyenums;');
  6009. Add('begin');
  6010. Add(' dodefault(aset);');
  6011. Add(' doconst(aset);');
  6012. ConvertProgram;
  6013. CheckSource('TestSetFunctions',
  6014. LinesToStr([ // statements
  6015. 'this.TMyEnum = {',
  6016. ' "0":"Red",',
  6017. ' Red:0,',
  6018. ' "1":"Green",',
  6019. ' Green:1',
  6020. ' };',
  6021. 'this.DoDefault = function (s) {',
  6022. '};',
  6023. 'this.DoConst = function (s) {',
  6024. '};',
  6025. 'this.aSet = {};'
  6026. ]),
  6027. LinesToStr([
  6028. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6029. '$mod.DoConst($mod.aSet);',
  6030. '']));
  6031. end;
  6032. procedure TTestModule.TestSet_AsParams;
  6033. begin
  6034. StartProgram(false);
  6035. Add([
  6036. 'type TEnum = (Red,Blue);',
  6037. 'type TEnums = set of TEnum;',
  6038. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6039. 'var vJ: TEnums;',
  6040. 'begin',
  6041. ' Include(vg,red);',
  6042. ' Include(result,blue);',
  6043. ' vg:=vg;',
  6044. ' vj:=vh;',
  6045. ' vi:=vi;',
  6046. ' doit(vg,vg,vg);',
  6047. ' doit(vh,vh,vj);',
  6048. ' doit(vi,vi,vi);',
  6049. ' doit(vj,vj,vj);',
  6050. 'end;',
  6051. 'var i: TEnums;',
  6052. 'begin',
  6053. ' doit(i,i,i);']);
  6054. ConvertProgram;
  6055. CheckSource('TestSet_AsParams',
  6056. LinesToStr([ // statements
  6057. 'this.TEnum = {',
  6058. ' "0": "Red",',
  6059. ' Red: 0,',
  6060. ' "1": "Blue",',
  6061. ' Blue: 1',
  6062. '};',
  6063. 'this.DoIt = function (vG,vH,vI) {',
  6064. ' var Result = {};',
  6065. ' var vJ = {};',
  6066. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6067. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6068. ' vG = rtl.refSet(vG);',
  6069. ' vJ = rtl.refSet(vH);',
  6070. ' vI.set(rtl.refSet(vI.get()));',
  6071. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6072. ' get: function () {',
  6073. ' return vG;',
  6074. ' },',
  6075. ' set: function (v) {',
  6076. ' vG = v;',
  6077. ' }',
  6078. ' });',
  6079. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6080. ' get: function () {',
  6081. ' return vJ;',
  6082. ' },',
  6083. ' set: function (v) {',
  6084. ' vJ = v;',
  6085. ' }',
  6086. ' });',
  6087. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6088. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6089. ' get: function () {',
  6090. ' return vJ;',
  6091. ' },',
  6092. ' set: function (v) {',
  6093. ' vJ = v;',
  6094. ' }',
  6095. ' });',
  6096. ' return Result;',
  6097. '};',
  6098. 'this.i = {};'
  6099. ]),
  6100. LinesToStr([
  6101. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6102. ' p: $mod,',
  6103. ' get: function () {',
  6104. ' return this.p.i;',
  6105. ' },',
  6106. ' set: function (v) {',
  6107. ' this.p.i = v;',
  6108. ' }',
  6109. '});'
  6110. ]));
  6111. end;
  6112. procedure TTestModule.TestSet_Property;
  6113. begin
  6114. StartProgram(false);
  6115. Add('type');
  6116. Add(' TEnum = (Red,Blue);');
  6117. Add(' TEnums = set of TEnum;');
  6118. Add(' TObject = class');
  6119. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6120. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6121. Add(' property Colors: TEnums read GetColors write SetColors;');
  6122. Add(' end;');
  6123. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6124. Add('begin end;');
  6125. Add('var Obj: TObject;');
  6126. Add('begin');
  6127. Add(' Include(Obj.Colors,Red);');
  6128. Add(' Exclude(Obj.Colors,Red);');
  6129. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6130. ConvertProgram;
  6131. CheckSource('TestSet_Property',
  6132. LinesToStr([ // statements
  6133. 'this.TEnum = {',
  6134. ' "0": "Red",',
  6135. ' Red: 0,',
  6136. ' "1": "Blue",',
  6137. ' Blue: 1',
  6138. '};',
  6139. 'rtl.createClass(this, "TObject", null, function () {',
  6140. ' this.$init = function () {',
  6141. ' };',
  6142. ' this.$final = function () {',
  6143. ' };',
  6144. '});',
  6145. 'this.DoIt = function (i, j, k, l) {',
  6146. '};',
  6147. 'this.Obj = null;',
  6148. '']),
  6149. LinesToStr([
  6150. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6151. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6152. '']));
  6153. end;
  6154. procedure TTestModule.TestSet_EnumConst;
  6155. begin
  6156. StartProgram(false);
  6157. Add([
  6158. 'type',
  6159. ' TEnum = (Red,Blue);',
  6160. ' TEnums = set of TEnum;',
  6161. 'const',
  6162. ' Orange = red;',
  6163. 'var',
  6164. ' Enum: tenum;',
  6165. ' Enums: tenums;',
  6166. 'begin',
  6167. ' Include(enums,orange);',
  6168. ' Exclude(enums,orange);',
  6169. ' if orange in enums then;',
  6170. ' if orange in [orange,red] then;']);
  6171. ConvertProgram;
  6172. CheckSource('TestSet_EnumConst',
  6173. LinesToStr([ // statements
  6174. 'this.TEnum = {',
  6175. ' "0": "Red",',
  6176. ' Red: 0,',
  6177. ' "1": "Blue",',
  6178. ' Blue: 1',
  6179. '};',
  6180. 'this.Orange = this.TEnum.Red;',
  6181. 'this.Enum = 0;',
  6182. 'this.Enums = {};',
  6183. '']),
  6184. LinesToStr([
  6185. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6186. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6187. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6188. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6189. '']));
  6190. end;
  6191. procedure TTestModule.TestSet_IntConst;
  6192. begin
  6193. StartProgram(false);
  6194. Add([
  6195. 'type',
  6196. ' TEnums = set of Byte;',
  6197. 'const',
  6198. ' Orange = 0;',
  6199. 'var',
  6200. ' Enum: byte;',
  6201. ' Enums: tenums;',
  6202. 'begin',
  6203. ' Enums:=[];',
  6204. ' Enums:=[0];',
  6205. ' Enums:=[1..2];',
  6206. //' Include(enums,orange);',
  6207. //' Exclude(enums,orange);',
  6208. ' if orange in enums then;',
  6209. ' if orange in [orange,1] then;']);
  6210. ConvertProgram;
  6211. CheckSource('TestSet_IntConst',
  6212. LinesToStr([ // statements
  6213. 'this.Orange = 0;',
  6214. 'this.Enum = 0;',
  6215. 'this.Enums = {};',
  6216. '']),
  6217. LinesToStr([
  6218. '$mod.Enums = {};',
  6219. '$mod.Enums = rtl.createSet(0);',
  6220. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6221. 'if (0 in $mod.Enums) ;',
  6222. 'if (0 in rtl.createSet(0, 1)) ;',
  6223. '']));
  6224. end;
  6225. procedure TTestModule.TestSet_IntRange;
  6226. begin
  6227. StartProgram(false);
  6228. Add([
  6229. 'type',
  6230. ' TRange = 1..3;',
  6231. ' TEnums = set of TRange;',
  6232. 'const',
  6233. ' Orange = 2;',
  6234. 'var',
  6235. ' Enum: byte;',
  6236. ' Enums: TEnums;',
  6237. 'begin',
  6238. ' Enums:=[];',
  6239. ' Enums:=[1];',
  6240. ' Enums:=[2..3];',
  6241. ' Include(enums,orange);',
  6242. ' Exclude(enums,orange);',
  6243. ' if orange in enums then;',
  6244. ' if orange in [orange,1] then;']);
  6245. ConvertProgram;
  6246. CheckSource('TestSet_IntRange',
  6247. LinesToStr([ // statements
  6248. 'this.Orange = 2;',
  6249. 'this.Enum = 0;',
  6250. 'this.Enums = {};',
  6251. '']),
  6252. LinesToStr([
  6253. '$mod.Enums = {};',
  6254. '$mod.Enums = rtl.createSet(1);',
  6255. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6256. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6257. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6258. 'if (2 in $mod.Enums) ;',
  6259. 'if (2 in rtl.createSet(2, 1)) ;',
  6260. '']));
  6261. end;
  6262. procedure TTestModule.TestSet_AnonymousEnumType;
  6263. begin
  6264. StartProgram(false);
  6265. Add('type');
  6266. Add(' TFlags = set of (red, green);');
  6267. Add('const');
  6268. Add(' favorite = red;');
  6269. Add('var');
  6270. Add(' f: TFlags;');
  6271. Add(' i: longint;');
  6272. Add('begin');
  6273. Add(' Include(f,red);');
  6274. Add(' Include(f,favorite);');
  6275. Add(' i:=ord(red);');
  6276. Add(' i:=ord(favorite);');
  6277. Add(' i:=ord(low(TFlags));');
  6278. Add(' i:=ord(low(f));');
  6279. Add(' i:=ord(low(favorite));');
  6280. Add(' i:=ord(high(TFlags));');
  6281. Add(' i:=ord(high(f));');
  6282. Add(' i:=ord(high(favorite));');
  6283. Add(' f:=[green,favorite];');
  6284. ConvertProgram;
  6285. CheckSource('TestSet_AnonymousEnumType',
  6286. LinesToStr([ // statements
  6287. 'this.TFlags$a = {',
  6288. ' "0": "red",',
  6289. ' red: 0,',
  6290. ' "1": "green",',
  6291. ' green: 1',
  6292. '};',
  6293. 'this.favorite = this.TFlags$a.red;',
  6294. 'this.f = {};',
  6295. 'this.i = 0;',
  6296. '']),
  6297. LinesToStr([
  6298. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6299. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6300. '$mod.i = $mod.TFlags$a.red;',
  6301. '$mod.i = $mod.TFlags$a.red;',
  6302. '$mod.i = $mod.TFlags$a.red;',
  6303. '$mod.i = $mod.TFlags$a.red;',
  6304. '$mod.i = $mod.TFlags$a.red;',
  6305. '$mod.i = $mod.TFlags$a.green;',
  6306. '$mod.i = $mod.TFlags$a.green;',
  6307. '$mod.i = $mod.TFlags$a.green;',
  6308. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6309. '']));
  6310. end;
  6311. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6312. begin
  6313. exit;
  6314. StartProgram(false);
  6315. Add([
  6316. 'type',
  6317. ' TAtoZ = ''A''..''Z'';',
  6318. ' TSetOfAZ = set of TAtoZ;',
  6319. 'var',
  6320. ' c: char;',
  6321. ' a: TAtoZ;',
  6322. ' s: TSetOfAZ = [''P'',''A''];',
  6323. ' i: longint;',
  6324. 'begin',
  6325. ' Include(s,''S'');',
  6326. ' Include(s,c);',
  6327. ' Include(s,a);',
  6328. ' c:=low(TAtoZ);',
  6329. ' i:=ord(low(TAtoZ));',
  6330. ' a:=high(TAtoZ);',
  6331. ' a:=high(TSetOfAtoZ);',
  6332. ' s:=[a,c,''M''];',
  6333. '']);
  6334. ConvertProgram;
  6335. CheckSource('TestSet_AnonymousEnumTypeChar',
  6336. LinesToStr([ // statements
  6337. '']),
  6338. LinesToStr([
  6339. '']));
  6340. end;
  6341. procedure TTestModule.TestSet_ConstEnum;
  6342. begin
  6343. StartProgram(false);
  6344. Add([
  6345. 'type',
  6346. ' TEnum = (red,blue,green);',
  6347. ' TEnums = set of TEnum;',
  6348. 'const',
  6349. ' teAny = [low(TEnum)..high(TEnum)];',
  6350. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6351. 'var',
  6352. ' e: TEnum;',
  6353. ' s: TEnums;',
  6354. 'begin',
  6355. ' if blue in teAny then;',
  6356. ' if blue in teAny+[e] then;',
  6357. ' if blue in teAny+teRedBlue then;',
  6358. ' if e in [red,blue] then;',
  6359. ' s:=teAny;',
  6360. ' s:=teAny+[e];',
  6361. ' s:=[e]+teAny;',
  6362. ' s:=teAny+teRedBlue;',
  6363. ' s:=teAny+teRedBlue+[e];',
  6364. '']);
  6365. ConvertProgram;
  6366. CheckSource('TestSet_ConstEnum',
  6367. LinesToStr([ // statements
  6368. 'this.TEnum = {',
  6369. ' "0": "red",',
  6370. ' red: 0,',
  6371. ' "1": "blue",',
  6372. ' blue: 1,',
  6373. ' "2": "green",',
  6374. ' green: 2',
  6375. '};',
  6376. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6377. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6378. 'this.e = 0;',
  6379. 'this.s = {};',
  6380. '']),
  6381. LinesToStr([
  6382. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6383. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6384. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6385. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6386. '$mod.s = rtl.refSet($mod.teAny);',
  6387. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6388. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6389. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6390. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6391. '']));
  6392. end;
  6393. procedure TTestModule.TestSet_ConstChar;
  6394. begin
  6395. StartProgram(false);
  6396. Add([
  6397. 'const',
  6398. ' LowChars = [''a''..''z''];',
  6399. ' Chars = LowChars+[''A''..''Z''];',
  6400. ' sc = [''А'', ''Я''];',
  6401. 'var',
  6402. ' c: char;',
  6403. ' s: string;',
  6404. 'begin',
  6405. ' if c in lowchars then ;',
  6406. ' if ''a'' in lowchars then ;',
  6407. ' if s[1] in lowchars then ;',
  6408. ' if c in chars then ;',
  6409. ' if c in [''a''..''z'',''_''] then ;',
  6410. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6411. ' if ''Я'' in sc then ;',
  6412. ' if 3=ord('' '') then ;',
  6413. '']);
  6414. ConvertProgram;
  6415. CheckSource('TestSet_ConstChar',
  6416. LinesToStr([ // statements
  6417. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6418. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6419. 'this.sc = rtl.createSet(1040, 1071);',
  6420. 'this.c = "";',
  6421. 'this.s = "";',
  6422. '']),
  6423. LinesToStr([
  6424. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6425. 'if (97 in $mod.LowChars) ;',
  6426. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6427. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6428. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6429. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6430. 'if (1071 in $mod.sc) ;',
  6431. 'if (3 === 32) ;',
  6432. '']));
  6433. end;
  6434. procedure TTestModule.TestSet_ConstInt;
  6435. begin
  6436. StartProgram(false);
  6437. Add([
  6438. 'const',
  6439. ' Months = [1..12];',
  6440. ' Mirror = [-12..-1]+Months;',
  6441. 'var',
  6442. ' i: smallint;',
  6443. 'begin',
  6444. ' if 3 in Months then;',
  6445. ' if i in Months+[i] then;',
  6446. ' if i in Months+Mirror then;',
  6447. ' if i in [4..6,8] then;',
  6448. '']);
  6449. ConvertProgram;
  6450. CheckSource('TestSet_ConstInt',
  6451. LinesToStr([ // statements
  6452. 'this.Months = rtl.createSet(null, 1, 12);',
  6453. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6454. 'this.i = 0;',
  6455. '']),
  6456. LinesToStr([
  6457. 'if (3 in $mod.Months) ;',
  6458. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6459. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6460. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6461. '']));
  6462. end;
  6463. procedure TTestModule.TestSet_InFunction;
  6464. begin
  6465. StartProgram(false);
  6466. Add([
  6467. 'const',
  6468. ' TEnum = 3;',
  6469. ' TSetOfEnum = 4;',
  6470. ' TSetOfAno = 5;',
  6471. 'procedure DoIt;',
  6472. 'type',
  6473. ' TEnum = (red, blue);',
  6474. ' TSetOfEnum = set of TEnum;',
  6475. ' TSetOfAno = set of (up,down);',
  6476. 'var',
  6477. ' e: TEnum;',
  6478. ' se: TSetOfEnum;',
  6479. ' sa: TSetOfAno;',
  6480. 'begin',
  6481. ' se:=[e];',
  6482. ' sa:=[up];',
  6483. 'end;',
  6484. 'begin',
  6485. '']);
  6486. ConvertProgram;
  6487. CheckSource('TestSet_InFunction',
  6488. LinesToStr([ // statements
  6489. 'this.TEnum = 3;',
  6490. 'this.TSetOfEnum = 4;',
  6491. 'this.TSetOfAno = 5;',
  6492. 'var TEnum$1 = {',
  6493. ' "0": "red",',
  6494. ' red: 0,',
  6495. ' "1": "blue",',
  6496. ' blue: 1',
  6497. '};',
  6498. 'var TSetOfAno$a = {',
  6499. ' "0": "up",',
  6500. ' up: 0,',
  6501. ' "1": "down",',
  6502. ' down: 1',
  6503. '};',
  6504. 'this.DoIt = function () {',
  6505. ' var e = 0;',
  6506. ' var se = {};',
  6507. ' var sa = {};',
  6508. ' se = rtl.createSet(e);',
  6509. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6510. '};',
  6511. '']),
  6512. LinesToStr([
  6513. '']));
  6514. end;
  6515. procedure TTestModule.TestSet_ForIn;
  6516. begin
  6517. StartProgram(false);
  6518. Add([
  6519. 'type',
  6520. ' TEnum = (Red, Green, Blue);',
  6521. ' TEnumRg = green..blue;',
  6522. ' TSetOfEnum = set of TEnum;',
  6523. ' TSetOfEnumRg = set of TEnumRg;',
  6524. 'var',
  6525. ' e, e2: TEnum;',
  6526. ' er: TEnum;',
  6527. ' s: TSetOfEnum;',
  6528. 'begin',
  6529. ' for e in TSetOfEnum do ;',
  6530. ' for e in TSetOfEnumRg do ;',
  6531. ' for e in [] do e2:=e;',
  6532. ' for e in [red..green] do e2:=e;',
  6533. ' for e in [green,blue] do e2:=e;',
  6534. ' for e in [red,blue] do e2:=e;',
  6535. ' for e in s do e2:=e;',
  6536. ' for er in TSetOfEnumRg do ;',
  6537. '']);
  6538. ConvertProgram;
  6539. CheckSource('TestSet_ForIn',
  6540. LinesToStr([ // statements
  6541. 'this.TEnum = {',
  6542. ' "0":"Red",',
  6543. ' Red:0,',
  6544. ' "1":"Green",',
  6545. ' Green:1,',
  6546. ' "2":"Blue",',
  6547. ' Blue:2',
  6548. ' };',
  6549. 'this.e = 0;',
  6550. 'this.e2 = 0;',
  6551. 'this.er = 0;',
  6552. 'this.s = {};',
  6553. '']),
  6554. LinesToStr([
  6555. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6556. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6557. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6558. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6559. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6560. 'for (var $l in $mod.s){',
  6561. ' $mod.e = +$l;',
  6562. ' $mod.e2 = $mod.e;',
  6563. '};',
  6564. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6565. '']));
  6566. end;
  6567. procedure TTestModule.TestNestBegin;
  6568. begin
  6569. StartProgram(false);
  6570. Add('begin');
  6571. Add(' begin');
  6572. Add(' begin');
  6573. Add(' end;');
  6574. Add(' begin');
  6575. Add(' if true then ;');
  6576. Add(' end;');
  6577. Add(' end;');
  6578. ConvertProgram;
  6579. CheckSource('TestNestBegin',
  6580. '',
  6581. 'if (true) ;');
  6582. end;
  6583. procedure TTestModule.TestUnitImplVars;
  6584. begin
  6585. StartUnit(false);
  6586. Add('interface');
  6587. Add('implementation');
  6588. Add('var');
  6589. Add(' V1:longint;');
  6590. Add(' V2:longint = 3;');
  6591. Add(' V3:string = ''abc'';');
  6592. ConvertUnit;
  6593. CheckSource('TestUnitImplVars',
  6594. LinesToStr([ // statements
  6595. 'var $impl = $mod.$impl;',
  6596. '']),
  6597. '', // this.$init
  6598. LinesToStr([ // implementation
  6599. '$impl.V1 = 0;',
  6600. '$impl.V2 = 3;',
  6601. '$impl.V3 = "abc";',
  6602. '']) );
  6603. end;
  6604. procedure TTestModule.TestUnitImplConsts;
  6605. begin
  6606. StartUnit(false);
  6607. Add('interface');
  6608. Add('implementation');
  6609. Add('const');
  6610. Add(' v1 = 3;');
  6611. Add(' v2:longint = 4;');
  6612. Add(' v3:string = ''abc'';');
  6613. ConvertUnit;
  6614. CheckSource('TestUnitImplConsts',
  6615. LinesToStr([ // statements
  6616. 'var $impl = $mod.$impl;',
  6617. '']),
  6618. '', // this.$init
  6619. LinesToStr([ // implementation
  6620. '$impl.v1 = 3;',
  6621. '$impl.v2 = 4;',
  6622. '$impl.v3 = "abc";',
  6623. '']) );
  6624. end;
  6625. procedure TTestModule.TestUnitImplRecord;
  6626. begin
  6627. StartUnit(false);
  6628. Add('interface');
  6629. Add('implementation');
  6630. Add('type');
  6631. Add(' TMyRecord = record');
  6632. Add(' i: longint;');
  6633. Add(' end;');
  6634. Add('var aRec: TMyRecord;');
  6635. Add('initialization');
  6636. Add(' arec.i:=3;');
  6637. ConvertUnit;
  6638. CheckSource('TestUnitImplRecord',
  6639. LinesToStr([ // statements
  6640. 'var $impl = $mod.$impl;',
  6641. '']),
  6642. // this.$init
  6643. '$impl.aRec.i = 3;',
  6644. LinesToStr([ // implementation
  6645. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6646. ' this.i = 0;',
  6647. ' this.$eq = function (b) {',
  6648. ' return this.i === b.i;',
  6649. ' };',
  6650. ' this.$assign = function (s) {',
  6651. ' this.i = s.i;',
  6652. ' return this;',
  6653. ' };',
  6654. '});',
  6655. '$impl.aRec = $impl.TMyRecord.$new();',
  6656. '']) );
  6657. end;
  6658. procedure TTestModule.TestRenameJSNameConflict;
  6659. begin
  6660. StartProgram(false);
  6661. Add('var apply: longint;');
  6662. Add('var bind: longint;');
  6663. Add('var call: longint;');
  6664. Add('begin');
  6665. ConvertProgram;
  6666. CheckSource('TestRenameJSNameConflict',
  6667. LinesToStr([ // statements
  6668. 'this.Apply = 0;',
  6669. 'this.Bind = 0;',
  6670. 'this.Call = 0;'
  6671. ]),
  6672. LinesToStr([ // this.$main
  6673. ''
  6674. ]));
  6675. end;
  6676. procedure TTestModule.TestLocalConst;
  6677. begin
  6678. StartProgram(false);
  6679. Add('procedure DoIt;');
  6680. Add('const');
  6681. Add(' cA: longint = 1;');
  6682. Add(' cB = 2;');
  6683. Add(' procedure Sub;');
  6684. Add(' const');
  6685. Add(' csA = 3;');
  6686. Add(' cB: double = 4;');
  6687. Add(' begin');
  6688. Add(' cb:=cb+csa;');
  6689. Add(' ca:=ca+csa+5;');
  6690. Add(' end;');
  6691. Add('begin');
  6692. Add(' ca:=ca+cb+6;');
  6693. Add('end;');
  6694. Add('begin');
  6695. ConvertProgram;
  6696. CheckSource('TestLocalConst',
  6697. LinesToStr([
  6698. 'var cA = 1;',
  6699. 'var cB = 2;',
  6700. 'var csA = 3;',
  6701. 'var cB$1 = 4;',
  6702. 'this.DoIt = function () {',
  6703. ' function Sub() {',
  6704. ' cB$1 = cB$1 + 3;',
  6705. ' cA = cA + 3 + 5;',
  6706. ' };',
  6707. ' cA = cA + 2 + 6;',
  6708. '};'
  6709. ]),
  6710. LinesToStr([
  6711. ]));
  6712. end;
  6713. procedure TTestModule.TestVarExternal;
  6714. begin
  6715. StartProgram(false);
  6716. Add('var');
  6717. Add(' NaN: double; external name ''Global.NaN'';');
  6718. Add(' d: double;');
  6719. Add('begin');
  6720. Add(' d:=NaN;');
  6721. ConvertProgram;
  6722. CheckSource('TestVarExternal',
  6723. LinesToStr([
  6724. 'this.d = 0.0;'
  6725. ]),
  6726. LinesToStr([
  6727. '$mod.d = Global.NaN;'
  6728. ]));
  6729. end;
  6730. procedure TTestModule.TestVarExternalOtherUnit;
  6731. begin
  6732. AddModuleWithIntfImplSrc('unit2.pas',
  6733. LinesToStr([
  6734. 'var NaN: double; external name ''Global.NaN'';',
  6735. 'var iV: longint;'
  6736. ]),
  6737. '');
  6738. StartUnit(true);
  6739. Add('interface');
  6740. Add('uses unit2;');
  6741. Add('implementation');
  6742. Add('var');
  6743. Add(' d: double;');
  6744. Add(' i: longint; external name ''$i'';');
  6745. Add('begin');
  6746. Add(' d:=nan;');
  6747. Add(' d:=uNit2.nan;');
  6748. Add(' d:=test1.d;');
  6749. Add(' i:=iv;');
  6750. Add(' i:=uNit2.iv;');
  6751. Add(' i:=test1.i;');
  6752. ConvertUnit;
  6753. CheckSource('TestVarExternalOtherUnit',
  6754. LinesToStr([
  6755. 'var $impl = $mod.$impl;',
  6756. '']),
  6757. LinesToStr([ // this.$init
  6758. '$impl.d = Global.NaN;',
  6759. '$impl.d = Global.NaN;',
  6760. '$impl.d = $impl.d;',
  6761. '$i = pas.unit2.iV;',
  6762. '$i = pas.unit2.iV;',
  6763. '$i = $i;',
  6764. '']),
  6765. LinesToStr([ // implementation
  6766. '$impl.d = 0.0;',
  6767. '']) );
  6768. end;
  6769. procedure TTestModule.TestVarAbsoluteFail;
  6770. begin
  6771. StartProgram(false);
  6772. Add([
  6773. 'var',
  6774. ' a: longint;',
  6775. ' b: longword absolute a;',
  6776. 'begin']);
  6777. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6778. ConvertProgram;
  6779. end;
  6780. procedure TTestModule.TestConstExternal;
  6781. begin
  6782. StartProgram(false);
  6783. Add([
  6784. 'const',
  6785. ' PI: double; external name ''Global.PI'';',
  6786. ' Tau = 2*pi;',
  6787. 'var d: double;',
  6788. 'begin',
  6789. ' d:=pi;',
  6790. ' d:=tau+pi;']);
  6791. ConvertProgram;
  6792. CheckSource('TestConstExternal',
  6793. LinesToStr([
  6794. 'this.Tau = 2*Global.PI;',
  6795. 'this.d = 0.0;'
  6796. ]),
  6797. LinesToStr([
  6798. '$mod.d = Global.PI;',
  6799. '$mod.d = $mod.Tau + Global.PI;'
  6800. ]));
  6801. end;
  6802. procedure TTestModule.TestDouble;
  6803. begin
  6804. StartProgram(false);
  6805. Add([
  6806. 'type',
  6807. ' TDateTime = double;',
  6808. 'const',
  6809. ' a = TDateTime(2.7);',
  6810. ' b = a + TDateTime(1.7);',
  6811. ' c = 0.9 + 0.1;',
  6812. ' f0_1 = 0.1;',
  6813. ' f0_3 = 0.3;',
  6814. ' fn0_1 = -0.1;',
  6815. ' fn0_3 = -0.3;',
  6816. ' fn0_003 = -0.003;',
  6817. ' fn0_123456789 = -0.123456789;',
  6818. ' fn300_0 = -300.0;',
  6819. ' fn123456_0 = -123456.0;',
  6820. ' fn1234567_8 = -1234567.8;',
  6821. ' fn12345678_9 = -12345678.9;',
  6822. ' f1_0En12 = 1E-12;',
  6823. ' fn1_0En12 = -1E-12;',
  6824. ' maxdouble = 1.7e+308;',
  6825. ' mindouble = -1.7e+308;',
  6826. ' MinSafeIntDouble = -$1fffffffffffff;',
  6827. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6828. ' MaxSafeIntDouble = $1fffffffffffff;',
  6829. ' DZeroResolution = 1E-12;',
  6830. ' Minus1 = -1E-12;',
  6831. ' EPS = 1E-9;',
  6832. ' DELTA = 0.001;',
  6833. ' Big = 129.789E+100;',
  6834. ' Test0_15 = 0.15;',
  6835. ' Test999 = 2.9999999999999;',
  6836. ' Test111999 = 211199999999999000.0;',
  6837. ' TestMinus111999 = -211199999999999000.0;',
  6838. 'var',
  6839. ' d: double = b;',
  6840. 'begin',
  6841. ' d:=1.0;',
  6842. ' d:=1.0/3.0;',
  6843. ' d:=1/3;',
  6844. ' d:=5.0E-324;',
  6845. ' d:=1.7E308;',
  6846. ' d:=001.00E00;',
  6847. ' d:=002.00E001;',
  6848. ' d:=003.000E000;',
  6849. ' d:=-004.00E-00;',
  6850. ' d:=-005.00E-001;',
  6851. ' d:=10**3;',
  6852. ' d:=10 mod 3;',
  6853. ' d:=10 div 3;',
  6854. ' d:=c;',
  6855. ' d:=f0_1;',
  6856. ' d:=f0_3;',
  6857. ' d:=fn0_1;',
  6858. ' d:=fn0_3;',
  6859. ' d:=fn0_003;',
  6860. ' d:=fn0_123456789;',
  6861. ' d:=fn300_0;',
  6862. ' d:=fn123456_0;',
  6863. ' d:=fn1234567_8;',
  6864. ' d:=fn12345678_9;',
  6865. ' d:=f1_0En12;',
  6866. ' d:=fn1_0En12;',
  6867. ' d:=maxdouble;',
  6868. ' d:=mindouble;',
  6869. ' d:=MinSafeIntDouble;',
  6870. ' d:=double(MinSafeIntDouble);',
  6871. ' d:=MinSafeIntDouble2;',
  6872. ' d:=double(MinSafeIntDouble2);',
  6873. ' d:=MaxSafeIntDouble;',
  6874. ' d:=default(double);',
  6875. '']);
  6876. ConvertProgram;
  6877. CheckSource('TestDouble',
  6878. LinesToStr([
  6879. 'this.a = 2.7;',
  6880. 'this.b = 2.7 + 1.7;',
  6881. 'this.c = 0.9 + 0.1;',
  6882. 'this.f0_1 = 0.1;',
  6883. 'this.f0_3 = 0.3;',
  6884. 'this.fn0_1 = -0.1;',
  6885. 'this.fn0_3 = -0.3;',
  6886. 'this.fn0_003 = -0.003;',
  6887. 'this.fn0_123456789 = -0.123456789;',
  6888. 'this.fn300_0 = -300.0;',
  6889. 'this.fn123456_0 = -123456.0;',
  6890. 'this.fn1234567_8 = -1234567.8;',
  6891. 'this.fn12345678_9 = -12345678.9;',
  6892. 'this.f1_0En12 = 1E-12;',
  6893. 'this.fn1_0En12 = -1E-12;',
  6894. 'this.maxdouble = 1.7e+308;',
  6895. 'this.mindouble = -1.7e+308;',
  6896. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6897. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6898. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6899. 'this.DZeroResolution = 1E-12;',
  6900. 'this.Minus1 = -1E-12;',
  6901. 'this.EPS = 1E-9;',
  6902. 'this.DELTA = 0.001;',
  6903. 'this.Big = 129.789E+100;',
  6904. 'this.Test0_15 = 0.15;',
  6905. 'this.Test999 = 2.9999999999999;',
  6906. 'this.Test111999 = 211199999999999000.0;',
  6907. 'this.TestMinus111999 = -211199999999999000.0;',
  6908. 'this.d = 4.4;'
  6909. ]),
  6910. LinesToStr([
  6911. '$mod.d = 1.0;',
  6912. '$mod.d = 1.0 / 3.0;',
  6913. '$mod.d = 1 / 3;',
  6914. '$mod.d = 5.0E-324;',
  6915. '$mod.d = 1.7E308;',
  6916. '$mod.d = 1.00E0;',
  6917. '$mod.d = 2.00E1;',
  6918. '$mod.d = 3.000E0;',
  6919. '$mod.d = -4.00E-0;',
  6920. '$mod.d = -5.00E-1;',
  6921. '$mod.d = Math.pow(10, 3);',
  6922. '$mod.d = 10 % 3;',
  6923. '$mod.d = rtl.trunc(10 / 3);',
  6924. '$mod.d = 1;',
  6925. '$mod.d = 0.1;',
  6926. '$mod.d = 0.3;',
  6927. '$mod.d = -0.1;',
  6928. '$mod.d = -0.3;',
  6929. '$mod.d = -0.003;',
  6930. '$mod.d = -0.123456789;',
  6931. '$mod.d = -300;',
  6932. '$mod.d = -123456;',
  6933. '$mod.d = -1234567.8;',
  6934. '$mod.d = -1.23456789E7;',
  6935. '$mod.d = 1E-12;',
  6936. '$mod.d = -1E-12;',
  6937. '$mod.d = 1.7E308;',
  6938. '$mod.d = -1.7E308;',
  6939. '$mod.d = -9007199254740991;',
  6940. '$mod.d = -9007199254740991;',
  6941. '$mod.d = -9.007199254740992E15;',
  6942. '$mod.d = -9.007199254740992E15;',
  6943. '$mod.d = 9007199254740991;',
  6944. '$mod.d = 0.0;',
  6945. '']));
  6946. end;
  6947. procedure TTestModule.TestInteger;
  6948. begin
  6949. StartProgram(false);
  6950. Add([
  6951. 'const',
  6952. ' MinInt = low(NativeInt);',
  6953. ' MaxInt = high(NativeInt);',
  6954. 'type',
  6955. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6956. 'const',
  6957. ' a = low(TMyInt)+High(TMyInt);',
  6958. 'var',
  6959. ' i: TMyInt;',
  6960. 'begin',
  6961. ' i:=-MinInt;',
  6962. ' i:=default(TMyInt);',
  6963. ' i:=low(i)+high(i);',
  6964. '']);
  6965. ConvertProgram;
  6966. CheckSource('TestIntegerRange',
  6967. LinesToStr([
  6968. 'this.MinInt = -9007199254740991;',
  6969. 'this.MaxInt = 9007199254740991;',
  6970. 'this.a = -9007199254740991 + 9007199254740991;',
  6971. 'this.i = 0;',
  6972. '']),
  6973. LinesToStr([
  6974. '$mod.i = - -9007199254740991;',
  6975. '$mod.i = -9007199254740991;',
  6976. '$mod.i = -9007199254740991 + 9007199254740991;',
  6977. '']));
  6978. end;
  6979. procedure TTestModule.TestIntegerRange;
  6980. begin
  6981. StartProgram(false);
  6982. Add([
  6983. 'const',
  6984. ' MinInt = -1;',
  6985. ' MaxInt = +1;',
  6986. 'type',
  6987. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6988. ' TInt2 = 1..3;',
  6989. 'const',
  6990. ' a = low(TMyInt)+High(TMyInt);',
  6991. ' b = low(TInt2)+High(TInt2);',
  6992. ' s1 = [1];',
  6993. ' s2 = [1,2];',
  6994. ' s3 = [1..3];',
  6995. ' s4 = [low(shortint)..high(shortint)];',
  6996. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6997. ' s6 = 1 in s2;',
  6998. 'var',
  6999. ' i: TMyInt;',
  7000. ' i2: TInt2;',
  7001. 'begin',
  7002. ' i:=i2;',
  7003. ' i:=default(TMyInt);',
  7004. ' if i=i2 then ;',
  7005. ' i:=ord(i2);',
  7006. '']);
  7007. ConvertProgram;
  7008. CheckSource('TestIntegerRange',
  7009. LinesToStr([
  7010. 'this.MinInt = -1;',
  7011. 'this.MaxInt = +1;',
  7012. 'this.a = -1 + 1;',
  7013. 'this.b = 1 + 3;',
  7014. 'this.s1 = rtl.createSet(1);',
  7015. 'this.s2 = rtl.createSet(1, 2);',
  7016. 'this.s3 = rtl.createSet(null, 1, 3);',
  7017. 'this.s4 = rtl.createSet(null, -128, 127);',
  7018. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7019. 'this.s6 = 1 in this.s2;',
  7020. 'this.i = 0;',
  7021. 'this.i2 = 0;',
  7022. '']),
  7023. LinesToStr([
  7024. '$mod.i = $mod.i2;',
  7025. '$mod.i = -1;',
  7026. 'if ($mod.i === $mod.i2) ;',
  7027. '$mod.i = $mod.i2;',
  7028. '']));
  7029. end;
  7030. procedure TTestModule.TestIntegerTypecasts;
  7031. begin
  7032. StartProgram(false);
  7033. Add([
  7034. 'var',
  7035. ' i: nativeint;',
  7036. ' b: byte;',
  7037. ' sh: shortint;',
  7038. ' w: word;',
  7039. ' sm: smallint;',
  7040. ' lw: longword;',
  7041. ' li: longint;',
  7042. 'begin',
  7043. ' b:=byte(i);',
  7044. ' sh:=shortint(i);',
  7045. ' w:=word(i);',
  7046. ' sm:=smallint(i);',
  7047. ' lw:=longword(i);',
  7048. ' li:=longint(i);',
  7049. '']);
  7050. ConvertProgram;
  7051. CheckSource('TestIntegerTypecasts',
  7052. LinesToStr([
  7053. 'this.i = 0;',
  7054. 'this.b = 0;',
  7055. 'this.sh = 0;',
  7056. 'this.w = 0;',
  7057. 'this.sm = 0;',
  7058. 'this.lw = 0;',
  7059. 'this.li = 0;',
  7060. '']),
  7061. LinesToStr([
  7062. '$mod.b = $mod.i & 255;',
  7063. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7064. '$mod.w = $mod.i & 65535;',
  7065. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7066. '$mod.lw = $mod.i >>> 0;',
  7067. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7068. '']));
  7069. end;
  7070. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7071. begin
  7072. StartProgram(false);
  7073. Add([
  7074. 'var',
  7075. ' i,j: nativeint;',
  7076. 'begin',
  7077. ' i:=i shr 0;',
  7078. ' i:=i shr 1;',
  7079. ' i:=i shr 3;',
  7080. ' i:=i shr 54;',
  7081. ' i:=j shr i;',
  7082. '']);
  7083. ConvertProgram;
  7084. CheckResolverUnexpectedHints;
  7085. CheckSource('TestInteger_BitwiseShrNativeInt',
  7086. LinesToStr([
  7087. 'this.i = 0;',
  7088. 'this.j = 0;',
  7089. '']),
  7090. LinesToStr([
  7091. '$mod.i = $mod.i;',
  7092. '$mod.i = Math.floor($mod.i / 2);',
  7093. '$mod.i = Math.floor($mod.i / 8);',
  7094. '$mod.i = 0;',
  7095. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7096. '']));
  7097. end;
  7098. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7099. begin
  7100. StartProgram(false);
  7101. Add([
  7102. 'var',
  7103. ' i: nativeint;',
  7104. 'begin',
  7105. ' i:=i shl 0;',
  7106. ' i:=i shl 54;',
  7107. ' i:=123456789012 shl 1;',
  7108. ' i:=i shl 1;',
  7109. '']);
  7110. ConvertProgram;
  7111. CheckResolverUnexpectedHints;
  7112. CheckSource('TestInteger_BitwiseShrNativeInt',
  7113. LinesToStr([
  7114. 'this.i = 0;',
  7115. '']),
  7116. LinesToStr([
  7117. '$mod.i = $mod.i;',
  7118. '$mod.i = 0;',
  7119. '$mod.i = 246913578024;',
  7120. '$mod.i = rtl.shl($mod.i, 1);',
  7121. '']));
  7122. end;
  7123. procedure TTestModule.TestInteger_SystemFunc;
  7124. begin
  7125. StartProgram(true);
  7126. Add([
  7127. 'var',
  7128. ' i: byte;',
  7129. ' s: string;',
  7130. 'begin',
  7131. ' system.inc(i);',
  7132. ' system.str(i,s);',
  7133. ' s:=system.str(i);',
  7134. ' i:=system.low(i);',
  7135. ' i:=system.high(i);',
  7136. ' i:=system.pred(i);',
  7137. ' i:=system.succ(i);',
  7138. ' i:=system.ord(i);',
  7139. '']);
  7140. ConvertProgram;
  7141. CheckResolverUnexpectedHints;
  7142. CheckSource('TestInteger_SystemFunc',
  7143. LinesToStr([
  7144. 'this.i = 0;',
  7145. 'this.s = "";',
  7146. '']),
  7147. LinesToStr([
  7148. '$mod.i += 1;',
  7149. '$mod.s = "" + $mod.i;',
  7150. '$mod.s = "" + $mod.i;',
  7151. '$mod.i = 0;',
  7152. '$mod.i = 255;',
  7153. '$mod.i = $mod.i - 1;',
  7154. '$mod.i = $mod.i + 1;',
  7155. '$mod.i = $mod.i;',
  7156. '']));
  7157. end;
  7158. procedure TTestModule.TestCurrency;
  7159. begin
  7160. StartProgram(false);
  7161. Add([
  7162. 'type',
  7163. ' TCoin = currency;',
  7164. 'const',
  7165. ' a = TCoin(2.7);',
  7166. ' b = a + TCoin(1.7);',
  7167. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7168. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7169. 'var',
  7170. ' c: TCoin = b;',
  7171. ' i: nativeint;',
  7172. ' d: double;',
  7173. ' j: jsvalue;',
  7174. 'function DoIt(c: currency): currency; begin end;',
  7175. 'function GetIt(d: double): double; begin end;',
  7176. 'procedure Write(v: jsvalue); begin end;',
  7177. 'begin',
  7178. ' c:=1.0;',
  7179. ' c:=0.1;',
  7180. ' c:=1.0/3.0;',
  7181. ' c:=1/3;',
  7182. ' c:=a;',
  7183. ' d:=c;',
  7184. ' c:=d;',
  7185. ' c:=currency(c);',
  7186. ' c:=currency(d);',
  7187. ' d:=double(c);',
  7188. ' c:=i;',
  7189. ' c:=currency(i);',
  7190. //' i:=c;', not allowed
  7191. ' i:=nativeint(c);',
  7192. ' c:=c+a;',
  7193. ' c:=-c-a;',
  7194. ' c:=d+c;',
  7195. ' c:=c+d;',
  7196. ' c:=d-c;',
  7197. ' c:=c-d;',
  7198. ' c:=c*a;',
  7199. ' c:=a*c;',
  7200. ' c:=d*c;',
  7201. ' c:=c*d;',
  7202. ' c:=c/a;',
  7203. ' c:=a/c;',
  7204. ' c:=d/c;',
  7205. ' c:=c/d;',
  7206. ' c:=c**a;',
  7207. ' c:=a**c;',
  7208. ' c:=d**c;',
  7209. ' c:=c**d;',
  7210. ' if c=c then ;',
  7211. ' if c=a then ;',
  7212. ' if a=c then ;',
  7213. ' if d=c then ;',
  7214. ' if c=d then ;',
  7215. ' c:=DoIt(c);',
  7216. ' c:=DoIt(i);',
  7217. ' c:=DoIt(d);',
  7218. ' c:=GetIt(c);',
  7219. ' j:=c;',
  7220. ' Write(c);',
  7221. ' c:=default(currency);',
  7222. ' j:=str(c);',
  7223. ' j:=str(c:0:3);',
  7224. '']);
  7225. ConvertProgram;
  7226. CheckSource('TestCurrency',
  7227. LinesToStr([
  7228. 'this.a = 27000;',
  7229. 'this.b = this.a + 17000;',
  7230. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7231. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7232. 'this.c = this.b;',
  7233. 'this.i = 0;',
  7234. 'this.d = 0.0;',
  7235. 'this.j = undefined;',
  7236. 'this.DoIt = function (c) {',
  7237. ' var Result = 0;',
  7238. ' return Result;',
  7239. '};',
  7240. 'this.GetIt = function (d) {',
  7241. ' var Result = 0.0;',
  7242. ' return Result;',
  7243. '};',
  7244. 'this.Write = function (v) {',
  7245. '};',
  7246. '']),
  7247. LinesToStr([
  7248. '$mod.c = 10000;',
  7249. '$mod.c = 1000;',
  7250. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7251. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7252. '$mod.c = $mod.a;',
  7253. '$mod.d = $mod.c / 10000;',
  7254. '$mod.c = rtl.trunc($mod.d * 10000);',
  7255. '$mod.c = $mod.c;',
  7256. '$mod.c = $mod.d * 10000;',
  7257. '$mod.d = $mod.c / 10000;',
  7258. '$mod.c = $mod.i * 10000;',
  7259. '$mod.c = $mod.i * 10000;',
  7260. '$mod.i = rtl.trunc($mod.c / 10000);',
  7261. '$mod.c = $mod.c + $mod.a;',
  7262. '$mod.c = -$mod.c - $mod.a;',
  7263. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7264. '$mod.c = $mod.c + ($mod.d * 10000);',
  7265. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7266. '$mod.c = $mod.c - ($mod.d * 10000);',
  7267. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7268. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7269. '$mod.c = $mod.d * $mod.c;',
  7270. '$mod.c = $mod.c * $mod.d;',
  7271. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7272. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7273. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7274. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7275. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7276. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7277. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7278. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7279. 'if ($mod.c === $mod.c) ;',
  7280. 'if ($mod.c === $mod.a) ;',
  7281. 'if ($mod.a === $mod.c) ;',
  7282. 'if (($mod.d * 10000) === $mod.c) ;',
  7283. 'if ($mod.c === ($mod.d * 10000)) ;',
  7284. '$mod.c = $mod.DoIt($mod.c);',
  7285. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7286. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7287. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7288. '$mod.j = $mod.c / 10000;',
  7289. '$mod.Write($mod.c / 10000);',
  7290. '$mod.c = 0;',
  7291. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7292. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7293. '']));
  7294. end;
  7295. procedure TTestModule.TestForBoolDo;
  7296. begin
  7297. StartProgram(false);
  7298. Add([
  7299. 'var b: boolean;',
  7300. 'begin',
  7301. ' for b:=false to true do ;',
  7302. ' for b:=b downto false do ;',
  7303. ' for b in boolean do ;',
  7304. '']);
  7305. ConvertProgram;
  7306. CheckSource('TestForBoolDo',
  7307. LinesToStr([ // statements
  7308. 'this.b = false;']),
  7309. LinesToStr([ // this.$main
  7310. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7311. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7312. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7313. '']));
  7314. end;
  7315. procedure TTestModule.TestForIntDo;
  7316. begin
  7317. StartProgram(false);
  7318. Add([
  7319. 'var i: longint;',
  7320. 'begin',
  7321. ' for i:=3 to 5 do ;',
  7322. ' for i:=i downto 2 do ;',
  7323. ' for i in byte do ;',
  7324. '']);
  7325. ConvertProgram;
  7326. CheckSource('TestForIntDo',
  7327. LinesToStr([ // statements
  7328. 'this.i = 0;']),
  7329. LinesToStr([ // this.$main
  7330. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7331. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7332. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7333. '']));
  7334. end;
  7335. procedure TTestModule.TestForIntInDo;
  7336. begin
  7337. StartProgram(false);
  7338. Add([
  7339. 'type',
  7340. ' TSetOfInt = set of byte;',
  7341. ' TIntRg = 3..7;',
  7342. ' TSetOfIntRg = set of TIntRg;',
  7343. 'var',
  7344. ' i,i2: longint;',
  7345. ' a1: array of byte;',
  7346. ' a2: array[1..3] of byte;',
  7347. ' soi: TSetOfInt;',
  7348. ' soir: TSetOfIntRg;',
  7349. ' ir: TIntRg;',
  7350. 'begin',
  7351. ' for i in byte do ;',
  7352. ' for i in a1 do ;',
  7353. ' for i in a2 do ;',
  7354. ' for i in [11..13] do ;',
  7355. ' for i in TSetOfInt do ;',
  7356. ' for i in TIntRg do ;',
  7357. ' for i in soi do i2:=i;',
  7358. ' for i in TSetOfIntRg do ;',
  7359. ' for i in soir do ;',
  7360. ' for ir in TIntRg do ;',
  7361. ' for ir in TSetOfIntRg do ;',
  7362. ' for ir in soir do ;',
  7363. '']);
  7364. ConvertProgram;
  7365. CheckSource('TestForIntInDo',
  7366. LinesToStr([ // statements
  7367. 'this.i = 0;',
  7368. 'this.i2 = 0;',
  7369. 'this.a1 = [];',
  7370. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7371. 'this.soi = {};',
  7372. 'this.soir = {};',
  7373. 'this.ir = 0;',
  7374. '']),
  7375. LinesToStr([ // this.$main
  7376. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7377. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7378. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7379. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7380. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7381. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7382. 'for (var $l6 in $mod.soi) {',
  7383. ' $mod.i = +$l6;',
  7384. ' $mod.i2 = $mod.i;',
  7385. '};',
  7386. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7387. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7388. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7389. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7390. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7391. '']));
  7392. end;
  7393. procedure TTestModule.TestCharConst;
  7394. begin
  7395. StartProgram(false);
  7396. Add([
  7397. 'const',
  7398. ' a = #$00F3;',
  7399. ' c: char = ''1'';',
  7400. ' wc: widechar = ''ä'';',
  7401. 'begin',
  7402. ' c:=#0;',
  7403. ' c:=#1;',
  7404. ' c:=#9;',
  7405. ' c:=#10;',
  7406. ' c:=#13;',
  7407. ' c:=#31;',
  7408. ' c:=#32;',
  7409. ' c:=#$A;',
  7410. ' c:=#$0A;',
  7411. ' c:=#$b;',
  7412. ' c:=#$0b;',
  7413. ' c:=^A;',
  7414. ' c:=''"'';',
  7415. ' c:=default(char);',
  7416. ' c:=#$00E4;', // ä
  7417. ' c:=''ä'';',
  7418. ' c:=#$E4;', // ä
  7419. ' c:=#$D800;', // invalid UTF-16
  7420. ' c:=#$DFFF;', // invalid UTF-16
  7421. ' c:=#$FFFF;', // last UCS-2
  7422. ' c:=high(c);', // last UCS-2
  7423. ' c:=#269;',
  7424. '']);
  7425. ConvertProgram;
  7426. CheckSource('TestCharConst',
  7427. LinesToStr([
  7428. 'this.a="ó";',
  7429. 'this.c="1";',
  7430. 'this.wc="ä";'
  7431. ]),
  7432. LinesToStr([
  7433. '$mod.c="\x00";',
  7434. '$mod.c="\x01";',
  7435. '$mod.c="\t";',
  7436. '$mod.c="\n";',
  7437. '$mod.c="\r";',
  7438. '$mod.c="\x1F";',
  7439. '$mod.c=" ";',
  7440. '$mod.c="\n";',
  7441. '$mod.c="\n";',
  7442. '$mod.c="\x0B";',
  7443. '$mod.c="\x0B";',
  7444. '$mod.c="\x01";',
  7445. '$mod.c=''"'';',
  7446. '$mod.c="\x00";',
  7447. '$mod.c = "ä";',
  7448. '$mod.c = "ä";',
  7449. '$mod.c = "ä";',
  7450. '$mod.c="\uD800";',
  7451. '$mod.c="\uDFFF";',
  7452. '$mod.c="\uFFFF";',
  7453. '$mod.c="\uFFFF";',
  7454. '$mod.c = "č";',
  7455. '']));
  7456. end;
  7457. procedure TTestModule.TestChar_Compare;
  7458. begin
  7459. StartProgram(false);
  7460. Add('var');
  7461. Add(' c: char;');
  7462. Add(' b: boolean;');
  7463. Add('begin');
  7464. Add(' b:=c=''1'';');
  7465. Add(' b:=''2''=c;');
  7466. Add(' b:=''3''=''4'';');
  7467. Add(' b:=c<>''5'';');
  7468. Add(' b:=''6''<>c;');
  7469. Add(' b:=c>''7'';');
  7470. Add(' b:=''8''>c;');
  7471. Add(' b:=c>=''9'';');
  7472. Add(' b:=''A''>=c;');
  7473. Add(' b:=c<''B'';');
  7474. Add(' b:=''C''<c;');
  7475. Add(' b:=c<=''D'';');
  7476. Add(' b:=''E''<=c;');
  7477. ConvertProgram;
  7478. CheckSource('TestChar_Compare',
  7479. LinesToStr([
  7480. 'this.c="";',
  7481. 'this.b = false;'
  7482. ]),
  7483. LinesToStr([
  7484. '$mod.b = $mod.c === "1";',
  7485. '$mod.b = "2" === $mod.c;',
  7486. '$mod.b = "3" === "4";',
  7487. '$mod.b = $mod.c !== "5";',
  7488. '$mod.b = "6" !== $mod.c;',
  7489. '$mod.b = $mod.c > "7";',
  7490. '$mod.b = "8" > $mod.c;',
  7491. '$mod.b = $mod.c >= "9";',
  7492. '$mod.b = "A" >= $mod.c;',
  7493. '$mod.b = $mod.c < "B";',
  7494. '$mod.b = "C" < $mod.c;',
  7495. '$mod.b = $mod.c <= "D";',
  7496. '$mod.b = "E" <= $mod.c;',
  7497. '']));
  7498. end;
  7499. procedure TTestModule.TestChar_BuiltInProcs;
  7500. begin
  7501. StartProgram(false);
  7502. Add([
  7503. 'var',
  7504. ' c: char;',
  7505. ' i: longint;',
  7506. ' s: string;',
  7507. 'begin',
  7508. ' i:=ord(c);',
  7509. ' i:=ord(s[i]);',
  7510. ' c:=chr(i);',
  7511. ' c:=pred(c);',
  7512. ' c:=succ(c);',
  7513. ' c:=low(c);',
  7514. ' c:=high(c);',
  7515. ' i:=byte(c);',
  7516. ' i:=word(c);',
  7517. ' i:=longint(c);',
  7518. '']);
  7519. ConvertProgram;
  7520. CheckSource('TestChar_BuiltInProcs',
  7521. LinesToStr([
  7522. 'this.c = "";',
  7523. 'this.i = 0;',
  7524. 'this.s = "";'
  7525. ]),
  7526. LinesToStr([
  7527. '$mod.i = $mod.c.charCodeAt();',
  7528. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7529. '$mod.c = String.fromCharCode($mod.i);',
  7530. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7531. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7532. '$mod.c = "\x00";',
  7533. '$mod.c = "\uFFFF";',
  7534. '$mod.i = $mod.c.charCodeAt() & 255;',
  7535. '$mod.i = $mod.c.charCodeAt();',
  7536. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7537. '']));
  7538. end;
  7539. procedure TTestModule.TestStringConst;
  7540. begin
  7541. StartProgram(false);
  7542. Add([
  7543. '{$H+}',
  7544. 'const',
  7545. ' a = #$00F3#$017C;', // first <256, then >=256
  7546. ' b = string(''a'');',
  7547. ' c = string(''ä'');',
  7548. ' d = UnicodeString(''b'');',
  7549. ' e = UnicodeString(''ö'');',
  7550. 'var',
  7551. ' s: string = ''abc'';',
  7552. 'begin',
  7553. ' s:='''';',
  7554. ' s:=#13#10;',
  7555. ' s:=#9''foo'';',
  7556. ' s:=#$A9;',
  7557. ' s:=''foo''#13''bar'';',
  7558. ' s:=''"'';',
  7559. ' s:=''"''''"'';',
  7560. ' s:=#$20AC;', // euro
  7561. ' s:=#$10437;', // outside BMP
  7562. ' s:=''abc''#$20AC;', // ascii,#
  7563. ' s:=''ä''#$20AC;', // non ascii,#
  7564. ' s:=#$20AC''abc'';', // #, ascii
  7565. ' s:=#$20AC''ä'';', // #, non ascii
  7566. ' s:=default(string);',
  7567. ' s:=concat(s);',
  7568. ' s:=concat(s,''a'',s);',
  7569. ' s:=#250#269;',
  7570. //' s:=#$2F804;',
  7571. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7572. '']);
  7573. ConvertProgram;
  7574. CheckSource('TestStringConst',
  7575. LinesToStr([
  7576. 'this.a = "óż";',
  7577. 'this.b = "a";',
  7578. 'this.c = "ä";',
  7579. 'this.d = "b";',
  7580. 'this.e = "ö";',
  7581. 'this.s="abc";',
  7582. '']),
  7583. LinesToStr([
  7584. '$mod.s="";',
  7585. '$mod.s="\r\n";',
  7586. '$mod.s="\tfoo";',
  7587. '$mod.s="©";',
  7588. '$mod.s="foo\rbar";',
  7589. '$mod.s=''"'';',
  7590. '$mod.s=''"\''"'';',
  7591. '$mod.s="€";',
  7592. '$mod.s="'#$F0#$90#$90#$B7'";',
  7593. '$mod.s = "abc€";',
  7594. '$mod.s = "ä€";',
  7595. '$mod.s = "€abc";',
  7596. '$mod.s = "ۊ";',
  7597. '$mod.s="";',
  7598. '$mod.s = $mod.s;',
  7599. '$mod.s = $mod.s.concat("a", $mod.s);',
  7600. '$mod.s = "úč";',
  7601. '']));
  7602. end;
  7603. procedure TTestModule.TestStringConst_InvalidUTF16;
  7604. begin
  7605. StartProgram(false);
  7606. Add([
  7607. 'const',
  7608. ' a: char = #$D87E;',
  7609. ' b: string = #$D87E;',
  7610. ' c: string = #$D87E#43;',
  7611. 'begin',
  7612. ' c:=''abc''#$D87E;',
  7613. ' c:=#0#1#2;',
  7614. ' c:=#127;',
  7615. ' c:=#128;',
  7616. ' c:=#255;',
  7617. ' c:=#256;',
  7618. '']);
  7619. ConvertProgram;
  7620. CheckSource('TestStringConst',
  7621. LinesToStr([
  7622. 'this.a = "\uD87E";',
  7623. 'this.b = "\uD87E";',
  7624. 'this.c = "\uD87E+";',
  7625. '']),
  7626. LinesToStr([
  7627. '$mod.c = "abc\uD87E";',
  7628. '$mod.c = "\x00\x01\x02";',
  7629. '$mod.c = "'#127'";',
  7630. '$mod.c = "'#$c2#$80'";',
  7631. '$mod.c = "'#$c3#$BF'";',
  7632. '$mod.c = "'#$c4#$80'";',
  7633. '']));
  7634. end;
  7635. procedure TTestModule.TestStringConstSurrogate;
  7636. begin
  7637. StartProgram(false);
  7638. Add([
  7639. 'var',
  7640. ' s: string;',
  7641. 'begin',
  7642. ' s:=''😊'';', // 1F60A
  7643. '']);
  7644. ConvertProgram;
  7645. CheckSource('TestStringConstSurrogate',
  7646. LinesToStr([
  7647. 'this.s="";'
  7648. ]),
  7649. LinesToStr([
  7650. '$mod.s="😊";'
  7651. ]));
  7652. end;
  7653. procedure TTestModule.TestString_Length;
  7654. begin
  7655. StartProgram(false);
  7656. Add('const c = ''foo'';');
  7657. Add('var');
  7658. Add(' s: string;');
  7659. Add(' i: longint;');
  7660. Add('begin');
  7661. Add(' i:=length(s);');
  7662. Add(' i:=length(s+s);');
  7663. Add(' i:=length(''abc'');');
  7664. Add(' i:=length(c);');
  7665. ConvertProgram;
  7666. CheckSource('TestString_Length',
  7667. LinesToStr([
  7668. 'this.c = "foo";',
  7669. 'this.s = "";',
  7670. 'this.i = 0;',
  7671. '']),
  7672. LinesToStr([
  7673. '$mod.i = $mod.s.length;',
  7674. '$mod.i = ($mod.s+$mod.s).length;',
  7675. '$mod.i = "abc".length;',
  7676. '$mod.i = $mod.c.length;',
  7677. '']));
  7678. end;
  7679. procedure TTestModule.TestString_Compare;
  7680. begin
  7681. StartProgram(false);
  7682. Add('var');
  7683. Add(' s, t: string;');
  7684. Add(' b: boolean;');
  7685. Add('begin');
  7686. Add(' b:=s=t;');
  7687. Add(' b:=s<>t;');
  7688. Add(' b:=s>t;');
  7689. Add(' b:=s>=t;');
  7690. Add(' b:=s<t;');
  7691. Add(' b:=s<=t;');
  7692. ConvertProgram;
  7693. CheckSource('TestString_Compare',
  7694. LinesToStr([ // statements
  7695. 'this.s = "";',
  7696. 'this.t = "";',
  7697. 'this.b =false;'
  7698. ]),
  7699. LinesToStr([ // this.$main
  7700. '$mod.b = $mod.s === $mod.t;',
  7701. '$mod.b = $mod.s !== $mod.t;',
  7702. '$mod.b = $mod.s > $mod.t;',
  7703. '$mod.b = $mod.s >= $mod.t;',
  7704. '$mod.b = $mod.s < $mod.t;',
  7705. '$mod.b = $mod.s <= $mod.t;',
  7706. '']));
  7707. end;
  7708. procedure TTestModule.TestString_SetLength;
  7709. begin
  7710. StartProgram(false);
  7711. Add([
  7712. 'procedure DoIt(var s: string);',
  7713. 'begin',
  7714. ' SetLength(s,2);',
  7715. 'end;',
  7716. 'var s: string;',
  7717. 'begin',
  7718. ' SetLength(s,3);',
  7719. '']);
  7720. ConvertProgram;
  7721. CheckSource('TestString_SetLength',
  7722. LinesToStr([ // statements
  7723. 'this.DoIt = function (s) {',
  7724. ' s.set(rtl.strSetLength(s.get(), 2));',
  7725. '};',
  7726. 'this.s = "";',
  7727. '']),
  7728. LinesToStr([ // this.$main
  7729. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7730. ]));
  7731. end;
  7732. procedure TTestModule.TestString_CharAt;
  7733. begin
  7734. StartProgram(false);
  7735. Add([
  7736. 'var',
  7737. ' s: string;',
  7738. ' c: char;',
  7739. ' b: boolean;',
  7740. 'begin',
  7741. ' b:= s[1] = c;',
  7742. ' b:= c = s[1];',
  7743. ' b:= c <> s[1];',
  7744. ' b:= c > s[1];',
  7745. ' b:= c >= s[1];',
  7746. ' b:= c < s[2];',
  7747. ' b:= c <= s[1];',
  7748. ' s[1] := c;',
  7749. ' s[2+3] := c;']);
  7750. ConvertProgram;
  7751. CheckSource('TestString_CharAt',
  7752. LinesToStr([ // statements
  7753. 'this.s = "";',
  7754. 'this.c = "";',
  7755. 'this.b = false;'
  7756. ]),
  7757. LinesToStr([ // this.$main
  7758. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7759. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7760. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7761. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7762. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7763. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7764. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7765. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7766. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7767. '']));
  7768. end;
  7769. procedure TTestModule.TestStringHMinusFail;
  7770. begin
  7771. StartProgram(false);
  7772. Add([
  7773. '{$H-}',
  7774. 'var s: string;',
  7775. 'begin']);
  7776. ConvertProgram;
  7777. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7778. end;
  7779. procedure TTestModule.TestStr;
  7780. begin
  7781. StartProgram(false);
  7782. Add('var');
  7783. Add(' b: boolean;');
  7784. Add(' i: longint;');
  7785. Add(' d: double;');
  7786. Add(' s: string;');
  7787. Add('begin');
  7788. Add(' str(b,s);');
  7789. Add(' str(i,s);');
  7790. Add(' str(d,s);');
  7791. Add(' str(i:3,s);');
  7792. Add(' str(d:3:2,s);');
  7793. Add(' Str(12.456:12:1,s);');
  7794. Add(' Str(12.456:12,s);');
  7795. Add(' s:=str(b);');
  7796. Add(' s:=str(i);');
  7797. Add(' s:=str(d);');
  7798. Add(' s:=str(i,i);');
  7799. Add(' s:=str(i:3);');
  7800. Add(' s:=str(d:3:2);');
  7801. Add(' s:=str(i:4,i);');
  7802. Add(' s:=str(i,i:5);');
  7803. Add(' s:=str(i:4,i:5);');
  7804. Add(' s:=str(s,s);');
  7805. Add(' s:=str(s,''foo'');');
  7806. ConvertProgram;
  7807. CheckSource('TestStr',
  7808. LinesToStr([ // statements
  7809. 'this.b = false;',
  7810. 'this.i = 0;',
  7811. 'this.d = 0.0;',
  7812. 'this.s = "";',
  7813. '']),
  7814. LinesToStr([ // this.$main
  7815. '$mod.s = ""+$mod.b;',
  7816. '$mod.s = ""+$mod.i;',
  7817. '$mod.s = rtl.floatToStr($mod.d);',
  7818. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7819. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7820. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7821. '$mod.s = rtl.floatToStr(12.456,12);',
  7822. '$mod.s = ""+$mod.b;',
  7823. '$mod.s = ""+$mod.i;',
  7824. '$mod.s = rtl.floatToStr($mod.d);',
  7825. '$mod.s = ""+$mod.i+$mod.i;',
  7826. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7827. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7828. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7829. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7830. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7831. '$mod.s = $mod.s + $mod.s;',
  7832. '$mod.s = $mod.s + "foo";',
  7833. '']));
  7834. end;
  7835. procedure TTestModule.TestBaseType_AnsiStringFail;
  7836. begin
  7837. StartProgram(false);
  7838. Add('var s: AnsiString');
  7839. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7840. ConvertProgram;
  7841. end;
  7842. procedure TTestModule.TestBaseType_WideStringFail;
  7843. begin
  7844. StartProgram(false);
  7845. Add('var s: WideString');
  7846. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7847. ConvertProgram;
  7848. end;
  7849. procedure TTestModule.TestBaseType_ShortStringFail;
  7850. begin
  7851. StartProgram(false);
  7852. Add('var s: ShortString');
  7853. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7854. ConvertProgram;
  7855. end;
  7856. procedure TTestModule.TestBaseType_RawByteStringFail;
  7857. begin
  7858. StartProgram(false);
  7859. Add('var s: RawByteString');
  7860. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7861. ConvertProgram;
  7862. end;
  7863. procedure TTestModule.TestTypeShortstring_Fail;
  7864. begin
  7865. StartProgram(false);
  7866. Add('type t = string[12];');
  7867. Add('var s: t;');
  7868. Add('begin');
  7869. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7870. ConvertProgram;
  7871. end;
  7872. procedure TTestModule.TestCharSet_Custom;
  7873. begin
  7874. StartProgram(false);
  7875. Add([
  7876. 'type',
  7877. ' TCharRg = ''a''..''z'';',
  7878. ' TSetOfCharRg = set of TCharRg;',
  7879. ' TCharRg2 = ''m''..''p'';',
  7880. 'const',
  7881. ' crg: TCharRg = ''b'';',
  7882. 'var',
  7883. ' c: char;',
  7884. ' crg2: TCharRg2;',
  7885. ' s: TSetOfCharRg;',
  7886. 'begin',
  7887. ' c:=crg;',
  7888. ' crg:=c;',
  7889. ' crg2:=crg;',
  7890. ' if c=crg then ;',
  7891. ' if crg=c then ;',
  7892. ' if crg=crg2 then ;',
  7893. ' if c in s then ;',
  7894. ' if crg2 in s then ;',
  7895. ' c:=default(TCharRg);',
  7896. '']);
  7897. ConvertProgram;
  7898. CheckSource('TestCharSet_Custom',
  7899. LinesToStr([ // statements
  7900. 'this.crg = "b";',
  7901. 'this.c = "";',
  7902. 'this.crg2 = "m";',
  7903. 'this.s = {};',
  7904. '']),
  7905. LinesToStr([ // this.$main
  7906. '$mod.c = $mod.crg;',
  7907. '$mod.crg = $mod.c;',
  7908. '$mod.crg2 = $mod.crg;',
  7909. 'if ($mod.c === $mod.crg) ;',
  7910. 'if ($mod.crg === $mod.c) ;',
  7911. 'if ($mod.crg === $mod.crg2) ;',
  7912. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7913. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7914. '$mod.c = "a";',
  7915. '']));
  7916. end;
  7917. procedure TTestModule.TestWideChar;
  7918. begin
  7919. StartProgram(false);
  7920. Add([
  7921. 'procedure Fly(var c: char);',
  7922. 'begin',
  7923. 'end;',
  7924. 'procedure Run(var c: widechar);',
  7925. 'begin',
  7926. 'end;',
  7927. 'var',
  7928. ' c: char;',
  7929. ' wc: widechar;',
  7930. ' w: word;',
  7931. 'begin',
  7932. ' Fly(wc);',
  7933. ' Run(c);',
  7934. ' wc:=WideChar(w);',
  7935. ' w:=ord(wc);',
  7936. '']);
  7937. ConvertProgram;
  7938. CheckSource('TestWideChar_VarArg',
  7939. LinesToStr([ // statements
  7940. 'this.Fly = function (c) {',
  7941. '};',
  7942. 'this.Run = function (c) {',
  7943. '};',
  7944. 'this.c = "";',
  7945. 'this.wc = "";',
  7946. 'this.w = 0;',
  7947. '']),
  7948. LinesToStr([ // this.$main
  7949. '$mod.Fly({',
  7950. ' p: $mod,',
  7951. ' get: function () {',
  7952. ' return this.p.wc;',
  7953. ' },',
  7954. ' set: function (v) {',
  7955. ' this.p.wc = v;',
  7956. ' }',
  7957. '});',
  7958. '$mod.Run({',
  7959. ' p: $mod,',
  7960. ' get: function () {',
  7961. ' return this.p.c;',
  7962. ' },',
  7963. ' set: function (v) {',
  7964. ' this.p.c = v;',
  7965. ' }',
  7966. '});',
  7967. '$mod.wc = String.fromCharCode($mod.w);',
  7968. '$mod.w = $mod.wc.charCodeAt();',
  7969. '',
  7970. '']));
  7971. end;
  7972. procedure TTestModule.TestForCharDo;
  7973. begin
  7974. StartProgram(false);
  7975. Add([
  7976. 'var c: char;',
  7977. 'begin',
  7978. ' for c:=''a'' to ''c'' do ;',
  7979. ' for c:=c downto ''a'' do ;',
  7980. ' for c:=''Б'' to ''Я'' do ;',
  7981. '']);
  7982. ConvertProgram;
  7983. CheckSource('TestForCharDo',
  7984. LinesToStr([ // statements
  7985. 'this.c = "";']),
  7986. LinesToStr([ // this.$main
  7987. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7988. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7989. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7990. '']));
  7991. end;
  7992. procedure TTestModule.TestForCharInDo;
  7993. begin
  7994. StartProgram(false);
  7995. Add([
  7996. 'type',
  7997. ' TSetOfChar = set of char;',
  7998. ' TCharRg = ''a''..''z'';',
  7999. ' TSetOfCharRg = set of TCharRg;',
  8000. 'const Foo = ''foo'';',
  8001. 'var',
  8002. ' c,c2: char;',
  8003. ' s: string;',
  8004. ' a1: array of char;',
  8005. ' a2: array[1..3] of char;',
  8006. ' soc: TSetOfChar;',
  8007. ' socr: TSetOfCharRg;',
  8008. ' cr: TCharRg;',
  8009. 'begin',
  8010. ' for c in foo do ;',
  8011. ' for c in s do ;',
  8012. ' for c in char do ;',
  8013. ' for c in a1 do ;',
  8014. ' for c in a2 do ;',
  8015. ' for c in [''1''..''3''] do ;',
  8016. ' for c in TSetOfChar do ;',
  8017. ' for c in TCharRg do ;',
  8018. ' for c in soc do c2:=c;',
  8019. ' for c in TSetOfCharRg do ;',
  8020. ' for c in socr do ;',
  8021. ' for cr in TCharRg do ;',
  8022. ' for cr in TSetOfCharRg do ;',
  8023. ' for cr in socr do ;',
  8024. '']);
  8025. ConvertProgram;
  8026. CheckSource('TestForCharInDo',
  8027. LinesToStr([ // statements
  8028. 'this.Foo = "foo";',
  8029. 'this.c = "";',
  8030. 'this.c2 = "";',
  8031. 'this.s = "";',
  8032. 'this.a1 = [];',
  8033. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8034. 'this.soc = {};',
  8035. 'this.socr = {};',
  8036. 'this.cr = "a";',
  8037. '']),
  8038. LinesToStr([ // this.$main
  8039. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8040. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8041. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8042. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8043. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8044. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8045. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8046. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8047. 'for (var $l8 in $mod.soc) {',
  8048. ' $mod.c = String.fromCharCode($l8);',
  8049. ' $mod.c2 = $mod.c;',
  8050. '};',
  8051. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8052. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8053. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8054. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8055. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8056. '']));
  8057. end;
  8058. procedure TTestModule.TestProcTwoArgs;
  8059. begin
  8060. StartProgram(false);
  8061. Add('procedure Test(a,b: longint);');
  8062. Add('begin');
  8063. Add('end;');
  8064. Add('begin');
  8065. ConvertProgram;
  8066. CheckSource('TestProcTwoArgs',
  8067. LinesToStr([ // statements
  8068. 'this.Test = function (a,b) {',
  8069. '};'
  8070. ]),
  8071. LinesToStr([ // this.$main
  8072. ''
  8073. ]));
  8074. end;
  8075. procedure TTestModule.TestProc_DefaultValue;
  8076. begin
  8077. StartProgram(false);
  8078. Add('procedure p1(i: longint = 1);');
  8079. Add('begin');
  8080. Add('end;');
  8081. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8082. Add('begin');
  8083. Add('end;');
  8084. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8085. Add('begin');
  8086. Add('end;');
  8087. Add('begin');
  8088. Add(' p1;');
  8089. Add(' p1();');
  8090. Add(' p1(11);');
  8091. Add(' p2;');
  8092. Add(' p2();');
  8093. Add(' p2(12);');
  8094. Add(' p2(13,''b'');');
  8095. Add(' p3();');
  8096. ConvertProgram;
  8097. CheckSource('TestProc_DefaultValue',
  8098. LinesToStr([ // statements
  8099. 'this.p1 = function (i) {',
  8100. '};',
  8101. 'this.p2 = function (i,c) {',
  8102. '};',
  8103. 'this.p3 = function (d,b,s) {',
  8104. '};'
  8105. ]),
  8106. LinesToStr([ // this.$main
  8107. ' $mod.p1(1);',
  8108. ' $mod.p1(1);',
  8109. ' $mod.p1(11);',
  8110. ' $mod.p2(1,"a");',
  8111. ' $mod.p2(1,"a");',
  8112. ' $mod.p2(12,"a");',
  8113. ' $mod.p2(13,"b");',
  8114. ' $mod.p3(1.0,false,"abc");'
  8115. ]));
  8116. end;
  8117. procedure TTestModule.TestFunctionInt;
  8118. begin
  8119. StartProgram(false);
  8120. Add('function MyTest(Bar: longint): longint;');
  8121. Add('begin');
  8122. Add(' Result:=2*bar');
  8123. Add('end;');
  8124. Add('begin');
  8125. ConvertProgram;
  8126. CheckSource('TestFunctionInt',
  8127. LinesToStr([ // statements
  8128. 'this.MyTest = function (Bar) {',
  8129. ' var Result = 0;',
  8130. ' Result = 2*Bar;',
  8131. ' return Result;',
  8132. '};'
  8133. ]),
  8134. LinesToStr([ // this.$main
  8135. ''
  8136. ]));
  8137. end;
  8138. procedure TTestModule.TestFunctionString;
  8139. begin
  8140. StartProgram(false);
  8141. Add('function Test(Bar: string): string;');
  8142. Add('begin');
  8143. Add(' Result:=bar+BAR');
  8144. Add('end;');
  8145. Add('begin');
  8146. ConvertProgram;
  8147. CheckSource('TestFunctionString',
  8148. LinesToStr([ // statements
  8149. 'this.Test = function (Bar) {',
  8150. ' var Result = "";',
  8151. ' Result = Bar+Bar;',
  8152. ' return Result;',
  8153. '};'
  8154. ]),
  8155. LinesToStr([ // this.$main
  8156. ''
  8157. ]));
  8158. end;
  8159. procedure TTestModule.TestIfThen;
  8160. begin
  8161. StartProgram(false);
  8162. Add([
  8163. 'var b: boolean;',
  8164. 'begin',
  8165. ' if b then ;',
  8166. ' if b then else ;']);
  8167. ConvertProgram;
  8168. CheckSource('TestIfThen',
  8169. LinesToStr([ // statements
  8170. 'this.b = false;',
  8171. '']),
  8172. LinesToStr([ // this.$main
  8173. 'if ($mod.b) ;',
  8174. 'if ($mod.b) ;',
  8175. '']));
  8176. end;
  8177. procedure TTestModule.TestForLoop;
  8178. begin
  8179. StartProgram(false);
  8180. Add('var');
  8181. Add(' vI, vJ, vN: longint;');
  8182. Add('begin');
  8183. Add(' VJ:=0;');
  8184. Add(' VN:=3;');
  8185. Add(' for VI:=1 to VN do');
  8186. Add(' begin');
  8187. Add(' VJ:=VJ+VI;');
  8188. Add(' end;');
  8189. ConvertProgram;
  8190. CheckSource('TestForLoop',
  8191. LinesToStr([ // statements
  8192. 'this.vI = 0;',
  8193. 'this.vJ = 0;',
  8194. 'this.vN = 0;'
  8195. ]),
  8196. LinesToStr([ // this.$main
  8197. ' $mod.vJ = 0;',
  8198. ' $mod.vN = 3;',
  8199. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8200. ' $mod.vI = $l;',
  8201. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8202. ' };',
  8203. '']));
  8204. end;
  8205. procedure TTestModule.TestForLoopInsideFunction;
  8206. begin
  8207. StartProgram(false);
  8208. Add('function SumNumbers(Count: longint): longint;');
  8209. Add('var');
  8210. Add(' vI, vJ: longint;');
  8211. Add('begin');
  8212. Add(' vj:=0;');
  8213. Add(' for vi:=1 to count do');
  8214. Add(' begin');
  8215. Add(' vj:=vj+vi;');
  8216. Add(' end;');
  8217. Add('end;');
  8218. Add('begin');
  8219. Add(' sumnumbers(3);');
  8220. ConvertProgram;
  8221. CheckSource('TestForLoopInsideFunction',
  8222. LinesToStr([ // statements
  8223. 'this.SumNumbers = function (Count) {',
  8224. ' var Result = 0;',
  8225. ' var vI = 0;',
  8226. ' var vJ = 0;',
  8227. ' vJ = 0;',
  8228. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8229. ' vI = $l;',
  8230. ' vJ = vJ + vI;',
  8231. ' };',
  8232. ' return Result;',
  8233. '};'
  8234. ]),
  8235. LinesToStr([ // $mod.$main
  8236. ' $mod.SumNumbers(3);'
  8237. ]));
  8238. end;
  8239. procedure TTestModule.TestForLoop_ReadVarAfter;
  8240. begin
  8241. StartProgram(false);
  8242. Add('var');
  8243. Add(' vI: longint;');
  8244. Add('begin');
  8245. Add(' for vi:=1 to 2 do ;');
  8246. Add(' if vi=3 then ;');
  8247. ConvertProgram;
  8248. CheckSource('TestForLoop',
  8249. LinesToStr([ // statements
  8250. 'this.vI = 0;'
  8251. ]),
  8252. LinesToStr([ // this.$main
  8253. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8254. ' if ($mod.vI===3) ;'
  8255. ]));
  8256. end;
  8257. procedure TTestModule.TestForLoop_Nested;
  8258. begin
  8259. StartProgram(false);
  8260. Add('function SumNumbers(Count: longint): longint;');
  8261. Add('var');
  8262. Add(' vI, vJ, vK: longint;');
  8263. Add('begin');
  8264. Add(' VK:=0;');
  8265. Add(' for VI:=1 to count do');
  8266. Add(' begin');
  8267. Add(' for vj:=1 to vi do');
  8268. Add(' begin');
  8269. Add(' vk:=VK+VI;');
  8270. Add(' end;');
  8271. Add(' end;');
  8272. Add('end;');
  8273. Add('begin');
  8274. Add(' sumnumbers(3);');
  8275. ConvertProgram;
  8276. CheckSource('TestForLoopInFunction',
  8277. LinesToStr([ // statements
  8278. 'this.SumNumbers = function (Count) {',
  8279. ' var Result = 0;',
  8280. ' var vI = 0;',
  8281. ' var vJ = 0;',
  8282. ' var vK = 0;',
  8283. ' vK = 0;',
  8284. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8285. ' vI = $l;',
  8286. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8287. ' vJ = $l1;',
  8288. ' vK = vK + vI;',
  8289. ' };',
  8290. ' };',
  8291. ' return Result;',
  8292. '};'
  8293. ]),
  8294. LinesToStr([ // $mod.$main
  8295. ' $mod.SumNumbers(3);'
  8296. ]));
  8297. end;
  8298. procedure TTestModule.TestRepeatUntil;
  8299. begin
  8300. StartProgram(false);
  8301. Add('var');
  8302. Add(' vI, vJ, vN: longint;');
  8303. Add('begin');
  8304. Add(' vn:=3;');
  8305. Add(' vj:=0;');
  8306. Add(' VI:=0;');
  8307. Add(' repeat');
  8308. Add(' VI:=vi+1;');
  8309. Add(' vj:=VJ+vI;');
  8310. Add(' until vi>=vn');
  8311. ConvertProgram;
  8312. CheckSource('TestRepeatUntil',
  8313. LinesToStr([ // statements
  8314. 'this.vI = 0;',
  8315. 'this.vJ = 0;',
  8316. 'this.vN = 0;'
  8317. ]),
  8318. LinesToStr([ // $mod.$main
  8319. ' $mod.vN = 3;',
  8320. ' $mod.vJ = 0;',
  8321. ' $mod.vI = 0;',
  8322. ' do{',
  8323. ' $mod.vI = $mod.vI + 1;',
  8324. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8325. ' }while(!($mod.vI>=$mod.vN));'
  8326. ]));
  8327. end;
  8328. procedure TTestModule.TestAsmBlock;
  8329. begin
  8330. StartProgram(false);
  8331. Add([
  8332. 'var',
  8333. ' vI: longint;',
  8334. 'begin',
  8335. ' vi:=1;',
  8336. ' asm',
  8337. ' if (vI===1) {',
  8338. ' vI=2;',
  8339. //' console.log(''end;'');', ToDo
  8340. ' }',
  8341. ' if (vI===2){ vI=3; }',
  8342. ' end;',
  8343. ' VI:=4;']);
  8344. ConvertProgram;
  8345. CheckSource('TestAsmBlock',
  8346. LinesToStr([ // statements
  8347. 'this.vI = 0;'
  8348. ]),
  8349. LinesToStr([ // $mod.$main
  8350. '$mod.vI = 1;',
  8351. 'if (vI===1) {',
  8352. ' vI=2;',
  8353. '}',
  8354. 'if (vI===2){ vI=3; }',
  8355. ';',
  8356. '$mod.vI = 4;'
  8357. ]));
  8358. end;
  8359. procedure TTestModule.TestAsmPas_Impl;
  8360. begin
  8361. StartUnit(false);
  8362. Add('interface');
  8363. Add('const cIntf: longint = 1;');
  8364. Add('var vIntf: longint;');
  8365. Add('implementation');
  8366. Add('const cImpl: longint = 2;');
  8367. Add('var vImpl: longint;');
  8368. Add('procedure DoIt;');
  8369. Add('const cLoc: longint = 3;');
  8370. Add('var vLoc: longint;');
  8371. Add('begin;');
  8372. Add(' asm');
  8373. //Add(' pas(vIntf)=pas(cIntf);');
  8374. //Add(' pas(vImpl)=pas(cImpl);');
  8375. //Add(' pas(vLoc)=pas(cLoc);');
  8376. Add(' end;');
  8377. Add('end;');
  8378. ConvertUnit;
  8379. CheckSource('TestAsmPas_Impl',
  8380. LinesToStr([
  8381. 'var $impl = $mod.$impl;',
  8382. 'this.cIntf = 1;',
  8383. 'this.vIntf = 0;',
  8384. '']),
  8385. '', // this.$init
  8386. LinesToStr([ // implementation
  8387. '$impl.cImpl = 2;',
  8388. '$impl.vImpl = 0;',
  8389. 'var cLoc = 3;',
  8390. '$impl.DoIt = function () {',
  8391. ' var vLoc = 0;',
  8392. '};',
  8393. '']) );
  8394. end;
  8395. procedure TTestModule.TestTryFinally;
  8396. begin
  8397. StartProgram(false);
  8398. Add('var i: longint;');
  8399. Add('begin');
  8400. Add(' try');
  8401. Add(' i:=0; i:=2 div i;');
  8402. Add(' finally');
  8403. Add(' i:=3');
  8404. Add(' end;');
  8405. ConvertProgram;
  8406. CheckSource('TestTryFinally',
  8407. LinesToStr([ // statements
  8408. 'this.i = 0;'
  8409. ]),
  8410. LinesToStr([ // $mod.$main
  8411. 'try {',
  8412. ' $mod.i = 0;',
  8413. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8414. '} finally {',
  8415. ' $mod.i = 3;',
  8416. '};'
  8417. ]));
  8418. end;
  8419. procedure TTestModule.TestTryExcept;
  8420. begin
  8421. StartProgram(false);
  8422. Add([
  8423. 'type',
  8424. ' TObject = class end;',
  8425. ' Exception = class Msg: string; end;',
  8426. ' EInvalidCast = class(Exception) end;',
  8427. 'var vI: longint;',
  8428. 'begin',
  8429. ' try',
  8430. ' vi:=1;',
  8431. ' except',
  8432. ' vi:=2',
  8433. ' end;',
  8434. ' try',
  8435. ' vi:=3;',
  8436. ' except',
  8437. ' raise;',
  8438. ' end;',
  8439. ' try',
  8440. ' VI:=4;',
  8441. ' except',
  8442. ' on einvalidcast do',
  8443. ' raise;',
  8444. ' on E: exception do',
  8445. ' if e.msg='''' then',
  8446. ' raise e;',
  8447. ' else',
  8448. ' vi:=5',
  8449. ' end;',
  8450. ' try',
  8451. ' VI:=6;',
  8452. ' except',
  8453. ' on einvalidcast do ;',
  8454. ' end;',
  8455. '']);
  8456. ConvertProgram;
  8457. CheckSource('TestTryExcept',
  8458. LinesToStr([ // statements
  8459. 'rtl.createClass(this, "TObject", null, function () {',
  8460. ' this.$init = function () {',
  8461. ' };',
  8462. ' this.$final = function () {',
  8463. ' };',
  8464. '});',
  8465. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8466. ' this.$init = function () {',
  8467. ' $mod.TObject.$init.call(this);',
  8468. ' this.Msg = "";',
  8469. ' };',
  8470. '});',
  8471. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8472. '});',
  8473. 'this.vI = 0;'
  8474. ]),
  8475. LinesToStr([ // $mod.$main
  8476. 'try {',
  8477. ' $mod.vI = 1;',
  8478. '} catch ($e) {',
  8479. ' $mod.vI = 2;',
  8480. '};',
  8481. 'try {',
  8482. ' $mod.vI = 3;',
  8483. '} catch ($e) {',
  8484. ' throw $e;',
  8485. '};',
  8486. 'try {',
  8487. ' $mod.vI = 4;',
  8488. '} catch ($e) {',
  8489. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8490. ' throw $e',
  8491. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8492. ' var E = $e;',
  8493. ' if (E.Msg === "") throw E;',
  8494. ' } else {',
  8495. ' $mod.vI = 5;',
  8496. ' }',
  8497. '};',
  8498. 'try {',
  8499. ' $mod.vI = 6;',
  8500. '} catch ($e) {',
  8501. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8502. ' } else throw $e',
  8503. '};',
  8504. '']));
  8505. end;
  8506. procedure TTestModule.TestTryExcept_ReservedWords;
  8507. begin
  8508. StartProgram(false);
  8509. Add([
  8510. 'type',
  8511. ' TObject = class end;',
  8512. ' Exception = class',
  8513. ' Symbol: string;',
  8514. ' end;',
  8515. 'var &try: longint;',
  8516. 'begin',
  8517. ' try',
  8518. ' &try:=4;',
  8519. ' except',
  8520. ' on Error: exception do',
  8521. ' if errOR.symBol='''' then',
  8522. ' raise ERRor;',
  8523. ' end;',
  8524. '']);
  8525. ConvertProgram;
  8526. CheckSource('TestTryExcept_ReservedWords',
  8527. LinesToStr([ // statements
  8528. 'rtl.createClass(this, "TObject", null, function () {',
  8529. ' this.$init = function () {',
  8530. ' };',
  8531. ' this.$final = function () {',
  8532. ' };',
  8533. '});',
  8534. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8535. ' this.$init = function () {',
  8536. ' $mod.TObject.$init.call(this);',
  8537. ' this.Symbol = "";',
  8538. ' };',
  8539. '});',
  8540. 'this.Try = 0;',
  8541. '']),
  8542. LinesToStr([ // $mod.$main
  8543. 'try {',
  8544. ' $mod.Try = 4;',
  8545. '} catch ($e) {',
  8546. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8547. ' var error = $e;',
  8548. ' if (error.Symbol === "") throw error;',
  8549. ' } else throw $e',
  8550. '};',
  8551. '']));
  8552. end;
  8553. procedure TTestModule.TestIfThenRaiseElse;
  8554. begin
  8555. StartProgram(false);
  8556. Add([
  8557. 'type',
  8558. ' TObject = class',
  8559. ' constructor Create;',
  8560. ' end;',
  8561. 'constructor TObject.Create;',
  8562. 'begin',
  8563. 'end;',
  8564. 'var b: boolean;',
  8565. 'begin',
  8566. ' if b then',
  8567. ' raise TObject.Create',
  8568. ' else',
  8569. ' b:=false;',
  8570. '']);
  8571. ConvertProgram;
  8572. CheckSource('TestIfThenRaiseElse',
  8573. LinesToStr([ // statements
  8574. 'rtl.createClass(this, "TObject", null, function () {',
  8575. ' this.$init = function () {',
  8576. ' };',
  8577. ' this.$final = function () {',
  8578. ' };',
  8579. ' this.Create = function () {',
  8580. ' return this;',
  8581. ' };',
  8582. '});',
  8583. 'this.b = false;',
  8584. '']),
  8585. LinesToStr([ // $mod.$main
  8586. 'if ($mod.b) {',
  8587. ' throw $mod.TObject.$create("Create")}',
  8588. ' else $mod.b = false;',
  8589. '']));
  8590. end;
  8591. procedure TTestModule.TestCaseOf;
  8592. begin
  8593. StartProgram(false);
  8594. Add([
  8595. 'const e: longint; external name ''$e'';',
  8596. 'var vI: longint;',
  8597. 'begin',
  8598. ' case vi of',
  8599. ' 1: ;',
  8600. ' 2: vi:=3;',
  8601. ' e: ;',
  8602. ' else',
  8603. ' VI:=4',
  8604. ' end;']);
  8605. ConvertProgram;
  8606. CheckSource('TestCaseOf',
  8607. LinesToStr([ // statements
  8608. 'this.vI = 0;'
  8609. ]),
  8610. LinesToStr([ // $mod.$main
  8611. 'var $tmp = $mod.vI;',
  8612. 'if ($tmp === 1) {}',
  8613. 'else if ($tmp === 2) {',
  8614. ' $mod.vI = 3}',
  8615. ' else if ($tmp === $e) {}',
  8616. 'else {',
  8617. ' $mod.vI = 4;',
  8618. '};'
  8619. ]));
  8620. end;
  8621. procedure TTestModule.TestCaseOf_UseSwitch;
  8622. begin
  8623. StartProgram(false);
  8624. Converter.UseSwitchStatement:=true;
  8625. Add('var Vi: longint;');
  8626. Add('begin');
  8627. Add(' case vi of');
  8628. Add(' 1: ;');
  8629. Add(' 2: VI:=3;');
  8630. Add(' else');
  8631. Add(' vi:=4');
  8632. Add(' end;');
  8633. ConvertProgram;
  8634. CheckSource('TestCaseOf_UseSwitch',
  8635. LinesToStr([ // statements
  8636. 'this.Vi = 0;'
  8637. ]),
  8638. LinesToStr([ // $mod.$main
  8639. 'switch ($mod.Vi) {',
  8640. 'case 1:',
  8641. ' break;',
  8642. 'case 2:',
  8643. ' $mod.Vi = 3;',
  8644. ' break;',
  8645. 'default:',
  8646. ' $mod.Vi = 4;',
  8647. '};'
  8648. ]));
  8649. end;
  8650. procedure TTestModule.TestCaseOfNoElse;
  8651. begin
  8652. StartProgram(false);
  8653. Add('var Vi: longint;');
  8654. Add('begin');
  8655. Add(' case vi of');
  8656. Add(' 1: begin vi:=2; VI:=3; end;');
  8657. Add(' end;');
  8658. ConvertProgram;
  8659. CheckSource('TestCaseOfNoElse',
  8660. LinesToStr([ // statements
  8661. 'this.Vi = 0;'
  8662. ]),
  8663. LinesToStr([ // $mod.$main
  8664. 'var $tmp = $mod.Vi;',
  8665. 'if ($tmp === 1) {',
  8666. ' $mod.Vi = 2;',
  8667. ' $mod.Vi = 3;',
  8668. '};'
  8669. ]));
  8670. end;
  8671. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8672. begin
  8673. StartProgram(false);
  8674. Converter.UseSwitchStatement:=true;
  8675. Add('var vI: longint;');
  8676. Add('begin');
  8677. Add(' case vi of');
  8678. Add(' 1: begin VI:=2; vi:=3; end;');
  8679. Add(' end;');
  8680. ConvertProgram;
  8681. CheckSource('TestCaseOfNoElse_UseSwitch',
  8682. LinesToStr([ // statements
  8683. 'this.vI = 0;'
  8684. ]),
  8685. LinesToStr([ // $mod.$main
  8686. 'switch ($mod.vI) {',
  8687. 'case 1:',
  8688. ' $mod.vI = 2;',
  8689. ' $mod.vI = 3;',
  8690. ' break;',
  8691. '};'
  8692. ]));
  8693. end;
  8694. procedure TTestModule.TestCaseOfRange;
  8695. begin
  8696. StartProgram(false);
  8697. Add('var vI: longint;');
  8698. Add('begin');
  8699. Add(' case vi of');
  8700. Add(' 1..3: vi:=14;');
  8701. Add(' 4,5: vi:=16;');
  8702. Add(' 6..7,9..10: ;');
  8703. Add(' else ;');
  8704. Add(' end;');
  8705. ConvertProgram;
  8706. CheckSource('TestCaseOfRange',
  8707. LinesToStr([ // statements
  8708. 'this.vI = 0;'
  8709. ]),
  8710. LinesToStr([ // $mod.$main
  8711. 'var $tmp = $mod.vI;',
  8712. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8713. ' $mod.vI = 14',
  8714. '} else if (($tmp === 4) || ($tmp === 5)){',
  8715. ' $mod.vI = 16',
  8716. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8717. ]));
  8718. end;
  8719. procedure TTestModule.TestCaseOfString;
  8720. begin
  8721. StartProgram(false);
  8722. Add([
  8723. 'var s,h: string;',
  8724. 'begin',
  8725. ' case s of',
  8726. ' ''foo'': s:=h;',
  8727. ' ''a''..''z'': h:=s;',
  8728. ' ''ў'', ''ё'': ;',
  8729. ' ''Б''..''Я'': ;',
  8730. ' end;',
  8731. '']);
  8732. ConvertProgram;
  8733. CheckSource('TestCaseOfString',
  8734. LinesToStr([ // statements
  8735. 'this.s = "";',
  8736. 'this.h = "";',
  8737. '']),
  8738. LinesToStr([ // $mod.$main
  8739. 'var $tmp = $mod.s;',
  8740. 'if ($tmp === "foo") {',
  8741. ' $mod.s = $mod.h}',
  8742. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8743. ' $mod.h = $mod.s}',
  8744. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8745. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8746. '']));
  8747. end;
  8748. procedure TTestModule.TestCaseOfChar;
  8749. begin
  8750. StartProgram(false);
  8751. Add([
  8752. 'var s,h: char;',
  8753. 'begin',
  8754. ' case s of',
  8755. ' ''a''..''z'': h:=s;',
  8756. ' ''ä'': ;',
  8757. ' ''ў'', ''ё'': ;',
  8758. ' ''Б''..''Я'': ;',
  8759. ' end;',
  8760. '']);
  8761. ConvertProgram;
  8762. CheckSource('TestCaseOfString',
  8763. LinesToStr([ // statements
  8764. 'this.s = "";',
  8765. 'this.h = "";',
  8766. '']),
  8767. LinesToStr([ // $mod.$main
  8768. 'var $tmp = $mod.s;',
  8769. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8770. ' $mod.h = $mod.s}',
  8771. ' else if ($tmp === "ä") {}',
  8772. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8773. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8774. '']));
  8775. end;
  8776. procedure TTestModule.TestCaseOfExternalClassConst;
  8777. begin
  8778. StartProgram(false);
  8779. Add([
  8780. '{$modeswitch externalclass}',
  8781. 'type',
  8782. ' TBird = class external name ''Bird''',
  8783. ' const e: longint;',
  8784. ' end;',
  8785. 'var vI: longint;',
  8786. 'begin',
  8787. ' case vi of',
  8788. ' 1: vi:=3;',
  8789. ' TBird.e: ;',
  8790. ' end;']);
  8791. ConvertProgram;
  8792. CheckSource('TestCaseOfExternalClassConst',
  8793. LinesToStr([ // statements
  8794. 'this.vI = 0;'
  8795. ]),
  8796. LinesToStr([ // $mod.$main
  8797. 'var $tmp = $mod.vI;',
  8798. 'if ($tmp === 1) {',
  8799. ' $mod.vI = 3}',
  8800. ' else if ($tmp === Bird.e) ;'
  8801. ]));
  8802. end;
  8803. procedure TTestModule.TestDebugger;
  8804. begin
  8805. StartProgram(false);
  8806. Add([
  8807. 'procedure DoIt;',
  8808. 'begin',
  8809. ' deBugger;',
  8810. ' DeBugger();',
  8811. 'end;',
  8812. 'begin',
  8813. ' Debugger;']);
  8814. ConvertProgram;
  8815. CheckSource('TestDebugger',
  8816. LinesToStr([ // statements
  8817. 'this.DoIt = function () {',
  8818. ' debugger;',
  8819. ' debugger;',
  8820. '};',
  8821. '']),
  8822. LinesToStr([ // $mod.$main
  8823. 'debugger;',
  8824. '']));
  8825. end;
  8826. procedure TTestModule.TestArray_Dynamic;
  8827. begin
  8828. StartProgram(false);
  8829. Add([
  8830. 'type',
  8831. ' TArrayInt = array of longint;',
  8832. 'var',
  8833. ' Arr: TArrayInt;',
  8834. ' i: longint;',
  8835. ' b: boolean;',
  8836. 'begin',
  8837. ' SetLength(arr,3);',
  8838. ' arr[0]:=4;',
  8839. ' arr[1]:=length(arr)+arr[0];',
  8840. ' arr[i]:=5;',
  8841. ' arr[arr[i]]:=arr[6];',
  8842. ' i:=low(arr);',
  8843. ' i:=high(arr);',
  8844. ' b:=Assigned(arr);',
  8845. ' Arr:=default(TArrayInt);']);
  8846. ConvertProgram;
  8847. CheckSource('TestArray_Dynamic',
  8848. LinesToStr([ // statements
  8849. 'this.Arr = [];',
  8850. 'this.i = 0;',
  8851. 'this.b = false;'
  8852. ]),
  8853. LinesToStr([ // $mod.$main
  8854. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8855. '$mod.Arr[0] = 4;',
  8856. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8857. '$mod.Arr[$mod.i] = 5;',
  8858. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8859. '$mod.i = 0;',
  8860. '$mod.i = rtl.length($mod.Arr) - 1;',
  8861. '$mod.b = rtl.length($mod.Arr) > 0;',
  8862. '$mod.Arr = [];',
  8863. '']));
  8864. end;
  8865. procedure TTestModule.TestArray_Dynamic_Nil;
  8866. begin
  8867. StartProgram(false);
  8868. Add('type');
  8869. Add(' TArrayInt = array of longint;');
  8870. Add('var');
  8871. Add(' Arr: TArrayInt;');
  8872. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8873. Add('begin');
  8874. Add(' arr:=nil;');
  8875. Add(' if arr=nil then;');
  8876. Add(' if nil=arr then;');
  8877. Add(' if arr<>nil then;');
  8878. Add(' if nil<>arr then;');
  8879. Add(' DoIt(nil,nil);');
  8880. ConvertProgram;
  8881. CheckSource('TestArray_Dynamic',
  8882. LinesToStr([ // statements
  8883. 'this.Arr = [];',
  8884. 'this.DoIt = function(i,j){',
  8885. '};'
  8886. ]),
  8887. LinesToStr([ // $mod.$main
  8888. '$mod.Arr = [];',
  8889. 'if (rtl.length($mod.Arr) === 0) ;',
  8890. 'if (rtl.length($mod.Arr) === 0) ;',
  8891. 'if (rtl.length($mod.Arr) > 0) ;',
  8892. 'if (rtl.length($mod.Arr) > 0) ;',
  8893. '$mod.DoIt([],[]);',
  8894. '']));
  8895. end;
  8896. procedure TTestModule.TestArray_DynMultiDimensional;
  8897. begin
  8898. StartProgram(false);
  8899. Add([
  8900. 'type',
  8901. ' TArrayInt = array of longint;',
  8902. ' TArrayArrayInt = array of TArrayInt;',
  8903. 'var',
  8904. ' Arr: TArrayInt;',
  8905. ' Arr2: TArrayArrayInt;',
  8906. ' i: longint;',
  8907. 'begin',
  8908. ' arr2:=nil;',
  8909. ' if arr2=nil then;',
  8910. ' if nil=arr2 then;',
  8911. ' i:=low(arr2);',
  8912. ' i:=low(arr2[1]);',
  8913. ' i:=high(arr2);',
  8914. ' i:=high(arr2[2]);',
  8915. ' arr2[3]:=arr;',
  8916. ' arr2[4][5]:=i;',
  8917. ' i:=arr2[6][7];',
  8918. ' arr2[8,9]:=i;',
  8919. ' i:=arr2[10,11];',
  8920. ' SetLength(arr2,14);',
  8921. ' SetLength(arr2[15],16);']);
  8922. ConvertProgram;
  8923. CheckSource('TestArray_Dynamic',
  8924. LinesToStr([ // statements
  8925. 'this.Arr = [];',
  8926. 'this.Arr2 = [];',
  8927. 'this.i = 0;'
  8928. ]),
  8929. LinesToStr([ // $mod.$main
  8930. '$mod.Arr2 = [];',
  8931. 'if (rtl.length($mod.Arr2) === 0) ;',
  8932. 'if (rtl.length($mod.Arr2) === 0) ;',
  8933. '$mod.i = 0;',
  8934. '$mod.i = 0;',
  8935. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8936. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8937. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8938. '$mod.Arr2[4][5] = $mod.i;',
  8939. '$mod.i = $mod.Arr2[6][7];',
  8940. '$mod.Arr2[8][9] = $mod.i;',
  8941. '$mod.i = $mod.Arr2[10][11];',
  8942. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8943. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8944. '']));
  8945. end;
  8946. procedure TTestModule.TestArray_DynamicAssign;
  8947. begin
  8948. StartProgram(false);
  8949. Add([
  8950. 'type',
  8951. ' TArrayInt = array of longint;',
  8952. ' TArrayArrayInt = array of TArrayInt;',
  8953. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8954. 'begin',
  8955. 'end;',
  8956. 'procedure Fly(var a: TArrayInt);',
  8957. 'begin',
  8958. 'end;',
  8959. 'var',
  8960. ' Arr: TArrayInt;',
  8961. ' Arr2: TArrayArrayInt;',
  8962. 'begin',
  8963. ' arr:=nil;',
  8964. ' arr2:=nil;',
  8965. ' arr2[1]:=nil;',
  8966. ' arr2[2]:=arr;',
  8967. ' Run(arr,arr,arr);',
  8968. ' Fly(arr);',
  8969. ' Run(arr2[4],arr2[5],arr2[6]);',
  8970. ' Fly(arr2[7]);',
  8971. '']);
  8972. ConvertProgram;
  8973. CheckSource('TestArray_DynamicAssign',
  8974. LinesToStr([ // statements
  8975. 'this.Run = function (a, b, c) {',
  8976. '};',
  8977. 'this.Fly = function (a) {',
  8978. '};',
  8979. 'this.Arr = [];',
  8980. 'this.Arr2 = [];',
  8981. '']),
  8982. LinesToStr([ // $mod.$main
  8983. '$mod.Arr = [];',
  8984. '$mod.Arr2 = [];',
  8985. '$mod.Arr2[1] = [];',
  8986. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8987. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8988. '$mod.Fly({',
  8989. ' p: $mod,',
  8990. ' get: function () {',
  8991. ' return this.p.Arr;',
  8992. ' },',
  8993. ' set: function (v) {',
  8994. ' this.p.Arr = v;',
  8995. ' }',
  8996. '});',
  8997. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8998. '$mod.Fly({',
  8999. ' a: 7,',
  9000. ' p: $mod.Arr2,',
  9001. ' get: function () {',
  9002. ' return this.p[this.a];',
  9003. ' },',
  9004. ' set: function (v) {',
  9005. ' this.p[this.a] = v;',
  9006. ' }',
  9007. '});',
  9008. '']));
  9009. end;
  9010. procedure TTestModule.TestArray_StaticInt;
  9011. begin
  9012. StartProgram(false);
  9013. Add('type');
  9014. Add(' TArrayInt = array[2..4] of longint;');
  9015. Add('var');
  9016. Add(' Arr: TArrayInt;');
  9017. Add(' Arr2: TArrayInt = (5,6,7);');
  9018. Add(' i: longint;');
  9019. Add(' b: boolean;');
  9020. Add('begin');
  9021. Add(' arr[2]:=4;');
  9022. Add(' arr[3]:=arr[2]+arr[3];');
  9023. Add(' arr[i]:=5;');
  9024. Add(' arr[arr[i]]:=arr[high(arr)];');
  9025. Add(' i:=low(arr);');
  9026. Add(' i:=high(arr);');
  9027. Add(' b:=arr[2]=arr[3];');
  9028. Add(' arr:=default(TArrayInt);');
  9029. ConvertProgram;
  9030. CheckSource('TestArray_StaticInt',
  9031. LinesToStr([ // statements
  9032. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9033. 'this.Arr2 = [5, 6, 7];',
  9034. 'this.i = 0;',
  9035. 'this.b = false;'
  9036. ]),
  9037. LinesToStr([ // $mod.$main
  9038. '$mod.Arr[0] = 4;',
  9039. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9040. '$mod.Arr[$mod.i-2] = 5;',
  9041. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9042. '$mod.i = 2;',
  9043. '$mod.i = 4;',
  9044. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9045. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9046. '']));
  9047. end;
  9048. procedure TTestModule.TestArray_StaticBool;
  9049. begin
  9050. StartProgram(false);
  9051. Add('type');
  9052. Add(' TBools = array[boolean] of boolean;');
  9053. Add(' TBool2 = array[true..true] of boolean;');
  9054. Add('var');
  9055. Add(' Arr: TBools;');
  9056. Add(' Arr2: TBool2;');
  9057. Add(' Arr3: TBools = (true,false);');
  9058. Add(' b: boolean;');
  9059. Add('begin');
  9060. Add(' b:=low(arr);');
  9061. Add(' b:=high(arr);');
  9062. Add(' arr[true]:=false;');
  9063. Add(' arr[false]:=arr[b] or arr[true];');
  9064. Add(' arr[b]:=true;');
  9065. Add(' arr[arr[b]]:=arr[high(arr)];');
  9066. Add(' b:=arr[false]=arr[true];');
  9067. Add(' b:=low(arr2);');
  9068. Add(' b:=high(arr2);');
  9069. Add(' arr2[true]:=true;');
  9070. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9071. Add(' arr2[b]:=false;');
  9072. ConvertProgram;
  9073. CheckSource('TestArray_StaticBool',
  9074. LinesToStr([ // statements
  9075. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9076. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9077. 'this.Arr3 = [true, false];',
  9078. 'this.b = false;'
  9079. ]),
  9080. LinesToStr([ // $mod.$main
  9081. '$mod.b = false;',
  9082. '$mod.b = true;',
  9083. '$mod.Arr[1] = false;',
  9084. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9085. '$mod.Arr[+$mod.b] = true;',
  9086. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9087. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9088. '$mod.b = true;',
  9089. '$mod.b = true;',
  9090. '$mod.Arr2[0] = true;',
  9091. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9092. '$mod.Arr2[1-$mod.b] = false;',
  9093. '']));
  9094. end;
  9095. procedure TTestModule.TestArray_StaticChar;
  9096. begin
  9097. StartProgram(false);
  9098. Add([
  9099. 'type',
  9100. ' TChars = array[char] of char;',
  9101. ' TChars2 = array[''a''..''z''] of char;',
  9102. 'var',
  9103. ' Arr: TChars;',
  9104. ' Arr2: TChars2;',
  9105. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9106. ' Arr4: array[11..13] of char = ''pas'';',
  9107. ' Arr5: array[21..22] of char = ''äö'';',
  9108. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9109. ' c: char;',
  9110. ' b: boolean;',
  9111. 'begin',
  9112. ' c:=low(arr);',
  9113. ' c:=high(arr);',
  9114. ' arr[''B'']:=''a'';',
  9115. ' arr[''D'']:=arr[c];',
  9116. ' arr[c]:=arr[''d''];',
  9117. ' arr[arr[c]]:=arr[high(arr)];',
  9118. ' b:=arr[low(arr)]=arr[''e''];',
  9119. ' c:=low(arr2);',
  9120. ' c:=high(arr2);',
  9121. ' arr2[''b'']:=''f'';',
  9122. ' arr2[''a'']:=arr2[c];',
  9123. ' arr2[c]:=arr2[''g''];']);
  9124. ConvertProgram;
  9125. CheckSource('TestArray_StaticChar',
  9126. LinesToStr([ // statements
  9127. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9128. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9129. 'this.Arr3 = ["p", "a", "s"];',
  9130. 'this.Arr4 = ["p", "a", "s"];',
  9131. 'this.Arr5 = ["ä", "ö"];',
  9132. 'this.Arr6 = ["ä", "ö"];',
  9133. 'this.c = "";',
  9134. 'this.b = false;',
  9135. '']),
  9136. LinesToStr([ // $mod.$main
  9137. '$mod.c = "\x00";',
  9138. '$mod.c = "\uFFFF";',
  9139. '$mod.Arr[66] = "a";',
  9140. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9141. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9142. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9143. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9144. '$mod.c = "a";',
  9145. '$mod.c = "z";',
  9146. '$mod.Arr2[1] = "f";',
  9147. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9148. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9149. '']));
  9150. end;
  9151. procedure TTestModule.TestArray_StaticMultiDim;
  9152. begin
  9153. StartProgram(false);
  9154. Add([
  9155. 'type',
  9156. ' TArrayInt = array[1..3] of longint;',
  9157. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9158. 'var',
  9159. ' Arr: TArrayInt;',
  9160. ' Arr2: TArrayArrayInt;',
  9161. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9162. ' i: longint;',
  9163. 'begin',
  9164. ' i:=low(arr);',
  9165. ' i:=low(arr2);',
  9166. ' i:=low(arr2[5]);',
  9167. ' i:=high(arr);',
  9168. ' i:=high(arr2);',
  9169. ' i:=high(arr2[6]);',
  9170. ' arr2[5]:=arr;',
  9171. ' arr2[6][2]:=i;',
  9172. ' i:=arr2[6][3];',
  9173. ' arr2[6,3]:=i;',
  9174. ' i:=arr2[5,2];',
  9175. ' arr2:=arr2;',// clone multi dim static array
  9176. ' arr3:=arr3;',// clone anonymous multi dim static array
  9177. '']);
  9178. ConvertProgram;
  9179. CheckSource('TestArray_StaticMultiDim',
  9180. LinesToStr([ // statements
  9181. 'this.TArrayArrayInt$clone = function (a) {',
  9182. ' var r = [];',
  9183. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9184. ' return r;',
  9185. '};',
  9186. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9187. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9188. 'this.Arr3$a$clone = function (a) {',
  9189. ' var r = [];',
  9190. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9191. ' return r;',
  9192. '};',
  9193. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9194. 'this.i = 0;'
  9195. ]),
  9196. LinesToStr([ // $mod.$main
  9197. '$mod.i = 1;',
  9198. '$mod.i = 5;',
  9199. '$mod.i = 1;',
  9200. '$mod.i = 3;',
  9201. '$mod.i = 6;',
  9202. '$mod.i = 3;',
  9203. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9204. '$mod.Arr2[1][1] = $mod.i;',
  9205. '$mod.i = $mod.Arr2[1][2];',
  9206. '$mod.Arr2[1][2] = $mod.i;',
  9207. '$mod.i = $mod.Arr2[0][1];',
  9208. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9209. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9210. '']));
  9211. end;
  9212. procedure TTestModule.TestArray_StaticInFunction;
  9213. begin
  9214. StartProgram(false);
  9215. Add([
  9216. 'const TArrayInt = 3;',
  9217. 'const TArrayArrayInt = 4;',
  9218. 'procedure DoIt;',
  9219. 'type',
  9220. ' TArrayInt = array[1..3] of longint;',
  9221. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9222. 'var',
  9223. ' Arr: TArrayInt;',
  9224. ' Arr2: TArrayArrayInt;',
  9225. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9226. ' i: longint;',
  9227. 'begin',
  9228. ' arr2[5]:=arr;',
  9229. ' arr2:=arr2;',// clone multi dim static array
  9230. ' arr3:=arr3;',// clone multi dim anonymous static array
  9231. 'end;',
  9232. 'begin',
  9233. '']);
  9234. ConvertProgram;
  9235. CheckSource('TestArray_StaticInFunction',
  9236. LinesToStr([ // statements
  9237. 'this.TArrayInt = 3;',
  9238. 'this.TArrayArrayInt = 4;',
  9239. 'var TArrayArrayInt$1$clone = function (a) {',
  9240. ' var r = [];',
  9241. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9242. ' return r;',
  9243. '};',
  9244. 'var Arr3$a$clone = function (a) {',
  9245. ' var r = [];',
  9246. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9247. ' return r;',
  9248. '};',
  9249. 'this.DoIt = function () {',
  9250. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9251. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9252. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9253. ' var i = 0;',
  9254. ' Arr2[0] = Arr.slice(0);',
  9255. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9256. ' Arr3 = Arr3$a$clone(Arr3);',
  9257. '};',
  9258. '']),
  9259. LinesToStr([ // $mod.$main
  9260. '']));
  9261. end;
  9262. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9263. begin
  9264. StartProgram(false);
  9265. Add([
  9266. 'type',
  9267. ' TArrayInt = array[1..3,1..2] of longint;',
  9268. 'var',
  9269. ' a,b: TArrayInt;',
  9270. 'begin',
  9271. ' if a=b then ;',
  9272. '']);
  9273. SetExpectedPasResolverError('compare static array is not supported',
  9274. nXIsNotSupported);
  9275. ConvertProgram;
  9276. end;
  9277. procedure TTestModule.TestArrayOfRecord;
  9278. begin
  9279. StartProgram(false);
  9280. Add([
  9281. 'type',
  9282. ' TRec = record',
  9283. ' Int: longint;',
  9284. ' end;',
  9285. ' TArrayRec = array of TRec;',
  9286. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9287. 'begin',
  9288. 'end;',
  9289. 'var',
  9290. ' Arr: TArrayRec;',
  9291. ' r: TRec;',
  9292. ' i: longint;',
  9293. 'begin',
  9294. ' SetLength(arr,3);',
  9295. ' arr[0].int:=4;',
  9296. ' arr[1].int:=length(arr)+arr[2].int;',
  9297. ' arr[arr[i].int].int:=arr[5].int;',
  9298. ' arr[7]:=r;',
  9299. ' r:=arr[8];',
  9300. ' i:=low(arr);',
  9301. ' i:=high(arr);',
  9302. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9303. ConvertProgram;
  9304. CheckSource('TestArrayOfRecord',
  9305. LinesToStr([ // statements
  9306. 'rtl.recNewT(this, "TRec", function () {',
  9307. ' this.Int = 0;',
  9308. ' this.$eq = function (b) {',
  9309. ' return this.Int === b.Int;',
  9310. ' };',
  9311. ' this.$assign = function (s) {',
  9312. ' this.Int = s.Int;',
  9313. ' return this;',
  9314. ' };',
  9315. '});',
  9316. 'this.DoIt = function (vd, vc, vv) {',
  9317. '};',
  9318. 'this.Arr = [];',
  9319. 'this.r = this.TRec.$new();',
  9320. 'this.i = 0;'
  9321. ]),
  9322. LinesToStr([ // $mod.$main
  9323. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9324. '$mod.Arr[0].Int = 4;',
  9325. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9326. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9327. '$mod.Arr[7].$assign($mod.r);',
  9328. '$mod.r.$assign($mod.Arr[8]);',
  9329. '$mod.i = 0;',
  9330. '$mod.i = rtl.length($mod.Arr)-1;',
  9331. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9332. '']));
  9333. end;
  9334. procedure TTestModule.TestArray_StaticRecord;
  9335. begin
  9336. StartProgram(false);
  9337. Add([
  9338. 'type',
  9339. ' TRec = record',
  9340. ' Int: longint;',
  9341. ' end;',
  9342. ' TArrayRec = array[1..2] of TRec;',
  9343. 'var',
  9344. ' Arr: TArrayRec;',
  9345. 'begin',
  9346. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9347. '']);
  9348. ConvertProgram;
  9349. CheckSource('TestArray_StaticRecord',
  9350. LinesToStr([ // statements
  9351. 'rtl.recNewT(this, "TRec", function () {',
  9352. ' this.Int = 0;',
  9353. ' this.$eq = function (b) {',
  9354. ' return this.Int === b.Int;',
  9355. ' };',
  9356. ' this.$assign = function (s) {',
  9357. ' this.Int = s.Int;',
  9358. ' return this;',
  9359. ' };',
  9360. '});',
  9361. 'this.TArrayRec$clone = function (a) {',
  9362. ' var r = [];',
  9363. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9364. ' return r;',
  9365. '};',
  9366. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9367. '']),
  9368. LinesToStr([ // $mod.$main
  9369. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9370. end;
  9371. procedure TTestModule.TestArrayOfSet;
  9372. begin
  9373. StartProgram(false);
  9374. Add([
  9375. 'type',
  9376. ' TFlag = (big,small);',
  9377. ' TSetOfFlag = set of tflag;',
  9378. ' TArrayFlag = array of TSetOfFlag;',
  9379. 'procedure DoIt(const a: Tarrayflag);',
  9380. 'begin',
  9381. 'end;',
  9382. 'var',
  9383. ' f: TFlag;',
  9384. ' s: TSetOfFlag;',
  9385. ' Arr: TArrayFlag;',
  9386. ' i: longint;',
  9387. 'begin',
  9388. ' SetLength(arr,3);',
  9389. ' arr[0]:=s;',
  9390. ' arr[1]:=[big];',
  9391. ' arr[2]:=[big]+s;',
  9392. ' arr[3]:=s+[big];',
  9393. ' arr[4]:=arr[5];',
  9394. ' s:=arr[6];',
  9395. ' i:=low(arr);',
  9396. ' i:=high(arr);',
  9397. ' DoIt(arr);',
  9398. ' DoIt([s]);',
  9399. ' DoIt([[],s]);',
  9400. ' DoIt([s,[]]);',
  9401. '']);
  9402. ConvertProgram;
  9403. CheckSource('TestArrayOfSet',
  9404. LinesToStr([ // statements
  9405. 'this.TFlag = {',
  9406. ' "0": "big",',
  9407. ' big: 0,',
  9408. ' "1": "small",',
  9409. ' small: 1',
  9410. '};',
  9411. 'this.DoIt = function (a) {',
  9412. '};',
  9413. 'this.f = 0;',
  9414. 'this.s = {};',
  9415. 'this.Arr = [];',
  9416. 'this.i = 0;',
  9417. '']),
  9418. LinesToStr([ // $mod.$main
  9419. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9420. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9421. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9422. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9423. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9424. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9425. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9426. '$mod.i = 0;',
  9427. '$mod.i = rtl.length($mod.Arr) - 1;',
  9428. '$mod.DoIt($mod.Arr);',
  9429. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9430. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9431. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9432. '']));
  9433. end;
  9434. procedure TTestModule.TestArray_DynAsParam;
  9435. begin
  9436. StartProgram(false);
  9437. Add([
  9438. 'type integer = longint;',
  9439. 'type TArrInt = array of integer;',
  9440. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9441. 'var vJ: TArrInt;',
  9442. 'begin',
  9443. ' vg:=vg;',
  9444. ' vj:=vh;',
  9445. ' vi:=vi;',
  9446. ' doit(vg,vg,vg);',
  9447. ' doit(vh,vh,vj);',
  9448. ' doit(vi,vi,vi);',
  9449. ' doit(vj,vj,vj);',
  9450. 'end;',
  9451. 'var i: TArrInt;',
  9452. 'begin',
  9453. ' doit(i,i,i);']);
  9454. ConvertProgram;
  9455. CheckSource('TestArray_DynAsParams',
  9456. LinesToStr([ // statements
  9457. 'this.DoIt = function (vG,vH,vI) {',
  9458. ' var vJ = [];',
  9459. ' vG = rtl.arrayRef(vG);',
  9460. ' vJ = rtl.arrayRef(vH);',
  9461. ' vI.set(rtl.arrayRef(vI.get()));',
  9462. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9463. ' get: function () {',
  9464. ' return vG;',
  9465. ' },',
  9466. ' set: function (v) {',
  9467. ' vG = v;',
  9468. ' }',
  9469. ' });',
  9470. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9471. ' get: function () {',
  9472. ' return vJ;',
  9473. ' },',
  9474. ' set: function (v) {',
  9475. ' vJ = v;',
  9476. ' }',
  9477. ' });',
  9478. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9479. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9480. ' get: function () {',
  9481. ' return vJ;',
  9482. ' },',
  9483. ' set: function (v) {',
  9484. ' vJ = v;',
  9485. ' }',
  9486. ' });',
  9487. '};',
  9488. 'this.i = [];'
  9489. ]),
  9490. LinesToStr([
  9491. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9492. ' p: $mod,',
  9493. ' get: function () {',
  9494. ' return this.p.i;',
  9495. ' },',
  9496. ' set: function (v) {',
  9497. ' this.p.i = v;',
  9498. ' }',
  9499. '});'
  9500. ]));
  9501. end;
  9502. procedure TTestModule.TestArray_StaticAsParam;
  9503. begin
  9504. StartProgram(false);
  9505. Add([
  9506. 'type integer = longint;',
  9507. 'type TArrInt = array[1..2] of integer;',
  9508. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9509. 'var vJ: TArrInt;',
  9510. 'begin',
  9511. ' vg:=vg;',
  9512. ' vj:=vh;',
  9513. ' vi:=vi;',
  9514. ' doit(vg,vg,vg);',
  9515. ' doit(vh,vh,vj);',
  9516. ' doit(vi,vi,vi);',
  9517. ' doit(vj,vj,vj);',
  9518. 'end;',
  9519. 'var i: TArrInt;',
  9520. 'begin',
  9521. ' doit(i,i,i);']);
  9522. ConvertProgram;
  9523. CheckSource('TestArray_StaticAsParams',
  9524. LinesToStr([ // statements
  9525. 'this.DoIt = function (vG,vH,vI) {',
  9526. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9527. ' vG = vG.slice(0);',
  9528. ' vJ = vH.slice(0);',
  9529. ' vI.set(vI.get().slice(0));',
  9530. ' $mod.DoIt(vG.slice(0), vG, {',
  9531. ' get: function () {',
  9532. ' return vG;',
  9533. ' },',
  9534. ' set: function (v) {',
  9535. ' vG = v;',
  9536. ' }',
  9537. ' });',
  9538. ' $mod.DoIt(vH.slice(0), vH, {',
  9539. ' get: function () {',
  9540. ' return vJ;',
  9541. ' },',
  9542. ' set: function (v) {',
  9543. ' vJ = v;',
  9544. ' }',
  9545. ' });',
  9546. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9547. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9548. ' get: function () {',
  9549. ' return vJ;',
  9550. ' },',
  9551. ' set: function (v) {',
  9552. ' vJ = v;',
  9553. ' }',
  9554. ' });',
  9555. '};',
  9556. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9557. ]),
  9558. LinesToStr([
  9559. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9560. ' p: $mod,',
  9561. ' get: function () {',
  9562. ' return this.p.i;',
  9563. ' },',
  9564. ' set: function (v) {',
  9565. ' this.p.i = v;',
  9566. ' }',
  9567. '});'
  9568. ]));
  9569. end;
  9570. procedure TTestModule.TestArrayElement_AsParams;
  9571. begin
  9572. StartProgram(false);
  9573. Add('type integer = longint;');
  9574. Add('type TArrayInt = array of integer;');
  9575. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9576. Add('var vJ: tarrayint;');
  9577. Add('begin');
  9578. Add(' vi:=vi;');
  9579. Add(' doit(vi,vi,vi);');
  9580. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9581. Add('end;');
  9582. Add('var a: TArrayInt;');
  9583. Add('begin');
  9584. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9585. ConvertProgram;
  9586. CheckSource('TestArrayElement_AsParams',
  9587. LinesToStr([ // statements
  9588. 'this.DoIt = function (vG,vH,vI) {',
  9589. ' var vJ = [];',
  9590. ' vI.set(vI.get());',
  9591. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9592. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9593. ' a:1+3,',
  9594. ' p:vJ,',
  9595. ' get: function () {',
  9596. ' return this.p[this.a];',
  9597. ' },',
  9598. ' set: function (v) {',
  9599. ' this.p[this.a] = v;',
  9600. ' }',
  9601. ' });',
  9602. '};',
  9603. 'this.a = [];'
  9604. ]),
  9605. LinesToStr([
  9606. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9607. ' a: 1+6,',
  9608. ' p: $mod.a,',
  9609. ' get: function () {',
  9610. ' return this.p[this.a];',
  9611. ' },',
  9612. ' set: function (v) {',
  9613. ' this.p[this.a] = v;',
  9614. ' }',
  9615. '});'
  9616. ]));
  9617. end;
  9618. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9619. begin
  9620. StartProgram(false);
  9621. Add('type Integer = longint;');
  9622. Add('type TArrayInt = array of integer;');
  9623. Add('function GetArr(vB: integer = 0): tarrayint;');
  9624. Add('begin');
  9625. Add('end;');
  9626. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9627. Add('begin');
  9628. Add('end;');
  9629. Add('begin');
  9630. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9631. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9632. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9633. ConvertProgram;
  9634. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9635. LinesToStr([ // statements
  9636. 'this.GetArr = function (vB) {',
  9637. ' var Result = [];',
  9638. ' return Result;',
  9639. '};',
  9640. 'this.DoIt = function (vG,vH,vI) {',
  9641. '};'
  9642. ]),
  9643. LinesToStr([
  9644. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9645. ' a: 1+3,',
  9646. ' p: $mod.GetArr(0),',
  9647. ' get: function () {',
  9648. ' return this.p[this.a];',
  9649. ' },',
  9650. ' set: function (v) {',
  9651. ' this.p[this.a] = v;',
  9652. ' }',
  9653. '});',
  9654. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9655. ' a: 2+3,',
  9656. ' p: $mod.GetArr(0),',
  9657. ' get: function () {',
  9658. ' return this.p[this.a];',
  9659. ' },',
  9660. ' set: function (v) {',
  9661. ' this.p[this.a] = v;',
  9662. ' }',
  9663. '});',
  9664. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9665. ' a: 3+3,',
  9666. ' p: $mod.GetArr(9),',
  9667. ' get: function () {',
  9668. ' return this.p[this.a];',
  9669. ' },',
  9670. ' set: function (v) {',
  9671. ' this.p[this.a] = v;',
  9672. ' }',
  9673. '});',
  9674. '']));
  9675. end;
  9676. procedure TTestModule.TestArrayEnumTypeRange;
  9677. begin
  9678. StartProgram(false);
  9679. Add([
  9680. 'type',
  9681. ' TEnum = (red,blue);',
  9682. ' TEnumArray = array[TEnum] of longint;',
  9683. 'var',
  9684. ' e: TEnum;',
  9685. ' i: longint;',
  9686. ' a: TEnumArray;',
  9687. ' numbers: TEnumArray = (1,2);',
  9688. ' names: array[TEnum] of string = (''red'',''blue'');',
  9689. 'begin',
  9690. ' e:=low(a);',
  9691. ' e:=high(a);',
  9692. ' i:=a[red];',
  9693. ' a[e]:=a[e];']);
  9694. ConvertProgram;
  9695. CheckSource('TestArrayEnumTypeRange',
  9696. LinesToStr([ // statements
  9697. ' this.TEnum = {',
  9698. ' "0": "red",',
  9699. ' red: 0,',
  9700. ' "1": "blue",',
  9701. ' blue: 1',
  9702. '};',
  9703. 'this.e = 0;',
  9704. 'this.i = 0;',
  9705. 'this.a = rtl.arraySetLength(null,0,2);',
  9706. 'this.numbers = [1, 2];',
  9707. 'this.names = ["red", "blue"];',
  9708. '']),
  9709. LinesToStr([ // $mod.$main
  9710. '$mod.e = $mod.TEnum.red;',
  9711. '$mod.e = $mod.TEnum.blue;',
  9712. '$mod.i = $mod.a[$mod.TEnum.red];',
  9713. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9714. '']));
  9715. end;
  9716. procedure TTestModule.TestArray_SetLengthOutArg;
  9717. begin
  9718. StartProgram(false);
  9719. Add([
  9720. 'type TArrInt = array of longint;',
  9721. 'procedure DoIt(out a: TArrInt);',
  9722. 'begin',
  9723. ' SetLength(a,2);',
  9724. 'end;',
  9725. 'begin',
  9726. '']);
  9727. ConvertProgram;
  9728. CheckSource('TestArray_SetLengthOutArg',
  9729. LinesToStr([ // statements
  9730. 'this.DoIt = function (a) {',
  9731. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9732. '};',
  9733. '']),
  9734. LinesToStr([
  9735. '']));
  9736. end;
  9737. procedure TTestModule.TestArray_SetLengthProperty;
  9738. begin
  9739. StartProgram(false);
  9740. Add('type');
  9741. Add(' TArrInt = array of longint;');
  9742. Add(' TObject = class');
  9743. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9744. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9745. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9746. Add(' end;');
  9747. Add('var Obj: TObject;');
  9748. Add('begin');
  9749. Add(' SetLength(Obj.Colors,2);');
  9750. ConvertProgram;
  9751. CheckSource('TestArray_SetLengthProperty',
  9752. LinesToStr([ // statements
  9753. 'rtl.createClass(this, "TObject", null, function () {',
  9754. ' this.$init = function () {',
  9755. ' };',
  9756. ' this.$final = function () {',
  9757. ' };',
  9758. '});',
  9759. 'this.Obj = null;',
  9760. '']),
  9761. LinesToStr([
  9762. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9763. '']));
  9764. end;
  9765. procedure TTestModule.TestArray_SetLengthMultiDim;
  9766. begin
  9767. StartProgram(false);
  9768. Add([
  9769. 'type',
  9770. ' TArrArrInt = array of array of longint;',
  9771. ' TArrStaInt = array of array[1..2] of longint;',
  9772. 'var',
  9773. ' a: TArrArrInt;',
  9774. ' b: TArrStaInt;',
  9775. 'begin',
  9776. ' SetLength(a,2);',
  9777. ' SetLength(a,3,4);',
  9778. ' SetLength(b,5);',
  9779. '']);
  9780. ConvertProgram;
  9781. CheckSource('TestArray_SetLengthMultiDim',
  9782. LinesToStr([ // statements
  9783. 'this.a = [];',
  9784. 'this.b = [];',
  9785. '']),
  9786. LinesToStr([
  9787. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9788. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9789. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9790. '']));
  9791. end;
  9792. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9793. begin
  9794. StartProgram(false);
  9795. Add([
  9796. 'type',
  9797. ' TStaArr1 = array[1..3] of boolean;',
  9798. //' TStaArr2 = array[5..6] of TStaArr1;',
  9799. ' TDynArr1StaArr1 = array of TStaArr1;',
  9800. //' TDynArr1StaArr2 = array of TStaArr2;',
  9801. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9802. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9803. 'var',
  9804. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9805. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9806. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9807. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9808. 'begin',
  9809. ' SetLength(DynArr1StaArr1,11);',
  9810. ' SetLength(DynArr2StaArr1,12);',
  9811. ' SetLength(DynArr2StaArr1[13],14);',
  9812. ' SetLength(DynArr2StaArr1,15,16);',
  9813. //' SetLength(DynArr1StaArr2,21);',
  9814. //' SetLength(DynArr2StaArr2,22);',
  9815. //' SetLength(DynArr2StaArr2[23],24);',
  9816. //' SetLength(DynArr2StaArr2,25,26);',
  9817. '']);
  9818. ConvertProgram;
  9819. CheckSource('TestArray_DynOfStatic',
  9820. LinesToStr([ // statements
  9821. 'this.DynArr1StaArr1 = [];',
  9822. 'this.DynArr2StaArr1 = [];',
  9823. '']),
  9824. LinesToStr([ // $mod.$main
  9825. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9826. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9827. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9828. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9829. ' $mod.DynArr2StaArr1,',
  9830. ' false,',
  9831. ' 15,',
  9832. ' 16,',
  9833. ' "s",',
  9834. ' 3',
  9835. ');',
  9836. '']));
  9837. end;
  9838. procedure TTestModule.TestArray_OpenArrayOfString;
  9839. begin
  9840. StartProgram(false);
  9841. Add('procedure DoIt(const a: array of String);');
  9842. Add('var');
  9843. Add(' i: longint;');
  9844. Add(' s: string;');
  9845. Add('begin');
  9846. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9847. Add('end;');
  9848. Add('var s: string;');
  9849. Add('begin');
  9850. Add(' DoIt([]);');
  9851. Add(' DoIt([s,''foo'','''',s+s]);');
  9852. ConvertProgram;
  9853. CheckSource('TestArray_OpenArrayOfString',
  9854. LinesToStr([ // statements
  9855. 'this.DoIt = function (a) {',
  9856. ' var i = 0;',
  9857. ' var s = "";',
  9858. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9859. ' i = $l;',
  9860. ' s = a[rtl.length(a) - i - 1];',
  9861. ' };',
  9862. '};',
  9863. 'this.s = "";',
  9864. '']),
  9865. LinesToStr([
  9866. '$mod.DoIt([]);',
  9867. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9868. '']));
  9869. end;
  9870. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9871. begin
  9872. StartProgram(false);
  9873. Add([
  9874. 'type TArr = array of char;',
  9875. 'var',
  9876. ' c: char;',
  9877. ' s: string;',
  9878. ' a: TArr;',
  9879. 'procedure Run(const a: array of char);',
  9880. 'begin',
  9881. ' Run(c);',
  9882. ' Run(s);',
  9883. 'end;',
  9884. 'begin',
  9885. ' a:=c;',
  9886. ' a:=s;',
  9887. ' a:=#13;',
  9888. ' a:=''Foo'';',
  9889. ' Run(c);',
  9890. ' Run(s);',
  9891. '']);
  9892. ConvertProgram;
  9893. CheckSource('TestArray_ArrayOfCharAssignString',
  9894. LinesToStr([ // statements
  9895. 'this.c = "";',
  9896. 'this.s = "";',
  9897. 'this.a = [];',
  9898. 'this.Run = function (a) {',
  9899. ' $mod.Run($mod.c.split(""));',
  9900. ' $mod.Run($mod.s.split(""));',
  9901. '};',
  9902. '']),
  9903. LinesToStr([
  9904. '$mod.a = $mod.c.split("");',
  9905. '$mod.a = $mod.s.split("");',
  9906. '$mod.a = "\r".split("");',
  9907. '$mod.a = "Foo".split("");',
  9908. '$mod.Run($mod.c.split(""));',
  9909. '$mod.Run($mod.s.split(""));',
  9910. '']));
  9911. end;
  9912. procedure TTestModule.TestArray_ConstRef;
  9913. begin
  9914. StartProgram(false);
  9915. Add([
  9916. 'type TArr = array of word;',
  9917. 'procedure Run(constref a: TArr);',
  9918. 'begin',
  9919. 'end;',
  9920. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9921. 'var l: TArr;',
  9922. 'begin',
  9923. ' Run(l);',
  9924. ' Run(a);',
  9925. ' Run(b);',
  9926. ' Run(c);',
  9927. ' Run(d);',
  9928. ' Run(e);',
  9929. 'end;',
  9930. 'begin',
  9931. '']);
  9932. ConvertProgram;
  9933. CheckResolverUnexpectedHints();
  9934. CheckSource('TestArray_ConstRef',
  9935. LinesToStr([ // statements
  9936. 'this.Run = function (a) {',
  9937. '};',
  9938. 'this.Fly = function (a, b, c, d, e) {',
  9939. ' var l = [];',
  9940. ' $mod.Run(l);',
  9941. ' $mod.Run(a);',
  9942. ' $mod.Run(b.get());',
  9943. ' $mod.Run(c.get());',
  9944. ' $mod.Run(d);',
  9945. ' $mod.Run(e);',
  9946. '};',
  9947. '']),
  9948. LinesToStr([
  9949. '']));
  9950. end;
  9951. procedure TTestModule.TestArray_Concat;
  9952. begin
  9953. StartProgram(false);
  9954. Add([
  9955. 'type',
  9956. ' integer = longint;',
  9957. ' TFlag = (big,small);',
  9958. ' TFlags = set of TFlag;',
  9959. ' TRec = record',
  9960. ' i: integer;',
  9961. ' end;',
  9962. ' TArrInt = array of integer;',
  9963. ' TArrRec = array of TRec;',
  9964. ' TArrFlag = array of TFlag;',
  9965. ' TArrSet = array of TFlags;',
  9966. ' TArrJSValue = array of jsvalue;',
  9967. 'var',
  9968. ' ArrInt: tarrint;',
  9969. ' ArrRec: tarrrec;',
  9970. ' ArrFlag: tarrflag;',
  9971. ' ArrSet: tarrset;',
  9972. ' ArrJSValue: tarrjsvalue;',
  9973. 'begin',
  9974. ' arrint:=concat(arrint);',
  9975. ' arrint:=concat(arrint,arrint);',
  9976. ' arrint:=concat(arrint,arrint,arrint);',
  9977. ' arrrec:=concat(arrrec);',
  9978. ' arrrec:=concat(arrrec,arrrec);',
  9979. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9980. ' arrset:=concat(arrset);',
  9981. ' arrset:=concat(arrset,arrset);',
  9982. ' arrset:=concat(arrset,arrset,arrset);',
  9983. ' arrjsvalue:=concat(arrjsvalue);',
  9984. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9985. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9986. ' arrint:=concat([1],arrint);',
  9987. ' arrflag:=concat([big]);',
  9988. ' arrflag:=concat([big],arrflag);',
  9989. ' arrflag:=concat(arrflag,[small]);',
  9990. '']);
  9991. ConvertProgram;
  9992. CheckSource('TestArray_Concat',
  9993. LinesToStr([ // statements
  9994. 'this.TFlag = {',
  9995. ' "0": "big",',
  9996. ' big: 0,',
  9997. ' "1": "small",',
  9998. ' small: 1',
  9999. '};',
  10000. 'rtl.recNewT(this, "TRec", function () {',
  10001. ' this.i = 0;',
  10002. ' this.$eq = function (b) {',
  10003. ' return this.i === b.i;',
  10004. ' };',
  10005. ' this.$assign = function (s) {',
  10006. ' this.i = s.i;',
  10007. ' return this;',
  10008. ' };',
  10009. '});',
  10010. 'this.ArrInt = [];',
  10011. 'this.ArrRec = [];',
  10012. 'this.ArrFlag = [];',
  10013. 'this.ArrSet = [];',
  10014. 'this.ArrJSValue = [];',
  10015. '']),
  10016. LinesToStr([ // $mod.$main
  10017. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10018. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10019. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10020. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10021. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10022. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10023. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10024. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10025. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10026. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10027. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10028. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10029. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10030. '$mod.ArrFlag = [$mod.TFlag.big];',
  10031. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10032. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10033. '']));
  10034. end;
  10035. procedure TTestModule.TestArray_Copy;
  10036. begin
  10037. StartProgram(false);
  10038. Add([
  10039. 'type',
  10040. ' integer = longint;',
  10041. ' TFlag = (big,small);',
  10042. ' TFlags = set of TFlag;',
  10043. ' TRec = record',
  10044. ' i: integer;',
  10045. ' end;',
  10046. ' TArrInt = array of integer;',
  10047. ' TArrRec = array of TRec;',
  10048. ' TArrSet = array of TFlags;',
  10049. ' TArrJSValue = array of jsvalue;',
  10050. 'var',
  10051. ' ArrInt: tarrint;',
  10052. ' ArrRec: tarrrec;',
  10053. ' ArrSet: tarrset;',
  10054. ' ArrJSValue: tarrjsvalue;',
  10055. 'begin',
  10056. ' arrint:=copy(arrint);',
  10057. ' arrint:=copy(arrint,2);',
  10058. ' arrint:=copy(arrint,3,4);',
  10059. ' arrint:=copy([1,1],1,2);',
  10060. ' arrrec:=copy(arrrec);',
  10061. ' arrrec:=copy(arrrec,5);',
  10062. ' arrrec:=copy(arrrec,6,7);',
  10063. ' arrset:=copy(arrset);',
  10064. ' arrset:=copy(arrset,8);',
  10065. ' arrset:=copy(arrset,9,10);',
  10066. ' arrjsvalue:=copy(arrjsvalue);',
  10067. ' arrjsvalue:=copy(arrjsvalue,11);',
  10068. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10069. ' ']);
  10070. ConvertProgram;
  10071. CheckSource('TestArray_Copy',
  10072. LinesToStr([ // statements
  10073. 'this.TFlag = {',
  10074. ' "0": "big",',
  10075. ' big: 0,',
  10076. ' "1": "small",',
  10077. ' small: 1',
  10078. '};',
  10079. 'rtl.recNewT(this, "TRec", function () {',
  10080. ' this.i = 0;',
  10081. ' this.$eq = function (b) {',
  10082. ' return this.i === b.i;',
  10083. ' };',
  10084. ' this.$assign = function (s) {',
  10085. ' this.i = s.i;',
  10086. ' return this;',
  10087. ' };',
  10088. '});',
  10089. 'this.ArrInt = [];',
  10090. 'this.ArrRec = [];',
  10091. 'this.ArrSet = [];',
  10092. 'this.ArrJSValue = [];',
  10093. '']),
  10094. LinesToStr([ // $mod.$main
  10095. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10096. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10097. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10098. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10099. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10100. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10101. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10102. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10103. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10104. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10105. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10106. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10107. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10108. '']));
  10109. end;
  10110. procedure TTestModule.TestArray_InsertDelete;
  10111. begin
  10112. StartProgram(false);
  10113. Add([
  10114. 'type',
  10115. ' integer = longint;',
  10116. ' TFlag = (big,small);',
  10117. ' TFlags = set of TFlag;',
  10118. ' TRec = record',
  10119. ' i: integer;',
  10120. ' end;',
  10121. ' TArrInt = array of integer;',
  10122. ' TArrRec = array of TRec;',
  10123. ' TArrSet = array of TFlags;',
  10124. ' TArrJSValue = array of jsvalue;',
  10125. ' TArrArrInt = array of TArrInt;',
  10126. 'var',
  10127. ' ArrInt: tarrint;',
  10128. ' ArrRec: tarrrec;',
  10129. ' ArrSet: tarrset;',
  10130. ' ArrJSValue: tarrjsvalue;',
  10131. ' ArrArrInt: TArrArrInt;',
  10132. 'begin',
  10133. ' Insert(1,arrint,2);',
  10134. ' Insert(arrint[3],arrint,4);',
  10135. ' Insert(arrrec[5],arrrec,6);',
  10136. ' Insert(arrset[7],arrset,7);',
  10137. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10138. ' Insert(10,arrjsvalue,11);',
  10139. ' Insert([23],arrarrint,22);',
  10140. ' Delete(arrint,12,13);',
  10141. ' Delete(arrrec,14,15);',
  10142. ' Delete(arrset,17,18);',
  10143. ' Delete(arrjsvalue,19,10);']);
  10144. ConvertProgram;
  10145. CheckSource('TestArray_InsertDelete',
  10146. LinesToStr([ // statements
  10147. 'this.TFlag = {',
  10148. ' "0": "big",',
  10149. ' big: 0,',
  10150. ' "1": "small",',
  10151. ' small: 1',
  10152. '};',
  10153. 'rtl.recNewT(this, "TRec", function () {',
  10154. ' this.i = 0;',
  10155. ' this.$eq = function (b) {',
  10156. ' return this.i === b.i;',
  10157. ' };',
  10158. ' this.$assign = function (s) {',
  10159. ' this.i = s.i;',
  10160. ' return this;',
  10161. ' };',
  10162. '});',
  10163. 'this.ArrInt = [];',
  10164. 'this.ArrRec = [];',
  10165. 'this.ArrSet = [];',
  10166. 'this.ArrJSValue = [];',
  10167. 'this.ArrArrInt = [];',
  10168. '']),
  10169. LinesToStr([ // $mod.$main
  10170. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10171. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10172. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10173. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10174. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10175. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10176. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10177. '$mod.ArrInt.splice(12, 13);',
  10178. '$mod.ArrRec.splice(14, 15);',
  10179. '$mod.ArrSet.splice(17, 18);',
  10180. '$mod.ArrJSValue.splice(19, 10);',
  10181. '']));
  10182. end;
  10183. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10184. begin
  10185. Parser.Options:=Parser.Options+[po_cassignments];
  10186. StartProgram(false);
  10187. Add([
  10188. '{$modeswitch arrayoperators}',
  10189. 'type',
  10190. ' integer = longint;',
  10191. ' TArrInt = array of integer;',
  10192. ' TArrStr = array of string;',
  10193. 'const',
  10194. ' Ints: TArrInt = (1,2,3);',
  10195. ' Aliases: TarrStr = (''foo'',''b'');',
  10196. ' OneInt: TArrInt = (7);',
  10197. ' OneStr: array of integer = (7);',
  10198. ' Chars: array of char = ''aoc'';',
  10199. ' Names: array of string = (''a'',''foo'');',
  10200. ' NameCount = low(Names)+high(Names)+length(Names);',
  10201. 'var i: integer;',
  10202. 'begin',
  10203. ' Ints:=[];',
  10204. ' Ints:=[1,1];',
  10205. ' Ints:=[1]+[2];',
  10206. ' Ints:=[2];',
  10207. ' Ints:=[]+ints;',
  10208. ' Ints:=Ints+[];',
  10209. ' Ints:=Ints+OneInt;',
  10210. ' Ints:=Ints+[1,1];',
  10211. ' Ints:=[i,i]+Ints;',
  10212. ' Ints:=[1]+[i]+[3];',
  10213. '']);
  10214. ConvertProgram;
  10215. CheckSource('TestArray_DynArrayConstObjFPC',
  10216. LinesToStr([ // statements
  10217. 'this.Ints = [1, 2, 3];',
  10218. 'this.Aliases = ["foo", "b"];',
  10219. 'this.OneInt = [7];',
  10220. 'this.OneStr = [7];',
  10221. 'this.Chars = ["a", "o", "c"];',
  10222. 'this.Names = ["a", "foo"];',
  10223. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10224. 'this.i = 0;',
  10225. '']),
  10226. LinesToStr([ // $mod.$main
  10227. '$mod.Ints = [];',
  10228. '$mod.Ints = [1, 1];',
  10229. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10230. '$mod.Ints = [2];',
  10231. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10232. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10233. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10234. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10235. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10236. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10237. '']));
  10238. end;
  10239. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10240. begin
  10241. StartProgram(false);
  10242. // Note: const c = [1,1]; defines a set!
  10243. Add([
  10244. '{$mode delphi}',
  10245. 'type',
  10246. ' integer = longint;',
  10247. ' TArrInt = array of integer;',
  10248. ' TArrStr = array of string;',
  10249. 'const',
  10250. ' Ints: TArrInt = [1,1,2];',
  10251. ' Aliases: TarrStr = [''foo'',''b''];',
  10252. ' OneInt: TArrInt = [7];',
  10253. ' OneStr: array of integer = [7]+[8];',
  10254. ' Chars: array of char = ''aoc'';',
  10255. ' Names: array of string = [''a'',''a''];',
  10256. ' NameCount = low(Names)+high(Names)+length(Names);',
  10257. 'begin',
  10258. '']);
  10259. ConvertProgram;
  10260. CheckSource('TestArray_DynArrayConstDelphi',
  10261. LinesToStr([ // statements
  10262. 'this.Ints = [1, 1, 2];',
  10263. 'this.Aliases = ["foo", "b"];',
  10264. 'this.OneInt = [7];',
  10265. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10266. 'this.Chars = ["a", "o", "c"];',
  10267. 'this.Names = ["a", "a"];',
  10268. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10269. '']),
  10270. LinesToStr([ // $mod.$main
  10271. '']));
  10272. end;
  10273. procedure TTestModule.TestArray_ArrayLitAsParam;
  10274. begin
  10275. StartProgram(false);
  10276. Add([
  10277. '{$modeswitch arrayoperators}',
  10278. 'type',
  10279. ' integer = longint;',
  10280. ' TArrInt = array of integer;',
  10281. ' TArrSet = array of (red,green,blue);',
  10282. 'procedure DoOpenInt(const a: array of integer); forward;',
  10283. 'procedure DoInt(const a: TArrInt);',
  10284. 'begin',
  10285. ' DoInt(a+[1]);',
  10286. ' DoInt([1]+a);',
  10287. ' DoOpenInt(a);',
  10288. ' DoOpenInt(a+[1]);',
  10289. ' DoOpenInt([1]+a);',
  10290. 'end;',
  10291. 'procedure DoOpenInt(const a: array of integer);',
  10292. 'begin',
  10293. ' DoOpenInt(a+[1]);',
  10294. ' DoOpenInt([1]+a);',
  10295. ' DoInt(a);',
  10296. ' DoInt(a+[1]);',
  10297. ' DoInt([1]+a);',
  10298. 'end;',
  10299. 'procedure DoSet(const a: TArrSet);',
  10300. 'begin',
  10301. ' DoSet(a+[red]);',
  10302. ' DoSet([blue]+a);',
  10303. 'end;',
  10304. 'var',
  10305. ' i: TArrInt;',
  10306. ' s: TArrSet;',
  10307. 'begin',
  10308. ' DoInt([1]);',
  10309. ' DoInt([1]+[2]);',
  10310. ' DoInt(i+[1]);',
  10311. ' DoInt([1]+i);',
  10312. ' DoOpenInt([1]);',
  10313. ' DoOpenInt([1]+[2]);',
  10314. ' DoOpenInt(i+[1]);',
  10315. ' DoOpenInt([1]+i);',
  10316. ' DoSet([red]);',
  10317. ' DoSet([blue]+[green]);',
  10318. ' DoSet(s+[blue]);',
  10319. ' DoSet([red]+s);',
  10320. '']);
  10321. ConvertProgram;
  10322. CheckSource('TestArray_ArrayLitAsParam',
  10323. LinesToStr([ // statements
  10324. 'this.TArrSet$a = {',
  10325. ' "0": "red",',
  10326. ' red: 0,',
  10327. ' "1": "green",',
  10328. ' green: 1,',
  10329. ' "2": "blue",',
  10330. ' blue: 2',
  10331. '};',
  10332. 'this.DoInt = function (a) {',
  10333. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10334. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10335. ' $mod.DoOpenInt(a);',
  10336. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10337. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10338. '};',
  10339. 'this.DoOpenInt = function (a) {',
  10340. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10341. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10342. ' $mod.DoInt(a);',
  10343. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10344. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10345. '};',
  10346. 'this.DoSet = function (a) {',
  10347. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10348. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10349. '};',
  10350. 'this.i = [];',
  10351. 'this.s = [];',
  10352. '']),
  10353. LinesToStr([ // $mod.$main
  10354. '$mod.DoInt([1]);',
  10355. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10356. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10357. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10358. '$mod.DoOpenInt([1]);',
  10359. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10360. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10361. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10362. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10363. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10364. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10365. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10366. '']));
  10367. end;
  10368. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10369. begin
  10370. StartProgram(false);
  10371. Add([
  10372. '{$modeswitch arrayoperators}',
  10373. 'type',
  10374. ' integer = longint;',
  10375. ' TArrInt = array of integer;',
  10376. ' TArrArrInt = array of TArrInt;',
  10377. 'procedure DoInt(const a: TArrArrInt);',
  10378. 'begin',
  10379. ' DoInt(a+[[1]]);',
  10380. ' DoInt([[1]]+a);',
  10381. ' DoInt(a);',
  10382. 'end;',
  10383. 'var',
  10384. ' i: TArrInt;',
  10385. ' a: TArrArrInt;',
  10386. 'begin',
  10387. ' a:=[[1]];',
  10388. ' a:=[i];',
  10389. ' a:=a+[i];',
  10390. ' a:=[i]+a;',
  10391. ' a:=[[1]+i];',
  10392. ' a:=[[1]+[2]];',
  10393. ' a:=[i+[2]];',
  10394. ' DoInt([[1]]);',
  10395. ' DoInt([[1]+[2],[3,4],[5]]);',
  10396. ' DoInt([i+[1]]+a);',
  10397. ' DoInt([i]+a);',
  10398. '']);
  10399. ConvertProgram;
  10400. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10401. LinesToStr([ // statements
  10402. 'this.DoInt = function (a) {',
  10403. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10404. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10405. ' $mod.DoInt(a);',
  10406. '};',
  10407. 'this.i = [];',
  10408. 'this.a = [];',
  10409. '']),
  10410. LinesToStr([ // $mod.$main
  10411. '$mod.a = [[1]];',
  10412. '$mod.a = [$mod.i];',
  10413. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10414. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10415. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10416. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10417. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10418. '$mod.DoInt([[1]]);',
  10419. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10420. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10421. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10422. '']));
  10423. end;
  10424. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10425. begin
  10426. StartProgram(false);
  10427. Add([
  10428. '{$modeswitch arrayoperators}',
  10429. 'type',
  10430. ' integer = longint;',
  10431. ' TArrInt = array[1..2] of integer;',
  10432. ' TArrArrInt = array of TArrInt;',
  10433. 'procedure DoInt(const a: TArrArrInt);',
  10434. 'begin',
  10435. ' DoInt(a+[[1,2]]);',
  10436. ' DoInt([[1,2]]+a);',
  10437. ' DoInt(a);',
  10438. 'end;',
  10439. 'var',
  10440. ' i: TArrInt;',
  10441. ' a: TArrArrInt;',
  10442. 'begin',
  10443. ' a:=[[1,1]];',
  10444. ' a:=[i];',
  10445. ' a:=a+[i];',
  10446. ' a:=[i]+a;',
  10447. ' DoInt([[1,1]]);',
  10448. ' DoInt([[1,2],[3,4]]);',
  10449. '']);
  10450. ConvertProgram;
  10451. CheckSource('TestArray_ArrayLitStaticAsParam',
  10452. LinesToStr([ // statements
  10453. 'this.DoInt = function (a) {',
  10454. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10455. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10456. ' $mod.DoInt(a);',
  10457. '};',
  10458. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10459. 'this.a = [];',
  10460. '']),
  10461. LinesToStr([ // $mod.$main
  10462. '$mod.a = [[1, 1]];',
  10463. '$mod.a = [$mod.i.slice(0)];',
  10464. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10465. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10466. '$mod.DoInt([[1, 1]]);',
  10467. '$mod.DoInt([[1, 2], [3, 4]]);',
  10468. '']));
  10469. end;
  10470. procedure TTestModule.TestArray_ForInArrOfString;
  10471. begin
  10472. StartProgram(false);
  10473. Add([
  10474. 'type',
  10475. 'type',
  10476. ' TMonthNameArray = array [1..12] of string;',
  10477. ' TMonthNames = TMonthNameArray;',
  10478. ' TObject = class',
  10479. ' private',
  10480. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10481. ' public',
  10482. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10483. ' end;',
  10484. 'var',
  10485. ' f: TObject;',
  10486. ' Month: string;',
  10487. ' Names: array of string = (''a'',''foo'',''bar'');',
  10488. ' i: longint;',
  10489. 'begin',
  10490. ' for Month in f.LongMonthNames do ;',
  10491. ' for Month in Names do ;',
  10492. ' for i:=low(Names) to high(Names) do ;',
  10493. '']);
  10494. ConvertProgram;
  10495. CheckSource('TestArray_ForInArrOfString',
  10496. LinesToStr([ // statements
  10497. 'rtl.createClass(this, "TObject", null, function () {',
  10498. ' this.$init = function () {',
  10499. ' };',
  10500. ' this.$final = function () {',
  10501. ' };',
  10502. '});',
  10503. 'this.f = null;',
  10504. 'this.Month = "";',
  10505. 'this.Names = ["a", "foo", "bar"];',
  10506. 'this.i = 0;',
  10507. '']),
  10508. LinesToStr([ // $mod.$main
  10509. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10510. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10511. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10512. '']));
  10513. end;
  10514. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10515. begin
  10516. StartProgram(false);
  10517. Add([
  10518. '{$modeswitch externalclass}',
  10519. 'type',
  10520. ' TJSObject = class external name ''Object''',
  10521. ' end;',
  10522. ' TJSArray = class external name ''Array''',
  10523. ' class function isArray(Value: JSValue) : boolean;',
  10524. ' function concat() : TJSArray; varargs;',
  10525. ' end;',
  10526. 'var',
  10527. ' aObj: TJSArray;',
  10528. ' a: array of longint;',
  10529. ' o: TJSObject;',
  10530. 'begin',
  10531. ' if TJSArray.isArray(65) then ;',
  10532. ' aObj:=TJSArray(a).concat(a);',
  10533. ' o:=TJSObject(a);']);
  10534. ConvertProgram;
  10535. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10536. LinesToStr([ // statements
  10537. 'this.aObj = null;',
  10538. 'this.a = [];',
  10539. 'this.o = null;',
  10540. '']),
  10541. LinesToStr([ // $mod.$main
  10542. 'if (Array.isArray(65)) ;',
  10543. '$mod.aObj = $mod.a.concat($mod.a);',
  10544. '$mod.o = $mod.a;',
  10545. '']));
  10546. end;
  10547. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10548. begin
  10549. StartProgram(false);
  10550. Add([
  10551. '{$modeswitch externalclass}',
  10552. 'type',
  10553. ' TArrStr = array of string;',
  10554. ' TJSArray = class external name ''Array''',
  10555. ' end;',
  10556. ' TJSObject = class external name ''Object''',
  10557. ' end;',
  10558. 'var',
  10559. ' aObj: TJSArray;',
  10560. ' a: TArrStr;',
  10561. ' jo: TJSObject;',
  10562. 'begin',
  10563. ' a:=TArrStr(aObj);',
  10564. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10565. ' a:=TarrStr(jo);',
  10566. '']);
  10567. ConvertProgram;
  10568. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10569. LinesToStr([ // statements
  10570. 'this.aObj = null;',
  10571. 'this.a = [];',
  10572. 'this.jo = null;',
  10573. '']),
  10574. LinesToStr([ // $mod.$main
  10575. '$mod.a = $mod.aObj;',
  10576. '$mod.aObj[1] = $mod.aObj[2];',
  10577. '$mod.a = $mod.jo;',
  10578. '']));
  10579. end;
  10580. procedure TTestModule.TestArrayOfConst_TVarRec;
  10581. begin
  10582. StartProgram(true,[supTVarRec]);
  10583. Add([
  10584. 'procedure Say(args: array of const);',
  10585. 'var',
  10586. ' i: longint;',
  10587. ' v: TVarRec;',
  10588. 'begin',
  10589. ' for i:=low(args) to high(args) do begin',
  10590. ' v:=args[i];',
  10591. ' case v.vtype of',
  10592. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10593. ' end;',
  10594. ' end;',
  10595. ' for v in args do ;',
  10596. ' args:=nil;',
  10597. ' SetLength(args,2);',
  10598. 'end;',
  10599. 'begin']);
  10600. ConvertProgram;
  10601. CheckSource('TestArrayOfConst_TVarRec',
  10602. LinesToStr([ // statements
  10603. 'this.Say = function (args) {',
  10604. ' var i = 0;',
  10605. ' var v = pas.system.TVarRec.$new();',
  10606. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10607. ' i = $l;',
  10608. ' v.$assign(args[i]);',
  10609. ' var $tmp = v.VType;',
  10610. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10611. ' };',
  10612. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10613. ' args = [];',
  10614. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10615. '};',
  10616. '']),
  10617. LinesToStr([ // $mod.$main
  10618. ]));
  10619. end;
  10620. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10621. begin
  10622. StartProgram(true,[supTVarRec]);
  10623. Add([
  10624. 'procedure Say(args: array of const);',
  10625. 'begin',
  10626. ' Say(args);',
  10627. 'end;',
  10628. 'var',
  10629. ' p: Pointer;',
  10630. ' j: jsvalue;',
  10631. ' c: currency;',
  10632. 'begin',
  10633. ' Say([]);',
  10634. ' Say([1]);',
  10635. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10636. '']);
  10637. ConvertProgram;
  10638. CheckSource('TestArrayOfConst_PassBaseTypes',
  10639. LinesToStr([ // statements
  10640. 'this.Say = function (args) {',
  10641. ' $mod.Say(args);',
  10642. '};',
  10643. 'this.p = null;',
  10644. 'this.j = undefined;',
  10645. 'this.c = 0;',
  10646. '']),
  10647. LinesToStr([ // $mod.$main
  10648. '$mod.Say([]);',
  10649. '$mod.Say(pas.system.VarRecs(0, 1));',
  10650. '$mod.Say(pas.system.VarRecs(',
  10651. ' 9,',
  10652. ' "c",',
  10653. ' 18,',
  10654. ' "foo",',
  10655. ' 5,',
  10656. ' null,',
  10657. ' 1,',
  10658. ' true,',
  10659. ' 3,',
  10660. ' 1.3,',
  10661. ' 5,',
  10662. ' $mod.p,',
  10663. ' 20,',
  10664. ' $mod.j,',
  10665. ' 12,',
  10666. ' $mod.c',
  10667. ' ));',
  10668. '']));
  10669. end;
  10670. procedure TTestModule.TestArrayOfConst_PassObj;
  10671. begin
  10672. StartProgram(true,[supTVarRec]);
  10673. Add([
  10674. '{$interfaces corba}',
  10675. 'type',
  10676. ' TObject = class',
  10677. ' end;',
  10678. ' TClass = class of TObject;',
  10679. ' IUnknown = interface',
  10680. ' end;',
  10681. 'procedure Say(args: array of const);',
  10682. 'begin',
  10683. 'end;',
  10684. 'var',
  10685. ' o: TObject;',
  10686. ' c: TClass;',
  10687. ' i: IUnknown;',
  10688. 'begin',
  10689. ' Say([o,c,TObject]);',
  10690. ' Say([nil,i]);',
  10691. '']);
  10692. ConvertProgram;
  10693. CheckSource('TestArrayOfConst_PassObj',
  10694. LinesToStr([ // statements
  10695. 'rtl.createClass(this, "TObject", null, function () {',
  10696. ' this.$init = function () {',
  10697. ' };',
  10698. ' this.$final = function () {',
  10699. ' };',
  10700. '});',
  10701. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10702. 'this.Say = function (args) {',
  10703. '};',
  10704. 'this.o = null;',
  10705. 'this.c = null;',
  10706. 'this.i = null;',
  10707. '']),
  10708. LinesToStr([ // $mod.$main
  10709. '$mod.Say(pas.system.VarRecs(',
  10710. ' 7,',
  10711. ' $mod.o,',
  10712. ' 8,',
  10713. ' $mod.c,',
  10714. ' 8,',
  10715. ' $mod.TObject',
  10716. '));',
  10717. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10718. '']));
  10719. end;
  10720. procedure TTestModule.TestRecord_Empty;
  10721. begin
  10722. StartProgram(false);
  10723. Add([
  10724. 'type',
  10725. ' TRecA = record',
  10726. ' end;',
  10727. 'var a,b: TRecA;',
  10728. 'begin',
  10729. ' if a=b then ;']);
  10730. ConvertProgram;
  10731. CheckSource('TestRecord_Empty',
  10732. LinesToStr([ // statements
  10733. 'rtl.recNewT(this, "TRecA", function () {',
  10734. ' this.$eq = function (b) {',
  10735. ' return true;',
  10736. ' };',
  10737. ' this.$assign = function (s) {',
  10738. ' return this;',
  10739. ' };',
  10740. '});',
  10741. 'this.a = this.TRecA.$new();',
  10742. 'this.b = this.TRecA.$new();',
  10743. '']),
  10744. LinesToStr([ // $mod.$main
  10745. 'if ($mod.a.$eq($mod.b)) ;'
  10746. ]));
  10747. end;
  10748. procedure TTestModule.TestRecord_Var;
  10749. begin
  10750. StartProgram(false);
  10751. Add('type');
  10752. Add(' TRecA = record');
  10753. Add(' Bold: longint;');
  10754. Add(' end;');
  10755. Add('var Rec: TRecA;');
  10756. Add('begin');
  10757. Add(' rec.bold:=123');
  10758. ConvertProgram;
  10759. CheckSource('TestRecord_Var',
  10760. LinesToStr([ // statements
  10761. 'rtl.recNewT(this, "TRecA", function () {',
  10762. ' this.Bold = 0;',
  10763. ' this.$eq = function (b) {',
  10764. ' return this.Bold === b.Bold;',
  10765. ' };',
  10766. ' this.$assign = function (s) {',
  10767. ' this.Bold = s.Bold;',
  10768. ' return this;',
  10769. ' };',
  10770. '});',
  10771. 'this.Rec = this.TRecA.$new();',
  10772. '']),
  10773. LinesToStr([ // $mod.$main
  10774. '$mod.Rec.Bold = 123;'
  10775. ]));
  10776. end;
  10777. procedure TTestModule.TestRecord_VarExternal;
  10778. begin
  10779. StartProgram(false);
  10780. Add([
  10781. '{$modeswitch externalclass}',
  10782. 'type',
  10783. ' TRecA = record',
  10784. ' i: byte;',
  10785. ' length_: longint external name ''length'';',
  10786. ' end;',
  10787. 'var Rec: TRecA;',
  10788. 'begin',
  10789. ' rec.length_ := rec.length_',
  10790. '']);
  10791. ConvertProgram;
  10792. CheckSource('TestRecord_VarExternal',
  10793. LinesToStr([ // statements
  10794. 'rtl.recNewT(this, "TRecA", function () {',
  10795. ' this.i = 0;',
  10796. ' this.$eq = function (b) {',
  10797. ' return (this.i === b.i) && (this.length === b.length);',
  10798. ' };',
  10799. ' this.$assign = function (s) {',
  10800. ' this.i = s.i;',
  10801. ' this.length = s.length;',
  10802. ' return this;',
  10803. ' };',
  10804. '});',
  10805. 'this.Rec = this.TRecA.$new();',
  10806. '']),
  10807. LinesToStr([ // $mod.$main
  10808. '$mod.Rec.length = $mod.Rec.length;'
  10809. ]));
  10810. end;
  10811. procedure TTestModule.TestRecord_WithDo;
  10812. begin
  10813. StartProgram(false);
  10814. Add('type');
  10815. Add(' TRec = record');
  10816. Add(' vI: longint;');
  10817. Add(' end;');
  10818. Add('var');
  10819. Add(' Int: longint;');
  10820. Add(' r: TRec;');
  10821. Add('begin');
  10822. Add(' with r do');
  10823. Add(' int:=vi;');
  10824. Add(' with r do begin');
  10825. Add(' int:=vi;');
  10826. Add(' vi:=int;');
  10827. Add(' end;');
  10828. ConvertProgram;
  10829. CheckSource('TestWithRecordDo',
  10830. LinesToStr([ // statements
  10831. 'rtl.recNewT(this, "TRec", function () {',
  10832. ' this.vI = 0;',
  10833. ' this.$eq = function (b) {',
  10834. ' return this.vI === b.vI;',
  10835. ' };',
  10836. ' this.$assign = function (s) {',
  10837. ' this.vI = s.vI;',
  10838. ' return this;',
  10839. ' };',
  10840. '});',
  10841. 'this.Int = 0;',
  10842. 'this.r = this.TRec.$new();',
  10843. '']),
  10844. LinesToStr([ // $mod.$main
  10845. 'var $with = $mod.r;',
  10846. '$mod.Int = $with.vI;',
  10847. 'var $with1 = $mod.r;',
  10848. '$mod.Int = $with1.vI;',
  10849. '$with1.vI = $mod.Int;'
  10850. ]));
  10851. end;
  10852. procedure TTestModule.TestRecord_Assign;
  10853. begin
  10854. StartProgram(false);
  10855. Add([
  10856. 'type',
  10857. ' TEnum = (red,green);',
  10858. ' TEnums = set of TEnum;',
  10859. ' TSmallRec = record',
  10860. ' N: longint;',
  10861. ' end;',
  10862. ' TBigRec = record',
  10863. ' Int: longint;',
  10864. ' D: double;',
  10865. ' Arr: array of longint;',
  10866. ' Arr2: array[1..2] of longint;',
  10867. ' Small: TSmallRec;',
  10868. ' Enums: TEnums;',
  10869. ' end;',
  10870. 'var',
  10871. ' r, s: TBigRec;',
  10872. 'begin',
  10873. ' r:=s;',
  10874. ' r:=default(TBigRec);',
  10875. ' r:=default(s);',
  10876. '']);
  10877. ConvertProgram;
  10878. CheckSource('TestRecord_Assign',
  10879. LinesToStr([ // statements
  10880. 'this.TEnum = {',
  10881. ' "0": "red",',
  10882. ' red: 0,',
  10883. ' "1": "green",',
  10884. ' green: 1',
  10885. '};',
  10886. 'rtl.recNewT(this, "TSmallRec", function () {',
  10887. ' this.N = 0;',
  10888. ' this.$eq = function (b) {',
  10889. ' return this.N === b.N;',
  10890. ' };',
  10891. ' this.$assign = function (s) {',
  10892. ' this.N = s.N;',
  10893. ' return this;',
  10894. ' };',
  10895. '});',
  10896. 'rtl.recNewT(this, "TBigRec", function () {',
  10897. ' this.Int = 0;',
  10898. ' this.D = 0.0;',
  10899. ' this.$new = function () {',
  10900. ' var r = Object.create(this);',
  10901. ' r.Arr = [];',
  10902. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10903. ' r.Small = $mod.TSmallRec.$new();',
  10904. ' r.Enums = {};',
  10905. ' return r;',
  10906. ' };',
  10907. ' this.$eq = function (b) {',
  10908. ' 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);',
  10909. ' };',
  10910. ' this.$assign = function (s) {',
  10911. ' this.Int = s.Int;',
  10912. ' this.D = s.D;',
  10913. ' this.Arr = rtl.arrayRef(s.Arr);',
  10914. ' this.Arr2 = s.Arr2.slice(0);',
  10915. ' this.Small.$assign(s.Small);',
  10916. ' this.Enums = rtl.refSet(s.Enums);',
  10917. ' return this;',
  10918. ' };',
  10919. '});',
  10920. 'this.r = this.TBigRec.$new();',
  10921. 'this.s = this.TBigRec.$new();',
  10922. '']),
  10923. LinesToStr([ // $mod.$main
  10924. '$mod.r.$assign($mod.s);',
  10925. '$mod.r.$assign($mod.TBigRec.$new());',
  10926. '$mod.r.$assign($mod.TBigRec.$new());',
  10927. '']));
  10928. end;
  10929. procedure TTestModule.TestRecord_AsParams;
  10930. begin
  10931. StartProgram(false);
  10932. Add([
  10933. 'type',
  10934. ' integer = longint;',
  10935. ' TRecord = record',
  10936. ' i: integer;',
  10937. ' end;',
  10938. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10939. 'var vL: TRecord;',
  10940. 'begin',
  10941. ' vd:=vd;',
  10942. ' vd.i:=vd.i;',
  10943. ' vl:=vc;',
  10944. ' vv:=vv;',
  10945. ' vv.i:=vv.i;',
  10946. ' U:=vl;',
  10947. ' U:=vd;',
  10948. ' U:=vc;',
  10949. ' U:=vv;',
  10950. ' vl:=TRecord(U);',
  10951. ' vd:=TRecord(U);',
  10952. ' vv:=TRecord(U);',
  10953. ' doit(vd,vd,vd,vd);',
  10954. ' doit(vc,vc,vl,vl);',
  10955. ' doit(vv,vv,vv,vv);',
  10956. ' doit(vl,vl,vl,vl);',
  10957. ' TRecord(U).i:=3;',
  10958. 'end;',
  10959. 'var i: TRecord;',
  10960. 'begin',
  10961. ' doit(i,i,i,i);',
  10962. '']);
  10963. ConvertProgram;
  10964. CheckSource('TestRecord_AsParams',
  10965. LinesToStr([ // statements
  10966. 'rtl.recNewT(this, "TRecord", function () {',
  10967. ' this.i = 0;',
  10968. ' this.$eq = function (b) {',
  10969. ' return this.i === b.i;',
  10970. ' };',
  10971. ' this.$assign = function (s) {',
  10972. ' this.i = s.i;',
  10973. ' return this;',
  10974. ' };',
  10975. '});',
  10976. 'this.DoIt = function (vD, vC, vV, U) {',
  10977. ' var vL = $mod.TRecord.$new();',
  10978. ' vD.$assign(vD);',
  10979. ' vD.i = vD.i;',
  10980. ' vL.$assign(vC);',
  10981. ' vV.$assign(vV);',
  10982. ' vV.i = vV.i;',
  10983. ' U.$assign(vL);',
  10984. ' U.$assign(vD);',
  10985. ' U.$assign(vC);',
  10986. ' U.$assign(vV);',
  10987. ' vL.$assign(U);',
  10988. ' vD.$assign(U);',
  10989. ' vV.$assign(U);',
  10990. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10991. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10992. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10993. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10994. ' U.i = 3;',
  10995. '};',
  10996. 'this.i = this.TRecord.$new();'
  10997. ]),
  10998. LinesToStr([
  10999. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11000. '']));
  11001. end;
  11002. procedure TTestModule.TestRecord_ConstRef;
  11003. begin
  11004. StartProgram(false);
  11005. Add([
  11006. 'type TRec = record i: word; end;',
  11007. 'procedure Run(constref a: TRec);',
  11008. 'begin',
  11009. 'end;',
  11010. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11011. 'var l: TRec;',
  11012. 'begin',
  11013. ' Run(l);',
  11014. ' Run(a);',
  11015. ' Run(b);',
  11016. ' Run(c);',
  11017. ' Run(d);',
  11018. ' Run(e);',
  11019. 'end;',
  11020. 'begin',
  11021. '']);
  11022. ConvertProgram;
  11023. CheckResolverUnexpectedHints();
  11024. CheckSource('TestRecord_ConstRef',
  11025. LinesToStr([ // statements
  11026. 'rtl.recNewT(this, "TRec", function () {',
  11027. ' this.i = 0;',
  11028. ' this.$eq = function (b) {',
  11029. ' return this.i === b.i;',
  11030. ' };',
  11031. ' this.$assign = function (s) {',
  11032. ' this.i = s.i;',
  11033. ' return this;',
  11034. ' };',
  11035. '});',
  11036. 'this.Run = function (a) {',
  11037. '};',
  11038. 'this.Fly = function (a, b, c, d, e) {',
  11039. ' var l = $mod.TRec.$new();',
  11040. ' $mod.Run(l);',
  11041. ' $mod.Run(a);',
  11042. ' $mod.Run(b);',
  11043. ' $mod.Run(c);',
  11044. ' $mod.Run(d);',
  11045. ' $mod.Run(e);',
  11046. '};',
  11047. '']),
  11048. LinesToStr([
  11049. '']));
  11050. end;
  11051. procedure TTestModule.TestRecordElement_AsParams;
  11052. begin
  11053. StartProgram(false);
  11054. Add('type');
  11055. Add(' integer = longint;');
  11056. Add(' TRecord = record');
  11057. Add(' i: integer;');
  11058. Add(' end;');
  11059. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11060. Add('var vJ: TRecord;');
  11061. Add('begin');
  11062. Add(' doit(vj.i,vj.i,vj.i);');
  11063. Add('end;');
  11064. Add('var r: TRecord;');
  11065. Add('begin');
  11066. Add(' doit(r.i,r.i,r.i);');
  11067. ConvertProgram;
  11068. CheckSource('TestRecordElement_AsParams',
  11069. LinesToStr([ // statements
  11070. 'rtl.recNewT(this, "TRecord", function () {',
  11071. ' this.i = 0;',
  11072. ' this.$eq = function (b) {',
  11073. ' return this.i === b.i;',
  11074. ' };',
  11075. ' this.$assign = function (s) {',
  11076. ' this.i = s.i;',
  11077. ' return this;',
  11078. ' };',
  11079. '});',
  11080. 'this.DoIt = function (vG,vH,vI) {',
  11081. ' var vJ = $mod.TRecord.$new();',
  11082. ' $mod.DoIt(vJ.i, vJ.i, {',
  11083. ' p: vJ,',
  11084. ' get: function () {',
  11085. ' return this.p.i;',
  11086. ' },',
  11087. ' set: function (v) {',
  11088. ' this.p.i = v;',
  11089. ' }',
  11090. ' });',
  11091. '};',
  11092. 'this.r = this.TRecord.$new();'
  11093. ]),
  11094. LinesToStr([
  11095. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11096. ' p: $mod.r,',
  11097. ' get: function () {',
  11098. ' return this.p.i;',
  11099. ' },',
  11100. ' set: function (v) {',
  11101. ' this.p.i = v;',
  11102. ' }',
  11103. '});'
  11104. ]));
  11105. end;
  11106. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11107. begin
  11108. StartProgram(false);
  11109. Add('type');
  11110. Add(' integer = longint;');
  11111. Add(' TRecord = record');
  11112. Add(' i: integer;');
  11113. Add(' end;');
  11114. Add('function GetRec(vB: integer = 0): TRecord;');
  11115. Add('begin');
  11116. Add('end;');
  11117. Add('procedure DoIt(vG: integer; const vH: integer);');
  11118. Add('begin');
  11119. Add('end;');
  11120. Add('begin');
  11121. Add(' doit(getrec.i,getrec.i);');
  11122. Add(' doit(getrec().i,getrec().i);');
  11123. Add(' doit(getrec(1).i,getrec(2).i);');
  11124. ConvertProgram;
  11125. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11126. LinesToStr([ // statements
  11127. 'rtl.recNewT(this, "TRecord", function () {',
  11128. ' this.i = 0;',
  11129. ' this.$eq = function (b) {',
  11130. ' return this.i === b.i;',
  11131. ' };',
  11132. ' this.$assign = function (s) {',
  11133. ' this.i = s.i;',
  11134. ' return this;',
  11135. ' };',
  11136. '});',
  11137. 'this.GetRec = function (vB) {',
  11138. ' var Result = $mod.TRecord.$new();',
  11139. ' return Result;',
  11140. '};',
  11141. 'this.DoIt = function (vG, vH) {',
  11142. '};',
  11143. '']),
  11144. LinesToStr([
  11145. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11146. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11147. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11148. '']));
  11149. end;
  11150. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11151. begin
  11152. StartProgram(false);
  11153. Add('type');
  11154. Add(' integer = longint;');
  11155. Add(' TRecord = record');
  11156. Add(' i: integer;');
  11157. Add(' end;');
  11158. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11159. Add('begin');
  11160. Add('end;');
  11161. Add('var r: trecord;');
  11162. Add('begin');
  11163. Add(' with r do ');
  11164. Add(' doit(i,i,i);');
  11165. ConvertProgram;
  11166. CheckSource('TestRecordElementFromWith_AsParams',
  11167. LinesToStr([ // statements
  11168. 'rtl.recNewT(this, "TRecord", function () {',
  11169. ' this.i = 0;',
  11170. ' this.$eq = function (b) {',
  11171. ' return this.i === b.i;',
  11172. ' };',
  11173. ' this.$assign = function (s) {',
  11174. ' this.i = s.i;',
  11175. ' return this;',
  11176. ' };',
  11177. '});',
  11178. 'this.DoIt = function (vG,vH,vI) {',
  11179. '};',
  11180. 'this.r = this.TRecord.$new();'
  11181. ]),
  11182. LinesToStr([
  11183. 'var $with = $mod.r;',
  11184. '$mod.DoIt($with.i,$with.i,{',
  11185. ' p: $with,',
  11186. ' get: function () {',
  11187. ' return this.p.i;',
  11188. ' },',
  11189. ' set: function (v) {',
  11190. ' this.p.i = v;',
  11191. ' }',
  11192. '});',
  11193. '']));
  11194. end;
  11195. procedure TTestModule.TestRecord_Equal;
  11196. begin
  11197. StartProgram(false);
  11198. Add('type');
  11199. Add(' integer = longint;');
  11200. Add(' TFlag = (red,blue);');
  11201. Add(' TFlags = set of TFlag;');
  11202. Add(' TProc = procedure;');
  11203. Add(' TRecord = record');
  11204. Add(' i: integer;');
  11205. Add(' Event: TProc;');
  11206. Add(' f: TFlags;');
  11207. Add(' end;');
  11208. Add(' TNested = record');
  11209. Add(' r: TRecord;');
  11210. Add(' end;');
  11211. Add('var');
  11212. Add(' b: boolean;');
  11213. Add(' r,s: trecord;');
  11214. Add('begin');
  11215. Add(' b:=r=s;');
  11216. Add(' b:=r<>s;');
  11217. ConvertProgram;
  11218. CheckSource('TestRecord_Equal',
  11219. LinesToStr([ // statements
  11220. 'this.TFlag = {',
  11221. ' "0": "red",',
  11222. ' red: 0,',
  11223. ' "1": "blue",',
  11224. ' blue: 1',
  11225. '};',
  11226. 'rtl.recNewT(this, "TRecord", function () {',
  11227. ' this.i = 0;',
  11228. ' this.Event = null;',
  11229. ' this.$new = function () {',
  11230. ' var r = Object.create(this);',
  11231. ' r.f = {};',
  11232. ' return r;',
  11233. ' };',
  11234. ' this.$eq = function (b) {',
  11235. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11236. ' };',
  11237. ' this.$assign = function (s) {',
  11238. ' this.i = s.i;',
  11239. ' this.Event = s.Event;',
  11240. ' this.f = rtl.refSet(s.f);',
  11241. ' return this;',
  11242. ' };',
  11243. '});',
  11244. 'rtl.recNewT(this, "TNested", function () {',
  11245. ' this.$new = function () {',
  11246. ' var r = Object.create(this);',
  11247. ' r.r = $mod.TRecord.$new();',
  11248. ' return r;',
  11249. ' };',
  11250. ' this.$eq = function (b) {',
  11251. ' return this.r.$eq(b.r);',
  11252. ' };',
  11253. ' this.$assign = function (s) {',
  11254. ' this.r.$assign(s.r);',
  11255. ' return this;',
  11256. ' };',
  11257. '});',
  11258. 'this.b = false;',
  11259. 'this.r = this.TRecord.$new();',
  11260. 'this.s = this.TRecord.$new();',
  11261. '']),
  11262. LinesToStr([
  11263. '$mod.b = $mod.r.$eq($mod.s);',
  11264. '$mod.b = !$mod.r.$eq($mod.s);',
  11265. '']));
  11266. end;
  11267. procedure TTestModule.TestRecord_JSValue;
  11268. begin
  11269. StartProgram(false);
  11270. Add([
  11271. 'type',
  11272. ' TRecord = record',
  11273. ' i: longint;',
  11274. ' end;',
  11275. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11276. 'begin',
  11277. 'end;',
  11278. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11279. 'begin',
  11280. ' if jsvalue(d) then ;',
  11281. ' if jsvalue(c) then ;',
  11282. ' if jsvalue(v) then ;',
  11283. 'end;',
  11284. 'var',
  11285. ' Jv: jsvalue;',
  11286. ' Rec: trecord;',
  11287. 'begin',
  11288. ' rec:=trecord(jv);',
  11289. ' jv:=rec;',
  11290. ' Fly(rec,rec);',
  11291. ' Fly(@rec,@rec);',
  11292. ' if jsvalue(Rec) then ;',
  11293. ' Run(trecord(jv),trecord(jv),rec);',
  11294. '']);
  11295. ConvertProgram;
  11296. CheckSource('TestRecord_JSValue',
  11297. LinesToStr([ // statements
  11298. 'rtl.recNewT(this, "TRecord", function () {',
  11299. ' this.i = 0;',
  11300. ' this.$eq = function (b) {',
  11301. ' return this.i === b.i;',
  11302. ' };',
  11303. ' this.$assign = function (s) {',
  11304. ' this.i = s.i;',
  11305. ' return this;',
  11306. ' };',
  11307. '});',
  11308. 'this.Fly = function (d, c) {',
  11309. '};',
  11310. 'this.Run = function (d, c, v) {',
  11311. ' if (d) ;',
  11312. ' if (c) ;',
  11313. ' if (v) ;',
  11314. '};',
  11315. 'this.Jv = undefined;',
  11316. 'this.Rec = this.TRecord.$new();',
  11317. '']),
  11318. LinesToStr([
  11319. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11320. '$mod.Jv = $mod.Rec;',
  11321. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11322. '$mod.Fly($mod.Rec, $mod.Rec);',
  11323. 'if ($mod.Rec) ;',
  11324. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11325. '']));
  11326. end;
  11327. procedure TTestModule.TestRecord_VariantFail;
  11328. begin
  11329. StartProgram(false);
  11330. Add([
  11331. 'type',
  11332. ' TRec = record',
  11333. ' case word of',
  11334. ' 0: (b0, b1: Byte);',
  11335. ' 1: (i: word);',
  11336. ' end;',
  11337. 'begin']);
  11338. SetExpectedPasResolverError('variant record is not supported',
  11339. nXIsNotSupported);
  11340. ConvertProgram;
  11341. end;
  11342. procedure TTestModule.TestRecord_FieldArray;
  11343. begin
  11344. StartProgram(false);
  11345. Add([
  11346. 'type',
  11347. ' TArrInt = array[3..4] of longint;',
  11348. ' TArrArrInt = array[3..4] of longint;',
  11349. ' TRec = record',
  11350. ' a: array of longint;',
  11351. ' s: array[1..2] of longint;',
  11352. ' m: array[1..2,3..4] of longint;',
  11353. ' o: TArrArrInt;',
  11354. ' end;',
  11355. 'begin']);
  11356. ConvertProgram;
  11357. CheckSource('TestRecord_FieldArray',
  11358. LinesToStr([ // statements
  11359. 'rtl.recNewT(this, "TRec", function () {',
  11360. ' this.$new = function () {',
  11361. ' var r = Object.create(this);',
  11362. ' r.a = [];',
  11363. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11364. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11365. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11366. ' return r;',
  11367. ' };',
  11368. ' this.$eq = function (b) {',
  11369. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11370. ' };',
  11371. ' this.$assign = function (s) {',
  11372. ' this.a = rtl.arrayRef(s.a);',
  11373. ' this.s = s.s.slice(0);',
  11374. ' this.m = s.m.slice(0);',
  11375. ' this.o = s.o.slice(0);',
  11376. ' return this;',
  11377. ' };',
  11378. '});',
  11379. '']),
  11380. LinesToStr([ // $mod.$main
  11381. '']));
  11382. end;
  11383. procedure TTestModule.TestRecord_Const;
  11384. begin
  11385. StartProgram(false);
  11386. Add([
  11387. 'type',
  11388. ' TArrInt = array[3..4] of longint;',
  11389. ' TPoint = record x,y: longint; end;',
  11390. ' TRec = record',
  11391. ' i: longint;',
  11392. ' a: array of longint;',
  11393. ' s: array[1..2] of longint;',
  11394. ' m: array[1..2,3..4] of longint;',
  11395. ' p: TPoint;',
  11396. ' end;',
  11397. ' TPoints = array of TPoint;',
  11398. 'const',
  11399. ' r: TRec = (',
  11400. ' i:1;',
  11401. ' a:(2,3);',
  11402. ' s:(4,5);',
  11403. ' m:( (11,12), (13,14) );',
  11404. ' p: (x:21; y:22)',
  11405. ' );',
  11406. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11407. 'begin']);
  11408. ConvertProgram;
  11409. CheckSource('TestRecord_Const',
  11410. LinesToStr([ // statements
  11411. 'rtl.recNewT(this, "TPoint", function () {',
  11412. ' this.x = 0;',
  11413. ' this.y = 0;',
  11414. ' this.$eq = function (b) {',
  11415. ' return (this.x === b.x) && (this.y === b.y);',
  11416. ' };',
  11417. ' this.$assign = function (s) {',
  11418. ' this.x = s.x;',
  11419. ' this.y = s.y;',
  11420. ' return this;',
  11421. ' };',
  11422. '});',
  11423. 'rtl.recNewT(this, "TRec", function () {',
  11424. ' this.i = 0;',
  11425. ' this.$new = function () {',
  11426. ' var r = Object.create(this);',
  11427. ' r.a = [];',
  11428. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11429. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11430. ' r.p = $mod.TPoint.$new();',
  11431. ' return r;',
  11432. ' };',
  11433. ' this.$eq = function (b) {',
  11434. ' 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);',
  11435. ' };',
  11436. ' this.$assign = function (s) {',
  11437. ' this.i = s.i;',
  11438. ' this.a = rtl.arrayRef(s.a);',
  11439. ' this.s = s.s.slice(0);',
  11440. ' this.m = s.m.slice(0);',
  11441. ' this.p.$assign(s.p);',
  11442. ' return this;',
  11443. ' };',
  11444. '});',
  11445. 'this.r = this.TRec.$clone({',
  11446. ' i: 1,',
  11447. ' a: [2, 3],',
  11448. ' s: [4, 5],',
  11449. ' m: [[11, 12], [13, 14]],',
  11450. ' p: this.TPoint.$clone({',
  11451. ' x: 21,',
  11452. ' y: 22',
  11453. ' })',
  11454. '});',
  11455. 'this.p = [this.TPoint.$clone({',
  11456. ' x: 1,',
  11457. ' y: 2',
  11458. '}), this.TPoint.$clone({',
  11459. ' x: 3,',
  11460. ' y: 4',
  11461. '})];',
  11462. '']),
  11463. LinesToStr([ // $mod.$main
  11464. '']));
  11465. end;
  11466. procedure TTestModule.TestRecord_TypecastFail;
  11467. begin
  11468. StartProgram(false);
  11469. Add([
  11470. 'type',
  11471. ' TPoint = record x,y: longint; end;',
  11472. ' TRec = record l: longint end;',
  11473. 'var p: TPoint;',
  11474. 'begin',
  11475. ' if TRec(p).l=2 then ;']);
  11476. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11477. nIllegalTypeConversionTo);
  11478. ConvertProgram;
  11479. end;
  11480. procedure TTestModule.TestRecord_InFunction;
  11481. begin
  11482. StartProgram(false);
  11483. Add([
  11484. 'var TPoint: longint = 3;',
  11485. 'procedure DoIt;',
  11486. 'type',
  11487. ' TPoint = record x,y: longint; end;',
  11488. ' TPoints = array of TPoint;',
  11489. 'var',
  11490. ' r: TPoint;',
  11491. ' p: TPoints;',
  11492. 'begin',
  11493. ' SetLength(p,2);',
  11494. 'end;',
  11495. 'begin']);
  11496. ConvertProgram;
  11497. CheckSource('TestRecord_InFunction',
  11498. LinesToStr([ // statements
  11499. 'this.TPoint = 3;',
  11500. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11501. ' this.x = 0;',
  11502. ' this.y = 0;',
  11503. ' this.$eq = function (b) {',
  11504. ' return (this.x === b.x) && (this.y === b.y);',
  11505. ' };',
  11506. ' this.$assign = function (s) {',
  11507. ' this.x = s.x;',
  11508. ' this.y = s.y;',
  11509. ' return this;',
  11510. ' };',
  11511. '});',
  11512. 'this.DoIt = function () {',
  11513. ' var r = TPoint$1.$new();',
  11514. ' var p = [];',
  11515. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11516. '};',
  11517. '']),
  11518. LinesToStr([ // $mod.$main
  11519. '']));
  11520. end;
  11521. procedure TTestModule.TestRecord_AnonymousFail;
  11522. begin
  11523. StartProgram(false);
  11524. Add([
  11525. 'var',
  11526. ' r: record x: word end;',
  11527. 'begin']);
  11528. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11529. nNotYetImplemented);
  11530. ConvertProgram;
  11531. end;
  11532. procedure TTestModule.TestAdvRecord_Function;
  11533. begin
  11534. StartProgram(false);
  11535. Parser.Options:=Parser.Options+[po_cassignments];
  11536. Add([
  11537. '{$modeswitch AdvancedRecords}',
  11538. 'type',
  11539. ' TPoint = record',
  11540. ' x,y: word;',
  11541. ' function Add(const apt: TPoint): TPoint;',
  11542. ' end;',
  11543. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11544. 'begin',
  11545. ' Result:=Self;',
  11546. ' Result.x+=apt.x;',
  11547. ' Result.y:=Result.y+apt.y;',
  11548. ' Self:=apt;',
  11549. 'end;',
  11550. 'var p,q: TPoint;',
  11551. 'begin',
  11552. ' p.add(q);',
  11553. ' p:=default(TPoint);',
  11554. ' p:=q;',
  11555. '']);
  11556. ConvertProgram;
  11557. CheckSource('TestAdvRecord_Function',
  11558. LinesToStr([ // statements
  11559. 'rtl.recNewT(this, "TPoint", function () {',
  11560. ' this.x = 0;',
  11561. ' this.y = 0;',
  11562. ' this.$eq = function (b) {',
  11563. ' return (this.x === b.x) && (this.y === b.y);',
  11564. ' };',
  11565. ' this.$assign = function (s) {',
  11566. ' this.x = s.x;',
  11567. ' this.y = s.y;',
  11568. ' return this;',
  11569. ' };',
  11570. ' this.Add = function (apt) {',
  11571. ' var Result = $mod.TPoint.$new();',
  11572. ' Result.$assign(this);',
  11573. ' Result.x += apt.x;',
  11574. ' Result.y = Result.y + apt.y;',
  11575. ' this.$assign(apt);',
  11576. ' return Result;',
  11577. ' };',
  11578. '});',
  11579. 'this.p = this.TPoint.$new();',
  11580. 'this.q = this.TPoint.$new();',
  11581. '']),
  11582. LinesToStr([ // $mod.$main
  11583. '$mod.p.Add($mod.q);',
  11584. '$mod.p.$assign($mod.TPoint.$new());',
  11585. '$mod.p.$assign($mod.q);',
  11586. '']));
  11587. end;
  11588. procedure TTestModule.TestAdvRecord_Property;
  11589. begin
  11590. StartProgram(false);
  11591. Add([
  11592. '{$modeswitch AdvancedRecords}',
  11593. 'type',
  11594. ' TPoint = record',
  11595. ' x,y: word;',
  11596. ' strict private',
  11597. ' function GetSize: longword;',
  11598. ' procedure SetSize(Value: longword);',
  11599. ' public',
  11600. ' property Size: longword read GetSize write SetSize;',
  11601. ' property Left: word read x write y;',
  11602. ' end;',
  11603. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11604. 'function TPoint.GetSize: longword;',
  11605. 'begin',
  11606. ' x:=y;',
  11607. ' Size:=Size;',
  11608. ' Left:=Left;',
  11609. 'end;',
  11610. 'procedure TPoint.SetSize(Value: longword);',
  11611. 'begin',
  11612. 'end;',
  11613. 'var p,q: TPoint;',
  11614. 'begin',
  11615. ' p.Size:=q.Size;',
  11616. ' p.Left:=q.Left;',
  11617. '']);
  11618. ConvertProgram;
  11619. CheckSource('TestAdvRecord_Property',
  11620. LinesToStr([ // statements
  11621. 'rtl.recNewT(this, "TPoint", function () {',
  11622. ' this.x = 0;',
  11623. ' this.y = 0;',
  11624. ' this.$eq = function (b) {',
  11625. ' return (this.x === b.x) && (this.y === b.y);',
  11626. ' };',
  11627. ' this.$assign = function (s) {',
  11628. ' this.x = s.x;',
  11629. ' this.y = s.y;',
  11630. ' return this;',
  11631. ' };',
  11632. ' this.GetSize = function () {',
  11633. ' var Result = 0;',
  11634. ' this.x = this.y;',
  11635. ' this.SetSize(this.GetSize());',
  11636. ' this.y = this.x;',
  11637. ' return Result;',
  11638. ' };',
  11639. ' this.SetSize = function (Value) {',
  11640. ' };',
  11641. '});',
  11642. 'this.SetSize = function (Value) {',
  11643. '};',
  11644. 'this.p = this.TPoint.$new();',
  11645. 'this.q = this.TPoint.$new();',
  11646. '']),
  11647. LinesToStr([ // $mod.$main
  11648. '$mod.p.SetSize($mod.q.GetSize());',
  11649. '$mod.p.y = $mod.q.x;',
  11650. '']));
  11651. end;
  11652. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11653. begin
  11654. StartProgram(false);
  11655. Add([
  11656. '{$modeswitch AdvancedRecords}',
  11657. 'type',
  11658. ' TPoint = record',
  11659. ' strict private',
  11660. ' function GetItems(Index: word): word;',
  11661. ' procedure SetItems(Index: word; Value: word);',
  11662. ' public',
  11663. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11664. ' end;',
  11665. 'function TPoint.GetItems(Index: word): word;',
  11666. 'begin',
  11667. ' Items[index]:=Items[index];',
  11668. ' self.Items[index]:=self.Items[index];',
  11669. 'end;',
  11670. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11671. 'begin',
  11672. 'end;',
  11673. 'var p: TPoint;',
  11674. 'begin',
  11675. ' p[1]:=p[2];',
  11676. ' p.Items[3]:=p.Items[4];',
  11677. '']);
  11678. ConvertProgram;
  11679. CheckSource('TestAdvRecord_PropertyDefault',
  11680. LinesToStr([ // statements
  11681. 'rtl.recNewT(this, "TPoint", function () {',
  11682. ' this.$eq = function (b) {',
  11683. ' return true;',
  11684. ' };',
  11685. ' this.$assign = function (s) {',
  11686. ' return this;',
  11687. ' };',
  11688. ' this.GetItems = function (Index) {',
  11689. ' var Result = 0;',
  11690. ' this.SetItems(Index, this.GetItems(Index));',
  11691. ' this.SetItems(Index, this.GetItems(Index));',
  11692. ' return Result;',
  11693. ' };',
  11694. ' this.SetItems = function (Index, Value) {',
  11695. ' };',
  11696. '});',
  11697. 'this.p = this.TPoint.$new();',
  11698. '']),
  11699. LinesToStr([ // $mod.$main
  11700. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11701. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11702. '']));
  11703. end;
  11704. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11705. begin
  11706. StartProgram(false);
  11707. Add([
  11708. '{$modeswitch AdvancedRecords}',
  11709. 'type',
  11710. ' TRec = record',
  11711. ' class var',
  11712. ' Fx: longint;',
  11713. ' Fy: longint;',
  11714. ' class function GetInt: longint; static;',
  11715. ' class procedure SetInt(Value: longint); static;',
  11716. ' class procedure DoIt; static;',
  11717. ' class property IntA: longint read Fx write Fy;',
  11718. ' class property IntB: longint read GetInt write SetInt;',
  11719. ' end;',
  11720. 'class function trec.getint: longint;',
  11721. 'begin',
  11722. ' result:=fx;',
  11723. 'end;',
  11724. 'class procedure trec.setint(value: longint);',
  11725. 'begin',
  11726. 'end;',
  11727. 'class procedure trec.doit;',
  11728. 'begin',
  11729. ' IntA:=IntA+1;',
  11730. ' IntB:=IntB+1;',
  11731. 'end;',
  11732. 'var r: trec;',
  11733. 'begin',
  11734. ' trec.inta:=trec.inta+1;',
  11735. ' if trec.intb=2 then;',
  11736. ' trec.intb:=trec.intb+2;',
  11737. ' trec.setint(trec.inta);',
  11738. ' r.inta:=r.inta+1;',
  11739. ' if r.intb=2 then;',
  11740. ' r.intb:=r.intb+2;',
  11741. ' r.setint(r.inta);']);
  11742. ConvertProgram;
  11743. CheckSource('TestAdvRecord_Property_ClassMethod',
  11744. LinesToStr([ // statements
  11745. 'rtl.recNewT(this, "TRec", function () {',
  11746. ' this.Fx = 0;',
  11747. ' this.Fy = 0;',
  11748. ' this.$eq = function (b) {',
  11749. ' return true;',
  11750. ' };',
  11751. ' this.$assign = function (s) {',
  11752. ' return this;',
  11753. ' };',
  11754. ' this.GetInt = function () {',
  11755. ' var Result = 0;',
  11756. ' Result = $mod.TRec.Fx;',
  11757. ' return Result;',
  11758. ' };',
  11759. ' this.SetInt = function (Value) {',
  11760. ' };',
  11761. ' this.DoIt = function () {',
  11762. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11763. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11764. ' };',
  11765. '}, true);',
  11766. 'this.r = this.TRec.$new();',
  11767. '']),
  11768. LinesToStr([ // $mod.$main
  11769. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11770. 'if ($mod.TRec.GetInt() === 2) ;',
  11771. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11772. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11773. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11774. 'if ($mod.TRec.GetInt() === 2) ;',
  11775. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11776. '$mod.TRec.SetInt($mod.r.Fx);',
  11777. '']));
  11778. end;
  11779. procedure TTestModule.TestAdvRecord_Const;
  11780. begin
  11781. StartProgram(false);
  11782. Add([
  11783. '{$modeswitch AdvancedRecords}',
  11784. 'type',
  11785. ' TArrInt = array[3..4] of longint;',
  11786. ' TPoint = record',
  11787. ' x,y: longint;',
  11788. ' class var Count: nativeint;',
  11789. ' end;',
  11790. ' TRec = record',
  11791. ' i: longint;',
  11792. ' a: array of longint;',
  11793. ' s: array[1..2] of longint;',
  11794. ' m: array[1..2,3..4] of longint;',
  11795. ' p: TPoint;',
  11796. ' end;',
  11797. ' TPoints = array of TPoint;',
  11798. 'const',
  11799. ' r: TRec = (',
  11800. ' i:1;',
  11801. ' a:(2,3);',
  11802. ' s:(4,5);',
  11803. ' m:( (11,12), (13,14) );',
  11804. ' p: (x:21)',
  11805. ' );',
  11806. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11807. 'begin']);
  11808. ConvertProgram;
  11809. CheckSource('TestAdvRecord_Const',
  11810. LinesToStr([ // statements
  11811. 'rtl.recNewT(this, "TPoint", function () {',
  11812. ' this.x = 0;',
  11813. ' this.y = 0;',
  11814. ' this.Count = 0;',
  11815. ' this.$eq = function (b) {',
  11816. ' return (this.x === b.x) && (this.y === b.y);',
  11817. ' };',
  11818. ' this.$assign = function (s) {',
  11819. ' this.x = s.x;',
  11820. ' this.y = s.y;',
  11821. ' return this;',
  11822. ' };',
  11823. '}, true);',
  11824. 'rtl.recNewT(this, "TRec", function () {',
  11825. ' this.i = 0;',
  11826. ' this.$new = function () {',
  11827. ' var r = Object.create(this);',
  11828. ' r.a = [];',
  11829. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11830. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11831. ' r.p = $mod.TPoint.$new();',
  11832. ' return r;',
  11833. ' };',
  11834. ' this.$eq = function (b) {',
  11835. ' 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);',
  11836. ' };',
  11837. ' this.$assign = function (s) {',
  11838. ' this.i = s.i;',
  11839. ' this.a = rtl.arrayRef(s.a);',
  11840. ' this.s = s.s.slice(0);',
  11841. ' this.m = s.m.slice(0);',
  11842. ' this.p.$assign(s.p);',
  11843. ' return this;',
  11844. ' };',
  11845. '});',
  11846. 'this.r = this.TRec.$clone({',
  11847. ' i: 1,',
  11848. ' a: [2, 3],',
  11849. ' s: [4, 5],',
  11850. ' m: [[11, 12], [13, 14]],',
  11851. ' p: this.TPoint.$clone({',
  11852. ' x: 21,',
  11853. ' y: 0',
  11854. ' })',
  11855. '});',
  11856. 'this.p = [this.TPoint.$clone({',
  11857. ' x: 1,',
  11858. ' y: 2',
  11859. '}), this.TPoint.$clone({',
  11860. ' x: 3,',
  11861. ' y: 4',
  11862. '})];',
  11863. '']),
  11864. LinesToStr([ // $mod.$main
  11865. '']));
  11866. end;
  11867. procedure TTestModule.TestAdvRecord_ExternalField;
  11868. begin
  11869. StartProgram(false);
  11870. Add([
  11871. '{$modeswitch AdvancedRecords}',
  11872. '{$modeswitch externalclass}',
  11873. 'type',
  11874. ' TCar = record',
  11875. ' public',
  11876. ' Intern: longint external name ''$Intern'';',
  11877. ' Intern2: longint external name ''$Intern2'';',
  11878. ' Bracket: longint external name ''["A B"]'';',
  11879. ' procedure DoIt;',
  11880. ' end;',
  11881. 'procedure tcar.doit;',
  11882. 'begin',
  11883. ' Intern:=Intern+1;',
  11884. ' Intern2:=Intern2+2;',
  11885. ' Bracket:=Bracket+3;',
  11886. 'end;',
  11887. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11888. 'begin',
  11889. ' Rec.intern:=Rec.intern+1;',
  11890. ' Rec.intern2:=Rec.intern2+2;',
  11891. ' Rec.Bracket:=Rec.Bracket+3;',
  11892. ' with Rec do begin',
  11893. ' intern:=intern+1;',
  11894. ' intern2:=intern2+2;',
  11895. ' Bracket:=Bracket+3;',
  11896. ' end;']);
  11897. ConvertProgram;
  11898. CheckSource('TestAdvRecord_ExternalField',
  11899. LinesToStr([ // statements
  11900. 'rtl.recNewT(this, "TCar", function () {',
  11901. ' this.$eq = function (b) {',
  11902. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11903. ' };',
  11904. ' this.$assign = function (s) {',
  11905. ' this.$Intern = s.$Intern;',
  11906. ' this.$Intern2 = s.$Intern2;',
  11907. ' this["A B"] = s["A B"];',
  11908. ' return this;',
  11909. ' };',
  11910. ' this.DoIt = function () {',
  11911. ' this.$Intern = this.$Intern + 1;',
  11912. ' this.$Intern2 = this.$Intern2 + 2;',
  11913. ' this["A B"] = this["A B"] + 3;',
  11914. ' };',
  11915. '});',
  11916. 'this.Rec = this.TCar.$clone({',
  11917. ' $Intern: 11,',
  11918. ' $Intern2: 12,',
  11919. ' "A B": 13',
  11920. '});',
  11921. '']),
  11922. LinesToStr([ // $mod.$main
  11923. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11924. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11925. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11926. 'var $with = $mod.Rec;',
  11927. '$with.$Intern = $with.$Intern + 1;',
  11928. '$with.$Intern2 = $with.$Intern2 + 2;',
  11929. '$with["A B"] = $with["A B"] + 3;',
  11930. '']));
  11931. end;
  11932. procedure TTestModule.TestAdvRecord_SubRecord;
  11933. begin
  11934. StartProgram(false);
  11935. Add([
  11936. '{$modeswitch AdvancedRecords}',
  11937. 'type',
  11938. ' TRec = record',
  11939. ' type',
  11940. ' TPoint = record',
  11941. ' x,y: longint;',
  11942. ' class var Count: nativeint;',
  11943. ' procedure DoIt;',
  11944. ' class procedure DoThat; static;',
  11945. ' end;',
  11946. ' var',
  11947. ' i: longint;',
  11948. ' p: TPoint;',
  11949. ' procedure DoSome;',
  11950. ' end;',
  11951. 'const',
  11952. ' r: TRec = (',
  11953. ' i:1;',
  11954. ' p: (x:21;y:22)',
  11955. ' );',
  11956. 'procedure TRec.DoSome;',
  11957. 'begin',
  11958. ' p.x:=p.y+1;',
  11959. ' p.Count:=p.Count+2;',
  11960. 'end;',
  11961. 'procedure TRec.TPoint.DoIt;',
  11962. 'begin',
  11963. ' Count:=Count+3;',
  11964. 'end;',
  11965. 'class procedure TRec.TPoint.DoThat;',
  11966. 'begin',
  11967. ' Count:=Count+4;',
  11968. 'end;',
  11969. 'begin']);
  11970. ConvertProgram;
  11971. CheckSource('TestAdvRecord_SubRecord',
  11972. LinesToStr([ // statements
  11973. 'rtl.recNewT(this, "TRec", function () {',
  11974. ' rtl.recNewT(this, "TPoint", function () {',
  11975. ' this.x = 0;',
  11976. ' this.y = 0;',
  11977. ' this.Count = 0;',
  11978. ' this.$eq = function (b) {',
  11979. ' return (this.x === b.x) && (this.y === b.y);',
  11980. ' };',
  11981. ' this.$assign = function (s) {',
  11982. ' this.x = s.x;',
  11983. ' this.y = s.y;',
  11984. ' return this;',
  11985. ' };',
  11986. ' this.DoIt = function () {',
  11987. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11988. ' };',
  11989. ' this.DoThat = function () {',
  11990. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11991. ' };',
  11992. ' }, true);',
  11993. ' this.i = 0;',
  11994. ' this.$new = function () {',
  11995. ' var r = Object.create(this);',
  11996. ' r.p = this.TPoint.$new();',
  11997. ' return r;',
  11998. ' };',
  11999. ' this.$eq = function (b) {',
  12000. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12001. ' };',
  12002. ' this.$assign = function (s) {',
  12003. ' this.i = s.i;',
  12004. ' this.p.$assign(s.p);',
  12005. ' return this;',
  12006. ' };',
  12007. ' this.DoSome = function () {',
  12008. ' this.p.x = this.p.y + 1;',
  12009. ' this.TPoint.Count = this.p.Count + 2;',
  12010. ' };',
  12011. '}, true);',
  12012. 'this.r = this.TRec.$clone({',
  12013. ' i: 1,',
  12014. ' p: this.TRec.TPoint.$clone({',
  12015. ' x: 21,',
  12016. ' y: 22',
  12017. ' })',
  12018. '});',
  12019. '']),
  12020. LinesToStr([ // $mod.$main
  12021. '']));
  12022. end;
  12023. procedure TTestModule.TestAdvRecord_SubClass;
  12024. begin
  12025. StartProgram(false);
  12026. Add([
  12027. '{$modeswitch AdvancedRecords}',
  12028. 'type',
  12029. ' TObject = class end;',
  12030. ' TPoint = record',
  12031. ' type',
  12032. ' TBird = class',
  12033. ' procedure DoIt;',
  12034. ' class procedure Glob;',
  12035. ' end;',
  12036. ' procedure DoIt(b: TBird);',
  12037. ' end;',
  12038. 'procedure TPoint.TBird.DoIt;',
  12039. 'begin',
  12040. ' doit;',
  12041. ' self.doit;',
  12042. ' glob;',
  12043. ' self.glob;',
  12044. 'end;',
  12045. 'class procedure TPoint.TBird.Glob;',
  12046. 'begin',
  12047. ' glob;',
  12048. ' self.glob;',
  12049. 'end;',
  12050. 'procedure TPoint.DoIt(b: TBird);',
  12051. 'begin',
  12052. ' b.doit;',
  12053. ' b.glob;',
  12054. ' TBird.glob;',
  12055. 'end;',
  12056. 'begin',
  12057. '']);
  12058. ConvertProgram;
  12059. CheckSource('TestAdvRecord_SubClass',
  12060. LinesToStr([ // statements
  12061. 'rtl.createClass(this, "TObject", null, function () {',
  12062. ' this.$init = function () {',
  12063. ' };',
  12064. ' this.$final = function () {',
  12065. ' };',
  12066. '});',
  12067. 'rtl.recNewT(this, "TPoint", function () {',
  12068. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12069. ' this.DoIt = function () {',
  12070. ' this.DoIt();',
  12071. ' this.DoIt();',
  12072. ' this.$class.Glob();',
  12073. ' this.$class.Glob();',
  12074. ' };',
  12075. ' this.Glob = function () {',
  12076. ' this.Glob();',
  12077. ' this.Glob();',
  12078. ' };',
  12079. ' }, "TPoint.TBird");',
  12080. ' this.$eq = function (b) {',
  12081. ' return true;',
  12082. ' };',
  12083. ' this.$assign = function (s) {',
  12084. ' return this;',
  12085. ' };',
  12086. ' this.DoIt = function (b) {',
  12087. ' b.DoIt();',
  12088. ' b.$class.Glob();',
  12089. ' this.TBird.Glob();',
  12090. ' };',
  12091. '}, true);',
  12092. '']),
  12093. LinesToStr([ // $mod.$main
  12094. '']));
  12095. end;
  12096. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12097. begin
  12098. StartProgram(false);
  12099. Add([
  12100. '{$modeswitch AdvancedRecords}',
  12101. 'type',
  12102. ' IUnknown = interface end;',
  12103. ' TPoint = record',
  12104. ' type IBird = interface end;',
  12105. ' end;',
  12106. 'begin',
  12107. '']);
  12108. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12109. nNotYetImplemented);
  12110. ParseProgram;
  12111. end;
  12112. procedure TTestModule.TestAdvRecord_Constructor;
  12113. begin
  12114. StartProgram(false);
  12115. Add([
  12116. '{$modeswitch AdvancedRecords}',
  12117. 'type',
  12118. ' TPoint = record',
  12119. ' x,y: longint;',
  12120. ' class procedure Run(w: longint = 13); static;',
  12121. ' constructor Create(ax: longint; ay: longint = -1);',
  12122. ' end;',
  12123. 'class procedure tpoint.run(w: longint);',
  12124. 'begin',
  12125. ' run;',
  12126. ' run();',
  12127. 'end;',
  12128. 'constructor tpoint.create(ax,ay: longint);',
  12129. 'begin',
  12130. ' x:=ax;',
  12131. ' self.y:=ay;',
  12132. ' run;',
  12133. ' run(ax);',
  12134. 'end;',
  12135. 'var r: TPoint;',
  12136. 'begin',
  12137. ' r:=TPoint.Create(1,2);',
  12138. ' with TPoint do r:=Create(1,2);',
  12139. ' r.Create(3);',
  12140. ' r:=r.Create(4);',
  12141. '']);
  12142. ConvertProgram;
  12143. CheckSource('TestAdvRecord_Constructor',
  12144. LinesToStr([ // statements
  12145. 'rtl.recNewT(this, "TPoint", function () {',
  12146. ' this.x = 0;',
  12147. ' this.y = 0;',
  12148. ' this.$eq = function (b) {',
  12149. ' return (this.x === b.x) && (this.y === b.y);',
  12150. ' };',
  12151. ' this.$assign = function (s) {',
  12152. ' this.x = s.x;',
  12153. ' this.y = s.y;',
  12154. ' return this;',
  12155. ' };',
  12156. ' this.Run = function (w) {',
  12157. ' $mod.TPoint.Run(13);',
  12158. ' $mod.TPoint.Run(13);',
  12159. ' };',
  12160. ' this.Create = function (ax, ay) {',
  12161. ' this.x = ax;',
  12162. ' this.y = ay;',
  12163. ' this.Run(13);',
  12164. ' this.Run(ax);',
  12165. ' return this;',
  12166. ' };',
  12167. '});',
  12168. 'this.r = this.TPoint.$new();',
  12169. '']),
  12170. LinesToStr([ // $mod.$main
  12171. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12172. 'var $with = $mod.TPoint;',
  12173. '$mod.r.$assign($with.$new().Create(1, 2));',
  12174. '$mod.r.Create(3, -1);',
  12175. '$mod.r.$assign($mod.r.Create(4, -1));',
  12176. '']));
  12177. end;
  12178. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12179. begin
  12180. StartProgram(false);
  12181. Add([
  12182. '{$modeswitch AdvancedRecords}',
  12183. 'type',
  12184. ' TPoint = record',
  12185. ' class var x: longint;',
  12186. ' class procedure Fly; static;',
  12187. ' class constructor Init;',
  12188. ' end;',
  12189. 'var count: word;',
  12190. 'class procedure Tpoint.Fly;',
  12191. 'begin',
  12192. 'end;',
  12193. 'class constructor tpoint.init;',
  12194. 'begin',
  12195. ' count:=count+1;',
  12196. ' x:=x+3;',
  12197. ' tpoint.x:=tpoint.x+4;',
  12198. ' fly;',
  12199. ' tpoint.fly;',
  12200. 'end;',
  12201. 'var r: TPoint;',
  12202. 'begin',
  12203. ' r.x:=r.x+10;',
  12204. ' r.Fly;',
  12205. ' r.Fly();',
  12206. '']);
  12207. ConvertProgram;
  12208. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12209. LinesToStr([ // statements
  12210. 'rtl.recNewT(this, "TPoint", function () {',
  12211. ' this.x = 0;',
  12212. ' this.$eq = function (b) {',
  12213. ' return true;',
  12214. ' };',
  12215. ' this.$assign = function (s) {',
  12216. ' return this;',
  12217. ' };',
  12218. ' this.Fly = function () {',
  12219. ' };',
  12220. '}, true);',
  12221. 'this.count = 0;',
  12222. 'this.r = this.TPoint.$new();',
  12223. '']),
  12224. LinesToStr([ // $mod.$main
  12225. '(function () {',
  12226. ' $mod.count = $mod.count + 1;',
  12227. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12228. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12229. ' $mod.TPoint.Fly();',
  12230. ' $mod.TPoint.Fly();',
  12231. '})();',
  12232. '$mod.TPoint.x = $mod.r.x + 10;',
  12233. '$mod.TPoint.Fly();',
  12234. '$mod.TPoint.Fly();',
  12235. '']));
  12236. end;
  12237. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12238. begin
  12239. StartUnit(false);
  12240. Add([
  12241. 'interface',
  12242. '{$modeswitch AdvancedRecords}',
  12243. 'type',
  12244. ' TPoint = record',
  12245. ' class var x: longint;',
  12246. ' class procedure Fly; static;',
  12247. ' class constructor Init;',
  12248. ' end;',
  12249. 'implementation',
  12250. 'var count: word;',
  12251. 'class procedure Tpoint.Fly;',
  12252. 'begin',
  12253. 'end;',
  12254. 'class constructor tpoint.init;',
  12255. 'begin',
  12256. ' count:=count+1;',
  12257. ' x:=3;',
  12258. ' tpoint.x:=4;',
  12259. ' fly;',
  12260. ' tpoint.fly;',
  12261. 'end;',
  12262. '']);
  12263. ConvertUnit;
  12264. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12265. LinesToStr([ // statements
  12266. 'var $impl = $mod.$impl;',
  12267. 'rtl.recNewT(this, "TPoint", function () {',
  12268. ' this.x = 0;',
  12269. ' this.$eq = function (b) {',
  12270. ' return true;',
  12271. ' };',
  12272. ' this.$assign = function (s) {',
  12273. ' return this;',
  12274. ' };',
  12275. ' this.Fly = function () {',
  12276. ' };',
  12277. '}, true);',
  12278. '']),
  12279. LinesToStr([ // $mod.$init
  12280. '(function () {',
  12281. ' $impl.count = $impl.count + 1;',
  12282. ' $mod.TPoint.x = 3;',
  12283. ' $mod.TPoint.x = 4;',
  12284. ' $mod.TPoint.Fly();',
  12285. ' $mod.TPoint.Fly();',
  12286. '})();',
  12287. '']),
  12288. LinesToStr([ // $mod.$main
  12289. '$impl.count = 0;',
  12290. '']));
  12291. end;
  12292. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12293. begin
  12294. StartProgram(false);
  12295. Add(['type',
  12296. ' TObject = class',
  12297. ' public',
  12298. ' constructor Create;',
  12299. ' destructor Destroy;',
  12300. ' end;',
  12301. ' TBird = TObject;',
  12302. 'constructor tobject.create;',
  12303. 'begin end;',
  12304. 'destructor tobject.destroy;',
  12305. 'begin end;',
  12306. 'var Obj: tobject;',
  12307. 'begin',
  12308. ' obj:=tobject.create;',
  12309. ' obj:=tobject.create();',
  12310. ' obj:=tbird.create;',
  12311. ' obj:=tbird.create();',
  12312. ' obj:=obj.create();',
  12313. ' obj.destroy;',
  12314. '']);
  12315. ConvertProgram;
  12316. CheckSource('TestClass_TObjectDefaultConstructor',
  12317. LinesToStr([ // statements
  12318. 'rtl.createClass(this,"TObject",null,function(){',
  12319. ' this.$init = function () {',
  12320. ' };',
  12321. ' this.$final = function () {',
  12322. ' };',
  12323. ' this.Create = function(){',
  12324. ' return this;',
  12325. ' };',
  12326. ' this.Destroy = function(){',
  12327. ' };',
  12328. '});',
  12329. 'this.Obj = null;'
  12330. ]),
  12331. LinesToStr([ // $mod.$main
  12332. '$mod.Obj = $mod.TObject.$create("Create");',
  12333. '$mod.Obj = $mod.TObject.$create("Create");',
  12334. '$mod.Obj = $mod.TObject.$create("Create");',
  12335. '$mod.Obj = $mod.TObject.$create("Create");',
  12336. '$mod.Obj = $mod.Obj.Create();',
  12337. '$mod.Obj.$destroy("Destroy");',
  12338. '']));
  12339. end;
  12340. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12341. begin
  12342. StartProgram(false);
  12343. Add('type');
  12344. Add(' TObject = class');
  12345. Add(' public');
  12346. Add(' constructor Create(Par: longint);');
  12347. Add(' end;');
  12348. Add('constructor tobject.create(par: longint);');
  12349. Add('begin end;');
  12350. Add('var Obj: tobject;');
  12351. Add('begin');
  12352. Add(' obj:=tobject.create(3);');
  12353. ConvertProgram;
  12354. CheckSource('TestClass_TObjectConstructorWithParams',
  12355. LinesToStr([ // statements
  12356. 'rtl.createClass(this,"TObject",null,function(){',
  12357. ' this.$init = function () {',
  12358. ' };',
  12359. ' this.$final = function () {',
  12360. ' };',
  12361. ' this.Create = function(Par){',
  12362. ' return this;',
  12363. ' };',
  12364. '});',
  12365. 'this.Obj = null;'
  12366. ]),
  12367. LinesToStr([ // $mod.$main
  12368. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12369. ]));
  12370. end;
  12371. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12372. begin
  12373. StartProgram(false);
  12374. Add('type');
  12375. Add(' TObject = class');
  12376. Add(' public');
  12377. Add(' constructor Create;');
  12378. Add(' end;');
  12379. Add(' TTest = class(TObject)');
  12380. Add(' public');
  12381. Add(' constructor Create(const Par: longint = 1);');
  12382. Add(' end;');
  12383. Add('constructor tobject.create;');
  12384. Add('begin end;');
  12385. Add('constructor ttest.create(const par: longint);');
  12386. Add('begin end;');
  12387. Add('var t: ttest;');
  12388. Add('begin');
  12389. Add(' t:=ttest.create;');
  12390. Add(' t:=ttest.create(2);');
  12391. ConvertProgram;
  12392. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12393. LinesToStr([ // statements
  12394. 'rtl.createClass(this,"TObject",null,function(){',
  12395. ' this.$init = function () {',
  12396. ' };',
  12397. ' this.$final = function () {',
  12398. ' };',
  12399. ' this.Create = function(){',
  12400. ' return this;',
  12401. ' };',
  12402. '});',
  12403. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12404. ' this.Create$1 = function (Par) {',
  12405. ' return this;',
  12406. ' };',
  12407. '});',
  12408. 'this.t = null;'
  12409. ]),
  12410. LinesToStr([ // $mod.$main
  12411. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12412. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12413. ]));
  12414. end;
  12415. procedure TTestModule.TestClass_Var;
  12416. begin
  12417. StartProgram(false);
  12418. Add([
  12419. 'type',
  12420. ' TObject = class',
  12421. ' public',
  12422. ' vI: longint;',
  12423. ' constructor Create(Par: longint);',
  12424. ' end;',
  12425. 'constructor tobject.create(par: longint);',
  12426. 'begin',
  12427. ' vi:=par+3',
  12428. 'end;',
  12429. 'var Obj: tobject;',
  12430. 'begin',
  12431. ' obj:=tobject.create(4);',
  12432. ' obj.vi:=obj.VI+5;']);
  12433. ConvertProgram;
  12434. CheckSource('TestClass_Var',
  12435. LinesToStr([ // statements
  12436. 'rtl.createClass(this,"TObject",null,function(){',
  12437. ' this.$init = function () {',
  12438. ' this.vI = 0;',
  12439. ' };',
  12440. ' this.$final = function () {',
  12441. ' };',
  12442. ' this.Create = function(Par){',
  12443. ' this.vI = Par+3;',
  12444. ' return this;',
  12445. ' };',
  12446. '});',
  12447. 'this.Obj = null;'
  12448. ]),
  12449. LinesToStr([ // $mod.$main
  12450. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12451. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12452. ]));
  12453. end;
  12454. procedure TTestModule.TestClass_Method;
  12455. begin
  12456. StartProgram(false);
  12457. Add('type');
  12458. Add(' TObject = class');
  12459. Add(' public');
  12460. Add(' vI: longint;');
  12461. Add(' Sub: TObject;');
  12462. Add(' constructor Create;');
  12463. Add(' function GetIt(Par: longint): tobject;');
  12464. Add(' end;');
  12465. Add('constructor tobject.create; begin end;');
  12466. Add('function tobject.getit(par: longint): tobject;');
  12467. Add('begin');
  12468. Add(' Self.vi:=par+3;');
  12469. Add(' Result:=self.sub;');
  12470. Add('end;');
  12471. Add('var Obj: tobject;');
  12472. Add('begin');
  12473. Add(' obj:=tobject.create;');
  12474. Add(' obj.getit(4);');
  12475. Add(' obj.sub.sub:=nil;');
  12476. Add(' obj.sub.getit(5);');
  12477. Add(' obj.sub.getit(6).SUB:=nil;');
  12478. Add(' obj.sub.getit(7).GETIT(8);');
  12479. Add(' obj.sub.getit(9).SuB.getit(10);');
  12480. ConvertProgram;
  12481. CheckSource('TestClass_Method',
  12482. LinesToStr([ // statements
  12483. 'rtl.createClass(this,"TObject",null,function(){',
  12484. ' this.$init = function () {',
  12485. ' this.vI = 0;',
  12486. ' this.Sub = null;',
  12487. ' };',
  12488. ' this.$final = function () {',
  12489. ' this.Sub = undefined;',
  12490. ' };',
  12491. ' this.Create = function(){',
  12492. ' return this;',
  12493. ' };',
  12494. ' this.GetIt = function(Par){',
  12495. ' var Result = null;',
  12496. ' this.vI = Par + 3;',
  12497. ' Result = this.Sub;',
  12498. ' return Result;',
  12499. ' };',
  12500. '});',
  12501. 'this.Obj = null;'
  12502. ]),
  12503. LinesToStr([ // $mod.$main
  12504. '$mod.Obj = $mod.TObject.$create("Create");',
  12505. '$mod.Obj.GetIt(4);',
  12506. '$mod.Obj.Sub.Sub=null;',
  12507. '$mod.Obj.Sub.GetIt(5);',
  12508. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12509. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12510. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12511. ]));
  12512. end;
  12513. procedure TTestModule.TestClass_Implementation;
  12514. begin
  12515. StartUnit(false);
  12516. Add([
  12517. 'interface',
  12518. 'type',
  12519. ' TObject = class',
  12520. ' constructor Create;',
  12521. ' end;',
  12522. 'implementation',
  12523. 'type',
  12524. ' TIntClass = class',
  12525. ' constructor Create; reintroduce;',
  12526. ' class procedure DoGlob;',
  12527. ' end;',
  12528. 'constructor tintclass.create;',
  12529. 'begin',
  12530. ' inherited;',
  12531. ' inherited create;',
  12532. ' doglob;',
  12533. 'end;',
  12534. 'class procedure tintclass.doglob;',
  12535. 'begin',
  12536. 'end;',
  12537. 'constructor tobject.create;',
  12538. 'var',
  12539. ' iC: tintclass;',
  12540. 'begin',
  12541. ' ic:=tintclass.create;',
  12542. ' tintclass.doglob;',
  12543. ' ic.doglob;',
  12544. 'end;',
  12545. 'initialization',
  12546. ' tintclass.doglob;',
  12547. '']);
  12548. ConvertUnit;
  12549. CheckSource('TestClass_Implementation',
  12550. LinesToStr([ // statements
  12551. 'var $impl = $mod.$impl;',
  12552. 'rtl.createClass(this, "TObject", null, function () {',
  12553. ' this.$init = function () {',
  12554. ' };',
  12555. ' this.$final = function () {',
  12556. ' };',
  12557. ' this.Create = function () {',
  12558. ' var iC = null;',
  12559. ' iC = $impl.TIntClass.$create("Create$1");',
  12560. ' $impl.TIntClass.DoGlob();',
  12561. ' iC.$class.DoGlob();',
  12562. ' return this;',
  12563. ' };',
  12564. '});',
  12565. '']),
  12566. LinesToStr([ // $mod.$main
  12567. '$impl.TIntClass.DoGlob();',
  12568. '']),
  12569. LinesToStr([
  12570. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12571. ' this.Create$1 = function () {',
  12572. ' $mod.TObject.Create.call(this);',
  12573. ' $mod.TObject.Create.call(this);',
  12574. ' this.$class.DoGlob();',
  12575. ' return this;',
  12576. ' };',
  12577. ' this.DoGlob = function () {',
  12578. ' };',
  12579. '});',
  12580. '']));
  12581. end;
  12582. procedure TTestModule.TestClass_Inheritance;
  12583. begin
  12584. StartProgram(false);
  12585. Add('type');
  12586. Add(' TObject = class');
  12587. Add(' public');
  12588. Add(' constructor Create;');
  12589. Add(' end;');
  12590. Add(' TClassA = class');
  12591. Add(' end;');
  12592. Add(' TClassB = class(TObject)');
  12593. Add(' procedure ProcB;');
  12594. Add(' end;');
  12595. Add('constructor tobject.create; begin end;');
  12596. Add('procedure tclassb.procb; begin end;');
  12597. Add('var');
  12598. Add(' oO: TObject;');
  12599. Add(' oA: TClassA;');
  12600. Add(' oB: TClassB;');
  12601. Add('begin');
  12602. Add(' oO:=tobject.Create;');
  12603. Add(' oA:=tclassa.Create;');
  12604. Add(' ob:=tclassb.Create;');
  12605. Add(' if oo is tclassa then ;');
  12606. Add(' ob:=oo as tclassb;');
  12607. Add(' (oo as tclassb).procb;');
  12608. ConvertProgram;
  12609. CheckSource('TestClass_Inheritance',
  12610. LinesToStr([ // statements
  12611. 'rtl.createClass(this,"TObject",null,function(){',
  12612. ' this.$init = function () {',
  12613. ' };',
  12614. ' this.$final = function () {',
  12615. ' };',
  12616. ' this.Create = function () {',
  12617. ' return this;',
  12618. ' };',
  12619. '});',
  12620. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12621. '});',
  12622. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12623. ' this.ProcB = function () {',
  12624. ' };',
  12625. '});',
  12626. 'this.oO = null;',
  12627. 'this.oA = null;',
  12628. 'this.oB = null;'
  12629. ]),
  12630. LinesToStr([ // $mod.$main
  12631. '$mod.oO = $mod.TObject.$create("Create");',
  12632. '$mod.oA = $mod.TClassA.$create("Create");',
  12633. '$mod.oB = $mod.TClassB.$create("Create");',
  12634. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12635. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12636. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12637. ]));
  12638. end;
  12639. procedure TTestModule.TestClass_TypeAlias;
  12640. begin
  12641. StartProgram(false);
  12642. Add([
  12643. '{$interfaces corba}',
  12644. 'type',
  12645. ' IObject = interface',
  12646. ' end;',
  12647. ' IBird = type IObject;',
  12648. ' TObject = class',
  12649. ' end;',
  12650. ' TBird = type TObject;',
  12651. 'var',
  12652. ' oObj: TObject;',
  12653. ' oBird: TBird;',
  12654. ' IntfObj: IObject;',
  12655. ' IntfBird: IBird;',
  12656. 'begin',
  12657. ' oObj:=oBird;',
  12658. '']);
  12659. ConvertProgram;
  12660. CheckSource('TestClass_TypeAlias',
  12661. LinesToStr([ // statements
  12662. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12663. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12664. 'rtl.createClass(this, "TObject", null, function () {',
  12665. ' this.$init = function () {',
  12666. ' };',
  12667. ' this.$final = function () {',
  12668. ' };',
  12669. '});',
  12670. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12671. '});',
  12672. 'this.oObj = null;',
  12673. 'this.oBird = null;',
  12674. 'this.IntfObj = null;',
  12675. 'this.IntfBird = null;',
  12676. '']),
  12677. LinesToStr([ // $mod.$main
  12678. '$mod.oObj = $mod.oBird;',
  12679. '']));
  12680. end;
  12681. procedure TTestModule.TestClass_AbstractMethod;
  12682. begin
  12683. StartProgram(false);
  12684. Add('type');
  12685. Add(' TObject = class');
  12686. Add(' public');
  12687. Add(' procedure DoIt; virtual; abstract;');
  12688. Add(' end;');
  12689. Add('begin');
  12690. ConvertProgram;
  12691. CheckSource('TestClass_AbstractMethod',
  12692. LinesToStr([ // statements
  12693. 'rtl.createClass(this,"TObject",null,function(){',
  12694. ' this.$init = function () {',
  12695. ' };',
  12696. ' this.$final = function () {',
  12697. ' };',
  12698. '});'
  12699. ]),
  12700. LinesToStr([ // this.$main
  12701. ''
  12702. ]));
  12703. end;
  12704. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12705. begin
  12706. StartProgram(false);
  12707. Add([
  12708. 'type',
  12709. ' TObject = class',
  12710. ' procedure DoAbstract; virtual; abstract;',
  12711. ' procedure DoVirtual; virtual;',
  12712. ' procedure DoIt;',
  12713. ' end;',
  12714. ' TA = class',
  12715. ' procedure doabstract; override;',
  12716. ' procedure dovirtual; override;',
  12717. ' procedure DoSome;',
  12718. ' end;',
  12719. 'procedure tobject.dovirtual;',
  12720. 'begin',
  12721. ' inherited; // call non existing ancestor -> ignore silently',
  12722. 'end;',
  12723. 'procedure tobject.doit;',
  12724. 'begin',
  12725. 'end;',
  12726. 'procedure ta.doabstract;',
  12727. 'begin',
  12728. ' inherited dovirtual; // call TObject.DoVirtual',
  12729. 'end;',
  12730. 'procedure ta.dovirtual;',
  12731. 'begin',
  12732. ' inherited; // call TObject.DoVirtual',
  12733. ' inherited dovirtual; // call TObject.DoVirtual',
  12734. ' inherited dovirtual(); // call TObject.DoVirtual',
  12735. ' doit;',
  12736. ' doit();',
  12737. 'end;',
  12738. 'procedure ta.dosome;',
  12739. 'begin',
  12740. ' inherited; // call non existing ancestor method -> silently ignore',
  12741. 'end;',
  12742. 'begin']);
  12743. ConvertProgram;
  12744. CheckSource('TestClass_CallInherited_ProcNoParams',
  12745. LinesToStr([ // statements
  12746. 'rtl.createClass(this,"TObject",null,function(){',
  12747. ' this.$init = function () {',
  12748. ' };',
  12749. ' this.$final = function () {',
  12750. ' };',
  12751. ' this.DoVirtual = function () {',
  12752. ' };',
  12753. ' this.DoIt = function () {',
  12754. ' };',
  12755. '});',
  12756. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12757. ' this.DoAbstract = function () {',
  12758. ' $mod.TObject.DoVirtual.call(this);',
  12759. ' };',
  12760. ' this.DoVirtual = function () {',
  12761. ' $mod.TObject.DoVirtual.call(this);',
  12762. ' $mod.TObject.DoVirtual.call(this);',
  12763. ' $mod.TObject.DoVirtual.call(this);',
  12764. ' this.DoIt();',
  12765. ' this.DoIt();',
  12766. ' };',
  12767. ' this.DoSome = function () {',
  12768. ' };',
  12769. '});'
  12770. ]),
  12771. LinesToStr([ // this.$main
  12772. ''
  12773. ]));
  12774. end;
  12775. procedure TTestModule.TestClass_CallInherited_WithParams;
  12776. begin
  12777. StartProgram(false);
  12778. Add([
  12779. 'type',
  12780. ' TObject = class',
  12781. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12782. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12783. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12784. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12785. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12786. ' end;',
  12787. ' TClassA = class',
  12788. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12789. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12790. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12791. ' end;',
  12792. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12793. 'begin',
  12794. 'end;',
  12795. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12796. 'begin',
  12797. 'end;',
  12798. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12799. 'begin',
  12800. 'end;',
  12801. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12802. 'begin',
  12803. 'end;',
  12804. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12805. 'begin',
  12806. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12807. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12808. 'end;',
  12809. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12810. 'begin',
  12811. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12812. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12813. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12814. ' doit(pa,pb);',
  12815. ' doit(pa);',
  12816. ' doit2(pa);',
  12817. ' doit2;',
  12818. 'end;',
  12819. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12820. 'begin',
  12821. ' pa:=inherited;',
  12822. 'end;',
  12823. 'begin']);
  12824. ConvertProgram;
  12825. CheckSource('TestClass_CallInherited_WithParams',
  12826. LinesToStr([ // statements
  12827. 'rtl.createClass(this,"TObject",null,function(){',
  12828. ' this.$init = function () {',
  12829. ' };',
  12830. ' this.$final = function () {',
  12831. ' };',
  12832. ' this.DoVirtual = function (pA,pB) {',
  12833. ' };',
  12834. ' this.DoIt = function (pA,pB) {',
  12835. ' };',
  12836. ' this.DoIt2 = function (pA,pB) {',
  12837. ' };',
  12838. ' this.GetIt = function (pA, pB) {',
  12839. ' var Result = 0;',
  12840. ' return Result;',
  12841. ' };',
  12842. '});',
  12843. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12844. ' this.DoAbstract = function (pA,pB) {',
  12845. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12846. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12847. ' };',
  12848. ' this.DoVirtual = function (pA,pB) {',
  12849. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12850. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12851. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12852. ' this.DoIt(pA,pB);',
  12853. ' this.DoIt(pA,0);',
  12854. ' this.DoIt2(pA,2);',
  12855. ' this.DoIt2(1,2);',
  12856. ' };',
  12857. ' this.GetIt$1 = function (pA, pB) {',
  12858. ' var Result = 0;',
  12859. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12860. ' return Result;',
  12861. ' };',
  12862. '});'
  12863. ]),
  12864. LinesToStr([ // this.$main
  12865. ''
  12866. ]));
  12867. end;
  12868. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12869. begin
  12870. StartProgram(false);
  12871. Add('type');
  12872. Add(' TObject = class');
  12873. Add(' constructor Create; virtual;');
  12874. Add(' constructor CreateWithB(b: boolean);');
  12875. Add(' end;');
  12876. Add(' TA = class');
  12877. Add(' constructor Create; override;');
  12878. Add(' constructor CreateWithC(c: char);');
  12879. Add(' procedure DoIt;');
  12880. Add(' class function DoSome: TObject;');
  12881. Add(' end;');
  12882. Add('constructor tobject.create;');
  12883. Add('begin');
  12884. Add(' inherited; // call non existing ancestor -> ignore silently');
  12885. Add('end;');
  12886. Add('constructor tobject.createwithb(b: boolean);');
  12887. Add('begin');
  12888. Add(' inherited; // call non existing ancestor -> ignore silently');
  12889. Add(' create; // normal call');
  12890. Add('end;');
  12891. Add('constructor ta.create;');
  12892. Add('begin');
  12893. Add(' inherited; // normal call TObject.Create');
  12894. Add(' inherited create; // normal call TObject.Create');
  12895. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12896. Add('end;');
  12897. Add('constructor ta.createwithc(c: char);');
  12898. Add('begin');
  12899. Add(' inherited create; // call TObject.Create');
  12900. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12901. Add(' doit;');
  12902. Add(' doit();');
  12903. Add(' dosome;');
  12904. Add('end;');
  12905. Add('procedure ta.doit;');
  12906. Add('begin');
  12907. Add(' create; // normal call');
  12908. Add(' createwithb(false); // normal call');
  12909. Add(' createwithc(''c''); // normal call');
  12910. Add('end;');
  12911. Add('class function ta.dosome: TObject;');
  12912. Add('begin');
  12913. Add(' Result:=create; // constructor');
  12914. Add(' Result:=createwithb(true); // constructor');
  12915. Add(' Result:=createwithc(''c''); // constructor');
  12916. Add('end;');
  12917. Add('begin');
  12918. ConvertProgram;
  12919. CheckSource('TestClass_CallInheritedConstructor',
  12920. LinesToStr([ // statements
  12921. 'rtl.createClass(this,"TObject",null,function(){',
  12922. ' this.$init = function () {',
  12923. ' };',
  12924. ' this.$final = function () {',
  12925. ' };',
  12926. ' this.Create = function () {',
  12927. ' return this;',
  12928. ' };',
  12929. ' this.CreateWithB = function (b) {',
  12930. ' this.Create();',
  12931. ' return this;',
  12932. ' };',
  12933. '});',
  12934. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12935. ' this.Create = function () {',
  12936. ' $mod.TObject.Create.call(this);',
  12937. ' $mod.TObject.Create.call(this);',
  12938. ' $mod.TObject.CreateWithB.call(this, false);',
  12939. ' return this;',
  12940. ' };',
  12941. ' this.CreateWithC = function (c) {',
  12942. ' $mod.TObject.Create.call(this);',
  12943. ' $mod.TObject.CreateWithB.call(this, true);',
  12944. ' this.DoIt();',
  12945. ' this.DoIt();',
  12946. ' this.$class.DoSome();',
  12947. ' return this;',
  12948. ' };',
  12949. ' this.DoIt = function () {',
  12950. ' this.Create();',
  12951. ' this.CreateWithB(false);',
  12952. ' this.CreateWithC("c");',
  12953. ' };',
  12954. ' this.DoSome = function () {',
  12955. ' var Result = null;',
  12956. ' Result = this.$create("Create");',
  12957. ' Result = this.$create("CreateWithB", [true]);',
  12958. ' Result = this.$create("CreateWithC", ["c"]);',
  12959. ' return Result;',
  12960. ' };',
  12961. '});'
  12962. ]),
  12963. LinesToStr([ // this.$main
  12964. ''
  12965. ]));
  12966. end;
  12967. procedure TTestModule.TestClass_ClassVar_Assign;
  12968. begin
  12969. StartProgram(false);
  12970. Add([
  12971. 'type',
  12972. ' TObject = class',
  12973. ' public',
  12974. ' class var vI: longint;',
  12975. ' class var Sub: TObject;',
  12976. ' constructor Create;',
  12977. ' class function GetIt(var Par: longint): tobject;',
  12978. ' end;',
  12979. 'constructor tobject.create;',
  12980. 'begin',
  12981. ' vi:=vi+1;',
  12982. ' Self.vi:=Self.vi+1;',
  12983. ' inc(vi);',
  12984. 'end;',
  12985. 'class function tobject.getit(var par: longint): tobject;',
  12986. 'begin',
  12987. ' vi:=vi+3;',
  12988. ' Self.vi:=Self.vi+4;',
  12989. ' inc(vi);',
  12990. ' Result:=self.sub;',
  12991. ' GetIt(vi);',
  12992. 'end;',
  12993. 'var Obj: tobject;',
  12994. 'begin',
  12995. ' obj:=tobject.create;',
  12996. ' tobject.vi:=3;',
  12997. ' if tobject.vi=4 then ;',
  12998. ' tobject.sub:=nil;',
  12999. ' obj.sub:=nil;',
  13000. ' obj.sub.sub:=nil;']);
  13001. ConvertProgram;
  13002. CheckSource('TestClass_ClassVar_Assign',
  13003. LinesToStr([ // statements
  13004. 'rtl.createClass(this,"TObject",null,function(){',
  13005. ' this.vI = 0;',
  13006. ' this.Sub = null;',
  13007. ' this.$init = function () {',
  13008. ' };',
  13009. ' this.$final = function () {',
  13010. ' };',
  13011. ' this.Create = function(){',
  13012. ' $mod.TObject.vI = this.vI+1;',
  13013. ' $mod.TObject.vI = this.vI+1;',
  13014. ' $mod.TObject.vI += 1;',
  13015. ' return this;',
  13016. ' };',
  13017. ' this.GetIt = function(Par){',
  13018. ' var Result = null;',
  13019. ' $mod.TObject.vI = this.vI + 3;',
  13020. ' $mod.TObject.vI = this.vI + 4;',
  13021. ' $mod.TObject.vI += 1;',
  13022. ' Result = this.Sub;',
  13023. ' this.GetIt({',
  13024. ' p: $mod.TObject,',
  13025. ' get: function () {',
  13026. ' return this.p.vI;',
  13027. ' },',
  13028. ' set: function (v) {',
  13029. ' this.p.vI = v;',
  13030. ' }',
  13031. ' });',
  13032. ' return Result;',
  13033. ' };',
  13034. '});',
  13035. 'this.Obj = null;'
  13036. ]),
  13037. LinesToStr([ // $mod.$main
  13038. '$mod.Obj = $mod.TObject.$create("Create");',
  13039. '$mod.TObject.vI = 3;',
  13040. 'if ($mod.TObject.vI === 4);',
  13041. '$mod.TObject.Sub=null;',
  13042. '$mod.TObject.Sub=null;',
  13043. '$mod.TObject.Sub=null;',
  13044. '']));
  13045. end;
  13046. procedure TTestModule.TestClass_CallClassMethod;
  13047. begin
  13048. StartProgram(false);
  13049. Add('type');
  13050. Add(' TObject = class');
  13051. Add(' public');
  13052. Add(' class var vI: longint;');
  13053. Add(' class var Sub: TObject;');
  13054. Add(' constructor Create;');
  13055. Add(' function GetMore(Par: longint): longint;');
  13056. Add(' class function GetIt(Par: longint): tobject;');
  13057. Add(' end;');
  13058. Add('constructor tobject.create;');
  13059. Add('begin');
  13060. Add(' sub:=getit(3);');
  13061. Add(' vi:=getmore(4);');
  13062. Add(' sub:=Self.getit(5);');
  13063. Add(' vi:=Self.getmore(6);');
  13064. Add('end;');
  13065. Add('function tobject.getmore(par: longint): longint;');
  13066. Add('begin');
  13067. Add(' sub:=getit(11);');
  13068. Add(' vi:=getmore(12);');
  13069. Add(' sub:=self.getit(13);');
  13070. Add(' vi:=self.getmore(14);');
  13071. Add('end;');
  13072. Add('class function tobject.getit(par: longint): tobject;');
  13073. Add('begin');
  13074. Add(' sub:=getit(21);');
  13075. Add(' vi:=sub.getmore(22);');
  13076. Add(' sub:=self.getit(23);');
  13077. Add(' vi:=self.sub.getmore(24);');
  13078. Add('end;');
  13079. Add('var Obj: tobject;');
  13080. Add('begin');
  13081. Add(' obj:=tobject.create;');
  13082. Add(' tobject.getit(5);');
  13083. Add(' obj.getit(6);');
  13084. Add(' obj.sub.getit(7);');
  13085. Add(' obj.sub.getit(8).SUB:=nil;');
  13086. Add(' obj.sub.getit(9).GETIT(10);');
  13087. Add(' obj.sub.getit(11).SuB.getit(12);');
  13088. ConvertProgram;
  13089. CheckSource('TestClass_CallClassMethod',
  13090. LinesToStr([ // statements
  13091. 'rtl.createClass(this,"TObject",null,function(){',
  13092. ' this.vI = 0;',
  13093. ' this.Sub = null;',
  13094. ' this.$init = function () {',
  13095. ' };',
  13096. ' this.$final = function () {',
  13097. ' };',
  13098. ' this.Create = function(){',
  13099. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13100. ' $mod.TObject.vI = this.GetMore(4);',
  13101. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13102. ' $mod.TObject.vI = this.GetMore(6);',
  13103. ' return this;',
  13104. ' };',
  13105. ' this.GetMore = function(Par){',
  13106. ' var Result = 0;',
  13107. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13108. ' $mod.TObject.vI = this.GetMore(12);',
  13109. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13110. ' $mod.TObject.vI = this.GetMore(14);',
  13111. ' return Result;',
  13112. ' };',
  13113. ' this.GetIt = function(Par){',
  13114. ' var Result = null;',
  13115. ' $mod.TObject.Sub = this.GetIt(21);',
  13116. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13117. ' $mod.TObject.Sub = this.GetIt(23);',
  13118. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13119. ' return Result;',
  13120. ' };',
  13121. '});',
  13122. 'this.Obj = null;'
  13123. ]),
  13124. LinesToStr([ // $mod.$main
  13125. '$mod.Obj = $mod.TObject.$create("Create");',
  13126. '$mod.TObject.GetIt(5);',
  13127. '$mod.Obj.$class.GetIt(6);',
  13128. '$mod.Obj.Sub.$class.GetIt(7);',
  13129. '$mod.TObject.Sub=null;',
  13130. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13131. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13132. '']));
  13133. end;
  13134. procedure TTestModule.TestClass_CallClassMethodStatic;
  13135. begin
  13136. StartProgram(false);
  13137. Add([
  13138. 'type',
  13139. ' TObject = class',
  13140. ' public',
  13141. ' class function Fly: tobject; static;',
  13142. ' end;',
  13143. 'class function tobject.Fly: tobject;',
  13144. 'begin',
  13145. ' Result.Fly;',
  13146. ' Result.Fly();',
  13147. ' Fly;',
  13148. ' Fly();',
  13149. ' Fly.Fly;',
  13150. ' Fly.Fly();',
  13151. 'end;',
  13152. 'var Obj: tobject;',
  13153. 'begin',
  13154. ' obj.Fly;',
  13155. ' obj.Fly();',
  13156. ' with obj do begin',
  13157. ' Fly;',
  13158. ' Fly();',
  13159. ' end;',
  13160. '']);
  13161. ConvertProgram;
  13162. CheckSource('TestClass_CallClassMethodStatic',
  13163. LinesToStr([ // statements
  13164. 'rtl.createClass(this, "TObject", null, function () {',
  13165. ' this.$init = function () {',
  13166. ' };',
  13167. ' this.$final = function () {',
  13168. ' };',
  13169. ' this.Fly = function () {',
  13170. ' var Result = null;',
  13171. ' $mod.TObject.Fly();',
  13172. ' $mod.TObject.Fly();',
  13173. ' $mod.TObject.Fly();',
  13174. ' $mod.TObject.Fly();',
  13175. ' $mod.TObject.Fly();',
  13176. ' $mod.TObject.Fly();',
  13177. ' return Result;',
  13178. ' };',
  13179. '});',
  13180. 'this.Obj = null;'
  13181. ]),
  13182. LinesToStr([ // $mod.$main
  13183. '$mod.TObject.Fly();',
  13184. '$mod.TObject.Fly();',
  13185. 'var $with = $mod.Obj;',
  13186. '$with.Fly();',
  13187. '$with.Fly();',
  13188. '']));
  13189. end;
  13190. procedure TTestModule.TestClass_Property;
  13191. begin
  13192. StartProgram(false);
  13193. Add('type');
  13194. Add(' TObject = class');
  13195. Add(' Fx: longint;');
  13196. Add(' Fy: longint;');
  13197. Add(' function GetInt: longint;');
  13198. Add(' procedure SetInt(Value: longint);');
  13199. Add(' procedure DoIt;');
  13200. Add(' property IntA: longint read Fx write Fy;');
  13201. Add(' property IntB: longint read GetInt write SetInt;');
  13202. Add(' end;');
  13203. Add('function tobject.getint: longint;');
  13204. Add('begin');
  13205. Add(' result:=fx;');
  13206. Add('end;');
  13207. Add('procedure tobject.setint(value: longint);');
  13208. Add('begin');
  13209. Add(' if value=fy then exit;');
  13210. Add(' fy:=value;');
  13211. Add('end;');
  13212. Add('procedure tobject.doit;');
  13213. Add('begin');
  13214. Add(' IntA:=IntA+1;');
  13215. Add(' Self.IntA:=Self.IntA+1;');
  13216. Add(' IntB:=IntB+1;');
  13217. Add(' Self.IntB:=Self.IntB+1;');
  13218. Add('end;');
  13219. Add('var Obj: tobject;');
  13220. Add('begin');
  13221. Add(' obj.inta:=obj.inta+1;');
  13222. Add(' if obj.intb=2 then;');
  13223. Add(' obj.intb:=obj.intb+2;');
  13224. Add(' obj.setint(obj.inta);');
  13225. ConvertProgram;
  13226. CheckSource('TestClass_Property',
  13227. LinesToStr([ // statements
  13228. 'rtl.createClass(this, "TObject", null, function () {',
  13229. ' this.$init = function () {',
  13230. ' this.Fx = 0;',
  13231. ' this.Fy = 0;',
  13232. ' };',
  13233. ' this.$final = function () {',
  13234. ' };',
  13235. ' this.GetInt = function () {',
  13236. ' var Result = 0;',
  13237. ' Result = this.Fx;',
  13238. ' return Result;',
  13239. ' };',
  13240. ' this.SetInt = function (Value) {',
  13241. ' if (Value === this.Fy) return;',
  13242. ' this.Fy = Value;',
  13243. ' };',
  13244. ' this.DoIt = function () {',
  13245. ' this.Fy = this.Fx + 1;',
  13246. ' this.Fy = this.Fx + 1;',
  13247. ' this.SetInt(this.GetInt() + 1);',
  13248. ' this.SetInt(this.GetInt() + 1);',
  13249. ' };',
  13250. '});',
  13251. 'this.Obj = null;'
  13252. ]),
  13253. LinesToStr([ // $mod.$main
  13254. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13255. 'if ($mod.Obj.GetInt() === 2);',
  13256. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13257. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13258. ]));
  13259. end;
  13260. procedure TTestModule.TestClass_Property_ClassMethod;
  13261. begin
  13262. StartProgram(false);
  13263. Add([
  13264. 'type',
  13265. ' TObject = class',
  13266. ' class var Fx: longint;',
  13267. ' class var Fy: longint;',
  13268. ' class function GetInt: longint;',
  13269. ' class procedure SetInt(Value: longint);',
  13270. ' end;',
  13271. ' TBird = class',
  13272. ' class procedure DoIt;',
  13273. ' class property IntA: longint read Fx write Fy;',
  13274. ' class property IntB: longint read GetInt write SetInt;',
  13275. ' end;',
  13276. 'class function tobject.getint: longint;',
  13277. 'begin',
  13278. ' result:=fx;',
  13279. 'end;',
  13280. 'class procedure tobject.setint(value: longint);',
  13281. 'begin',
  13282. 'end;',
  13283. 'class procedure tbird.doit;',
  13284. 'begin',
  13285. ' FX:=3;',
  13286. ' IntA:=IntA+1;',
  13287. ' Self.IntA:=Self.IntA+1;',
  13288. ' IntB:=IntB+1;',
  13289. ' Self.IntB:=Self.IntB+1;',
  13290. ' with Self do begin',
  13291. ' FX:=11;',
  13292. ' IntA:=IntA+12;',
  13293. ' IntB:=IntB+13;',
  13294. ' end;',
  13295. 'end;',
  13296. 'var Obj: tbird;',
  13297. 'begin',
  13298. ' tbird.fx:=tbird.fx+1;',
  13299. ' tbird.inta:=tbird.inta+1;',
  13300. ' if tbird.intb=2 then;',
  13301. ' tbird.intb:=tbird.intb+2;',
  13302. ' tbird.setint(tbird.inta);',
  13303. ' obj.inta:=obj.inta+1;',
  13304. ' if obj.intb=2 then;',
  13305. ' obj.intb:=obj.intb+2;',
  13306. ' obj.setint(obj.inta);',
  13307. ' with Tbird do begin',
  13308. ' FX:=FY+1;',
  13309. ' inta:=inta+2;',
  13310. ' intb:=intb+3;',
  13311. ' end;',
  13312. ' with Obj do begin',
  13313. ' FX:=FY+1;',
  13314. ' inta:=inta+2;',
  13315. ' intb:=intb+3;',
  13316. ' end;',
  13317. '']);
  13318. ConvertProgram;
  13319. CheckSource('TestClass_Property_ClassMethod',
  13320. LinesToStr([ // statements
  13321. 'rtl.createClass(this, "TObject", null, function () {',
  13322. ' this.Fx = 0;',
  13323. ' this.Fy = 0;',
  13324. ' this.$init = function () {',
  13325. ' };',
  13326. ' this.$final = function () {',
  13327. ' };',
  13328. ' this.GetInt = function () {',
  13329. ' var Result = 0;',
  13330. ' Result = this.Fx;',
  13331. ' return Result;',
  13332. ' };',
  13333. ' this.SetInt = function (Value) {',
  13334. ' };',
  13335. '});',
  13336. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13337. ' this.DoIt = function () {',
  13338. ' $mod.TObject.Fx = 3;',
  13339. ' $mod.TObject.Fy = this.Fx + 1;',
  13340. ' $mod.TObject.Fy = this.Fx + 1;',
  13341. ' this.SetInt(this.GetInt() + 1);',
  13342. ' this.SetInt(this.GetInt() + 1);',
  13343. ' $mod.TObject.Fx = 11;',
  13344. ' $mod.TObject.Fy = this.Fx + 12;',
  13345. ' this.SetInt(this.GetInt() + 13);',
  13346. ' };',
  13347. '});',
  13348. 'this.Obj = null;'
  13349. ]),
  13350. LinesToStr([ // $mod.$main
  13351. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13352. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13353. 'if ($mod.TBird.GetInt() === 2);',
  13354. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13355. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13356. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13357. 'if ($mod.Obj.$class.GetInt() === 2);',
  13358. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13359. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13360. 'var $with = $mod.TBird;',
  13361. '$mod.TObject.Fx = $with.Fy + 1;',
  13362. '$mod.TObject.Fy = $with.Fx + 2;',
  13363. '$with.SetInt($with.GetInt() + 3);',
  13364. 'var $with1 = $mod.Obj;',
  13365. '$mod.TObject.Fx = $with1.Fy + 1;',
  13366. '$mod.TObject.Fy = $with1.Fx + 2;',
  13367. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13368. '']));
  13369. end;
  13370. procedure TTestModule.TestClass_Property_Indexed;
  13371. begin
  13372. StartProgram(false);
  13373. Add([
  13374. 'type',
  13375. ' TObject = class',
  13376. ' FItems: array of longint;',
  13377. ' function GetItems(Index: longint): longint;',
  13378. ' procedure SetItems(Index: longint; Value: longint);',
  13379. ' procedure DoIt;',
  13380. ' property Items[Index: longint]: longint read getitems write setitems;',
  13381. ' end;',
  13382. 'function tobject.getitems(index: longint): longint;',
  13383. 'begin',
  13384. ' Result:=fitems[index];',
  13385. 'end;',
  13386. 'procedure tobject.setitems(index: longint; value: longint);',
  13387. 'begin',
  13388. ' fitems[index]:=value;',
  13389. 'end;',
  13390. 'procedure tobject.doit;',
  13391. 'begin',
  13392. ' items[1]:=2;',
  13393. ' items[3]:=items[4];',
  13394. ' self.items[5]:=self.items[6];',
  13395. ' items[items[7]]:=items[items[8]];',
  13396. 'end;',
  13397. 'var Obj: tobject;',
  13398. 'begin',
  13399. ' obj.Items[11]:=obj.Items[12];',
  13400. '']);
  13401. ConvertProgram;
  13402. CheckSource('TestClass_Property_Indexed',
  13403. LinesToStr([ // statements
  13404. 'rtl.createClass(this, "TObject", null, function () {',
  13405. ' this.$init = function () {',
  13406. ' this.FItems = [];',
  13407. ' };',
  13408. ' this.$final = function () {',
  13409. ' this.FItems = undefined;',
  13410. ' };',
  13411. ' this.GetItems = function (Index) {',
  13412. ' var Result = 0;',
  13413. ' Result = this.FItems[Index];',
  13414. ' return Result;',
  13415. ' };',
  13416. ' this.SetItems = function (Index, Value) {',
  13417. ' this.FItems[Index] = Value;',
  13418. ' };',
  13419. ' this.DoIt = function () {',
  13420. ' this.SetItems(1, 2);',
  13421. ' this.SetItems(3,this.GetItems(4));',
  13422. ' this.SetItems(5,this.GetItems(6));',
  13423. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13424. ' };',
  13425. '});',
  13426. 'this.Obj = null;'
  13427. ]),
  13428. LinesToStr([ // $mod.$main
  13429. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13430. ]));
  13431. end;
  13432. procedure TTestModule.TestClass_Property_IndexSpec;
  13433. begin
  13434. StartProgram(false);
  13435. Add([
  13436. 'type',
  13437. ' TEnum = (red, blue);',
  13438. ' TObject = class',
  13439. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13440. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13441. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13442. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13443. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13444. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13445. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13446. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13447. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13448. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13449. ' end;',
  13450. 'procedure DoIt(b: boolean); begin end;',
  13451. 'var',
  13452. ' o: TObject;',
  13453. 'begin',
  13454. ' o.B1:=o.B1;',
  13455. ' o.B2:=o.B2;',
  13456. ' o.B3:=o.B3;',
  13457. ' o.I1[''a'']:=o.I1[''b''];',
  13458. ' doit(o.b1);',
  13459. ' doit(o.b2);',
  13460. ' doit(o.i1[''c'']);',
  13461. '']);
  13462. ConvertProgram;
  13463. CheckSource('TestClass_Property_IndexSpec',
  13464. LinesToStr([ // statements
  13465. 'this.TEnum = {',
  13466. ' "0": "red",',
  13467. ' red: 0,',
  13468. ' "1": "blue",',
  13469. ' blue: 1',
  13470. '};',
  13471. 'rtl.createClass(this, "TObject", null, function () {',
  13472. ' this.$init = function () {',
  13473. ' };',
  13474. ' this.$final = function () {',
  13475. ' };',
  13476. '});',
  13477. 'this.DoIt = function (b) {',
  13478. '};',
  13479. 'this.o = null;',
  13480. '']),
  13481. LinesToStr([ // $mod.$main
  13482. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13483. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13484. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13485. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13486. '$mod.DoIt($mod.o.GetIntBool(1));',
  13487. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13488. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13489. '']));
  13490. end;
  13491. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13492. begin
  13493. StartProgram(false);
  13494. Add('type');
  13495. Add(' TArray = array of longint;');
  13496. Add(' TObject = class');
  13497. Add(' FItems: TArray;');
  13498. Add(' function GetItems: tarray;');
  13499. Add(' procedure SetItems(Value: tarray);');
  13500. Add(' property Items: tarray read getitems write setitems;');
  13501. Add(' procedure SetNumbers(const Value: tarray);');
  13502. Add(' property Numbers: tarray write setnumbers;');
  13503. Add(' end;');
  13504. Add('function tobject.getitems: tarray;');
  13505. Add('begin');
  13506. Add(' Result:=fitems;');
  13507. Add('end;');
  13508. Add('procedure tobject.setitems(value: tarray);');
  13509. Add('begin');
  13510. Add(' fitems:=value;');
  13511. Add(' fitems:=nil;');
  13512. Add(' Items:=nil;');
  13513. Add(' Items:=Items;');
  13514. Add(' Items[1]:=2;');
  13515. Add(' fitems[3]:=Items[4];');
  13516. Add(' Items[5]:=Items[6];');
  13517. Add(' Self.Items[7]:=8;');
  13518. Add(' Self.Items[9]:=Self.Items[10];');
  13519. Add(' Items[Items[11]]:=Items[Items[12]];');
  13520. Add('end;');
  13521. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13522. Add('begin;');
  13523. Add(' Numbers:=nil;');
  13524. Add(' Numbers:=Value;');
  13525. Add(' Self.Numbers:=Value;');
  13526. Add('end;');
  13527. Add('var Obj: tobject;');
  13528. Add('begin');
  13529. Add(' obj.items:=nil;');
  13530. Add(' obj.items:=obj.items;');
  13531. Add(' obj.items[11]:=obj.items[12];');
  13532. ConvertProgram;
  13533. CheckSource('TestClass_PropertyOfTypeArray',
  13534. LinesToStr([ // statements
  13535. 'rtl.createClass(this, "TObject", null, function () {',
  13536. ' this.$init = function () {',
  13537. ' this.FItems = [];',
  13538. ' };',
  13539. ' this.$final = function () {',
  13540. ' this.FItems = undefined;',
  13541. ' };',
  13542. ' this.GetItems = function () {',
  13543. ' var Result = [];',
  13544. ' Result = rtl.arrayRef(this.FItems);',
  13545. ' return Result;',
  13546. ' };',
  13547. ' this.SetItems = function (Value) {',
  13548. ' this.FItems = rtl.arrayRef(Value);',
  13549. ' this.FItems = [];',
  13550. ' this.SetItems([]);',
  13551. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13552. ' this.GetItems()[1] = 2;',
  13553. ' this.FItems[3] = this.GetItems()[4];',
  13554. ' this.GetItems()[5] = this.GetItems()[6];',
  13555. ' this.GetItems()[7] = 8;',
  13556. ' this.GetItems()[9] = this.GetItems()[10];',
  13557. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13558. ' };',
  13559. ' this.SetNumbers = function (Value) {',
  13560. ' this.SetNumbers([]);',
  13561. ' this.SetNumbers(Value);',
  13562. ' this.SetNumbers(Value);',
  13563. ' };',
  13564. '});',
  13565. 'this.Obj = null;'
  13566. ]),
  13567. LinesToStr([ // $mod.$main
  13568. '$mod.Obj.SetItems([]);',
  13569. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13570. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13571. ]));
  13572. end;
  13573. procedure TTestModule.TestClass_PropertyDefault;
  13574. begin
  13575. StartProgram(false);
  13576. Add([
  13577. 'type',
  13578. ' TArray = array of longint;',
  13579. ' TObject = class',
  13580. ' end;',
  13581. ' TBird = class',
  13582. ' FItems: TArray;',
  13583. ' function GetItems(Index: longint): longint;',
  13584. ' procedure SetItems(Index, Value: longint);',
  13585. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13586. ' end;',
  13587. 'function TBird.getitems(index: longint): longint;',
  13588. 'begin',
  13589. 'end;',
  13590. 'procedure TBird.setitems(index, value: longint);',
  13591. 'begin',
  13592. ' Self[1]:=2;',
  13593. ' Self[3]:=Self[index];',
  13594. ' Self[index]:=Self[Self[value]];',
  13595. ' Self[Self[4]]:=value;',
  13596. 'end;',
  13597. 'var',
  13598. ' Bird: TBird;',
  13599. ' Obj: TObject;',
  13600. 'begin',
  13601. ' bird[11]:=12;',
  13602. ' bird[13]:=bird[14];',
  13603. ' bird[Bird[15]]:=bird[Bird[15]];',
  13604. ' TBird(obj)[16]:=TBird(obj)[17];',
  13605. ' (obj as tbird)[18]:=19;',
  13606. '']);
  13607. ConvertProgram;
  13608. CheckSource('TestClass_PropertyDefault',
  13609. LinesToStr([ // statements
  13610. 'rtl.createClass(this, "TObject", null, function () {',
  13611. ' this.$init = function () {',
  13612. ' };',
  13613. ' this.$final = function () {',
  13614. ' };',
  13615. '});',
  13616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13617. ' this.$init = function () {',
  13618. ' $mod.TObject.$init.call(this);',
  13619. ' this.FItems = [];',
  13620. ' };',
  13621. ' this.$final = function () {',
  13622. ' this.FItems = undefined;',
  13623. ' $mod.TObject.$final.call(this);',
  13624. ' };',
  13625. ' this.GetItems = function (Index) {',
  13626. ' var Result = 0;',
  13627. ' return Result;',
  13628. ' };',
  13629. ' this.SetItems = function (Index, Value) {',
  13630. ' this.SetItems(1, 2);',
  13631. ' this.SetItems(3, this.GetItems(Index));',
  13632. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13633. ' this.SetItems(this.GetItems(4), Value);',
  13634. ' };',
  13635. '});',
  13636. 'this.Bird = null;',
  13637. 'this.Obj = null;',
  13638. '']),
  13639. LinesToStr([ // $mod.$main
  13640. '$mod.Bird.SetItems(11, 12);',
  13641. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13642. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13643. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13644. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13645. '']));
  13646. end;
  13647. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13648. begin
  13649. StartProgram(false);
  13650. Add([
  13651. 'type',
  13652. ' TObject = class end;',
  13653. ' TAlphaList = class',
  13654. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13655. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13656. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13657. ' end;',
  13658. ' TBetaList = class',
  13659. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13660. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13661. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13662. ' end;',
  13663. ' TBird = class',
  13664. ' procedure DoIt;',
  13665. ' end;',
  13666. 'procedure TBird.DoIt;',
  13667. 'var',
  13668. ' List: TAlphaList;',
  13669. 'begin',
  13670. ' if TBetaList(List[true])[3]=nil then ;',
  13671. ' TBetaList(List[false])[5]:=nil;',
  13672. 'end;',
  13673. 'var',
  13674. ' List: TAlphaList;',
  13675. 'begin',
  13676. ' if TBetaList(List[true])[3]=nil then ;',
  13677. ' TBetaList(List[false])[5]:=nil;',
  13678. '']);
  13679. ConvertProgram;
  13680. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13681. LinesToStr([ // statements
  13682. 'rtl.createClass(this, "TObject", null, function () {',
  13683. ' this.$init = function () {',
  13684. ' };',
  13685. ' this.$final = function () {',
  13686. ' };',
  13687. '});',
  13688. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13689. '});',
  13690. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13691. '});',
  13692. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13693. ' this.DoIt = function () {',
  13694. ' var List = null;',
  13695. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13696. ' List.GetAlphas(false).SetBetas(5, null);',
  13697. ' };',
  13698. '});',
  13699. 'this.List = null;',
  13700. '']),
  13701. LinesToStr([ // $mod.$main
  13702. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13703. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13704. '']));
  13705. end;
  13706. procedure TTestModule.TestClass_PropertyOverride;
  13707. begin
  13708. StartProgram(false);
  13709. Add('type');
  13710. Add(' integer = longint;');
  13711. Add(' TObject = class');
  13712. Add(' FItem: integer;');
  13713. Add(' function GetItem: integer; external name ''GetItem'';');
  13714. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13715. Add(' property Item: integer read getitem write setitem;');
  13716. Add(' end;');
  13717. Add(' TCar = class');
  13718. Add(' FBag: integer;');
  13719. Add(' function GetBag: integer; external name ''GetBag'';');
  13720. Add(' property Item read getbag;');
  13721. Add(' end;');
  13722. Add('var');
  13723. Add(' Obj: tobject;');
  13724. Add(' Car: tcar;');
  13725. Add('begin');
  13726. Add(' Obj.Item:=Obj.Item;');
  13727. Add(' Car.Item:=Car.Item;');
  13728. ConvertProgram;
  13729. CheckSource('TestClass_PropertyOverride',
  13730. LinesToStr([ // statements
  13731. 'rtl.createClass(this, "TObject", null, function () {',
  13732. ' this.$init = function () {',
  13733. ' this.FItem = 0;',
  13734. ' };',
  13735. ' this.$final = function () {',
  13736. ' };',
  13737. '});',
  13738. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13739. ' this.$init = function () {',
  13740. ' $mod.TObject.$init.call(this);',
  13741. ' this.FBag = 0;',
  13742. ' };',
  13743. '});',
  13744. 'this.Obj = null;',
  13745. 'this.Car = null;',
  13746. '']),
  13747. LinesToStr([ // $mod.$main
  13748. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13749. '$mod.Car.SetItem($mod.Car.GetBag());',
  13750. '']));
  13751. end;
  13752. procedure TTestModule.TestClass_PropertyIncVisibility;
  13753. begin
  13754. AddModuleWithIntfImplSrc('unit1.pp',
  13755. LinesToStr([
  13756. 'type',
  13757. ' TNumber = longint;',
  13758. ' TInteger = longint;',
  13759. ' TObject = class',
  13760. ' private',
  13761. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13762. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13763. ' protected',
  13764. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13765. ' end;']),
  13766. LinesToStr([
  13767. '']));
  13768. StartProgram(true);
  13769. Add([
  13770. 'uses unit1;',
  13771. 'type',
  13772. ' TBird = class',
  13773. ' public',
  13774. ' property Items;',
  13775. ' end;',
  13776. 'procedure DoIt(i: TInteger);',
  13777. 'begin',
  13778. 'end;',
  13779. 'var b: TBird;',
  13780. 'begin',
  13781. ' b.Items[1]:=2;',
  13782. ' b.Items[3]:=b.Items[4];',
  13783. ' DoIt(b.Items[5]);',
  13784. '']);
  13785. ConvertProgram;
  13786. CheckSource('TestClass_PropertyIncVisibility',
  13787. LinesToStr([ // statements
  13788. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13789. '});',
  13790. 'this.DoIt = function (i) {',
  13791. '};',
  13792. 'this.b = null;'
  13793. ]),
  13794. LinesToStr([ // $mod.$main
  13795. '$mod.b.SetItems(1, 2);',
  13796. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13797. '$mod.DoIt($mod.b.GetItems(5));'
  13798. ]));
  13799. end;
  13800. procedure TTestModule.TestClass_Assigned;
  13801. begin
  13802. StartProgram(false);
  13803. Add('type');
  13804. Add(' TObject = class');
  13805. Add(' end;');
  13806. Add('var');
  13807. Add(' Obj: tobject;');
  13808. Add(' b: boolean;');
  13809. Add('begin');
  13810. Add(' if Assigned(obj) then ;');
  13811. Add(' b:=Assigned(obj) or false;');
  13812. ConvertProgram;
  13813. CheckSource('TestClass_Assigned',
  13814. LinesToStr([ // statements
  13815. 'rtl.createClass(this, "TObject", null, function () {',
  13816. ' this.$init = function () {',
  13817. ' };',
  13818. ' this.$final = function () {',
  13819. ' };',
  13820. '});',
  13821. 'this.Obj = null;',
  13822. 'this.b = false;'
  13823. ]),
  13824. LinesToStr([ // $mod.$main
  13825. 'if ($mod.Obj != null);',
  13826. '$mod.b = ($mod.Obj != null) || false;'
  13827. ]));
  13828. end;
  13829. procedure TTestModule.TestClass_WithClassDoCreate;
  13830. begin
  13831. StartProgram(false);
  13832. Add('type');
  13833. Add(' TObject = class');
  13834. Add(' aBool: boolean;');
  13835. Add(' Arr: array of boolean;');
  13836. Add(' constructor Create;');
  13837. Add(' end;');
  13838. Add('constructor TObject.Create; begin end;');
  13839. Add('var');
  13840. Add(' Obj: tobject;');
  13841. Add(' b: boolean;');
  13842. Add('begin');
  13843. Add(' with tobject.create do begin');
  13844. Add(' b:=abool;');
  13845. Add(' abool:=b;');
  13846. Add(' b:=arr[1];');
  13847. Add(' arr[2]:=b;');
  13848. Add(' end;');
  13849. Add(' with tobject do');
  13850. Add(' obj:=create;');
  13851. Add(' with obj do begin');
  13852. Add(' create;');
  13853. Add(' b:=abool;');
  13854. Add(' abool:=b;');
  13855. Add(' b:=arr[3];');
  13856. Add(' arr[4]:=b;');
  13857. Add(' end;');
  13858. ConvertProgram;
  13859. CheckSource('TestClass_WithClassDoCreate',
  13860. LinesToStr([ // statements
  13861. 'rtl.createClass(this, "TObject", null, function () {',
  13862. ' this.$init = function () {',
  13863. ' this.aBool = false;',
  13864. ' this.Arr = [];',
  13865. ' };',
  13866. ' this.$final = function () {',
  13867. ' this.Arr = undefined;',
  13868. ' };',
  13869. ' this.Create = function () {',
  13870. ' return this;',
  13871. ' };',
  13872. '});',
  13873. 'this.Obj = null;',
  13874. 'this.b = false;'
  13875. ]),
  13876. LinesToStr([ // $mod.$main
  13877. 'var $with = $mod.TObject.$create("Create");',
  13878. '$mod.b = $with.aBool;',
  13879. '$with.aBool = $mod.b;',
  13880. '$mod.b = $with.Arr[1];',
  13881. '$with.Arr[2] = $mod.b;',
  13882. 'var $with1 = $mod.TObject;',
  13883. '$mod.Obj = $with1.$create("Create");',
  13884. 'var $with2 = $mod.Obj;',
  13885. '$with2.Create();',
  13886. '$mod.b = $with2.aBool;',
  13887. '$with2.aBool = $mod.b;',
  13888. '$mod.b = $with2.Arr[3];',
  13889. '$with2.Arr[4] = $mod.b;',
  13890. '']));
  13891. end;
  13892. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13893. begin
  13894. StartProgram(false);
  13895. Add('type');
  13896. Add(' TObject = class');
  13897. Add(' FInt: longint;');
  13898. Add(' constructor Create;');
  13899. Add(' function GetSize: longint;');
  13900. Add(' procedure SetSize(Value: longint);');
  13901. Add(' property Int: longint read FInt write FInt;');
  13902. Add(' property Size: longint read GetSize write SetSize;');
  13903. Add(' end;');
  13904. Add('constructor TObject.Create; begin end;');
  13905. Add('function TObject.GetSize: longint; begin; end;');
  13906. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13907. Add('var');
  13908. Add(' Obj: tobject;');
  13909. Add(' i: longint;');
  13910. Add('begin');
  13911. Add(' with TObject.Create do begin');
  13912. Add(' i:=int;');
  13913. Add(' int:=i;');
  13914. Add(' i:=size;');
  13915. Add(' size:=i;');
  13916. Add(' end;');
  13917. Add(' with obj do begin');
  13918. Add(' i:=int;');
  13919. Add(' int:=i;');
  13920. Add(' i:=size;');
  13921. Add(' size:=i;');
  13922. Add(' end;');
  13923. ConvertProgram;
  13924. CheckSource('TestClass_WithClassInstDoProperty',
  13925. LinesToStr([ // statements
  13926. 'rtl.createClass(this, "TObject", null, function () {',
  13927. ' this.$init = function () {',
  13928. ' this.FInt = 0;',
  13929. ' };',
  13930. ' this.$final = function () {',
  13931. ' };',
  13932. ' this.Create = function () {',
  13933. ' return this;',
  13934. ' };',
  13935. ' this.GetSize = function () {',
  13936. ' var Result = 0;',
  13937. ' return Result;',
  13938. ' };',
  13939. ' this.SetSize = function (Value) {',
  13940. ' };',
  13941. '});',
  13942. 'this.Obj = null;',
  13943. 'this.i = 0;'
  13944. ]),
  13945. LinesToStr([ // $mod.$main
  13946. 'var $with = $mod.TObject.$create("Create");',
  13947. '$mod.i = $with.FInt;',
  13948. '$with.FInt = $mod.i;',
  13949. '$mod.i = $with.GetSize();',
  13950. '$with.SetSize($mod.i);',
  13951. 'var $with1 = $mod.Obj;',
  13952. '$mod.i = $with1.FInt;',
  13953. '$with1.FInt = $mod.i;',
  13954. '$mod.i = $with1.GetSize();',
  13955. '$with1.SetSize($mod.i);',
  13956. '']));
  13957. end;
  13958. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13959. begin
  13960. StartProgram(false);
  13961. Add('type');
  13962. Add(' TObject = class');
  13963. Add(' constructor Create;');
  13964. Add(' function GetItems(Index: longint): longint;');
  13965. Add(' procedure SetItems(Index, Value: longint);');
  13966. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13967. Add(' end;');
  13968. Add('constructor TObject.Create; begin end;');
  13969. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13970. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13971. Add('var');
  13972. Add(' Obj: tobject;');
  13973. Add(' i: longint;');
  13974. Add('begin');
  13975. Add(' with TObject.Create do begin');
  13976. Add(' i:=Items[1];');
  13977. Add(' Items[2]:=i;');
  13978. Add(' end;');
  13979. Add(' with obj do begin');
  13980. Add(' i:=Items[3];');
  13981. Add(' Items[4]:=i;');
  13982. Add(' end;');
  13983. ConvertProgram;
  13984. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13985. LinesToStr([ // statements
  13986. 'rtl.createClass(this, "TObject", null, function () {',
  13987. ' this.$init = function () {',
  13988. ' };',
  13989. ' this.$final = function () {',
  13990. ' };',
  13991. ' this.Create = function () {',
  13992. ' return this;',
  13993. ' };',
  13994. ' this.GetItems = function (Index) {',
  13995. ' var Result = 0;',
  13996. ' return Result;',
  13997. ' };',
  13998. ' this.SetItems = function (Index, Value) {',
  13999. ' };',
  14000. '});',
  14001. 'this.Obj = null;',
  14002. 'this.i = 0;'
  14003. ]),
  14004. LinesToStr([ // $mod.$main
  14005. 'var $with = $mod.TObject.$create("Create");',
  14006. '$mod.i = $with.GetItems(1);',
  14007. '$with.SetItems(2, $mod.i);',
  14008. 'var $with1 = $mod.Obj;',
  14009. '$mod.i = $with1.GetItems(3);',
  14010. '$with1.SetItems(4, $mod.i);',
  14011. '']));
  14012. end;
  14013. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14014. begin
  14015. StartProgram(false);
  14016. Add('type');
  14017. Add(' TObject = class');
  14018. Add(' constructor Create;');
  14019. Add(' function GetSize: longint;');
  14020. Add(' procedure SetSize(Value: longint);');
  14021. Add(' end;');
  14022. Add('constructor TObject.Create; begin end;');
  14023. Add('function TObject.GetSize: longint; begin; end;');
  14024. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14025. Add('var');
  14026. Add(' Obj: tobject;');
  14027. Add(' i: longint;');
  14028. Add('begin');
  14029. Add(' with TObject.Create do begin');
  14030. Add(' i:=GetSize;');
  14031. Add(' i:=GetSize();');
  14032. Add(' SetSize(i);');
  14033. Add(' end;');
  14034. Add(' with obj do begin');
  14035. Add(' i:=GetSize;');
  14036. Add(' i:=GetSize();');
  14037. Add(' SetSize(i);');
  14038. Add(' end;');
  14039. ConvertProgram;
  14040. CheckSource('TestClass_WithClassInstDoFunc',
  14041. LinesToStr([ // statements
  14042. 'rtl.createClass(this, "TObject", null, function () {',
  14043. ' this.$init = function () {',
  14044. ' };',
  14045. ' this.$final = function () {',
  14046. ' };',
  14047. ' this.Create = function () {',
  14048. ' return this;',
  14049. ' };',
  14050. ' this.GetSize = function () {',
  14051. ' var Result = 0;',
  14052. ' return Result;',
  14053. ' };',
  14054. ' this.SetSize = function (Value) {',
  14055. ' };',
  14056. '});',
  14057. 'this.Obj = null;',
  14058. 'this.i = 0;'
  14059. ]),
  14060. LinesToStr([ // $mod.$main
  14061. 'var $with = $mod.TObject.$create("Create");',
  14062. '$mod.i = $with.GetSize();',
  14063. '$mod.i = $with.GetSize();',
  14064. '$with.SetSize($mod.i);',
  14065. 'var $with1 = $mod.Obj;',
  14066. '$mod.i = $with1.GetSize();',
  14067. '$mod.i = $with1.GetSize();',
  14068. '$with1.SetSize($mod.i);',
  14069. '']));
  14070. end;
  14071. procedure TTestModule.TestClass_TypeCast;
  14072. begin
  14073. StartProgram(false);
  14074. Add('type');
  14075. Add(' TObject = class');
  14076. Add(' Next: TObject;');
  14077. Add(' constructor Create;');
  14078. Add(' end;');
  14079. Add(' TControl = class(TObject)');
  14080. Add(' Arr: array of TObject;');
  14081. Add(' function GetIt(vI: longint = 0): TObject;');
  14082. Add(' end;');
  14083. Add('constructor tobject.create; begin end;');
  14084. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14085. Add('var');
  14086. Add(' Obj: tobject;');
  14087. Add('begin');
  14088. Add(' obj:=tcontrol(obj).next;');
  14089. Add(' tcontrol(obj):=nil;');
  14090. Add(' obj:=tcontrol(obj);');
  14091. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14092. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14093. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14094. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14095. Add(' obj:=tcontrol(nil);');
  14096. ConvertProgram;
  14097. CheckSource('TestClass_TypeCast',
  14098. LinesToStr([ // statements
  14099. 'rtl.createClass(this, "TObject", null, function () {',
  14100. ' this.$init = function () {',
  14101. ' this.Next = null;',
  14102. ' };',
  14103. ' this.$final = function () {',
  14104. ' this.Next = undefined;',
  14105. ' };',
  14106. ' this.Create = function () {',
  14107. ' return this;',
  14108. ' };',
  14109. '});',
  14110. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14111. ' this.$init = function () {',
  14112. ' $mod.TObject.$init.call(this);',
  14113. ' this.Arr = [];',
  14114. ' };',
  14115. ' this.$final = function () {',
  14116. ' this.Arr = undefined;',
  14117. ' $mod.TObject.$final.call(this);',
  14118. ' };',
  14119. ' this.GetIt = function (vI) {',
  14120. ' var Result = null;',
  14121. ' return Result;',
  14122. ' };',
  14123. '});',
  14124. 'this.Obj = null;'
  14125. ]),
  14126. LinesToStr([ // $mod.$main
  14127. '$mod.Obj = $mod.Obj.Next;',
  14128. '$mod.Obj = null;',
  14129. '$mod.Obj = $mod.Obj;',
  14130. '$mod.Obj = $mod.Obj.GetIt(0);',
  14131. '$mod.Obj = $mod.Obj.GetIt(0);',
  14132. '$mod.Obj = $mod.Obj.GetIt(1);',
  14133. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14134. '$mod.Obj = null;',
  14135. '']));
  14136. end;
  14137. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14138. begin
  14139. StartProgram(false);
  14140. Add('type');
  14141. Add(' TObject = class end;');
  14142. Add('procedure ProcA(var A);');
  14143. Add('begin');
  14144. Add(' TObject(A):=nil;');
  14145. Add(' TObject(A):=TObject(A);');
  14146. Add(' if TObject(A)=nil then ;');
  14147. Add(' if nil=TObject(A) then ;');
  14148. Add('end;');
  14149. Add('procedure ProcB(out A);');
  14150. Add('begin');
  14151. Add(' TObject(A):=nil;');
  14152. Add(' TObject(A):=TObject(A);');
  14153. Add(' if TObject(A)=nil then ;');
  14154. Add(' if nil=TObject(A) then ;');
  14155. Add('end;');
  14156. Add('procedure ProcC(const A);');
  14157. Add('begin');
  14158. Add(' if TObject(A)=nil then ;');
  14159. Add(' if nil=TObject(A) then ;');
  14160. Add('end;');
  14161. Add('var o: TObject;');
  14162. Add('begin');
  14163. Add(' ProcA(o);');
  14164. Add(' ProcB(o);');
  14165. Add(' ProcC(o);');
  14166. ConvertProgram;
  14167. CheckSource('TestClass_TypeCastUntypedParam',
  14168. LinesToStr([ // statements
  14169. 'rtl.createClass(this, "TObject", null, function () {',
  14170. ' this.$init = function () {',
  14171. ' };',
  14172. ' this.$final = function () {',
  14173. ' };',
  14174. '});',
  14175. 'this.ProcA = function (A) {',
  14176. ' A.set(null);',
  14177. ' A.set(A.get());',
  14178. ' if (A.get() === null);',
  14179. ' if (null === A.get());',
  14180. '};',
  14181. 'this.ProcB = function (A) {',
  14182. ' A.set(null);',
  14183. ' A.set(A.get());',
  14184. ' if (A.get() === null);',
  14185. ' if (null === A.get());',
  14186. '};',
  14187. 'this.ProcC = function (A) {',
  14188. ' if (A === null);',
  14189. ' if (null === A);',
  14190. '};',
  14191. 'this.o = null;',
  14192. '']),
  14193. LinesToStr([ // $mod.$main
  14194. '$mod.ProcA({',
  14195. ' p: $mod,',
  14196. ' get: function () {',
  14197. ' return this.p.o;',
  14198. ' },',
  14199. ' set: function (v) {',
  14200. ' this.p.o = v;',
  14201. ' }',
  14202. '});',
  14203. '$mod.ProcB({',
  14204. ' p: $mod,',
  14205. ' get: function () {',
  14206. ' return this.p.o;',
  14207. ' },',
  14208. ' set: function (v) {',
  14209. ' this.p.o = v;',
  14210. ' }',
  14211. '});',
  14212. '$mod.ProcC($mod.o);',
  14213. '']));
  14214. end;
  14215. procedure TTestModule.TestClass_Overloads;
  14216. begin
  14217. StartProgram(false);
  14218. Add('type');
  14219. Add(' TObject = class');
  14220. Add(' procedure DoIt;');
  14221. Add(' procedure DoIt(vI: longint);');
  14222. Add(' end;');
  14223. Add('procedure TObject.DoIt;');
  14224. Add('begin');
  14225. Add(' DoIt;');
  14226. Add(' DoIt(1);');
  14227. Add('end;');
  14228. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14229. Add('begin');
  14230. ConvertProgram;
  14231. CheckSource('TestClass_Overloads',
  14232. LinesToStr([ // statements
  14233. 'rtl.createClass(this, "TObject", null, function () {',
  14234. ' this.$init = function () {',
  14235. ' };',
  14236. ' this.$final = function () {',
  14237. ' };',
  14238. ' this.DoIt = function () {',
  14239. ' this.DoIt();',
  14240. ' this.DoIt$1(1);',
  14241. ' };',
  14242. ' this.DoIt$1 = function (vI) {',
  14243. ' };',
  14244. '});',
  14245. '']),
  14246. LinesToStr([ // $mod.$main
  14247. '']));
  14248. end;
  14249. procedure TTestModule.TestClass_OverloadsAncestor;
  14250. begin
  14251. StartProgram(false);
  14252. Add('type');
  14253. Add(' TObject = class;');
  14254. Add(' TObject = class');
  14255. Add(' procedure DoIt(vA: longint);');
  14256. Add(' procedure DoIt(vA, vB: longint);');
  14257. Add(' end;');
  14258. Add(' TCar = class;');
  14259. Add(' TCar = class');
  14260. Add(' procedure DoIt(vA: longint);');
  14261. Add(' procedure DoIt(vA, vB: longint);');
  14262. Add(' end;');
  14263. Add('procedure tobject.doit(va: longint);');
  14264. Add('begin');
  14265. Add(' doit(1);');
  14266. Add(' doit(1,2);');
  14267. Add('end;');
  14268. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14269. Add('procedure tcar.doit(va: longint);');
  14270. Add('begin');
  14271. Add(' doit(1);');
  14272. Add(' doit(1,2);');
  14273. Add(' inherited doit(1);');
  14274. Add(' inherited doit(1,2);');
  14275. Add('end;');
  14276. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14277. Add('begin');
  14278. ConvertProgram;
  14279. CheckSource('TestClass_OverloadsAncestor',
  14280. LinesToStr([ // statements
  14281. 'rtl.createClass(this, "TObject", null, function () {',
  14282. ' this.$init = function () {',
  14283. ' };',
  14284. ' this.$final = function () {',
  14285. ' };',
  14286. ' this.DoIt = function (vA) {',
  14287. ' this.DoIt(1);',
  14288. ' this.DoIt$1(1,2);',
  14289. ' };',
  14290. ' this.DoIt$1 = function (vA, vB) {',
  14291. ' };',
  14292. '});',
  14293. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14294. ' this.DoIt$2 = function (vA) {',
  14295. ' this.DoIt$2(1);',
  14296. ' this.DoIt$3(1, 2);',
  14297. ' $mod.TObject.DoIt.call(this, 1);',
  14298. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14299. ' };',
  14300. ' this.DoIt$3 = function (vA, vB) {',
  14301. ' };',
  14302. '});',
  14303. '']),
  14304. LinesToStr([ // $mod.$main
  14305. '']));
  14306. end;
  14307. procedure TTestModule.TestClass_OverloadConstructor;
  14308. begin
  14309. StartProgram(false);
  14310. Add('type');
  14311. Add(' TObject = class');
  14312. Add(' constructor Create(vA: longint);');
  14313. Add(' constructor Create(vA, vB: longint);');
  14314. Add(' end;');
  14315. Add(' TCar = class');
  14316. Add(' constructor Create(vA: longint);');
  14317. Add(' constructor Create(vA, vB: longint);');
  14318. Add(' end;');
  14319. Add('constructor tobject.create(va: longint);');
  14320. Add('begin');
  14321. Add(' create(1);');
  14322. Add(' create(1,2);');
  14323. Add('end;');
  14324. Add('constructor tobject.create(va, vb: longint); begin end;');
  14325. Add('constructor tcar.create(va: longint);');
  14326. Add('begin');
  14327. Add(' create(1);');
  14328. Add(' create(1,2);');
  14329. Add(' inherited create(1);');
  14330. Add(' inherited create(1,2);');
  14331. Add('end;');
  14332. Add('constructor tcar.create(va, vb: longint); begin end;');
  14333. Add('begin');
  14334. Add(' tobject.create(1);');
  14335. Add(' tobject.create(1,2);');
  14336. Add(' tcar.create(1);');
  14337. Add(' tcar.create(1,2);');
  14338. ConvertProgram;
  14339. CheckSource('TestClass_OverloadConstructor',
  14340. LinesToStr([ // statements
  14341. 'rtl.createClass(this, "TObject", null, function () {',
  14342. ' this.$init = function () {',
  14343. ' };',
  14344. ' this.$final = function () {',
  14345. ' };',
  14346. ' this.Create = function (vA) {',
  14347. ' this.Create(1);',
  14348. ' this.Create$1(1,2);',
  14349. ' return this;',
  14350. ' };',
  14351. ' this.Create$1 = function (vA, vB) {',
  14352. ' return this;',
  14353. ' };',
  14354. '});',
  14355. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14356. ' this.Create$2 = function (vA) {',
  14357. ' this.Create$2(1);',
  14358. ' this.Create$3(1, 2);',
  14359. ' $mod.TObject.Create.call(this, 1);',
  14360. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14361. ' return this;',
  14362. ' };',
  14363. ' this.Create$3 = function (vA, vB) {',
  14364. ' return this;',
  14365. ' };',
  14366. '});',
  14367. '']),
  14368. LinesToStr([ // $mod.$main
  14369. '$mod.TObject.$create("Create", [1]);',
  14370. '$mod.TObject.$create("Create$1", [1, 2]);',
  14371. '$mod.TCar.$create("Create$2", [1]);',
  14372. '$mod.TCar.$create("Create$3", [1, 2]);',
  14373. '']));
  14374. end;
  14375. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14376. begin
  14377. StartProgram(false);
  14378. Add([
  14379. '{$mode delphi}',
  14380. 'type',
  14381. ' TObject = class end;',
  14382. ' TBird = class',
  14383. ' function {#a}GetValue: longint; overload; virtual;',
  14384. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14385. ' end;',
  14386. ' TEagle = class(TBird)',
  14387. ' function {#c}GetValue: longint; overload; override;',
  14388. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14389. ' end;',
  14390. 'function TBird.GetValue: longint;',
  14391. 'begin',
  14392. ' if 3={@a}GetValue then ;',
  14393. ' if 4={@b}GetValue(5) then ;',
  14394. 'end;',
  14395. 'function TBird.GetValue(AValue: longint): longint;',
  14396. 'begin',
  14397. 'end;',
  14398. 'function TEagle.GetValue: longint;',
  14399. 'begin',
  14400. ' if 13={@c}GetValue then ;',
  14401. ' if 14={@d}GetValue(15) then ;',
  14402. ' if 15=inherited {@a}GetValue then ;',
  14403. ' if 16=inherited {@b}GetValue(17) then ;',
  14404. 'end;',
  14405. 'function TEagle.GetValue(AValue: longint): longint;',
  14406. 'begin',
  14407. 'end;',
  14408. 'var',
  14409. ' e: TEagle;',
  14410. 'begin',
  14411. ' if 23=e.{@c}GetValue then ;',
  14412. ' if 24=e.{@d}GetValue(25) then ;']);
  14413. ConvertProgram;
  14414. CheckSource('TestClass_OverloadDelphiOverride',
  14415. LinesToStr([ // statements
  14416. 'rtl.createClass(this, "TObject", null, function () {',
  14417. ' this.$init = function () {',
  14418. ' };',
  14419. ' this.$final = function () {',
  14420. ' };',
  14421. '});',
  14422. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14423. ' this.GetValue = function () {',
  14424. ' var Result = 0;',
  14425. ' if (3 === this.GetValue()) ;',
  14426. ' if (4 === this.GetValue$1(5)) ;',
  14427. ' return Result;',
  14428. ' };',
  14429. ' this.GetValue$1 = function (AValue) {',
  14430. ' var Result = 0;',
  14431. ' return Result;',
  14432. ' };',
  14433. '});',
  14434. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14435. ' this.GetValue = function () {',
  14436. ' var Result = 0;',
  14437. ' if (13 === this.GetValue()) ;',
  14438. ' if (14 === this.GetValue$1(15)) ;',
  14439. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14440. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14441. ' return Result;',
  14442. ' };',
  14443. ' this.GetValue$1 = function (AValue) {',
  14444. ' var Result = 0;',
  14445. ' return Result;',
  14446. ' };',
  14447. '});',
  14448. 'this.e = null;',
  14449. '']),
  14450. LinesToStr([ // $mod.$main
  14451. 'if (23 === $mod.e.GetValue()) ;',
  14452. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14453. '']));
  14454. end;
  14455. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14456. begin
  14457. StartProgram(false);
  14458. Add([
  14459. '{$mode delphi}',
  14460. 'type',
  14461. ' TObject = class end;',
  14462. ' TAnimal = class',
  14463. ' public',
  14464. ' {#animal_a}A: longint;',
  14465. ' function {#animal_b}B: longint;',
  14466. ' end;',
  14467. ' TBird = class(TAnimal)',
  14468. ' public',
  14469. ' {#bird_a}A: double;',
  14470. ' {#bird_b}B: boolean;',
  14471. ' end;',
  14472. ' TEagle = class(TBird)',
  14473. ' public',
  14474. ' function {#eagle_a}A: boolean;',
  14475. ' {#eagle_b}B: double;',
  14476. ' end;',
  14477. 'function TAnimal.B: longint;',
  14478. 'begin',
  14479. 'end;',
  14480. 'function TEagle.A: boolean;',
  14481. 'begin',
  14482. ' {@eagle_b}B:=3.3;',
  14483. ' {@eagle_a}A();',
  14484. ' TBird(Self).{@bird_b}B:=true;',
  14485. ' TAnimal(Self).{@animal_a}A:=17;',
  14486. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14487. 'end;',
  14488. 'var',
  14489. ' e: TEagle;',
  14490. 'begin',
  14491. ' e.{@eagle_b}B:=5.3;',
  14492. ' if e.{@eagle_a}A then ;',
  14493. '']);
  14494. ConvertProgram;
  14495. CheckSource('TestClass_ReintroduceVarDelphi',
  14496. LinesToStr([ // statements
  14497. 'rtl.createClass(this, "TObject", null, function () {',
  14498. ' this.$init = function () {',
  14499. ' };',
  14500. ' this.$final = function () {',
  14501. ' };',
  14502. '});',
  14503. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14504. ' this.$init = function () {',
  14505. ' $mod.TObject.$init.call(this);',
  14506. ' this.A = 0;',
  14507. ' };',
  14508. ' this.B = function () {',
  14509. ' var Result = 0;',
  14510. ' return Result;',
  14511. ' };',
  14512. '});',
  14513. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14514. ' this.$init = function () {',
  14515. ' $mod.TAnimal.$init.call(this);',
  14516. ' this.A$1 = 0.0;',
  14517. ' this.B$1 = false;',
  14518. ' };',
  14519. '});',
  14520. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14521. ' this.$init = function () {',
  14522. ' $mod.TBird.$init.call(this);',
  14523. ' this.B$2 = 0.0;',
  14524. ' };',
  14525. ' this.A$2 = function () {',
  14526. ' var Result = false;',
  14527. ' this.B$2 = 3.3;',
  14528. ' this.A$2();',
  14529. ' this.B$1 = true;',
  14530. ' this.A = 17;',
  14531. ' this.B$1 = this.A$1 > 1;',
  14532. ' return Result;',
  14533. ' };',
  14534. '});',
  14535. 'this.e = null;',
  14536. '']),
  14537. LinesToStr([ // $mod.$main
  14538. '$mod.e.B$2 = 5.3;',
  14539. 'if ($mod.e.A$2()) ;',
  14540. '']));
  14541. end;
  14542. procedure TTestModule.TestClass_ReintroducedVar;
  14543. begin
  14544. StartProgram(false);
  14545. Add('type');
  14546. Add(' TObject = class');
  14547. Add(' strict private');
  14548. Add(' Some: longint;');
  14549. Add(' end;');
  14550. Add(' TMobile = class');
  14551. Add(' strict private');
  14552. Add(' Some: string;');
  14553. Add(' end;');
  14554. Add(' TCar = class(tmobile)');
  14555. Add(' procedure Some;');
  14556. Add(' procedure Some(vA: longint);');
  14557. Add(' end;');
  14558. Add('procedure tcar.some;');
  14559. Add('begin');
  14560. Add(' Some;');
  14561. Add(' Some(1);');
  14562. Add('end;');
  14563. Add('procedure tcar.some(va: longint); begin end;');
  14564. Add('begin');
  14565. ConvertProgram;
  14566. CheckSource('TestClass_ReintroducedVar',
  14567. LinesToStr([ // statements
  14568. 'rtl.createClass(this, "TObject", null, function () {',
  14569. ' this.$init = function () {',
  14570. ' this.Some = 0;',
  14571. ' };',
  14572. ' this.$final = function () {',
  14573. ' };',
  14574. '});',
  14575. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14576. ' this.$init = function () {',
  14577. ' $mod.TObject.$init.call(this);',
  14578. ' this.Some$1 = "";',
  14579. ' };',
  14580. '});',
  14581. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14582. ' this.Some$2 = function () {',
  14583. ' this.Some$2();',
  14584. ' this.Some$3(1);',
  14585. ' };',
  14586. ' this.Some$3 = function (vA) {',
  14587. ' };',
  14588. '});',
  14589. '']),
  14590. LinesToStr([ // $mod.$main
  14591. '']));
  14592. end;
  14593. procedure TTestModule.TestClass_RaiseDescendant;
  14594. begin
  14595. StartProgram(false);
  14596. Add([
  14597. 'type',
  14598. ' TObject = class',
  14599. ' constructor Create(Msg: string);',
  14600. ' end;',
  14601. ' Exception = class',
  14602. ' end;',
  14603. ' EConvertError = class(Exception)',
  14604. ' end;',
  14605. 'constructor TObject.Create(Msg: string); begin end;',
  14606. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14607. 'begin',
  14608. ' raise Exception.Create(''Bar1'');',
  14609. ' raise EConvertError.Create(''Bar2'');',
  14610. ' raise AssertConv(''Bar2'');',
  14611. ' raise AssertConv;',
  14612. '']);
  14613. ConvertProgram;
  14614. CheckSource('TestClass_RaiseDescendant',
  14615. LinesToStr([ // statements
  14616. 'rtl.createClass(this, "TObject", null, function () {',
  14617. ' this.$init = function () {',
  14618. ' };',
  14619. ' this.$final = function () {',
  14620. ' };',
  14621. ' this.Create = function (Msg) {',
  14622. ' return this;',
  14623. ' };',
  14624. '});',
  14625. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14626. '});',
  14627. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14628. '});',
  14629. 'this.AssertConv = function (Msg) {',
  14630. ' var Result = null;',
  14631. ' return Result;',
  14632. '};',
  14633. '']),
  14634. LinesToStr([ // $mod.$main
  14635. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14636. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14637. 'throw $mod.AssertConv("Bar2");',
  14638. 'throw $mod.AssertConv("def");',
  14639. '']));
  14640. end;
  14641. procedure TTestModule.TestClass_ExternalMethod;
  14642. begin
  14643. AddModuleWithIntfImplSrc('unit2.pas',
  14644. LinesToStr([
  14645. 'type',
  14646. ' TObject = class',
  14647. ' public',
  14648. ' procedure Intern; external name ''$DoIntern'';',
  14649. ' end;',
  14650. '']),
  14651. LinesToStr([
  14652. '']));
  14653. StartUnit(true);
  14654. Add('interface');
  14655. Add('uses unit2;');
  14656. Add('type');
  14657. Add(' TCar = class(TObject)');
  14658. Add(' public');
  14659. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14660. Add(' procedure DoIt;');
  14661. Add(' end;');
  14662. Add('implementation');
  14663. Add('procedure tcar.doit;');
  14664. Add('begin');
  14665. Add(' Intern;');
  14666. Add(' Intern();');
  14667. Add(' Intern2;');
  14668. Add(' Intern2();');
  14669. Add('end;');
  14670. Add('var Obj: TCar;');
  14671. Add('begin');
  14672. Add(' obj.intern;');
  14673. Add(' obj.intern();');
  14674. Add(' obj.intern2;');
  14675. Add(' obj.intern2();');
  14676. Add(' obj.doit;');
  14677. Add(' obj.doit();');
  14678. Add(' with obj do begin');
  14679. Add(' Intern;');
  14680. Add(' Intern();');
  14681. Add(' Intern2;');
  14682. Add(' Intern2();');
  14683. Add(' end;');
  14684. ConvertUnit;
  14685. CheckSource('TestClass_ExternalMethod',
  14686. LinesToStr([
  14687. 'var $impl = $mod.$impl;',
  14688. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14689. ' this.DoIt = function () {',
  14690. ' this.$DoIntern();',
  14691. ' this.$DoIntern();',
  14692. ' this.$DoIntern2();',
  14693. ' this.$DoIntern2();',
  14694. ' };',
  14695. ' });',
  14696. '']),
  14697. LinesToStr([ // this.$init
  14698. '$impl.Obj.$DoIntern();',
  14699. '$impl.Obj.$DoIntern();',
  14700. '$impl.Obj.$DoIntern2();',
  14701. '$impl.Obj.$DoIntern2();',
  14702. '$impl.Obj.DoIt();',
  14703. '$impl.Obj.DoIt();',
  14704. 'var $with = $impl.Obj;',
  14705. '$with.$DoIntern();',
  14706. '$with.$DoIntern();',
  14707. '$with.$DoIntern2();',
  14708. '$with.$DoIntern2();',
  14709. '']),
  14710. LinesToStr([ // implementation
  14711. '$impl.Obj = null;',
  14712. '']) );
  14713. end;
  14714. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14715. begin
  14716. StartProgram(false);
  14717. Add('type');
  14718. Add(' TObject = class');
  14719. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14720. Add(' end;');
  14721. Add('begin');
  14722. SetExpectedPasResolverError('Virtual method name must match external',
  14723. nVirtualMethodNameMustMatchExternal);
  14724. ConvertProgram;
  14725. end;
  14726. procedure TTestModule.TestClass_ExternalOverrideFail;
  14727. begin
  14728. StartProgram(false);
  14729. Add('type');
  14730. Add(' TObject = class');
  14731. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14732. Add(' end;');
  14733. Add(' TCar = class');
  14734. Add(' procedure DoIt; override; external name ''DoIt'';');
  14735. Add(' end;');
  14736. Add('begin');
  14737. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14738. nInvalidXModifierY);
  14739. ConvertProgram;
  14740. end;
  14741. procedure TTestModule.TestClass_ExternalVar;
  14742. begin
  14743. AddModuleWithIntfImplSrc('unit2.pas',
  14744. LinesToStr([
  14745. '{$modeswitch externalclass}',
  14746. 'type',
  14747. ' TObject = class',
  14748. ' public',
  14749. ' Intern: longint external name ''$Intern'';',
  14750. ' Bracket: longint external name ''["A B"]'';',
  14751. ' end;',
  14752. '']),
  14753. LinesToStr([
  14754. '']));
  14755. StartUnit(true);
  14756. Add([
  14757. 'interface',
  14758. 'uses unit2;',
  14759. '{$modeswitch externalclass}',
  14760. 'type',
  14761. ' TCar = class(tobject)',
  14762. ' public',
  14763. ' Intern2: longint external name ''$Intern2'';',
  14764. ' procedure DoIt;',
  14765. ' end;',
  14766. 'implementation',
  14767. 'procedure tcar.doit;',
  14768. 'begin',
  14769. ' Intern:=Intern+1;',
  14770. ' Intern2:=Intern2+2;',
  14771. ' Bracket:=Bracket+3;',
  14772. 'end;',
  14773. 'var Obj: TCar;',
  14774. 'begin',
  14775. ' obj.intern:=obj.intern+1;',
  14776. ' obj.intern2:=obj.intern2+2;',
  14777. ' obj.Bracket:=obj.Bracket+3;',
  14778. ' with obj do begin',
  14779. ' intern:=intern+1;',
  14780. ' intern2:=intern2+2;',
  14781. ' Bracket:=Bracket+3;',
  14782. ' end;']);
  14783. ConvertUnit;
  14784. CheckSource('TestClass_ExternalVar',
  14785. LinesToStr([
  14786. 'var $impl = $mod.$impl;',
  14787. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14788. ' this.DoIt = function () {',
  14789. ' this.$Intern = this.$Intern + 1;',
  14790. ' this.$Intern2 = this.$Intern2 + 2;',
  14791. ' this["A B"] = this["A B"] + 3;',
  14792. ' };',
  14793. ' });',
  14794. '']),
  14795. LinesToStr([
  14796. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14797. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14798. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14799. 'var $with = $impl.Obj;',
  14800. '$with.$Intern = $with.$Intern + 1;',
  14801. '$with.$Intern2 = $with.$Intern2 + 2;',
  14802. '$with["A B"] = $with["A B"] + 3;',
  14803. '']),
  14804. LinesToStr([ // implementation
  14805. '$impl.Obj = null;',
  14806. '']));
  14807. end;
  14808. procedure TTestModule.TestClass_Const;
  14809. begin
  14810. StartProgram(false);
  14811. Add([
  14812. 'type',
  14813. ' integer = longint;',
  14814. ' TClass = class of TObject;',
  14815. ' TObject = class',
  14816. ' public',
  14817. ' const cI: integer = 3;',
  14818. ' procedure DoIt;',
  14819. ' class procedure DoMore;',
  14820. ' end;',
  14821. 'procedure tobject.doit;',
  14822. 'begin',
  14823. ' if cI=4 then;',
  14824. ' if 5=cI then;',
  14825. ' if Self.cI=6 then;',
  14826. ' if 7=Self.cI then;',
  14827. ' with Self do begin',
  14828. ' if cI=11 then;',
  14829. ' if 12=cI then;',
  14830. ' end;',
  14831. 'end;',
  14832. 'class procedure tobject.domore;',
  14833. 'begin',
  14834. ' if cI=8 then;',
  14835. ' if Self.cI=9 then;',
  14836. ' if 10=cI then;',
  14837. ' if 11=Self.cI then;',
  14838. ' with Self do begin',
  14839. ' if cI=13 then;',
  14840. ' if 14=cI then;',
  14841. ' end;',
  14842. 'end;',
  14843. 'var',
  14844. ' Obj: TObject;',
  14845. ' Cla: TClass;',
  14846. 'begin',
  14847. ' if TObject.cI=21 then ;',
  14848. ' if Obj.cI=22 then ;',
  14849. ' if Cla.cI=23 then ;',
  14850. ' with obj do if ci=24 then;',
  14851. ' with TObject do if ci=25 then;',
  14852. ' with Cla do if ci=26 then;']);
  14853. ConvertProgram;
  14854. CheckSource('TestClass_Const',
  14855. LinesToStr([
  14856. 'rtl.createClass(this, "TObject", null, function () {',
  14857. ' this.cI = 3;',
  14858. ' this.$init = function () {',
  14859. ' };',
  14860. ' this.$final = function () {',
  14861. ' };',
  14862. ' this.DoIt = function () {',
  14863. ' if (this.cI === 4) ;',
  14864. ' if (5 === this.cI) ;',
  14865. ' if (this.cI === 6) ;',
  14866. ' if (7 === this.cI) ;',
  14867. ' if (this.cI === 11) ;',
  14868. ' if (12 === this.cI) ;',
  14869. ' };',
  14870. ' this.DoMore = function () {',
  14871. ' if (this.cI === 8) ;',
  14872. ' if (this.cI === 9) ;',
  14873. ' if (10 === this.cI) ;',
  14874. ' if (11 === this.cI) ;',
  14875. ' if (this.cI === 13) ;',
  14876. ' if (14 === this.cI) ;',
  14877. ' };',
  14878. '});',
  14879. 'this.Obj = null;',
  14880. 'this.Cla = null;',
  14881. '']),
  14882. LinesToStr([
  14883. 'if ($mod.TObject.cI === 21) ;',
  14884. 'if ($mod.Obj.cI === 22) ;',
  14885. 'if ($mod.Cla.cI === 23) ;',
  14886. 'var $with = $mod.Obj;',
  14887. 'if ($with.cI === 24) ;',
  14888. 'var $with1 = $mod.TObject;',
  14889. 'if ($with1.cI === 25) ;',
  14890. 'var $with2 = $mod.Cla;',
  14891. 'if ($with2.cI === 26) ;',
  14892. '']));
  14893. end;
  14894. procedure TTestModule.TestClass_ConstEnum;
  14895. begin
  14896. StartProgram(false);
  14897. Add([
  14898. 'type',
  14899. ' TEnum = (red,blue);',
  14900. ' TObject = class',
  14901. ' end;',
  14902. ' TAnimal = class',
  14903. ' public',
  14904. ' type TSubEnum = (light,dark);',
  14905. ' const a = high(TEnum);',
  14906. ' const b = high(TSubEnum);',
  14907. ' end;',
  14908. ' TBird = class(TAnimal)',
  14909. ' public',
  14910. ' const c = high(TEnum);',
  14911. ' const d = high(TSubEnum);',
  14912. ' end;',
  14913. ' TAnt = class',
  14914. ' public',
  14915. ' const e = high(TEnum);',
  14916. ' const f = high(TBird.TSubEnum);',
  14917. ' end;',
  14918. 'begin',
  14919. '']);
  14920. ConvertProgram;
  14921. CheckSource('TestClass_ConstEnum',
  14922. LinesToStr([
  14923. 'this.TEnum = {',
  14924. ' "0": "red",',
  14925. ' red: 0,',
  14926. ' "1": "blue",',
  14927. ' blue: 1',
  14928. '};',
  14929. 'rtl.createClass(this, "TObject", null, function () {',
  14930. ' this.$init = function () {',
  14931. ' };',
  14932. ' this.$final = function () {',
  14933. ' };',
  14934. '});',
  14935. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14936. ' this.TSubEnum = {',
  14937. ' "0": "light",',
  14938. ' light: 0,',
  14939. ' "1": "dark",',
  14940. ' dark: 1',
  14941. ' };',
  14942. ' this.a = $mod.TEnum.blue;',
  14943. ' this.b = this.TSubEnum.dark;',
  14944. '});',
  14945. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14946. ' this.c = $mod.TEnum.blue;',
  14947. ' this.d = this.TSubEnum.dark;',
  14948. '});',
  14949. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14950. ' this.e = $mod.TEnum.blue;',
  14951. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14952. '});',
  14953. '']),
  14954. LinesToStr([
  14955. '']));
  14956. end;
  14957. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14958. begin
  14959. StartProgram(false);
  14960. Add([
  14961. 'type',
  14962. ' TObject = class',
  14963. ' const cI: longint = 3;',
  14964. ' procedure Fly;',
  14965. ' procedure Run;',
  14966. ' end;',
  14967. ' TBird = class',
  14968. ' procedure Go;',
  14969. ' end;',
  14970. 'procedure tobject.fly;',
  14971. 'const cI: word = 4;',
  14972. 'begin',
  14973. ' if cI=Self.cI then ;',
  14974. 'end;',
  14975. 'procedure tobject.run;',
  14976. 'const cI: word = 5;',
  14977. 'begin',
  14978. ' if cI=Self.cI then ;',
  14979. 'end;',
  14980. 'procedure tbird.go;',
  14981. 'const cI: word = 6;',
  14982. 'begin',
  14983. ' if cI=Self.cI then ;',
  14984. 'end;',
  14985. 'begin',
  14986. '']);
  14987. ConvertProgram;
  14988. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14989. LinesToStr([
  14990. 'rtl.createClass(this, "TObject", null, function () {',
  14991. ' this.cI = 3;',
  14992. ' this.$init = function () {',
  14993. ' };',
  14994. ' this.$final = function () {',
  14995. ' };',
  14996. ' var cI$1 = 4;',
  14997. ' this.Fly = function () {',
  14998. ' if (cI$1 === this.cI) ;',
  14999. ' };',
  15000. ' var cI$2 = 5;',
  15001. ' this.Run = function () {',
  15002. ' if (cI$2 === this.cI) ;',
  15003. ' };',
  15004. '});',
  15005. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15006. ' var cI$3 = 6;',
  15007. ' this.Go = function () {',
  15008. ' if (cI$3 === this.cI) ;',
  15009. ' };',
  15010. '});',
  15011. '']),
  15012. LinesToStr([
  15013. '']));
  15014. end;
  15015. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15016. begin
  15017. StartUnit(false);
  15018. Add([
  15019. 'interface',
  15020. 'type',
  15021. ' TObject = class',
  15022. ' const cI: longint = 3;',
  15023. ' procedure Fly;',
  15024. ' procedure Run;',
  15025. ' end;',
  15026. ' TBird = class',
  15027. ' procedure Go;',
  15028. ' end;',
  15029. 'implementation',
  15030. 'procedure tobject.fly;',
  15031. 'const cI: word = 4;',
  15032. 'begin',
  15033. ' if cI=Self.cI then ;',
  15034. 'end;',
  15035. 'procedure tobject.run;',
  15036. 'const cI: word = 5;',
  15037. 'begin',
  15038. ' if cI=Self.cI then ;',
  15039. 'end;',
  15040. 'procedure tbird.go;',
  15041. 'const cI: word = 6;',
  15042. 'begin',
  15043. ' if cI=Self.cI then ;',
  15044. 'end;',
  15045. '']);
  15046. ConvertUnit;
  15047. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15048. LinesToStr([
  15049. 'rtl.createClass(this, "TObject", null, function () {',
  15050. ' this.cI = 3;',
  15051. ' this.$init = function () {',
  15052. ' };',
  15053. ' this.$final = function () {',
  15054. ' };',
  15055. ' var cI$1 = 4;',
  15056. ' this.Fly = function () {',
  15057. ' if (cI$1 === this.cI) ;',
  15058. ' };',
  15059. ' var cI$2 = 5;',
  15060. ' this.Run = function () {',
  15061. ' if (cI$2 === this.cI) ;',
  15062. ' };',
  15063. '});',
  15064. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15065. ' var cI$3 = 6;',
  15066. ' this.Go = function () {',
  15067. ' if (cI$3 === this.cI) ;',
  15068. ' };',
  15069. '});',
  15070. '']),
  15071. '',
  15072. '');
  15073. end;
  15074. procedure TTestModule.TestClass_LocalVarSelfFail;
  15075. begin
  15076. StartProgram(false);
  15077. Add([
  15078. 'type',
  15079. ' TObject = class',
  15080. ' constructor Create;',
  15081. ' end;',
  15082. 'constructor tobject.create;',
  15083. 'var self: longint;',
  15084. 'begin',
  15085. 'end',
  15086. 'begin',
  15087. '']);
  15088. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15089. ConvertProgram;
  15090. end;
  15091. procedure TTestModule.TestClass_ArgSelfFail;
  15092. begin
  15093. StartProgram(false);
  15094. Add([
  15095. 'type',
  15096. ' TObject = class',
  15097. ' procedure DoIt(Self: longint);',
  15098. ' end;',
  15099. 'procedure tobject.doit(self: longint);',
  15100. 'begin',
  15101. 'end',
  15102. 'begin',
  15103. '']);
  15104. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15105. ConvertProgram;
  15106. end;
  15107. procedure TTestModule.TestClass_NestedProcSelf;
  15108. begin
  15109. StartProgram(false);
  15110. Add([
  15111. 'type',
  15112. ' TObject = class',
  15113. ' Key: longint;',
  15114. ' class var State: longint;',
  15115. ' procedure DoIt;',
  15116. ' function GetSize: longint; virtual; abstract;',
  15117. ' procedure SetSize(Value: longint); virtual; abstract;',
  15118. ' property Size: longint read GetSize write SetSize;',
  15119. ' end;',
  15120. 'procedure tobject.doit;',
  15121. ' procedure Sub;',
  15122. ' begin',
  15123. ' key:=key+2;',
  15124. ' self.key:=self.key+3;',
  15125. ' state:=state+4;',
  15126. ' self.state:=self.state+5;',
  15127. ' tobject.state:=tobject.state+6;',
  15128. ' size:=size+7;',
  15129. ' self.size:=self.size+8;',
  15130. ' end;',
  15131. 'begin',
  15132. ' sub;',
  15133. ' key:=key+12;',
  15134. ' self.key:=self.key+13;',
  15135. ' state:=state+14;',
  15136. ' self.state:=self.state+15;',
  15137. ' tobject.state:=tobject.state+16;',
  15138. ' size:=size+17;',
  15139. ' self.size:=self.size+18;',
  15140. 'end;',
  15141. 'begin',
  15142. '']);
  15143. ConvertProgram;
  15144. CheckSource('TestClass_NestedProcSelf',
  15145. LinesToStr([ // statements
  15146. 'rtl.createClass(this, "TObject", null, function () {',
  15147. ' this.State = 0;',
  15148. ' this.$init = function () {',
  15149. ' this.Key = 0;',
  15150. ' };',
  15151. ' this.$final = function () {',
  15152. ' };',
  15153. ' this.DoIt = function () {',
  15154. ' var $Self = this;',
  15155. ' function Sub() {',
  15156. ' $Self.Key = $Self.Key + 2;',
  15157. ' $Self.Key = $Self.Key + 3;',
  15158. ' $mod.TObject.State = $Self.State + 4;',
  15159. ' $mod.TObject.State = $Self.State + 5;',
  15160. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15161. ' $Self.SetSize($Self.GetSize() + 7);',
  15162. ' $Self.SetSize($Self.GetSize() + 8);',
  15163. ' };',
  15164. ' Sub();',
  15165. ' this.Key = this.Key + 12;',
  15166. ' $Self.Key = $Self.Key + 13;',
  15167. ' $mod.TObject.State = this.State + 14;',
  15168. ' $mod.TObject.State = $Self.State + 15;',
  15169. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15170. ' this.SetSize(this.GetSize() + 17);',
  15171. ' $Self.SetSize($Self.GetSize() + 18);',
  15172. ' };',
  15173. '});',
  15174. '']),
  15175. LinesToStr([ // $mod.$main
  15176. '']));
  15177. end;
  15178. procedure TTestModule.TestClass_NestedProcSelf2;
  15179. begin
  15180. StartProgram(false);
  15181. Add([
  15182. 'type',
  15183. ' TObject = class',
  15184. ' Key: longint;',
  15185. ' class var State: longint;',
  15186. ' function GetSize: longint; virtual; abstract;',
  15187. ' procedure SetSize(Value: longint); virtual; abstract;',
  15188. ' property Size: longint read GetSize write SetSize;',
  15189. ' end;',
  15190. ' TBird = class',
  15191. ' procedure DoIt;',
  15192. ' end;',
  15193. 'procedure tbird.doit;',
  15194. ' procedure Sub;',
  15195. ' begin',
  15196. ' key:=key+2;',
  15197. ' self.key:=self.key+3;',
  15198. ' state:=state+4;',
  15199. ' self.state:=self.state+5;',
  15200. ' tobject.state:=tobject.state+6;',
  15201. ' size:=size+7;',
  15202. ' self.size:=self.size+8;',
  15203. ' end;',
  15204. 'begin',
  15205. ' sub;',
  15206. ' key:=key+12;',
  15207. ' self.key:=self.key+13;',
  15208. ' state:=state+14;',
  15209. ' self.state:=self.state+15;',
  15210. ' tobject.state:=tobject.state+16;',
  15211. ' size:=size+17;',
  15212. ' self.size:=self.size+18;',
  15213. 'end;',
  15214. 'begin',
  15215. '']);
  15216. ConvertProgram;
  15217. CheckSource('TestClass_NestedProcSelf2',
  15218. LinesToStr([ // statements
  15219. 'rtl.createClass(this, "TObject", null, function () {',
  15220. ' this.State = 0;',
  15221. ' this.$init = function () {',
  15222. ' this.Key = 0;',
  15223. ' };',
  15224. ' this.$final = function () {',
  15225. ' };',
  15226. '});',
  15227. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15228. ' this.DoIt = function () {',
  15229. ' var $Self = this;',
  15230. ' function Sub() {',
  15231. ' $Self.Key = $Self.Key + 2;',
  15232. ' $Self.Key = $Self.Key + 3;',
  15233. ' $mod.TObject.State = $Self.State + 4;',
  15234. ' $mod.TObject.State = $Self.State + 5;',
  15235. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15236. ' $Self.SetSize($Self.GetSize() + 7);',
  15237. ' $Self.SetSize($Self.GetSize() + 8);',
  15238. ' };',
  15239. ' Sub();',
  15240. ' this.Key = this.Key + 12;',
  15241. ' $Self.Key = $Self.Key + 13;',
  15242. ' $mod.TObject.State = this.State + 14;',
  15243. ' $mod.TObject.State = $Self.State + 15;',
  15244. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15245. ' this.SetSize(this.GetSize() + 17);',
  15246. ' $Self.SetSize($Self.GetSize() + 18);',
  15247. ' };',
  15248. '});',
  15249. '']),
  15250. LinesToStr([ // $mod.$main
  15251. '']));
  15252. end;
  15253. procedure TTestModule.TestClass_NestedProcClassSelf;
  15254. begin
  15255. StartProgram(false);
  15256. Add([
  15257. 'type',
  15258. ' TObject = class',
  15259. ' class var State: longint;',
  15260. ' class procedure DoIt;',
  15261. ' class function GetSize: longint; virtual; abstract;',
  15262. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15263. ' class property Size: longint read GetSize write SetSize;',
  15264. ' end;',
  15265. 'class procedure tobject.doit;',
  15266. ' procedure Sub;',
  15267. ' begin',
  15268. ' state:=state+2;',
  15269. ' self.state:=self.state+3;',
  15270. ' tobject.state:=tobject.state+4;',
  15271. ' size:=size+5;',
  15272. ' self.size:=self.size+6;',
  15273. ' tobject.size:=tobject.size+7;',
  15274. ' end;',
  15275. 'begin',
  15276. ' sub;',
  15277. ' state:=state+12;',
  15278. ' self.state:=self.state+13;',
  15279. ' tobject.state:=tobject.state+14;',
  15280. ' size:=size+15;',
  15281. ' self.size:=self.size+16;',
  15282. ' tobject.size:=tobject.size+17;',
  15283. 'end;',
  15284. 'begin',
  15285. '']);
  15286. ConvertProgram;
  15287. CheckSource('TestClass_NestedProcClassSelf',
  15288. LinesToStr([ // statements
  15289. 'rtl.createClass(this, "TObject", null, function () {',
  15290. ' this.State = 0;',
  15291. ' this.$init = function () {',
  15292. ' };',
  15293. ' this.$final = function () {',
  15294. ' };',
  15295. ' this.DoIt = function () {',
  15296. ' var $Self = this;',
  15297. ' function Sub() {',
  15298. ' $mod.TObject.State = $Self.State + 2;',
  15299. ' $mod.TObject.State = $Self.State + 3;',
  15300. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15301. ' $Self.SetSize($Self.GetSize() + 5);',
  15302. ' $Self.SetSize($Self.GetSize() + 6);',
  15303. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15304. ' };',
  15305. ' Sub();',
  15306. ' $mod.TObject.State = this.State + 12;',
  15307. ' $mod.TObject.State = $Self.State + 13;',
  15308. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15309. ' this.SetSize(this.GetSize() + 15);',
  15310. ' $Self.SetSize($Self.GetSize() + 16);',
  15311. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15312. ' };',
  15313. '});',
  15314. '']),
  15315. LinesToStr([ // $mod.$main
  15316. '']));
  15317. end;
  15318. procedure TTestModule.TestClass_NestedProcCallInherited;
  15319. begin
  15320. StartProgram(false);
  15321. Add([
  15322. 'type',
  15323. ' TObject = class',
  15324. ' function DoIt(k: boolean): longint; virtual;',
  15325. ' end;',
  15326. ' TBird = class',
  15327. ' function DoIt(k: boolean): longint; override;',
  15328. ' end;',
  15329. 'function tobject.doit(k: boolean): longint;',
  15330. 'begin',
  15331. 'end;',
  15332. 'function tbird.doit(k: boolean): longint;',
  15333. ' procedure Sub;',
  15334. ' begin',
  15335. ' inherited DoIt(true);',
  15336. //' if inherited DoIt(false)=4 then ;',
  15337. ' end;',
  15338. 'begin',
  15339. ' Sub;',
  15340. ' inherited;',
  15341. ' inherited DoIt(true);',
  15342. //' if inherited DoIt(false)=14 then ;',
  15343. 'end;',
  15344. 'begin',
  15345. '']);
  15346. ConvertProgram;
  15347. CheckSource('TestClass_NestedProcCallInherited',
  15348. LinesToStr([ // statements
  15349. 'rtl.createClass(this, "TObject", null, function () {',
  15350. ' this.$init = function () {',
  15351. ' };',
  15352. ' this.$final = function () {',
  15353. ' };',
  15354. ' this.DoIt = function (k) {',
  15355. ' var Result = 0;',
  15356. ' return Result;',
  15357. ' };',
  15358. '});',
  15359. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15360. ' this.DoIt = function (k) {',
  15361. ' var $Self = this;',
  15362. ' var Result = 0;',
  15363. ' function Sub() {',
  15364. ' $mod.TObject.DoIt.call($Self, true);',
  15365. ' };',
  15366. ' Sub();',
  15367. ' $mod.TObject.DoIt.apply(this, arguments);',
  15368. ' $mod.TObject.DoIt.call(this, true);',
  15369. ' return Result;',
  15370. ' };',
  15371. '});',
  15372. '']),
  15373. LinesToStr([ // $mod.$main
  15374. '']));
  15375. end;
  15376. procedure TTestModule.TestClass_TObjectFree;
  15377. begin
  15378. StartProgram(false);
  15379. Add([
  15380. 'type',
  15381. ' TObject = class',
  15382. ' Obj: tobject;',
  15383. ' procedure Free;',
  15384. ' procedure Release;',
  15385. ' end;',
  15386. 'procedure tobject.free;',
  15387. 'begin',
  15388. 'end;',
  15389. 'procedure tobject.release;',
  15390. 'begin',
  15391. ' free;',
  15392. ' if true then free;',
  15393. 'end;',
  15394. 'function DoIt(o: tobject): tobject;',
  15395. 'var l: tobject;',
  15396. 'begin',
  15397. ' o.free;',
  15398. ' o.free();',
  15399. ' l.free;',
  15400. ' l.free();',
  15401. ' o.obj.free;',
  15402. ' o.obj.free();',
  15403. ' with o do obj.free;',
  15404. ' with o do obj.free();',
  15405. ' result.Free;',
  15406. ' result.Free();',
  15407. 'end;',
  15408. 'var o: tobject;',
  15409. ' a: array of tobject;',
  15410. 'begin',
  15411. ' o.free;',
  15412. ' o.obj.free;',
  15413. ' a[1+2].free;',
  15414. '']);
  15415. ConvertProgram;
  15416. CheckSource('TestClass_TObjectFree',
  15417. LinesToStr([ // statements
  15418. 'rtl.createClass(this, "TObject", null, function () {',
  15419. ' this.$init = function () {',
  15420. ' this.Obj = null;',
  15421. ' };',
  15422. ' this.$final = function () {',
  15423. ' this.Obj = undefined;',
  15424. ' };',
  15425. ' this.Free = function () {',
  15426. ' };',
  15427. ' this.Release = function () {',
  15428. ' this.Free();',
  15429. ' if (true) this.Free();',
  15430. ' };',
  15431. '});',
  15432. 'this.DoIt = function (o) {',
  15433. ' var Result = null;',
  15434. ' var l = null;',
  15435. ' o = rtl.freeLoc(o);',
  15436. ' o = rtl.freeLoc(o);',
  15437. ' l = rtl.freeLoc(l);',
  15438. ' l = rtl.freeLoc(l);',
  15439. ' rtl.free(o, "Obj");',
  15440. ' rtl.free(o, "Obj");',
  15441. ' rtl.free(o, "Obj");',
  15442. ' rtl.free(o, "Obj");',
  15443. ' Result = rtl.freeLoc(Result);',
  15444. ' Result = rtl.freeLoc(Result);',
  15445. ' return Result;',
  15446. '};',
  15447. 'this.o = null;',
  15448. 'this.a = [];',
  15449. '']),
  15450. LinesToStr([ // $mod.$main
  15451. 'rtl.free($mod, "o");',
  15452. 'rtl.free($mod.o, "Obj");',
  15453. 'rtl.free($mod.a, 1 + 2);',
  15454. '']));
  15455. end;
  15456. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15457. begin
  15458. StartProgram(false);
  15459. Add([
  15460. 'type',
  15461. ' TObject = class',
  15462. ' Obj: tobject;',
  15463. ' procedure Free;',
  15464. ' end;',
  15465. 'procedure tobject.free;',
  15466. 'begin',
  15467. 'end;',
  15468. 'procedure DoIt(var o: tobject);',
  15469. 'begin',
  15470. ' o.free;',
  15471. ' o.free();',
  15472. 'end;',
  15473. 'begin',
  15474. '']);
  15475. ConvertProgram;
  15476. CheckSource('TestClass_TObjectFree_VarArg',
  15477. LinesToStr([ // statements
  15478. 'rtl.createClass(this, "TObject", null, function () {',
  15479. ' this.$init = function () {',
  15480. ' this.Obj = null;',
  15481. ' };',
  15482. ' this.$final = function () {',
  15483. ' this.Obj = undefined;',
  15484. ' };',
  15485. ' this.Free = function () {',
  15486. ' };',
  15487. '});',
  15488. 'this.DoIt = function (o) {',
  15489. ' o.set(rtl.freeLoc(o.get()));',
  15490. ' o.set(rtl.freeLoc(o.get()));',
  15491. '};',
  15492. '']),
  15493. LinesToStr([ // $mod.$main
  15494. '']));
  15495. end;
  15496. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15497. begin
  15498. StartProgram(false);
  15499. Add([
  15500. 'type',
  15501. ' TObject = class',
  15502. ' constructor Create;',
  15503. ' procedure Free;',
  15504. ' end;',
  15505. 'constructor TObject.Create; begin end;',
  15506. 'procedure tobject.free; begin end;',
  15507. 'begin',
  15508. ' with tobject.create do free;',
  15509. '']);
  15510. ConvertProgram;
  15511. CheckSource('TestClass_TObjectFreeNewInstance',
  15512. LinesToStr([ // statements
  15513. 'rtl.createClass(this, "TObject", null, function () {',
  15514. ' this.$init = function () {',
  15515. ' };',
  15516. ' this.$final = function () {',
  15517. ' };',
  15518. ' this.Create = function () {',
  15519. ' return this;',
  15520. ' };',
  15521. ' this.Free = function () {',
  15522. ' };',
  15523. '});',
  15524. '']),
  15525. LinesToStr([ // $mod.$main
  15526. 'var $with = $mod.TObject.$create("Create");',
  15527. '$with=rtl.freeLoc($with);',
  15528. '']));
  15529. end;
  15530. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15531. begin
  15532. StartProgram(false);
  15533. Add([
  15534. 'type',
  15535. ' TObject = class',
  15536. ' destructor Destroy;',
  15537. ' procedure Free;',
  15538. ' end;',
  15539. 'destructor TObject.Destroy; begin end;',
  15540. 'procedure tobject.free; begin end;',
  15541. 'var o: tobject;',
  15542. 'begin',
  15543. ' o.free;',
  15544. '']);
  15545. Converter.UseLowerCase:=true;
  15546. ConvertProgram;
  15547. CheckSource('TestClass_TObjectFreeLowerCase',
  15548. LinesToStr([ // statements
  15549. 'rtl.createClass(this, "tobject", null, function () {',
  15550. ' this.$init = function () {',
  15551. ' };',
  15552. ' this.$final = function () {',
  15553. ' };',
  15554. ' rtl.tObjectDestroy = "destroy";',
  15555. ' this.destroy = function () {',
  15556. ' };',
  15557. ' this.free = function () {',
  15558. ' };',
  15559. '});',
  15560. 'this.o = null;',
  15561. '']),
  15562. LinesToStr([ // $mod.$main
  15563. 'rtl.free($mod, "o");',
  15564. '']));
  15565. end;
  15566. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15567. begin
  15568. StartProgram(false);
  15569. Add([
  15570. 'type',
  15571. ' TObject = class',
  15572. ' procedure Free;',
  15573. ' function GetObj: tobject; virtual; abstract;',
  15574. ' end;',
  15575. 'procedure tobject.free;',
  15576. 'begin',
  15577. 'end;',
  15578. 'var o: tobject;',
  15579. 'begin',
  15580. ' o.getobj.free;',
  15581. '']);
  15582. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15583. ConvertProgram;
  15584. end;
  15585. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15586. begin
  15587. StartProgram(false);
  15588. Add([
  15589. 'type',
  15590. ' TObject = class',
  15591. ' procedure Free;',
  15592. ' FObj: TObject;',
  15593. ' property Obj: tobject read FObj write FObj;',
  15594. ' end;',
  15595. 'procedure tobject.free;',
  15596. 'begin',
  15597. 'end;',
  15598. 'var o: tobject;',
  15599. 'begin',
  15600. ' o.obj.free;',
  15601. '']);
  15602. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15603. ConvertProgram;
  15604. end;
  15605. procedure TTestModule.TestClass_ForIn;
  15606. begin
  15607. StartProgram(false);
  15608. Add([
  15609. 'type',
  15610. ' TObject = class end;',
  15611. ' TItem = TObject;',
  15612. ' TEnumerator = class',
  15613. ' FCurrent: TItem;',
  15614. ' property Current: TItem read FCurrent;',
  15615. ' function MoveNext: boolean;',
  15616. ' end;',
  15617. ' TBird = class',
  15618. ' function GetEnumerator: TEnumerator;',
  15619. ' end;',
  15620. 'function TEnumerator.MoveNext: boolean;',
  15621. 'begin',
  15622. 'end;',
  15623. 'function TBird.GetEnumerator: TEnumerator;',
  15624. 'begin',
  15625. 'end;',
  15626. 'var',
  15627. ' b: TBird;',
  15628. ' i, i2: TItem;',
  15629. 'begin',
  15630. ' for i in b do i2:=i;']);
  15631. ConvertProgram;
  15632. CheckSource('TestClass_ForIn',
  15633. LinesToStr([ // statements
  15634. 'rtl.createClass(this, "TObject", null, function () {',
  15635. ' this.$init = function () {',
  15636. ' };',
  15637. ' this.$final = function () {',
  15638. ' };',
  15639. '});',
  15640. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15641. ' this.$init = function () {',
  15642. ' $mod.TObject.$init.call(this);',
  15643. ' this.FCurrent = null;',
  15644. ' };',
  15645. ' this.$final = function () {',
  15646. ' this.FCurrent = undefined;',
  15647. ' $mod.TObject.$final.call(this);',
  15648. ' };',
  15649. ' this.MoveNext = function () {',
  15650. ' var Result = false;',
  15651. ' return Result;',
  15652. ' };',
  15653. '});',
  15654. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15655. ' this.GetEnumerator = function () {',
  15656. ' var Result = null;',
  15657. ' return Result;',
  15658. ' };',
  15659. '});',
  15660. 'this.b = null;',
  15661. 'this.i = null;',
  15662. 'this.i2 = null;'
  15663. ]),
  15664. LinesToStr([ // $mod.$main
  15665. 'var $in = $mod.b.GetEnumerator();',
  15666. 'try {',
  15667. ' while ($in.MoveNext()){',
  15668. ' $mod.i = $in.FCurrent;',
  15669. ' $mod.i2 = $mod.i;',
  15670. ' }',
  15671. '} finally {',
  15672. ' $in = rtl.freeLoc($in)',
  15673. '};',
  15674. '']));
  15675. end;
  15676. procedure TTestModule.TestClass_DispatchMessage;
  15677. begin
  15678. StartProgram(false);
  15679. Add([
  15680. 'type',
  15681. ' TObject = class',
  15682. ' {$DispatchField DispInt}',
  15683. ' procedure Dispatch(var Msg); virtual; abstract;',
  15684. ' {$DispatchStrField DispStr}',
  15685. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15686. ' end;',
  15687. ' THopMsg = record',
  15688. ' DispInt: longint;',
  15689. ' end;',
  15690. ' TPutMsg = record',
  15691. ' DispStr: string;',
  15692. ' end;',
  15693. ' TBird = class',
  15694. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15695. ' procedure Run; overload; virtual; abstract;',
  15696. ' procedure Run(var Msg); overload; message ''Fast'';',
  15697. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15698. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15699. ' end;',
  15700. 'procedure TBird.Run(var Msg);',
  15701. 'begin',
  15702. 'end;',
  15703. 'begin',
  15704. '']);
  15705. ConvertProgram;
  15706. CheckSource('TestClass_Message',
  15707. LinesToStr([ // statements
  15708. 'rtl.createClass(this, "TObject", null, function () {',
  15709. ' this.$init = function () {',
  15710. ' };',
  15711. ' this.$final = function () {',
  15712. ' };',
  15713. '});',
  15714. 'rtl.recNewT(this, "THopMsg", function () {',
  15715. ' this.DispInt = 0;',
  15716. ' this.$eq = function (b) {',
  15717. ' return this.DispInt === b.DispInt;',
  15718. ' };',
  15719. ' this.$assign = function (s) {',
  15720. ' this.DispInt = s.DispInt;',
  15721. ' return this;',
  15722. ' };',
  15723. '});',
  15724. 'rtl.recNewT(this, "TPutMsg", function () {',
  15725. ' this.DispStr = "";',
  15726. ' this.$eq = function (b) {',
  15727. ' return this.DispStr === b.DispStr;',
  15728. ' };',
  15729. ' this.$assign = function (s) {',
  15730. ' this.DispStr = s.DispStr;',
  15731. ' return this;',
  15732. ' };',
  15733. '});',
  15734. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15735. ' this.Run$1 = function (Msg) {',
  15736. ' };',
  15737. ' this.$msgint = {',
  15738. ' "2": "Fly",',
  15739. ' "3": "Hop"',
  15740. ' };',
  15741. ' this.$msgstr = {',
  15742. ' Fast: "Run$1",',
  15743. ' foo: "Put"',
  15744. ' };',
  15745. '});',
  15746. '']),
  15747. LinesToStr([ // $mod.$main
  15748. '']));
  15749. end;
  15750. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15751. begin
  15752. StartProgram(false);
  15753. Add([
  15754. 'type',
  15755. ' TObject = class',
  15756. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15757. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15758. ' end;',
  15759. 'begin',
  15760. '']);
  15761. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15762. ConvertProgram;
  15763. end;
  15764. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15765. begin
  15766. StartProgram(false);
  15767. Add([
  15768. 'type',
  15769. ' TObject = class',
  15770. ' {$dispatchfield Msg}',
  15771. ' procedure Dispatch(var Msg); virtual; abstract;',
  15772. ' end;',
  15773. ' TFlyMsg = record',
  15774. ' FlyId: longint;',
  15775. ' end;',
  15776. ' TBird = class',
  15777. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15778. ' end;',
  15779. 'begin',
  15780. '']);
  15781. ConvertProgram;
  15782. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15783. end;
  15784. procedure TTestModule.TestClassOf_Create;
  15785. begin
  15786. StartProgram(false);
  15787. Add('type');
  15788. Add(' TObject = class');
  15789. Add(' constructor Create;');
  15790. Add(' end;');
  15791. Add(' TClass = class of TObject;');
  15792. Add('constructor tobject.create; begin end;');
  15793. Add('var');
  15794. Add(' Obj: tobject;');
  15795. Add(' C: tclass;');
  15796. Add('begin');
  15797. Add(' obj:=C.create;');
  15798. Add(' with c do obj:=create;');
  15799. ConvertProgram;
  15800. CheckSource('TestClassOf_Create',
  15801. LinesToStr([ // statements
  15802. 'rtl.createClass(this, "TObject", null, function () {',
  15803. ' this.$init = function () {',
  15804. ' };',
  15805. ' this.$final = function () {',
  15806. ' };',
  15807. ' this.Create = function () {',
  15808. ' return this;',
  15809. ' };',
  15810. '});',
  15811. 'this.Obj = null;',
  15812. 'this.C = null;'
  15813. ]),
  15814. LinesToStr([ // $mod.$main
  15815. '$mod.Obj = $mod.C.$create("Create");',
  15816. 'var $with = $mod.C;',
  15817. '$mod.Obj = $with.$create("Create");',
  15818. '']));
  15819. end;
  15820. procedure TTestModule.TestClassOf_Call;
  15821. begin
  15822. StartProgram(false);
  15823. Add('type');
  15824. Add(' TObject = class');
  15825. Add(' class procedure DoIt;');
  15826. Add(' end;');
  15827. Add(' TClass = class of TObject;');
  15828. Add('class procedure tobject.doit; begin end;');
  15829. Add('var');
  15830. Add(' C: tclass;');
  15831. Add('begin');
  15832. Add(' c.doit;');
  15833. Add(' with c do doit;');
  15834. ConvertProgram;
  15835. CheckSource('TestClassOf_Call',
  15836. LinesToStr([ // statements
  15837. 'rtl.createClass(this, "TObject", null, function () {',
  15838. ' this.$init = function () {',
  15839. ' };',
  15840. ' this.$final = function () {',
  15841. ' };',
  15842. ' this.DoIt = function () {',
  15843. ' };',
  15844. '});',
  15845. 'this.C = null;'
  15846. ]),
  15847. LinesToStr([ // $mod.$main
  15848. '$mod.C.DoIt();',
  15849. 'var $with = $mod.C;',
  15850. '$with.DoIt();',
  15851. '']));
  15852. end;
  15853. procedure TTestModule.TestClassOf_Assign;
  15854. begin
  15855. StartProgram(false);
  15856. Add('type');
  15857. Add(' TClass = class of TObject;');
  15858. Add(' TObject = class');
  15859. Add(' ClassType: TClass; ');
  15860. Add(' end;');
  15861. Add('var');
  15862. Add(' Obj: tobject;');
  15863. Add(' C: tclass;');
  15864. Add('begin');
  15865. Add(' c:=nil;');
  15866. Add(' c:=obj.classtype;');
  15867. ConvertProgram;
  15868. CheckSource('TestClassOf_Assign',
  15869. LinesToStr([ // statements
  15870. 'rtl.createClass(this, "TObject", null, function () {',
  15871. ' this.$init = function () {',
  15872. ' this.ClassType = null;',
  15873. ' };',
  15874. ' this.$final = function () {',
  15875. ' this.ClassType = undefined;',
  15876. ' };',
  15877. '});',
  15878. 'this.Obj = null;',
  15879. 'this.C = null;'
  15880. ]),
  15881. LinesToStr([ // $mod.$main
  15882. '$mod.C = null;',
  15883. '$mod.C = $mod.Obj.ClassType;',
  15884. '']));
  15885. end;
  15886. procedure TTestModule.TestClassOf_Is;
  15887. begin
  15888. StartProgram(false);
  15889. Add('type');
  15890. Add(' TClass = class of TObject;');
  15891. Add(' TObject = class');
  15892. Add(' end;');
  15893. Add(' TCar = class');
  15894. Add(' end;');
  15895. Add(' TCars = class of TCar;');
  15896. Add('var');
  15897. Add(' Obj: tobject;');
  15898. Add(' C: tclass;');
  15899. Add(' Cars: tcars;');
  15900. Add('begin');
  15901. Add(' if c is tcar then ;');
  15902. Add(' if c is tcars then ;');
  15903. ConvertProgram;
  15904. CheckSource('TestClassOf_Is',
  15905. LinesToStr([ // statements
  15906. 'rtl.createClass(this, "TObject", null, function () {',
  15907. ' this.$init = function () {',
  15908. ' };',
  15909. ' this.$final = function () {',
  15910. ' };',
  15911. '});',
  15912. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15913. '});',
  15914. 'this.Obj = null;',
  15915. 'this.C = null;',
  15916. 'this.Cars = null;'
  15917. ]),
  15918. LinesToStr([ // $mod.$main
  15919. 'if(rtl.is($mod.C,$mod.TCar));',
  15920. 'if(rtl.is($mod.C,$mod.TCar));',
  15921. '']));
  15922. end;
  15923. procedure TTestModule.TestClassOf_Compare;
  15924. begin
  15925. StartProgram(false);
  15926. Add('type');
  15927. Add(' TClass = class of TObject;');
  15928. Add(' TObject = class');
  15929. Add(' ClassType: TClass; ');
  15930. Add(' end;');
  15931. Add('var');
  15932. Add(' b: boolean;');
  15933. Add(' Obj: tobject;');
  15934. Add(' C: tclass;');
  15935. Add('begin');
  15936. Add(' b:=c=nil;');
  15937. Add(' b:=nil=c;');
  15938. Add(' b:=c=obj.classtype;');
  15939. Add(' b:=obj.classtype=c;');
  15940. Add(' b:=c=TObject;');
  15941. Add(' b:=TObject=c;');
  15942. Add(' b:=c<>nil;');
  15943. Add(' b:=nil<>c;');
  15944. Add(' b:=c<>obj.classtype;');
  15945. Add(' b:=obj.classtype<>c;');
  15946. Add(' b:=c<>TObject;');
  15947. Add(' b:=TObject<>c;');
  15948. ConvertProgram;
  15949. CheckSource('TestClassOf_Compare',
  15950. LinesToStr([ // statements
  15951. 'rtl.createClass(this, "TObject", null, function () {',
  15952. ' this.$init = function () {',
  15953. ' this.ClassType = null;',
  15954. ' };',
  15955. ' this.$final = function () {',
  15956. ' this.ClassType = undefined;',
  15957. ' };',
  15958. '});',
  15959. 'this.b = false;',
  15960. 'this.Obj = null;',
  15961. 'this.C = null;'
  15962. ]),
  15963. LinesToStr([ // $mod.$main
  15964. '$mod.b = $mod.C === null;',
  15965. '$mod.b = null === $mod.C;',
  15966. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15967. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15968. '$mod.b = $mod.C === $mod.TObject;',
  15969. '$mod.b = $mod.TObject === $mod.C;',
  15970. '$mod.b = $mod.C !== null;',
  15971. '$mod.b = null !== $mod.C;',
  15972. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15973. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15974. '$mod.b = $mod.C !== $mod.TObject;',
  15975. '$mod.b = $mod.TObject !== $mod.C;',
  15976. '']));
  15977. end;
  15978. procedure TTestModule.TestClassOf_ClassVar;
  15979. begin
  15980. StartProgram(false);
  15981. Add('type');
  15982. Add(' TObject = class');
  15983. Add(' class var id: longint;');
  15984. Add(' end;');
  15985. Add(' TClass = class of TObject;');
  15986. Add('var');
  15987. Add(' C: tclass;');
  15988. Add('begin');
  15989. Add(' C.id:=C.id;');
  15990. ConvertProgram;
  15991. CheckSource('TestClassOf_ClassVar',
  15992. LinesToStr([ // statements
  15993. 'rtl.createClass(this, "TObject", null, function () {',
  15994. ' this.id = 0;',
  15995. ' this.$init = function () {',
  15996. ' };',
  15997. ' this.$final = function () {',
  15998. ' };',
  15999. '});',
  16000. 'this.C = null;'
  16001. ]),
  16002. LinesToStr([ // $mod.$main
  16003. '$mod.TObject.id = $mod.C.id;',
  16004. '']));
  16005. end;
  16006. procedure TTestModule.TestClassOf_ClassMethod;
  16007. begin
  16008. StartProgram(false);
  16009. Add('type');
  16010. Add(' TObject = class');
  16011. Add(' class function DoIt(i: longint = 0): longint;');
  16012. Add(' end;');
  16013. Add(' TClass = class of TObject;');
  16014. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16015. Add('var');
  16016. Add(' i: longint;');
  16017. Add(' C: tclass;');
  16018. Add('begin');
  16019. Add(' C.DoIt;');
  16020. Add(' C.DoIt();');
  16021. Add(' i:=C.DoIt;');
  16022. Add(' i:=C.DoIt();');
  16023. ConvertProgram;
  16024. CheckSource('TestClassOf_ClassMethod',
  16025. LinesToStr([ // statements
  16026. 'rtl.createClass(this, "TObject", null, function () {',
  16027. ' this.$init = function () {',
  16028. ' };',
  16029. ' this.$final = function () {',
  16030. ' };',
  16031. ' this.DoIt = function (i) {',
  16032. ' var Result = 0;',
  16033. ' return Result;',
  16034. ' };',
  16035. '});',
  16036. 'this.i = 0;',
  16037. 'this.C = null;'
  16038. ]),
  16039. LinesToStr([ // $mod.$main
  16040. '$mod.C.DoIt(0);',
  16041. '$mod.C.DoIt(0);',
  16042. '$mod.i = $mod.C.DoIt(0);',
  16043. '$mod.i = $mod.C.DoIt(0);',
  16044. '']));
  16045. end;
  16046. procedure TTestModule.TestClassOf_ClassProperty;
  16047. begin
  16048. StartProgram(false);
  16049. Add([
  16050. 'type',
  16051. ' TObject = class',
  16052. ' class var FA: longint;',
  16053. ' class function GetA: longint;',
  16054. ' class procedure SetA(Value: longint);',
  16055. ' class property pA: longint read fa write fa;',
  16056. ' class property pB: longint read geta write seta;',
  16057. ' end;',
  16058. ' TObjectClass = class of tobject;',
  16059. 'class function tobject.geta: longint; begin end;',
  16060. 'class procedure tobject.seta(value: longint); begin end;',
  16061. 'var',
  16062. ' b: boolean;',
  16063. ' Obj: tobject;',
  16064. ' Cla: tobjectclass;',
  16065. 'begin',
  16066. ' obj.pa:=obj.pa;',
  16067. ' obj.pb:=obj.pb;',
  16068. ' b:=obj.pa=4;',
  16069. ' b:=obj.pb=obj.pb;',
  16070. ' b:=5=obj.pa;',
  16071. ' cla.pa:=6;',
  16072. ' cla.pa:=cla.pa;',
  16073. ' cla.pb:=cla.pb;',
  16074. ' b:=cla.pa=7;',
  16075. ' b:=cla.pb=cla.pb;',
  16076. ' b:=8=cla.pa;',
  16077. ' tobject.pa:=9;',
  16078. ' tobject.pb:=tobject.pb;',
  16079. ' b:=tobject.pa=10;',
  16080. ' b:=11=tobject.pa;',
  16081. '']);
  16082. ConvertProgram;
  16083. CheckSource('TestClassOf_ClassProperty',
  16084. LinesToStr([ // statements
  16085. 'rtl.createClass(this, "TObject", null, function () {',
  16086. ' this.FA = 0;',
  16087. ' this.$init = function () {',
  16088. ' };',
  16089. ' this.$final = function () {',
  16090. ' };',
  16091. ' this.GetA = function () {',
  16092. ' var Result = 0;',
  16093. ' return Result;',
  16094. ' };',
  16095. ' this.SetA = function (Value) {',
  16096. ' };',
  16097. '});',
  16098. 'this.b = false;',
  16099. 'this.Obj = null;',
  16100. 'this.Cla = null;'
  16101. ]),
  16102. LinesToStr([ // $mod.$main
  16103. '$mod.TObject.FA = $mod.Obj.FA;',
  16104. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16105. '$mod.b = $mod.Obj.FA === 4;',
  16106. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16107. '$mod.b = 5 === $mod.Obj.FA;',
  16108. '$mod.TObject.FA = 6;',
  16109. '$mod.TObject.FA = $mod.Cla.FA;',
  16110. '$mod.Cla.SetA($mod.Cla.GetA());',
  16111. '$mod.b = $mod.Cla.FA === 7;',
  16112. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16113. '$mod.b = 8 === $mod.Cla.FA;',
  16114. '$mod.TObject.FA = 9;',
  16115. '$mod.TObject.SetA($mod.TObject.GetA());',
  16116. '$mod.b = $mod.TObject.FA === 10;',
  16117. '$mod.b = 11 === $mod.TObject.FA;',
  16118. '']));
  16119. end;
  16120. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16121. begin
  16122. StartProgram(false);
  16123. Add('type');
  16124. Add(' TObject = class');
  16125. Add(' class var GlobalId: longint;');
  16126. Add(' class procedure ProcA;');
  16127. Add(' end;');
  16128. Add('class procedure tobject.proca;');
  16129. Add('var b: boolean;');
  16130. Add('begin');
  16131. Add(' b:=self=nil;');
  16132. Add(' b:=self.globalid=3;');
  16133. Add(' b:=4=self.globalid;');
  16134. Add(' self.globalid:=5;');
  16135. Add(' self.proca;');
  16136. Add('end;');
  16137. Add('begin');
  16138. ConvertProgram;
  16139. CheckSource('TestClassOf_ClassMethodSelf',
  16140. LinesToStr([ // statements
  16141. 'rtl.createClass(this, "TObject", null, function () {',
  16142. ' this.GlobalId = 0;',
  16143. ' this.$init = function () {',
  16144. ' };',
  16145. ' this.$final = function () {',
  16146. ' };',
  16147. ' this.ProcA = function () {',
  16148. ' var b = false;',
  16149. ' b = this === null;',
  16150. ' b = this.GlobalId === 3;',
  16151. ' b = 4 === this.GlobalId;',
  16152. ' $mod.TObject.GlobalId = 5;',
  16153. ' this.ProcA();',
  16154. ' };',
  16155. '});'
  16156. ]),
  16157. LinesToStr([ // $mod.$main
  16158. '']));
  16159. end;
  16160. procedure TTestModule.TestClassOf_TypeCast;
  16161. begin
  16162. StartProgram(false);
  16163. Add('type');
  16164. Add(' TObject = class');
  16165. Add(' class procedure {#TObject_DoIt}DoIt;');
  16166. Add(' end;');
  16167. Add(' TClass = class of TObject;');
  16168. Add(' TMobile = class');
  16169. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16170. Add(' end;');
  16171. Add(' TMobileClass = class of TMobile;');
  16172. Add(' TCar = class(TMobile)');
  16173. Add(' class procedure {#TCar_DoIt}DoIt;');
  16174. Add(' end;');
  16175. Add(' TCarClass = class of TCar;');
  16176. Add('class procedure TObject.DoIt;');
  16177. Add('begin');
  16178. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16179. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16180. Add('end;');
  16181. Add('class procedure TMobile.DoIt;');
  16182. Add('begin');
  16183. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16184. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16185. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16186. Add('end;');
  16187. Add('class procedure TCar.DoIt; begin end;');
  16188. Add('var');
  16189. Add(' ObjC: TClass;');
  16190. Add(' MobileC: TMobileClass;');
  16191. Add(' CarC: TCarClass;');
  16192. Add('begin');
  16193. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16194. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16195. Add(' CarC.{@TCar_DoIt}DoIt;');
  16196. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16197. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16198. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16199. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16200. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16201. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16202. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16203. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16204. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16205. ConvertProgram;
  16206. CheckSource('TestClassOf_TypeCast',
  16207. LinesToStr([ // statements
  16208. 'rtl.createClass(this, "TObject", null, function () {',
  16209. ' this.$init = function () {',
  16210. ' };',
  16211. ' this.$final = function () {',
  16212. ' };',
  16213. ' this.DoIt = function () {',
  16214. ' this.DoIt();',
  16215. ' this.DoIt$1();',
  16216. ' };',
  16217. '});',
  16218. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16219. ' this.DoIt$1 = function () {',
  16220. ' this.DoIt();',
  16221. ' this.DoIt$1();',
  16222. ' this.DoIt$2();',
  16223. ' };',
  16224. '});',
  16225. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16226. ' this.DoIt$2 = function () {',
  16227. ' };',
  16228. '});',
  16229. 'this.ObjC = null;',
  16230. 'this.MobileC = null;',
  16231. 'this.CarC = null;',
  16232. '']),
  16233. LinesToStr([ // $mod.$main
  16234. '$mod.ObjC.DoIt();',
  16235. '$mod.MobileC.DoIt$1();',
  16236. '$mod.CarC.DoIt$2();',
  16237. '$mod.ObjC.DoIt();',
  16238. '$mod.ObjC.DoIt$1();',
  16239. '$mod.ObjC.DoIt$2();',
  16240. '$mod.MobileC.DoIt();',
  16241. '$mod.MobileC.DoIt$1();',
  16242. '$mod.MobileC.DoIt$2();',
  16243. '$mod.CarC.DoIt();',
  16244. '$mod.CarC.DoIt$1();',
  16245. '$mod.CarC.DoIt$2();',
  16246. '']));
  16247. end;
  16248. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16249. begin
  16250. StartProgram(false);
  16251. Add('type');
  16252. Add(' TObject = class');
  16253. Add(' function CurNow: longint; ');
  16254. Add(' class function Now: longint; ');
  16255. Add(' end;');
  16256. Add('function TObject.CurNow: longint; begin end;');
  16257. Add('class function TObject.Now: longint; begin end;');
  16258. Add('var');
  16259. Add(' Obj: tobject;');
  16260. Add(' vI: longint;');
  16261. Add('begin');
  16262. Add(' obj.curnow;');
  16263. Add(' vi:=obj.curnow;');
  16264. Add(' tobject.now;');
  16265. Add(' vi:=tobject.now;');
  16266. ConvertProgram;
  16267. CheckSource('TestClassOf_ImplicitFunctionCall',
  16268. LinesToStr([ // statements
  16269. 'rtl.createClass(this, "TObject", null, function () {',
  16270. ' this.$init = function () {',
  16271. ' };',
  16272. ' this.$final = function () {',
  16273. ' };',
  16274. ' this.CurNow = function () {',
  16275. ' var Result = 0;',
  16276. ' return Result;',
  16277. ' };',
  16278. ' this.Now = function () {',
  16279. ' var Result = 0;',
  16280. ' return Result;',
  16281. ' };',
  16282. '});',
  16283. 'this.Obj = null;',
  16284. 'this.vI = 0;',
  16285. '']),
  16286. LinesToStr([ // $mod.$main
  16287. '$mod.Obj.CurNow();',
  16288. '$mod.vI = $mod.Obj.CurNow();',
  16289. '$mod.TObject.Now();',
  16290. '$mod.vI = $mod.TObject.Now();',
  16291. '']));
  16292. end;
  16293. procedure TTestModule.TestClassOf_Const;
  16294. begin
  16295. StartProgram(false);
  16296. Add([
  16297. 'type',
  16298. ' TObject = class',
  16299. ' end;',
  16300. ' TBird = TObject;',
  16301. ' TBirds = class of TBird;',
  16302. ' TEagles = TBirds;',
  16303. ' THawk = class(TBird);',
  16304. 'const',
  16305. ' Hawk: TEagles = THawk;',
  16306. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16307. ' TBird,',
  16308. ' THawk',
  16309. ' );',
  16310. 'begin']);
  16311. ConvertProgram;
  16312. CheckSource('TestClassOf_Const',
  16313. LinesToStr([ // statements
  16314. 'rtl.createClass(this, "TObject", null, function () {',
  16315. ' this.$init = function () {',
  16316. ' };',
  16317. ' this.$final = function () {',
  16318. ' };',
  16319. '});',
  16320. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16321. '});',
  16322. 'this.Hawk = this.THawk;',
  16323. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16324. '']),
  16325. LinesToStr([ // $mod.$main
  16326. '']));
  16327. end;
  16328. procedure TTestModule.TestNestedClass_Alias;
  16329. begin
  16330. WithTypeInfo:=true;
  16331. StartProgram(false);
  16332. Add([
  16333. 'type',
  16334. ' TObject = class',
  16335. ' type TNested = type longint;',
  16336. ' end;',
  16337. 'type TAlias = type tobject.tnested;',
  16338. 'var i: tobject.tnested = 3;',
  16339. 'var j: TAlias = 4;',
  16340. 'begin',
  16341. ' if typeinfo(TAlias)=nil then ;',
  16342. ' if typeinfo(tobject.tnested)=nil then ;',
  16343. '']);
  16344. ConvertProgram;
  16345. CheckSource('TestNestedClass_Alias',
  16346. LinesToStr([ // statements
  16347. 'rtl.createClass(this, "TObject", null, function () {',
  16348. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16349. ' this.$init = function () {',
  16350. ' };',
  16351. ' this.$final = function () {',
  16352. ' };',
  16353. '});',
  16354. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16355. 'this.i = 3;',
  16356. 'this.j = 4;',
  16357. '']),
  16358. LinesToStr([ // $mod.$main
  16359. 'if ($mod.$rtti["TAlias"] === null) ;',
  16360. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16361. '']));
  16362. end;
  16363. procedure TTestModule.TestNestedClass_Record;
  16364. begin
  16365. WithTypeInfo:=true;
  16366. StartProgram(false);
  16367. Add([
  16368. 'type',
  16369. ' TObject = class',
  16370. ' type TPoint = record',
  16371. ' x,y: byte;',
  16372. ' end;',
  16373. ' procedure DoIt(t: TPoint);',
  16374. ' end;',
  16375. 'procedure tobject.DoIt(t: TPoint);',
  16376. 'var p: TPoint;',
  16377. 'begin',
  16378. ' t.x:=t.y;',
  16379. ' p:=t;',
  16380. 'end;',
  16381. 'var',
  16382. ' p: tobject.tpoint = (x:2; y:4);',
  16383. ' o: TObject;',
  16384. 'begin',
  16385. ' p:=p;',
  16386. ' o.doit(p);',
  16387. '']);
  16388. ConvertProgram;
  16389. CheckSource('TestNestedClass_Record',
  16390. LinesToStr([ // statements
  16391. 'rtl.createClass(this, "TObject", null, function () {',
  16392. ' rtl.recNewT(this, "TPoint", function () {',
  16393. ' this.x = 0;',
  16394. ' this.y = 0;',
  16395. ' this.$eq = function (b) {',
  16396. ' return (this.x === b.x) && (this.y === b.y);',
  16397. ' };',
  16398. ' this.$assign = function (s) {',
  16399. ' this.x = s.x;',
  16400. ' this.y = s.y;',
  16401. ' return this;',
  16402. ' };',
  16403. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16404. ' $r.addField("x", rtl.byte);',
  16405. ' $r.addField("y", rtl.byte);',
  16406. ' });',
  16407. ' this.$init = function () {',
  16408. ' };',
  16409. ' this.$final = function () {',
  16410. ' };',
  16411. ' this.DoIt = function (t) {',
  16412. ' var p = this.TPoint.$new();',
  16413. ' t.x = t.y;',
  16414. ' p.$assign(t);',
  16415. ' };',
  16416. '});',
  16417. 'this.p = this.TObject.TPoint.$clone({',
  16418. ' x: 2,',
  16419. ' y: 4',
  16420. '});',
  16421. 'this.o = null;',
  16422. '']),
  16423. LinesToStr([ // $mod.$main
  16424. '$mod.p.$assign($mod.p);',
  16425. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16426. '']));
  16427. end;
  16428. procedure TTestModule.TestNestedClass_Class;
  16429. begin
  16430. WithTypeInfo:=true;
  16431. StartProgram(false);
  16432. Add([
  16433. 'type',
  16434. ' TObject = class end;',
  16435. ' TBird = class',
  16436. ' type TLeg = class',
  16437. ' FId: longint;',
  16438. ' constructor Create;',
  16439. ' function Create(i: longint): TLeg;',
  16440. ' end;',
  16441. ' function DoIt(b: TBird): Tleg;',
  16442. ' end;',
  16443. 'constructor tbird.tleg.create;',
  16444. 'begin',
  16445. ' FId:=3;',
  16446. 'end;',
  16447. 'function tbird.tleg.Create(i: longint): TLeg;',
  16448. 'begin',
  16449. ' Create;',
  16450. ' Result:=TLeg.Create;',
  16451. ' Result:=TBird.TLeg.Create;',
  16452. ' Result:=Create(3);',
  16453. ' FId:=i;',
  16454. 'end;',
  16455. 'function tbird.DoIt(b: tbird): tleg;',
  16456. 'begin',
  16457. ' Result.Create;',
  16458. ' Result:=TLeg.Create;',
  16459. ' Result:=TBird.TLeg.Create;',
  16460. ' Result:=Result.Create(3);',
  16461. 'end;',
  16462. 'var',
  16463. ' b: Tbird.tleg;',
  16464. 'begin',
  16465. ' b.Create;',
  16466. ' b:=TBird.TLeg.Create;',
  16467. ' b:=b.Create(3);',
  16468. '']);
  16469. ConvertProgram;
  16470. CheckSource('TestNestedClass_Class',
  16471. LinesToStr([ // statements
  16472. 'rtl.createClass(this, "TObject", null, function () {',
  16473. ' this.$init = function () {',
  16474. ' };',
  16475. ' this.$final = function () {',
  16476. ' };',
  16477. '});',
  16478. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16479. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16480. ' this.$init = function () {',
  16481. ' $mod.TObject.$init.call(this);',
  16482. ' this.FId = 0;',
  16483. ' };',
  16484. ' this.Create = function () {',
  16485. ' this.FId = 3;',
  16486. ' return this;',
  16487. ' };',
  16488. ' this.Create$1 = function (i) {',
  16489. ' var Result = null;',
  16490. ' this.Create();',
  16491. ' Result = $mod.TBird.TLeg.$create("Create");',
  16492. ' Result = $mod.TBird.TLeg.$create("Create");',
  16493. ' Result = this.Create$1(3);',
  16494. ' this.FId = i;',
  16495. ' return Result;',
  16496. ' };',
  16497. ' }, "TBird.TLeg");',
  16498. ' this.DoIt = function (b) {',
  16499. ' var Result = null;',
  16500. ' Result.Create();',
  16501. ' Result = this.TLeg.$create("Create");',
  16502. ' Result = $mod.TBird.TLeg.$create("Create");',
  16503. ' Result = Result.Create$1(3);',
  16504. ' return Result;',
  16505. ' };',
  16506. '});',
  16507. 'this.b = null;',
  16508. '']),
  16509. LinesToStr([ // $mod.$main
  16510. '$mod.b.Create();',
  16511. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16512. '$mod.b = $mod.b.Create$1(3);',
  16513. '']));
  16514. end;
  16515. procedure TTestModule.TestExternalClass_Var;
  16516. begin
  16517. StartProgram(false);
  16518. Add([
  16519. '{$modeswitch externalclass}',
  16520. 'type',
  16521. ' TExtA = class external name ''ExtObj''',
  16522. ' Id: longint external name ''$Id'';',
  16523. ' B: longint;',
  16524. ' end;',
  16525. 'var Obj: TExtA;',
  16526. 'begin',
  16527. ' obj.id:=obj.id+1;',
  16528. ' obj.B:=obj.B+1;']);
  16529. ConvertProgram;
  16530. CheckSource('TestExternalClass_Var',
  16531. LinesToStr([ // statements
  16532. 'this.Obj = null;',
  16533. '']),
  16534. LinesToStr([ // $mod.$main
  16535. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16536. '$mod.Obj.B = $mod.Obj.B + 1;',
  16537. '']));
  16538. end;
  16539. procedure TTestModule.TestExternalClass_Const;
  16540. begin
  16541. StartProgram(false);
  16542. Add([
  16543. '{$modeswitch externalclass}',
  16544. 'type',
  16545. ' TExtA = class external name ''ExtObj''',
  16546. ' const Two: longint = 2;',
  16547. ' const Three = 3;',
  16548. ' const Id: longint;',
  16549. ' end;',
  16550. ' TExtB = class external name ''ExtB''',
  16551. ' A: TExtA;',
  16552. ' end;',
  16553. 'var',
  16554. ' A: texta;',
  16555. ' B: textb;',
  16556. ' i: longint;',
  16557. 'begin',
  16558. ' i:=a.two;',
  16559. ' i:=texta.two;',
  16560. ' i:=a.three;',
  16561. ' i:=texta.three;',
  16562. ' i:=a.id;',
  16563. ' i:=texta.id;',
  16564. '']);
  16565. ConvertProgram;
  16566. CheckSource('TestExternalClass_Const',
  16567. LinesToStr([ // statements
  16568. 'this.A = null;',
  16569. 'this.B = null;',
  16570. 'this.i = 0;',
  16571. '']),
  16572. LinesToStr([ // $mod.$main
  16573. '$mod.i = 2;',
  16574. '$mod.i = 2;',
  16575. '$mod.i = 3;',
  16576. '$mod.i = 3;',
  16577. '$mod.i = $mod.A.Id;',
  16578. '$mod.i = ExtObj.Id;',
  16579. '']));
  16580. end;
  16581. procedure TTestModule.TestExternalClass_Dollar;
  16582. begin
  16583. StartProgram(false);
  16584. Add([
  16585. '{$modeswitch externalclass}',
  16586. 'type',
  16587. ' TExtA = class external name ''$''',
  16588. ' Id: longint external name ''$'';',
  16589. ' function Bla(i: longint): longint; external name ''$'';',
  16590. ' end;',
  16591. 'function dollar(k: longint): longint; external name ''$'';',
  16592. 'var Obj: TExtA;',
  16593. 'begin',
  16594. ' dollar(1);',
  16595. ' obj.id:=obj.id+2;',
  16596. ' obj.Bla(3);',
  16597. '']);
  16598. ConvertProgram;
  16599. CheckSource('TestExternalClass_Dollar',
  16600. LinesToStr([ // statements
  16601. 'this.Obj = null;',
  16602. '']),
  16603. LinesToStr([ // $mod.$main
  16604. '$(1);',
  16605. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16606. '$mod.Obj.$(3);',
  16607. '']));
  16608. end;
  16609. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16610. begin
  16611. StartProgram(false);
  16612. Add('{$modeswitch externalclass}');
  16613. Add('type');
  16614. Add(' TExtA = class external name ''ExtA''');
  16615. Add(' Id: longint external name ''$Id'';');
  16616. Add(' end;');
  16617. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16618. Add(' Id: longint;');
  16619. Add(' end;');
  16620. Add('begin');
  16621. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16622. ConvertProgram;
  16623. end;
  16624. procedure TTestModule.TestExternalClass_Method;
  16625. begin
  16626. StartProgram(false);
  16627. Add(['{$modeswitch externalclass}',
  16628. 'type',
  16629. ' TExtA = class external name ''ExtObj''',
  16630. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16631. ' procedure DoSome(Id: longint = 1);',
  16632. ' end;',
  16633. 'var Obj: texta;',
  16634. 'begin',
  16635. ' obj.doit;',
  16636. ' obj.doit();',
  16637. ' obj.doit(2);',
  16638. ' with obj do begin',
  16639. ' doit;',
  16640. ' doit();',
  16641. ' doit(3);',
  16642. ' end;']);
  16643. ConvertProgram;
  16644. CheckSource('TestExternalClass_Method',
  16645. LinesToStr([ // statements
  16646. 'this.Obj = null;',
  16647. '']),
  16648. LinesToStr([ // $mod.$main
  16649. '$mod.Obj.$Execute(1);',
  16650. '$mod.Obj.$Execute(1);',
  16651. '$mod.Obj.$Execute(2);',
  16652. 'var $with = $mod.Obj;',
  16653. '$with.$Execute(1);',
  16654. '$with.$Execute(1);',
  16655. '$with.$Execute(3);',
  16656. '']));
  16657. end;
  16658. procedure TTestModule.TestExternalClass_ClassMethod;
  16659. begin
  16660. StartProgram(false);
  16661. Add([
  16662. '{$modeswitch externalclass}',
  16663. 'type',
  16664. ' TExtA = class external name ''ExtObj''',
  16665. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16666. ' end;',
  16667. ' TExtB = TExtA;',
  16668. 'var p: Pointer;',
  16669. 'begin',
  16670. ' texta.doit;',
  16671. ' texta.doit();',
  16672. ' texta.doit(2);',
  16673. ' p:[email protected];',
  16674. ' with texta do begin',
  16675. ' doit;',
  16676. ' doit();',
  16677. ' doit(3);',
  16678. ' p:=@DoIt;',
  16679. ' end;',
  16680. ' textb.doit;',
  16681. ' textb.doit();',
  16682. ' textb.doit(4);',
  16683. ' with textb do begin',
  16684. ' doit;',
  16685. ' doit();',
  16686. ' doit(5);',
  16687. ' end;',
  16688. '']);
  16689. ConvertProgram;
  16690. CheckSource('TestExternalClass_ClassMethod',
  16691. LinesToStr([ // statements
  16692. 'this.p = null;',
  16693. '']),
  16694. LinesToStr([ // $mod.$main
  16695. 'ExtObj.$Execute(1);',
  16696. 'ExtObj.$Execute(1);',
  16697. 'ExtObj.$Execute(2);',
  16698. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16699. 'ExtObj.$Execute(1);',
  16700. 'ExtObj.$Execute(1);',
  16701. 'ExtObj.$Execute(3);',
  16702. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16703. 'ExtObj.$Execute(1);',
  16704. 'ExtObj.$Execute(1);',
  16705. 'ExtObj.$Execute(4);',
  16706. 'ExtObj.$Execute(1);',
  16707. 'ExtObj.$Execute(1);',
  16708. 'ExtObj.$Execute(5);',
  16709. '']));
  16710. end;
  16711. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16712. begin
  16713. StartProgram(false);
  16714. Add([
  16715. '{$modeswitch externalclass}',
  16716. 'type',
  16717. ' TExtA = class external name ''ExtObj''',
  16718. ' class procedure DoIt(Id: longint = 1); static;',
  16719. ' end;',
  16720. 'var p: Pointer;',
  16721. 'begin',
  16722. ' texta.doit;',
  16723. ' texta.doit();',
  16724. ' texta.doit(2);',
  16725. ' p:[email protected];',
  16726. ' with texta do begin',
  16727. ' doit;',
  16728. ' doit();',
  16729. ' doit(3);',
  16730. ' p:=@DoIt;',
  16731. ' end;',
  16732. '']);
  16733. ConvertProgram;
  16734. CheckSource('TestExternalClass_ClassMethodStatic',
  16735. LinesToStr([ // statements
  16736. 'this.p = null;',
  16737. '']),
  16738. LinesToStr([ // $mod.$main
  16739. 'ExtObj.DoIt(1);',
  16740. 'ExtObj.DoIt(1);',
  16741. 'ExtObj.DoIt(2);',
  16742. '$mod.p = ExtObj.DoIt;',
  16743. 'ExtObj.DoIt(1);',
  16744. 'ExtObj.DoIt(1);',
  16745. 'ExtObj.DoIt(3);',
  16746. '$mod.p = ExtObj.DoIt;',
  16747. '']));
  16748. end;
  16749. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16750. begin
  16751. StartProgram(false);
  16752. Add([
  16753. '{$modeswitch externalclass}',
  16754. 'type',
  16755. ' TBird = class external name ''Array''',
  16756. ' end;',
  16757. 'function GetPtr: Pointer;',
  16758. 'begin',
  16759. 'end;',
  16760. 'procedure Write(const p);',
  16761. 'begin',
  16762. 'end;',
  16763. 'procedure WriteLn; varargs;',
  16764. 'begin',
  16765. 'end;',
  16766. 'begin',
  16767. ' if TBird(GetPtr)=nil then ;',
  16768. ' Write(GetPtr);',
  16769. ' WriteLn(GetPtr);',
  16770. ' Write(TBird(GetPtr));',
  16771. ' WriteLn(TBird(GetPtr));',
  16772. '']);
  16773. ConvertProgram;
  16774. CheckSource('TestFunctionResultInTypeCast',
  16775. LinesToStr([ // statements
  16776. 'this.GetPtr = function () {',
  16777. ' var Result = null;',
  16778. ' return Result;',
  16779. '};',
  16780. 'this.Write = function (p) {',
  16781. '};',
  16782. 'this.WriteLn = function () {',
  16783. '};',
  16784. '']),
  16785. LinesToStr([
  16786. 'if ($mod.GetPtr() === null) ;',
  16787. '$mod.Write($mod.GetPtr());',
  16788. '$mod.WriteLn($mod.GetPtr());',
  16789. '$mod.Write($mod.GetPtr());',
  16790. '$mod.WriteLn($mod.GetPtr());',
  16791. '']));
  16792. end;
  16793. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16794. begin
  16795. StartProgram(false);
  16796. Add([
  16797. '{$modeswitch externalclass}',
  16798. 'type',
  16799. ' TExtA = class external name ''ExtObjA''',
  16800. ' procedure ProcA; virtual;',
  16801. ' procedure ProcB; virtual;',
  16802. ' end;',
  16803. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16804. ' end;',
  16805. ' TExtC = class (TExtB)',
  16806. ' procedure ProcA; override;',
  16807. ' end;',
  16808. 'procedure TExtC.ProcA;',
  16809. 'begin',
  16810. ' ProcA;',
  16811. ' Self.ProcA;',
  16812. ' ProcB;',
  16813. ' Self.ProcB;',
  16814. 'end;',
  16815. 'var',
  16816. ' A: texta;',
  16817. ' B: textb;',
  16818. ' C: textc;',
  16819. 'begin',
  16820. ' a.proca;',
  16821. ' b.proca;',
  16822. ' c.proca;']);
  16823. ConvertProgram;
  16824. CheckSource('TestExternalClass_NonExternalOverride',
  16825. LinesToStr([ // statements
  16826. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16827. ' this.$init = function () {',
  16828. ' };',
  16829. ' this.$final = function () {',
  16830. ' };',
  16831. ' this.ProcA = function () {',
  16832. ' this.ProcA();',
  16833. ' this.ProcA();',
  16834. ' this.ProcB();',
  16835. ' this.ProcB();',
  16836. ' };',
  16837. '});',
  16838. 'this.A = null;',
  16839. 'this.B = null;',
  16840. 'this.C = null;',
  16841. '']),
  16842. LinesToStr([ // $mod.$main
  16843. '$mod.A.ProcA();',
  16844. '$mod.B.ProcA();',
  16845. '$mod.C.ProcA();',
  16846. '']));
  16847. end;
  16848. procedure TTestModule.TestExternalClass_OverloadHint;
  16849. begin
  16850. StartProgram(false);
  16851. Add([
  16852. '{$modeswitch externalclass}',
  16853. 'type',
  16854. ' TExtA = class external name ''ExtObjA''',
  16855. ' procedure DoIt;',
  16856. ' procedure DoIt(i: longint);',
  16857. ' end;',
  16858. 'begin',
  16859. '']);
  16860. ConvertProgram;
  16861. CheckResolverUnexpectedHints(true);
  16862. CheckSource('TestExternalClass_OverloadHint',
  16863. LinesToStr([ // statements
  16864. '']),
  16865. LinesToStr([ // $mod.$main
  16866. '']));
  16867. end;
  16868. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16869. begin
  16870. WithTypeInfo:=true;
  16871. StartProgram(false);
  16872. Add([
  16873. '{$modeswitch externalclass}',
  16874. 'type',
  16875. ' JSwiper = class external name ''Swiper''',
  16876. ' constructor New;',
  16877. ' end;',
  16878. ' TObject = class',
  16879. ' private',
  16880. ' FSwiper: JSwiper;',
  16881. ' published',
  16882. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16883. ' end;',
  16884. 'begin',
  16885. ' JSwiper.new;',
  16886. '']);
  16887. ConvertProgram;
  16888. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16889. LinesToStr([ // statements
  16890. 'this.$rtti.$ExtClass("JSwiper", {',
  16891. ' jsclass: "Swiper"',
  16892. '});',
  16893. 'rtl.createClass(this, "TObject", null, function () {',
  16894. ' this.$init = function () {',
  16895. ' this.FSwiper = null;',
  16896. ' };',
  16897. ' this.$final = function () {',
  16898. ' this.FSwiper = undefined;',
  16899. ' };',
  16900. ' var $r = this.$rtti;',
  16901. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16902. '});',
  16903. '']),
  16904. LinesToStr([ // $mod.$main
  16905. 'new Swiper();',
  16906. '']));
  16907. end;
  16908. procedure TTestModule.TestExternalClass_Property;
  16909. begin
  16910. StartProgram(false);
  16911. Add([
  16912. '{$modeswitch externalclass}',
  16913. 'type',
  16914. ' TExtA = class external name ''ExtA''',
  16915. ' function getYear: longint;',
  16916. ' procedure setYear(Value: longint);',
  16917. ' property Year: longint read getyear write setyear;',
  16918. ' end;',
  16919. ' TExtB = class (TExtA)',
  16920. ' procedure OtherSetYear(Value: longint);',
  16921. ' property year write othersetyear;',
  16922. ' end;',
  16923. 'procedure textb.othersetyear(value: longint);',
  16924. 'begin',
  16925. ' setYear(Value+4);',
  16926. 'end;',
  16927. 'var',
  16928. ' A: texta;',
  16929. ' B: textb;',
  16930. 'begin',
  16931. ' a.year:=a.year+1;',
  16932. ' b.year:=b.year+2;']);
  16933. ConvertProgram;
  16934. CheckSource('TestExternalClass_NonExternalOverride',
  16935. LinesToStr([ // statements
  16936. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16937. ' this.$init = function () {',
  16938. ' };',
  16939. ' this.$final = function () {',
  16940. ' };',
  16941. ' this.OtherSetYear = function (Value) {',
  16942. ' this.setYear(Value+4);',
  16943. ' };',
  16944. '});',
  16945. 'this.A = null;',
  16946. 'this.B = null;',
  16947. '']),
  16948. LinesToStr([ // $mod.$main
  16949. '$mod.A.setYear($mod.A.getYear()+1);',
  16950. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16951. '']));
  16952. end;
  16953. procedure TTestModule.TestExternalClass_PropertyDate;
  16954. begin
  16955. StartProgram(false);
  16956. Add([
  16957. '{$modeswitch externalclass}',
  16958. 'type',
  16959. ' TExtA = class external name ''ExtA''',
  16960. ' end;',
  16961. ' TExtB = class (TExtA)',
  16962. ' FDate: string;',
  16963. ' property Date: string read FDate write FDate;',
  16964. ' property ExtA: string read FDate write FDate;',
  16965. ' end;',
  16966. ' {$M+}',
  16967. ' TObject = class',
  16968. ' FDate: string;',
  16969. ' published',
  16970. ' property Date: string read FDate write FDate;',
  16971. ' property ExtA: string read FDate write FDate;',
  16972. ' end;',
  16973. 'var',
  16974. ' B: textb;',
  16975. ' o: TObject;',
  16976. 'begin',
  16977. ' b.date:=b.exta;',
  16978. ' o.date:=o.exta;']);
  16979. ConvertProgram;
  16980. CheckSource('TestExternalClass_PropertyDate',
  16981. LinesToStr([ // statements
  16982. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16983. ' this.$init = function () {',
  16984. ' this.FDate = "";',
  16985. ' };',
  16986. ' this.$final = function () {',
  16987. ' };',
  16988. '});',
  16989. 'rtl.createClass(this, "TObject", null, function () {',
  16990. ' this.$init = function () {',
  16991. ' this.FDate = "";',
  16992. ' };',
  16993. ' this.$final = function () {',
  16994. ' };',
  16995. ' var $r = this.$rtti;',
  16996. ' $r.addField("FDate", rtl.string);',
  16997. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16998. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16999. '});',
  17000. 'this.B = null;',
  17001. 'this.o = null;',
  17002. '']),
  17003. LinesToStr([ // $mod.$main
  17004. '$mod.B.FDate = $mod.B.FDate;',
  17005. '$mod.o.FDate = $mod.o.FDate;',
  17006. '']));
  17007. end;
  17008. procedure TTestModule.TestExternalClass_ClassProperty;
  17009. begin
  17010. StartProgram(false);
  17011. Add('{$modeswitch externalclass}');
  17012. Add('type');
  17013. Add(' TExtA = class external name ''ExtA''');
  17014. Add(' class function getYear: longint;');
  17015. Add(' class procedure setYear(Value: longint);');
  17016. Add(' class property Year: longint read getyear write setyear;');
  17017. Add(' end;');
  17018. Add(' TExtB = class (TExtA)');
  17019. Add(' class function GetCentury: longint;');
  17020. Add(' class procedure SetCentury(Value: longint);');
  17021. Add(' class property Century: longint read getcentury write setcentury;');
  17022. Add(' end;');
  17023. Add('class function textb.getcentury: longint;');
  17024. Add('begin');
  17025. Add('end;');
  17026. Add('class procedure textb.setcentury(value: longint);');
  17027. Add('begin');
  17028. Add(' setyear(value+11);');
  17029. Add(' texta.year:=texta.year+12;');
  17030. Add(' year:=year+13;');
  17031. Add(' textb.century:=textb.century+14;');
  17032. Add(' century:=century+15;');
  17033. Add('end;');
  17034. Add('var');
  17035. Add(' A: texta;');
  17036. Add(' B: textb;');
  17037. Add('begin');
  17038. Add(' texta.year:=texta.year+1;');
  17039. Add(' textb.year:=textb.year+2;');
  17040. Add(' TextA.year:=TextA.year+3;');
  17041. Add(' b.year:=b.year+4;');
  17042. Add(' textb.century:=textb.century+5;');
  17043. Add(' b.century:=b.century+6;');
  17044. ConvertProgram;
  17045. CheckSource('TestExternalClass_ClassProperty',
  17046. LinesToStr([ // statements
  17047. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17048. ' this.$init = function () {',
  17049. ' };',
  17050. ' this.$final = function () {',
  17051. ' };',
  17052. ' this.GetCentury = function () {',
  17053. ' var Result = 0;',
  17054. ' return Result;',
  17055. ' };',
  17056. ' this.SetCentury = function (Value) {',
  17057. ' this.setYear(Value + 11);',
  17058. ' ExtA.setYear(ExtA.getYear() + 12);',
  17059. ' this.setYear(this.getYear() + 13);',
  17060. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17061. ' this.SetCentury(this.GetCentury() + 15);',
  17062. ' };',
  17063. '});',
  17064. 'this.A = null;',
  17065. 'this.B = null;',
  17066. '']),
  17067. LinesToStr([ // $mod.$main
  17068. 'ExtA.setYear(ExtA.getYear() + 1);',
  17069. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17070. 'ExtA.setYear(ExtA.getYear() + 3);',
  17071. '$mod.B.setYear($mod.B.getYear() + 4);',
  17072. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17073. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17074. '']));
  17075. end;
  17076. procedure TTestModule.TestExternalClass_ClassOf;
  17077. begin
  17078. StartProgram(false);
  17079. Add('{$modeswitch externalclass}');
  17080. Add('type');
  17081. Add(' TExtA = class external name ''ExtA''');
  17082. Add(' procedure ProcA; virtual;');
  17083. Add(' procedure ProcB; virtual;');
  17084. Add(' end;');
  17085. Add(' TExtAClass = class of TExtA;');
  17086. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17087. Add(' end;');
  17088. Add(' TExtBClass = class of TExtB;');
  17089. Add(' TExtC = class (TExtB)');
  17090. Add(' procedure ProcA; override;');
  17091. Add(' end;');
  17092. Add(' TExtCClass = class of TExtC;');
  17093. Add('procedure TExtC.ProcA; begin end;');
  17094. Add('var');
  17095. Add(' A: texta; ClA: TExtAClass;');
  17096. Add(' B: textb; ClB: TExtBClass;');
  17097. Add(' C: textc; ClC: TExtCClass;');
  17098. Add('begin');
  17099. Add(' ClA:=texta;');
  17100. Add(' ClA:=textb;');
  17101. Add(' ClA:=textc;');
  17102. Add(' ClB:=textb;');
  17103. Add(' ClB:=textc;');
  17104. Add(' ClC:=textc;');
  17105. ConvertProgram;
  17106. CheckSource('TestExternalClass_ClassOf',
  17107. LinesToStr([ // statements
  17108. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17109. ' this.$init = function () {',
  17110. ' };',
  17111. ' this.$final = function () {',
  17112. ' };',
  17113. ' this.ProcA = function () {',
  17114. ' };',
  17115. '});',
  17116. 'this.A = null;',
  17117. 'this.ClA = null;',
  17118. 'this.B = null;',
  17119. 'this.ClB = null;',
  17120. 'this.C = null;',
  17121. 'this.ClC = null;',
  17122. '']),
  17123. LinesToStr([ // $mod.$main
  17124. '$mod.ClA = ExtA;',
  17125. '$mod.ClA = ExtB;',
  17126. '$mod.ClA = $mod.TExtC;',
  17127. '$mod.ClB = ExtB;',
  17128. '$mod.ClB = $mod.TExtC;',
  17129. '$mod.ClC = $mod.TExtC;',
  17130. '']));
  17131. end;
  17132. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17133. begin
  17134. AddModuleWithIntfImplSrc('unit2.pas',
  17135. LinesToStr([
  17136. '{$modeswitch externalclass}',
  17137. 'type',
  17138. ' TExtA = class external name ''ExtA''',
  17139. ' class var Id: longint;',
  17140. ' end;',
  17141. '']),
  17142. '');
  17143. StartUnit(true);
  17144. Add('interface');
  17145. Add('uses unit2;');
  17146. Add('implementation');
  17147. Add('begin');
  17148. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17149. ConvertUnit;
  17150. CheckSource('TestExternalClass_ClassOtherUnit',
  17151. LinesToStr([
  17152. '']),
  17153. LinesToStr([
  17154. 'ExtA.Id = ExtA.Id + 1;',
  17155. '']));
  17156. end;
  17157. procedure TTestModule.TestExternalClass_Is;
  17158. begin
  17159. StartProgram(false);
  17160. Add([
  17161. '{$modeswitch externalclass}',
  17162. 'type',
  17163. ' TExtA = class external name ''ExtA''',
  17164. ' end;',
  17165. ' TExtAClass = class of TExtA;',
  17166. ' TExtB = class external name ''ExtB'' (TExtA)',
  17167. ' end;',
  17168. ' TExtBClass = class of TExtB;',
  17169. ' TExtC = class (TExtB)',
  17170. ' end;',
  17171. ' TExtCClass = class of TExtC;',
  17172. 'var',
  17173. ' A: texta; ClA: TExtAClass;',
  17174. ' B: textb; ClB: TExtBClass;',
  17175. ' C: textc; ClC: TExtCClass;',
  17176. 'begin',
  17177. ' if a is textb then ;',
  17178. ' if a is textc then ;',
  17179. ' if b is textc then ;',
  17180. ' if cla is textb then ;',
  17181. ' if cla is textc then ;',
  17182. ' if clb is textc then ;',
  17183. ' try',
  17184. ' except',
  17185. ' on TExtA do ;',
  17186. ' on e: TExtB do ;',
  17187. ' end;',
  17188. '']);
  17189. ConvertProgram;
  17190. CheckSource('TestExternalClass_Is',
  17191. LinesToStr([ // statements
  17192. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17193. ' this.$init = function () {',
  17194. ' };',
  17195. ' this.$final = function () {',
  17196. ' };',
  17197. '});',
  17198. 'this.A = null;',
  17199. 'this.ClA = null;',
  17200. 'this.B = null;',
  17201. 'this.ClB = null;',
  17202. 'this.C = null;',
  17203. 'this.ClC = null;',
  17204. '']),
  17205. LinesToStr([ // $mod.$main
  17206. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17207. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17208. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17209. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17210. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17211. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17212. 'try {} catch ($e) {',
  17213. ' if (rtl.isExt($e,ExtA)) {}',
  17214. ' else if (rtl.isExt($e,ExtB)) {',
  17215. ' var e = $e;',
  17216. ' } else throw $e',
  17217. '};',
  17218. '']));
  17219. end;
  17220. procedure TTestModule.TestExternalClass_As;
  17221. begin
  17222. StartProgram(false);
  17223. Add('{$modeswitch externalclass}');
  17224. Add('type');
  17225. Add(' TExtA = class external name ''ExtA''');
  17226. Add(' end;');
  17227. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17228. Add(' end;');
  17229. Add(' TExtC = class (TExtB)');
  17230. Add(' end;');
  17231. Add('var');
  17232. Add(' A: texta;');
  17233. Add(' B: textb;');
  17234. Add(' C: textc;');
  17235. Add('begin');
  17236. Add(' b:=a as textb;');
  17237. Add(' c:=a as textc;');
  17238. Add(' c:=b as textc;');
  17239. ConvertProgram;
  17240. CheckSource('TestExternalClass_Is',
  17241. LinesToStr([ // statements
  17242. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17243. ' this.$init = function () {',
  17244. ' };',
  17245. ' this.$final = function () {',
  17246. ' };',
  17247. '});',
  17248. 'this.A = null;',
  17249. 'this.B = null;',
  17250. 'this.C = null;',
  17251. '']),
  17252. LinesToStr([ // $mod.$main
  17253. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17254. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17255. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17256. '']));
  17257. end;
  17258. procedure TTestModule.TestExternalClass_DestructorFail;
  17259. begin
  17260. StartProgram(false);
  17261. Add('{$modeswitch externalclass}');
  17262. Add('type');
  17263. Add(' TExtA = class external name ''ExtA''');
  17264. Add(' destructor Free;');
  17265. Add(' end;');
  17266. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17267. nPasElementNotSupported);
  17268. ConvertProgram;
  17269. end;
  17270. procedure TTestModule.TestExternalClass_New;
  17271. begin
  17272. StartProgram(false);
  17273. Add([
  17274. '{$modeswitch externalclass}',
  17275. 'type',
  17276. ' TExtA = class external name ''ExtA''',
  17277. ' constructor New;',
  17278. ' constructor New(i: longint; j: longint = 2);',
  17279. ' end;',
  17280. 'var',
  17281. ' A: texta;',
  17282. 'begin',
  17283. ' a:=texta.new;',
  17284. ' a:=texta(texta.new);',
  17285. ' a:=texta.new();',
  17286. ' a:=texta.new(1);',
  17287. ' with texta do begin',
  17288. ' a:=new;',
  17289. ' a:=new();',
  17290. ' a:=new(2);',
  17291. ' end;',
  17292. ' a:=test1.texta.new;',
  17293. ' a:=test1.texta.new();',
  17294. ' a:=test1.texta.new(3);',
  17295. '']);
  17296. ConvertProgram;
  17297. CheckSource('TestExternalClass_New',
  17298. LinesToStr([ // statements
  17299. 'this.A = null;',
  17300. '']),
  17301. LinesToStr([ // $mod.$main
  17302. '$mod.A = new ExtA();',
  17303. '$mod.A = new ExtA();',
  17304. '$mod.A = new ExtA();',
  17305. '$mod.A = new ExtA(1,2);',
  17306. '$mod.A = new ExtA();',
  17307. '$mod.A = new ExtA();',
  17308. '$mod.A = new ExtA(2,2);',
  17309. '$mod.A = new ExtA();',
  17310. '$mod.A = new ExtA();',
  17311. '$mod.A = new ExtA(3,2);',
  17312. '']));
  17313. end;
  17314. procedure TTestModule.TestExternalClass_ClassOf_New;
  17315. begin
  17316. StartProgram(false);
  17317. Add('{$modeswitch externalclass}');
  17318. Add('type');
  17319. Add(' TExtAClass = class of TExtA;');
  17320. Add(' TExtA = class external name ''ExtA''');
  17321. Add(' C: TExtAClass;');
  17322. Add(' constructor New;');
  17323. Add(' end;');
  17324. Add('var');
  17325. Add(' A: texta;');
  17326. Add(' C: textaclass;');
  17327. Add('begin');
  17328. Add(' a:=c.new;');
  17329. Add(' a:=c.new();');
  17330. Add(' with C do begin');
  17331. Add(' a:=new;');
  17332. Add(' a:=new();');
  17333. Add(' end;');
  17334. Add(' a:=test1.c.new;');
  17335. Add(' a:=test1.c.new();');
  17336. Add(' a:=A.c.new();');
  17337. ConvertProgram;
  17338. CheckSource('TestExternalClass_ClassOf_New',
  17339. LinesToStr([ // statements
  17340. 'this.A = null;',
  17341. 'this.C = null;',
  17342. '']),
  17343. LinesToStr([ // $mod.$main
  17344. '$mod.A = new $mod.C();',
  17345. '$mod.A = new $mod.C();',
  17346. 'var $with = $mod.C;',
  17347. '$mod.A = new $with();',
  17348. '$mod.A = new $with();',
  17349. '$mod.A = new $mod.C();',
  17350. '$mod.A = new $mod.C();',
  17351. '$mod.A = new $mod.A.C();',
  17352. '']));
  17353. end;
  17354. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17355. begin
  17356. StartProgram(false);
  17357. Add([
  17358. '{$modeswitch externalclass}',
  17359. 'type',
  17360. ' TExtAClass = class of TExtA;',
  17361. ' TExtA = class external name ''ExtA''',
  17362. ' constructor New;',
  17363. ' end;',
  17364. 'function GetCreator: TExtAClass;',
  17365. 'begin',
  17366. ' Result:=TExtA;',
  17367. 'end;',
  17368. 'var',
  17369. ' A: texta;',
  17370. 'begin',
  17371. ' a:=getcreator.new;',
  17372. ' a:=getcreator().new;',
  17373. ' a:=getcreator().new();',
  17374. ' a:=getcreator.new();',
  17375. ' with getcreator do begin',
  17376. ' a:=new;',
  17377. ' a:=new();',
  17378. ' end;']);
  17379. ConvertProgram;
  17380. CheckSource('TestExternalClass_FuncClassOf_New',
  17381. LinesToStr([ // statements
  17382. 'this.GetCreator = function () {',
  17383. ' var Result = null;',
  17384. ' Result = ExtA;',
  17385. ' return Result;',
  17386. '};',
  17387. 'this.A = null;',
  17388. '']),
  17389. LinesToStr([ // $mod.$main
  17390. '$mod.A = new ($mod.GetCreator())();',
  17391. '$mod.A = new ($mod.GetCreator())();',
  17392. '$mod.A = new ($mod.GetCreator())();',
  17393. '$mod.A = new ($mod.GetCreator())();',
  17394. 'var $with = $mod.GetCreator();',
  17395. '$mod.A = new $with();',
  17396. '$mod.A = new $with();',
  17397. '']));
  17398. end;
  17399. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17400. begin
  17401. StartProgram(false);
  17402. Add([
  17403. '{$modeswitch externalclass}',
  17404. 'type',
  17405. ' TExtA = class external name ''ExtA''',
  17406. ' constructor New;',
  17407. ' end;',
  17408. ' TBird = class(TExtA)',
  17409. ' end;',
  17410. 'begin',
  17411. ' TBird.new;',
  17412. '']);
  17413. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17414. ConvertProgram;
  17415. end;
  17416. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17417. begin
  17418. StartProgram(false);
  17419. Add([
  17420. '{$modeswitch externalclass}',
  17421. 'type',
  17422. ' TExtA = class external name ''ExtA''',
  17423. ' constructor New;',
  17424. ' end;',
  17425. ' TBird = class(TExtA)',
  17426. ' end;',
  17427. 'begin',
  17428. ' TBird.new();',
  17429. '']);
  17430. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17431. ConvertProgram;
  17432. end;
  17433. procedure TTestModule.TestExternalClass_NewExtName;
  17434. begin
  17435. StartProgram(false);
  17436. Add([
  17437. '{$modeswitch externalclass}',
  17438. 'type',
  17439. ' TExtA = class external name ''ExtA''',
  17440. ' constructor New; external name ''Other'';',
  17441. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17442. ' end;',
  17443. 'var',
  17444. ' A: texta;',
  17445. 'begin',
  17446. ' a:=texta.new;',
  17447. ' a:=texta(texta.new);',
  17448. ' a:=texta.new();',
  17449. ' a:=texta.new(1);',
  17450. ' with texta do begin',
  17451. ' a:=new;',
  17452. ' a:=new();',
  17453. ' a:=new(2);',
  17454. ' end;',
  17455. ' a:=test1.texta.new;',
  17456. ' a:=test1.texta.new();',
  17457. ' a:=test1.texta.new(3);',
  17458. '']);
  17459. ConvertProgram;
  17460. CheckSource('TestExternalClass_NewExtName',
  17461. LinesToStr([ // statements
  17462. 'this.A = null;',
  17463. '']),
  17464. LinesToStr([ // $mod.$main
  17465. '$mod.A = new Other();',
  17466. '$mod.A = new Other();',
  17467. '$mod.A = new Other();',
  17468. '$mod.A = new A.B(1,2);',
  17469. '$mod.A = new Other();',
  17470. '$mod.A = new Other();',
  17471. '$mod.A = new A.B(2,2);',
  17472. '$mod.A = new Other();',
  17473. '$mod.A = new Other();',
  17474. '$mod.A = new A.B(3,2);',
  17475. '']));
  17476. end;
  17477. procedure TTestModule.TestExternalClass_Constructor;
  17478. begin
  17479. StartProgram(false);
  17480. Add([
  17481. '{$modeswitch externalclass}',
  17482. 'type',
  17483. ' TExtA = class external name ''ExtA''',
  17484. ' constructor Create;',
  17485. ' constructor Create(i: longint; j: longint = 2);',
  17486. ' end;',
  17487. 'var',
  17488. ' A: texta;',
  17489. 'begin',
  17490. ' a:=texta.create;',
  17491. ' a:=texta(texta.create);',
  17492. ' a:=texta.create();',
  17493. ' a:=texta.create(1);',
  17494. ' with texta do begin',
  17495. ' a:=create;',
  17496. ' a:=create();',
  17497. ' a:=create(2);',
  17498. ' end;',
  17499. ' a:=test1.texta.create;',
  17500. ' a:=test1.texta.create();',
  17501. ' a:=test1.texta.create(3);',
  17502. '']);
  17503. ConvertProgram;
  17504. CheckSource('TestExternalClass_Constructor',
  17505. LinesToStr([ // statements
  17506. 'this.A = null;',
  17507. '']),
  17508. LinesToStr([ // $mod.$main
  17509. '$mod.A = new ExtA.Create();',
  17510. '$mod.A = new ExtA.Create();',
  17511. '$mod.A = new ExtA.Create();',
  17512. '$mod.A = new ExtA.Create(1,2);',
  17513. '$mod.A = new ExtA.Create();',
  17514. '$mod.A = new ExtA.Create();',
  17515. '$mod.A = new ExtA.Create(2,2);',
  17516. '$mod.A = new ExtA.Create();',
  17517. '$mod.A = new ExtA.Create();',
  17518. '$mod.A = new ExtA.Create(3,2);',
  17519. '']));
  17520. end;
  17521. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17522. begin
  17523. StartProgram(false);
  17524. Add([
  17525. '{$modeswitch externalclass}',
  17526. 'type',
  17527. ' TExtA = class external name ''ExtA''',
  17528. ' constructor Create; external name ''{}'';',
  17529. ' end;',
  17530. 'var',
  17531. ' A: texta;',
  17532. 'begin',
  17533. ' a:=texta.create;',
  17534. ' a:=texta(texta.create);',
  17535. ' a:=texta.create();',
  17536. ' with texta do begin',
  17537. ' a:=create;',
  17538. ' a:=create();',
  17539. ' end;',
  17540. ' a:=test1.texta.create;',
  17541. ' a:=test1.texta.create();',
  17542. '']);
  17543. ConvertProgram;
  17544. CheckSource('TestExternalClass_ConstructorBrackets',
  17545. LinesToStr([ // statements
  17546. 'this.A = null;',
  17547. '']),
  17548. LinesToStr([ // $mod.$main
  17549. '$mod.A = {};',
  17550. '$mod.A = {};',
  17551. '$mod.A = {};',
  17552. '$mod.A = {};',
  17553. '$mod.A = {};',
  17554. '$mod.A = {};',
  17555. '$mod.A = {};',
  17556. '']));
  17557. end;
  17558. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17559. begin
  17560. StartProgram(false);
  17561. Add('{$modeswitch externalclass}');
  17562. Add('type');
  17563. Add(' TExtA = class external name ''ExtA''');
  17564. Add(' constructor New;');
  17565. Add(' end;');
  17566. Add('function DoIt: longint;');
  17567. Add('const ExtA: longint = 3;');
  17568. Add('begin');
  17569. Add(' Result:=ExtA;');
  17570. Add('end;');
  17571. Add('var');
  17572. Add(' A: texta;');
  17573. Add('begin');
  17574. Add(' a:=texta.new;');
  17575. ConvertProgram;
  17576. CheckSource('TestExternalClass_LocalConstSameName',
  17577. LinesToStr([ // statements
  17578. 'var ExtA$1 = 3;',
  17579. 'this.DoIt = function () {',
  17580. ' var Result = 0;',
  17581. ' Result = ExtA$1;',
  17582. ' return Result;',
  17583. '};',
  17584. 'this.A = null;',
  17585. '']),
  17586. LinesToStr([ // $mod.$main
  17587. '$mod.A = new ExtA();',
  17588. '']));
  17589. end;
  17590. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17591. begin
  17592. StartProgram(false);
  17593. Add('{$modeswitch externalclass}');
  17594. Add('type');
  17595. Add(' TExtA = class external name ''ExtA''');
  17596. Add(' procedure DoIt;');
  17597. Add(' end;');
  17598. Add(' TMyA = class(TExtA)');
  17599. Add(' procedure DoIt;');
  17600. Add(' end;');
  17601. Add('procedure TMyA.DoIt; begin end;');
  17602. Add('begin');
  17603. ConvertProgram;
  17604. CheckSource('TestExternalClass_ReintroduceOverload',
  17605. LinesToStr([ // statements
  17606. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17607. ' this.$init = function () {',
  17608. ' };',
  17609. ' this.$final = function () {',
  17610. ' };',
  17611. ' this.DoIt$1 = function () {',
  17612. ' };',
  17613. '});',
  17614. '']),
  17615. LinesToStr([ // $mod.$main
  17616. '']));
  17617. end;
  17618. procedure TTestModule.TestExternalClass_Inherited;
  17619. begin
  17620. StartProgram(false);
  17621. Add('{$modeswitch externalclass}');
  17622. Add('type');
  17623. Add(' TExtA = class external name ''ExtA''');
  17624. Add(' procedure DoIt(i: longint = 1); virtual;');
  17625. Add(' procedure DoSome(j: longint = 2);');
  17626. Add(' end;');
  17627. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17628. Add(' end;');
  17629. Add(' TMyC = class(TExtB)');
  17630. Add(' procedure DoIt(i: longint = 1); override;');
  17631. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17632. Add(' end;');
  17633. Add('procedure TMyC.DoIt(i: longint);');
  17634. Add('begin');
  17635. Add(' inherited;');
  17636. Add(' inherited DoIt;');
  17637. Add(' inherited DoIt();');
  17638. Add(' inherited DoIt(3);');
  17639. Add(' inherited DoSome;');
  17640. Add(' inherited DoSome();');
  17641. Add(' inherited DoSome(4);');
  17642. Add('end;');
  17643. Add('procedure TMyC.DoSome(j: longint);');
  17644. Add('begin');
  17645. Add(' inherited;');
  17646. Add('end;');
  17647. Add('begin');
  17648. ConvertProgram;
  17649. CheckSource('TestExternalClass_ReintroduceOverload',
  17650. LinesToStr([ // statements
  17651. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17652. ' this.$init = function () {',
  17653. ' };',
  17654. ' this.$final = function () {',
  17655. ' };',
  17656. ' this.DoIt = function (i) {',
  17657. ' ExtB.DoIt.apply(this, arguments);',
  17658. ' ExtB.DoIt.call(this, 1);',
  17659. ' ExtB.DoIt.call(this, 1);',
  17660. ' ExtB.DoIt.call(this, 3);',
  17661. ' ExtB.DoSome.call(this, 2);',
  17662. ' ExtB.DoSome.call(this, 2);',
  17663. ' ExtB.DoSome.call(this, 4);',
  17664. ' };',
  17665. ' this.DoSome$1 = function (j) {',
  17666. ' ExtB.DoSome.apply(this, arguments);',
  17667. ' };',
  17668. '});',
  17669. '']),
  17670. LinesToStr([ // $mod.$main
  17671. '']));
  17672. end;
  17673. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17674. begin
  17675. StartProgram(false);
  17676. Add('{$modeswitch externalclass}');
  17677. Add('type');
  17678. Add(' TObject = class');
  17679. Add(' end;');
  17680. Add(' TExtA = class external name ''ExtA''(TObject)');
  17681. Add(' end;');
  17682. Add('begin');
  17683. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17684. ConvertProgram;
  17685. end;
  17686. procedure TTestModule.TestExternalClass_NewInstance;
  17687. begin
  17688. StartProgram(false);
  17689. Add('{$modeswitch externalclass}');
  17690. Add('type');
  17691. Add(' TExtA = class external name ''ExtA''');
  17692. Add(' end;');
  17693. Add(' TMyB = class(TExtA)');
  17694. Add(' protected');
  17695. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17696. Add(' end;');
  17697. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17698. Add('begin end;');
  17699. Add('begin');
  17700. ConvertProgram;
  17701. CheckSource('TestExternalClass_NewInstance',
  17702. LinesToStr([ // statements
  17703. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17704. ' this.$init = function () {',
  17705. ' };',
  17706. ' this.$final = function () {',
  17707. ' };',
  17708. ' this.NewInstance = function (fnname, paramarray) {',
  17709. ' var Result = null;',
  17710. ' return Result;',
  17711. ' };',
  17712. '});',
  17713. '']),
  17714. LinesToStr([ // $mod.$main
  17715. '']));
  17716. end;
  17717. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17718. begin
  17719. StartProgram(false);
  17720. Add('{$modeswitch externalclass}');
  17721. Add('type');
  17722. Add(' TExtA = class external name ''ExtA''');
  17723. Add(' end;');
  17724. Add(' TMyB = class(TExtA)');
  17725. Add(' protected');
  17726. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17727. Add(' end;');
  17728. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17729. Add('begin end;');
  17730. Add('begin');
  17731. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17732. ConvertProgram;
  17733. end;
  17734. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17735. begin
  17736. StartProgram(false);
  17737. Add('{$modeswitch externalclass}');
  17738. Add('type');
  17739. Add(' TExtA = class external name ''ExtA''');
  17740. Add(' end;');
  17741. Add(' TMyB = class(TExtA)');
  17742. Add(' protected');
  17743. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17744. Add(' end;');
  17745. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17746. Add('begin end;');
  17747. Add('begin');
  17748. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17749. nIncompatibleTypeArgNo);
  17750. ConvertProgram;
  17751. end;
  17752. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17753. begin
  17754. StartProgram(false);
  17755. Add('{$modeswitch externalclass}');
  17756. Add('type');
  17757. Add(' TExtA = class external name ''ExtA''');
  17758. Add(' end;');
  17759. Add(' TMyB = class(TExtA)');
  17760. Add(' protected');
  17761. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17762. Add(' end;');
  17763. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17764. Add('begin end;');
  17765. Add('begin');
  17766. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17767. nIncompatibleTypeArgNo);
  17768. ConvertProgram;
  17769. end;
  17770. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17771. begin
  17772. StartProgram(false);
  17773. Add([
  17774. '{$modeswitch externalclass}',
  17775. 'type',
  17776. ' TJSFunction = class external name ''Function''',
  17777. ' end;',
  17778. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17779. ' constructor New(w: word);',
  17780. ' end;',
  17781. ' TBird = class (TExtA)',
  17782. ' public',
  17783. ' Size: word;',
  17784. ' class var Legs: word;',
  17785. ' constructor Create(a: word);',
  17786. ' end;',
  17787. ' TEagle = class (TBird)',
  17788. ' public',
  17789. ' constructor Create(b: word); reintroduce;',
  17790. ' end;',
  17791. 'constructor TBird.Create(a: word);',
  17792. 'begin',
  17793. ' inherited;', // silently ignored
  17794. ' inherited New(a);', // this.$func(a)
  17795. 'end;',
  17796. 'constructor TEagle.Create(b: word);',
  17797. 'begin',
  17798. ' inherited Create(b);',
  17799. 'end;',
  17800. 'var',
  17801. ' Bird: TBird;',
  17802. ' Eagle: TEagle;',
  17803. 'begin',
  17804. ' Bird:=TBird.Create(3);',
  17805. ' Eagle:=TEagle.Create(4);',
  17806. ' Bird.Size:=Bird.Size+5;',
  17807. ' Bird.Legs:=Bird.Legs+6;',
  17808. ' Eagle.Size:=Eagle.Size+5;',
  17809. ' Eagle.Legs:=Eagle.Legs+6;',
  17810. '']);
  17811. ConvertProgram;
  17812. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17813. LinesToStr([ // statements
  17814. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17815. ' this.Legs = 0;',
  17816. ' this.$init = function () {',
  17817. ' this.Size = 0;',
  17818. ' };',
  17819. ' this.$final = function () {',
  17820. ' };',
  17821. ' this.Create = function (a) {',
  17822. ' this.$ancestorfunc(a);',
  17823. ' return this;',
  17824. ' };',
  17825. '});',
  17826. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17827. ' this.Create$1 = function (b) {',
  17828. ' $mod.TBird.Create.call(this, b);',
  17829. ' return this;',
  17830. ' };',
  17831. '});',
  17832. 'this.Bird = null;',
  17833. 'this.Eagle = null;',
  17834. '']),
  17835. LinesToStr([ // $mod.$main
  17836. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17837. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17838. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17839. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17840. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17841. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17842. '']));
  17843. end;
  17844. procedure TTestModule.TestExternalClass_PascalProperty;
  17845. begin
  17846. StartProgram(false);
  17847. Add('{$modeswitch externalclass}');
  17848. Add('type');
  17849. Add(' TJSElement = class;');
  17850. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17851. Add(' TJSElement = class external name ''ExtA''');
  17852. Add(' end;');
  17853. Add(' TControl = class(TJSElement)');
  17854. Add(' private');
  17855. Add(' FOnClick: TJSNotifyEvent;');
  17856. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17857. Add(' procedure Click(Sender: TJSElement);');
  17858. Add(' end;');
  17859. Add('procedure TControl.Click(Sender: TJSElement);');
  17860. Add('begin');
  17861. Add(' OnClick(Self);');
  17862. Add('end;');
  17863. Add('var');
  17864. Add(' Ctrl: TControl;');
  17865. Add('begin');
  17866. Add(' Ctrl.OnClick:[email protected];');
  17867. Add(' Ctrl.OnClick(Ctrl);');
  17868. ConvertProgram;
  17869. CheckSource('TestExternalClass_PascalProperty',
  17870. LinesToStr([ // statements
  17871. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17872. ' this.$init = function () {',
  17873. ' this.FOnClick = null;',
  17874. ' };',
  17875. ' this.$final = function () {',
  17876. ' this.FOnClick = undefined;',
  17877. ' };',
  17878. ' this.Click = function (Sender) {',
  17879. ' this.FOnClick(this);',
  17880. ' };',
  17881. '});',
  17882. 'this.Ctrl = null;',
  17883. '']),
  17884. LinesToStr([ // $mod.$main
  17885. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17886. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17887. '']));
  17888. end;
  17889. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17890. begin
  17891. StartProgram(false);
  17892. Add([
  17893. '{$modeswitch externalclass}',
  17894. 'type',
  17895. ' IUnknown = interface end;',
  17896. ' TObject = class',
  17897. ' end;',
  17898. ' TChild = class',
  17899. ' end;',
  17900. ' TExtRootA = class external name ''ExtRootA''',
  17901. ' end;',
  17902. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17903. ' end;',
  17904. ' TExtRootB = class external name ''ExtRootB''',
  17905. ' end;',
  17906. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17907. ' end;',
  17908. 'var',
  17909. ' Obj: TObject;',
  17910. ' Child: TChild;',
  17911. ' RootA: TExtRootA;',
  17912. ' ChildA: TExtChildA;',
  17913. ' RootB: TExtRootB;',
  17914. ' ChildB: TExtChildB;',
  17915. ' i: IUnknown;',
  17916. 'begin',
  17917. ' obj:=tobject(roota);',
  17918. ' obj:=tobject(childa);',
  17919. ' child:=tchild(tobject(roota));',
  17920. ' roota:=textroota(obj);',
  17921. ' roota:=textroota(child);',
  17922. ' roota:=textroota(rootb);',
  17923. ' roota:=textroota(childb);',
  17924. ' childa:=textchilda(textroota(obj));',
  17925. ' roota:=TExtRootA(i)',
  17926. '']);
  17927. ConvertProgram;
  17928. CheckSource('TestExternalClass_TypeCastToRootClass',
  17929. LinesToStr([ // statements
  17930. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17931. 'rtl.createClass(this, "TObject", null, function () {',
  17932. ' this.$init = function () {',
  17933. ' };',
  17934. ' this.$final = function () {',
  17935. ' };',
  17936. '});',
  17937. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17938. '});',
  17939. 'this.Obj = null;',
  17940. 'this.Child = null;',
  17941. 'this.RootA = null;',
  17942. 'this.ChildA = null;',
  17943. 'this.RootB = null;',
  17944. 'this.ChildB = null;',
  17945. 'this.i = null;',
  17946. '']),
  17947. LinesToStr([ // $mod.$main
  17948. '$mod.Obj = $mod.RootA;',
  17949. '$mod.Obj = $mod.ChildA;',
  17950. '$mod.Child = $mod.RootA;',
  17951. '$mod.RootA = $mod.Obj;',
  17952. '$mod.RootA = $mod.Child;',
  17953. '$mod.RootA = $mod.RootB;',
  17954. '$mod.RootA = $mod.ChildB;',
  17955. '$mod.ChildA = $mod.Obj;',
  17956. '$mod.RootA = $mod.i;',
  17957. '']));
  17958. end;
  17959. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17960. begin
  17961. StartProgram(false);
  17962. Add([
  17963. '{$modeswitch externalclass}',
  17964. 'type',
  17965. ' IUnknown = interface end;',
  17966. ' IBird = interface(IUnknown) end;',
  17967. ' TClass = class of TObject;',
  17968. ' TObject = class',
  17969. ' end;',
  17970. ' TChild = class',
  17971. ' end;',
  17972. ' TJSObject = class external name ''Object''',
  17973. ' end;',
  17974. ' TRec = record end;',
  17975. 'var',
  17976. ' Obj: TObject;',
  17977. ' Child: TChild;',
  17978. ' i: IUnknown;',
  17979. ' Bird: IBird;',
  17980. ' j: TJSObject;',
  17981. ' r: TRec;',
  17982. ' c: TClass;',
  17983. 'begin',
  17984. ' j:=tjsobject(IUnknown);',
  17985. ' j:=tjsobject(IBird);',
  17986. ' j:=tjsobject(TObject);',
  17987. ' j:=tjsobject(TChild);',
  17988. ' j:=tjsobject(TRec);',
  17989. ' j:=tjsobject(Obj);',
  17990. ' j:=tjsobject(Child);',
  17991. ' j:=tjsobject(i);',
  17992. ' j:=tjsobject(Bird);',
  17993. ' j:=tjsobject(r);',
  17994. ' j:=tjsobject(c);',
  17995. '']);
  17996. ConvertProgram;
  17997. CheckSource('TestExternalClass_TypeCastToJSObject',
  17998. LinesToStr([ // statements
  17999. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18000. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18001. 'rtl.createClass(this, "TObject", null, function () {',
  18002. ' this.$init = function () {',
  18003. ' };',
  18004. ' this.$final = function () {',
  18005. ' };',
  18006. '});',
  18007. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18008. '});',
  18009. 'rtl.recNewT(this, "TRec", function () {',
  18010. ' this.$eq = function (b) {',
  18011. ' return true;',
  18012. ' };',
  18013. ' this.$assign = function (s) {',
  18014. ' return this;',
  18015. ' };',
  18016. '});',
  18017. 'this.Obj = null;',
  18018. 'this.Child = null;',
  18019. 'this.i = null;',
  18020. 'this.Bird = null;',
  18021. 'this.j = null;',
  18022. 'this.r = this.TRec.$new();',
  18023. 'this.c = null;',
  18024. '']),
  18025. LinesToStr([ // $mod.$main
  18026. '$mod.j = $mod.IUnknown;',
  18027. '$mod.j = $mod.IBird;',
  18028. '$mod.j = $mod.TObject;',
  18029. '$mod.j = $mod.TChild;',
  18030. '$mod.j = $mod.TRec;',
  18031. '$mod.j = $mod.Obj;',
  18032. '$mod.j = $mod.Child;',
  18033. '$mod.j = $mod.i;',
  18034. '$mod.j = $mod.Bird;',
  18035. '$mod.j = $mod.r;',
  18036. '$mod.j = $mod.c;',
  18037. '']));
  18038. end;
  18039. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18040. begin
  18041. StartProgram(false);
  18042. Add('{$modeswitch externalclass}');
  18043. Add('type');
  18044. Add(' TJSString = class external name ''String''');
  18045. Add(' class function fromCharCode() : string; varargs;');
  18046. Add(' function anchor(const aName : string) : string;');
  18047. Add(' end;');
  18048. Add('var');
  18049. Add(' s: string;');
  18050. Add('begin');
  18051. Add(' s:=TJSString.fromCharCode(65,66);');
  18052. Add(' s:=TJSString(s).anchor(s);');
  18053. Add(' s:=TJSString(''foo'').anchor(s);');
  18054. ConvertProgram;
  18055. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18056. LinesToStr([ // statements
  18057. 'this.s = "";',
  18058. '']),
  18059. LinesToStr([ // $mod.$main
  18060. '$mod.s = String.fromCharCode(65, 66);',
  18061. '$mod.s = $mod.s.anchor($mod.s);',
  18062. '$mod.s = "foo".anchor($mod.s);',
  18063. '']));
  18064. end;
  18065. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18066. begin
  18067. StartProgram(false);
  18068. Add([
  18069. '{$modeswitch externalclass}',
  18070. 'type',
  18071. ' TJSObject = class external name ''Object'' end;',
  18072. ' TJSFunction = class external name ''Function''',
  18073. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18074. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18075. ' end;',
  18076. ' TObject = class',
  18077. ' procedure DoIt(i: longint);',
  18078. ' end;',
  18079. ' TFuncInt = function(o: TObject): longint;',
  18080. 'function GetIt(o: TObject): longint;',
  18081. ' procedure Sub; begin end;',
  18082. 'var',
  18083. ' f: TJSFunction;',
  18084. ' fi: TFuncInt;',
  18085. 'begin',
  18086. ' fi:=TFuncInt(f);',
  18087. ' f:=TJSFunction(fi);',
  18088. ' f:=TJSFunction(@GetIt);',
  18089. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18090. ' f:=TJSFunction(@Sub);',
  18091. ' f:=TJSFunction(@o.doit);',
  18092. ' f:=TJSFunction(fi).bind(nil,4)',
  18093. 'end;',
  18094. 'procedure TObject.DoIt(i: longint);',
  18095. ' procedure Sub; begin end;',
  18096. 'var f: TJSFunction;',
  18097. 'begin',
  18098. ' f:=TJSFunction(@DoIt);',
  18099. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18100. ' f:=TJSFunction(@Sub);',
  18101. ' f:=TJSFunction(@GetIt);',
  18102. 'end;',
  18103. 'begin']);
  18104. ConvertProgram;
  18105. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18106. LinesToStr([ // statements
  18107. 'rtl.createClass(this, "TObject", null, function () {',
  18108. ' this.$init = function () {',
  18109. ' };',
  18110. ' this.$final = function () {',
  18111. ' };',
  18112. ' this.DoIt = function (i) {',
  18113. ' var $Self = this;',
  18114. ' function Sub() {',
  18115. ' };',
  18116. ' var f = null;',
  18117. ' f = this.DoIt;',
  18118. ' f = this.DoIt.bind(null, 13);',
  18119. ' f = Sub;',
  18120. ' f = $mod.GetIt;',
  18121. ' };',
  18122. '});',
  18123. 'this.GetIt = function (o) {',
  18124. ' var Result = 0;',
  18125. ' function Sub() {',
  18126. ' };',
  18127. ' var f = null;',
  18128. ' var fi = null;',
  18129. ' fi = f;',
  18130. ' f = fi;',
  18131. ' f = $mod.GetIt;',
  18132. ' f = $mod.GetIt.bind(null, 3);',
  18133. ' f = Sub;',
  18134. ' f = $mod.TObject.DoIt;',
  18135. ' f = fi.bind(null, 4);',
  18136. ' return Result;',
  18137. '};',
  18138. '']),
  18139. LinesToStr([ // $mod.$main
  18140. '']));
  18141. end;
  18142. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18143. begin
  18144. StartProgram(false);
  18145. Add([
  18146. '{$mode delphi}',
  18147. '{$modeswitch externalclass}',
  18148. 'type',
  18149. ' TJSObject = class external name ''Object'' end;',
  18150. ' TJSWindow = class external name ''Window''(TJSObject)',
  18151. ' procedure Open;',
  18152. ' end;',
  18153. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18154. ' procedure Execute;',
  18155. ' end;',
  18156. 'procedure Fly;',
  18157. 'var',
  18158. ' w: TJSWindow;',
  18159. ' e: TJSEventTarget;',
  18160. 'begin',
  18161. ' w:=TJSWindow(e);',
  18162. ' e:=TJSEventTarget(w);',
  18163. 'end;',
  18164. 'begin']);
  18165. ConvertProgram;
  18166. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18167. LinesToStr([ // statements
  18168. 'this.Fly = function () {',
  18169. ' var w = null;',
  18170. ' var e = null;',
  18171. ' w = e;',
  18172. ' e = w;',
  18173. '};',
  18174. '']),
  18175. LinesToStr([ // $mod.$main
  18176. '']));
  18177. end;
  18178. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18179. begin
  18180. StartProgram(false);
  18181. Add('{$modeswitch externalclass}');
  18182. Add('type');
  18183. Add(' TJSString = class external name ''String''');
  18184. Add(' class function fromCharCode() : string; varargs;');
  18185. Add(' end;');
  18186. Add('var');
  18187. Add(' s: string;');
  18188. Add(' sObj: TJSString;');
  18189. Add('begin');
  18190. Add(' s:=sObj.fromCharCode(65,66);');
  18191. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18192. nExternalClassInstanceCannotAccessStaticX);
  18193. ConvertProgram;
  18194. end;
  18195. procedure TTestModule.TestExternalClass_BracketAccessor;
  18196. begin
  18197. StartProgram(false);
  18198. Add([
  18199. '{$modeswitch externalclass}',
  18200. 'type',
  18201. ' TJSArray = class external name ''Array2''',
  18202. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18203. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18204. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18205. ' end;',
  18206. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18207. 'begin end;',
  18208. 'var',
  18209. ' Arr: tjsarray;',
  18210. ' s: string;',
  18211. ' i: longint;',
  18212. ' v: jsvalue;',
  18213. 'begin',
  18214. ' v:=arr[0];',
  18215. ' v:=arr.items[1];',
  18216. ' arr[2]:=s;',
  18217. ' arr.items[3]:=s;',
  18218. ' arr[4]:=i;',
  18219. ' arr[5]:=arr[6];',
  18220. ' arr.items[7]:=arr.items[8];',
  18221. ' with arr do items[9]:=items[10];',
  18222. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18223. ' with arr do begin',
  18224. ' v:=GetItems(14);',
  18225. ' setitems(15,16);',
  18226. ' end;',
  18227. ' v:=test1.arr.items[17];',
  18228. ' test1.arr.items[18]:=v;',
  18229. '']);
  18230. ConvertProgram;
  18231. CheckSource('TestExternalClass_BracketAccessor',
  18232. LinesToStr([ // statements
  18233. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18234. '};',
  18235. 'this.Arr = null;',
  18236. 'this.s = "";',
  18237. 'this.i = 0;',
  18238. 'this.v = undefined;',
  18239. '']),
  18240. LinesToStr([ // $mod.$main
  18241. '$mod.v = $mod.Arr[0];',
  18242. '$mod.v = $mod.Arr[1];',
  18243. '$mod.Arr[2] = $mod.s;',
  18244. '$mod.Arr[3] = $mod.s;',
  18245. '$mod.Arr[4] = $mod.i;',
  18246. '$mod.Arr[5] = $mod.Arr[6];',
  18247. '$mod.Arr[7] = $mod.Arr[8];',
  18248. 'var $with = $mod.Arr;',
  18249. '$with[9] = $with[10];',
  18250. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18251. ' a: 9,',
  18252. ' p: $mod.Arr,',
  18253. ' get: function () {',
  18254. ' return this.p[this.a];',
  18255. ' },',
  18256. ' set: function (v) {',
  18257. ' this.p[this.a] = v;',
  18258. ' }',
  18259. '}, {',
  18260. ' a: 10,',
  18261. ' p: $mod.Arr,',
  18262. ' get: function () {',
  18263. ' return this.p[this.a];',
  18264. ' },',
  18265. ' set: function (v) {',
  18266. ' this.p[this.a] = v;',
  18267. ' }',
  18268. '});',
  18269. 'var $with1 = $mod.Arr;',
  18270. '$mod.v = $with1[14];',
  18271. '$with1[15] = 16;',
  18272. '$mod.v = $mod.Arr[17];',
  18273. '$mod.Arr[18] = $mod.v;',
  18274. '']));
  18275. end;
  18276. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18277. begin
  18278. StartProgram(false);
  18279. Add([
  18280. '{$modeswitch externalclass}',
  18281. 'type',
  18282. ' TJSArray = class external name ''Array2''',
  18283. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18284. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18285. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18286. ' end;',
  18287. ' TMyArr = class(TJSArray)',
  18288. ' procedure DoIt;',
  18289. ' end;',
  18290. 'procedure tmyarr.DoIt;',
  18291. 'begin',
  18292. ' Items[1]:=Items[2];',
  18293. ' SetItems(3,getItems(4));',
  18294. 'end;',
  18295. 'var',
  18296. ' Arr: tmyarr;',
  18297. ' s: string;',
  18298. ' i: longint;',
  18299. ' v: jsvalue;',
  18300. 'begin',
  18301. ' v:=arr[0];',
  18302. ' v:=arr.items[1];',
  18303. ' arr[2]:=s;',
  18304. ' arr.items[3]:=s;',
  18305. ' arr[4]:=i;',
  18306. ' arr[5]:=arr[6];',
  18307. ' arr.items[7]:=arr.items[8];',
  18308. ' with arr do items[9]:=items[10];',
  18309. ' with arr do begin',
  18310. ' v:=GetItems(14);',
  18311. ' setitems(15,16);',
  18312. ' end;',
  18313. '']);
  18314. ConvertProgram;
  18315. CheckSource('TestExternalClass_BracketAccessor_Call',
  18316. LinesToStr([ // statements
  18317. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18318. ' this.$init = function () {',
  18319. ' };',
  18320. ' this.$final = function () {',
  18321. ' };',
  18322. ' this.DoIt = function () {',
  18323. ' this[1] = this[2];',
  18324. ' this[3] = this[4];',
  18325. ' };',
  18326. '});',
  18327. 'this.Arr = null;',
  18328. 'this.s = "";',
  18329. 'this.i = 0;',
  18330. 'this.v = undefined;',
  18331. '']),
  18332. LinesToStr([ // $mod.$main
  18333. '$mod.v = $mod.Arr[0];',
  18334. '$mod.v = $mod.Arr[1];',
  18335. '$mod.Arr[2] = $mod.s;',
  18336. '$mod.Arr[3] = $mod.s;',
  18337. '$mod.Arr[4] = $mod.i;',
  18338. '$mod.Arr[5] = $mod.Arr[6];',
  18339. '$mod.Arr[7] = $mod.Arr[8];',
  18340. 'var $with = $mod.Arr;',
  18341. '$with[9] = $with[10];',
  18342. 'var $with1 = $mod.Arr;',
  18343. '$mod.v = $with1[14];',
  18344. '$with1[15] = 16;',
  18345. '']));
  18346. end;
  18347. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18348. begin
  18349. StartProgram(false);
  18350. Add('{$modeswitch externalclass}');
  18351. Add('type');
  18352. Add(' TJSArray = class external name ''Array2''');
  18353. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18354. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18355. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18356. Add(' end;');
  18357. Add('begin');
  18358. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18359. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18360. ConvertProgram;
  18361. end;
  18362. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18363. begin
  18364. StartProgram(false);
  18365. Add('{$modeswitch externalclass}');
  18366. Add('type');
  18367. Add(' TJSArray = class external name ''Array2''');
  18368. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18369. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18370. Add(' end;');
  18371. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18372. Add('begin end;');
  18373. Add('var');
  18374. Add(' Arr: tjsarray;');
  18375. Add(' v: jsvalue;');
  18376. Add('begin');
  18377. Add(' v:=arr[0];');
  18378. Add(' v:=arr.items[1];');
  18379. Add(' with arr do v:=items[2];');
  18380. Add(' doit(arr[3],arr[4]);');
  18381. ConvertProgram;
  18382. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18383. LinesToStr([ // statements
  18384. 'this.DoIt = function (vI, vJ) {',
  18385. '};',
  18386. 'this.Arr = null;',
  18387. 'this.v = undefined;',
  18388. '']),
  18389. LinesToStr([ // $mod.$main
  18390. '$mod.v = $mod.Arr[0];',
  18391. '$mod.v = $mod.Arr[1];',
  18392. 'var $with = $mod.Arr;',
  18393. '$mod.v = $with[2];',
  18394. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18395. '']));
  18396. end;
  18397. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18398. begin
  18399. StartProgram(false);
  18400. Add('{$modeswitch externalclass}');
  18401. Add('type');
  18402. Add(' TJSArray = class external name ''Array2''');
  18403. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18404. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18405. Add(' end;');
  18406. Add('var');
  18407. Add(' Arr: tjsarray;');
  18408. Add(' s: string;');
  18409. Add(' i: longint;');
  18410. Add(' v: jsvalue;');
  18411. Add('begin');
  18412. Add(' arr[2]:=s;');
  18413. Add(' arr.items[3]:=s;');
  18414. Add(' arr[4]:=i;');
  18415. Add(' with arr do items[5]:=i;');
  18416. ConvertProgram;
  18417. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18418. LinesToStr([ // statements
  18419. 'this.Arr = null;',
  18420. 'this.s = "";',
  18421. 'this.i = 0;',
  18422. 'this.v = undefined;',
  18423. '']),
  18424. LinesToStr([ // $mod.$main
  18425. '$mod.Arr[2] = $mod.s;',
  18426. '$mod.Arr[3] = $mod.s;',
  18427. '$mod.Arr[4] = $mod.i;',
  18428. 'var $with = $mod.Arr;',
  18429. '$with[5] = $mod.i;',
  18430. '']));
  18431. end;
  18432. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18433. begin
  18434. StartProgram(false);
  18435. Add('{$modeswitch externalclass}');
  18436. Add('type');
  18437. Add(' TJSArray = class external name ''Array2''');
  18438. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18439. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18440. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18441. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18442. Add(' end;');
  18443. Add('var');
  18444. Add(' Arr: tjsarray;');
  18445. Add(' s: string;');
  18446. Add(' i: longint;');
  18447. Add(' v: jsvalue;');
  18448. Add('begin');
  18449. Add(' arr[2]:=s;');
  18450. Add(' arr.items[3]:=s;');
  18451. Add(' arr.numbers[4]:=i;');
  18452. Add(' with arr do items[5]:=i;');
  18453. Add(' with arr do numbers[6]:=i;');
  18454. ConvertProgram;
  18455. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18456. LinesToStr([ // statements
  18457. 'this.Arr = null;',
  18458. 'this.s = "";',
  18459. 'this.i = 0;',
  18460. 'this.v = undefined;',
  18461. '']),
  18462. LinesToStr([ // $mod.$main
  18463. '$mod.Arr[2] = $mod.s;',
  18464. '$mod.Arr[3] = $mod.s;',
  18465. '$mod.Arr[4] = $mod.i;',
  18466. 'var $with = $mod.Arr;',
  18467. '$with[5] = $mod.i;',
  18468. 'var $with1 = $mod.Arr;',
  18469. '$with1[6] = $mod.i;',
  18470. '']));
  18471. end;
  18472. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18473. begin
  18474. StartProgram(false);
  18475. Add('{$modeswitch externalclass}');
  18476. Add('type');
  18477. Add(' TJSArray = class external name ''Array2''');
  18478. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18479. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18480. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18481. Add(' end;');
  18482. Add('var');
  18483. Add(' Arr: tjsarray;');
  18484. Add(' i: longint;');
  18485. Add(' IntArr: array of longint;');
  18486. Add(' v: jsvalue;');
  18487. Add('begin');
  18488. Add(' v:=arr.items[i];');
  18489. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18490. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18491. ConvertProgram;
  18492. CheckSource('TestExternalClass_BracketAccessor_Index',
  18493. LinesToStr([ // statements
  18494. 'this.Arr = null;',
  18495. 'this.i = 0;',
  18496. 'this.IntArr = [];',
  18497. 'this.v = undefined;',
  18498. '']),
  18499. LinesToStr([ // $mod.$main
  18500. '$mod.v = $mod.Arr[$mod.i];',
  18501. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18502. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18503. '']));
  18504. end;
  18505. procedure TTestModule.TestExternalClass_ForInJSObject;
  18506. begin
  18507. StartProgram(false);
  18508. Add([
  18509. '{$modeswitch externalclass}',
  18510. 'type',
  18511. ' TJSObject = class external name ''Object''',
  18512. ' end;',
  18513. 'var',
  18514. ' o: TJSObject;',
  18515. ' key: string;',
  18516. 'begin',
  18517. ' for key in o do',
  18518. ' if key=''abc'' then ;',
  18519. '']);
  18520. ConvertProgram;
  18521. CheckSource('TestExternalClass_ForInJSObject',
  18522. LinesToStr([ // statements
  18523. 'this.o = null;',
  18524. 'this.key = "";',
  18525. '']),
  18526. LinesToStr([ // $mod.$main
  18527. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18528. '']));
  18529. end;
  18530. procedure TTestModule.TestExternalClass_ForInJSArray;
  18531. begin
  18532. StartProgram(false);
  18533. Add([
  18534. '{$modeswitch externalclass}',
  18535. 'type',
  18536. ' TJSInt8Array = class external name ''Int8Array''',
  18537. ' private',
  18538. ' flength: NativeInt external name ''length'';',
  18539. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18540. ' public',
  18541. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18542. ' property Length: NativeInt read flength;',
  18543. ' end;',
  18544. 'var',
  18545. ' a: TJSInt8Array;',
  18546. ' value: shortint;',
  18547. 'begin',
  18548. ' for value in a do',
  18549. ' if value=3 then ;',
  18550. '']);
  18551. ConvertProgram;
  18552. CheckSource('TestExternalClass_ForInJSArray',
  18553. LinesToStr([ // statements
  18554. 'this.a = null;',
  18555. 'this.value = 0;',
  18556. '']),
  18557. LinesToStr([ // $mod.$main
  18558. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18559. ' $mod.value = $in[$l];',
  18560. ' if ($mod.value === 3) ;',
  18561. '};',
  18562. '']));
  18563. end;
  18564. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18565. begin
  18566. AddModuleWithIntfImplSrc('unit2.pas',
  18567. LinesToStr([
  18568. '{$modeswitch externalclass}',
  18569. 'type',
  18570. ' TJSBufferSource = class external name ''BufferSource''',
  18571. ' end;',
  18572. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18573. '']),
  18574. '');
  18575. AddModuleWithIntfImplSrc('unit3.pas',
  18576. LinesToStr([
  18577. '{$modeswitch externalclass}',
  18578. 'type',
  18579. ' TJSBufferSource = class external name ''BufferSource''',
  18580. ' end;',
  18581. '']),
  18582. '');
  18583. StartUnit(true);
  18584. Add([
  18585. 'interface',
  18586. 'uses unit2, unit3;',
  18587. 'procedure DoSome(s: TJSBufferSource);',
  18588. 'implementation',
  18589. 'procedure DoSome(s: TJSBufferSource);',
  18590. 'begin',
  18591. ' DoIt(s);',
  18592. 'end;',
  18593. '']);
  18594. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18595. nIncompatibleTypeArgNo);
  18596. ConvertUnit;
  18597. end;
  18598. procedure TTestModule.TestClassInterface_Corba;
  18599. begin
  18600. StartProgram(false);
  18601. Add([
  18602. '{$interfaces corba}',
  18603. 'type',
  18604. ' IUnknown = interface;',
  18605. ' IUnknown = interface',
  18606. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18607. ' end;',
  18608. ' IInterface = IUnknown;',
  18609. ' IBird = interface(IInterface)',
  18610. ' function GetSize: longint;',
  18611. ' procedure SetSize(i: longint);',
  18612. ' property Size: longint read GetSize write SetSize;',
  18613. ' procedure DoIt(i: longint);',
  18614. ' end;',
  18615. ' TObject = class',
  18616. ' end;',
  18617. ' TBird = class(TObject,IBird)',
  18618. ' function GetSize: longint; virtual; abstract;',
  18619. ' procedure SetSize(i: longint); virtual; abstract;',
  18620. ' procedure DoIt(i: longint); virtual; abstract;',
  18621. ' end;',
  18622. 'var',
  18623. ' BirdIntf: IBird;',
  18624. 'begin',
  18625. ' BirdIntf.Size:=BirdIntf.Size;',
  18626. '']);
  18627. ConvertProgram;
  18628. CheckSource('TestClassInterface_Corba',
  18629. LinesToStr([ // statements
  18630. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18631. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18632. 'rtl.createClass(this, "TObject", null, function () {',
  18633. ' this.$init = function () {',
  18634. ' };',
  18635. ' this.$final = function () {',
  18636. ' };',
  18637. '});',
  18638. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18639. ' rtl.addIntf(this, $mod.IBird);',
  18640. '});',
  18641. 'this.BirdIntf = null;',
  18642. '']),
  18643. LinesToStr([ // $mod.$main
  18644. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18645. '']));
  18646. end;
  18647. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18648. begin
  18649. StartProgram(false);
  18650. Add([
  18651. '{$interfaces corba}',
  18652. 'type',
  18653. ' IUnknown = interface',
  18654. ' procedure DoIt; external name ''foo'';',
  18655. ' end;',
  18656. 'begin']);
  18657. SetExpectedParserError(
  18658. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18659. nParserNoFieldsAllowed);
  18660. ConvertProgram;
  18661. end;
  18662. procedure TTestModule.TestClassInterface_Overloads;
  18663. begin
  18664. StartProgram(false);
  18665. Add([
  18666. '{$interfaces corba}',
  18667. 'type',
  18668. ' integer = longint;',
  18669. ' IUnknown = interface',
  18670. ' procedure DoIt(i: integer);',
  18671. ' procedure DoIt(s: string);',
  18672. ' end;',
  18673. ' IBird = interface(IUnknown)',
  18674. ' procedure DoIt(b: boolean); overload;',
  18675. ' end;',
  18676. ' TObject = class',
  18677. ' end;',
  18678. ' TBird = class(TObject,IBird)',
  18679. ' procedure DoIt(o: TObject);',
  18680. ' procedure DoIt(s: string);',
  18681. ' procedure DoIt(i: integer);',
  18682. ' procedure DoIt(b: boolean);',
  18683. ' end;',
  18684. 'procedure TBird.DoIt(o: TObject); begin end;',
  18685. 'procedure TBird.DoIt(s: string); begin end;',
  18686. 'procedure TBird.DoIt(i: integer); begin end;',
  18687. 'procedure TBird.DoIt(b: boolean); begin end;',
  18688. 'var',
  18689. ' BirdIntf: IBird;',
  18690. 'begin',
  18691. ' BirdIntf.DoIt(3);',
  18692. ' BirdIntf.DoIt(''abc'');',
  18693. ' BirdIntf.DoIt(true);',
  18694. '']);
  18695. ConvertProgram;
  18696. CheckSource('TestClassInterface_Overloads',
  18697. LinesToStr([ // statements
  18698. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18699. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18700. 'rtl.createClass(this, "TObject", null, function () {',
  18701. ' this.$init = function () {',
  18702. ' };',
  18703. ' this.$final = function () {',
  18704. ' };',
  18705. '});',
  18706. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18707. ' this.DoIt = function (o) {',
  18708. ' };',
  18709. ' this.DoIt$1 = function (s) {',
  18710. ' };',
  18711. ' this.DoIt$2 = function (i) {',
  18712. ' };',
  18713. ' this.DoIt$3 = function (b) {',
  18714. ' };',
  18715. ' rtl.addIntf(this, $mod.IBird, {',
  18716. ' DoIt$2: "DoIt$3",',
  18717. ' DoIt: "DoIt$2"',
  18718. ' });',
  18719. '});',
  18720. 'this.BirdIntf = null;',
  18721. '']),
  18722. LinesToStr([ // $mod.$main
  18723. '$mod.BirdIntf.DoIt(3);',
  18724. '$mod.BirdIntf.DoIt$1("abc");',
  18725. '$mod.BirdIntf.DoIt$2(true);',
  18726. '']));
  18727. end;
  18728. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18729. begin
  18730. StartProgram(false);
  18731. Add([
  18732. '{$interfaces corba}',
  18733. 'type',
  18734. ' IBird = interface',
  18735. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18736. ' end;',
  18737. ' IDog = interface',
  18738. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18739. ' end;',
  18740. ' TObject = class(IBird,IDog)',
  18741. ' end;',
  18742. 'begin']);
  18743. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18744. nDuplicateGUIDXInYZ);
  18745. ConvertProgram;
  18746. end;
  18747. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18748. begin
  18749. StartProgram(false);
  18750. Add([
  18751. '{$interfaces corba}',
  18752. 'type',
  18753. ' IAnimal = interface',
  18754. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18755. ' end;',
  18756. ' IBird = interface(IAnimal)',
  18757. ' end;',
  18758. ' IHawk = interface(IBird)',
  18759. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18760. ' end;',
  18761. 'begin']);
  18762. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18763. nDuplicateGUIDXInYZ);
  18764. ConvertProgram;
  18765. end;
  18766. procedure TTestModule.TestClassInterface_AncestorImpl;
  18767. begin
  18768. StartProgram(false);
  18769. Add([
  18770. '{$interfaces corba}',
  18771. 'type',
  18772. ' integer = longint;',
  18773. ' IUnknown = interface',
  18774. ' procedure DoIt(i: integer);',
  18775. ' end;',
  18776. ' IBird = interface',
  18777. ' procedure Fly(i: integer);',
  18778. ' end;',
  18779. ' TObject = class(IUnknown)',
  18780. ' procedure DoIt(i: integer);',
  18781. ' end;',
  18782. ' TBird = class(IBird)',
  18783. ' procedure Fly(i: integer);',
  18784. ' end;',
  18785. 'procedure TObject.DoIt(i: integer); begin end;',
  18786. 'procedure TBird.Fly(i: integer); begin end;',
  18787. 'begin',
  18788. '']);
  18789. ConvertProgram;
  18790. CheckSource('TestClassInterface_AncestorIntf',
  18791. LinesToStr([ // statements
  18792. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18793. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18794. 'rtl.createClass(this, "TObject", null, function () {',
  18795. ' this.$init = function () {',
  18796. ' };',
  18797. ' this.$final = function () {',
  18798. ' };',
  18799. ' this.DoIt = function (i) {',
  18800. ' };',
  18801. ' rtl.addIntf(this, $mod.IUnknown);',
  18802. '});',
  18803. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18804. ' this.Fly = function (i) {',
  18805. ' };',
  18806. ' rtl.addIntf(this, $mod.IBird);',
  18807. ' rtl.addIntf(this, $mod.IUnknown);',
  18808. '});',
  18809. '']),
  18810. LinesToStr([ // $mod.$main
  18811. '']));
  18812. end;
  18813. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18814. begin
  18815. StartProgram(false);
  18816. Add([
  18817. '{$interfaces corba}',
  18818. 'type',
  18819. ' integer = longint;',
  18820. ' IBird = interface',
  18821. ' procedure DoIt(i: integer);',
  18822. ' end;',
  18823. ' TObject = class',
  18824. ' procedure DoIt(i: integer);',
  18825. ' end;',
  18826. ' TBird = class(IBird)',
  18827. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18828. ' end;',
  18829. 'procedure TObject.DoIt(i: integer); begin end;',
  18830. 'procedure TBird.DoIt(i: integer); begin end;',
  18831. 'begin',
  18832. '']);
  18833. ConvertProgram;
  18834. CheckSource('TestClassInterface_ImplReintroduce',
  18835. LinesToStr([ // statements
  18836. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18837. 'rtl.createClass(this, "TObject", null, function () {',
  18838. ' this.$init = function () {',
  18839. ' };',
  18840. ' this.$final = function () {',
  18841. ' };',
  18842. ' this.DoIt = function (i) {',
  18843. ' };',
  18844. '});',
  18845. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18846. ' this.DoIt$1 = function (i) {',
  18847. ' };',
  18848. ' rtl.addIntf(this, $mod.IBird, {',
  18849. ' DoIt: "DoIt$1"',
  18850. ' });',
  18851. '});',
  18852. '']),
  18853. LinesToStr([ // $mod.$main
  18854. '']));
  18855. end;
  18856. procedure TTestModule.TestClassInterface_MethodResolution;
  18857. begin
  18858. StartProgram(false);
  18859. Add([
  18860. '{$interfaces corba}',
  18861. 'type',
  18862. ' IUnknown = interface',
  18863. ' procedure Walk(i: longint);',
  18864. ' end;',
  18865. ' IBird = interface(IUnknown)',
  18866. ' procedure Walk(b: boolean); overload;',
  18867. ' procedure Fly(s: string);',
  18868. ' end;',
  18869. ' TObject = class',
  18870. ' end;',
  18871. ' TBird = class(TObject,IBird)',
  18872. ' procedure IBird.Fly = Move;',
  18873. ' procedure IBird.Walk = Hop;',
  18874. ' procedure Hop(i: longint);',
  18875. ' procedure Move(s: string);',
  18876. ' procedure Hop(b: boolean);',
  18877. ' end;',
  18878. 'procedure TBird.Move(s: string); begin end;',
  18879. 'procedure TBird.Hop(i: longint); begin end;',
  18880. 'procedure TBird.Hop(b: boolean); begin end;',
  18881. 'var',
  18882. ' BirdIntf: IBird;',
  18883. 'begin',
  18884. ' BirdIntf.Walk(3);',
  18885. ' BirdIntf.Walk(true);',
  18886. ' BirdIntf.Fly(''abc'');',
  18887. '']);
  18888. ConvertProgram;
  18889. CheckSource('TestClassInterface_MethodResolution',
  18890. LinesToStr([ // statements
  18891. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18892. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18893. 'rtl.createClass(this, "TObject", null, function () {',
  18894. ' this.$init = function () {',
  18895. ' };',
  18896. ' this.$final = function () {',
  18897. ' };',
  18898. '});',
  18899. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18900. ' this.Hop = function (i) {',
  18901. ' };',
  18902. ' this.Move = function (s) {',
  18903. ' };',
  18904. ' this.Hop$1 = function (b) {',
  18905. ' };',
  18906. ' rtl.addIntf(this, $mod.IBird, {',
  18907. ' Walk$1: "Hop$1",',
  18908. ' Fly: "Move",',
  18909. ' Walk: "Hop"',
  18910. ' });',
  18911. '});',
  18912. 'this.BirdIntf = null;',
  18913. '']),
  18914. LinesToStr([ // $mod.$main
  18915. '$mod.BirdIntf.Walk(3);',
  18916. '$mod.BirdIntf.Walk$1(true);',
  18917. '$mod.BirdIntf.Fly("abc");',
  18918. '']));
  18919. end;
  18920. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18921. begin
  18922. StartProgram(false);
  18923. Add([
  18924. '{$interfaces com}',
  18925. 'type',
  18926. ' IUnknown = interface',
  18927. ' function _AddRef: longint;',
  18928. ' procedure Walk;',
  18929. ' end;',
  18930. ' IBird = interface end;',
  18931. ' IDog = interface end;',
  18932. ' TObject = class(IBird,IDog)',
  18933. ' function _AddRef: longint; virtual; abstract;',
  18934. ' procedure Walk; virtual; abstract;',
  18935. ' end;',
  18936. ' TBird = class(IUnknown)',
  18937. ' end;',
  18938. 'begin',
  18939. '']);
  18940. ConvertProgram;
  18941. CheckSource('TestClassInterface_COM_AncestorLess',
  18942. LinesToStr([ // statements
  18943. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18944. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18945. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18946. 'rtl.createClass(this, "TObject", null, function () {',
  18947. ' this.$init = function () {',
  18948. ' };',
  18949. ' this.$final = function () {',
  18950. ' };',
  18951. ' rtl.addIntf(this, $mod.IBird);',
  18952. ' rtl.addIntf(this, $mod.IDog);',
  18953. '});',
  18954. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18955. ' rtl.addIntf(this, $mod.IUnknown);',
  18956. ' rtl.addIntf(this, $mod.IBird);',
  18957. ' rtl.addIntf(this, $mod.IDog);',
  18958. '});',
  18959. '']),
  18960. LinesToStr([ // $mod.$main
  18961. '']));
  18962. end;
  18963. procedure TTestModule.TestClassInterface_MethodOverride;
  18964. begin
  18965. StartProgram(false);
  18966. Add([
  18967. '{$interfaces corba}',
  18968. 'type',
  18969. ' IUnknown = interface',
  18970. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18971. ' procedure Go;',
  18972. ' end;',
  18973. ' TObject = class(IUnknown)',
  18974. ' procedure Go; virtual; abstract;',
  18975. ' end;',
  18976. ' TBird = class',
  18977. ' procedure Go; override;',
  18978. ' end;',
  18979. ' TCat = class(TObject)',
  18980. ' procedure Go; override;',
  18981. ' end;',
  18982. ' TDog = class(TObject, IUnknown)',
  18983. ' procedure Go; override;',
  18984. ' end;',
  18985. 'procedure TBird.Go; begin end;',
  18986. 'procedure TCat.Go; begin end;',
  18987. 'procedure TDog.Go; begin end;',
  18988. 'begin',
  18989. '']);
  18990. ConvertProgram;
  18991. CheckSource('TestClassInterface_MethodOverride',
  18992. LinesToStr([ // statements
  18993. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18994. 'rtl.createClass(this, "TObject", null, function () {',
  18995. ' this.$init = function () {',
  18996. ' };',
  18997. ' this.$final = function () {',
  18998. ' };',
  18999. ' rtl.addIntf(this, $mod.IUnknown);',
  19000. '});',
  19001. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19002. ' this.Go = function () {',
  19003. ' };',
  19004. ' rtl.addIntf(this, $mod.IUnknown);',
  19005. '});',
  19006. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  19007. ' this.Go = function () {',
  19008. ' };',
  19009. ' rtl.addIntf(this, $mod.IUnknown);',
  19010. '});',
  19011. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  19012. ' this.Go = function () {',
  19013. ' };',
  19014. ' rtl.addIntf(this, $mod.IUnknown);',
  19015. '});',
  19016. '']),
  19017. LinesToStr([ // $mod.$main
  19018. '']));
  19019. end;
  19020. procedure TTestModule.TestClassInterface_Corba_Delegation;
  19021. begin
  19022. StartProgram(false);
  19023. Add([
  19024. '{$interfaces corba}',
  19025. 'type',
  19026. ' IUnknown = interface',
  19027. ' end;',
  19028. ' IBird = interface(IUnknown)',
  19029. ' procedure Fly(s: string);',
  19030. ' end;',
  19031. ' IEagle = interface(IBird)',
  19032. ' end;',
  19033. ' IDove = interface(IBird)',
  19034. ' end;',
  19035. ' ISwallow = interface(IBird)',
  19036. ' end;',
  19037. ' TObject = class',
  19038. ' end;',
  19039. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19040. ' procedure Fly(s: string); virtual; abstract;',
  19041. ' end;',
  19042. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19043. ' FBirdIntf: IBird;',
  19044. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19045. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19046. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19047. ' FDoveObj: TBird;',
  19048. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19049. ' function GetSwallowObj: TBird; virtual; abstract;',
  19050. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19051. ' end;',
  19052. 'begin',
  19053. '']);
  19054. ConvertProgram;
  19055. CheckSource('TestClassInterface_Corba_Delegation',
  19056. LinesToStr([ // statements
  19057. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19058. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19059. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19060. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19061. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19062. 'rtl.createClass(this, "TObject", null, function () {',
  19063. ' this.$init = function () {',
  19064. ' };',
  19065. ' this.$final = function () {',
  19066. ' };',
  19067. '});',
  19068. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19069. ' rtl.addIntf(this, $mod.IBird);',
  19070. ' rtl.addIntf(this, $mod.IEagle);',
  19071. ' rtl.addIntf(this, $mod.IDove);',
  19072. ' rtl.addIntf(this, $mod.ISwallow);',
  19073. '});',
  19074. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19075. ' this.$init = function () {',
  19076. ' $mod.TObject.$init.call(this);',
  19077. ' this.FBirdIntf = null;',
  19078. ' this.FDoveObj = null;',
  19079. ' };',
  19080. ' this.$final = function () {',
  19081. ' this.FBirdIntf = undefined;',
  19082. ' this.FDoveObj = undefined;',
  19083. ' $mod.TObject.$final.call(this);',
  19084. ' };',
  19085. ' this.$intfmaps = {',
  19086. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19087. ' return this.FBirdIntf;',
  19088. ' },',
  19089. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19090. ' return this.GetEagleIntf();',
  19091. ' },',
  19092. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19093. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19094. ' },',
  19095. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19096. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19097. ' }',
  19098. ' };',
  19099. '});',
  19100. '']),
  19101. LinesToStr([ // $mod.$main
  19102. '']));
  19103. end;
  19104. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19105. begin
  19106. StartProgram(false);
  19107. Add([
  19108. '{$interfaces corba}',
  19109. 'type',
  19110. ' IUnknown = interface',
  19111. ' end;',
  19112. ' IBird = interface(IUnknown)',
  19113. ' procedure Fly(s: string);',
  19114. ' end;',
  19115. ' IEagle = interface(IBird)',
  19116. ' end;',
  19117. ' IDove = interface(IBird)',
  19118. ' end;',
  19119. ' ISwallow = interface(IBird)',
  19120. ' end;',
  19121. ' TObject = class',
  19122. ' end;',
  19123. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19124. ' procedure Fly(s: string); virtual; abstract;',
  19125. ' end;',
  19126. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19127. ' private',
  19128. ' class var FBirdIntf: IBird;',
  19129. ' class var FDoveObj: TBird;',
  19130. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19131. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19132. ' protected',
  19133. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19134. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19135. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19136. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19137. ' end;',
  19138. 'begin',
  19139. '']);
  19140. ConvertProgram;
  19141. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19142. LinesToStr([ // statements
  19143. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19144. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19145. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19146. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19147. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19148. 'rtl.createClass(this, "TObject", null, function () {',
  19149. ' this.$init = function () {',
  19150. ' };',
  19151. ' this.$final = function () {',
  19152. ' };',
  19153. '});',
  19154. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19155. ' rtl.addIntf(this, $mod.IBird);',
  19156. ' rtl.addIntf(this, $mod.IEagle);',
  19157. ' rtl.addIntf(this, $mod.IDove);',
  19158. ' rtl.addIntf(this, $mod.ISwallow);',
  19159. '});',
  19160. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19161. ' this.FBirdIntf = null;',
  19162. ' this.FDoveObj = null;',
  19163. ' this.$intfmaps = {',
  19164. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19165. ' return this.FBirdIntf;',
  19166. ' },',
  19167. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19168. ' return this.GetEagleIntf();',
  19169. ' },',
  19170. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19171. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19172. ' },',
  19173. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19174. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19175. ' }',
  19176. ' };',
  19177. '});',
  19178. '']),
  19179. LinesToStr([ // $mod.$main
  19180. '']));
  19181. end;
  19182. procedure TTestModule.TestClassInterface_Corba_Operators;
  19183. begin
  19184. StartProgram(false);
  19185. Add([
  19186. '{$interfaces corba}',
  19187. 'type',
  19188. ' IUnknown = interface',
  19189. ' end;',
  19190. ' IBird = interface(IUnknown)',
  19191. ' function GetItems(Index: longint): longint;',
  19192. ' procedure SetItems(Index: longint; Value: longint);',
  19193. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19194. ' end;',
  19195. ' TObject = class',
  19196. ' end;',
  19197. ' TBird = class(TObject,IBird)',
  19198. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19199. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19200. ' end;',
  19201. 'var',
  19202. ' IntfVar: IBird = nil;',
  19203. ' IntfVar2: IBird;',
  19204. ' ObjVar: TBird;',
  19205. ' v: JSValue;',
  19206. 'begin',
  19207. ' IntfVar:=nil;',
  19208. ' IntfVar[3]:=IntfVar[4];',
  19209. ' if Assigned(IntfVar) then ;',
  19210. ' IntfVar:=IntfVar2;',
  19211. ' IntfVar:=ObjVar;',
  19212. ' if IntfVar=IntfVar2 then ;',
  19213. ' if IntfVar<>IntfVar2 then ;',
  19214. ' if IntfVar is IBird then ;',
  19215. ' if IntfVar is TBird then ;',
  19216. ' if ObjVar is IBird then ;',
  19217. ' IntfVar:=IntfVar2 as IBird;',
  19218. ' ObjVar:=IntfVar2 as TBird;',
  19219. ' IntfVar:=ObjVar as IBird;',
  19220. ' IntfVar:=IBird(IntfVar2);',
  19221. ' ObjVar:=TBird(IntfVar);',
  19222. ' IntfVar:=IBird(ObjVar);',
  19223. ' v:=IntfVar;',
  19224. ' IntfVar:=IBird(v);',
  19225. ' if v is IBird then ;',
  19226. ' v:=JSValue(IntfVar);',
  19227. ' v:=IBird;',
  19228. '']);
  19229. ConvertProgram;
  19230. CheckSource('TestClassInterface_Corba_Operators',
  19231. LinesToStr([ // statements
  19232. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19233. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19234. 'rtl.createClass(this, "TObject", null, function () {',
  19235. ' this.$init = function () {',
  19236. ' };',
  19237. ' this.$final = function () {',
  19238. ' };',
  19239. '});',
  19240. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19241. ' rtl.addIntf(this, $mod.IBird);',
  19242. '});',
  19243. 'this.IntfVar = null;',
  19244. 'this.IntfVar2 = null;',
  19245. 'this.ObjVar = null;',
  19246. 'this.v = undefined;',
  19247. '']),
  19248. LinesToStr([ // $mod.$main
  19249. '$mod.IntfVar = null;',
  19250. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19251. 'if ($mod.IntfVar != null) ;',
  19252. '$mod.IntfVar = $mod.IntfVar2;',
  19253. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19254. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19255. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19256. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19257. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19258. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19259. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19260. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19261. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19262. '$mod.IntfVar = $mod.IntfVar2;',
  19263. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19264. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19265. '$mod.v = $mod.IntfVar;',
  19266. '$mod.IntfVar = rtl.getObject($mod.v);',
  19267. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19268. '$mod.v = $mod.IntfVar;',
  19269. '$mod.v = $mod.IBird;',
  19270. '']));
  19271. end;
  19272. procedure TTestModule.TestClassInterface_Corba_Args;
  19273. begin
  19274. StartProgram(false);
  19275. Add([
  19276. '{$interfaces corba}',
  19277. 'type',
  19278. ' IUnknown = interface',
  19279. ' end;',
  19280. ' IBird = interface(IUnknown)',
  19281. ' end;',
  19282. ' TObject = class',
  19283. ' end;',
  19284. ' TBird = class(TObject,IBird)',
  19285. ' end;',
  19286. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19287. 'begin',
  19288. ' DoIt(i,i,i);',
  19289. 'end;',
  19290. 'procedure Change(var i: IBird; out j: IBird);',
  19291. 'begin',
  19292. ' DoIt(i,i,i);',
  19293. ' Change(i,i);',
  19294. 'end;',
  19295. 'var',
  19296. ' i: IBird;',
  19297. ' o: TBird;',
  19298. 'begin',
  19299. ' DoIt(i,i,i);',
  19300. ' Change(i,i);',
  19301. ' DoIt(o,o,o);',
  19302. '']);
  19303. ConvertProgram;
  19304. CheckSource('TestClassInterface_Corba_Args',
  19305. LinesToStr([ // statements
  19306. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19307. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19308. 'rtl.createClass(this, "TObject", null, function () {',
  19309. ' this.$init = function () {',
  19310. ' };',
  19311. ' this.$final = function () {',
  19312. ' };',
  19313. '});',
  19314. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19315. ' rtl.addIntf(this, $mod.IBird);',
  19316. '});',
  19317. 'this.DoIt = function (u, i, j) {',
  19318. ' $mod.DoIt({',
  19319. ' get: function () {',
  19320. ' return i;',
  19321. ' },',
  19322. ' set: function (v) {',
  19323. ' i = v;',
  19324. ' }',
  19325. ' }, i, i);',
  19326. '};',
  19327. 'this.Change = function (i, j) {',
  19328. ' $mod.DoIt(i, i.get(), i.get());',
  19329. ' $mod.Change(i, i);',
  19330. '};',
  19331. 'this.i = null;',
  19332. 'this.o = null;',
  19333. '']),
  19334. LinesToStr([ // $mod.$main
  19335. '$mod.DoIt({',
  19336. ' p: $mod,',
  19337. ' get: function () {',
  19338. ' return this.p.i;',
  19339. ' },',
  19340. ' set: function (v) {',
  19341. ' this.p.i = v;',
  19342. ' }',
  19343. '}, $mod.i, $mod.i);',
  19344. '$mod.Change({',
  19345. ' p: $mod,',
  19346. ' get: function () {',
  19347. ' return this.p.i;',
  19348. ' },',
  19349. ' set: function (v) {',
  19350. ' this.p.i = v;',
  19351. ' }',
  19352. '}, {',
  19353. ' p: $mod,',
  19354. ' get: function () {',
  19355. ' return this.p.i;',
  19356. ' },',
  19357. ' set: function (v) {',
  19358. ' this.p.i = v;',
  19359. ' }',
  19360. '});',
  19361. '$mod.DoIt({',
  19362. ' p: $mod,',
  19363. ' get: function () {',
  19364. ' return this.p.o;',
  19365. ' },',
  19366. ' set: function (v) {',
  19367. ' this.p.o = v;',
  19368. ' }',
  19369. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19370. '']));
  19371. end;
  19372. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19373. begin
  19374. StartProgram(false);
  19375. Add([
  19376. '{$interfaces corba}',
  19377. 'type',
  19378. ' IUnknown = interface end;',
  19379. ' TObject = class',
  19380. ' Id: longint;',
  19381. ' end;',
  19382. ' IEnumerator = interface(IUnknown)',
  19383. ' function GetCurrent: TObject;',
  19384. ' function MoveNext: Boolean;',
  19385. ' property Current: TObject read GetCurrent;',
  19386. ' end;',
  19387. ' IEnumerable = interface(IUnknown)',
  19388. ' function GetEnumerator: IEnumerator;',
  19389. ' end;',
  19390. 'var',
  19391. ' o: TObject;',
  19392. ' i: IEnumerable;',
  19393. 'begin',
  19394. ' for o in i do o.Id:=3;',
  19395. '']);
  19396. ConvertProgram;
  19397. CheckSource('TestClassInterface_Corba_ForIn',
  19398. LinesToStr([ // statements
  19399. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19400. 'rtl.createClass(this, "TObject", null, function () {',
  19401. ' this.$init = function () {',
  19402. ' this.Id = 0;',
  19403. ' };',
  19404. ' this.$final = function () {',
  19405. ' };',
  19406. '});',
  19407. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19408. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19409. 'this.o = null;',
  19410. 'this.i = null;',
  19411. '']),
  19412. LinesToStr([ // $mod.$main
  19413. 'var $in = $mod.i.GetEnumerator();',
  19414. 'while ($in.MoveNext()) {',
  19415. ' $mod.o = $in.GetCurrent();',
  19416. ' $mod.o.Id = 3;',
  19417. '};',
  19418. '']));
  19419. end;
  19420. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19421. begin
  19422. StartProgram(false);
  19423. Add([
  19424. '{$interfaces com}',
  19425. 'type',
  19426. ' IUnknown = interface',
  19427. ' function _AddRef: longint;',
  19428. ' function _Release: longint;',
  19429. ' end;',
  19430. ' TObject = class(IUnknown)',
  19431. ' function _AddRef: longint; virtual; abstract;',
  19432. ' function _Release: longint; virtual; abstract;',
  19433. ' end;',
  19434. 'var',
  19435. ' i: IUnknown;',
  19436. 'procedure DoGlobal(o: TObject);',
  19437. 'begin',
  19438. ' i:=nil;',
  19439. ' i:=o;',
  19440. ' i:=i;',
  19441. 'end;',
  19442. 'procedure DoLocal(o: TObject);',
  19443. 'const k: IUnknown = nil;',
  19444. 'var j: IUnknown;',
  19445. 'begin',
  19446. ' k:=o;',
  19447. ' k:=i;',
  19448. ' j:=o;',
  19449. ' j:=i;',
  19450. 'end;',
  19451. 'var o: TObject;',
  19452. 'begin',
  19453. ' i:=nil;',
  19454. ' i:=o;',
  19455. '']);
  19456. ConvertProgram;
  19457. CheckSource('TestClassInterface_COM_AssignVar',
  19458. LinesToStr([ // statements
  19459. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19460. 'rtl.createClass(this, "TObject", null, function () {',
  19461. ' this.$init = function () {',
  19462. ' };',
  19463. ' this.$final = function () {',
  19464. ' };',
  19465. ' rtl.addIntf(this, $mod.IUnknown);',
  19466. '});',
  19467. 'this.i = null;',
  19468. 'this.DoGlobal = function (o) {',
  19469. ' rtl.setIntfP($mod, "i", null);',
  19470. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19471. ' rtl.setIntfP($mod, "i", $mod.i);',
  19472. '};',
  19473. 'var k = null;',
  19474. 'this.DoLocal = function (o) {',
  19475. ' var j = null;',
  19476. ' try{',
  19477. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19478. ' k = rtl.setIntfL(k, $mod.i);',
  19479. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19480. ' j = rtl.setIntfL(j, $mod.i);',
  19481. ' }finally{',
  19482. ' rtl._Release(j);',
  19483. ' };',
  19484. '};',
  19485. 'this.o = null;',
  19486. '']),
  19487. LinesToStr([ // $mod.$main
  19488. 'rtl.setIntfP($mod, "i", null);',
  19489. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19490. '']));
  19491. end;
  19492. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19493. begin
  19494. StartProgram(false);
  19495. Add([
  19496. '{$interfaces com}',
  19497. 'type',
  19498. ' IUnknown = interface',
  19499. ' function _AddRef: longint;',
  19500. ' function _Release: longint;',
  19501. ' end;',
  19502. ' TObject = class(IUnknown)',
  19503. ' function _AddRef: longint; virtual; abstract;',
  19504. ' function _Release: longint; virtual; abstract;',
  19505. ' end;',
  19506. 'procedure DoDefault(i, j: IUnknown);',
  19507. 'begin',
  19508. ' i:=nil;',
  19509. ' i:=j;',
  19510. 'end;',
  19511. 'begin',
  19512. '']);
  19513. ConvertProgram;
  19514. CheckSource('TestClassInterface_COM_AssignArg',
  19515. LinesToStr([ // statements
  19516. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19517. 'rtl.createClass(this, "TObject", null, function () {',
  19518. ' this.$init = function () {',
  19519. ' };',
  19520. ' this.$final = function () {',
  19521. ' };',
  19522. ' rtl.addIntf(this, $mod.IUnknown);',
  19523. '});',
  19524. 'this.DoDefault = function (i, j) {',
  19525. ' rtl._AddRef(i);',
  19526. ' try {',
  19527. ' i = rtl.setIntfL(i, null);',
  19528. ' i = rtl.setIntfL(i, j);',
  19529. ' } finally {',
  19530. ' rtl._Release(i);',
  19531. ' };',
  19532. '};',
  19533. '']),
  19534. LinesToStr([ // $mod.$main
  19535. '']));
  19536. end;
  19537. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19538. begin
  19539. StartProgram(false);
  19540. Add([
  19541. '{$interfaces com}',
  19542. 'type',
  19543. ' IUnknown = interface',
  19544. ' function _AddRef: longint;',
  19545. ' function _Release: longint;',
  19546. ' end;',
  19547. ' TObject = class(IUnknown)',
  19548. ' function _AddRef: longint; virtual; abstract;',
  19549. ' function _Release: longint; virtual; abstract;',
  19550. ' end;',
  19551. 'function DoDefault(i: IUnknown): IUnknown;',
  19552. 'begin',
  19553. ' Result:=i;',
  19554. ' if Result<>nil then exit;',
  19555. 'end;',
  19556. 'begin',
  19557. '']);
  19558. ConvertProgram;
  19559. CheckSource('TestClassInterface_COM_FunctionResult',
  19560. LinesToStr([ // statements
  19561. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19562. 'rtl.createClass(this, "TObject", null, function () {',
  19563. ' this.$init = function () {',
  19564. ' };',
  19565. ' this.$final = function () {',
  19566. ' };',
  19567. ' rtl.addIntf(this, $mod.IUnknown);',
  19568. '});',
  19569. 'this.DoDefault = function (i) {',
  19570. ' var Result = null;',
  19571. ' var $ok = false;',
  19572. ' try {',
  19573. ' Result = rtl.setIntfL(Result, i);',
  19574. ' if(Result !== null){',
  19575. ' $ok = true;',
  19576. ' return Result;',
  19577. ' };',
  19578. ' $ok = true;',
  19579. ' } finally {',
  19580. ' if(!$ok) rtl._Release(Result);',
  19581. ' };',
  19582. ' return Result;',
  19583. '};',
  19584. '']),
  19585. LinesToStr([ // $mod.$main
  19586. '']));
  19587. end;
  19588. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19589. begin
  19590. StartProgram(false);
  19591. Add([
  19592. '{$interfaces com}',
  19593. 'type',
  19594. ' IUnknown = interface',
  19595. ' function _AddRef: longint;',
  19596. ' function _Release: longint;',
  19597. ' end;',
  19598. ' TObject = class(IUnknown)',
  19599. ' function _AddRef: longint; virtual; abstract;',
  19600. ' function _Release: longint; virtual; abstract;',
  19601. ' function GetIntf: IUnknown; virtual;',
  19602. ' end;',
  19603. ' TMouse = class',
  19604. ' function GetIntf: IUnknown; override;',
  19605. ' end;',
  19606. 'function TObject.GetIntf: IUnknown; begin end;',
  19607. 'function TMouse.GetIntf: IUnknown;',
  19608. 'var i: IUnknown;',
  19609. 'begin',
  19610. ' inherited;',
  19611. ' inherited GetIntf;',
  19612. ' inherited GetIntf();',
  19613. ' Result:=inherited GetIntf;',
  19614. ' Result:=inherited GetIntf();',
  19615. ' i:=inherited GetIntf;',
  19616. ' i:=inherited GetIntf();',
  19617. 'end;',
  19618. 'begin',
  19619. '']);
  19620. ConvertProgram;
  19621. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19622. LinesToStr([ // statements
  19623. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19624. 'rtl.createClass(this, "TObject", null, function () {',
  19625. ' this.$init = function () {',
  19626. ' };',
  19627. ' this.$final = function () {',
  19628. ' };',
  19629. ' this.GetIntf = function () {',
  19630. ' var Result = null;',
  19631. ' return Result;',
  19632. ' };',
  19633. ' rtl.addIntf(this, $mod.IUnknown);',
  19634. '});',
  19635. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19636. ' this.GetIntf = function () {',
  19637. ' var Result = null;',
  19638. ' var i = null;',
  19639. ' var $ir = rtl.createIntfRefs();',
  19640. ' var $ok = false;',
  19641. ' try {',
  19642. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19643. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19644. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19645. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19646. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19647. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19648. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19649. ' $ok = true;',
  19650. ' } finally {',
  19651. ' $ir.free();',
  19652. ' rtl._Release(i);',
  19653. ' if (!$ok) rtl._Release(Result);',
  19654. ' };',
  19655. ' return Result;',
  19656. ' };',
  19657. ' rtl.addIntf(this, $mod.IUnknown);',
  19658. '});',
  19659. '']),
  19660. LinesToStr([ // $mod.$main
  19661. '']));
  19662. end;
  19663. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19664. begin
  19665. StartProgram(false);
  19666. Add([
  19667. '{$interfaces com}',
  19668. 'type',
  19669. ' IUnknown = interface',
  19670. ' function _AddRef: longint;',
  19671. ' function _Release: longint;',
  19672. ' end;',
  19673. ' TObject = class(IUnknown)',
  19674. ' function _AddRef: longint; virtual; abstract;',
  19675. ' function _Release: longint; virtual; abstract;',
  19676. ' end;',
  19677. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19678. 'begin',
  19679. ' if i is IUnknown then ;',
  19680. ' if o is IUnknown then ;',
  19681. ' if i is TObject then ;',
  19682. ' i:=j as IUnknown;',
  19683. ' i:=o as IUnknown;',
  19684. ' o:=j as TObject;',
  19685. ' i:=IUnknown(j);',
  19686. ' i:=IUnknown(o);',
  19687. ' o:=TObject(i);',
  19688. 'end;',
  19689. 'begin',
  19690. '']);
  19691. ConvertProgram;
  19692. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19693. LinesToStr([ // statements
  19694. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19695. 'rtl.createClass(this, "TObject", null, function () {',
  19696. ' this.$init = function () {',
  19697. ' };',
  19698. ' this.$final = function () {',
  19699. ' };',
  19700. ' rtl.addIntf(this, $mod.IUnknown);',
  19701. '});',
  19702. 'this.DoDefault = function (i, j, o) {',
  19703. ' rtl._AddRef(i);',
  19704. ' try {',
  19705. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19706. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19707. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19708. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19709. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19710. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19711. ' i = rtl.setIntfL(i, j);',
  19712. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19713. ' o = rtl.intfToClass(i, $mod.TObject);',
  19714. ' } finally {',
  19715. ' rtl._Release(i);',
  19716. ' };',
  19717. '};',
  19718. '']),
  19719. LinesToStr([ // $mod.$main
  19720. '']));
  19721. end;
  19722. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19723. begin
  19724. StartProgram(false);
  19725. Add([
  19726. '{$interfaces com}',
  19727. 'type',
  19728. ' IUnknown = interface',
  19729. ' function _AddRef: longint;',
  19730. ' function _Release: longint;',
  19731. ' end;',
  19732. ' TObject = class(IUnknown)',
  19733. ' function _AddRef: longint; virtual; abstract;',
  19734. ' function _Release: longint; virtual; abstract;',
  19735. ' end;',
  19736. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19737. 'var o: TObject;',
  19738. 'begin',
  19739. ' DoIt(v,v,v,v);',
  19740. ' DoIt(o,o,k,k);',
  19741. 'end;',
  19742. 'procedure DoSome;',
  19743. 'var v: IUnknown;',
  19744. 'begin',
  19745. ' DoIt(v,v,v,v);',
  19746. 'end;',
  19747. 'var i: IUnknown;',
  19748. 'begin',
  19749. ' DoIt(i,i,i,i);',
  19750. '']);
  19751. ConvertProgram;
  19752. CheckSource('TestClassInterface_COM_PassAsArg',
  19753. LinesToStr([ // statements
  19754. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19755. 'rtl.createClass(this, "TObject", null, function () {',
  19756. ' this.$init = function () {',
  19757. ' };',
  19758. ' this.$final = function () {',
  19759. ' };',
  19760. ' rtl.addIntf(this, $mod.IUnknown);',
  19761. '});',
  19762. 'this.DoIt = function (v, j, k, l) {',
  19763. ' var o = null;',
  19764. ' var $ir = rtl.createIntfRefs();',
  19765. ' rtl._AddRef(v);',
  19766. ' try {',
  19767. ' $mod.DoIt(v, v, {',
  19768. ' get: function () {',
  19769. ' return v;',
  19770. ' },',
  19771. ' set: function (w) {',
  19772. ' v = rtl.setIntfL(v, w);',
  19773. ' }',
  19774. ' }, {',
  19775. ' get: function () {',
  19776. ' return v;',
  19777. ' },',
  19778. ' set: function (w) {',
  19779. ' v = rtl.setIntfL(v, w);',
  19780. ' }',
  19781. ' });',
  19782. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19783. ' } finally {',
  19784. ' $ir.free();',
  19785. ' rtl._Release(v);',
  19786. ' };',
  19787. '};',
  19788. 'this.DoSome = function () {',
  19789. ' var v = null;',
  19790. ' try {',
  19791. ' $mod.DoIt(v, v, {',
  19792. ' get: function () {',
  19793. ' return v;',
  19794. ' },',
  19795. ' set: function (w) {',
  19796. ' v = rtl.setIntfL(v, w);',
  19797. ' }',
  19798. ' }, {',
  19799. ' get: function () {',
  19800. ' return v;',
  19801. ' },',
  19802. ' set: function (w) {',
  19803. ' v = rtl.setIntfL(v, w);',
  19804. ' }',
  19805. ' });',
  19806. ' } finally {',
  19807. ' rtl._Release(v);',
  19808. ' };',
  19809. '};',
  19810. 'this.i = null;',
  19811. '']),
  19812. LinesToStr([ // $mod.$main
  19813. '$mod.DoIt($mod.i, $mod.i, {',
  19814. ' p: $mod,',
  19815. ' get: function () {',
  19816. ' return this.p.i;',
  19817. ' },',
  19818. ' set: function (v) {',
  19819. ' rtl.setIntfP(this.p, "i", v);',
  19820. ' }',
  19821. '}, {',
  19822. ' p: $mod,',
  19823. ' get: function () {',
  19824. ' return this.p.i;',
  19825. ' },',
  19826. ' set: function (v) {',
  19827. ' rtl.setIntfP(this.p, "i", v);',
  19828. ' }',
  19829. '});',
  19830. '']));
  19831. end;
  19832. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19833. begin
  19834. StartProgram(false);
  19835. Add([
  19836. '{$interfaces com}',
  19837. 'type',
  19838. ' IUnknown = interface',
  19839. ' function _AddRef: longint;',
  19840. ' function _Release: longint;',
  19841. ' end;',
  19842. ' TObject = class(IUnknown)',
  19843. ' function _AddRef: longint; virtual; abstract;',
  19844. ' function _Release: longint; virtual; abstract;',
  19845. ' end;',
  19846. 'procedure DoIt(out i);',
  19847. 'begin end;',
  19848. 'procedure DoSome;',
  19849. 'var v: IUnknown;',
  19850. 'begin',
  19851. ' DoIt(v);',
  19852. 'end;',
  19853. 'function GetIt: IUnknown;',
  19854. 'begin',
  19855. ' DoIt(Result);',
  19856. 'end;',
  19857. 'var i: IUnknown;',
  19858. 'begin',
  19859. ' DoIt(i);',
  19860. '']);
  19861. ConvertProgram;
  19862. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19863. LinesToStr([ // statements
  19864. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19865. 'rtl.createClass(this, "TObject", null, function () {',
  19866. ' this.$init = function () {',
  19867. ' };',
  19868. ' this.$final = function () {',
  19869. ' };',
  19870. ' rtl.addIntf(this, $mod.IUnknown);',
  19871. '});',
  19872. 'this.DoIt = function (i) {',
  19873. '};',
  19874. 'this.DoSome = function () {',
  19875. ' var v = null;',
  19876. ' try {',
  19877. ' $mod.DoIt({',
  19878. ' get: function () {',
  19879. ' return v;',
  19880. ' },',
  19881. ' set: function (w) {',
  19882. ' v = w;',
  19883. ' }',
  19884. ' });',
  19885. ' } finally {',
  19886. ' rtl._Release(v);',
  19887. ' };',
  19888. '};',
  19889. 'this.GetIt = function () {',
  19890. ' var Result = null;',
  19891. ' var $ok = false;',
  19892. ' try {',
  19893. ' $mod.DoIt({',
  19894. ' get: function () {',
  19895. ' return Result;',
  19896. ' },',
  19897. ' set: function (v) {',
  19898. ' Result = v;',
  19899. ' }',
  19900. ' });',
  19901. ' $ok = true;',
  19902. ' } finally {',
  19903. ' if (!$ok) rtl._Release(Result);',
  19904. ' };',
  19905. ' return Result;',
  19906. '};',
  19907. 'this.i = null;',
  19908. '']),
  19909. LinesToStr([ // $mod.$main
  19910. 'try {',
  19911. ' $mod.DoIt({',
  19912. ' p: $mod,',
  19913. ' get: function () {',
  19914. ' return this.p.i;',
  19915. ' },',
  19916. ' set: function (v) {',
  19917. ' this.p.i = v;',
  19918. ' }',
  19919. ' });',
  19920. '} finally {',
  19921. ' rtl._Release($mod.i);',
  19922. '};',
  19923. '']));
  19924. end;
  19925. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19926. begin
  19927. StartProgram(false);
  19928. Add([
  19929. '{$interfaces com}',
  19930. 'type',
  19931. ' IUnknown = interface',
  19932. ' function _AddRef: longint;',
  19933. ' function _Release: longint;',
  19934. ' end;',
  19935. ' TObject = class(IUnknown)',
  19936. ' function _AddRef: longint; virtual; abstract;',
  19937. ' function _Release: longint; virtual; abstract;',
  19938. ' end;',
  19939. 'function GetIt: IUnknown;',
  19940. 'begin',
  19941. 'end;',
  19942. 'procedure DoSome;',
  19943. 'var v: IUnknown;',
  19944. ' i: longint;',
  19945. 'begin',
  19946. ' v:=GetIt;',
  19947. ' v:=GetIt();',
  19948. ' GetIt()._AddRef;',
  19949. ' i:=GetIt()._AddRef;',
  19950. 'end;',
  19951. 'var v: IUnknown;',
  19952. ' i: longint;',
  19953. 'begin',
  19954. ' v:=GetIt;',
  19955. ' v:=GetIt();',
  19956. ' GetIt()._AddRef;',
  19957. ' i:=GetIt()._AddRef;',
  19958. '']);
  19959. ConvertProgram;
  19960. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19961. LinesToStr([ // statements
  19962. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19963. 'rtl.createClass(this, "TObject", null, function () {',
  19964. ' this.$init = function () {',
  19965. ' };',
  19966. ' this.$final = function () {',
  19967. ' };',
  19968. ' rtl.addIntf(this, $mod.IUnknown);',
  19969. '});',
  19970. 'this.GetIt = function () {',
  19971. ' var Result = null;',
  19972. ' return Result;',
  19973. '};',
  19974. 'this.DoSome = function () {',
  19975. ' var v = null;',
  19976. ' var i = 0;',
  19977. ' var $ir = rtl.createIntfRefs();',
  19978. ' try {',
  19979. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19980. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19981. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19982. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19983. ' } finally {',
  19984. ' $ir.free();',
  19985. ' rtl._Release(v);',
  19986. ' };',
  19987. '};',
  19988. 'this.v = null;',
  19989. 'this.i = 0;',
  19990. '']),
  19991. LinesToStr([ // $mod.$main
  19992. 'var $ir = rtl.createIntfRefs();',
  19993. 'try {',
  19994. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19995. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19996. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19997. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19998. '} finally {',
  19999. ' $ir.free();',
  20000. '};',
  20001. '']));
  20002. end;
  20003. procedure TTestModule.TestClassInterface_COM_Property;
  20004. begin
  20005. StartProgram(false);
  20006. Add([
  20007. '{$interfaces com}',
  20008. 'type',
  20009. ' IUnknown = interface',
  20010. ' function _AddRef: longint;',
  20011. ' function _Release: longint;',
  20012. ' end;',
  20013. ' TObject = class(IUnknown)',
  20014. ' FAnt: IUnknown;',
  20015. ' function _AddRef: longint; virtual; abstract;',
  20016. ' function _Release: longint; virtual; abstract;',
  20017. ' function GetBird: IUnknown; virtual; abstract;',
  20018. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20019. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20020. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20021. ' property Ant: IUnknown read FAnt write FAnt;',
  20022. ' property Bird: IUnknown read GetBird write SetBird;',
  20023. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20024. ' end;',
  20025. 'procedure DoIt;',
  20026. 'var',
  20027. ' o: TObject;',
  20028. ' v: IUnknown;',
  20029. 'begin',
  20030. ' v:=o.Ant;',
  20031. ' o.Ant:=v;',
  20032. ' o.Ant:=o.Ant;',
  20033. ' v:=o.Bird;',
  20034. ' o.Bird:=v;',
  20035. ' o.Bird:=o.Bird;',
  20036. ' v:=o.Items[1];',
  20037. ' o.Items[2]:=v;',
  20038. ' o.Items[3]:=o.Items[4];',
  20039. ' v:=o[5];',
  20040. ' o[6]:=v;',
  20041. ' o[7]:=o[8];',
  20042. 'end;',
  20043. 'begin',
  20044. '']);
  20045. ConvertProgram;
  20046. CheckSource('TestClassInterface_COM_Property',
  20047. LinesToStr([ // statements
  20048. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20049. 'rtl.createClass(this, "TObject", null, function () {',
  20050. ' this.$init = function () {',
  20051. ' this.FAnt = null;',
  20052. ' };',
  20053. ' this.$final = function () {',
  20054. ' this.FAnt = undefined;',
  20055. ' };',
  20056. ' rtl.addIntf(this, $mod.IUnknown);',
  20057. '});',
  20058. 'this.DoIt = function () {',
  20059. ' var o = null;',
  20060. ' var v = null;',
  20061. ' var $ir = rtl.createIntfRefs();',
  20062. ' try {',
  20063. ' v = rtl.setIntfL(v, o.FAnt);',
  20064. ' rtl.setIntfP(o, "FAnt", v);',
  20065. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20066. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20067. ' o.SetBird(v);',
  20068. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20069. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20070. ' o.SetItems(2, v);',
  20071. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20072. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20073. ' o.SetItems(6, v);',
  20074. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20075. ' } finally {',
  20076. ' $ir.free();',
  20077. ' rtl._Release(v);',
  20078. ' };',
  20079. '};',
  20080. '']),
  20081. LinesToStr([ // $mod.$main
  20082. '']));
  20083. end;
  20084. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20085. begin
  20086. StartProgram(false);
  20087. Add([
  20088. '{$interfaces com}',
  20089. 'type',
  20090. ' IUnknown = interface',
  20091. ' function _AddRef: longint;',
  20092. ' function _Release: longint;',
  20093. ' function GetBird: IUnknown;',
  20094. ' procedure SetBird(Value: IUnknown);',
  20095. ' function GetItems(Index: longint): IUnknown;',
  20096. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20097. ' property Bird: IUnknown read GetBird write SetBird;',
  20098. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20099. ' end;',
  20100. ' TObject = class(IUnknown)',
  20101. ' function _AddRef: longint; virtual; abstract;',
  20102. ' function _Release: longint; virtual; abstract;',
  20103. ' function GetBird: IUnknown; virtual; abstract;',
  20104. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20105. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20106. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20107. ' end;',
  20108. 'procedure DoIt;',
  20109. 'var',
  20110. ' o: TObject;',
  20111. ' v: IUnknown;',
  20112. 'begin',
  20113. ' v:=v.Items[1];',
  20114. ' v.Items[2]:=v;',
  20115. ' v.Items[3]:=v.Items[4];',
  20116. ' v:=v[5];',
  20117. ' v[6]:=v;',
  20118. ' v[7]:=v[8];',
  20119. ' v[9].Bird.Bird:=v;',
  20120. ' v:=v.Bird[10].Bird',
  20121. 'end;',
  20122. 'begin',
  20123. '']);
  20124. ConvertProgram;
  20125. CheckSource('TestClassInterface_COM_IntfProperty',
  20126. LinesToStr([ // statements
  20127. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20128. ' "_AddRef",',
  20129. ' "_Release",',
  20130. ' "GetBird",',
  20131. ' "SetBird",',
  20132. ' "GetItems",',
  20133. ' "SetItems"',
  20134. '], null);',
  20135. 'rtl.createClass(this, "TObject", null, function () {',
  20136. ' this.$init = function () {',
  20137. ' };',
  20138. ' this.$final = function () {',
  20139. ' };',
  20140. ' rtl.addIntf(this, $mod.IUnknown);',
  20141. '});',
  20142. 'this.DoIt = function () {',
  20143. ' var o = null;',
  20144. ' var v = null;',
  20145. ' var $ir = rtl.createIntfRefs();',
  20146. ' try {',
  20147. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20148. ' v.SetItems(2, v);',
  20149. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20150. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20151. ' v.SetItems(6, v);',
  20152. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20153. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20154. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20155. ' } finally {',
  20156. ' $ir.free();',
  20157. ' rtl._Release(v);',
  20158. ' };',
  20159. '};',
  20160. '']),
  20161. LinesToStr([ // $mod.$main
  20162. '']));
  20163. end;
  20164. procedure TTestModule.TestClassInterface_COM_Delegation;
  20165. begin
  20166. StartProgram(false);
  20167. Add([
  20168. '{$interfaces com}',
  20169. 'type',
  20170. ' IUnknown = interface',
  20171. ' function _AddRef: longint;',
  20172. ' function _Release: longint;',
  20173. ' end;',
  20174. ' IBird = interface(IUnknown)',
  20175. ' procedure Fly(s: string);',
  20176. ' end;',
  20177. ' IEagle = interface(IBird) end;',
  20178. ' IDove = interface(IBird) end;',
  20179. ' ISwallow = interface(IBird) end;',
  20180. ' TObject = class',
  20181. ' end;',
  20182. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20183. ' function _AddRef: longint; virtual; abstract;',
  20184. ' function _Release: longint; virtual; abstract;',
  20185. ' procedure Fly(s: string); virtual; abstract;',
  20186. ' end;',
  20187. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20188. ' function _AddRef: longint; virtual; abstract;',
  20189. ' function _Release: longint; virtual; abstract;',
  20190. ' FBirdIntf: IBird;',
  20191. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20192. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20193. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20194. ' FDoveObj: TBird;',
  20195. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20196. ' function GetSwallowObj: TBird; virtual; abstract;',
  20197. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20198. ' end;',
  20199. 'begin',
  20200. '']);
  20201. ConvertProgram;
  20202. CheckSource('TestClassInterface_COM_Delegation',
  20203. LinesToStr([ // statements
  20204. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20205. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20206. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20207. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20208. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20209. 'rtl.createClass(this, "TObject", null, function () {',
  20210. ' this.$init = function () {',
  20211. ' };',
  20212. ' this.$final = function () {',
  20213. ' };',
  20214. '});',
  20215. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20216. ' rtl.addIntf(this, $mod.IBird);',
  20217. ' rtl.addIntf(this, $mod.IEagle);',
  20218. ' rtl.addIntf(this, $mod.IDove);',
  20219. ' rtl.addIntf(this, $mod.ISwallow);',
  20220. '});',
  20221. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20222. ' this.$init = function () {',
  20223. ' $mod.TObject.$init.call(this);',
  20224. ' this.FBirdIntf = null;',
  20225. ' this.FDoveObj = null;',
  20226. ' };',
  20227. ' this.$final = function () {',
  20228. ' this.FBirdIntf = undefined;',
  20229. ' this.FDoveObj = undefined;',
  20230. ' $mod.TObject.$final.call(this);',
  20231. ' };',
  20232. ' this.$intfmaps = {',
  20233. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20234. ' return rtl._AddRef(this.FBirdIntf);',
  20235. ' },',
  20236. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20237. ' return this.GetEagleIntf();',
  20238. ' },',
  20239. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20240. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20241. ' },',
  20242. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20243. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20244. ' }',
  20245. ' };',
  20246. '});',
  20247. '']),
  20248. LinesToStr([ // $mod.$main
  20249. '']));
  20250. end;
  20251. procedure TTestModule.TestClassInterface_COM_With;
  20252. begin
  20253. StartProgram(false);
  20254. Add([
  20255. '{$interfaces com}',
  20256. 'type',
  20257. ' IUnknown = interface',
  20258. ' function _AddRef: longint;',
  20259. ' function _Release: longint;',
  20260. ' function GetAnt: IUnknown;',
  20261. ' property Ant: IUnknown read GetAnt;',
  20262. ' end;',
  20263. ' TObject = class(IUnknown)',
  20264. ' function _AddRef: longint; virtual; abstract;',
  20265. ' function _Release: longint; virtual; abstract;',
  20266. ' function GetAnt: IUnknown; virtual; abstract;',
  20267. ' property Ant: IUnknown read GetAnt;',
  20268. ' end;',
  20269. 'procedure DoIt;',
  20270. 'var',
  20271. ' i: IUnknown;',
  20272. 'begin',
  20273. ' with i do ',
  20274. ' GetAnt;',
  20275. ' with i.Ant, Ant do ',
  20276. ' GetAnt;',
  20277. 'end;',
  20278. 'begin',
  20279. '']);
  20280. ConvertProgram;
  20281. CheckSource('TestClassInterface_COM_With',
  20282. LinesToStr([ // statements
  20283. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20284. 'rtl.createClass(this, "TObject", null, function () {',
  20285. ' this.$init = function () {',
  20286. ' };',
  20287. ' this.$final = function () {',
  20288. ' };',
  20289. ' rtl.addIntf(this, $mod.IUnknown);',
  20290. '});',
  20291. 'this.DoIt = function () {',
  20292. ' var i = null;',
  20293. ' var $ir = rtl.createIntfRefs();',
  20294. ' try {',
  20295. ' $ir.ref(1, i.GetAnt());',
  20296. ' var $with = $ir.ref(2, i.GetAnt());',
  20297. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20298. ' $ir.ref(4, $with1.GetAnt());',
  20299. ' } finally {',
  20300. ' $ir.free();',
  20301. ' };',
  20302. '};',
  20303. '']),
  20304. LinesToStr([ // $mod.$main
  20305. '']));
  20306. end;
  20307. procedure TTestModule.TestClassInterface_COM_ForIn;
  20308. begin
  20309. StartProgram(false);
  20310. Add([
  20311. '{$interfaces com}',
  20312. 'type',
  20313. ' IUnknown = interface end;',
  20314. ' TObject = class',
  20315. ' Id: longint;',
  20316. ' end;',
  20317. ' IEnumerator = interface(IUnknown)',
  20318. ' function GetCurrent: TObject;',
  20319. ' function MoveNext: Boolean;',
  20320. ' property Current: TObject read GetCurrent;',
  20321. ' end;',
  20322. ' IEnumerable = interface(IUnknown)',
  20323. ' function GetEnumerator: IEnumerator;',
  20324. ' end;',
  20325. 'var',
  20326. ' o: TObject;',
  20327. ' i: IEnumerable;',
  20328. 'begin',
  20329. ' for o in i do o.Id:=3;',
  20330. '']);
  20331. ConvertProgram;
  20332. CheckSource('TestClassInterface_COM_ForIn',
  20333. LinesToStr([ // statements
  20334. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20335. 'rtl.createClass(this, "TObject", null, function () {',
  20336. ' this.$init = function () {',
  20337. ' this.Id = 0;',
  20338. ' };',
  20339. ' this.$final = function () {',
  20340. ' };',
  20341. '});',
  20342. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20343. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20344. 'this.o = null;',
  20345. 'this.i = null;',
  20346. '']),
  20347. LinesToStr([ // $mod.$main
  20348. 'var $in = $mod.i.GetEnumerator();',
  20349. 'try {',
  20350. ' while ($in.MoveNext()) {',
  20351. ' $mod.o = $in.GetCurrent();',
  20352. ' $mod.o.Id = 3;',
  20353. ' }',
  20354. '} finally {',
  20355. ' rtl._Release($in)',
  20356. '};',
  20357. '']));
  20358. end;
  20359. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20360. begin
  20361. StartProgram(false);
  20362. Add([
  20363. '{$interfaces com}',
  20364. 'type',
  20365. ' IUnknown = interface',
  20366. ' function _AddRef: longint;',
  20367. ' function _Release: longint;',
  20368. ' end;',
  20369. ' TObject = class',
  20370. ' end;',
  20371. ' TArrOfIntf = array of IUnknown;',
  20372. 'begin',
  20373. '']);
  20374. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20375. ConvertProgram;
  20376. end;
  20377. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20378. begin
  20379. StartProgram(false);
  20380. Add([
  20381. '{$interfaces com}',
  20382. 'type',
  20383. ' IUnknown = interface',
  20384. ' function _AddRef: longint;',
  20385. ' function _Release: longint;',
  20386. ' end;',
  20387. ' TRec = record',
  20388. ' i: IUnknown;',
  20389. ' end;',
  20390. 'begin',
  20391. '']);
  20392. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20393. ConvertProgram;
  20394. end;
  20395. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20396. begin
  20397. StartUnit(false);
  20398. Add([
  20399. '{$interfaces com}',
  20400. 'interface',
  20401. 'implementation',
  20402. 'type',
  20403. ' IUnknown = interface',
  20404. ' function _AddRef: longint;',
  20405. ' end;',
  20406. ' TObject = class(IUnknown)',
  20407. ' function _AddRef: longint;',
  20408. ' end;',
  20409. 'function TObject._AddRef: longint; begin end;',
  20410. 'var i: IUnknown;',
  20411. ' o: TObject;',
  20412. 'initialization',
  20413. ' i:=nil;',
  20414. ' i:=i;',
  20415. ' i:=o;',
  20416. ' if (o as IUnknown)=nil then ;',
  20417. '']);
  20418. ConvertUnit;
  20419. CheckSource('TestClassInterface_COM_UnitInitialization',
  20420. LinesToStr([ // statements
  20421. 'var $impl = $mod.$impl;',
  20422. '']),
  20423. LinesToStr([ // this.$init
  20424. 'var $ir = rtl.createIntfRefs();',
  20425. 'try {',
  20426. ' rtl.setIntfP($impl, "i", null);',
  20427. ' rtl.setIntfP($impl, "i", $impl.i);',
  20428. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20429. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20430. '} finally {',
  20431. ' $ir.free();',
  20432. '};',
  20433. '']),
  20434. LinesToStr([ // implementation
  20435. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20436. 'rtl.createClass($impl, "TObject", null, function () {',
  20437. ' this.$init = function () {',
  20438. ' };',
  20439. ' this.$final = function () {',
  20440. ' };',
  20441. ' this._AddRef = function () {',
  20442. ' var Result = 0;',
  20443. ' return Result;',
  20444. ' };',
  20445. ' rtl.addIntf(this, $impl.IUnknown);',
  20446. '});',
  20447. '$impl.i = null;',
  20448. '$impl.o = null;',
  20449. ''])
  20450. );
  20451. end;
  20452. procedure TTestModule.TestClassInterface_GUID;
  20453. begin
  20454. StartProgram(false);
  20455. Add([
  20456. '{$interfaces corba}',
  20457. 'type',
  20458. ' IUnknown = interface',
  20459. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20460. ' end;',
  20461. ' TObject = class end;',
  20462. ' TGUID = record D1, D2, D3, D4: word; end;',
  20463. ' TAliasGUID = TGUID;',
  20464. ' TGUIDString = type string;',
  20465. ' TAliasGUIDString = TGUIDString;',
  20466. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20467. 'begin end;',
  20468. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20469. 'begin end;',
  20470. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20471. 'begin end;',
  20472. 'var',
  20473. ' i: IUnknown;',
  20474. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20475. ' s: TAliasGUIDString;',
  20476. 'begin',
  20477. ' DoConstGUIDIt(IUnknown);',
  20478. ' DoDefGUID(IUnknown);',
  20479. ' DoStr(IUnknown);',
  20480. ' DoConstGUIDIt(i);',
  20481. ' DoDefGUID(i);',
  20482. ' DoStr(i);',
  20483. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20484. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20485. ' DoStr(g);',
  20486. ' g:=i;',
  20487. ' g:=IUnknown;',
  20488. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20489. ' s:=i;',
  20490. ' s:=IUnknown;',
  20491. ' s:=g;',
  20492. ' if g=i then ;',
  20493. ' if i=g then ;',
  20494. ' if g=IUnknown then ;',
  20495. ' if IUnknown=g then ;',
  20496. ' if s=i then ;',
  20497. ' if i=s then ;',
  20498. ' if s=IUnknown then ;',
  20499. ' if IUnknown=s then ;',
  20500. ' if s=g then ;',
  20501. ' if g=s then ;',
  20502. '']);
  20503. ConvertProgram;
  20504. CheckSource('TestClassInterface_GUID',
  20505. LinesToStr([ // statements
  20506. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20507. 'rtl.createClass(this, "TObject", null, function () {',
  20508. ' this.$init = function () {',
  20509. ' };',
  20510. ' this.$final = function () {',
  20511. ' };',
  20512. '});',
  20513. 'rtl.recNewT(this, "TGUID", function () {',
  20514. ' this.D1 = 0;',
  20515. ' this.D2 = 0;',
  20516. ' this.D3 = 0;',
  20517. ' this.D4 = 0;',
  20518. ' this.$eq = function (b) {',
  20519. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20520. ' };',
  20521. ' this.$assign = function (s) {',
  20522. ' this.D1 = s.D1;',
  20523. ' this.D2 = s.D2;',
  20524. ' this.D3 = s.D3;',
  20525. ' this.D4 = s.D4;',
  20526. ' return this;',
  20527. ' };',
  20528. '});',
  20529. 'this.DoConstGUIDIt = function (g) {',
  20530. '};',
  20531. 'this.DoDefGUID = function (g) {',
  20532. '};',
  20533. 'this.DoStr = function (s) {',
  20534. '};',
  20535. 'this.i = null;',
  20536. 'this.g = this.TGUID.$clone({',
  20537. ' D1: 0xD91C9AF4,',
  20538. ' D2: 0x3C93,',
  20539. ' D3: 0x420F,',
  20540. ' D4: [',
  20541. ' 0xA3,',
  20542. ' 0x03,',
  20543. ' 0xBF,',
  20544. ' 0x5B,',
  20545. ' 0xA8,',
  20546. ' 0x2B,',
  20547. ' 0xFD,',
  20548. ' 0x23',
  20549. ' ]',
  20550. '});',
  20551. 'this.s = "";',
  20552. '']),
  20553. LinesToStr([ // $mod.$main
  20554. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20555. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20556. '$mod.DoStr($mod.IUnknown.$guid);',
  20557. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20558. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20559. '$mod.DoStr($mod.i.$guid);',
  20560. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20561. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20562. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20563. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20564. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20565. '$mod.g.$assign({',
  20566. ' D1: 0xD91C9AF4,',
  20567. ' D2: 0x3C93,',
  20568. ' D3: 0x420F,',
  20569. ' D4: [',
  20570. ' 0xA3,',
  20571. ' 0x03,',
  20572. ' 0xBF,',
  20573. ' 0x5B,',
  20574. ' 0xA8,',
  20575. ' 0x2B,',
  20576. ' 0xFD,',
  20577. ' 0x23',
  20578. ' ]',
  20579. '});',
  20580. '$mod.s = $mod.i.$guid;',
  20581. '$mod.s = $mod.IUnknown.$guid;',
  20582. '$mod.s = rtl.guidrToStr($mod.g);',
  20583. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20584. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20585. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20586. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20587. 'if ($mod.s === $mod.i.$guid) ;',
  20588. 'if ($mod.i.$guid === $mod.s) ;',
  20589. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20590. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20591. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20592. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20593. '']));
  20594. end;
  20595. procedure TTestModule.TestClassInterface_GUIDProperty;
  20596. begin
  20597. StartProgram(false);
  20598. Add([
  20599. '{$interfaces corba}',
  20600. 'type',
  20601. ' IUnknown = interface',
  20602. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20603. ' end;',
  20604. ' TGUID = record D1, D2, D3, D4: word; end;',
  20605. ' TAliasGUID = TGUID;',
  20606. ' TGUIDString = type string;',
  20607. ' TAliasGUIDString = TGUIDString;',
  20608. ' TObject = class',
  20609. ' function GetG: TAliasGUID; virtual; abstract;',
  20610. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20611. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20612. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20613. ' property g: TAliasGUID read GetG write SetG;',
  20614. ' property s: TAliasGUIDString read GetS write SetS;',
  20615. ' end;',
  20616. 'var o: TObject;',
  20617. 'begin',
  20618. ' o.g:=IUnknown;',
  20619. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20620. ' o.s:=IUnknown;',
  20621. ' o.s:=o.g;',
  20622. '']);
  20623. ConvertProgram;
  20624. CheckSource('TestClassInterface_GUIDProperty',
  20625. LinesToStr([ // statements
  20626. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20627. 'rtl.recNewT(this, "TGUID", function () {',
  20628. ' this.D1 = 0;',
  20629. ' this.D2 = 0;',
  20630. ' this.D3 = 0;',
  20631. ' this.D4 = 0;',
  20632. ' this.$eq = function (b) {',
  20633. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20634. ' };',
  20635. ' this.$assign = function (s) {',
  20636. ' this.D1 = s.D1;',
  20637. ' this.D2 = s.D2;',
  20638. ' this.D3 = s.D3;',
  20639. ' this.D4 = s.D4;',
  20640. ' return this;',
  20641. ' };',
  20642. '});',
  20643. 'rtl.createClass(this, "TObject", null, function () {',
  20644. ' this.$init = function () {',
  20645. ' };',
  20646. ' this.$final = function () {',
  20647. ' };',
  20648. '});',
  20649. 'this.o = null;',
  20650. '']),
  20651. LinesToStr([ // $mod.$main
  20652. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20653. '$mod.o.SetG({',
  20654. ' D1: 0xD91C9AF4,',
  20655. ' D2: 0x3C93,',
  20656. ' D3: 0x420F,',
  20657. ' D4: [',
  20658. ' 0xA3,',
  20659. ' 0x03,',
  20660. ' 0xBF,',
  20661. ' 0x5B,',
  20662. ' 0xA8,',
  20663. ' 0x2B,',
  20664. ' 0xFD,',
  20665. ' 0x23',
  20666. ' ]',
  20667. '});',
  20668. '$mod.o.SetS($mod.IUnknown.$guid);',
  20669. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20670. '']));
  20671. end;
  20672. procedure TTestModule.TestClassHelper_ClassVar;
  20673. begin
  20674. StartProgram(false);
  20675. Add([
  20676. 'type',
  20677. ' TObject = class',
  20678. ' end;',
  20679. ' THelper = class helper for TObject',
  20680. ' const',
  20681. ' One = 1;',
  20682. ' Two: word = 2;',
  20683. ' class var',
  20684. ' Glob: word;',
  20685. ' function Foo(w: word): word;',
  20686. ' class function Bar(w: word): word;',
  20687. ' end;',
  20688. 'function THelper.foo(w: word): word;',
  20689. 'begin',
  20690. ' Result:=w;',
  20691. ' Two:=One+w;',
  20692. ' Glob:=Glob;',
  20693. ' Result:=Self.Glob;',
  20694. ' Self.Glob:=Self.Glob;',
  20695. ' with Self do Glob:=Glob;',
  20696. 'end;',
  20697. 'class function THelper.bar(w: word): word;',
  20698. 'begin',
  20699. ' Result:=w;',
  20700. ' Two:=One;',
  20701. ' Glob:=Glob;',
  20702. ' Self.Glob:=Self.Glob;',
  20703. ' with Self do Glob:=Glob;',
  20704. 'end;',
  20705. 'var o: TObject;',
  20706. 'begin',
  20707. ' tobject.two:=tobject.one;',
  20708. ' tobject.Glob:=tobject.Glob;',
  20709. ' with tobject do begin',
  20710. ' two:=one;',
  20711. ' Glob:=Glob;',
  20712. ' end;',
  20713. ' o.two:=o.one;',
  20714. ' o.Glob:=o.Glob;',
  20715. ' with o do begin',
  20716. ' two:=one;',
  20717. ' Glob:=Glob;',
  20718. ' end;',
  20719. '']);
  20720. ConvertProgram;
  20721. CheckSource('TestClassHelper_ClassVar',
  20722. LinesToStr([ // statements
  20723. 'rtl.createClass(this, "TObject", null, function () {',
  20724. ' this.$init = function () {',
  20725. ' };',
  20726. ' this.$final = function () {',
  20727. ' };',
  20728. '});',
  20729. 'rtl.createHelper(this, "THelper", null, function () {',
  20730. ' this.One = 1;',
  20731. ' this.Two = 2;',
  20732. ' this.Glob = 0;',
  20733. ' this.Foo = function (w) {',
  20734. ' var Result = 0;',
  20735. ' Result = w;',
  20736. ' $mod.THelper.Two = 1 + w;',
  20737. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20738. ' Result = $mod.THelper.Glob;',
  20739. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20740. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20741. ' return Result;',
  20742. ' };',
  20743. ' this.Bar = function (w) {',
  20744. ' var Result = 0;',
  20745. ' Result = w;',
  20746. ' $mod.THelper.Two = 1;',
  20747. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20748. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20749. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20750. ' return Result;',
  20751. ' };',
  20752. '});',
  20753. 'this.o = null;',
  20754. '']),
  20755. LinesToStr([ // $mod.$main
  20756. '$mod.THelper.Two = 1;',
  20757. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20758. 'var $with = $mod.TObject;',
  20759. '$mod.THelper.Two = 1;',
  20760. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20761. '$mod.THelper.Two = 1;',
  20762. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20763. 'var $with1 = $mod.o;',
  20764. '$mod.THelper.Two = 1;',
  20765. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20766. '']));
  20767. end;
  20768. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20769. begin
  20770. StartProgram(false);
  20771. Add([
  20772. 'type',
  20773. ' TObject = class',
  20774. ' FSize: word;',
  20775. ' property Size: word read FSize write FSize;',
  20776. ' end;',
  20777. ' THelper = class helper for TObject',
  20778. ' function Foo(w: word = 1): word;',
  20779. ' end;',
  20780. 'function THelper.foo(w: word): word;',
  20781. 'begin',
  20782. ' Result:=Size;',
  20783. ' Size:=Size+2;',
  20784. ' Self.Size:=Self.Size+3;',
  20785. ' FSize:=FSize+4;',
  20786. ' Self.FSize:=Self.FSize+5;',
  20787. ' with Self do begin',
  20788. ' Size:=Size+6;',
  20789. ' FSize:=FSize+7;',
  20790. ' FSize:=FSize+8;',
  20791. ' end;',
  20792. 'end;',
  20793. 'begin',
  20794. '']);
  20795. ConvertProgram;
  20796. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20797. LinesToStr([ // statements
  20798. 'rtl.createClass(this, "TObject", null, function () {',
  20799. ' this.$init = function () {',
  20800. ' this.FSize = 0;',
  20801. ' };',
  20802. ' this.$final = function () {',
  20803. ' };',
  20804. '});',
  20805. 'rtl.createHelper(this, "THelper", null, function () {',
  20806. ' this.Foo = function (w) {',
  20807. ' var Result = 0;',
  20808. ' Result = this.FSize;',
  20809. ' this.FSize = this.FSize + 2;',
  20810. ' this.FSize = this.FSize + 3;',
  20811. ' this.FSize = this.FSize + 4;',
  20812. ' this.FSize = this.FSize + 5;',
  20813. ' this.FSize = this.FSize + 6;',
  20814. ' this.FSize = this.FSize + 7;',
  20815. ' this.FSize = this.FSize + 8;',
  20816. ' return Result;',
  20817. ' };',
  20818. '});',
  20819. '']),
  20820. LinesToStr([ // $mod.$main
  20821. '']));
  20822. end;
  20823. procedure TTestModule.TestClassHelper_Method_Call;
  20824. begin
  20825. StartProgram(false);
  20826. Add([
  20827. 'type',
  20828. ' TObject = class',
  20829. ' procedure Run(w: word = 10);',
  20830. ' end;',
  20831. ' THelper = class helper for TObject',
  20832. ' function Foo(w: word = 1): word;',
  20833. ' end;',
  20834. 'procedure TObject.Run(w: word);',
  20835. 'var o: TObject;',
  20836. 'begin',
  20837. ' Foo;',
  20838. ' Foo();',
  20839. ' Foo(2);',
  20840. ' Self.Foo;',
  20841. ' Self.Foo();',
  20842. ' Self.Foo(3);',
  20843. ' with Self do begin',
  20844. ' Foo;',
  20845. ' Foo();',
  20846. ' Foo(4);',
  20847. ' end;',
  20848. ' with o do Foo(5);',
  20849. 'end;',
  20850. 'function THelper.foo(w: word): word;',
  20851. 'begin',
  20852. ' Run;',
  20853. ' Run();',
  20854. ' Run(11);',
  20855. ' Foo;',
  20856. ' Foo();',
  20857. ' Foo(12);',
  20858. ' Self.Foo;',
  20859. ' Self.Foo();',
  20860. ' Self.Foo(13);',
  20861. ' with Self do begin',
  20862. ' Foo;',
  20863. ' Foo();',
  20864. ' Foo(14);',
  20865. ' end;',
  20866. 'end;',
  20867. 'var Obj: TObject;',
  20868. 'begin',
  20869. ' obj.Foo;',
  20870. ' obj.Foo();',
  20871. ' obj.Foo(21);',
  20872. ' with obj do begin',
  20873. ' Foo;',
  20874. ' Foo();',
  20875. ' Foo(22);',
  20876. ' end;',
  20877. '']);
  20878. ConvertProgram;
  20879. CheckSource('TestClassHelper_Method_Call',
  20880. LinesToStr([ // statements
  20881. 'rtl.createClass(this, "TObject", null, function () {',
  20882. ' this.$init = function () {',
  20883. ' };',
  20884. ' this.$final = function () {',
  20885. ' };',
  20886. ' this.Run = function (w) {',
  20887. ' var o = null;',
  20888. ' $mod.THelper.Foo.call(this, 1);',
  20889. ' $mod.THelper.Foo.call(this, 1);',
  20890. ' $mod.THelper.Foo.call(this, 2);',
  20891. ' $mod.THelper.Foo.call(this, 1);',
  20892. ' $mod.THelper.Foo.call(this, 1);',
  20893. ' $mod.THelper.Foo.call(this, 3);',
  20894. ' $mod.THelper.Foo.call(this, 1);',
  20895. ' $mod.THelper.Foo.call(this, 1);',
  20896. ' $mod.THelper.Foo.call(this, 4);',
  20897. ' $mod.THelper.Foo.call(o, 5);',
  20898. ' };',
  20899. '});',
  20900. 'rtl.createHelper(this, "THelper", null, function () {',
  20901. ' this.Foo = function (w) {',
  20902. ' var Result = 0;',
  20903. ' this.Run(10);',
  20904. ' this.Run(10);',
  20905. ' this.Run(11);',
  20906. ' $mod.THelper.Foo.call(this, 1);',
  20907. ' $mod.THelper.Foo.call(this, 1);',
  20908. ' $mod.THelper.Foo.call(this, 12);',
  20909. ' $mod.THelper.Foo.call(this, 1);',
  20910. ' $mod.THelper.Foo.call(this, 1);',
  20911. ' $mod.THelper.Foo.call(this, 13);',
  20912. ' $mod.THelper.Foo.call(this, 1);',
  20913. ' $mod.THelper.Foo.call(this, 1);',
  20914. ' $mod.THelper.Foo.call(this, 14);',
  20915. ' return Result;',
  20916. ' };',
  20917. '});',
  20918. 'this.Obj = null;',
  20919. '']),
  20920. LinesToStr([ // $mod.$main
  20921. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20922. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20923. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20924. 'var $with = $mod.Obj;',
  20925. '$mod.THelper.Foo.call($with, 1);',
  20926. '$mod.THelper.Foo.call($with, 1);',
  20927. '$mod.THelper.Foo.call($with, 22);',
  20928. '']));
  20929. end;
  20930. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20931. begin
  20932. StartProgram(false);
  20933. Add([
  20934. 'type',
  20935. ' TObject = class',
  20936. ' procedure Run(w: word = 10);',
  20937. ' end;',
  20938. ' THelper = class helper for TObject',
  20939. ' function Foo(w: word = 1): word;',
  20940. ' end;',
  20941. 'procedure TObject.Run(w: word);',
  20942. ' procedure Sub(Self: TObject);',
  20943. ' begin',
  20944. ' Foo;',
  20945. ' Foo();',
  20946. ' Self.Foo;',
  20947. ' Self.Foo();',
  20948. ' with Self do begin',
  20949. ' Foo;',
  20950. ' Foo();',
  20951. ' end;',
  20952. ' end;',
  20953. 'begin',
  20954. 'end;',
  20955. 'function THelper.foo(w: word): word;',
  20956. ' procedure Sub(Self: TObject);',
  20957. ' begin',
  20958. ' Run;',
  20959. ' Run();',
  20960. ' Foo;',
  20961. ' Foo();',
  20962. ' Self.Foo;',
  20963. ' Self.Foo();',
  20964. ' with Self do begin',
  20965. ' Foo;',
  20966. ' Foo();',
  20967. ' end;',
  20968. ' end;',
  20969. 'begin',
  20970. 'end;',
  20971. 'begin',
  20972. '']);
  20973. ConvertProgram;
  20974. CheckSource('TestClassHelper_Method_Nested_Call',
  20975. LinesToStr([ // statements
  20976. 'rtl.createClass(this, "TObject", null, function () {',
  20977. ' this.$init = function () {',
  20978. ' };',
  20979. ' this.$final = function () {',
  20980. ' };',
  20981. ' this.Run = function (w) {',
  20982. ' var $Self = this;',
  20983. ' function Sub(Self) {',
  20984. ' $mod.THelper.Foo.call($Self, 1);',
  20985. ' $mod.THelper.Foo.call($Self, 1);',
  20986. ' $mod.THelper.Foo.call(Self, 1);',
  20987. ' $mod.THelper.Foo.call(Self, 1);',
  20988. ' $mod.THelper.Foo.call(Self, 1);',
  20989. ' $mod.THelper.Foo.call(Self, 1);',
  20990. ' };',
  20991. ' };',
  20992. '});',
  20993. 'rtl.createHelper(this, "THelper", null, function () {',
  20994. ' this.Foo = function (w) {',
  20995. ' var $Self = this;',
  20996. ' var Result = 0;',
  20997. ' function Sub(Self) {',
  20998. ' $Self.Run(10);',
  20999. ' $Self.Run(10);',
  21000. ' $mod.THelper.Foo.call($Self, 1);',
  21001. ' $mod.THelper.Foo.call($Self, 1);',
  21002. ' $mod.THelper.Foo.call(Self, 1);',
  21003. ' $mod.THelper.Foo.call(Self, 1);',
  21004. ' $mod.THelper.Foo.call(Self, 1);',
  21005. ' $mod.THelper.Foo.call(Self, 1);',
  21006. ' };',
  21007. ' return Result;',
  21008. ' };',
  21009. '});',
  21010. '']),
  21011. LinesToStr([ // $mod.$main
  21012. '']));
  21013. end;
  21014. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  21015. begin
  21016. StartProgram(false);
  21017. Add([
  21018. 'type',
  21019. ' TObject = class',
  21020. ' class procedure Run(w: word = 10);',
  21021. ' end;',
  21022. ' THelper = class helper for TObject',
  21023. ' class function Foo(w: word = 1): word;',
  21024. ' end;',
  21025. 'class procedure TObject.Run(w: word);',
  21026. 'begin',
  21027. ' Foo;',
  21028. ' Foo();',
  21029. ' Self.Foo;',
  21030. ' Self.Foo();',
  21031. ' with Self do begin',
  21032. ' Foo;',
  21033. ' Foo();',
  21034. ' end;',
  21035. 'end;',
  21036. 'class function THelper.foo(w: word): word;',
  21037. 'begin',
  21038. ' Run;',
  21039. ' Run();',
  21040. ' Foo;',
  21041. ' Foo();',
  21042. ' Self.Foo;',
  21043. ' Self.Foo();',
  21044. ' with Self do begin',
  21045. ' Foo;',
  21046. ' Foo();',
  21047. ' end;',
  21048. 'end;',
  21049. 'var',
  21050. ' Obj: TObject;',
  21051. 'begin',
  21052. ' obj.Foo;',
  21053. ' obj.Foo();',
  21054. ' with obj do begin',
  21055. ' Foo;',
  21056. ' Foo();',
  21057. ' end;',
  21058. ' tobject.Foo;',
  21059. ' tobject.Foo();',
  21060. ' with tobject do begin',
  21061. ' Foo;',
  21062. ' Foo();',
  21063. ' end;',
  21064. '']);
  21065. ConvertProgram;
  21066. CheckSource('TestClassHelper_ClassMethod_Call',
  21067. LinesToStr([ // statements
  21068. 'rtl.createClass(this, "TObject", null, function () {',
  21069. ' this.$init = function () {',
  21070. ' };',
  21071. ' this.$final = function () {',
  21072. ' };',
  21073. ' this.Run = function (w) {',
  21074. ' $mod.THelper.Foo.call(this, 1);',
  21075. ' $mod.THelper.Foo.call(this, 1);',
  21076. ' $mod.THelper.Foo.call(this, 1);',
  21077. ' $mod.THelper.Foo.call(this, 1);',
  21078. ' $mod.THelper.Foo.call(this, 1);',
  21079. ' $mod.THelper.Foo.call(this, 1);',
  21080. ' };',
  21081. '});',
  21082. 'rtl.createHelper(this, "THelper", null, function () {',
  21083. ' this.Foo = function (w) {',
  21084. ' var Result = 0;',
  21085. ' this.Run(10);',
  21086. ' this.Run(10);',
  21087. ' $mod.THelper.Foo.call(this, 1);',
  21088. ' $mod.THelper.Foo.call(this, 1);',
  21089. ' $mod.THelper.Foo.call(this, 1);',
  21090. ' $mod.THelper.Foo.call(this, 1);',
  21091. ' $mod.THelper.Foo.call(this, 1);',
  21092. ' $mod.THelper.Foo.call(this, 1);',
  21093. ' return Result;',
  21094. ' };',
  21095. '});',
  21096. 'this.Obj = null;',
  21097. '']),
  21098. LinesToStr([ // $mod.$main
  21099. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21100. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21101. 'var $with = $mod.Obj;',
  21102. '$mod.THelper.Foo.call($with.$class, 1);',
  21103. '$mod.THelper.Foo.call($with.$class, 1);',
  21104. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21105. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21106. 'var $with1 = $mod.TObject;',
  21107. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21108. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21109. '']));
  21110. end;
  21111. procedure TTestModule.TestClassHelper_ClassOf;
  21112. begin
  21113. StartProgram(false);
  21114. Add([
  21115. 'type',
  21116. ' TObject = class',
  21117. ' end;',
  21118. ' TClass = class of TObject;',
  21119. ' THelper = class helper for TObject',
  21120. ' class function Foo(w: word = 1): word;',
  21121. ' end;',
  21122. 'class function THelper.foo(w: word): word;',
  21123. 'begin',
  21124. 'end;',
  21125. 'var',
  21126. ' c: TClass;',
  21127. 'begin',
  21128. ' c.Foo;',
  21129. ' c.Foo();',
  21130. ' with c do begin',
  21131. ' Foo;',
  21132. ' Foo();',
  21133. ' end;',
  21134. '']);
  21135. ConvertProgram;
  21136. CheckSource('TestClassHelper_ClassOf',
  21137. LinesToStr([ // statements
  21138. 'rtl.createClass(this, "TObject", null, function () {',
  21139. ' this.$init = function () {',
  21140. ' };',
  21141. ' this.$final = function () {',
  21142. ' };',
  21143. '});',
  21144. 'rtl.createHelper(this, "THelper", null, function () {',
  21145. ' this.Foo = function (w) {',
  21146. ' var Result = 0;',
  21147. ' return Result;',
  21148. ' };',
  21149. '});',
  21150. 'this.c = null;',
  21151. '']),
  21152. LinesToStr([ // $mod.$main
  21153. '$mod.THelper.Foo.call($mod.c, 1);',
  21154. '$mod.THelper.Foo.call($mod.c, 1);',
  21155. 'var $with = $mod.c;',
  21156. '$mod.THelper.Foo.call($with, 1);',
  21157. '$mod.THelper.Foo.call($with, 1);',
  21158. '']));
  21159. end;
  21160. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21161. begin
  21162. StartProgram(false);
  21163. Add([
  21164. '{$mode objfpc}',
  21165. 'type',
  21166. ' TObject = class',
  21167. ' procedure DoIt;',
  21168. ' end;',
  21169. ' THelper = class helper for TObject',
  21170. ' procedure Fly(w: word = 1);',
  21171. ' class procedure Glide(w: word = 1);',
  21172. ' class procedure Run(w: word = 1); static;',
  21173. ' end;',
  21174. ' TFly = procedure(w: word) of object;',
  21175. ' TGlide = TFly;',
  21176. ' TRun = procedure(w: word);',
  21177. 'var',
  21178. ' f: TFly;',
  21179. ' g: TGlide;',
  21180. ' r: TRun;',
  21181. 'procedure TObject.DoIt;',
  21182. 'begin',
  21183. ' f:=@fly;',
  21184. ' g:=@glide;',
  21185. ' r:=@run;',
  21186. ' f:[email protected];',
  21187. ' g:[email protected];',
  21188. ' r:[email protected];',
  21189. ' with self do begin',
  21190. ' f:=@fly;',
  21191. ' g:=@glide;',
  21192. ' r:=@run;',
  21193. ' end;',
  21194. 'end;',
  21195. 'procedure THelper.fly(w: word);',
  21196. 'begin',
  21197. ' f:=@fly;',
  21198. ' g:=@glide;',
  21199. ' r:=@run;',
  21200. 'end;',
  21201. 'class procedure THelper.glide(w: word);',
  21202. 'begin',
  21203. ' g:=@glide;',
  21204. ' r:=@run;',
  21205. 'end;',
  21206. 'class procedure THelper.run(w: word);',
  21207. 'begin',
  21208. ' g:=@glide;',
  21209. ' r:=@run;',
  21210. 'end;',
  21211. 'var',
  21212. ' Obj: TObject;',
  21213. 'begin',
  21214. ' f:[email protected];',
  21215. ' g:[email protected];',
  21216. ' r:[email protected];',
  21217. ' with obj do begin',
  21218. ' f:=@fly;',
  21219. ' g:=@glide;',
  21220. ' r:=@run;',
  21221. ' end;',
  21222. ' g:[email protected];',
  21223. ' r:[email protected];',
  21224. ' with tobject do begin',
  21225. ' g:=@glide;',
  21226. ' r:=@run;',
  21227. ' end;',
  21228. '']);
  21229. ConvertProgram;
  21230. CheckSource('TestClassHelper_MethodRefObjFPC',
  21231. LinesToStr([ // statements
  21232. 'rtl.createClass(this, "TObject", null, function () {',
  21233. ' this.$init = function () {',
  21234. ' };',
  21235. ' this.$final = function () {',
  21236. ' };',
  21237. ' this.DoIt = function () {',
  21238. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21239. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21240. ' $mod.r = $mod.THelper.Run;',
  21241. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21242. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21243. ' $mod.r = $mod.THelper.Run;',
  21244. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21245. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21246. ' $mod.r = $mod.THelper.Run;',
  21247. ' };',
  21248. '});',
  21249. 'rtl.createHelper(this, "THelper", null, function () {',
  21250. ' this.Fly = function (w) {',
  21251. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21252. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21253. ' $mod.r = $mod.THelper.Run;',
  21254. ' };',
  21255. ' this.Glide = function (w) {',
  21256. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21257. ' $mod.r = $mod.THelper.Run;',
  21258. ' };',
  21259. ' this.Run = function (w) {',
  21260. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21261. ' $mod.r = $mod.THelper.Run;',
  21262. ' };',
  21263. '});',
  21264. 'this.f = null;',
  21265. 'this.g = null;',
  21266. 'this.r = null;',
  21267. 'this.Obj = null;',
  21268. '']),
  21269. LinesToStr([ // $mod.$main
  21270. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21271. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21272. '$mod.r = $mod.THelper.Run;',
  21273. 'var $with = $mod.Obj;',
  21274. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21275. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21276. '$mod.r = $mod.THelper.Run;',
  21277. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21278. '$mod.r = $mod.THelper.Run;',
  21279. 'var $with1 = $mod.TObject;',
  21280. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21281. '$mod.r = $mod.THelper.Run;',
  21282. '']));
  21283. end;
  21284. procedure TTestModule.TestClassHelper_Constructor;
  21285. begin
  21286. StartProgram(false);
  21287. Add([
  21288. 'type',
  21289. ' TObject = class',
  21290. ' constructor Create;',
  21291. ' end;',
  21292. ' TClass = class of TObject;',
  21293. ' THelper = class helper for TObject',
  21294. ' constructor NewHlp(w: word);',
  21295. ' end;',
  21296. 'var',
  21297. ' obj: TObject;',
  21298. ' c: TClass;',
  21299. 'constructor TObject.Create;',
  21300. 'begin',
  21301. ' NewHlp(2);', // normal call
  21302. ' tobject.NewHlp(3);', // new instance
  21303. ' c.newhlp(4);', // new instance
  21304. 'end;',
  21305. 'constructor THelper.NewHlp(w: word);',
  21306. 'begin',
  21307. ' create;', // normal call
  21308. ' tobject.create;', // new instance
  21309. ' NewHlp(2);', // normal call
  21310. ' tobject.NewHlp(3);', // new instance
  21311. ' c.newhlp(4);', // new instance
  21312. 'end;',
  21313. 'begin',
  21314. ' obj.newhlp(2);', // normal call
  21315. ' with Obj do newhlp(12);', // normal call
  21316. ' tobject.newhlp(3);', // new instance
  21317. ' with tobject do newhlp(13);', // new instance
  21318. ' c.newhlp(4);', // new instance
  21319. ' with c do newhlp(14);', // new instance
  21320. '']);
  21321. ConvertProgram;
  21322. CheckSource('TestClassHelper_Constructor',
  21323. LinesToStr([ // statements
  21324. 'rtl.createClass(this, "TObject", null, function () {',
  21325. ' this.$init = function () {',
  21326. ' };',
  21327. ' this.$final = function () {',
  21328. ' };',
  21329. ' this.Create = function () {',
  21330. ' $mod.THelper.NewHlp.call(this, 2);',
  21331. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21332. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21333. ' return this;',
  21334. ' };',
  21335. '});',
  21336. 'rtl.createHelper(this, "THelper", null, function () {',
  21337. ' this.NewHlp = function (w) {',
  21338. ' this.Create();',
  21339. ' $mod.TObject.$create("Create");',
  21340. ' $mod.THelper.NewHlp.call(this, 2);',
  21341. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21342. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21343. ' return this;',
  21344. ' };',
  21345. '});',
  21346. 'this.obj = null;',
  21347. 'this.c = null;',
  21348. '']),
  21349. LinesToStr([ // $mod.$main
  21350. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21351. 'var $with = $mod.obj;',
  21352. '$mod.THelper.NewHlp.call($with, 12);',
  21353. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21354. 'var $with1 = $mod.TObject;',
  21355. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21356. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21357. 'var $with2 = $mod.c;',
  21358. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21359. '']));
  21360. end;
  21361. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21362. begin
  21363. StartProgram(false);
  21364. Add([
  21365. 'type',
  21366. ' TObject = class',
  21367. ' procedure Fly;',
  21368. ' end;',
  21369. ' TObjHelper = class helper for TObject',
  21370. ' procedure Fly;',
  21371. ' end;',
  21372. ' TBird = class',
  21373. ' procedure Fly;',
  21374. ' end;',
  21375. ' TBirdHelper = class helper for TBird',
  21376. ' procedure Fly;',
  21377. ' procedure Walk(w: word);',
  21378. ' end;',
  21379. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21380. ' procedure Fly;',
  21381. ' procedure Walk(w: word);',
  21382. ' end;',
  21383. 'procedure Tobject.fly;',
  21384. 'begin',
  21385. ' inherited;', // ignore
  21386. 'end;',
  21387. 'procedure Tobjhelper.fly;',
  21388. 'begin',
  21389. ' {@TObject_Fly}inherited;',
  21390. ' inherited {@TObject_Fly}Fly;',
  21391. 'end;',
  21392. 'procedure Tbird.fly;',
  21393. 'begin',
  21394. ' {@TObjHelper_Fly}inherited;',
  21395. ' inherited {@TObjHelper_Fly}Fly;',
  21396. 'end;',
  21397. 'procedure Tbirdhelper.fly;',
  21398. 'begin',
  21399. ' {@TBird_Fly}inherited;',
  21400. ' inherited {@TBird_Fly}Fly;',
  21401. 'end;',
  21402. 'procedure Tbirdhelper.walk(w: word);',
  21403. 'begin',
  21404. 'end;',
  21405. 'procedure teagleHelper.fly;',
  21406. 'begin',
  21407. ' {@TBird_Fly}inherited;',
  21408. ' inherited {@TBird_Fly}Fly;',
  21409. 'end;',
  21410. 'procedure teagleHelper.walk(w: word);',
  21411. 'begin',
  21412. ' {@TBirdHelper_Walk}inherited;',
  21413. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21414. 'end;',
  21415. 'begin',
  21416. '']);
  21417. ConvertProgram;
  21418. CheckSource('TestClassHelper_InheritedObjFPC',
  21419. LinesToStr([ // statements
  21420. 'rtl.createClass(this, "TObject", null, function () {',
  21421. ' this.$init = function () {',
  21422. ' };',
  21423. ' this.$final = function () {',
  21424. ' };',
  21425. ' this.Fly = function () {',
  21426. ' };',
  21427. '});',
  21428. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21429. ' this.Fly = function () {',
  21430. ' $mod.TObject.Fly.call(this);',
  21431. ' $mod.TObject.Fly.call(this);',
  21432. ' };',
  21433. '});',
  21434. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21435. ' this.Fly$1 = function () {',
  21436. ' $mod.TObjHelper.Fly.call(this);',
  21437. ' $mod.TObjHelper.Fly.call(this);',
  21438. ' };',
  21439. '});',
  21440. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21441. ' this.Fly = function () {',
  21442. ' $mod.TBird.Fly$1.call(this);',
  21443. ' $mod.TBird.Fly$1.call(this);',
  21444. ' };',
  21445. ' this.Walk = function (w) {',
  21446. ' };',
  21447. '});',
  21448. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21449. ' this.Fly$1 = function () {',
  21450. ' $mod.TBird.Fly$1.call(this);',
  21451. ' $mod.TBird.Fly$1.call(this);',
  21452. ' };',
  21453. ' this.Walk$1 = function (w) {',
  21454. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21455. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21456. ' };',
  21457. '});',
  21458. '']),
  21459. LinesToStr([ // $mod.$main
  21460. '']));
  21461. end;
  21462. procedure TTestModule.TestClassHelper_Property;
  21463. begin
  21464. StartProgram(false);
  21465. Add([
  21466. 'type',
  21467. ' TObject = class',
  21468. ' FSize: word;',
  21469. ' function GetSpeed: word;',
  21470. ' procedure SetSpeed(Value: word);',
  21471. ' end;',
  21472. ' TObjHelper = class helper for TObject',
  21473. ' function GetLeft: word;',
  21474. ' procedure SetLeft(Value: word);',
  21475. ' property Size: word read FSize write FSize;',
  21476. ' property Speed: word read GetSpeed write SetSpeed;',
  21477. ' property Left: word read GetLeft write SetLeft;',
  21478. ' end;',
  21479. ' TBird = class',
  21480. ' property NotRight: word read GetLeft write SetLeft;',
  21481. ' procedure DoIt;',
  21482. ' end;',
  21483. 'var',
  21484. ' b: TBird;',
  21485. 'function Tobject.GetSpeed: word;',
  21486. 'begin',
  21487. ' Size:=Size+11;',
  21488. ' Speed:=Speed+12;',
  21489. ' Result:=Left+13;',
  21490. ' Left:=13;',
  21491. ' Left:=Left+13;',
  21492. ' Self.Size:=Self.Size+21;',
  21493. ' Self.Speed:=Self.Speed+22;',
  21494. ' Self.Left:=Self.Left+23;',
  21495. ' with Self do begin',
  21496. ' Size:=Size+31;',
  21497. ' Speed:=Speed+32;',
  21498. ' Left:=Left+33;',
  21499. ' end;',
  21500. 'end;',
  21501. 'procedure Tobject.SetSpeed(Value: word);',
  21502. 'begin',
  21503. 'end;',
  21504. 'function TObjHelper.GetLeft: word;',
  21505. 'begin',
  21506. ' Size:=Size+11;',
  21507. ' Speed:=Speed+12;',
  21508. ' Left:=Left+13;',
  21509. ' Self.Size:=Self.Size+21;',
  21510. ' Self.Speed:=Self.Speed+22;',
  21511. ' Self.Left:=Self.Left+23;',
  21512. ' with Self do begin',
  21513. ' Size:=Size+31;',
  21514. ' Speed:=Speed+32;',
  21515. ' Left:=Left+33;',
  21516. ' end;',
  21517. 'end;',
  21518. 'procedure TObjHelper.SetLeft(Value: word);',
  21519. 'begin',
  21520. 'end;',
  21521. 'procedure TBird.DoIt;',
  21522. 'begin',
  21523. ' NotRight:=NotRight+11;',
  21524. ' Self.NotRight:=Self.NotRight+21;',
  21525. ' with Self do begin',
  21526. ' NotRight:=NotRight+31;',
  21527. ' end;',
  21528. 'end;',
  21529. 'begin',
  21530. ' b.Size:=b.Size+11;',
  21531. ' b.Speed:=b.Speed+12;',
  21532. ' b.Left:=b.Left+13;',
  21533. ' b.NotRight:=b.NotRight+14;',
  21534. ' with b do begin',
  21535. ' Size:=Size+31;',
  21536. ' Speed:=Speed+32;',
  21537. ' Left:=Left+33;',
  21538. ' NotRight:=NotRight+34;',
  21539. ' end;',
  21540. '']);
  21541. ConvertProgram;
  21542. CheckSource('TestClassHelper_Property',
  21543. LinesToStr([ // statements
  21544. 'rtl.createClass(this, "TObject", null, function () {',
  21545. ' this.$init = function () {',
  21546. ' this.FSize = 0;',
  21547. ' };',
  21548. ' this.$final = function () {',
  21549. ' };',
  21550. ' this.GetSpeed = function () {',
  21551. ' var Result = 0;',
  21552. ' this.FSize = this.FSize + 11;',
  21553. ' this.SetSpeed(this.GetSpeed() + 12);',
  21554. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21555. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21556. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21557. ' this.FSize = this.FSize + 21;',
  21558. ' this.SetSpeed(this.GetSpeed() + 22);',
  21559. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21560. ' this.FSize = this.FSize + 31;',
  21561. ' this.SetSpeed(this.GetSpeed() + 32);',
  21562. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21563. ' return Result;',
  21564. ' };',
  21565. ' this.SetSpeed = function (Value) {',
  21566. ' };',
  21567. '});',
  21568. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21569. ' this.GetLeft = function () {',
  21570. ' var Result = 0;',
  21571. ' this.FSize = this.FSize + 11;',
  21572. ' this.SetSpeed(this.GetSpeed() + 12);',
  21573. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21574. ' this.FSize = this.FSize + 21;',
  21575. ' this.SetSpeed(this.GetSpeed() + 22);',
  21576. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21577. ' this.FSize = this.FSize + 31;',
  21578. ' this.SetSpeed(this.GetSpeed() + 32);',
  21579. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21580. ' return Result;',
  21581. ' };',
  21582. ' this.SetLeft = function (Value) {',
  21583. ' };',
  21584. '});',
  21585. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21586. ' this.DoIt = function () {',
  21587. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21588. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21589. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21590. ' };',
  21591. '});',
  21592. 'this.b = null;',
  21593. '']),
  21594. LinesToStr([ // $mod.$main
  21595. '$mod.b.FSize = $mod.b.FSize + 11;',
  21596. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21597. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21598. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21599. 'var $with = $mod.b;',
  21600. '$with.FSize = $with.FSize + 31;',
  21601. '$with.SetSpeed($with.GetSpeed() + 32);',
  21602. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21603. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21604. '']));
  21605. end;
  21606. procedure TTestModule.TestClassHelper_Property_Array;
  21607. begin
  21608. StartProgram(false);
  21609. Add([
  21610. 'type',
  21611. ' TObject = class',
  21612. ' function GetSpeed(Index: boolean): word;',
  21613. ' procedure SetSpeed(Index: boolean; Value: word);',
  21614. ' end;',
  21615. ' TObjHelper = class helper for TObject',
  21616. ' function GetSize(Index: boolean): word;',
  21617. ' procedure SetSize(Index: boolean; Value: word);',
  21618. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21619. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21620. ' end;',
  21621. ' TBird = class',
  21622. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21623. ' procedure DoIt;',
  21624. ' end;',
  21625. 'var',
  21626. ' b: TBird;',
  21627. 'function Tobject.GetSpeed(Index: boolean): word;',
  21628. 'begin',
  21629. ' Result:=Size[false];',
  21630. ' Size[true]:=Size[false]+11;',
  21631. ' Speed[true]:=Speed[false]+12;',
  21632. ' Self.Size[true]:=Self.Size[false]+21;',
  21633. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21634. ' with Self do begin',
  21635. ' Size[true]:=Size[false]+31;',
  21636. ' Speed[true]:=Speed[false]+32;',
  21637. ' end;',
  21638. 'end;',
  21639. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21640. 'begin',
  21641. 'end;',
  21642. 'function TObjHelper.GetSize(Index: boolean): word;',
  21643. 'begin',
  21644. ' Size[true]:=Size[false]+11;',
  21645. ' Speed[true]:=Speed[false]+12;',
  21646. ' Self.Size[true]:=Self.Size[false]+21;',
  21647. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21648. ' with Self do begin',
  21649. ' Size[true]:=Size[false]+31;',
  21650. ' Speed[true]:=Speed[false]+32;',
  21651. ' end;',
  21652. 'end;',
  21653. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21654. 'begin',
  21655. 'end;',
  21656. 'procedure TBird.DoIt;',
  21657. 'begin',
  21658. ' Items[true]:=Items[false]+11;',
  21659. ' Self.Items[true]:=Self.Items[false]+21;',
  21660. ' with Self do Items[true]:=Items[false]+31;',
  21661. 'end;',
  21662. 'begin',
  21663. ' b.Size[true]:=b.Size[false]+11;',
  21664. ' b.Speed[true]:=b.Speed[false]+12;',
  21665. ' b.Items[true]:=b.Items[false]+13;',
  21666. ' with b do begin',
  21667. ' Size[true]:=Size[false]+21;',
  21668. ' Speed[true]:=Speed[false]+22;',
  21669. ' Items[true]:=Items[false]+23;',
  21670. ' end;',
  21671. '']);
  21672. ConvertProgram;
  21673. CheckSource('TestClassHelper_Property_Array',
  21674. LinesToStr([ // statements
  21675. 'rtl.createClass(this, "TObject", null, function () {',
  21676. ' this.$init = function () {',
  21677. ' };',
  21678. ' this.$final = function () {',
  21679. ' };',
  21680. ' this.GetSpeed = function (Index) {',
  21681. ' var Result = 0;',
  21682. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21683. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21684. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21685. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21686. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21687. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21688. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21689. ' return Result;',
  21690. ' };',
  21691. ' this.SetSpeed = function (Index, Value) {',
  21692. ' };',
  21693. '});',
  21694. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21695. ' this.GetSize = function (Index) {',
  21696. ' var Result = 0;',
  21697. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21698. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21699. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21700. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21701. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21702. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21703. ' return Result;',
  21704. ' };',
  21705. ' this.SetSize = function (Index, Value) {',
  21706. ' };',
  21707. '});',
  21708. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21709. ' this.DoIt = function () {',
  21710. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21711. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21712. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21713. ' };',
  21714. '});',
  21715. 'this.b = null;',
  21716. '']),
  21717. LinesToStr([ // $mod.$main
  21718. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21719. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21720. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21721. 'var $with = $mod.b;',
  21722. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21723. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21724. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21725. '']));
  21726. end;
  21727. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21728. begin
  21729. StartProgram(false);
  21730. Add([
  21731. 'type',
  21732. ' TObject = class',
  21733. ' function GetSpeed(Index: boolean): word;',
  21734. ' procedure SetSpeed(Index: boolean; Value: word);',
  21735. ' end;',
  21736. ' TObjHelper = class helper for TObject',
  21737. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21738. ' end;',
  21739. ' TBird = class',
  21740. ' end;',
  21741. ' TBirdHelper = class helper for TBird',
  21742. ' function GetSize(Index: word): boolean;',
  21743. ' procedure SetSize(Index: word; Value: boolean);',
  21744. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21745. ' end;',
  21746. 'function Tobject.GetSpeed(Index: boolean): word;',
  21747. 'begin',
  21748. ' Self[true]:=Self[false]+1;',
  21749. 'end;',
  21750. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21751. 'begin',
  21752. 'end;',
  21753. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21754. 'begin',
  21755. ' Self[1]:=not Self[2];',
  21756. 'end;',
  21757. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21758. 'begin',
  21759. 'end;',
  21760. 'var',
  21761. ' o: TObject;',
  21762. ' b: TBird;',
  21763. 'begin',
  21764. ' o[true]:=o[false]+1;',
  21765. ' b[3]:=not b[4];',
  21766. '']);
  21767. ConvertProgram;
  21768. CheckSource('TestClassHelper_Property_Array_Default',
  21769. LinesToStr([ // statements
  21770. 'rtl.createClass(this, "TObject", null, function () {',
  21771. ' this.$init = function () {',
  21772. ' };',
  21773. ' this.$final = function () {',
  21774. ' };',
  21775. ' this.GetSpeed = function (Index) {',
  21776. ' var Result = 0;',
  21777. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21778. ' return Result;',
  21779. ' };',
  21780. ' this.SetSpeed = function (Index, Value) {',
  21781. ' };',
  21782. '});',
  21783. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21784. '});',
  21785. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21786. '});',
  21787. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21788. ' this.GetSize = function (Index) {',
  21789. ' var Result = false;',
  21790. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21791. ' return Result;',
  21792. ' };',
  21793. ' this.SetSize = function (Index, Value) {',
  21794. ' };',
  21795. '});',
  21796. 'this.o = null;',
  21797. 'this.b = null;',
  21798. '']),
  21799. LinesToStr([ // $mod.$main
  21800. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21801. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21802. '']));
  21803. end;
  21804. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21805. begin
  21806. StartProgram(false);
  21807. Add([
  21808. 'type',
  21809. ' TObject = class',
  21810. ' end;',
  21811. ' TObjHelper = class helper for TObject',
  21812. ' function GetItems(Index: word): TObject;',
  21813. ' procedure SetItems(Index: word; Value: TObject);',
  21814. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21815. ' end;',
  21816. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21817. 'begin',
  21818. ' Self[1][2]:=Self[3][4];',
  21819. 'end;',
  21820. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21821. 'begin',
  21822. 'end;',
  21823. 'var',
  21824. ' o: TObject;',
  21825. 'begin',
  21826. ' o[1][2]:=o[3][4];',
  21827. '']);
  21828. ConvertProgram;
  21829. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21830. LinesToStr([ // statements
  21831. 'rtl.createClass(this, "TObject", null, function () {',
  21832. ' this.$init = function () {',
  21833. ' };',
  21834. ' this.$final = function () {',
  21835. ' };',
  21836. '});',
  21837. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21838. ' this.GetItems = function (Index) {',
  21839. ' var Result = null;',
  21840. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21841. ' return Result;',
  21842. ' };',
  21843. ' this.SetItems = function (Index, Value) {',
  21844. ' };',
  21845. '});',
  21846. 'this.o = null;',
  21847. '']),
  21848. LinesToStr([ // $mod.$main
  21849. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21850. '']));
  21851. end;
  21852. procedure TTestModule.TestClassHelper_ClassProperty;
  21853. begin
  21854. StartProgram(false);
  21855. Add([
  21856. 'type',
  21857. ' TObject = class',
  21858. ' class var FSize: word;',
  21859. ' class function GetSpeed: word;',
  21860. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21861. ' end;',
  21862. ' TObjHelper = class helper for TObject',
  21863. ' class function GetLeft: word;',
  21864. ' class procedure SetLeft(Value: word);',
  21865. ' class property Size: word read FSize write FSize;',
  21866. ' class property Speed: word read GetSpeed write SetSpeed;',
  21867. ' class property Left: word read GetLeft write SetLeft;',
  21868. ' end;',
  21869. ' TBird = class',
  21870. ' class property NotRight: word read GetLeft write SetLeft;',
  21871. ' class procedure DoIt;',
  21872. ' end;',
  21873. ' TBirdClass = class of TBird;',
  21874. 'class function Tobject.GetSpeed: word;',
  21875. 'begin',
  21876. ' Size:=Size+11;',
  21877. ' Speed:=Speed+12;',
  21878. ' Left:=Left+13;',
  21879. ' Self.Size:=Self.Size+21;',
  21880. ' Self.Speed:=Self.Speed+22;',
  21881. ' Self.Left:=Self.Left+23;',
  21882. ' with Self do begin',
  21883. ' Size:=Size+31;',
  21884. ' Speed:=Speed+32;',
  21885. ' Left:=Left+33;',
  21886. ' end;',
  21887. 'end;',
  21888. 'class function TObjHelper.GetLeft: word;',
  21889. 'begin',
  21890. ' Size:=Size+11;',
  21891. ' Speed:=Speed+12;',
  21892. ' Left:=Left+13;',
  21893. ' Self.Size:=Self.Size+21;',
  21894. ' Self.Speed:=Self.Speed+22;',
  21895. ' Self.Left:=Self.Left+23;',
  21896. ' with Self do begin',
  21897. ' Size:=Size+31;',
  21898. ' Speed:=Speed+32;',
  21899. ' Left:=Left+33;',
  21900. ' end;',
  21901. 'end;',
  21902. 'class procedure TObjHelper.SetLeft(Value: word);',
  21903. 'begin',
  21904. 'end;',
  21905. 'class procedure TBird.DoIt;',
  21906. 'begin',
  21907. ' NotRight:=NotRight+11;',
  21908. ' Self.NotRight:=Self.NotRight+21;',
  21909. ' with Self do NotRight:=NotRight+31;',
  21910. 'end;',
  21911. 'var',
  21912. ' b: TBird;',
  21913. ' c: TBirdClass;',
  21914. 'begin',
  21915. ' b.Size:=b.Size+11;',
  21916. ' b.Speed:=b.Speed+12;',
  21917. ' b.Left:=b.Left+13;',
  21918. ' b.NotRight:=b.NotRight+14;',
  21919. ' with b do begin',
  21920. ' Size:=Size+31;',
  21921. ' Speed:=Speed+32;',
  21922. ' Left:=Left+33;',
  21923. ' NotRight:=NotRight+34;',
  21924. ' end;',
  21925. ' c.Size:=c.Size+11;',
  21926. ' c.Speed:=c.Speed+12;',
  21927. ' c.Left:=c.Left+13;',
  21928. ' c.NotRight:=c.NotRight+14;',
  21929. ' with c do begin',
  21930. ' Size:=Size+31;',
  21931. ' Speed:=Speed+32;',
  21932. ' Left:=Left+33;',
  21933. ' NotRight:=NotRight+34;',
  21934. ' end;',
  21935. ' tbird.Size:=tbird.Size+11;',
  21936. ' tbird.Speed:=tbird.Speed+12;',
  21937. ' tbird.Left:=tbird.Left+13;',
  21938. ' tbird.NotRight:=tbird.NotRight+14;',
  21939. ' with tbird do begin',
  21940. ' Size:=Size+31;',
  21941. ' Speed:=Speed+32;',
  21942. ' Left:=Left+33;',
  21943. ' NotRight:=NotRight+34;',
  21944. ' end;',
  21945. '']);
  21946. ConvertProgram;
  21947. CheckSource('TestClassHelper_ClassProperty',
  21948. LinesToStr([ // statements
  21949. 'rtl.createClass(this, "TObject", null, function () {',
  21950. ' this.FSize = 0;',
  21951. ' this.$init = function () {',
  21952. ' };',
  21953. ' this.$final = function () {',
  21954. ' };',
  21955. ' this.GetSpeed = function () {',
  21956. ' var Result = 0;',
  21957. ' $mod.TObject.FSize = this.FSize + 11;',
  21958. ' this.SetSpeed(this.GetSpeed() + 12);',
  21959. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21960. ' $mod.TObject.FSize = this.FSize + 21;',
  21961. ' this.SetSpeed(this.GetSpeed() + 22);',
  21962. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21963. ' $mod.TObject.FSize = this.FSize + 31;',
  21964. ' this.SetSpeed(this.GetSpeed() + 32);',
  21965. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21966. ' return Result;',
  21967. ' };',
  21968. '});',
  21969. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21970. ' this.GetLeft = function () {',
  21971. ' var Result = 0;',
  21972. ' $mod.TObject.FSize = this.FSize + 11;',
  21973. ' this.SetSpeed(this.GetSpeed() + 12);',
  21974. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21975. ' $mod.TObject.FSize = this.FSize + 21;',
  21976. ' this.SetSpeed(this.GetSpeed() + 22);',
  21977. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21978. ' $mod.TObject.FSize = this.FSize + 31;',
  21979. ' this.SetSpeed(this.GetSpeed() + 32);',
  21980. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21981. ' return Result;',
  21982. ' };',
  21983. ' this.SetLeft = function (Value) {',
  21984. ' };',
  21985. '});',
  21986. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21987. ' this.DoIt = function () {',
  21988. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21989. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21990. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21991. ' };',
  21992. '});',
  21993. 'this.b = null;',
  21994. 'this.c = null;',
  21995. '']),
  21996. LinesToStr([ // $mod.$main
  21997. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21998. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21999. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  22000. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  22001. 'var $with = $mod.b;',
  22002. '$mod.TObject.FSize = $with.FSize + 31;',
  22003. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  22004. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  22005. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  22006. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  22007. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22008. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  22009. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  22010. 'var $with1 = $mod.c;',
  22011. '$mod.TObject.FSize = $with1.FSize + 31;',
  22012. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22013. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  22014. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  22015. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  22016. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22017. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  22018. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  22019. 'var $with2 = $mod.TBird;',
  22020. '$mod.TObject.FSize = $with2.FSize + 31;',
  22021. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22022. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  22023. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  22024. '']));
  22025. end;
  22026. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22027. begin
  22028. StartProgram(false);
  22029. Add([
  22030. 'type',
  22031. ' TObject = class',
  22032. ' class function GetSpeed: word; static;',
  22033. ' class procedure SetSpeed(Value: word); static;',
  22034. ' end;',
  22035. ' TObjHelper = class helper for TObject',
  22036. ' class function GetLeft: word; static;',
  22037. ' class procedure SetLeft(Value: word); static;',
  22038. ' class property Speed: word read GetSpeed write SetSpeed;',
  22039. ' class property Left: word read GetLeft write SetLeft;',
  22040. ' end;',
  22041. ' TBird = class',
  22042. ' class property NotRight: word read GetLeft write SetLeft;',
  22043. ' class procedure DoIt; static;',
  22044. ' class procedure DoSome;',
  22045. ' end;',
  22046. ' TBirdClass = class of TBird;',
  22047. 'class function Tobject.GetSpeed: word;',
  22048. 'begin',
  22049. ' Speed:=Speed+12;',
  22050. ' Left:=Left+13;',
  22051. 'end;',
  22052. 'class procedure TObject.SetSpeed(Value: word);',
  22053. 'begin',
  22054. 'end;',
  22055. 'class function TObjHelper.GetLeft: word;',
  22056. 'begin',
  22057. ' Speed:=Speed+12;',
  22058. ' Left:=Left+13;',
  22059. 'end;',
  22060. 'class procedure TObjHelper.SetLeft(Value: word);',
  22061. 'begin',
  22062. 'end;',
  22063. 'class procedure TBird.DoIt;',
  22064. 'begin',
  22065. ' NotRight:=NotRight+11;',
  22066. 'end;',
  22067. 'class procedure TBird.DoSome;',
  22068. 'begin',
  22069. ' Speed:=Speed+12;',
  22070. ' Left:=Left+13;',
  22071. ' Self.Speed:=Self.Speed+22;',
  22072. ' Self.Left:=Self.Left+23;',
  22073. ' with Self do begin',
  22074. ' Speed:=Speed+32;',
  22075. ' Left:=Left+33;',
  22076. ' end;',
  22077. ' NotRight:=NotRight+11;',
  22078. ' Self.NotRight:=Self.NotRight+21;',
  22079. ' with Self do NotRight:=NotRight+31;',
  22080. 'end;',
  22081. 'var',
  22082. ' b: TBird;',
  22083. ' c: TBirdClass;',
  22084. 'begin',
  22085. ' b.Speed:=b.Speed+12;',
  22086. ' b.Left:=b.Left+13;',
  22087. ' b.NotRight:=b.NotRight+14;',
  22088. ' with b do begin',
  22089. ' Speed:=Speed+32;',
  22090. ' Left:=Left+33;',
  22091. ' NotRight:=NotRight+34;',
  22092. ' end;',
  22093. ' c.Speed:=c.Speed+12;',
  22094. ' c.Left:=c.Left+13;',
  22095. ' c.NotRight:=c.NotRight+14;',
  22096. ' with c do begin',
  22097. ' Speed:=Speed+32;',
  22098. ' Left:=Left+33;',
  22099. ' NotRight:=NotRight+34;',
  22100. ' end;',
  22101. ' tbird.Speed:=tbird.Speed+12;',
  22102. ' tbird.Left:=tbird.Left+13;',
  22103. ' tbird.NotRight:=tbird.NotRight+14;',
  22104. ' with tbird do begin',
  22105. ' Speed:=Speed+32;',
  22106. ' Left:=Left+33;',
  22107. ' NotRight:=NotRight+34;',
  22108. ' end;',
  22109. '']);
  22110. ConvertProgram;
  22111. CheckSource('TestClassHelper_ClassPropertyStatic',
  22112. LinesToStr([ // statements
  22113. 'rtl.createClass(this, "TObject", null, function () {',
  22114. ' this.$init = function () {',
  22115. ' };',
  22116. ' this.$final = function () {',
  22117. ' };',
  22118. ' this.GetSpeed = function () {',
  22119. ' var Result = 0;',
  22120. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22121. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22122. ' return Result;',
  22123. ' };',
  22124. ' this.SetSpeed = function (Value) {',
  22125. ' };',
  22126. '});',
  22127. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22128. ' this.GetLeft = function () {',
  22129. ' var Result = 0;',
  22130. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22131. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22132. ' return Result;',
  22133. ' };',
  22134. ' this.SetLeft = function (Value) {',
  22135. ' };',
  22136. '});',
  22137. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22138. ' this.DoIt = function () {',
  22139. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22140. ' };',
  22141. ' this.DoSome = function () {',
  22142. ' this.SetSpeed(this.GetSpeed() + 12);',
  22143. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22144. ' this.SetSpeed(this.GetSpeed() + 22);',
  22145. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22146. ' this.SetSpeed(this.GetSpeed() + 32);',
  22147. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22148. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22149. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22150. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22151. ' };',
  22152. '});',
  22153. 'this.b = null;',
  22154. 'this.c = null;',
  22155. '']),
  22156. LinesToStr([ // $mod.$main
  22157. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22158. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22159. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22160. 'var $with = $mod.b;',
  22161. '$with.SetSpeed($with.GetSpeed() + 32);',
  22162. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22163. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22164. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22165. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22166. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22167. 'var $with1 = $mod.c;',
  22168. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22169. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22170. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22171. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22172. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22173. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22174. 'var $with2 = $mod.TBird;',
  22175. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22176. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22177. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22178. '']));
  22179. end;
  22180. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22181. begin
  22182. StartProgram(false);
  22183. Add([
  22184. 'type',
  22185. ' TObject = class',
  22186. ' class function GetSpeed(Index: boolean): word;',
  22187. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22188. ' end;',
  22189. ' TObjHelper = class helper for TObject',
  22190. ' class function GetSize(Index: boolean): word;',
  22191. ' class procedure SetSize(Index: boolean; Value: word);',
  22192. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22193. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22194. ' end;',
  22195. ' TBird = class',
  22196. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22197. ' class procedure DoIt;',
  22198. ' end;',
  22199. ' TBirdClass = class of TBird;',
  22200. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22201. 'begin',
  22202. ' Size[true]:=Size[false]+11;',
  22203. ' Speed[true]:=Speed[false]+12;',
  22204. ' Self.Size[true]:=Self.Size[false]+21;',
  22205. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22206. ' with Self do begin',
  22207. ' Size[true]:=Size[false]+31;',
  22208. ' Speed[true]:=Speed[false]+32;',
  22209. ' end;',
  22210. 'end;',
  22211. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22212. 'begin',
  22213. ' Size[true]:=Size[false]+11;',
  22214. ' Speed[true]:=Speed[false]+12;',
  22215. ' Self.Size[true]:=Self.Size[false]+21;',
  22216. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22217. ' with Self do begin',
  22218. ' Size[true]:=Size[false]+31;',
  22219. ' Speed[true]:=Speed[false]+32;',
  22220. ' end;',
  22221. 'end;',
  22222. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22223. 'begin',
  22224. 'end;',
  22225. 'class procedure TBird.DoIt;',
  22226. 'begin',
  22227. ' Items[true]:=Items[false]+11;',
  22228. ' Self.Items[true]:=Self.Items[false]+21;',
  22229. ' with Self do Items[true]:=Items[false]+31;',
  22230. 'end;',
  22231. 'var',
  22232. ' b: TBird;',
  22233. ' c: TBirdClass;',
  22234. 'begin',
  22235. ' b.Size[true]:=b.Size[false]+11;',
  22236. ' b.Speed[true]:=b.Speed[false]+12;',
  22237. ' b.Items[true]:=b.Items[false]+13;',
  22238. ' with b do begin',
  22239. ' Size[true]:=Size[false]+21;',
  22240. ' Speed[true]:=Speed[false]+22;',
  22241. ' Items[true]:=Items[false]+23;',
  22242. ' end;',
  22243. ' c.Size[true]:=c.Size[false]+11;',
  22244. ' c.Speed[true]:=c.Speed[false]+12;',
  22245. ' c.Items[true]:=c.Items[false]+13;',
  22246. ' with c do begin',
  22247. ' Size[true]:=Size[false]+21;',
  22248. ' Speed[true]:=Speed[false]+22;',
  22249. ' Items[true]:=Items[false]+23;',
  22250. ' end;',
  22251. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22252. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22253. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22254. ' with TBird do begin',
  22255. ' Size[true]:=Size[false]+21;',
  22256. ' Speed[true]:=Speed[false]+22;',
  22257. ' Items[true]:=Items[false]+23;',
  22258. ' end;',
  22259. '']);
  22260. ConvertProgram;
  22261. CheckSource('TestClassHelper_ClassProperty_Array',
  22262. LinesToStr([ // statements
  22263. 'rtl.createClass(this, "TObject", null, function () {',
  22264. ' this.$init = function () {',
  22265. ' };',
  22266. ' this.$final = function () {',
  22267. ' };',
  22268. ' this.GetSpeed = function (Index) {',
  22269. ' var Result = 0;',
  22270. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22271. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22272. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22273. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22274. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22275. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22276. ' return Result;',
  22277. ' };',
  22278. '});',
  22279. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22280. ' this.GetSize = function (Index) {',
  22281. ' var Result = 0;',
  22282. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22283. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22284. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22285. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22286. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22287. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22288. ' return Result;',
  22289. ' };',
  22290. ' this.SetSize = function (Index, Value) {',
  22291. ' };',
  22292. '});',
  22293. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22294. ' this.DoIt = function () {',
  22295. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22296. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22297. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22298. ' };',
  22299. '});',
  22300. 'this.b = null;',
  22301. 'this.c = null;',
  22302. '']),
  22303. LinesToStr([ // $mod.$main
  22304. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22305. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22306. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22307. 'var $with = $mod.b;',
  22308. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22309. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22310. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22311. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22312. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22313. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22314. 'var $with1 = $mod.c;',
  22315. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22316. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22317. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22318. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22319. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22320. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22321. 'var $with2 = $mod.TBird;',
  22322. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22323. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22324. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22325. '']));
  22326. end;
  22327. procedure TTestModule.TestClassHelper_ForIn;
  22328. begin
  22329. StartProgram(false);
  22330. Add([
  22331. 'type',
  22332. ' TObject = class end;',
  22333. ' TItem = TObject;',
  22334. ' TEnumerator = class',
  22335. ' FCurrent: TItem;',
  22336. ' property Current: TItem read FCurrent;',
  22337. ' function MoveNext: boolean;',
  22338. ' end;',
  22339. ' TBird = class',
  22340. ' end;',
  22341. ' TBirdHelper = class helper for TBird',
  22342. ' function GetEnumerator: TEnumerator;',
  22343. ' end;',
  22344. 'function TEnumerator.MoveNext: boolean;',
  22345. 'begin',
  22346. 'end;',
  22347. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22348. 'begin',
  22349. 'end;',
  22350. 'var',
  22351. ' b: TBird;',
  22352. ' i, i2: TItem;',
  22353. 'begin',
  22354. ' for i in b do i2:=i;']);
  22355. ConvertProgram;
  22356. CheckSource('TestClassHelper_ForIn',
  22357. LinesToStr([ // statements
  22358. 'rtl.createClass(this, "TObject", null, function () {',
  22359. ' this.$init = function () {',
  22360. ' };',
  22361. ' this.$final = function () {',
  22362. ' };',
  22363. '});',
  22364. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22365. ' this.$init = function () {',
  22366. ' $mod.TObject.$init.call(this);',
  22367. ' this.FCurrent = null;',
  22368. ' };',
  22369. ' this.$final = function () {',
  22370. ' this.FCurrent = undefined;',
  22371. ' $mod.TObject.$final.call(this);',
  22372. ' };',
  22373. ' this.MoveNext = function () {',
  22374. ' var Result = false;',
  22375. ' return Result;',
  22376. ' };',
  22377. '});',
  22378. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22379. '});',
  22380. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22381. ' this.GetEnumerator = function () {',
  22382. ' var Result = null;',
  22383. ' return Result;',
  22384. ' };',
  22385. '});',
  22386. 'this.b = null;',
  22387. 'this.i = null;',
  22388. 'this.i2 = null;'
  22389. ]),
  22390. LinesToStr([ // $mod.$main
  22391. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22392. 'try {',
  22393. ' while ($in.MoveNext()){',
  22394. ' $mod.i = $in.FCurrent;',
  22395. ' $mod.i2 = $mod.i;',
  22396. ' }',
  22397. '} finally {',
  22398. ' $in = rtl.freeLoc($in)',
  22399. '};',
  22400. '']));
  22401. end;
  22402. procedure TTestModule.TestClassHelper_PassProperty;
  22403. begin
  22404. StartProgram(false);
  22405. Add([
  22406. 'type',
  22407. ' TObject = class',
  22408. ' FField: TObject;',
  22409. ' property Field: TObject read FField write FField;',
  22410. ' end;',
  22411. ' THelper = class helper for TObject',
  22412. ' procedure Fly;',
  22413. ' class procedure Run;',
  22414. ' class procedure Jump; static;',
  22415. ' end;',
  22416. 'procedure THelper.Fly;',
  22417. 'begin',
  22418. ' Field.Fly;',
  22419. ' Field.Run;',
  22420. ' Field.Jump;',
  22421. ' with Field do begin',
  22422. ' Fly;',
  22423. ' Run;',
  22424. ' Jump;',
  22425. ' end;',
  22426. 'end;',
  22427. 'class procedure THelper.Run;',
  22428. 'begin',
  22429. 'end;',
  22430. 'class procedure THelper.Jump;',
  22431. 'begin',
  22432. 'end;',
  22433. 'var',
  22434. ' b: TObject;',
  22435. 'begin',
  22436. ' b.Field.Fly;',
  22437. ' b.Field.Run;',
  22438. ' b.Field.Jump;',
  22439. ' with b do begin',
  22440. ' Field.Run;',
  22441. ' Field.Fly;',
  22442. ' Field.Jump;',
  22443. ' end;',
  22444. ' with b.Field do begin',
  22445. ' Run;',
  22446. ' Fly;',
  22447. ' Jump;',
  22448. ' end;',
  22449. '']);
  22450. ConvertProgram;
  22451. CheckSource('TestClassHelper_PassProperty',
  22452. LinesToStr([ // statements
  22453. 'rtl.createClass(this, "TObject", null, function () {',
  22454. ' this.$init = function () {',
  22455. ' this.FField = null;',
  22456. ' };',
  22457. ' this.$final = function () {',
  22458. ' this.FField = undefined;',
  22459. ' };',
  22460. '});',
  22461. 'rtl.createHelper(this, "THelper", null, function () {',
  22462. ' this.Fly = function () {',
  22463. ' $mod.THelper.Fly.call(this.FField);',
  22464. ' $mod.THelper.Run.call(this.FField.$class);',
  22465. ' $mod.THelper.Jump();',
  22466. ' var $with = this.FField;',
  22467. ' $mod.THelper.Fly.call($with);',
  22468. ' $mod.THelper.Run.call($with.$class);',
  22469. ' $mod.THelper.Jump();',
  22470. ' };',
  22471. ' this.Run = function () {',
  22472. ' };',
  22473. ' this.Jump = function () {',
  22474. ' };',
  22475. '});',
  22476. 'this.b = null;',
  22477. '']),
  22478. LinesToStr([ // $mod.$main
  22479. '$mod.THelper.Fly.call($mod.b.FField);',
  22480. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22481. '$mod.THelper.Jump();',
  22482. 'var $with = $mod.b;',
  22483. '$mod.THelper.Run.call($with.FField.$class);',
  22484. '$mod.THelper.Fly.call($with.FField);',
  22485. '$mod.THelper.Jump();',
  22486. 'var $with1 = $mod.b.FField;',
  22487. '$mod.THelper.Run.call($with1.$class);',
  22488. '$mod.THelper.Fly.call($with1);',
  22489. '$mod.THelper.Jump();',
  22490. '']));
  22491. end;
  22492. procedure TTestModule.TestExtClassHelper_ClassVar;
  22493. begin
  22494. StartProgram(false);
  22495. Add([
  22496. '{$modeswitch externalclass}',
  22497. 'type',
  22498. ' TExtA = class external name ''ExtObj''',
  22499. ' end;',
  22500. ' THelper = class helper for TExtA',
  22501. ' const',
  22502. ' One = 1;',
  22503. ' Two: word = 2;',
  22504. ' class var',
  22505. ' Glob: word;',
  22506. ' function Foo(w: word): word;',
  22507. ' class function Bar(w: word): word; static;',
  22508. ' end;',
  22509. 'function THelper.foo(w: word): word;',
  22510. 'begin',
  22511. ' Result:=w;',
  22512. ' Two:=One+w;',
  22513. ' Glob:=Glob;',
  22514. ' Result:=Self.Glob;',
  22515. ' Self.Glob:=Self.Glob;',
  22516. ' with Self do Glob:=Glob;',
  22517. 'end;',
  22518. 'class function THelper.bar(w: word): word;',
  22519. 'begin',
  22520. ' Result:=w;',
  22521. ' Two:=One;',
  22522. ' Glob:=Glob;',
  22523. 'end;',
  22524. 'var o: TExtA;',
  22525. 'begin',
  22526. ' texta.two:=texta.one;',
  22527. ' texta.Glob:=texta.Glob;',
  22528. ' with texta do begin',
  22529. ' two:=one;',
  22530. ' Glob:=Glob;',
  22531. ' end;',
  22532. ' o.two:=o.one;',
  22533. ' o.Glob:=o.Glob;',
  22534. ' with o do begin',
  22535. ' two:=one;',
  22536. ' Glob:=Glob;',
  22537. ' end;',
  22538. '']);
  22539. ConvertProgram;
  22540. CheckSource('TestExtClassHelper_ClassVar',
  22541. LinesToStr([ // statements
  22542. 'rtl.createHelper(this, "THelper", null, function () {',
  22543. ' this.One = 1;',
  22544. ' this.Two = 2;',
  22545. ' this.Glob = 0;',
  22546. ' this.Foo = function (w) {',
  22547. ' var Result = 0;',
  22548. ' Result = w;',
  22549. ' $mod.THelper.Two = 1 + w;',
  22550. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22551. ' Result = $mod.THelper.Glob;',
  22552. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22553. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22554. ' return Result;',
  22555. ' };',
  22556. ' this.Bar = function (w) {',
  22557. ' var Result = 0;',
  22558. ' Result = w;',
  22559. ' $mod.THelper.Two = 1;',
  22560. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22561. ' return Result;',
  22562. ' };',
  22563. '});',
  22564. 'this.o = null;',
  22565. '']),
  22566. LinesToStr([ // $mod.$main
  22567. '$mod.THelper.Two = 1;',
  22568. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22569. '$mod.THelper.Two = 1;',
  22570. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22571. '$mod.THelper.Two = 1;',
  22572. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22573. 'var $with = $mod.o;',
  22574. '$mod.THelper.Two = 1;',
  22575. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22576. '']));
  22577. end;
  22578. procedure TTestModule.TestExtClassHelper_Method_Call;
  22579. begin
  22580. StartProgram(false);
  22581. Add([
  22582. '{$modeswitch externalclass}',
  22583. 'type',
  22584. ' TFly = function(w: word): word of object;',
  22585. ' TExtA = class external name ''ExtObj''',
  22586. ' procedure Run(w: word = 10);',
  22587. ' end;',
  22588. ' THelper = class helper for TExtA',
  22589. ' function Foo(w: word = 1): word;',
  22590. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22591. ' end;',
  22592. 'var p: TFly;',
  22593. 'function THelper.foo(w: word): word;',
  22594. 'begin',
  22595. ' Run;',
  22596. ' Run();',
  22597. ' Run(11);',
  22598. ' Foo;',
  22599. ' Foo();',
  22600. ' Foo(12);',
  22601. ' Self.Foo;',
  22602. ' Self.Foo();',
  22603. ' Self.Foo(13);',
  22604. ' Fly;',
  22605. ' Fly();',
  22606. ' with Self do begin',
  22607. ' Foo;',
  22608. ' Foo();',
  22609. ' Foo(14);',
  22610. ' Fly;',
  22611. ' Fly();',
  22612. ' end;',
  22613. ' p:=@Fly;',
  22614. 'end;',
  22615. 'var Obj: TExtA;',
  22616. 'begin',
  22617. ' obj.Foo;',
  22618. ' obj.Foo();',
  22619. ' obj.Foo(21);',
  22620. ' obj.Fly;',
  22621. ' obj.Fly();',
  22622. ' with obj do begin',
  22623. ' Foo;',
  22624. ' Foo();',
  22625. ' Foo(22);',
  22626. ' Fly;',
  22627. ' Fly();',
  22628. ' end;',
  22629. ' p:[email protected];',
  22630. '']);
  22631. ConvertProgram;
  22632. CheckSource('TestExtClassHelper_Method_Call',
  22633. LinesToStr([ // statements
  22634. 'rtl.createHelper(this, "THelper", null, function () {',
  22635. ' this.Foo = function (w) {',
  22636. ' var Result = 0;',
  22637. ' this.Run(10);',
  22638. ' this.Run(10);',
  22639. ' this.Run(11);',
  22640. ' $mod.THelper.Foo.call(this, 1);',
  22641. ' $mod.THelper.Foo.call(this, 1);',
  22642. ' $mod.THelper.Foo.call(this, 12);',
  22643. ' $mod.THelper.Foo.call(this, 1);',
  22644. ' $mod.THelper.Foo.call(this, 1);',
  22645. ' $mod.THelper.Foo.call(this, 13);',
  22646. ' this.Fly(2);',
  22647. ' this.Fly(2);',
  22648. ' $mod.THelper.Foo.call(this, 1);',
  22649. ' $mod.THelper.Foo.call(this, 1);',
  22650. ' $mod.THelper.Foo.call(this, 14);',
  22651. ' this.Fly(2);',
  22652. ' this.Fly(2);',
  22653. ' $mod.p = rtl.createCallback(this, "Fly");',
  22654. ' return Result;',
  22655. ' };',
  22656. '});',
  22657. 'this.p = null;',
  22658. 'this.Obj = null;',
  22659. '']),
  22660. LinesToStr([ // $mod.$main
  22661. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22662. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22663. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22664. '$mod.Obj.Fly(2);',
  22665. '$mod.Obj.Fly(2);',
  22666. 'var $with = $mod.Obj;',
  22667. '$mod.THelper.Foo.call($with, 1);',
  22668. '$mod.THelper.Foo.call($with, 1);',
  22669. '$mod.THelper.Foo.call($with, 22);',
  22670. '$with.Fly(2);',
  22671. '$with.Fly(2);',
  22672. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22673. '']));
  22674. end;
  22675. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22676. begin
  22677. StartProgram(false);
  22678. Add([
  22679. '{$modeswitch externalclass}',
  22680. 'type',
  22681. ' TExtA = class external name ''ExtObj''',
  22682. ' procedure Run(w: word = 10);',
  22683. ' end;',
  22684. ' THelper = class helper for TExtA',
  22685. ' class procedure Fly;',
  22686. ' end;',
  22687. 'class procedure THelper.Fly;',
  22688. 'begin end;',
  22689. 'begin',
  22690. '']);
  22691. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22692. nHelperClassMethodForExtClassMustBeStatic);
  22693. ConvertProgram;
  22694. end;
  22695. procedure TTestModule.TestRecordHelper_ClassVar;
  22696. begin
  22697. StartProgram(false);
  22698. Add([
  22699. 'type',
  22700. ' TRec = record',
  22701. ' end;',
  22702. ' THelper = record helper for TRec',
  22703. ' const',
  22704. ' One = 1;',
  22705. ' Two: word = 2;',
  22706. ' class var',
  22707. ' Glob: word;',
  22708. ' function Foo(w: word): word;',
  22709. ' class function Bar(w: word): word; static;',
  22710. ' end;',
  22711. 'function THelper.foo(w: word): word;',
  22712. 'begin',
  22713. ' Result:=w;',
  22714. ' Two:=One+w;',
  22715. ' Glob:=Glob;',
  22716. ' Result:=Self.Glob;',
  22717. ' Self.Glob:=Self.Glob;',
  22718. ' with Self do Glob:=Glob;',
  22719. ' Self:=Self;',
  22720. 'end;',
  22721. 'class function THelper.bar(w: word): word;',
  22722. 'begin',
  22723. ' Result:=w;',
  22724. ' Two:=One;',
  22725. ' Glob:=Glob;',
  22726. 'end;',
  22727. 'var r: TRec;',
  22728. 'begin',
  22729. ' trec.two:=trec.one;',
  22730. ' trec.Glob:=trec.Glob;',
  22731. ' with trec do begin',
  22732. ' two:=one;',
  22733. ' Glob:=Glob;',
  22734. ' end;',
  22735. ' r.two:=r.one;',
  22736. ' r.Glob:=r.Glob;',
  22737. ' with r do begin',
  22738. ' two:=one;',
  22739. ' Glob:=Glob;',
  22740. ' end;',
  22741. '']);
  22742. ConvertProgram;
  22743. CheckSource('TestRecordHelper_ClassVar',
  22744. LinesToStr([ // statements
  22745. 'rtl.recNewT(this, "TRec", function () {',
  22746. ' this.$eq = function (b) {',
  22747. ' return true;',
  22748. ' };',
  22749. ' this.$assign = function (s) {',
  22750. ' return this;',
  22751. ' };',
  22752. '});',
  22753. 'rtl.createHelper(this, "THelper", null, function () {',
  22754. ' this.One = 1;',
  22755. ' this.Two = 2;',
  22756. ' this.Glob = 0;',
  22757. ' this.Foo = function (w) {',
  22758. ' var Result = 0;',
  22759. ' Result = w;',
  22760. ' $mod.THelper.Two = 1 + w;',
  22761. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22762. ' Result = $mod.THelper.Glob;',
  22763. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22764. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22765. ' this.$assign(this);',
  22766. ' return Result;',
  22767. ' };',
  22768. ' this.Bar = function (w) {',
  22769. ' var Result = 0;',
  22770. ' Result = w;',
  22771. ' $mod.THelper.Two = 1;',
  22772. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22773. ' return Result;',
  22774. ' };',
  22775. '});',
  22776. 'this.r = this.TRec.$new();',
  22777. '']),
  22778. LinesToStr([ // $mod.$main
  22779. '$mod.THelper.Two = 1;',
  22780. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22781. 'var $with = $mod.TRec;',
  22782. '$mod.THelper.Two = 1;',
  22783. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22784. '$mod.THelper.Two = 1;',
  22785. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22786. 'var $with1 = $mod.r;',
  22787. '$mod.THelper.Two = 1;',
  22788. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22789. '']));
  22790. end;
  22791. procedure TTestModule.TestRecordHelper_Method_Call;
  22792. begin
  22793. StartProgram(false);
  22794. Add([
  22795. '{$modeswitch AdvancedRecords}',
  22796. 'type',
  22797. ' TRec = record',
  22798. ' procedure Run(w: word = 10);',
  22799. ' end;',
  22800. ' THelper = record helper for TRec',
  22801. ' function Foo(w: word = 1): word;',
  22802. ' end;',
  22803. 'procedure TRec.Run(w: word);',
  22804. 'begin',
  22805. ' Foo;',
  22806. ' Foo();',
  22807. ' Foo(2);',
  22808. ' Self.Foo;',
  22809. ' Self.Foo();',
  22810. ' Self.Foo(3);',
  22811. ' with Self do begin',
  22812. ' Foo;',
  22813. ' Foo();',
  22814. ' Foo(4);',
  22815. ' end;',
  22816. 'end;',
  22817. 'function THelper.foo(w: word): word;',
  22818. 'begin',
  22819. ' Run;',
  22820. ' Run();',
  22821. ' Run(11);',
  22822. ' Foo;',
  22823. ' Foo();',
  22824. ' Foo(12);',
  22825. ' Self.Foo;',
  22826. ' Self.Foo();',
  22827. ' Self.Foo(13);',
  22828. ' with Self do begin',
  22829. ' Foo;',
  22830. ' Foo();',
  22831. ' Foo(14);',
  22832. ' end;',
  22833. 'end;',
  22834. 'var Rec: TRec;',
  22835. 'begin',
  22836. ' Rec.Foo;',
  22837. ' Rec.Foo();',
  22838. ' Rec.Foo(21);',
  22839. ' with Rec do begin',
  22840. ' Foo;',
  22841. ' Foo();',
  22842. ' Foo(22);',
  22843. ' end;',
  22844. '']);
  22845. ConvertProgram;
  22846. CheckSource('TestRecordHelper_Method_Call',
  22847. LinesToStr([ // statements
  22848. 'rtl.recNewT(this, "TRec", function () {',
  22849. ' this.$eq = function (b) {',
  22850. ' return true;',
  22851. ' };',
  22852. ' this.$assign = function (s) {',
  22853. ' return this;',
  22854. ' };',
  22855. ' this.Run = function (w) {',
  22856. ' $mod.THelper.Foo.call(this, 1);',
  22857. ' $mod.THelper.Foo.call(this, 1);',
  22858. ' $mod.THelper.Foo.call(this, 2);',
  22859. ' $mod.THelper.Foo.call(this, 1);',
  22860. ' $mod.THelper.Foo.call(this, 1);',
  22861. ' $mod.THelper.Foo.call(this, 3);',
  22862. ' $mod.THelper.Foo.call(this, 1);',
  22863. ' $mod.THelper.Foo.call(this, 1);',
  22864. ' $mod.THelper.Foo.call(this, 4);',
  22865. ' };',
  22866. '});',
  22867. 'rtl.createHelper(this, "THelper", null, function () {',
  22868. ' this.Foo = function (w) {',
  22869. ' var Result = 0;',
  22870. ' this.Run(10);',
  22871. ' this.Run(10);',
  22872. ' this.Run(11);',
  22873. ' $mod.THelper.Foo.call(this, 1);',
  22874. ' $mod.THelper.Foo.call(this, 1);',
  22875. ' $mod.THelper.Foo.call(this, 12);',
  22876. ' $mod.THelper.Foo.call(this, 1);',
  22877. ' $mod.THelper.Foo.call(this, 1);',
  22878. ' $mod.THelper.Foo.call(this, 13);',
  22879. ' $mod.THelper.Foo.call(this, 1);',
  22880. ' $mod.THelper.Foo.call(this, 1);',
  22881. ' $mod.THelper.Foo.call(this, 14);',
  22882. ' return Result;',
  22883. ' };',
  22884. '});',
  22885. 'this.Rec = this.TRec.$new();',
  22886. '']),
  22887. LinesToStr([ // $mod.$main
  22888. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22889. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22890. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22891. 'var $with = $mod.Rec;',
  22892. '$mod.THelper.Foo.call($with, 1);',
  22893. '$mod.THelper.Foo.call($with, 1);',
  22894. '$mod.THelper.Foo.call($with, 22);',
  22895. '']));
  22896. end;
  22897. procedure TTestModule.TestRecordHelper_Constructor;
  22898. begin
  22899. StartProgram(false);
  22900. Add([
  22901. '{$modeswitch AdvancedRecords}',
  22902. 'type',
  22903. ' TRec = record',
  22904. ' constructor Create(w: word);',
  22905. ' end;',
  22906. ' THelper = record helper for TRec',
  22907. ' constructor NewHlp(w: word);',
  22908. ' end;',
  22909. 'var',
  22910. ' Rec: TRec;',
  22911. 'constructor TRec.Create(w: word);',
  22912. 'begin',
  22913. ' NewHlp(2);', // normal call
  22914. ' trec.NewHlp(3);', // new instance
  22915. 'end;',
  22916. 'constructor THelper.NewHlp(w: word);',
  22917. 'begin',
  22918. ' create(2);', // normal call
  22919. ' trec.create(3);', // new instance
  22920. ' NewHlp(4);', // normal call
  22921. ' trec.NewHlp(5);', // new instance
  22922. 'end;',
  22923. 'begin',
  22924. ' rec.newhlp(2);', // normal call
  22925. ' with rec do newhlp(12);', // normal call
  22926. ' trec.newhlp(3);', // new instance
  22927. ' with trec do newhlp(13);', // new instance
  22928. '']);
  22929. ConvertProgram;
  22930. CheckSource('TestRecordHelper_Constructor',
  22931. LinesToStr([ // statements
  22932. 'rtl.recNewT(this, "TRec", function () {',
  22933. ' this.$eq = function (b) {',
  22934. ' return true;',
  22935. ' };',
  22936. ' this.$assign = function (s) {',
  22937. ' return this;',
  22938. ' };',
  22939. ' this.Create = function (w) {',
  22940. ' $mod.THelper.NewHlp.call(this, 2);',
  22941. ' $mod.THelper.$new("NewHlp", [3]);',
  22942. ' return this;',
  22943. ' };',
  22944. '});',
  22945. 'rtl.createHelper(this, "THelper", null, function () {',
  22946. ' this.NewHlp = function (w) {',
  22947. ' this.Create(2);',
  22948. ' $mod.TRec.$new().Create(3);',
  22949. ' $mod.THelper.NewHlp.call(this, 4);',
  22950. ' $mod.THelper.$new("NewHlp", [5]);',
  22951. ' return this;',
  22952. ' };',
  22953. ' this.$new = function (fn, args) {',
  22954. ' return this[fn].apply($mod.TRec.$new(), args);',
  22955. ' };',
  22956. '});',
  22957. 'this.Rec = this.TRec.$new();',
  22958. '']),
  22959. LinesToStr([ // $mod.$main
  22960. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22961. 'var $with = $mod.Rec;',
  22962. '$mod.THelper.NewHlp.call($with, 12);',
  22963. '$mod.THelper.$new("NewHlp", [3]);',
  22964. 'var $with1 = $mod.TRec;',
  22965. '$mod.THelper.$new("NewHlp", [13]);',
  22966. '']));
  22967. end;
  22968. procedure TTestModule.TestTypeHelper_ClassVar;
  22969. begin
  22970. StartProgram(false);
  22971. Add([
  22972. '{$modeswitch typehelpers}',
  22973. 'type',
  22974. ' THelper = type helper for byte',
  22975. ' const',
  22976. ' One = 1;',
  22977. ' Two: word = 2;',
  22978. ' class var',
  22979. ' Glob: word;',
  22980. ' function Foo(w: word): word;',
  22981. ' class function Bar(w: word): word; static;',
  22982. ' end;',
  22983. 'function THelper.foo(w: word): word;',
  22984. 'begin',
  22985. ' Result:=w;',
  22986. ' Two:=One+w;',
  22987. ' Glob:=Glob;',
  22988. ' Result:=Self.Glob;',
  22989. ' Self.Glob:=Self.Glob;',
  22990. ' with Self do Glob:=Glob;',
  22991. 'end;',
  22992. 'class function THelper.bar(w: word): word;',
  22993. 'begin',
  22994. ' Result:=w;',
  22995. ' Two:=One;',
  22996. ' Glob:=Glob;',
  22997. 'end;',
  22998. 'var b: byte;',
  22999. 'begin',
  23000. ' byte.two:=byte.one;',
  23001. ' byte.Glob:=byte.Glob;',
  23002. ' with byte do begin',
  23003. ' two:=one;',
  23004. ' Glob:=Glob;',
  23005. ' end;',
  23006. ' b.two:=b.one;',
  23007. ' b.Glob:=b.Glob;',
  23008. ' with b do begin',
  23009. ' two:=one;',
  23010. ' Glob:=Glob;',
  23011. ' end;',
  23012. '']);
  23013. ConvertProgram;
  23014. CheckSource('TestTypeHelper_ClassVar',
  23015. LinesToStr([ // statements
  23016. 'rtl.createHelper(this, "THelper", null, function () {',
  23017. ' this.One = 1;',
  23018. ' this.Two = 2;',
  23019. ' this.Glob = 0;',
  23020. ' this.Foo = function (w) {',
  23021. ' var Result = 0;',
  23022. ' Result = w;',
  23023. ' $mod.THelper.Two = 1 + w;',
  23024. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23025. ' Result = $mod.THelper.Glob;',
  23026. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23027. ' var $with = this.get();',
  23028. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23029. ' return Result;',
  23030. ' };',
  23031. ' this.Bar = function (w) {',
  23032. ' var Result = 0;',
  23033. ' Result = w;',
  23034. ' $mod.THelper.Two = 1;',
  23035. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23036. ' return Result;',
  23037. ' };',
  23038. '});',
  23039. 'this.b = 0;',
  23040. '']),
  23041. LinesToStr([ // $mod.$main
  23042. '$mod.THelper.Two = 1;',
  23043. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23044. '$mod.THelper.Two = 1;',
  23045. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23046. '$mod.THelper.Two = 1;',
  23047. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23048. 'var $with = $mod.b;',
  23049. '$mod.THelper.Two = 1;',
  23050. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23051. '']));
  23052. end;
  23053. procedure TTestModule.TestTypeHelper_PassResultElement;
  23054. begin
  23055. StartProgram(false);
  23056. Add([
  23057. '{$modeswitch typehelpers}',
  23058. 'type',
  23059. ' THelper = type helper for word',
  23060. ' procedure DoIt(e: byte = 123);',
  23061. ' class procedure DoSome(e: byte = 456); static;',
  23062. ' end;',
  23063. 'procedure THelper.DoIt(e: byte);',
  23064. 'begin',
  23065. 'end;',
  23066. 'class procedure THelper.DoSome(e: byte);',
  23067. 'begin',
  23068. 'end;',
  23069. 'function Foo(w: word): word;',
  23070. 'begin',
  23071. ' Result.DoIt;',
  23072. ' Result.DoIt();',
  23073. ' Result.DoSome;',
  23074. ' Result.DoSome();',
  23075. ' with Result do begin',
  23076. ' DoIt;',
  23077. ' DoIt();',
  23078. ' DoSome;',
  23079. ' DoSome();',
  23080. ' end;',
  23081. 'end;',
  23082. 'begin',
  23083. '']);
  23084. ConvertProgram;
  23085. CheckSource('TestTypeHelper_PassResultElement',
  23086. LinesToStr([ // statements
  23087. 'rtl.createHelper(this, "THelper", null, function () {',
  23088. ' this.DoIt = function (e) {',
  23089. ' };',
  23090. ' this.DoSome = function (e) {',
  23091. ' };',
  23092. '});',
  23093. 'this.Foo = function (w) {',
  23094. ' var Result = 0;',
  23095. ' $mod.THelper.DoIt.call({',
  23096. ' get: function () {',
  23097. ' return Result;',
  23098. ' },',
  23099. ' set: function (v) {',
  23100. ' Result = v;',
  23101. ' }',
  23102. ' }, 123);',
  23103. ' $mod.THelper.DoIt.call({',
  23104. ' get: function () {',
  23105. ' return Result;',
  23106. ' },',
  23107. ' set: function (v) {',
  23108. ' Result = v;',
  23109. ' }',
  23110. ' }, 123);',
  23111. ' $mod.THelper.DoSome(456);',
  23112. ' $mod.THelper.DoSome(456);',
  23113. ' $mod.THelper.DoIt.call({',
  23114. ' get: function () {',
  23115. ' return Result;',
  23116. ' },',
  23117. ' set: function (v) {',
  23118. ' Result = v;',
  23119. ' }',
  23120. ' }, 123);',
  23121. ' $mod.THelper.DoIt.call({',
  23122. ' get: function () {',
  23123. ' return Result;',
  23124. ' },',
  23125. ' set: function (v) {',
  23126. ' Result = v;',
  23127. ' }',
  23128. ' }, 123);',
  23129. ' $mod.THelper.DoSome(456);',
  23130. ' $mod.THelper.DoSome(456);',
  23131. ' return Result;',
  23132. '};',
  23133. '']),
  23134. LinesToStr([ // $mod.$main
  23135. '']));
  23136. end;
  23137. procedure TTestModule.TestTypeHelper_PassArgs;
  23138. begin
  23139. StartProgram(false);
  23140. Add([
  23141. '{$modeswitch typehelpers}',
  23142. 'type',
  23143. ' THelper = type helper for word',
  23144. ' procedure DoIt(e: byte = 123);',
  23145. ' end;',
  23146. 'procedure THelper.DoIt(e: byte);',
  23147. 'begin',
  23148. 'end;',
  23149. 'procedure FooDefault(a: word);',
  23150. 'begin',
  23151. ' a.DoIt;',
  23152. ' with a do DoIt;',
  23153. 'end;',
  23154. 'procedure FooConst(const a: word);',
  23155. 'begin',
  23156. ' a.DoIt;',
  23157. ' with a do DoIt;',
  23158. 'end;',
  23159. 'procedure FooVar(var a: word);',
  23160. 'begin',
  23161. ' a.DoIt;',
  23162. ' with a do DoIt;',
  23163. 'end;',
  23164. 'begin',
  23165. '']);
  23166. ConvertProgram;
  23167. CheckSource('TestTypeHelper_PassArgs',
  23168. LinesToStr([ // statements
  23169. 'rtl.createHelper(this, "THelper", null, function () {',
  23170. ' this.DoIt = function (e) {',
  23171. ' };',
  23172. '});',
  23173. 'this.FooDefault = function (a) {',
  23174. ' $mod.THelper.DoIt.call({',
  23175. ' get: function () {',
  23176. ' return a;',
  23177. ' },',
  23178. ' set: function (v) {',
  23179. ' a = v;',
  23180. ' }',
  23181. ' }, 123);',
  23182. ' $mod.THelper.DoIt.call({',
  23183. ' get: function () {',
  23184. ' return a;',
  23185. ' },',
  23186. ' set: function (v) {',
  23187. ' a = v;',
  23188. ' }',
  23189. ' }, 123);',
  23190. '};',
  23191. 'this.FooConst = function (a) {',
  23192. ' $mod.THelper.DoIt.call({',
  23193. ' get: function () {',
  23194. ' return a;',
  23195. ' },',
  23196. ' set: function (v) {',
  23197. ' rtl.raiseE("EPropReadOnly");',
  23198. ' }',
  23199. ' }, 123);',
  23200. ' $mod.THelper.DoIt.call({',
  23201. ' get: function () {',
  23202. ' return a;',
  23203. ' },',
  23204. ' set: function () {',
  23205. ' rtl.raiseE("EPropReadOnly");',
  23206. ' }',
  23207. ' }, 123);',
  23208. '};',
  23209. 'this.FooVar = function (a) {',
  23210. ' $mod.THelper.DoIt.call(a, 123);',
  23211. ' var $with = a.get();',
  23212. ' $mod.THelper.DoIt.call(a, 123);',
  23213. '};',
  23214. '']),
  23215. LinesToStr([ // $mod.$main
  23216. '']));
  23217. end;
  23218. procedure TTestModule.TestTypeHelper_PassVarConst;
  23219. begin
  23220. StartProgram(false);
  23221. Add([
  23222. '{$modeswitch typehelpers}',
  23223. 'type',
  23224. ' THelper = type helper for word',
  23225. ' procedure DoIt(e: byte = 123);',
  23226. ' end;',
  23227. 'procedure THelper.DoIt(e: byte);',
  23228. 'begin',
  23229. 'end;',
  23230. 'var a: word;',
  23231. 'const c: word = 2;',
  23232. '{$writeableconst off}',
  23233. 'const r: word = 3;',
  23234. 'begin',
  23235. ' a.DoIt;',
  23236. ' with a do DoIt;',
  23237. ' c.DoIt;',
  23238. ' with c do DoIt;',
  23239. ' r.DoIt;',
  23240. ' with r do DoIt;',
  23241. '']);
  23242. ConvertProgram;
  23243. CheckSource('TestTypeHelper_PassVarConst',
  23244. LinesToStr([ // statements
  23245. 'rtl.createHelper(this, "THelper", null, function () {',
  23246. ' this.DoIt = function (e) {',
  23247. ' };',
  23248. '});',
  23249. 'this.a = 0;',
  23250. 'this.c = 2;',
  23251. 'this.r = 3;',
  23252. '']),
  23253. LinesToStr([ // $mod.$main
  23254. '$mod.THelper.DoIt.call({',
  23255. ' p: $mod,',
  23256. ' get: function () {',
  23257. ' return this.p.a;',
  23258. ' },',
  23259. ' set: function (v) {',
  23260. ' this.p.a = v;',
  23261. ' }',
  23262. '}, 123);',
  23263. 'var $with = $mod.a;',
  23264. '$mod.THelper.DoIt.call({',
  23265. ' get: function () {',
  23266. ' return $with;',
  23267. ' },',
  23268. ' set: function (v) {',
  23269. ' $with = v;',
  23270. ' }',
  23271. '}, 123);',
  23272. '$mod.THelper.DoIt.call({',
  23273. ' p: $mod,',
  23274. ' get: function () {',
  23275. ' return this.p.c;',
  23276. ' },',
  23277. ' set: function (v) {',
  23278. ' this.p.c = v;',
  23279. ' }',
  23280. '}, 123);',
  23281. 'var $with1 = $mod.c;',
  23282. '$mod.THelper.DoIt.call({',
  23283. ' get: function () {',
  23284. ' return $with1;',
  23285. ' },',
  23286. ' set: function (v) {',
  23287. ' $with1 = v;',
  23288. ' }',
  23289. '}, 123);',
  23290. '$mod.THelper.DoIt.call({',
  23291. ' get: function () {',
  23292. ' return 3;',
  23293. ' },',
  23294. ' set: function (v) {',
  23295. ' rtl.raiseE("EPropReadOnly");',
  23296. ' }',
  23297. '}, 123);',
  23298. 'var $with2 = 3;',
  23299. ' $mod.THelper.DoIt.call({',
  23300. ' get: function () {',
  23301. ' return $with2;',
  23302. ' },',
  23303. ' set: function () {',
  23304. ' rtl.raiseE("EPropReadOnly");',
  23305. ' }',
  23306. ' }, 123);',
  23307. '']));
  23308. end;
  23309. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23310. begin
  23311. StartProgram(false);
  23312. Add([
  23313. '{$modeswitch typehelpers}',
  23314. 'type',
  23315. ' THelper = type helper for word',
  23316. ' procedure DoIt(e: byte = 123);',
  23317. ' end;',
  23318. 'procedure THelper.DoIt(e: byte);',
  23319. 'begin',
  23320. 'end;',
  23321. 'function Foo(b: byte = 1): word;',
  23322. 'begin',
  23323. 'end;',
  23324. 'begin',
  23325. ' Foo.DoIt;',
  23326. ' Foo().DoIt;',
  23327. ' with Foo do DoIt;',
  23328. ' with Foo() do DoIt;',
  23329. '']);
  23330. ConvertProgram;
  23331. CheckSource('TestTypeHelper_PassFuncResult',
  23332. LinesToStr([ // statements
  23333. 'rtl.createHelper(this, "THelper", null, function () {',
  23334. ' this.DoIt = function (e) {',
  23335. ' };',
  23336. '});',
  23337. 'this.Foo = function (b) {',
  23338. ' var Result = 0;',
  23339. ' return Result;',
  23340. '};',
  23341. '']),
  23342. LinesToStr([ // $mod.$main
  23343. '$mod.THelper.DoIt.call({',
  23344. ' a: $mod.Foo(1),',
  23345. ' get: function () {',
  23346. ' return this.a;',
  23347. ' },',
  23348. ' set: function (v) {',
  23349. ' this.a = v;',
  23350. ' }',
  23351. '}, 123);',
  23352. '$mod.THelper.DoIt.call({',
  23353. ' a: $mod.Foo(1),',
  23354. ' get: function () {',
  23355. ' return this.a;',
  23356. ' },',
  23357. ' set: function (v) {',
  23358. ' this.a = v;',
  23359. ' }',
  23360. '}, 123);',
  23361. 'var $with = $mod.Foo(1);',
  23362. '$mod.THelper.DoIt.call({',
  23363. ' get: function () {',
  23364. ' return $with;',
  23365. ' },',
  23366. ' set: function (v) {',
  23367. ' $with = v;',
  23368. ' }',
  23369. '}, 123);',
  23370. 'var $with1 = $mod.Foo(1);',
  23371. '$mod.THelper.DoIt.call({',
  23372. ' get: function () {',
  23373. ' return $with1;',
  23374. ' },',
  23375. ' set: function (v) {',
  23376. ' $with1 = v;',
  23377. ' }',
  23378. '}, 123);',
  23379. '']));
  23380. end;
  23381. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23382. begin
  23383. StartProgram(false);
  23384. Add([
  23385. '{$modeswitch typehelpers}',
  23386. 'type',
  23387. ' TObject = class',
  23388. ' FField: word;',
  23389. ' procedure SetField(Value: word);',
  23390. ' property Field: word read FField write SetField;',
  23391. ' end;',
  23392. ' THelper = type helper for word',
  23393. ' procedure Fly;',
  23394. ' class procedure Run; static;',
  23395. ' end;',
  23396. 'procedure TObject.SetField(Value: word);',
  23397. 'begin',
  23398. ' Field.Fly;',
  23399. ' Field.Run;',
  23400. ' Self.Field.Fly;',
  23401. ' Self.Field.Run;',
  23402. ' with Self do begin',
  23403. ' Field.Fly;',
  23404. ' Field.Run;',
  23405. ' end;',
  23406. ' with Self.Field do begin',
  23407. ' Fly;',
  23408. ' Run;',
  23409. ' end;',
  23410. 'end;',
  23411. 'procedure THelper.Fly;',
  23412. 'begin',
  23413. 'end;',
  23414. 'class procedure THelper.Run;',
  23415. 'begin',
  23416. 'end;',
  23417. 'var',
  23418. ' o: TObject;',
  23419. 'begin',
  23420. ' o.Field.Fly;',
  23421. ' o.Field.Run;',
  23422. ' with o do begin',
  23423. ' Field.Fly;',
  23424. ' Field.Run;',
  23425. ' end;',
  23426. ' with o.Field do begin',
  23427. ' Fly;',
  23428. ' Run;',
  23429. ' end;',
  23430. '']);
  23431. ConvertProgram;
  23432. CheckSource('TestTypeHelper_PassPropertyField',
  23433. LinesToStr([ // statements
  23434. 'rtl.createClass(this, "TObject", null, function () {',
  23435. ' this.$init = function () {',
  23436. ' this.FField = 0;',
  23437. ' };',
  23438. ' this.$final = function () {',
  23439. ' };',
  23440. ' this.SetField = function (Value) {',
  23441. ' $mod.THelper.Fly.call({',
  23442. ' p: this,',
  23443. ' get: function () {',
  23444. ' return this.p.FField;',
  23445. ' },',
  23446. ' set: function (v) {',
  23447. ' this.p.FField = v;',
  23448. ' }',
  23449. ' });',
  23450. ' $mod.THelper.Run();',
  23451. ' $mod.THelper.Fly.call({',
  23452. ' p: this,',
  23453. ' get: function () {',
  23454. ' return this.p.FField;',
  23455. ' },',
  23456. ' set: function (v) {',
  23457. ' this.p.FField = v;',
  23458. ' }',
  23459. ' });',
  23460. ' $mod.THelper.Run();',
  23461. ' $mod.THelper.Fly.call({',
  23462. ' p: this,',
  23463. ' get: function () {',
  23464. ' return this.p.FField;',
  23465. ' },',
  23466. ' set: function (v) {',
  23467. ' this.p.FField = v;',
  23468. ' }',
  23469. ' });',
  23470. ' $mod.THelper.Run();',
  23471. ' var $with = this.FField;',
  23472. ' $mod.THelper.Fly.call({',
  23473. ' get: function () {',
  23474. ' return $with;',
  23475. ' },',
  23476. ' set: function (v) {',
  23477. ' $with = v;',
  23478. ' }',
  23479. ' });',
  23480. ' $mod.THelper.Run();',
  23481. ' };',
  23482. '});',
  23483. 'rtl.createHelper(this, "THelper", null, function () {',
  23484. ' this.Fly = function () {',
  23485. ' };',
  23486. ' this.Run = function () {',
  23487. ' };',
  23488. '});',
  23489. 'this.o = null;',
  23490. '']),
  23491. LinesToStr([ // $mod.$main
  23492. '$mod.THelper.Fly.call({',
  23493. ' p: $mod.o,',
  23494. ' get: function () {',
  23495. ' return this.p.FField;',
  23496. ' },',
  23497. ' set: function (v) {',
  23498. ' this.p.FField = v;',
  23499. ' }',
  23500. '});',
  23501. '$mod.THelper.Run();',
  23502. 'var $with = $mod.o;',
  23503. '$mod.THelper.Fly.call({',
  23504. ' p: $with,',
  23505. ' get: function () {',
  23506. ' return this.p.FField;',
  23507. ' },',
  23508. ' set: function (v) {',
  23509. ' this.p.FField = v;',
  23510. ' }',
  23511. '});',
  23512. '$mod.THelper.Run();',
  23513. 'var $with1 = $mod.o.FField;',
  23514. '$mod.THelper.Fly.call({',
  23515. ' get: function () {',
  23516. ' return $with1;',
  23517. ' },',
  23518. ' set: function (v) {',
  23519. ' $with1 = v;',
  23520. ' }',
  23521. '});',
  23522. '$mod.THelper.Run();',
  23523. '']));
  23524. end;
  23525. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23526. begin
  23527. StartProgram(false);
  23528. Add([
  23529. '{$modeswitch typehelpers}',
  23530. 'type',
  23531. ' TObject = class',
  23532. ' FField: word;',
  23533. ' function GetField: word;',
  23534. ' property Field: word read GetField write FField;',
  23535. ' end;',
  23536. ' THelper = type helper for word',
  23537. ' procedure Fly;',
  23538. ' class procedure Run; static;',
  23539. ' end;',
  23540. 'function TObject.GetField: word;',
  23541. 'begin',
  23542. ' Field.Fly;',
  23543. ' Field.Run;',
  23544. ' Self.Field.Fly;',
  23545. ' Self.Field.Run;',
  23546. ' with Self do begin',
  23547. ' Field.Fly;',
  23548. ' Field.Run;',
  23549. ' end;',
  23550. ' with Self.Field do begin',
  23551. ' Fly;',
  23552. ' Run;',
  23553. ' end;',
  23554. 'end;',
  23555. 'procedure THelper.Fly;',
  23556. 'begin',
  23557. 'end;',
  23558. 'class procedure THelper.Run;',
  23559. 'begin',
  23560. 'end;',
  23561. 'var',
  23562. ' o: TObject;',
  23563. 'begin',
  23564. ' o.Field.Fly;',
  23565. ' o.Field.Run;',
  23566. ' with o do begin',
  23567. ' Field.Fly;',
  23568. ' Field.Run;',
  23569. ' end;',
  23570. ' with o.Field do begin',
  23571. ' Fly;',
  23572. ' Run;',
  23573. ' end;',
  23574. '']);
  23575. ConvertProgram;
  23576. CheckSource('TestTypeHelper_PassPropertyGetter',
  23577. LinesToStr([ // statements
  23578. 'rtl.createClass(this, "TObject", null, function () {',
  23579. ' this.$init = function () {',
  23580. ' this.FField = 0;',
  23581. ' };',
  23582. ' this.$final = function () {',
  23583. ' };',
  23584. ' this.GetField = function () {',
  23585. ' var Result = 0;',
  23586. ' $mod.THelper.Fly.call({',
  23587. ' p: this.GetField(),',
  23588. ' get: function () {',
  23589. ' return this.p;',
  23590. ' },',
  23591. ' set: function (v) {',
  23592. ' this.p = v;',
  23593. ' }',
  23594. ' });',
  23595. ' $mod.THelper.Run();',
  23596. ' $mod.THelper.Fly.call({',
  23597. ' p: this.GetField(),',
  23598. ' get: function () {',
  23599. ' return this.p;',
  23600. ' },',
  23601. ' set: function (v) {',
  23602. ' this.p = v;',
  23603. ' }',
  23604. ' });',
  23605. ' $mod.THelper.Run();',
  23606. ' $mod.THelper.Fly.call({',
  23607. ' p: this.GetField(),',
  23608. ' get: function () {',
  23609. ' return this.p;',
  23610. ' },',
  23611. ' set: function (v) {',
  23612. ' this.p = v;',
  23613. ' }',
  23614. ' });',
  23615. ' $mod.THelper.Run();',
  23616. ' var $with = this.GetField();',
  23617. ' $mod.THelper.Fly.call({',
  23618. ' get: function () {',
  23619. ' return $with;',
  23620. ' },',
  23621. ' set: function (v) {',
  23622. ' $with = v;',
  23623. ' }',
  23624. ' });',
  23625. ' $mod.THelper.Run();',
  23626. ' return Result;',
  23627. ' };',
  23628. '});',
  23629. 'rtl.createHelper(this, "THelper", null, function () {',
  23630. ' this.Fly = function () {',
  23631. ' };',
  23632. ' this.Run = function () {',
  23633. ' };',
  23634. '});',
  23635. 'this.o = null;',
  23636. '']),
  23637. LinesToStr([ // $mod.$main
  23638. '$mod.THelper.Fly.call({',
  23639. ' p: $mod.o.GetField(),',
  23640. ' get: function () {',
  23641. ' return this.p;',
  23642. ' },',
  23643. ' set: function (v) {',
  23644. ' this.p = v;',
  23645. ' }',
  23646. '});',
  23647. '$mod.THelper.Run();',
  23648. 'var $with = $mod.o;',
  23649. '$mod.THelper.Fly.call({',
  23650. ' p: $with.GetField(),',
  23651. ' get: function () {',
  23652. ' return this.p;',
  23653. ' },',
  23654. ' set: function (v) {',
  23655. ' this.p = v;',
  23656. ' }',
  23657. '});',
  23658. '$mod.THelper.Run();',
  23659. 'var $with1 = $mod.o.GetField();',
  23660. '$mod.THelper.Fly.call({',
  23661. ' get: function () {',
  23662. ' return $with1;',
  23663. ' },',
  23664. ' set: function (v) {',
  23665. ' $with1 = v;',
  23666. ' }',
  23667. '});',
  23668. '$mod.THelper.Run();',
  23669. '']));
  23670. end;
  23671. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23672. begin
  23673. StartProgram(false);
  23674. Add([
  23675. '{$modeswitch typehelpers}',
  23676. 'type',
  23677. ' TObject = class',
  23678. ' class var FField: word;',
  23679. ' class procedure SetField(Value: word);',
  23680. ' class property Field: word read FField write SetField;',
  23681. ' end;',
  23682. ' THelper = type helper for word',
  23683. ' procedure Fly(n: byte);',
  23684. ' end;',
  23685. 'class procedure TObject.SetField(Value: word);',
  23686. 'begin',
  23687. ' Field.Fly(1);',
  23688. ' Self.Field.Fly(2);',
  23689. ' with Self do Field.Fly(3);',
  23690. ' with Self.Field do Fly(4);',
  23691. ' TObject.Field.Fly(5);',
  23692. ' with TObject do Field.Fly(6);',
  23693. ' with TObject.Field do Fly(7);',
  23694. 'end;',
  23695. 'procedure THelper.Fly(n: byte);',
  23696. 'begin',
  23697. 'end;',
  23698. 'var',
  23699. ' o: TObject;',
  23700. 'begin',
  23701. ' o.Field.Fly(11);',
  23702. ' with o do Field.Fly(12);',
  23703. ' with o.Field do Fly(13);',
  23704. ' TObject.Field.Fly(14);',
  23705. ' with TObject do Field.Fly(15);',
  23706. ' with TObject.Field do Fly(16);',
  23707. '']);
  23708. ConvertProgram;
  23709. CheckSource('TestTypeHelper_PassClassPropertyField',
  23710. LinesToStr([ // statements
  23711. 'rtl.createClass(this, "TObject", null, function () {',
  23712. ' this.FField = 0;',
  23713. ' this.$init = function () {',
  23714. ' };',
  23715. ' this.$final = function () {',
  23716. ' };',
  23717. ' this.SetField = function (Value) {',
  23718. ' $mod.THelper.Fly.call({',
  23719. ' p: this,',
  23720. ' get: function () {',
  23721. ' return this.p.FField;',
  23722. ' },',
  23723. ' set: function (v) {',
  23724. ' $mod.TObject.FField = v;',
  23725. ' }',
  23726. ' }, 1);',
  23727. ' $mod.THelper.Fly.call({',
  23728. ' p: this,',
  23729. ' get: function () {',
  23730. ' return this.p.FField;',
  23731. ' },',
  23732. ' set: function (v) {',
  23733. ' $mod.TObject.FField = v;',
  23734. ' }',
  23735. ' }, 2);',
  23736. ' $mod.THelper.Fly.call({',
  23737. ' p: this,',
  23738. ' get: function () {',
  23739. ' return this.p.FField;',
  23740. ' },',
  23741. ' set: function (v) {',
  23742. ' $mod.TObject.FField = v;',
  23743. ' }',
  23744. ' }, 3);',
  23745. ' var $with = this.FField;',
  23746. ' $mod.THelper.Fly.call({',
  23747. ' get: function () {',
  23748. ' return $with;',
  23749. ' },',
  23750. ' set: function (v) {',
  23751. ' $with = v;',
  23752. ' }',
  23753. ' }, 4);',
  23754. ' $mod.THelper.Fly.call({',
  23755. ' p: $mod.TObject,',
  23756. ' get: function () {',
  23757. ' return this.p.FField;',
  23758. ' },',
  23759. ' set: function (v) {',
  23760. ' $mod.TObject.FField = v;',
  23761. ' }',
  23762. ' }, 5);',
  23763. ' var $with1 = $mod.TObject;',
  23764. ' $mod.THelper.Fly.call({',
  23765. ' p: $with1,',
  23766. ' get: function () {',
  23767. ' return this.p.FField;',
  23768. ' },',
  23769. ' set: function (v) {',
  23770. ' $mod.TObject.FField = v;',
  23771. ' }',
  23772. ' }, 6);',
  23773. ' var $with2 = $mod.TObject.FField;',
  23774. ' $mod.THelper.Fly.call({',
  23775. ' get: function () {',
  23776. ' return $with2;',
  23777. ' },',
  23778. ' set: function (v) {',
  23779. ' $with2 = v;',
  23780. ' }',
  23781. ' }, 7);',
  23782. ' };',
  23783. '});',
  23784. 'rtl.createHelper(this, "THelper", null, function () {',
  23785. ' this.Fly = function (n) {',
  23786. ' };',
  23787. '});',
  23788. 'this.o = null;',
  23789. '']),
  23790. LinesToStr([ // $mod.$main
  23791. '$mod.THelper.Fly.call({',
  23792. ' p: $mod.o,',
  23793. ' get: function () {',
  23794. ' return this.p.FField;',
  23795. ' },',
  23796. ' set: function (v) {',
  23797. ' $mod.TObject.FField = v;',
  23798. ' }',
  23799. '}, 11);',
  23800. 'var $with = $mod.o;',
  23801. '$mod.THelper.Fly.call({',
  23802. ' p: $with,',
  23803. ' get: function () {',
  23804. ' return this.p.FField;',
  23805. ' },',
  23806. ' set: function (v) {',
  23807. ' $mod.TObject.FField = v;',
  23808. ' }',
  23809. '}, 12);',
  23810. 'var $with1 = $mod.o.FField;',
  23811. '$mod.THelper.Fly.call({',
  23812. ' get: function () {',
  23813. ' return $with1;',
  23814. ' },',
  23815. ' set: function (v) {',
  23816. ' $with1 = v;',
  23817. ' }',
  23818. '}, 13);',
  23819. '$mod.THelper.Fly.call({',
  23820. ' p: $mod.TObject,',
  23821. ' get: function () {',
  23822. ' return this.p.FField;',
  23823. ' },',
  23824. ' set: function (v) {',
  23825. ' $mod.TObject.FField = v;',
  23826. ' }',
  23827. '}, 14);',
  23828. 'var $with2 = $mod.TObject;',
  23829. '$mod.THelper.Fly.call({',
  23830. ' p: $with2,',
  23831. ' get: function () {',
  23832. ' return this.p.FField;',
  23833. ' },',
  23834. ' set: function (v) {',
  23835. ' $mod.TObject.FField = v;',
  23836. ' }',
  23837. '}, 15);',
  23838. 'var $with3 = $mod.TObject.FField;',
  23839. '$mod.THelper.Fly.call({',
  23840. ' get: function () {',
  23841. ' return $with3;',
  23842. ' },',
  23843. ' set: function (v) {',
  23844. ' $with3 = v;',
  23845. ' }',
  23846. '}, 16);',
  23847. '']));
  23848. end;
  23849. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23850. begin
  23851. StartProgram(false);
  23852. Add([
  23853. '{$modeswitch typehelpers}',
  23854. 'type',
  23855. ' TObject = class',
  23856. ' class var FField: word;',
  23857. ' class function GetField: word; static;',
  23858. ' class property Field: word read GetField write FField;',
  23859. ' end;',
  23860. ' THelper = type helper for word',
  23861. ' procedure Fly(n: byte);',
  23862. ' end;',
  23863. 'class function TObject.GetField: word;',
  23864. 'begin',
  23865. ' Field.Fly(1);',
  23866. ' TObject.Field.Fly(5);',
  23867. ' with TObject do Field.Fly(6);',
  23868. ' with TObject.Field do Fly(7);',
  23869. 'end;',
  23870. 'procedure THelper.Fly(n: byte);',
  23871. 'begin',
  23872. 'end;',
  23873. 'var',
  23874. ' o: TObject;',
  23875. 'begin',
  23876. ' o.Field.Fly(11);',
  23877. ' with o do Field.Fly(12);',
  23878. ' with o.Field do Fly(13);',
  23879. '']);
  23880. ConvertProgram;
  23881. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23882. LinesToStr([ // statements
  23883. 'rtl.createClass(this, "TObject", null, function () {',
  23884. ' this.FField = 0;',
  23885. ' this.$init = function () {',
  23886. ' };',
  23887. ' this.$final = function () {',
  23888. ' };',
  23889. ' this.GetField = function () {',
  23890. ' var Result = 0;',
  23891. ' $mod.THelper.Fly.call({',
  23892. ' p: $mod.TObject.GetField(),',
  23893. ' get: function () {',
  23894. ' return this.p;',
  23895. ' },',
  23896. ' set: function (v) {',
  23897. ' this.p = v;',
  23898. ' }',
  23899. ' }, 1);',
  23900. ' $mod.THelper.Fly.call({',
  23901. ' p: $mod.TObject.GetField(),',
  23902. ' get: function () {',
  23903. ' return this.p;',
  23904. ' },',
  23905. ' set: function (v) {',
  23906. ' this.p = v;',
  23907. ' }',
  23908. ' }, 5);',
  23909. ' var $with = $mod.TObject;',
  23910. ' $mod.THelper.Fly.call({',
  23911. ' p: $with.GetField(),',
  23912. ' get: function () {',
  23913. ' return this.p;',
  23914. ' },',
  23915. ' set: function (v) {',
  23916. ' this.p = v;',
  23917. ' }',
  23918. ' }, 6);',
  23919. ' var $with1 = $mod.TObject.GetField();',
  23920. ' $mod.THelper.Fly.call({',
  23921. ' get: function () {',
  23922. ' return $with1;',
  23923. ' },',
  23924. ' set: function (v) {',
  23925. ' $with1 = v;',
  23926. ' }',
  23927. ' }, 7);',
  23928. ' return Result;',
  23929. ' };',
  23930. '});',
  23931. 'rtl.createHelper(this, "THelper", null, function () {',
  23932. ' this.Fly = function (n) {',
  23933. ' };',
  23934. '});',
  23935. 'this.o = null;',
  23936. '']),
  23937. LinesToStr([ // $mod.$main
  23938. '$mod.THelper.Fly.call({',
  23939. ' p: $mod.TObject.GetField(),',
  23940. ' get: function () {',
  23941. ' return this.p;',
  23942. ' },',
  23943. ' set: function (v) {',
  23944. ' this.p = v;',
  23945. ' }',
  23946. '}, 11);',
  23947. 'var $with = $mod.o;',
  23948. '$mod.THelper.Fly.call({',
  23949. ' p: $with.GetField(),',
  23950. ' get: function () {',
  23951. ' return this.p;',
  23952. ' },',
  23953. ' set: function (v) {',
  23954. ' this.p = v;',
  23955. ' }',
  23956. '}, 12);',
  23957. 'var $with1 = $mod.TObject.GetField();',
  23958. '$mod.THelper.Fly.call({',
  23959. ' get: function () {',
  23960. ' return $with1;',
  23961. ' },',
  23962. ' set: function (v) {',
  23963. ' $with1 = v;',
  23964. ' }',
  23965. '}, 13);',
  23966. '']));
  23967. end;
  23968. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23969. begin
  23970. StartProgram(false);
  23971. Add([
  23972. '{$modeswitch typehelpers}',
  23973. 'type',
  23974. ' TObject = class',
  23975. ' class var FField: word;',
  23976. ' class function GetField: word;',
  23977. ' class property Field: word read GetField write FField;',
  23978. ' end;',
  23979. ' TClass = class of TObject;',
  23980. ' THelper = type helper for word',
  23981. ' procedure Fly(n: byte);',
  23982. ' end;',
  23983. 'class function TObject.GetField: word;',
  23984. 'begin',
  23985. ' Field.Fly(1);',
  23986. ' Self.Field.Fly(5);',
  23987. ' with Self do Field.Fly(6);',
  23988. ' with Self.Field do Fly(7);',
  23989. 'end;',
  23990. 'procedure THelper.Fly(n: byte);',
  23991. 'begin',
  23992. 'end;',
  23993. 'var',
  23994. ' o: TObject;',
  23995. ' c: TClass;',
  23996. 'begin',
  23997. ' o.Field.Fly(11);',
  23998. ' with o do Field.Fly(12);',
  23999. ' with o.Field do Fly(13);',
  24000. ' c.Field.Fly(14);',
  24001. ' with c do Field.Fly(15);',
  24002. ' with c.Field do Fly(16);',
  24003. '']);
  24004. ConvertProgram;
  24005. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  24006. LinesToStr([ // statements
  24007. 'rtl.createClass(this, "TObject", null, function () {',
  24008. ' this.FField = 0;',
  24009. ' this.$init = function () {',
  24010. ' };',
  24011. ' this.$final = function () {',
  24012. ' };',
  24013. ' this.GetField = function () {',
  24014. ' var Result = 0;',
  24015. ' $mod.THelper.Fly.call({',
  24016. ' p: this.GetField(),',
  24017. ' get: function () {',
  24018. ' return this.p;',
  24019. ' },',
  24020. ' set: function (v) {',
  24021. ' this.p = v;',
  24022. ' }',
  24023. ' }, 1);',
  24024. ' $mod.THelper.Fly.call({',
  24025. ' p: this.GetField(),',
  24026. ' get: function () {',
  24027. ' return this.p;',
  24028. ' },',
  24029. ' set: function (v) {',
  24030. ' this.p = v;',
  24031. ' }',
  24032. ' }, 5);',
  24033. ' $mod.THelper.Fly.call({',
  24034. ' p: this.GetField(),',
  24035. ' get: function () {',
  24036. ' return this.p;',
  24037. ' },',
  24038. ' set: function (v) {',
  24039. ' this.p = v;',
  24040. ' }',
  24041. ' }, 6);',
  24042. ' var $with = this.GetField();',
  24043. ' $mod.THelper.Fly.call({',
  24044. ' get: function () {',
  24045. ' return $with;',
  24046. ' },',
  24047. ' set: function (v) {',
  24048. ' $with = v;',
  24049. ' }',
  24050. ' }, 7);',
  24051. ' return Result;',
  24052. ' };',
  24053. '});',
  24054. 'rtl.createHelper(this, "THelper", null, function () {',
  24055. ' this.Fly = function (n) {',
  24056. ' };',
  24057. '});',
  24058. 'this.o = null;',
  24059. 'this.c = null;',
  24060. '']),
  24061. LinesToStr([ // $mod.$main
  24062. '$mod.THelper.Fly.call({',
  24063. ' p: $mod.o.$class.GetField(),',
  24064. ' get: function () {',
  24065. ' return this.p;',
  24066. ' },',
  24067. ' set: function (v) {',
  24068. ' this.p = v;',
  24069. ' }',
  24070. '}, 11);',
  24071. 'var $with = $mod.o;',
  24072. '$mod.THelper.Fly.call({',
  24073. ' p: $with.$class.GetField(),',
  24074. ' get: function () {',
  24075. ' return this.p;',
  24076. ' },',
  24077. ' set: function (v) {',
  24078. ' this.p = v;',
  24079. ' }',
  24080. '}, 12);',
  24081. 'var $with1 = $mod.o.$class.GetField();',
  24082. '$mod.THelper.Fly.call({',
  24083. ' get: function () {',
  24084. ' return $with1;',
  24085. ' },',
  24086. ' set: function (v) {',
  24087. ' $with1 = v;',
  24088. ' }',
  24089. '}, 13);',
  24090. '$mod.THelper.Fly.call({',
  24091. ' p: $mod.c.GetField(),',
  24092. ' get: function () {',
  24093. ' return this.p;',
  24094. ' },',
  24095. ' set: function (v) {',
  24096. ' this.p = v;',
  24097. ' }',
  24098. '}, 14);',
  24099. 'var $with2 = $mod.c;',
  24100. '$mod.THelper.Fly.call({',
  24101. ' p: $with2.GetField(),',
  24102. ' get: function () {',
  24103. ' return this.p;',
  24104. ' },',
  24105. ' set: function (v) {',
  24106. ' this.p = v;',
  24107. ' }',
  24108. '}, 15);',
  24109. 'var $with3 = $mod.c.GetField();',
  24110. '$mod.THelper.Fly.call({',
  24111. ' get: function () {',
  24112. ' return $with3;',
  24113. ' },',
  24114. ' set: function (v) {',
  24115. ' $with3 = v;',
  24116. ' }',
  24117. '}, 16);',
  24118. '']));
  24119. end;
  24120. procedure TTestModule.TestTypeHelper_Property;
  24121. begin
  24122. StartProgram(false);
  24123. Add([
  24124. '{$modeswitch typehelpers}',
  24125. 'type',
  24126. ' THelper = type helper for word',
  24127. ' function GetSize: longint;',
  24128. ' procedure SetSize(Value: longint);',
  24129. ' property Size: longint read GetSize write SetSize;',
  24130. ' end;',
  24131. 'function THelper.GetSize: longint;',
  24132. 'begin',
  24133. ' Result:=Size+1;',
  24134. ' Size:=2;',
  24135. ' Result:=Self.Size+3;',
  24136. ' Self.Size:=4;',
  24137. ' with Self do begin',
  24138. ' Result:=Size+5;',
  24139. ' Size:=6;',
  24140. ' end;',
  24141. 'end;',
  24142. 'procedure THelper.SetSize(Value: longint);',
  24143. 'begin',
  24144. 'end;',
  24145. 'var w: word;',
  24146. 'begin',
  24147. ' w:=w.Size+7;',
  24148. ' w.Size:=w+8;',
  24149. ' with w do begin',
  24150. ' w:=Size+9;',
  24151. ' Size:=w+10;',
  24152. ' end;',
  24153. '']);
  24154. ConvertProgram;
  24155. CheckSource('TestTypeHelper_Property',
  24156. LinesToStr([ // statements
  24157. 'rtl.createHelper(this, "THelper", null, function () {',
  24158. ' this.GetSize = function () {',
  24159. ' var Result = 0;',
  24160. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24161. ' $mod.THelper.SetSize.call(this, 2);',
  24162. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24163. ' $mod.THelper.SetSize.call(this, 4);',
  24164. ' var $with = this.get();',
  24165. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24166. ' $mod.THelper.SetSize.call(this, 6);',
  24167. ' return Result;',
  24168. ' };',
  24169. ' this.SetSize = function (Value) {',
  24170. ' };',
  24171. '});',
  24172. 'this.w = 0;',
  24173. '']),
  24174. LinesToStr([ // $mod.$main
  24175. '$mod.w = $mod.THelper.GetSize.call({',
  24176. ' p: $mod,',
  24177. ' get: function () {',
  24178. ' return this.p.w;',
  24179. ' },',
  24180. ' set: function (v) {',
  24181. ' this.p.w = v;',
  24182. ' }',
  24183. '}) + 7;',
  24184. '$mod.THelper.SetSize.call({',
  24185. ' p: $mod,',
  24186. ' get: function () {',
  24187. ' return this.p.w;',
  24188. ' },',
  24189. ' set: function (v) {',
  24190. ' this.p.w = v;',
  24191. ' }',
  24192. '}, $mod.w + 8);',
  24193. 'var $with = $mod.w;',
  24194. '$mod.w = $mod.THelper.GetSize.call({',
  24195. ' get: function () {',
  24196. ' return $with;',
  24197. ' },',
  24198. ' set: function (v) {',
  24199. ' $with = v;',
  24200. ' }',
  24201. '}) + 9;',
  24202. '$mod.THelper.SetSize.call({',
  24203. ' get: function () {',
  24204. ' return $with;',
  24205. ' },',
  24206. ' set: function (v) {',
  24207. ' $with = v;',
  24208. ' }',
  24209. '}, $mod.w + 10);',
  24210. '']));
  24211. end;
  24212. procedure TTestModule.TestTypeHelper_Property_Array;
  24213. begin
  24214. StartProgram(false);
  24215. Add([
  24216. '{$modeswitch typehelpers}',
  24217. 'type',
  24218. ' THelper = type helper for word',
  24219. ' function GetItems(Index: byte): boolean;',
  24220. ' procedure SetItems(Index: byte; Value: boolean);',
  24221. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24222. ' end;',
  24223. 'function THelper.GetItems(Index: byte): boolean;',
  24224. 'begin',
  24225. ' Result:=Items[1];',
  24226. ' Items[2]:=false;',
  24227. ' Result:=Self.Items[3];',
  24228. ' Self.Items[4]:=true;',
  24229. ' with Self do begin',
  24230. ' Result:=Items[5];',
  24231. ' Items[6]:=false;',
  24232. ' end;',
  24233. 'end;',
  24234. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24235. 'begin',
  24236. 'end;',
  24237. 'var',
  24238. ' w: word;',
  24239. ' b: boolean;',
  24240. 'begin',
  24241. ' b:=w.Items[1];',
  24242. ' w.Items[2]:=b;',
  24243. ' with w do begin',
  24244. ' b:=Items[3];',
  24245. ' Items[4]:=b;',
  24246. ' end;',
  24247. '']);
  24248. ConvertProgram;
  24249. CheckSource('TestTypeHelper_Property_Array',
  24250. LinesToStr([ // statements
  24251. 'rtl.createHelper(this, "THelper", null, function () {',
  24252. ' this.GetItems = function (Index) {',
  24253. ' var Result = false;',
  24254. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24255. ' $mod.THelper.SetItems.call(this, 2, false);',
  24256. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24257. ' $mod.THelper.SetItems.call(this, 4, true);',
  24258. ' var $with = this.get();',
  24259. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24260. ' $mod.THelper.SetItems.call(this, 6, false);',
  24261. ' return Result;',
  24262. ' };',
  24263. ' this.SetItems = function (Index, Value) {',
  24264. ' };',
  24265. '});',
  24266. 'this.w = 0;',
  24267. 'this.b = false;',
  24268. '']),
  24269. LinesToStr([ // $mod.$main
  24270. '$mod.b = $mod.THelper.GetItems.call({',
  24271. ' p: $mod,',
  24272. ' get: function () {',
  24273. ' return this.p.w;',
  24274. ' },',
  24275. ' set: function (v) {',
  24276. ' this.p.w = v;',
  24277. ' }',
  24278. '}, 1);',
  24279. '$mod.THelper.SetItems.call({',
  24280. ' p: $mod,',
  24281. ' get: function () {',
  24282. ' return this.p.w;',
  24283. ' },',
  24284. ' set: function (v) {',
  24285. ' this.p.w = v;',
  24286. ' }',
  24287. '}, 2, $mod.b);',
  24288. 'var $with = $mod.w;',
  24289. '$mod.b = $mod.THelper.GetItems.call({',
  24290. ' get: function () {',
  24291. ' return $with;',
  24292. ' },',
  24293. ' set: function (v) {',
  24294. ' $with = v;',
  24295. ' }',
  24296. '}, 3);',
  24297. '$mod.THelper.SetItems.call({',
  24298. ' get: function () {',
  24299. ' return $with;',
  24300. ' },',
  24301. ' set: function (v) {',
  24302. ' $with = v;',
  24303. ' }',
  24304. '}, 4, $mod.b);',
  24305. '']));
  24306. end;
  24307. procedure TTestModule.TestTypeHelper_ClassProperty;
  24308. begin
  24309. StartProgram(false);
  24310. Add([
  24311. '{$modeswitch typehelpers}',
  24312. 'type',
  24313. ' THelper = type helper for word',
  24314. ' class function GetSize: longint; static;',
  24315. ' class procedure SetSize(Value: longint); static;',
  24316. ' class property Size: longint read GetSize write SetSize;',
  24317. ' end;',
  24318. 'class function THelper.GetSize: longint;',
  24319. 'begin',
  24320. ' Result:=Size+1;',
  24321. ' Size:=2;',
  24322. 'end;',
  24323. 'class procedure THelper.SetSize(Value: longint);',
  24324. 'begin',
  24325. 'end;',
  24326. 'begin',
  24327. '']);
  24328. ConvertProgram;
  24329. CheckSource('TestTypeHelper_ClassProperty',
  24330. LinesToStr([ // statements
  24331. 'rtl.createHelper(this, "THelper", null, function () {',
  24332. ' this.GetSize = function () {',
  24333. ' var Result = 0;',
  24334. ' Result = $mod.THelper.GetSize() + 1;',
  24335. ' $mod.THelper.SetSize(2);',
  24336. ' return Result;',
  24337. ' };',
  24338. ' this.SetSize = function (Value) {',
  24339. ' };',
  24340. '});',
  24341. '']),
  24342. LinesToStr([ // $mod.$main
  24343. '']));
  24344. end;
  24345. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24346. begin
  24347. StartProgram(false);
  24348. Add([
  24349. '{$modeswitch typehelpers}',
  24350. 'type',
  24351. ' THelper = type helper for word',
  24352. ' class function GetItems(Index: byte): boolean; static;',
  24353. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24354. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24355. ' end;',
  24356. 'class function THelper.GetItems(Index: byte): boolean;',
  24357. 'begin',
  24358. ' Result:=Items[1];',
  24359. ' Items[2]:=false;',
  24360. 'end;',
  24361. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24362. 'begin',
  24363. 'end;',
  24364. 'var',
  24365. ' w: word;',
  24366. ' b: boolean;',
  24367. 'begin',
  24368. ' b:=w.Items[1];',
  24369. ' w.Items[2]:=b;',
  24370. ' with w do begin',
  24371. ' b:=Items[3];',
  24372. ' Items[4]:=b;',
  24373. ' end;',
  24374. '']);
  24375. ConvertProgram;
  24376. CheckSource('TestTypeHelper_ClassProperty_Array',
  24377. LinesToStr([ // statements
  24378. 'rtl.createHelper(this, "THelper", null, function () {',
  24379. ' this.GetItems = function (Index) {',
  24380. ' var Result = false;',
  24381. ' Result = $mod.THelper.GetItems(1);',
  24382. ' $mod.THelper.SetItems(2, false);',
  24383. ' return Result;',
  24384. ' };',
  24385. ' this.SetItems = function (Index, Value) {',
  24386. ' };',
  24387. '});',
  24388. 'this.w = 0;',
  24389. 'this.b = false;',
  24390. '']),
  24391. LinesToStr([ // $mod.$main
  24392. '$mod.b = $mod.THelper.GetItems(1);',
  24393. '$mod.THelper.SetItems(2, $mod.b);',
  24394. 'var $with = $mod.w;',
  24395. '$mod.b = $mod.THelper.GetItems(3);',
  24396. '$mod.THelper.SetItems(4, $mod.b);',
  24397. '']));
  24398. end;
  24399. procedure TTestModule.TestTypeHelper_ClassMethod;
  24400. begin
  24401. StartProgram(false);
  24402. Add([
  24403. '{$modeswitch typehelpers}',
  24404. 'type',
  24405. ' THelper = type helper for word',
  24406. ' class procedure DoStatic; static;',
  24407. ' end;',
  24408. 'class procedure THelper.DoStatic;',
  24409. 'begin',
  24410. ' DoStatic;',
  24411. ' DoStatic();',
  24412. 'end;',
  24413. 'var w: word;',
  24414. 'begin',
  24415. ' w.DoStatic;',
  24416. ' w.DoStatic();',
  24417. '']);
  24418. ConvertProgram;
  24419. CheckSource('TestTypeHelper_ClassMethod',
  24420. LinesToStr([ // statements
  24421. 'rtl.createHelper(this, "THelper", null, function () {',
  24422. ' this.DoStatic = function () {',
  24423. ' $mod.THelper.DoStatic();',
  24424. ' $mod.THelper.DoStatic();',
  24425. ' };',
  24426. '});',
  24427. 'this.w = 0;',
  24428. '']),
  24429. LinesToStr([ // $mod.$main
  24430. '$mod.THelper.DoStatic();',
  24431. '$mod.THelper.DoStatic();',
  24432. '']));
  24433. end;
  24434. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24435. begin
  24436. StartProgram(false);
  24437. Add([
  24438. '{$modeswitch typehelpers}',
  24439. 'type',
  24440. ' THelper = type helper for word',
  24441. ' procedure Run; external name ''Run'';',
  24442. ' end;',
  24443. 'var w: word;',
  24444. 'begin',
  24445. ' w.Run;',
  24446. '']);
  24447. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24448. ConvertProgram;
  24449. end;
  24450. procedure TTestModule.TestTypeHelper_Constructor;
  24451. begin
  24452. StartProgram(false);
  24453. Add([
  24454. '{$modeswitch typehelpers}',
  24455. 'type',
  24456. ' THelper = type helper for word',
  24457. ' constructor Init(e: longint);',
  24458. ' end;',
  24459. 'constructor THelper.Init(e: longint);',
  24460. 'begin',
  24461. ' Self:=e;',
  24462. ' Init(e+1);',
  24463. 'end;',
  24464. 'var w: word;',
  24465. 'begin',
  24466. ' w:=word.Init(2);',
  24467. ' w:=w.Init(3);',
  24468. ' with word do w:=Init(4);',
  24469. ' with w do w:=Init(5);',
  24470. '']);
  24471. ConvertProgram;
  24472. CheckSource('TestTypeHelper_Constructor',
  24473. LinesToStr([ // statements
  24474. 'rtl.createHelper(this, "THelper", null, function () {',
  24475. ' this.Init = function (e) {',
  24476. ' this.set(e);',
  24477. ' $mod.THelper.Init.call(this, e + 1);',
  24478. ' return this.get();',
  24479. ' };',
  24480. ' this.$new = function (fn, args) {',
  24481. ' return this[fn].apply({',
  24482. ' p: 0,',
  24483. ' get: function () {',
  24484. ' return this.p;',
  24485. ' },',
  24486. ' set: function (v) {',
  24487. ' this.p = v;',
  24488. ' }',
  24489. ' }, args);',
  24490. ' };',
  24491. '});',
  24492. 'this.w = 0;',
  24493. '']),
  24494. LinesToStr([ // $mod.$main
  24495. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24496. '$mod.w = $mod.THelper.Init.call({',
  24497. ' p: $mod,',
  24498. ' get: function () {',
  24499. ' return this.p.w;',
  24500. ' },',
  24501. ' set: function (v) {',
  24502. ' this.p.w = v;',
  24503. ' }',
  24504. '}, 3);',
  24505. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24506. 'var $with = $mod.w;',
  24507. '$mod.w = $mod.THelper.Init.call({',
  24508. ' get: function () {',
  24509. ' return $with;',
  24510. ' },',
  24511. ' set: function (v) {',
  24512. ' $with = v;',
  24513. ' }',
  24514. '}, 5);',
  24515. '']));
  24516. end;
  24517. procedure TTestModule.TestTypeHelper_Word;
  24518. begin
  24519. StartProgram(false);
  24520. Add([
  24521. '{$modeswitch typehelpers}',
  24522. 'type',
  24523. ' THelper = type helper for word',
  24524. ' procedure DoIt(e: byte = 123);',
  24525. ' end;',
  24526. 'procedure THelper.DoIt(e: byte);',
  24527. 'begin',
  24528. ' Self:=e;',
  24529. ' Self:=Self+1;',
  24530. ' with Self do Doit;',
  24531. 'end;',
  24532. 'begin',
  24533. ' word(3).DoIt;',
  24534. '']);
  24535. ConvertProgram;
  24536. CheckSource('TestTypeHelper_Word',
  24537. LinesToStr([ // statements
  24538. 'rtl.createHelper(this, "THelper", null, function () {',
  24539. ' this.DoIt = function (e) {',
  24540. ' this.set(e);',
  24541. ' this.set(this.get() + 1);',
  24542. ' var $with = this.get();',
  24543. ' $mod.THelper.DoIt.call(this, 123);',
  24544. ' };',
  24545. '});',
  24546. '']),
  24547. LinesToStr([ // $mod.$main
  24548. '$mod.THelper.DoIt.call({',
  24549. ' get: function () {',
  24550. ' return 3;',
  24551. ' },',
  24552. ' set: function (v) {',
  24553. ' rtl.raiseE("EPropReadOnly");',
  24554. ' }',
  24555. '}, 123);',
  24556. '']));
  24557. end;
  24558. procedure TTestModule.TestTypeHelper_Boolean;
  24559. begin
  24560. StartProgram(false);
  24561. Add([
  24562. '{$modeswitch typehelpers}',
  24563. 'type',
  24564. ' Integer = longint;',
  24565. ' THelper = type helper for boolean',
  24566. ' procedure Run(e: wordbool = true);',
  24567. ' end;',
  24568. 'procedure THelper.Run(e: wordbool);',
  24569. 'begin',
  24570. ' Self:=e;',
  24571. ' Self:=not Self;',
  24572. ' with Self do Run;',
  24573. ' if Integer(Self)=0 then ;',
  24574. 'end;',
  24575. 'begin',
  24576. ' boolean(3).Run;',
  24577. '']);
  24578. ConvertProgram;
  24579. CheckSource('TestTypeHelper_Boolean',
  24580. LinesToStr([ // statements
  24581. 'rtl.createHelper(this, "THelper", null, function () {',
  24582. ' this.Run = function (e) {',
  24583. ' this.set(e);',
  24584. ' this.set(!this.get());',
  24585. ' var $with = this.get();',
  24586. ' $mod.THelper.Run.call(this, true);',
  24587. ' if ((this.get() ? 1 : 0) === 0) ;',
  24588. ' };',
  24589. '});',
  24590. '']),
  24591. LinesToStr([ // $mod.$main
  24592. '$mod.THelper.Run.call({',
  24593. ' a: 3 != 0,',
  24594. ' get: function () {',
  24595. ' return this.a;',
  24596. ' },',
  24597. ' set: function (v) {',
  24598. ' rtl.raiseE("EPropReadOnly");',
  24599. ' }',
  24600. '}, true);',
  24601. '']));
  24602. end;
  24603. procedure TTestModule.TestTypeHelper_WordBool;
  24604. begin
  24605. StartProgram(false);
  24606. Add([
  24607. '{$modeswitch typehelpers}',
  24608. 'type',
  24609. ' Integer = longint;',
  24610. ' THelper = type helper for WordBool',
  24611. ' procedure Run(e: wordbool = true);',
  24612. ' end;',
  24613. 'procedure THelper.Run(e: wordbool);',
  24614. 'var i: integer;',
  24615. 'begin',
  24616. ' i:=Integer(Self);',
  24617. 'end;',
  24618. 'var w: wordbool;',
  24619. 'begin',
  24620. ' w.Run;',
  24621. ' wordbool(3).Run;',
  24622. '']);
  24623. ConvertProgram;
  24624. CheckSource('TestTypeHelper_WordBool',
  24625. LinesToStr([ // statements
  24626. 'rtl.createHelper(this, "THelper", null, function () {',
  24627. ' this.Run = function (e) {',
  24628. ' var i = 0;',
  24629. ' i = (this.get() ? 1 : 0);',
  24630. ' };',
  24631. '});',
  24632. 'this.w = false;',
  24633. '']),
  24634. LinesToStr([ // $mod.$main
  24635. '$mod.THelper.Run.call({',
  24636. ' p: $mod,',
  24637. ' get: function () {',
  24638. ' return this.p.w;',
  24639. ' },',
  24640. ' set: function (v) {',
  24641. ' this.p.w = v;',
  24642. ' }',
  24643. '}, true);',
  24644. '$mod.THelper.Run.call({',
  24645. ' a: 3 != 0,',
  24646. ' get: function () {',
  24647. ' return this.a;',
  24648. ' },',
  24649. ' set: function (v) {',
  24650. ' rtl.raiseE("EPropReadOnly");',
  24651. ' }',
  24652. '}, true);',
  24653. '']));
  24654. end;
  24655. procedure TTestModule.TestTypeHelper_Double;
  24656. begin
  24657. StartProgram(false);
  24658. Add([
  24659. '{$modeswitch typehelpers}',
  24660. 'type',
  24661. ' Float = type double;',
  24662. ' THelper = type helper for Float',
  24663. ' const NPI = 3.141592;',
  24664. ' function ToStr: String;',
  24665. ' end;',
  24666. 'function THelper.ToStr: String;',
  24667. 'begin',
  24668. 'end;',
  24669. 'procedure DoIt(s: string);',
  24670. 'begin',
  24671. 'end;',
  24672. 'var f: Float;',
  24673. 'begin',
  24674. ' DoIt(f.toStr);',
  24675. ' DoIt(f.toStr());',
  24676. ' (f*f).toStr;',
  24677. ' DoIt((f*f).toStr);',
  24678. '']);
  24679. ConvertProgram;
  24680. CheckSource('TestTypeHelper_Double',
  24681. LinesToStr([ // statements
  24682. 'rtl.createHelper(this, "THelper", null, function () {',
  24683. ' this.NPI = 3.141592;',
  24684. ' this.ToStr = function () {',
  24685. ' var Result = "";',
  24686. ' return Result;',
  24687. ' };',
  24688. '});',
  24689. 'this.DoIt = function (s) {',
  24690. '};',
  24691. 'this.f = 0.0;',
  24692. '']),
  24693. LinesToStr([ // $mod.$main
  24694. '$mod.DoIt($mod.THelper.ToStr.call({',
  24695. ' p: $mod,',
  24696. ' get: function () {',
  24697. ' return this.p.f;',
  24698. ' },',
  24699. ' set: function (v) {',
  24700. ' this.p.f = v;',
  24701. ' }',
  24702. '}));',
  24703. '$mod.DoIt($mod.THelper.ToStr.call({',
  24704. ' p: $mod,',
  24705. ' get: function () {',
  24706. ' return this.p.f;',
  24707. ' },',
  24708. ' set: function (v) {',
  24709. ' this.p.f = v;',
  24710. ' }',
  24711. '}));',
  24712. '$mod.THelper.ToStr.call({',
  24713. ' a: $mod.f * $mod.f,',
  24714. ' get: function () {',
  24715. ' return this.a;',
  24716. ' },',
  24717. ' set: function (v) {',
  24718. ' rtl.raiseE("EPropReadOnly");',
  24719. ' }',
  24720. '});',
  24721. '$mod.DoIt($mod.THelper.ToStr.call({',
  24722. ' a: $mod.f * $mod.f,',
  24723. ' get: function () {',
  24724. ' return this.a;',
  24725. ' },',
  24726. ' set: function (v) {',
  24727. ' rtl.raiseE("EPropReadOnly");',
  24728. ' }',
  24729. '}));',
  24730. '']));
  24731. end;
  24732. procedure TTestModule.TestTypeHelper_NativeInt;
  24733. begin
  24734. StartProgram(false);
  24735. Add([
  24736. '{$modeswitch typehelpers}',
  24737. 'type',
  24738. ' MaxInt = type nativeint;',
  24739. ' THelperI = type helper for MaxInt',
  24740. ' function ToStr: String;',
  24741. ' end;',
  24742. ' MaxUInt = type nativeuint;',
  24743. ' THelperU = type helper for MaxUInt',
  24744. ' function ToStr: String;',
  24745. ' end;',
  24746. 'function THelperI.ToStr: String;',
  24747. 'begin',
  24748. ' Result:=str(Self);',
  24749. 'end;',
  24750. 'function THelperU.ToStr: String;',
  24751. 'begin',
  24752. ' Result:=str(Self);',
  24753. 'end;',
  24754. 'procedure DoIt(s: string);',
  24755. 'begin',
  24756. 'end;',
  24757. 'var i: MaxInt;',
  24758. 'begin',
  24759. ' DoIt(i.toStr);',
  24760. ' DoIt(i.toStr());',
  24761. ' (i*i).toStr;',
  24762. ' DoIt((i*i).toStr);',
  24763. '']);
  24764. ConvertProgram;
  24765. CheckSource('TestTypeHelper_NativeInt',
  24766. LinesToStr([ // statements
  24767. 'rtl.createHelper(this, "THelperI", null, function () {',
  24768. ' this.ToStr = function () {',
  24769. ' var Result = "";',
  24770. ' Result = "" + this.get();',
  24771. ' return Result;',
  24772. ' };',
  24773. '});',
  24774. 'rtl.createHelper(this, "THelperU", null, function () {',
  24775. ' this.ToStr = function () {',
  24776. ' var Result = "";',
  24777. ' Result = "" + this.get();',
  24778. ' return Result;',
  24779. ' };',
  24780. '});',
  24781. 'this.DoIt = function (s) {',
  24782. '};',
  24783. 'this.i = 0;',
  24784. '']),
  24785. LinesToStr([ // $mod.$main
  24786. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24787. ' p: $mod,',
  24788. ' get: function () {',
  24789. ' return this.p.i;',
  24790. ' },',
  24791. ' set: function (v) {',
  24792. ' this.p.i = v;',
  24793. ' }',
  24794. '}));',
  24795. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24796. ' p: $mod,',
  24797. ' get: function () {',
  24798. ' return this.p.i;',
  24799. ' },',
  24800. ' set: function (v) {',
  24801. ' this.p.i = v;',
  24802. ' }',
  24803. '}));',
  24804. '$mod.THelperI.ToStr.call({',
  24805. ' a: $mod.i * $mod.i,',
  24806. ' get: function () {',
  24807. ' return this.a;',
  24808. ' },',
  24809. ' set: function (v) {',
  24810. ' rtl.raiseE("EPropReadOnly");',
  24811. ' }',
  24812. '});',
  24813. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24814. ' a: $mod.i * $mod.i,',
  24815. ' get: function () {',
  24816. ' return this.a;',
  24817. ' },',
  24818. ' set: function (v) {',
  24819. ' rtl.raiseE("EPropReadOnly");',
  24820. ' }',
  24821. '}));',
  24822. '']));
  24823. end;
  24824. procedure TTestModule.TestTypeHelper_StringChar;
  24825. begin
  24826. StartProgram(false);
  24827. Add([
  24828. '{$modeswitch typehelpers}',
  24829. 'type',
  24830. ' TStringHelper = type helper for string',
  24831. ' procedure DoIt(e: byte = 123);',
  24832. ' end;',
  24833. ' TCharHelper = type helper for char',
  24834. ' procedure Fly;',
  24835. ' end;',
  24836. 'procedure TStringHelper.DoIt(e: byte);',
  24837. 'begin',
  24838. ' Self[1]:=''c'';',
  24839. ' Self[2]:=Self[3];',
  24840. 'end;',
  24841. 'procedure TCharHelper.Fly;',
  24842. 'begin',
  24843. ' Self:=''c'';',
  24844. 'end;',
  24845. 'begin',
  24846. ' ''abc''.DoIt;',
  24847. ' ''xyz''.DoIt();',
  24848. ' ''c''.Fly();',
  24849. '']);
  24850. ConvertProgram;
  24851. CheckSource('TestTypeHelper_StringChar',
  24852. LinesToStr([ // statements
  24853. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24854. ' this.DoIt = function (e) {',
  24855. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24856. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24857. ' };',
  24858. '});',
  24859. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24860. ' this.Fly = function () {',
  24861. ' this.set("c");',
  24862. ' };',
  24863. '});',
  24864. '']),
  24865. LinesToStr([ // $mod.$main
  24866. '$mod.TStringHelper.DoIt.call({',
  24867. ' get: function () {',
  24868. ' return "abc";',
  24869. ' },',
  24870. ' set: function (v) {',
  24871. ' rtl.raiseE("EPropReadOnly");',
  24872. ' }',
  24873. '}, 123);',
  24874. '$mod.TStringHelper.DoIt.call({',
  24875. ' get: function () {',
  24876. ' return "xyz";',
  24877. ' },',
  24878. ' set: function (v) {',
  24879. ' rtl.raiseE("EPropReadOnly");',
  24880. ' }',
  24881. '}, 123);',
  24882. '$mod.TCharHelper.Fly.call({',
  24883. ' get: function () {',
  24884. ' return "c";',
  24885. ' },',
  24886. ' set: function (v) {',
  24887. ' rtl.raiseE("EPropReadOnly");',
  24888. ' }',
  24889. '});',
  24890. '']));
  24891. end;
  24892. procedure TTestModule.TestTypeHelper_JSValue;
  24893. begin
  24894. StartProgram(false);
  24895. Add([
  24896. '{$modeswitch typehelpers}',
  24897. 'type',
  24898. ' TExtValue = type jsvalue;',
  24899. ' THelper = type helper for TExtValue',
  24900. ' function ToStr: String;',
  24901. ' end;',
  24902. 'function THelper.ToStr: String;',
  24903. 'begin',
  24904. 'end;',
  24905. 'var',
  24906. ' s: string;',
  24907. ' v: TExtValue;',
  24908. 'begin',
  24909. ' s:=v.toStr;',
  24910. ' s:=v.toStr();',
  24911. ' TExtValue(s).toStr;',
  24912. '']);
  24913. ConvertProgram;
  24914. CheckSource('TestTypeHelper_JSValue',
  24915. LinesToStr([ // statements
  24916. 'rtl.createHelper(this, "THelper", null, function () {',
  24917. ' this.ToStr = function () {',
  24918. ' var Result = "";',
  24919. ' return Result;',
  24920. ' };',
  24921. '});',
  24922. 'this.s = "";',
  24923. 'this.v = undefined;',
  24924. '']),
  24925. LinesToStr([ // $mod.$main
  24926. '$mod.s = $mod.THelper.ToStr.call({',
  24927. ' p: $mod,',
  24928. ' get: function () {',
  24929. ' return this.p.v;',
  24930. ' },',
  24931. ' set: function (v) {',
  24932. ' this.p.v = v;',
  24933. ' }',
  24934. '});',
  24935. '$mod.s = $mod.THelper.ToStr.call({',
  24936. ' p: $mod,',
  24937. ' get: function () {',
  24938. ' return this.p.v;',
  24939. ' },',
  24940. ' set: function (v) {',
  24941. ' this.p.v = v;',
  24942. ' }',
  24943. '});',
  24944. '$mod.THelper.ToStr.call({',
  24945. ' p: $mod,',
  24946. ' get: function () {',
  24947. ' return this.p.s;',
  24948. ' },',
  24949. ' set: function (v) {',
  24950. ' rtl.raiseE("EPropReadOnly");',
  24951. ' }',
  24952. '});',
  24953. '']));
  24954. end;
  24955. procedure TTestModule.TestTypeHelper_Array;
  24956. begin
  24957. StartProgram(false);
  24958. Add([
  24959. '{$modeswitch typehelpers}',
  24960. 'type',
  24961. ' TArrOfBool = array of boolean;',
  24962. ' TArrOfJS = array of jsvalue;',
  24963. ' THelper = type helper for TArrOfBool',
  24964. ' procedure DoIt(e: byte = 123);',
  24965. ' end;',
  24966. 'procedure THelper.DoIt(e: byte);',
  24967. 'begin',
  24968. ' Self[1]:=true;',
  24969. ' Self[2]:=not Self[3];',
  24970. ' SetLength(Self,4);',
  24971. 'end;',
  24972. 'var',
  24973. ' b: TArrOfBool;',
  24974. ' j: TArrOfJS;',
  24975. 'begin',
  24976. ' b.DoIt;',
  24977. ' TArrOfBool(j).DoIt();',
  24978. '']);
  24979. ConvertProgram;
  24980. CheckSource('TestTypeHelper_Array',
  24981. LinesToStr([ // statements
  24982. 'rtl.createHelper(this, "THelper", null, function () {',
  24983. ' this.DoIt = function (e) {',
  24984. ' this.get()[1] = true;',
  24985. ' this.get()[2] = !this.get()[3];',
  24986. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24987. ' };',
  24988. '});',
  24989. 'this.b = [];',
  24990. 'this.j = [];',
  24991. '']),
  24992. LinesToStr([ // $mod.$main
  24993. '$mod.THelper.DoIt.call({',
  24994. ' p: $mod,',
  24995. ' get: function () {',
  24996. ' return this.p.b;',
  24997. ' },',
  24998. ' set: function (v) {',
  24999. ' this.p.b = v;',
  25000. ' }',
  25001. '}, 123);',
  25002. '$mod.THelper.DoIt.call({',
  25003. ' p: $mod,',
  25004. ' get: function () {',
  25005. ' return this.p.j;',
  25006. ' },',
  25007. ' set: function (v) {',
  25008. ' this.p.j = v;',
  25009. ' }',
  25010. '}, 123);',
  25011. '']));
  25012. end;
  25013. procedure TTestModule.TestTypeHelper_EnumType;
  25014. begin
  25015. StartProgram(false);
  25016. Add([
  25017. '{$modeswitch typehelpers}',
  25018. 'type',
  25019. ' TEnum = (red,blue);',
  25020. ' THelper = type helper for TEnum',
  25021. ' procedure DoIt(e: byte = 123);',
  25022. ' class procedure Swing(w: word); static;',
  25023. ' end;',
  25024. 'procedure THelper.DoIt(e: byte);',
  25025. 'begin',
  25026. ' Self:=red;',
  25027. ' Self:=succ(Self);',
  25028. ' with Self do Doit;',
  25029. 'end;',
  25030. 'class procedure THelper.Swing(w: word);',
  25031. 'begin',
  25032. 'end;',
  25033. 'var e: TEnum;',
  25034. 'begin',
  25035. ' e.DoIt;',
  25036. ' red.DoIt;',
  25037. ' TEnum.blue.DoIt;',
  25038. ' TEnum(1).DoIt;',
  25039. ' TEnum.Swing(3);',
  25040. '']);
  25041. ConvertProgram;
  25042. CheckSource('TestTypeHelper_EnumType',
  25043. LinesToStr([ // statements
  25044. 'this.TEnum = {',
  25045. ' "0": "red",',
  25046. ' red: 0,',
  25047. ' "1": "blue",',
  25048. ' blue: 1',
  25049. '};',
  25050. 'rtl.createHelper(this, "THelper", null, function () {',
  25051. ' this.DoIt = function (e) {',
  25052. ' this.set($mod.TEnum.red);',
  25053. ' this.set(this.get() + 1);',
  25054. ' var $with = this.get();',
  25055. ' $mod.THelper.DoIt.call(this, 123);',
  25056. ' };',
  25057. ' this.Swing = function (w) {',
  25058. ' };',
  25059. '});',
  25060. 'this.e = 0;',
  25061. '']),
  25062. LinesToStr([ // $mod.$main
  25063. '$mod.THelper.DoIt.call({',
  25064. ' p: $mod,',
  25065. ' get: function () {',
  25066. ' return this.p.e;',
  25067. ' },',
  25068. ' set: function (v) {',
  25069. ' this.p.e = v;',
  25070. ' }',
  25071. '}, 123);',
  25072. '$mod.THelper.DoIt.call({',
  25073. ' p: $mod.TEnum,',
  25074. ' get: function () {',
  25075. ' return this.p.red;',
  25076. ' },',
  25077. ' set: function (v) {',
  25078. ' rtl.raiseE("EPropReadOnly");',
  25079. ' }',
  25080. '}, 123);',
  25081. '$mod.THelper.DoIt.call({',
  25082. ' p: $mod.TEnum,',
  25083. ' get: function () {',
  25084. ' return this.p.blue;',
  25085. ' },',
  25086. ' set: function (v) {',
  25087. ' rtl.raiseE("EPropReadOnly");',
  25088. ' }',
  25089. '}, 123);',
  25090. '$mod.THelper.DoIt.call({',
  25091. ' get: function () {',
  25092. ' return 1;',
  25093. ' },',
  25094. ' set: function (v) {',
  25095. ' rtl.raiseE("EPropReadOnly");',
  25096. ' }',
  25097. '}, 123);',
  25098. '$mod.THelper.Swing(3);',
  25099. '']));
  25100. end;
  25101. procedure TTestModule.TestTypeHelper_SetType;
  25102. begin
  25103. StartProgram(false);
  25104. Add([
  25105. '{$modeswitch typehelpers}',
  25106. 'type',
  25107. ' TEnum = (red,blue);',
  25108. ' TSetOfEnum = set of TEnum;',
  25109. ' THelper = type helper for TSetOfEnum',
  25110. ' procedure DoIt(e: byte = 123);',
  25111. ' constructor Init(e: TEnum);',
  25112. ' constructor InitEmpty;',
  25113. ' end;',
  25114. 'procedure THelper.DoIt(e: byte);',
  25115. 'begin',
  25116. ' Self:=[];',
  25117. ' Self:=[red];',
  25118. ' Include(Self,blue);',
  25119. 'end;',
  25120. 'constructor THelper.Init(e: TEnum);',
  25121. 'begin',
  25122. ' Self:=[];',
  25123. ' Self:=[e];',
  25124. ' Include(Self,blue);',
  25125. 'end;',
  25126. 'constructor THelper.InitEmpty;',
  25127. 'begin',
  25128. 'end;',
  25129. 'var s: TSetOfEnum;',
  25130. 'begin',
  25131. ' s.DoIt;',
  25132. //' [red].DoIt;',
  25133. //' with s do DoIt;',
  25134. //' with [red,blue] do DoIt;',
  25135. ' s:=TSetOfEnum.Init(blue);',
  25136. ' s:=s.Init(blue);',
  25137. '']);
  25138. ConvertProgram;
  25139. CheckSource('TestTypeHelper_SetType',
  25140. LinesToStr([ // statements
  25141. 'this.TEnum = {',
  25142. ' "0": "red",',
  25143. ' red: 0,',
  25144. ' "1": "blue",',
  25145. ' blue: 1',
  25146. '};',
  25147. 'rtl.createHelper(this, "THelper", null, function () {',
  25148. ' this.DoIt = function (e) {',
  25149. ' this.set({});',
  25150. ' this.set(rtl.createSet($mod.TEnum.red));',
  25151. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25152. ' };',
  25153. ' this.Init = function (e) {',
  25154. ' this.set({});',
  25155. ' this.set(rtl.createSet(e));',
  25156. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25157. ' return this.get();',
  25158. ' };',
  25159. ' this.InitEmpty = function () {',
  25160. ' return this.get();',
  25161. ' };',
  25162. ' this.$new = function (fn, args) {',
  25163. ' return this[fn].apply({',
  25164. ' p: {},',
  25165. ' get: function () {',
  25166. ' return this.p;',
  25167. ' },',
  25168. ' set: function (v) {',
  25169. ' this.p = v;',
  25170. ' }',
  25171. ' }, args);',
  25172. ' };',
  25173. '});',
  25174. 'this.s = {};',
  25175. '']),
  25176. LinesToStr([ // $mod.$main
  25177. '$mod.THelper.DoIt.call({',
  25178. ' p: $mod,',
  25179. ' get: function () {',
  25180. ' return this.p.s;',
  25181. ' },',
  25182. ' set: function (v) {',
  25183. ' this.p.s = v;',
  25184. ' }',
  25185. '}, 123);',
  25186. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25187. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25188. ' p: $mod,',
  25189. ' get: function () {',
  25190. ' return this.p.s;',
  25191. ' },',
  25192. ' set: function (v) {',
  25193. ' this.p.s = v;',
  25194. ' }',
  25195. '}, $mod.TEnum.blue));',
  25196. '']));
  25197. end;
  25198. procedure TTestModule.TestTypeHelper_InterfaceType;
  25199. begin
  25200. StartProgram(false);
  25201. Add([
  25202. '{$interfaces com}',
  25203. '{$modeswitch typehelpers}',
  25204. 'type',
  25205. ' IUnknown = interface',
  25206. ' function _AddRef: longint;',
  25207. ' function _Release: longint;',
  25208. ' end;',
  25209. ' TObject = class(IUnknown)',
  25210. ' function _AddRef: longint; virtual; abstract;',
  25211. ' function _Release: longint; virtual; abstract;',
  25212. ' end;',
  25213. ' THelper = type helper for IUnknown',
  25214. ' procedure Fly(e: byte = 123);',
  25215. ' class procedure Run; static;',
  25216. ' end;',
  25217. 'var',
  25218. ' i: IUnknown;',
  25219. ' o: TObject;',
  25220. 'procedure THelper.Fly(e: byte);',
  25221. 'begin',
  25222. ' i:=Self;',
  25223. ' o:=Self as TObject;',
  25224. ' Self:=nil;',
  25225. ' Self:=i;',
  25226. ' Self:=o;',
  25227. ' with Self do begin',
  25228. ' Fly;',
  25229. ' Fly();',
  25230. ' end;',
  25231. 'end;',
  25232. 'class procedure THelper.Run;',
  25233. 'var l: IUnknown;',
  25234. 'begin',
  25235. ' l.Fly;',
  25236. ' l.Fly();',
  25237. 'end;',
  25238. 'begin',
  25239. ' i.Fly;',
  25240. ' i.Fly();',
  25241. ' i.Run;',
  25242. ' i.Run();',
  25243. ' IUnknown.Run;',
  25244. ' IUnknown.Run();',
  25245. '']);
  25246. ConvertProgram;
  25247. CheckSource('TestTypeHelper_InterfaceType',
  25248. LinesToStr([ // statements
  25249. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25250. 'rtl.createClass(this, "TObject", null, function () {',
  25251. ' this.$init = function () {',
  25252. ' };',
  25253. ' this.$final = function () {',
  25254. ' };',
  25255. ' rtl.addIntf(this, $mod.IUnknown);',
  25256. '});',
  25257. 'rtl.createHelper(this, "THelper", null, function () {',
  25258. ' this.Fly = function (e) {',
  25259. ' var $ir = rtl.createIntfRefs();',
  25260. ' try {',
  25261. ' rtl.setIntfP($mod, "i", this.get());',
  25262. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25263. ' this.set(null);',
  25264. ' this.set($mod.i);',
  25265. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25266. ' var $with = this.get();',
  25267. ' $mod.THelper.Fly.call(this, 123);',
  25268. ' $mod.THelper.Fly.call(this, 123);',
  25269. ' } finally {',
  25270. ' $ir.free();',
  25271. ' };',
  25272. ' };',
  25273. ' this.Run = function () {',
  25274. ' var l = null;',
  25275. ' try {',
  25276. ' $mod.THelper.Fly.call({',
  25277. ' get: function () {',
  25278. ' return l;',
  25279. ' },',
  25280. ' set: function (v) {',
  25281. ' l = rtl.setIntfL(l, v);',
  25282. ' }',
  25283. ' }, 123);',
  25284. ' $mod.THelper.Fly.call({',
  25285. ' get: function () {',
  25286. ' return l;',
  25287. ' },',
  25288. ' set: function (v) {',
  25289. ' l = rtl.setIntfL(l, v);',
  25290. ' }',
  25291. ' }, 123);',
  25292. ' } finally {',
  25293. ' rtl._Release(l);',
  25294. ' };',
  25295. ' };',
  25296. '});',
  25297. 'this.i = null;',
  25298. 'this.o = null;',
  25299. '']),
  25300. LinesToStr([ // $mod.$main
  25301. '$mod.THelper.Fly.call({',
  25302. ' p: $mod,',
  25303. ' get: function () {',
  25304. ' return this.p.i;',
  25305. ' },',
  25306. ' set: function (v) {',
  25307. ' rtl.setIntfP(this.p, "i", v);',
  25308. ' }',
  25309. '}, 123);',
  25310. '$mod.THelper.Fly.call({',
  25311. ' p: $mod,',
  25312. ' get: function () {',
  25313. ' return this.p.i;',
  25314. ' },',
  25315. ' set: function (v) {',
  25316. ' rtl.setIntfP(this.p, "i", v);',
  25317. ' }',
  25318. '}, 123);',
  25319. '$mod.THelper.Run();',
  25320. '$mod.THelper.Run();',
  25321. '$mod.THelper.Run();',
  25322. '$mod.THelper.Run();',
  25323. '']));
  25324. end;
  25325. procedure TTestModule.TestTypeHelper_NestedSelf;
  25326. begin
  25327. StartProgram(false);
  25328. Add([
  25329. '{$modeswitch typehelpers}',
  25330. 'type',
  25331. ' THelper = type helper for string',
  25332. ' procedure Run(Value: string);',
  25333. ' end;',
  25334. 'procedure THelper.Run(Value: string);',
  25335. ' function Sub(i: nativeint): boolean;',
  25336. ' begin',
  25337. ' Result:=Self[i+1]=Value[i];',
  25338. ' end;',
  25339. 'begin',
  25340. ' if Self[3]=Value[4] then ;',
  25341. 'end;',
  25342. 'begin',
  25343. '']);
  25344. ConvertProgram;
  25345. CheckSource('TestTypeHelper_NestedSelf',
  25346. LinesToStr([ // statements
  25347. 'rtl.createHelper(this, "THelper", null, function () {',
  25348. ' this.Run = function (Value) {',
  25349. ' var $Self = this;',
  25350. ' function Sub(i) {',
  25351. ' var Result = false;',
  25352. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25353. ' return Result;',
  25354. ' };',
  25355. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25356. ' };',
  25357. '});',
  25358. '']),
  25359. LinesToStr([ // $mod.$main
  25360. '']));
  25361. end;
  25362. procedure TTestModule.TestProcType;
  25363. begin
  25364. StartProgram(false);
  25365. Add([
  25366. 'type',
  25367. ' TProcInt = procedure(vI: longint = 1);',
  25368. 'procedure DoIt(vJ: longint);',
  25369. 'begin end;',
  25370. 'var',
  25371. ' b: boolean;',
  25372. ' vP, vQ: tprocint;',
  25373. 'begin',
  25374. ' vp:=nil;',
  25375. ' vp:=vp;',
  25376. ' vp:=@doit;',
  25377. ' vp;',
  25378. ' vp();',
  25379. ' vp(2);',
  25380. ' b:=vp=nil;',
  25381. ' b:=nil=vp;',
  25382. ' b:=vp=vq;',
  25383. ' b:=vp=@doit;',
  25384. ' b:=@doit=vp;',
  25385. ' b:=vp<>nil;',
  25386. ' b:=nil<>vp;',
  25387. ' b:=vp<>vq;',
  25388. ' b:=vp<>@doit;',
  25389. ' b:=@doit<>vp;',
  25390. ' b:=Assigned(vp);',
  25391. ' if Assigned(vp) then ;']);
  25392. ConvertProgram;
  25393. CheckSource('TestProcType',
  25394. LinesToStr([ // statements
  25395. 'this.DoIt = function(vJ) {',
  25396. '};',
  25397. 'this.b = false;',
  25398. 'this.vP = null;',
  25399. 'this.vQ = null;'
  25400. ]),
  25401. LinesToStr([ // $mod.$main
  25402. '$mod.vP = null;',
  25403. '$mod.vP = $mod.vP;',
  25404. '$mod.vP = $mod.DoIt;',
  25405. '$mod.vP(1);',
  25406. '$mod.vP(1);',
  25407. '$mod.vP(2);',
  25408. '$mod.b = $mod.vP === null;',
  25409. '$mod.b = null === $mod.vP;',
  25410. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25411. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25412. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25413. '$mod.b = $mod.vP !== null;',
  25414. '$mod.b = null !== $mod.vP;',
  25415. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25416. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25417. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25418. '$mod.b = $mod.vP != null;',
  25419. 'if ($mod.vP != null) ;',
  25420. '']));
  25421. end;
  25422. procedure TTestModule.TestProcType_Arg;
  25423. begin
  25424. StartProgram(false);
  25425. Add([
  25426. 'type',
  25427. ' TProcInt = procedure(vI: longint = 1);',
  25428. 'procedure DoIt(vJ: longint); begin end;',
  25429. 'procedure DoSome(vP, vQ: TProcInt);',
  25430. 'var',
  25431. ' b: boolean;',
  25432. 'begin',
  25433. ' vp:=nil;',
  25434. ' vp:=vp;',
  25435. ' vp:=@doit;',
  25436. ' vp;',
  25437. ' vp();',
  25438. ' vp(2);',
  25439. ' b:=vp=nil;',
  25440. ' b:=nil=vp;',
  25441. ' b:=vp=vq;',
  25442. ' b:=vp=@doit;',
  25443. ' b:=@doit=vp;',
  25444. ' b:=vp<>nil;',
  25445. ' b:=nil<>vp;',
  25446. ' b:=vp<>vq;',
  25447. ' b:=vp<>@doit;',
  25448. ' b:=@doit<>vp;',
  25449. ' b:=Assigned(vp);',
  25450. ' if Assigned(vp) then ;',
  25451. 'end;',
  25452. 'begin',
  25453. ' DoSome(@DoIt,nil);']);
  25454. ConvertProgram;
  25455. CheckSource('TestProcType_Arg',
  25456. LinesToStr([ // statements
  25457. 'this.DoIt = function(vJ) {',
  25458. '};',
  25459. 'this.DoSome = function(vP, vQ) {',
  25460. ' var b = false;',
  25461. ' vP = null;',
  25462. ' vP = vP;',
  25463. ' vP = $mod.DoIt;',
  25464. ' vP(1);',
  25465. ' vP(1);',
  25466. ' vP(2);',
  25467. ' b = vP === null;',
  25468. ' b = null === vP;',
  25469. ' b = rtl.eqCallback(vP,vQ);',
  25470. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25471. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25472. ' b = vP !== null;',
  25473. ' b = null !== vP;',
  25474. ' b = !rtl.eqCallback(vP, vQ);',
  25475. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25476. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25477. ' b = vP != null;',
  25478. ' if (vP != null) ;',
  25479. '};',
  25480. '']),
  25481. LinesToStr([ // $mod.$main
  25482. '$mod.DoSome($mod.DoIt,null);',
  25483. '']));
  25484. end;
  25485. procedure TTestModule.TestProcType_FunctionFPC;
  25486. begin
  25487. StartProgram(false);
  25488. Add('type');
  25489. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25490. Add('function DoIt(vI: longint): longint;');
  25491. Add('begin end;');
  25492. Add('var');
  25493. Add(' b: boolean;');
  25494. Add(' vP, vQ: tfuncint;');
  25495. Add('begin');
  25496. Add(' vp:=nil;');
  25497. Add(' vp:=vp;');
  25498. Add(' vp:=@doit;'); // ok in fpc and delphi
  25499. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25500. Add(' vp;'); // ok in fpc and delphi
  25501. Add(' vp();');
  25502. Add(' vp(2);');
  25503. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25504. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25505. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25506. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25507. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25508. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25509. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25510. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25511. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25512. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25513. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25514. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25515. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25516. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25517. Add(' b:=Assigned(vp);');
  25518. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25519. Add(' doit(vp());'); // ok in fpc and delphi
  25520. Add(' doit(vp(2));'); // ok in fpc and delphi
  25521. ConvertProgram;
  25522. CheckSource('TestProcType_FunctionFPC',
  25523. LinesToStr([ // statements
  25524. 'this.DoIt = function(vI) {',
  25525. ' var Result = 0;',
  25526. ' return Result;',
  25527. '};',
  25528. 'this.b = false;',
  25529. 'this.vP = null;',
  25530. 'this.vQ = null;'
  25531. ]),
  25532. LinesToStr([ // $mod.$main
  25533. '$mod.vP = null;',
  25534. '$mod.vP = $mod.vP;',
  25535. '$mod.vP = $mod.DoIt;',
  25536. '$mod.vP(1);',
  25537. '$mod.vP(1);',
  25538. '$mod.vP(2);',
  25539. '$mod.b = $mod.vP === null;',
  25540. '$mod.b = null === $mod.vP;',
  25541. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25542. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25543. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25544. '$mod.b = 4 === $mod.vP(1);',
  25545. '$mod.b = $mod.vP !== null;',
  25546. '$mod.b = null !== $mod.vP;',
  25547. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25548. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25549. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25550. '$mod.b = 6 !== $mod.vP(1);',
  25551. '$mod.b = $mod.vP != null;',
  25552. '$mod.DoIt($mod.vP(1));',
  25553. '$mod.DoIt($mod.vP(2));',
  25554. '']));
  25555. end;
  25556. procedure TTestModule.TestProcType_FunctionDelphi;
  25557. begin
  25558. StartProgram(false);
  25559. Add('{$mode Delphi}');
  25560. Add('type');
  25561. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25562. Add('function DoIt(vI: longint): longint;');
  25563. Add('begin end;');
  25564. Add('var');
  25565. Add(' b: boolean;');
  25566. Add(' vP, vQ: tfuncint;');
  25567. Add('begin');
  25568. Add(' vp:=nil;');
  25569. Add(' vp:=vp;');
  25570. Add(' vp:=@doit;'); // ok in fpc and delphi
  25571. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25572. Add(' vp;'); // ok in fpc and delphi
  25573. Add(' vp();');
  25574. Add(' vp(2);');
  25575. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25576. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25577. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25578. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25579. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25580. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25581. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25582. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25583. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25584. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25585. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25586. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25587. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25588. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25589. Add(' b:=Assigned(vp);');
  25590. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25591. Add(' doit(vp());'); // ok in fpc and delphi
  25592. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25593. ConvertProgram;
  25594. CheckSource('TestProcType_FunctionDelphi',
  25595. LinesToStr([ // statements
  25596. 'this.DoIt = function(vI) {',
  25597. ' var Result = 0;',
  25598. ' return Result;',
  25599. '};',
  25600. 'this.b = false;',
  25601. 'this.vP = null;',
  25602. 'this.vQ = null;'
  25603. ]),
  25604. LinesToStr([ // $mod.$main
  25605. '$mod.vP = null;',
  25606. '$mod.vP = $mod.vP;',
  25607. '$mod.vP = $mod.DoIt;',
  25608. '$mod.vP = $mod.DoIt;',
  25609. '$mod.vP(1);',
  25610. '$mod.vP(1);',
  25611. '$mod.vP(2);',
  25612. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25613. '$mod.b = $mod.vP(1) === 3;',
  25614. '$mod.b = 4 === $mod.vP(1);',
  25615. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25616. '$mod.b = $mod.vP(1) !== 5;',
  25617. '$mod.b = 6 !== $mod.vP(1);',
  25618. '$mod.b = $mod.vP != null;',
  25619. '$mod.DoIt($mod.vP(1));',
  25620. '$mod.DoIt($mod.vP(1));',
  25621. '$mod.DoIt($mod.vP(2));',
  25622. '']));
  25623. end;
  25624. procedure TTestModule.TestProcType_ProcedureDelphi;
  25625. begin
  25626. StartProgram(false);
  25627. Add('{$mode Delphi}');
  25628. Add('type');
  25629. Add(' TProc = procedure;');
  25630. Add('procedure DoIt;');
  25631. Add('begin end;');
  25632. Add('var');
  25633. Add(' b: boolean;');
  25634. Add(' vP, vQ: tproc;');
  25635. Add('begin');
  25636. Add(' vp:=nil;');
  25637. Add(' vp:=vp;');
  25638. Add(' vp:=vq;');
  25639. 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
  25640. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25641. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25642. Add(' vp;'); // ok in fpc and delphi
  25643. Add(' vp();');
  25644. // equal
  25645. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25646. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25647. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25648. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25649. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25650. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25651. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25652. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25653. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25654. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25655. // unequal
  25656. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25657. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25658. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25659. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25660. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25661. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25662. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25663. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25664. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25665. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25666. Add(' b:=Assigned(vp);');
  25667. ConvertProgram;
  25668. CheckSource('TestProcType_ProcedureDelphi',
  25669. LinesToStr([ // statements
  25670. 'this.DoIt = function() {',
  25671. '};',
  25672. 'this.b = false;',
  25673. 'this.vP = null;',
  25674. 'this.vQ = null;'
  25675. ]),
  25676. LinesToStr([ // $mod.$main
  25677. '$mod.vP = null;',
  25678. '$mod.vP = $mod.vP;',
  25679. '$mod.vP = $mod.vQ;',
  25680. '$mod.vP = $mod.DoIt;',
  25681. '$mod.vP = $mod.DoIt;',
  25682. '$mod.vP();',
  25683. '$mod.vP();',
  25684. '$mod.b = $mod.vP === null;',
  25685. '$mod.b = null === $mod.vP;',
  25686. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25687. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25688. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25689. '$mod.b = $mod.vP !== null;',
  25690. '$mod.b = null !== $mod.vP;',
  25691. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25692. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25693. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25694. '$mod.b = $mod.vP != null;',
  25695. '']));
  25696. end;
  25697. procedure TTestModule.TestProcType_AsParam;
  25698. begin
  25699. StartProgram(false);
  25700. Add('type');
  25701. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25702. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25703. Add('var vJ: tfuncint;');
  25704. Add('begin');
  25705. Add(' vg:=vg;');
  25706. Add(' vj:=vh;');
  25707. Add(' vi:=vi;');
  25708. Add(' doit(vg,vg,vg);');
  25709. Add(' doit(vh,vh,vj);');
  25710. Add(' doit(vi,vi,vi);');
  25711. Add(' doit(vj,vj,vj);');
  25712. Add('end;');
  25713. Add('var i: tfuncint;');
  25714. Add('begin');
  25715. Add(' doit(i,i,i);');
  25716. ConvertProgram;
  25717. CheckSource('TestProcType_AsParam',
  25718. LinesToStr([ // statements
  25719. 'this.DoIt = function (vG,vH,vI) {',
  25720. ' var vJ = null;',
  25721. ' vG = vG;',
  25722. ' vJ = vH;',
  25723. ' vI.set(vI.get());',
  25724. ' $mod.DoIt(vG, vG, {',
  25725. ' get: function () {',
  25726. ' return vG;',
  25727. ' },',
  25728. ' set: function (v) {',
  25729. ' vG = v;',
  25730. ' }',
  25731. ' });',
  25732. ' $mod.DoIt(vH, vH, {',
  25733. ' get: function () {',
  25734. ' return vJ;',
  25735. ' },',
  25736. ' set: function (v) {',
  25737. ' vJ = v;',
  25738. ' }',
  25739. ' });',
  25740. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25741. ' $mod.DoIt(vJ, vJ, {',
  25742. ' get: function () {',
  25743. ' return vJ;',
  25744. ' },',
  25745. ' set: function (v) {',
  25746. ' vJ = v;',
  25747. ' }',
  25748. ' });',
  25749. '};',
  25750. 'this.i = null;'
  25751. ]),
  25752. LinesToStr([
  25753. '$mod.DoIt($mod.i,$mod.i,{',
  25754. ' p: $mod,',
  25755. ' get: function () {',
  25756. ' return this.p.i;',
  25757. ' },',
  25758. ' set: function (v) {',
  25759. ' this.p.i = v;',
  25760. ' }',
  25761. '});'
  25762. ]));
  25763. end;
  25764. procedure TTestModule.TestProcType_MethodFPC;
  25765. begin
  25766. StartProgram(false);
  25767. Add('type');
  25768. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25769. Add(' TObject = class');
  25770. Add(' function DoIt(vA: longint = 1): longint;');
  25771. Add(' end;');
  25772. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25773. Add('begin');
  25774. Add('end;');
  25775. Add('var');
  25776. Add(' Obj: TObject;');
  25777. Add(' vP: tfuncint;');
  25778. Add(' b: boolean;');
  25779. Add('begin');
  25780. Add(' vp:[email protected];'); // ok in fpc and delphi
  25781. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25782. Add(' vp;'); // ok in fpc and delphi
  25783. Add(' vp();');
  25784. Add(' vp(2);');
  25785. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25786. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25787. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25788. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25789. ConvertProgram;
  25790. CheckSource('TestProcType_MethodFPC',
  25791. LinesToStr([ // statements
  25792. 'rtl.createClass(this, "TObject", null, function () {',
  25793. ' this.$init = function () {',
  25794. ' };',
  25795. ' this.$final = function () {',
  25796. ' };',
  25797. ' this.DoIt = function (vA) {',
  25798. ' var Result = 0;',
  25799. ' return Result;',
  25800. ' };',
  25801. '});',
  25802. 'this.Obj = null;',
  25803. 'this.vP = null;',
  25804. 'this.b = false;'
  25805. ]),
  25806. LinesToStr([
  25807. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25808. '$mod.vP(1);',
  25809. '$mod.vP(1);',
  25810. '$mod.vP(2);',
  25811. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25812. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25813. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25814. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25815. '']));
  25816. end;
  25817. procedure TTestModule.TestProcType_MethodDelphi;
  25818. begin
  25819. StartProgram(false);
  25820. Add([
  25821. '{$mode delphi}',
  25822. 'type',
  25823. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25824. ' TObject = class',
  25825. ' function DoIt(vA: longint = 1): longint;',
  25826. ' end;',
  25827. 'function TObject.DoIt(vA: longint = 1): longint;',
  25828. 'begin',
  25829. 'end;',
  25830. 'var',
  25831. ' Obj: TObject;',
  25832. ' vP: tfuncint;',
  25833. ' b: boolean;',
  25834. 'begin',
  25835. ' vp:[email protected];', // ok in fpc and delphi
  25836. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25837. ' vp;', // ok in fpc and delphi
  25838. ' vp();',
  25839. ' vp(2);',
  25840. //' b:[email protected];', // ok in fpc, illegal in delphi
  25841. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25842. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25843. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25844. '']);
  25845. ConvertProgram;
  25846. CheckSource('TestProcType_MethodDelphi',
  25847. LinesToStr([ // statements
  25848. 'rtl.createClass(this, "TObject", null, function () {',
  25849. ' this.$init = function () {',
  25850. ' };',
  25851. ' this.$final = function () {',
  25852. ' };',
  25853. ' this.DoIt = function (vA) {',
  25854. ' var Result = 0;',
  25855. ' return Result;',
  25856. ' };',
  25857. '});',
  25858. 'this.Obj = null;',
  25859. 'this.vP = null;',
  25860. 'this.b = false;'
  25861. ]),
  25862. LinesToStr([
  25863. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25864. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25865. '$mod.vP(1);',
  25866. '$mod.vP(1);',
  25867. '$mod.vP(2);',
  25868. '']));
  25869. end;
  25870. procedure TTestModule.TestProcType_PropertyFPC;
  25871. begin
  25872. StartProgram(false);
  25873. Add('type');
  25874. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25875. Add(' TObject = class');
  25876. Add(' FOnFoo: TFuncInt;');
  25877. Add(' function DoIt(vA: longint = 1): longint;');
  25878. Add(' function GetFoo: TFuncInt;');
  25879. Add(' procedure SetFoo(const Value: TFuncInt);');
  25880. Add(' function GetEvents(Index: longint): TFuncInt;');
  25881. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25882. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25883. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25884. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25885. Add(' end;');
  25886. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25887. Add('function tobject.getfoo: tfuncint; begin end;');
  25888. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25889. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25890. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25891. Add('var');
  25892. Add(' Obj: TObject;');
  25893. Add(' vP: tfuncint;');
  25894. Add(' b: boolean;');
  25895. Add('begin');
  25896. Add(' obj.onfoo:=nil;');
  25897. Add(' obj.onbar:=nil;');
  25898. Add(' obj.events[1]:=nil;');
  25899. Add(' obj.onfoo:=obj.onfoo;');
  25900. Add(' obj.onbar:=obj.onbar;');
  25901. Add(' obj.events[2]:=obj.events[3];');
  25902. Add(' obj.onfoo:[email protected];');
  25903. Add(' obj.onbar:[email protected];');
  25904. Add(' obj.events[4]:[email protected];');
  25905. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25906. //Add(' obj.onbar:=obj.doit;'); // delphi
  25907. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25908. Add(' obj.onfoo;');
  25909. Add(' obj.onbar;');
  25910. //Add(' obj.events[5];'); ToDo in pasresolver
  25911. Add(' obj.onfoo();');
  25912. Add(' obj.onbar();');
  25913. Add(' obj.events[6]();');
  25914. Add(' b:=obj.onfoo=nil;');
  25915. Add(' b:=obj.onbar=nil;');
  25916. Add(' b:=obj.events[7]=nil;');
  25917. Add(' b:=obj.onfoo<>nil;');
  25918. Add(' b:=obj.onbar<>nil;');
  25919. Add(' b:=obj.events[8]<>nil;');
  25920. Add(' b:=obj.onfoo=vp;');
  25921. Add(' b:=obj.onbar=vp;');
  25922. Add(' b:=obj.events[9]=vp;');
  25923. Add(' b:=obj.onfoo=obj.onfoo;');
  25924. Add(' b:=obj.onbar=obj.onfoo;');
  25925. Add(' b:=obj.events[10]=obj.onfoo;');
  25926. Add(' b:=obj.onfoo<>obj.onfoo;');
  25927. Add(' b:=obj.onbar<>obj.onfoo;');
  25928. Add(' b:=obj.events[11]<>obj.onfoo;');
  25929. Add(' b:[email protected];');
  25930. Add(' b:[email protected];');
  25931. Add(' b:=obj.events[12][email protected];');
  25932. Add(' b:=obj.onfoo<>@obj.doit;');
  25933. Add(' b:=obj.onbar<>@obj.doit;');
  25934. Add(' b:=obj.events[12]<>@obj.doit;');
  25935. Add(' b:=Assigned(obj.onfoo);');
  25936. Add(' b:=Assigned(obj.onbar);');
  25937. Add(' b:=Assigned(obj.events[13]);');
  25938. ConvertProgram;
  25939. CheckSource('TestProcType_PropertyFPC',
  25940. LinesToStr([ // statements
  25941. 'rtl.createClass(this, "TObject", null, function () {',
  25942. ' this.$init = function () {',
  25943. ' this.FOnFoo = null;',
  25944. ' };',
  25945. ' this.$final = function () {',
  25946. ' this.FOnFoo = undefined;',
  25947. ' };',
  25948. ' this.DoIt = function (vA) {',
  25949. ' var Result = 0;',
  25950. ' return Result;',
  25951. ' };',
  25952. 'this.GetFoo = function () {',
  25953. ' var Result = null;',
  25954. ' return Result;',
  25955. '};',
  25956. 'this.SetFoo = function (Value) {',
  25957. '};',
  25958. 'this.GetEvents = function (Index) {',
  25959. ' var Result = null;',
  25960. ' return Result;',
  25961. '};',
  25962. 'this.SetEvents = function (Index, Value) {',
  25963. '};',
  25964. '});',
  25965. 'this.Obj = null;',
  25966. 'this.vP = null;',
  25967. 'this.b = false;'
  25968. ]),
  25969. LinesToStr([
  25970. '$mod.Obj.FOnFoo = null;',
  25971. '$mod.Obj.SetFoo(null);',
  25972. '$mod.Obj.SetEvents(1, null);',
  25973. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25974. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25975. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25976. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25977. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25978. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25979. '$mod.Obj.FOnFoo(1);',
  25980. '$mod.Obj.GetFoo();',
  25981. '$mod.Obj.FOnFoo(1);',
  25982. '$mod.Obj.GetFoo()(1);',
  25983. '$mod.Obj.GetEvents(6)(1);',
  25984. '$mod.b = $mod.Obj.FOnFoo === null;',
  25985. '$mod.b = $mod.Obj.GetFoo() === null;',
  25986. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25987. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25988. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25989. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25990. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25991. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25992. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25993. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25994. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25995. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25996. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25997. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25998. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25999. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26000. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26001. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26002. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26003. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26004. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26005. '$mod.b = $mod.Obj.FOnFoo != null;',
  26006. '$mod.b = $mod.Obj.GetFoo() != null;',
  26007. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26008. '']));
  26009. end;
  26010. procedure TTestModule.TestProcType_PropertyDelphi;
  26011. begin
  26012. StartProgram(false);
  26013. Add('{$mode delphi}');
  26014. Add('type');
  26015. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26016. Add(' TObject = class');
  26017. Add(' FOnFoo: TFuncInt;');
  26018. Add(' function DoIt(vA: longint = 1): longint;');
  26019. Add(' function GetFoo: TFuncInt;');
  26020. Add(' procedure SetFoo(const Value: TFuncInt);');
  26021. Add(' function GetEvents(Index: longint): TFuncInt;');
  26022. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26023. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26024. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26025. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26026. Add(' end;');
  26027. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26028. Add('function tobject.getfoo: tfuncint; begin end;');
  26029. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26030. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26031. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26032. Add('var');
  26033. Add(' Obj: TObject;');
  26034. Add(' vP: tfuncint;');
  26035. Add(' b: boolean;');
  26036. Add('begin');
  26037. Add(' obj.onfoo:=nil;');
  26038. Add(' obj.onbar:=nil;');
  26039. Add(' obj.events[1]:=nil;');
  26040. Add(' obj.onfoo:=obj.onfoo;');
  26041. Add(' obj.onbar:=obj.onbar;');
  26042. Add(' obj.events[2]:=obj.events[3];');
  26043. Add(' obj.onfoo:[email protected];');
  26044. Add(' obj.onbar:[email protected];');
  26045. Add(' obj.events[4]:[email protected];');
  26046. Add(' obj.onfoo:=obj.doit;'); // delphi
  26047. Add(' obj.onbar:=obj.doit;'); // delphi
  26048. Add(' obj.events[4]:=obj.doit;'); // delphi
  26049. Add(' obj.onfoo;');
  26050. Add(' obj.onbar;');
  26051. //Add(' obj.events[5];'); ToDo in pasresolver
  26052. Add(' obj.onfoo();');
  26053. Add(' obj.onbar();');
  26054. Add(' obj.events[6]();');
  26055. //Add(' b:=obj.onfoo=nil;'); // fpc
  26056. //Add(' b:=obj.onbar=nil;'); // fpc
  26057. //Add(' b:=obj.events[7]=nil;'); // fpc
  26058. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26059. //Add(' b:=obj.onbar<>nil;'); // fpc
  26060. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26061. Add(' b:=obj.onfoo=vp;');
  26062. Add(' b:=obj.onbar=vp;');
  26063. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26064. Add(' b:=obj.onfoo=obj.onfoo;');
  26065. Add(' b:=obj.onbar=obj.onfoo;');
  26066. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26067. Add(' b:=obj.onfoo<>obj.onfoo;');
  26068. Add(' b:=obj.onbar<>obj.onfoo;');
  26069. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26070. //Add(' b:[email protected];'); // fpc
  26071. //Add(' b:[email protected];'); // fpc
  26072. //Add(' b:=obj.events[12][email protected];'); // fpc
  26073. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26074. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26075. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26076. Add(' b:=Assigned(obj.onfoo);');
  26077. Add(' b:=Assigned(obj.onbar);');
  26078. Add(' b:=Assigned(obj.events[13]);');
  26079. ConvertProgram;
  26080. CheckSource('TestProcType_PropertyDelphi',
  26081. LinesToStr([ // statements
  26082. 'rtl.createClass(this, "TObject", null, function () {',
  26083. ' this.$init = function () {',
  26084. ' this.FOnFoo = null;',
  26085. ' };',
  26086. ' this.$final = function () {',
  26087. ' this.FOnFoo = undefined;',
  26088. ' };',
  26089. ' this.DoIt = function (vA) {',
  26090. ' var Result = 0;',
  26091. ' return Result;',
  26092. ' };',
  26093. 'this.GetFoo = function () {',
  26094. ' var Result = null;',
  26095. ' return Result;',
  26096. '};',
  26097. 'this.SetFoo = function (Value) {',
  26098. '};',
  26099. 'this.GetEvents = function (Index) {',
  26100. ' var Result = null;',
  26101. ' return Result;',
  26102. '};',
  26103. 'this.SetEvents = function (Index, Value) {',
  26104. '};',
  26105. '});',
  26106. 'this.Obj = null;',
  26107. 'this.vP = null;',
  26108. 'this.b = false;'
  26109. ]),
  26110. LinesToStr([
  26111. '$mod.Obj.FOnFoo = null;',
  26112. '$mod.Obj.SetFoo(null);',
  26113. '$mod.Obj.SetEvents(1, null);',
  26114. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26115. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26116. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26117. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26118. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26119. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26120. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26121. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26122. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26123. '$mod.Obj.FOnFoo(1);',
  26124. '$mod.Obj.GetFoo();',
  26125. '$mod.Obj.FOnFoo(1);',
  26126. '$mod.Obj.GetFoo()(1);',
  26127. '$mod.Obj.GetEvents(6)(1);',
  26128. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26129. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26130. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26131. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26132. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26133. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26134. '$mod.b = $mod.Obj.FOnFoo != null;',
  26135. '$mod.b = $mod.Obj.GetFoo() != null;',
  26136. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26137. '']));
  26138. end;
  26139. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26140. begin
  26141. StartProgram(false);
  26142. Add('type');
  26143. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26144. Add(' TObject = class');
  26145. Add(' FOnFoo: TFuncInt;');
  26146. Add(' function DoIt(vA: longint = 1): longint;');
  26147. Add(' function GetFoo: TFuncInt;');
  26148. Add(' procedure SetFoo(const Value: TFuncInt);');
  26149. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26150. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26151. Add(' end;');
  26152. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26153. Add('function tobject.getfoo: tfuncint; begin end;');
  26154. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26155. Add('var');
  26156. Add(' Obj: TObject;');
  26157. Add(' vP: tfuncint;');
  26158. Add(' b: boolean;');
  26159. Add('begin');
  26160. Add('with obj do begin');
  26161. Add(' fonfoo:=nil;');
  26162. Add(' onfoo:=nil;');
  26163. Add(' onbar:=nil;');
  26164. Add(' fonfoo:=fonfoo;');
  26165. Add(' onfoo:=onfoo;');
  26166. Add(' onbar:=onbar;');
  26167. Add(' fonfoo:=@doit;');
  26168. Add(' onfoo:=@doit;');
  26169. Add(' onbar:=@doit;');
  26170. //Add(' fonfoo:=doit;'); // delphi
  26171. //Add(' onfoo:=doit;'); // delphi
  26172. //Add(' onbar:=doit;'); // delphi
  26173. Add(' fonfoo;');
  26174. Add(' onfoo;');
  26175. Add(' onbar;');
  26176. Add(' fonfoo();');
  26177. Add(' onfoo();');
  26178. Add(' onbar();');
  26179. Add(' b:=fonfoo=nil;');
  26180. Add(' b:=onfoo=nil;');
  26181. Add(' b:=onbar=nil;');
  26182. Add(' b:=fonfoo<>nil;');
  26183. Add(' b:=onfoo<>nil;');
  26184. Add(' b:=onbar<>nil;');
  26185. Add(' b:=fonfoo=vp;');
  26186. Add(' b:=onfoo=vp;');
  26187. Add(' b:=onbar=vp;');
  26188. Add(' b:=fonfoo=fonfoo;');
  26189. Add(' b:=onfoo=onfoo;');
  26190. Add(' b:=onbar=onfoo;');
  26191. Add(' b:=fonfoo<>fonfoo;');
  26192. Add(' b:=onfoo<>onfoo;');
  26193. Add(' b:=onbar<>onfoo;');
  26194. Add(' b:=fonfoo=@doit;');
  26195. Add(' b:=onfoo=@doit;');
  26196. Add(' b:=onbar=@doit;');
  26197. Add(' b:=fonfoo<>@doit;');
  26198. Add(' b:=onfoo<>@doit;');
  26199. Add(' b:=onbar<>@doit;');
  26200. Add(' b:=Assigned(fonfoo);');
  26201. Add(' b:=Assigned(onfoo);');
  26202. Add(' b:=Assigned(onbar);');
  26203. Add('end;');
  26204. ConvertProgram;
  26205. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26206. LinesToStr([ // statements
  26207. 'rtl.createClass(this, "TObject", null, function () {',
  26208. ' this.$init = function () {',
  26209. ' this.FOnFoo = null;',
  26210. ' };',
  26211. ' this.$final = function () {',
  26212. ' this.FOnFoo = undefined;',
  26213. ' };',
  26214. ' this.DoIt = function (vA) {',
  26215. ' var Result = 0;',
  26216. ' return Result;',
  26217. ' };',
  26218. ' this.GetFoo = function () {',
  26219. ' var Result = null;',
  26220. ' return Result;',
  26221. ' };',
  26222. ' this.SetFoo = function (Value) {',
  26223. ' };',
  26224. '});',
  26225. 'this.Obj = null;',
  26226. 'this.vP = null;',
  26227. 'this.b = false;'
  26228. ]),
  26229. LinesToStr([
  26230. 'var $with = $mod.Obj;',
  26231. '$with.FOnFoo = null;',
  26232. '$with.FOnFoo = null;',
  26233. '$with.SetFoo(null);',
  26234. '$with.FOnFoo = $with.FOnFoo;',
  26235. '$with.FOnFoo = $with.FOnFoo;',
  26236. '$with.SetFoo($with.GetFoo());',
  26237. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26238. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26239. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26240. '$with.FOnFoo(1);',
  26241. '$with.FOnFoo(1);',
  26242. '$with.GetFoo();',
  26243. '$with.FOnFoo(1);',
  26244. '$with.FOnFoo(1);',
  26245. '$with.GetFoo()(1);',
  26246. '$mod.b = $with.FOnFoo === null;',
  26247. '$mod.b = $with.FOnFoo === null;',
  26248. '$mod.b = $with.GetFoo() === null;',
  26249. '$mod.b = $with.FOnFoo !== null;',
  26250. '$mod.b = $with.FOnFoo !== null;',
  26251. '$mod.b = $with.GetFoo() !== null;',
  26252. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26253. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26254. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26255. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26256. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26257. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26258. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26259. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26260. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26261. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26262. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26263. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26264. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26265. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26266. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26267. '$mod.b = $with.FOnFoo != null;',
  26268. '$mod.b = $with.FOnFoo != null;',
  26269. '$mod.b = $with.GetFoo() != null;',
  26270. '']));
  26271. end;
  26272. procedure TTestModule.TestProcType_Nested;
  26273. begin
  26274. StartProgram(false);
  26275. Add([
  26276. 'type',
  26277. ' TProcInt = procedure(vI: longint = 1);',
  26278. 'procedure DoIt(vJ: longint);',
  26279. 'var aProc: TProcInt;',
  26280. ' b: boolean;',
  26281. ' procedure Sub(vK: longint);',
  26282. ' var aSub: TProcInt;',
  26283. ' procedure SubSub(vK: longint);',
  26284. ' var aSubSub: TProcInt;',
  26285. ' begin;',
  26286. ' aProc:=@DoIt;',
  26287. ' aSub:=@DoIt;',
  26288. ' aSubSub:=@DoIt;',
  26289. ' aProc:=@Sub;',
  26290. ' aSub:=@Sub;',
  26291. ' aSubSub:=@Sub;',
  26292. ' aProc:=@SubSub;',
  26293. ' aSub:=@SubSub;',
  26294. ' aSubSub:=@SubSub;',
  26295. ' end;',
  26296. ' begin;',
  26297. ' end;',
  26298. 'begin;',
  26299. ' aProc:=@Sub;',
  26300. ' b:=aProc=@Sub;',
  26301. ' b:=@Sub=aProc;',
  26302. 'end;',
  26303. 'begin',
  26304. '']);
  26305. ConvertProgram;
  26306. CheckSource('TestProcType_Nested',
  26307. LinesToStr([ // statements
  26308. 'this.DoIt = function (vJ) {',
  26309. ' var aProc = null;',
  26310. ' var b = false;',
  26311. ' function Sub(vK) {',
  26312. ' var aSub = null;',
  26313. ' function SubSub(vK) {',
  26314. ' var aSubSub = null;',
  26315. ' aProc = $mod.DoIt;',
  26316. ' aSub = $mod.DoIt;',
  26317. ' aSubSub = $mod.DoIt;',
  26318. ' aProc = Sub;',
  26319. ' aSub = Sub;',
  26320. ' aSubSub = Sub;',
  26321. ' aProc = SubSub;',
  26322. ' aSub = SubSub;',
  26323. ' aSubSub = SubSub;',
  26324. ' };',
  26325. ' };',
  26326. ' aProc = Sub;',
  26327. ' b = rtl.eqCallback(aProc, Sub);',
  26328. ' b = rtl.eqCallback(Sub, aProc);',
  26329. '};',
  26330. '']),
  26331. LinesToStr([ // $mod.$main
  26332. '']));
  26333. end;
  26334. procedure TTestModule.TestProcType_NestedOfObject;
  26335. begin
  26336. StartProgram(false);
  26337. Add([
  26338. 'type',
  26339. ' TProcInt = procedure(vI: longint = 1) of object;',
  26340. ' TObject = class',
  26341. ' procedure DoIt(vJ: longint);',
  26342. ' end;',
  26343. 'procedure TObject.DoIt(vJ: longint);',
  26344. 'var aProc: TProcInt;',
  26345. ' b: boolean;',
  26346. ' procedure Sub(vK: longint);',
  26347. ' var aSub: TProcInt;',
  26348. ' procedure SubSub(vK: longint);',
  26349. ' var aSubSub: TProcInt;',
  26350. ' begin;',
  26351. ' aProc:=@DoIt;',
  26352. ' aSub:=@DoIt;',
  26353. ' aSubSub:=@DoIt;',
  26354. ' aProc:=@Sub;',
  26355. ' aSub:=@Sub;',
  26356. ' aSubSub:=@Sub;',
  26357. ' aProc:=@SubSub;',
  26358. ' aSub:=@SubSub;',
  26359. ' aSubSub:=@SubSub;',
  26360. ' end;',
  26361. ' begin;',
  26362. ' end;',
  26363. 'begin;',
  26364. ' aProc:=@Sub;',
  26365. ' b:=aProc=@Sub;',
  26366. ' b:=@Sub=aProc;',
  26367. 'end;',
  26368. 'begin',
  26369. '']);
  26370. ConvertProgram;
  26371. CheckSource('TestProcType_Nested',
  26372. LinesToStr([ // statements
  26373. 'rtl.createClass(this, "TObject", null, function () {',
  26374. ' this.$init = function () {',
  26375. ' };',
  26376. ' this.$final = function () {',
  26377. ' };',
  26378. ' this.DoIt = function (vJ) {',
  26379. ' var $Self = this;',
  26380. ' var aProc = null;',
  26381. ' var b = false;',
  26382. ' function Sub(vK) {',
  26383. ' var aSub = null;',
  26384. ' function SubSub(vK) {',
  26385. ' var aSubSub = null;',
  26386. ' aProc = rtl.createCallback($Self, "DoIt");',
  26387. ' aSub = rtl.createCallback($Self, "DoIt");',
  26388. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26389. ' aProc = Sub;',
  26390. ' aSub = Sub;',
  26391. ' aSubSub = Sub;',
  26392. ' aProc = SubSub;',
  26393. ' aSub = SubSub;',
  26394. ' aSubSub = SubSub;',
  26395. ' };',
  26396. ' };',
  26397. ' aProc = Sub;',
  26398. ' b = rtl.eqCallback(aProc, Sub);',
  26399. ' b = rtl.eqCallback(Sub, aProc);',
  26400. ' };',
  26401. '});',
  26402. '']),
  26403. LinesToStr([ // $mod.$main
  26404. '']));
  26405. end;
  26406. procedure TTestModule.TestProcType_ReferenceToProc;
  26407. begin
  26408. StartProgram(false);
  26409. Add([
  26410. 'type',
  26411. ' TProcRef = reference to procedure(i: longint = 0);',
  26412. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26413. 'var',
  26414. ' p: TProcRef;',
  26415. ' f: TFuncRef;',
  26416. 'procedure DoIt(i: longint);',
  26417. 'begin',
  26418. 'end;',
  26419. 'function GetIt(i: longint): longint;',
  26420. 'begin',
  26421. ' p:=@DoIt;',
  26422. ' f:=@GetIt;',
  26423. ' f;',
  26424. ' f();',
  26425. ' f(1);',
  26426. 'end;',
  26427. 'begin',
  26428. ' p:=@DoIt;',
  26429. ' f:=@GetIt;',
  26430. ' f;',
  26431. ' f();',
  26432. ' f(1);',
  26433. ' p:=TProcRef(f);',
  26434. '']);
  26435. ConvertProgram;
  26436. CheckSource('TestProcType_ReferenceToProc',
  26437. LinesToStr([ // statements
  26438. 'this.p = null;',
  26439. 'this.f = null;',
  26440. 'this.DoIt = function (i) {',
  26441. '};',
  26442. 'this.GetIt = function (i) {',
  26443. ' var Result = 0;',
  26444. ' $mod.p = $mod.DoIt;',
  26445. ' $mod.f = $mod.GetIt;',
  26446. ' $mod.f(0);',
  26447. ' $mod.f(0);',
  26448. ' $mod.f(1);',
  26449. ' return Result;',
  26450. '};',
  26451. '']),
  26452. LinesToStr([ // $mod.$main
  26453. '$mod.p = $mod.DoIt;',
  26454. '$mod.f = $mod.GetIt;',
  26455. '$mod.f(0);',
  26456. '$mod.f(0);',
  26457. '$mod.f(1);',
  26458. '$mod.p = $mod.f;',
  26459. '']));
  26460. end;
  26461. procedure TTestModule.TestProcType_ReferenceToMethod;
  26462. begin
  26463. StartProgram(false);
  26464. Add([
  26465. 'type',
  26466. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26467. ' TObject = class',
  26468. ' function Grow(s: longint): longint;',
  26469. ' end;',
  26470. 'var',
  26471. ' f: tfuncref;',
  26472. 'function tobject.grow(s: longint): longint;',
  26473. ' function GrowSub(i: longint): longint;',
  26474. ' begin',
  26475. ' f:=@grow;',
  26476. ' f:=@growsub;',
  26477. ' end;',
  26478. 'begin',
  26479. ' f:=@grow;',
  26480. ' f:=@growsub;',
  26481. 'end;',
  26482. 'begin',
  26483. '']);
  26484. ConvertProgram;
  26485. CheckSource('TestProcType_ReferenceToMethod',
  26486. LinesToStr([ // statements
  26487. 'rtl.createClass(this, "TObject", null, function () {',
  26488. ' this.$init = function () {',
  26489. ' };',
  26490. ' this.$final = function () {',
  26491. ' };',
  26492. ' this.Grow = function (s) {',
  26493. ' var $Self = this;',
  26494. ' var Result = 0;',
  26495. ' function GrowSub(i) {',
  26496. ' var Result = 0;',
  26497. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26498. ' $mod.f = GrowSub;',
  26499. ' return Result;',
  26500. ' };',
  26501. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26502. ' $mod.f = GrowSub;',
  26503. ' return Result;',
  26504. ' };',
  26505. '});',
  26506. 'this.f = null;',
  26507. '']),
  26508. LinesToStr([ // $mod.$main
  26509. '']));
  26510. end;
  26511. procedure TTestModule.TestProcType_Typecast;
  26512. begin
  26513. StartProgram(false);
  26514. Add([
  26515. 'type',
  26516. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26517. ' TEvent = procedure of object;',
  26518. ' TGetter = function:longint of object;',
  26519. ' TProcA = procedure(i: longint);',
  26520. ' TFuncB = function(i, j: longint): longint;',
  26521. 'procedure DoIt(); varargs; begin end;',
  26522. 'var',
  26523. ' Notify: tnotifyevent;',
  26524. ' Event: tevent;',
  26525. ' Getter: tgetter;',
  26526. ' ProcA: tproca;',
  26527. ' FuncB: tfuncb;',
  26528. ' p: pointer;',
  26529. 'begin',
  26530. ' notify:=tnotifyevent(event);',
  26531. ' event:=tevent(event);',
  26532. ' event:=tevent(notify);',
  26533. ' event:=tevent(getter);',
  26534. ' event:=tevent(proca);',
  26535. ' proca:=tproca(funcb);',
  26536. ' funcb:=tfuncb(funcb);',
  26537. ' funcb:=tfuncb(proca);',
  26538. ' funcb:=tfuncb(getter);',
  26539. ' proca:=tproca(p);',
  26540. ' funcb:=tfuncb(p);',
  26541. ' getter:=tgetter(p);',
  26542. ' p:=pointer(notify);',
  26543. ' p:=notify;',
  26544. ' p:=pointer(proca);',
  26545. ' p:=proca;',
  26546. ' p:=pointer(funcb);',
  26547. ' p:=funcb;',
  26548. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26549. '']);
  26550. ConvertProgram;
  26551. CheckSource('TestProcType_Typecast',
  26552. LinesToStr([ // statements
  26553. 'this.DoIt = function () {',
  26554. '};',
  26555. 'this.Notify = null;',
  26556. 'this.Event = null;',
  26557. 'this.Getter = null;',
  26558. 'this.ProcA = null;',
  26559. 'this.FuncB = null;',
  26560. 'this.p = null;',
  26561. '']),
  26562. LinesToStr([ // $mod.$main
  26563. '$mod.Notify = $mod.Event;',
  26564. '$mod.Event = $mod.Event;',
  26565. '$mod.Event = $mod.Notify;',
  26566. '$mod.Event = $mod.Getter;',
  26567. '$mod.Event = $mod.ProcA;',
  26568. '$mod.ProcA = $mod.FuncB;',
  26569. '$mod.FuncB = $mod.FuncB;',
  26570. '$mod.FuncB = $mod.ProcA;',
  26571. '$mod.FuncB = $mod.Getter;',
  26572. '$mod.ProcA = $mod.p;',
  26573. '$mod.FuncB = $mod.p;',
  26574. '$mod.Getter = $mod.p;',
  26575. '$mod.p = $mod.Notify;',
  26576. '$mod.p = $mod.Notify;',
  26577. '$mod.p = $mod.ProcA;',
  26578. '$mod.p = $mod.ProcA;',
  26579. '$mod.p = $mod.FuncB;',
  26580. '$mod.p = $mod.FuncB;',
  26581. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26582. '']));
  26583. end;
  26584. procedure TTestModule.TestProcType_PassProcToUntyped;
  26585. begin
  26586. StartProgram(false);
  26587. Add([
  26588. 'type',
  26589. ' TEvent = procedure of object;',
  26590. ' TFunc = function: longint;',
  26591. 'procedure DoIt(); varargs; begin end;',
  26592. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26593. 'var',
  26594. ' Event: tevent;',
  26595. ' Func: TFunc;',
  26596. 'begin',
  26597. ' doit(event,func);',
  26598. ' dosome(event,event,event);',
  26599. ' dosome(func,func,func);',
  26600. '']);
  26601. ConvertProgram;
  26602. CheckSource('TestProcType_PassProcToUntyped',
  26603. LinesToStr([ // statements
  26604. 'this.DoIt = function () {',
  26605. '};',
  26606. 'this.DoSome = function (a, b, p) {',
  26607. '};',
  26608. 'this.Event = null;',
  26609. 'this.Func = null;',
  26610. '']),
  26611. LinesToStr([ // $mod.$main
  26612. '$mod.DoIt($mod.Event, $mod.Func);',
  26613. '$mod.DoSome($mod.Event, {',
  26614. ' p: $mod,',
  26615. ' get: function () {',
  26616. ' return this.p.Event;',
  26617. ' },',
  26618. ' set: function (v) {',
  26619. ' this.p.Event = v;',
  26620. ' }',
  26621. '}, $mod.Event);',
  26622. '$mod.DoSome($mod.Func, {',
  26623. ' p: $mod,',
  26624. ' get: function () {',
  26625. ' return this.p.Func;',
  26626. ' },',
  26627. ' set: function (v) {',
  26628. ' this.p.Func = v;',
  26629. ' }',
  26630. '}, $mod.Func);',
  26631. '']));
  26632. end;
  26633. procedure TTestModule.TestProcType_PassProcToArray;
  26634. begin
  26635. StartProgram(false);
  26636. Add([
  26637. 'type',
  26638. ' TFunc = function: longint;',
  26639. ' TArrFunc = array of TFunc;',
  26640. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26641. 'function GetIt: longint; begin end;',
  26642. 'var',
  26643. ' Func: tfunc;',
  26644. 'begin',
  26645. ' doit([]);',
  26646. ' doit([@GetIt]);',
  26647. ' doit([Func]);',
  26648. '']);
  26649. ConvertProgram;
  26650. CheckSource('TestProcType_PassProcToArray',
  26651. LinesToStr([ // statements
  26652. 'this.DoIt = function (Arr) {',
  26653. '};',
  26654. 'this.GetIt = function () {',
  26655. ' var Result = 0;',
  26656. ' return Result;',
  26657. '};',
  26658. 'this.Func = null;',
  26659. '']),
  26660. LinesToStr([ // $mod.$main
  26661. '$mod.DoIt([]);',
  26662. '$mod.DoIt([$mod.GetIt]);',
  26663. '$mod.DoIt([$mod.Func]);',
  26664. '']));
  26665. end;
  26666. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26667. begin
  26668. StartProgram(false);
  26669. Add([
  26670. '{$modeswitch externalclass}',
  26671. 'type',
  26672. ' TProc = reference to procedure(i: longint); safecall;',
  26673. ' TEvent = procedure(i: longint) of object; safecall;',
  26674. ' TExtA = class external name ''ExtObj''',
  26675. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26676. ' procedure DoSome(Id: longint = 1);',
  26677. ' procedure SetOnClick(const e: TEvent);',
  26678. ' property OnClick: TEvent write SetOnClick;',
  26679. ' class procedure Fly(Id: longint = 1); static;',
  26680. ' procedure SetOnShow(const p: TProc);',
  26681. ' property OnShow: TProc write SetOnShow;',
  26682. ' end;',
  26683. 'procedure Run(i: longint = 1);',
  26684. 'begin',
  26685. 'end;',
  26686. 'var',
  26687. ' Obj: texta;',
  26688. ' e: TEvent;',
  26689. ' p: TProc;',
  26690. 'begin',
  26691. ' e:=e;',
  26692. ' e:[email protected];',
  26693. ' e:[email protected];',
  26694. ' e:=TEvent(@obj.dosome);', // no safecall
  26695. ' obj.OnClick:[email protected];',
  26696. ' obj.OnClick:[email protected];',
  26697. ' obj.setonclick(@obj.doit);',
  26698. ' obj.setonclick(@obj.dosome);',
  26699. ' p:=@Run;',
  26700. ' p:[email protected];',
  26701. ' obj.OnShow:=@Run;',
  26702. ' obj.OnShow:[email protected];',
  26703. ' obj.setOnShow(@Run);',
  26704. ' obj.setOnShow(@TExtA.Fly);',
  26705. ' with obj do begin',
  26706. ' e:=@doit;',
  26707. ' e:=@dosome;',
  26708. ' OnClick:=@doit;',
  26709. ' OnClick:=@dosome;',
  26710. ' setonclick(@doit);',
  26711. ' setonclick(@dosome);',
  26712. ' OnShow:=@Run;',
  26713. ' setOnShow(@Run);',
  26714. ' end;']);
  26715. ConvertProgram;
  26716. CheckSource('TestProcType_SafeCallObjFPC',
  26717. LinesToStr([ // statements
  26718. 'this.Run = function (i) {',
  26719. '};',
  26720. 'this.Obj = null;',
  26721. 'this.e = null;',
  26722. 'this.p = null;',
  26723. '']),
  26724. LinesToStr([ // $mod.$main
  26725. '$mod.e = $mod.e;',
  26726. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26727. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26728. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26729. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26730. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26731. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26732. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26733. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26734. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26735. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26736. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26737. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26738. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26739. 'var $with = $mod.Obj;',
  26740. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26741. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26742. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26743. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26744. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26745. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26746. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26747. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26748. '']));
  26749. end;
  26750. procedure TTestModule.TestProcType_SafeCallDelphi;
  26751. begin
  26752. StartProgram(false);
  26753. Add([
  26754. '{$mode delphi}',
  26755. '{$modeswitch externalclass}',
  26756. 'type',
  26757. ' TProc = reference to procedure(i: longint); safecall;',
  26758. ' TEvent = procedure(i: longint) of object; safecall;',
  26759. ' TExtA = class external name ''ExtObj''',
  26760. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26761. ' procedure DoSome(Id: longint = 1);',
  26762. ' procedure SetOnClick(const e: TEvent);',
  26763. ' property OnClick: TEvent write SetOnClick;',
  26764. ' class procedure Fly(Id: longint = 1); static;',
  26765. ' procedure SetOnShow(const p: TProc);',
  26766. ' property OnShow: TProc write SetOnShow;',
  26767. ' end;',
  26768. 'procedure Run(i: longint = 1);',
  26769. 'begin',
  26770. 'end;',
  26771. 'var',
  26772. ' Obj: texta;',
  26773. ' e: TEvent;',
  26774. ' p: TProc;',
  26775. 'begin',
  26776. ' e:=e;',
  26777. ' e:=obj.doit;',
  26778. ' e:=obj.dosome;',
  26779. ' e:=TEvent(@obj.dosome);', // no safecall
  26780. ' obj.OnClick:=obj.doit;',
  26781. ' obj.OnClick:=obj.dosome;',
  26782. ' obj.setonclick(obj.doit);',
  26783. ' obj.setonclick(obj.dosome);',
  26784. ' p:=Run;',
  26785. ' p:=TExtA.Fly;',
  26786. ' obj.OnShow:=Run;',
  26787. ' obj.OnShow:=TExtA.Fly;',
  26788. ' obj.setOnShow(Run);',
  26789. ' obj.setOnShow(TExtA.Fly);',
  26790. ' with obj do begin',
  26791. ' e:=doit;',
  26792. ' e:=dosome;',
  26793. ' OnClick:=doit;',
  26794. ' OnClick:=dosome;',
  26795. ' setonclick(doit);',
  26796. ' setonclick(dosome);',
  26797. ' OnShow:=@Run;',
  26798. ' setOnShow(@Run);',
  26799. ' end;']);
  26800. ConvertProgram;
  26801. CheckSource('TestProcType_SafeCallDelphi',
  26802. LinesToStr([ // statements
  26803. 'this.Run = function (i) {',
  26804. '};',
  26805. 'this.Obj = null;',
  26806. 'this.e = null;',
  26807. 'this.p = null;',
  26808. '']),
  26809. LinesToStr([ // $mod.$main
  26810. '$mod.e = $mod.e;',
  26811. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26812. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26813. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26814. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26815. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26816. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26817. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26818. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26819. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26820. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26821. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26822. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26823. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26824. 'var $with = $mod.Obj;',
  26825. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26826. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26827. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26828. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26829. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26830. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26831. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26832. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26833. '']));
  26834. end;
  26835. procedure TTestModule.TestPointer;
  26836. begin
  26837. StartProgram(false);
  26838. Add(['type',
  26839. ' TObject = class end;',
  26840. ' TClass = class of TObject;',
  26841. ' TArrInt = array of longint;',
  26842. 'const',
  26843. ' n = nil;',
  26844. 'var',
  26845. ' v: jsvalue;',
  26846. ' Obj: tobject;',
  26847. ' C: tclass;',
  26848. ' a: tarrint;',
  26849. ' p: Pointer = nil;',
  26850. ' s: string;',
  26851. 'begin',
  26852. ' p:=p;',
  26853. ' p:=nil;',
  26854. ' if p=nil then;',
  26855. ' if nil=p then;',
  26856. ' if Assigned(p) then;',
  26857. ' p:=Pointer(v);',
  26858. ' p:=obj;',
  26859. ' p:=c;',
  26860. ' p:=a;',
  26861. ' p:=tobject;',
  26862. ' obj:=TObject(p);',
  26863. ' c:=TClass(p);',
  26864. ' a:=TArrInt(p);',
  26865. ' p:=n;',
  26866. ' p:=Pointer(a);',
  26867. ' p:=pointer(s);',
  26868. ' s:=string(p);',
  26869. '']);
  26870. ConvertProgram;
  26871. CheckSource('TestPointer',
  26872. LinesToStr([ // statements
  26873. 'rtl.createClass(this, "TObject", null, function () {',
  26874. ' this.$init = function () {',
  26875. ' };',
  26876. ' this.$final = function () {',
  26877. ' };',
  26878. '});',
  26879. 'this.n = null;',
  26880. 'this.v = undefined;',
  26881. 'this.Obj = null;',
  26882. 'this.C = null;',
  26883. 'this.a = [];',
  26884. 'this.p = null;',
  26885. 'this.s = "";',
  26886. '']),
  26887. LinesToStr([ // $mod.$main
  26888. '$mod.p = $mod.p;',
  26889. '$mod.p = null;',
  26890. 'if ($mod.p === null) ;',
  26891. 'if (null === $mod.p) ;',
  26892. 'if ($mod.p != null) ;',
  26893. '$mod.p = $mod.v;',
  26894. '$mod.p = $mod.Obj;',
  26895. '$mod.p = $mod.C;',
  26896. '$mod.p = $mod.a;',
  26897. '$mod.p = $mod.TObject;',
  26898. '$mod.Obj = $mod.p;',
  26899. '$mod.C = $mod.p;',
  26900. '$mod.a = $mod.p;',
  26901. '$mod.p = null;',
  26902. '$mod.p = $mod.a;',
  26903. '$mod.p = $mod.s;',
  26904. '$mod.s = $mod.p;',
  26905. '']));
  26906. end;
  26907. procedure TTestModule.TestPointer_Proc;
  26908. begin
  26909. StartProgram(false);
  26910. Add('type');
  26911. Add(' TObject = class');
  26912. Add(' procedure DoIt; virtual; abstract;');
  26913. Add(' end;');
  26914. Add('procedure DoSome; begin end;');
  26915. Add('var');
  26916. Add(' o: TObject;');
  26917. Add(' p: Pointer;');
  26918. Add('begin');
  26919. Add(' p:=@DoSome;');
  26920. Add(' p:[email protected];');
  26921. ConvertProgram;
  26922. CheckSource('TestPointer_Proc',
  26923. LinesToStr([ // statements
  26924. 'rtl.createClass(this, "TObject", null, function () {',
  26925. ' this.$init = function () {',
  26926. ' };',
  26927. ' this.$final = function () {',
  26928. ' };',
  26929. '});',
  26930. 'this.DoSome = function () {',
  26931. '};',
  26932. 'this.o = null;',
  26933. 'this.p = null;',
  26934. '']),
  26935. LinesToStr([ // $mod.$main
  26936. '$mod.p = $mod.DoSome;',
  26937. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26938. '']));
  26939. end;
  26940. procedure TTestModule.TestPointer_AssignRecordFail;
  26941. begin
  26942. StartProgram(false);
  26943. Add('type');
  26944. Add(' TRec = record end;');
  26945. Add('var');
  26946. Add(' p: Pointer;');
  26947. Add(' r: TRec;');
  26948. Add('begin');
  26949. Add(' p:=r;');
  26950. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26951. nIncompatibleTypesGotExpected);
  26952. ConvertProgram;
  26953. end;
  26954. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26955. begin
  26956. StartProgram(false);
  26957. Add('type');
  26958. Add(' TArr = array[boolean] of longint;');
  26959. Add('var');
  26960. Add(' p: Pointer;');
  26961. Add(' a: TArr;');
  26962. Add('begin');
  26963. Add(' p:=a;');
  26964. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26965. nIncompatibleTypesGotExpected);
  26966. ConvertProgram;
  26967. end;
  26968. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26969. begin
  26970. StartProgram(false);
  26971. Add([
  26972. 'procedure DoIt(args: array of jsvalue); begin end;',
  26973. 'procedure DoAll; varargs; begin end;',
  26974. 'var',
  26975. ' v: jsvalue;',
  26976. 'begin',
  26977. ' DoIt([pointer(v)]);',
  26978. ' DoAll(pointer(v));',
  26979. '']);
  26980. ConvertProgram;
  26981. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26982. LinesToStr([ // statements
  26983. 'this.DoIt = function (args) {',
  26984. '};',
  26985. 'this.DoAll = function () {',
  26986. '};',
  26987. 'this.v = undefined;',
  26988. '']),
  26989. LinesToStr([ // $mod.$main
  26990. '$mod.DoIt([$mod.v]);',
  26991. '$mod.DoAll($mod.v);',
  26992. '']));
  26993. end;
  26994. procedure TTestModule.TestPointer_NonRecordFail;
  26995. begin
  26996. StartProgram(false);
  26997. Add([
  26998. 'type',
  26999. ' p = ^longint;',
  27000. 'begin',
  27001. '']);
  27002. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  27003. ConvertProgram;
  27004. end;
  27005. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  27006. begin
  27007. StartProgram(false);
  27008. Add([
  27009. 'procedure DoIt(p: ^longint); begin end;',
  27010. 'begin',
  27011. '']);
  27012. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27013. ConvertProgram;
  27014. end;
  27015. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  27016. begin
  27017. StartProgram(false);
  27018. Add([
  27019. 'var p: ^longint;',
  27020. 'begin',
  27021. '']);
  27022. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27023. ConvertProgram;
  27024. end;
  27025. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  27026. begin
  27027. StartProgram(false);
  27028. Add([
  27029. 'function DoIt: ^longint; begin end;',
  27030. 'begin',
  27031. '']);
  27032. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27033. ConvertProgram;
  27034. end;
  27035. procedure TTestModule.TestPointer_AddrOperatorFail;
  27036. begin
  27037. StartProgram(false);
  27038. Add([
  27039. 'var i: longint;',
  27040. 'begin',
  27041. ' if @i=nil then ;',
  27042. '']);
  27043. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27044. ConvertProgram;
  27045. end;
  27046. procedure TTestModule.TestPointer_ArrayParamsFail;
  27047. begin
  27048. StartProgram(false);
  27049. Add([
  27050. 'var',
  27051. ' p: Pointer;',
  27052. 'begin',
  27053. ' p:=p[1];',
  27054. '']);
  27055. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27056. ConvertProgram;
  27057. end;
  27058. procedure TTestModule.TestPointer_PointerAddFail;
  27059. begin
  27060. StartProgram(false);
  27061. Add([
  27062. 'var',
  27063. ' p: Pointer;',
  27064. 'begin',
  27065. ' p:=p+1;',
  27066. '']);
  27067. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27068. ConvertProgram;
  27069. end;
  27070. procedure TTestModule.TestPointer_IncPointerFail;
  27071. begin
  27072. StartProgram(false);
  27073. Add([
  27074. 'var',
  27075. ' p: Pointer;',
  27076. 'begin',
  27077. ' inc(p,1);',
  27078. '']);
  27079. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27080. nIncompatibleTypeArgNo);
  27081. ConvertProgram;
  27082. end;
  27083. procedure TTestModule.TestPointer_Record;
  27084. begin
  27085. StartProgram(false);
  27086. Add([
  27087. 'type',
  27088. ' TRec = record x: longint; end;',
  27089. ' PRec = ^TRec;',
  27090. 'var',
  27091. ' r: TRec;',
  27092. ' p: PRec;',
  27093. ' q: ^TRec;',
  27094. ' Ptr: pointer;',
  27095. 'begin',
  27096. ' new(p);',
  27097. ' p:=@r;',
  27098. ' r:=p^;',
  27099. ' r.x:=p^.x;',
  27100. ' p^.x:=r.x;',
  27101. ' if p^.x=3 then ;',
  27102. ' if 4=p^.x then ;',
  27103. ' dispose(p);',
  27104. ' new(q);',
  27105. ' dispose(q);',
  27106. ' Ptr:=p;',
  27107. ' p:=PRec(ptr);',
  27108. '']);
  27109. ConvertProgram;
  27110. CheckSource('TestPointer_Record',
  27111. LinesToStr([ // statements
  27112. 'rtl.recNewT(this, "TRec", function () {',
  27113. ' this.x = 0;',
  27114. ' this.$eq = function (b) {',
  27115. ' return this.x === b.x;',
  27116. ' };',
  27117. ' this.$assign = function (s) {',
  27118. ' this.x = s.x;',
  27119. ' return this;',
  27120. ' };',
  27121. '});',
  27122. 'this.r = this.TRec.$new();',
  27123. 'this.p = null;',
  27124. 'this.q = null;',
  27125. 'this.Ptr = null;',
  27126. '']),
  27127. LinesToStr([ // $mod.$main
  27128. '$mod.p = $mod.TRec.$new();',
  27129. '$mod.p = $mod.r;',
  27130. '$mod.r.$assign($mod.p);',
  27131. '$mod.r.x = $mod.p.x;',
  27132. '$mod.p.x = $mod.r.x;',
  27133. 'if ($mod.p.x === 3) ;',
  27134. 'if (4 === $mod.p.x) ;',
  27135. '$mod.p = null;',
  27136. '$mod.q = $mod.TRec.$new();',
  27137. '$mod.q = null;',
  27138. '$mod.Ptr = $mod.p;',
  27139. '$mod.p = $mod.Ptr;',
  27140. '']));
  27141. end;
  27142. procedure TTestModule.TestPointer_RecordArg;
  27143. begin
  27144. StartProgram(false);
  27145. Add([
  27146. '{$modeswitch autoderef}',
  27147. 'type',
  27148. ' TRec = record x: longint; end;',
  27149. ' PRec = ^TRec;',
  27150. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27151. 'begin',
  27152. ' a.x:=a.x;',
  27153. ' a^.x:=a^.x;',
  27154. ' with a^ do',
  27155. ' x:=x;',
  27156. 'end;',
  27157. 'function GetIt(p: PRec): PRec;',
  27158. 'begin',
  27159. ' p.x:=p.x;',
  27160. ' p^.x:=p^.x;',
  27161. ' with p^ do',
  27162. ' x:=x;',
  27163. 'end;',
  27164. 'var',
  27165. ' r: TRec;',
  27166. ' p: PRec;',
  27167. 'begin',
  27168. ' p:=GetIt(p);',
  27169. ' p^:=GetIt(@r)^;',
  27170. ' DoIt(p,p,p);',
  27171. ' DoIt(@r,p,p);',
  27172. '']);
  27173. ConvertProgram;
  27174. CheckSource('TestPointer_RecordArg',
  27175. LinesToStr([ // statements
  27176. 'rtl.recNewT(this, "TRec", function () {',
  27177. ' this.x = 0;',
  27178. ' this.$eq = function (b) {',
  27179. ' return this.x === b.x;',
  27180. ' };',
  27181. ' this.$assign = function (s) {',
  27182. ' this.x = s.x;',
  27183. ' return this;',
  27184. ' };',
  27185. '});',
  27186. 'this.DoIt = function (a, b, c) {',
  27187. ' var Result = $mod.TRec.$new();',
  27188. ' a.x = a.x;',
  27189. ' a.x = a.x;',
  27190. ' a.x = a.x;',
  27191. ' return Result;',
  27192. '};',
  27193. 'this.GetIt = function (p) {',
  27194. ' var Result = null;',
  27195. ' p.x = p.x;',
  27196. ' p.x = p.x;',
  27197. ' p.x = p.x;',
  27198. ' return Result;',
  27199. '};',
  27200. 'this.r = this.TRec.$new();',
  27201. 'this.p = null;',
  27202. '']),
  27203. LinesToStr([ // $mod.$main
  27204. '$mod.p = $mod.GetIt($mod.p);',
  27205. '$mod.p.$assign($mod.GetIt($mod.r));',
  27206. '$mod.DoIt($mod.p, {',
  27207. ' p: $mod,',
  27208. ' get: function () {',
  27209. ' return this.p.p;',
  27210. ' },',
  27211. ' set: function (v) {',
  27212. ' this.p.p = v;',
  27213. ' }',
  27214. '}, {',
  27215. ' p: $mod,',
  27216. ' get: function () {',
  27217. ' return this.p.p;',
  27218. ' },',
  27219. ' set: function (v) {',
  27220. ' this.p.p = v;',
  27221. ' }',
  27222. '});',
  27223. '$mod.DoIt($mod.r, {',
  27224. ' p: $mod,',
  27225. ' get: function () {',
  27226. ' return this.p.p;',
  27227. ' },',
  27228. ' set: function (v) {',
  27229. ' this.p.p = v;',
  27230. ' }',
  27231. '}, {',
  27232. ' p: $mod,',
  27233. ' get: function () {',
  27234. ' return this.p.p;',
  27235. ' },',
  27236. ' set: function (v) {',
  27237. ' this.p.p = v;',
  27238. ' }',
  27239. '});',
  27240. '']));
  27241. end;
  27242. procedure TTestModule.TestJSValue_AssignToJSValue;
  27243. begin
  27244. StartProgram(false);
  27245. Add('var');
  27246. Add(' v: jsvalue;');
  27247. Add(' i: longint;');
  27248. Add(' s: string;');
  27249. Add(' b: boolean;');
  27250. Add(' d: double;');
  27251. Add(' p: pointer;');
  27252. Add('begin');
  27253. Add(' v:=v;');
  27254. Add(' v:=1;');
  27255. Add(' v:=i;');
  27256. Add(' v:='''';');
  27257. Add(' v:=''c'';');
  27258. Add(' v:=''foo'';');
  27259. Add(' v:=s;');
  27260. Add(' v:=false;');
  27261. Add(' v:=true;');
  27262. Add(' v:=b;');
  27263. Add(' v:=0.1;');
  27264. Add(' v:=d;');
  27265. Add(' v:=nil;');
  27266. Add(' v:=p;');
  27267. ConvertProgram;
  27268. CheckSource('TestJSValue_AssignToJSValue',
  27269. LinesToStr([ // statements
  27270. 'this.v = undefined;',
  27271. 'this.i = 0;',
  27272. 'this.s = "";',
  27273. 'this.b = false;',
  27274. 'this.d = 0.0;',
  27275. 'this.p = null;',
  27276. '']),
  27277. LinesToStr([ // $mod.$main
  27278. '$mod.v = $mod.v;',
  27279. '$mod.v = 1;',
  27280. '$mod.v = $mod.i;',
  27281. '$mod.v = "";',
  27282. '$mod.v = "c";',
  27283. '$mod.v = "foo";',
  27284. '$mod.v = $mod.s;',
  27285. '$mod.v = false;',
  27286. '$mod.v = true;',
  27287. '$mod.v = $mod.b;',
  27288. '$mod.v = 0.1;',
  27289. '$mod.v = $mod.d;',
  27290. '$mod.v = null;',
  27291. '$mod.v = $mod.p;',
  27292. '']));
  27293. end;
  27294. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27295. begin
  27296. StartProgram(false);
  27297. Add('type');
  27298. Add(' integer = longint;');
  27299. Add(' TYesNo = boolean;');
  27300. Add(' TFloat = double;');
  27301. Add(' TCaption = string;');
  27302. Add(' TChar = char;');
  27303. Add('var');
  27304. Add(' v: jsvalue;');
  27305. Add(' i: integer;');
  27306. Add(' s: TCaption;');
  27307. Add(' b: TYesNo;');
  27308. Add(' d: TFloat;');
  27309. Add(' c: char;');
  27310. Add('begin');
  27311. Add(' i:=longint(v);');
  27312. Add(' i:=integer(v);');
  27313. Add(' s:=string(v);');
  27314. Add(' s:=TCaption(v);');
  27315. Add(' b:=boolean(v);');
  27316. Add(' b:=TYesNo(v);');
  27317. Add(' d:=double(v);');
  27318. Add(' d:=TFloat(v);');
  27319. Add(' c:=char(v);');
  27320. Add(' c:=TChar(v);');
  27321. ConvertProgram;
  27322. CheckSource('TestJSValue_TypeCastToBaseType',
  27323. LinesToStr([ // statements
  27324. 'this.v = undefined;',
  27325. 'this.i = 0;',
  27326. 'this.s = "";',
  27327. 'this.b = false;',
  27328. 'this.d = 0.0;',
  27329. 'this.c = "";',
  27330. '']),
  27331. LinesToStr([ // $mod.$main
  27332. '$mod.i = rtl.trunc($mod.v);',
  27333. '$mod.i = rtl.trunc($mod.v);',
  27334. '$mod.s = "" + $mod.v;',
  27335. '$mod.s = "" + $mod.v;',
  27336. '$mod.b = !($mod.v == false);',
  27337. '$mod.b = !($mod.v == false);',
  27338. '$mod.d = rtl.getNumber($mod.v);',
  27339. '$mod.d = rtl.getNumber($mod.v);',
  27340. '$mod.c = rtl.getChar($mod.v);',
  27341. '$mod.c = rtl.getChar($mod.v);',
  27342. '']));
  27343. end;
  27344. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27345. begin
  27346. StartProgram(false);
  27347. Add([
  27348. 'type',
  27349. ' TArr = array of word;',
  27350. ' TRec = record end;',
  27351. ' TSet = set of boolean;',
  27352. 'procedure Fly(v: jsvalue);',
  27353. 'begin',
  27354. 'end;',
  27355. 'var',
  27356. ' a: TArr;',
  27357. ' r: TRec;',
  27358. ' s: TSet;',
  27359. 'begin',
  27360. ' Fly(jsvalue(a));',
  27361. ' Fly(jsvalue(r));',
  27362. ' Fly(jsvalue(s));',
  27363. '']);
  27364. ConvertProgram;
  27365. CheckSource('TestJSValue_TypecastToJSValue',
  27366. LinesToStr([ // statements
  27367. 'rtl.recNewT(this, "TRec", function () {',
  27368. ' this.$eq = function (b) {',
  27369. ' return true;',
  27370. ' };',
  27371. ' this.$assign = function (s) {',
  27372. ' return this;',
  27373. ' };',
  27374. '});',
  27375. 'this.Fly = function (v) {',
  27376. '};',
  27377. 'this.a = [];',
  27378. 'this.r = this.TRec.$new();',
  27379. 'this.s = {};',
  27380. '']),
  27381. LinesToStr([ // $mod.$main
  27382. '$mod.Fly($mod.a);',
  27383. '$mod.Fly($mod.r);',
  27384. '$mod.Fly($mod.s);',
  27385. '']));
  27386. end;
  27387. procedure TTestModule.TestJSValue_Equal;
  27388. begin
  27389. StartProgram(false);
  27390. Add('type');
  27391. Add(' integer = longint;');
  27392. Add(' TYesNo = boolean;');
  27393. Add(' TFloat = double;');
  27394. Add(' TCaption = string;');
  27395. Add(' TChar = char;');
  27396. Add(' TMulti = JSValue;');
  27397. Add('var');
  27398. Add(' v: jsvalue;');
  27399. Add(' i: integer;');
  27400. Add(' s: TCaption;');
  27401. Add(' b: TYesNo;');
  27402. Add(' d: TFloat;');
  27403. Add(' c: char;');
  27404. Add(' m: TMulti;');
  27405. Add('begin');
  27406. Add(' b:=v=v;');
  27407. Add(' b:=v<>v;');
  27408. Add(' b:=v=1;');
  27409. Add(' b:=v<>1;');
  27410. Add(' b:=2=v;');
  27411. Add(' b:=2<>v;');
  27412. Add(' b:=v=i;');
  27413. Add(' b:=i=v;');
  27414. Add(' b:=v=nil;');
  27415. Add(' b:=nil=v;');
  27416. Add(' b:=v=false;');
  27417. Add(' b:=true=v;');
  27418. Add(' b:=v=b;');
  27419. Add(' b:=b=v;');
  27420. Add(' b:=v=s;');
  27421. Add(' b:=s=v;');
  27422. Add(' b:=v=''foo'';');
  27423. Add(' b:=''''=v;');
  27424. Add(' b:=v=d;');
  27425. Add(' b:=d=v;');
  27426. Add(' b:=v=3.4;');
  27427. Add(' b:=5.6=v;');
  27428. Add(' b:=v=c;');
  27429. Add(' b:=c=v;');
  27430. Add(' b:=m=m;');
  27431. Add(' b:=v=m;');
  27432. Add(' b:=m=v;');
  27433. ConvertProgram;
  27434. CheckSource('TestJSValue_Equal',
  27435. LinesToStr([ // statements
  27436. 'this.v = undefined;',
  27437. 'this.i = 0;',
  27438. 'this.s = "";',
  27439. 'this.b = false;',
  27440. 'this.d = 0.0;',
  27441. 'this.c = "";',
  27442. 'this.m = undefined;',
  27443. '']),
  27444. LinesToStr([ // $mod.$main
  27445. '$mod.b = $mod.v == $mod.v;',
  27446. '$mod.b = $mod.v != $mod.v;',
  27447. '$mod.b = $mod.v == 1;',
  27448. '$mod.b = $mod.v != 1;',
  27449. '$mod.b = 2 == $mod.v;',
  27450. '$mod.b = 2 != $mod.v;',
  27451. '$mod.b = $mod.v == $mod.i;',
  27452. '$mod.b = $mod.i == $mod.v;',
  27453. '$mod.b = $mod.v == null;',
  27454. '$mod.b = null == $mod.v;',
  27455. '$mod.b = $mod.v == false;',
  27456. '$mod.b = true == $mod.v;',
  27457. '$mod.b = $mod.v == $mod.b;',
  27458. '$mod.b = $mod.b == $mod.v;',
  27459. '$mod.b = $mod.v == $mod.s;',
  27460. '$mod.b = $mod.s == $mod.v;',
  27461. '$mod.b = $mod.v == "foo";',
  27462. '$mod.b = "" == $mod.v;',
  27463. '$mod.b = $mod.v == $mod.d;',
  27464. '$mod.b = $mod.d == $mod.v;',
  27465. '$mod.b = $mod.v == 3.4;',
  27466. '$mod.b = 5.6 == $mod.v;',
  27467. '$mod.b = $mod.v == $mod.c;',
  27468. '$mod.b = $mod.c == $mod.v;',
  27469. '$mod.b = $mod.m == $mod.m;',
  27470. '$mod.b = $mod.v == $mod.m;',
  27471. '$mod.b = $mod.m == $mod.v;',
  27472. '']));
  27473. end;
  27474. procedure TTestModule.TestJSValue_If;
  27475. begin
  27476. StartProgram(false);
  27477. Add([
  27478. 'procedure Fly(var u);',
  27479. 'begin',
  27480. ' if jsvalue(u) then ;',
  27481. 'end;',
  27482. 'var',
  27483. ' v: jsvalue;',
  27484. 'begin',
  27485. ' if v then ;',
  27486. ' while v do ;',
  27487. ' repeat until v;',
  27488. '']);
  27489. ConvertProgram;
  27490. CheckSource('TestJSValue_If',
  27491. LinesToStr([ // statements
  27492. 'this.Fly = function (u) {',
  27493. ' if (u.get()) ;',
  27494. '};',
  27495. 'this.v = undefined;',
  27496. '']),
  27497. LinesToStr([ // $mod.$main
  27498. 'if ($mod.v) ;',
  27499. 'while($mod.v){',
  27500. '};',
  27501. 'do{',
  27502. '} while(!$mod.v);',
  27503. '']));
  27504. end;
  27505. procedure TTestModule.TestJSValue_Not;
  27506. begin
  27507. StartProgram(false);
  27508. Add([
  27509. 'var',
  27510. ' v: jsvalue;',
  27511. ' b: boolean;',
  27512. 'begin',
  27513. ' b:=not v;',
  27514. ' if not v then ;',
  27515. ' while not v do ;',
  27516. ' repeat until not v;',
  27517. '']);
  27518. ConvertProgram;
  27519. CheckSource('TestJSValue_If',
  27520. LinesToStr([ // statements
  27521. 'this.v = undefined;',
  27522. 'this.b = false;',
  27523. '']),
  27524. LinesToStr([ // $mod.$main
  27525. '$mod.b=!$mod.v;',
  27526. 'if (!$mod.v) ;',
  27527. 'while(!$mod.v){',
  27528. '};',
  27529. 'do{',
  27530. '} while($mod.v);',
  27531. '']));
  27532. end;
  27533. procedure TTestModule.TestJSValue_Enum;
  27534. begin
  27535. StartProgram(false);
  27536. Add('type');
  27537. Add(' TColor = (red, blue);');
  27538. Add(' TRedBlue = TColor;');
  27539. Add('var');
  27540. Add(' v: jsvalue;');
  27541. Add(' e: TColor;');
  27542. Add('begin');
  27543. Add(' v:=e;');
  27544. Add(' v:=TColor(e);');
  27545. Add(' v:=TRedBlue(e);');
  27546. Add(' e:=TColor(v);');
  27547. Add(' e:=TRedBlue(v);');
  27548. ConvertProgram;
  27549. CheckSource('TestJSValue_Enum',
  27550. LinesToStr([ // statements
  27551. 'this.TColor = {',
  27552. ' "0": "red",',
  27553. ' red: 0,',
  27554. ' "1": "blue",',
  27555. ' blue: 1',
  27556. '};',
  27557. 'this.v = undefined;',
  27558. 'this.e = 0;',
  27559. '']),
  27560. LinesToStr([ // $mod.$main
  27561. '$mod.v = $mod.e;',
  27562. '$mod.v = $mod.e;',
  27563. '$mod.v = $mod.e;',
  27564. '$mod.e = $mod.v;',
  27565. '$mod.e = $mod.v;',
  27566. '']));
  27567. end;
  27568. procedure TTestModule.TestJSValue_ClassInstance;
  27569. begin
  27570. StartProgram(false);
  27571. Add([
  27572. 'type',
  27573. ' TObject = class',
  27574. ' end;',
  27575. ' TBirdObject = TObject;',
  27576. 'var',
  27577. ' v: jsvalue;',
  27578. ' o: TObject;',
  27579. 'begin',
  27580. ' v:=o;',
  27581. ' v:=TObject(o);',
  27582. ' v:=TBirdObject(o);',
  27583. ' o:=TObject(v);',
  27584. ' o:=TBirdObject(v);',
  27585. ' if v is TObject then ;',
  27586. '']);
  27587. ConvertProgram;
  27588. CheckSource('TestJSValue_ClassInstance',
  27589. LinesToStr([ // statements
  27590. 'rtl.createClass(this, "TObject", null, function () {',
  27591. ' this.$init = function () {',
  27592. ' };',
  27593. ' this.$final = function () {',
  27594. ' };',
  27595. '});',
  27596. 'this.v = undefined;',
  27597. 'this.o = null;',
  27598. '']),
  27599. LinesToStr([ // $mod.$main
  27600. '$mod.v = $mod.o;',
  27601. '$mod.v = $mod.o;',
  27602. '$mod.v = $mod.o;',
  27603. '$mod.o = rtl.getObject($mod.v);',
  27604. '$mod.o = rtl.getObject($mod.v);',
  27605. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27606. '']));
  27607. end;
  27608. procedure TTestModule.TestJSValue_ClassOf;
  27609. begin
  27610. StartProgram(false);
  27611. Add([
  27612. 'type',
  27613. ' TClass = class of TObject;',
  27614. ' TObject = class',
  27615. ' end;',
  27616. ' TBirds = class of TBird;',
  27617. ' TBird = class(TObject) end;',
  27618. 'var',
  27619. ' v: jsvalue;',
  27620. ' c: TClass;',
  27621. 'begin',
  27622. ' v:=c;',
  27623. ' v:=TObject;',
  27624. ' v:=TClass(c);',
  27625. ' v:=TBirds(c);',
  27626. ' c:=TClass(v);',
  27627. ' c:=TBirds(v);',
  27628. ' if v is TClass then ;',
  27629. '']);
  27630. ConvertProgram;
  27631. CheckSource('TestJSValue_ClassOf',
  27632. LinesToStr([ // statements
  27633. 'rtl.createClass(this, "TObject", null, function () {',
  27634. ' this.$init = function () {',
  27635. ' };',
  27636. ' this.$final = function () {',
  27637. ' };',
  27638. '});',
  27639. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27640. '});',
  27641. 'this.v = undefined;',
  27642. 'this.c = null;',
  27643. '']),
  27644. LinesToStr([ // $mod.$main
  27645. '$mod.v = $mod.c;',
  27646. '$mod.v = $mod.TObject;',
  27647. '$mod.v = $mod.c;',
  27648. '$mod.v = $mod.c;',
  27649. '$mod.c = rtl.getObject($mod.v);',
  27650. '$mod.c = rtl.getObject($mod.v);',
  27651. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27652. '']));
  27653. end;
  27654. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27655. begin
  27656. StartProgram(false);
  27657. Add([
  27658. 'type',
  27659. ' integer = longint;',
  27660. ' TArray = array of JSValue;',
  27661. ' TArrgh = tarray;',
  27662. ' TArrInt = array of integer;',
  27663. 'var',
  27664. ' v: jsvalue;',
  27665. ' TheArray: tarray = (1,''2'');',
  27666. ' Arr: tarrgh;',
  27667. ' i: integer;',
  27668. ' ArrInt: tarrint;',
  27669. 'begin',
  27670. ' arr:=thearray;',
  27671. ' thearray:=arr;',
  27672. ' setlength(arr,2);',
  27673. ' setlength(thearray,3);',
  27674. ' arr[4]:=v;',
  27675. ' arr[5]:=length(thearray);',
  27676. ' arr[6]:=nil;',
  27677. ' arr[7]:=thearray[8];',
  27678. ' arr[low(arr)]:=high(thearray);',
  27679. ' arr:=arrint;',
  27680. ' arrInt:=tarrint(arr);',
  27681. ' if TheArray = nil then ;',
  27682. ' if nil = TheArray then ;',
  27683. ' if TheArray <> nil then ;',
  27684. ' if nil <> TheArray then ;',
  27685. '']);
  27686. ConvertProgram;
  27687. CheckSource('TestJSValue_ArrayOfJSValue',
  27688. LinesToStr([ // statements
  27689. 'this.v = undefined;',
  27690. 'this.TheArray = [1, "2"];',
  27691. 'this.Arr = [];',
  27692. 'this.i = 0;',
  27693. 'this.ArrInt = [];',
  27694. '']),
  27695. LinesToStr([ // $mod.$main
  27696. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27697. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27698. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27699. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27700. '$mod.Arr[4] = $mod.v;',
  27701. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27702. '$mod.Arr[6] = null;',
  27703. '$mod.Arr[7] = $mod.TheArray[8];',
  27704. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27705. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27706. '$mod.ArrInt = $mod.Arr;',
  27707. 'if (rtl.length($mod.TheArray) === 0) ;',
  27708. 'if (rtl.length($mod.TheArray) === 0) ;',
  27709. 'if (rtl.length($mod.TheArray) > 0) ;',
  27710. 'if (rtl.length($mod.TheArray) > 0) ;',
  27711. '']));
  27712. end;
  27713. procedure TTestModule.TestJSValue_ArrayLit;
  27714. begin
  27715. StartProgram(false);
  27716. Add([
  27717. 'type',
  27718. ' TFlag = (big,small);',
  27719. ' TArray = array of JSValue;',
  27720. ' TObject = class end;',
  27721. ' TClass = class of TObject;',
  27722. 'var',
  27723. ' v: jsvalue;',
  27724. ' a: TArray;',
  27725. ' o: TObject;',
  27726. 'begin',
  27727. ' a:=[];',
  27728. ' a:=[1];',
  27729. ' a:=[1,2];',
  27730. ' a:=[big];',
  27731. ' a:=[1,big];',
  27732. ' a:=[o,nil];',
  27733. '']);
  27734. ConvertProgram;
  27735. CheckSource('TestJSValue_ArrayLit',
  27736. LinesToStr([ // statements
  27737. 'this.TFlag = {',
  27738. ' "0": "big",',
  27739. ' big: 0,',
  27740. ' "1": "small",',
  27741. ' small: 1',
  27742. '};',
  27743. 'rtl.createClass(this, "TObject", null, function () {',
  27744. ' this.$init = function () {',
  27745. ' };',
  27746. ' this.$final = function () {',
  27747. ' };',
  27748. '});',
  27749. 'this.v = undefined;',
  27750. 'this.a = [];',
  27751. 'this.o = null;',
  27752. '']),
  27753. LinesToStr([ // $mod.$main
  27754. '$mod.a = [];',
  27755. '$mod.a = [1];',
  27756. '$mod.a = [1, 2];',
  27757. '$mod.a = [$mod.TFlag.big];',
  27758. '$mod.a = [1, $mod.TFlag.big];',
  27759. '$mod.a = [$mod.o, null];',
  27760. '']));
  27761. end;
  27762. procedure TTestModule.TestJSValue_Params;
  27763. begin
  27764. StartProgram(false);
  27765. Add('type');
  27766. Add(' integer = longint;');
  27767. Add(' TYesNo = boolean;');
  27768. Add(' TFloat = double;');
  27769. Add(' TCaption = string;');
  27770. Add(' TChar = char;');
  27771. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27772. Add('var');
  27773. Add(' l: jsvalue;');
  27774. Add('begin');
  27775. Add(' a:=a;');
  27776. Add(' l:=b;');
  27777. Add(' c:=c;');
  27778. Add(' d:=d;');
  27779. Add(' Result:=l;');
  27780. Add('end;');
  27781. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27782. Add('var');
  27783. Add(' v: jsvalue;');
  27784. Add(' i: integer;');
  27785. Add(' b: TYesNo;');
  27786. Add(' d: TFloat;');
  27787. Add(' s: TCaption;');
  27788. Add(' c: TChar;');
  27789. Add('begin');
  27790. Add(' v:=doit(v,v,v,v);');
  27791. Add(' i:=integer(dosome(i,i));');
  27792. Add(' b:=TYesNo(dosome(b,b));');
  27793. Add(' d:=TFloat(dosome(d,d));');
  27794. Add(' s:=TCaption(dosome(s,s));');
  27795. Add(' c:=TChar(dosome(c,c));');
  27796. ConvertProgram;
  27797. CheckSource('TestJSValue_Params',
  27798. LinesToStr([ // statements
  27799. 'this.DoIt = function (a, b, c, d) {',
  27800. ' var Result = undefined;',
  27801. ' var l = undefined;',
  27802. ' a = a;',
  27803. ' l = b;',
  27804. ' c.set(c.get());',
  27805. ' d.set(d.get());',
  27806. ' Result = l;',
  27807. ' return Result;',
  27808. '};',
  27809. 'this.DoSome = function (a, b) {',
  27810. ' var Result = undefined;',
  27811. ' return Result;',
  27812. '};',
  27813. 'this.v = undefined;',
  27814. 'this.i = 0;',
  27815. 'this.b = false;',
  27816. 'this.d = 0.0;',
  27817. 'this.s = "";',
  27818. 'this.c = "";',
  27819. '']),
  27820. LinesToStr([ // $mod.$main
  27821. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27822. ' p: $mod,',
  27823. ' get: function () {',
  27824. ' return this.p.v;',
  27825. ' },',
  27826. ' set: function (v) {',
  27827. ' this.p.v = v;',
  27828. ' }',
  27829. '}, {',
  27830. ' p: $mod,',
  27831. ' get: function () {',
  27832. ' return this.p.v;',
  27833. ' },',
  27834. ' set: function (v) {',
  27835. ' this.p.v = v;',
  27836. ' }',
  27837. '});',
  27838. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27839. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27840. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27841. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27842. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27843. '']));
  27844. end;
  27845. procedure TTestModule.TestJSValue_UntypedParam;
  27846. begin
  27847. StartProgram(false);
  27848. Add('function DoIt(const a; var b; out c): jsvalue;');
  27849. Add('begin');
  27850. Add(' Result:=a;');
  27851. Add(' Result:=b;');
  27852. Add(' Result:=c;');
  27853. Add(' b:=Result;');
  27854. Add(' c:=Result;');
  27855. Add('end;');
  27856. Add('var i: longint;');
  27857. Add('begin');
  27858. Add(' doit(i,i,i);');
  27859. ConvertProgram;
  27860. CheckSource('TestJSValue_UntypedParam',
  27861. LinesToStr([ // statements
  27862. 'this.DoIt = function (a, b, c) {',
  27863. ' var Result = undefined;',
  27864. ' Result = a;',
  27865. ' Result = b.get();',
  27866. ' Result = c.get();',
  27867. ' b.set(Result);',
  27868. ' c.set(Result);',
  27869. ' return Result;',
  27870. '};',
  27871. 'this.i = 0;',
  27872. '']),
  27873. LinesToStr([ // $mod.$main
  27874. '$mod.DoIt($mod.i, {',
  27875. ' p: $mod,',
  27876. ' get: function () {',
  27877. ' return this.p.i;',
  27878. ' },',
  27879. ' set: function (v) {',
  27880. ' this.p.i = v;',
  27881. ' }',
  27882. '}, {',
  27883. ' p: $mod,',
  27884. ' get: function () {',
  27885. ' return this.p.i;',
  27886. ' },',
  27887. ' set: function (v) {',
  27888. ' this.p.i = v;',
  27889. ' }',
  27890. '});',
  27891. '']));
  27892. end;
  27893. procedure TTestModule.TestJSValue_FuncResultType;
  27894. begin
  27895. StartProgram(false);
  27896. Add('type');
  27897. Add(' integer = longint;');
  27898. Add(' TJSValueArray = array of JSValue;');
  27899. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27900. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27901. Add('begin');
  27902. Add(' while Compare(P,aList[0])>0 do ;');
  27903. Add('end;');
  27904. Add('var');
  27905. Add(' Compare: TListSortCompare;');
  27906. Add(' V: JSValue;');
  27907. Add(' i: integer;');
  27908. Add('begin');
  27909. Add(' if Compare(V,V)>0 then ;');
  27910. Add(' if Compare(i,i)>1 then ;');
  27911. Add(' if Compare(nil,false)>2 then ;');
  27912. Add(' if Compare(1,true)>3 then ;');
  27913. ConvertProgram;
  27914. CheckSource('TestJSValue_UntypedParam',
  27915. LinesToStr([ // statements
  27916. 'this.Sort = function (P, aList, Compare) {',
  27917. ' while (Compare(P, aList[0]) > 0) {',
  27918. ' };',
  27919. '};',
  27920. 'this.Compare = null;',
  27921. 'this.V = undefined;',
  27922. 'this.i = 0;',
  27923. '']),
  27924. LinesToStr([ // $mod.$main
  27925. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27926. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27927. 'if ($mod.Compare(null, false) > 2) ;',
  27928. 'if ($mod.Compare(1, true) > 3) ;',
  27929. '']));
  27930. end;
  27931. procedure TTestModule.TestJSValue_ProcType_Assign;
  27932. begin
  27933. StartProgram(false);
  27934. Add('type');
  27935. Add(' integer = longint;');
  27936. Add(' TObject = class');
  27937. Add(' class function GetGlob: integer;');
  27938. Add(' function Getter: integer;');
  27939. Add(' end;');
  27940. Add('class function TObject.GetGlob: integer;');
  27941. Add('var v1: jsvalue;');
  27942. Add('begin');
  27943. Add(' v1:=@GetGlob;');
  27944. Add(' v1:[email protected];');
  27945. Add('end;');
  27946. Add('function TObject.Getter: integer;');
  27947. Add('var v2: jsvalue;');
  27948. Add('begin');
  27949. Add(' v2:=@Getter;');
  27950. Add(' v2:[email protected];');
  27951. Add(' v2:=@GetGlob;');
  27952. Add(' v2:[email protected];');
  27953. Add('end;');
  27954. Add('function GetIt(i: integer): integer;');
  27955. Add('var v3: jsvalue;');
  27956. Add('begin');
  27957. Add(' v3:=@GetIt;');
  27958. Add('end;');
  27959. Add('var');
  27960. Add(' V: JSValue;');
  27961. Add(' o: TObject;');
  27962. Add('begin');
  27963. Add(' v:=@GetIt;');
  27964. Add(' v:[email protected];');
  27965. Add(' v:[email protected];');
  27966. ConvertProgram;
  27967. CheckSource('TestJSValue_ProcType_Assign',
  27968. LinesToStr([ // statements
  27969. 'rtl.createClass(this, "TObject", null, function () {',
  27970. ' this.$init = function () {',
  27971. ' };',
  27972. ' this.$final = function () {',
  27973. ' };',
  27974. ' this.GetGlob = function () {',
  27975. ' var Result = 0;',
  27976. ' var v1 = undefined;',
  27977. ' v1 = rtl.createCallback(this, "GetGlob");',
  27978. ' v1 = rtl.createCallback(this, "GetGlob");',
  27979. ' return Result;',
  27980. ' };',
  27981. ' this.Getter = function () {',
  27982. ' var Result = 0;',
  27983. ' var v2 = undefined;',
  27984. ' v2 = rtl.createCallback(this, "Getter");',
  27985. ' v2 = rtl.createCallback(this, "Getter");',
  27986. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27987. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27988. ' return Result;',
  27989. ' };',
  27990. '});',
  27991. 'this.GetIt = function (i) {',
  27992. ' var Result = 0;',
  27993. ' var v3 = undefined;',
  27994. ' v3 = $mod.GetIt;',
  27995. ' return Result;',
  27996. '};',
  27997. 'this.V = undefined;',
  27998. 'this.o = null;',
  27999. '']),
  28000. LinesToStr([ // $mod.$main
  28001. '$mod.V = $mod.GetIt;',
  28002. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  28003. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  28004. '']));
  28005. end;
  28006. procedure TTestModule.TestJSValue_ProcType_Equal;
  28007. begin
  28008. StartProgram(false);
  28009. Add('type');
  28010. Add(' integer = longint;');
  28011. Add(' TObject = class');
  28012. Add(' class function GetGlob: integer;');
  28013. Add(' function Getter: integer;');
  28014. Add(' end;');
  28015. Add('class function TObject.GetGlob: integer;');
  28016. Add('var v1: jsvalue;');
  28017. Add('begin');
  28018. Add(' if v1=@GetGlob then;');
  28019. Add(' if [email protected] then ;');
  28020. Add('end;');
  28021. Add('function TObject.Getter: integer;');
  28022. Add('var v2: jsvalue;');
  28023. Add('begin');
  28024. Add(' if v2=@Getter then;');
  28025. Add(' if [email protected] then ;');
  28026. Add(' if v2=@GetGlob then;');
  28027. Add(' if [email protected] then;');
  28028. Add('end;');
  28029. Add('function GetIt(i: integer): integer;');
  28030. Add('var v3: jsvalue;');
  28031. Add('begin');
  28032. Add(' if v3=@GetIt then;');
  28033. Add('end;');
  28034. Add('var');
  28035. Add(' V: JSValue;');
  28036. Add(' o: TObject;');
  28037. Add('begin');
  28038. Add(' if v=@GetIt then;');
  28039. Add(' if [email protected] then;');
  28040. Add(' if [email protected] then;');
  28041. Add(' if @GetIt=v then;');
  28042. Add(' if @o.Getter=v then;');
  28043. Add(' if @o.GetGlob=v then;');
  28044. ConvertProgram;
  28045. CheckSource('TestJSValue_ProcType_Equal',
  28046. LinesToStr([ // statements
  28047. 'rtl.createClass(this, "TObject", null, function () {',
  28048. ' this.$init = function () {',
  28049. ' };',
  28050. ' this.$final = function () {',
  28051. ' };',
  28052. ' this.GetGlob = function () {',
  28053. ' var Result = 0;',
  28054. ' var v1 = undefined;',
  28055. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28056. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28057. ' return Result;',
  28058. ' };',
  28059. ' this.Getter = function () {',
  28060. ' var Result = 0;',
  28061. ' var v2 = undefined;',
  28062. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28063. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28064. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28065. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28066. ' return Result;',
  28067. ' };',
  28068. '});',
  28069. 'this.GetIt = function (i) {',
  28070. ' var Result = 0;',
  28071. ' var v3 = undefined;',
  28072. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28073. ' return Result;',
  28074. '};',
  28075. 'this.V = undefined;',
  28076. 'this.o = null;',
  28077. '']),
  28078. LinesToStr([ // $mod.$main
  28079. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28080. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28081. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28082. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28083. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28084. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28085. '']));
  28086. end;
  28087. procedure TTestModule.TestJSValue_ProcType_Param;
  28088. begin
  28089. StartProgram(false);
  28090. Add([
  28091. 'type',
  28092. ' variant = jsvalue;',
  28093. ' TArrVariant = array of variant;',
  28094. ' TArrVar2 = TArrVariant;',
  28095. ' TFuncInt = function: longint;',
  28096. 'function GetIt: longint;',
  28097. 'begin',
  28098. 'end;',
  28099. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28100. 'var v: variant;',
  28101. 'begin',
  28102. ' v:=arr[1];',
  28103. 'end;',
  28104. 'var s: string;',
  28105. 'begin',
  28106. ' DoIt(GetIt,[]);',
  28107. ' DoIt(@GetIt,[]);',
  28108. ' DoIt(1,[s,GetIt]);',
  28109. ' DoIt(1,[s,@GetIt]);',
  28110. '']);
  28111. ConvertProgram;
  28112. CheckSource('TestJSValue_ProcType_Param',
  28113. LinesToStr([ // statements
  28114. 'this.GetIt = function () {',
  28115. ' var Result = 0;',
  28116. ' return Result;',
  28117. '};',
  28118. 'this.DoIt = function (p, Arr) {',
  28119. ' var v = undefined;',
  28120. ' v = Arr[1];',
  28121. '};',
  28122. 'this.s = "";',
  28123. '']),
  28124. LinesToStr([ // $mod.$main
  28125. '$mod.DoIt($mod.GetIt(), []);',
  28126. '$mod.DoIt($mod.GetIt, []);',
  28127. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28128. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28129. '']));
  28130. end;
  28131. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28132. begin
  28133. StartProgram(false);
  28134. Add([
  28135. 'var',
  28136. ' v: JSValue;',
  28137. ' p: Pointer;',
  28138. 'begin',
  28139. ' p:=v;',
  28140. '']);
  28141. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28142. nIncompatibleTypesGotExpected);
  28143. ConvertProgram;
  28144. end;
  28145. procedure TTestModule.TestJSValue_OverloadDouble;
  28146. begin
  28147. StartProgram(false);
  28148. Add([
  28149. 'type',
  28150. ' integer = longint;',
  28151. ' tdatetime = double;',
  28152. 'procedure DoIt(d: double); begin end;',
  28153. 'procedure DoIt(v: jsvalue); begin end;',
  28154. 'var',
  28155. ' d: double;',
  28156. ' dt: tdatetime;',
  28157. ' i: integer;',
  28158. ' b: byte;',
  28159. ' shi: shortint;',
  28160. ' w: word;',
  28161. ' smi: smallint;',
  28162. ' lw: longword;',
  28163. ' li: longint;',
  28164. ' ni: nativeint;',
  28165. ' nu: nativeuint;',
  28166. 'begin',
  28167. ' DoIt(d);',
  28168. ' DoIt(dt);',
  28169. ' DoIt(i);',
  28170. ' DoIt(b);',
  28171. ' DoIt(shi);',
  28172. ' DoIt(w);',
  28173. ' DoIt(smi);',
  28174. ' DoIt(lw);',
  28175. ' DoIt(li);',
  28176. ' DoIt(ni);',
  28177. ' DoIt(nu);',
  28178. '']);
  28179. ConvertProgram;
  28180. CheckSource('TestJSValue_OverloadDouble',
  28181. LinesToStr([ // statements
  28182. 'this.DoIt = function (d) {',
  28183. '};',
  28184. 'this.DoIt$1 = function (v) {',
  28185. '};',
  28186. 'this.d = 0.0;',
  28187. 'this.dt = 0.0;',
  28188. 'this.i = 0;',
  28189. 'this.b = 0;',
  28190. 'this.shi = 0;',
  28191. 'this.w = 0;',
  28192. 'this.smi = 0;',
  28193. 'this.lw = 0;',
  28194. 'this.li = 0;',
  28195. 'this.ni = 0;',
  28196. 'this.nu = 0;',
  28197. '']),
  28198. LinesToStr([ // $mod.$main
  28199. '$mod.DoIt($mod.d);',
  28200. '$mod.DoIt($mod.dt);',
  28201. '$mod.DoIt$1($mod.i);',
  28202. '$mod.DoIt$1($mod.b);',
  28203. '$mod.DoIt$1($mod.shi);',
  28204. '$mod.DoIt$1($mod.w);',
  28205. '$mod.DoIt$1($mod.smi);',
  28206. '$mod.DoIt$1($mod.lw);',
  28207. '$mod.DoIt$1($mod.li);',
  28208. '$mod.DoIt$1($mod.ni);',
  28209. '$mod.DoIt$1($mod.nu);',
  28210. '']));
  28211. end;
  28212. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28213. begin
  28214. StartProgram(false);
  28215. Add([
  28216. 'type',
  28217. ' integer = longint;',
  28218. ' int53 = nativeint;',
  28219. ' tdatetime = double;',
  28220. 'procedure DoIt(n: nativeint); begin end;',
  28221. 'procedure DoIt(v: jsvalue); begin end;',
  28222. 'var',
  28223. ' d: double;',
  28224. ' dt: tdatetime;',
  28225. ' i: integer;',
  28226. ' b: byte;',
  28227. ' shi: shortint;',
  28228. ' w: word;',
  28229. ' smi: smallint;',
  28230. ' lw: longword;',
  28231. ' li: longint;',
  28232. ' ni: nativeint;',
  28233. ' nu: nativeuint;',
  28234. 'begin',
  28235. ' DoIt(d);',
  28236. ' DoIt(dt);',
  28237. ' DoIt(i);',
  28238. ' DoIt(b);',
  28239. ' DoIt(shi);',
  28240. ' DoIt(w);',
  28241. ' DoIt(smi);',
  28242. ' DoIt(lw);',
  28243. ' DoIt(li);',
  28244. ' DoIt(ni);',
  28245. ' DoIt(nu);',
  28246. '']);
  28247. ConvertProgram;
  28248. CheckSource('TestJSValue_OverloadNativeInt',
  28249. LinesToStr([ // statements
  28250. 'this.DoIt = function (n) {',
  28251. '};',
  28252. 'this.DoIt$1 = function (v) {',
  28253. '};',
  28254. 'this.d = 0.0;',
  28255. 'this.dt = 0.0;',
  28256. 'this.i = 0;',
  28257. 'this.b = 0;',
  28258. 'this.shi = 0;',
  28259. 'this.w = 0;',
  28260. 'this.smi = 0;',
  28261. 'this.lw = 0;',
  28262. 'this.li = 0;',
  28263. 'this.ni = 0;',
  28264. 'this.nu = 0;',
  28265. '']),
  28266. LinesToStr([ // $mod.$main
  28267. '$mod.DoIt$1($mod.d);',
  28268. '$mod.DoIt$1($mod.dt);',
  28269. '$mod.DoIt($mod.i);',
  28270. '$mod.DoIt($mod.b);',
  28271. '$mod.DoIt($mod.shi);',
  28272. '$mod.DoIt($mod.w);',
  28273. '$mod.DoIt($mod.smi);',
  28274. '$mod.DoIt($mod.lw);',
  28275. '$mod.DoIt($mod.li);',
  28276. '$mod.DoIt($mod.ni);',
  28277. '$mod.DoIt($mod.nu);',
  28278. '']));
  28279. end;
  28280. procedure TTestModule.TestJSValue_OverloadWord;
  28281. begin
  28282. StartProgram(false);
  28283. Add([
  28284. 'type',
  28285. ' integer = longint;',
  28286. ' int53 = nativeint;',
  28287. ' tdatetime = double;',
  28288. 'procedure DoIt(w: word); begin end;',
  28289. 'procedure DoIt(v: jsvalue); begin end;',
  28290. 'var',
  28291. ' d: double;',
  28292. ' dt: tdatetime;',
  28293. ' i: integer;',
  28294. ' b: byte;',
  28295. ' shi: shortint;',
  28296. ' w: word;',
  28297. ' smi: smallint;',
  28298. ' lw: longword;',
  28299. ' li: longint;',
  28300. ' ni: nativeint;',
  28301. ' nu: nativeuint;',
  28302. 'begin',
  28303. ' DoIt(d);',
  28304. ' DoIt(dt);',
  28305. ' DoIt(i);',
  28306. ' DoIt(b);',
  28307. ' DoIt(shi);',
  28308. ' DoIt(w);',
  28309. ' DoIt(smi);',
  28310. ' DoIt(lw);',
  28311. ' DoIt(li);',
  28312. ' DoIt(ni);',
  28313. ' DoIt(nu);',
  28314. '']);
  28315. ConvertProgram;
  28316. CheckSource('TestJSValue_OverloadWord',
  28317. LinesToStr([ // statements
  28318. 'this.DoIt = function (w) {',
  28319. '};',
  28320. 'this.DoIt$1 = function (v) {',
  28321. '};',
  28322. 'this.d = 0.0;',
  28323. 'this.dt = 0.0;',
  28324. 'this.i = 0;',
  28325. 'this.b = 0;',
  28326. 'this.shi = 0;',
  28327. 'this.w = 0;',
  28328. 'this.smi = 0;',
  28329. 'this.lw = 0;',
  28330. 'this.li = 0;',
  28331. 'this.ni = 0;',
  28332. 'this.nu = 0;',
  28333. '']),
  28334. LinesToStr([ // $mod.$main
  28335. '$mod.DoIt$1($mod.d);',
  28336. '$mod.DoIt$1($mod.dt);',
  28337. '$mod.DoIt$1($mod.i);',
  28338. '$mod.DoIt($mod.b);',
  28339. '$mod.DoIt($mod.shi);',
  28340. '$mod.DoIt($mod.w);',
  28341. '$mod.DoIt$1($mod.smi);',
  28342. '$mod.DoIt$1($mod.lw);',
  28343. '$mod.DoIt$1($mod.li);',
  28344. '$mod.DoIt$1($mod.ni);',
  28345. '$mod.DoIt$1($mod.nu);',
  28346. '']));
  28347. end;
  28348. procedure TTestModule.TestJSValue_OverloadString;
  28349. begin
  28350. StartProgram(false);
  28351. Add([
  28352. 'type',
  28353. ' uni = string;',
  28354. ' WChar = char;',
  28355. 'procedure DoIt(s: string); begin end;',
  28356. 'procedure DoIt(v: jsvalue); begin end;',
  28357. 'var',
  28358. ' s: string;',
  28359. ' c: char;',
  28360. ' u: uni;',
  28361. 'begin',
  28362. ' DoIt(s);',
  28363. ' DoIt(c);',
  28364. ' DoIt(u);',
  28365. '']);
  28366. ConvertProgram;
  28367. CheckSource('TestJSValue_OverloadString',
  28368. LinesToStr([ // statements
  28369. 'this.DoIt = function (s) {',
  28370. '};',
  28371. 'this.DoIt$1 = function (v) {',
  28372. '};',
  28373. 'this.s = "";',
  28374. 'this.c = "";',
  28375. 'this.u = "";',
  28376. '']),
  28377. LinesToStr([ // $mod.$main
  28378. '$mod.DoIt($mod.s);',
  28379. '$mod.DoIt($mod.c);',
  28380. '$mod.DoIt($mod.u);',
  28381. '']));
  28382. end;
  28383. procedure TTestModule.TestJSValue_OverloadChar;
  28384. begin
  28385. StartProgram(false);
  28386. Add([
  28387. 'type',
  28388. ' uni = string;',
  28389. ' WChar = char;',
  28390. 'procedure DoIt(c: char); begin end;',
  28391. 'procedure DoIt(v: jsvalue); begin end;',
  28392. 'var',
  28393. ' s: string;',
  28394. ' c: char;',
  28395. ' u: uni;',
  28396. 'begin',
  28397. ' DoIt(s);',
  28398. ' DoIt(c);',
  28399. ' DoIt(u);',
  28400. '']);
  28401. ConvertProgram;
  28402. CheckSource('TestJSValue_OverloadChar',
  28403. LinesToStr([ // statements
  28404. 'this.DoIt = function (c) {',
  28405. '};',
  28406. 'this.DoIt$1 = function (v) {',
  28407. '};',
  28408. 'this.s = "";',
  28409. 'this.c = "";',
  28410. 'this.u = "";',
  28411. '']),
  28412. LinesToStr([ // $mod.$main
  28413. '$mod.DoIt$1($mod.s);',
  28414. '$mod.DoIt($mod.c);',
  28415. '$mod.DoIt$1($mod.u);',
  28416. '']));
  28417. end;
  28418. procedure TTestModule.TestJSValue_OverloadPointer;
  28419. begin
  28420. StartProgram(false);
  28421. Add([
  28422. 'type',
  28423. ' TObject = class end;',
  28424. 'procedure DoIt(p: pointer); begin end;',
  28425. 'procedure DoIt(v: jsvalue); begin end;',
  28426. 'var',
  28427. ' o: TObject;',
  28428. 'begin',
  28429. ' DoIt(o);',
  28430. '']);
  28431. ConvertProgram;
  28432. CheckSource('TestJSValue_OverloadPointer',
  28433. LinesToStr([ // statements
  28434. 'rtl.createClass(this, "TObject", null, function () {',
  28435. ' this.$init = function () {',
  28436. ' };',
  28437. ' this.$final = function () {',
  28438. ' };',
  28439. '});',
  28440. 'this.DoIt = function (p) {',
  28441. '};',
  28442. 'this.DoIt$1 = function (v) {',
  28443. '};',
  28444. 'this.o = null;',
  28445. '']),
  28446. LinesToStr([ // $mod.$main
  28447. '$mod.DoIt($mod.o);',
  28448. '']));
  28449. end;
  28450. procedure TTestModule.TestJSValue_ForIn;
  28451. begin
  28452. StartProgram(false);
  28453. Add([
  28454. 'var',
  28455. ' v: JSValue;',
  28456. ' key: string;',
  28457. 'begin',
  28458. ' for key in v do begin',
  28459. ' if key=''abc'' then ;',
  28460. ' end;',
  28461. '']);
  28462. ConvertProgram;
  28463. CheckSource('TestJSValue_ForIn',
  28464. LinesToStr([ // statements
  28465. 'this.v = undefined;',
  28466. 'this.key = "";',
  28467. '']),
  28468. LinesToStr([ // $mod.$main
  28469. 'for ($mod.key in $mod.v) {',
  28470. ' if ($mod.key === "abc") ;',
  28471. '};',
  28472. '']));
  28473. end;
  28474. procedure TTestModule.TestRTTI_IntRange;
  28475. begin
  28476. WithTypeInfo:=true;
  28477. StartProgram(true,[supTypeInfo]);
  28478. Add([
  28479. '{$modeswitch externalclass}',
  28480. 'type',
  28481. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28482. ' TColor = type TGraphicsColor;',
  28483. 'var',
  28484. ' p: TTypeInfo;',
  28485. ' k: TTypeKind;',
  28486. 'begin',
  28487. ' p:=typeinfo(TGraphicsColor);',
  28488. ' p:=typeinfo(TColor);',
  28489. ' k:=GetTypeKind(TGraphicsColor);',
  28490. ' k:=GetTypeKind(TColor);',
  28491. '']);
  28492. ConvertProgram;
  28493. CheckSource('TestRTTI_IntRange',
  28494. LinesToStr([ // statements
  28495. 'this.$rtti.$Int("TGraphicsColor", {',
  28496. ' minvalue: -2147483648,',
  28497. ' maxvalue: 2147483647,',
  28498. ' ordtype: 4',
  28499. '});',
  28500. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28501. 'this.p = null;',
  28502. 'this.k = 0;',
  28503. '']),
  28504. LinesToStr([ // $mod.$main
  28505. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28506. '$mod.p = $mod.$rtti["TColor"];',
  28507. '$mod.k = 1;',
  28508. '$mod.k = 1;',
  28509. '']));
  28510. end;
  28511. procedure TTestModule.TestRTTI_Double;
  28512. begin
  28513. WithTypeInfo:=true;
  28514. StartProgram(true,[supTypeInfo]);
  28515. Add([
  28516. '{$modeswitch externalclass}',
  28517. 'type',
  28518. ' TFloat = type double;',
  28519. 'var',
  28520. ' p: TTypeInfo;',
  28521. 'begin',
  28522. ' p:=typeinfo(double);',
  28523. ' p:=typeinfo(TFloat);',
  28524. '']);
  28525. ConvertProgram;
  28526. CheckSource('TestRTTI_Double',
  28527. LinesToStr([ // statements
  28528. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28529. 'this.p = null;',
  28530. '']),
  28531. LinesToStr([ // $mod.$main
  28532. '$mod.p = rtl.double;',
  28533. '$mod.p = $mod.$rtti["TFloat"];',
  28534. '']));
  28535. end;
  28536. procedure TTestModule.TestRTTI_ProcType;
  28537. begin
  28538. WithTypeInfo:=true;
  28539. StartProgram(false);
  28540. Add('type');
  28541. Add(' TProcA = procedure;');
  28542. Add(' TMethodB = procedure of object;');
  28543. Add(' TProcC = procedure; varargs;');
  28544. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28545. Add(' TProcE = function: nativeint;');
  28546. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28547. Add('var p: pointer;');
  28548. Add('begin');
  28549. Add(' p:=typeinfo(tproca);');
  28550. ConvertProgram;
  28551. CheckSource('TestRTTI_ProcType',
  28552. LinesToStr([ // statements
  28553. 'this.$rtti.$ProcVar("TProcA", {',
  28554. ' procsig: rtl.newTIProcSig([])',
  28555. '});',
  28556. 'this.$rtti.$MethodVar("TMethodB", {',
  28557. ' procsig: rtl.newTIProcSig([]),',
  28558. ' methodkind: 0',
  28559. '});',
  28560. 'this.$rtti.$ProcVar("TProcC", {',
  28561. ' procsig: rtl.newTIProcSig([], null, 2)',
  28562. '});',
  28563. 'this.$rtti.$ProcVar("TProcD", {',
  28564. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28565. '});',
  28566. 'this.$rtti.$ProcVar("TProcE", {',
  28567. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  28568. '});',
  28569. 'this.$rtti.$ProcVar("TProcF", {',
  28570. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28571. '});',
  28572. 'this.p = null;',
  28573. '']),
  28574. LinesToStr([ // $mod.$main
  28575. '$mod.p = $mod.$rtti["TProcA"];',
  28576. '']));
  28577. end;
  28578. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28579. begin
  28580. WithTypeInfo:=true;
  28581. AddModuleWithIntfImplSrc('unit2.pas',
  28582. LinesToStr([
  28583. 'type',
  28584. ' TObject = class end;'
  28585. ]),
  28586. '');
  28587. StartUnit(true);
  28588. Add('interface');
  28589. Add('uses unit2;');
  28590. Add('type');
  28591. Add(' TProcA = function(o: tobject): tobject;');
  28592. Add('implementation');
  28593. Add('type');
  28594. Add(' TProcB = function(o: tobject): tobject;');
  28595. Add('var p: Pointer;');
  28596. Add('initialization');
  28597. Add(' p:=typeinfo(tproca);');
  28598. Add(' p:=typeinfo(tprocb);');
  28599. ConvertUnit;
  28600. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28601. LinesToStr([ // statements
  28602. 'var $impl = $mod.$impl;',
  28603. 'this.$rtti.$ProcVar("TProcA", {',
  28604. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28605. '});',
  28606. '']),
  28607. LinesToStr([ // this.$init
  28608. '$impl.p = $mod.$rtti["TProcA"];',
  28609. '$impl.p = $mod.$rtti["TProcB"];',
  28610. '']),
  28611. LinesToStr([ // implementation
  28612. '$mod.$rtti.$ProcVar("TProcB", {',
  28613. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28614. '});',
  28615. '$impl.p = null;',
  28616. '']) );
  28617. end;
  28618. procedure TTestModule.TestRTTI_EnumAndSetType;
  28619. begin
  28620. WithTypeInfo:=true;
  28621. StartProgram(false);
  28622. Add('type');
  28623. Add(' TFlag = (light,dark);');
  28624. Add(' TFlags = set of TFlag;');
  28625. Add(' TProc = function(f: TFlags): TFlag;');
  28626. Add('var p: pointer;');
  28627. Add('begin');
  28628. Add(' p:=typeinfo(tflag);');
  28629. Add(' p:=typeinfo(tflags);');
  28630. ConvertProgram;
  28631. CheckSource('TestRTTI_EnumAndType',
  28632. LinesToStr([ // statements
  28633. 'this.TFlag = {',
  28634. ' "0": "light",',
  28635. ' light: 0,',
  28636. ' "1": "dark",',
  28637. ' dark: 1',
  28638. '};',
  28639. 'this.$rtti.$Enum("TFlag", {',
  28640. ' minvalue: 0,',
  28641. ' maxvalue: 1,',
  28642. ' ordtype: 1,',
  28643. ' enumtype: this.TFlag',
  28644. '});',
  28645. 'this.$rtti.$Set("TFlags", {',
  28646. ' comptype: this.$rtti["TFlag"]',
  28647. '});',
  28648. 'this.$rtti.$ProcVar("TProc", {',
  28649. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28650. '});',
  28651. 'this.p = null;',
  28652. '']),
  28653. LinesToStr([ // $mod.$main
  28654. '$mod.p = $mod.$rtti["TFlag"];',
  28655. '$mod.p = $mod.$rtti["TFlags"];',
  28656. '']));
  28657. end;
  28658. procedure TTestModule.TestRTTI_EnumRange;
  28659. begin
  28660. WithTypeInfo:=true;
  28661. StartProgram(false);
  28662. Add([
  28663. 'type',
  28664. ' TCol = (red,green,blue);',
  28665. ' TColRg = green..blue;',
  28666. ' TSetOfColRg = set of TColRg;',
  28667. 'var p: pointer;',
  28668. 'begin',
  28669. ' p:=typeinfo(tcolrg);',
  28670. ' p:=typeinfo(tsetofcolrg);',
  28671. '']);
  28672. ConvertProgram;
  28673. end;
  28674. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28675. begin
  28676. WithTypeInfo:=true;
  28677. StartProgram(false);
  28678. Add('type');
  28679. Add(' TFlags = set of (red, green);');
  28680. Add('var');
  28681. Add(' f: TFlags;');
  28682. Add('begin');
  28683. Add(' Include(f,red);');
  28684. ConvertProgram;
  28685. CheckSource('TestRTTI_AnonymousEnumType',
  28686. LinesToStr([ // statements
  28687. 'this.TFlags$a = {',
  28688. ' "0": "red",',
  28689. ' red: 0,',
  28690. ' "1": "green",',
  28691. ' green: 1',
  28692. '};',
  28693. 'this.$rtti.$Enum("TFlags$a", {',
  28694. ' minvalue: 0,',
  28695. ' maxvalue: 1,',
  28696. ' ordtype: 1,',
  28697. ' enumtype: this.TFlags$a',
  28698. '});',
  28699. 'this.$rtti.$Set("TFlags", {',
  28700. ' comptype: this.$rtti["TFlags$a"]',
  28701. '});',
  28702. 'this.f = {};',
  28703. '']),
  28704. LinesToStr([
  28705. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28706. '']));
  28707. end;
  28708. procedure TTestModule.TestRTTI_StaticArray;
  28709. begin
  28710. WithTypeInfo:=true;
  28711. StartProgram(false);
  28712. Add('type');
  28713. Add(' TFlag = (light,dark);');
  28714. Add(' TFlagNames = array[TFlag] of string;');
  28715. Add(' TBoolNames = array[boolean] of string;');
  28716. Add(' TByteArray = array[1..32768] of byte;');
  28717. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28718. Add('var p: pointer;');
  28719. Add('begin');
  28720. Add(' p:=typeinfo(TFlagNames);');
  28721. Add(' p:=typeinfo(TBoolNames);');
  28722. ConvertProgram;
  28723. CheckSource('TestRTTI_StaticArray',
  28724. LinesToStr([ // statements
  28725. 'this.TFlag = {',
  28726. ' "0": "light",',
  28727. ' light: 0,',
  28728. ' "1": "dark",',
  28729. ' dark: 1',
  28730. '};',
  28731. 'this.$rtti.$Enum("TFlag", {',
  28732. ' minvalue: 0,',
  28733. ' maxvalue: 1,',
  28734. ' ordtype: 1,',
  28735. ' enumtype: this.TFlag',
  28736. '});',
  28737. 'this.$rtti.$StaticArray("TFlagNames", {',
  28738. ' dims: [2],',
  28739. ' eltype: rtl.string',
  28740. '});',
  28741. 'this.$rtti.$StaticArray("TBoolNames", {',
  28742. ' dims: [2],',
  28743. ' eltype: rtl.string',
  28744. '});',
  28745. 'this.$rtti.$StaticArray("TByteArray", {',
  28746. ' dims: [32768],',
  28747. ' eltype: rtl.byte',
  28748. '});',
  28749. 'this.$rtti.$ProcVar("TProc", {',
  28750. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28751. '});',
  28752. 'this.p = null;',
  28753. '']),
  28754. LinesToStr([ // $mod.$main
  28755. '$mod.p = $mod.$rtti["TFlagNames"];',
  28756. '$mod.p = $mod.$rtti["TBoolNames"];',
  28757. '']));
  28758. end;
  28759. procedure TTestModule.TestRTTI_DynArray;
  28760. begin
  28761. WithTypeInfo:=true;
  28762. StartProgram(false);
  28763. Add('type');
  28764. Add(' TArrStr = array of string;');
  28765. Add(' TArr2Dim = array of tarrstr;');
  28766. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28767. Add('var p: pointer;');
  28768. Add('begin');
  28769. Add(' p:=typeinfo(tarrstr);');
  28770. Add(' p:=typeinfo(tarr2dim);');
  28771. ConvertProgram;
  28772. CheckSource('TestRTTI_DynArray',
  28773. LinesToStr([ // statements
  28774. 'this.$rtti.$DynArray("TArrStr", {',
  28775. ' eltype: rtl.string',
  28776. '});',
  28777. 'this.$rtti.$DynArray("TArr2Dim", {',
  28778. ' eltype: this.$rtti["TArrStr"]',
  28779. '});',
  28780. 'this.$rtti.$ProcVar("TProc", {',
  28781. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28782. '});',
  28783. 'this.p = null;',
  28784. '']),
  28785. LinesToStr([ // $mod.$main
  28786. '$mod.p = $mod.$rtti["TArrStr"];',
  28787. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28788. '']));
  28789. end;
  28790. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28791. begin
  28792. WithTypeInfo:=true;
  28793. StartProgram(false);
  28794. Add('type');
  28795. Add(' TArr = array of array of longint;');
  28796. Add('var a: TArr;');
  28797. Add('begin');
  28798. ConvertProgram;
  28799. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28800. LinesToStr([ // statements
  28801. 'this.$rtti.$DynArray("TArr$a", {',
  28802. ' eltype: rtl.longint',
  28803. '});',
  28804. 'this.$rtti.$DynArray("TArr", {',
  28805. ' eltype: this.$rtti["TArr$a"]',
  28806. '});',
  28807. 'this.a = [];',
  28808. '']),
  28809. LinesToStr([ // $mod.$main
  28810. ]));
  28811. end;
  28812. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28813. begin
  28814. WithTypeInfo:=true;
  28815. StartProgram(false);
  28816. Add('type');
  28817. Add(' TObject = class');
  28818. Add(' published');
  28819. Add(' procedure Proc; virtual; abstract;');
  28820. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28821. Add(' end;');
  28822. Add('begin');
  28823. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28824. nDuplicatePublishedMethodXAtY);
  28825. ConvertProgram;
  28826. end;
  28827. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  28828. begin
  28829. WithTypeInfo:=true;
  28830. StartUnit(false);
  28831. Add([
  28832. 'interface',
  28833. 'type',
  28834. ' TObject = class',
  28835. ' end;',
  28836. ' {$M+}',
  28837. ' TBird = class',
  28838. ' procedure Fly;',
  28839. ' end;',
  28840. ' {$M-}',
  28841. 'type',
  28842. ' TEagle = class(TBird)',
  28843. ' procedure Fly;',
  28844. ' end;',
  28845. 'implementation',
  28846. 'procedure TBird.Fly;',
  28847. 'begin',
  28848. 'end;',
  28849. 'procedure TEagle.Fly;',
  28850. 'begin',
  28851. 'end;',
  28852. '']);
  28853. ConvertUnit;
  28854. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  28855. LinesToStr([ // statements
  28856. 'rtl.createClass(this, "TObject", null, function () {',
  28857. ' this.$init = function () {',
  28858. ' };',
  28859. ' this.$final = function () {',
  28860. ' };',
  28861. '});',
  28862. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28863. ' this.Fly = function () {',
  28864. ' };',
  28865. ' var $r = this.$rtti;',
  28866. ' $r.addMethod("Fly", 0, []);',
  28867. '});',
  28868. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  28869. ' this.Fly = function () {',
  28870. ' };',
  28871. ' var $r = this.$rtti;',
  28872. ' $r.addMethod("Fly", 0, []);',
  28873. '});',
  28874. '']),
  28875. LinesToStr([ // $mod.$main
  28876. ]));
  28877. CheckResolverUnexpectedHints(true);
  28878. end;
  28879. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28880. begin
  28881. WithTypeInfo:=true;
  28882. StartProgram(false);
  28883. Add('type');
  28884. Add(' TObject = class');
  28885. Add(' published');
  28886. Add(' procedure Proc; external name ''foo'';');
  28887. Add(' end;');
  28888. Add('begin');
  28889. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28890. nPublishedNameMustMatchExternal);
  28891. ConvertProgram;
  28892. end;
  28893. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28894. begin
  28895. WithTypeInfo:=true;
  28896. StartProgram(false);
  28897. Add('type');
  28898. Add(' TObject = class');
  28899. Add(' class var FA: longint;');
  28900. Add(' published');
  28901. Add(' class property A: longint read FA;');
  28902. Add(' end;');
  28903. Add('begin');
  28904. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28905. nInvalidXModifierY);
  28906. ConvertProgram;
  28907. end;
  28908. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28909. begin
  28910. WithTypeInfo:=true;
  28911. StartProgram(false);
  28912. Add('type');
  28913. Add(' TObject = class');
  28914. Add(' published');
  28915. Add(' class var FA: longint;');
  28916. Add(' end;');
  28917. Add('begin');
  28918. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28919. nSymbolCannotBePublished);
  28920. ConvertProgram;
  28921. end;
  28922. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28923. begin
  28924. WithTypeInfo:=true;
  28925. StartProgram(false);
  28926. Add('{$modeswitch externalclass}');
  28927. Add('type');
  28928. Add(' TObject = class');
  28929. Add(' published');
  28930. Add(' V: longint; external name ''foo'';');
  28931. Add(' end;');
  28932. Add('begin');
  28933. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28934. nPublishedNameMustMatchExternal);
  28935. ConvertProgram;
  28936. end;
  28937. procedure TTestModule.TestRTTI_Class_Field;
  28938. begin
  28939. WithTypeInfo:=true;
  28940. StartProgram(false);
  28941. Add('{$modeswitch externalclass}');
  28942. Add('type');
  28943. Add(' TObject = class');
  28944. Add(' private');
  28945. Add(' FPropA: string;');
  28946. Add(' published');
  28947. Add(' VarLI: longint;');
  28948. Add(' VarC: char;');
  28949. Add(' VarS: string;');
  28950. Add(' VarD: double;');
  28951. Add(' VarB: boolean;');
  28952. Add(' VarLW: longword;');
  28953. Add(' VarSmI: smallint;');
  28954. Add(' VarW: word;');
  28955. Add(' VarShI: shortint;');
  28956. Add(' VarBy: byte;');
  28957. Add(' VarExt: longint external name ''VarExt'';');
  28958. Add(' ArrA, ArrB: array of byte;');
  28959. Add(' end;');
  28960. Add('var p: pointer;');
  28961. Add(' Obj: tobject;');
  28962. Add('begin');
  28963. Add(' p:=typeinfo(tobject);');
  28964. Add(' p:=typeinfo(p);');
  28965. Add(' p:=typeinfo(obj);');
  28966. ConvertProgram;
  28967. CheckSource('TestRTTI_Class_Field',
  28968. LinesToStr([ // statements
  28969. 'rtl.createClass(this, "TObject", null, function () {',
  28970. ' this.$init = function () {',
  28971. ' this.FPropA = "";',
  28972. ' this.VarLI = 0;',
  28973. ' this.VarC = "";',
  28974. ' this.VarS = "";',
  28975. ' this.VarD = 0.0;',
  28976. ' this.VarB = false;',
  28977. ' this.VarLW = 0;',
  28978. ' this.VarSmI = 0;',
  28979. ' this.VarW = 0;',
  28980. ' this.VarShI = 0;',
  28981. ' this.VarBy = 0;',
  28982. ' this.ArrA = [];',
  28983. ' this.ArrB = [];',
  28984. ' };',
  28985. ' this.$final = function () {',
  28986. ' this.ArrA = undefined;',
  28987. ' this.ArrB = undefined;',
  28988. ' };',
  28989. ' var $r = this.$rtti;',
  28990. ' $r.addField("VarLI", rtl.longint);',
  28991. ' $r.addField("VarC", rtl.char);',
  28992. ' $r.addField("VarS", rtl.string);',
  28993. ' $r.addField("VarD", rtl.double);',
  28994. ' $r.addField("VarB", rtl.boolean);',
  28995. ' $r.addField("VarLW", rtl.longword);',
  28996. ' $r.addField("VarSmI", rtl.smallint);',
  28997. ' $r.addField("VarW", rtl.word);',
  28998. ' $r.addField("VarShI", rtl.shortint);',
  28999. ' $r.addField("VarBy", rtl.byte);',
  29000. ' $r.addField("VarExt", rtl.longint);',
  29001. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  29002. ' eltype: rtl.byte',
  29003. ' });',
  29004. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  29005. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  29006. '});',
  29007. 'this.p = null;',
  29008. 'this.Obj = null;',
  29009. '']),
  29010. LinesToStr([ // $mod.$main
  29011. '$mod.p = $mod.$rtti["TObject"];',
  29012. '$mod.p = rtl.pointer;',
  29013. '$mod.p = $mod.Obj.$rtti;',
  29014. '']));
  29015. end;
  29016. procedure TTestModule.TestRTTI_Class_Method;
  29017. begin
  29018. WithTypeInfo:=true;
  29019. StartProgram(false);
  29020. Add([
  29021. 'type',
  29022. ' TObject = class',
  29023. ' private',
  29024. ' procedure Internal; external name ''$intern'';',
  29025. ' published',
  29026. ' procedure Click; virtual; abstract;',
  29027. ' procedure Notify(Sender: TObject); virtual; abstract;',
  29028. ' function GetNotify: boolean; external name ''GetNotify'';',
  29029. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  29030. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  29031. ' end;',
  29032. 'begin']);
  29033. ConvertProgram;
  29034. CheckSource('TestRTTI_Class_Method',
  29035. LinesToStr([ // statements
  29036. 'rtl.createClass(this, "TObject", null, function () {',
  29037. ' this.$init = function () {',
  29038. ' };',
  29039. ' this.$final = function () {',
  29040. ' };',
  29041. ' var $r = this.$rtti;',
  29042. ' $r.addMethod("Click", 0, []);',
  29043. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29044. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  29045. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  29046. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  29047. '});',
  29048. '']),
  29049. LinesToStr([ // $mod.$main
  29050. '']));
  29051. end;
  29052. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29053. begin
  29054. WithTypeInfo:=true;
  29055. StartProgram(false);
  29056. Add('type');
  29057. Add(' TObject = class');
  29058. Add(' published');
  29059. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29060. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29061. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29062. Add(' end;');
  29063. Add('begin');
  29064. ConvertProgram;
  29065. CheckSource('TestRTTI_Class_MethodOpenArray',
  29066. LinesToStr([ // statements
  29067. 'rtl.createClass(this, "TObject", null, function () {',
  29068. ' this.$init = function () {',
  29069. ' };',
  29070. ' this.$final = function () {',
  29071. ' };',
  29072. ' var $r = this.$rtti;',
  29073. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29074. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29075. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29076. '});',
  29077. '']),
  29078. LinesToStr([ // $mod.$main
  29079. '']));
  29080. end;
  29081. procedure TTestModule.TestRTTI_Class_Property;
  29082. begin
  29083. WithTypeInfo:=true;
  29084. StartProgram(false);
  29085. Add('{$modeswitch externalclass}');
  29086. Add('type');
  29087. Add(' TObject = class');
  29088. Add(' private');
  29089. Add(' FColor: longint;');
  29090. Add(' FColorStored: boolean;');
  29091. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29092. Add(' function GetColor: longint; virtual; abstract;');
  29093. Add(' function GetColorStored: boolean; virtual; abstract;');
  29094. Add(' FExtSize: longint external name ''$extSize'';');
  29095. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29096. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29097. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29098. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29099. Add(' published');
  29100. Add(' property ColorA: longint read FColor;');
  29101. Add(' property ColorB: longint write FColor;');
  29102. Add(' property ColorC: longint read GetColor write SetColor;');
  29103. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29104. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29105. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29106. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29107. Add(' end;');
  29108. Add('begin');
  29109. ConvertProgram;
  29110. CheckSource('TestRTTI_Class_Property',
  29111. LinesToStr([ // statements
  29112. 'rtl.createClass(this, "TObject", null, function () {',
  29113. ' this.$init = function () {',
  29114. ' this.FColor = 0;',
  29115. ' this.FColorStored = false;',
  29116. ' };',
  29117. ' this.$final = function () {',
  29118. ' };',
  29119. ' var $r = this.$rtti;',
  29120. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29121. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29122. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29123. ' $r.addProperty(',
  29124. ' "ColorD",',
  29125. ' 8,',
  29126. ' rtl.longint,',
  29127. ' "FColor",',
  29128. ' "FColor",',
  29129. ' {',
  29130. ' stored: "FColorStored"',
  29131. ' }',
  29132. ' );',
  29133. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29134. ' $r.addProperty(',
  29135. ' "ExtSizeB",',
  29136. ' 11,',
  29137. ' rtl.longint,',
  29138. ' "$getSize",',
  29139. ' "$setSize",',
  29140. ' {',
  29141. ' stored: "$extSizeStored"',
  29142. ' }',
  29143. ' );',
  29144. ' $r.addProperty(',
  29145. ' "ExtSizeC",',
  29146. ' 12,',
  29147. ' rtl.longint,',
  29148. ' "$extSize",',
  29149. ' "$extSize",',
  29150. ' {',
  29151. ' stored: "$getExtSizeStored"',
  29152. ' }',
  29153. ' );',
  29154. '});',
  29155. '']),
  29156. LinesToStr([ // $mod.$main
  29157. '']));
  29158. end;
  29159. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29160. begin
  29161. WithTypeInfo:=true;
  29162. StartProgram(false);
  29163. Add('{$modeswitch externalclass}');
  29164. Add('type');
  29165. Add(' integer = longint;');
  29166. Add(' TObject = class');
  29167. Add(' private');
  29168. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29169. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29170. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29171. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29172. Add(' published');
  29173. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29174. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29175. Add(' end;');
  29176. Add('begin');
  29177. ConvertProgram;
  29178. CheckSource('TestRTTI_Class_PropertyParams',
  29179. LinesToStr([ // statements
  29180. 'rtl.createClass(this, "TObject", null, function () {',
  29181. ' this.$init = function () {',
  29182. ' };',
  29183. ' this.$final = function () {',
  29184. ' };',
  29185. ' var $r = this.$rtti;',
  29186. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29187. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29188. '});',
  29189. '']),
  29190. LinesToStr([ // $mod.$main
  29191. '']));
  29192. end;
  29193. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29194. begin
  29195. WithTypeInfo:=true;
  29196. AddModuleWithIntfImplSrc('unit1.pas',
  29197. 'type TColor = -5..5;',
  29198. '');
  29199. StartProgram(true);
  29200. Add([
  29201. 'uses unit1;',
  29202. 'type',
  29203. ' TColorAlias = TColor;',
  29204. ' TColorTypeAlias = type TColor;',
  29205. ' TObject = class',
  29206. ' private',
  29207. ' fColor: TColor;',
  29208. ' fAlias: TColorAlias;',
  29209. ' fTypeAlias: TColorTypeAlias;',
  29210. ' published',
  29211. ' property Color: TColor read fcolor;',
  29212. ' property Alias: TColorAlias read falias;',
  29213. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29214. ' end;',
  29215. 'begin',
  29216. '']);
  29217. ConvertProgram;
  29218. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29219. LinesToStr([ // statements
  29220. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29221. 'rtl.createClass(this, "TObject", null, function () {',
  29222. ' this.$init = function () {',
  29223. ' this.fColor = 0;',
  29224. ' this.fAlias = 0;',
  29225. ' this.fTypeAlias = 0;',
  29226. ' };',
  29227. ' this.$final = function () {',
  29228. ' };',
  29229. ' var $r = this.$rtti;',
  29230. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29231. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29232. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29233. '});',
  29234. '']),
  29235. LinesToStr([ // $mod.$main
  29236. '']));
  29237. end;
  29238. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29239. begin
  29240. WithTypeInfo:=true;
  29241. StartProgram(false);
  29242. Add([
  29243. '{$modeswitch omitrtti}',
  29244. 'type',
  29245. ' TObject = class',
  29246. ' private',
  29247. ' FA: byte;',
  29248. ' published',
  29249. ' property A: byte read FA write FA;',
  29250. ' end;',
  29251. 'begin']);
  29252. ConvertProgram;
  29253. CheckSource('TestRTTI_Class_OmitRTTI',
  29254. LinesToStr([ // statements
  29255. 'rtl.createClass(this, "TObject", null, function () {',
  29256. ' this.$init = function () {',
  29257. ' this.FA = 0;',
  29258. ' };',
  29259. ' this.$final = function () {',
  29260. ' };',
  29261. '});',
  29262. '']),
  29263. LinesToStr([ // $mod.$main
  29264. '']));
  29265. end;
  29266. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29267. begin
  29268. WithTypeInfo:=true;
  29269. StartUnit(true,[supTObject]);
  29270. Add([
  29271. 'interface',
  29272. 'type',
  29273. ' {$M+}',
  29274. ' TBird = class',
  29275. ' published',
  29276. ' Swarm: array of TBird;',
  29277. ' end;',
  29278. 'implementation',
  29279. '']);
  29280. ConvertUnit;
  29281. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29282. LinesToStr([ // statements
  29283. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29284. ' this.$init = function () {',
  29285. ' pas.system.TObject.$init.call(this);',
  29286. ' this.Swarm = [];',
  29287. ' };',
  29288. ' this.$final = function () {',
  29289. ' this.Swarm = undefined;',
  29290. ' pas.system.TObject.$final.call(this);',
  29291. ' };',
  29292. ' var $r = this.$rtti;',
  29293. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29294. ' eltype: $r',
  29295. ' });',
  29296. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29297. '});',
  29298. '']),
  29299. LinesToStr([ // $mod.$main
  29300. '']));
  29301. end;
  29302. procedure TTestModule.TestRTTI_IndexModifier;
  29303. begin
  29304. WithTypeInfo:=true;
  29305. StartProgram(false);
  29306. Add([
  29307. 'type',
  29308. ' TEnum = (red, blue);',
  29309. ' TObject = class',
  29310. ' FB: boolean;',
  29311. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29312. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29313. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29314. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29315. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29316. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29317. ' published',
  29318. ' property B1: boolean index 1 read FB write SetIntBool;',
  29319. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29320. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29321. ' end;',
  29322. 'begin']);
  29323. ConvertProgram;
  29324. CheckSource('TestRTTI_IndexModifier',
  29325. LinesToStr([ // statements
  29326. 'this.TEnum = {',
  29327. ' "0": "red",',
  29328. ' red: 0,',
  29329. ' "1": "blue",',
  29330. ' blue: 1',
  29331. '};',
  29332. 'this.$rtti.$Enum("TEnum", {',
  29333. ' minvalue: 0,',
  29334. ' maxvalue: 1,',
  29335. ' ordtype: 1,',
  29336. ' enumtype: this.TEnum',
  29337. '});',
  29338. 'rtl.createClass(this, "TObject", null, function () {',
  29339. ' this.$init = function () {',
  29340. ' this.FB = false;',
  29341. ' };',
  29342. ' this.$final = function () {',
  29343. ' };',
  29344. ' var $r = this.$rtti;',
  29345. ' $r.addProperty(',
  29346. ' "B1",',
  29347. ' 18,',
  29348. ' rtl.boolean,',
  29349. ' "FB",',
  29350. ' "SetIntBool",',
  29351. ' {',
  29352. ' index: 1',
  29353. ' }',
  29354. ' );',
  29355. ' $r.addProperty(',
  29356. ' "B2",',
  29357. ' 17,',
  29358. ' rtl.boolean,',
  29359. ' "GetEnumBool",',
  29360. ' "FB",',
  29361. ' {',
  29362. ' index: $mod.TEnum.blue',
  29363. ' }',
  29364. ' );',
  29365. ' $r.addProperty(',
  29366. ' "I1",',
  29367. ' 19,',
  29368. ' rtl.boolean,',
  29369. ' "GetStrIntBool",',
  29370. ' "SetStrIntBool",',
  29371. ' {',
  29372. ' index: 2',
  29373. ' }',
  29374. ' );',
  29375. '});',
  29376. '']),
  29377. LinesToStr([ // $mod.$main
  29378. '']));
  29379. end;
  29380. procedure TTestModule.TestRTTI_StoredModifier;
  29381. begin
  29382. WithTypeInfo:=true;
  29383. StartProgram(false);
  29384. Add([
  29385. 'const',
  29386. ' ConstB = true;',
  29387. 'type',
  29388. ' TObject = class',
  29389. ' private',
  29390. ' FB: boolean;',
  29391. ' function IsBStored: boolean; virtual; abstract;',
  29392. ' published',
  29393. ' property BoolA: boolean read FB stored true;',
  29394. ' property BoolB: boolean read FB stored false;',
  29395. ' property BoolC: boolean read FB stored FB;',
  29396. ' property BoolD: boolean read FB stored ConstB;',
  29397. ' property BoolE: boolean read FB stored IsBStored;',
  29398. ' end;',
  29399. 'begin']);
  29400. ConvertProgram;
  29401. CheckSource('TestRTTI_StoredModifier',
  29402. LinesToStr([ // statements
  29403. 'this.ConstB = true;',
  29404. 'rtl.createClass(this, "TObject", null, function () {',
  29405. ' this.$init = function () {',
  29406. ' this.FB = false;',
  29407. ' };',
  29408. ' this.$final = function () {',
  29409. ' };',
  29410. ' var $r = this.$rtti;',
  29411. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29412. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29413. ' $r.addProperty(',
  29414. ' "BoolC",',
  29415. ' 8,',
  29416. ' rtl.boolean,',
  29417. ' "FB",',
  29418. ' "",',
  29419. ' {',
  29420. ' stored: "FB"',
  29421. ' }',
  29422. ' );',
  29423. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29424. ' $r.addProperty(',
  29425. ' "BoolE",',
  29426. ' 12,',
  29427. ' rtl.boolean,',
  29428. ' "FB",',
  29429. ' "",',
  29430. ' {',
  29431. ' stored: "IsBStored"',
  29432. ' }',
  29433. ' );',
  29434. '});',
  29435. '']),
  29436. LinesToStr([ // $mod.$main
  29437. '']));
  29438. end;
  29439. procedure TTestModule.TestRTTI_DefaultValue;
  29440. begin
  29441. WithTypeInfo:=true;
  29442. StartProgram(false);
  29443. Add([
  29444. 'type',
  29445. ' TEnum = (red, blue);',
  29446. 'const',
  29447. ' CB = true or false;',
  29448. ' CI = 1+2;',
  29449. 'type',
  29450. ' TObject = class',
  29451. ' FB: boolean;',
  29452. ' FI: longint;',
  29453. ' FE: TEnum;',
  29454. ' published',
  29455. ' property B1: boolean read FB default true;',
  29456. ' property B2: boolean read FB default CB;',
  29457. ' property B3: boolean read FB default test1.cb;',
  29458. ' property I1: longint read FI default 2;',
  29459. ' property I2: longint read FI default CI;',
  29460. ' property E1: TEnum read FE default red;',
  29461. ' property E2: TEnum read FE default TEnum.blue;',
  29462. ' end;',
  29463. 'begin']);
  29464. ConvertProgram;
  29465. CheckSource('TestRTTI_DefaultValue',
  29466. LinesToStr([ // statements
  29467. 'this.TEnum = {',
  29468. ' "0": "red",',
  29469. ' red: 0,',
  29470. ' "1": "blue",',
  29471. ' blue: 1',
  29472. '};',
  29473. 'this.$rtti.$Enum("TEnum", {',
  29474. ' minvalue: 0,',
  29475. ' maxvalue: 1,',
  29476. ' ordtype: 1,',
  29477. ' enumtype: this.TEnum',
  29478. '});',
  29479. 'this.CB = true || false;',
  29480. 'this.CI = 1 + 2;',
  29481. 'rtl.createClass(this, "TObject", null, function () {',
  29482. ' this.$init = function () {',
  29483. ' this.FB = false;',
  29484. ' this.FI = 0;',
  29485. ' this.FE = 0;',
  29486. ' };',
  29487. ' this.$final = function () {',
  29488. ' };',
  29489. ' var $r = this.$rtti;',
  29490. ' $r.addProperty(',
  29491. ' "B1",',
  29492. ' 0,',
  29493. ' rtl.boolean,',
  29494. ' "FB",',
  29495. ' "",',
  29496. ' {',
  29497. ' Default: true',
  29498. ' }',
  29499. ' );',
  29500. ' $r.addProperty(',
  29501. ' "B2",',
  29502. ' 0,',
  29503. ' rtl.boolean,',
  29504. ' "FB",',
  29505. ' "",',
  29506. ' {',
  29507. ' Default: true',
  29508. ' }',
  29509. ' );',
  29510. ' $r.addProperty(',
  29511. ' "B3",',
  29512. ' 0,',
  29513. ' rtl.boolean,',
  29514. ' "FB",',
  29515. ' "",',
  29516. ' {',
  29517. ' Default: true',
  29518. ' }',
  29519. ' );',
  29520. ' $r.addProperty(',
  29521. ' "I1",',
  29522. ' 0,',
  29523. ' rtl.longint,',
  29524. ' "FI",',
  29525. ' "",',
  29526. ' {',
  29527. ' Default: 2',
  29528. ' }',
  29529. ' );',
  29530. ' $r.addProperty(',
  29531. ' "I2",',
  29532. ' 0,',
  29533. ' rtl.longint,',
  29534. ' "FI",',
  29535. ' "",',
  29536. ' {',
  29537. ' Default: 3',
  29538. ' }',
  29539. ' );',
  29540. ' $r.addProperty(',
  29541. ' "E1",',
  29542. ' 0,',
  29543. ' $mod.$rtti["TEnum"],',
  29544. ' "FE",',
  29545. ' "",',
  29546. ' {',
  29547. ' Default: $mod.TEnum.red',
  29548. ' }',
  29549. ' );',
  29550. ' $r.addProperty(',
  29551. ' "E2",',
  29552. ' 0,',
  29553. ' $mod.$rtti["TEnum"],',
  29554. ' "FE",',
  29555. ' "",',
  29556. ' {',
  29557. ' Default: $mod.TEnum.blue',
  29558. ' }',
  29559. ' );',
  29560. '});',
  29561. '']),
  29562. LinesToStr([ // $mod.$main
  29563. '']));
  29564. end;
  29565. procedure TTestModule.TestRTTI_DefaultValueSet;
  29566. begin
  29567. WithTypeInfo:=true;
  29568. StartProgram(false);
  29569. Add([
  29570. 'type',
  29571. ' TEnum = (red, blue);',
  29572. ' TSet = set of TEnum;',
  29573. 'const',
  29574. ' CSet = [red,blue];',
  29575. 'type',
  29576. ' TObject = class',
  29577. ' FSet: TSet;',
  29578. ' published',
  29579. ' property Set1: TSet read FSet default [];',
  29580. ' property Set2: TSet read FSet default [red];',
  29581. ' property Set3: TSet read FSet default [red,blue];',
  29582. ' property Set4: TSet read FSet default CSet;',
  29583. ' end;',
  29584. 'begin']);
  29585. ConvertProgram;
  29586. CheckSource('TestRTTI_DefaultValueSet',
  29587. LinesToStr([ // statements
  29588. 'this.TEnum = {',
  29589. ' "0": "red",',
  29590. ' red: 0,',
  29591. ' "1": "blue",',
  29592. ' blue: 1',
  29593. '};',
  29594. 'this.$rtti.$Enum("TEnum", {',
  29595. ' minvalue: 0,',
  29596. ' maxvalue: 1,',
  29597. ' ordtype: 1,',
  29598. ' enumtype: this.TEnum',
  29599. '});',
  29600. 'this.$rtti.$Set("TSet", {',
  29601. ' comptype: this.$rtti["TEnum"]',
  29602. '});',
  29603. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29604. 'rtl.createClass(this, "TObject", null, function () {',
  29605. ' this.$init = function () {',
  29606. ' this.FSet = {};',
  29607. ' };',
  29608. ' this.$final = function () {',
  29609. ' this.FSet = undefined;',
  29610. ' };',
  29611. ' var $r = this.$rtti;',
  29612. ' $r.addProperty(',
  29613. ' "Set1",',
  29614. ' 0,',
  29615. ' $mod.$rtti["TSet"],',
  29616. ' "FSet",',
  29617. ' "",',
  29618. ' {',
  29619. ' Default: {}',
  29620. ' }',
  29621. ' );',
  29622. ' $r.addProperty(',
  29623. ' "Set2",',
  29624. ' 0,',
  29625. ' $mod.$rtti["TSet"],',
  29626. ' "FSet",',
  29627. ' "",',
  29628. ' {',
  29629. ' Default: rtl.createSet($mod.TEnum.red)',
  29630. ' }',
  29631. ' );',
  29632. ' $r.addProperty(',
  29633. ' "Set3",',
  29634. ' 0,',
  29635. ' $mod.$rtti["TSet"],',
  29636. ' "FSet",',
  29637. ' "",',
  29638. ' {',
  29639. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29640. ' }',
  29641. ' );',
  29642. ' $r.addProperty(',
  29643. ' "Set4",',
  29644. ' 0,',
  29645. ' $mod.$rtti["TSet"],',
  29646. ' "FSet",',
  29647. ' "",',
  29648. ' {',
  29649. ' Default: $mod.CSet',
  29650. ' }',
  29651. ' );',
  29652. '});',
  29653. '']),
  29654. LinesToStr([ // $mod.$main
  29655. '']));
  29656. end;
  29657. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29658. begin
  29659. WithTypeInfo:=true;
  29660. StartProgram(false);
  29661. Add([
  29662. 'type',
  29663. ' TRg = -1..1;',
  29664. 'const',
  29665. ' l = low(TRg);',
  29666. ' h = high(TRg);',
  29667. 'type',
  29668. ' TObject = class',
  29669. ' FV: TRg;',
  29670. ' published',
  29671. ' property V1: TRg read FV default -1;',
  29672. ' end;',
  29673. 'begin']);
  29674. ConvertProgram;
  29675. CheckSource('TestRTTI_DefaultValueRangeType',
  29676. LinesToStr([ // statements
  29677. 'this.$rtti.$Int("TRg", {',
  29678. ' minvalue: -1,',
  29679. ' maxvalue: 1,',
  29680. ' ordtype: 0',
  29681. '});',
  29682. 'this.l = -1;',
  29683. 'this.h = 1;',
  29684. 'rtl.createClass(this, "TObject", null, function () {',
  29685. ' this.$init = function () {',
  29686. ' this.FV = 0;',
  29687. ' };',
  29688. ' this.$final = function () {',
  29689. ' };',
  29690. ' var $r = this.$rtti;',
  29691. ' $r.addProperty(',
  29692. ' "V1",',
  29693. ' 0,',
  29694. ' $mod.$rtti["TRg"],',
  29695. ' "FV",',
  29696. ' "",',
  29697. ' {',
  29698. ' Default: -1',
  29699. ' }',
  29700. ' );',
  29701. '});',
  29702. '']),
  29703. LinesToStr([ // $mod.$main
  29704. '']));
  29705. end;
  29706. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29707. begin
  29708. WithTypeInfo:=true;
  29709. StartProgram(false);
  29710. Add([
  29711. 'type',
  29712. ' TObject = class',
  29713. ' FA, FB: byte;',
  29714. ' property A: byte read FA default 1;',
  29715. ' property B: byte read FB default 2;',
  29716. ' end;',
  29717. ' TBird = class',
  29718. ' published',
  29719. ' property A;',
  29720. ' property B nodefault;',
  29721. ' end;',
  29722. 'begin']);
  29723. ConvertProgram;
  29724. CheckSource('TestRTTI_DefaultValueInherit',
  29725. LinesToStr([ // statements
  29726. 'rtl.createClass(this, "TObject", null, function () {',
  29727. ' this.$init = function () {',
  29728. ' this.FA = 0;',
  29729. ' this.FB = 0;',
  29730. ' };',
  29731. ' this.$final = function () {',
  29732. ' };',
  29733. '});',
  29734. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29735. ' var $r = this.$rtti;',
  29736. ' $r.addProperty(',
  29737. ' "A",',
  29738. ' 0,',
  29739. ' rtl.byte,',
  29740. ' "FA",',
  29741. ' "",',
  29742. ' {',
  29743. ' Default: 1',
  29744. ' }',
  29745. ' );',
  29746. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29747. '});',
  29748. '']),
  29749. LinesToStr([ // $mod.$main
  29750. '']));
  29751. end;
  29752. procedure TTestModule.TestRTTI_OverrideMethod;
  29753. begin
  29754. WithTypeInfo:=true;
  29755. StartProgram(false);
  29756. Add('type');
  29757. Add(' TObject = class');
  29758. Add(' published');
  29759. Add(' procedure DoIt; virtual; abstract;');
  29760. Add(' end;');
  29761. Add(' TSky = class');
  29762. Add(' published');
  29763. Add(' procedure DoIt; override;');
  29764. Add(' end;');
  29765. Add('procedure TSky.DoIt; begin end;');
  29766. Add('begin');
  29767. ConvertProgram;
  29768. CheckSource('TestRTTI_OverrideMethod',
  29769. LinesToStr([ // statements
  29770. 'rtl.createClass(this, "TObject", null, function () {',
  29771. ' this.$init = function () {',
  29772. ' };',
  29773. ' this.$final = function () {',
  29774. ' };',
  29775. ' var $r = this.$rtti;',
  29776. ' $r.addMethod("DoIt", 0, []);',
  29777. '});',
  29778. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29779. ' this.DoIt = function () {',
  29780. ' };',
  29781. '});',
  29782. '']),
  29783. LinesToStr([ // $mod.$main
  29784. '']));
  29785. end;
  29786. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29787. begin
  29788. WithTypeInfo:=true;
  29789. StartProgram(false);
  29790. Add([
  29791. 'type',
  29792. ' TObject = class',
  29793. ' published',
  29794. ' procedure DoIt;',
  29795. ' end;',
  29796. ' TSky = class',
  29797. ' published',
  29798. ' procedure DoIt; reintroduce;',
  29799. ' end;',
  29800. 'procedure TObject.DoIt; begin end;',
  29801. 'procedure TSky.DoIt;',
  29802. 'begin',
  29803. ' inherited DoIt;',
  29804. 'end;',
  29805. 'begin']);
  29806. ConvertProgram;
  29807. CheckSource('TestRTTI_ReintroduceMethod',
  29808. LinesToStr([ // statements
  29809. 'rtl.createClass(this, "TObject", null, function () {',
  29810. ' this.$init = function () {',
  29811. ' };',
  29812. ' this.$final = function () {',
  29813. ' };',
  29814. ' this.DoIt = function () {',
  29815. ' };',
  29816. ' var $r = this.$rtti;',
  29817. ' $r.addMethod("DoIt", 0, []);',
  29818. '});',
  29819. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29820. ' this.DoIt = function () {',
  29821. ' $mod.TObject.DoIt.call(this);',
  29822. ' };',
  29823. ' var $r = this.$rtti;',
  29824. ' $r.addMethod("DoIt", 0, []);',
  29825. '});',
  29826. '']),
  29827. LinesToStr([ // $mod.$main
  29828. '']));
  29829. end;
  29830. procedure TTestModule.TestRTTI_OverloadProperty;
  29831. begin
  29832. WithTypeInfo:=true;
  29833. StartProgram(false);
  29834. Add('type');
  29835. Add(' TObject = class');
  29836. Add(' protected');
  29837. Add(' FFlag: longint;');
  29838. Add(' published');
  29839. Add(' property Flag: longint read fflag;');
  29840. Add(' end;');
  29841. Add(' TSky = class');
  29842. Add(' published');
  29843. Add(' property FLAG: longint write fflag;');
  29844. Add(' end;');
  29845. Add('begin');
  29846. ConvertProgram;
  29847. CheckSource('TestRTTI_OverrideMethod',
  29848. LinesToStr([ // statements
  29849. 'rtl.createClass(this, "TObject", null, function () {',
  29850. ' this.$init = function () {',
  29851. ' this.FFlag = 0;',
  29852. ' };',
  29853. ' this.$final = function () {',
  29854. ' };',
  29855. ' var $r = this.$rtti;',
  29856. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29857. '});',
  29858. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29859. ' var $r = this.$rtti;',
  29860. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29861. '});',
  29862. '']),
  29863. LinesToStr([ // $mod.$main
  29864. '']));
  29865. end;
  29866. procedure TTestModule.TestRTTI_ClassForward;
  29867. begin
  29868. WithTypeInfo:=true;
  29869. StartProgram(false);
  29870. Add('type');
  29871. Add(' TObject = class end;');
  29872. Add(' tbridge = class;');
  29873. Add(' TProc = function: tbridge;');
  29874. Add(' TOger = class');
  29875. Add(' published');
  29876. Add(' FBridge: tbridge;');
  29877. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29878. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29879. Add(' end;');
  29880. Add(' TBridge = class');
  29881. Add(' FOger: toger;');
  29882. Add(' end;');
  29883. Add('var p: Pointer;');
  29884. Add(' b: tbridge;');
  29885. Add('begin');
  29886. Add(' p:=typeinfo(tbridge);');
  29887. Add(' p:=typeinfo(b);');
  29888. ConvertProgram;
  29889. CheckSource('TestRTTI_ClassForward',
  29890. LinesToStr([ // statements
  29891. 'rtl.createClass(this, "TObject", null, function () {',
  29892. ' this.$init = function () {',
  29893. ' };',
  29894. ' this.$final = function () {',
  29895. ' };',
  29896. '});',
  29897. 'this.$rtti.$Class("TBridge");',
  29898. 'this.$rtti.$ProcVar("TProc", {',
  29899. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  29900. '});',
  29901. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29902. ' this.$init = function () {',
  29903. ' $mod.TObject.$init.call(this);',
  29904. ' this.FBridge = null;',
  29905. ' };',
  29906. ' this.$final = function () {',
  29907. ' this.FBridge = undefined;',
  29908. ' $mod.TObject.$final.call(this);',
  29909. ' };',
  29910. ' var $r = this.$rtti;',
  29911. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29912. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29913. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29914. '});',
  29915. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29916. ' this.$init = function () {',
  29917. ' $mod.TObject.$init.call(this);',
  29918. ' this.FOger = null;',
  29919. ' };',
  29920. ' this.$final = function () {',
  29921. ' this.FOger = undefined;',
  29922. ' $mod.TObject.$final.call(this);',
  29923. ' };',
  29924. '});',
  29925. 'this.p = null;',
  29926. 'this.b = null;',
  29927. '']),
  29928. LinesToStr([ // $mod.$main
  29929. '$mod.p = $mod.$rtti["TBridge"];',
  29930. '$mod.p = $mod.b.$rtti;',
  29931. '']));
  29932. end;
  29933. procedure TTestModule.TestRTTI_ClassOf;
  29934. begin
  29935. WithTypeInfo:=true;
  29936. StartProgram(false);
  29937. Add('type');
  29938. Add(' TClass = class of tobject;');
  29939. Add(' TProcA = function: TClass;');
  29940. Add(' TObject = class');
  29941. Add(' published');
  29942. Add(' C: tclass;');
  29943. Add(' end;');
  29944. Add(' tfox = class;');
  29945. Add(' TBird = class end;');
  29946. Add(' TBirds = class of tbird;');
  29947. Add(' TFox = class end;');
  29948. Add(' TFoxes = class of tfox;');
  29949. Add(' TCows = class of TCow;');
  29950. Add(' TCow = class;');
  29951. Add(' TCow = class end;');
  29952. Add('begin');
  29953. ConvertProgram;
  29954. CheckSource('TestRTTI_ClassOf',
  29955. LinesToStr([ // statements
  29956. 'this.$rtti.$Class("TObject");',
  29957. 'this.$rtti.$ClassRef("TClass", {',
  29958. ' instancetype: this.$rtti["TObject"]',
  29959. '});',
  29960. 'this.$rtti.$ProcVar("TProcA", {',
  29961. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  29962. '});',
  29963. 'rtl.createClass(this, "TObject", null, function () {',
  29964. ' this.$init = function () {',
  29965. ' this.C = null;',
  29966. ' };',
  29967. ' this.$final = function () {',
  29968. ' this.C = undefined;',
  29969. ' };',
  29970. ' var $r = this.$rtti;',
  29971. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29972. '});',
  29973. 'this.$rtti.$Class("TFox");',
  29974. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29975. '});',
  29976. 'this.$rtti.$ClassRef("TBirds", {',
  29977. ' instancetype: this.$rtti["TBird"]',
  29978. '});',
  29979. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29980. '});',
  29981. 'this.$rtti.$ClassRef("TFoxes", {',
  29982. ' instancetype: this.$rtti["TFox"]',
  29983. '});',
  29984. 'this.$rtti.$Class("TCow");',
  29985. 'this.$rtti.$ClassRef("TCows", {',
  29986. ' instancetype: this.$rtti["TCow"]',
  29987. '});',
  29988. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29989. '});',
  29990. '']),
  29991. LinesToStr([ // $mod.$main
  29992. '']));
  29993. end;
  29994. procedure TTestModule.TestRTTI_Record;
  29995. begin
  29996. WithTypeInfo:=true;
  29997. StartProgram(false);
  29998. Add('type');
  29999. Add(' integer = longint;');
  30000. Add(' TPoint = record');
  30001. Add(' x,y: integer;');
  30002. Add(' end;');
  30003. Add('var p: pointer;');
  30004. Add(' r: tpoint;');
  30005. Add('begin');
  30006. Add(' p:=typeinfo(tpoint);');
  30007. Add(' p:=typeinfo(r);');
  30008. Add(' p:=typeinfo(r.x);');
  30009. ConvertProgram;
  30010. CheckSource('TestRTTI_Record',
  30011. LinesToStr([ // statements
  30012. 'rtl.recNewT(this, "TPoint", function () {',
  30013. ' this.x = 0;',
  30014. ' this.y = 0;',
  30015. ' this.$eq = function (b) {',
  30016. ' return (this.x === b.x) && (this.y === b.y);',
  30017. ' };',
  30018. ' this.$assign = function (s) {',
  30019. ' this.x = s.x;',
  30020. ' this.y = s.y;',
  30021. ' return this;',
  30022. ' };',
  30023. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30024. ' $r.addField("x", rtl.longint);',
  30025. ' $r.addField("y", rtl.longint);',
  30026. '});',
  30027. 'this.p = null;',
  30028. 'this.r = this.TPoint.$new();',
  30029. '']),
  30030. LinesToStr([ // $mod.$main
  30031. '$mod.p = $mod.$rtti["TPoint"];',
  30032. '$mod.p = $mod.$rtti["TPoint"];',
  30033. '$mod.p = rtl.longint;',
  30034. '']));
  30035. end;
  30036. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30037. begin
  30038. WithTypeInfo:=true;
  30039. StartProgram(false);
  30040. Add('type');
  30041. Add(' TFloatRec = record');
  30042. Add(' c,d: array of char;');
  30043. // Add(' i: array of array of longint;');
  30044. Add(' end;');
  30045. Add('var p: pointer;');
  30046. Add(' r: tfloatrec;');
  30047. Add('begin');
  30048. Add(' p:=typeinfo(tfloatrec);');
  30049. Add(' p:=typeinfo(r);');
  30050. Add(' p:=typeinfo(r.d);');
  30051. ConvertProgram;
  30052. CheckSource('TestRTTI_Record',
  30053. LinesToStr([ // statements
  30054. 'rtl.recNewT(this, "TFloatRec", function () {',
  30055. ' this.$new = function () {',
  30056. ' var r = Object.create(this);',
  30057. ' r.c = [];',
  30058. ' r.d = [];',
  30059. ' return r;',
  30060. ' };',
  30061. ' this.$eq = function (b) {',
  30062. ' return (this.c === b.c) && (this.d === b.d);',
  30063. ' };',
  30064. ' this.$assign = function (s) {',
  30065. ' this.c = rtl.arrayRef(s.c);',
  30066. ' this.d = rtl.arrayRef(s.d);',
  30067. ' return this;',
  30068. ' };',
  30069. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30070. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30071. ' eltype: rtl.char',
  30072. ' });',
  30073. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30074. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30075. '});',
  30076. 'this.p = null;',
  30077. 'this.r = this.TFloatRec.$new();',
  30078. '']),
  30079. LinesToStr([ // $mod.$main
  30080. '$mod.p = $mod.$rtti["TFloatRec"];',
  30081. '$mod.p = $mod.$rtti["TFloatRec"];',
  30082. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30083. '']));
  30084. end;
  30085. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30086. begin
  30087. WithTypeInfo:=true;
  30088. StartProgram(false);
  30089. Add([
  30090. '{$modeswitch AdvancedRecords}',
  30091. 'type',
  30092. ' TPoint = record',
  30093. ' type TProc = procedure(w: word);',
  30094. ' class var p: TProc;',
  30095. ' end;',
  30096. 'begin',
  30097. '']);
  30098. ConvertProgram;
  30099. CheckSource('TestRTTI_Record_ClassVarType',
  30100. LinesToStr([ // statements
  30101. 'rtl.recNewT(this, "TPoint", function () {',
  30102. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30103. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30104. ' });',
  30105. ' this.p = null;',
  30106. ' this.$eq = function (b) {',
  30107. ' return true;',
  30108. ' };',
  30109. ' this.$assign = function (s) {',
  30110. ' return this;',
  30111. ' };',
  30112. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30113. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30114. '}, true);',
  30115. '']),
  30116. LinesToStr([ // $mod.$main
  30117. '']));
  30118. end;
  30119. procedure TTestModule.TestRTTI_LocalTypes;
  30120. begin
  30121. WithTypeInfo:=true;
  30122. StartProgram(false);
  30123. Add([
  30124. 'procedure DoIt;',
  30125. 'type',
  30126. ' integer = longint;',
  30127. ' TPoint = record',
  30128. ' x,y: integer;',
  30129. ' end;',
  30130. 'var p: TPoint;',
  30131. 'begin',
  30132. 'end;',
  30133. 'begin']);
  30134. ConvertProgram;
  30135. CheckSource('TestRTTI_LocalTypes',
  30136. LinesToStr([ // statements
  30137. 'var TPoint = rtl.recNewT(null, "", function () {',
  30138. ' this.x = 0;',
  30139. ' this.y = 0;',
  30140. ' this.$eq = function (b) {',
  30141. ' return (this.x === b.x) && (this.y === b.y);',
  30142. ' };',
  30143. ' this.$assign = function (s) {',
  30144. ' this.x = s.x;',
  30145. ' this.y = s.y;',
  30146. ' return this;',
  30147. ' };',
  30148. '});',
  30149. 'this.DoIt = function () {',
  30150. ' var p = TPoint.$new();',
  30151. '};',
  30152. '']),
  30153. LinesToStr([ // $mod.$main
  30154. '']));
  30155. end;
  30156. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30157. begin
  30158. WithTypeInfo:=true;
  30159. StartProgram(false);
  30160. Add([
  30161. 'type',
  30162. ' TCaption = string;',
  30163. ' TYesNo = boolean;',
  30164. ' TLetter = char;',
  30165. ' TFloat = double;',
  30166. ' TPtr = pointer;',
  30167. ' TShortInt = shortint;',
  30168. ' TByte = byte;',
  30169. ' TSmallInt = smallint;',
  30170. ' TWord = word;',
  30171. ' TInt32 = longint;',
  30172. ' TDWord = longword;',
  30173. ' TValue = jsvalue;',
  30174. 'var p: TPtr;',
  30175. 'begin',
  30176. ' p:=typeinfo(string);',
  30177. ' p:=typeinfo(tcaption);',
  30178. ' p:=typeinfo(boolean);',
  30179. ' p:=typeinfo(tyesno);',
  30180. ' p:=typeinfo(char);',
  30181. ' p:=typeinfo(tletter);',
  30182. ' p:=typeinfo(double);',
  30183. ' p:=typeinfo(tfloat);',
  30184. ' p:=typeinfo(pointer);',
  30185. ' p:=typeinfo(tptr);',
  30186. ' p:=typeinfo(shortint);',
  30187. ' p:=typeinfo(tshortint);',
  30188. ' p:=typeinfo(byte);',
  30189. ' p:=typeinfo(tbyte);',
  30190. ' p:=typeinfo(smallint);',
  30191. ' p:=typeinfo(tsmallint);',
  30192. ' p:=typeinfo(word);',
  30193. ' p:=typeinfo(tword);',
  30194. ' p:=typeinfo(longword);',
  30195. ' p:=typeinfo(tdword);',
  30196. ' p:=typeinfo(jsvalue);',
  30197. ' p:=typeinfo(tvalue);',
  30198. '']);
  30199. ConvertProgram;
  30200. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30201. LinesToStr([ // statements
  30202. 'this.p = null;',
  30203. '']),
  30204. LinesToStr([ // $mod.$main
  30205. '$mod.p = rtl.string;',
  30206. '$mod.p = rtl.string;',
  30207. '$mod.p = rtl.boolean;',
  30208. '$mod.p = rtl.boolean;',
  30209. '$mod.p = rtl.char;',
  30210. '$mod.p = rtl.char;',
  30211. '$mod.p = rtl.double;',
  30212. '$mod.p = rtl.double;',
  30213. '$mod.p = rtl.pointer;',
  30214. '$mod.p = rtl.pointer;',
  30215. '$mod.p = rtl.shortint;',
  30216. '$mod.p = rtl.shortint;',
  30217. '$mod.p = rtl.byte;',
  30218. '$mod.p = rtl.byte;',
  30219. '$mod.p = rtl.smallint;',
  30220. '$mod.p = rtl.smallint;',
  30221. '$mod.p = rtl.word;',
  30222. '$mod.p = rtl.word;',
  30223. '$mod.p = rtl.longword;',
  30224. '$mod.p = rtl.longword;',
  30225. '$mod.p = rtl.jsvalue;',
  30226. '$mod.p = rtl.jsvalue;',
  30227. '']));
  30228. end;
  30229. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30230. begin
  30231. WithTypeInfo:=true;
  30232. StartProgram(false);
  30233. Add([
  30234. 'type',
  30235. ' TCaption = type string;',
  30236. ' TYesNo = type boolean;',
  30237. ' TLetter = type char;',
  30238. ' TFloat = type double;',
  30239. ' TPtr = type pointer;',
  30240. ' TShortInt = type shortint;',
  30241. ' TByte = type byte;',
  30242. ' TSmallInt = type smallint;',
  30243. ' TWord = type word;',
  30244. ' TInt32 = type longint;',
  30245. ' TDWord = type longword;',
  30246. ' TValue = type jsvalue;',
  30247. ' TAliasValue = type TValue;',
  30248. 'var',
  30249. ' p: TPtr;',
  30250. ' a: TAliasValue;',
  30251. 'begin',
  30252. ' p:=typeinfo(tcaption);',
  30253. ' p:=typeinfo(tyesno);',
  30254. ' p:=typeinfo(tletter);',
  30255. ' p:=typeinfo(tfloat);',
  30256. ' p:=typeinfo(tptr);',
  30257. ' p:=typeinfo(tshortint);',
  30258. ' p:=typeinfo(tbyte);',
  30259. ' p:=typeinfo(tsmallint);',
  30260. ' p:=typeinfo(tword);',
  30261. ' p:=typeinfo(tdword);',
  30262. ' p:=typeinfo(tvalue);',
  30263. ' p:=typeinfo(taliasvalue);',
  30264. ' p:=typeinfo(a);',
  30265. '']);
  30266. ConvertProgram;
  30267. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30268. LinesToStr([ // statements
  30269. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30270. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30271. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30272. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30273. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30274. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30275. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30276. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30277. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30278. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30279. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30280. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30281. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30282. 'this.p = null;',
  30283. 'this.a = undefined;',
  30284. '']),
  30285. LinesToStr([ // $mod.$main
  30286. '$mod.p = $mod.$rtti["TCaption"];',
  30287. '$mod.p = $mod.$rtti["TYesNo"];',
  30288. '$mod.p = $mod.$rtti["TLetter"];',
  30289. '$mod.p = $mod.$rtti["TFloat"];',
  30290. '$mod.p = $mod.$rtti["TPtr"];',
  30291. '$mod.p = $mod.$rtti["TShortInt"];',
  30292. '$mod.p = $mod.$rtti["TByte"];',
  30293. '$mod.p = $mod.$rtti["TSmallInt"];',
  30294. '$mod.p = $mod.$rtti["TWord"];',
  30295. '$mod.p = $mod.$rtti["TDWord"];',
  30296. '$mod.p = $mod.$rtti["TValue"];',
  30297. '$mod.p = $mod.$rtti["TAliasValue"];',
  30298. '$mod.p = $mod.$rtti["TAliasValue"];',
  30299. '']));
  30300. end;
  30301. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30302. begin
  30303. WithTypeInfo:=true;
  30304. StartProgram(false);
  30305. Add('procedure DoIt;');
  30306. Add('type');
  30307. Add(' integer = longint;');
  30308. Add(' TPoint = record');
  30309. Add(' x,y: integer;');
  30310. Add(' end;');
  30311. Add('var p: pointer;');
  30312. Add('begin');
  30313. Add(' p:=typeinfo(tpoint);');
  30314. Add('end;');
  30315. Add('begin');
  30316. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30317. ConvertProgram;
  30318. end;
  30319. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30320. begin
  30321. WithTypeInfo:=true;
  30322. StartProgram(true,[supTypeInfo]);
  30323. Add([
  30324. '{$modeswitch externalclass}',
  30325. 'type',
  30326. ' TFlag = (up,down);',
  30327. ' TFlags = set of TFlag;',
  30328. 'var',
  30329. ' ti: TTypeInfo;',
  30330. ' tiInt: TTypeInfoInteger;',
  30331. ' tiEnum: TTypeInfoEnum;',
  30332. ' tiSet: TTypeInfoSet;',
  30333. 'begin',
  30334. ' ti:=typeinfo(string);',
  30335. ' ti:=typeinfo(boolean);',
  30336. ' ti:=typeinfo(char);',
  30337. ' ti:=typeinfo(double);',
  30338. ' tiInt:=typeinfo(shortint);',
  30339. ' tiInt:=typeinfo(byte);',
  30340. ' tiInt:=typeinfo(smallint);',
  30341. ' tiInt:=typeinfo(word);',
  30342. ' tiInt:=typeinfo(longint);',
  30343. ' tiInt:=typeinfo(longword);',
  30344. ' ti:=typeinfo(jsvalue);',
  30345. ' tiEnum:=typeinfo(tflag);',
  30346. ' tiSet:=typeinfo(tflags);']);
  30347. ConvertProgram;
  30348. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30349. LinesToStr([ // statements
  30350. 'this.TFlag = {',
  30351. ' "0": "up",',
  30352. ' up: 0,',
  30353. ' "1": "down",',
  30354. ' down: 1',
  30355. '};',
  30356. 'this.$rtti.$Enum("TFlag", {',
  30357. ' minvalue: 0,',
  30358. ' maxvalue: 1,',
  30359. ' ordtype: 1,',
  30360. ' enumtype: this.TFlag',
  30361. '});',
  30362. 'this.$rtti.$Set("TFlags", {',
  30363. ' comptype: this.$rtti["TFlag"]',
  30364. '});',
  30365. 'this.ti = null;',
  30366. 'this.tiInt = null;',
  30367. 'this.tiEnum = null;',
  30368. 'this.tiSet = null;',
  30369. '']),
  30370. LinesToStr([ // $mod.$main
  30371. '$mod.ti = rtl.string;',
  30372. '$mod.ti = rtl.boolean;',
  30373. '$mod.ti = rtl.char;',
  30374. '$mod.ti = rtl.double;',
  30375. '$mod.tiInt = rtl.shortint;',
  30376. '$mod.tiInt = rtl.byte;',
  30377. '$mod.tiInt = rtl.smallint;',
  30378. '$mod.tiInt = rtl.word;',
  30379. '$mod.tiInt = rtl.longint;',
  30380. '$mod.tiInt = rtl.longword;',
  30381. '$mod.ti = rtl.jsvalue;',
  30382. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30383. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30384. '']));
  30385. end;
  30386. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30387. begin
  30388. WithTypeInfo:=true;
  30389. StartProgram(true,[supTypeInfo]);
  30390. Add('{$modeswitch externalclass}');
  30391. Add('type');
  30392. Add(' TStaticArr = array[boolean] of string;');
  30393. Add(' TDynArr = array of string;');
  30394. Add(' TProc = procedure;');
  30395. Add(' TMethod = procedure of object;');
  30396. Add('var');
  30397. Add(' StaticArray: TStaticArr;');
  30398. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30399. Add(' DynArray: TDynArr;');
  30400. Add(' tiDynArray: TTypeInfoDynArray;');
  30401. Add(' ProcVar: TProc;');
  30402. Add(' tiProcVar: TTypeInfoProcVar;');
  30403. Add(' MethodVar: TMethod;');
  30404. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30405. Add('begin');
  30406. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30407. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30408. Add(' tiDynArray:=typeinfo(DynArray);');
  30409. Add(' tiDynArray:=typeinfo(TDynArr);');
  30410. Add(' tiProcVar:=typeinfo(ProcVar);');
  30411. Add(' tiProcVar:=typeinfo(TProc);');
  30412. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30413. Add(' tiMethodVar:=typeinfo(TMethod);');
  30414. ConvertProgram;
  30415. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30416. LinesToStr([ // statements
  30417. 'this.$rtti.$StaticArray("TStaticArr", {',
  30418. ' dims: [2],',
  30419. ' eltype: rtl.string',
  30420. '});',
  30421. 'this.$rtti.$DynArray("TDynArr", {',
  30422. ' eltype: rtl.string',
  30423. '});',
  30424. 'this.$rtti.$ProcVar("TProc", {',
  30425. ' procsig: rtl.newTIProcSig([])',
  30426. '});',
  30427. 'this.$rtti.$MethodVar("TMethod", {',
  30428. ' procsig: rtl.newTIProcSig([]),',
  30429. ' methodkind: 0',
  30430. '});',
  30431. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30432. 'this.tiStaticArray = null;',
  30433. 'this.DynArray = [];',
  30434. 'this.tiDynArray = null;',
  30435. 'this.ProcVar = null;',
  30436. 'this.tiProcVar = null;',
  30437. 'this.MethodVar = null;',
  30438. 'this.tiMethodVar = null;',
  30439. '']),
  30440. LinesToStr([ // $mod.$main
  30441. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30442. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30443. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30444. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30445. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30446. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30447. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30448. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30449. '']));
  30450. end;
  30451. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30452. begin
  30453. WithTypeInfo:=true;
  30454. StartProgram(true,[supTypeInfo]);
  30455. Add('{$modeswitch externalclass}');
  30456. Add('type');
  30457. Add(' TRec = record end;');
  30458. // ToDo: ^TRec
  30459. Add(' TObject = class end;');
  30460. Add(' TClass = class of tobject;');
  30461. Add('var');
  30462. Add(' Rec: trec;');
  30463. Add(' tiRecord: ttypeinforecord;');
  30464. Add(' Obj: tobject;');
  30465. Add(' tiClass: ttypeinfoclass;');
  30466. Add(' aClass: tclass;');
  30467. Add(' tiClassRef: ttypeinfoclassref;');
  30468. // ToDo: ^TRec
  30469. Add(' tiPointer: ttypeinfopointer;');
  30470. Add('begin');
  30471. Add(' tirecord:=typeinfo(trec);');
  30472. Add(' tirecord:=typeinfo(trec);');
  30473. Add(' ticlass:=typeinfo(obj);');
  30474. Add(' ticlass:=typeinfo(tobject);');
  30475. Add(' ticlass:=typeinfo(aclass);');
  30476. Add(' ticlassref:=typeinfo(tclass);');
  30477. ConvertProgram;
  30478. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30479. LinesToStr([ // statements
  30480. 'rtl.recNewT(this, "TRec", function () {',
  30481. ' this.$eq = function (b) {',
  30482. ' return true;',
  30483. ' };',
  30484. ' this.$assign = function (s) {',
  30485. ' return this;',
  30486. ' };',
  30487. ' $mod.$rtti.$Record("TRec", {});',
  30488. '});',
  30489. 'rtl.createClass(this, "TObject", null, function () {',
  30490. ' this.$init = function () {',
  30491. ' };',
  30492. ' this.$final = function () {',
  30493. ' };',
  30494. '});',
  30495. 'this.$rtti.$ClassRef("TClass", {',
  30496. ' instancetype: this.$rtti["TObject"]',
  30497. '});',
  30498. 'this.Rec = this.TRec.$new();',
  30499. 'this.tiRecord = null;',
  30500. 'this.Obj = null;',
  30501. 'this.tiClass = null;',
  30502. 'this.aClass = null;',
  30503. 'this.tiClassRef = null;',
  30504. 'this.tiPointer = null;',
  30505. '']),
  30506. LinesToStr([ // $mod.$main
  30507. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30508. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30509. '$mod.tiClass = $mod.Obj.$rtti;',
  30510. '$mod.tiClass = $mod.$rtti["TObject"];',
  30511. '$mod.tiClass = $mod.aClass.$rtti;',
  30512. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30513. '']));
  30514. end;
  30515. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30516. begin
  30517. WithTypeInfo:=true;
  30518. StartProgram(true,[supTypeInfo]);
  30519. Add([
  30520. '{$modeswitch externalclass}',
  30521. 'type',
  30522. ' TClass = class of tobject;',
  30523. ' TObject = class',
  30524. ' function MyClass: TClass;',
  30525. ' class function ClassType: TClass;',
  30526. ' end;',
  30527. 'function TObject.MyClass: TClass;',
  30528. 'var t: TTypeInfoClass;',
  30529. 'begin',
  30530. ' t:=TypeInfo(Self);',
  30531. ' t:=TypeInfo(Result);',
  30532. ' t:=TypeInfo(TObject);',
  30533. 'end;',
  30534. 'class function TObject.ClassType: TClass;',
  30535. 'var t: TTypeInfoClass;',
  30536. 'begin',
  30537. ' t:=TypeInfo(Self);',
  30538. ' t:=TypeInfo(Result);',
  30539. 'end;',
  30540. 'var',
  30541. ' Obj: TObject;',
  30542. ' t: TTypeInfoClass;',
  30543. 'begin',
  30544. ' t:=TypeInfo(TObject.ClassType);',
  30545. ' t:=TypeInfo(Obj.ClassType);',
  30546. ' t:=TypeInfo(Obj.MyClass);',
  30547. '']);
  30548. ConvertProgram;
  30549. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30550. LinesToStr([ // statements
  30551. 'this.$rtti.$Class("TObject");',
  30552. 'this.$rtti.$ClassRef("TClass", {',
  30553. ' instancetype: this.$rtti["TObject"]',
  30554. '});',
  30555. 'rtl.createClass(this, "TObject", null, function () {',
  30556. ' this.$init = function () {',
  30557. ' };',
  30558. ' this.$final = function () {',
  30559. ' };',
  30560. ' this.MyClass = function () {',
  30561. ' var Result = null;',
  30562. ' var t = null;',
  30563. ' t = this.$rtti;',
  30564. ' t = Result.$rtti;',
  30565. ' t = $mod.$rtti["TObject"];',
  30566. ' return Result;',
  30567. ' };',
  30568. ' this.ClassType = function () {',
  30569. ' var Result = null;',
  30570. ' var t = null;',
  30571. ' t = this.$rtti;',
  30572. ' t = Result.$rtti;',
  30573. ' return Result;',
  30574. ' };',
  30575. '});',
  30576. 'this.Obj = null;',
  30577. 'this.t = null;',
  30578. '']),
  30579. LinesToStr([ // $mod.$main
  30580. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30581. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30582. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30583. '']));
  30584. end;
  30585. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30586. begin
  30587. WithTypeInfo:=true;
  30588. AddModuleWithIntfImplSrc('typinfo.pas',
  30589. LinesToStr([
  30590. '{$modeswitch externalclass}',
  30591. 'type',
  30592. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30593. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30594. '']),
  30595. '');
  30596. AddModuleWithIntfImplSrc('unit2.pas',
  30597. LinesToStr([
  30598. 'uses typinfo;',
  30599. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30600. 'procedure DoPtr(p: PTypeInfo);',
  30601. 'procedure DoInfo(t: TTypeInfo);',
  30602. 'procedure DoInt(t: TTypeInfoInteger);',
  30603. '']),
  30604. LinesToStr([
  30605. 'procedure DoPtr(p: PTypeInfo);',
  30606. 'begin end;',
  30607. 'procedure DoInfo(t: TTypeInfo);',
  30608. 'begin end;',
  30609. 'procedure DoInt(t: TTypeInfoInteger);',
  30610. 'begin end;',
  30611. '']));
  30612. StartUnit(true);
  30613. Add([
  30614. 'interface',
  30615. 'uses unit2;', // does not use unit typinfo
  30616. 'implementation',
  30617. 'var',
  30618. ' i: byte;',
  30619. ' p: pointer;',
  30620. ' t: PTypeInfo;',
  30621. 'initialization',
  30622. ' p:=typeinfo(i);',
  30623. ' t:=typeinfo(i);',
  30624. ' if p=t then ;',
  30625. ' if p=typeinfo(i) then ;',
  30626. ' if typeinfo(i)=p then ;',
  30627. ' if t=typeinfo(i) then ;',
  30628. ' if typeinfo(i)=t then ;',
  30629. ' DoPtr(p);',
  30630. ' DoPtr(t);',
  30631. ' DoPtr(typeinfo(i));',
  30632. ' DoInfo(p);',
  30633. ' DoInfo(t);',
  30634. ' DoInfo(typeinfo(i));',
  30635. ' DoInt(typeinfo(i));',
  30636. '']);
  30637. ConvertUnit;
  30638. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30639. LinesToStr([ // statements
  30640. 'var $impl = $mod.$impl;',
  30641. '']),
  30642. LinesToStr([ // this.$init
  30643. '$impl.p = rtl.byte;',
  30644. '$impl.t = rtl.byte;',
  30645. 'if ($impl.p === $impl.t) ;',
  30646. 'if ($impl.p === rtl.byte) ;',
  30647. 'if (rtl.byte === $impl.p) ;',
  30648. 'if ($impl.t === rtl.byte) ;',
  30649. 'if (rtl.byte === $impl.t) ;',
  30650. 'pas.unit2.DoPtr($impl.p);',
  30651. 'pas.unit2.DoPtr($impl.t);',
  30652. 'pas.unit2.DoPtr(rtl.byte);',
  30653. 'pas.unit2.DoInfo($impl.p);',
  30654. 'pas.unit2.DoInfo($impl.t);',
  30655. 'pas.unit2.DoInfo(rtl.byte);',
  30656. 'pas.unit2.DoInt(rtl.byte);',
  30657. '']),
  30658. LinesToStr([ // implementation
  30659. '$impl.i = 0;',
  30660. '$impl.p = null;',
  30661. '$impl.t = null;',
  30662. '']) );
  30663. end;
  30664. procedure TTestModule.TestRTTI_Interface_Corba;
  30665. begin
  30666. WithTypeInfo:=true;
  30667. StartProgram(true,[supTypeInfo]);
  30668. Add([
  30669. '{$interfaces corba}',
  30670. '{$modeswitch externalclass}',
  30671. 'type',
  30672. ' IUnknown = interface',
  30673. ' end;',
  30674. ' IBird = interface',
  30675. ' function GetItem: longint;',
  30676. ' procedure SetItem(Value: longint);',
  30677. ' property Item: longint read GetItem write SetItem;',
  30678. ' end;',
  30679. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30680. 'var',
  30681. ' i: IBird;',
  30682. ' t: TTypeInfoInterface;',
  30683. 'begin',
  30684. ' t:=TypeInfo(IBird);',
  30685. ' t:=TypeInfo(i);',
  30686. ' DoIt(t);',
  30687. ' DoIt(TypeInfo(IBird));',
  30688. '']);
  30689. ConvertProgram;
  30690. CheckSource('TestRTTI_Interface_Corba',
  30691. LinesToStr([ // statements
  30692. 'rtl.createInterface(',
  30693. ' this,',
  30694. ' "IUnknown",',
  30695. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30696. ' [],',
  30697. ' null,',
  30698. ' function () {',
  30699. ' }',
  30700. ');',
  30701. 'rtl.createInterface(',
  30702. ' this,',
  30703. ' "IBird",',
  30704. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30705. ' ["GetItem", "SetItem"],',
  30706. ' null,',
  30707. ' function () {',
  30708. ' var $r = this.$rtti;',
  30709. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30710. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30711. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30712. ' }',
  30713. ');',
  30714. 'this.DoIt = function (t) {',
  30715. '}; ',
  30716. 'this.i = null;',
  30717. 'this.t = null;',
  30718. '']),
  30719. LinesToStr([ // $mod.$main
  30720. '$mod.t = $mod.$rtti["IBird"];',
  30721. '$mod.t = $mod.i.$rtti;',
  30722. '$mod.DoIt($mod.t);',
  30723. '$mod.DoIt($mod.$rtti["IBird"]);',
  30724. '']));
  30725. end;
  30726. procedure TTestModule.TestRTTI_Interface_COM;
  30727. begin
  30728. WithTypeInfo:=true;
  30729. StartProgram(true,[supTypeInfo]);
  30730. Add([
  30731. '{$interfaces com}',
  30732. '{$modeswitch externalclass}',
  30733. 'type',
  30734. ' TGuid = record end;',
  30735. ' integer = longint;',
  30736. ' IUnknown = interface',
  30737. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30738. ' function _AddRef: Integer;',
  30739. ' function _Release: Integer;',
  30740. ' end;',
  30741. ' IBird = interface',
  30742. ' function GetItem: longint;',
  30743. ' procedure SetItem(Value: longint);',
  30744. ' property Item: longint read GetItem write SetItem;',
  30745. ' end;',
  30746. 'var',
  30747. ' i: IBird;',
  30748. ' t: TTypeInfoInterface;',
  30749. 'begin',
  30750. ' t:=TypeInfo(IBird);',
  30751. ' t:=TypeInfo(i);',
  30752. '']);
  30753. ConvertProgram;
  30754. CheckSource('TestRTTI_Interface_COM',
  30755. LinesToStr([ // statements
  30756. 'rtl.recNewT(this, "TGuid", function () {',
  30757. ' this.$eq = function (b) {',
  30758. ' return true;',
  30759. ' };',
  30760. ' this.$assign = function (s) {',
  30761. ' return this;',
  30762. ' };',
  30763. ' $mod.$rtti.$Record("TGuid", {});',
  30764. '});',
  30765. 'rtl.createInterface(',
  30766. ' this,',
  30767. ' "IUnknown",',
  30768. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30769. ' ["QueryInterface", "_AddRef", "_Release"],',
  30770. ' null,',
  30771. ' function () {',
  30772. ' this.$kind = "com";',
  30773. ' var $r = this.$rtti;',
  30774. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30775. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  30776. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  30777. ' }',
  30778. ');',
  30779. 'rtl.createInterface(',
  30780. ' this,',
  30781. ' "IBird",',
  30782. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30783. ' ["GetItem", "SetItem"],',
  30784. ' this.IUnknown,',
  30785. ' function () {',
  30786. ' var $r = this.$rtti;',
  30787. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30788. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30789. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30790. ' }',
  30791. ');',
  30792. 'this.i = null;',
  30793. 'this.t = null;',
  30794. '']),
  30795. LinesToStr([ // $mod.$main
  30796. '$mod.t = $mod.$rtti["IBird"];',
  30797. '$mod.t = $mod.i.$rtti;',
  30798. '']));
  30799. end;
  30800. procedure TTestModule.TestRTTI_ClassHelper;
  30801. begin
  30802. WithTypeInfo:=true;
  30803. StartProgram(true,[supTypeInfo]);
  30804. Add([
  30805. '{$interfaces com}',
  30806. '{$modeswitch externalclass}',
  30807. 'type',
  30808. ' TObject = class',
  30809. ' end;',
  30810. ' THelper = class helper for TObject',
  30811. ' published',
  30812. ' function GetItem: longint;',
  30813. ' property Item: longint read GetItem;',
  30814. ' end;',
  30815. 'function THelper.GetItem: longint;',
  30816. 'begin',
  30817. 'end;',
  30818. 'var',
  30819. ' t: TTypeInfoHelper;',
  30820. 'begin',
  30821. ' t:=TypeInfo(THelper);',
  30822. '']);
  30823. ConvertProgram;
  30824. CheckSource('TestRTTI_ClassHelper',
  30825. LinesToStr([ // statements
  30826. 'rtl.createClass(this, "TObject", null, function () {',
  30827. ' this.$init = function () {',
  30828. ' };',
  30829. ' this.$final = function () {',
  30830. ' };',
  30831. '});',
  30832. 'rtl.createHelper(this, "THelper", null, function () {',
  30833. ' this.GetItem = function () {',
  30834. ' var Result = 0;',
  30835. ' return Result;',
  30836. ' };',
  30837. ' var $r = this.$rtti;',
  30838. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30839. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30840. '});',
  30841. 'this.t = null;',
  30842. '']),
  30843. LinesToStr([ // $mod.$main
  30844. '$mod.t = $mod.$rtti["THelper"];',
  30845. '']));
  30846. end;
  30847. procedure TTestModule.TestRTTI_ExternalClass;
  30848. begin
  30849. WithTypeInfo:=true;
  30850. StartProgram(true,[supTypeInfo]);
  30851. Add([
  30852. '{$modeswitch externalclass}',
  30853. 'type',
  30854. ' TJSObject = class external name ''Object''',
  30855. ' end;',
  30856. ' TJSArray = class external name ''Array'' (TJSObject)',
  30857. ' end;',
  30858. 'var',
  30859. ' p: Pointer;',
  30860. ' tc: TTypeInfoExtClass;',
  30861. 'begin',
  30862. ' p:=typeinfo(TJSArray);']);
  30863. ConvertProgram;
  30864. CheckSource('TestRTTI_ExternalClass',
  30865. LinesToStr([ // statements
  30866. 'this.$rtti.$ExtClass("TJSObject", {',
  30867. ' jsclass: "Object"',
  30868. '});',
  30869. 'this.$rtti.$ExtClass("TJSArray", {',
  30870. ' ancestor: this.$rtti["TJSObject"],',
  30871. ' jsclass: "Array"',
  30872. '});',
  30873. 'this.p = null;',
  30874. 'this.tc = null;',
  30875. '']),
  30876. LinesToStr([ // $mod.$main
  30877. '$mod.p = $mod.$rtti["TJSArray"];',
  30878. '']));
  30879. end;
  30880. procedure TTestModule.TestRTTI_Unit;
  30881. begin
  30882. WithTypeInfo:=true;
  30883. AddModuleWithIntfImplSrc('unit2.pas',
  30884. LinesToStr([
  30885. '{$mode delphi}',
  30886. 'type',
  30887. ' TWordArray = array of word;',
  30888. ' TArray<T> = array of T;',
  30889. '']),
  30890. '');
  30891. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30892. Add([
  30893. '{$mode delphi}',
  30894. 'interface',
  30895. 'uses unit2;',
  30896. 'type',
  30897. ' IBird = interface',
  30898. ' function Swoop: TWordArray;',
  30899. ' function Glide: TArray<word>;',
  30900. ' end;',
  30901. 'procedure Fly;',
  30902. 'implementation',
  30903. 'procedure Fly;',
  30904. 'var',
  30905. ' ta: tTypeInfoDynArray;',
  30906. ' ti: tTypeInfoInterface;',
  30907. 'begin',
  30908. ' ta:=typeinfo(TWordArray);',
  30909. ' ta:=typeinfo(TArray<word>);',
  30910. ' ti:=typeinfo(IBird);',
  30911. 'end;',
  30912. '']);
  30913. ConvertUnit;
  30914. CheckSource('TestRTTI_ExternalClass',
  30915. LinesToStr([ // statements
  30916. 'rtl.createInterface(',
  30917. ' this,',
  30918. ' "IBird",',
  30919. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30920. ' ["Swoop", "Glide"],',
  30921. ' pas.system.IUnknown,',
  30922. ' function () {',
  30923. ' var $r = this.$rtti;',
  30924. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  30925. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  30926. ' }',
  30927. ');',
  30928. 'this.Fly = function () {',
  30929. ' var ta = null;',
  30930. ' var ti = null;',
  30931. ' ta = pas.unit2.$rtti["TWordArray"];',
  30932. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30933. ' ti = $mod.$rtti["IBird"];',
  30934. '};',
  30935. '']),
  30936. LinesToStr([ // $mod.$main
  30937. '']));
  30938. end;
  30939. procedure TTestModule.TestResourcestringProgram;
  30940. begin
  30941. AddModuleWithIntfImplSrc('unit2.pas',
  30942. LinesToStr([
  30943. 'resourcestring Title = ''Nice'';',
  30944. '']),
  30945. '');
  30946. StartProgram(true);
  30947. Add([
  30948. 'uses unit2;',
  30949. 'const Bar = ''bar'';',
  30950. 'resourcestring',
  30951. ' Red = ''red'';',
  30952. ' Foobar = ''fOo''+bar;',
  30953. 'var s: string;',
  30954. ' c: char;',
  30955. 'begin',
  30956. ' s:=red;',
  30957. ' s:=test1.red;',
  30958. ' s:=Title;',
  30959. ' c:=red[1];',
  30960. ' c:=test1.red[2];',
  30961. ' if red=foobar then ;',
  30962. ' if red[3]=red[4] then ;']);
  30963. ConvertProgram;
  30964. CheckSource('TestResourcestringProgram',
  30965. LinesToStr([ // statements
  30966. 'this.Bar = "bar";',
  30967. 'this.s = "";',
  30968. 'this.c = "";',
  30969. '$mod.$resourcestrings = {',
  30970. ' Red: {',
  30971. ' org: "red"',
  30972. ' },',
  30973. ' Foobar: {',
  30974. ' org: "fOobar"',
  30975. ' }',
  30976. '};',
  30977. '']),
  30978. LinesToStr([ // $mod.$main
  30979. '$mod.s = rtl.getResStr($mod, "Red");',
  30980. '$mod.s = rtl.getResStr($mod, "Red");',
  30981. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30982. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30983. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30984. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30985. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30986. '']));
  30987. end;
  30988. procedure TTestModule.TestResourcestringUnit;
  30989. begin
  30990. AddModuleWithIntfImplSrc('unit2.pas',
  30991. LinesToStr([
  30992. 'resourcestring Title = ''Nice'';',
  30993. '']),
  30994. '');
  30995. StartUnit(true);
  30996. Add([
  30997. 'interface',
  30998. 'uses unit2;',
  30999. 'const Red = ''rEd'';',
  31000. 'resourcestring',
  31001. ' Blue = ''blue'';',
  31002. ' NotRed = ''not''+Red;',
  31003. 'var s: string;',
  31004. 'implementation',
  31005. 'resourcestring',
  31006. ' ImplGreen = ''green'';',
  31007. 'initialization',
  31008. ' s:=blue+ImplGreen;',
  31009. ' s:=test1.blue+test1.implgreen;',
  31010. ' s:=blue[1]+implgreen[2];',
  31011. ' s:=Title;',
  31012. '']);
  31013. ConvertUnit;
  31014. CheckSource('TestResourcestringUnit',
  31015. LinesToStr([ // statements
  31016. 'this.Red = "rEd";',
  31017. 'this.s = "";',
  31018. '$mod.$resourcestrings = {',
  31019. ' Blue: {',
  31020. ' org: "blue"',
  31021. ' },',
  31022. ' NotRed: {',
  31023. ' org: "notrEd"',
  31024. ' },',
  31025. ' ImplGreen: {',
  31026. ' org: "green"',
  31027. ' }',
  31028. '};',
  31029. '']),
  31030. LinesToStr([ // $mod.$main
  31031. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31032. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31033. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31034. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31035. '']));
  31036. end;
  31037. procedure TTestModule.TestResourcestringImplementation;
  31038. begin
  31039. StartUnit(false);
  31040. Add([
  31041. 'interface',
  31042. 'implementation',
  31043. 'resourcestring',
  31044. ' ImplRed = ''red'';']);
  31045. ConvertUnit;
  31046. CheckSource('TestResourcestringImplementation',
  31047. LinesToStr([ // intf statements
  31048. 'var $impl = $mod.$impl;']),
  31049. LinesToStr([ // $mod.$init
  31050. '']),
  31051. LinesToStr([ // impl statements
  31052. '$mod.$resourcestrings = {',
  31053. ' ImplRed: {',
  31054. ' org: "red"',
  31055. ' }',
  31056. '};',
  31057. '']));
  31058. end;
  31059. procedure TTestModule.TestAttributes_Members;
  31060. begin
  31061. WithTypeInfo:=true;
  31062. StartProgram(false);
  31063. Add([
  31064. '{$modeswitch PrefixedAttributes}',
  31065. 'type',
  31066. ' TObject = class',
  31067. ' constructor Create;',
  31068. ' end;',
  31069. ' TCustomAttribute = class',
  31070. ' constructor Create(Id: word);',
  31071. ' end;',
  31072. ' [Missing]',
  31073. ' TBird = class',
  31074. ' published',
  31075. ' [Tcustom]',
  31076. ' FField: word;',
  31077. ' [tcustom(14)]',
  31078. ' property Size: word read FField;',
  31079. ' [Tcustom(15)]',
  31080. ' procedure Fly; virtual; abstract;',
  31081. ' end;',
  31082. ' TRec = record',
  31083. ' [Tcustom,tcustom(14)]',
  31084. ' Size: word;',
  31085. ' end;',
  31086. 'constructor TObject.Create; begin end;',
  31087. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31088. 'begin',
  31089. '']);
  31090. ConvertProgram;
  31091. CheckSource('TestAttributes_Members',
  31092. LinesToStr([ // statements
  31093. 'rtl.createClass(this, "TObject", null, function () {',
  31094. ' this.$init = function () {',
  31095. ' };',
  31096. ' this.$final = function () {',
  31097. ' };',
  31098. ' this.Create = function () {',
  31099. ' return this;',
  31100. ' };',
  31101. '});',
  31102. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31103. ' this.Create$1 = function (Id) {',
  31104. ' return this;',
  31105. ' };',
  31106. '});',
  31107. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31108. ' this.$init = function () {',
  31109. ' $mod.TObject.$init.call(this);',
  31110. ' this.FField = 0;',
  31111. ' };',
  31112. ' var $r = this.$rtti;',
  31113. ' $r.addField("FField", rtl.word, {',
  31114. ' attr: [$mod.TCustomAttribute, "Create"]',
  31115. ' });',
  31116. ' $r.addProperty(',
  31117. ' "Size",',
  31118. ' 0,',
  31119. ' rtl.word,',
  31120. ' "FField",',
  31121. ' "",',
  31122. ' {',
  31123. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31124. ' }',
  31125. ' );',
  31126. ' $r.addMethod("Fly", 0, [], null, 0, {',
  31127. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31128. ' });',
  31129. '});',
  31130. 'rtl.recNewT(this, "TRec", function () {',
  31131. ' this.Size = 0;',
  31132. ' this.$eq = function (b) {',
  31133. ' return this.Size === b.Size;',
  31134. ' };',
  31135. ' this.$assign = function (s) {',
  31136. ' this.Size = s.Size;',
  31137. ' return this;',
  31138. ' };',
  31139. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31140. ' $r.addField("Size", rtl.word, {',
  31141. ' attr: [',
  31142. ' $mod.TCustomAttribute,',
  31143. ' "Create",',
  31144. ' $mod.TCustomAttribute,',
  31145. ' "Create$1",',
  31146. ' [14]',
  31147. ' ]',
  31148. ' });',
  31149. '});',
  31150. '']),
  31151. LinesToStr([ // $mod.$main
  31152. '']));
  31153. end;
  31154. procedure TTestModule.TestAttributes_Types;
  31155. begin
  31156. WithTypeInfo:=true;
  31157. StartProgram(false);
  31158. Add([
  31159. '{$modeswitch PrefixedAttributes}',
  31160. 'type',
  31161. ' TObject = class',
  31162. ' constructor Create(Id: word);',
  31163. ' end;',
  31164. ' TCustomAttribute = class',
  31165. ' end;',
  31166. ' [TCustom(1)]',
  31167. ' TMyClass = class',
  31168. ' end;',
  31169. ' [TCustom(11)]',
  31170. ' TMyDescendant = class(TMyClass)',
  31171. ' end;',
  31172. ' [TCustom(2)]',
  31173. ' TRec = record',
  31174. ' end;',
  31175. ' [TCustom(3)]',
  31176. ' TInt = type word;',
  31177. 'constructor TObject.Create(Id: word);',
  31178. 'begin',
  31179. 'end;',
  31180. 'var p: pointer;',
  31181. 'begin',
  31182. ' p:=typeinfo(TMyClass);',
  31183. ' p:=typeinfo(TRec);',
  31184. ' p:=typeinfo(TInt);',
  31185. '']);
  31186. ConvertProgram;
  31187. CheckSource('TestAttributes_Types',
  31188. LinesToStr([ // statements
  31189. 'rtl.createClass(this, "TObject", null, function () {',
  31190. ' this.$init = function () {',
  31191. ' };',
  31192. ' this.$final = function () {',
  31193. ' };',
  31194. ' this.Create = function (Id) {',
  31195. ' return this;',
  31196. ' };',
  31197. '});',
  31198. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31199. '});',
  31200. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31201. ' var $r = this.$rtti;',
  31202. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31203. '});',
  31204. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31205. ' var $r = this.$rtti;',
  31206. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31207. '});',
  31208. 'rtl.recNewT(this, "TRec", function () {',
  31209. ' this.$eq = function (b) {',
  31210. ' return true;',
  31211. ' };',
  31212. ' this.$assign = function (s) {',
  31213. ' return this;',
  31214. ' };',
  31215. ' $mod.$rtti.$Record("TRec", {',
  31216. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31217. ' });',
  31218. '});',
  31219. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31220. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31221. '});',
  31222. 'this.p = null;',
  31223. '']),
  31224. LinesToStr([ // $mod.$main
  31225. '$mod.p = $mod.$rtti["TMyClass"];',
  31226. '$mod.p = $mod.$rtti["TRec"];',
  31227. '$mod.p = $mod.$rtti["TInt"];',
  31228. '']));
  31229. end;
  31230. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31231. begin
  31232. WithTypeInfo:=true;
  31233. StartProgram(false);
  31234. Add([
  31235. '{$modeswitch PrefixedAttributes}',
  31236. 'type',
  31237. ' TObject = class',
  31238. ' constructor Create;',
  31239. ' end;',
  31240. ' TCustomAttribute = class',
  31241. ' end;',
  31242. ' THelper = class helper for TCustomAttribute',
  31243. ' constructor Create(Id: word);',
  31244. ' end;',
  31245. ' [TCustom(3)]',
  31246. ' TMyInt = word;',
  31247. 'constructor TObject.Create; begin end;',
  31248. 'constructor THelper.Create(Id: word); begin end;',
  31249. 'begin',
  31250. ' if typeinfo(TMyInt)=nil then ;']);
  31251. ConvertProgram;
  31252. end;
  31253. procedure TTestModule.TestAssert;
  31254. begin
  31255. StartProgram(false);
  31256. Add([
  31257. 'procedure DoIt;',
  31258. 'var',
  31259. ' b: boolean;',
  31260. ' s: string;',
  31261. 'begin',
  31262. ' {$Assertions on}',
  31263. ' Assert(b);',
  31264. 'end;',
  31265. 'begin',
  31266. ' DoIt;',
  31267. '']);
  31268. ConvertProgram;
  31269. CheckSource('TestAssert',
  31270. LinesToStr([ // statements
  31271. 'this.DoIt = function () {',
  31272. ' var b = false;',
  31273. ' var s = "";',
  31274. ' if (!b) throw "assert failed";',
  31275. '};',
  31276. '']),
  31277. LinesToStr([ // $mod.$main
  31278. '$mod.DoIt();',
  31279. '']));
  31280. end;
  31281. procedure TTestModule.TestAssert_SysUtils;
  31282. begin
  31283. AddModuleWithIntfImplSrc('SysUtils.pas',
  31284. LinesToStr([
  31285. 'type',
  31286. ' TObject = class',
  31287. ' constructor Create;',
  31288. ' end;',
  31289. ' EAssertionFailed = class',
  31290. ' constructor Create(s: string);',
  31291. ' end;',
  31292. '']),
  31293. LinesToStr([
  31294. 'constructor TObject.Create;',
  31295. 'begin end;',
  31296. 'constructor EAssertionFailed.Create(s: string);',
  31297. 'begin end;',
  31298. '']) );
  31299. StartProgram(true);
  31300. Add([
  31301. 'uses sysutils;',
  31302. 'procedure DoIt;',
  31303. 'var',
  31304. ' b: boolean;',
  31305. ' s: string;',
  31306. 'begin',
  31307. ' {$Assertions on}',
  31308. ' Assert(b);',
  31309. ' Assert(b,''msg'');',
  31310. 'end;',
  31311. 'begin',
  31312. ' DoIt;',
  31313. '']);
  31314. ConvertProgram;
  31315. CheckSource('TestAssert_SysUtils',
  31316. LinesToStr([ // statements
  31317. 'this.DoIt = function () {',
  31318. ' var b = false;',
  31319. ' var s = "";',
  31320. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31321. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31322. '};',
  31323. '']),
  31324. LinesToStr([ // $mod.$main
  31325. '$mod.DoIt();',
  31326. '']));
  31327. end;
  31328. procedure TTestModule.TestObjectChecks;
  31329. begin
  31330. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31331. StartProgram(false);
  31332. Add([
  31333. 'type',
  31334. ' TObject = class',
  31335. ' procedure DoIt;',
  31336. ' end;',
  31337. ' TClass = class of tobject;',
  31338. ' TBird = class',
  31339. ' end;',
  31340. ' TBirdClass = class of TBird;',
  31341. 'var',
  31342. ' o : TObject;',
  31343. ' c: TClass;',
  31344. ' b: TBird;',
  31345. ' bc: TBirdClass;',
  31346. 'procedure TObject.DoIt;',
  31347. 'begin',
  31348. ' b:=TBird(o);',
  31349. 'end;',
  31350. 'begin',
  31351. ' o.DoIt;',
  31352. ' b:=TBird(o);',
  31353. ' bc:=TBirdClass(c);',
  31354. '']);
  31355. ConvertProgram;
  31356. CheckSource('TestCheckMethodCall',
  31357. LinesToStr([ // statements
  31358. 'rtl.createClass(this, "TObject", null, function () {',
  31359. ' this.$init = function () {',
  31360. ' };',
  31361. ' this.$final = function () {',
  31362. ' };',
  31363. ' this.DoIt = function () {',
  31364. ' rtl.checkMethodCall(this,$mod.TObject);',
  31365. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31366. ' };',
  31367. '});',
  31368. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31369. '});',
  31370. 'this.o = null;',
  31371. 'this.c = null;',
  31372. 'this.b = null;',
  31373. 'this.bc = null;',
  31374. '']),
  31375. LinesToStr([ // $mod.$main
  31376. '$mod.o.DoIt();',
  31377. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31378. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31379. '']));
  31380. end;
  31381. procedure TTestModule.TestOverflowChecks_Int;
  31382. begin
  31383. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31384. StartProgram(false);
  31385. Add([
  31386. 'procedure DoIt;',
  31387. 'var',
  31388. ' b: byte;',
  31389. ' n: nativeint;',
  31390. ' u: nativeuint;',
  31391. ' c: currency;',
  31392. 'begin',
  31393. ' n:=n+n;',
  31394. ' n:=n-n;',
  31395. ' n:=n+b;',
  31396. ' n:=b-n;',
  31397. ' n:=n*n;',
  31398. ' n:=n*u;',
  31399. ' c:=c+b;',
  31400. ' c:=b+c;',
  31401. ' c:=c*b;',
  31402. ' c:=b*c;',
  31403. 'end;',
  31404. 'begin',
  31405. '']);
  31406. ConvertProgram;
  31407. CheckSource('TestOverflowChecks_Int',
  31408. LinesToStr([ // statements
  31409. 'this.DoIt = function () {',
  31410. ' var b = 0;',
  31411. ' var n = 0;',
  31412. ' var u = 0;',
  31413. ' var c = 0;',
  31414. ' n = rtl.oc(n + n);',
  31415. ' n = rtl.oc(n - n);',
  31416. ' n = rtl.oc(n + b);',
  31417. ' n = rtl.oc(b - n);',
  31418. ' n = rtl.oc(n * n);',
  31419. ' n = rtl.oc(n * u);',
  31420. ' c = rtl.oc(c + (b * 10000));',
  31421. ' c = rtl.oc((b * 10000) + c);',
  31422. ' c = rtl.oc(c * b);',
  31423. ' c = rtl.oc(b * c);',
  31424. '};',
  31425. '']),
  31426. LinesToStr([ // $mod.$main
  31427. '']));
  31428. end;
  31429. procedure TTestModule.TestRangeChecks_AssignInt;
  31430. begin
  31431. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31432. StartProgram(false);
  31433. Add([
  31434. '{$R+}',
  31435. 'var',
  31436. ' b: byte = 2;',
  31437. ' w: word = 3;',
  31438. 'procedure DoIt(p: byte);',
  31439. 'begin',
  31440. ' b:=w;',
  31441. ' b+=w;',
  31442. ' b:=1;',
  31443. 'end;',
  31444. '{$R-}',
  31445. 'procedure DoSome;',
  31446. 'begin',
  31447. ' DoIt(w);',
  31448. ' b:=w;',
  31449. ' b:=2;',
  31450. 'end;',
  31451. 'begin',
  31452. '{$R+}',
  31453. '']);
  31454. ConvertProgram;
  31455. CheckSource('TestRangeChecks_AssignInt',
  31456. LinesToStr([ // statements
  31457. 'this.b = 2;',
  31458. 'this.w = 3;',
  31459. 'this.DoIt = function (p) {',
  31460. ' rtl.rc(p, 0, 255);',
  31461. ' $mod.b = rtl.rc($mod.w,0,255);',
  31462. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31463. ' $mod.b = 1;',
  31464. '};',
  31465. 'this.DoSome = function () {',
  31466. ' $mod.DoIt($mod.w);',
  31467. ' $mod.b = $mod.w;',
  31468. ' $mod.b = 2;',
  31469. '};',
  31470. '']),
  31471. LinesToStr([ // $mod.$main
  31472. '']));
  31473. end;
  31474. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31475. begin
  31476. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31477. StartProgram(false);
  31478. Add([
  31479. '{$R+}',
  31480. 'type Ten = 1..10;',
  31481. 'var',
  31482. ' b: Ten = 2;',
  31483. ' w: Ten = 3;',
  31484. 'procedure DoIt(p: Ten);',
  31485. 'begin',
  31486. ' b:=w;',
  31487. ' b+=w;',
  31488. ' b:=1;',
  31489. 'end;',
  31490. '{$R-}',
  31491. 'procedure DoSome;',
  31492. 'begin',
  31493. ' DoIt(w);',
  31494. ' b:=w;',
  31495. ' b:=2;',
  31496. 'end;',
  31497. 'begin',
  31498. '{$R+}',
  31499. '']);
  31500. ConvertProgram;
  31501. CheckSource('TestRangeChecks_AssignIntRange',
  31502. LinesToStr([ // statements
  31503. 'this.b = 2;',
  31504. 'this.w = 3;',
  31505. 'this.DoIt = function (p) {',
  31506. ' rtl.rc(p, 1, 10);',
  31507. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31508. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31509. ' $mod.b = 1;',
  31510. '};',
  31511. 'this.DoSome = function () {',
  31512. ' $mod.DoIt($mod.w);',
  31513. ' $mod.b = $mod.w;',
  31514. ' $mod.b = 2;',
  31515. '};',
  31516. '']),
  31517. LinesToStr([ // $mod.$main
  31518. '']));
  31519. end;
  31520. procedure TTestModule.TestRangeChecks_AssignEnum;
  31521. begin
  31522. StartProgram(false);
  31523. Add([
  31524. '{$R+}',
  31525. 'type TEnum = (red,green);',
  31526. 'var',
  31527. ' e: TEnum = red;',
  31528. 'procedure DoIt(p: TEnum);',
  31529. 'begin',
  31530. ' e:=p;',
  31531. ' p:=TEnum(0);',
  31532. ' p:=succ(e);',
  31533. 'end;',
  31534. '{$R-}',
  31535. 'procedure DoSome;',
  31536. 'begin',
  31537. ' DoIt(e);',
  31538. ' e:=TEnum(1);',
  31539. ' e:=pred(e);',
  31540. 'end;',
  31541. 'begin',
  31542. '{$R+}',
  31543. '']);
  31544. ConvertProgram;
  31545. CheckSource('TestRangeChecks_AssignEnum',
  31546. LinesToStr([ // statements
  31547. 'this.TEnum = {',
  31548. ' "0": "red",',
  31549. ' red: 0,',
  31550. ' "1": "green",',
  31551. ' green: 1',
  31552. '};',
  31553. 'this.e = this.TEnum.red;',
  31554. 'this.DoIt = function (p) {',
  31555. ' rtl.rc(p, 0, 1);',
  31556. ' $mod.e = rtl.rc(p, 0, 1);',
  31557. ' p = 0;',
  31558. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31559. '};',
  31560. 'this.DoSome = function () {',
  31561. ' $mod.DoIt($mod.e);',
  31562. ' $mod.e = 1;',
  31563. ' $mod.e = $mod.e - 1;',
  31564. '};',
  31565. '']),
  31566. LinesToStr([ // $mod.$main
  31567. '']));
  31568. end;
  31569. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31570. begin
  31571. StartProgram(false);
  31572. Add([
  31573. '{$R+}',
  31574. 'type',
  31575. ' TEnum = (red,green);',
  31576. ' TEnumRg = red..green;',
  31577. 'var',
  31578. ' e: TEnumRg = red;',
  31579. 'procedure DoIt(p: TEnumRg);',
  31580. 'begin',
  31581. ' e:=p;',
  31582. ' p:=TEnumRg(0);',
  31583. ' p:=succ(e);',
  31584. 'end;',
  31585. '{$R-}',
  31586. 'procedure DoSome;',
  31587. 'begin',
  31588. ' DoIt(e);',
  31589. ' e:=TEnum(1);',
  31590. ' e:=pred(e);',
  31591. 'end;',
  31592. 'begin',
  31593. '{$R+}',
  31594. '']);
  31595. ConvertProgram;
  31596. CheckSource('TestRangeChecks_AssignEnumRange',
  31597. LinesToStr([ // statements
  31598. 'this.TEnum = {',
  31599. ' "0": "red",',
  31600. ' red: 0,',
  31601. ' "1": "green",',
  31602. ' green: 1',
  31603. '};',
  31604. 'this.e = this.TEnum.red;',
  31605. 'this.DoIt = function (p) {',
  31606. ' rtl.rc(p, 0, 1);',
  31607. ' $mod.e = rtl.rc(p, 0, 1);',
  31608. ' p = 0;',
  31609. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31610. '};',
  31611. 'this.DoSome = function () {',
  31612. ' $mod.DoIt($mod.e);',
  31613. ' $mod.e = 1;',
  31614. ' $mod.e = $mod.e - 1;',
  31615. '};',
  31616. '']),
  31617. LinesToStr([ // $mod.$main
  31618. '']));
  31619. end;
  31620. procedure TTestModule.TestRangeChecks_AssignChar;
  31621. begin
  31622. StartProgram(false);
  31623. Add([
  31624. '{$R+}',
  31625. 'type',
  31626. ' TLetter = char;',
  31627. 'var',
  31628. ' b: TLetter = ''2'';',
  31629. ' w: TLetter = ''3'';',
  31630. 'procedure DoIt(p: TLetter);',
  31631. 'begin',
  31632. ' b:=w;',
  31633. ' b:=''1'';',
  31634. 'end;',
  31635. '{$R-}',
  31636. 'procedure DoSome;',
  31637. 'begin',
  31638. ' DoIt(w);',
  31639. ' b:=w;',
  31640. ' b:=''2'';',
  31641. 'end;',
  31642. 'begin',
  31643. '{$R+}',
  31644. '']);
  31645. ConvertProgram;
  31646. CheckSource('TestRangeChecks_AssignChar',
  31647. LinesToStr([ // statements
  31648. 'this.b = "2";',
  31649. 'this.w = "3";',
  31650. 'this.DoIt = function (p) {',
  31651. ' rtl.rcc(p, 0, 65535);',
  31652. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31653. ' $mod.b = "1";',
  31654. '};',
  31655. 'this.DoSome = function () {',
  31656. ' $mod.DoIt($mod.w);',
  31657. ' $mod.b = $mod.w;',
  31658. ' $mod.b = "2";',
  31659. '};',
  31660. '']),
  31661. LinesToStr([ // $mod.$main
  31662. '']));
  31663. end;
  31664. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31665. begin
  31666. StartProgram(false);
  31667. Add([
  31668. '{$R+}',
  31669. 'type TDigit = ''0''..''9'';',
  31670. 'var',
  31671. ' b: TDigit = ''2'';',
  31672. ' w: TDigit = ''3'';',
  31673. 'procedure DoIt(p: TDigit);',
  31674. 'begin',
  31675. ' b:=w;',
  31676. ' b:=''1'';',
  31677. 'end;',
  31678. '{$R-}',
  31679. 'procedure DoSome;',
  31680. 'begin',
  31681. ' DoIt(w);',
  31682. ' b:=w;',
  31683. ' b:=''2'';',
  31684. 'end;',
  31685. 'begin',
  31686. '{$R+}',
  31687. '']);
  31688. ConvertProgram;
  31689. CheckSource('TestRangeChecks_AssignCharRange',
  31690. LinesToStr([ // statements
  31691. 'this.b = "2";',
  31692. 'this.w = "3";',
  31693. 'this.DoIt = function (p) {',
  31694. ' rtl.rcc(p, 48, 57);',
  31695. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31696. ' $mod.b = "1";',
  31697. '};',
  31698. 'this.DoSome = function () {',
  31699. ' $mod.DoIt($mod.w);',
  31700. ' $mod.b = $mod.w;',
  31701. ' $mod.b = "2";',
  31702. '};',
  31703. '']),
  31704. LinesToStr([ // $mod.$main
  31705. '']));
  31706. end;
  31707. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31708. begin
  31709. StartProgram(false);
  31710. Add([
  31711. '{$R+}',
  31712. 'type',
  31713. ' Ten = 1..10;',
  31714. ' TArr = array of Ten;',
  31715. ' TArrArr = array of TArr;',
  31716. ' TArrByte = array[byte] of Ten;',
  31717. ' TArrChar = array[''0''..''9''] of Ten;',
  31718. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31719. ' TObject = class',
  31720. ' A: TArr;',
  31721. ' end;',
  31722. 'procedure DoIt;',
  31723. 'var',
  31724. ' Arr: TArr;',
  31725. ' ArrArr: TArrArr;',
  31726. ' ArrByte: TArrByte;',
  31727. ' ArrChar: TArrChar;',
  31728. ' ArrByteChar: TArrByteChar;',
  31729. ' i: Ten;',
  31730. ' c: char;',
  31731. ' o: tobject;',
  31732. 'begin',
  31733. ' i:=Arr[1];',
  31734. ' i:=ArrByteChar[1,''2''];',
  31735. ' Arr[1]:=Arr[1];',
  31736. ' Arr[i]:=Arr[i];',
  31737. ' ArrByte[3]:=ArrByte[3];',
  31738. ' ArrByte[i]:=ArrByte[i];',
  31739. ' ArrChar[''5'']:=ArrChar[''5''];',
  31740. ' ArrChar[c]:=ArrChar[c];',
  31741. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31742. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31743. ' o.a[i]:=o.a[i];',
  31744. 'end;',
  31745. 'begin',
  31746. '']);
  31747. ConvertProgram;
  31748. CheckSource('TestRangeChecks_ArrayIndex',
  31749. LinesToStr([ // statements
  31750. 'rtl.createClass(this, "TObject", null, function () {',
  31751. ' this.$init = function () {',
  31752. ' this.A = [];',
  31753. ' };',
  31754. ' this.$final = function () {',
  31755. ' this.A = undefined;',
  31756. ' };',
  31757. '});',
  31758. 'this.DoIt = function () {',
  31759. ' var Arr = [];',
  31760. ' var ArrArr = [];',
  31761. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31762. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31763. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31764. ' var i = 0;',
  31765. ' var c = "";',
  31766. ' var o = null;',
  31767. ' i = rtl.rc(Arr[1], 1, 10);',
  31768. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31769. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31770. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31771. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31772. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31773. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31774. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31775. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31776. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31777. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31778. '};',
  31779. '']),
  31780. LinesToStr([ // $mod.$main
  31781. '']));
  31782. end;
  31783. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31784. begin
  31785. StartProgram(false);
  31786. Add([
  31787. '{$R+}',
  31788. 'type',
  31789. ' Ten = 1..10;',
  31790. ' TRec = record x: Ten end;',
  31791. ' TArr = array of TRec;',
  31792. ' TArrArr = array of TArr;',
  31793. ' TObject = class',
  31794. ' A: TArr;',
  31795. ' end;',
  31796. 'procedure DoIt;',
  31797. 'var',
  31798. ' Arr: TArr;',
  31799. ' ArrArr: TArrArr;',
  31800. ' i: Ten;',
  31801. ' o: tobject;',
  31802. 'begin',
  31803. ' Arr[1]:=Arr[1];',
  31804. ' Arr[i]:=Arr[i+1];',
  31805. ' o.a[i]:=o.a[i+2];',
  31806. 'end;',
  31807. 'begin',
  31808. '']);
  31809. ConvertProgram;
  31810. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31811. LinesToStr([ // statements
  31812. 'rtl.recNewT(this, "TRec", function () {',
  31813. ' this.x = 0;',
  31814. ' this.$eq = function (b) {',
  31815. ' return this.x === b.x;',
  31816. ' };',
  31817. ' this.$assign = function (s) {',
  31818. ' this.x = s.x;',
  31819. ' return this;',
  31820. ' };',
  31821. '});',
  31822. 'rtl.createClass(this, "TObject", null, function () {',
  31823. ' this.$init = function () {',
  31824. ' this.A = [];',
  31825. ' };',
  31826. ' this.$final = function () {',
  31827. ' this.A = undefined;',
  31828. ' };',
  31829. '});',
  31830. 'this.DoIt = function () {',
  31831. ' var Arr = [];',
  31832. ' var ArrArr = [];',
  31833. ' var i = 0;',
  31834. ' var o = null;',
  31835. ' Arr[1].$assign(Arr[1]);',
  31836. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31837. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31838. '};',
  31839. '']),
  31840. LinesToStr([ // $mod.$main
  31841. '']));
  31842. end;
  31843. procedure TTestModule.TestRangeChecks_StringIndex;
  31844. begin
  31845. StartProgram(false);
  31846. Add([
  31847. 'type',
  31848. ' TObject = class',
  31849. ' S: string;',
  31850. ' end;',
  31851. '{$R+}',
  31852. 'procedure DoIt(var h: string);',
  31853. 'var',
  31854. ' s: string;',
  31855. ' i: longint;',
  31856. ' c: char;',
  31857. ' o: tobject;',
  31858. 'begin',
  31859. ' c:=s[1];',
  31860. ' s[i]:=s[i];',
  31861. ' h[i]:=h[i];',
  31862. ' c:=o.s[i];',
  31863. ' o.s[i]:=c;',
  31864. 'end;',
  31865. 'begin',
  31866. '']);
  31867. ConvertProgram;
  31868. CheckSource('TestRangeChecks_StringIndex',
  31869. LinesToStr([ // statements
  31870. 'rtl.createClass(this, "TObject", null, function () {',
  31871. ' this.$init = function () {',
  31872. ' this.S = "";',
  31873. ' };',
  31874. ' this.$final = function () {',
  31875. ' };',
  31876. '});',
  31877. 'this.DoIt = function (h) {',
  31878. ' var s = "";',
  31879. ' var i = 0;',
  31880. ' var c = "";',
  31881. ' var o = null;',
  31882. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31883. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31884. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31885. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31886. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31887. '};',
  31888. '']),
  31889. LinesToStr([ // $mod.$main
  31890. '']));
  31891. end;
  31892. procedure TTestModule.TestRangeChecks_TypecastInt;
  31893. begin
  31894. StartProgram(false);
  31895. Add([
  31896. '{$R+}',
  31897. 'var',
  31898. ' i: nativeint;',
  31899. ' b: byte;',
  31900. ' sh: shortint;',
  31901. ' w: word;',
  31902. ' sm: smallint;',
  31903. ' lw: longword;',
  31904. ' li: longint;',
  31905. 'begin',
  31906. ' b:=12+byte(i);',
  31907. ' sh:=12+shortint(i);',
  31908. ' w:=12+word(i);',
  31909. ' sm:=12+smallint(i);',
  31910. ' lw:=12+longword(i);',
  31911. ' li:=12+longint(i);',
  31912. '']);
  31913. ConvertProgram;
  31914. CheckSource('TestRangeChecks_TypecastInt',
  31915. LinesToStr([
  31916. 'this.i = 0;',
  31917. 'this.b = 0;',
  31918. 'this.sh = 0;',
  31919. 'this.w = 0;',
  31920. 'this.sm = 0;',
  31921. 'this.lw = 0;',
  31922. 'this.li = 0;',
  31923. '']),
  31924. LinesToStr([
  31925. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31926. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31927. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31928. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31929. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31930. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31931. '']));
  31932. end;
  31933. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31934. begin
  31935. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31936. StartProgram(false);
  31937. Add([
  31938. '{$modeswitch typehelpers}',
  31939. '{$R+}',
  31940. 'type',
  31941. ' TObject = class',
  31942. ' FSize: byte;',
  31943. ' property Size: byte read FSize;',
  31944. ' end;',
  31945. ' THelper = type helper for byte',
  31946. ' procedure SetIt(w: word);',
  31947. ' end;',
  31948. 'procedure THelper.SetIt(w: word);',
  31949. 'begin',
  31950. ' Self:=w;',
  31951. 'end;',
  31952. 'function GetIt: byte;',
  31953. 'begin',
  31954. ' Result.SetIt(2);',
  31955. 'end;',
  31956. 'var',
  31957. ' b: byte = 3;',
  31958. ' o: TObject;',
  31959. 'begin',
  31960. ' b.SetIt(14);',
  31961. ' with b do SetIt(15);',
  31962. ' o.Size.SetIt(16);',
  31963. '']);
  31964. ConvertProgram;
  31965. CheckSource('TestRangeChecks_AssignInt',
  31966. LinesToStr([ // statements
  31967. 'rtl.createClass(this, "TObject", null, function () {',
  31968. ' this.$init = function () {',
  31969. ' this.FSize = 0;',
  31970. ' };',
  31971. ' this.$final = function () {',
  31972. ' };',
  31973. '});',
  31974. 'rtl.createHelper(this, "THelper", null, function () {',
  31975. ' this.SetIt = function (w) {',
  31976. ' rtl.rc(w, 0, 65535);',
  31977. ' this.set(w);',
  31978. ' };',
  31979. '});',
  31980. 'this.GetIt = function () {',
  31981. ' var Result = 0;',
  31982. ' $mod.THelper.SetIt.call({',
  31983. ' get: function () {',
  31984. ' return Result;',
  31985. ' },',
  31986. ' set: function (v) {',
  31987. ' rtl.rc(v, 0, 255);',
  31988. ' Result = v;',
  31989. ' }',
  31990. ' }, 2);',
  31991. ' return Result;',
  31992. '};',
  31993. 'this.b = 3;',
  31994. 'this.o = null;',
  31995. '']),
  31996. LinesToStr([ // $mod.$main
  31997. '$mod.THelper.SetIt.call({',
  31998. ' p: $mod,',
  31999. ' get: function () {',
  32000. ' return this.p.b;',
  32001. ' },',
  32002. ' set: function (v) {',
  32003. ' rtl.rc(v, 0, 255);',
  32004. ' this.p.b = v;',
  32005. ' }',
  32006. '}, 14);',
  32007. 'var $with = $mod.b;',
  32008. '$mod.THelper.SetIt.call({',
  32009. ' get: function () {',
  32010. ' return $with;',
  32011. ' },',
  32012. ' set: function (v) {',
  32013. ' rtl.rc(v, 0, 255);',
  32014. ' $with = v;',
  32015. ' }',
  32016. '}, 15);',
  32017. '$mod.THelper.SetIt.call({',
  32018. ' p: $mod.o,',
  32019. ' get: function () {',
  32020. ' return this.p.FSize;',
  32021. ' },',
  32022. ' set: function (v) {',
  32023. ' rtl.rc(v, 0, 255);',
  32024. ' this.p.FSize = v;',
  32025. ' }',
  32026. '}, 16);',
  32027. '']));
  32028. end;
  32029. procedure TTestModule.TestAsync_Proc;
  32030. begin
  32031. StartProgram(false);
  32032. Add([
  32033. 'procedure Fly(w: word = 1); async; forward;',
  32034. 'procedure Run(w: word = 2); async;',
  32035. 'begin',
  32036. ' Fly(w);',
  32037. ' Fly;',
  32038. ' await(Fly(w));',
  32039. ' await(Fly);',
  32040. 'end;',
  32041. 'procedure Fly(w: word); ',
  32042. 'begin',
  32043. 'end;',
  32044. 'begin',
  32045. ' Run;',
  32046. ' Run(3);',
  32047. '']);
  32048. CheckResolverUnexpectedHints();
  32049. ConvertProgram;
  32050. CheckSource('TestAsync_Proc',
  32051. LinesToStr([ // statements
  32052. 'this.Run = async function (w) {',
  32053. ' $mod.Fly(w);',
  32054. ' $mod.Fly(1);',
  32055. ' await $mod.Fly(w);',
  32056. ' await $mod.Fly(1);',
  32057. '};',
  32058. 'this.Fly = async function (w) {',
  32059. '};',
  32060. '']),
  32061. LinesToStr([
  32062. '$mod.Run(2);',
  32063. '$mod.Run(3);',
  32064. '']));
  32065. end;
  32066. procedure TTestModule.TestAsync_CallResultIsPromise;
  32067. begin
  32068. StartProgram(false);
  32069. Add([
  32070. '{$modeswitch externalclass}',
  32071. 'type',
  32072. ' TObject = class',
  32073. ' end;',
  32074. ' TJSPromise = class external name ''Promise''',
  32075. ' end;',
  32076. ' TBird = class',
  32077. ' function Fly: word; async; ',
  32078. ' end;',
  32079. 'function TBird.Fly: word; async; ',
  32080. 'begin',
  32081. ' Result:=3;',
  32082. ' Fly:=4+Result;',
  32083. ' if Result=5 then ;',
  32084. ' exit(6);',
  32085. 'end;',
  32086. 'function Run: word; async;',
  32087. 'begin',
  32088. ' Result:=11+Result;',
  32089. ' inc(Result);',
  32090. 'end;',
  32091. 'var',
  32092. ' p: TJSPromise;',
  32093. ' o: TBird;',
  32094. 'begin',
  32095. ' p:=Run;',
  32096. ' p:=Run();',
  32097. ' if Run=p then ;',
  32098. ' if p=Run then ;',
  32099. ' if Run()=p then ;',
  32100. ' if p=Run() then ;',
  32101. ' p:=o.Fly;',
  32102. ' p:=o.Fly();',
  32103. ' if o.Fly=p then ;',
  32104. ' if o.Fly()=p then ;',
  32105. ' with o do begin',
  32106. ' p:=Fly;',
  32107. ' p:=Fly();',
  32108. ' if Fly=p then ;',
  32109. ' if Fly()=p then ;',
  32110. ' end;',
  32111. '']);
  32112. CheckResolverUnexpectedHints();
  32113. ConvertProgram;
  32114. CheckSource('TestAsync_CallResultIsPromise',
  32115. LinesToStr([ // statements
  32116. 'rtl.createClass(this, "TObject", null, function () {',
  32117. ' this.$init = function () {',
  32118. ' };',
  32119. ' this.$final = function () {',
  32120. ' };',
  32121. '});',
  32122. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32123. ' this.Fly = async function () {',
  32124. ' var Result = 0;',
  32125. ' Result = 3;',
  32126. ' Result = 4 + Result;',
  32127. ' if (Result === 5) ;',
  32128. ' return 6;',
  32129. ' return Result;',
  32130. ' };',
  32131. '});',
  32132. 'this.Run = async function () {',
  32133. ' var Result = 0;',
  32134. ' Result = 11 + Result;',
  32135. ' Result += 1;',
  32136. ' return Result;',
  32137. '};',
  32138. 'this.p = null;',
  32139. 'this.o = null;',
  32140. '']),
  32141. LinesToStr([
  32142. '$mod.p = $mod.Run();',
  32143. '$mod.p = $mod.Run();',
  32144. 'if ($mod.Run() === $mod.p) ;',
  32145. 'if ($mod.p === $mod.Run()) ;',
  32146. 'if ($mod.Run() === $mod.p) ;',
  32147. 'if ($mod.p === $mod.Run()) ;',
  32148. '$mod.p = $mod.o.Fly();',
  32149. '$mod.p = $mod.o.Fly();',
  32150. 'if ($mod.o.Fly() === $mod.p) ;',
  32151. 'if ($mod.o.Fly() === $mod.p) ;',
  32152. 'var $with = $mod.o;',
  32153. '$mod.p = $with.Fly();',
  32154. '$mod.p = $with.Fly();',
  32155. 'if ($with.Fly() === $mod.p) ;',
  32156. 'if ($with.Fly() === $mod.p) ;',
  32157. '']));
  32158. end;
  32159. procedure TTestModule.TestAsync_ConstructorFail;
  32160. begin
  32161. StartProgram(false);
  32162. Add([
  32163. 'type',
  32164. ' TObject = class',
  32165. ' end;',
  32166. ' TBird = class',
  32167. ' constructor Create; async;',
  32168. ' end;',
  32169. 'constructor TBird.Create; async;',
  32170. 'begin',
  32171. 'end;',
  32172. 'begin',
  32173. '']);
  32174. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32175. ConvertProgram;
  32176. end;
  32177. procedure TTestModule.TestAsync_PropertyGetterFail;
  32178. begin
  32179. StartProgram(false);
  32180. Add([
  32181. 'type',
  32182. ' TObject = class',
  32183. ' end;',
  32184. ' TBird = class',
  32185. ' function GetSize: word; async;',
  32186. ' property Size: word read GetSize;',
  32187. ' end;',
  32188. 'function TBird.GetSize: word; async;',
  32189. 'begin',
  32190. 'end;',
  32191. 'begin',
  32192. '']);
  32193. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32194. ConvertProgram;
  32195. end;
  32196. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32197. begin
  32198. StartProgram(false);
  32199. Add([
  32200. 'procedure Run; async;',
  32201. 'begin',
  32202. ' await(word,1);',
  32203. 'end;',
  32204. 'begin',
  32205. '']);
  32206. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32207. ConvertProgram;
  32208. end;
  32209. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32210. begin
  32211. StartProgram(false);
  32212. Add([
  32213. 'type',
  32214. ' TObject = class',
  32215. ' end;',
  32216. ' TBird = class',
  32217. ' end;',
  32218. 'function Fly: TObject; async;',
  32219. 'begin',
  32220. 'end;',
  32221. 'procedure Run; async;',
  32222. 'begin',
  32223. ' await(TBird,Fly);',
  32224. 'end;',
  32225. 'begin',
  32226. '']);
  32227. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32228. ConvertProgram;
  32229. end;
  32230. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32231. begin
  32232. StartProgram(false);
  32233. Add([
  32234. 'procedure Crawl(w: double); ',
  32235. 'begin',
  32236. 'end;',
  32237. 'procedure Run(w: double);',
  32238. 'begin',
  32239. ' await(Crawl(w));',
  32240. 'end;',
  32241. 'begin',
  32242. ' Run(1);']);
  32243. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32244. ConvertProgram;
  32245. end;
  32246. procedure TTestModule.TestAWait_IntegerFail;
  32247. begin
  32248. StartProgram(false);
  32249. Add([
  32250. 'function Run: word;',
  32251. 'begin',
  32252. 'end;',
  32253. 'procedure Fly(w: word); async;',
  32254. 'begin',
  32255. ' await(Run());',
  32256. 'end;',
  32257. 'begin',
  32258. ' Fly(1);']);
  32259. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32260. ConvertProgram;
  32261. end;
  32262. procedure TTestModule.TestAWait_ExternalClassPromise;
  32263. begin
  32264. StartProgram(false);
  32265. Add([
  32266. '{$modeswitch externalclass}',
  32267. 'type',
  32268. ' TJSPromise = class external name ''Promise''',
  32269. ' end;',
  32270. ' TJSThenable = class external name ''Thenable''',
  32271. ' end;',
  32272. 'function Fly(w: word): TJSPromise;',
  32273. 'begin',
  32274. 'end;',
  32275. 'function Jump(w: word): word; async;',
  32276. 'begin',
  32277. 'end;',
  32278. 'function Eat(w: word): TJSPromise; async;',
  32279. 'begin',
  32280. 'end;',
  32281. 'function Run(d: double): word; async;',
  32282. 'var',
  32283. ' p: TJSPromise;',
  32284. 'begin',
  32285. ' Result:=await(word,p);', // promise needs type
  32286. ' Result:=await(word,Fly(3));', // promise needs type
  32287. ' Result:=await(Jump(4));', // async non promise must omit the type
  32288. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32289. ' Result:=await(word,Eat(6));', // promise needs type
  32290. 'end;',
  32291. 'begin',
  32292. '']);
  32293. ConvertProgram;
  32294. CheckSource('TestAWait_ExternalClassPromise',
  32295. LinesToStr([ // statements
  32296. 'this.Fly = function (w) {',
  32297. ' var Result = null;',
  32298. ' return Result;',
  32299. '};',
  32300. 'this.Jump = async function (w) {',
  32301. ' var Result = 0;',
  32302. ' return Result;',
  32303. '};',
  32304. 'this.Eat = async function (w) {',
  32305. ' var Result = null;',
  32306. ' return Result;',
  32307. '};',
  32308. 'this.Run = async function (d) {',
  32309. ' var Result = 0;',
  32310. ' var p = null;',
  32311. ' Result = await p;',
  32312. ' Result = await $mod.Fly(3);',
  32313. ' Result = await $mod.Jump(4);',
  32314. ' Result = await $mod.Jump(5);',
  32315. ' Result = await $mod.Eat(6);',
  32316. ' return Result;',
  32317. '};',
  32318. '']),
  32319. LinesToStr([
  32320. ]));
  32321. CheckResolverUnexpectedHints();
  32322. end;
  32323. procedure TTestModule.TestAWait_JSValue;
  32324. begin
  32325. StartProgram(false);
  32326. Add([
  32327. '{$modeswitch externalclass}',
  32328. 'type',
  32329. ' TJSPromise = class external name ''Promise''',
  32330. ' end;',
  32331. 'function Fly(w: word): jsvalue; async;',
  32332. 'begin',
  32333. 'end;',
  32334. 'function Run(d: jsvalue; var e): word; async;',
  32335. 'begin',
  32336. ' Result:=await(word,d);', // promise needs type
  32337. ' d:=await(Fly(4));', // async non promise must omit the type
  32338. ' Result:=await(word,e);', // promise needs type
  32339. 'end;',
  32340. 'begin',
  32341. '']);
  32342. ConvertProgram;
  32343. CheckSource('TestAWait_JSValue',
  32344. LinesToStr([ // statements
  32345. 'this.Fly = async function (w) {',
  32346. ' var Result = undefined;',
  32347. ' return Result;',
  32348. '};',
  32349. 'this.Run = async function (d, e) {',
  32350. ' var Result = 0;',
  32351. ' Result = await d;',
  32352. ' d = await $mod.Fly(4);',
  32353. ' Result = await e.get();',
  32354. ' return Result;',
  32355. '};',
  32356. '']),
  32357. LinesToStr([
  32358. ]));
  32359. CheckResolverUnexpectedHints();
  32360. end;
  32361. procedure TTestModule.TestAWait_Result;
  32362. begin
  32363. StartProgram(false);
  32364. Add([
  32365. '{$modeswitch externalclass}',
  32366. 'type',
  32367. ' TJSPromise = class external name ''Promise''',
  32368. ' end;',
  32369. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32370. 'begin',
  32371. 'end;',
  32372. 'function Run(d: double = 1.6): word; async;',
  32373. 'begin',
  32374. ' Result:=await(word,Crawl);',
  32375. ' Result:=await(word,Crawl(4.5));',
  32376. ' Result:=await(Run);',
  32377. ' Result:=await(Run(6.7));',
  32378. 'end;',
  32379. 'begin',
  32380. ' Run(1);']);
  32381. ConvertProgram;
  32382. CheckSource('TestAWait_Result',
  32383. LinesToStr([ // statements
  32384. 'this.Crawl = function (d) {',
  32385. ' var Result = null;',
  32386. ' return Result;',
  32387. '};',
  32388. 'this.Run = async function (d) {',
  32389. ' var Result = 0;',
  32390. ' Result = await $mod.Crawl(1.3);',
  32391. ' Result = await $mod.Crawl(4.5);',
  32392. ' Result = await $mod.Run(1.6);',
  32393. ' Result = await $mod.Run(6.7);',
  32394. ' return Result;',
  32395. '};',
  32396. '']),
  32397. LinesToStr([
  32398. '$mod.Run(1);'
  32399. ]));
  32400. CheckResolverUnexpectedHints();
  32401. end;
  32402. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32403. begin
  32404. StartProgram(false);
  32405. Add([
  32406. '{$mode objfpc}',
  32407. '{$modeswitch externalclass}',
  32408. 'type',
  32409. ' TJSPromise = class external name ''Promise''',
  32410. ' end;',
  32411. 'function Run: TJSPromise; async;',
  32412. 'begin',
  32413. 'end;',
  32414. 'procedure Fly(w: word); async;',
  32415. 'begin',
  32416. ' await(Run());',
  32417. 'end;',
  32418. 'begin',
  32419. ' Fly(1);']);
  32420. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32421. nWrongNumberOfParametersForCallTo);
  32422. ConvertProgram;
  32423. end;
  32424. procedure TTestModule.TestAsync_AnonymousProc;
  32425. begin
  32426. StartProgram(false);
  32427. Add([
  32428. '{$mode objfpc}',
  32429. '{$modeswitch externalclass}',
  32430. 'type',
  32431. ' TJSPromise = class external name ''Promise''',
  32432. ' end;',
  32433. 'type',
  32434. ' TFunc = reference to function(x: double): word; async;',
  32435. 'function Crawl(d: double = 1.3): word; async;',
  32436. 'begin',
  32437. 'end;',
  32438. 'var Func: TFunc;',
  32439. 'begin',
  32440. ' Func:=function(c:double):word async begin',
  32441. ' Result:=await(Crawl(c));',
  32442. ' end;',
  32443. ' Func:=function(c:double):word async assembler asm',
  32444. ' end;',
  32445. ' ']);
  32446. ConvertProgram;
  32447. CheckSource('TestAsync_AnonymousProc',
  32448. LinesToStr([ // statements
  32449. 'this.Crawl = async function (d) {',
  32450. ' var Result = 0;',
  32451. ' return Result;',
  32452. '};',
  32453. 'this.Func = null;',
  32454. '']),
  32455. LinesToStr([
  32456. '$mod.Func = async function (c) {',
  32457. ' var Result = 0;',
  32458. ' Result = await $mod.Crawl(c);',
  32459. ' return Result;',
  32460. '};',
  32461. '$mod.Func = async function (c) {',
  32462. '};',
  32463. '']));
  32464. CheckResolverUnexpectedHints();
  32465. end;
  32466. procedure TTestModule.TestAsync_ProcType;
  32467. begin
  32468. StartProgram(false);
  32469. Add([
  32470. '{$mode objfpc}',
  32471. 'type',
  32472. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32473. ' TFunc = function(x: double = 1.1): word; async;',
  32474. ' TProc = procedure(x: longint = 7); async;',
  32475. 'function Crawl(d: double): word; async;',
  32476. 'begin',
  32477. 'end;',
  32478. 'procedure Run(e:longint); async;',
  32479. 'begin',
  32480. 'end;',
  32481. 'procedure Fly(p: TProc); async;',
  32482. 'begin',
  32483. ' await(p);',
  32484. ' await(p());',
  32485. 'end;',
  32486. 'var',
  32487. ' RefFunc: TRefFunc;',
  32488. ' Func: TFunc;',
  32489. ' Proc, ProcB: TProc;',
  32490. 'begin',
  32491. ' Func:=@Crawl;',
  32492. ' RefFunc:=@Crawl;',
  32493. ' RefFunc:=function(c:double):word async begin',
  32494. ' Result:=await(RefFunc);',
  32495. ' Result:=await(RefFunc());',
  32496. ' Result:=await(Func);',
  32497. ' Result:=await(Func());',
  32498. ' await(Proc);',
  32499. ' await(Proc());',
  32500. ' await(Proc(13));',
  32501. ' end;',
  32502. ' Proc:=@Run;',
  32503. ' if Proc=ProcB then ;',
  32504. ' ']);
  32505. ConvertProgram;
  32506. CheckResolverUnexpectedHints();
  32507. CheckSource('TestAsync_ProcType',
  32508. LinesToStr([ // statements
  32509. 'this.Crawl = async function (d) {',
  32510. ' var Result = 0;',
  32511. ' return Result;',
  32512. '};',
  32513. 'this.Run = async function (e) {',
  32514. '};',
  32515. 'this.Fly = async function (p) {',
  32516. ' await p(7);',
  32517. ' await p(7);',
  32518. '};',
  32519. 'this.RefFunc = null;',
  32520. 'this.Func = null;',
  32521. 'this.Proc = null;',
  32522. 'this.ProcB = null;',
  32523. '']),
  32524. LinesToStr([
  32525. '$mod.Func = $mod.Crawl;',
  32526. '$mod.RefFunc = $mod.Crawl;',
  32527. '$mod.RefFunc = async function (c) {',
  32528. ' var Result = 0;',
  32529. ' Result = await $mod.RefFunc(1.3);',
  32530. ' Result = await $mod.RefFunc(1.3);',
  32531. ' Result = await $mod.Func(1.1);',
  32532. ' Result = await $mod.Func(1.1);',
  32533. ' await $mod.Proc(7);',
  32534. ' await $mod.Proc(7);',
  32535. ' await $mod.Proc(13);',
  32536. ' return Result;',
  32537. '};',
  32538. '$mod.Proc = $mod.Run;',
  32539. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32540. '']));
  32541. end;
  32542. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32543. begin
  32544. StartProgram(false);
  32545. Add([
  32546. '{$mode objfpc}',
  32547. 'type',
  32548. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32549. 'function Crawl(d: double): word; async;',
  32550. 'begin',
  32551. 'end;',
  32552. 'var',
  32553. ' RefFunc: TRefFunc;',
  32554. 'begin',
  32555. ' RefFunc:=@Crawl;',
  32556. ' ']);
  32557. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32558. ConvertProgram;
  32559. end;
  32560. procedure TTestModule.TestAsync_Inherited;
  32561. begin
  32562. StartProgram(false);
  32563. Add([
  32564. '{$mode objfpc}',
  32565. '{$modeswitch externalclass}',
  32566. 'type',
  32567. ' TJSPromise = class external name ''Promise''',
  32568. ' end;',
  32569. ' TObject = class',
  32570. ' function Run(w: word = 3): word; async; virtual;',
  32571. ' end;',
  32572. ' TBird = class',
  32573. ' function Run(w: word = 3): word; async; override;',
  32574. ' end;',
  32575. 'function TObject.Run(w: word = 3): word; async;',
  32576. 'begin',
  32577. 'end;',
  32578. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32579. 'var p: TJSPromise;',
  32580. 'begin',
  32581. ' p:=inherited;',
  32582. ' p:=inherited Run;',
  32583. ' p:=inherited Run();',
  32584. ' p:=inherited Run(4);',
  32585. ' exit(p);',
  32586. ' exit(inherited);',
  32587. ' exit(inherited Run);',
  32588. ' exit(inherited Run(5));',
  32589. ' exit(6);',
  32590. 'end;',
  32591. 'begin',
  32592. ' ']);
  32593. ConvertProgram;
  32594. CheckSource('TestAsync_Inherited',
  32595. LinesToStr([ // statements
  32596. 'rtl.createClass(this, "TObject", null, function () {',
  32597. ' this.$init = function () {',
  32598. ' };',
  32599. ' this.$final = function () {',
  32600. ' };',
  32601. ' this.Run = async function (w) {',
  32602. ' var Result = 0;',
  32603. ' return Result;',
  32604. ' };',
  32605. '});',
  32606. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32607. ' this.Run = async function (w) {',
  32608. ' var Result = 0;',
  32609. ' var p = null;',
  32610. ' p = $mod.TObject.Run.apply(this, arguments);',
  32611. ' p = $mod.TObject.Run.call(this, 3);',
  32612. ' p = $mod.TObject.Run.call(this, 3);',
  32613. ' p = $mod.TObject.Run.call(this, 4);',
  32614. ' return p;',
  32615. ' return $mod.TObject.Run.apply(this, arguments);',
  32616. ' return $mod.TObject.Run.call(this, 3);',
  32617. ' return $mod.TObject.Run.call(this, 5);',
  32618. ' return 6;',
  32619. ' return Result;',
  32620. ' };',
  32621. '});',
  32622. '']),
  32623. LinesToStr([
  32624. '']));
  32625. CheckResolverUnexpectedHints();
  32626. end;
  32627. procedure TTestModule.TestAsync_ClassInterface;
  32628. begin
  32629. StartProgram(false);
  32630. Add([
  32631. '{$mode objfpc}',
  32632. '{$modeswitch externalclass}',
  32633. 'type',
  32634. ' TJSPromise = class external name ''Promise''',
  32635. ' end;',
  32636. ' IUnknown = interface',
  32637. ' function _AddRef: longint;',
  32638. ' function _Release: longint;',
  32639. ' end;',
  32640. 'function Say(i: IUnknown): IUnknown; async;',
  32641. 'begin',
  32642. 'end;',
  32643. 'function Run: IUnknown; async;',
  32644. 'begin',
  32645. ' Result:=await(Run);',
  32646. ' Result:=await(Run());',
  32647. ' Result:=await(Run) as IUnknown;',
  32648. ' Result:=await(Say(nil));',
  32649. ' Result:=await(Say(await(Run())));',
  32650. ' Result:=await(Say(await(Run()) as IUnknown));',
  32651. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32652. 'end;',
  32653. 'procedure Fly;',
  32654. 'var p: TJSPromise;',
  32655. 'begin',
  32656. ' Run;',
  32657. ' Run();',
  32658. ' p:=Run;',
  32659. ' p:=Run();',
  32660. 'end;',
  32661. 'begin',
  32662. ' ']);
  32663. ConvertProgram;
  32664. CheckSource('TestAsync_ClassInterface',
  32665. LinesToStr([ // statements
  32666. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32667. 'this.Say = async function (i) {',
  32668. ' var Result = null;',
  32669. ' return Result;',
  32670. '};',
  32671. 'this.Run = async function () {',
  32672. ' var Result = null;',
  32673. ' var $ok = false;',
  32674. ' try {',
  32675. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32676. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32677. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32678. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32679. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32680. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32681. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32682. ' $ok = true;',
  32683. ' } finally {',
  32684. ' if (!$ok) rtl._Release(Result);',
  32685. ' };',
  32686. ' return Result;',
  32687. '};',
  32688. 'this.Fly = function () {',
  32689. ' var p = null;',
  32690. ' $mod.Run();',
  32691. ' $mod.Run();',
  32692. ' p = $mod.Run();',
  32693. ' p = $mod.Run();',
  32694. '};',
  32695. '']),
  32696. LinesToStr([
  32697. '']));
  32698. CheckResolverUnexpectedHints();
  32699. end;
  32700. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32701. begin
  32702. StartProgram(true,[supTInterfacedObject]);
  32703. Add([
  32704. '{$mode objfpc}',
  32705. '{$modeswitch externalclass}',
  32706. 'type',
  32707. ' TJSPromise = class external name ''Promise''',
  32708. ' end;',
  32709. ' IBird = interface',
  32710. ' procedure Run;',
  32711. ' end;',
  32712. ' TBird = class(TInterfacedObject,IBird)',
  32713. ' procedure Run; async;',
  32714. ' end;',
  32715. 'procedure TBird.Run;',
  32716. 'begin',
  32717. 'end;',
  32718. 'begin',
  32719. ' ']);
  32720. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32721. ConvertProgram;
  32722. end;
  32723. procedure TTestModule.TestLibrary_Empty;
  32724. begin
  32725. StartLibrary(false);
  32726. Add([
  32727. '']);
  32728. ConvertLibrary;
  32729. CheckSource('TestLibrary_Empty',
  32730. LinesToStr([ // statements
  32731. '']),
  32732. LinesToStr([
  32733. '']));
  32734. CheckResolverUnexpectedHints();
  32735. end;
  32736. procedure TTestModule.TestLibrary_ExportFunc;
  32737. begin
  32738. exit;
  32739. StartLibrary(false);
  32740. Add([
  32741. 'procedure Run(w: word);',
  32742. 'begin',
  32743. 'end;',
  32744. 'exports',
  32745. ' Run,',
  32746. ' run name ''Foo'';',
  32747. '']);
  32748. ConvertLibrary;
  32749. CheckSource('TestLibrary_ExportFunc',
  32750. LinesToStr([ // statements
  32751. '']),
  32752. LinesToStr([
  32753. '']));
  32754. CheckResolverUnexpectedHints();
  32755. end;
  32756. Initialization
  32757. RegisterTests([TTestModule]);
  32758. end.